cogpy.utils.xarr

Module: xarr Status: REVIEW Last Updated: 2025-08-26 Author: Arash Shahidi, A.Shahidi@campus.lmu.de

Summary:

This module provides utility functions for working with xarray objects.

Functions:

spaced_sample_around_coord: Returns indices spaced around a center coordinate. drop_attrs: Removes attributes from an xarray DataArray except specified ones. save_xarray: Saves an xarray DataArray to disk with attributes in a JSON file. load_xarray: Loads an xarray DataArray from disk and restores attributes. reshape_dimension: Reshapes a dimension of an xarray DataArray into new dimensions. unstack: Unstacks a multi-indexed dimension in an xarray DataArray. coords_from_multitindex: Extracts coordinates from a multi-indexed xarray dimension. axis_dim_from_xarr: Maps between axis numbers and dimension names in xarray. roll_dim: Rolls the dimensions of an xarray DataArray. inject_fs: Decorator to inject the ‘fs’ attribute from xarray DataArray into a function.

Example:

Functions

axis_dim_from_xarr(x[, axis, dim])

Handle axis and dim arguments for xarray DataArray.

coords_from_multitindex(xarr_multiindex)

dim_dur_slice(time_vec, time_fraction, duration)

Return a slice of the time series based on fraction and duration.

dur_slice(xsig, dim, fraction, duration)

Returns a slice of the time series based on fraction and duration.

reshape_dimension(dataarray, dim, new_shape, ...)

A much quicker version of unstacking a DataArray than the built-in unstack method.

roll_dim(x, nroll)

Rolls the dimensions of an xarray.DataArray by nroll positions.

slice_around(it, nsample[, step])

spaced_sample_around_coord(xsig, dim, ...[, ...])

Returns indices spaced around a center coordinate.

unstack(dataarray, dim)

xarr_wrap(func_)

Decorator to wrap a function that takes numpy arrays as input and output and applies it to xarray.DataArray objects, retaining the original metadata.

xdim_subsample_around(xsig, dim, center, nsample)

Returns indices spaced around a center coordinate.