DocOtak / gsw-xarray

Wrapper for gsw that will add CF attributes to xarray.DataArray outputs
https://gsw-xarray.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
23 stars 3 forks source link

pint does not parse well unit with exponents #63

Closed rcaneill closed 1 year ago

rcaneill commented 1 year ago

Some units consists of multiple set of parenthesis, with exponents -1 (e.g. next line). However they are not parsed correctly by pint. https://github.com/DocOtak/gsw-xarray/blob/08a13415e0993ae2b76991d956b569c239d842a6/gsw_xarray/_attributes.py#L651

import cf_xarray.units  # must be imported before pint_xarray
import pint_xarray
from pint_xarray import unit_registry as ureg

ureg('(kg/m^3)*(g/kg)^-1')

outputs (what we want):

1.0 kilogram2/(gram meter3)

but without the star

ureg('(kg/m^3)(g/kg)^-1')

output

1.0 meter3/gram

I opened an issue in pint about this: https://github.com/hgrecco/pint/issues/1730. In the meantime, we should replace these unit with either a '*' between the parenthesis, or with an extra set of parenthesis. Any opinion on which solution we should implement?

dcherian commented 1 year ago

cc @keewis @aulemahal

DocOtak commented 1 year ago

Maybe (kg/m^3)/(g/kg) and just avoid the exponent?

udunits2 thinks all these options are equal to 1000 m^-3 kg i.e. cancels the g/kg to scaling constant.

rcaneill commented 1 year ago

So I confirm that this is an issue from pint, that was already described (https://github.com/hgrecco/pint/issues/1485). The take home message for us is that we should not use parenthesis without the * sign outside of them.