-
Notifications
You must be signed in to change notification settings - Fork 0
/
ensemble_mean-hmxl.py
executable file
·41 lines (31 loc) · 1.23 KB
/
ensemble_mean-hmxl.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/usr/bin/env python3
# inagler 11/09/24
import xarray as xr
import numpy as np
import os
import glob
var = 'hmxl'
input_base_directory = '/Data/gfi/share/ModData/CESM2_LENS2/ocean/monthly/'
output_directory = '/Data/gfi/share/ModData/CESM2_LENS2/ocean/monthly/comp/ensemble_mean/'
chunk_size = 10 # Adjust chunk size based on memory constraints
os.makedirs(output_directory, exist_ok=True)
def process_variable(var):
input_directory = os.path.join(input_base_directory, var)
file_pattern = os.path.join(input_directory, f'{var}_*.nc')
files = glob.glob(file_pattern)
sum_ds = None
count = 0
for i in range(0, len(files), chunk_size):
subset_files = files[i:i+chunk_size]
subset_ds = xr.concat([xr.open_dataset(file) for file in subset_files], dim='ensemble')
if sum_ds is None:
sum_ds = subset_ds.sum(dim='ensemble')
else:
sum_ds += subset_ds.sum(dim='ensemble')
count += subset_ds.sizes['ensemble']
subset_ds.close()
ensemble_mean = sum_ds / count
output_file = os.path.join(output_directory, f'ensemble_mean_{var}.nc')
ensemble_mean.to_netcdf(output_file)
print(f"Ensemble mean for {var} saved")
process_variable(var)