-
Notifications
You must be signed in to change notification settings - Fork 300
Tests for rotate_grid_vectors #3148
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 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a173d0c
Tests for rotate_grid_vectors.
pp-mo d631237
Small fix to assertArrayAllClose for values near 0.
pp-mo 3de3a29
Small tweaks.
pp-mo b4e7b46
Fix test method.
pp-mo 273c6d3
Fix 1 test for 'equal_nans' no longer the default.
pp-mo 8d0aa42
Review changes.
pp-mo 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
148 changes: 148 additions & 0 deletions
148
lib/iris/tests/unit/analysis/cartography/test_rotate_grid_vectors.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,148 @@ | ||
| # (C) British Crown Copyright 2018, Met Office | ||
| # | ||
| # This file is part of Iris. | ||
| # | ||
| # Iris is free software: you can redistribute it and/or modify it under | ||
| # the terms of the GNU Lesser General Public License as published by the | ||
| # Free Software Foundation, either version 3 of the License, or | ||
| # (at your option) any later version. | ||
| # | ||
| # Iris is distributed in the hope that it will be useful, | ||
| # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| # GNU Lesser General Public License for more details. | ||
| # | ||
| # You should have received a copy of the GNU Lesser General Public License | ||
| # along with Iris. If not, see <http://www.gnu.org/licenses/>. | ||
| """ | ||
| Unit tests for the function | ||
| :func:`iris.analysis.cartography.rotate_grid_vectors`. | ||
|
|
||
| """ | ||
| from __future__ import (absolute_import, division, print_function) | ||
| from six.moves import (filter, input, map, range, zip) # noqa | ||
|
|
||
| # Import iris.tests first so that some things can be initialised before | ||
| # importing anything else. | ||
| import iris.tests as tests | ||
|
|
||
| from mock import Mock, call as mock_call | ||
| import numpy as np | ||
|
|
||
| from iris.cube import Cube | ||
|
|
||
| from iris.analysis.cartography import rotate_grid_vectors | ||
| from iris.tests.stock import sample_2d_latlons | ||
|
|
||
|
|
||
| class TestRotateGridVectors(tests.IrisTest): | ||
| def _check_angles_calculation(self, angles_in_degrees=True, | ||
| nan_angles_mask=None): | ||
| # Check basic maths on a 2d latlon grid. | ||
| u_cube = sample_2d_latlons(regional=True, transformed=True) | ||
| u_cube.units = 'ms-1' | ||
| u_cube.rename('dx') | ||
| u_cube.data[...] = 0 | ||
| v_cube = u_cube.copy() | ||
| v_cube.name('dy') | ||
|
|
||
| # Define 6 different vectors, repeated in each data row. | ||
| in_vu = np.array([(0, 1), (2, -1), (-1, -1), (-3, 1), (2, 0), (0, 0)]) | ||
| in_angs = np.rad2deg(np.arctan2(in_vu[..., 0], in_vu[..., 1])) | ||
| in_mags = np.sqrt(np.sum(in_vu * in_vu, axis=1)) | ||
| v_cube.data[...] = in_vu[..., 0] | ||
| u_cube.data[...] = in_vu[..., 1] | ||
|
|
||
| # Define 5 different test rotation angles, one for each data row. | ||
| rotation_angles = np.array([0., -45., 135, -140., 90.]) | ||
| ang_cube_data = np.broadcast_to(rotation_angles[:, None], | ||
| u_cube.shape) | ||
| ang_cube = u_cube.copy() | ||
| if angles_in_degrees: | ||
| ang_cube.units = 'degrees' | ||
| else: | ||
| ang_cube.units = 'radians' | ||
| ang_cube_data = np.deg2rad(ang_cube_data) | ||
| ang_cube.data[:] = ang_cube_data | ||
|
|
||
| if nan_angles_mask is not None: | ||
| ang_cube.data[nan_angles_mask] = np.nan | ||
|
|
||
| # Rotate all vectors by all the given angles. | ||
| result = rotate_grid_vectors(u_cube, v_cube, ang_cube) | ||
| out_u, out_v = [cube.data for cube in result] | ||
|
|
||
| # Check that vector magnitudes were unchanged. | ||
| out_mags = np.sqrt(out_u * out_u + out_v * out_v) | ||
| expect_mags = in_mags[None, :] | ||
| self.assertArrayAllClose(out_mags, expect_mags) | ||
|
|
||
| # Check that vector angles are all as expected. | ||
| out_angs = np.rad2deg(np.arctan2(out_v, out_u)) | ||
| expect_angs = in_angs[None, :] + rotation_angles[:, None] | ||
| ang_diffs = out_angs - expect_angs | ||
| # Fix for null vectors, and +/-360 differences. | ||
| ang_diffs[np.abs(out_mags) < 0.001] = 0.0 | ||
| ang_diffs = ang_diffs % 360.0 | ||
| # Check that any differences are very small. | ||
| self.assertArrayAllClose(ang_diffs, 0.0) | ||
|
|
||
| # Check that results are always masked arrays, masked at NaN angles. | ||
| self.assertTrue(np.ma.isMaskedArray(out_u)) | ||
| self.assertTrue(np.ma.isMaskedArray(out_v)) | ||
| if nan_angles_mask is not None: | ||
| self.assertArrayEqual(out_u.mask, nan_angles_mask) | ||
| self.assertArrayEqual(out_v.mask, nan_angles_mask) | ||
|
|
||
| def test_angles_calculation(self): | ||
| self._check_angles_calculation() | ||
|
|
||
| def test_angles_in_radians(self): | ||
| self._check_angles_calculation(angles_in_degrees=True) | ||
|
||
|
|
||
| def test_angles_from_grid(self): | ||
| # Check it will gets angles from 'u_cube', and pass any kwargs on to | ||
| # the angles routine. | ||
| u_cube = sample_2d_latlons(regional=True, transformed=True) | ||
| u_cube = u_cube[:2, :3] | ||
| u_cube.units = 'ms-1' | ||
| u_cube.rename('dx') | ||
| u_cube.data[...] = 1.0 | ||
| v_cube = u_cube.copy() | ||
| v_cube.name('dy') | ||
| v_cube.data[...] = 0.0 | ||
|
|
||
| # Setup a fake angles result from the inner call to 'gridcell_angles'. | ||
| angles_result_data = np.array([[0.0, 90.0, 180.0], | ||
| [-180.0, -90.0, 270.0]]) | ||
| angles_result_cube = Cube(angles_result_data, units='degrees') | ||
| angles_kwargs = {'this': 2} | ||
| angles_call_patch = self.patch( | ||
| 'iris.analysis._grid_angles.gridcell_angles', | ||
| Mock(return_value=angles_result_cube)) | ||
|
|
||
| # Call the routine. | ||
| result = rotate_grid_vectors(u_cube, v_cube, | ||
| grid_angles_kwargs=angles_kwargs) | ||
|
|
||
| self.assertEqual(angles_call_patch.call_args_list, | ||
| [mock_call(u_cube, this=2)]) | ||
|
|
||
| out_u, out_v = [cube.data for cube in result] | ||
| # Records what results should be for the various n*90deg rotations. | ||
| expect_u = np.array([[1.0, 0.0, -1.0], | ||
| [-1.0, 0.0, 0.0]]) | ||
| expect_v = np.array([[0.0, 1.0, 0.0], | ||
| [0.0, -1.0, -1.0]]) | ||
| # Check results are as expected. | ||
| self.assertArrayAllClose(out_u, expect_u) | ||
| self.assertArrayAllClose(out_v, expect_v) | ||
|
|
||
| def test_nan_vectors(self): | ||
| bad_angle_points = np.zeros((5, 6), dtype=bool) | ||
| bad_angle_points[2, 3] = True | ||
| self._check_angles_calculation(nan_angles_mask=bad_angle_points) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| tests.main() | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why the extra line on line 33?
Also surely alphabetically this should be
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The last bit is conventionally separate as it is the "Device Under Test"
But you're right, that the
sample_2d_latlonsneeds to be integrated with the rest of the 'supporting' imports. Will fix ..