Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/CSET/loaders/spatial_difference_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ def load(conf: Config):
]:
base_model = models[0]
yield RawRecipe(
recipe=f"mlevel_spatial_difference_case_aggregation_mean_{atype}.yaml",
recipe=f"level_spatial_difference_case_aggregation_mean_{atype}.yaml",
variables={
"VARNAME": field,
"LEVELTYPE": "model_level_number",
Expand Down
48 changes: 38 additions & 10 deletions src/CSET/operators/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"""Operators to perform various kind of filtering."""

import logging
from typing import Literal

import iris
import iris.cube
Expand Down Expand Up @@ -70,8 +71,9 @@ def apply_mask(
def filter_cubes(
cube: iris.cube.Cube | iris.cube.CubeList,
constraint: iris.Constraint,
per_model: Literal[True] | Literal[False] = False,
**kwargs,
) -> iris.cube.Cube:
) -> iris.cube.Cube | iris.cube.CubeList:
"""Filter a CubeList down to a single Cube based on a constraint.

Arguments
Expand All @@ -80,28 +82,54 @@ def filter_cubes(
Cube(s) to filter
constraint: iris.Constraint
Constraint to extract
per_model: bool
If True, the filter is expected to return a CubeList with
one cube per model, otherwise a single cube is expected.

Returns
-------
iris.cube.Cube
iris.cube.Cube | iris.cube.CubeList

Raises
------
ValueError
If the constraint doesn't produce a single cube.
"""
model_names = set()

if per_model:
for c in iter_maybe(cube):
model_names.add(c.attributes.get("model_name", None))
if None in model_names:
raise ValueError(
"per_model mode can only be used if all cubes have a model_name attribute."
)

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 isinstance(filtered_cubes, iris.cube.CubeList) and len(filtered_cubes) == 1:
return filtered_cubes[0]
else:

if per_model:
if isinstance(filtered_cubes, iris.cube.Cube) and len(model_names) == 1:
return iris.cube.CubeList((filtered_cubes,))
if isinstance(filtered_cubes, iris.cube.CubeList) and len(
filtered_cubes
) == len(model_names):
return filtered_cubes
raise ValueError(
f"Constraint doesn't produce single cube. Constraint: {constraint}"
f"Constraint doesn't produce one cube per model. Constraint: {constraint}"
f"\nSource: {cube}\nResult: {filtered_cubes}"
)
else:
# 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 isinstance(filtered_cubes, iris.cube.CubeList) and len(filtered_cubes) == 1:
return filtered_cubes[0]
else:
raise ValueError(
f"Constraint doesn't produce single cube. Constraint: {constraint}"
f"\nSource: {cube}\nResult: {filtered_cubes}"
)


def filter_multiple_cubes(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ steps:
- operator: read.read_cubes
file_paths: $INPUT_PATHS
model_names: $MODEL_NAME
constraint:
operator: constraints.generate_var_constraint
varname: $VARNAME
subarea_type: $SUBAREA_TYPE
subarea_extent: $SUBAREA_EXTENT
- operator: filters.filter_cubes
constraint:
operator: constraints.combine_constraints
varname_constraint:
Expand All @@ -15,8 +21,6 @@ steps:
operator: constraints.generate_level_constraint
coordinate: $LEVELTYPE
levels: $LEVEL
subarea_type: $SUBAREA_TYPE
subarea_extent: $SUBAREA_EXTENT

- operator: collapse.collapse
coordinate: [grid_latitude, grid_longitude]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ steps:
- operator: read.read_cubes
file_paths: $INPUT_PATHS
model_names: $MODEL_NAME
constraint:
operator: constraints.generate_var_constraint
varname: $VARNAME
- operator: filters.filter_cubes
constraint:
operator: constraints.combine_constraints
varname_constraint:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ steps:
- operator: read.read_cubes
file_paths: $INPUT_PATHS
model_names: $MODEL_NAME
constraint:
operator: constraints.generate_var_constraint
varname: $VARNAME
- operator: filters.filter_cubes
constraint:
operator: constraints.combine_constraints
varname_constraint:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ steps:
- operator: read.read_cubes
file_paths: $INPUT_PATHS
model_names: $MODEL_NAME
constraint:
operator: constraints.generate_var_constraint
varname: $VARNAME
- operator: filters.filter_cubes
constraint:
operator: constraints.combine_constraints
varname_constraint:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ steps:
- operator: read.read_cubes
file_paths: $INPUT_PATHS
model_names: $MODEL_NAME
constraint:
operator: constraints.generate_var_constraint
varname: $VARNAME
- operator: filters.filter_cubes
constraint:
operator: constraints.combine_constraints
varname_constraint:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ steps:
- operator: read.read_cubes
file_paths: $INPUT_PATHS
model_names: $MODEL_NAME
constraint:
operator: constraints.generate_var_constraint
varname: $VARNAME
subarea_type: $SUBAREA_TYPE
subarea_extent: $SUBAREA_EXTENT
- operator: filters.filter_cubes
constraint:
operator: constraints.combine_constraints
variable_constraint:
Expand All @@ -18,8 +24,6 @@ steps:
operator: constraints.generate_level_constraint
coordinate: $LEVELTYPE
levels: "*"
subarea_type: $SUBAREA_TYPE
subarea_extent: $SUBAREA_EXTENT

- operator: collapse.collapse
coordinate: [grid_latitude, grid_longitude]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ steps:
- operator: read.read_cubes
file_paths: $INPUT_PATHS
model_names: $MODEL_NAME
constraint:
operator: constraints.generate_var_constraint
varname: $VARNAME
- operator: filters.filter_cubes
constraint:
operator: constraints.combine_constraints
variable_constraint:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ steps:
- operator: read.read_cubes
file_paths: $INPUT_PATHS
model_names: $MODEL_NAME
constraint:
operator: constraints.generate_var_constraint
varname: $VARNAME
subarea_type: $SUBAREA_TYPE
subarea_extent: $SUBAREA_EXTENT
- operator: filters.filter_cubes
constraint:
operator: constraints.combine_constraints
variable_constraint:
Expand All @@ -20,8 +26,6 @@ steps:
operator: constraints.generate_level_constraint
coordinate: $LEVELTYPE
levels: "*"
subarea_type: $SUBAREA_TYPE
subarea_extent: $SUBAREA_EXTENT

- operator: aggregate.ensure_aggregatable_across_cases

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ steps:
- operator: read.read_cubes
file_paths: $INPUT_PATHS
model_names: $MODEL_NAME
constraint:
operator: constraints.generate_var_constraint
varname: $VARNAME
subarea_type: $SUBAREA_TYPE
subarea_extent: $SUBAREA_EXTENT
- operator: filters.filter_cubes
constraint:
operator: constraints.combine_constraints
variable_constraint:
Expand All @@ -20,8 +26,6 @@ steps:
operator: constraints.generate_level_constraint
coordinate: $LEVELTYPE
levels: "*"
subarea_type: $SUBAREA_TYPE
subarea_extent: $SUBAREA_EXTENT

- operator: aggregate.ensure_aggregatable_across_cases

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ steps:
- operator: read.read_cubes
file_paths: $INPUT_PATHS
model_names: $MODEL_NAME
constraint:
operator: constraints.generate_var_constraint
varname: $VARNAME
subarea_type: $SUBAREA_TYPE
subarea_extent: $SUBAREA_EXTENT
- operator: filters.filter_cubes
constraint:
operator: constraints.combine_constraints
variable_constraint:
Expand All @@ -20,8 +26,6 @@ steps:
operator: constraints.generate_level_constraint
coordinate: $LEVELTYPE
levels: "*"
subarea_type: $SUBAREA_TYPE
subarea_extent: $SUBAREA_EXTENT

- operator: aggregate.ensure_aggregatable_across_cases

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ steps:
- operator: read.read_cubes
model_names: $MODEL_NAME
file_paths: $INPUT_PATHS
subarea_type: $SUBAREA_TYPE
subarea_extent: $SUBAREA_EXTENT
constraint:
operator: constraints.generate_var_constraint
varname: $VARNAME
- operator: filters.filter_cubes
constraint:
operator: constraints.combine_constraints
variable_constraint:
Expand All @@ -26,8 +32,6 @@ steps:
operator: constraints.generate_level_constraint
coordinate: $LEVELTYPE
levels: $LEVEL
subarea_type: $SUBAREA_TYPE
subarea_extent: $SUBAREA_EXTENT

- operator: plot.plot_histogram_series
sequence_coordinate: $SEQUENCE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ steps:
- operator: read.read_cubes
model_names: $MODEL_NAME
file_paths: $INPUT_PATHS
constraint:
operator: constraints.generate_var_constraint
varname: $VARNAME
subarea_type: $SUBAREA_TYPE
subarea_extent: $SUBAREA_EXTENT
- operator: filters.filter_cubes
constraint:
operator: constraints.combine_constraints
variable_constraint:
Expand All @@ -28,8 +34,6 @@ steps:
operator: constraints.generate_level_constraint
coordinate: $LEVELTYPE
levels: $LEVEL
subarea_type: $SUBAREA_TYPE
subarea_extent: $SUBAREA_EXTENT

- operator: aggregate.ensure_aggregatable_across_cases

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ steps:
- operator: read.read_cubes
file_paths: $INPUT_PATHS
model_names: $MODEL_NAME
constraint:
operator: constraints.generate_var_constraint
varname: $VARNAME
subarea_type: $SUBAREA_TYPE
subarea_extent: $SUBAREA_EXTENT
- operator: filters.filter_cubes
constraint:
operator: constraints.combine_constraints
variable_constraint:
Expand All @@ -28,8 +34,6 @@ steps:
operator: constraints.generate_level_constraint
coordinate: $LEVELTYPE
levels: $LEVEL
subarea_type: $SUBAREA_TYPE
subarea_extent: $SUBAREA_EXTENT

- operator: aggregate.ensure_aggregatable_across_cases

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ steps:
- operator: read.read_cubes
model_names: $MODEL_NAME
file_paths: $INPUT_PATHS
constraint:
operator: constraints.generate_var_constraint
varname: $VARNAME
subarea_type: $SUBAREA_TYPE
subarea_extent: $SUBAREA_EXTENT
- operator: filters.filter_cubes
constraint:
operator: constraints.combine_constraints
variable_constraint:
Expand All @@ -28,8 +34,6 @@ steps:
operator: constraints.generate_level_constraint
coordinate: $LEVELTYPE
levels: $LEVEL
subarea_type: $SUBAREA_TYPE
subarea_extent: $SUBAREA_EXTENT

- operator: aggregate.ensure_aggregatable_across_cases

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ steps:
- operator: read.read_cubes
file_paths: $INPUT_PATHS
model_names: $MODEL_NAME
constraint:
operator: constraints.generate_var_constraint
varname: $VARNAME
subarea_type: $SUBAREA_TYPE
subarea_extent: $SUBAREA_EXTENT
- operator: filters.filter_cubes
constraint:
operator: constraints.combine_constraints
variable_constraint:
Expand All @@ -28,8 +34,6 @@ steps:
operator: constraints.generate_level_constraint
coordinate: $LEVELTYPE
levels: $LEVEL
subarea_type: $SUBAREA_TYPE
subarea_extent: $SUBAREA_EXTENT

- operator: aggregate.ensure_aggregatable_across_cases

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ description: |
steps:
- operator: read.read_cube
file_paths: $INPUT_PATHS
constraint:
operator: constraints.generate_var_constraint
varname: $VARNAME
subarea_type: $SUBAREA_TYPE
subarea_extent: $SUBAREA_EXTENT
- operator: filters.filter_cubes
constraint:
operator: constraints.combine_constraints
variable_constraint:
Expand All @@ -15,8 +21,6 @@ steps:
operator: constraints.generate_level_constraint
coordinate: $LEVELTYPE
levels: $LEVEL
subarea_type: $SUBAREA_TYPE
subarea_extent: $SUBAREA_EXTENT

- operator: collapse.collapse
coordinate: [time]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ description: |
steps:
- operator: read.read_cubes
file_paths: $INPUT_PATHS
constraint:
operator: constraints.generate_var_constraint
varname: $VARNAME
- operator: filters.filter_cubes
constraint:
operator: constraints.combine_constraints
variable_constraint:
Expand Down
Loading