diff --git a/src/pymorize/units.py b/src/pymorize/units.py index e34bffba..f231901c 100644 --- a/src/pymorize/units.py +++ b/src/pymorize/units.py @@ -29,6 +29,8 @@ ureg = pint_xarray.unit_registry +ureg.define("practical_salinity_unit = g/kg = psu = PSU") + def handle_chemicals( s: Union[str, None] = None, pattern: Pattern = re.compile(r"mol(?P\w+)") @@ -114,9 +116,13 @@ def handle_unit_conversion(da: xr.DataArray, rule: Rule) -> xr.DataArray: else: _to_unit = to_unit if _to_unit == to_unit: - logger.info(f"Converting units: ({da.name} -> {cmor_variable}) {from_unit} -> {to_unit}") + logger.info( + f"Converting units: ({da.name} -> {cmor_variable}) {from_unit} -> {to_unit}" + ) else: - logger.info(f"Converting units: ({da.name} -> {cmor_variable}) {from_unit} -> {_to_unit} ({to_unit})") + logger.info( + f"Converting units: ({da.name} -> {cmor_variable}) {from_unit} -> {_to_unit} ({to_unit})" + ) new_da = new_da.pint.to(_to_unit).pint.dequantify() if new_da.attrs.get("units") != to_unit: logger.debug( diff --git a/tests/unit/test_dimensionless_units.py b/tests/unit/test_dimensionless_units.py index 18bc26c6..07516232 100644 --- a/tests/unit/test_dimensionless_units.py +++ b/tests/unit/test_dimensionless_units.py @@ -28,6 +28,6 @@ def test_units_with_psu(): new_da = handle_unit_conversion(da, rule) assert new_da.attrs.get("units") == "0.001" # Check the magnitude of the data - # 1 g/kg is approximately 1 psu, so the values should be 10 * 1000 + # 1 g/kg is approximately 1 psu, so the values should be 10 # after conversion: - assert np.allclose(new_da.values, 10000) + assert np.allclose(new_da.values, 10)