-
Notifications
You must be signed in to change notification settings - Fork 41
Add unit support to cf-xarray #197
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
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
4d852c3
Port MetPy's custom Pint registry to cf-xarray
jthielen c7c0e9b
Add pint to test envs
dcherian 23a9bec
import functools
keewis ad42f46
Add CI env without optional deps
dcherian e9699b9
typo
dcherian d4a7bae
fix order of imports
dcherian 0c773e1
Updates based on review feedback
jthielen 3c9225c
black/isort run
jthielen d112483
Update cf_xarray/units.py with suggestion
jthielen 0567326
Update cf_xarray/tests/test_units.py
dcherian 4205064
Merge branch 'main' into add-pint-registry
keewis 82d9a52
linting
keewis 22c7b89
ignore flake8 unused import errors
keewis af01b74
add whats-new
dcherian 0daca00
Merge remote-tracking branch 'upstream/main' into add-pint-registry
dcherian File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
r"""Tests the operation of cf_xarray's ported unit support code. | ||
|
||
Reused with modification from MetPy under the terms of the BSD 3-Clause License. | ||
Copyright (c) 2017 MetPy Developers. | ||
""" | ||
|
||
import pytest | ||
|
||
pytest.importorskip("pint") | ||
|
||
from ..units import units | ||
|
||
|
||
def test_added_degrees_units(): | ||
"""Test that our added degrees units are present in the registry.""" | ||
# Test equivalence of abbreviations/aliases to our defined names | ||
assert str(units("degrees_N").units) == "degrees_north" | ||
assert str(units("degreesN").units) == "degrees_north" | ||
assert str(units("degree_north").units) == "degrees_north" | ||
assert str(units("degree_N").units) == "degrees_north" | ||
assert str(units("degreeN").units) == "degrees_north" | ||
assert str(units("degrees_E").units) == "degrees_east" | ||
assert str(units("degreesE").units) == "degrees_east" | ||
assert str(units("degree_east").units) == "degrees_east" | ||
assert str(units("degree_E").units) == "degrees_east" | ||
assert str(units("degreeE").units) == "degrees_east" | ||
|
||
# Test equivalence of our defined units to base units | ||
assert units("degrees_north") == units("degrees") | ||
assert units("degrees_north").to_base_units().units == units.radian | ||
assert units("degrees_east") == units("degrees") | ||
assert units("degrees_east").to_base_units().units == units.radian | ||
|
||
|
||
def test_gpm_unit(): | ||
"""Test that the gpm unit does alias to meters.""" | ||
x = 1 * units("gpm") | ||
assert str(x.units) == "meter" | ||
|
||
|
||
def test_psu_unit(): | ||
"""Test that the psu unit are present in the registry.""" | ||
x = 1 * units("psu") | ||
assert str(x.units) == "practical_salinity_unit" | ||
|
||
|
||
def test_percent_units(): | ||
"""Test that percent sign units are properly parsed and interpreted.""" | ||
assert str(units("%").units) == "percent" | ||
|
||
|
||
@pytest.mark.xfail(reason="not supported by pint, yet: hgrecco/pint#1295") | ||
def test_udunits_power_syntax(): | ||
"""Test that UDUNITS style powers are properly parsed and interpreted.""" | ||
assert units("m2 s-2").units == units.m ** 2 / units.s ** 2 | ||
dcherian marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
def test_udunits_power_syntax_parse_units(): | ||
"""Test that UDUNITS style powers are properly parsed and interpreted.""" | ||
assert units.parse_units("m2 s-2") == units.m ** 2 / units.s ** 2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
r"""Module to provide unit support via pint approximating UDUNITS/CF. | ||
|
||
Reused with modification from MetPy under the terms of the BSD 3-Clause License. | ||
dcherian marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Copyright (c) 2015,2017,2019 MetPy Developers. | ||
""" | ||
import functools | ||
import re | ||
keewis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
import warnings | ||
|
||
import pint | ||
from pint import ( # noqa: F401 | ||
DimensionalityError, | ||
UndefinedUnitError, | ||
UnitStrippedWarning, | ||
) | ||
|
||
# Create registry, with preprocessors for UDUNITS-style powers (m2 s-2) and percent signs | ||
units = pint.UnitRegistry( | ||
dcherian marked this conversation as resolved.
Show resolved
Hide resolved
|
||
autoconvert_offset_to_baseunit=True, | ||
preprocessors=[ | ||
functools.partial( | ||
re.compile( | ||
r"(?<=[A-Za-z])(?![A-Za-z])(?<![0-9\-][eE])(?<![0-9\-])(?=[0-9\-])" | ||
).sub, | ||
"**", | ||
), | ||
lambda string: string.replace("%", "percent"), | ||
], | ||
force_ndarray_like=True, | ||
) | ||
|
||
units.define( | ||
pint.unit.UnitDefinition("percent", "%", (), pint.converters.ScaleConverter(0.01)) | ||
) | ||
|
||
# Define commonly encoutered units (both CF and non-CF) not defined by pint | ||
units.define( | ||
"degrees_north = degree = degrees_N = degreesN = degree_north = degree_N = degreeN" | ||
) | ||
units.define( | ||
"degrees_east = degree = degrees_E = degreesE = degree_east = degree_E = degreeE" | ||
) | ||
units.define("@alias meter = gpm") | ||
units.define("practical_salinity_unit = [] = psu") | ||
|
||
# Enable pint's built-in matplotlib support | ||
try: | ||
units.setup_matplotlib() | ||
except ImportError: | ||
warnings.warn( | ||
"Import(s) unavailable to set up matplotlib support...skipping this portion " | ||
"of the setup." | ||
) | ||
|
||
# Set as application registry | ||
pint.set_application_registry(units) | ||
|
||
del pint |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
name: cf_xarray_test | ||
channels: | ||
- conda-forge | ||
dependencies: | ||
- pytest-cov | ||
- pytest | ||
- pytest-xdist | ||
- dask | ||
- matplotlib-base | ||
- netcdf4 | ||
- pandas | ||
- pooch | ||
- xarray |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,5 +9,6 @@ dependencies: | |
- matplotlib-base | ||
- netcdf4 | ||
- pandas | ||
- pint | ||
- pooch | ||
- xarray |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.