def test_output_datatype(self): """Test resampling method using a scaling factor of 4""" scale = 4 # assumes square cells self.ifgs.append(DEM(SML_TEST_DEM_TIF)) self.ifg_paths = [i.data_path for i in self.ifgs] + [SML_TEST_DEM_TIF] self.headers.append(SML_TEST_DEM_HDR) cext = self._custom_extents_tuple() xlooks = ylooks = scale prepare_ifgs(self.ifg_paths, CUSTOM_CROP, xlooks, ylooks, thresh=1.0, user_exts=cext, headers=self.headers) for i in self.ifg_paths: mlooked_ifg = mlooked_path(i, xlooks, CUSTOM_CROP) ds1 = DEM(mlooked_ifg) ds1.open() ds2 = DEM(i) ds2.open() self.assertEqual( ds1.dataset.GetRasterBand(1).DataType, ds2.dataset.GetRasterBand(1).DataType) ds1 = ds2 = None
def test_conv2tif_file_types(tempdir, gamma_conf): tdir = Path(tempdir()) params = manipulate_test_conf(gamma_conf, tdir) params[cf.COH_MASK] = 1 output_conf_file = 'conf.conf' output_conf = tdir.joinpath(output_conf_file) cf.write_config_file(params=params, output_conf_file=output_conf) params_s = configuration.Configuration(output_conf).__dict__ conv2tif.main(params_s) ifg_files = list( Path(tdir.joinpath(params_s[cf.OUT_DIR])).glob('*_ifg.tif')) coh_files = list( Path(tdir.joinpath(params_s[cf.OUT_DIR])).glob('*_coh.tif')) dem_file = list( Path(tdir.joinpath(params_s[cf.OUT_DIR])).glob('*_dem.tif'))[0] # assert coherence and ifgs have correct metadata for i in itertools.chain(*[ifg_files, coh_files]): ifg = Ifg(i) ifg.open() md = ifg.meta_data if i.name.endswith('_ifg.tif'): assert md[ifc.DATA_TYPE] == ifc.ORIG continue if i.name.endswith('_coh.tif'): assert md[ifc.DATA_TYPE] == ifc.COH continue # assert dem has correct metadata dem = DEM(dem_file.as_posix()) dem.open() md = dem.dataset.GetMetadata() assert md[ifc.DATA_TYPE] == ifc.DEM shutil.rmtree(tdir)
def test_output_datatype(self): """Test resampling method using a scaling factor of 4""" scale = 4 # assumes square cells self.ifgs.append(DEM(SML_TEST_DEM_TIF)) ifg_paths = [i.data_path for i in self.ifgs] data_types = [InputTypes.IFG] * len(self.ifg_paths) ifg_paths.append(SML_TEST_DEM_TIF) data_types.append(InputTypes.DEM) self.headers.append(SML_TEST_DEM_HDR) cext = self._custom_extents_tuple() xlooks = ylooks = scale prepare_ifgs(ifg_paths, CUSTOM_CROP, xlooks, ylooks, thresh=1.0, user_exts=cext, headers=self.headers, params=self.params) self.params[C.IFG_LKSX] = xlooks self.params[C.IFG_LKSY] = ylooks self.params[C.IFG_CROP_OPT] = CUSTOM_CROP for i, t in zip(ifg_paths, data_types): mlooked_ifg = mlooked_path(i, self.params, input_type=t) ds1 = DEM(mlooked_ifg) ds1.open() ds2 = DEM(i) ds2.open() self.assertEqual( ds1.dataset.GetRasterBand(1).DataType, ds2.dataset.GetRasterBand(1).DataType) ds1 = ds2 = None
def test_multilook(self): """Test resampling method using a scaling factor of 4""" scale = 4 # assumes square cells self.ifgs.append(DEM(SML_TEST_DEM_TIF)) self.ifg_paths = [i.data_path for i in self.ifgs] # append the dem header self.headers.append(SML_TEST_DEM_HDR) cext = self._custom_extents_tuple() xlooks = ylooks = scale prepare_ifgs(self.ifg_paths, CUSTOM_CROP, xlooks, ylooks, thresh=1.0, user_exts=cext, headers=self.headers, params=self.params) for n, ipath in enumerate([self.exp_files[3], self.exp_files[7]]): i = Ifg(ipath) i.open() self.assertEqual(i.dataset.RasterXSize, 20 / scale) self.assertEqual(i.dataset.RasterYSize, 28 / scale) # verify resampling path = join(PREP_TEST_TIF, "geo_%s.tif" % Path(ipath).name.split('_ifg')[0]) ds = gdal.Open(path) src_data = ds.GetRasterBand(2).ReadAsArray() exp_resample = multilooking(src_data, scale, scale, thresh=0) self.assertEqual(exp_resample.shape, (7, 5)) assert_array_almost_equal(exp_resample, i.phase_band.ReadAsArray()) ds = None i.close() os.remove(ipath) # verify DEM has been correctly processed # ignore output values as resampling has already been tested for phase exp_dem_path = join(self.params[C.GEOMETRY_DIR], 'dem.tif') self.assertTrue(exists(exp_dem_path)) orignal_dem = DEM(SML_TEST_DEM_TIF) orignal_dem.open() dem_dtype = orignal_dem.dataset.GetRasterBand(1).DataType orignal_dem.close() dem = DEM(exp_dem_path) dem.open() # test multilooked dem is of the same datatype as the original dem tif self.assertEqual(dem_dtype, dem.dataset.GetRasterBand(1).DataType) self.assertEqual(dem.dataset.RasterXSize, 20 / scale) self.assertEqual(dem.dataset.RasterYSize, 28 / scale) data = dem.data self.assertTrue(data.ptp() != 0) # close ifgs dem.close() for i in self.ifgs: i.close() os.remove(exp_dem_path)
def gen_color_file(input_file): fp, temp_file = tempfile.mkstemp(suffix='.txt') dem = DEM(input_file) dem.open() phase_data = dem.height_band.ReadAsArray() max_ph = np.nanmax(phase_data) min_ph = np.nanmin(phase_data) range_ph = max_ph - min_ph colors = ['black', 'blue', 'yellow', 'orange', 'red', 'white'] with open(temp_file, 'w') as f: for i, c in enumerate(colors[:-1]): f.write( str(int(min_ph + (i + 1) * range_ph / len(colors))) + ' ' + c + '\n') f.write( str(int(max_ph - range_ph / len(colors))) + ' ' + colors[-1] + '\n') os.close(fp) return temp_file
class TestDEMTests: 'Unit tests to verify operations on GeoTIFF format DEMs' def setup_method(self): self.ras = DEM(SML_TEST_DEM_TIF) def test_create_raster(self): # validate header path assert os.path.exists(self.ras.data_path) def test_headers_as_attr(self): self.ras.open() attrs = ['ncols', 'nrows', 'x_first', 'x_step', 'y_first', 'y_step'] # TODO: are 'projection' and 'datum' attrs needed? for a in attrs: assert getattr(self.ras, a) is not None def test_is_dem(self): self.ras = DEM(join(SML_TEST_TIF, 'geo_060619-061002_unw.tif')) assert ~hasattr(self.ras, 'datum') def test_open(self): assert self.ras.dataset is None self.ras.open() assert self.ras.dataset is not None assert isinstance(self.ras.dataset, Dataset) # ensure open cannot be called twice with pytest.raises(RasterException): self.ras.open() def test_band_fails_with_unopened_raster(self): # test accessing bands with open and unopened datasets with pytest.raises(RasterException): self.ras.height_band def test_band_read_with_open_raster(self): self.ras.open() data = self.ras.height_band.ReadAsArray() assert data.shape == (72, 47)
class DEMTests(unittest.TestCase): 'Unit tests to verify operations on GeoTIFF format DEMs' def setUp(self): self.ras = DEM(SML_TEST_DEM_TIF) def test_create_raster(self): # validate header path self.assertTrue(os.path.exists(self.ras.data_path)) def test_headers_as_attr(self): self.ras.open() attrs = ['ncols', 'nrows', 'x_first', 'x_step', 'y_first', 'y_step'] # TODO: are 'projection' and 'datum' attrs needed? for a in attrs: self.assertTrue(getattr(self.ras, a) is not None) def test_is_dem(self): self.ras = DEM(join(SML_TEST_TIF, 'geo_060619-061002_unw.tif')) self.assertFalse(hasattr(self.ras, 'datum')) def test_open(self): self.assertTrue(self.ras.dataset is None) self.ras.open() self.assertTrue(self.ras.dataset is not None) self.assertTrue(isinstance(self.ras.dataset, Dataset)) # ensure open cannot be called twice self.assertRaises(RasterException, self.ras.open) def test_band(self): # test accessing bands with open and unopened datasets try: _ = self.ras.height_band self.fail("Should not be able to access band without open dataset") except RasterException: pass self.ras.open() data = self.ras.height_band.ReadAsArray() self.assertEqual(data.shape, (72, 47))
def test_prepifg_file_types(tempdir, gamma_conf, coh_mask): tdir = Path(tempdir()) params = manipulate_test_conf(gamma_conf, tdir) params[cf.COH_MASK] = coh_mask params[cf.PARALLEL] = 0 output_conf_file = 'conf.conf' output_conf = tdir.joinpath(output_conf_file) cf.write_config_file(params=params, output_conf_file=output_conf) params_s = Configuration(output_conf).__dict__ conv2tif.main(params_s) # reread params from config params_s = Configuration(output_conf).__dict__ prepifg.main(params_s) ifg_files = list( Path(tdir.joinpath(params_s[cf.OUT_DIR])).glob('*_ifg.tif')) assert len(ifg_files) == 17 mlooked_files = list( Path(tdir.joinpath(params_s[cf.OUT_DIR])).glob('*_ifg_1rlks_1cr.tif')) assert len(mlooked_files) == 17 coh_files = list( Path(tdir.joinpath(params_s[cf.OUT_DIR])).glob('*_cc_coh.tif')) mlooked_coh_files = list( Path(tdir.joinpath( params_s[cf.OUT_DIR])).glob('*_cc_coh_1rlks_1cr.tif')) if coh_mask: assert len(coh_files) == 17 assert len(mlooked_coh_files) == 17 dem_file = list( Path(tdir.joinpath(params_s[cf.OUT_DIR])).glob('*_dem.tif'))[0] mlooked_dem_file = list( Path(tdir.joinpath( params_s[cf.OUT_DIR])).glob('*_dem_1rlks_1cr.tif'))[0] import itertools # assert coherence and ifgs have correct metadata for i in itertools.chain( *[ifg_files, mlooked_files, coh_files, mlooked_coh_files]): ifg = Ifg(i) ifg.open() md = ifg.meta_data if i.name.endswith('_ifg.tif'): assert md[ifc.DATA_TYPE] == ifc.ORIG continue if i.name.endswith('_coh.tif'): assert md[ifc.DATA_TYPE] == ifc.COH continue if i.name.endswith('_cc_coh_1rlks_1cr.tif'): assert md[ifc.DATA_TYPE] == ifc.MULTILOOKED_COH continue if i.name.endswith('_ifg_1rlks_1cr.tif'): if coh_mask: assert md[ifc.DATA_TYPE] == ifc.COHERENCE else: assert md[ifc.DATA_TYPE] == ifc.MULTILOOKED continue # assert dem has correct metadata dem = DEM(dem_file.as_posix()) dem.open() md = dem.dataset.GetMetadata() assert md[ifc.DATA_TYPE] == ifc.DEM dem = DEM(mlooked_dem_file.as_posix()) dem.open() md = dem.dataset.GetMetadata() assert md[ifc.DATA_TYPE] == ifc.MLOOKED_DEM shutil.rmtree(tdir)
def test_prepifg_file_types(tempdir, gamma_conf, coh_mask): tdir = Path(tempdir()) params = manipulate_test_conf(gamma_conf, tdir) params[C.COH_MASK] = coh_mask params[C.PARALLEL] = 0 output_conf_file = 'conf.conf' output_conf = tdir.joinpath(output_conf_file) pyrate.configuration.write_config_file(params=params, output_conf_file=output_conf) params_s = Configuration(output_conf).__dict__ conv2tif.main(params_s) # reread params from config params_s = Configuration(output_conf).__dict__ prepifg.main(params_s) ifg_files = list( Path(tdir.joinpath(params_s[C.INTERFEROGRAM_DIR])).glob('*_unw.tif')) assert len(ifg_files) == 17 mlooked_files = list( Path(tdir.joinpath(params_s[C.INTERFEROGRAM_DIR])).glob('*_ifg.tif')) assert len(mlooked_files) == 17 coh_files = list( Path(tdir.joinpath(params_s[C.COHERENCE_DIR])).glob('*_cc.tif')) mlooked_coh_files = list( Path(tdir.joinpath(params_s[C.COHERENCE_DIR])).glob('*_coh.tif')) if coh_mask: assert len(coh_files) == 17 assert len(mlooked_coh_files) == 17 dem_file = list( Path(tdir.joinpath(params_s[C.GEOMETRY_DIR])).glob('*_dem.tif'))[0] mlooked_dem_file = list( Path(tdir.joinpath(params_s[C.GEOMETRY_DIR])).glob('dem.tif'))[0] import itertools # assert coherence and ifgs have correct metadata for i in itertools.chain( *[ifg_files, mlooked_files, coh_files, mlooked_coh_files]): ifg = Ifg(i) ifg.open() md = ifg.meta_data if i.name.endswith('_unw.tif'): assert md[ifc.DATA_TYPE] == ifc.ORIG assert ifc.IFG_LKSX not in md assert ifc.IFG_LKSY not in md assert ifc.IFG_CROP not in md continue if i.name.endswith('_cc.tif'): assert md[ifc.DATA_TYPE] == ifc.COH assert ifc.IFG_LKSX not in md assert ifc.IFG_LKSY not in md assert ifc.IFG_CROP not in md continue if i.name.endswith('_coh.tif'): assert md[ifc.DATA_TYPE] == ifc.MULTILOOKED_COH assert md[ifc.IFG_LKSX] == '1' assert md[ifc.IFG_LKSY] == '1' assert md[ifc.IFG_CROP] == '1' continue if i.name.endswith('_ifg.tif'): if coh_mask: assert md[ifc.DATA_TYPE] == ifc.MLOOKED_COH_MASKED_IFG assert md[ifc.IFG_LKSX] == '1' assert md[ifc.IFG_LKSY] == '1' assert md[ifc.IFG_CROP] == '1' else: assert md[ifc.DATA_TYPE] == ifc.MULTILOOKED assert md[ifc.IFG_LKSX] == '1' assert md[ifc.IFG_LKSY] == '1' assert md[ifc.IFG_CROP] == '1' continue # assert dem has correct metadata dem = DEM(dem_file.as_posix()) dem.open() md = dem.dataset.GetMetadata() assert md[ifc.DATA_TYPE] == ifc.DEM assert ifc.IFG_LKSX not in md assert ifc.IFG_LKSY not in md assert ifc.IFG_CROP not in md dem = DEM(mlooked_dem_file.as_posix()) dem.open() md = dem.dataset.GetMetadata() assert md[ifc.DATA_TYPE] == ifc.MLOOKED_DEM assert ifc.IFG_LKSX in md assert ifc.IFG_LKSY in md assert ifc.IFG_CROP in md shutil.rmtree(tdir)