class Test_fill_in_by_horizontal_interpolation(IrisTest):
    """Test the fill_in_by_horizontal_interpolation method"""
    def setUp(self):
        """ Set up arrays for testing."""
        self.snow_level_data = np.array([[1.0, 1.0, 2.0], [1.0, np.nan, 2.0],
                                         [1.0, 2.0, 2.0]])
        self.orog_data = np.array([[6.0, 6.0, 6.0], [6.0, 7.0, 6.0],
                                   [6.0, 6.0, 6.0]])
        self.max_in_nbhood_orog = np.array([[7.0, 7.0, 7.0], [7.0, 7.0, 7.0],
                                            [7.0, 7.0, 7.0]])
        self.plugin = FallingSnowLevel()

    def test_basic(self):
        """Test when all the points around the missing data are the same."""
        snow_level_data = np.ones((3, 3))
        snow_level_data[1, 1] = np.nan
        expected = np.array([[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0,
                                                                1.0]])
        snow_level_updated = self.plugin.fill_in_by_horizontal_interpolation(
            snow_level_data, self.max_in_nbhood_orog, self.orog_data)
        self.assertArrayEqual(snow_level_updated, expected)

    def test_different_data(self):
        """Test when the points around the missing data have different
           values."""
        expected = np.array([[1.0, 1.0, 2.0], [1.0, 1.5, 2.0], [1.0, 2.0,
                                                                2.0]])
        snow_level_updated = self.plugin.fill_in_by_horizontal_interpolation(
            self.snow_level_data, self.max_in_nbhood_orog, self.orog_data)
        self.assertArrayEqual(snow_level_updated, expected)

    def test_lots_missing(self):
        """Test when there's an extra missing value at the corner
           of the grid. This point can't be filled in by linear interpolation,
           but is instead filled by nearest neighbour extrapolation."""
        self.snow_level_data[2, 2] = np.nan
        expected = np.array([[1.0, 1.0, 2.0], [1.0, 1.5, 2.0], [1.0, 2.0,
                                                                2.0]])
        snow_level_updated = self.plugin.fill_in_by_horizontal_interpolation(
            self.snow_level_data, self.max_in_nbhood_orog, self.orog_data)
        self.assertArrayEqual(snow_level_updated, expected)

    def test_all_above_max_orography(self):
        """Test that nothing is filled in if all the snow falling levels are
           above the maximum orography"""
        max_in_nbhood_orog = np.zeros((3, 3))
        orography = np.zeros((3, 3))
        expected = np.array([[1.0, 1.0, 2.0], [1.0, np.nan, 2.0],
                             [1.0, 2.0, 2.0]])
        snow_level_updated = self.plugin.fill_in_by_horizontal_interpolation(
            self.snow_level_data, max_in_nbhood_orog, orography)
        self.assertArrayEqual(snow_level_updated, expected)

    def test_set_to_orography(self):
        """Test when the linear interpolation gives values that are higher
           than the orography the snow falling level is set back to the
           orography"""
        snow_falling_level = np.array([[10.0, np.nan, np.nan, np.nan, 20.0],
                                       [10.0, np.nan, np.nan, np.nan, 20.0],
                                       [10.0, np.nan, np.nan, np.nan, 20.0],
                                       [10.0, np.nan, np.nan, np.nan, 20.0],
                                       [10.0, np.nan, np.nan, np.nan, 20.0]])

        orography = np.array([[0.0, 30.0, 12.0, 30.0, 0.0],
                              [0.0, 30.0, 12.0, 30.0, 0.0],
                              [0.0, 30.0, 12.0, 30.0, 0.0],
                              [0.0, 30.0, 12.0, 30.0, 0.0],
                              [0.0, 30.0, 12.0, 30.0, 0.0]])

        max_in_nbhood_orog = np.ones((5, 5)) * 30.0
        expected = np.array([[10.0, 12.5, 12.0, 17.5, 20.0],
                             [10.0, 12.5, 12.0, 17.5, 20.0],
                             [10.0, 12.5, 12.0, 17.5, 20.0],
                             [10.0, 12.5, 12.0, 17.5, 20.0],
                             [10.0, 12.5, 12.0, 17.5, 20.0]])
        snow_level_updated = self.plugin.fill_in_by_horizontal_interpolation(
            snow_falling_level, max_in_nbhood_orog, orography)
        self.assertArrayEqual(snow_level_updated, expected)
