Skip to content

Instantly share code, notes, and snippets.

@bblay
Last active June 4, 2025 12:09
Show Gist options
  • Save bblay/d0a3af8e353114ce4cf02635943a8fb1 to your computer and use it in GitHub Desktop.
Save bblay/d0a3af8e353114ce4cf02635943a8fb1 to your computer and use it in GitHub Desktop.
Iris time constraints, with and without bounds.
"""This example demonstrates a difference between a cell constraint and a point constraint."""
import iris
from iris.cube import Cube
from iris.coords import AuxCoord
# the required points arrive in a coord
wanted_points = AuxCoord([1, 2, 3])
# create the cube and constraints
cube = Cube([10, 20, 30, 40, 50], long_name='temperature', units='K',
aux_coords_and_dims=[(
AuxCoord([1, 2, 3, 4, 5], standard_name='time', units='days'), 0)])
print('cube', cube.shape)
cell_constraint = iris.Constraint(coord_values={'time': lambda cell: cell in wanted_points.cells()})
point_constraint = iris.Constraint(coord_values={'time': lambda cell: cell.point in wanted_points.cells()})
# both constraints work
print('cell constraint', cube.extract(cell_constraint).shape)
print('cell point constraint', cube.extract(point_constraint).shape)
# cell constraint fails when these bounds are added
print('\nadding bounds')
cube.coord('time').bounds = [[0, 2], [1, 3], [2, 4], [3, 5], [4, 6]]
print('cell constraint', cube.extract(cell_constraint)) # fails
print('cell point constraint', cube.extract(point_constraint).shape)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment