Exemple #1
0
    def test_chunks_are_larger_than_sizes(self):
        cube1 = new_cube(variables=dict(chl=0.6, tsm=0.9, flags=16))
        for var in cube1.variables.values():
            self.assertIsNone(var.chunks)

        rc = CubeRechunker()
        cube2, gm, cc = rc.transform_cube(
            cube1, GridMapping.from_dataset(cube1),
            CubeConfig(chunks=dict(time=64, lat=512, lon=512)))

        self.assertIsInstance(cube2, xr.Dataset)
        self.assertIsInstance(gm, GridMapping)
        self.assertIsInstance(cc, CubeConfig)
        self.assertEqual(cube1.attrs, cube2.attrs)
        self.assertEqual(set(cube1.coords), set(cube2.coords))
        self.assertEqual(set(cube1.data_vars), set(cube2.data_vars))

        for k, v in cube2.coords.items():
            if v.chunks is not None:
                self.assertIsInstance(v.chunks, tuple, msg=f'{k!r}={v!r}')
                self.assertNotIn('chunks', v.encoding)
        for k, v in cube2.data_vars.items():
            self.assertIsInstance(v.chunks, tuple, msg=f'{k!r}={v!r}')
            self.assertEqual(((5, ), (180, ), (360, )), v.chunks)
            self.assertNotIn('chunks', v.encoding)
Exemple #2
0
    def test_rectify_2x2_to_13x13_output_ij_names(self):
        source_ds = self.new_2x2_dataset_with_irregular_coords()

        target_gm = GridMapping.regular(size=(13, 13),
                                        xy_min=(-0.25, 49.75),
                                        xy_res=0.5,
                                        crs=CRS_WGS84)

        target_ds = rectify_dataset(source_ds,
                                    target_gm=target_gm,
                                    output_ij_names=('source_i', 'source_j'))

        lon, lat, rad, source_i, source_j = self._assert_shape_and_dim(
            target_ds, (13, 13), var_names=('rad', 'source_i', 'source_j'))
        np.testing.assert_almost_equal(
            lon.values,
            np.array([
                0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5., 5.5, 6.
            ],
                     dtype=lon.dtype))
        np.testing.assert_almost_equal(
            lat.values,
            np.array([
                56., 55.5, 55., 54.5, 54., 53.5, 53., 52.5, 52., 51.5, 51.,
                50.5, 50.
            ],
                     dtype=lat.dtype))
        np.testing.assert_almost_equal(rad.values,
                                       self.expected_rad_13x13(rad.dtype))
        np.testing.assert_almost_equal(np.floor(source_i.values + 0.5),
                                       self.expected_i_13x13())
        np.testing.assert_almost_equal(np.floor(source_j.values + 0.5),
                                       self.expected_j_13x13())
Exemple #3
0
    def test_chunks_are_smaller_than_sizes(self):
        cube1 = new_cube(variables=dict(chl=0.6, tsm=0.9, flags=16))
        for var in cube1.variables.values():
            self.assertIsNone(var.chunks)

        rc = CubeRechunker()
        cube2, gm, cc = rc.transform_cube(
            cube1, GridMapping.from_dataset(cube1),
            CubeConfig(chunks=dict(time=2, lat=100, lon=200)))

        self.assertIsInstance(cube2, xr.Dataset)
        self.assertIsInstance(gm, GridMapping)
        self.assertIsInstance(cc, CubeConfig)
        self.assertEqual(cube1.attrs, cube2.attrs)
        self.assertEqual({'time': 2, 'lat': 100, 'lon': 200}, cc.chunks)
        self.assertEqual(set(cube1.coords), set(cube2.coords))
        self.assertEqual(set(cube1.data_vars), set(cube2.data_vars))

        for k, v in cube2.coords.items():
            if v.chunks is not None:
                self.assertIsInstance(v.chunks, tuple, msg=f'{k!r}={v!r}')
                self.assertNotIn('chunks', v.encoding)
                # self.assertIn('chunks', v.encoding)
                # self.assertEqual([v.sizes[d] for d in v.dims],
                #                  v.encoding['chunks'])
        for k, v in cube2.data_vars.items():
            self.assertIsInstance(v.chunks, tuple, msg=f'{k!r}={v!r}')
            self.assertEqual(((2, 2, 1), (100, 80), (200, 160)), v.chunks)
            self.assertNotIn('chunks', v.encoding)
