Skip to content

Commit

Permalink
redefined psu
Browse files Browse the repository at this point in the history
  • Loading branch information
siligam committed Nov 12, 2024
1 parent aa51c32 commit aaa1f19
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
10 changes: 8 additions & 2 deletions src/pymorize/units.py
Original file line number Diff line number Diff line change
Expand Up @@ -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<symbol>\w+)")
Expand Down Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/test_dimensionless_units.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit aaa1f19

Please sign in to comment.