Question

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!

Was it helpful?

Solution 2

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.

OTHER TIPS

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

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 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top