示例#2
0
class Test_fill_in_by_horizontal_interpolation(IrisTest):
    """Test the fill_in_by_horizontal_interpolation method"""
    def setUp(self):
        """ Set up arrays for testing."""
        self.snow_level_data = np.array([[1.0, 1.0, 2.0], [1.0, np.nan, 2.0],
                                         [1.0, 2.0, 2.0]])
        self.orog_data = np.array([[6.0, 6.0, 6.0], [6.0, 7.0, 6.0],
                                   [6.0, 6.0, 6.0]])
        self.max_in_nbhood_orog = np.array([[7.0, 7.0, 7.0], [7.0, 7.0, 7.0],
                                            [7.0, 7.0, 7.0]])
        self.plugin = FallingSnowLevel()

    def test_basic(self):
        """Test when all the points around the missing data are the same."""
        snow_level_data = np.ones((3, 3))
        snow_level_data[1, 1] = np.nan
        expected = np.array([[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0,
                                                                1.0]])
        snow_level_updated = self.plugin.fill_in_by_horizontal_interpolation(
            snow_level_data, self.max_in_nbhood_orog, self.orog_data)
        self.assertArrayEqual(snow_level_updated, expected)

    def test_not_enough_points_to_fill(self):
        """Test when there are not enough points to fill the gaps.
           This raises a QhullError if there are less than 3 points available
           to use for the interpolation. The QhullError is different to the one
           raised by test_badly_arranged_valid_data"""
        snow_level_data = np.array([[np.nan, 1, np.nan],
                                    [np.nan, np.nan, np.nan],
                                    [np.nan, 1, np.nan]])
        expected = np.array([[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0,
                                                                1.0]])
        snow_level_updated = self.plugin.fill_in_by_horizontal_interpolation(
            snow_level_data, self.max_in_nbhood_orog, self.orog_data)
        self.assertArrayEqual(snow_level_updated, expected)

    def test_badly_arranged_valid_data(self):
        """Test when there are enough points but they aren't arranged in a
           suitable way to allow horizontal interpolation. This raises a
           QhullError that we want to ignore and use nearest neighbour
           interpolation instead. This QhullError is different to the one
           raised by test_not_enough_points_to_fill."""
        snow_level_data = np.array([[np.nan, 1, np.nan], [np.nan, 1, np.nan],
                                    [np.nan, 1, np.nan]])
        expected = np.array([[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0,
                                                                1.0]])
        snow_level_updated = self.plugin.fill_in_by_horizontal_interpolation(
            snow_level_data, self.max_in_nbhood_orog, self.orog_data)
        self.assertArrayEqual(snow_level_updated, expected)

    def test_different_data(self):
        """Test when the points around the missing data have different
           values."""
        expected = np.array([[1.0, 1.0, 2.0], [1.0, 1.5, 2.0], [1.0, 2.0,
                                                                2.0]])
        snow_level_updated = self.plugin.fill_in_by_horizontal_interpolation(
            self.snow_level_data, self.max_in_nbhood_orog, self.orog_data)
        self.assertArrayEqual(snow_level_updated, expected)

    def test_lots_missing(self):
        """Test when there's an extra missing value at the corner
           of the grid. This point can't be filled in by linear interpolation,
           but is instead filled by nearest neighbour extrapolation."""
        self.snow_level_data[2, 2] = np.nan
        expected = np.array([[1.0, 1.0, 2.0], [1.0, 1.5, 2.0], [1.0, 2.0,
                                                                2.0]])
        snow_level_updated = self.plugin.fill_in_by_horizontal_interpolation(
            self.snow_level_data, self.max_in_nbhood_orog, self.orog_data)
        self.assertArrayEqual(snow_level_updated, expected)

    def test_all_above_max_orography(self):
        """Test that nothing is filled in if all the snow falling levels are
           above the maximum orography"""
        max_in_nbhood_orog = np.zeros((3, 3))
        orography = np.zeros((3, 3))
        expected = np.array([[1.0, 1.0, 2.0], [1.0, np.nan, 2.0],
                             [1.0, 2.0, 2.0]])
        snow_level_updated = self.plugin.fill_in_by_horizontal_interpolation(
            self.snow_level_data, max_in_nbhood_orog, orography)
        self.assertArrayEqual(snow_level_updated, expected)

    def test_set_to_orography(self):
        """Test when the linear interpolation gives values that are higher
           than the orography the snow falling level is set back to the
           orography"""
        snow_falling_level = np.array([[10.0, np.nan, np.nan, np.nan, 20.0],
                                       [10.0, np.nan, np.nan, np.nan, 20.0],
                                       [10.0, np.nan, np.nan, np.nan, 20.0],
                                       [10.0, np.nan, np.nan, np.nan, 20.0],
                                       [10.0, np.nan, np.nan, np.nan, 20.0]])

        orography = np.array([[0.0, 30.0, 12.0, 30.0, 0.0],
                              [0.0, 30.0, 12.0, 30.0, 0.0],
                              [0.0, 30.0, 12.0, 30.0, 0.0],
                              [0.0, 30.0, 12.0, 30.0, 0.0],
                              [0.0, 30.0, 12.0, 30.0, 0.0]])

        max_in_nbhood_orog = np.ones((5, 5)) * 30.0
        expected = np.array([[10.0, 12.5, 12.0, 17.5, 20.0],
                             [10.0, 12.5, 12.0, 17.5, 20.0],
                             [10.0, 12.5, 12.0, 17.5, 20.0],
                             [10.0, 12.5, 12.0, 17.5, 20.0],
                             [10.0, 12.5, 12.0, 17.5, 20.0]])
        snow_level_updated = self.plugin.fill_in_by_horizontal_interpolation(
            snow_falling_level, max_in_nbhood_orog, orography)
        self.assertArrayEqual(snow_level_updated, expected)