Skip to content

Commit

Permalink
rename aggregate operator to collapse operator.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sylviabohnenstengel committed Jul 26, 2023
1 parent a259dc4 commit 9bd3018
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/CSET/operators/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"""This subpackage contains all of CSET's operators."""

# Import operators here so they are exported for use by recipes.
from CSET.operators import constraints, read, write, filters, aggregate, plot, misc
from CSET.operators import constraints, read, write, filters, collapse, plot, misc


from pathlib import Path
Expand Down
40 changes: 20 additions & 20 deletions src/CSET/operators/aggregate.py → src/CSET/operators/collapse.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,30 @@
# limitations under the License.

"""
Operators to perform various kind of aggregation on either 1 or 2 dimensions.
Operators to perform various kind of collapse on either 1 or 2 dimensions.
"""

import iris
import iris.cube
import iris.analysis


def aggregate_1dim(
def collapse_1dim(
cube: iris.cube.Cube, coordinate: str, method: str, **kwargs
) -> iris.cube.Cube:
"""
Aggregates similar (stash) fields in a cube into a cube aggregating around the specified coordinate and method.
This could be a (weighted) mean or an accumulation.
Collapses similar (stash) fields in a cube into a cube collapsing around the specified coordinate and method.
This could be a (weighted) mean or percentile.
Arguments
---------
cube: iris.cube.Cube
Cube to aggregate and iterate over one dimension
Cube to collapse and iterate over one dimension
coordinate: str
Coordinate to aggregate over i.e.
Coordinate to collapse over i.e.
'time', 'longitude', 'latitude','model_level_number'
method: str
Type of aggregation i.e. method: 'MEAN', 'MAX', 'MIN', 'MEDIAN', 'PERCENTILE'
Type of collapse i.e. method: 'MEAN', 'MAX', 'MIN', 'MEDIAN', 'PERCENTILE'
getattr creates iris.analysis.MEAN, etc
For PERCENTILE YAML file requires i.e.
method: 'PERCENTILE'
Expand All @@ -55,30 +55,30 @@ def aggregate_1dim(
"""

if method != "PERCENTILE":
aggregated_cube = cube.collapsed(coordinate, getattr(iris.analysis, method))
collapsed_cube = cube.collapsed(coordinate, getattr(iris.analysis, method))
if method == "PERCENTILE":
for num in kwargs.values():
aggregated_cube = cube.collapsed(
collapsed_cube = cube.collapsed(
coordinate, getattr(iris.analysis, method), percent=num
)

return aggregated_cube
return collapsed_cube


def aggregate_cube_2dim(
def collapse_cube_2dim(
cube: iris.cube.Cube, coordinate1: str, coordinate2: str, method: str, **kargs
) -> iris.cube.Cube:
"""
Aggregates similar (stash) fields in a cube into a 2D field in a cube based on 2 coordinates and method to aggregated. This could include time and vertical coordinate for instance.
Collapses similar (stash) fields in a cube into a 2D field in a cube based on 2 coordinates and method to collapsed. This could include time and vertical coordinate for instance.
This could be a (weighted) mean or an accumulation.
Arguments
---------
cube: iris.cube.Cube
Cube to aggregate and iterate over one dimension
Cube to collapse and iterate over one dimension
coordinate: str
Coordinate to aggregate over
method: Type of aggregation i.e. method: 'MEAN', 'MAX', 'MIN', 'MEDIAN', 'PERCENTILE'
Coordinate to collapse over
method: Type of collapse i.e. method: 'MEAN', 'MAX', 'MIN', 'MEDIAN', 'PERCENTILE'
getattr creates iris.analysis.MEAN, etc
For PERCENTILE YAML file requires i.e.
method: 'PERCENTILE'
Expand All @@ -88,7 +88,7 @@ def aggregate_cube_2dim(
Returns
-------
cube: iris.cube.Cube
Single variable but several methods of aggregation such as mean, median, max, min, percentile
Single variable but several methods of collapse such as mean, median, max, min, percentile
Raises
Expand All @@ -97,13 +97,13 @@ def aggregate_cube_2dim(
If the constraint doesn't produce a single cube containing a field.
"""

# aggregate cube over single dimension
# collapse cube over single dimension
# dimensions: 2 of i.e. 'time', 'longitude', 'latitude'
# method: iris.analysis.MEAN, iris.analysis.MAX, iris.analysis.MIN
# ensure that time is passed in as 'time'
# aggregated_cube = cube.collapsed("time", iris.analysis.MEAN)
aggregated_cube = cube.collapsed(
# collapsed_cube = cube.collapsed("time", iris.analysis.MEAN)
collapsed_cube = cube.collapsed(
[coordinate2, coordinate2], getattr(iris.analysis, method)
)

return aggregated_cube
return collapsed_cube
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@
from CSET.operators import read, filters, constraints


def test_aggregate_1dim():
def test_collapse_1dim():
"""Reduces dimension of cube."""
cubes = read.read_cubes("tests/test_data/air_temp.nc")
constraint = constraints.combine_constraints(
constraints.generate_stash_constraint("m01s03i236"),
a=constraints.generate_cell_methods_constraint([]),
)
cube = filters.filter_cubes(cubes, constraint)
aggregated_cube = cube.collapsed("time", iris.analysis.MEAN)
assert aggregated_cube.cell_methods == ()
collapsed_cube = cube.collapsed("time", iris.analysis.MEAN)
assert collapsed_cube.cell_methods == ()
expected_cube = "<iris 'Cube' of air_temperature / (K) (time: 1; grid_latitude: 17; grid_longitude: 13)>"
assert repr(aggregated_cube) == expected_cube
assert repr(collapsed_cube) == expected_cube
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ steps:
# time_start: 2022-09-21T03:00:00
# time_end: 2022-09-21T03:30:00

- operator: aggregate.aggregate_1dim
- operator: collapse.collapse_1dim
coordinate: 'time'
method: 'MEAN'
additional_percent: 90
Expand Down

0 comments on commit 9bd3018

Please sign in to comment.