Ejemplo n.º 1
0
    def setUpClass(cls):

        cls.eopatch = EOPatch.load(os.path.join(os.path.dirname(os.path.realpath(__file__)),
                                                '../../../example_data/TestEOPatch'))

        dates = cls.eopatch.timestamp
        scalar_array = np.arange(10 * 6, dtype=np.float).reshape(10, 6)
        mask_array = np.arange(5*3*2*1, dtype=np.uint16).reshape(5, 3, 2, 1)
        data_timeless_array = np.arange(3*2*5, dtype=np.float64).reshape(3, 2, 5)
        data_array = np.arange(10 * 3 * 2 * 6, dtype=np.float32).reshape(10, 3, 2, 6)

        cls.test_cases = [
            cls.TestCase('scalar_timeless', FeatureType.SCALAR_TIMELESS, np.arange(3)),
            cls.TestCase('scalar_timeless_list', FeatureType.SCALAR_TIMELESS, np.arange(5), bands=[3, 0, 2]),
            cls.TestCase('scalar_timeless_tuple', FeatureType.SCALAR_TIMELESS, np.arange(6), bands=(1, 4)),
            cls.TestCase('scalar_band_single_time_single', FeatureType.SCALAR, scalar_array, bands=[3], times=[7]),
            cls.TestCase('scalar_band_list_time_list', FeatureType.SCALAR, scalar_array,
                         bands=[2, 4, 1, 0], times=[1, 7, 0, 2, 3]),
            cls.TestCase('scalar_band_tuple_time_tuple', FeatureType.SCALAR, scalar_array, bands=(1, 4), times=(2, 8)),
            cls.TestCase('mask_timeless', FeatureType.MASK_TIMELESS, np.arange(3*3*1).reshape(3, 3, 1)),
            cls.TestCase('mask_single', FeatureType.MASK, mask_array, times=[4]),
            cls.TestCase('mask_list', FeatureType.MASK, mask_array, times=[4, 2]),
            cls.TestCase('mask_tuple_int', FeatureType.MASK, mask_array, times=(2, 4)),
            cls.TestCase('mask_tuple_datetime', FeatureType.MASK, mask_array, times=(dates[2], dates[4]),
                         expected_times=(2, 4)),
            cls.TestCase('mask_tuple_string', FeatureType.MASK, mask_array,
                         times=(datetime_to_iso(dates[2], only_date=False), datetime_to_iso(dates[4], only_date=False)),
                         expected_times=(2, 4)),
            cls.TestCase('data_timeless_band_list', FeatureType.DATA_TIMELESS, data_timeless_array, bands=[2, 4, 1, 0]),
            cls.TestCase('data_timeless_band_tuple', FeatureType.DATA_TIMELESS, data_timeless_array, bands=(1, 4)),
            cls.TestCase('data_band_list_time_list', FeatureType.DATA, data_array,
                         bands=[2, 4, 1, 0], times=[1, 7, 0, 2, 3]),
            cls.TestCase('data_band_tuple_time_tuple', FeatureType.DATA, data_array, bands=(1, 4), times=(2, 8)),
            cls.TestCase('', FeatureType.DATA, data_array),
        ]
Ejemplo n.º 2
0
    def test_export2tiff_mask_tuple_string(self):
        eop = EOPatch.load(self.PATCH_FILENAME)
        dates = np.array(eop.timestamp)

        mask = np.arange(len(dates) * 3 * 2 * 1).reshape(len(dates), 3, 2, 1)
        eop.mask['mask'] = mask

        indices = [2, 4]

        # day time gets floored
        times = (datetime_to_iso(dates[indices[0]]),
                 datetime_to_iso(dates[indices[1]]))

        selection = np.nonzero(
            np.where((dates >= iso_to_datetime(times[0])) &
                     (dates <= iso_to_datetime(times[1])), dates, 0))

        subset = mask[selection].squeeze()

        with tempfile.TemporaryDirectory() as tmp_dir_name:
            tmp_file_name = 'temp_file.tiff'
            task = ExportToTiff((FeatureType.MASK, 'mask'),
                                folder=tmp_dir_name,
                                date_indices=times)
            task.execute(eop, filename=tmp_file_name)

            # rasterio saves `bands` to the last dimension, move it up front
            raster = read_data(os.path.join(tmp_dir_name, tmp_file_name))
            raster = np.moveaxis(raster, -1, 0)

            self.assertTrue(np.all(subset == raster))
Ejemplo n.º 3
0
    def _prepare_request_data(self, eopatch, bbox, time_interval):
        """ Collects all parameters used for DataRequest, each one is taken either from initialization parameters or
        from EOPatch
        """

        service_type = ServiceType(self._get_parameter('service_type', eopatch))
        if time_interval is None:
            time_interval = self._get_parameter('time_interval', eopatch)
        if service_type is ServiceType.WMS:
            size_x_name, size_y_name = 'width', 'height'
        else:
            size_x_name, size_y_name = 'resx', 'resy'
        return {
            'layer': self.layer,
            'bbox': bbox if bbox is not None else self._get_parameter('bbox', eopatch),
            'time': datetime_to_iso(time_interval) if isinstance(time_interval, str) else [
                datetime_to_iso(x) if not isinstance(x, str) else x for x in time_interval],
            'time_difference': self._get_parameter('time_difference', eopatch),
            'maxcc': self._get_parameter('maxcc', eopatch),
            'image_format': self.image_format,
            'custom_url_params': self.custom_url_params,
            'data_source': self.data_source,
            'instance_id': self.instance_id,
            size_x_name: self._get_parameter('size_x', eopatch),
            size_y_name: self._get_parameter('size_y', eopatch)
        }, service_type
Ejemplo n.º 4
0
 def test_datetime_to_iso(self):
     test_pairs = [
         (datetime.datetime(2018, 1, 1), '2018-01-01'),
         (datetime.datetime(2017, 2, 28), '2017-02-28')
     ]
     for date_dt, date_str in test_pairs:
         with self.subTest(msg=date_str):
             res_str = time_utils.datetime_to_iso(date_dt)
             self.assertEqual(res_str, date_str,
                              msg='Expected {}, got {}'.format(date_str, res_str))