deriv_half

deriv_half(x, y, /, order=1, axis=0, cyclic=False)[source]

Return an arbitrary order finite difference approximation by taking successive half-level differences. This will change both the length of the data and the x coordinates of the data. While this is not always practical, it retains data resolution better than the centered methods.

Parameters
  • x (float or array-like) – The step size, a 1-d coordinate vector, or an array of coordinates matching the shape of y.

  • y (array-like) – The data.

  • order (int, optional) – The order of the derivative, i.e. the \(n\) in \(d^ny/dx^n\). Default is 1.

  • axis (int, optional) – Axis along which derivative is taken.

  • dim (str, optional) – For `xarray.DataArray` input only. Named dimension along which derivative is taken.

  • cyclic (bool, optional) – Whether to treat the axis cyclically. If True, the dimension size is not reduced. This is appropriate for derivatives across longitudes and cyclic idealized model domains.

Returns

  • x (array-like) – The new x coordinates.

  • diff (array-like) – The “derivative”.

Examples

>>> import xarray as xr
>>> import climopy as climo
>>> x = xr.DataArray([0, 2, 4], name='x', dims='p', coords={'p': [1000, 800, 600]})
>>> y = xr.DataArray([0, 4, 16], name='y', dims='p')
>>> dx, dy = climo.deriv_half(x, y)
>>> dx
<xarray.DataArray 'x' (p: 2)>
array([1., 3.])
Coordinates:
  * p        (p) float64 900.0 700.0
>>> dy
<xarray.DataArray 'y' (p: 2)>
array([2., 6.])
Dimensions without coordinates: p