Exemple #4
0
    def test_rectify_2x2_to_13x13_dask_13x3(self):
        source_ds = self.new_2x2_dataset_with_irregular_coords()

        target_gm = GridMapping.regular(size=(13, 13),
                                        xy_min=(-0.25, 49.75),
                                        xy_res=0.5,
                                        crs=CRS_WGS84,
                                        tile_size=(13, 3))

        target_ds = rectify_dataset(source_ds, target_gm=target_gm)

        lon, lat, rad = self._assert_shape_and_dim(target_ds, (13, 13),
                                                   chunks=((3, 3, 3, 3, 1),
                                                           (13, )))

        np.testing.assert_almost_equal(
            lon.values,
            np.array([
                0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5., 5.5, 6.
            ],
                     dtype=lon.dtype))
        np.testing.assert_almost_equal(
            lat.values,
            np.array([
                56., 55.5, 55., 54.5, 54., 53.5, 53., 52.5, 52., 51.5, 51.,
                50.5, 50.
            ],
                     dtype=lat.dtype))
        np.testing.assert_almost_equal(rad.values,
                                       self.expected_rad_13x13(rad.dtype))
Exemple #5
0
    def test_rectify_2x2_to_13x13_antimeridian(self):
        source_ds = self.new_2x2_dataset_with_irregular_coords_antimeridian()

        target_gm = GridMapping.regular(size=(13, 13),
                                        xy_min=(177.75, 49.75),
                                        xy_res=0.5,
                                        crs=CRS_WGS84)

        self.assertEqual(True, target_gm.is_lon_360)

        target_ds = rectify_dataset(source_ds, target_gm=target_gm)

        self.assertIsNotNone(target_ds)
        lon, lat, rad = self._assert_shape_and_dim(target_ds, (13, 13))
        np.testing.assert_almost_equal(
            lon.values,
            np.array([
                178., 178.5, 179., 179.5, 180., -179.5, -179., -178.5, -178.,
                -177.5, -177., -176.5, -176.
            ],
                     dtype=lon.dtype))
        np.testing.assert_almost_equal(
            lat.values,
            np.array([
                56., 55.5, 55., 54.5, 54., 53.5, 53., 52.5, 52., 51.5, 51.,
                50.5, 50.
            ],
                     dtype=lat.dtype))
        np.testing.assert_almost_equal(rad.values,
                                       self.expected_rad_13x13(rad.dtype))
Exemple #6
0
 def test_from_regular_cube_with_crs(self):
     dataset = xcube.core.new.new_cube(variables=dict(rad=0.5),
                                       x_start=0,
                                       y_start=0,
                                       x_name='x',
                                       y_name='y',
                                       crs='epsg:25832')
     gm1 = GridMapping.from_dataset(dataset)
     self.assertEqual(pyproj.CRS.from_string('epsg:25832'), gm1.crs)
     dataset = dataset.drop_vars('crs')
     gm2 = GridMapping.from_dataset(dataset)
     self.assertEqual(GEO_CRS, gm2.crs)
     gm3 = GridMapping.from_dataset(dataset, crs=gm1.crs)
     self.assertEqual(gm1.crs, gm3.crs)
     self.assertEqual(('x', 'y'), gm3.xy_var_names)
     self.assertEqual(('x', 'y'), gm3.xy_dim_names)
