class Test__update_metadata(IrisTest):
    """Test the _update_metadata method."""
    def setUp(self):
        """Create a cube like this:
        probability_of_lwe_precipitation_rate_above_threshold / (1)
        Dimension coordinates:
            lwe_precipitation_rate: 1;
            projection_y_coordinate: 16;
            projection_x_coordinate: 16;
        Scalar coordinates:
            forecast_period: 14400 seconds
            forecast_reference_time: 2017-11-10 00:00:00
            time: 2017-11-10 04:00:00
        Cell methods:
            mean: realization

        The lwe_precipitation_rate coordinate will have the attribute:
            spp__relative_to_threshold: above.
        """
        data = np.ones((1, 16, 16), dtype=np.float32)
        thresholds = np.array([0.5], dtype=np.float32)
        self.cube = set_up_probability_cube(
            data,
            thresholds,
            variable_name="lwe_precipitation_rate",
            threshold_units="mm h-1",
        )
        self.cube.add_cell_method(CellMethod("mean", coords="realization"))
        self.plugin = Plugin()

    def test_basic(self):
        """Test that the method returns the expected cube type
        and that the metadata are as expected.
        We expect a new name, the threshold coord to be removed and
        cell methods to be discarded."""
        result = self.plugin._update_metadata(self.cube)
        self.assertIsInstance(result, Cube)
        self.assertEqual(result.name(),
                         "probability_of_rate_of_lightning_above_threshold")
        msg = "No threshold coord found"
        with self.assertRaisesRegex(CoordinateNotFoundError, msg):
            find_threshold_coordinate(result)
        self.assertEqual(result.cell_methods, ())

    def test_input(self):
        """Test that the method does not modify the input cube data."""
        incube = self.cube.copy()
        self.plugin._update_metadata(incube)
        self.assertArrayAlmostEqual(incube.data, self.cube.data)
        self.assertEqual(incube.metadata, self.cube.metadata)

    def test_missing_threshold_coord(self):
        """Test that the method raises an error in Iris if the cube doesn't
        have a threshold coordinate to remove."""
        self.cube.remove_coord(find_threshold_coordinate(self.cube))
        msg = "No threshold coord found"
        with self.assertRaisesRegex(CoordinateNotFoundError, msg):
            self.plugin._update_metadata(self.cube)
Beispiel #2
0
class Test__update_metadata(IrisTest):
    """Test the _update_metadata method."""
    def setUp(self):
        """Create a cube with a single non-zero point like this:
        precipitation_amount / (kg m^-2)
        Dimension coordinates:
            realization: 1;
            time: 1;
            projection_y_coordinate: 3;
            projection_x_coordinate: 3;
        Auxiliary coordinates:
            forecast_period (on time coord): 4.0 hours
        Scalar coordinates:
            forecast_reference_time: 2015-11-23 03:00:00
            threshold: 0.5 mm hr-1
        Data:
            All points contain float(1.) except the
            zero point [0, 0, 1, 1] which is float(0.)
        """
        self.cube = add_forecast_reference_time_and_forecast_period(
            set_up_cube())
        coord = DimCoord(0.5, long_name="threshold", units='mm hr^-1')
        self.cube.add_aux_coord(coord)
        self.cube.add_cell_method(CellMethod('mean', coords='realization'))
        self.plugin = Plugin()

    def test_basic(self):
        """Test that the method returns the expected cube type
        and that the metadata are as expected.
        We expect a new name, the threshold coord to be removed and
        cell methods to be discarded."""
        result = self.plugin._update_metadata(self.cube)
        self.assertIsInstance(result, Cube)
        self.assertEqual(result.name(), "probability_of_lightning")
        msg = ("Expected to find exactly 1 threshold coordinate, but found "
               "none.")
        with self.assertRaisesRegex(CoordinateNotFoundError, msg):
            result.coord('threshold')
        self.assertEqual(result.cell_methods, ())

    def test_input(self):
        """Test that the method does not modify the input cube data."""
        incube = self.cube.copy()
        self.plugin._update_metadata(incube)
        self.assertArrayAlmostEqual(incube.data, self.cube.data)
        self.assertEqual(incube.metadata, self.cube.metadata)

    def test_missing_threshold_coord(self):
        """Test that the method raises an error in Iris if the cube doesn't
        have a threshold coordinate to remove."""
        self.cube.remove_coord('threshold')
        msg = ("Expected to find exactly 1 threshold coordinate, but found no")
        with self.assertRaisesRegex(CoordinateNotFoundError, msg):
            self.plugin._update_metadata(self.cube)