-
Notifications
You must be signed in to change notification settings - Fork 300
Normalise units of coordinate bounds #5746
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
7 commits
Select commit
Hold shift + click to select a range
eb6f58e
Normalise coordinate bounds units
bjlittle b9b7c29
fix mock tests
bjlittle bca0313
add normalise unit tests
bjlittle c618276
extend build aux/dim coords unit tests with normalisation
bjlittle 80e4194
support build aux coord with rollaxis
bjlittle 8858f4f
add whatsnew entry
bjlittle 35a629f
review actions
bjlittle 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
102 changes: 102 additions & 0 deletions
102
lib/iris/tests/unit/fileformats/nc_load_rules/helpers/test__normalise_bounds_units.py
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,102 @@ | ||
| # Copyright Iris contributors | ||
| # | ||
| # This file is part of Iris and is released under the BSD license. | ||
| # See LICENSE in the root of the repository for full licensing details. | ||
| """Test function :func:`iris.fileformats._nc_load_rules.helpers._normalise_bounds_units`.""" | ||
|
|
||
| # import iris tests first so that some things can be initialised before | ||
| # importing anything else | ||
| from typing import Optional | ||
| from unittest import mock | ||
|
|
||
| import numpy as np | ||
| import pytest | ||
|
|
||
| from iris.exceptions import IrisCfLoadWarning | ||
| from iris.fileformats._nc_load_rules.helpers import ( | ||
| _normalise_bounds_units, | ||
| _WarnComboIgnoringCfLoad, | ||
| ) | ||
|
|
||
| BOUNDS = mock.sentinel.bounds | ||
| CF_NAME = "dummy_bnds" | ||
|
|
||
|
|
||
| def _make_cf_bounds_var( | ||
| units: Optional[str] = None, | ||
| unitless: bool = False, | ||
| ) -> mock.MagicMock: | ||
| """Construct a mock CF bounds variable.""" | ||
| if units is None: | ||
| units = "days since 1970-01-01" | ||
|
|
||
| cf_data = mock.Mock(spec=[]) | ||
| # we want to mock the absence of flag attributes to helpers.get_attr_units | ||
| # see https://docs.python.org/3/library/unittest.mock.html#deleting-attributes | ||
| del cf_data.flag_values | ||
| del cf_data.flag_masks | ||
| del cf_data.flag_meanings | ||
|
|
||
| cf_var = mock.MagicMock( | ||
| cf_name=CF_NAME, | ||
| cf_data=cf_data, | ||
| units=units, | ||
| calendar=None, | ||
| dtype=float, | ||
| ) | ||
|
|
||
| if unitless: | ||
| del cf_var.units | ||
|
|
||
| return cf_var | ||
|
|
||
|
|
||
| def test_unitless() -> None: | ||
| """Test bounds variable with no units.""" | ||
| cf_bounds_var = _make_cf_bounds_var(unitless=True) | ||
| result = _normalise_bounds_units(None, cf_bounds_var, BOUNDS) | ||
| assert result == BOUNDS | ||
|
|
||
|
|
||
| def test_invalid_units__pass_through() -> None: | ||
| """Test bounds variable with invalid units.""" | ||
| units = "invalid" | ||
| cf_bounds_var = _make_cf_bounds_var(units=units) | ||
| wmsg = f"Ignoring invalid units {units!r} on netCDF variable {CF_NAME!r}" | ||
| with pytest.warns(_WarnComboIgnoringCfLoad, match=wmsg): | ||
| result = _normalise_bounds_units(None, cf_bounds_var, BOUNDS) | ||
| assert result == BOUNDS | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("units", ["unknown", "no_unit", "1", "kelvin"]) | ||
| def test_ignore_bounds(units) -> None: | ||
| """Test bounds variable with incompatible units compared to points.""" | ||
| points_units = "km" | ||
| cf_bounds_var = _make_cf_bounds_var(units=units) | ||
| wmsg = ( | ||
| f"Ignoring bounds on NetCDF variable {CF_NAME!r}. " | ||
| f"Expected units compatible with {points_units!r}" | ||
| ) | ||
| with pytest.warns(IrisCfLoadWarning, match=wmsg): | ||
| result = _normalise_bounds_units(points_units, cf_bounds_var, BOUNDS) | ||
| assert result is None | ||
|
|
||
|
|
||
| def test_compatible() -> None: | ||
| """Test bounds variable with compatible units requiring conversion.""" | ||
| points_units, bounds_units = "days since 1970-01-01", "hours since 1970-01-01" | ||
| cf_bounds_var = _make_cf_bounds_var(units=bounds_units) | ||
| bounds = np.arange(10, dtype=float) * 24 | ||
| result = _normalise_bounds_units(points_units, cf_bounds_var, bounds) | ||
| expected = bounds / 24 | ||
| np.testing.assert_array_equal(result, expected) | ||
|
|
||
|
|
||
| def test_same_units() -> None: | ||
| """Test bounds variable with same units as points.""" | ||
| units = "days since 1970-01-01" | ||
| cf_bounds_var = _make_cf_bounds_var(units=units) | ||
| bounds = np.arange(10, dtype=float) | ||
| result = _normalise_bounds_units(units, cf_bounds_var, bounds) | ||
| np.testing.assert_array_equal(result, bounds) | ||
| assert result is bounds |
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
Oops, something went wrong.
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.