Exemple #7
0
 def test_tile_grid(self):
     gm = GridMapping.regular((7200, 3600),
                              (-180, -90),
                              360 / 7200,
                              'epsg:4326',
                              tile_size=(720, 360),
                              is_j_axis_up=True)
     tile_grid = gm.tile_grid
     self.assertIsInstance(tile_grid, TileGrid)
     self.assertEqual([(1, 1),
                       (2, 2),
                       (3, 3),
                       (5, 5),
                       (10, 10)],
                      [tile_grid.get_num_tiles(i)
                       for i in range(tile_grid.num_levels)])
     self.assertEqual([(450, 225),
                       (900, 450),
                       (1800, 900),
                       (3600, 1800),
                       (7200, 3600)],
                      [tile_grid.get_image_size(i)
                       for i in range(tile_grid.num_levels)])
     self.assertEqual([0.8,
                       0.4,
                       0.2,
                       0.1,
                       0.05],
                      [tile_grid.get_resolution(i)
                       for i in range(tile_grid.num_levels)])
Exemple #8
0
 def _new_xy_coords(self) -> xr.DataArray:
     return GridMapping.regular(size=self.size,
                                tile_size=self.tile_size,
                                is_j_axis_up=self.is_j_axis_up,
                                xy_res=self.xy_res,
                                xy_min=(self.xy_bbox[0], self.xy_bbox[1]),
                                crs=self.crs).xy_coords
Exemple #9
0
    def test_rectify_2x2_to_7x7_subset(self):
        source_ds = self.new_2x2_dataset_with_irregular_coords()

        target_gm = GridMapping.regular(size=(7, 7),
                                        xy_min=(1.5, 50.5),
                                        xy_res=1.0,
                                        crs=CRS_WGS84)

        target_ds = rectify_dataset(source_ds, target_gm=target_gm)
        lon, lat, rad = self._assert_shape_and_dim(target_ds, (7, 7))
        np.testing.assert_almost_equal(
            lon.values,
            np.array([2.0, 3.0, 4.0, 5., 6., 7., 8.], dtype=lon.dtype))
        np.testing.assert_almost_equal(
            lat.values,
            np.array([57., 56., 55., 54., 53., 52., 51.], dtype=lat.dtype))
        np.testing.assert_almost_equal(
            rad.values,
            np.array([
                [nan, nan, nan, nan, nan, nan, nan],
                [nan, nan, nan, nan, nan, nan, nan],
                [1.0, nan, nan, nan, nan, nan, nan],
                [1.0, 1.0, 2.0, nan, nan, nan, nan],
                [3.0, 1.0, 2.0, 2.0, 2.0, nan, nan],
                [3.0, 4.0, 2.0, nan, nan, nan, nan],
                [4.0, 4.0, nan, nan, nan, nan, nan],
            ],
                     dtype=rad.dtype))
Exemple #10
0
    def transform_cube(self,
                       cube: xr.Dataset,
                       gm: GridMapping,
                       cube_config: CubeConfig) -> TransformedCube:

        desired_var_names = cube_config.variable_names
        if desired_var_names:
            cube = select_variables_subset(cube,
                                           var_names=desired_var_names)
            cube_config = cube_config.drop_props('variable_names')

        desired_bbox = cube_config.bbox
        if desired_bbox is not None:
            # Find out whether its possible to make a spatial subset
            # without resampling. First, grid mapping must be regular.
            can_do_spatial_subset = False
            if gm.is_regular:
                can_do_spatial_subset = True
                # Current spatial resolution must be the
                # desired spatial resolution, otherwise spatial resampling
                # is required later, which will include the desired
                # subsetting.
                desired_res = cube_config.spatial_res
                if desired_res is not None \
                        and not (math.isclose(gm.x_res, desired_res)
                                 and math.isclose(gm.y_res, desired_res)):
                    can_do_spatial_subset = False
                if can_do_spatial_subset:
                    # Finally, the desired CRS must be equal to the current
                    # one, or they must both be geographic.
                    desired_crs = cube_config.crs
                    if desired_crs:
                        desired_crs = pyproj.CRS.from_string(desired_crs)
                        if desired_crs != gm.crs \
                                and not (desired_crs.is_geographic
                                         and gm.crs.is_geographic):
                            can_do_spatial_subset = False
            if can_do_spatial_subset:
                cube = select_spatial_subset(cube,
                                             xy_bbox=desired_bbox)
                # Now that we have a new cube subset, we must adjust
                # its grid mapping.
                gm = GridMapping.from_dataset(
                    cube,
                    crs=gm.crs,
                    xy_var_names=gm.xy_var_names,
                )
                # Consume spatial properties
                cube_config = cube_config.drop_props(['bbox',
                                                      'spatial_res',
                                                      'crs'])

        desired_time_range = cube_config.time_range
        if desired_time_range:
            cube = select_temporal_subset(cube,
                                          time_range=desired_time_range)
            cube_config = cube_config.drop_props('time_range')

        return cube, gm, cube_config
