def setup_class(cls, gamma_params): # change the required params shutil.rmtree(gamma_params[cf.OUT_DIR]) # start with a clean directory shared.mkdir_p(gamma_params[cf.OUT_DIR]) cls.params = gamma_params cls.params[cf.OBS_DIR] = common.SML_TEST_GAMMA cls.params[cf.PROCESSOR] = 1 # gamma cls.params[cf.PARALLEL] = 0 cls.params[cf.REF_EST_METHOD] = 1 cls.params[cf.DEM_FILE] = common.SML_TEST_DEM_GAMMA # base_unw_paths need to be geotiffed and multilooked by run_prepifg cls.base_unw_paths = cf.original_ifg_paths( cls.params[cf.IFG_FILE_LIST], cls.params[cf.OBS_DIR]) cls.base_unw_paths.append(common.SML_TEST_DEM_GAMMA) # dest_paths are tifs that have been geotif converted and multilooked conv2tif.main(cls.params) prepifg.main(cls.params) cls.dest_paths = [ Path(cls.params[cf.OUT_DIR]).joinpath(Path( c.sampled_path).name).as_posix() for c in cls.params[cf.INTERFEROGRAM_FILES][:-2] ] cls.ifgs = [dem_or_ifg(i) for i in cls.dest_paths] for i in cls.ifgs: i.open() i.nodata_value = 0
def prepare_ifgs(raster_data_paths, crop_opt, xlooks, ylooks, headers, thresh=0.5, user_exts=None, write_to_disc=True, out_path=None): """ Wrapper function to prepare a sequence of interferogram files for PyRate analysis. See prepifg.prepare_ifg() for full description of inputs and returns. Note: function need refining for crop options :param list raster_data_paths: List of interferogram file paths :param int crop_opt: Crop option :param int xlooks: Number of multi-looks in x; 5 is 5 times smaller, 1 is no change :param int ylooks: Number of multi-looks in y :param float thresh: see thresh in prepare_ifgs() :param tuple user_exts: Tuple of user defined georeferenced extents for new file: (xfirst, yfirst, xlast, ylast)cropping coordinates :param bool write_to_disk: Write new data to disk :return: resampled_data: output cropped and resampled image :rtype: ndarray :return: out_ds: destination gdal dataset object :rtype: List[gdal.Dataset] """ # use metadata check to check whether it's a dem or ifg rasters = [dem_or_ifg(r) for r in raster_data_paths] exts = get_analysis_extent(crop_opt, rasters, xlooks, ylooks, user_exts) return [prepare_ifg(d, xlooks, ylooks, exts, thresh, crop_opt, h, write_to_disc, out_path) for d, h in zip(raster_data_paths, headers)]
def setup_class(cls, gamma_params): # change the required params shared.mkdir_p(gamma_params[C.OUT_DIR]) from copy import deepcopy cls.params = deepcopy(gamma_params) cls.params[WORKING_DIR] = common.GAMMA_SML_TEST_DIR cls.params[C.PROCESSOR] = 1 # gamma cls.params[C.PARALLEL] = 0 cls.params[C.REF_EST_METHOD] = 1 cls.params[C.DEM_FILE] = common.SML_TEST_DEM_GAMMA cls.params[C.BASE_FILE_LIST] = common.GAMMA_SML_TEST_DIR # base_unw_paths need to be geotiffed and multilooked by run_prepifg cls.base_unw_paths = tests.common.original_ifg_paths( cls.params[C.IFG_FILE_LIST], cls.params[WORKING_DIR]) cls.base_unw_paths.append(common.SML_TEST_DEM_GAMMA) # dest_paths are tifs that have been geotif converted and multilooked conv2tif.main(cls.params) prepifg.main(cls.params) cls.dest_paths = [ Path(cls.params[C.INTERFEROGRAM_DIR]).joinpath( Path(c.sampled_path).name).as_posix() for c in gamma_params[C.INTERFEROGRAM_FILES] ] cls.dest_paths += [ Path(cls.params[C.COHERENCE_DIR]).joinpath( Path(c.sampled_path).name).as_posix() for c in gamma_params[C.COHERENCE_FILE_PATHS] ] cls.ifgs = [dem_or_ifg(i) for i in cls.dest_paths] for i in cls.ifgs: i.open() i.nodata_value = 0
def __setup_edges(ifg_files: List[str]) -> List[Edge]: ifg_files.sort() ifgs = [dem_or_ifg(i) for i in ifg_files] for i in ifgs: i.open() i.nodata_value = 0 return [Edge(i.first, i.second) for i in ifgs]
def test_orbital_correction(self): key = 'ORBITAL_ERROR' value = 'REMOVED' ifgs = [dem_or_ifg(i) for i in self.sampled_paths] for i in ifgs: i.open() i.nodata_value = 0 self.key_check(i, key, value)
def copy_and_setup_small_data(): temp_dir, datafiles = copy_small_ifg_file_list() datafiles.sort() ifgs = [dem_or_ifg(i) for i in datafiles] for i in ifgs: i.open() i.nodata_value = 0 return temp_dir, ifgs
def test_phase_conversion(self): # ensure phase has been converted from radians to millimetres key = 'DATA_UNITS' value = 'MILLIMETRES' ifgs = [dem_or_ifg(i) for i in self.sampled_paths] for i in ifgs: i.open() i.nodata_value = 0 self.key_check(i, key, value)
def __extents_from_params(params): multi_paths = params[C.INTERFEROGRAM_FILES] ifg_paths = [p.tmp_sampled_path for p in multi_paths] rasters = [shared.dem_or_ifg(r) for r in ifg_paths] crop_opt = prepifg_helper.ALREADY_SAME_SIZE xlooks = params[C.ORBITAL_FIT_LOOKS_X] ylooks = params[C.ORBITAL_FIT_LOOKS_Y] exts = prepifg_helper.get_analysis_extent(crop_opt, rasters, xlooks, ylooks, None) return exts, ifg_paths, multi_paths
def prepare_ifg(raster_path, xlooks, ylooks, exts, thresh, crop_opt, header, write_to_disk=True, out_path=None, coherence_path=None, coherence_thresh=None): """ Open, resample, crop and optionally save to disk an interferogram or DEM. Returns are only given if write_to_disk=False :param str raster_path: Input raster file path name :param int xlooks: Number of multi-looks in x; 5 is 5 times smaller, 1 is no change :param int ylooks: Number of multi-looks in y :param tuple exts: Tuple of user defined georeferenced extents for new file: (xfirst, yfirst, xlast, ylast)cropping coordinates :param float thresh: see thresh in prepare_ifgs() :param int crop_opt: Crop option :param bool write_to_disk: Write new data to disk :param str out_path: Path for output file :param dict header: dictionary of metadata from header file :return: resampled_data: output cropped and resampled image :rtype: ndarray :return: out_ds: destination gdal dataset object :rtype: gdal.Dataset """ do_multilook = xlooks > 1 or ylooks > 1 # resolution=None completes faster for non-multilooked layers in gdalwarp resolution = [None, None] raster = dem_or_ifg(raster_path) if not raster.is_open: raster.open() if do_multilook: resolution = [xlooks * raster.x_step, ylooks * raster.y_step] # cut, average, resample the final output layers op = output_tiff_filename(raster.data_path, out_path) looks_path = cf.mlooked_path(op, ylooks, crop_opt) if xlooks != ylooks: raise ValueError('X and Y looks mismatch') # # Add missing/updated metadata to resampled ifg/DEM # new_lyr = type(ifg)(looks_path) # new_lyr.open(readonly=True) # # for non-DEMs, phase bands need extra metadata & conversions # if hasattr(new_lyr, "phase_band"): # # TODO: LOS conversion to vertical/horizontal (projection) # # TODO: push out to workflow # #if params.has_key(REPROJECTION_FLAG): # # reproject() driver_type = 'GTiff' if write_to_disk else 'MEM' resampled_data, out_ds = crop_resample_average( input_tif=raster.data_path, extents=exts, new_res=resolution, output_file=looks_path, thresh=thresh, out_driver_type=driver_type, hdr=header, coherence_path=coherence_path, coherence_thresh=coherence_thresh ) return resampled_data, out_ds
def prepare_ifgs(raster_data_paths, crop_opt, xlooks, ylooks, headers, params, thresh=0.5, user_exts=None, write_to_disc=True): """ Wrapper function to prepare a sequence of interferogram files for PyRate analysis. See prepifg.prepare_ifg() for full description of inputs and returns. Note: function need refining for crop options :param list raster_data_paths: List of interferogram file paths :param int crop_opt: Crop option :param int xlooks: Number of multi-looks in x; 5 is 5 times smaller, 1 is no change :param int ylooks: Number of multi-looks in y :param float thresh: see thresh in prepare_ifgs() :param tuple user_exts: Tuple of user defined georeferenced extents for new file: (xfirst, yfirst, xlast, ylast)cropping coordinates :param bool write_to_disc: Write new data to disk :return: resampled_data: output cropped and resampled image :rtype: ndarray :return: out_ds: destination gdal dataset object :rtype: List[gdal.Dataset] """ if xlooks != ylooks: log.warning('X and Y multi-look factors are not equal') # use metadata check to check whether it's a dem or ifg rasters = [dem_or_ifg(r) for r in raster_data_paths] exts = get_analysis_extent(crop_opt, rasters, xlooks, ylooks, user_exts) out_paths = [] for r, t in zip(raster_data_paths, rasters): if isinstance(t, DEM): input_type = InputTypes.DEM else: input_type = InputTypes.IFG out_path = MultiplePaths(r, params, input_type).sampled_path out_paths.append(out_path) return [ prepare_ifg(d, xlooks, ylooks, exts, thresh, crop_opt, h, write_to_disc, p) for d, h, p in zip(raster_data_paths, headers, out_paths) ]
def _setup_source(input_tif): """convenience setup function for gdal_average""" src_ds = gdal.Open(input_tif) data = src_ds.GetRasterBand(1).ReadAsArray() src_dtype = src_ds.GetRasterBand(1).DataType mem_driver = gdal.GetDriverByName('MEM') src_ds_mem = mem_driver.Create('', src_ds.RasterXSize, src_ds.RasterYSize, 2, src_dtype) if isinstance(shared.dem_or_ifg(data_path=input_tif), shared.Ifg): data[np.isclose(data, 0, atol=1e-6)] = np.nan # nan conversion of phase data src_ds_mem.GetRasterBand(1).WriteArray(data) src_ds_mem.GetRasterBand(1).SetNoDataValue(np.nan) src_ds_mem.GetRasterBand(2).SetNoDataValue(np.nan) src_ds_mem.SetGeoTransform(src_ds.GetGeoTransform()) return src_ds, src_ds_mem
def small_data_setup(datafiles=None, is_dir=False): """Returns Ifg objs for the files in the small test dir input phase data is in radians; these ifgs are in radians - not converted to mm""" if is_dir: datafiles = glob.glob(join(datafiles, "*.tif")) else: if datafiles: for i, d in enumerate(datafiles): datafiles[i] = os.path.join(SML_TEST_TIF, d) else: datafiles = glob.glob(join(SML_TEST_TIF, "*.tif")) datafiles.sort() ifgs = [dem_or_ifg(i) for i in datafiles] for i in ifgs: i.open() i.nodata_value = 0 return ifgs
def __create_multilooked_dataset_for_network_correction(params): multi_paths = params[cf.INTERFEROGRAM_FILES] ifg_paths = [p.tmp_sampled_path for p in multi_paths] headers = [find_header(p, params) for p in multi_paths] crop_opt = prepifg_helper.ALREADY_SAME_SIZE xlooks = params[cf.ORBITAL_FIT_LOOKS_X] ylooks = params[cf.ORBITAL_FIT_LOOKS_Y] thresh = params[cf.NO_DATA_AVERAGING_THRESHOLD] rasters = [shared.dem_or_ifg(r) for r in ifg_paths] exts = prepifg_helper.get_analysis_extent(crop_opt, rasters, xlooks, ylooks, None) out_paths = [tempfile.mktemp() for _ in ifg_paths] mlooked_dataset = [ prepifg_helper.prepare_ifg(d, xlooks, ylooks, exts, thresh, crop_opt, h, False, p) for d, h, p in zip(ifg_paths, headers, out_paths) ] mlooked = [Ifg(m[1]) for m in mlooked_dataset] for m in mlooked: m.initialize() shared.nan_and_mm_convert(m, params) return mlooked
def dem(): d = shared.dem_or_ifg(SML_TEST_DEM_TIF) d.open() return d
def independent_orbital_correction(ifg_path, params): """ Calculates and removes an orbital error surface from a single independent interferogram. Warning: This will write orbital error corrected phase_data to the ifg. :param Ifg class instance ifg: the interferogram to be corrected :param dict params: dictionary of configuration parameters :return: None - interferogram phase data is updated and saved to disk """ log.debug(f"Orbital correction of {ifg_path}") degree = params[C.ORBITAL_FIT_DEGREE] offset = params[C.ORBFIT_OFFSET] intercept = params[C.ORBFIT_INTERCEPT] xlooks = params[C.ORBITAL_FIT_LOOKS_X] ylooks = params[C.ORBITAL_FIT_LOOKS_Y] ifg0 = shared.Ifg(ifg_path) if isinstance(ifg_path, str) else ifg_path # get full-resolution design matrix fullres_dm = get_design_matrix(ifg0, degree, intercept=intercept) ifg = shared.dem_or_ifg(ifg_path) if isinstance(ifg_path, str) else ifg_path multi_path = MultiplePaths(ifg.data_path, params) orb_on_disc = MultiplePaths.orb_error_path(ifg.data_path, params) if not ifg.is_open: ifg.open() shared.nan_and_mm_convert(ifg, params) fullres_ifg = ifg # keep a backup fullres_phase = fullres_ifg.phase_data if orb_on_disc.exists(): log.info( f'Reusing already computed orbital fit correction: {orb_on_disc}') orbital_correction = np.load(file=orb_on_disc) else: # Multi-look the ifg data if either X or Y is greater than 1 if (xlooks > 1) or (ylooks > 1): exts, _, _ = __extents_from_params(params) mlooked = _create_mlooked_dataset(multi_path, ifg.data_path, exts, params) ifg = Ifg(mlooked) # multi-looked Ifg object ifg.initialize() shared.nan_and_mm_convert(ifg, params) # vectorise phase data, keeping NODATA vphase = reshape(ifg.phase_data, ifg.num_cells) # compute design matrix for multi-looked data mlooked_dm = get_design_matrix(ifg, degree, intercept=intercept) # invert to obtain the correction image (forward model) at full-res orbital_correction = __orb_correction(fullres_dm, mlooked_dm, fullres_phase, vphase, offset=offset) # save correction to disc if not orb_on_disc.parent.exists(): shared.mkdir_p(orb_on_disc.parent) np.save(file=orb_on_disc, arr=orbital_correction) # subtract orbital correction from the full-res ifg fullres_ifg.phase_data -= orbital_correction # set orbfit meta tag and save phase to file _save_orbital_error_corrected_phase(fullres_ifg, params)