Skip to content

Commit

Permalink
Review changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
bayliffe committed May 23, 2024
1 parent f96cba7 commit b9c944e
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 126 deletions.
44 changes: 9 additions & 35 deletions improver/spotdata/spot_manipulation.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,7 @@
# -*- coding: utf-8 -*-
# -----------------------------------------------------------------------------
# (C) British Crown copyright. The Met Office.
# All rights reserved.
# (C) Crown copyright, 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.
# This file is part of IMPROVER and is released under a BSD 3-Clause license.
# See LICENSE in the root of the repository for full licensing details.

"""Spot data extraction from diagnostic fields using neighbour cubes."""

Expand Down Expand Up @@ -204,13 +178,13 @@ def process(self, cubes: CubeList) -> Cube:
fast_percentile_method=fast_percentile_method,
)(result)
else:
msg = (
"Diagnostic cube is not a known probabilistic type. "
"The {} percentile could not be extracted. Extracting "
"data from the cube including any leading "
"dimensions.".format(extract_percentiles)
)
if not self.suppress_warnings:
msg = (
"Diagnostic cube is not a known probabilistic type. "
"The {} percentile(s) could not be extracted. The "
"spot-extracted outputs will be returned without "
"percentile extraction.".format(extract_percentiles)
)
warnings.warn(msg)
else:
if set(extract_percentiles).issubset(perc_coordinate.points):
Expand Down
32 changes: 3 additions & 29 deletions improver/spotdata/utilities.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,7 @@
# -*- coding: utf-8 -*-
# -----------------------------------------------------------------------------
# (C) British Crown copyright. The Met Office.
# All rights reserved.
# (C) Crown copyright, 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.
# This file is part of IMPROVER and is released under a BSD 3-Clause license.
# See LICENSE in the root of the repository for full licensing details.

"""Spot data utilities."""

Expand Down
60 changes: 27 additions & 33 deletions improver_tests/spotdata/test_SpotManipulation.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,8 @@
# -*- coding: utf-8 -*-
# -----------------------------------------------------------------------------
# (C) British Crown copyright. The Met Office.
# All rights reserved.
# (C) Crown copyright, 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.
# This file is part of IMPROVER and is released under a BSD 3-Clause license.
# See LICENSE in the root of the repository for full licensing details.

"""Unit tests for SpotManipulation class"""

import warnings
Expand All @@ -52,7 +27,7 @@


def gridded_variable(forecast_data):
"""Create a gridded variable cube from which to extract spot foreasts."""
"""Create a gridded variable cube from which to extract spot forecasts."""
height_crd = DimCoord(
np.array([1.5], dtype=np.float32), standard_name="height", units="m"
)
Expand All @@ -77,14 +52,14 @@ def gridded_lapse_rate(lapse_rates):


def gridded_percentiles(forecast_data):
"""Create a gridded percentile cube from which to extract spot foreasts."""
"""Create a gridded percentile cube from which to extract spot forecasts."""
n_percentiles = forecast_data.shape[0]
percentiles = np.linspace(20, 80, n_percentiles)
return set_up_percentile_cube(forecast_data, percentiles, attributes=ATTRIBUTES,)


def gridded_probabilities(forecast_data):
"""Create a gridded probability cube from which to extract spot foreasts."""
"""Create a gridded probability cube from which to extract spot forecasts."""
n_thresholds = forecast_data.shape[0]
thresholds = np.linspace(273, 283, n_thresholds)
return set_up_probability_cube(forecast_data, thresholds, attributes=ATTRIBUTES,)
Expand Down Expand Up @@ -267,7 +242,7 @@ def test_extraction(ftype, forecast_data, neighbour_cube, kwargs, expected):
def test_unknown_prob_type_warning(neighbour_cube):
"""Test a warning is raised if percentiles are requested from a cube from
which they cannot be extracted and that all data is returned in spot
format. In this case that is the 20th and 80th pecentiles are both
format. In this case that is the 20th and 80th percentiles are both
returned rather than the requested 50th percentile. If the
suppress_warnings option is set then test that no warning is raised."""

Expand Down Expand Up @@ -363,3 +338,22 @@ def test_missing_lapse_rate_warning(forecast_data, neighbour_cube, kwargs, expec
result = SpotManipulation(**kwargs)([forecast, neighbour_cube])

assert_array_equal(result.data, expected)


@pytest.mark.parametrize(
"kwargs,expected",
[
({}, "nearest"),
({"land_constraint": True}, "nearest_land"),
({"similar_altitude": True}, "nearest_minimum_dz"),
(
{"land_constraint": True, "similar_altitude": True},
"nearest_land_minimum_dz",
),
],
)
def test_neighbour_selection_method_setting(kwargs, expected):
"""Test that the neighbour_selection_method argument is set correctly
within the __init__ method."""
plugin = SpotManipulation(**kwargs)
assert plugin.neighbour_selection_method == expected
32 changes: 3 additions & 29 deletions improver_tests/spotdata/test_utilities.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,7 @@
# -*- coding: utf-8 -*-
# -----------------------------------------------------------------------------
# (C) British Crown copyright. The Met Office.
# All rights reserved.
# (C) Crown copyright, 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.
# This file is part of IMPROVER and is released under a BSD 3-Clause license.
# See LICENSE in the root of the repository for full licensing details.

"""Unit tests for spot data utilities"""

Expand Down

0 comments on commit b9c944e

Please sign in to comment.