harmonica.filters.pad

Contents

harmonica.filters.pad#

harmonica.filters.pad(da, pad_width=None, mode='constant', stat_length=None, constant_values=0, end_values=None, reflect_type=None, **pad_width_kwargs)[source]#

Pad array with evenly spaced coordinates.

Wraps the xarray.DataArray.pad method but also pads the evenly spaced coordinates by extrapolation using the same coordinate spacing. The pad_width used for each coordinate is stored as one of its attributes.

Parameters:
daxarray.DataArray

Array to be padded. The coordinates along which the array will be padded must be evenly spaced.

pad_widthmapping of hashable to tuple of int

Mapping with the form of {dim: (pad_before, pad_after)} describing the number of values padded along each dimension. {dim: pad} is a shortcut for pad_before = pad_after = pad.

modestr, default: “constant”

One of the following string values (taken from numpy docs). - constant: Pads with a constant value. - edge: Pads with the edge values of array. - linear_ramp: Pads with the linear ramp between end_value and the

array edge value.

  • maximum: Pads with the maximum value of all or part of the vector along each axis.

  • mean: Pads with the mean value of all or part of the vector along each axis.

  • median: Pads with the median value of all or part of the vector along each axis.

  • minimum: Pads with the minimum value of all or part of the vector along each axis.

  • reflect: Pads with the reflection of the vector mirrored on the first and last values of the vector along each axis.

  • symmetric: Pads with the reflection of the vector mirrored along the edge of the array.

  • wrap: Pads with the wrap of the vector along the axis. The first values are used to pad the end and the end values are used to pad the beginning.

stat_lengthint, tuple or mapping of hashable to tuple, default: None

Used in ‘maximum’, ‘mean’, ‘median’, and ‘minimum’. Number of values at edge of each axis used to calculate the statistic value. {dim_1: (before_1, after_1), … dim_N: (before_N, after_N)} unique statistic lengths along each dimension. ((before, after),) yields same before and after statistic lengths for each dimension. (stat_length,) or int is a shortcut for before = after = statistic length for all axes. Default is None, to use the entire axis.

constant_valuesscalar, tuple or mapping of hashable to tuple, default: 0

Used in ‘constant’. The values to set the padded values for each axis. {dim_1: (before_1, after_1), ... dim_N: (before_N, after_N)} unique pad constants along each dimension. ((before, after),) yields same before and after constants for each dimension. (constant,) or constant is a shortcut for before = after = constant for all dimensions. Default is 0.

end_valuesscalar, tuple or mapping of hashable to tuple, default: 0

Used in ‘linear_ramp’. The values used for the ending value of the linear_ramp and that will form the edge of the padded array. {dim_1: (before_1, after_1), ... dim_N: (before_N, after_N)} unique end values along each dimension. ((before, after),) yields same before and after end values for each axis. (constant,) or constant is a shortcut for before = after = constant for all axes. Default is 0.

reflect_type{“even”, “odd”}, optional

Used in “reflect”, and “symmetric”. The “even” style is the default with an unaltered reflection around the edge value. For the “odd” style, the extended part of the array is created by subtracting the reflected values from two times the edge value.

**pad_width_kwargs

The keyword arguments form of pad_width. One of pad_width or pad_width_kwargs must be provided.

Returns:
da_paddedxarray.DataArray

Examples

>>> import xarray as xr
>>> import pytest
>>> from packaging.version import Version
>>> if Version(xr.__version__) < Version("2025.10.1"):
...     pytest.skip("This doctest works only in Xarray >= 2025.10.1")
>>> da = xr.DataArray(
...     [[1, 2, 3], [4, 5, 6], [7, 8, 9]],
...     coords={"x": [0, 1, 2], "y": [-5, -4, -3]},
...     dims=("y", "x"),
... )
>>> da_padded = pad(da, x=2, y=1)
>>> da_padded
<xarray.DataArray (y: 5, x: 7)> Size: 280B
array([[0, 0, 0, 0, 0, 0, 0],
       [0, 0, 1, 2, 3, 0, 0],
       [0, 0, 4, 5, 6, 0, 0],
       [0, 0, 7, 8, 9, 0, 0],
       [0, 0, 0, 0, 0, 0, 0]])
Coordinates:
  * y        (y) int64 40B -6 -5 -4 -3 -2
  * x        (x) int64 56B -2 -1 0 1 2 3 4
>>> da_padded.x
<xarray.DataArray 'x' (x: 7)> Size: 56B
array([-2, -1,  0,  1,  2,  3,  4])
Coordinates:
  * x        (x) int64 56B -2 -1 0 1 2 3 4
Attributes:
    pad_width:  2
>>> da_padded.y
<xarray.DataArray 'y' (y: 5)> Size: 40B
array([-6, -5, -4, -3, -2])
Coordinates:
  * y        (y) int64 40B -6 -5 -4 -3 -2
Attributes:
    pad_width:  1

Asymmetric padding

>>> da_padded = pad(da, x=(1, 4))
>>> da_padded
<xarray.DataArray (y: 3, x: 8)> Size: 192B
array([[0, 1, 2, 3, 0, 0, 0, 0],
       [0, 4, 5, 6, 0, 0, 0, 0],
       [0, 7, 8, 9, 0, 0, 0, 0]])
Coordinates:
  * y        (y) int64 24B -5 -4 -3
  * x        (x) int64 64B -1 0 1 2 3 4 5 6
>>> da_padded.x
<xarray.DataArray 'x' (x: 8)> Size: 64B
array([-1,  0,  1,  2,  3,  4,  5,  6])
Coordinates:
  * x        (x) int64 64B -1 0 1 2 3 4 5 6
Attributes:
    pad_width:  (1, 4)