-
Notifications
You must be signed in to change notification settings - Fork 89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Maximum value over a height coordinate #1945
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a6b8dc1
Adds in plugin and tests to calculate the maximum value over a height…
mspelman07 b6b95b4
formatting line length
mspelman07 0d6b3d0
remove print and update docstring
mspelman07 41557df
Move plugin to cube_manipulation
mspelman07 67e52a3
formatting
mspelman07 ee1ee4f
Formatting isort and flake8
mspelman07 7eaf42e
Update metadata of cube
mspelman07 74cb410
Update checksums for new data
mspelman07 087b10b
Raises an error if requested height bounds don't cover any height levels
mspelman07 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 | ||
) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it is possible for
cube_subsetted
to beNone
at this point if certain height bounds are input, which will cause anAttribute_Error
on line 750.I think handling of this issue should be added, with an error message explaining that no heights on the input cube are within the given bounds, so the max over height levels cannot be calculated. Tests should also be added for this error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are right this could cause a problem. I've added in an error message and a test for this.