Is there a netcdf operator (from nco or any python netcdf library) which can be used to overwrite specific cells in a netcdf file?
I want to change the values of a small region in a netcdf file containing global climate data. thanks!
Is there a netcdf operator (from nco or any python netcdf library) which can be used to overwrite specific cells in a netcdf file?
I want to change the values of a small region in a netcdf file containing global climate data. thanks!
Or NCO's ncap2 does this in one command with
ncap2 -s 'var[0,0,0]=-999' in.nc out.nc
ncap2 is described here
This is easy with netCDF4-python. For example, suppose nc
is your netCDF file, the variable is named var
, and the index of the cell you want to change is (0,0,0)
. Then:
from netCDF4 import Dataset
new_value = -999
nc.variables['var'][0,0,0] = new_value
netCDF4 represents all netCDF arrays using numpy, which enables their powerful manipulation using numpy's slicing and other capabilities.
CDO can set a specific lon-lat "rectangular" region to a value, here is the manual example:
To set all values in the region with the longitudes from 120E to 90W and latitudes from 20N to 20S to the constant value -999 use:
cdo setclonlatbox,-999,120,-90,20,-20 infile outfile