-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add aggregation functionality * Handle case where std_dev is undefined * all dimensions to be a string * update cli and acceptance tests * Undo testing change * formatting * reverse testing change * update * change parameter name * fix docstring * Check dimension exists * Refactor to use collapse-realizations cli * Remove old file * Handle nans * fix bug * Remove check * simplify code * Check that realization is a dimension in cli * fix docstring * fix docstring * Add test for invalid method error * Add acceptance test for when new name not specified * formatting * update docstring * update docstring * update docstring * Use pytest * simplify code * formatting * update checksum * Remove argument. --------- Co-authored-by: Belinda Trotta <[email protected]> Co-authored-by: Gavin Evans <[email protected]>
- Loading branch information
1 parent
fdc96a4
commit 2520f58
Showing
5 changed files
with
267 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
# ----------------------------------------------------------------------------- | ||
# (C) British Crown copyright. The Met Office. | ||
# All rights reserved. | ||
# | ||
# Redistribution and use in source and binary forms, with or without | ||
# modification, are permitted provided that the following conditions are met: | ||
# | ||
# * Redistributions of source code must retain the above copyright notice, this | ||
# list of conditions and the following disclaimer. | ||
# | ||
# * Redistributions in binary form must reproduce the above copyright notice, | ||
# this list of conditions and the following disclaimer in the documentation | ||
# and/or other materials provided with the distribution. | ||
# | ||
# * Neither the name of the copyright holder nor the names of its | ||
# contributors may be used to endorse or promote products derived from | ||
# this software without specific prior written permission. | ||
# | ||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
# POSSIBILITY OF SUCH DAMAGE. | ||
"""Script to collapse the realizations dimension of a cube.""" | ||
|
||
|
||
from improver import cli | ||
|
||
|
||
@cli.clizefy | ||
@cli.with_output | ||
def process( | ||
cube: cli.inputcube, *, method: str = "mean", new_name: str = None, | ||
): | ||
"""Collapse the realization dimension of a cube. | ||
Args: | ||
cube (iris.cube.Cube): | ||
Cube to be collapsed. | ||
method (str): | ||
One of "sum", "mean", "median", "std_dev", "min", "max". | ||
new_name (str): | ||
New name for output cube; if None use iris default. | ||
Returns: | ||
iris.cube.Cube: | ||
Collapsed cube. Dimensions are the same as input cube, | ||
without realization dimension. | ||
Raises: | ||
ValueError: if realization is not a dimension coordinate. | ||
""" | ||
|
||
from improver.utilities.cube_manipulation import collapse_realizations | ||
|
||
if not (cube.coords("realization", dim_coords=True)): | ||
raise ValueError("realization must be a dimension coordinate.") | ||
|
||
collapsed_cube = collapse_realizations(cube, method=method) | ||
if new_name: | ||
collapsed_cube.rename(new_name) | ||
|
||
return collapsed_cube |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
improver_tests/acceptance/test_collapse_realizations.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
# -*- coding: utf-8 -*- | ||
# ----------------------------------------------------------------------------- | ||
# (C) British Crown copyright. The Met Office. | ||
# All rights reserved. | ||
# | ||
# Redistribution and use in source and binary forms, with or without | ||
# modification, are permitted provided that the following conditions are met: | ||
# | ||
# * Redistributions of source code must retain the above copyright notice, this | ||
# list of conditions and the following disclaimer. | ||
# | ||
# * Redistributions in binary form must reproduce the above copyright notice, | ||
# this list of conditions and the following disclaimer in the documentation | ||
# and/or other materials provided with the distribution. | ||
# | ||
# * Neither the name of the copyright holder nor the names of its | ||
# contributors may be used to endorse or promote products derived from | ||
# this software without specific prior written permission. | ||
# | ||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
# POSSIBILITY OF SUCH DAMAGE. | ||
"""Tests for the collapse-realizations CLI""" | ||
|
||
|
||
import pytest | ||
|
||
from . import acceptance as acc | ||
|
||
pytestmark = [pytest.mark.acc, acc.skip_if_kgo_missing] | ||
CLI = acc.cli_name_with_dashes(__file__) | ||
run_cli = acc.run_cli(CLI) | ||
|
||
|
||
def test_basic(tmp_path): | ||
""" | ||
Test mean aggregation. | ||
""" | ||
|
||
kgo_dir = acc.kgo_root() / "collapse-realizations" | ||
kgo_path = kgo_dir / "kgo_mean.nc" | ||
input_path = kgo_dir / "input.nc" | ||
output_path = tmp_path / "output.nc" | ||
args = [ | ||
input_path, | ||
"--method", | ||
"mean", | ||
"--new-name", | ||
"ensemble_mean_of_air_temperature", | ||
"--output", | ||
output_path, | ||
] | ||
run_cli(args) | ||
acc.compare(output_path, kgo_path) | ||
|
||
|
||
def test_no_rename(tmp_path): | ||
""" | ||
Test mean aggregation with new-name unspecified. | ||
""" | ||
|
||
kgo_dir = acc.kgo_root() / "collapse-realizations" | ||
kgo_path = kgo_dir / "kgo_no_rename.nc" | ||
input_path = kgo_dir / "input.nc" | ||
output_path = tmp_path / "output.nc" | ||
args = [ | ||
input_path, | ||
"--method", | ||
"mean", | ||
"--output", | ||
output_path, | ||
] | ||
run_cli(args) | ||
acc.compare(output_path, kgo_path) | ||
|
||
|
||
def test_no_realization_coord(tmp_path): | ||
""" | ||
Test that an error is raised if there is no realization dimension. | ||
""" | ||
|
||
kgo_dir = acc.kgo_root() / "collapse-realizations" | ||
input_path = kgo_dir / "input_no_realization.nc" | ||
output_path = tmp_path / "output.nc" | ||
args = [ | ||
input_path, | ||
"--method", | ||
"mean", | ||
"--new-name", | ||
"ensemble_mean_of_air_temperature", | ||
"--output", | ||
output_path, | ||
] | ||
with pytest.raises(ValueError): | ||
run_cli(args) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters