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:

[1]:
import nctoolkit as nc
ds = nc.open_thredds("https://psl.noaa.gov/thredds/dodsC/Datasets/COBE2/sst.mon.mean.nc")
ds.select(time = 0)
ds.plot()
nctoolkit is using Climate Data Operators version 1.9.10
[1]:

Interpolating to a set of coordinates

If you want to regrid a dataset to a specified set of coordinates you can regrid and a pandas dataframe. The first column of the dataframe should be the longitudes and the second should be latitudes. The example below regrids a sea-surface temperature dataset to a single location with longitude -30 and latitude 50.

[2]:
import pandas as pd
ds = nc.open_thredds("https://psl.noaa.gov/thredds/dodsC/Datasets/COBE2/sst.mon.mean.nc")
ds.select(timestep = 0)
coords = pd.DataFrame({"lon":[-30], "lat":[50]})
ds.regrid(coords)
ds.to_dataframe()

[2]:
lon lat sst
time ncells
1850-01-01 0 -30.0 50.0 10.935501

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 2 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:

[3]:
ds = nc.open_thredds("https://psl.noaa.gov/thredds/dodsC/Datasets/COBE2/sst.mon.mean.nc")
ds.select(timestep = 0)
ds.to_latlon(lon = [-79.5, 79.5], lat = [0.75, 89.75], res = [1, 0.5])
ds.plot()
[3]:

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.

[4]:
ds1 = nc.open_thredds("https://psl.noaa.gov/thredds/dodsC/Datasets/COBE2/sst.mon.mean.nc")
ds1.select(timestep = 0)
ds1.crop(lat = [0, 90])
ds1.plot()
[4]:

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

[5]:
ds2 = nc.open_thredds("https://psl.noaa.gov/thredds/dodsC/Datasets/COBE2/sst.mon.mean.nc")
ds2.select(timestep = 0)
ds2.regrid(ds1)
ds2.plot()
[5]:

This method will also work using netCDF files. So, if you wanted you can also use a path to a netCDF file as the target grid.

How to reuse the weights for regridding

Under the hood nctoolkit regrids data by first generating a weights file. There are situations where you will want to be able to re-use these weights. For example, if you are post-processing a large number of files one after the other. To make this easier nctoolkit let’s you recycle the regridding info. This let’s you interpolate using either regrid or to_latlon, but keep the regridding data for future use by regrid.

[6]:
ds = nc.open_thredds("https://psl.noaa.gov/thredds/dodsC/Datasets/COBE2/sst.mon.mean.nc")
ds.select(timestep = 0)
ds.to_latlon(lon = [-79.5, 79.5], lat = [-0.75, 89.75], res = [1, 0.5], recycle = True)
ds.plot()
[6]:
[7]:
ds1 = nc.open_thredds("https://psl.noaa.gov/thredds/dodsC/Datasets/COBE2/sst.mon.mean.nc")
ds1.select(timestep = 0)
ds1.regrid(ds)
ds1.plot()
[7]:

Horizontal Resampling

If you want to make data more coarse spatially, just use the resample_grid method. This will, for example, let you select every 2nd grid grid cell in a north-south and east-west direction. This is illustrated in the example below, where a dataset which has spatial resolution of 1 by 1 degrees is coarsened, so that only every 10th cell is selected in a north-south and east-west. In other words it is now a 10 degrees by 10 degrees dataset.

[8]:
ds = nc.open_thredds("https://psl.noaa.gov/thredds/dodsC/Datasets/COBE2/sst.mon.mean.nc")
ds.select(timestep = 0)
ds.resample_grid(10)
ds.plot()
[8]:

Spatial Infilling

Some times you will have data with missing values, which you want to replace with a nearby value. nctoolkit handles this situation using the fill_na method. This uses distance-weighting. You just need to specify the number of nearest-neighbours to use for the weighting. For example, if you simply want to replace missing values with their nearest neighbour, you just set the number to 1, as follows:

[9]:
ds = nc.open_thredds("https://psl.noaa.gov/thredds/dodsC/Datasets/COBE2/sst.mon.mean.nc")
ds.select(timestep = 0)
ds.fill_na(1)
ds.plot()
[9]:

Vertical interpolation

We can carry out vertical interpolation using the vertical_interp method. This is particularly useful for oceanic data. This is illustrated below by interpolating depth-resolved ocean temperatures from NOAA’s World Ocean Atlas for January to a depth of 500 metres. The vertical_interp method requires a levels argument, which is sea-depth in this case.

[10]:
ds = nc.open_thredds("https://data.nodc.noaa.gov/thredds/dodsC/ncei/woa/temperature/A5B7/1.00/woa18_A5B7_t01_01.nc")
ds.select(variables="t_an")
ds.vertical_interp(levels= [500])
ds.plot()
Warning: xarray could not decode times!
[10]:
[ ]: