コード例 #1
0
    def test_preservation_of_dimensions(self):
        """Test that if the pecentiles_cube has other dimension coordinates
        over which slicing is performed, that these dimensions are properly
        restored in the resulting probability cube."""
        percentiles_cube = set_up_percentiles_cube()
        test_data = np.array([percentiles_cube.data, percentiles_cube.data])
        percentiles = percentiles_cube.coord('percentiles')
        grid_x = percentiles_cube.coord('projection_x_coordinate')
        grid_y = percentiles_cube.coord('projection_y_coordinate')

        new_model_coord = build_coordinate([0, 1],
                                           long_name='leading_coord',
                                           coord_type=DimCoord,
                                           data_type=int)
        input_cube = iris.cube.Cube(
            test_data, long_name="snow_level", units="m",
            dim_coords_and_dims=[(new_model_coord, 0),
                                 (percentiles, 1),
                                 (grid_y, 2), (grid_x, 3)])

        plugin_instance = ProbabilitiesFromPercentiles2D(
            input_cube, 'new_name')
        probability_cube = plugin_instance.process(self.orography_cube)
        self.assertEqual(input_cube.coords(dim_coords=True)[0],
                         probability_cube.coords(dim_coords=True)[0])
コード例 #2
0
 def test_use_many_keyword_arguments(self):
     """Test that a coordinate is built when most of the keyword arguments
     are specified."""
     standard_name = "height"
     long_name = "height"
     var_name = "height"
     coord_type = AuxCoord
     data_type = np.int64
     units = "m"
     bounds = np.array([0.5, 1.5])
     coord_system = TransverseMercator
     result = build_coordinate([1.0],
                               standard_name=standard_name,
                               long_name=long_name,
                               var_name=var_name,
                               coord_type=coord_type,
                               data_type=data_type,
                               units=units,
                               bounds=bounds,
                               coord_system=coord_system)
     self.assertIsInstance(result, AuxCoord)
     self.assertEqual(result.standard_name, "height")
     self.assertEqual(result.long_name, "height")
     self.assertEqual(result.var_name, "height")
     self.assertIsInstance(result.points[0], np.int64)
     self.assertEqual(result.units, Unit("m"))
     self.assertArrayAlmostEqual(result.bounds, np.array([[0.5, 1.5]]))
     self.assertArrayAlmostEqual(result.points, np.array([1.0]))
     self.assertEqual(result.coord_system, TransverseMercator)
コード例 #3
0
 def test_template_coord(self):
     """Test that a coordinate can be built from a template coordinate."""
     template_coord = DimCoord([2.0], standard_name="height", units="m")
     result = build_coordinate([5.0, 10.0], template_coord=template_coord)
     self.assertIsInstance(result, DimCoord)
     self.assertEqual(result.standard_name, "height")
     self.assertEqual(result.units, Unit("m"))
     self.assertArrayAlmostEqual(result.points, np.array([5.0, 10.0]))
コード例 #4
0
 def test_custom_function(self):
     """Test that a coordinate can be built when using a custom function."""
     def divide_data(data):
         """Basic custom function for testing in build_coordinate"""
         return data/2
     result = build_coordinate(
         [1.0], long_name="realization", custom_function=divide_data)
     self.assertArrayAlmostEqual(result.points, np.array([0.5]))
コード例 #5
0
ファイル: temporal.py プロジェクト: TomekTrzeciak/improver
def unify_forecast_reference_time(cubes, cycletime):
    """Function to unify the forecast_reference_time across the input cubes
    provided. The cycletime specified is used as the forecast_reference_time.
    This function is intended for use in grid blending, where the models
    being blended may not have been run at the same cycle time, but should
    be given the same forecast period weightings.

    Args:
        cubes (iris.cube.CubeList or iris.cube.Cube):
            Cubes that will have their forecast_reference_time unified.
            If a single cube is provided the forecast_reference_time will be
            updated. Any bounds on the forecast_reference_time coord will be
            discarded.
        cycletime (datetime.datetime):
            Datetime for the cycletime that will be used to replace the
            forecast_reference_time on the individual cubes.

    Returns:
        result_cubes (iris.cube.CubeList):
            Cubes that have had their forecast_reference_time unified.

    Raises:
        ValueError: if forecast_reference_time is a dimension coordinate
    """
    if isinstance(cubes, iris.cube.Cube):
        cubes = iris.cube.CubeList([cubes])

    result_cubes = iris.cube.CubeList([])
    for cube in cubes:
        frt_units = cube.coord('forecast_reference_time').units
        frt_type = cube.coord('forecast_reference_time').dtype
        new_frt_units = Unit('seconds since 1970-01-01 00:00:00')
        frt_points = np.round([new_frt_units.date2num(cycletime)
                               ]).astype(frt_type)
        frt_coord = build_coordinate(
            frt_points,
            standard_name="forecast_reference_time",
            bounds=None,
            template_coord=cube.coord('forecast_reference_time'),
            units=new_frt_units)
        frt_coord.convert_units(frt_units)
        frt_coord.points = frt_coord.points.astype(frt_type)
        cube.remove_coord("forecast_reference_time")
        cube.add_aux_coord(frt_coord, data_dims=None)

        # If a forecast period coordinate already exists on a cube, replace
        # this coordinate, otherwise create a new coordinate.
        fp_units = "seconds"
        if cube.coords("forecast_period"):
            fp_units = cube.coord("forecast_period").units
            cube.remove_coord("forecast_period")
        fp_coord = forecast_period_coord(cube,
                                         force_lead_time_calculation=True,
                                         result_units=fp_units)
        cube.add_aux_coord(fp_coord, data_dims=cube.coord_dims("time"))
        result_cubes.append(cube)
    return result_cubes
コード例 #6
0
    def _create_model_coordinates(self, cubelist):
        """
        Adds numerical model ID and string model configuration scalar
        coordinates to input cubes if self.model_id_attr is specified.
        Sets the original attribute value to "blend", in anticipation.
        Modifies cubes in place.

        Args:
            cubelist (iris.cube.CubeList):
                List of cubes to be merged for blending

        Raises:
            ValueError:
                If self.model_id_attr is not present on all cubes
            ValueError:
                If input cubelist contains cubes from the same model
        """
        model_titles = []
        for i, cube in enumerate(cubelist):
            if self.model_id_attr not in cube.attributes:
                msg = ('Cannot create model ID coordinate for grid blending '
                       'as "model_id_attr={}" was not found within the cube '
                       'attributes'.format(self.model_id_attr))
                raise ValueError(msg)

            model_title = cube.attributes.pop(self.model_id_attr)
            if model_title in model_titles:
                raise ValueError('Cannot create model dimension coordinate '
                                 'with duplicate points')
            model_titles.append(model_title)
            cube.attributes[self.model_id_attr] = "blend"

            new_model_id_coord = build_coordinate([1000 * i],
                                                  long_name='model_id',
                                                  data_type=np.int32)
            new_model_coord = (build_coordinate(
                [model_title],
                long_name='model_configuration',
                coord_type=AuxCoord,
                data_type=np.str))

            cube.add_aux_coord(new_model_id_coord)
            cube.add_aux_coord(new_model_coord)
コード例 #7
0
 def test_build_latitude_coordinate(self):
     """Test building a latitude coordinate."""
     latitudes = np.linspace(-90, 90, 20)
     coord_system = iris.coord_systems.GeogCS(6371229.0)
     result = build_coordinate(latitudes, long_name='latitude',
                               units='degrees',
                               coord_system=coord_system)
     self.assertArrayEqual(result.points, latitudes)
     self.assertEqual(result.name(), 'latitude')
     self.assertIsInstance(result, DimCoord)
     self.assertEqual(result.units, 'degrees')
コード例 #8
0
ファイル: forecast_times.py プロジェクト: arh89/improver
def unify_cycletime(cubes, cycletime):
    """
    Function to unify the forecast_reference_time and update forecast_period.
    The cycletime specified is used as the forecast_reference_time, and the
    forecast_period is recalculated using the time coordinate and updated
    forecast_reference_time.

    Args:
        cubes (iris.cube.CubeList or list of iris.cube.Cube):
            Cubes that will have their forecast_reference_time and
            forecast_period updated. Any bounds on the forecast_reference_time
            coordinate will be discarded.
        cycletime (datetime.datetime):
            Datetime for the cycletime that will be used to replace the
            forecast_reference_time on the individual cubes.

    Returns:
        iris.cube.CubeList:
            Updated cubes

    Raises:
        ValueError: if forecast_reference_time is a dimension coordinate
    """
    result_cubes = iris.cube.CubeList([])
    for cube in cubes:
        cube = cube.copy()
        frt_units = cube.coord('forecast_reference_time').units
        frt_type = cube.coord('forecast_reference_time').dtype
        new_frt_units = Unit(TIME_REFERENCE_UNIT)
        frt_points = np.round([new_frt_units.date2num(cycletime)
                               ]).astype(frt_type)
        frt_coord = build_coordinate(
            frt_points,
            standard_name="forecast_reference_time",
            bounds=None,
            template_coord=cube.coord('forecast_reference_time'),
            units=new_frt_units)
        frt_coord.convert_units(frt_units)
        frt_coord.points = frt_coord.points.astype(frt_type)
        cube.remove_coord("forecast_reference_time")
        cube.add_aux_coord(frt_coord, data_dims=None)

        # Update the forecast period for consistency within each cube
        if cube.coords("forecast_period"):
            cube.remove_coord("forecast_period")
        fp_coord = forecast_period_coord(cube,
                                         force_lead_time_calculation=True)
        cube.add_aux_coord(fp_coord, data_dims=cube.coord_dims("time"))
        result_cubes.append(cube)
    return result_cubes
