def test_unlimited(self):
        """Test interpolation to complete an incomplete field using a reference
        field. No limit is imposed upon the returned interpolated values."""

        expected = np.array(
            [[4.0, 4.0, 4.0], [8.5, 8.5, 8.5], [3.0, 3.0, 3.0]], dtype=np.float32
        )

        result = InterpolateUsingDifference().process(self.sleet_rain, self.snow_sleet)

        assert_array_equal(result.data, expected)
        self.assertEqual(result.coords(), self.sleet_rain.coords())
        self.assertEqual(result.metadata, self.sleet_rain.metadata)
    def test_minimum_limited(self):
        """Test interpolation to complete an incomplete field using a reference
        field. A limit is imposed upon the returned interpolated values,
        forcing these values to the minimum limit if they are below it."""

        expected = np.array(
            [[4.0, 4.0, 4.0], [10.0, 8.5, 8.5], [3.0, 3.0, 3.0]], dtype=np.float32
        )

        result = InterpolateUsingDifference().process(
            self.sleet_rain, self.snow_sleet, limit=self.limit, limit_as_maximum=False
        )

        assert_array_equal(result.data, expected)
        self.assertEqual(result.coords(), self.sleet_rain.coords())
        self.assertEqual(result.metadata, self.sleet_rain.metadata)
    def test_incomplete_reference_data(self):
        """Test an exception is raised if the reference field is incomplete."""

        self.snow_sleet.data[1, 1] = np.nan
        msg = "The reference cube contains np.nan data"
        with self.assertRaisesRegex(ValueError, msg):
            InterpolateUsingDifference()._check_inputs(self.sleet_rain,
                                                       self.snow_sleet, None)
    def test_incompatible_reference_cube_units(self):
        """Test an exception is raised if the reference cube has units that
        are incompatible with the input cube."""

        self.snow_sleet.units = "s"
        msg = "Reference cube and/or limit do not have units compatible"
        with self.assertRaisesRegex(ValueError, msg):
            InterpolateUsingDifference()._check_inputs(self.sleet_rain,
                                                       self.snow_sleet, None)
    def test_convert_units(self):
        """Test that a reference cube and limit cube with different but
        compatible units are converted for use and return the expected
        result."""

        expected = np.array(
            [[4.0, 4.0, 4.0], [8.5, 8.0, 6.0], [3.0, 3.0, 3.0]], dtype=np.float32
        )

        self.snow_sleet.convert_units("cm")
        self.limit.convert_units("cm")

        result = InterpolateUsingDifference().process(
            self.sleet_rain, self.snow_sleet, limit=self.limit
        )

        assert_array_equal(result.data, expected)
        self.assertEqual(result.coords(), self.sleet_rain.coords())
        self.assertEqual(result.metadata, self.sleet_rain.metadata)
    def test_convert_units(self):
        """Test that a reference cube and limit cube with different but
        compatible units are converted without an exception being raised."""

        self.snow_sleet.convert_units("cm")
        self.limit.convert_units("cm")

        InterpolateUsingDifference().process(
            self.sleet_rain, self.snow_sleet, limit=self.limit
        )
    def test_crossing_values(self):
        """Test interpolation when the reference field and field to be
        completed by interpolation cross one another. In the absence of any
        limit it should be possible to return an interpolated field of values
        that pass through the reference field in an expected way. In another
        case we apply the reference field as a lower bound to the interpolated
        values."""

        snow_sleet = np.array(
            [[15.0, 15.0, 15.0], [10.0, 10.0, 10.0], [8.0, 8.0, 8.0]], dtype=np.float32
        )

        sleet_rain = np.array(
            [[5.0, 5.0, 5.0], [np.nan, np.nan, np.nan], [15.0, 15.0, 15.0]],
            dtype=np.float32,
        )
        sleet_rain = np.ma.masked_invalid(sleet_rain)

        self.snow_sleet.data = snow_sleet
        self.sleet_rain.data = sleet_rain

        expected_unlimited = np.array(
            [[5.0, 5.0, 5.0], [8.5, 8.5, 8.5], [15.0, 15.0, 15.0]], dtype=np.float32
        )
        expected_limited = np.array(
            [[5.0, 5.0, 5.0], [10.0, 10.0, 10.0], [15.0, 15.0, 15.0]], dtype=np.float32
        )

        result_unlimited = InterpolateUsingDifference().process(
            self.sleet_rain, self.snow_sleet
        )

        result_limited = InterpolateUsingDifference().process(
            self.sleet_rain,
            self.snow_sleet,
            limit=self.snow_sleet,
            limit_as_maximum=False,
        )

        assert_array_equal(result_unlimited.data, expected_unlimited)
        assert_array_equal(result_limited.data, expected_limited)
    def test_unmasked_input_cube(self, warning_list=None):
        """Test a warning is raised if the input cube is not masked and that
        the input cube is returned unchanged."""

        self.sleet_rain.data = np.ones((3, 3), dtype=np.float32)
        expected = self.sleet_rain.copy()
        warning_msg = "Input cube unmasked, no data to fill in, returning"

        result = InterpolateUsingDifference().process(self.sleet_rain, self.snow_sleet)

        self.assertEqual(result, expected)
        self.assertTrue(any(item.category == UserWarning for item in warning_list))
        self.assertTrue(any(warning_msg in str(item) for item in warning_list))
    def test_linear_failure(self):
        """Test that if the use of linear interpolation does not result in a
        complete difference field, and thus a complete field of interest, the
        secondary use of nearest neighbour interpolation completes the
        field."""

        sleet_rain = np.array(
            [[np.nan, np.nan, 4.0], [np.nan, np.nan, np.nan], [3.0, 3.0, 3.0]],
            dtype=np.float32,
        )
        sleet_rain = np.ma.masked_invalid(sleet_rain)
        self.sleet_rain.data = sleet_rain

        expected = np.array(
            [[3.5, 4.0, 4.0], [8.5, 8.5, 8.5], [3.0, 3.0, 3.0]], dtype=np.float32
        )

        result = InterpolateUsingDifference().process(self.sleet_rain, self.snow_sleet)

        assert_array_equal(result.data, expected)
        self.assertEqual(result.coords(), self.sleet_rain.coords())
        self.assertEqual(result.metadata, self.sleet_rain.metadata)
    def test_multi_realization_limited(self):
        """Test interpolation to complete an incomplete field using a reference
        field. A limit is imposed upon the returned interpolated values,
        forcing these values to the minimum limit if they are below it. The
        inputs are multi-realization."""

        snow_sleet = add_coordinate(self.snow_sleet, [0, 1], "realization")
        sleet_rain = add_coordinate(self.sleet_rain, [0, 1], "realization")

        expected = np.array(
            [[4.0, 4.0, 4.0], [10.0, 8.5, 8.5], [3.0, 3.0, 3.0]], dtype=np.float32
        )

        result = InterpolateUsingDifference().process(
            sleet_rain, snow_sleet, limit=self.limit, limit_as_maximum=False
        )

        assert_array_equal(result[0].data, expected)
        assert_array_equal(result[1].data, expected)
        self.assertEqual(result.shape, sleet_rain.shape)
        self.assertEqual(result.coords(), sleet_rain.coords())
        self.assertEqual(result.metadata, sleet_rain.metadata)
