Skip to content

Commit

Permalink
fix intersection out of bounds point
Browse files Browse the repository at this point in the history
  • Loading branch information
rcomer committed Mar 10, 2021
1 parent 4885e12 commit 9862a22
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
21 changes: 11 additions & 10 deletions lib/iris/cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -3130,18 +3130,19 @@ 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:
# Recalculate the extended minimum.
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])

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

Expand Down
8 changes: 8 additions & 0 deletions lib/iris/tests/unit/cube/test_Cube.py
Original file line number Diff line number Diff line change
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

0 comments on commit 9862a22

Please sign in to comment.