Interpolation#

nctoolkit features built in methods for horizontal and vertical interpolation.

Horizontal interpolation#

We will illustrate how to carry out horizontal interpolation using a global dataset of global SST from NOAA. Find out more information about the datset here.

The data is available using a thredds server. So we will work with the first time step, which looks like this:

import nctoolkit as nc
ds = nc.open_thredds("https://psl.noaa.gov/thredds/dodsC/Datasets/COBE2/sst.mon.mean.nc")
ds.subset(time = 0)
ds.plot()
interpolate_plot1

Interpolating to a regular lonlat grid#

If you want to interpolate to a regular latlon grid, you can use to_latlon. lon and lat specify the minimum and maximum longitudes and latitudes, while res, a 3 variable list specifies the resolution. For example, if we wanted to regrid the globe to 0.5 degree north-south by 1 degree east-west resolution, we could do the following:

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])
ds.plot()
interpolate_plot2

Interpolating to another dataset’s grid#

If we are working with two datasets and want to put them on a common grid, we can interpolate one onto the other’s grid. We can illustate this with a dataset of global sea surface temperature. Let’s start by cropping this dataset to the northern hemisphere.

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()
interpolate_plot3

Now, we can regrid the original file to this northern hemisphere grid.

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()
interpolate_plot4