def test_from_sentinel_2(self): dataset = create_s2plus_dataset() gm = GridMapping.from_dataset(dataset) # Should pick the projected one which is regular self.assertEqual("Projected CRS", gm.crs.type_name) self.assertEqual(True, gm.is_regular) gm = GridMapping.from_dataset(dataset, prefer_is_regular=True) # Should pick the projected one which is regular self.assertEqual("Projected CRS", gm.crs.type_name) self.assertEqual(True, gm.is_regular) gm = GridMapping.from_dataset(dataset, prefer_is_regular=False) # Should pick the geographic one which is irregular self.assertEqual('Geographic 2D CRS', gm.crs.type_name) self.assertEqual(False, gm.is_regular) gm = GridMapping.from_dataset(dataset, prefer_crs=GEO_CRS) # Should pick the geographic one which is irregular self.assertEqual('Geographic 2D CRS', gm.crs.type_name) self.assertEqual(False, gm.is_regular) gm = GridMapping.from_dataset(dataset, prefer_crs=GEO_CRS, prefer_is_regular=True) # Should pick the geographic one which is irregular self.assertEqual('Geographic 2D CRS', gm.crs.type_name) self.assertEqual(False, gm.is_regular)
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)
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)
def test_reproject_crs_to_wgs84_s2plus(self): dst_width = 6 dst_height = 4 dataset = create_s2plus_dataset() proj_params = dataset.transverse_mercator.attrs wkt = get_projection_wkt( "Some S2+ Tile", "Transverse_Mercator", latitude_of_origin=proj_params["latitude_of_projection_origin"], central_meridian=proj_params["longitude_of_central_meridian"], scale_factor=proj_params["scale_factor_at_central_meridian"], false_easting=proj_params["false_easting"], false_northing=proj_params["false_northing"]) proj_dataset = reproject_crs_to_wgs84(dataset, src_projection=wkt, dst_region=( 0.2727627754211426, 51.3291015625, 0.273336261510849, 51.329463958740234, ), dst_size=(dst_width, dst_height)) self.assertIsNotNone(proj_dataset) self.assertEqual(dict(lon=dst_width, lat=dst_height, bnds=2), proj_dataset.sizes) self.assertIn('lon', proj_dataset) self.assertEqual(proj_dataset.lon.shape, (dst_width, )) self.assertIn('lat', proj_dataset) self.assertEqual(proj_dataset.lat.shape, (dst_height, )) self.assertIn('lon_bnds', proj_dataset) self.assertEqual(proj_dataset.lon_bnds.shape, (dst_width, 2)) self.assertIn('lat_bnds', proj_dataset) self.assertEqual(proj_dataset.lat_bnds.shape, (dst_height, 2)) expected_rrs_665 = np.array( [[0.025002, 0.019001, 0.019001, 0.008999, 0.012001, 0.012001], [0.028, 0.021, 0.021, 0.009998, 0.008999, 0.008999], [0.036999, 0.022999, 0.022999, 0.007, 0.009998, 0.009998], [0.033001, 0.018002, 0.018002, 0.007999, 0.008999, 0.008999]], dtype=np.float32) self.assertIn('rrs_665', proj_dataset) self.assertEqual((dst_height, dst_width), proj_dataset.rrs_665.shape) self.assertEqual(np.float32, proj_dataset.rrs_665.dtype) assert_array_almost_equal(expected_rrs_665, proj_dataset.rrs_665)
def test_transform_no_op(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(gm.crs) self.assertIs(gm, gm_t) # Almost no op gm = GridMapping.regular(size=(3, 3), xy_min=(10, 53), xy_res=0.1, crs=CRS_CRS84) gm_t = gm.transform(crs=gm.crs, xy_var_names=('x', 'y')) self.assertEqual(('x', 'y'), gm_t.xy_var_names)
def test_post_process(self): ds1 = create_s2plus_dataset() ds2 = self.processor.post_process(ds1) self.assertIs(ds1, ds2)
def test_get_time_range(self): ds = create_s2plus_dataset() t1, t2 = self.processor.get_time_range(ds) self.assertEqual(to_time_in_days_since_1970("2018-08-02T10:59:38.888000Z"), t1) self.assertEqual(to_time_in_days_since_1970("2018-08-02T10:59:38.888000Z"), t2)
def setUp(self): clean_up() dataset = create_s2plus_dataset() dataset.to_netcdf(INPUT_FILE) dataset.close()