xoa.filter.convolve

xoa.filter.convolve(data, kernel, normalize=False, na_thres=0, kernel_kwargs=None, **kwargs)[source]

N-dimensional convolution that takes care of nans

Parameters:
  • data (xarray.DataArray) – Array to filter

  • kernel (int, tuple, numpy.ndarray, xarray.DataArray) – Convolution kernel. See generate_kernel().

  • normalize (bool) – Divide the convolution product by the local sum weights. The result is then a weighted average.

  • na_thres (float) –

    A float between 0 and 1 that defines the allowed level a NaN contamination. Examples of the behavioir at a single location:

    • 0: Output is masked if a single NaN is found.

    • 0.5: Output is masked only more than 50% of the input data are masked.

    • 1: Output is masked if all input data are masked.

  • kernel_kwargs (dict, None) – Extra parameters passed to generate_kernel().

  • kwargs (dict) – Extra parameters are merged with kernel_kwargs

Returns:

xarray.DataArray – The filtered array with the same shape, attributes and coordinates as the input array.

Example

In [1]: data = xr.DataArray(np.random.normal(size=(50, 70)), dims=('y', 'x'))

In [2]: data[10:20, 10:20] = np.nan # introduce missing data

In [3]: kernel = dict(x=[1, 2, 5, 2, 1], y=[1, 2, 1])

In [4]: datac = convolve(data, kernel, normalize=True, na_thres=1)

In [5]: fig, (ax0, ax1) = plt.subplots(ncols=2, figsize=(7, 3))

In [6]: kw = dict(vmin=data.min(), vmax=data.max())

In [7]: data.plot.pcolormesh(ax=ax0, **kw);

In [8]: datac.plot.pcolormesh(ax=ax1, **kw);
../_images/api.filter.convolve.png