pyinterp.backends.xarray.DataArrayAccessor.regrid

pyinterp.backends.xarray.DataArrayAccessor.regrid#

DataArrayAccessor.regrid(coords=None, method='conservative', *, source_bounds=None, **coords_kwargs)[source]#

Regrid the data array onto new cells, conserving its integral.

Each target cell receives the mean of the source values weighted by the area of their overlap with the cell (first order conservative regridding, conservative_normed in xESMF by default). Unlike interp(), which samples the variable at points, the regridding averages it: use it to coarsen a grid, or to regrid fluxes and budgets.

Two dimensions are regridded: a longitude and a latitude, identified by their units (degrees_east and degrees_north) as in interp(), or two Cartesian axes. Longitudes are periodic: the source and target grids can use different conventions. The other dimensions (time, depth) and their coordinates are kept. The areas are computed on a sphere; pass spheroid=Spheroid() for WGS-84 (see pyinterp.regrid.conservative(), which also details the differences with xESMF).

The cells of a dimension are given by their centers, a 1D array, or by their bounds, an array of shape (n, 2) as the CF bounds variables, whose midpoints label the result. The bounds of cells given by their centers are the midpoints between consecutive centers, so a dimension of a single cell must be given by its bounds.

Parameters:
  • coords (Mapping[Hashable, Any] | None) – Mapping from dimension names to the target cells.

  • method (RegriddingMethods) – Regridding method, only "conservative" for now.

  • source_bounds (Mapping[Hashable, Any] | None) – Mapping from dimension names to the bounds of the source cells, arrays of shape (n, 2). Defaults to the midpoints between the coordinates. A data array does not carry the CF bounds variables of its dataset: regrid the dataset with DatasetAccessor.regrid() to use them.

  • **coords_kwargs (Any) – The target cells, for keyword arguments named after a dimension of the variable, or options of the regridding: min_valid_fraction, normalize, spheroid and num_threads (see pyinterp.regrid.conservative()).

Returns:

The regridded values, labelled with the centers of the target cells, with the name and attributes of the variable.

Raises:

ValueError – If the regridded dimensions are not two dimensions of the variable, or if their cells are invalid.

Return type:

xr.DataArray

Examples

Coarsen a variable to 1°

>>> result = da.pyinterp.regrid(
...     lon=np.arange(-179.5, 180), lat=np.arange(-89.5, 90)
... )

Mean over a box, given by its bounds

>>> box = da.pyinterp.regrid(lon=[[-10, 10]], lat=[[40, 50]])