class Test_process(IrisTest):
    """Class to test end-to-end smoothing_coefficients creation"""

    def setUp(self):
        """Set up cube & plugin"""
        self.plugin = OrographicSmoothingCoefficients(
            min_smoothing_coefficient=1.0, max_smoothing_coefficient=0.0
        )
        self.cube = set_up_cube()

    def test_basic(self):
        """Tests that the final processing step gets the right values."""
        result = self.plugin.process(self.cube)

        expected_x = np.array([[0.4, 0.2], [1.0, 0.6], [0.8, 1.0],])

        expected_y = np.array([[0.8, 1.0, 0.6], [0.6, 0.8, 0.0],])

        self.assertArrayAlmostEqual(result[0].data, expected_x)
        self.assertArrayAlmostEqual(result[1].data, expected_y)

    def test_list_error(self):
        """Test that a list of orography input cubes raises a value error"""
        with self.assertRaises(ValueError):
            self.plugin.process([self.cube, self.cube])
class Test_scale_smoothing_coefficients(IrisTest):
    """Class to test the scale_smoothing_coefficients function"""

    def setUp(self):
        """Set up cube & plugin"""
        self.plugin = OrographicSmoothingCoefficients()
        cube = set_up_cube()
        self.cubelist = [cube, cube]

    def test_basic(self):
        """
        Test the basic function of scale_smoothing_coefficients, using the
        standard max and min smoothing_coefficients.
        """
        result = self.plugin.scale_smoothing_coefficients(self.cubelist)
        expected = np.array([[0.1, 0.5, 1.0], [0.3, 0.4, 0.7], [0.0, 0.2, 0.1]])
        self.assertArrayAlmostEqual(result[0].data, expected)
        self.assertArrayAlmostEqual(result[1].data, expected)

    def test_maxmin(self):
        """
        Tests the function of scale_smoothing_coefficients, using a max
        and min value for smoothing_coefficient.
        """
        result = self.plugin.scale_smoothing_coefficients(self.cubelist, 0.3, 0.5)
        expected = np.array(
            [[0.32, 0.40, 0.50], [0.36, 0.38, 0.44], [0.30, 0.34, 0.32]]
        )
        self.assertArrayAlmostEqual(result[0].data, expected)
        self.assertArrayAlmostEqual(result[1].data, expected)
Beispiel #3
0
 def setUp(self):
     """Set up cube & plugin"""
     self.plugin = OrographicSmoothingCoefficients(
         min_smoothing_coefficient=0.5, max_smoothing_coefficient=0.3)
     self.cube = set_up_cube()
     self.gradient_x, self.gradient_y = DifferenceBetweenAdjacentGridSquares(
         gradient=True).process(self.cube)
 def test_basic(self):
     """Test default attribute initialisation"""
     result = OrographicSmoothingCoefficients()
     self.assertEqual(result.min_smoothing_coefficient, 0.0)
     self.assertEqual(result.max_smoothing_coefficient, 1.0)
     self.assertEqual(result.coefficient, 1.0)
     self.assertEqual(result.power, 1.0)
Beispiel #5
0
 def test_basic(self):
     """Test that the __repr__ returns the expected string."""
     result = str(OrographicSmoothingCoefficients())
     msg = ("<OrographicSmoothingCoefficients: min_smoothing_coefficient: "
            "{}; max_smoothing_coefficient: {}; coefficient: {}; power: {}"
            ">".format(0.0, 1.0, 1, 1))
     self.assertEqual(result, msg)
