Exemple #1
0
 def test_raises_error(self):
     """Test the error is raised if time is dimensional"""
     input_cube2 = iris.util.new_axis(self.input_cube2,
                                      "forecast_reference_time")
     input_cubelist = iris.cube.CubeList([self.input_cube, input_cube2])
     msg = "Expecting scalar forecast_reference_time for each input cube"
     with self.assertRaisesRegex(ValueError, msg):
         find_latest_cycletime(input_cubelist)
Exemple #2
0
    def _rationalise_blend_time_coords(self, cubelist, cycletime=None):
        """
        Updates time coordinates on unmerged input cubes before blending
        depending on the coordinate over which the blend will be performed.
        Modifies cubes in place.

        If self.blend_coord is forecast_reference_time, ensures the cube does
        not have a forecast_period coordinate (this is recreated after
        blending). If self.weighting_coord is forecast_period, equalises
        forecast_reference_time on each cube before blending.

        Args:
            cubelist (iris.cube.CubeList):
                List of cubes containing data to be blended
            cycletime (str or None):
                The cycletime in a YYYYMMDDTHHMMZ format e.g. 20171122T0100Z

        Raises:
            ValueError: if forecast_reference_time (to be unified) is a
                dimension coordinate
        """
        if "forecast_reference_time" in self.blend_coord:
            for cube in cubelist:
                coord_names = [x.name() for x in cube.coords()]
                if "forecast_period" in coord_names:
                    cube.remove_coord("forecast_period")

        # if blending models using weights by forecast period, set forecast
        # reference times to current cycle time
        if ("model" in self.blend_coord and self.weighting_coord is not None
                and "forecast_period" in self.weighting_coord):
            cycletime = (find_latest_cycletime(cubelist) if cycletime is None
                         else cycletime_to_datetime(cycletime))
            unify_forecast_reference_time(cubelist, cycletime)
Exemple #3
0
 def test_one_input_cube(self):
     """Test the a cycletime is still found when only one input cube."""
     input_cubelist = iris.cube.CubeList([self.input_cube])
     cycletime = find_latest_cycletime(input_cubelist)
     expected_datetime = datetime.datetime(2015, 11, 23, 3, 0, 0)
     self.assertEqual(timedelta(hours=0, seconds=0),
                      cycletime - expected_datetime)
Exemple #4
0
 def test_basic(self):
     """Test the type of the output and that the input is unchanged."""
     original_cubelist = iris.cube.CubeList(
         [self.input_cube.copy(), self.input_cube2.copy()])
     cycletime = find_latest_cycletime(self.input_cubelist)
     self.assertEqual(self.input_cubelist[0], original_cubelist[0])
     self.assertEqual(self.input_cubelist[1], original_cubelist[1])
     self.assertIsInstance(cycletime, datetime.datetime)
Exemple #5
0
 def test_different_units(self):
     """Test the right cycletime is still the coords have different
        units."""
     self.input_cube2.coord("forecast_reference_time").convert_units(
         'minutes since 1970-01-01 00:00:00')
     cycletime = find_latest_cycletime(self.input_cubelist)
     expected_datetime = datetime.datetime(2015, 11, 23, 4, 0, 0)
     self.assertEqual(timedelta(hours=0, seconds=0),
                      cycletime - expected_datetime)
Exemple #6
0
 def test_two_cubes_same_reference_time(self):
     """Test the a cycletime is still found when two cubes have the same
        cycletime."""
     input_cubelist = iris.cube.CubeList(
         [self.input_cube, self.input_cube.copy()])
     cycletime = find_latest_cycletime(input_cubelist)
     expected_datetime = datetime.datetime(2015, 11, 23, 3, 0, 0)
     self.assertEqual(timedelta(hours=0, seconds=0),
                      cycletime - expected_datetime)
Exemple #7
0
    def process(self, cubelist):
        """
        Take an input cubelist containing forecasts from different cycles and
        merges them into a single cube.

        The steps taken are:
            1. If no cycletime is given then find the latest cycle time from
               the input cubes.
            2. Update the forecast periods in each input cube to be relative
               to the new cycletime.
            3. Checks if there are duplicate realization numbers. If a
               duplicate is found, renumbers all of the realizations to remove
               any duplicates.
            4. Merge cubes into one cube, removing any metadata that
               doesn't match.
        """
        if self.cycletime is None:
            cycletime = find_latest_cycletime(cubelist)
        else:
            cycletime = cycletime_to_datetime(self.cycletime)
        cubelist = unify_forecast_reference_time(cubelist, cycletime)

        # Take all the realizations from all the input cube and
        # put in one array
        all_realizations = [
            cube.coord("realization").points for cube in cubelist
        ]
        all_realizations = np.concatenate(all_realizations)
        # Find unique realizations
        unique_realizations = np.unique(all_realizations)

        # If we have fewer unique realizations than total realizations we have
        # duplicate realizations so we rebadge all realizations in the cubelist
        if len(unique_realizations) < len(all_realizations):
            first_realization = 0
            for cube in cubelist:
                n_realization = len(cube.coord("realization").points)
                cube.coord("realization").points = np.arange(
                    first_realization, first_realization + n_realization)
                first_realization = first_realization + n_realization

        # slice over realization to deal with cases where direct concatenation
        # would result in a non-monotonic coordinate
        lagged_ensemble = concatenate_cubes(
            cubelist,
            master_coord="realization",
            coords_to_slice_over=["realization"])

        return lagged_ensemble
def rationalise_blend_time_coords(cubelist,
                                  blend_coord,
                                  cycletime=None,
                                  weighting_coord=None):
    """
    Updates time coordinates on unmerged input cubes before blending depending
    on the coordinate over which the blend will be performed.  Modifies cubes
    in place.

    If blend_coord is forecast_reference_time, ensures the cube does not have
    a forecast_period dimension.  If weighting_coord is forecast_period,
    equalises forecast_reference_time on each cube before blending.

    Args:
        cubelist (iris.cube.CubeList):
            List of cubes containing data to be blended
        blend_coord (str):
            Name of coordinate over which the blend will be performed

    Kwargs:
        cycletime (str or None):
            The cycletime in a YYYYMMDDTHHMMZ format e.g. 20171122T0100Z
        weighting_coord (str or None):
            The coordinate across which weights will be scaled in a
            multi-model blend.

    Raises:
        ValueError: if forecast_reference_time (to be unified) is a
            dimension coordinate
    """
    if "forecast_reference_time" in blend_coord:
        for cube in cubelist:
            coord_names = [x.name() for x in cube.coords()]
            if "forecast_period" in coord_names:
                cube.remove_coord("forecast_period")

    # if blending models using weights by forecast period, set forecast
    # reference times to current cycle time
    if ("model" in blend_coord and weighting_coord is not None
            and "forecast_period" in weighting_coord):
        if cycletime is None:
            cycletime = find_latest_cycletime(cubelist)
        else:
            cycletime = cycletime_to_datetime(cycletime)
        cubelist = unify_forecast_reference_time(cubelist, cycletime)
Exemple #9
0
 def test_returns_latest(self):
     """Test the returned cycle time is the latest in the input cubelist."""
     cycletime = find_latest_cycletime(self.input_cubelist)
     expected_datetime = datetime.datetime(2015, 11, 23, 4, 0, 0)
     self.assertEqual(timedelta(hours=0, seconds=0),
                      cycletime - expected_datetime)