Example #1
0
class Test_process(IrisTest):
    """Class to test end-to-end alphas creation"""
    def setUp(self):
        """Set up cube & plugin"""
        self.plugin = OrographicAlphas()
        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.53333333, 0.4, 0.26666667],
                               [1., 0.73333333, 0.46666667],
                               [0.53333333, 0.93333333, 0.66666667]])

        expected_y = np.array([[0.4, 0.93333333, 0.8], [0.93333333, 0.8, 0.4],
                               [0.26666667, 0.66666667, 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])
Example #2
0
class Test_scale_alphas(IrisTest):
    """Class to test the scale_alphas function"""
    def setUp(self):
        """Set up cube & plugin"""
        self.plugin = OrographicAlphas()
        cube = set_up_cube()
        self.cubelist = [cube, cube]

    def test_basic(self):
        """
        Test the basic function of scale_alphas, using the
        standard max and min alphas.
        """
        result = self.plugin.scale_alphas(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_alphas, using a max
        and min value for alpha.
        """
        result = self.plugin.scale_alphas(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)
Example #3
0
 def setUp(self):
     """Set up cube & plugin"""
     self.plugin = OrographicAlphas(min_alpha=0.3, max_alpha=0.5)
     self.cube = set_up_cube()
     self.gradient_x, self.gradient_y = \
         DifferenceBetweenAdjacentGridSquares(gradient=True).process(
             self.cube)
Example #4
0
 def test_basic(self):
     """Test that the __repr__ returns the expected string."""
     result = str(OrographicAlphas())
     msg = ('<OrographicAlphas: min_alpha: {}; max_alpha: {}; '
            'coefficient: {}; power: {}; '
            'invert_alphas: {}>'.format(0.0, 1.0, 1, 1, True))
     self.assertEqual(result, msg)
Example #5
0
class Test_gradient_to_alpha(IrisTest):
    """Class to test alphas data and metadata output"""
    def setUp(self):
        """Set up cube & plugin"""
        self.plugin = OrographicAlphas(min_alpha=0.3, max_alpha=0.5)
        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 alpha"""

        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_alpha(self.gradient_x,
                                               self.gradient_y)
        self.assertEqual(result[0].name(), 'alphas')
        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()])
def process(orography: cli.inputcube,
            *,
            min_alpha: float = 0.0,
            max_alpha: float = 1.0,
            coefficient: float = 1.0,
            power: float = 1.0,
            invert_alphas=True):
    """Generate alpha smoothing parameters for recursive filtering based on
    orography gradients.

    Args:
        orography (iris.cube.Cube):
            A 2D field of orography for the grid to generate alphas for.
        min_alpha (float):
            The minimum value of alpha.
        max_alpha (float):
            The maximum value of alpha.
        coefficient (float):
            The coefficient for the alpha calculation.
        power (float):
            The power for the alpha equation.
        invert_alphas (bool):
            If True then the max and min alpha values will be swapped.

    Returns:
        iris.cube.CubeList:
            Processed CubeList containing alpha_x and alpha_y cubes.
    """
    from improver.utilities.ancillary_creation import OrographicAlphas
    return OrographicAlphas(min_alpha, max_alpha, coefficient, power,
                            invert_alphas).process(orography)
Example #7
0
 def test_basic(self):
     """Test default attribute initialisation"""
     result = OrographicAlphas()
     self.assertTrue(result.invert_alphas)
     self.assertEqual(result.min_alpha, 0.)
     self.assertEqual(result.max_alpha, 1.)
     self.assertEqual(result.coefficient, 1.)
     self.assertEqual(result.power, 1.)
Example #8
0
class Test_unnormalised_alphas(IrisTest):
    """Class to test the basic alphas function"""
    def setUp(self):
        """Set up cube & plugin"""
        self.plugin = OrographicAlphas(coefficient=0.5, power=2.)
        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.5, 2.],
                             [1.53125, 0.03125, 0.78125]])
        gradient_x, _ = \
            DifferenceBetweenAdjacentGridSquares(gradient=True).process(
                self.cube)
        alpha_x = self.plugin.unnormalised_alphas(gradient_x)
        self.assertArrayAlmostEqual(alpha_x.data, expected)
Example #9
0
 def setUp(self):
     """Set up cube & plugin"""
     self.plugin = OrographicAlphas()
     cube = set_up_cube()
     self.cubelist = [cube, cube]
Example #10
0
 def setUp(self):
     """Set up cube & plugin"""
     self.plugin = OrographicAlphas(coefficient=0.5, power=2.)
     self.cube = set_up_cube()