Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rewrite units for plotting #156

Open
dcherian opened this issue Feb 3, 2021 · 3 comments
Open

rewrite units for plotting #156

dcherian opened this issue Feb 3, 2021 · 3 comments

Comments

@dcherian
Copy link
Contributor

dcherian commented Feb 3, 2021

It would be nice if we could rewrite the units attribute when using .cf.plot() so units = "kg/m3" becomes units = "kg/m³". There must be some convenient way of doing this, maybe with pint?

cc @keewis @jthielen

@jthielen
Copy link
Contributor

jthielen commented Feb 3, 2021

Yes, this could definitely be done with Pint (see the formatting options here: https://pint.readthedocs.io/en/stable/tutorial.html#string-formatting)! Though, in order to interpret CF/UDUNITS-style exponents, a custom preprocessor (like that in MetPy) is needed, which connects back to the larger issue of xarray-contrib/pint-xarray#26.

@dcherian
Copy link
Contributor Author

dcherian commented Feb 3, 2021

Thanks.

Another option is cf_units

import cf_units

for string in ["degreeN", "m2/s4", "kg/m3", "degC"]:
    print(cf_units.Unit(string).format(cf_units.UT_UTF8))

which looks good except for the reversed order for kg/m³

°
m²·s⁻⁴
m⁻³·kg
°C

@aulemahal
Copy link
Contributor

One argument against cf_units is that it is not pip-installable. It requires the UDUNITS-2 library which is not on pypi. Works well with conda, but potential headaches otherwise!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants