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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Intersection bounds fix (replacement PR) #4059

Merged
merged 3 commits into from May 13, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/iris/src/whatsnew/3.0.2.rst
Expand Up @@ -21,6 +21,10 @@ This document explains the changes made to Iris for this release
#. `@jonseddon`_ handled a malformed ``um_stash_source`` CF variable attribute in
a netCDF file rather than raising a ``ValueError``. (:pull:`4035`)

#. `@rcomer`_ fixed :meth:`~iris.cube.Cube.intersection` for special cases
where one cell's bounds align with the requested maximum and minimum, as
reported in :issue:`3391`. (:pull:`4059`)

馃摎 **Documentation**

#. `@bjlittle`_ updated the ``intersphinx_mapping`` and fixed documentation
Expand Down
24 changes: 14 additions & 10 deletions lib/iris/cube.py
Expand Up @@ -3185,18 +3185,22 @@ def _intersect_modulus(
pre_wrap_delta != post_wrap_delta
)

# Recalculate the extended minimum.
indices = inside_indices[split_cell_indices]
cells = bounds[indices]
cells_delta = np.diff(coord.bounds[indices])

# Watch out for ascending/descending bounds
if cells_delta[0, 0] > 0:
cells[:, 0] = cells[:, 1] - cells_delta[:, 0]
minimum = np.min(cells[:, 0])
else:
cells[:, 1] = cells[:, 0] + cells_delta[:, 0]
minimum = np.min(cells[:, 1])
if maximum % modulus not in cells:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps a comment here, even referencing the exact problem that this code is aiming to solve.

# Recalculate the extended minimum only if the output bounds
# do not span the requested (minumum, maximum) range. If
# they do span that range, this adjustment would give unexpected
# results (see #3391).
cells_delta = np.diff(coord.bounds[indices])

# Watch out for ascending/descending bounds.
if cells_delta[0, 0] > 0:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously, minimum would have been set by either branch of this if statement on every call. Now that it is contained within if maximum % modulus not in cells it may not be. Should there be an else to the if on L3135?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's only been two months but I had completely forgotten the logic here! I think this is fine though: we don't explicitly set minimum here, so it is just left to whatever minimum was originally passed into the method. Note that because of the previous logic

maximum - modulus <= minimum (line 3081)
cells >= minimum (line 3098)

So the only way that maximum - modulus in cells will be True is if maximum - modulus == minimum and minimum is in cells. I.e. the very specific case described in #3391. In that case, it is expected that the bounds on the output coordinate will precisely span the requested (minimum, maximum) range.

Definitely needs a comment though!

cells[:, 0] = cells[:, 1] - cells_delta[:, 0]
minimum = np.min(cells[:, 0])
else:
cells[:, 1] = cells[:, 0] + cells_delta[:, 0]
minimum = np.min(cells[:, 1])

points = wrap_lons(coord.points, minimum, modulus)

Expand Down
8 changes: 8 additions & 0 deletions lib/iris/tests/unit/cube/test_Cube.py
Expand Up @@ -1731,6 +1731,14 @@ def test_aligned_exclusive(self):
self.assertEqual(result.data[0, 0, 0], 171)
self.assertEqual(result.data[0, 0, -1], 189)

def test_aligned_bounds_at_modulus(self):
cube = create_cube(-179.5, 180.5, bounds=True)
result = cube.intersection(longitude=(0, 360))
self.assertArrayEqual(result.coord("longitude").bounds[0], [0, 1])
self.assertArrayEqual(result.coord("longitude").bounds[-1], [359, 360])
self.assertEqual(result.data[0, 0, 0], 180)
self.assertEqual(result.data[0, 0, -1], 179)

def test_negative_misaligned_points_inside(self):
cube = create_cube(0, 360, bounds=True)
result = cube.intersection(longitude=(-10.25, 10.25))
Expand Down