Example #1
0
    def test_dict(self):
        """Test dictionary option for model blending with non-equal weights"""
        data = np.ones((3, 3, 3), dtype=np.float32)
        thresholds = np.array([276, 277, 278], dtype=np.float32)
        ukv_cube = set_up_probability_cube(data,
                                           thresholds,
                                           time=dt(2018, 9, 10, 7),
                                           frt=dt(2018, 9, 10, 1),
                                           standard_grid_metadata="uk_det")
        enukx_cube = set_up_probability_cube(data,
                                             thresholds,
                                             time=dt(2018, 9, 10, 7),
                                             frt=dt(2018, 9, 10, 1),
                                             standard_grid_metadata="uk_ens")
        merger = MergeCubesForWeightedBlending(
            "model_id",
            weighting_coord="forecast_period",
            model_id_attr="mosg__model_configuration")
        cube = merger.process([ukv_cube, enukx_cube])

        plugin = WeightAndBlend("model_id",
                                "dict",
                                weighting_coord="forecast_period",
                                wts_dict=MODEL_WEIGHTS)

        # at 6 hours lead time we should have 1/3 UKV and 2/3 MOGREPS-UK,
        # according to the dictionary weights specified above
        weights = plugin._calculate_blending_weights(cube)
        self.assertArrayEqual(
            weights.coord("model_configuration").points, ["uk_det", "uk_ens"])
        self.assertArrayAlmostEqual(weights.data,
                                    np.array([0.3333333, 0.6666667]))
Example #2
0
 def test_default_linear(self):
     """Test linear weighting over realizations"""
     cube = set_up_variable_cube(278.0 * np.ones((4, 3, 3), dtype=np.float32))
     plugin = WeightAndBlend("realization", "linear", y0val=1, ynval=1)
     weights = plugin._calculate_blending_weights(cube)
     self.assertIsInstance(weights, iris.cube.Cube)
     weights_dims = [coord.name() for coord in weights.coords(dim_coords=True)]
     self.assertSequenceEqual(weights_dims, ["realization"])
     self.assertArrayAlmostEqual(weights.data, 0.25 * np.ones((4,)))
class Test__update_spatial_weights(IrisTest):
    """Test the _update_spatial_weights method"""
    @ManageWarnings(ignored_messages=["Deleting unmatched attribute"])
    def setUp(self):
        """Set up cube and plugin"""
        cubelist = set_up_masked_cubes()
        merger = MergeCubesForWeightedBlending(
            "model_id",
            weighting_coord="forecast_period",
            model_id_attr="mosg__model_configuration",
        )
        self.cube = merger.process(cubelist)
        self.plugin = WeightAndBlend(
            "model_id",
            "dict",
            weighting_coord="forecast_period",
            wts_dict=MODEL_WEIGHTS,
        )
        self.initial_weights = self.plugin._calculate_blending_weights(
            self.cube)

    @ManageWarnings(
        ignored_messages=["Collapsing a non-contiguous coordinate"])
    def test_basic(self):
        """Test function returns a cube of the expected shape"""
        expected_dims = [
            "model_id",
            "projection_y_coordinate",
            "projection_x_coordinate",
        ]
        expected_shape = (2, 5, 5)
        result = self.plugin._update_spatial_weights(self.cube,
                                                     self.initial_weights,
                                                     20000)
        result_dims = [
            coord.name() for coord in result.coords(dim_coords=True)
        ]
        self.assertSequenceEqual(result_dims, expected_dims)
        self.assertSequenceEqual(result.shape, expected_shape)

    @ManageWarnings(
        ignored_messages=["Collapsing a non-contiguous coordinate"])
    def test_values(self):
        """Test weights are fuzzified as expected"""
        expected_data = np.array(
            [
                np.broadcast_to([0.5, 0.5, 0.5, 0.5, 0.5], (5, 5)),
                np.broadcast_to([0.5, 0.5, 0.25, 0.0, 0.0], (5, 5)),
            ],
            dtype=np.float32,
        )
        result = self.plugin._update_spatial_weights(self.cube,
                                                     self.initial_weights,
                                                     400000)
        self.assertArrayEqual(
            result.coord("model_configuration").points, ["uk_det", "nc_det"])
        self.assertArrayAlmostEqual(result.data, expected_data)
Example #4
0
    def test_default_nonlinear(self):
        """Test non-linear weighting over forecast reference time, where the
        earlier cycle has a higher weighting"""
        data = np.ones((3, 3, 3), dtype=np.float32)
        thresholds = np.array([276, 277, 278], dtype=np.float32)
        ukv_cube_earlier = set_up_probability_cube(
            data, thresholds, time=dt(2018, 9, 10, 7), frt=dt(2018, 9, 10, 3)
        )
        ukv_cube_later = set_up_probability_cube(
            data, thresholds, time=dt(2018, 9, 10, 7), frt=dt(2018, 9, 10, 4)
        )
        cube = iris.cube.CubeList([ukv_cube_later, ukv_cube_earlier]).merge_cube()

        plugin = WeightAndBlend("forecast_reference_time", "nonlinear", cval=0.85)
        weights = plugin._calculate_blending_weights(cube)
        self.assertArrayAlmostEqual(weights.data, np.array([0.5405405, 0.45945945]))