Regrid and interpolate horizontally#

In this tutorial, we show how to:

  • regrid data to another regular or curvilinear grid with the bilinear, bicubic and conservative methods,

  • reuse the weights, in memory and in a file,

  • interpolate to arbitrary points, such as a transect, with and without time,

  • handle land points with the skipna and na_thres options,

  • use the same tools from the xoa accessors.

The concepts are explained in the Horizontal interpolation and regridding guide. See also: Inspect and manipulate horizontal and vertical grids.

Initialisations#

import os
import tempfile
import time

import cartopy.crs as ccrs
import cmocean
import matplotlib.pyplot as plt
import numpy as np
import xarray as xr

import xoa
from xoa import geo, interp, regrid, weights
from xoa.core.grid import create_rotated_grid
from xoa.plot import add_colorbar, plot_field

xr.set_options(display_style="text")
plt.rcParams.update({"font.size": 9, "axes.titlesize": 10, "axes.titleweight": "bold"})

Read the source data#

We use the surface temperature of a regional model on a staggered grid. Longitudes and latitudes are 2D, and the temperature is defined at the “rho” points, which are the coordinates it carries. Land points are stored as zeros in the file: we set them to nan with the land-sea mask.

ds = xoa.open_data_sample("MODELS/CROCO/SOUTH-AFRICA/croco.south-africa.surf.nc")
ds["temp"] = ds.temp.where(ds.mask_rho.astype(bool))
temp = ds.temp.isel(time=0, s_rho=-1)
print(temp)
<xarray.DataArray 'temp' (eta_rho: 28, xi_rho: 42)> Size: 5kB
array([[1.2376944, 1.2556195, 1.3359301, ..., 1.138714 , 1.0061263,
        1.0009613],
       [1.1853043, 1.2229587, 1.2887297, ..., 1.4914453, 1.310838 ,
        1.3313618],
       [1.1678141, 1.1776136, 1.2055497, ..., 1.7796904, 1.7321255,
        1.7022741],
       ...,
       [1.6081119, 1.6241077, 1.6406591, ...,       nan,       nan,
              nan],
       [1.7010186, 1.7136791, 1.7443202, ...,       nan,       nan,
              nan],
       [1.7513992, 1.7772378, 1.8208438, ...,       nan,       nan,
              nan]], shape=(28, 42), dtype=float32)
Coordinates:
  * eta_rho  (eta_rho) float32 112B 6.0 8.0 10.0 12.0 ... 54.0 56.0 58.0 60.0
  * xi_rho   (xi_rho) float32 168B 86.0 88.0 90.0 92.0 ... 164.0 166.0 168.0
    lat_rho  (eta_rho, xi_rho) float32 5kB ...
    lon_rho  (eta_rho, xi_rho) float32 5kB ...
    s_rho    float32 4B -0.9219
    time     float64 8B 2.592e+06
Attributes:
    long_name:      potential temperature
    units:          Celsius
    field:          temperature, scalar, series
    standard_name:  sea_water_potential_temperature

The coordinates are found from their meta-data, whatever their names.

lon_rho lat_rho

Regrid to a regular grid#

A destination grid is any object with longitude and latitude coordinates, that can be 1D or 2D.

lon_attrs = {"standard_name": "longitude", "units": "degrees_east"}
lat_attrs = {"standard_name": "latitude", "units": "degrees_north"}
dst = xr.Dataset(
    coords={
        "lon": ("lon", np.linspace(15.5, 21.5, 61), lon_attrs),
        "lat": ("lat", np.linspace(-37.4, -34.2, 33), lat_attrs),
    }
)

The Regridder class supports the bilinear, bicubic and conservative methods.

results = {}
for method in "bilinear", "bicubic", "conservative":
    regridder = regrid.Regridder(temp, dst, method=method)
    results[method] = regridder.regrid(temp)
print(results["bilinear"])
<xarray.DataArray 'temp' (lat: 33, lon: 61)> Size: 16kB
array([[1.2220604 , 1.24091675, 1.25552897, ..., 1.84527673, 1.81082153,
        1.79502936],
       [1.21636995, 1.22779059, 1.2397578 , ..., 1.83388771, 1.83087237,
        1.84611624],
       [1.20599237, 1.22084619, 1.23227058, ..., 1.96024973, 1.94974484,
        1.9555029 ],
       ...,
       [1.63399829, 1.66515223, 1.70681088, ...,        nan,        nan,
               nan],
       [1.66232279, 1.68833149, 1.7235959 , ...,        nan,        nan,
               nan],
       [1.73938662, 1.76647504, 1.79739482, ...,        nan,        nan,
               nan]], shape=(33, 61))
