def manual_select_pairs_to_remove(File): '''Manually select interferograms to remove''' print '----------------------------------------------------------------------------' print 'Manually select interferograms to remove' print 'Click two dates - points - in the figure to select one pair of interferogram' print 'repeat until you select all pairs you would like to remove' print 'then close the figure to continue the program ...' print '----------------------------------------------------------------------------' # Display the network fig = plt.figure() ax = fig.add_subplot(111) date12_orig = pnet.get_date12_list(File) bperp_list = ut.perp_baseline_ifgram2timeseries(File)[0].tolist() date8_list = ptime.ifgram_date_list(File) ax = pnet.plot_network(ax, date12_orig, date8_list, bperp_list) print 'display the network of interferogram of file: ' + File date6_list = ptime.yymmdd(date8_list) dates_array = np.array(ptime.date_list2vector(date8_list)[0]) dateNum_array = mdates.date2num(dates_array) bperp_array = np.array(bperp_list) date_click = [] date12_click = [] def onclick(event): xClick = event.xdata yClick = event.ydata idx = nearest_neighbor(xClick, yClick, dateNum_array, bperp_array) date6 = date6_list[idx] print 'click at ' + date6 date_click.append(date6) if len(date_click) % 2 == 0 and date_click[-2] != date_click[-1]: [m_date, s_date] = sorted(date_click[-2:]) m_idx = date6_list.index(m_date) s_idx = date6_list.index(s_date) date12 = m_date + '-' + s_date if date12 in date12_orig: print 'select date12: ' + date12 date12_click.append(date12) ax.plot([dateNum_array[m_idx], dateNum_array[s_idx]], [bperp_array[m_idx], bperp_array[s_idx]], 'r', lw=4) else: print date12 + ' is not existed in input file' plt.draw() cid = fig.canvas.mpl_connect('button_press_event', onclick) plt.show() return date12_click
def read_igram_pairs(igramFile): '''Read pairs index from hdf5 file''' ## Read Igram file h5file = h5py.File(igramFile,'r') k = h5file.keys() if 'interferograms' in k: k[0] = 'interferograms' elif 'coherence' in k: k[0] = 'coherence' if k[0] not in ['interferograms','coherence','wrapped']: print 'Only interferograms / coherence / wrapped are supported.'; sys.exit(1) dateList = ptime.ifgram_date_list(igramFile) dateList6 = ptime.yymmdd(dateList) pairs = [] igramList=h5file[k[0]].keys() for igram in igramList: date12 = h5file[k[0]][igram].attrs['DATE12'].split('-') pairs.append([dateList6.index(date12[0]),dateList6.index(date12[1])]) h5file.close() pairs = pair_sort(pairs) return pairs
def main(argv): inps = cmdLineParse() if not inps.disp_fig: plt.switch_backend('Agg') if inps.template_file: inps = read_template2inps(inps.template_file, inps) ##### 1. Read Info # Read dateList and bperpList ext = os.path.splitext(inps.file)[1] if ext in ['.h5']: atr = readfile.read_attribute(inps.file) k = atr['FILE_TYPE'] print 'reading date and perpendicular baseline from ' + k + ' file: ' + os.path.basename( inps.file) if not k in multi_group_hdf5_file: raise ValueError('only the following file type are supported:\n' + str(multi_group_hdf5_file)) if not inps.coherence_file and k == 'coherence': inps.coherence_file = inps.file pbase_list = ut.perp_baseline_ifgram2timeseries(inps.file)[0] date8_list = ptime.ifgram_date_list(inps.file) else: print 'reading date and perpendicular baseline from baseline list file: ' + inps.bl_list_file date8_list, pbase_list = pnet.read_baseline_file( inps.bl_list_file)[0:2] print 'number of acquisitions : ' + str(len(date8_list)) # Read Pairs Info print 'reading pairs info from file: ' + inps.file date12_list = pnet.get_date12_list(inps.file) print 'number of interferograms: ' + str(len(date12_list)) # Read drop_ifgram date8_list_drop = [] date12_list_drop = [] if ext in ['.h5', '.he5']: h5 = h5py.File(inps.file, 'r') ifgram_list_all = sorted(h5[k].keys()) ifgram_list_keep = ut.check_drop_ifgram(h5) date12_list_keep = ptime.list_ifgram2date12(ifgram_list_keep) # Get date12_list_drop date12_list_drop = sorted( list(set(date12_list) - set(date12_list_keep))) print 'number of interferograms marked as dropped: ' + str( len(date12_list_drop)) print 'number of interferograms marked as kept : ' + str( len(date12_list_keep)) # Get date_list_drop m_dates = [i.split('-')[0] for i in date12_list_keep] s_dates = [i.split('-')[1] for i in date12_list_keep] date8_list_keep = ptime.yyyymmdd(sorted(list(set(m_dates + s_dates)))) date8_list_drop = sorted(list(set(date8_list) - set(date8_list_keep))) print 'number of acquisitions marked as dropped: ' + str( len(date8_list_drop)) # Read Coherence List inps.coherence_list = None if inps.coherence_file and os.path.isfile(inps.coherence_file): if inps.mask_file and not os.path.isfile(inps.mask_file): inps.mask_file = None inps.coherence_list, inps.coh_date12_list = ut.spatial_average(inps.coherence_file, inps.mask_file, \ saveList=True, checkAoi=False) if all(np.isnan(inps.coherence_list)): print 'WARNING: all coherence value are nan! Do not use this and continue.' inps.coherence_list = None # Check subset of date12 info between input file and coherence file if not set(inps.coh_date12_list) >= set(date12_list): print 'WARNING: not every pair/date12 from input file is in coherence file' print 'turn off the color plotting of interferograms based on coherence' inps.coherence_list = None elif set(inps.coh_date12_list) > set(date12_list): print 'extract coherence value for all pair/date12 in input file' inps.coherence_list = [ inps.coherence_list[inps.coh_date12_list.index(i)] for i in date12_list ] #inps.coh_thres = 0.7 ##### 2. Plot inps.cbar_label = 'Average spatial coherence' # Fig 1 - Baseline History fig = plt.figure() ax = fig.add_subplot(111) ax = pnet.plot_perp_baseline_hist(ax, date8_list, pbase_list, vars(inps), date8_list_drop) figName = 'BperpHistory' + inps.fig_ext if inps.save_fig: fig.savefig(figName, bbox_inches='tight') print 'save figure to ' + figName # Fig 2 - Coherence Matrix if inps.coherence_list: figName = 'CoherenceMatrix' + inps.fig_ext if inps.fig_size: fig = plt.figure(figsize=inps.fig_size) else: fig = plt.figure() ax = fig.add_subplot(111) ax = pnet.plot_coherence_matrix(ax, date12_list, inps.coherence_list,\ date12_list_drop, plot_dict=vars(inps)) if inps.save_fig: fig.savefig(figName, bbox_inches='tight', dpi=150) print 'save figure to ' + figName # Fig 3 - Min/Max Coherence History if inps.coherence_list: figName = 'CoherenceHistory' + inps.fig_ext fig = plt.figure() ax = fig.add_subplot(111) ax = pnet.plot_coherence_history(ax, date12_list, inps.coherence_list) if inps.save_fig: fig.savefig(figName, bbox_inches='tight') print 'save figure to ' + figName # Fig 4 - Interferogram Network if inps.fig_size: fig = plt.figure(figsize=inps.fig_size) else: fig = plt.figure() ax = fig.add_subplot(111) ax = pnet.plot_network(ax, date12_list, date8_list, pbase_list, vars(inps), date12_list_drop) figName = 'Network' + inps.fig_ext if inps.save_fig: fig.savefig(figName, bbox_inches='tight') print 'save figure to ' + figName if inps.save_list: txtFile = os.path.splitext(inps.file)[0] + '_date12_list.txt' np.savetxt(txtFile, date12_list, fmt='%s') print 'save pairs/date12 info to file: ' + txtFile if inps.disp_fig: plt.show()
def main(argv): inps = cmdLineParse() if not inps.disp_fig: plt.switch_backend('Agg') #print '\n******************** Plot Network **********************' ##### 1. Read Info # Read dateList and bperpList ext = os.path.splitext(inps.file)[1] if ext in ['.h5']: atr = readfile.read_attribute(inps.file) k = atr['FILE_TYPE'] print 'reading date and perpendicular baseline from '+k+' file: '+os.path.basename(inps.file) if not k in multi_group_hdf5_file: raise ValueError('only the following file type are supported:\n'+str(multi_group_hdf5_file)) pbase_list = ut.perp_baseline_ifgram2timeseries(inps.file)[0] date8_list = ptime.ifgram_date_list(inps.file) else: print 'reading date and perpendicular baseline from baseline list file: '+inps.bl_list_file date8_list, pbase_list = pnet.read_baseline_file(inps.bl_list_file)[0:2] print 'number of acquisitions : '+str(len(date8_list)) # Read Pairs Info print 'reading pairs info from file: '+inps.file date12_list = pnet.get_date12_list(inps.file) print 'number of interferograms: '+str(len(date12_list)) # Read drop_ifgram date8_list_drop = [] date12_list_drop = [] if ext in ['.h5','.he5']: h5 = h5py.File(inps.file, 'r') ifgram_list_all = sorted(h5[k].keys()) ifgram_list_keep = ut.check_drop_ifgram(h5, atr, ifgram_list_all) date12_list_keep = ptime.list_ifgram2date12(ifgram_list_keep) # Get date12_list_drop date12_list_drop = sorted(list(set(date12_list) - set(date12_list_keep))) print 'number of interferograms marked as dropped: '+str(len(date12_list_drop)) # Get date_list_drop m_dates = [i.split('-')[0] for i in date12_list_keep] s_dates = [i.split('-')[1] for i in date12_list_keep] date8_list_keep = ptime.yyyymmdd(sorted(list(set(m_dates + s_dates)))) date8_list_drop = sorted(list(set(date8_list) - set(date8_list_keep))) print 'number of acquisitions marked as dropped: '+str(len(date8_list_drop)) # Read Coherence List inps.coherence_list = None if inps.coherence_file and os.path.isfile(inps.coherence_file): ext = os.path.splitext(inps.coherence_file)[1] if ext in ['.h5']: listFile = os.path.splitext(inps.coherence_file)[0]+'_spatialAverage.txt' if os.path.isfile(listFile): print 'reading coherence value from existed '+listFile fcoh = np.loadtxt(listFile, dtype=str) inps.coherence_list = [float(i) for i in fcoh[:,1]] inps.coh_date12_list = [i for i in fcoh[:,0]] else: print 'calculating average coherence value from '+inps.coherence_file if inps.mask_file: mask = readfile.read(inps.mask_file)[0] else: mask = None inps.coherence_list = ut.spatial_average(inps.coherence_file, mask, saveList=True) inps.coh_date12_list = pnet.get_date12_list(inps.coherence_file) else: print 'reading coherence value from '+inps.coherence_file fcoh = np.loadtxt(inps.coherence_file, dtype=str) inps.coherence_list = [float(i) for i in fcoh[:,1]] inps.coh_date12_list = [i for i in fcoh[:,0]] # Check length of coherence file and input file if not set(inps.coh_date12_list) == set(date12_list): print 'WARNING: input coherence list has different pairs/date12 from input file' print 'turn off the color plotting of interferograms based on coherence' inps.coherence_list = None #inps.coh_thres = 0.7 ##### 2. Plot # Fig 1 - Baseline History fig = plt.figure() ax = fig.add_subplot(111) ax = pnet.plot_perp_baseline_hist(ax, date8_list, pbase_list, vars(inps), date8_list_drop) figName = 'BperpHistory'+inps.fig_ext if inps.save_fig: fig.savefig(figName,bbox_inches='tight') print 'save figure to '+figName # Fig 2 - Coherence Matrix if inps.coherence_list: figName = 'CoherenceMatrix'+inps.fig_ext if inps.fig_size: fig = plt.figure(figsize=inps.fig_size) else: fig = plt.figure() ax = fig.add_subplot(111) ax = pnet.plot_coherence_matrix(ax, date12_list, inps.coherence_list) if inps.save_fig: fig.savefig(figName, bbox_inches='tight') print 'save figure to '+figName # Fig 3 - Min/Max Coherence History if inps.coherence_list: figName = 'CoherenceHistory'+inps.fig_ext fig = plt.figure() ax = fig.add_subplot(111) ax = pnet.plot_coherence_history(ax, date12_list, inps.coherence_list) if inps.save_fig: fig.savefig(figName, bbox_inches='tight') print 'save figure to '+figName # Fig 4 - Interferogram Network if inps.fig_size: fig = plt.figure(figsize=inps.fig_size) else: fig = plt.figure() ax = fig.add_subplot(111) ax = pnet.plot_network(ax, date12_list, date8_list, pbase_list, vars(inps), date12_list_drop) figName = 'Network'+inps.fig_ext if inps.save_fig: fig.savefig(figName,bbox_inches='tight') print 'save figure to '+figName if inps.save_list: txtFile = os.path.splitext(inps.file)[0]+'_date12_list.txt' np.savetxt(txtFile, date12_list, fmt='%s') print 'save pairs/date12 info to file: '+txtFile if inps.disp_fig: plt.show()
def main(argv): ##### Read Inputs inps = cmdLineParse() inps.file = ut.get_file_list(inps.file) date12_orig = pnet.get_date12_list(inps.file[0]) print 'input file(s) to be modified: ' + str(inps.file) print 'number of interferograms: ' + str(len(date12_orig)) atr = readfile.read_attribute(inps.file[0]) # Update inps if template is input if inps.template_file: inps = read_template2inps(inps.template_file, inps) if all(not i for i in [inps.reference_file, inps.max_temp_baseline, inps.max_perp_baseline,\ inps.exclude_ifg_index, inps.exclude_date, inps.coherence_based,\ inps.start_date, inps.end_date, inps.reset]): # Display network for manually modification when there is no other modification input. print 'No input option found to remove interferogram' if inps.template_file: print 'Keep all interferograms by enable --reset option' inps.reset = True else: print 'To manually modify network, please use --manual option ' return if inps.reset: print '----------------------------------------------------------------------------' for file in inps.file: reset_pairs(file) mean_coh_txt_file = os.path.splitext( inps.coherence_file)[0] + '_spatialAverage.txt' if os.path.isfile(mean_coh_txt_file): rmCmd = 'rm ' + mean_coh_txt_file print rmCmd os.system(rmCmd) return # Convert index : input to continous index list if inps.exclude_ifg_index: ifg_index = list(inps.exclude_ifg_index) inps.exclude_ifg_index = [] for index in ifg_index: index_temp = [int(i) for i in index.split(':')] index_temp.sort() if len(index_temp) == 2: for j in range(index_temp[0], index_temp[1] + 1): inps.exclude_ifg_index.append(j) elif len(index_temp) == 1: inps.exclude_ifg_index.append(int(index)) else: print 'Unrecoganized input: ' + index inps.exclude_ifg_index = sorted(inps.exclude_ifg_index) if max(inps.exclude_ifg_index) > len(date12_orig): raise Exception('Input index out of range!\n'+\ 'input index:'+str(inps.exclude_ifg_index)+'\n'+\ 'index range of file: '+str(len(date12_orig))) ##### Get date12_to_rmv date12_to_rmv = [] # 1. Update date12_to_rmv from reference file if inps.reference_file: date12_to_keep = pnet.get_date12_list(inps.reference_file, check_drop_ifgram=True) print '----------------------------------------------------------------------------' print 'use reference pairs info from file: ' + inps.reference_file print 'number of interferograms in reference: ' + str( len(date12_to_keep)) print 'date12 not in reference file:' date12_to_rmv_temp = [] for date12 in date12_orig: if date12 not in date12_to_keep: date12_to_rmv.append(date12) date12_to_rmv_temp.append(date12) print date12_to_rmv_temp # 2.1 Update date12_to_rmv from coherence file if inps.coherence_based and os.path.isfile(inps.coherence_file): print '----------------------------------------------------------------------------' print 'use coherence-based network modification from coherence file: ' + inps.coherence_file # check mask AOI in lalo if inps.aoi_geo_box and inps.lookup_file: print 'input AOI in (lon0, lat1, lon1, lat0): ' + str( inps.aoi_geo_box) inps.aoi_pix_box = subset.bbox_geo2radar(inps.aoi_geo_box, atr, inps.lookup_file) if inps.aoi_pix_box: # check mask AOI within the data coverage inps.aoi_pix_box = subset.check_box_within_data_coverage( inps.aoi_pix_box, atr) print 'input AOI in (x0,y0,x1,y1): ' + str(inps.aoi_pix_box) # Calculate spatial average coherence coh_list, coh_date12_list = ut.spatial_average(inps.coherence_file, inps.mask_file,\ inps.aoi_pix_box, saveList=True) # MST network if inps.keep_mst: print 'Get minimum spanning tree (MST) of interferograms with inverse of coherence.' print 'date12 with 1) average coherence < ' + str( inps.min_coherence) + ' AND 2) not in MST network: ' mst_date12_list = pnet.threshold_coherence_based_mst( coh_date12_list, coh_list) else: print 'date12 with average coherence < ' + str(inps.min_coherence) mst_date12_list = [] date12_to_rmv_temp = [] for i in range(len(coh_date12_list)): date12 = coh_date12_list[i] if coh_list[ i] < inps.min_coherence and date12 not in mst_date12_list: date12_to_rmv.append(date12) date12_to_rmv_temp.append(date12) print date12_to_rmv_temp # 2.2 Update date12_to_rmv from temp baseline threshold if inps.max_temp_baseline: print '----------------------------------------------------------------------------' print 'Drop pairs with temporal baseline > ' + str( inps.max_temp_baseline) + ' days' date8_list = ptime.ifgram_date_list(inps.file[0]) date6_list = ptime.yymmdd(date8_list) tbase_list = ptime.date_list2tbase(date8_list)[0] date12_to_rmv_temp = [] for i in range(len(date12_orig)): date1, date2 = date12_orig[i].split('-') idx1 = date6_list.index(date1) idx2 = date6_list.index(date2) t_diff = tbase_list[idx2] - tbase_list[idx1] if t_diff > inps.max_temp_baseline: date12 = date12_orig[i] date12_to_rmv.append(date12) date12_to_rmv_temp.append(date12) print 'number of pairs to drop: %d' % (len(date12_to_rmv_temp)) print date12_to_rmv_temp # 2.3 Update date12_to_rmv from perp baseline threshold if inps.max_perp_baseline: print '----------------------------------------------------------------------------' print 'Drop pairs with perpendicular spatial baseline > ' + str( inps.max_perp_baseline) + ' meters' ifg_bperp_list = pnet.igram_perp_baseline_list(inps.file[0]) date12_to_rmv_temp = [] for i in range(len(ifg_bperp_list)): if abs(ifg_bperp_list[i]) > inps.max_perp_baseline: date12 = date12_orig[i] date12_to_rmv.append(date12) date12_to_rmv_temp.append(date12) print 'number of pairs to drop: %d' % (len(date12_to_rmv_temp)) print date12_to_rmv_temp # 2.4 Update date12_to_rmv from exclude_ifg_index if inps.exclude_ifg_index: print '----------------------------------------------------------------------------' print 'drop date12/pair with the following index number:' for index in inps.exclude_ifg_index: date12 = date12_orig[index - 1] date12_to_rmv.append(date12) print str(index) + ' ' + date12 # 2.5 Update date12_to_rmv from exclude_date if inps.exclude_date: inps.exclude_date = ptime.yymmdd(inps.exclude_date) print '----------------------------------------------------------------------------' print 'Drop pairs including the following dates: \n' + str( inps.exclude_date) date12_to_rmv_temp = [] for i in range(len(date12_orig)): date1, date2 = date12_orig[i].split('-') if (date1 in inps.exclude_date) or (date2 in inps.exclude_date): date12 = date12_orig[i] date12_to_rmv.append(date12) date12_to_rmv_temp.append(date12) print date12_to_rmv_temp # 2.6 Update date12_to_rmv from start_date if inps.start_date: inps.start_date = ptime.yymmdd(inps.start_date) print '----------------------------------------------------------------------------' print 'Drop pairs with date earlier than start-date: ' + inps.start_date min_date = int(ptime.yyyymmdd(inps.start_date)) date12_to_rmv_temp = [] for i in range(len(date12_orig)): date12 = date12_orig[i] if any( int(j) < min_date for j in ptime.yyyymmdd(date12.split('-'))): date12_to_rmv.append(date12) date12_to_rmv_temp.append(date12) print date12_to_rmv_temp # 2.7 Update date12_to_rmv from end_date if inps.end_date: inps.end_date = ptime.yymmdd(inps.end_date) print '----------------------------------------------------------------------------' print 'Drop pairs with date earlier than end-date: ' + inps.end_date max_date = int(ptime.yyyymmdd(inps.end_date)) date12_to_rmv_temp = [] for i in range(len(date12_orig)): date12 = date12_orig[i] if any( int(j) > max_date for j in ptime.yyyymmdd(date12.split('-'))): date12_to_rmv.append(date12) date12_to_rmv_temp.append(date12) print date12_to_rmv_temp # 3. Manually drop pairs if inps.disp_network: date12_click = manual_select_pairs_to_remove(inps.file[0]) for date12 in list(date12_click): if date12 not in date12_orig: date12_click.remove(date12) print 'date12 selected to remove:' print date12_click date12_to_rmv += date12_click # 4. drop duplicate date12 and sort in order date12_to_rmv = sorted(list(set(date12_to_rmv))) date12_keep = sorted(list(set(date12_orig) - set(date12_to_rmv))) print '----------------------------------------------------------------------------' print 'number of interferograms to remove: ' + str(len(date12_to_rmv)) print 'number of interferograms kept : ' + str(len(date12_keep)) ##### Calculated date12_to_drop v.s. existing date12_to_drop # Get list of date12 of interferograms already been marked to drop k = readfile.read_attribute(inps.file[0])['FILE_TYPE'] h5 = h5py.File(inps.file[0], 'r') ifgram_list_all = sorted(h5[k].keys()) ifgram_list_keep = ut.check_drop_ifgram(h5, print_msg=False) ifgram_list_dropped = sorted( list(set(ifgram_list_all) - set(ifgram_list_keep))) date12_list_dropped = ptime.list_ifgram2date12(ifgram_list_dropped) h5.close() if date12_to_rmv == date12_list_dropped and inps.mark_attribute: print 'Calculated date12 to drop is the same as exsiting marked input file, skip update file attributes.' date12_to_rmv = [] ##### Update date12 to drop if date12_to_rmv: ##### Update Input Files with date12_to_rmv Modified_CoherenceFile = 'Modified_coherence.h5' for File in inps.file: Modified_File = modify_file_date12_list(File, date12_to_rmv, inps.mark_attribute) k = readfile.read_attribute(File)['FILE_TYPE'] # Update Mask File if k == 'interferograms' and inps.update_aux: print 'update mask file for input ' + k + ' file based on ' + Modified_File inps.mask_file = 'mask.h5' print 'writing >>> ' + inps.mask_file ut.nonzero_mask(Modified_File, inps.mask_file) elif k == 'coherence' and inps.update_aux: inps.coherence_file = Modified_File print 'update average spatial coherence for input ' + k + ' file based on: ' + Modified_File outFile = 'averageSpatialCoherence.h5' print 'writing >>> ' + outFile ut.temporal_average(Modified_File, outFile) # Touch spatial average txt file of coherence if it's existed coh_spatialAverage_file = os.path.splitext( Modified_File)[0] + '_spatialAverage.txt' if os.path.isfile(coh_spatialAverage_file): touchCmd = 'touch ' + coh_spatialAverage_file print touchCmd os.system(touchCmd) # Plot result if inps.plot: print '\nplot modified network and save to file.' plotCmd = 'plot_network.py ' + inps.coherence_file + ' --coherence ' + inps.coherence_file + ' --nodisplay' if inps.template_file: plotCmd += ' --template ' + inps.template_file print plotCmd os.system(plotCmd) print 'Done.' return