Exemple #1
0
 def test_attribute_mismatch(self):
     """Test a mismatch in the presence of the model_id_attr attribute."""
     self.cube1 = self.cube.copy()
     self.cube2 = self.cube.copy()
     self.cube1.attributes["mosg__model_configuration"] = "uk_ens"
     msg = "Expected to find mosg__model_configuration attribute on all cubes"
     with self.assertRaisesRegex(AttributeError, msg):
         update_model_id_attr_attribute([self.cube1, self.cube2], self.model_id_attr)
    def create_symbol_cube(self, cubes: Union[List[Cube], CubeList]) -> Cube:
        """
        Create an empty weather symbol cube

        Args:
            cubes:
                List of input cubes used to generate weather symbols

        Returns:
            A cube with suitable metadata to describe the weather symbols
            that will fill it and data initiated with the value -1 to allow
            any unset points to be readily identified.
        """
        threshold_coord = find_threshold_coordinate(self.template_cube)
        template_cube = next(self.template_cube.slices_over([threshold_coord
                                                             ])).copy()
        # remove coordinates and bounds that do not apply to weather symbols
        template_cube.remove_coord(threshold_coord)

        mandatory_attributes = generate_mandatory_attributes(cubes)
        optional_attributes = weather_code_attributes()
        if self.model_id_attr:
            optional_attributes.update(
                update_model_id_attr_attribute(cubes, self.model_id_attr))

        symbols = create_new_diagnostic_cube(
            "weather_code",
            "1",
            template_cube,
            mandatory_attributes,
            optional_attributes=optional_attributes,
            data=np.ma.masked_all_like(template_cube.data).astype(np.int32),
        )
        return symbols
Exemple #3
0
 def test_two_matching_input_attributes(self):
     """Test handling of model_id_attr attribute for two matching inputs."""
     self.cube.attributes["mosg__model_configuration"] = "uk_ens"
     self.cube1 = self.cube.copy()
     self.cube2 = self.cube.copy()
     result = update_model_id_attr_attribute([self.cube1, self.cube2],
                                             self.model_id_attr)
     self.assertArrayEqual(result["mosg__model_configuration"], "uk_ens")
Exemple #4
0
 def test_two_different_input_attributes(self):
     """Test handling of model_id_attr attribute for two different inputs."""
     self.cube1 = self.cube.copy()
     self.cube2 = self.cube.copy()
     self.cube1.attributes["mosg__model_configuration"] = "uk_ens"
     self.cube2.attributes["mosg__model_configuration"] = "nc_det"
     result = update_model_id_attr_attribute([self.cube1, self.cube2],
                                             self.model_id_attr)
     self.assertArrayEqual(result["mosg__model_configuration"],
                           "nc_det uk_ens")
Exemple #5
0
 def test_compound_attributes(self):
     """Test handling of compound attributes."""
     self.cube1 = self.cube.copy()
     self.cube2 = self.cube.copy()
     self.cube1.attributes["mosg__model_configuration"] = "uk_det uk_ens"
     self.cube2.attributes[
         "mosg__model_configuration"] = "nc_det uk_det uk_ens"
     result = update_model_id_attr_attribute([self.cube1, self.cube2],
                                             self.model_id_attr)
     self.assertArrayEqual(result["mosg__model_configuration"],
                           "nc_det uk_det uk_ens")
Exemple #6
0
    def _calculate_freezing_rain_probability(self) -> Cube:
        """Calculate the probability of freezing rain from the probabilities
        of rain and sleet rates or accumulations, and the provided probabilities
        of temperature being below the freezing point of water.

        (probability of rain + probability of sleet) x (probability T < 0C)

        Returns:
            Cube of freezing rain probabilities.
        """
        freezing_rain_prob = (self.rain.data +
                              self.sleet.data) * self.temperature.data
        diagnostic_name = self.sleet.name().replace("sleet", "freezing_rain")
        threshold_name = (self.sleet.coord(
            var_name="threshold").name().replace("sleet", "freezing_rain"))
        mandatory_attributes = generate_mandatory_attributes(
            CubeList([self.rain, self.sleet]))
        optional_attributes = {}
        if self.model_id_attr:
            # Rain and sleet will always be derived from the same model, but temperature
            # may be diagnosed from a different model when creating a nowcast forecast.
            # The output in such a case is fundamentally a nowcast product, so we exclude
            # the temperature diagnostic when determining the model_id_attr.
            optional_attributes = update_model_id_attr_attribute(
                CubeList([self.rain, self.sleet]), self.model_id_attr)
        freezing_rain_cube = create_new_diagnostic_cube(
            diagnostic_name,
            "1",
            template_cube=self.sleet,
            mandatory_attributes=mandatory_attributes,
            optional_attributes=optional_attributes,
            data=freezing_rain_prob,
        )
        freezing_rain_cube.coord(var_name="threshold").rename(threshold_name)
        freezing_rain_cube.coord(threshold_name).var_name = "threshold"
        return freezing_rain_cube
Exemple #7
0
 def test_one_input_attribute(self):
     """Test handling of model_id_attr attribute for one input."""
     self.cube.attributes["mosg__model_configuration"] = "uk_ens"
     result = update_model_id_attr_attribute([self.cube],
                                             self.model_id_attr)
     self.assertArrayEqual(result["mosg__model_configuration"], "uk_ens")