def run_unwrap_error_bridge(ifgram_file, water_mask_file, ramp_type=None, radius=50, ccName='connectComponent', dsNameIn='unwrapPhase', dsNameOut='unwrapPhase_bridging'): """Run unwrapping error correction with bridging Parameters: ifgram_file : str, path of ifgram stack file water_mask_file : str, path of water mask file ramp_type : str, name of phase ramp to be removed during the phase jump estimation ccName : str, dataset name of connected components dsNameIn : str, dataset name of unwrap phase to be corrected dsNameOut : str, dataset name of unwrap phase to be saved after correction Returns: ifgram_file : str, path of ifgram stack file """ print('-'*50) print('correct unwrapping error in {} with bridging ...'.format(ifgram_file)) if ramp_type is not None: print('estimate and remove a {} ramp while calculating phase offset'.format(ramp_type)) # read water mask if water_mask_file and os.path.isfile(water_mask_file): print('read water mask from file:', water_mask_file) water_mask = readfile.read(water_mask_file)[0] else: water_mask = None # file info atr = readfile.read_attribute(ifgram_file) length, width = int(atr['LENGTH']), int(atr['WIDTH']) k = atr['FILE_TYPE'] # correct unwrap error ifgram by ifgram if k == 'ifgramStack': date12_list = ifgramStack(ifgram_file).get_date12_list(dropIfgram=False) num_ifgram = len(date12_list) shape_out = (num_ifgram, length, width) # prepare output data writing print('open {} with r+ mode'.format(ifgram_file)) f = h5py.File(ifgram_file, 'r+') print('input dataset:', dsNameIn) print('output dataset:', dsNameOut) if dsNameOut in f.keys(): ds = f[dsNameOut] print('access /{d} of np.float32 in size of {s}'.format(d=dsNameOut, s=shape_out)) else: ds = f.create_dataset(dsNameOut, shape_out, maxshape=(None, None, None), chunks=True, compression=None) print('create /{d} of np.float32 in size of {s}'.format(d=dsNameOut, s=shape_out)) # correct unwrap error ifgram by ifgram prog_bar = ptime.progressBar(maxValue=num_ifgram) for i in range(num_ifgram): # read unwrapPhase and connectComponent date12 = date12_list[i] unw = np.squeeze(f[dsNameIn][i, :, :]) cc = np.squeeze(f[ccName][i, :, :]) if water_mask is not None: cc[water_mask == 0] = 0 # bridging cc_obj = connectComponent(conncomp=cc, metadata=atr) cc_obj.label() cc_obj.find_mst_bridge() unw_cor = cc_obj.unwrap_conn_comp(unw, ramp_type=ramp_type) # write to hdf5 file ds[i, :, :] = unw_cor prog_bar.update(i+1, suffix=date12) prog_bar.close() ds.attrs['MODIFICATION_TIME'] = str(time.time()) f.close() print('close {} file.'.format(ifgram_file)) if k == '.unw': # read unwrap phase unw = readfile.read(ifgram_file)[0] # read connected components cc_files0 = [ifgram_file+'.conncomp', os.path.splitext(ifgram_file)[0]+'_snap_connect.byt'] cc_files = [i for i in cc_files0 if os.path.isfile(i)] if len(cc_files) == 0: raise FileNotFoundError(cc_files0) cc = readfile.read(cc_files[0])[0] if water_mask is not None: cc[water_mask == 0] = 0 # bridging cc_obj = connectComponent(conncomp=cc, metadata=atr) cc_obj.label() cc_obj.find_mst_bridge() unw_cor = cc_obj.unwrap_conn_comp(unw, ramp_type=ramp_type) # write to hdf5 file out_file = '{}_unwCor{}'.format(os.path.splitext(ifgram_file)[0], os.path.splitext(ifgram_file)[1]) print('writing >>> {}'.format(out_file)) writefile.write(unw_cor, out_file=out_file, ref_file=ifgram_file) return ifgram_file
def run_unwrap_error_phase_closure(ifgram_file, common_regions, water_mask_file=None, ccName='connectComponent', dsNameIn='unwrapPhase', dsNameOut='unwrapPhase_phaseClosure'): print('-' * 50) print('correct unwrapping error in {} with phase closure ...'.format( ifgram_file)) stack_obj = ifgramStack(ifgram_file) stack_obj.open() length, width = stack_obj.length, stack_obj.width ref_y, ref_x = stack_obj.refY, stack_obj.refX date12_list = stack_obj.get_date12_list(dropIfgram=False) num_ifgram = len(date12_list) shape_out = (num_ifgram, length, width) # read water mask if water_mask_file and os.path.isfile(water_mask_file): print('read water mask from file:', water_mask_file) water_mask = readfile.read(water_mask_file)[0] else: water_mask = None # prepare output data writing print('open {} with r+ mode'.format(ifgram_file)) f = h5py.File(ifgram_file, 'r+') print('input dataset:', dsNameIn) print('output dataset:', dsNameOut) if dsNameOut in f.keys(): ds = f[dsNameOut] print('access /{d} of np.float32 in size of {s}'.format(d=dsNameOut, s=shape_out)) else: ds = f.create_dataset(dsNameOut, shape_out, maxshape=(None, None, None), chunks=True, compression=None) print('create /{d} of np.float32 in size of {s}'.format(d=dsNameOut, s=shape_out)) # correct unwrap error ifgram by ifgram prog_bar = ptime.progressBar(maxValue=num_ifgram) for i in range(num_ifgram): date12 = date12_list[i] # read unwrap phase to be updated unw_cor = np.squeeze(f[dsNameIn][i, :, :]).astype(np.float32) unw_cor -= unw_cor[ref_y, ref_x] # update kept interferograms only if stack_obj.dropIfgram[i]: # get local region info from connectComponent cc = np.squeeze(f[ccName][i, :, :]) if water_mask is not None: cc[water_mask == 0] = 0 cc_obj = connectComponent(conncomp=cc, metadata=stack_obj.metadata) cc_obj.label() local_regions = measure.regionprops(cc_obj.labelImg) # matching regions and correct unwrap error idx_common = common_regions[0].date12_list.index(date12) for local_reg in local_regions: local_mask = cc_obj.labelImg == local_reg.label U = 0 for common_reg in common_regions: y = common_reg.sample_coords[:, 0] x = common_reg.sample_coords[:, 1] if all(local_mask[y, x]): U = common_reg.int_ambiguity[idx_common] break unw_cor[local_mask] += 2. * np.pi * U # write to hdf5 file ds[i, :, :] = unw_cor prog_bar.update(i + 1, suffix=date12) prog_bar.close() ds.attrs['MODIFICATION_TIME'] = str(time.time()) f.close() print('close {} file.'.format(ifgram_file)) return ifgram_file
def run_unwrap_error_phase_closure(ifgram_file, common_regions, water_mask_file=None, ccName='connectComponent', dsNameIn='unwrapPhase', dsNameOut='unwrapPhase_phaseClosure'): print('-'*50) print('correct unwrapping error in {} with phase closure ...'.format(ifgram_file)) stack_obj = ifgramStack(ifgram_file) stack_obj.open() length, width = stack_obj.length, stack_obj.width ref_y, ref_x = stack_obj.refY, stack_obj.refX date12_list = stack_obj.get_date12_list(dropIfgram=False) num_ifgram = len(date12_list) shape_out = (num_ifgram, length, width) # read water mask if water_mask_file and os.path.isfile(water_mask_file): print('read water mask from file:', water_mask_file) water_mask = readfile.read(water_mask_file)[0] else: water_mask = None # prepare output data writing print('open {} with r+ mode'.format(ifgram_file)) f = h5py.File(ifgram_file, 'r+') print('input dataset:', dsNameIn) print('output dataset:', dsNameOut) if dsNameOut in f.keys(): ds = f[dsNameOut] print('access /{d} of np.float32 in size of {s}'.format(d=dsNameOut, s=shape_out)) else: ds = f.create_dataset(dsNameOut, shape_out, maxshape=(None, None, None), chunks=True, compression=None) print('create /{d} of np.float32 in size of {s}'.format(d=dsNameOut, s=shape_out)) # correct unwrap error ifgram by ifgram prog_bar = ptime.progressBar(maxValue=num_ifgram) for i in range(num_ifgram): date12 = date12_list[i] # read unwrap phase to be updated unw_cor = np.squeeze(f[dsNameIn][i, :, :]).astype(np.float32) unw_cor -= unw_cor[ref_y, ref_x] # update kept interferograms only if stack_obj.dropIfgram[i]: # get local region info from connectComponent cc = np.squeeze(f[ccName][i, :, :]) if water_mask is not None: cc[water_mask == 0] = 0 cc_obj = connectComponent(conncomp=cc, metadata=stack_obj.metadata) cc_obj.label() local_regions = measure.regionprops(cc_obj.labelImg) # matching regions and correct unwrap error idx_common = common_regions[0].date12_list.index(date12) for local_reg in local_regions: local_mask = cc_obj.labelImg == local_reg.label U = 0 for common_reg in common_regions: y = common_reg.sample_coords[:,0] x = common_reg.sample_coords[:,1] if all(local_mask[y, x]): U = common_reg.int_ambiguity[idx_common] break unw_cor[local_mask] += 2. * np.pi * U # write to hdf5 file ds[i, :, :] = unw_cor prog_bar.update(i+1, suffix=date12) prog_bar.close() ds.attrs['MODIFICATION_TIME'] = str(time.time()) f.close() print('close {} file.'.format(ifgram_file)) return ifgram_file