简体   繁体   中英

Problems with NetCDF regridding, python

Hello, everyone, this is my first question here ^^

I need to regreed some NetCDF files, so I've tried to do it with cdo and cf modules, but everithing failed. Maybe someone knows some other modules for this purposes or can help me to get rid of my errors. First, I've tried cf:

df = cf.read('my_file.nc')[0]

#define new grid
lat1 = cf.DimensionCoordinate(data=cf.Data(np.arange(-90, 90.01, 0.1), 'degreesN'))
lon1 = cf.DimensionCoordinate(data=cf.Data(np.arange(-180, 180.01, 0.1), 'degreesE'))

#regrid
g = df.regrids({'lat': lat1, 'lon': lon1}, method='linear')

But it gave me: "RuntimeError: Regridding methods will not work unless the ESMF library is installed". And I've got no idea how to install it to the remote server where I'm working on.

Then I've tried cdo:

#define input and output files
infile='my_file.nc'
outfile='newgrid.nc' 

#file with needed grid parameters 
gridfile='gridfile.txt'

#bilinear regrid
cdo.remapbil('gridfile.txt', input=infile, output=outfile)

And it gave me: "... STDERR:Error (cdf_load_bounds) : Allocation of 134369280000 bytes failed. [ line 2048 file stream_cdf_i.c ] System error message : Cannot allocate memory"

My file is about 2 gb, how it comes 134 gb to be needed for this operation?

I've also tried xarray:

da_input = xarray.open_dataarray('my_file.nc') # the file the data will be loaded from
regrid_axis = np.arange(-90, 90, 0.1) # new coordinates
da_output = da_input.interp(latitude=regrid_axis) # specify calculation
da_output.to_netcdf('output.nc') # save direct to file

It worked, but I suppose that it does interpolation just on the cartesian plane and I need it to be on sphere.

Thank everyone in advance

You could try solving this with nctoolkit, which uses CDO as a backend:

import nctoolkit as nc
ds = nc.open_data("my_file.nc")
ds.to_latlon(lon = [-180, 180], lat = [-90, 90], res = 0.1)
ds.to_nc("output.nc")

If your file is 2 GB, then presumably you have made a mistake with the grid file in your CDO example. It shouldn't be trying to allocate that much RAM. nctoolkit will generate the appropriate CDO grid file and commands for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM