Units¶
See also
The xarray ecosystem supports unit-aware arrays using pint and pint-xarray. Some changes are required to make these packages work well with UDUNITS format recommended by the CF conventions.
cf_xarray
makes those recommended changes when you import cf_xarray.units
. These changes allow pint to parse and format UDUNIT units strings, and add several custom units like degrees_north
for latitude, psu
for ocean salinity, etc. Be aware that pint supports some units that UDUNITS does not recognize but cf-xarray
will not try to detect them and raise an error. For example, a temperature subtraction returns “delta_degC” units in pint, which does not exist in UDUNITS.
Formatting units¶
For now, only the short format using symbols is supported:
from pint import application_registry as ureg
import cf_xarray.units
u = ureg.Unit("m ** 3 / s ** 2")
f"{u:cf}" # or {u:~cf}, both return the same short format
'm3 s-2'