Skip to content

Commit

Permalink
Merge pull request #778 from MetOffice/777_unify_cell_methods
Browse files Browse the repository at this point in the history
Allow Point cell methods for empty constraint, making it possible to unify UM and LFRic recipes
  • Loading branch information
jfrost-mo authored Aug 15, 2024
2 parents cd485e2 + ccf159e commit 8cb0515
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
16 changes: 12 additions & 4 deletions src/CSET/operators/constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
---------
Expand All @@ -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


Expand Down
9 changes: 8 additions & 1 deletion tests/operators/test_constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -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=<function generate_cell_methods_constraint.<locals>.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=<function generate_cell_methods_constraint.<locals>.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
Expand Down

0 comments on commit 8cb0515

Please sign in to comment.