Beispiel #6
0
class Test_gradient_to_smoothing_coefficient(IrisTest):
    """Class to test smoothing_coefficients data and metadata output"""
    def setUp(self):
        """Set up cube & plugin"""
        self.plugin = OrographicSmoothingCoefficients(
            min_smoothing_coefficient=0.5, max_smoothing_coefficient=0.3)
        self.cube = set_up_cube()
        self.gradient_x, self.gradient_y = DifferenceBetweenAdjacentGridSquares(
            gradient=True).process(self.cube)

    def test_basic(self):
        """Test basic version of gradient to smoothing_coefficient"""

        expected = np.array([
            [0.40666667, 0.38, 0.35333333],
            [0.5, 0.44666667, 0.39333333],
            [0.40666667, 0.48666667, 0.43333333],
        ])

        result = self.plugin.gradient_to_smoothing_coefficient(
            self.gradient_x, self.gradient_y)
        self.assertEqual(result[0].name(), "smoothing_coefficient_x")
        self.assertArrayAlmostEqual(result[0].data, expected)
        self.assertNotIn("forecast_period",
                         [coord.name() for coord in result[0].coords()])
        self.assertNotIn("forecast_time",
                         [coord.name() for coord in result[0].coords()])
Beispiel #7
0
def process(
    orography: cli.inputcube,
    *,
    min_smoothing_coefficient: float = 0.0,
    max_smoothing_coefficient: float = 1.0,
    coefficient: float = 1.0,
    power: float = 1.0,
):
    """Generate smoothing coefficients for recursive filtering based on
    orography gradients.

    A smoothing coefficient determines how much "value" of a cell
    undergoing filtering is comprised of the current value at that cell and
    how much comes from the adjacent cell preceding it in the direction in
    which filtering is being applied. A larger smoothing_coefficient results in
    a more significant proportion of a cell's new value coming from its
    neighbouring cell.

    The smoothing coefficients are calculated from the orography gradient using
    a simple equation and the user defined values for coefficient and power:

    smoothing_coefficient = coefficient * gradient**power

    The resulting values are scaled between min_smoothing_coefficient and
    max_smoothing_coefficient to give the desired range of
    smoothing_coefficients. These can be provided in reverse (i.e. min > max)
    to invert the smoothing coefficients in relation to the orographic
    gradient, providing smoothing coefficients that are largest where the
    orography gradient is shallowest.

    Args:
        orography (iris.cube.Cube):
            A 2D field of orography on the grid for which
            smoothing_coefficients are to be generated.
        min_smoothing_coefficient (float):
            The minimum value of smoothing_coefficient.
        max_smoothing_coefficient (float):
            The maximum value of smoothing_coefficient.
        coefficient (float):
            The coefficient for the smoothing_coefficient equation.
        power (float):
            The power for the smoothing_coefficient equation.

    Returns:
        iris.cube.CubeList:
            Processed CubeList containing smoothing_coefficients_x and
            smoothing_coefficients_y cubes.
    """
    from improver.utilities.ancillary_creation import OrographicSmoothingCoefficients

    plugin = OrographicSmoothingCoefficients(
        min_smoothing_coefficient, max_smoothing_coefficient, coefficient, power
    )

    return plugin(orography)
Beispiel #8
0
class Test_unnormalised_smoothing_coefficients(IrisTest):
    """Class to test the basic smoothing_coefficients function"""
    def setUp(self):
        """Set up cube & plugin"""
        self.plugin = OrographicSmoothingCoefficients(coefficient=0.5,
                                                      power=2.0)
        self.cube = set_up_cube()

    def test_basic(self):
        """Test data are as expected"""
        expected = np.array([[1.53125, 2.53125, 3.78125], [0.0, 0.5, 2.0],
                             [1.53125, 0.03125, 0.78125]])
        gradient_x, _ = DifferenceBetweenAdjacentGridSquares(
            gradient=True).process(self.cube)
        smoothing_coefficient_x = self.plugin.unnormalised_smoothing_coefficients(
            gradient_x)
        self.assertArrayAlmostEqual(smoothing_coefficient_x.data, expected)
 def setUp(self):
     """Set up cube & plugin"""
     self.plugin = OrographicSmoothingCoefficients()
     cube = set_up_cube()
     self.cubelist = [cube, cube]
 def setUp(self):
     """Set up cube & plugin"""
     self.plugin = OrographicSmoothingCoefficients(
         min_smoothing_coefficient=1.0, max_smoothing_coefficient=0.0
     )
     self.cube = set_up_cube()
 def setUp(self):
     """Set up cube & plugin"""
     self.plugin = OrographicSmoothingCoefficients(coefficient=0.5, power=2.0)
     self.cube = set_up_cube()