Coordinates:
  * lat      (lat) float64 264B -37.4 -37.3 -37.2 -37.1 ... -34.4 -34.3 -34.2
  * lon      (lon) float64 488B 15.5 15.6 15.7 15.8 15.9 ... 21.2 21.3 21.4 21.5
    s_rho    float32 4B -0.9219
    time     float64 8B 2.592e+06
Attributes:
    long_name:      potential temperature
    units:          Celsius
    field:          temperature, scalar, series
    standard_name:  sea_water_potential_temperature

Compare the three methods with the source field. The conservative method is the best choice for fluxes or quantities that must be preserved when going to a coarser grid.

# We use :func:`xoa.plot.plot_field`, that finds the coordinates in the field
# and sets up the map, and give all panels the extent of the source.

extent = geo.get_extent(temp, margin=0.02)
fig, axes = plt.subplots(
    2, 2, figsize=(9, 7), subplot_kw={"projection": ccrs.Mercator()}, constrained_layout=True
)
kw = dict(
    vmin=float(temp.min()), vmax=float(temp.max()), cmap=cmocean.cm.thermal, add_colorbar=False
)
fields = {"source": temp, **results}
for ax, (title, field) in zip(axes.flat, fields.items()):
    mappable = plot_field(
        field, ax=ax, title=title, map_kw={"gridlines_labels_on": ["bottom"]}, **kw
    )
    ax.set_extent(extent, crs=ccrs.PlateCarree())
add_colorbar(mappable, axes, da=temp)
source, bilinear, bicubic, conservative
<matplotlib.colorbar.Colorbar object at 0x738c04f7fe00>

Weights#

The expensive part, finding where destination points are in the source grid, is done once, at the first call, and reused for all next calls and all data arrays sharing the same horizontal dimensions, whatever the other ones.

regridder = regrid.Regridder(temp, dst, method="bilinear")
t0 = time.perf_counter()
regridder.regrid(temp)
t1 = time.perf_counter()
regridder.regrid(ds.temp)  # all times and levels at once
t2 = time.perf_counter()
print(f"first call with weights computation: {t1 - t0:.3f} s")
print(f"second call on the full 4D variable: {t2 - t1:.3f} s")
print(ds.temp.dims, "->", regridder.regrid(ds.temp).dims)
first call with weights computation: 0.007 s
second call on the full 4D variable: 0.009 s
('time', 's_rho', 'eta_rho', 'xi_rho') -> ('time', 's_rho', 'lat', 'lon')

They are also shared in memory: another regridder or interpolator, or a call of the xoa accessors, that has the same grids, method and parameters reuses them. Weights are never computed twice for the same grids, whatever the number of variables.

print(regrid.Regridder(temp, dst, "bilinear").core_regridder is regridder.core_regridder)
True

The weights can also be saved to a netcdf file with the weights_file parameter, to be reused in another session. A single file can hold the weights of all your grids and methods: each one is saved in a group whose name contains the method and a fingerprint of the grids. The weights are thus found automatically, and can never be loaded for other grids. See xoa.weights.

with tempfile.TemporaryDirectory() as tmpdir:
    weights_file = os.path.join(tmpdir, "weights.nc")
    for method in "bilinear", "conservative":
        regrid.Regridder(temp, dst, method, weights_file=weights_file).regrid(temp)
    print(*weights.list_groups(weights_file), sep="\n")

    # In a new session, the weights are read from the file
    regrid.clear_weights_cache()
    reloaded = regrid.Regridder(temp, dst, "bilinear", weights_file=weights_file)
    print(reloaded.core_regridder.has_weights)
    print(np.allclose(reloaded.regrid(temp), results["bilinear"], equal_nan=True))
regrid_bilinear_30bcc3af9d4b7d38233f21772714fafd
regrid_conservative_30bcc3af9d4b7d38233f21772714fafd
True
True

Regrid to a curvilinear grid#

