Skip to content
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

Fix bounds after reversing coordinate values #1061

Merged
merged 2 commits into from
Apr 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions esmvalcore/cmor/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,16 @@ def _reverse_coord(self, coord):
if coord.ndim == 1:
self._cube = iris.util.reverse(self._cube,
self._cube.coord_dims(coord))
reversed_coord = self._cube.coord(var_name=coord.var_name)
if reversed_coord.has_bounds():
bounds = reversed_coord.bounds
right_bounds = bounds[:-2, 1]
left_bounds = bounds[1:-1, 0]
if np.all(right_bounds != left_bounds):
reversed_coord.bounds = np.fliplr(bounds)
coord = reversed_coord
self.report_debug_message(f'Coordinate {coord.var_name} values'
'have been reversed.')

def _check_coord_points(self, coord_info, coord, var_name):
"""Check coordinate points: values, bounds and monotonicity."""
Expand Down
22 changes: 22 additions & 0 deletions tests/unit/cmor/test_cmor_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,23 @@ def test_non_increasing(self):
self._update_coordinate_values(self.cube, coord, values)
self._check_fails_in_metadata()

def test_non_increasing_fix(self):
"""Check automatic fix for direction."""
coord = self.cube.coord('latitude')
values = np.linspace(
coord.points[-1],
coord.points[0],
len(coord.points)
)
self._update_coordinate_values(self.cube, coord, values)
self._check_cube(automatic_fixes=True)
self._check_cube()
# test bounds are contiguous
bounds = self.cube.coord('latitude').bounds
right_bounds = bounds[:-2, 1]
left_bounds = bounds[1:-1, 0]
self.assertTrue(np.all(left_bounds == right_bounds))

def test_non_decreasing(self):
"""Fail in metadata if decreasing coordinate is increasing."""
self.var_info.coordinates['lat'].stored_direction = 'decreasing'
Expand All @@ -639,6 +656,11 @@ def test_non_decreasing_fix(self):
for index in range(20):
self.assertTrue(
iris.util.approx_equal(cube_points[index], reference[index]))
# test bounds are contiguous
bounds = self.cube.coord('latitude').bounds
right_bounds = bounds[:-2, 1]
left_bounds = bounds[1:-1, 0]
self.assertTrue(np.all(left_bounds == right_bounds))

def test_not_bounds(self):
"""Warning if bounds are not available."""
Expand Down