Tuesday, May 7, 2019

[Python] Edit netCDF lat/lon without changing the original data

When I created a large netCDF file, I made a mistake by writing the wrong longitude range (-180 to 180) to the netCDF, which is inconsistent with the organization of the data (0 to 360). I realized the sellatlonbox command in CDO will not solve this mismatch problem. So I decided to use Python netCDF4 package to modify it (python-replacing-values-in-netcdf-file-using-netcdf4). Generally speaking, you can modify and update any variable in a netCDF file using this package. And it is very quick!

Here is the code:
dataset = netCDF4.Dataset('file.nc','r+')
dataset.variables['lon'][:] = np.linspace(0.125, 360, 1440)
dataset.close() 

No comments:

Post a Comment