interpolate

xarray spline interpolation functions

xarray_extras.interpolate.splev(x_new: xarray.DataArray, tck: xarray.Dataset, extrapolate: bool | str = True) xarray.DataArray

Evaluate the B-spline generated with splrep().

Parameters
  • x_new – Any DataArray with any number of dims, not necessarily the original interpolation dim. Alternatively, it can be any 1-dimensional array-like; it will be automatically converted to a DataArray on the interpolation dim.

  • tck (xarray.Dataset) –

    As returned by splrep(). It can have been:

    • transposed (not recommended, as performance will drop if c is not C-contiguous)

    • sliced, reordered, or (re)chunked, on any dim except the interpolation dim

    • computed from dask to numpy backend

    • round-tripped to disk

  • extrapolate

    True

    Extrapolate the first and last polynomial pieces of b-spline functions active on the base interval

    False

    Return NaNs outside of the base interval

    ’periodic’

    Periodic extrapolation is used

    ’clip’

    Return y[0] and y[-1] outside of the base interval

Returns

DataArray with all dims of the interpolated array, minus the interpolation dim, plus all dims of x_new

See splrep() for usage example.

xarray_extras.interpolate.splrep(a: xarray.DataArray, dim: collections.abc.Hashable, k: int = 3) xarray.Dataset

Calculate the univariate B-spline for an N-dimensional array

Parameters
  • a (xarray.DataArray) – any DataArray

  • dim – dimension of a to be interpolated. a.coords[dim] must be strictly monotonic ascending. All int, float (not complex), or datetime dtypes are supported.

  • k (int) –

    B-spline order:

    k

    interpolation kind

    0

    nearest neighbour

    1

    linear

    2

    quadratic

    3

    cubic

Returns

Dataset with t, c, k (knots, coefficients, order) variables, the same shape and coords as the input, that can be passed to splev().

Example:

>>> x = np.arange(0, 120, 20)
>>> x = xarray.DataArray(x, dims=['x'], coords={'x': x})
>>> s = xarray.DataArray(np.linspace(1, 20, 5), dims=['s'])
>>> y = np.exp(-x / s)
>>> x_new = np.arange(0, 120, 1)
>>> tck = splrep(y, 'x')
>>> y_new = splev(x_new, tck)

Features

  • Interpolate a ND array on any arbitrary dimension

  • dask supported on both on the interpolated array and x_new

  • Supports ND x_new arrays

  • The CPU-heavy interpolator generation (splrep()) is executed only once and then can be applied to multiple x_new (splev())

  • memory-efficient

  • Can be pickled and used on dask distributed

Limitations

  • Chunks are not supported along dim on the interpolated dimension.