Skip to content
Merged
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
29 changes: 18 additions & 11 deletions src/CSET/operators/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,15 @@ def generate_mask(
The field(s) to be used for creating the mask.
condition: str
The type of condition applied, six available options:
'==','!=','<','<=','>', and '>='. The condition is consistent
'eq','ne','lt','le','gt', and 'ge'. The condition is consistent
regardless of whether mask_field is a cube or CubeList.
The conditions are as follows
eq: equal to,
ne: not equal to,
lt: less than,
le: less than or equal to,
gt: greater than,
ge: greater than or equal to.
value: float
The value on the right hand side of the condition. The value is
consistent regardless of whether mask_field is a cube or CubeList.
Expand All @@ -167,7 +174,7 @@ def generate_mask(

Raises
------
ValueError: Unexpected value for condition. Expected ==, !=, >, >=, <, <=.
ValueError: Unexpected value for condition. Expected eq, ne, gt, ge, lt, le.
Got {condition}.
Raised when condition is not supported.

Expand All @@ -184,28 +191,28 @@ def generate_mask(

Examples
--------
>>> land_mask = generate_mask(land_sea_mask,'==',1)
>>> land_mask = generate_mask(land_sea_mask,'gt',1)
"""
mask_list = iris.cube.CubeList([])
for cube in iter_maybe(mask_field):
mask = cube.copy()
mask.data[:] = 0.0
match condition:
case "==":
case "eq":
mask.data[cube.data == value] = 1
case "!=":
case "ne":
mask.data[cube.data != value] = 1
case ">":
case "gt":
mask.data[cube.data > value] = 1
case ">=":
case "ge":
mask.data[cube.data >= value] = 1
case "<":
case "lt":
mask.data[cube.data < value] = 1
case "<=":
case "le":
mask.data[cube.data <= value] = 1
case _:
raise ValueError("""Unexpected value for condition. Expected ==, !=,
>, >=, <, <=. Got {condition}.""")
raise ValueError("""Unexpected value for condition. Expected eq, ne,
gt, ge, lt, le. Got {condition}.""")
mask.attributes["mask"] = f"mask_for_{cube.name()}_{condition}_{value}"
mask.rename(f"mask_for_{cube.name()}_{condition}_{value}")
mask.units = "1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ steps:
subarea_extent: $SUBAREA_EXTENT

- operator: filters.generate_mask
condition: '<'
condition: 'lt'
value: 1.0

- operator: collapse.collapse
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ steps:
subarea_extent: $SUBAREA_EXTENT

- operator: filters.generate_mask
condition: '<'
condition: 'lt'
value: 1.0

- operator: misc.difference
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ steps:
subarea_extent: $SUBAREA_EXTENT

- operator: filters.generate_mask
condition: '<'
condition: 'lt'
value: 1.0

- operator: plot.spatial_pcolormesh_plot
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ steps:
units: 'metre'

- operator: filters.generate_mask
condition: '<'
condition: 'lt'
value: 50

- operator: collapse.collapse
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ steps:
units: 'metre'

- operator: filters.generate_mask
condition: '<'
condition: 'lt'
value: 50

- operator: misc.difference
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ steps:
units: 'metre'

- operator: filters.generate_mask
condition: '<'
condition: 'lt'
value: 50

- operator: plot.spatial_pcolormesh_plot
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ steps:
- operator: wind.convert_to_beaufort_scale

- operator: filters.generate_mask
condition: '>='
condition: 'ge'
value: 8.0

- operator: collapse.collapse
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ steps:
- operator: wind.convert_to_beaufort_scale

- operator: filters.generate_mask
condition: '>='
condition: 'ge'
value: 8.0

- operator: misc.difference
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ steps:
- operator: wind.convert_to_beaufort_scale

- operator: filters.generate_mask
condition: '>='
condition: 'ge'
value: 8.0

- operator: plot.spatial_pcolormesh_plot
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ steps:
- operator: wind.convert_to_beaufort_scale

- operator: filters.generate_mask
condition: '>='
condition: 'ge'
value: 12.0

- operator: collapse.collapse
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ steps:
- operator: wind.convert_to_beaufort_scale

- operator: filters.generate_mask
condition: '>='
condition: 'ge'
value: 12.0

- operator: misc.difference
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ steps:
- operator: wind.convert_to_beaufort_scale

- operator: filters.generate_mask
condition: '>='
condition: 'ge'
value: 12.0

- operator: plot.spatial_pcolormesh_plot
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ steps:
- operator: wind.convert_to_beaufort_scale

- operator: filters.generate_mask
condition: '>='
condition: 'ge'
value: 10.0

- operator: collapse.collapse
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ steps:
- operator: wind.convert_to_beaufort_scale

- operator: filters.generate_mask
condition: '>='
condition: 'ge'
value: 10.0

- operator: misc.difference
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ steps:
- operator: wind.convert_to_beaufort_scale

- operator: filters.generate_mask
condition: '>='
condition: 'ge'
value: 10.0

- operator: plot.spatial_pcolormesh_plot
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ steps:
subarea_extent: $SUBAREA_EXTENT

- operator: filters.generate_mask
condition: '<'
condition: 'lt'
value: 0.25

- operator: collapse.collapse
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ steps:
subarea_extent: $SUBAREA_EXTENT

- operator: filters.generate_mask
condition: '<'
condition: 'lt'
value: 0.25

- operator: misc.difference
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ steps:
subarea_extent: $SUBAREA_EXTENT

- operator: filters.generate_mask
condition: '<'
condition: 'lt'
value: 0.25

- operator: plot.spatial_pcolormesh_plot
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ steps:
subarea_extent: $SUBAREA_EXTENT

- operator: filters.generate_mask
condition: '>'
condition: 'gt'
value: 0.0

- operator: collapse.collapse
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ steps:
subarea_extent: $SUBAREA_EXTENT

- operator: filters.generate_mask
condition: '>'
condition: 'gt'
value: 0.0

- operator: misc.difference
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ steps:
subarea_extent: $SUBAREA_EXTENT

- operator: filters.generate_mask
condition: '>'
condition: 'gt'
value: 0.0

- operator: plot.spatial_pcolormesh_plot
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ steps:
subarea_extent: $SUBAREA_EXTENT

- operator: filters.generate_mask
condition: '>'
condition: 'gt'
value: 0.0

- operator: aggregate.rolling_window_time_aggregation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ steps:
subarea_extent: $SUBAREA_EXTENT

- operator: filters.generate_mask
condition: '>'
condition: 'gt'
value: 0.0

- operator: aggregate.rolling_window_time_aggregation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ steps:
subarea_extent: $SUBAREA_EXTENT

- operator: filters.generate_mask
condition: '>'
condition: 'gt'
value: 0.0

- operator: collapse.collapse
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ steps:
subarea_extent: $SUBAREA_EXTENT

- operator: filters.generate_mask
condition: '>'
condition: 'gt'
value: 0.0

- operator: misc.difference
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ steps:
subarea_extent: $SUBAREA_EXTENT

- operator: filters.generate_mask
condition: '>'
condition: 'gt'
value: 0.0

- operator: plot.spatial_pcolormesh_plot
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ steps:
subarea_extent: $SUBAREA_EXTENT

- operator: filters.generate_mask
condition: '<'
condition: 'lt'
value: 273.15

- operator: collapse.collapse
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ steps:
subarea_extent: $SUBAREA_EXTENT

- operator: filters.generate_mask
condition: '<'
condition: 'lt'
value: 273.15

- operator: misc.difference
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ steps:
subarea_extent: $SUBAREA_EXTENT

- operator: filters.generate_mask
condition: '<'
condition: 'lt'
value: 273.15

- operator: plot.spatial_pcolormesh_plot
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ steps:
subarea_extent: $SUBAREA_EXTENT

- operator: filters.generate_mask
condition: '<'
condition: 'lt'
value: 273.15

- operator: collapse.collapse
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ steps:
subarea_extent: $SUBAREA_EXTENT

- operator: filters.generate_mask
condition: '<'
condition: 'lt'
value: 273.15

- operator: misc.difference
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ steps:
subarea_extent: $SUBAREA_EXTENT

- operator: filters.generate_mask
condition: '<'
condition: 'lt'
value: 273.15

- operator: plot.spatial_pcolormesh_plot
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ steps:
subarea_type: $SUBAREA_TYPE
subarea_extent: $SUBAREA_EXTENT

- operator: filters.generate_mask
condition: 'gt'
value: 0.0

- operator: collapse.collapse
coordinate: [grid_latitude, grid_longitude]
method: MEAN
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ steps:
subarea_extent: $SUBAREA_EXTENT

- operator: filters.generate_mask
condition: '>'
condition: 'gt'
value: 0.0

- operator: misc.difference
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ steps:
subarea_extent: $SUBAREA_EXTENT

- operator: filters.generate_mask
condition: '>'
condition: 'gt'
value: 0.0

- operator: plot.spatial_pcolormesh_plot
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ steps:
subarea_extent: $SUBAREA_EXTENT

- operator: filters.generate_mask
condition: '<='
condition: 'le'
value: 0.1

- operator: collapse.collapse
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ steps:
subarea_extent: $SUBAREA_EXTENT

- operator: filters.generate_mask
condition: '<='
condition: 'le'
value: 0.1

- operator: misc.difference
Expand Down
Loading