Interpolation & regridding

Starting point

The examples below regrid global sea-surface temperature from NOAA's COBE-SST 2, using NCToolkit's built-in horizontal and vertical interpolation methods. We'll speed things up by only interpolating the first timestep of data.

python
ds = nc.open_thredds("https://psl.noaa.gov/thredds/dodsC/Datasets/COBE2/sst.mon.mean.nc")
ds.subset(time=0)
ds.plot()
ds.plot() — interactive output Open interactive
Global sea surface temperature, first time step

Regular lon/lat grids

to_latlon regrids to a regular grid: give the extent (lon, lat) and resolution (res):

python
ds.to_latlon(lon=[-79.5, 79.5], lat=[0.75, 89.75], res=[1, 0.5])
ds.plot() — interactive output Open interactive
Regridded to a regular 1 x 0.5 degree lon/lat grid

Another dataset's grid

regrid interpolates a dataset onto the grid of another dataset (or a netCDF file used purely as a grid template). Here, we first crop one dataset to the northern hemisphere to use as the target grid:

python
ds1 = nc.open_thredds("https://psl.noaa.gov/thredds/dodsC/Datasets/COBE2/sst.mon.mean.nc")
ds1.subset(timestep=0)
ds1.subset(lat=[0, 90])
ds1.plot()
ds1.plot() — interactive output Open interactive
Northern hemisphere sea surface temperature, used as the target grid

Now regrid the original file onto that northern-hemisphere grid — this also works with a plain netCDF file path as the target:

python
ds2 = nc.open_thredds("https://psl.noaa.gov/thredds/dodsC/Datasets/COBE2/sst.mon.mean.nc")
ds2.subset(timestep=0)
ds2.regrid(ds1)
ds2.plot()
ds2.plot() — interactive output Open interactive
Regridded onto the northern-hemisphere target grid

Reusing regrid weights

Regridding generates a weights file first. If you're regridding many files onto the same target grid one after another — postprocessing a batch of files, say — set recycle=True on the first call so regrid can reuse those weights on later calls:

python
ds = nc.open_thredds("https://psl.noaa.gov/thredds/dodsC/Datasets/COBE2/sst.mon.mean.nc")
ds.subset(timestep=0)
ds.to_latlon(lon=[-79.5, 79.5], lat=[-0.75, 89.75], res=[1, 0.5], recycle=True)
ds.plot()
ds.plot() — interactive output Open interactive
Regridded with recycle=True, generating reusable weights
python
ds1 = nc.open_thredds("https://psl.noaa.gov/thredds/dodsC/Datasets/COBE2/sst.mon.mean.nc")
ds1.subset(timestep=0)
ds1.regrid(ds)   # reuses the weights generated above
ds1.plot()
ds1.plot() — interactive output Open interactive
A second dataset regridded using the recycled weights

A set of coordinates

regrid also accepts a pandas dataframe of lon/lat columns, to interpolate onto specific points:

python
coords = pd.DataFrame({"lon": [-30], "lat": [50]})
ds.regrid(coords)

Coarsening & infilling

resample_grid makes data spatially coarser — here, keeping only every 10th cell of a 1°×1° dataset turns it into a 10°×10° one:

python
ds = nc.open_thredds("https://psl.noaa.gov/thredds/dodsC/Datasets/COBE2/sst.mon.mean.nc")
ds.subset(timestep=0)
ds.resample_grid(10)
ds.plot()
ds.plot() — interactive output Open interactive
Grid resampled to every 10th cell

fill_na replaces missing values with a distance-weighted average of nearby cells:

python
ds = nc.open_thredds("https://psl.noaa.gov/thredds/dodsC/Datasets/COBE2/sst.mon.mean.nc")
ds.subset(timestep=0)
ds.fill_na(1)   # distance-weighted infill using 1 nearest neighbour
ds.plot()
ds.plot() — interactive output Open interactive
Missing values filled by distance-weighted infill