def run_deramp(fname, ramp_type, mask_file=None, out_file=None, datasetName=None): """ Remove ramp from each 2D matrix of input file Parameters: fname : str, data file to be derampped ramp_type : str, name of ramp to be estimated. mask_file : str, file of mask of pixels used for ramp estimation out_file : str, output file name datasetName : str, output dataset name, for ifgramStack file type only Returns: out_file : str, output file name """ print('remove {} ramp from file: {}'.format(ramp_type, fname)) if not out_file: fbase, fext = os.path.splitext(fname) out_file = '{}_ramp{}'.format(fbase, fext) start_time = time.time() atr = readfile.read_attribute(fname) # mask if os.path.isfile(mask_file): mask = readfile.read(mask_file, datasetName='mask')[0] print('read mask file: '+mask_file) else: mask = np.ones((int(atr['LENGTH']), int(atr['WIDTH']))) print('use mask of the whole area') # deramping k = atr['FILE_TYPE'] if k == 'timeseries': print('reading data ...') data = readfile.read(fname)[0] print('estimating phase ramp ...') data = deramp(data, mask, ramp_type=ramp_type, metadata=atr)[0] writefile.write(data, out_file, ref_file=fname) elif k == 'ifgramStack': obj = ifgramStack(fname) obj.open(print_msg=False) if not datasetName: datasetName = 'unwrapPhase' with h5py.File(fname, 'a') as f: ds = f[datasetName] dsNameOut = '{}_ramp'.format(datasetName) if dsNameOut in f.keys(): dsOut = f[dsNameOut] print('access HDF5 dataset /{}'.format(dsNameOut)) else: dsOut = f.create_dataset(dsNameOut, shape=(obj.numIfgram, obj.length, obj.width), dtype=np.float32, chunks=True, compression=None) print('create HDF5 dataset /{}'.format(dsNameOut)) prog_bar = ptime.progressBar(maxValue=obj.numIfgram) for i in range(obj.numIfgram): data = ds[i, :, :] data = deramp(data, mask, ramp_type=ramp_type, metadata=atr)[0] dsOut[i, :, :] = data prog_bar.update(i+1, suffix='{}/{}'.format(i+1, obj.numIfgram)) prog_bar.close() print('finished writing to file: '.format(fname)) # Single Dataset File else: data = readfile.read(fname)[0] data = deramp(data, mask, ramp_type, metadata=atr)[0] print('writing >>> {}'.format(out_file)) writefile.write(data, out_file=out_file, ref_file=fname) m, s = divmod(time.time()-start_time, 60) print('time used: {:02.0f} mins {:02.1f} secs.'.format(m, s)) return out_file
def run_unwrap_error_bridge(ifgram_file, mask_cc_file, bridges, dsNameIn='unwrapPhase', dsNameOut='unwrapPhase_bridging', ramp_type=None): """Run unwrapping error correction with bridging Parameters: ifgram_file : str, path of ifgram stack file mask_cc_file : str, path of conn comp mask file bridges : list of dicts, check bridge_unwrap_error() for details dsNameIn : str, dataset name of unwrap phase to be corrected dsNameOut : str, dataset name of unwrap phase to be saved after correction ramp_type : str, name of phase ramp to be removed during the phase jump estimation Returns: ifgram_file : str, path of ifgram stack file """ print('-'*50) print('correct unwrapping error in {} with bridging ...'.format(ifgram_file)) # file info atr = readfile.read_attribute(ifgram_file) length, width = int(atr['LENGTH']), int(atr['WIDTH']) ref_y, ref_x = int(atr['REF_Y']), int(atr['REF_X']) k = atr['FILE_TYPE'] # read mask print('read mask from file: {}'.format(mask_cc_file)) mask_cc = readfile.read(mask_cc_file, datasetName='mask')[0] if ramp_type is not None: print('estimate and remove phase ramp of {} during the correction'.format(ramp_type)) mask4ramp = (mask_cc == mask_cc[ref_y, ref_x]) # 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 date12 = date12_list[i] unw = np.squeeze(f[dsNameIn][i, :, :]) unw[unw != 0.] -= unw[ref_y, ref_x] # remove phase ramp before phase jump estimation if ramp_type is not None: unw, unw_ramp = deramp(unw, mask4ramp, ramp_type, metadata=atr) # estimate/correct phase jump unw_cor = bridge_unwrap_error(unw, mask_cc, bridges) if ramp_type is not None: unw_cor += unw_ramp # 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 data unw = readfile.read(ifgram_file)[0] unw[unw != 0.] -= unw[ref_y, ref_x] # remove phase ramp before phase jump estimation if ramp_type is not None: unw, unw_ramp = deramp(unw, mask4ramp, ramp_type, metadata=atr) # estimate/correct phase jump unw_cor = bridge_unwrap_error(unw, mask_cc, bridges) if ramp_type is not None: unw_cor += unw_ramp # 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 get_boxes4deforming_area(vel_file, mask_file, win_size=30, min_percentage=0.2, ramp_type='quadratic', display=False): """Get list of boxes to cover the deforming areas. A pixel is identified as deforming if its velocity exceeds the MAD of the whole image. Parameters: vel_file : str, path of velocity file mask_file : str, path of mask file win_size : int, length and width of the output box min_percentage : float between 0 and 1, minimum percentage of deforming points in the box ramp_type : str, type of phase ramps to be removed while evaluating the deformation display : bool, plot the identification result or not Returns: box_list : list of t-tuple of int, each indicating (col0, row0, col1, row1) """ print('-' * 30) print('get boxes on deforming areas') mask = readfile.read(mask_file)[0] vel, atr = readfile.read(vel_file) print('removing a {} phase ramp from input velocity before the evaluation'. format(ramp_type)) vel = deramp(vel, mask, ramp_type=ramp_type, metadata=atr)[0] #remove ramp before the evaluation # get deforming pixels mad = ut.median_abs_deviation_threshold(vel[mask], center=0., cutoff=3) #deformation threshold print('velocity threshold / median abs dev: {:.3f} cm/yr'.format(mad)) vel[mask == 0] = 0 mask_aoi = (vel >= mad) + (vel <= -1. * mad) print('number of points: {}'.format(np.sum(mask_aoi))) # get deforming boxes box_list = [] min_num = min_percentage * (win_size**2) length, width = vel.shape num_row = np.ceil(length / win_size).astype(int) num_col = np.ceil(width / win_size).astype(int) for i in range(num_row): r0 = i * win_size r1 = min([length, r0 + win_size]) for j in range(num_col): c0 = j * win_size c1 = min([width, c0 + win_size]) box = (c0, r0, c1, r1) if np.sum(mask_aoi[r0:r1, c0:c1]) >= min_num: box_list.append(box) print('number of boxes : {}'.format(len(box_list))) if display: fig, axs = plt.subplots(nrows=1, ncols=2, figsize=[12, 8], sharey=True) vel[mask == 0] = np.nan axs[0].imshow(vel, cmap='jet') axs[1].imshow(mask_aoi, cmap='gray') for box in box_list: for ax in axs: rect = Rectangle((box[0], box[1]), (box[2] - box[0]), (box[3] - box[1]), linewidth=2, edgecolor='r', fill=False) ax.add_patch(rect) plt.show() return box_list