def test_find_overlapping_tilenames(): """ Tests search for tiles which share the same extent_m but with different resolution and tilecode. """ e7_500 = Equi7Grid(500) e7_10 = Equi7Grid(10) tiles1_should = [ 'EU025M_E042N006T3', 'EU025M_E042N009T3', 'EU025M_E045N006T3', 'EU025M_E045N009T3' ] tiles1 = e7_500.EU.tilesys.find_overlapping_tilenames('EU500M_E042N006T6', target_sampling=25) assert sorted(tiles1) == sorted(tiles1_should) tiles2_should = ['E042N006T3', 'E042N009T3', 'E045N006T3', 'E045N009T3'] tiles2 = e7_500.EU.tilesys.find_overlapping_tilenames('E042N006T6', target_tiletype='T3') assert sorted(tiles2) == sorted(tiles2_should) tiles3_should = ['EU500M_E042N012T6'] tiles3 = e7_10.EU.tilesys.find_overlapping_tilenames('E044N015T1', target_sampling=500) assert sorted(tiles3) == sorted(tiles3_should) tiles4_should = ['E039N009T3'] tiles4 = e7_10.EU.tilesys.find_overlapping_tilenames('E041N011T1', target_tiletype='T3') assert sorted(tiles4) == sorted(tiles4_should)
def test_identify_tiles_overlapping_xybbox(): """ Tests identification of tiles covering a bounding box given in equi7 coordinats """ e7_500 = Equi7Grid(500) e7_10 = Equi7Grid(10) tiles1_should = [ 'EU500M_E048N006T6', 'EU500M_E054N006T6', 'EU500M_E060N006T6', 'EU500M_E048N012T6', 'EU500M_E054N012T6', 'EU500M_E060N012T6' ] tiles2_should = [ 'EU010M_E051N011T1', 'EU010M_E052N011T1', 'EU010M_E051N012T1', 'EU010M_E052N012T1' ] tiles1 = e7_500.EU.tilesys.identify_tiles_overlapping_xybbox( [5138743, 1111111, 6200015, 1534657]) tiles2 = e7_10.EU.tilesys.identify_tiles_overlapping_xybbox( [5138743, 1111111, 5299999, 1234657]) assert sorted(tiles1) == sorted(tiles1_should) assert sorted(tiles2) == sorted(tiles2_should)
def test_get_covering_tiles(): """ Tests the search for co-locating tiles of other type. """ e7g_40 = Equi7Grid(40) e7g_10 = Equi7Grid(10) fine_tiles = [ 'EU010M_E005N058T1', 'EU010M_E005N059T1', 'EU010M_E005N060T1', 'EU010M_E005N061T1' ] target_tiletype = e7g_40.get_tiletype() target_sampling = e7g_40.core.sampling # invoke the results as tile name in short form coarse_tiles_shortform = e7g_10.EU.tilesys.get_covering_tiles( fine_tiles, target_tiletype=target_tiletype) # invoke the results as tile name in long form coarse_tiles_longform = e7g_10.EU.tilesys.get_covering_tiles( fine_tiles, target_sampling=target_sampling) assert sorted(coarse_tiles_shortform) == ['E003N057T3', 'E003N060T3'] assert sorted(coarse_tiles_longform) == [ 'EU040M_E003N057T3', 'EU040M_E003N060T3' ]
def test_lonlat2xy_antimeridian(self): """ Tests lonlat to xy projection for locations close to the antimeridian. """ # Far-east Siberia e7 = Equi7Grid(500) x_should = 7048122.707876 y_should = 9238361.594967 lon, lat = -178.5, 67.75 sgrid_id, x, y = e7.lonlat2xy(lon, lat) assert sgrid_id == 'AS' nptest.assert_allclose(x_should, x) nptest.assert_allclose(y_should, y) # Far-west Aleutian Islands x_should = 3887311.532849 y_should = 7756934.345841 lon, lat = -178.0, 51.75 sgrid_id, x, y = e7.lonlat2xy(lon, lat) assert sgrid_id == 'NA' nptest.assert_allclose(x_should, x) nptest.assert_allclose(y_should, y) # Far-east Aleutian Islands x_should = 3865149.386282 y_should = 8432250.89933 lon, lat = 173.0, 53.0 sgrid_id, x, y = e7.lonlat2xy(lon, lat) assert sgrid_id == 'NA' nptest.assert_allclose(x_should, x) nptest.assert_allclose(y_should, y)
def test_geom_intersect(): geom_roi = setup_geom_roi() # reference file list ref_tiles = [ 'EU500M_E012N012T6', 'EU500M_E012N018T6', 'EU500M_E018N006T6', 'EU500M_E024N006T6', 'EU500M_E024N012T6', 'EU500M_E030N006T6', 'EU500M_E030N012T6', 'EU500M_E030N024T6', 'EU500M_E036N006T6', 'EU500M_E036N012T6', 'EU500M_E036N018T6', 'EU500M_E036N024T6', 'EU500M_E036N030T6', 'EU500M_E036N036T6', 'EU500M_E036N042T6', 'EU500M_E042N000T6', 'EU500M_E042N006T6', 'EU500M_E042N012T6', 'EU500M_E042N018T6', 'EU500M_E042N024T6', 'EU500M_E042N030T6', 'EU500M_E042N042T6', 'EU500M_E048N000T6', 'EU500M_E048N006T6', 'EU500M_E048N012T6', 'EU500M_E048N018T6', 'EU500M_E048N024T6', 'EU500M_E048N030T6', 'EU500M_E048N036T6', 'EU500M_E054N000T6', 'EU500M_E054N006T6', 'EU500M_E054N012T6', 'EU500M_E054N018T6', 'EU500M_E054N024T6', 'EU500M_E054N030T6', 'EU500M_E054N036T6', 'EU500M_E054N042T6', 'EU500M_E054N048T6', 'EU500M_E060N000T6', 'EU500M_E060N006T6', 'EU500M_E060N012T6', 'EU500M_E060N018T6', 'EU500M_E060N024T6', 'EU500M_E060N030T6', 'EU500M_E060N036T6', 'EU500M_E060N048T6', 'EU500M_E066N000T6', 'EU500M_E066N006T6', 'EU500M_E066N012T6', 'EU500M_E066N018T6', 'EU500M_E066N024T6', 'EU500M_E066N030T6', 'EU500M_E066N036T6', 'EU500M_E072N000T6', 'EU500M_E072N006T6', 'EU500M_E072N012T6', 'EU500M_E072N018T6', 'EU500M_E072N024T6', 'EU500M_E072N030T6', 'EU500M_E078N006T6', 'EU500M_E078N012T6' ] grid = Equi7Grid(500) res_tiles = grid._search_sgrid_tiles(geom_roi, 'EU', True) assert sorted(ref_tiles) == sorted(res_tiles)
def _build(self): if self.grid is None: self.grid = Equi7Grid(self.grid_sampling).EU folder_hierarchy = ["subgrid_name", "tile_name", "var_name"] print(datetime.now(), 'Build smart tree..') self.dir_tree = build_smarttree(self.path, folder_hierarchy, register_file_pattern="^[^Q].*.tif") dim = ["time", "var_name", "tile_name"] print(datetime.now(), 'Create data cube..') self.scatsardc = dc.EODataCube(filepaths=self.dir_tree.file_register, smart_filename_class=SgrtFilename, dimensions=dim, grid=self.grid, sdim_name="tile_name") self.scatsardc.filter_files_with_pattern(pattern=f'swi_t{self.tval}', full_path=True) self.sref = osr.SpatialReference() self.sref.ImportFromEPSG(4326)
def test_ij2xy(self): """ Tests tile indices to xy coordination in the subgrid projection """ # upperleft case (regular numpy array indexing) e7 = Equi7Grid(500) x_should = 3166500 y_should = 5178000 tile = e7.EU.tilesys.create_tile(x=3245631, y=5146545) x, y = tile.ij2xy(333, 444) nptest.assert_allclose(x_should, x) nptest.assert_allclose(y_should, y) # pixel center coordinates x_should = 3166750 y_should = 5177750 tile = e7.EU.tilesys.create_tile(x=3245631, y=5146545) x, y = tile.ij2xy(333, 444, offset='center') nptest.assert_allclose(x_should, x) nptest.assert_allclose(y_should, y) # lowerleft case x_should = 4800000 y_should = 1200000 tile = e7.EU.tilesys.create_tile(x=4800123, y=1200123) x, y = tile.ij2xy(0, 0, lowerleft=True, offset='ll') nptest.assert_allclose(x_should, x) nptest.assert_allclose(y_should, y)
def test_search_tiles_siberia_antimeridian(self): """ Tests the tile searching over Siberia and Alaska in the polar zone; ROI defined by a 4-corner polygon over high latitudes (is much curved on the globe). Comprises: - do not return tiles when not intersecting the zone - interpret correctly longitudes higher than 180 degrees """ grid = Equi7Grid(500) geom_siberia_tiles = sorted(['AS500M_E066N090T6', 'AS500M_E072N090T6']) poly_siberia_antim_180plus = setup_test_geom_siberia_antimeridian_180plus( ) tiles = sorted( grid.search_tiles_in_roi(poly_siberia_antim_180plus, coverland=False)) assert sorted(tiles) == sorted(geom_siberia_tiles) geom_siberia_alaska_tiles = sorted([ 'AS500M_E066N090T6', 'AS500M_E072N090T6', 'AS500M_E072N096T6', 'NA500M_E054N072T6', 'NA500M_E054N078T6', 'NA500M_E060N078T6' ]) poly_siberia_alaska = setup_test_geom_siberia_alaska() tiles = sorted( grid.search_tiles_in_roi(poly_siberia_alaska, coverland=True)) assert sorted(tiles) == sorted(geom_siberia_alaska_tiles)
def setup_nc_multi_test_data(): """ Creates test data as single-time and single-variable NetCDF files. Returns ------- list of str List of NetCDF test data filepaths. list of datetime List of timestamps as datetime objects. """ root_dirpath = os.path.join(dirpath_test(), 'data', 'Sentinel-1_CSAR') # create target folders dirpath = os.path.join(root_dirpath, 'IWGRDH', 'parameters', 'datasets', 'resampled', 'T0101', 'EQUI7_EU500M', 'E042N012T6', 'sig0') timestamps = [datetime(2016, 1, 1), datetime(2016, 2, 1), datetime(2017, 1, 1), datetime(2017, 2, 1)] pols = ["VV", "VH"] directions = ["A", "D"] filename_fmt = "D{}_000000--_SIG0-----_S1AIWGRDH1{}{}_146_T0101_EU500M_E042N012T6.nc" combs = itertools.product(pols, directions, timestamps) rows, cols = np.meshgrid(np.arange(0, 1200), np.arange(0, 1200)) data = (rows + cols).astype(float) equi7 = Equi7Grid(500) tile_oi = equi7.EU.tilesys.create_tile(name="E042N012T6") if not os.path.exists(dirpath): os.makedirs(dirpath) filepaths = [] for comb in combs: pol = comb[0] direction = comb[1] timestamp = comb[2] filename = filename_fmt.format(timestamp.strftime("%Y%m%d"), pol, direction) filepath = os.path.join(dirpath, filename) filepaths.append(filepath) if not os.path.exists(filepath): tags = {'direction': direction} nc_file = NcFile(filepath, mode='w', geotransform=tile_oi.geotransform(), spatialref=tile_oi.get_geotags()['spatialreference']) data_i = data + timestamps.index(timestamp) xr_ar = xr.DataArray(data=data_i[None, :, :], coords={'time': [timestamp]}, dims=['time', 'y', 'x']) xr_ds = xr.Dataset(data_vars={'1': xr_ar}, attrs=tags) nc_file.write(xr_ds) nc_file.close() timestamps = [pd.Timestamp(timestamp.strftime("%Y%m%d")) for timestamp in timestamps] return filepaths, timestamps
def setup_nc_single_test_data(): """ Creates test data as a multi-time and multi-variable NetCDF file. Returns ------- str NetCDF test data filepath. list of datetime List of timestamps as datetime objects. """ root_dirpath = os.path.join(dirpath_test(), 'data', 'Sentinel-1_CSAR') # create target folders dirpath = os.path.join(root_dirpath, 'IWGRDH', 'products', 'datasets', 'resampled', 'T0101', 'EQUI7_EU500M', 'E048N012T6', 'data') timestamps = [datetime(2016, 1, 1), datetime(2016, 2, 1), datetime(2017, 1, 1), datetime(2017, 2, 1)] var_names = ["SIG0", "GMR-"] directions = ["A", "D"] combs = itertools.product(var_names, directions, timestamps) rows, cols = np.meshgrid(np.arange(0, 1200), np.arange(0, 1200)) data = rows + cols equi7 = Equi7Grid(500) tile_oi = equi7.EU.tilesys.create_tile(name="E042N012T6") xr_dss = [] filepath = os.path.join(dirpath, "D20160101_20170201_PREPRO---_S1AIWGRDH1VV-_146_T0101_EU500M_E048N012T6.nc") if not os.path.exists(dirpath): os.makedirs(dirpath) if not os.path.exists(filepath): for comb in combs: var_name = comb[0] direction = comb[1] timestamp = comb[2] tags = {'direction': direction} data_i = data + timestamps.index(timestamp) xr_ar = xr.DataArray(data=data_i[None, :, :], coords={'time': [timestamp]}, dims=['time', 'y', 'x'], attrs=tags) xr_dss.append(xr.Dataset(data_vars={var_name.strip('-'): xr_ar})) nc_file = NcFile(filepath, mode='w', geotransform=tile_oi.geotransform(), spatialref=tile_oi.get_geotags()['spatialreference']) xr_ds = xr.merge(xr_dss) nc_file.write(xr_ds) nc_file.close() timestamps = [pd.Timestamp(timestamp.strftime("%Y%m%d")) for timestamp in timestamps] return filepath, timestamps
def test_decode_tilename(self): """ Tests the decoding of tilenames. """ e7_500 = Equi7Grid(500) e7_10 = Equi7Grid(10) assert e7_500.EU.tilesys.decode_tilename('EU500M_E042N006T6') == \ ('EU', 500, 600000, 4200000, 600000, 'T6') assert e7_10.OC.tilesys.decode_tilename('OC010M_E085N091T1') == \ ('OC', 10, 100000, 8500000, 9100000, 'T1') assert e7_500.EU.tilesys.decode_tilename('E042N006T6') == \ ('EU', 500, 600000, 4200000, 600000, 'T6') with nptest.assert_raises(ValueError) as excinfo: e7_10.EU.tilesys.decode_tilename('E042N006T6') assert str(excinfo.exception).startswith( '"tilename" is not properly defined!')
def test_xy2ij(): """ Tests xy to tile array indices. """ e7 = Equi7Grid(500) column_should = 333 row_should = 444 tile = e7.EU.tilesys.create_tile(x=3245631, y=5146545) column, row = tile.xy2ij(3166500, 5178000) nptest.assert_allclose(column_should, column) nptest.assert_allclose(row_should, row)
def test_ij2xy(): """ Test xy to lonlat projection using double numbers. """ e7 = Equi7Grid(500) x_should = 3166500 y_should = 5178000 tile = e7.EU.tilesys.create_tile(x=3245631, y=5146545) x, y = tile.ij2xy(333, 444) nptest.assert_allclose(x_should, x) nptest.assert_allclose(y_should, y)
def test_xy2lonlat_doubles(): """ Tests xy to lonlat projection using double numbers. """ e7 = Equi7Grid(500) x = 5138743.127891 y = 1307029.157093 lon_should, lat_should = 15.1, 45.3 lon, lat = e7.EU.xy2lonlat(x, y) nptest.assert_allclose(lon_should, lon) nptest.assert_allclose(lat_should, lat)
def test_xy2lonlat_numpy_array(): """ Tests xy to lonlat projection using numpy arrays. """ e7 = Equi7Grid(500) x = np.array([5138743.127891]) y = np.array([1307029.157093]) lon_should, lat_should = 15.1, 45.3 lon, lat = e7.EU.xy2lonlat(x, y) nptest.assert_allclose(lon_should, lon) nptest.assert_allclose(lat_should, lat)
def test_xy2ij(): """ Test xy to lonlat projection using double numbers. """ e7 = Equi7Grid(500) column_should = 333 row_should = 444 tile = e7.EU.tilesys.create_tile(x=3245631, y=5146545) column, row = tile.xy2ij(3166500, 5178000) nptest.assert_allclose(column_should, column) nptest.assert_allclose(row_should, row)
def test_ij2xy(): """ Tests tile indices to xy coordination in the subgrid projection """ e7 = Equi7Grid(500) x_should = 3166500 y_should = 5178000 tile = e7.EU.tilesys.create_tile(x=3245631, y=5146545) x, y = tile.ij2xy(333, 444) nptest.assert_allclose(x_should, x) nptest.assert_allclose(y_should, y)
def test_search_tiles_lon_lat_extent_poles(self): e7 = Equi7Grid(500) tiles = e7.search_tiles_in_roi(bbox=[(-170, 88), (150.0, 90)]) desired_tiles = ['NA500M_E078N084T6', 'NA500M_E078N090T6', 'NA500M_E084N084T6', 'NA500M_E084N090T6'] assert sorted(tiles) == sorted(desired_tiles) tiles = e7.search_tiles_in_roi(bbox=[(-170, -90), (150.0, -89)]) desired_tiles = ['AN500M_E036N030T6'] assert tiles == desired_tiles
def test_lonlat2xy_doubles(): """ Tests lonlat to xy projection using double numbers. """ e7 = Equi7Grid(500) x_should = 5138743.127891 y_should = 1307029.157093 lon, lat = 15.1, 45.3 sgrid_id, x, y = e7.lonlat2xy(lon, lat) assert sgrid_id == 'EU' nptest.assert_allclose(x_should, x) nptest.assert_allclose(y_should, y)
def test_search_tile_500_lon_lat_extent(): """ Test searching of tile with input of lon lat extent """ e7 = Equi7Grid(500) tiles = e7.search_tiles_in_geo_roi(extent=[10, 40, 20, 50], coverland=True) assert tiles == [ 'EU500M_E042N006T6', 'EU500M_E042N012T6', 'EU500M_E048N006T6', 'EU500M_E048N012T6', 'EU500M_E048N018T6', 'EU500M_E054N006T6', 'EU500M_E054N012T6', 'EU500M_E054N018T6', 'AF500M_E042N090T6' ]
def test_lonlat2xy_numpy_array_no_sgrid(): """ Tests lonlat to xy projection using numpy arrays. """ e7 = Equi7Grid(500) x_should = np.array([5138743.127891, 5138743.127891]) y_should = np.array([1307029.157093, 1307029.157093]) lon = np.array([15.1, 15.1]) lat = np.array([45.3, 45.3]) sgrid_id, x, y = e7.lonlat2xy(lon, lat) nptest.assert_array_equal(sgrid_id, np.array(['EU', 'EU'])) nptest.assert_allclose(x_should, x) nptest.assert_allclose(y_should, y)
def test_boundary(self): """ Tests equality of tile and data cube boundary. """ dc = SIG0DataCube(filepaths=self.gt_filepaths, dimensions=['time'], sres=500, sdim_name="tile_name") dc.filter_spatially_by_tilename("E042N012T6", inplace=True) boundary = dc.boundary equi7 = Equi7Grid(500) tile_oi = equi7.EU.tilesys.create_tile(name="E042N012T6") assert ogr.CreateGeometryFromWkt(boundary.wkt).ExportToWkt( ) == tile_oi.get_extent_geometry_proj().ConvexHull().ExportToWkt()
def test_search_tiles_lon_lat_extent_by_points(self): """ Tests searching for tiles with input of lon lat points """ e7 = Equi7Grid(500) tiles = e7.search_tiles_in_roi(points=[(10, 40), (5, 50), (-90.9, -1.2), (-175.2, 66)], coverland=True) desired_tiles = ['EU500M_E042N006T6', 'EU500M_E042N018T6', 'AS500M_E072N090T6', 'SA500M_E036N066T6'] assert sorted(tiles) == sorted(desired_tiles)
def test_lonlat2ij_in_tile(self): """ Tests the identification of column n rows indices in a Equi7Grid's tile """ e7 = Equi7Grid(500) column_should = 1199 row_should = 0 tile_should = 'EU500M_E048N012T6' tilename, i, j = e7.lonlat2ij_in_tile(18.507, 44.571, lowerleft=True) nptest.assert_equal(i, column_should) nptest.assert_equal(j, row_should) nptest.assert_equal(tilename, tile_should)
def test_search_tiles_spitzbergen(self): """ Tests the tile searching over Spitzbergen in the polar zone; ROI defined by a 4-corner polygon over high latitudes (is much curved on the globe). """ grid = Equi7Grid(500) spitzbergen_geom = setup_test_geom_spitzbergen() spitzbergen_geom_tiles = sorted(['EU500M_E054N042T6', 'EU500M_E054N048T6', 'EU500M_E060N042T6', 'EU500M_E060N048T6']) tiles = sorted(grid.search_tiles_in_roi(spitzbergen_geom, coverland=False)) assert sorted(tiles) == sorted(spitzbergen_geom_tiles)
def test_create_tiles_overlapping_xybbox(self): e7_500 = Equi7Grid(500) tiles = e7_500.EU.tilesys.create_tiles_overlapping_xybbox( [5138743, 1111111, 6200015, 1777777]) nptest.assert_equal(tiles.shape, (2, 3)) nptest.assert_equal([x.name for x in tiles.flatten()], ['EU500M_E048N012T6', 'EU500M_E054N012T6', 'EU500M_E060N012T6', 'EU500M_E048N006T6', 'EU500M_E054N006T6', 'EU500M_E060N006T6']) nptest.assert_equal(tiles[0,0].active_subset_px, (677, 0, 1200, 1155)) nptest.assert_equal(tiles[1,0].active_subset_px, (677, 1022, 1200, 1200)) nptest.assert_equal(tiles[0,2].active_subset_px, (0, 0, 400, 1155))
def test_search_tiles_lon_lat_extent(): """ Tests searching for tiles with input of lon lat extent """ e7 = Equi7Grid(500) tiles = e7.search_tiles_in_roi(extent=[0, 30, 10, 40], coverland=True) tiles_all = e7.search_tiles_in_roi(extent=[-179.9, -89.9, 179.9, 89.9], coverland=True) desired_tiles = [ 'EU500M_E036N006T6', 'EU500M_E042N000T6', 'EU500M_E042N006T6', 'AF500M_E030N084T6', 'AF500M_E030N090T6', 'AF500M_E036N084T6', 'AF500M_E036N090T6', 'AF500M_E042N084T6', 'AF500M_E042N090T6' ] assert len(tiles_all) == 832 assert sorted(tiles) == sorted(desired_tiles)
def _setUp(self): # build data cube if self.grid is None: self.grid = Equi7Grid(self.grid_sampling).EU self.sref = osr.SpatialReference() self.sref.ImportFromEPSG(4326) dir_tree = build_smarttree(self.path, hierarchy=[], register_file_pattern="^[^Q].*.tiff") filepaths = dir_tree.file_register dim = ["time", "area", "var_name"] self.dc = dc.EODataCube(filepaths=filepaths, smart_filename_class=CglsS1Filename, dimensions=dim)
def test_tile_get_extent_geometry_geog(self): """ Tests the geometry functions of the tile object. """ e7g = Equi7Grid(500) tile_up_north = e7g.create_tile('EU500M_E054N054T6') # more precise would be: (-35.42781, 81.57133, 55.67043, 87.77046) with decimal=5 nptest.assert_almost_equal(tile_up_north.bbox_geog, (-35.43, 81.57, 55.67, 87.77), decimal=2) tile_antimeridian = e7g.create_tile('NA500M_E042N078T6') nptest.assert_almost_equal(tile_antimeridian.bbox_geog, (174.93808, 54.32175, -172.33224, 60.55437), decimal=5)
def test_ij2xy(self): """ Tests tile indices to xy coordination in the subgrid projection """ e7 = Equi7Grid(500) x_should = 3166500 y_should = 5178000 tile = e7.EU.tilesys.create_tile(x=3245631, y=5146545) x, y = tile.ij2xy(333, 444) nptest.assert_allclose(x_should, x) nptest.assert_allclose(y_should, y) # lowerleft case x_should = 5399500 y_should = 1200000 tile = e7.EU.tilesys.create_tile(x=4800123, y=1200123) x, y = tile.ij2xy(1199, 0, lowerleft=True) nptest.assert_allclose(x_should, x) nptest.assert_allclose(y_should, y)