Grids may also be curvilinear, like this rotated one from xoa.core.grid.create_rotated_grid(). Points that are outside the source domain are set to nan.

grid = create_rotated_grid(30, 20, 18.5, -36.0, 25.0, 4.0, 2.5)
rotated = xr.Dataset(
    {
        "lon": (("y", "x"), grid["lon"], lon_attrs),
        "lat": (("y", "x"), grid["lat"], lat_attrs),
    }
)
on_rotated = regrid.Regridder(temp, rotated, "bilinear").regrid(temp)
print(on_rotated.dims, int(on_rotated.isnull().sum()), "missing points")
('y', 'x') 53 missing points

And back to the original grid, to check the loss of information.

src_grid = xr.Dataset(coords={"lon_rho": temp.lon_rho, "lat_rho": temp.lat_rho})
back = regrid.Regridder(on_rotated, src_grid, "bilinear").regrid(on_rotated)
fig, axes = plt.subplots(
    1, 3, figsize=(9, 3.5), subplot_kw={"projection": ccrs.Mercator()}, constrained_layout=True
)
kw = dict(map_kw={"gridlines_labels_on": ["bottom"]}, add_colorbar=False)
plot_field(on_rotated, ax=axes[0], title="rotated grid", cmap=cmocean.cm.thermal, **kw)
plot_field(back, ax=axes[1], title="back to the source grid", cmap=cmocean.cm.thermal, **kw)
diff = plot_field(
    back - temp, ax=axes[2], title="difference", cmap=cmocean.cm.balance, vmin=-1, vmax=1, **kw
)
extent_rot = geo.get_extent(on_rotated, margin=0.05)
for ax in axes:
    ax.set_extent(extent_rot, crs=ccrs.PlateCarree())
add_colorbar(diff, axes[2], label="Difference [Celsius]")
rotated grid, back to the source grid, difference
<matplotlib.colorbar.Colorbar object at 0x738c054d38c0>

Interpolate to points#

The Interpolator class interpolates to any set of points, like a transect or scattered positions, with the bilinear and bicubic methods. Dimensions that are not horizontal are preserved.

lons = np.linspace(16.5, 20.5, 40)
lats = np.linspace(-36.8, -35.2, 40)
transect = interp.Interpolator(temp, lons, lats, method="bicubic").interp(ds.temp.isel(time=0))
print(transect.dims, transect.shape)
('s_rho', 'pts') (4, 40)

The result is a section along the transect, for all levels at once.

fig, ax = plt.subplots(figsize=(7, 4), constrained_layout=True)
ax.plot(lons, transect.isel(s_rho=-1))
ax.set_xlabel("Longitude")
ax.set_ylabel("Surface temperature")
plot regrid interp
Text(18.16992968750001, 0.5, 'Surface temperature')

Destination coordinates can also have their own dimensions, or be a grid. Here is a 2D array of points, that is a destination grid.

pts_lon, pts_lat = np.meshgrid(np.linspace(17, 20, 4), np.linspace(-36.5, -35.5, 3))
print(interp.Interpolator(temp, pts_lon, pts_lat).interp(temp).dims)
('pts_y', 'pts_x')

A map shows where the field is interpolated: along the transect and on the grid of points.

fig, ax = plt.subplots(
    figsize=(7, 5.5), subplot_kw={"projection": ccrs.Mercator()}, constrained_layout=True
)
mappable = plot_field(
    temp,
    ax=ax,
    title="Interpolation points",
    cmap=cmocean.cm.thermal,
    map_kw={"gridlines_labels_on": ["bottom", "left"]},
    add_colorbar=False,
)
ax.set_extent(extent, crs=ccrs.PlateCarree())
pc = ccrs.PlateCarree()
ax.plot(lons, lats, "-", color="w", lw=4, transform=pc)
ax.plot(lons, lats, ".-", color="k", lw=1.5, ms=4, transform=pc, label="transect")
ax.plot(
    pts_lon.ravel(),
    pts_lat.ravel(),
    "o",
    mfc="w",
    mec="k",
    ms=7,
    ls="none",
    transform=pc,
    label="grid of points",
)
ax.legend(loc="lower right")
add_colorbar(mappable, ax, da=temp)
Interpolation points
<matplotlib.colorbar.Colorbar object at 0x738c046538c0>

Interpolate in space and time#