示例#11
0
def process(
    cube: cli.inputcube,
    reference_cube: cli.inputcube,
    limit: cli.inputcube = None,
    *,
    limit_as_maximum=True,
):
    """
    Uses interpolation to fill masked regions in the data contained within the
    input cube. This is achieved by calculating the difference between the
    input cube and a complete (i.e. complete across the whole domain) reference
    cube. The difference between the data in regions where they overlap is
    calculated and this difference field is then interpolated across the
    domain. Any masked regions in the input cube data are then filled with data
    calculated as the reference cube data minus the interpolated difference
    field.

    Args:
        cube (iris.cube.Cube):
            A cube containing data in which there are masked regions to be
            filled.
        reference_cube (iris.cube.Cube):
            A cube containing data in the same units as the cube of data to be
            interpolated. The data in this cube must be complete across the
            entire domain.
        limit (iris.cube.Cube):
            A cube of limiting values to apply to the cube that is being filled
            in. This can be used to ensure that the resulting values do not
            fall below / exceed the limiting values; whether the limit values
            should be used as minima or maxima is determined by the
            limit_as_maximum option.
        limit_as_maximum (bool):
            If True the limit values are treated as maxima for the data in the
            interpolated regions. If False the limit values are treated as
            minima.
    Returns:
        iris.cube.Cube:
            Processed cube with the masked regions filled in through
            interpolation.
    """
    from improver.utilities.interpolation import InterpolateUsingDifference

    result = InterpolateUsingDifference()(cube,
                                          reference_cube,
                                          limit=limit,
                                          limit_as_maximum=limit_as_maximum)
    return result
 def test_basic(self):
     """Test expected string representation is returned."""
     self.assertEqual(
         str(InterpolateUsingDifference()), "<InterpolateUsingDifference>"
     )