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

add pretty formatting for units #297

Open
dcherian opened this issue Jan 21, 2022 · 2 comments
Open

add pretty formatting for units #297

dcherian opened this issue Jan 21, 2022 · 2 comments

Comments

@dcherian
Copy link
Contributor

Continuing from #284 (comment) (cc @keewis)

I see pint has a "pretty" formatting mode. Is there a way we can define the "pretty" string?

u = units.Unit("degrees_east")
f"{u:~P}"
degrees_east

EDIT: I see even pint doesn't do the pretty ° for "degrees"

u = units.Unit("degrees")
f"{u:P}"
degree

EDIT2: Should we define a Pcf formatter for our own pretty strings?

@keewis
Copy link
Contributor

keewis commented Jan 21, 2022

I see even pint doesn't do the pretty ° for "degrees"

right, I was under the impression that it would do that for "~P", but that yields "deg", where for temperature units that would be e.g. "°C". If we look at the entry in default_en.txt, it's pretty obvious why:

degree = π / 180 * radian = deg = arcdeg = arcdegree = angular_degree

I think we have two options: use a different name (e.g. Pcf) or a new modifier. I don't think there is a way to add new modifiers at the moment, though, so that would need some time / work to implement in pint.

@aulemahal
Copy link
Contributor

aulemahal commented Mar 17, 2022

Would it make sense to use : ~cf as the pretty / short version of cf and cf as the normal version?

An issue I see is that, some might find dangerous that we put °C in the units attribute of netCDFs. So, I could suggest:

from cf_xarray.units import units
u = units.Unit('degC')

f"{u:cf}" -> "degrees_Celsius"
f"{u:~cf}" -> "°C"

And similarly with latitude and longitude. (AFAIU, we only need to remove the ~ in the current code so that this proposition works).

Moreover, if hgrecco/pint#1486 is solved this way, we could also control how dimensionless units are printed out:

u = units.Unit("")

f"{u:cf}" -> "1"
f"{u:~cf}" -> ""

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