-
Notifications
You must be signed in to change notification settings - Fork 297
Closed
Labels
Description
🐛 Bug Report
#4059 aimed to fix cube.intersection
so if a cell's bounds align with the requested maximum and minimum, the new bounds exactly span that range. This does not work if the requested minimum is negative.
How To Reproduce
import iris
import iris.coords
import iris.cube
import numpy as np
lon = iris.coords.DimCoord(
np.arange(0.5, 360.5, 1), standard_name="longitude", units='degrees', circular=True)
lon.guess_bounds()
cube = iris.cube.Cube(range(360))
cube.add_dim_coord(lon, 0)
new_cube = cube.intersection(longitude=(-180, 180))
print(new_cube.coord("longitude")[0])
print(new_cube.coord("longitude")[-1])
Output:
DimCoord(array([-180.5]), bounds=array([[-181., -180.]]), standard_name='longitude', units=Unit('degrees'))
DimCoord(array([178.5]), bounds=array([[178., 179.]]), standard_name='longitude', units=Unit('degrees'))
Expected behaviour
Above code should produce:
DimCoord(array([-179.5]), bounds=array([[-180., -179.]]), standard_name='longitude', units=Unit('degrees'))
DimCoord(array([179.5]), bounds=array([[179., 180.]]), standard_name='longitude', units=Unit('degrees'))
Proposed fix
I think this line
Line 3190 in 274057b
if maximum % modulus not in cells: |
should have been
if maximum - modulus not in cells:
though I have not checked this fix.
Had it been written this way, I do not think it would have triggered the bug highlighted at #4220.
Environment
- OS & Version: RHEL 7.9
- Iris Version: 3.0.2