steve
steve

Reputation: 531

Python - re-write a netcdf file after calculation

I have a netcdf4 file called test.nc

I am calculating monthly median (from the daily values) with the following code:

import xarray as xr
os.chdir(inbasedir)
data = xr.open_dataset('test.nc')
monthly_data = data.resample(freq='m', dim ='time', how = 'median')

My question is how can I write this output to a new netcdf file, without having to re-write all the variables and the metadata already included in the input netcdf file.

Upvotes: 1

Views: 374

Answers (1)

kmuehlbauer
kmuehlbauer

Reputation: 461

Not sure if it is what you want. But this creates a new netcdf file from the created Dataset:

monthly_data.to_netcdf('newfile.nc')

You might use .drop() on the Dataset to remove data which you don't want in the output.

Upvotes: 3

Related Questions