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

In cube.intersection, find split cells using a tolerant equality check #4220

Merged
merged 4 commits into from
Jul 6, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
12 changes: 5 additions & 7 deletions lib/iris/cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -3179,17 +3179,15 @@ def _intersect_modulus(
# and call the new bounds = the new points + the difference.
pre_wrap_delta = np.diff(coord.bounds[inside_indices])
post_wrap_delta = np.diff(bounds[inside_indices])
close_enough = np.allclose(pre_wrap_delta, post_wrap_delta)
if not close_enough:
split_cell_indices, _ = np.where(
pre_wrap_delta != post_wrap_delta
)

split_cell_indices, _ = np.where(
~np.isclose(pre_wrap_delta, post_wrap_delta)
)
if split_cell_indices.size:
indices = inside_indices[split_cell_indices]
cells = bounds[indices]
if maximum % modulus not in cells:
# Recalculate the extended minimum only if the output bounds
# do not span the requested (minumum, maximum) range. If
# do not span the requested (minimum, maximum) range. If
# they do span that range, this adjustment would give unexpected
# results (see #3391).
cells_delta = np.diff(coord.bounds[indices])
Expand Down
11 changes: 11 additions & 0 deletions lib/iris/tests/unit/cube/test_Cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -1817,6 +1817,17 @@ def test_numerical_tolerance(self):
self.assertAlmostEqual(result.coord("longitude").points[0], 28.5)
self.assertAlmostEqual(result.coord("longitude").points[-1], 67.5)

def test_numerical_tolerance_wrapped(self):
# test the tolerance on the coordinate value causes modulus wrapping
# where appropriate
cube = create_cube(0.5, 3600.5, bounds=True)
lons = cube.coord("longitude")
lons.points = lons.points / 10
lons.bounds = lons.bounds / 10
result = cube.intersection(longitude=(-60, 60))
self.assertAlmostEqual(result.coord("longitude").points[0], -60.05)
self.assertAlmostEqual(result.coord("longitude").points[-1], 60.05)
bjlittle marked this conversation as resolved.
Show resolved Hide resolved
bjlittle marked this conversation as resolved.
Show resolved Hide resolved


def unrolled_cube():
data = np.arange(5, dtype="f4")
Expand Down