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

Parse CF/UDUNITS-style powers of units #1134

Closed
jthielen opened this issue Aug 26, 2019 · 4 comments · Fixed by #1281
Closed

Parse CF/UDUNITS-style powers of units #1134

jthielen opened this issue Aug 26, 2019 · 4 comments · Fixed by #1281
Labels
Area: Units Pertains to unit information Type: Feature New functionality
Milestone

Comments

@jthielen
Copy link
Collaborator

jthielen commented Aug 26, 2019

I'm not sure if this is something to implement in MetPy or upstream in pint, but as I was working through some of the dataset helper functionality along with xarray and pint integration, I realized that some CF/UDUNITS-compliant units are not handled by pint, specifically powers of units in the default symbol notation. For example, the CF standard name table lists the canonical units for "geopotential" as "m2 s-2", which pint cannot parse.

I would think a fix could be as simple as parsing the unit string to recognize patterns like <unit><number> and inserting "**" (which is valid in both pint and UDUNITS) in between. I think the following regex should do the trick?

import re
from metpy.units import units

unit_str = 'kg m2 s-2'
unit_str_filtered = re.sub(r'(?<=[A-Za-z])(?![A-Za-z])(?<![0-9\-])(?=[0-9\-])',
                           '**', unit_str)
print(1 * units(unit_str_filtered))
1.0 kilogram * meter ** 2 / second ** 2
@jthielen jthielen added the Type: Feature New functionality label Aug 26, 2019
@dopplershift
Copy link
Member

My preference would be to see if pint is interested in the modification, but otherwise we'll need to support it.

@jthielen
Copy link
Collaborator Author

jthielen commented Dec 5, 2019

Now that hgrecco/pint#911 has been merged, this (and percent signs) can be supported by specifying something like the following:

units = pint.UnitRegistry(autoconvert_offset_to_baseunit=True,
                          preprocessors=[functools.partial(
                                         re.sub, (r'(?<=[A-Za-z])(?![A-Za-z])'
                                                  r'(?<![0-9\-][eE])(?<![0-9\-])(?=[0-9\-])'),
                                         '**'),
                                         lambda string: string.replace('%', 'percent')])

I'd guess that I should wait for a PR until the next pint release?

@dopplershift
Copy link
Member

I mean, the PR is welcome, but it won’t pass or get merged until there’s a pint release. Which as fast as I’m getting 0.12 out the door, may actually beat the next MetPy release... 🤣 😭

@dopplershift
Copy link
Member

For the record, MetPy 0.12 was out first, but about 3 hours.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Units Pertains to unit information Type: Feature New functionality
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants