Skip to content

Commit

Permalink
Fix filter operator for filtering cube
Browse files Browse the repository at this point in the history
fixes #256
  • Loading branch information
daflack committed Nov 17, 2023
1 parent afab373 commit 8637dcd
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/CSET/operators/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ def filter_cubes(
If the constraint doesn't produce a single cube.
"""
filtered_cubes = cube.extract(constraint)

# Return directly if already a cube.
if isinstance(filtered_cubes, iris.cube.Cube):
return filtered_cubes
# Check filtered cubes is a CubeList containing one cube.
if len(filtered_cubes) == 1:
return filtered_cubes[0]
Expand Down
4 changes: 4 additions & 0 deletions tests/operators/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def test_filters_operator():
constraints.generate_stash_constraint("m01s03i236"),
a=constraints.generate_cell_methods_constraint([]),
)
# Test filtering a CubeList.
cube = filters.filter_cubes(cubes, constraint)
assert cube.cell_methods == ()
expected_cube = "<iris 'Cube' of air_temperature / (K) (time: 3; grid_latitude: 17; grid_longitude: 13)>"
Expand All @@ -34,3 +35,6 @@ def test_filters_operator():
constraint = constraints.generate_stash_constraint("m01s03i236")
with pytest.raises(ValueError):
cube = filters.filter_cubes(cubes, constraint)
# Test filtering a Cube.
single_cube = filters.filter_cubes(cube, constraint)
assert repr(cube) == repr(single_cube)

0 comments on commit 8637dcd

Please sign in to comment.