gcpy.units
Contains methods for converting the units of data. Mainly used for model benchmarking purposes.
Functions
|
Creates a consistent unit string used in unit conversion routines. |
|
Ensures the units of two xarray DataArrays are the same. |
|
Converts a data array from kg to one of several target unit types. |
|
Converts data in an xarray DataArray from native units to target units. |
Checks whether the units of an xarray DataArray are mol/mol. |
- gcpy.units.adjust_units(units)[source]
Creates a consistent unit string used in unit conversion routines.
- Parameters:
units (
str) – Input unit string.- Returns:
adjusted_units – Output unit string, adjusted to a consistent value.
- Return type:
- Raises:
TypeError – If units is not of type str.
Notes
Unit list is incomplete – currently geared to units from common model diagnostics (e.g. kg/m2/s, kg, and variants).
Examples
>>> adjust_units("kg m-2 s-1") 'kg/m2/s' >>> adjust_units("kgC/m2/s") 'kgC/m2/s'
- gcpy.units.convert_kg_to_target_units(data_kg, target_units, kg_to_kgC)[source]
Converts a data array from kg to one of several target unit types.
- Parameters:
data_kg (
numpy.ndarray) – Input data array in units of kg.target_units (
str) – Name of the target units. Supported values include:'Tg','Tg C','Gg','Gg C','Mg','Mg C','kg','kg C','g','g C'.kg_to_kgC (
float) – Conversion factor from kg species to kg carbon.
- Returns:
data – Output data array converted to the units specified by
target_units.- Return type:
- Raises:
ValueError – If
target_unitsis not a supported unit string.
Notes
Only unit conversions corresponding to the GEOS-Chem benchmarks have been implemented. This is an internal routine intended to be called from
convert_units().Examples
>>> import numpy as np >>> data_kg = np.array([1.0e9, 2.0e9]) >>> convert_kg_to_target_units(data_kg, 'Tg', 1.0) array([1., 2.])
- gcpy.units.convert_units(dr, species_name, species_properties, target_units, interval=[2678400.0], area_m2=None, delta_p=None, box_height=None)[source]
Converts data in an xarray DataArray from native units to target units.
- Parameters:
dr (
xarray.DataArray) – Data to be converted from native units to target units.species_name (
str) – Name of the species corresponding to the data stored indr.species_properties (
dict) – Dictionary containing species properties such as molecular weights and other metadata for the given species.target_units (
str) – Units to which the data will be converted.interval (
listoffloat, optional) – Length of the averaging period in seconds. Default:[2678400.0]area_m2 (
xarray.DataArray, optional) – Surface area in square metres. Default:Nonedelta_p (
xarray.DataArray, optional) – Delta-pressure between top and bottom edges of grid box (dry air) in hPa. Default:Nonebox_height (
xarray.DataArray, optional) – Grid box height in metres. Default:None
- Returns:
dr_new – Data converted to
target_units. Identical todrexcept for the data values and theunitsattribute.- Return type:
- Raises:
ValueError – If
MW_gis not found inspecies_properties.ValueError – If conversion from
molmol-1dryis requested butarea_m2ordelta_pare not provided.ValueError – If a mass unit (containing
'g') is requested butMW_gisNone.ValueError – If
intervalhas length > 1 butdrhas no time dimension.ValueError – If the native units of
drare not supported.
Notes
Only unit conversions corresponding to common GEOS-Chem benchmark output have been implemented. When
molmol-1is present as the unit, dry air is assumed.Examples
>>> dr_converted = convert_units( ... dr, 'O3', species_props, 'Tg', ... interval=[2678400.0], area_m2=area ... )
- gcpy.units.check_units(ref_da, dev_da, enforce_units=True)[source]
Ensures the units of two xarray DataArrays are the same.
- Parameters:
ref_da (
xarray.DataArray) – First data array containing aunitsattribute.dev_da (
xarray.DataArray) – Second data array containing aunitsattribute.enforce_units (
bool, optional) – Whether to raise an AssertionError if ref and dev units do not match. Default:True
- Returns:
units_match –
Trueif the units ofref_daanddev_damatch,Falseotherwise.- Return type:
- Raises:
AssertionError – If
enforce_unitsisTrueand the units do not match.
Examples
>>> match = check_units(ref_da, dev_da, enforce_units=False) >>> print(match) True
- gcpy.units.data_unit_is_mol_per_mol(da)[source]
Checks whether the units of an xarray DataArray are mol/mol.
- Parameters:
da (
xarray.DataArray) – Data array containing aunitsattribute.- Returns:
is_molmol –
Trueif the units attribute ofdais a recognised mol/mol unit string,Falseotherwise.- Return type:
Notes
Recognised mol/mol unit strings are:
'mol mol-1 dry','mol/mol','mol mol-1'.Examples
>>> data_unit_is_mol_per_mol(da) True