From 8637dcd366852cbe55e47595a7435d42dbdd4d8e Mon Sep 17 00:00:00 2001 From: daflack Date: Fri, 17 Nov 2023 15:56:37 +0000 Subject: [PATCH] Fix filter operator for filtering cube fixes #256 --- src/CSET/operators/filters.py | 4 +++- tests/operators/test_filters.py | 4 ++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/CSET/operators/filters.py b/src/CSET/operators/filters.py index 432fd46a7..48c12c9cd 100644 --- a/src/CSET/operators/filters.py +++ b/src/CSET/operators/filters.py @@ -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] diff --git a/tests/operators/test_filters.py b/tests/operators/test_filters.py index abd912fe9..41470653c 100644 --- a/tests/operators/test_filters.py +++ b/tests/operators/test_filters.py @@ -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 = "" @@ -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)