pythoninterpolationnetcdfpython-xarraynetcdf4

Regrid Netcdf file in python


I'm trying to regrid a NetCDF file from 0.125 degrees to 0.083-degree spatial scale. The netcdf contains 224 latitudes and 464 longitudes and it has daily data for one year.

I tried xarray for it but it produces this memory error: MemoryError: Unable to allocate 103. GiB for an array with shape (13858233841,) and data type float64

How can I regrid the file with python?


Solution

  • A Python option, using CDO as a backend, is my package nctoolkit: https://nctoolkit.readthedocs.io/en/latest/, instalable via pip (https://pypi.org/project/nctoolkit/)

    It has a built in method called to_latlon which will regrid to a specified latlon grid

    In your case, you would need to do:

    import nctoolkit as nc
    data = nc.open_data(infile)
    data.to_latlon(lon = [lon_min,lon_max],lat=[lat_min,lat_max], res =[0.083, 0.083])