Exemple #11
0
    def test_rectify_dataset(self):
        source_ds = create_s2plus_dataset()

        expected_data = np.array([
            [nan, nan, nan, nan, nan, nan, nan, nan, nan],
            [
                nan, 0.019001, 0.019001, 0.008999, 0.012001, 0.012001,
                0.022999, nan, nan
            ],
            [
                nan, 0.021, 0.021, 0.009998, 0.009998, 0.008999, 0.022999, nan,
                nan
            ],
            [
                nan, 0.022999, 0.022999, 0.007999, 0.007999, 0.008999,
                0.023998, nan, nan
            ],
            [nan, 0.022999, 0.022999, 0.007, 0.007, 0.009998, 0.021, nan, nan],
            [nan, nan, nan, nan, nan, nan, nan, nan, nan]
        ])

        source_gm = GridMapping.from_dataset(source_ds, prefer_crs=CRS_WGS84)

        target_ds = rectify_dataset(source_ds,
                                    source_gm=source_gm,
                                    tile_size=None)
        self.assertEqual(None, target_ds.rrs_665.chunks)
        print(repr(target_ds.rrs_665.values))
        np.testing.assert_almost_equal(target_ds.rrs_665.values,
                                       expected_data,
                                       decimal=3)

        target_ds = rectify_dataset(source_ds,
                                    source_gm=source_gm,
                                    tile_size=5)
        self.assertEqual(((5, 1), (5, 4)), target_ds.rrs_665.chunks)
        np.testing.assert_almost_equal(target_ds.rrs_665.values,
                                       expected_data,
                                       decimal=3)

        target_ds = rectify_dataset(source_ds,
                                    source_gm=source_gm,
                                    tile_size=None,
                                    is_j_axis_up=True)
        self.assertEqual(None, target_ds.rrs_665.chunks)
        np.testing.assert_almost_equal(target_ds.rrs_665.values,
                                       expected_data[::-1],
                                       decimal=3)

        target_ds = rectify_dataset(source_ds,
                                    source_gm=source_gm,
                                    tile_size=5,
                                    is_j_axis_up=True)
        self.assertEqual(((5, 1), (5, 4)), target_ds.rrs_665.chunks)
        np.testing.assert_almost_equal(target_ds.rrs_665.values,
                                       expected_data[::-1],
                                       decimal=3)
Exemple #12
0
 def test_non_geographical_crs(self):
     cube = new_cube(x_name='x',
                     y_name='y',
                     crs='epsg:25832',
                     variables=dict(a=1, b=2, c=3))
     gm = GridMapping.from_dataset(cube)
     dataset = encode_cube(cube, gm, xr.Dataset(dict(d=True)))
     self.assertIsInstance(dataset, xr.Dataset)
     self.assertEqual({'a', 'b', 'c', 'd', 'crs'}, set(dataset.data_vars))
Exemple #13
0
 def test_default_props(self):
     gm = GridMapping.regular((1000, 1000), (10, 53), 0.01, CRS_WGS84)
     self.assertEqual((1000, 1000), gm.size)
     self.assertEqual((1000, 1000), gm.tile_size)
     self.assertEqual(10, gm.x_min)
     self.assertEqual(53, gm.y_min)
     self.assertEqual((0.01, 0.01), gm.xy_res)
     self.assertEqual(True, gm.is_regular)
     self.assertEqual(False, gm.is_j_axis_up)
