Add multiple variables with the same dimension to an existing nc file. Read the target nc file and include the new variables into the file and save to the new file.
Sample code
import xarray as xr #read nc file fn1 = 'Annual_average_temperature.nc' TxTn90p10p=xr.open_dataset(fn1) #add new variables TxTn90p10p['TX90p']=xr.DataArray(TX90p.astype(np.float32), coords=TxTn90p10p.coords, dims=TxTn90p10p.t2m.dims, attrs=TxTn90p10p.attrs) TxTn90p10p['TX10p']=xr.DataArray(TX10p.astype(np.float32), coords=TxTn90p10p.coords, dims=TxTn90p10p.t2m.dims, attrs=TxTn90p10p.attrs) TxTn90p10p['TN90p']=xr.DataArray(TN90p.astype(np.float32), coords=TxTn90p10p.coords, dims=TxTn90p10p.t2m.dims, attrs=TxTn90p10p.attrs) TxTn90p10p['TN10p']=xr.DataArray(TN10p.astype(np.float32), coords=TxTn90p10p.coords, dims=TxTn90p10p.t2m.dims, attrs=TxTn90p10p.attrs) #save update content to a new nc file TxTn90p10p.to_netcdf('TxTn90p10p.nc') #data structure
*My original code:Tmin_Tmax_indexes_calculation_ERA5_GreatLakesRegion_TX10pTX90pTN10pTN90p.ipynb