Positions may also come with times, like an observing platform. The temperature of a regional ocean model, here on a regular grid, is interpolated linearly between the two available dates.

ibi = xoa.open_data_sample("MODELS/CMEMS-IBI/ibi-argo-7900573.nc")
sst = ibi.thetao.isel(depth=0)
times = np.array(
    ["2022-02-06T18:00", "2022-02-07T00:00", "2022-02-07T06:00"], dtype="datetime64[ns]"
)
track = interp.Interpolator(sst, [-10.0, -9.8, -9.6], [43.8, 43.9, 44.0])
at_times = track.interp_with_time(sst, times)
print(at_times.values)
[14.04144573 13.8153053  13.54053395]

Masks and missing values#

Land points are nan in the source field. By default, a destination point that has at least one nan neighbour is nan too: the missing values contaminate the result near the coast. Use skipna to ignore them and compute the interpolation from the valid neighbours only. When land is not nan but a fill value like zero, provide the mask of the source grid with the src_mask parameter of the regridder instead.

coast = xr.Dataset(
    coords={
        "lon": ("lon", np.linspace(18.3, 20.5, 45), lon_attrs),
        "lat": ("lat", np.linspace(-34.8, -34.1, 15), lat_attrs),
    }
)
naive = regrid.Regridder(temp, coast, "bilinear").regrid(temp)
masked = regrid.Regridder(temp, coast, "bilinear").regrid(temp, skipna=True)
print(
    "missing points by default:",
    int(naive.isnull().sum()),
    "- with skipna:",
    int(masked.isnull().sum()),
)
missing points by default: 473 - with skipna: 293

The na_thres parameter controls how many missing neighbours are tolerated. With skipna, the weights of the valid neighbours of a destination point are renormalised, and the point is set to nan when they represent less than 1 - na_thres of the total weight:

  • na_thres=0: all the neighbours must be valid, so that the result is nan as soon as one neighbour is on land,

  • na_thres=0.5: at least half of the weight must come from valid neighbours,

  • na_thres=1: a single valid neighbour is enough to get a value.

Here is the effect of these three values on a fine grid that zooms on the coast, next to the cells of the source grid.

zoom = xr.Dataset(
    coords={
        "lon": ("lon", np.linspace(19.2, 20.6, 36), lon_attrs),
        "lat": ("lat", np.linspace(-35.0, -34.5, 17), lat_attrs),
    }
)
zoom_regridder = regrid.Regridder(temp, zoom, "bilinear")
extent_zoom = [19.2, 20.6, -35.0, -34.5]
fig, axes = plt.subplots(
    2, 2, figsize=(9, 4), subplot_kw={"projection": ccrs.Mercator()}, constrained_layout=True
)
kw = dict(
    vmin=float(temp.min()),
    vmax=float(temp.max()),
    cmap=cmocean.cm.thermal,
    add_colorbar=False,
    edgecolors="0.4",
    linewidth=0.1,
    map_kw={"gridlines_labels_on": ["bottom", "left"]},
)
fields = {"source cells": temp}
for na_thres in 0, 0.5, 1:
    fields[f"na_thres={na_thres}"] = zoom_regridder.regrid(temp, skipna=True, na_thres=na_thres)
for ax, (title, field) in zip(axes.flat, fields.items()):
    mappable = plot_field(field, ax=ax, title=title, **kw)
    ax.set_extent(extent_zoom, crs=ccrs.PlateCarree())
add_colorbar(mappable, axes, da=temp)
print({title: int(field.notnull().sum()) for title, field in list(fields.items())[1:]})
source cells, na_thres=0, na_thres=0.5, na_thres=1
{'na_thres=0': 247, 'na_thres=0.5': 381, 'na_thres=1': 497}

Use the accessors#

All of that is also available from the regrid() and interp() methods of the xoa accessors, which build the regridder or the interpolator on the fly.

print(temp.xoa.regrid(dst, method="bilinear").shape)
print(temp.xoa.interp(lons, lats, method="bilinear").shape)
(33, 61)
(40,)

Note

To keep the weights between calls, create the Regridder or Interpolator object yourself, as shown above. The numba kernels they rely on are available in xoa.core.interp and xoa.core.regrid for use with plain numpy arrays.

Total running time of the script: (0 minutes 15.319 seconds)

Gallery generated by Sphinx-Gallery