コード例 #9
0
 def test_preservation_of_single_valued_dimension(self):
     """Test that if the pecentiles_cube has a single value dimension
     coordinate over which slicing is performed, that this coordinate is
     restored as a dimension coordinate in the resulting probability
     cube."""
     percentiles_cube = set_up_percentiles_cube()
     new_model_coord = build_coordinate([0],
                                        long_name='leading_coord',
                                        coord_type=DimCoord,
                                        data_type=int)
     percentiles_cube.add_aux_coord(new_model_coord)
     percentiles_cube = iris.util.new_axis(percentiles_cube,
                                           scalar_coord='leading_coord')
     plugin_instance = ProbabilitiesFromPercentiles2D(percentiles_cube)
     probability_cube = plugin_instance.process(self.orography_cube)
     self.assertEqual(percentiles_cube.coords(dim_coords=True)[0],
                      probability_cube.coords(dim_coords=True)[0])
コード例 #10
0
    def make_cube(self, cube, data, sites):
        """
        Construct and return a cube containing the data extracted from the
        grids by the desired method for the sites provided.

        Args:
            cube (iris.cube.Cube):
                The original diagnostic cube from which data has been
                extracted.

            data (numpy.array):
                Array of diagnostic values extracted for the defined sites.

            sites (OrderedDict):
                A dictionary containing the properties of spotdata sites.

        Returns:
            cube (iris.cube.Cube):
                An irregularly (i.e. non-gridded) cube of diagnostic data
                extracted at the spotdata sites.

        """

        # Ensure time is a dimension coordinate and convert to seconds.
        cube_coords = [coord.name() for coord in cube.dim_coords]
        if 'time' not in cube_coords:
            cube = iris.util.new_axis(cube, 'time')
        cube.coord('time').convert_units('seconds since 1970-01-01 00:00:00')

        cube_coords = [coord.name() for coord in cube.coords()]
        if 'forecast_reference_time' not in cube_coords:
            raise CoordinateNotFoundError(
                'No forecast reference time found on source cube.')
        cube.coord('forecast_reference_time').convert_units(
            'seconds since 1970-01-01 00:00:00')

        # Replicate all non spatial dimension coodinates.
        n_non_spatial_dimcoords = len(cube.dim_coords) - 2
        non_spatial_dimcoords = cube.dim_coords[0:n_non_spatial_dimcoords]
        dim_coords = [coord for coord in non_spatial_dimcoords]

        # Add an index coordinate as a dimension coordinate.
        indices = build_coordinate(np.arange(len(sites)),
                                   long_name='index',
                                   data_type=int)
        dim_coords.append(indices)

        # Record existing scalar coordinates on source cube. Aux coords
        # associated with dimensions cannot be preserved as the dimensions will
        # be reshaped and the auxiliarys no longer compatible.
        # Forecast period is ignored for the case where the input data has
        # an existing forecast_period scalar coordinate.
        scalar_coordinates = [
            coord.name() for coord in cube.coords(dimensions=[])
            if coord.name() != 'forecast_period'
        ]

        # Build a forecast_period dimension.
        forecast_periods = (cube.coord('time').points -
                            cube.coord('forecast_reference_time').points)
        forecast_period = build_coordinate(forecast_periods,
                                           long_name='forecast_period',
                                           units='seconds')

        # Build the new auxiliary coordinates.
        crds = self._aux_coords_to_make()
        aux_crds = []
        for key, kwargs in zip(crds.keys(), crds.itervalues()):
            aux_data = np.array([entry[key] for entry in sites.itervalues()])
            crd = build_coordinate(aux_data, long_name=key, **kwargs)
            aux_crds.append(crd)

        # Construct zipped lists of coordinates and indices. New aux coords are
        # associated with the index dimension.
        n_dim_coords = len(dim_coords)
        dim_coords = zip(dim_coords, range(n_dim_coords))
        aux_coords = zip(aux_crds, [n_dim_coords - 1] * len(aux_crds))

        # Copy other cube metadata.
        metadata_dict = copy.deepcopy(cube.metadata._asdict())

        # Add leading dimension for time to the data array.
        data = np.expand_dims(data, axis=0)
        result_cube = Cube(data,
                           dim_coords_and_dims=dim_coords,
                           aux_coords_and_dims=aux_coords,
                           **metadata_dict)

        # Add back scalar coordinates from the original cube.
        for coord in scalar_coordinates:
            result_cube.add_aux_coord(cube.coord(coord))

        result_cube.add_aux_coord(forecast_period, cube.coord_dims('time'))

        # Enables use of long_name above for any name, and then moves it
        # to a standard name if possible.
        result_cube.rename(cube.name())

        result_cube = enforce_coordinate_ordering(
            result_cube, ["realization", "percentile_over"])
        return result_cube
コード例 #11
0
 def test_basic(self):
     """Test that the utility returns a coord."""
     result = build_coordinate([1.0], long_name='testing')
     self.assertIsInstance(result, DimCoord)