shift_and_combine#
- rojak.core.indexing.shift_and_combine(target_array, *, combine_func=<function <lambda>>, shift_dim='pressure_level', offset_start=1, offset_end=1, shift_fill=nan)[source]#
Combine each element along
shift_dimwith the elementsoffset_startbefore andoffset_endafter ittarget_arrayis shifted forward byoffset_startand backward byoffset_endalongshift_dim, and the two shifted copies are combined element-wise usingcombine_func. The result is that, at each position, the value atoffset_startbefore it is combined with the value atoffset_endafter it. The boundary positions which would have required data outside oftarget_array(and were therefore filled withshift_fill) are trimmed from the result.With the default
combine_func(logical OR), this is useful for e.g. dilating a boolean mask alongshift_dimso that a value is also marked True if either of its neighbours (at the given offsets) is True.- Parameters:
target_array (T) – Array to shift and combine
combine_func (Callable[[T, T], T]) – Function used to combine the two shifted copies of
target_array. Defaults to logical OR.shift_dim (str) – Coordinate/dimension to shift along. Defaults to
"pressure_level".offset_start (int) – Number of positions to look before each element. Must be non-negative. Defaults to
1.offset_end (int) – Number of positions to look after each element. Must be non-negative. Defaults to
1.shift_fill (Any) – Fill value used at the boundary introduced by shifting, before it is trimmed away. Defaults to
np.nan.
- Returns:
target_arraycombined with its shifted copies, with the invalid boundary positions removed- Raises:
ValueError – If
offset_startoroffset_endis negative, or ifshift_dimis not a coordinate oftarget_array- Return type:
T
>>> import xarray as xr >>> array = xr.DataArray([False, False, True, False, False], dims="x", coords={"x": [0, 1, 2, 3, 4]}) >>> shift_and_combine(array, shift_dim="x").values array([ True, False, True])