my_code_base.core.xarray_utils

Classes

HistoryAccessor

A class for accessing and manipulating the 'history' attribute of an xarray object.

Functions

compress_xarray(→ xarray.Dataset | xarray.DataArray)

Compress xarray.Dataset or xarray.DataArray.

get_dim_index(→ int)

Get the index of a dimension in a DataArray.

Module Contents

class my_code_base.core.xarray_utils.HistoryAccessor(xr_obj)[source]

A class for accessing and manipulating the ‘history’ attribute of an xarray object.

Parameters:
xr_obj : xarray.Dataset or xarray.DataArray

The xarray object to be accessed.

add(msg)[source]

Add an entry to the history.

Parameters:
msg : str

The message to be added to the history.

Example

>>> da.history.add("New entry to history")
>>> da.attrs['history']
'...: New entry to history; '
my_code_base.core.xarray_utils.compress_xarray(data: xarray.Dataset | xarray.DataArray, complevel: int) xarray.Dataset | xarray.DataArray[source]

Compress xarray.Dataset or xarray.DataArray.

Parameters:
data : xarray.Dataset | xarray.DataArray

Data to compress.

complevel : int

Compression level.

Returns:

Compressed data.

Return type:

xr.Dataset | xr.DataArray

my_code_base.core.xarray_utils.get_dim_index(da: xarray.DataArray, dim: str) int[source]

Get the index of a dimension in a DataArray.

Deprecated since version 0.1.0: Will be removed in a future version. Use xarray.DataArray.get_axis_num() instead.

Parameters:
da : xarray.DataArray

DataArray to get the dimension index from.

dim : str

Dimension name.

Return type:

Index of the dimension in the DataArray. Can be used for specifying the axis in a numpy operation.

Examples

>>> da = xr.DataArray(np.random.rand(2, 3, 4), dims=['time', 'lat', 'lon'])
>>> get_dim_index(da, 'lat')
1