lair.geo.regrid#
- lair.geo.regrid(data, out_grid, method='bilinear')[source]#
Regrid data to a new grid. Uses xesmf for regridding.
An output cell that the source does not reach is NaN: outside the source, and, for
'bilinear'and'patch', a center beyond the source’s outermost cell centers. A conservative regrid keeps the part of a cell that overlaps the source, so only a cell wholly outside it is NaN.'nearest_s2d'gives every cell its nearest source cell, however far.Note
At present, xesmf only supports regridding lat-lon grids. self.data must be on a lat-lon grid. Possibly could use xesmf.frontend.BaseRegridder to regrid to a generic grid.
Warning
xarray.Dataset.cf.add_bounds is known to have issues, including near the 180th meridian. Care should be taken when using this method, especially with global datasets.
- Parameters:
data (xr.DataArray | xr.Dataset) – The data to regrid.
out_grid (xr.DataArray) – The new grid to resample to. Must be a lat-lon grid.
method (str, optional) – The regridding method, by default ‘bilinear’.
- Returns:
xr.DataArray | xr.Dataset – The regridded data, the same type as
data.- Return type:
_XarrayT
See also
resampleTo a new resolution over the same area.
generate_regular_gridMakes a grid to regrid to.
Examples
A flux of 1 on four cells of 0.1 degree, to cells of 0.05 degree; a conservative regrid keeps the flux:
>>> import numpy as np >>> import xarray as xr >>> from lair.geo import generate_regular_grid, regrid >>> flux = xr.DataArray( ... np.ones((2, 2)), ... coords={"lat": [40.65, 40.75], "lon": [-111.95, -111.85]}, ... dims=("lat", "lon"), ... ) >>> fine = generate_regular_grid( ... -112.0, -111.8, 0.05, 40.6, 40.8, 0.05, x_label="lon", y_label="lat" ... ) >>> out = regrid(flux, fine, method="conservative") >>> out.sizes["lat"], out.sizes["lon"] (4, 4) >>> float(out.min().round(3)), float(out.max().round(3)) (1.0, 1.0)