-
Notifications
You must be signed in to change notification settings - Fork 40
ENH: Check units in xarray and iris interfaces. #134
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
Open
DWesl
wants to merge
6
commits into
ajdawson:main
Choose a base branch
from
DWesl:check-units
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
58e59e6
ENH: Check units in xarray and iris interfaces.
DWesl e8d94d5
TST: Add tests for the unit-handling in iris and xarray interfaces.
DWesl 1dfc816
STY: Run isort and black on test module.
DWesl 627f987
FIX,TST: Include actual data in the units tests.
DWesl 3bab94a
BUG: Use cf_units's build-in unknown-unit checker
DWesl b1b28e9
STY: Use cf_units API for checking unknown units
DWesl 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,109 @@ | ||
| """Test the unit handling of the iris and xarray interfaces.""" | ||
|
|
||
| import numpy as np | ||
| import pytest | ||
|
|
||
| try: | ||
| import iris.coords | ||
| import iris.cube | ||
|
|
||
| import windspharm.iris | ||
| except ImportError: | ||
| iris = None | ||
|
|
||
| try: | ||
| import xarray as xr | ||
|
|
||
| import windspharm.xarray | ||
| except ImportError: | ||
| xr = None | ||
|
|
||
|
|
||
| def create_data(package: str, wind_units: str): | ||
| """Create data for use by test functions.""" | ||
| data = np.zeros((7, 12), dtype="f4") | ||
| data[[1, -1], :] = 1 | ||
| data[[2, -2], :] = np.sqrt(3) | ||
| data[3, :] = 2 | ||
| lats = np.arange(-90, 91, 30, dtype="f4") | ||
| lons = np.arange(0, 360, 30, dtype="f4") | ||
| if package == "iris": | ||
| result = iris.cube.Cube( | ||
| data, | ||
| units=wind_units, | ||
| dim_coords_and_dims=( | ||
| (iris.coords.DimCoord(lats, "latitude", units="degrees_north"), 0), | ||
| ( | ||
| iris.coords.DimCoord( | ||
| lons, "longitude", units="degrees_east", circular=True | ||
| ), | ||
| 1, | ||
| ), | ||
| ), | ||
| ) | ||
| elif package == "xarray": | ||
| result = xr.DataArray( | ||
| data, | ||
| dims=("latitude", "longitude"), | ||
| coords={ | ||
| "latitude": (("latitude",), lats, {"units": "degrees_north"}), | ||
| "longitude": (("longitude",), lons, {"units": "degrees_easth"}), | ||
| }, | ||
| attrs={"units": wind_units}, | ||
| ) | ||
| return result | ||
|
|
||
|
|
||
| @pytest.mark.skipif("iris is None") | ||
| @pytest.mark.parametrize("units", ["knots", "miles per hour"]) | ||
| def test_iris_convert_units(units): | ||
| """Test that iris will convert speed units to m/s.""" | ||
| cube = create_data("iris", units) | ||
| vec_wind = windspharm.iris.VectorWind(cube, cube) | ||
| assert np.any(vec_wind.u() != cube) | ||
| assert np.any(vec_wind.v() != cube) | ||
| assert np.all(np.around(vec_wind.u().data[3, :]) == 1) | ||
|
|
||
|
|
||
| @pytest.mark.skipif("xr is None") | ||
| @pytest.mark.parametrize("units", ["knots", "mph"]) | ||
| def test_xr_warns_units(units): | ||
| """Test that XArray warns for non-m/s wind units.""" | ||
| data = create_data("xarray", units) | ||
| with pytest.warns(UserWarning): | ||
| vec_wind = windspharm.xarray.VectorWind(data, data) | ||
|
|
||
|
|
||
| @pytest.mark.skipif("xr is None") | ||
| @pytest.mark.parametrize("units", ["m/s", "m / s", "m s**-1", "m s^-1", "m s ** -1"]) | ||
| @pytest.mark.filterwarnings("error:Winds should have units of m/s") | ||
| def test_xr_unit_recognition(units): | ||
| """Test that XArray doesn't warn for different spellings of m/s.""" | ||
| data = create_data("xarray", units) | ||
| vec_wind = windspharm.xarray.VectorWind(data, data) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "package", | ||
| [ | ||
| pytest.param("iris", marks=pytest.mark.skipif("iris is None")), | ||
| pytest.param("xarray", marks=pytest.mark.skipif("xr is None")), | ||
| ], | ||
| ) | ||
| @pytest.mark.parametrize("units", ["K", "mg/kg"]) | ||
| def test_gradient_units(package, units): | ||
| """Test the units of the gradient. | ||
|
|
||
| They should be the units of the input per meter. | ||
| """ | ||
| scalar_data = create_data(package, units) | ||
| wind_data = create_data(package, "m/s") | ||
| vec_wind = getattr(windspharm, package).VectorWind(wind_data, wind_data) | ||
| grad_components = vec_wind.gradient(scalar_data) | ||
| for component in grad_components: | ||
| if package == "iris": | ||
| new_units = component.units | ||
| else: | ||
| new_units = component.attrs["units"] | ||
| assert new_units != units | ||
| assert new_units == "{:s} / m".format(units) |
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.