def get_boxes4deforming_area(vel_file, mask_file, step=2, num_pixel=30**2, 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) """ win_size = int(np.sqrt(num_pixel) * step) print('-'*30) print('get boxes on deforming areas with step: {} pixels'.format(step)) 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]), width=(box[2]-box[0]), height=(box[3]-box[1]), linewidth=2, edgecolor='r', fill=False) ax.add_patch(rect) plt.show() return box_list
def calculate_ramp(data, atr, inps): """calcualte ramp from data file""" ramp_type = inps.surface_type mask = readfile.read(inps.mask)[0] ramp = deramp(data, mask, ramp_type=ramp_type, metadata=atr)[1] ramp[mask == False] = np.nan return ramp
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
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_deramp(fname, ramp_type, mask_file=None, out_file=None, datasetName=None, save_ramp_coeff=False): """ 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 save_ramp_coeff : bool, save the estimated ramp coefficients to text file Returns: out_file : str, output file name """ start_time = time.time() atr = readfile.read_attribute(fname) k = atr['FILE_TYPE'] length = int(atr['LENGTH']) width = int(atr['WIDTH']) 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) if k == 'ifgramStack': out_file = fname # mask if os.path.isfile(mask_file): mask = readfile.read(mask_file)[0] print('read mask file: ' + mask_file) else: mask = np.ones((length, width), dtype=np.bool_) print('use mask of the whole area') # write coefficient of specified surface function fit coeff_file = None if save_ramp_coeff: fbase = os.path.splitext(os.path.basename(fname))[0] coeff_file = os.path.join(os.path.dirname(fbase), 'rampCoeff_{}.txt'.format(fbase)) with open(coeff_file, 'w') as f: f.write('# input file: {}\n'.format(fname)) f.write('# output file: {}\n'.format(out_file)) f.write('# ramp type: {}\n'.format(ramp_type)) # deramping if k == 'timeseries': # write HDF5 file with defined metadata and (empty) dataset structure writefile.layout_hdf5(out_file, ref_file=fname, print_msg=True) print('estimating phase ramp one date at a time ...') date_list = timeseries(fname).get_date_list() num_date = len(date_list) prog_bar = ptime.progressBar(maxValue=num_date) for i in range(num_date): if coeff_file: # prepend epoch name to line of coefficients with open(coeff_file, 'a') as f: f.write('{} '.format((date_list[i]))) # read data = readfile.read(fname, datasetName=date_list[i])[0] # deramp data = deramp(data, mask, ramp_type=ramp_type, metadata=atr, coeff_file=coeff_file)[0] # write writefile.write_hdf5_block(out_file, data, datasetName='timeseries', block=[i, i + 1, 0, length, 0, width], print_msg=False) prog_bar.update(i + 1, suffix='{}/{}'.format(i + 1, num_date)) prog_bar.close() print('finished writing to file: {}'.format(out_file)) 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, length, 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): if coeff_file: # prepend IFG date12 to line of coefficients with open(coeff_file, 'a') as f: f.write('{} '.format(str(obj.date12List[i]))) # read data = ds[i, :, :] # deramp data = deramp(data, mask, ramp_type=ramp_type, metadata=atr, coeff_file=coeff_file)[0] # write 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: if coeff_file: # prepend file-type to line of coefficients with open(coeff_file, 'a') as f: f.write('{} '.format(atr['FILE_TYPE'])) # read data = readfile.read(fname)[0] # deramp data = deramp(data, mask, ramp_type=ramp_type, metadata=atr, coeff_file=coeff_file)[0] # write 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_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 """ start_time = time.time() atr = readfile.read_attribute(fname) k = atr['FILE_TYPE'] 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) if k == 'ifgramStack': out_file = fname # mask if os.path.isfile(mask_file): mask = readfile.read(mask_file)[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 if k == 'timeseries': # initialize dataset structure ts_obj = timeseries(fname) ts_obj.open(print_msg=False) date_list = ts_obj.dateList num_date = len(date_list) date_dtype = np.dtype('S{}'.format(len(date_list[0]))) dsNameDict = { "date": (date_dtype, (num_date, )), "bperp": (np.float32, (num_date, )), "timeseries": (np.float32, (num_date, ts_obj.length, ts_obj.width)), } # write HDF5 file with defined metadata and (empty) dataset structure writefile.layout_hdf5(out_file, dsNameDict, atr, print_msg=False) # write date time-series date_list_utf8 = [dt.encode('utf-8') for dt in date_list] writefile.write_hdf5_block(out_file, date_list_utf8, datasetName='date', print_msg=False) # write bperp time-series bperp = ts_obj.pbase writefile.write_hdf5_block(out_file, bperp, datasetName='bperp', print_msg=False) print('estimating phase ramp one date at a time ...') prog_bar = ptime.progressBar(maxValue=num_date) for i in range(num_date): # read dsName = 'timeseries-{}'.format(date_list[i]) data = readfile.read(fname, datasetName=dsName)[0] # deramp data = deramp(data, mask, ramp_type=ramp_type, metadata=atr)[0] # write writefile.write_hdf5_block( out_file, data, datasetName='timeseries', block=[i, i + 1, 0, ts_obj.length, 0, ts_obj.width], print_msg=False) prog_bar.update(i + 1, suffix='{}/{}'.format(i + 1, num_date)) prog_bar.close() print('finished writing to file: {}'.format(out_file)) 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_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