简体   繁体   中英

Create one 4D (model, time, lon, lat) netcdf file from multiple netcdf files using Python

I am downloading climate data in netcdf format. For each variable (eg 'precipitation'), I need to merge 9 netcdfs, each belonging to an unique climate model. Each netcdf has the same size (time, lat, lon). How can I merge 9 3D netcdfs into one 4D netcdf? Ultimately, I want to calculate cumulative precipitation per month. Here's my current code:

variables = ['pr']         
scenarios = ['historical', 'ssp245']        #options ['historical', 'ssp126', 'ssp245', 'ssp370', 'ssp585']
models = ['UKESM1-0-LL', 'MRI-ESM2-0', 'MIROC6', 'MIROC-ES2L', 'IPSL-CM6A-LR',
         'GFDL-ESM4', 'FGOALS-g3', 'CNRM-ESM2-1', 'CanESM5']


save_folder = processing_fn / 'local_climate_assessment' / f'{variable}' / 'output'
if not os.path.exists(save_folder):
    os.makedirs(save_folder)

netcdfs = []

# Create one netcdf per model by merging annual netcdfs
for variable in variables:
    for scenario in scenarios:
        for model in models:
    

            source = processing_fn / 'local_climate_assessment' / f'{variable}' / f'{scenario}' / f'{model}'
            netcdf_fn = save_folder / f'{variable}_{scenario}_{model}.nc'
            
            if not os.path.exists(netcdf_fn):
        
                gdf_model = xr.open_mfdataset(str(source / '*.nc'), combine = 'nested', concat_dim="time", use_cftime=True)
                # rename_dict = {variable, f'{variable}_{scenario}_{model}'}
                # gdf_model.rename(rename_dict, inplace = True)
                gdf_model.to_netcdf(netcdf_fn)
                print(gdf_model.attrs['cmip6_source_id'])
                netcdfs.append(gdf_model)
                
            else:
                gdf_model = xr.open_mfdataset(netcdf_fn)
                netcdfs.append(gdf_model)

# Create one netcdf per variable by merging models
ds = xr.combine_nested(netcdfs, concat_dim = "time")
print(ds)
Out[33]: 
<xarray.Dataset>
Dimensions:  (time: 246095, lat: 47, lon: 50)
Coordinates:
  * time     (time) object 1981-01-01 12:00:00 ... 2060-12-31 12:00:00
  * lat      (lat) float64 31.62 31.88 32.12 32.38 ... 42.38 42.62 42.88 43.12
  * lon      (lon) float64 234.6 234.9 235.1 235.4 ... 246.1 246.4 246.6 246.9
Data variables:
    pr       (time, lat, lon) float32 dask.array<chunksize=(360, 47, 50), meta=np.ndarray>

The above code works, but I'm creating one big 3D netcdf instead of a 4D still containing the climate model names. The code below results in the following error:

a = ds.resample(time = 'M').sum()
ValueError: index must be monotonic for resampling

How to create a 4D netcdf with model names included, and resample to create monthly sum values?

I'd definitely recommend reading the xarray docs on combining data .

The concat_dim argument to combine_nested can be a list of dimensions along which you want to concatenate your data. You seem to be concatenating over variable, scenario, and model, not time. So passing time here and providing a 1-D list of netCDFs is creating a repeating time series with no information about your concatenation dimensions.

Instead, explicitly nest the datasets:

netcdfs = []
for variable in variables:
    netcdfs.append([])
    for scenario in scenarios:
        netcdfs[-1].append([])
        for model in models:
            ... # prep & read in your data
            netcdfs[-1][-1].append(gdf_model)

# use nested lists of datasets and an ordered list
# of coordinates matching the list of datasets
ds = xr.combine_nested(
    netcdfs,
    concat_dim=[
        pd.Index(variables, name="variable"),
        pd.Index(scenarios, name="sceanrio"),
        pd.Index(models, name="model"),
    ],
)

Alternatively, expand the dimensionality of each dataset first, then concat using combine_by_coords :

netcdfs = []
for variable in variables:
    for scenario in scenarios:
        for model in models:
            ... # prep & read in your data
            # add coordinates
            gdf_model = gdf_model.expand_dims(
                variable=[variable],
                scenario=[scenario],
                model=[model],
            )

            netcdfs.append(gdf_model)

# auto-combine using your new coordinates
ds = xr.combine_by_coords(netcdfs)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM