Regridding with Regrid¶
The Regrid resampler converts data from a complex source grid (e.g. HEALPix or reduced Gaussian) to a regular latitude/longitude grid before plotting, using earthkit-geo under the hood.
Use Regrid when:
your source data is on a HEALPix or reduced Gaussian (octahedral) grid, and
you want to plot it with methods that expect a regular lat/lon grid (e.g.
pcolormesh).
Regrid supports two interpolation methods:
'linear'(default) — bilinear interpolation in data space, producing smooth output.'nearest-neighbour'— nearest cell lookup, preserving exact grid-cell values.
NOTE: Regrid requires the earthkit-geo package (with MIR support). For regular lat/lon data, use Bilinear or NearestNeighbour instead — Regrid will raise an error if given a regular grid.
Example: HEALPix 2 m temperature¶
We load a HEALPix GRIB file at H128 resolution (nested ordering) containing 2 m temperature, then regrid it to a regular 0.5° lat/lon grid before plotting.
[1]:
import earthkit.data as ekd
import earthkit.plots as ekp
from earthkit.plots.resample import Regrid
data = ekd.from_source("sample", "healpix-h128-nested-2t.grib")
chart = ekp.Map(domain="Europe")
# Regrid to 0.5° lat/lon using linear interpolation
chart.pcolormesh(
data,
resample=Regrid(resolution=0.5),
style=ekp.styles.Style(
levels=range(240, 310, 5),
colors="Spectral_r",
),
)
chart.coastlines()
chart.gridlines()
chart.legend()
chart.show()
Choosing the output resolution¶
The resolution parameter controls the spacing (in degrees) of the regular lat/lon output grid. A finer resolution produces more detail but takes longer to compute. The default is 0.2°.
[2]:
style = ekp.styles.Style(
levels=range(240, 310, 5),
colors="Spectral_r",
)
figure = ekp.Figure(rows=1, columns=2, domain="Europe")
ax = figure.add_map()
ax.pcolormesh(data, resample=Regrid(resolution=2.0), style=style)
ax.title("resolution=2.0°")
ax = figure.add_map()
ax.pcolormesh(data, resample=Regrid(resolution=0.25), style=style)
ax.title("resolution=0.25°")
figure.coastlines()
figure.legend()
figure.show()
Supplying the grid spec manually¶
If your data does not carry the grid metadata that earthkit-plots needs (for example, after converting to xarray and stripping the _earthkit attribute), you can supply the source grid specification explicitly via the in_grid parameter.
[3]:
# Convert to xarray and strip the earthkit metadata
ring_data = ekd.from_source("sample", "healpix-h128-ring-2t.grib")
ds = ring_data.to_xarray()
ds.t.attrs.pop("_earthkit", None)
chart = ekp.Map(domain="Europe")
# Tell Regrid what the source grid is
chart.pcolormesh(
ds,
resample=Regrid(resolution=0.5, in_grid={"grid": "H128", "order": "ring"}),
style=style,
)
chart.coastlines()
chart.gridlines()
chart.legend()
chart.show()