Skip to content

Commit

Permalink
Merge pull request #272 from MetOffice/271-extent-cset-operator-const…
Browse files Browse the repository at this point in the history
…raint-model-level

Model level constraint operator
  • Loading branch information
jfrost-mo authored Nov 23, 2023
2 parents 2af64b8 + 24dba3b commit b9a2133
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/CSET/operators/constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"""Operators to generate constraints to filter with."""

from datetime import datetime
from typing import Union

import iris
import iris.cube
Expand Down Expand Up @@ -62,6 +63,32 @@ def generate_var_constraint(varname: str, **kwargs) -> iris.Constraint:
return varname_constraint


def generate_model_level_constraint(
model_level_number: Union[int, str], **kwargs
) -> iris.Constraint:
"""Generate constraint for a particular model level number.
Operator that takes a CF compliant model_level_number string, and uses iris to
generate a constraint to be passed into the read operator to minimize the
CubeList the read operator loads and speed up loading.
Arguments
---------
model_level_number: str
CF compliant model level number.
Returns
-------
model_level_number_constraint: iris.Constraint
"""
# Cast to int in case a string is given.
model_level_number = int(model_level_number)
model_level_number_constraint = iris.Constraint(
model_level_number=model_level_number
)
return model_level_number_constraint


def generate_cell_methods_constraint(cell_methods: list, **kwargs) -> iris.Constraint:
"""Generate constraint from cell methods.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
title: Meaned Model Level Air Temperature Spatial Plot
description: |
Extracts out the instantaneous model level air temperature from a file and writes it
to a new one.
steps:
- operator: read.read_cubes
constraint:
operator: constraints.generate_var_constraint
varname: air_temperature

- operator: filters.filter_cubes
constraint:
operator: constraints.combine_constraints
varname_constraint:
operator: constraints.generate_var_constraint
varname: air_temperature
model_level_number_constraint:
operator: constraints.generate_model_level_constraint
model_level_number: 2
cell_methods_constraint:
operator: constraints.generate_cell_methods_constraint
cell_methods: []

- operator: collapse.collapse
coordinate: time
method: MEAN

- operator: plot.spatial_contour_plot
file_path: CSET_OUTPUT_PATH

- operator: write.write_cube_to_nc
file_path: CSET_OUTPUT_PATH
9 changes: 9 additions & 0 deletions tests/operators/test_constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ def test_generate_var_constraint():
assert repr(var_constraint) == expected_var_constraint


def test_generate_model_level_constraint():
"""Generate iris cube constraint for model level number."""
var_constraint = constraints.generate_model_level_constraint("2")
expected_model_level_constraint = (
"Constraint(coord_values={'model_level_number': 2})"
)
assert repr(var_constraint) == expected_model_level_constraint


def test_generate_cell_methods_constraint():
"""Generate iris cube constraint for cell methods."""
cell_methods_constraint = constraints.generate_cell_methods_constraint([])
Expand Down

0 comments on commit b9a2133

Please sign in to comment.