geocat.comp.polynomial.detrend#

geocat.comp.polynomial.detrend(data, deg=1, axis=0, **kwargs)#

Estimates and removes the trend of the leftmost dimension from all grid points. This method, at the minimum, provides all the functionality that is provided by NCL’s ‘dtrend’, ‘dtrend_quadratic’, ‘dtrend_quadratic_msg_n’, ‘dtrend_msg_n’, ‘dtrend_msg’, ‘dtrend_n’. However, this function is not limited to quadratic detrending and you could use higher polynomial degree as well.

Parameters
  • data (array_like) – a multi-dimensional numeric array

  • deg (int, optional) – a non-negative integer determining the degree of the polynomial to use for detrending. Default value is 1.

  • axis (int, optional) – the axis along which the data is detrended. Default value is 0.

  • kwargs (dict, optional) – See below

Keyword Arguments
  • return_info (bool) – If set to true, the fitted polynomial is returned as part of the attributes. Default value is True.

  • missing_value (numeric) – A value that must be ignored. Default is NaN.

Returns

detrended_data (xarray.DataArray) – Array containing the detrended data.

Examples

  • Detrending a data:

>>> from geocat.comp.polynomial import ndpolyfit
>>> from geocat.comp.polynomial import detrend
>>> # Creating synthetic data
>>> x = np.linspace(-8*np.pi, 8 * np.pi, 33, dtype=np.float64)
>>> y0 = 1.0 * x
>>> y1 = np.sin(x)
>>> y = y0 + y1
>>> p = ndpolyfit(np.arange(x.size), y, deg=1)
>>> y_trend = ndpolyval(p, np.arange(x.size))
>>> y_detrended = detrend(y)
>>> np.testing.assert_almost_equal(y_detrended + y_trend, y)
  • Detrending a multi-dimensional data:

>>> # Creating synthetic data
>>> x = np.linspace(-8*np.pi, 8 * np.pi, 33, dtype=np.float64)
>>> y0 = 1.0 * x
>>> y1 = np.sin(x)
>>> y = np.tile((y0 + y1).reshape((1, -1, 1, 1)), (2, 1, 3, 4))
>>> p = ndpolyfit(x, y, deg=1, axis=1)
>>> y_trend = ndpolyval(p, x, axis=1)
>>> y_detrended = detrend(y, x=x, axis=1)
>>> np.testing.assert_almost_equal(y_detrended + y_trend, y)