From ccf159e2d2de7d94a75796f692fe937aac6b8d48 Mon Sep 17 00:00:00 2001 From: James Frost Date: Tue, 6 Aug 2024 17:57:30 +0100 Subject: [PATCH] Allow Point cell methods for empty constraint This allows UM and LFRic recipes to be the same, so we can remove the LFRic specific ones. This is done by treating the "point" cell method as being equivalent to an empty set of cell methods, as point indicates no aggregation has occurred. Fixes #777 --- src/CSET/operators/constraints.py | 16 ++++++++++++---- tests/operators/test_constraints.py | 9 ++++++++- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/CSET/operators/constraints.py b/src/CSET/operators/constraints.py index 92de1c5e8..da95731a9 100644 --- a/src/CSET/operators/constraints.py +++ b/src/CSET/operators/constraints.py @@ -118,7 +118,7 @@ def generate_cell_methods_constraint(cell_methods: list, **kwargs) -> iris.Const """Generate constraint from cell methods. Operator that takes a list of cell methods and generates a constraint from - that. + that. Use [] to specify non-aggregated data. Arguments --------- @@ -129,11 +129,19 @@ def generate_cell_methods_constraint(cell_methods: list, **kwargs) -> iris.Const ------- cell_method_constraint: iris.Constraint """ + if len(cell_methods) == 0: - def check_cell_methods(cube: iris.cube.Cube): - return cube.cell_methods == tuple(cell_methods) + def check_no_aggregation(cube: iris.cube.Cube) -> bool: + """Check that any cell methods are "point", meaning no aggregation.""" + return set(cm.method for cm in cube.cell_methods) <= {"point"} - cell_methods_constraint = iris.Constraint(cube_func=check_cell_methods) + cell_methods_constraint = iris.Constraint(cube_func=check_no_aggregation) + else: + + def check_cell_methods(cube: iris.cube.Cube) -> bool: + return cube.cell_methods == tuple(cell_methods) + + cell_methods_constraint = iris.Constraint(cube_func=check_cell_methods) return cell_methods_constraint diff --git a/tests/operators/test_constraints.py b/tests/operators/test_constraints.py index d9026a3ed..e9ecf47be 100644 --- a/tests/operators/test_constraints.py +++ b/tests/operators/test_constraints.py @@ -42,11 +42,18 @@ def test_generate_var_constraint_stash(): def test_generate_cell_methods_constraint(): """Generate iris cube constraint for cell methods.""" - cell_methods_constraint = constraints.generate_cell_methods_constraint([]) + cell_methods_constraint = constraints.generate_cell_methods_constraint(["mean"]) expected_cell_methods_constraint = "Constraint(cube_func=.check_cell_methods at" assert expected_cell_methods_constraint in repr(cell_methods_constraint) +def test_generate_cell_methods_constraint_no_aggregation(): + """Generate iris cube constraint for no aggregation cell methods.""" + cell_methods_constraint = constraints.generate_cell_methods_constraint([]) + expected_cell_methods_constraint = "Constraint(cube_func=.check_no_aggregation at" + assert expected_cell_methods_constraint in repr(cell_methods_constraint) + + def test_generate_time_constraint(): """Generate iris cube constraint for dates.""" # Try with str dates