xoa.grid.map_along_dim#
- xoa.grid.map_along_dim(func, da, dim, *coords, out_dim=None, out_size=None, dtype=None, **kwargs)[source]#
Apply a function along a dimension that is kept in place
Contrary to
xarray.apply_ufunc(), the working dimension is not moved to the end, so the arrays are not transposed and copied: the function can view them as(nouter, n, ninner)withxoa.core.num.broadcast_along_axis().The coordinates, like the input and output coordinates of an interpolation, may have any subset of the dimensions of the data array, plus other ones, which are put first in the output. Those that don’t have the working dimension get a size of 1 along it. Dask arrays are processed with
dask.array.map_blocks(): only the working dimension must be a single chunk, and the other chunks are kept.- Parameters:
func (callable) – Function called as
func(data, *coords, axis=axis, **kwargs)with numpy arrays that have all the dimensions of the output, in the same order, with a size of 1 for those they don’t have. It returns an array of the shape of the output.da (xarray.DataArray) – Data array
dim (str) – Working dimension
coords (xarray.DataArray) – Other arrays, which may have dim or out_dim, or none of them like the latitudes for a vertical dimension
out_dim (str, None) – Output dimension, which defaults to dim
out_size (int, None) – Output size along out_dim, which defaults to the size of the coordinates along out_dim if any, else to the size of da along dim
dtype (None, numpy.dtype) – Output data type, which defaults to the type of da if it is a float, else to float64
kwargs (dict) – Extra keywords are passed to func
- Returns:
xarray.DataArray – Array with the dimensions of da, dim being replaced by out_dim, and the other dimensions of the coordinates first. Its coordinates are those of da that don’t have dim, and its name and attributes are those of da.
Example
In [1]: def double(data, axis): ...: return np.repeat(data, 2, axis=axis) ...: In [2]: da = xr.DataArray(np.ones((3, 4, 5)), dims=("time", "z", "x")) In [3]: map_along_dim(double, da, "z", out_size=8).dims Out[3]: ('time', 'z', 'x')