-
Notifications
You must be signed in to change notification settings - Fork 313
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
Changes from 2 commits
a173d0c
d631237
3de3a29
b4e7b46
273c6d3
8d0aa42
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,150 @@ | ||
| # (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.gridcell_angles`. | ||
|
|
||
| """ | ||
| 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 | ||
| from mock import call as mock_call | ||
| import numpy as np | ||
|
|
||
| from iris.cube import Cube | ||
| from iris.coords import AuxCoord | ||
|
|
||
| from iris.analysis.cartography import rotate_grid_vectors | ||
| from iris.tests.stock import sample_2d_latlons | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why the extra line on line 33?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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" |
||
|
|
||
|
|
||
| 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) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test is called
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Whoops! |
||
|
|
||
| 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 | ||
|
pp-mo marked this conversation as resolved.
Outdated
|
||
| self._check_angles_calculation(nan_angles_mask=bad_angle_points) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| tests.main() | ||
Uh oh!
There was an error while loading. Please reload this page.