Пример #1
0
def set_up_wxcubes_global():
    """Set up cubes required for Weather Symbols """
    data_snow = np.array([
        0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
        0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0
    ]).reshape(3, 1, 3, 3)
    snowfall_rate = (set_up_probability_above_threshold_cube(
        data_snow,
        'lwe_snowfall_rate',
        'm s-1',
        forecast_thresholds=np.array(
            [8.33333333e-09, 2.77777778e-08, 2.77777778e-07])))

    data_rain = np.array([
        0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0,
        0.0, 1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0
    ]).reshape(3, 1, 3, 3)
    rainfall_rate = (set_up_probability_above_threshold_cube(
        data_rain,
        'rainfall_rate',
        'm s-1',
        forecast_thresholds=np.array(
            [8.33333333e-09, 2.77777778e-08, 2.77777778e-07])))

    data_cloud = np.array([
        0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0,
        1.0, 0.0, 0.0, 1.0
    ]).reshape(2, 1, 3, 3)
    cloud = (set_up_probability_above_threshold_cube(
        data_cloud,
        'cloud_area_fraction',
        '1',
        forecast_thresholds=np.array([0.1875, 0.8125])))

    data_cld_1000ft = np.array([0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0,
                                0.0]).reshape(1, 1, 3, 3)
    cloud_below_1000ft = (set_up_probability_above_threshold_cube(
        data_cld_1000ft, 'cloud_area_fraction_assuming_only'
        '_consider_surface_to_1000_feet_asl',
        '1',
        forecast_thresholds=np.array([0.85])))

    data_vis = np.array([
        0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 0.0,
        0.0, 0.0, 1.0, 0.0
    ]).reshape(2, 1, 3, 3)
    visibility = (set_up_probability_above_threshold_cube(
        data_vis,
        'visibility_in_air',
        'm',
        forecast_thresholds=np.array([1000.0, 5000.0])))
    visibility.attributes['relative_to_threshold'] = 'below'

    cubes = iris.cube.CubeList(
        [snowfall_rate, rainfall_rate, cloud, cloud_below_1000ft, visibility])
    return cubes
    def test_lots_of_probability_thresholds(self):
        """
        Test that the plugin returns an Iris.cube.Cube with the expected
        data values for the percentiles, if there are lots of thresholds.
        """
        input_probs_1d = np.linspace(1, 0, 30)
        input_probs = np.tile(input_probs_1d, (3, 3, 1, 1)).T

        data = np.array([[[[2.9, 2.9, 2.9], [2.9, 2.9, 2.9], [2.9, 2.9, 2.9]]],
                         [[[14.5, 14.5, 14.5], [14.5, 14.5, 14.5],
                           [14.5, 14.5, 14.5]]],
                         [[[26.1, 26.1, 26.1], [26.1, 26.1, 26.1],
                           [26.1, 26.1, 26.1]]]])

        temperature_values = np.arange(0, 30)
        cube = (add_forecast_reference_time_and_forecast_period(
            set_up_probability_above_threshold_cube(
                input_probs,
                "air_temperature",
                "1",
                forecast_thresholds=temperature_values)))
        percentiles = [10, 50, 90]
        bounds_pairing = (-40, 50)
        plugin = Plugin()
        result = plugin._probabilities_to_percentiles(cube, percentiles,
                                                      bounds_pairing)
        self.assertArrayAlmostEqual(result.data, data)
    def test_probabilities_not_monotonically_increasing(self):
        """
        Test that the plugin raises a ValueError when the probabilities
        of the Cumulative Distribution Function are not monotonically
        increasing.
        """
        data = np.array([0.05, 0.7, 0.95])
        data = data[:, np.newaxis, np.newaxis, np.newaxis]

        self.current_temperature_forecast_cube = (
            add_forecast_reference_time_and_forecast_period(
                set_up_probability_above_threshold_cube(
                    data,
                    "air_temperature",
                    "1",
                    forecast_thresholds=[8, 10, 12],
                    y_dimension_length=1,
                    x_dimension_length=1)))
        cube = self.current_temperature_forecast_cube
        percentiles = [10, 50, 90]
        bounds_pairing = (-40, 50)
        plugin = Plugin()
        msg = "The probability values used to construct the"
        with self.assertRaisesRegexp(ValueError, msg):
            plugin._probabilities_to_percentiles(cube, percentiles,
                                                 bounds_pairing)
    def test_check_data_multiple_timesteps(self):
        """
        Test that the plugin returns an Iris.cube.Cube with the expected
        data values for the percentiles.
        """
        expected = np.array([[[[8., 8.], [-8., 8.66666667]],
                              [[8., -16.], [8., -16.]]],
                             [[[12., 12.], [12., 12.]],
                              [[10.5, 10.], [10.5, 10.]]],
                             [[[31., 31.], [31., 31.]],
                              [[11.5, 11.33333333], [11.5, 12.]]]])

        data = np.array([[[[0.8, 0.8], [0.7, 0.9]], [[0.8, 0.6], [0.8, 0.6]]],
                         [[[0.6, 0.6], [0.6, 0.6]], [[0.5, 0.4], [0.5, 0.4]]],
                         [[[0.4, 0.4], [0.4, 0.4]], [[0.1, 0.1], [0.1, 0.2]]]])

        cube = set_up_probability_above_threshold_cube(data,
                                                       "air_temperature",
                                                       "degreesC",
                                                       timesteps=2,
                                                       x_dimension_length=2,
                                                       y_dimension_length=2)
        self.probability_cube = (
            add_forecast_reference_time_and_forecast_period(
                cube,
                time_point=np.array([402295.0, 402296.0]),
                fp_point=[2.0, 3.0]))
        cube = self.probability_cube
        percentiles = [20, 60, 80]
        bounds_pairing = (-40, 50)
        plugin = Plugin()
        result = plugin._probabilities_to_percentiles(cube, percentiles,
                                                      bounds_pairing)
        self.assertArrayAlmostEqual(result.data, expected)
    def test_simple_check_data_below(self):
        """
        Test that the plugin returns an Iris.cube.Cube with the expected
        data values for the percentiles when input probabilities are given
        for being below a threshold.

        The input cube contains probabilities that values are below a given
        threshold.
        """
        expected = np.array([8.4, 10.61538462, 11.84615385])
        expected = expected[:, np.newaxis, np.newaxis, np.newaxis]

        data = np.array([0.95, 0.3, 0.05])[::-1]
        data = data[:, np.newaxis, np.newaxis, np.newaxis]

        self.current_temperature_forecast_cube = (
            add_forecast_reference_time_and_forecast_period(
                set_up_probability_above_threshold_cube(
                    data,
                    "air_temperature",
                    "1",
                    forecast_thresholds=[8, 10, 12],
                    y_dimension_length=1,
                    x_dimension_length=1)))
        cube = self.current_temperature_forecast_cube
        cube.attributes["relative_to_threshold"] = "below"
        percentiles = [10, 50, 90]
        bounds_pairing = (-40, 50)
        plugin = Plugin()
        result = plugin._probabilities_to_percentiles(cube, percentiles,
                                                      bounds_pairing)
        self.assertArrayAlmostEqual(result.data, expected)