Exemple #14
0
 def test_easter_egg(self):
     cube = new_cube()
     md_adjuster = CubeMetadataAdjuster()
     with self.assertRaises(ValueError) as cm:
         md_adjuster.transform_cube(
             cube, GridMapping.from_dataset(cube),
             CubeConfig(metadata=dict(inverse_fine_structure_constant=136)))
     self.assertEqual(('inverse_fine_structure_constant must be 137'
                       ' or running in wrong universe', ),
                      cm.exception.args)
Exemple #15
0
    def test_geographical_crs(self):
        cube = new_cube(variables=dict(a=1, b=2, c=3))
        gm = GridMapping.from_dataset(cube)

        dataset = encode_cube(cube, gm)
        self.assertIs(cube, dataset)

        dataset = encode_cube(cube, gm, xr.Dataset(dict(d=True)))
        self.assertIsInstance(dataset, xr.Dataset)
        self.assertEqual({'a', 'b', 'c', 'd'}, set(dataset.data_vars))
Exemple #16
0
 def test_derive(self):
     gm = GridMapping.regular((1000, 1000), (10, 53), 0.01, CRS_WGS84)
     self.assertEqual((1000, 1000), gm.size)
     self.assertEqual((1000, 1000), gm.tile_size)
     self.assertEqual(False, gm.is_j_axis_up)
     derived_gm = gm.derive(tile_size=500, is_j_axis_up=True)
     self.assertIsNot(gm, derived_gm)
     self.assertIsInstance(derived_gm, RegularGridMapping)
     self.assertEqual((1000, 1000), derived_gm.size)
     self.assertEqual((500, 500), derived_gm.tile_size)
     self.assertEqual(True, derived_gm.is_j_axis_up)
Exemple #17
0
 def test_1d_xy_coords(self):
     gm = GridMapping.from_coords(
         x_coords=xr.DataArray(np.linspace(1.5, 8.5, 8), dims='lon'),
         y_coords=xr.DataArray(np.linspace(4.5, -4.5, 10), dims='lat'),
         crs=GEO_CRS)
     xy_coords = gm.xy_coords
     self.assertIsInstance(xy_coords, xr.DataArray)
     self.assertIs(xy_coords, gm.xy_coords)
     self.assertEqual(('coord', 'lat', 'lon'), xy_coords.dims)
     self.assertEqual((2, 10, 8), xy_coords.shape)
     self.assertEqual(('lon', 'lat'), gm.xy_var_names)
     self.assertEqual(('lon', 'lat'), gm.xy_dim_names)
Exemple #18
0
 def test_from_regular_cube(self):
     dataset = xcube.core.new.new_cube(variables=dict(rad=0.5))
     gm = GridMapping.from_dataset(dataset)
     self.assertEqual((360, 180), gm.size)
     self.assertEqual((360, 180), gm.tile_size)
     self.assertEqual(GEO_CRS, gm.crs)
     self.assertEqual((1, 1), gm.xy_res)
     self.assertEqual(True, gm.is_regular)
     self.assertEqual(False, gm.is_lon_360)
     self.assertEqual(True, gm.is_j_axis_up)
     self.assertEqual((2, 180, 360), gm.xy_coords.shape)
     self.assertEqual(('coord', 'lat', 'lon'), gm.xy_coords.dims)
Exemple #19
0
def _compute_target_grid_mapping(cube_config: CubeConfig,
                                 source_gm: GridMapping) -> GridMapping:
    # assert_true(source_gm.is_regular, 'source_gm must be regular')

    target_crs = cube_config.crs
    target_bbox = cube_config.bbox
    target_spatial_res = cube_config.spatial_res

    if target_crs is None \
            and target_bbox is None \
            and target_spatial_res is None:
        # Nothing to do
        if source_gm.is_regular:
            return source_gm
        return source_gm.to_regular(tile_size=cube_config.tile_size)

    if target_spatial_res is not None:
        xy_res = (target_spatial_res, target_spatial_res)
    else:
        xy_res = source_gm.xy_res
    if target_bbox is not None:
        x_res, y_res = xy_res
        x_min, y_min, x_max, y_max = target_bbox
        xy_min = x_min, y_min
        size = round((x_max - x_min) / x_res), round((y_max - y_min) / y_res)
    else:
        xy_min = source_gm.x_min, source_gm.y_min
        size = source_gm.size
    if target_crs is not None:
        crs = pyproj.crs.CRS.from_string(target_crs)
    else:
        crs = source_gm.crs
    target_gm = GridMapping.regular(size=size,
                                    xy_min=xy_min,
                                    xy_res=xy_res,
                                    crs=crs,
                                    tile_size=source_gm.tile_size,
                                    is_j_axis_up=source_gm.is_j_axis_up)
    return target_gm.derive(xy_var_names=source_gm.xy_var_names,
                            xy_dim_names=source_gm.xy_dim_names)
