Exemplo n.º 1
0
 def test_coordinate_not_found_exception(self):
     """Test an exception is raised if any of the temporal coordinates are
     missing."""
     self.ref_cube.remove_coord('time')
     msg = "Expected to find exactly 1 time coordinate, but found none."
     with self.assertRaisesRegex(CoordinateNotFoundError, msg):
         time_coords_match(self.ref_cube, self.ref_cube.copy())
Exemplo n.º 2
0
 def test_forecast_reference_time_mismatch_with_exception(self):
     """Test raises exception when cubes forecast reference times do not
     match and raise_exception=True."""
     cube_different_frt = set_up_variable_cube(self.data,
                                               frt=datetime(
                                                   2017, 11, 10, 1, 0))
     msg = ("The following coordinates of the two cubes do not match:"
            " forecast_period, forecast_reference_time")
     with self.assertRaisesRegex(ValueError, msg):
         time_coords_match(self.ref_cube,
                           cube_different_frt,
                           raise_exception=True)
Exemplo n.º 3
0
 def test_validity_time_mismatch(self):
     """Test returns False when cubes validity times do not match."""
     cube_different_vt = set_up_variable_cube(self.data,
                                              time=datetime(
                                                  2017, 11, 10, 5, 0))
     result = time_coords_match(self.ref_cube, cube_different_vt)
     self.assertFalse(result)
Exemplo n.º 4
0
 def test_match_with_raise_exception_option(self):
     """Test returns True when cubes time coordinates match. In this case
     the raise_exception option is True but we do not expect a exception."""
     result = time_coords_match(self.ref_cube,
                                self.ref_cube.copy(),
                                raise_exception=True)
     self.assertTrue(result)
Exemplo n.º 5
0
 def test_forecast_reference_time_mismatch(self):
     """Test returns False when cubes forecast reference times do not
     match."""
     cube_different_frt = set_up_variable_cube(self.data,
                                               frt=datetime(
                                                   2017, 11, 10, 1, 0))
     result = time_coords_match(self.ref_cube, cube_different_frt)
     self.assertFalse(result)
Exemplo n.º 6
0
 def test_match(self):
     """Test returns True when cubes time coordinates match."""
     result = time_coords_match(self.ref_cube, self.ref_cube.copy())
     self.assertTrue(result)
Exemplo n.º 7
0
    def process(self, current_forecast, coefficients_cube, landsea_mask=None):
        """
        Apply the EMOS coefficients to the current forecast, in order to
        generate location and scale parameters for creating the calibrated
        distribution.

        Args:
            current_forecast (iris.cube.Cube):
                The cube containing the current forecast.
            coefficients_cube (iris.cube.Cube):
                Cube containing the coefficients estimated using EMOS.
                The cube contains a coefficient_index dimension coordinate
                where the points of the coordinate are integer values and a
                coefficient_name auxiliary coordinate where the points of
                the coordinate are e.g. gamma, delta, alpha, beta.
            landsea_mask (iris.cube.Cube or None):
                The optional cube containing a land-sea mask. If provided sea
                points will be masked in the output cube.
                This cube needs to have land points set to 1 and
                sea points to 0.

        Returns:
            (tuple): tuple containing:
                **location_parameter_cube** (iris.cube.Cube):
                    Cube containing the location parameter of the calibrated
                    distribution calculated using either the ensemble mean or
                    the ensemble realizations. The location parameter
                    represents the point at which a resulting PDF would be
                    centred.
                **scale_parameter_cube** (iris.cube.Cube):
                    Cube containing the scale parameter of the calibrated
                    distribution calculated using either the ensemble mean or
                    the ensemble realizations. The scale parameter represents
                    the statistical dispersion of the resulting PDF, so a
                    larger scale parameter will result in a broader PDF.

        """
        self.current_forecast = current_forecast
        self.coefficients_cube = coefficients_cube

        # Check coefficients_cube and forecast cube are compatible.
        time_coords_match(self.current_forecast, self.coefficients_cube)
        self._spatial_domain_match()

        optimised_coeffs = (
            dict(zip(self.coefficients_cube.coord("coefficient_name").points,
                     self.coefficients_cube.data)))

        if self.predictor.lower() == "mean":
            location_parameter = (
                self._calculate_location_parameter_from_mean(optimised_coeffs))
        else:
            location_parameter = (
                self._calculate_location_parameter_from_realizations(
                    optimised_coeffs))

        scale_parameter = self._calculate_scale_parameter(optimised_coeffs)

        location_parameter_cube, scale_parameter_cube = (
            self._create_output_cubes(location_parameter, scale_parameter))

        # Use a mask to confine calibration to land regions by masking the
        # sea.
        if landsea_mask:
            # Calibration is applied to all grid points, but the areas
            # where a mask is valid is then masked out at the end. The cube
            # containing a land-sea mask has sea points defined as zeroes and
            # the land points as ones, so the mask needs to be flipped here.
            flip_mask = np.logical_not(landsea_mask.data)
            scale_parameter_cube.data = np.ma.masked_where(
                flip_mask, scale_parameter_cube.data)
            location_parameter_cube.data = np.ma.masked_where(
                flip_mask, location_parameter_cube.data)

        return location_parameter_cube, scale_parameter_cube