|
1 | 1 | """Unit tests for the :func:`esmvalcore.preprocessor.regrid.regrid`
|
2 | 2 | function."""
|
3 | 3 |
|
| 4 | +import logging |
4 | 5 | import unittest
|
5 | 6 | from unittest import mock
|
6 | 7 |
|
|
15 | 16 | from esmvalcore.preprocessor._regrid import (
|
16 | 17 | _CACHE,
|
17 | 18 | HORIZONTAL_SCHEMES,
|
| 19 | + _check_grid_discontiguities, |
18 | 20 | _horizontal_grid_is_close,
|
19 | 21 | _rechunk,
|
20 | 22 | )
|
21 | 23 |
|
22 | 24 |
|
23 | 25 | class Test(tests.Test):
|
| 26 | + |
24 | 27 | def _check(self, tgt_grid, scheme, spec=False):
|
25 | 28 | expected_scheme = HORIZONTAL_SCHEMES[scheme]
|
26 | 29 |
|
@@ -70,8 +73,8 @@ def setUp(self):
|
70 | 73 | )
|
71 | 74 | self.src_cube.ndim = 1
|
72 | 75 | self.tgt_grid_coord = mock.Mock()
|
73 |
| - self.tgt_grid = mock.Mock( |
74 |
| - spec=iris.cube.Cube, coord=self.tgt_grid_coord) |
| 76 | + self.tgt_grid = mock.Mock(spec=iris.cube.Cube, |
| 77 | + coord=self.tgt_grid_coord) |
75 | 78 | self.regrid_schemes = [
|
76 | 79 | 'linear', 'linear_extrapolate', 'nearest', 'area_weighted',
|
77 | 80 | 'unstructured_nearest'
|
@@ -111,8 +114,8 @@ def test_invalid_scheme__unknown(self):
|
111 | 114 | regrid(self.src_cube, self.src_cube, 'wibble')
|
112 | 115 |
|
113 | 116 | def test_horizontal_schemes(self):
|
114 |
| - self.assertEqual( |
115 |
| - set(HORIZONTAL_SCHEMES.keys()), set(self.regrid_schemes)) |
| 117 | + self.assertEqual(set(HORIZONTAL_SCHEMES.keys()), |
| 118 | + set(self.regrid_schemes)) |
116 | 119 |
|
117 | 120 | def test_regrid__horizontal_schemes(self):
|
118 | 121 | for scheme in self.regrid_schemes:
|
@@ -154,12 +157,12 @@ def test_regrid_generic_regridding(self):
|
154 | 157 | third_party_regridder = mock.Mock()
|
155 | 158 |
|
156 | 159 | def test_regrid_generic_third_party(self):
|
157 |
| - regrid(self.src_cube, '1x1', |
158 |
| - {"reference": |
159 |
| - "tests.unit.preprocessor._regrid.test_regrid:" |
| 160 | + regrid( |
| 161 | + self.src_cube, '1x1', { |
| 162 | + "reference": "tests.unit.preprocessor._regrid.test_regrid:" |
160 | 163 | "Test.third_party_regridder",
|
161 | 164 | "method": "good",
|
162 |
| - }) |
| 165 | + }) |
163 | 166 | self.third_party_regridder.assert_called_once_with(method="good")
|
164 | 167 |
|
165 | 168 |
|
@@ -266,6 +269,62 @@ def test_regrid_is_skipped_if_grids_are_the_same():
|
266 | 269 | assert expected_different_cube is not cube
|
267 | 270 |
|
268 | 271 |
|
| 272 | +def test_no_discontiguities_in_coords(): |
| 273 | + """Test that no mask is used if there are no discontinuities in coords.""" |
| 274 | + cube = _make_cube(lat=LAT_SPEC1, lon=LON_SPEC1) |
| 275 | + scheme = {} |
| 276 | + scheme = _check_grid_discontiguities(cube, scheme) |
| 277 | + assert scheme == {} |
| 278 | + |
| 279 | + |
| 280 | +def test_use_mask_if_discontiguities_in_coords(caplog): |
| 281 | + """Test use_src_mask is added to the scheme.""" |
| 282 | + lat_bounds = np.array( |
| 283 | + [[[-43.48076211, -34.01923789, -22.00961894, -31.47114317], |
| 284 | + [-34.01923789, -10.0, 2.00961894, -22.00961894], |
| 285 | + [-10.0, -0.53847577, 11.47114317, 2.00961894]], |
| 286 | + [[-31.47114317, -22.00961894, -10.0, -19.46152423], |
| 287 | + [-22.00961894, 2.00961894, 14.01923789, -10.0], |
| 288 | + [2.00961894, 11.47114317, 23.48076211, 14.01923789]]]) |
| 289 | + lat_coord = iris.coords.AuxCoord( |
| 290 | + [[-40.0, -20.0, 0.0], [-20.0, 0.0, 20.0]], |
| 291 | + var_name='lat', |
| 292 | + standard_name='latitude', |
| 293 | + units='degrees_north', |
| 294 | + bounds=lat_bounds, |
| 295 | + ) |
| 296 | + lon_bounds = np.array([[[140.625, 99.375, 99.375, 140.625], |
| 297 | + [99.375, 140.625, 140.625, 99.375], |
| 298 | + [140.625, 99.375, 99.375, 140.625]], |
| 299 | + [[140., 99.375, 99.375, 140.], |
| 300 | + [99.375, 140.625, 140.625, 99.375], |
| 301 | + [140., 99.375, 99.375, 140.]]]) |
| 302 | + lon_coord = iris.coords.AuxCoord( |
| 303 | + [[100.0, 140.0, 180.0], [80.0, 100.0, 120.0]], |
| 304 | + var_name='lon', |
| 305 | + standard_name='longitude', |
| 306 | + units='degrees_east', |
| 307 | + bounds=lon_bounds, |
| 308 | + ) |
| 309 | + data = np.ma.array( |
| 310 | + [[-40.0, -20.0, 0.0], [-20.0, 0.0, 20.0]], |
| 311 | + mask=[[True, False, True], [False, True, False]], |
| 312 | + ) |
| 313 | + cube = iris.cube.Cube( |
| 314 | + data, |
| 315 | + aux_coords_and_dims=[(lat_coord, (0, 1)), (lon_coord, (0, 1))], |
| 316 | + ) |
| 317 | + |
| 318 | + scheme = {} |
| 319 | + with caplog.at_level(logging.DEBUG): |
| 320 | + scheme = _check_grid_discontiguities(cube, scheme) |
| 321 | + assert scheme == {'use_src_mask': True} |
| 322 | + |
| 323 | + msg = ('Grid discontinuities were found in the source grid. ' |
| 324 | + 'Setting scheme argument `use_src_mask` to True.') |
| 325 | + assert msg in caplog.text |
| 326 | + |
| 327 | + |
269 | 328 | def test_rechunk_on_increased_grid():
|
270 | 329 | """Test that an increase in grid size rechunks."""
|
271 | 330 | with dask.config.set({'array.chunk-size': '128 M'}):
|
|
0 commit comments