Exemple #20
0
 def test_1d_lon_360(self):
     gm = GridMapping.from_coords(
         x_coords=xr.DataArray(np.linspace(177.5, 184.5, 8), dims='lon'),
         y_coords=xr.DataArray(np.linspace(4.5, -4.5, 10), dims='lat'),
         crs=GEO_CRS)
     self.assertEqual((8, 10), gm.size)
     self.assertEqual((8, 10), gm.tile_size)
     self.assertEqual((1, 1), gm.xy_res)
     self.assertEqual((177, -5, 185, 5), gm.xy_bbox)
     self.assertEqual(GEO_CRS, gm.crs)
     self.assertEqual(True, gm.is_regular)
     self.assertEqual(False, gm.is_j_axis_up)
     self.assertEqual(True, gm.is_lon_360)
Exemple #21
0
 def test_it(self):
     cube = new_cube(variables=dict(a=0.5))
     gm = GridMapping.from_dataset(cube)
     cube_config = CubeConfig()
     identity = CubeIdentity()
     t_cube = identity.transform_cube(cube,
                                      gm,
                                      cube_config)
     self.assertIsInstance(t_cube, tuple)
     self.assertEqual(3, len(t_cube))
     self.assertIs(cube, t_cube[0])
     self.assertIs(gm, t_cube[1])
     self.assertIs(cube_config, t_cube[2])
Exemple #22
0
 def assertCallableWorks(self, user_code_callable):
     code_config = CodeConfig(_callable=user_code_callable,
                              callable_params=self.good_params)
     executor = CubeUserCodeExecutor(code_config)
     ds_input = new_cube(variables=dict(a=1))
     ds_output, gm, cc = executor.transform_cube(
         ds_input, GridMapping.from_dataset(ds_input), CubeConfig())
     self.assertIsInstance(ds_output, xr.Dataset)
     self.assertIsInstance(gm, GridMapping)
     self.assertIsInstance(cc, CubeConfig)
     self.assertIsNot(ds_output, ds_input)
     self.assertIn('X', ds_output)
     self.assertEqual(42, ds_output.X)
Exemple #23
0
    def test_transform_s2(self):
        dataset = create_s2plus_dataset()

        gm = GridMapping.from_dataset(dataset, prefer_is_regular=True)
        # Assert we've picked the projected one which is regular
        self.assertEqual("Projected CRS", gm.crs.type_name)
        self.assertEqual(True, gm.is_regular)

        gm_t = gm.transform(CRS_CRS84)
        self.assertEqual(CRS_CRS84, gm_t.crs)

        gm_t = gm.transform(CRS_WGS84)
        self.assertEqual(CRS_WGS84, gm_t.crs)
