Exemple #1
0
    def test_two_region_time_aggregation(self):
        """Two regions, time aggregation by region is required
        """
        data = np.array(
            [
                # area a, months 1-12
                [
                    31,
                    28,
                    31,
                    30,
                    31,
                    30,
                    31,
                    31,
                    30,
                    31,
                    30,
                    31,
                ],
                # area b, months 1-12
                [
                    31 + 1,
                    28 + 1,
                    31 + 1,
                    30 + 1,
                    31 + 1,
                    30 + 1,
                    31 + 1,
                    31 + 1,
                    30 + 1,
                    31 + 1,
                    30 + 1,
                    31 + 1,
                ]
            ],
            dtype=float)

        convertor = SpaceTimeConvertor()
        actual = convertor.convert(data, 'half_squares', 'half_squares',
                                   'months', 'seasons')

        expected = np.array([[
            31 + 31 + 28,
            31 + 30 + 31,
            30 + 31 + 31,
            30 + 31 + 30,
        ],
                             [
                                 31 + 31 + 28 + 3,
                                 31 + 30 + 31 + 3,
                                 30 + 31 + 31 + 3,
                                 30 + 31 + 30 + 3,
                             ]],
                            dtype=float)

        assert np.allclose(actual, expected)
Exemple #2
0
    def test_one_region_convert_from_hour_to_day(self):
        """One region, time aggregation required
        """

        data = np.ones((1, 24))  # area a, hours 0-23
        convertor = SpaceTimeConvertor()
        actual = convertor.convert(data, 'half_squares', 'half_squares',
                                   'hourly_day', 'one_day')
        expected = np.array([[24]])  # area a, day 0
        assert np.allclose(actual, expected)
Exemple #3
0
    def test_one_region_convert_from_hour_to_day(self):
        """Two regions aggregated to one, one interval
        """

        data = np.array([[24], [24]])  # area a,b, single interval

        convertor = SpaceTimeConvertor()
        actual = convertor.convert(data, 'half_squares', 'rect', 'one_day',
                                   'one_day')
        expected = np.array([[48]])  # area zero, single interval
        assert np.allclose(actual, expected)
Exemple #4
0
    def test_half_to_one_region_pass_through_time(self):
        """Convert from half a region to one region, pass through time

        """

        data = np.ones((2, 12)) / 2  # area a,b, months 1-12

        convertor = SpaceTimeConvertor()
        actual = convertor.convert(data, 'half_squares', 'rect', 'months',
                                   'months')
        expected = np.ones((1, 12))  # area zero, months 1-12
        assert np.allclose(actual, expected)
Exemple #5
0
    def test_remap_months_to_timeslices(self):
        """One region, time remapping required
        """
        data = np.array([[31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]],
                        dtype=float)

        convertor = SpaceTimeConvertor()
        actual = convertor.convert(data, 'half_squares', 'half_squares',
                                   'months', 'remap_months')
        expected = np.array(
            [[30 + 31 + 31, 28 + 31 + 30, 31 + 31 + 30, 30 + 31 + 31]],
            dtype=float)
        assert np.allclose(actual, expected)
Exemple #6
0
    def test_one_region_time_aggregation(self):
        """Only one region, time aggregation is required
        """
        data = np.array([[31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]],
                        dtype=float)  # area a, months 1-12

        expected = np.array(
            [[31 + 31 + 28, 31 + 30 + 31, 30 + 31 + 31, 30 + 31 + 30]],
            dtype=float)  # area a, seasons 1-4

        convertor = SpaceTimeConvertor()

        actual = convertor.convert(data, 'half_squares', 'half_squares',
                                   'months', 'seasons')
        assert np.allclose(actual, expected)
Exemple #7
0
    def _default_convert(self, data):
        """Convert dependency data

        Arguments
        ---------
        data : numpy.ndarray
            The data series for conversion
        """
        from_units = self.source.units
        to_units = self.sink.units

        if from_units != to_units:
            self.logger.debug("Unit conversion: %s -> %s", from_units, to_units)
            convertor = UnitConvertor()
            data = convertor.convert(data, from_units, to_units)

        from_spatial = self.source.spatial_resolution.name
        to_spatial = self.sink.spatial_resolution.name
        from_temporal = self.source.temporal_resolution.name
        to_temporal = self.sink.temporal_resolution.name

        if from_spatial != to_spatial or from_temporal != to_temporal:
            self.logger.debug("Spacetime conversion: %s -> %s, %s -> %s",
                              from_spatial, to_spatial, from_temporal, to_temporal)
            convertor = SpaceTimeConvertor()
            data = convertor.convert(data, from_spatial, to_spatial, from_temporal, to_temporal)

        return data
Exemple #8
0
    def _convert_data(self, data, to_spatial_resolution,
                      to_temporal_resolution):
        """Convert data from one spatial and temporal resolution to another

        Parameters
        ----------
        data : numpy.ndarray
            The data series for conversion
        to_spatial_resolution : smif.convert.register.ResolutionSet
        to_temporal_resolution : smif.convert.register.ResolutionSet

        Returns
        -------
        converted_data : numpy.ndarray
            The converted data series

        """
        convertor = SpaceTimeConvertor()
        return convertor.convert(data, self.source.spatial_resolution.name,
                                 to_spatial_resolution,
                                 self.source.temporal_resolution.name,
                                 to_temporal_resolution)