def setUp(self):
        """Create test cubes and plugin instance.
        The cube coordinates look like this:
        Dimension coordinates:
            projection_y_coordinate: 3;
            projection_x_coordinate: 3;
        Scalar coordinates:
            time: 2015-11-23 07:00:00
            forecast_reference_time: 2015-11-23 07:00:00
            forecast_period: 0 seconds

        self.cube:
            Metadata describes the nowcast lightning fields to be calculated.
            forecast_period: 0 seconds (simulates nowcast data)
        self.fg_cube:
            Has 4 hour forecast period, to test impact at lr2
        self.ltng_cube:
            forecast_period: 0 seconds (simulates nowcast data)
        self.precip_cube:
            Has extra coordinate of length(3) "threshold" containing
            points [0.5, 7., 35.] mm h-1.
        self.vii_cube:
            Has extra coordinate of length(3) "threshold" containing
            points [0.5, 1., 2.] kg m-2.
        """
        (
            self.cube,
            self.fg_cube,
            self.ltng_cube,
            self.precip_cube,
            self.vii_cube,
        ) = set_up_lightning_test_cubes()
        self.plugin = Plugin()
 def test_with_radius(self):
     """
     Test that the radius keyword is accepted.
     """
     radius = 20000.0
     plugin = Plugin(radius=radius)
     self.assertEqual(plugin.radius, radius)
    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 __repr__ returns the expected string."""
        plugin = Plugin()
        result = str(plugin)
        msg = ("""<NowcastLightning: radius={radius},
 lightning mapping (lightning rate in "min^-1"):
   upper: lightning rate {lthru} => min lightning prob {lprobu}
   lower: lightning rate {lthrl} => min lightning prob {lprobl}
