def test_rectify_2x2_to_13x13_none(self): src_ds = self.new_source_dataset() output_geom = ImageGeom(size=(13, 13), x_min=10.0, y_min=50.0, xy_res=0.5) dst_ds = rectify_dataset(src_ds, output_geom=output_geom) self.assertIsNone(dst_ds) output_geom = ImageGeom(size=(13, 13), x_min=-10.0, y_min=50.0, xy_res=0.5) dst_ds = rectify_dataset(src_ds, output_geom=output_geom) self.assertIsNone(dst_ds) output_geom = ImageGeom(size=(13, 13), x_min=0.0, y_min=58.0, xy_res=0.5) dst_ds = rectify_dataset(src_ds, output_geom=output_geom) self.assertIsNone(dst_ds) output_geom = ImageGeom(size=(13, 13), x_min=0.0, y_min=42.0, xy_res=0.5) dst_ds = rectify_dataset(src_ds, output_geom=output_geom) self.assertIsNone(dst_ds)
def test_derive(self): image_geom = ImageGeom((2048, 1024)) self.assertEqual((2048, 1024), image_geom.tile_size) new_image_geom = image_geom.derive(tile_size=512) self.assertIsNot(new_image_geom, image_geom) self.assertEqual((2048, 1024), new_image_geom.size) self.assertEqual((512, 512), new_image_geom.tile_size)
def test_is_crossing_antimeridian(self): output_geom = ImageGeom(size=100, x_min=0.0, y_min=+50.0, xy_res=0.1, is_geo_crs=True) self.assertFalse(output_geom.is_crossing_antimeridian) output_geom = ImageGeom(size=100, x_min=178.0, y_min=+50.0, xy_res=0.1, is_geo_crs=True) self.assertTrue(output_geom.is_crossing_antimeridian)
def test_size(self): image_geom = ImageGeom((2000, 1000)) self.assertEqual((2000, 1000), image_geom.size) image_geom = ImageGeom(3600) self.assertEqual((3600, 3600), image_geom.size) with self.assertRaises(TypeError): # noinspection PyTypeChecker ImageGeom(None) with self.assertRaises(ValueError): # noinspection PyTypeChecker ImageGeom((3600, 1800, 4))
def test_coord_vars(self): image_geom = ImageGeom(size=(10, 6), x_min=-2600.0, y_min=1200.0, xy_res=10.0) cv = image_geom.coord_vars(xy_names=('x', 'y')) self._assert_coord_vars(cv, (10, 6), ('x', 'y'), (-2595., -2505.), (1205., 1255.), ('x_bnds', 'y_bnds'), ( (-2600., -2590.), (-2510., -2500.), ), ( (1200., 1210.), (1250., 1260.), ))
def test_xy_bbox_antimeridian(self): output_geom = ImageGeom(size=(20, 10), x_min=174.0, y_min=-30.0, xy_res=0.5, is_geo_crs=True) self.assertEqual((174.0, -30.0, -176.0, -25.0), output_geom.xy_bbox)
def test_rectify_2x2_to_13x13(self): src_ds = self.new_source_dataset() output_geom = ImageGeom(size=(13, 13), x_min=-0.25, y_min=49.75, xy_res=0.5) dst_ds = rectify_dataset(src_ds, output_geom=output_geom) lon, lat, rad = self._assert_shape_and_dim(dst_ds, 13, 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([50., 50.5, 51.0, 51.5, 52.0, 52.5, 53.0, 53.5, 54.0, 54.5, 55., 55.5, 56.], dtype=lat.dtype)) np.testing.assert_almost_equal(rad.values, np.array([ [nan, nan, nan, nan, 4.0, nan, nan, nan, nan, nan, nan, nan, nan], [nan, nan, nan, 4.0, 4.0, 4.0, nan, nan, nan, nan, nan, nan, nan], [nan, nan, 3.0, 4.0, 4.0, 4.0, 4.0, nan, nan, nan, nan, nan, nan], [nan, 3.0, 3.0, 3.0, 4.0, 4.0, 4.0, 4.0, 2.0, nan, nan, nan, nan], [3.0, 3.0, 3.0, 3.0, 3.0, 4.0, 4.0, 2.0, 2.0, 2.0, nan, nan, nan], [nan, 3.0, 3.0, 3.0, 3.0, 3.0, 1.0, 2.0, 2.0, 2.0, 2.0, nan, nan], [nan, 3.0, 3.0, 3.0, 3.0, 1.0, 1.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0], [nan, 3.0, 3.0, 1.0, 1.0, 1.0, 1.0, 2.0, 2.0, 2.0, 2.0, nan, nan], [nan, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 2.0, 2.0, nan, nan, nan, nan], [nan, nan, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, nan, nan, nan, nan, nan], [nan, nan, 1.0, 1.0, 1.0, 1.0, nan, nan, nan, nan, nan, nan, nan], [nan, nan, 1.0, 1.0, nan, nan, nan, nan, nan, nan, nan, nan, nan], [nan, nan, 1.0, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan] ], dtype=rad.dtype))
def test_rectify_2x2_to_13x13_antimeridian(self): src_ds = self.new_source_dataset_antimeridian() output_geom = ImageGeom(size=(13, 13), x_min=177.75, y_min=49.75, xy_res=0.5) dst_ds = rectify_dataset(src_ds, output_geom=output_geom) self.assertIsNotNone(dst_ds) lon, lat, rad = self._assert_shape_and_dim(dst_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([50., 50.5, 51.0, 51.5, 52.0, 52.5, 53.0, 53.5, 54.0, 54.5, 55., 55.5, 56.], dtype=lat.dtype)) np.testing.assert_almost_equal(rad.values, np.array([ [nan, nan, nan, nan, 4.0, nan, nan, nan, nan, nan, nan, nan, nan], [nan, nan, nan, 4.0, 4.0, 4.0, nan, nan, nan, nan, nan, nan, nan], [nan, nan, 3.0, 4.0, 4.0, 4.0, 4.0, nan, nan, nan, nan, nan, nan], [nan, 3.0, 3.0, 3.0, 4.0, 4.0, 4.0, 4.0, 2.0, nan, nan, nan, nan], [3.0, 3.0, 3.0, 3.0, 3.0, 4.0, 4.0, 2.0, 2.0, 2.0, nan, nan, nan], [nan, 3.0, 3.0, 3.0, 3.0, 3.0, 1.0, 2.0, 2.0, 2.0, 2.0, nan, nan], [nan, 3.0, 3.0, 3.0, 3.0, 1.0, 1.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0], [nan, 3.0, 3.0, 1.0, 1.0, 1.0, 1.0, 2.0, 2.0, 2.0, 2.0, nan, nan], [nan, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 2.0, 2.0, nan, nan, nan, nan], [nan, nan, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, nan, nan, nan, nan, nan], [nan, nan, 1.0, 1.0, 1.0, 1.0, nan, nan, nan, nan, nan, nan, nan], [nan, nan, 1.0, 1.0, nan, nan, nan, nan, nan, nan, nan, nan, nan], [nan, nan, 1.0, nan, nan, nan, nan, nan, nan, nan, nan, nan, nan] ], dtype=rad.dtype))
def test_rectify_2x2_to_7x7_subset(self): src_ds = self.new_source_dataset() output_geom = ImageGeom(size=(7, 7), x_min=1.5, y_min=50.5, xy_res=1.0) dst_ds = rectify_dataset(src_ds, output_geom=output_geom) lon, lat, rad = self._assert_shape_and_dim(dst_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([51.0, 52.0, 53.0, 54.0, 55., 56., 57.], dtype=lat.dtype)) np.testing.assert_almost_equal( rad.values, np.array([ [4.0, 4.0, nan, nan, nan, nan, nan], [3.0, 4.0, 2.0, nan, nan, nan, nan], [3.0, 1.0, 2.0, 2.0, 2.0, nan, nan], [1.0, 1.0, 2.0, nan, nan, nan, nan], [1.0, nan, nan, nan, nan, nan, nan], [nan, nan, nan, nan, nan, nan, nan], [nan, nan, nan, nan, nan, nan, nan], ], dtype=rad.dtype))
def test_rectify_2x2_to_13x13_output_ij_names(self): src_ds = self.new_source_dataset() output_geom = ImageGeom(size=(13, 13), x_min=-0.25, y_min=49.75, xy_res=0.5) dst_ds = rectify_dataset(src_ds, output_geom=output_geom, output_ij_names=('source_i', 'source_j')) lon, lat, rad, source_i, source_j = self._assert_shape_and_dim( dst_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([ 50., 50.5, 51.0, 51.5, 52.0, 52.5, 53.0, 53.5, 54.0, 54.5, 55., 55.5, 56. ], 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())
def test_rectify_2x2_to_13x13_dask_13x3(self): src_ds = self.new_source_dataset() output_geom = ImageGeom(size=(13, 13), x_min=-0.25, y_min=49.75, xy_res=0.5, tile_size=(13, 3)) dst_ds = rectify_dataset(src_ds, output_geom=output_geom) lon, lat, rad = self._assert_shape_and_dim(dst_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([ 50., 50.5, 51.0, 51.5, 52.0, 52.5, 53.0, 53.5, 54.0, 54.5, 55., 55.5, 56. ], dtype=lat.dtype)) np.testing.assert_almost_equal(rad.values, self.expected_rad_13x13(rad.dtype))
def test_xy_bboxes(self): image_geom = ImageGeom(size=(2000, 1000), x_min=10.0, y_min=20.0, xy_res=0.1) np.testing.assert_almost_equal(image_geom.xy_bboxes, np.array([[10., 20., 209.9, 119.9]], dtype=np.float64)) image_geom = ImageGeom(size=(2000, 1000), x_min=10.0, y_min=20.0, xy_res=0.1, tile_size=500) np.testing.assert_almost_equal(image_geom.xy_bboxes, np.array([ [10., 20., 59.9, 69.9], [60., 20., 109.9, 69.9], [110., 20., 159.9, 69.9], [160., 20., 209.9, 69.9], [10., 70., 59.9, 119.9], [60., 70., 109.9, 119.9], [110., 70., 159.9, 119.9], [160., 70., 209.9, 119.9] ], dtype=np.float64))
def test_ij_bboxes(self): image_geom = ImageGeom(size=(2000, 1000), x_min=10.0, y_min=20.0, xy_res=0.1) np.testing.assert_almost_equal(image_geom.ij_bboxes, np.array([[0, 0, 1999, 999]], dtype=np.int64)) image_geom = ImageGeom(size=(2000, 1000), x_min=10.0, y_min=20.0, xy_res=0.1, tile_size=500) np.testing.assert_almost_equal(image_geom.ij_bboxes, np.array([ [0, 0, 499, 499], [500, 0, 999, 499], [1000, 0, 1499, 499], [1500, 0, 1999, 499], [0, 500, 499, 999], [500, 500, 999, 999], [1000, 500, 1499, 999], [1500, 500, 1999, 999] ], dtype=np.int64))
def test_coord_vars_lon_normalized(self): image_geom = ImageGeom(size=(10, 10), x_min=172.0, y_min=53.0, xy_res=2.0, is_geo_crs=True) cv = image_geom.coord_vars(xy_names=('lon', 'lat'), is_lon_normalized=True) self._assert_coord_vars(cv, (10, 10), ('lon', 'lat'), (173.0, -169.0), (54.0, 72.0), ('lon_bnds', 'lat_bnds'), ( (172., 174.), (-170., -168.), ), ( (53., 55.), (71., 73.), ))
def _rectify(input_path: str, xy_names: Optional[Tuple[str, str]], var_names: Optional[Sequence[str]], output_path: str, output_format: Optional[str], output_size: Optional[Tuple[int, int]], output_tile_size: Optional[Tuple[int, int]], output_point: Optional[Tuple[float, float]], output_res: Optional[float], delta: float, dry_run: bool, monitor): from xcube.core.dsio import guess_dataset_format from xcube.core.dsio import open_dataset from xcube.core.dsio import write_dataset from xcube.core.rectify import rectify_dataset from xcube.core.rectify import ImageGeom from xcube.core.sentinel3 import is_sentinel3_product from xcube.core.sentinel3 import open_sentinel3_product if not output_format: output_format = guess_dataset_format(output_path) output_geom = None if output_size is not None and output_point is not None and output_res is not None: output_geom = ImageGeom(size=output_size, x_min=output_point[0], y_min=output_point[1], xy_res=output_res) elif output_size is not None or output_point is not None or output_res is not None: raise click.ClickException( 'SIZE, POINT, and RES must all be given or none of them.') monitor(f'Opening dataset from {input_path!r}...') if is_sentinel3_product(input_path): src_ds = open_sentinel3_product(input_path) else: src_ds = open_dataset(input_path) monitor('Rectifying...') reproj_ds = rectify_dataset(src_ds, xy_names=xy_names, var_names=var_names, output_geom=output_geom, tile_size=output_tile_size, uv_delta=delta) if reproj_ds is None: monitor( f'Dataset {input_path} does not seem to have an intersection with bounding box' ) return monitor(f'Writing rectified dataset to {output_path!r}...') if not dry_run: write_dataset(reproj_ds, output_path, output_format) monitor(f'Done.')
def test_from_olci(self): src_ds = open_sentinel3_product(olci_path, {'Oa06_radiance', 'Oa13_radiance', 'Oa20_radiance'}) src_ds.longitude.load() src_ds.latitude.load() output_geom = ImageGeom.from_dataset(src_ds, xy_names=('longitude', 'latitude')) self.assertEqual(True, output_geom.is_geo_crs) self.assertEqual(20259, output_geom.width) self.assertEqual(7386, output_geom.height) self.assertAlmostEqual(-11.918857, output_geom.x_min) self.assertAlmostEqual(59.959791, output_geom.y_min) self.assertAlmostEqual(0.00181345416, output_geom.xy_res)
def test_tile_size(self): image_geom = ImageGeom(size=(2000, 1000)) self.assertEqual((2000, 1000), image_geom.size) self.assertEqual((2000, 1000), image_geom.tile_size) self.assertEqual(False, image_geom.is_tiled) image_geom = ImageGeom(size=(2000, 1000), tile_size=None) self.assertEqual((2000, 1000), image_geom.size) self.assertEqual((2000, 1000), image_geom.tile_size) self.assertEqual(False, image_geom.is_tiled) image_geom = ImageGeom(size=(2000, 1000), tile_size=(512, 256)) self.assertEqual((2000, 1000), image_geom.size) self.assertEqual((512, 256), image_geom.tile_size) self.assertEqual(True, image_geom.is_tiled) image_geom = ImageGeom(size=(2000, 1000), tile_size=270) self.assertEqual((2000, 1000), image_geom.size) self.assertEqual((270, 270), image_geom.tile_size) self.assertEqual(True, image_geom.is_tiled) image_geom = ImageGeom(size=(400, 200), tile_size=(512, 256)) self.assertEqual((400, 200), image_geom.size) self.assertEqual((400, 200), image_geom.tile_size) self.assertEqual(False, image_geom.is_tiled) with self.assertRaises(ValueError): # noinspection PyTypeChecker ImageGeom((2000, 1000), ((512,)))
def test_from_dataset_antimeridian(self): src_ds = self.new_source_dataset_antimeridian() self._assert_image_geom(ImageGeom((4, 4), None, 178.0, 50.0, 2.0), ImageGeom.from_dataset(src_ds)) self._assert_image_geom( ImageGeom((7, 7), None, 178.0, 50.0, 1.0), ImageGeom.from_dataset(src_ds, xy_oversampling=2.0)) self._assert_image_geom( ImageGeom((8, 8), None, 178.0, 50.0, 1.0), ImageGeom.from_dataset(src_ds, ij_denom=4, xy_oversampling=2.0))
def test_from_default_new_cube(self): self._assert_image_geom( ImageGeom((508, 253), None, -179.5, -89.5, 2**-.5), ImageGeom.from_dataset(xcube.core.new.new_cube()))
def test_invalids(self): with self.assertRaises(ValueError): ImageGeom((3600, 0)) with self.assertRaises(ValueError): ImageGeom((-3600, 1800)) with self.assertRaises(ValueError): ImageGeom(1000, tile_size=0) with self.assertRaises(ValueError): ImageGeom(1000, tile_size=(100, -100)) with self.assertRaises(ValueError): ImageGeom(100, xy_res=0.0) with self.assertRaises(ValueError): ImageGeom(100, xy_res=-0.1) with self.assertRaises(ValueError): ImageGeom(100, x_min=-190.0, xy_res=0.1, is_geo_crs=True) with self.assertRaises(ValueError): ImageGeom(100, x_min=182.0, xy_res=0.1, is_geo_crs=True) with self.assertRaises(ValueError): ImageGeom(100, x_min=20.0, xy_res=2.0, is_geo_crs=True) with self.assertRaises(ValueError): ImageGeom(100, y_min=-100.0, xy_res=0.1, is_geo_crs=True) with self.assertRaises(ValueError): ImageGeom(100, y_min=100.0, xy_res=0.1, is_geo_crs=True) with self.assertRaises(ValueError): ImageGeom(100, y_min=50.0, xy_res=0.5, is_geo_crs=True)
def test_xy_bbox(self): output_geom = ImageGeom(size=(20, 10), x_min=0.0, y_min=+50.0, xy_res=0.5) self.assertEqual((0.0, 50.0, 10.0, 55.0), output_geom.xy_bbox)