Exemple #24
0
    def test_metadata_adjusted_geo_crs(self):
        (x1, x2), (y1, y2) = (53, 54), (11, 12)
        cube1 = new_cube(
            width=1000,
            height=1000,
            variables=dict(chl=0.6, tsm=0.9, flags=16),
            x_start=x1,
            y_start=y1,
            x_res=(x2 - x1) / 1000,
            y_res=(x2 - x1) / 1000,
        )
        cube1.attrs = {}

        md_adjuster = CubeMetadataAdjuster()
        cube2, gm, cc = md_adjuster.transform_cube(
            cube1, GridMapping.from_dataset(cube1),
            CubeConfig(metadata=dict(title='S2L2A subset'),
                       variable_metadata=dict(
                           chl=dict(long_name='Chlorophyll'),
                           tsm=dict(long_name='Total suspended matter'),
                           flags=dict(long_name='Quality flags'),
                       )))
        self.assertIsNot(cube1, cube2)
        self.assertIsInstance(gm, GridMapping)
        self.assertIsInstance(cc, CubeConfig)

        date_created = cube2.attrs.pop('date_created', None)
        self.assertIsInstance(date_created, str)
        history = cube2.attrs.pop('history', None)
        self.assertIsInstance(history, list)

        self.assertEqual(
            {
                'Conventions': 'CF-1.7',
                'title': 'S2L2A subset',
                'geospatial_bounds_crs': 'CRS84',
                'geospatial_bounds': 'POLYGON((53 11, 53 12,'
                ' 54 12, 54 11, 53 11))',
                'geospatial_lat_max': 12,
                'geospatial_lat_min': 11,
                'geospatial_lat_resolution': 0.001,
                'geospatial_lat_units': 'degrees_north',
                'geospatial_lon_max': 54,
                'geospatial_lon_min': 53,
                'geospatial_lon_resolution': 0.001,
                'geospatial_lon_units': 'degrees_east',
            }, cube2.attrs)
        self.assertEqual({'long_name': 'Chlorophyll'}, cube2.chl.attrs)
        self.assertEqual({'long_name': 'Total suspended matter'},
                         cube2.tsm.attrs)
        self.assertEqual({'long_name': 'Quality flags'}, cube2.flags.attrs)
Exemple #25
0
    def test_empty_cube(self):
        cube = new_cube()
        gm = GridMapping.from_dataset(cube)
        cube_config = CubeConfig(tile_size=180)

        t_cube = transform_cube((cube, gm, cube_config), MyTiler())
        self.assertIsInstance(t_cube, tuple)
        self.assertEqual(3, len(t_cube))

        cube2, gm2, cc2 = t_cube
        self.assertIs(cube, cube2)
        self.assertIs(gm, gm2)
        self.assertIs(cube_config, cc2)
        self.assertEqual((180, 180), cc2.tile_size)
Exemple #26
0
    def test_non_empty_cube(self):
        cube = new_cube(variables=dict(a=0.5))
        gm = GridMapping.from_dataset(cube)
        cube_config = CubeConfig(tile_size=180)

        t_cube = transform_cube((cube, gm, cube_config), MyTiler())
        self.assertIsInstance(t_cube, tuple)
        self.assertEqual(3, len(t_cube))

        cube2, gm2, cc2 = t_cube
        self.assertIsNot(cube, cube2)
        self.assertEqual(((5,), (180,), (180, 180)), cube2.a.chunks)
        self.assertIs(gm, gm2)
        self.assertEqual(None, cc2.tile_size)
Exemple #27
0
 def test_1d_x_irregular(self):
     gm = GridMapping.from_coords(
         x_coords=xr.DataArray([1.5, 2.5, 3.5, 4.5, 5.49, 6.5, 7.5, 8.5],
                               dims='lon'),
         y_coords=xr.DataArray(np.linspace(4.5, -4.5, 10), dims='lat'),
         crs=GEO_CRS)
     self.assertEqual((8, 10), gm.size)
     self.assertEqual((8, 10), gm.tile_size)
     self.assertEqual((1, 1), gm.xy_res)
     self.assertEqual((1, -5, 9, 5), gm.xy_bbox)
     self.assertEqual(GEO_CRS, gm.crs)
     self.assertEqual(False, gm.is_regular)
     self.assertEqual(False, gm.is_j_axis_up)
     self.assertEqual(False, gm.is_lon_360)
