grid_process.units_conv#
Module to control, parse and convert units
Attributes#
Exceptions#
Class to raise units errors |
Classes#
Functions#
|
Convert units in a xarray.DataArray. |
|
Convert units in a xarray.Dataset. |
|
Checks that the input datasets have same data variables and units. If it is |
|
Check that the data variables in the datasets all have same single units. |
Module Contents#
- logger#
- class DaUnits(da: xarray.DataArray)[source]#
Xarray accessors class for units utility functions on DataArray
- class DsUnits(ds: xarray.Dataset)[source]#
Xarray accessors class for units utility functions on Dataset
- dataarray_accessor#
- dataset_accessor#
- ureg: pint.UnitRegistry#
- da_to(da: xarray.DataArray, dst_units: str) xarray.DataArray[source]#
Convert units in a xarray.DataArray.
- Parameters:
da (xarray.DataArray) – with attribute “units”
dst_units (str) – destination units
- Returns:
da_out – contains the data array with converted units
- Return type:
xarray.DataArray
- Raises:
pint.errors.DimensionalityError – if the conversion is not possible
- ds_to(ds: xarray.Dataset, dst_units: str | dict[collections.abc.Hashable, str]) xarray.Dataset[source]#
Convert units in a xarray.Dataset.
- Parameters:
ds (xarray.Dataset) – with variable attributes “units”
dst_units (str or dict) – destination units for each data variable to be converted. Variable names not specified in
dst_unitsare kept in the same units. If passed as a single string, all variables in the dataset are converted to the same units
- Returns:
ds_out – contains the dataset with converted units
- Return type:
xarray.Dataset
- Raises:
pint.errors.DimensionalityError – if the conversion is not possible
- consistency_check(datasets: collections.abc.Sequence[xarray.Dataset], check_vars: bool = True) dict[str, dict[str, str]][source]#
Checks that the input datasets have same data variables and units. If it is the case, returns the ‘units’ attributes of each data variable otherwise raises an error.
- Parameters:
datasets (sequence of xarray.Dataset) – each data variable in the datasets must have a ‘units’ attributes
check_vars (bool, optional) – if False, allows datasets to have different variables, but common ones must have same units. Default is True
- Returns:
out – contains the ‘units’ attribute of the data variables in the input datasets. In case same units are specified in various format, the dominant format is kept if there is one otherwise the format from the first dataset in the input sequence
- Return type:
dict
- Raises:
UnitsError – if same variables across the input datasets have different units
ValueError – when
check_vars=Trueand the input datasets do not have exact same set of variables
- units_check(datasets: collections.abc.Sequence[xarray.Dataset]) None[source]#
Check that the data variables in the datasets all have same single units. If not, it raises an error.
- Parameters:
datasets (sequence of xarray.Dataset) – each data variable in the datasets must have a ‘units’ attributes
- Raises:
UnitsError – if any variable in any of the input datasets have different units