Пример #6
0
    def test_probabilities_not_monotonically_increasing(
            self, warning_list=None):
        """
        Test that the plugin raises a Warning when the probabilities
        of the Cumulative Distribution Function are not monotonically
        increasing.
        """
        data = np.array([0.05, 0.7, 0.95])
        data = data[:, np.newaxis, np.newaxis, np.newaxis]

        self.current_temperature_forecast_cube = (
            add_forecast_reference_time_and_forecast_period(
                set_up_probability_above_threshold_cube(
                    data,
                    "air_temperature",
                    "degreesC",
                    forecast_thresholds=[8, 10, 12],
                    y_dimension_length=1,
                    x_dimension_length=1)))
        cube = self.current_temperature_forecast_cube
        percentiles = [10, 50, 90]
        bounds_pairing = (-40, 50)
        plugin = Plugin()
        warning_msg = "The probability values used to construct the"
        plugin._probabilities_to_percentiles(cube, percentiles, bounds_pairing)
        self.assertTrue(any(warning_msg in str(item) for item in warning_list))
Пример #7
0
    def test_simple_check_data(self):
        """
        Test that the plugin returns an Iris.cube.Cube with the expected
        data values for the percentiles.

        The input cube contains probabilities greater than a given threshold.
        """
        expected = np.array([8.15384615, 9.38461538, 11.6])
        expected = expected[:, np.newaxis, np.newaxis, np.newaxis]

        data = np.array([0.95, 0.3, 0.05])
        data = data[:, np.newaxis, np.newaxis, np.newaxis]

        self.current_temperature_forecast_cube = (
            add_forecast_reference_time_and_forecast_period(
                set_up_probability_above_threshold_cube(
                    data,
                    "air_temperature",
                    "1",
                    forecast_thresholds=[8, 10, 12],
                    y_dimension_length=1,
                    x_dimension_length=1)))
        cube = self.current_temperature_forecast_cube
        percentiles = [10, 50, 90]
        bounds_pairing = (-40, 50)
        plugin = Plugin()
        result = plugin._probabilities_to_percentiles(cube, percentiles,
                                                      bounds_pairing)
        self.assertArrayAlmostEqual(result.data, expected)
 def setUp(self):
     """Set up cube """
     data = np.array([
         0.1, 0.3, 0.4, 0.2, 0.6, 0.7, 0.4, 0.2, 0.1, 0.2, 0.2, 0.5, 0.1,
         0.3, 0.9, 0.8, 0.5, 0.3, 0.6, 0.3, 0.5, 0.6, 0.8, 0.2, 0.8, 0.1,
         0.2
     ]).reshape(3, 1, 3, 3)
     self.cube = set_up_probability_above_threshold_cube(
         data, 'air_temperature', 'K')
     self.wxcode = np.array(WX_DICT.keys())
     self.wxmeaning = " ".join(WX_DICT.values())