Skip to content

Intersection bounds when requested minimum is negative #4221

@rcomer

Description

@rcomer

🐛 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

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

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions