-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Maximum value over a height coordinate (#1945)
* Adds in plugin and tests to calculate the maximum value over a height coordinate * formatting line length * remove print and update docstring * Move plugin to cube_manipulation * formatting * Formatting isort and flake8 * Update metadata of cube * Update checksums for new data * Raises an error if requested height bounds don't cover any height levels --------- Co-authored-by: Marcus Spelman <[email protected]>
- Loading branch information
1 parent
ac901c4
commit 8ff0b76
Showing
5 changed files
with
294 additions
and
0 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,69 @@ | ||
#!/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 calculate the maximum over the height coordinate""" | ||
|
||
from improver import cli | ||
|
||
|
||
@cli.clizefy | ||
@cli.with_output | ||
def process( | ||
cube: cli.inputcube, | ||
*, | ||
lower_height_bound: float = None, | ||
upper_height_bound: float = None, | ||
): | ||
"""Calculate the maximum value over the height coordinate of a cube. If height bounds are | ||
specified then the maximum value between these height levels is calculated. | ||
Args: | ||
cube (iris.cube.Cube): | ||
A cube with a height coordinate. | ||
lower_height_bound (float): | ||
The lower bound for the height coordinate. This is either a float or None if no lower | ||
bound is desired. Any specified bounds should have the same units as the height | ||
coordinate of cube. | ||
upper_height_bound (float): | ||
The upper bound for the height coordinate. This is either a float or None if no upper | ||
bound is desired. Any specified bounds should have the same units as the height | ||
coordinate of cube. | ||
Returns: | ||
A cube of the maximum value over the height coordinate or maximum value between the provided | ||
height bounds.""" | ||
|
||
from improver.utilities.cube_manipulation import maximum_in_height | ||
|
||
return maximum_in_height( | ||
cube, | ||
lower_height_bound=lower_height_bound, | ||
upper_height_bound=upper_height_bound, | ||
) |
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
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,73 @@ | ||
# -*- 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 the max_in_height 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_with_bounds(tmp_path): | ||
"""Test max_in_height computation with specified bounds""" | ||
|
||
kgo_dir = acc.kgo_root() / "max-in-height" | ||
input_path = kgo_dir / "input.nc" | ||
output_path = tmp_path / "output.nc" | ||
args = [ | ||
input_path, | ||
"--upper-height-bound", | ||
"3000", | ||
"--lower-height-bound", | ||
"500", | ||
"--output", | ||
f"{output_path}", | ||
] | ||
|
||
kgo_path = kgo_dir / "kgo_with_bounds.nc" | ||
run_cli(args) | ||
acc.compare(output_path, kgo_path) | ||
|
||
|
||
def test_without_bounds(tmp_path): | ||
"""Test max_in_height computation without bounds.""" | ||
|
||
kgo_dir = acc.kgo_root() / "max-in-height" | ||
input_path = kgo_dir / "input.nc" | ||
output_path = tmp_path / "output.nc" | ||
args = [input_path, "--output", f"{output_path}"] | ||
|
||
kgo_path = kgo_dir / "kgo_without_bounds.nc" | ||
run_cli(args) | ||
acc.compare(output_path, kgo_path) |
91 changes: 91 additions & 0 deletions
91
improver_tests/utilities/cube_manipulation/test_maximum_in_height.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,91 @@ | ||
# -*- 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. | ||
""" | ||
Unit tests for the function "cube_manipulation.maximum_in_height". | ||
""" | ||
|
||
import numpy as np | ||
import pytest | ||
from iris.cube import Cube | ||
|
||
from improver.synthetic_data.set_up_test_cubes import set_up_variable_cube | ||
from improver.utilities.cube_manipulation import maximum_in_height | ||
|
||
|
||
@pytest.fixture() | ||
def wet_bulb_temperature() -> Cube: | ||
"Generate a cube of wet bulb temperature on height levels" | ||
data = np.array( | ||
[ | ||
[[100, 200, 100], [100, 200, 100]], | ||
[[300, 400, 100], [300, 400, 100]], | ||
[[200, 300, 300], [200, 300, 300]], | ||
] | ||
) | ||
cube = set_up_variable_cube( | ||
data=data, name="wet_bulb_temperature", height_levels=[100, 200, 300] | ||
) | ||
return cube | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"lower_bound,upper_bound,expected", | ||
( | ||
(None, None, [300, 400, 300]), | ||
(None, 200, [300, 400, 100]), | ||
(250, None, [200, 300, 300]), | ||
(50, 1000, [300, 400, 300]), | ||
), | ||
) | ||
def test_maximum_in_height(lower_bound, upper_bound, expected, wet_bulb_temperature): | ||
"""Test that the maximum over the height coordinate is correctly calculated for | ||
different combinations of upper and lower bounds.""" | ||
|
||
result = maximum_in_height( | ||
wet_bulb_temperature, | ||
lower_height_bound=lower_bound, | ||
upper_height_bound=upper_bound, | ||
) | ||
|
||
assert np.allclose(result.data, [expected] * 2) | ||
assert "wet_bulb_temperature" == result.name() | ||
|
||
|
||
def test_height_bounds_error(wet_bulb_temperature): | ||
"""Test an error is raised if the input cube doesn't have any height levels | ||
between the height bounds.""" | ||
|
||
with pytest.raises( | ||
ValueError, match="any height levels between the provided bounds" | ||
): | ||
maximum_in_height( | ||
wet_bulb_temperature, lower_height_bound=50, upper_height_bound=75 | ||
) |