Exemple #28
0
def _compute_ij_images_xarray_dask(src_geo_coding: GridMapping,
                                   output_geom: GridMapping,
                                   uv_delta: float) -> da.Array:
    """
    Compute dask.array.Array destination image
    with source pixel i,j coords from xarray.DataArray x,y sources.
    """
    dst_width = output_geom.width
    dst_height = output_geom.height
    dst_tile_width = output_geom.tile_width
    dst_tile_height = output_geom.tile_height
    dst_var_shape = 2, dst_height, dst_width
    dst_var_chunks = 2, dst_tile_height, dst_tile_width

    dst_x_min, dst_y_min, dst_x_max, dst_y_max = output_geom.xy_bbox
    dst_x_res, dst_y_res = output_geom.xy_res
    dst_is_j_axis_up = output_geom.is_j_axis_up

    # Compute an empirical xy_border as a function of the
    # number of tiles, because the more tiles we have
    # the smaller the destination xy-bboxes and the higher
    # the risk to not find any source ij-bbox for a given xy-bbox.
    # xy_border will not be larger than half of the
    # coverage of a tile.
    #
    num_tiles_x = dst_width / dst_tile_width
    num_tiles_y = dst_height / dst_tile_height
    xy_border = min(
        min(2 * num_tiles_x * output_geom.x_res,
            2 * num_tiles_y * output_geom.y_res),
        min(0.5 * (dst_x_max - dst_x_min), 0.5 * (dst_y_max - dst_y_min)))

    dst_xy_bboxes = output_geom.xy_bboxes
    src_ij_bboxes = src_geo_coding.ij_bboxes_from_xy_bboxes(
        dst_xy_bboxes, xy_border=xy_border, ij_border=1)

    return compute_array_from_func(
        _compute_ij_images_xarray_dask_block,
        dst_var_shape,
        dst_var_chunks,
        np.float64,
        ctx_arg_names=[
            'dtype',
            'block_id',
            'block_shape',
            'block_slices',
        ],
        args=(src_geo_coding.xy_coords, src_ij_bboxes, dst_x_min, dst_y_min,
              dst_y_max, dst_x_res, dst_y_res, dst_is_j_axis_up, uv_delta),
        name='ij_pixels')
Exemple #29
0
    def test_xy_bboxes(self):
        gm = GridMapping.regular(size=(2000, 1000),
                                 xy_min=(10.0, 20.0),
                                 xy_res=0.1,
                                 crs=NOT_A_GEO_CRS)
        np.testing.assert_almost_equal(gm.xy_bboxes,
                                       np.array([[10., 20., 210., 120.]],
                                                dtype=np.float64))

        gm = GridMapping.regular(size=(2000, 1000),
                                 xy_min=(10.0, 20.0),
                                 xy_res=0.1,
                                 crs=NOT_A_GEO_CRS).derive(tile_size=500)
        np.testing.assert_almost_equal(gm.xy_bboxes,
                                       np.array([
                                           [10., 70, 60, 120.],
                                           [60., 70, 110, 120.],
                                           [110., 70, 160, 120.],
                                           [160., 70, 210, 120.],
                                           [10., 20, 60, 70.],
                                           [60., 20, 110, 70.],
                                           [110., 20, 160, 70.],
                                           [160., 20, 210, 70.]
                                       ], dtype=np.float64))
Exemple #30
0
    def test_ij_bboxes(self):
        gm = GridMapping.regular(size=(2000, 1000),
                                 xy_min=(10.0, 20.0),
                                 xy_res=0.1,
                                 crs=NOT_A_GEO_CRS)
        np.testing.assert_almost_equal(gm.ij_bboxes,
                                       np.array([[0, 0, 2000, 1000]],
                                                dtype=np.int64))

        gm = GridMapping.regular(size=(2000, 1000),
                                 xy_min=(10.0, 20.0),
                                 xy_res=0.1,
                                 crs=NOT_A_GEO_CRS).derive(tile_size=500)
        np.testing.assert_almost_equal(gm.ij_bboxes,
                                       np.array([
                                           [0, 0, 500, 500],
                                           [500, 0, 1000, 500],
                                           [1000, 0, 1500, 500],
                                           [1500, 0, 2000, 500],
                                           [0, 500, 500, 1000],
                                           [500, 500, 1000, 1000],
                                           [1000, 500, 1500, 1000],
                                           [1500, 500, 2000, 1000]
                                       ], dtype=np.int64))