pyinterp.backends.xarray.DataArrayAccessor.interp

pyinterp.backends.xarray.DataArrayAccessor.interp#

DataArrayAccessor.interp(coords=None, method='bilinear', **coords_kwargs)[source]#

Interpolate the data array at new coordinates.

This method is a faster replacement of xarray.DataArray.interp() for variables defined on 2D, 3D or 4D regular grids. As there, a coordinate can be:

  • a scalar, which gives a scalar coordinate of the result;

  • a one-dimensional array, which gives a dimension named after the coordinate: several such coordinates define a grid (outer product);

  • a xarray.DataArray, whose dimensions become those of the result: coordinates sharing a dimension are interpolated point by point.

Unlike xarray.DataArray.interp(), every dimension of the variable must be interpolated. Longitudes, latitudes and dates are identified as in RegularGridInterpolator: longitudes are periodic and dates can be given as strings.

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

  • method (InterpolationMethods) – Interpolation method. See RegularGridInterpolator.__call__() for the supported methods.

  • **coords_kwargs (Any) – The coordinates to interpolate at, for keyword arguments named after a dimension of the variable, or options of the interpolation method, such as bounds_error, num_threads or half_window_size_x. Use coords for a dimension whose name is that of an option.

Returns:

The interpolated values, labelled with the coordinates they were interpolated at, with the name and attributes of the variable.

Raises:

ValueError – If a dimension of the variable has no coordinate to interpolate at, or if a coordinate is not a dimension.

Return type:

xr.DataArray

Note

The data are loaded into memory and copied into a new grid at each call. To interpolate the same variable many times, build a RegularGridInterpolator once and reuse it.

Examples

Interpolate on a new grid

>>> result = da.pyinterp.interp(
...     lon=np.arange(-180, 180, 0.25),
...     lat=np.arange(-80, 80, 0.25),
...     method="bicubic",
... )

Interpolate along a track

>>> track = da.pyinterp.interp(
...     lon=xr.DataArray(lons, dims="obs"),
...     lat=xr.DataArray(lats, dims="obs"),
...     time=xr.DataArray(dates, dims="obs"),
... )