>""".format(radius=10000.,
            lthru="<class 'function'>", lthrl=0.,
            lprobu=1., lprobl=0.25)
              )
        self.assertEqual(result, msg)
Esempio n. 5
0
    def test_result_with_vii(self):
        """Test that the method returns the expected data when vii is
        present"""
        # Set precip_cube forecast period to be zero.
        self.precip_cube.coord("forecast_period").points = [0.0]
        expected = self.set_up_vii_input_output()

        # No halo - we're only testing this method.
        # 2000m is the grid-length, so halo includes only one pixel.
        plugin = Plugin(2000.0)
        result = plugin(
            CubeList([self.fg_cube, self.ltng_cube, self.precip_cube, self.vii_cube])
        )
        self.assertIsInstance(result, Cube)
        self.assertArrayAlmostEqual(result.data, expected.data)
    def setUp(self):
        """Create test cubes and plugin instance.
        The cube coordinates look like this:
        Dimension coordinates:
            projection_y_coordinate: 3;
            projection_x_coordinate: 3;
        Scalar coordinates:
            time: 2015-11-23 07:00:00
            forecast_reference_time: 2015-11-23 07:00:00
            forecast_period: 0 seconds

        self.vii_cube:
            Has extra coordinate of length(3) "threshold" containing
            points [0.5, 1., 2.] kg m-2.
        """
        (_, self.fg_cube, _, _, self.ice_cube) = set_up_lightning_test_cubes(
            validity_time=dt(2015, 11, 23, 7), fg_frt=dt(2015, 11, 23, 7))
        self.plugin = Plugin()
        self.ice_threshold_coord = find_threshold_coordinate(self.ice_cube)
Esempio n. 7
0
 def setUp(self):
     """Create cubes with a single zero prob(precip) point.
     The cubes look like this:
     precipitation_amount / (kg m^-2)
     Dimension coordinates:
         time: 1;
         projection_y_coordinate: 3;
         projection_x_coordinate: 3;
     Auxiliary coordinates:
         forecast_period (on time coord): 4.0 hours (simulates UM data)
     Scalar coordinates:
         forecast_reference_time: 2015-11-23 03:00:00
     Data:
     self.fg_cube:
         forecast_period (on time coord): 0.0 hours (simulates nowcast data)
         All points contain float(1.)
         Cube name is "probability_of_lightning".
     self.precip_cube:
         With extra coordinate of length(3) "threshold" containing
         points [0.5, 7., 35.] mm hr-1.
         All points contain float(1.) except the
         zero point [0, 0, 1, 1] which is float(0.)
         and [1:, 0, ...] which are float(0.)
         Cube name is "probability_of_precipitation".
         Cube has added attribute {'relative_to_threshold': 'above'}
     """
     self.fg_cube = add_forecast_reference_time_and_forecast_period(
         set_up_cube_with_no_realizations(zero_point_indices=[],
                                          num_grid_points=3),
         fp_point=0.0)
     self.fg_cube.rename("probability_of_lightning")
     self.precip_cube = (add_forecast_reference_time_and_forecast_period(
         set_up_cube(num_realization_points=3,
                     zero_point_indices=((0, 1, 1), ),
                     num_grid_points=3)))
     threshold_coord = self.precip_cube.coord('realization')
     threshold_coord.points = [0.5, 7.0, 35.0]
     threshold_coord.rename('threshold')
     threshold_coord.units = cf_units.Unit('mm hr-1')
     self.precip_cube.rename("probability_of_precipitation")
     self.precip_cube.attributes.update({'relative_to_threshold': 'above'})
     self.precip_cube.data[1:, 0, ...] = 0.
     self.plugin = Plugin()
    def setUp(self):
        """Create test cubes and plugin instance.
        The cube coordinates look like this:
        Dimension coordinates:
            projection_y_coordinate: 16;
            projection_x_coordinate: 16;
        Scalar coordinates:
            time: 2015-11-23 07:00:00
            forecast_reference_time: 2015-11-23 07:00:00
            forecast_period: 0 seconds

        self.fg_cube:
            Has 4 hour forecast period, to test impact on "lightning risk
            2 level" output (see improver.nowcasting.lightning for details)
        self.ltng_cube:
            forecast_period: 0 seconds (simulates nowcast data)
        self.precip_cube:
            Has extra coordinate of length(3) "threshold" containing
            points [0.5, 7., 35.] mm h-1.  Has a 4 hour forecast period.
        self.vii_cube:
            Has extra coordinate of length(3) "threshold" containing
            points [0.5, 1., 2.] kg m-2.
        """
        (
            _,
            self.fg_cube,
            self.ltng_cube,
            self.precip_cube,
            self.vii_cube,
        ) = set_up_lightning_test_cubes(grid_points=16)
        # reset some data and give precip cube a 4 hour forecast period
        self.precip_cube.data[0, ...] = 1.0
        self.precip_cube.coord("forecast_period").points = [4 * 3600.0]

        # sort out spatial coordinates - need smaller grid length (set to 2 km)
        for cube in [
                self.fg_cube, self.ltng_cube, self.precip_cube, self.vii_cube
        ]:
            points_array = 2000.0 * np.arange(16).astype(np.float32)
            cube.coord(axis="x").points = points_array
            cube.coord(axis="y").points = points_array
        self.plugin = Plugin()
Esempio n. 9
0
    def setUp(self):
        """Create test cubes and plugin instance.
        The cube coordinates look like this:
        Dimension coordinates:
            projection_y_coordinate: 3;
            projection_x_coordinate: 3;
        Scalar coordinates:
            time: 2015-11-23 07:00:00
            forecast_reference_time: 2015-11-23 07:00:00
            forecast_period: 0 seconds

        self.fg_cube:
            Has 4 hour forecast period
        self.precip_cube:
            Has extra coordinate of length(3) "threshold" containing
            points [0.5, 7., 35.] mm h-1.
        """
        (_, self.fg_cube, _, self.precip_cube, _) = set_up_lightning_test_cubes()
        self.plugin = Plugin()
        self.precip_threshold_coord = find_threshold_coordinate(self.precip_cube)
Esempio n. 10
0
 def setUp(self):
     """Create cubes with a single zero prob(precip) point.
     The cubes look like this:
     precipitation_amount / (kg m^-2)
     Dimension coordinates:
         time: 1;
         projection_y_coordinate: 3;
         projection_x_coordinate: 3;
     Auxiliary coordinates:
         forecast_period (on time coord): 0.0 hours (simulates nowcast data)
     Scalar coordinates:
         forecast_reference_time: 2015-11-23 03:00:00
     Data:
     self.fg_cube:
         All points contain float(1.)
         Cube name is "probability_of_lightning".
     self.ice_cube:
         With extra coordinate of length(3) "threshold" containing
         points [0.5, 1., 2.] kg m^-2.
         Time and forecast_period dimensions "sqeezed" to be Scalar coords.
         All points contain float(0.)
         Cube name is "probability_of_vertical_integral_of_ice".
     """
     self.fg_cube = add_forecast_reference_time_and_forecast_period(
         set_up_cube_with_no_realizations(zero_point_indices=[],
                                          num_grid_points=3),
         fp_point=0.0)
     self.fg_cube.rename("probability_of_lightning")
     self.ice_cube = squeeze(
         add_forecast_reference_time_and_forecast_period(set_up_cube(
             num_realization_points=3,
             zero_point_indices=[],
             num_grid_points=3),
                                                         fp_point=0.0))
     threshold_coord = self.ice_cube.coord('realization')
     threshold_coord.points = [0.5, 1.0, 2.0]
     threshold_coord.rename('threshold')
     threshold_coord.units = cf_units.Unit('kg m^-2')
     self.ice_cube.data = np.zeros_like(self.ice_cube.data)
     self.ice_cube.rename("probability_of_vertical_integral_of_ice")
     self.plugin = Plugin()
Esempio n. 11
0
    def test_result_with_vii_longfc(self):
        """Test that the method returns the expected data when vii is
        present and forecast time is 4 hours"""
        expected = self.set_up_vii_input_output()

        # test_vii_null with no precip will now return 0.0067
        expected.data[5, 5] = 0.0067

        # test_vii_small with no and light precip will now return zero
        expected.data[7, 5:7] = 0.0

        # test_vii_large with no and light precip now return zero
        # and 0.25 for heavy precip
        expected.data[8, 5:8] = [0.0, 0.0, 0.25]
        # No halo - we're only testing this method.
        # 2000m is the grid-length, so halo includes only one pixel.
        plugin = Plugin(2000.0)
        result = plugin(
            CubeList([self.fg_cube, self.ltng_cube, self.precip_cube, self.vii_cube])
        )
        self.assertIsInstance(result, Cube)
        self.assertArrayAlmostEqual(result.data, expected.data)
Esempio n. 12
0
 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()
Esempio n. 13
0
 def setUp(self):
     """Create cubes with a single zero prob(precip) point.
     The cubes look like this:
     precipitation_amount / (kg m^-2)
     Dimension coordinates:
         time: 1;
         projection_y_coordinate: 16;
         projection_x_coordinate: 16;
     Auxiliary coordinates:
         forecast_period (on time coord): 4.0 hours (simulates UM data)
     Scalar coordinates:
         forecast_reference_time: 2015-11-23 03:00:00
     Data:
     self.fg_cube:
         All points contain float(1.)
         Cube name is "probability_of_lightning".
     self.ltng_cube:
         forecast_period (on time coord): 0.0 hours (simulates nowcast data)
         All points contain float(1.)
         Cube name is "rate_of_lightning".
         Cube units are "min^-1".
     self.precip_cube:
         With extra coordinate of length(3) "threshold" containing
         points [0.5, 7., 35.] mm hr-1.
         All points contain float(1.) except the
         zero point [0, 0, 7, 7] which is float(0.)
         and [1:, 0, ...] which are float(0.)
         Cube name is "probability_of_precipitation".
         Cube has added attribute {'relative_to_threshold': 'above'}
     self.vii_cube:
         forecast_period (on time coord): 0.0 hours (simulates nowcast data)
         With extra coordinate of length(3) "threshold" containing
         points [0.5, 1., 2.] kg m^-2.
         forecast_period (on time coord): 0.0 hours (simulates nowcast data)
         Time and forecast_period dimensions "sqeezed" to be Scalar coords.
         All points contain float(0.)
         Cube name is "probability_of_vertical_integral_of_ice".
     """
     self.fg_cube = add_forecast_reference_time_and_forecast_period(
         set_up_cube_with_no_realizations(zero_point_indices=[]))
     self.fg_cube.rename("probability_of_lightning")
     self.ltng_cube = add_forecast_reference_time_and_forecast_period(
         set_up_cube_with_no_realizations(zero_point_indices=[]),
         fp_point=0.0)
     self.ltng_cube.rename("rate_of_lightning")
     self.ltng_cube.units = cf_units.Unit("min^-1")
     self.precip_cube = (add_forecast_reference_time_and_forecast_period(
         set_up_cube(num_realization_points=3)))
     threshold_coord = self.precip_cube.coord('realization')
     threshold_coord.points = [0.5, 7.0, 35.0]
     threshold_coord.rename('threshold')
     threshold_coord.units = cf_units.Unit('mm hr-1')
     self.precip_cube.rename("probability_of_precipitation")
     self.precip_cube.attributes.update({'relative_to_threshold': 'above'})
     self.precip_cube.data[1:, 0, ...] = 0.
     self.vii_cube = squeeze(
         add_forecast_reference_time_and_forecast_period(set_up_cube(
             num_realization_points=3, zero_point_indices=[]),
                                                         fp_point=0.0))
     threshold_coord = self.vii_cube.coord('realization')
     threshold_coord.points = [0.5, 1.0, 2.0]
     threshold_coord.rename('threshold')
     threshold_coord.units = cf_units.Unit('kg m^-2')
     self.vii_cube.data = np.zeros_like(self.vii_cube.data)
     self.vii_cube.rename("probability_of_vertical_integral_of_ice")
     self.plugin = Plugin()
Esempio n. 14
0
 def setUp(self):
     """Create cubes with a single zero prob(precip) point.
     The cubes look like this:
     precipitation_amount / (kg m^-2)
     Dimension coordinates:
         time: 1;
         projection_y_coordinate: 3;
         projection_x_coordinate: 3;
     Auxiliary coordinates:
         forecast_period (on time coord): 4.0 hours (simulates UM data)
     Scalar coordinates:
         forecast_reference_time: 2015-11-23 03:00:00
     Data:
     self.cube:
         Describes the nowcast fields to be calculated.
         forecast_period (on time coord): 0.0 hours (simulates nowcast data)
         All points contain float(1.) except the
         zero point [0, 1, 1] which is float(0.)
     self.fg_cube:
         All points contain float(1.)
     self.ltng_cube:
         forecast_period (on time coord): 0.0 hours (simulates nowcast data)
         All points contain float(1.)
     self.precip_cube:
         With extra coordinate of length(3) "threshold" containing
         points [0.5, 7., 35.] mm hr-1.
         All points contain float(1.) except the
         zero point [0, 0, 1, 1] which is float(0.)
         and [1:, 0, ...] which are float(0.)
     self.vii_cube:
         With extra coordinate of length(3) "threshold" containing
         points [0.5, 1., 2.] kg m^-2.
         forecast_period (on time coord): 0.0 hours (simulates nowcast data)
         Time and forecast_period dimensions "sqeezed" to be Scalar coords.
         All points contain float(0.)
     """
     self.cube = add_forecast_reference_time_and_forecast_period(
         set_up_cube_with_no_realizations(zero_point_indices=((0, 1, 1), ),
                                          num_grid_points=3),
         fp_point=0.0)
     self.fg_cube = add_forecast_reference_time_and_forecast_period(
         set_up_cube_with_no_realizations(zero_point_indices=[],
                                          num_grid_points=3))
     self.ltng_cube = add_forecast_reference_time_and_forecast_period(
         set_up_cube_with_no_realizations(zero_point_indices=[],
                                          num_grid_points=3),
         fp_point=0.0)
     self.precip_cube = (add_forecast_reference_time_and_forecast_period(
         set_up_cube(num_realization_points=3,
                     zero_point_indices=((0, 1, 1), ),
                     num_grid_points=3),
         fp_point=0.0))
     threshold_coord = self.precip_cube.coord('realization')
     threshold_coord.points = [0.5, 7.0, 35.0]
     threshold_coord.rename('threshold')
     threshold_coord.units = cf_units.Unit('mm hr-1')
     self.precip_cube.data[1:, 0, ...] = 0.
     # iris.util.queeze is applied here to demote the singular coord "time"
     # to a scalar coord.
     self.vii_cube = squeeze(
         add_forecast_reference_time_and_forecast_period(set_up_cube(
             num_realization_points=3,
             zero_point_indices=[],
             num_grid_points=3),
                                                         fp_point=0.0))
     threshold_coord = self.vii_cube.coord('realization')
     threshold_coord.points = [0.5, 1.0, 2.0]
     threshold_coord.rename('threshold')
     threshold_coord.units = cf_units.Unit('kg m^-2')
     self.vii_cube.data = np.zeros_like(self.vii_cube.data)
     self.plugin = Plugin()