Search code examples
pythonaveragenetcdfweathercdo-climate

How to plot ensemble models mean?


#read data
dset = xr.open_mfdataset('/home/users/ruoou/projects/data/cmip6/scenaromip/ssp126/tas/*'+ mod+ '*.nc', parallel=True)

#define timescale
time = dset.time
clim_set_t = dset.sel(time=slice("2015-01-16", "2100-12-16"))
clim_ta = clim_set_t['tas'].groupby('time.month').mean('time', keep_attrs=True)
clim_ta_c = (clim_ta - 273.15)

# define areas
lat = dset.lat
lon = dset.lon
lon_range = lon[(lon>60) & (lon<150)]
lat_range = lat[(lat>10) & (lat<60)]


clim_ta_c.sel(lon=lon_range, lat=lat_range, month = 1).plot.contourf(ax=ax,
                                          levels=np.arange(-30, 30 + 1, 1),
                                          transform=ccrs.PlateCarree(),
                                          cmap='Spectral_r',
                                          cbar_kwargs={'orientation': 'vertical',     #colorbar
                                                   'label': 'temperature (℃)',
                                                   'ticks': np.arange(-30, 30 + 5, 5),
                                                   'pad': 0.05,
                                                   'shrink': 0.8})

model = dset.attrs['source_id']
title = f'{model} near-surface temperature climatology 2015-2100 (1)'
plt.title(title, fontsize = 13)

This is the main part of my script to plot BCC in ssp126 near-surface temperature climatology for January. Bcc in ssp126 What should I do to add up all models and plot the mean (like average the BCC in ssp126 and IPSL in ssp126)? Thanks for help in advance.


Solution

  • You tagged the question cdo-climate, so I presume you are also happy to have a cdo based solution, I added a remapping step, but check out remapping questions on stackoverflow, or my remapping video guide for more details.

    dir=/home/users/ruoou/projects/data/cmip6/scenaromip/ssp126/tas/
    # remap all files to a 1x1 deg grid 
    for file in $dir/*modelname*.nc ; do 
       cdo remapbil,r360x180 $file ${file%???}_remap.nc 
    done 
    # calculate ens mean
    cdo ensmean *_remap.nc ensmean.nc 
    

    then you can open ensmean.nc and plot it as you wish.