def main(argv): inps = cmdLineParse() print('\n*************** Spatial Average ******************') for File in inps.file: mean_list, date_list = ut.spatial_average(File, inps.mask_file, saveList=True) atr = readfile.read_attribute(File) k = atr['FILE_TYPE'] if inps.disp_fig and k == 'timeseries': dates, datevector = ptime.date_list2vector(date_list) # plot fig = plt.figure() ax = fig.add_subplot(111) ax.plot(dates, mean_list, '-ko', lw=2, ms=16, alpha=0.7, mfc='crimson') ax.set_title('Spatial Average', fontsize=12) ax = ptime.auto_adjust_xaxis_date(ax, datevector)[0] ax.set_xlabel('Time [years]', fontsize=12) ax.set_ylabel('Mean', fontsize=12) plt.show()
def exclude_dates(): global inps, dateList if inps.ex_date_list: input_ex_date = list(inps.ex_date_list) inps.ex_date_list = [] if input_ex_date: for ex_date in input_ex_date: if os.path.isfile(ex_date): ex_date = ptime.read_date_list(ex_date) else: ex_date = [ptime.yyyymmdd(ex_date)] inps.ex_date_list += list( set(ex_date) - set(inps.ex_date_list)) # delete dates not existed in input file inps.ex_date_list = sorted( list(set(inps.ex_date_list).intersection(dateList))) inps.ex_dates = ptime.date_list2vector(inps.ex_date_list)[0] inps.ex_idx_list = sorted( [dateList.index(i) for i in inps.ex_date_list]) print('exclude date:' + str(inps.ex_date_list))
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_timeseries_info(): global atr, k, h5, dateList, tims, date_num, inps atr = readfile.read_attribute(inps.timeseries_file) k = atr['FILE_TYPE'] print('input file is ' + k + ': ' + inps.timeseries_file) if not k == 'timeseries': raise ValueError('Only timeseries file is supported!') h5 = h5py.File(inps.timeseries_file, 'r') dateList = sorted(h5[k].keys()) date_num = len(dateList) inps.dates, tims = ptime.date_list2vector(dateList)
def plot_coherence_history(ax, date12_list, coherence_list, plot_dict={}): '''Plot min/max Coherence of all interferograms for each date''' # Figure Setting keyList = list(plot_dict.keys()) if not 'fontsize' in keyList: plot_dict['fontsize'] = 12 if not 'linewidth' in keyList: plot_dict['linewidth'] = 2 if not 'markercolor' in keyList: plot_dict['markercolor'] = 'orange' if not 'markersize' in keyList: plot_dict['markersize'] = 16 if not 'disp_title' in keyList: plot_dict['disp_title'] = True # Get date list m_dates = [date12.split('-')[0] for date12 in date12_list] s_dates = [date12.split('-')[1] for date12 in date12_list] date8_list = sorted(ptime.yyyymmdd(list(set(m_dates + s_dates)))) dates, datevector = ptime.date_list2vector(date8_list) bar_width = mode(np.diff(dates).tolist()) * 3 / 4 x_list = [i - bar_width / 2 for i in dates] coh_mat = coherence_matrix(date12_list, coherence_list) ax.bar(x_list, np.nanmax(coh_mat, axis=0), bar_width.days, label='Max Coherence') ax.bar(x_list, np.nanmin(coh_mat, axis=0), bar_width.days, label='Min Coherence') if plot_dict['disp_title']: ax.set_title('Coherence History of All Related Interferograms') ax = ptime.auto_adjust_xaxis_date(ax, datevector, plot_dict['fontsize'])[0] ax.set_ylim([0.0, 1.0]) ax.set_xlabel('Time [years]', fontsize=plot_dict['fontsize']) ax.set_ylabel('Coherence', fontsize=plot_dict['fontsize']) ax.legend(loc='lower right') return ax
def main(argv): inps = cmdLineParse() if inps.template_file: inps = read_template2inps(inps.template_file) ##### calculate timeseries of residual Root Mean Square #std_list, date_list = ut.get_residual_std(inps.timeseries_file, inps.mask_file, inps.ramp_type) rms_list, date_list = ut.get_residual_rms(inps.timeseries_file, inps.mask_file, inps.ramp_type) ##### reference_date.txt print('------------------------------------------------------------') ref_idx = np.argmin(rms_list) ref_date = date_list[ref_idx] print('date with minimum residual RMS: %s - %.4f' % (ref_date, rms_list[ref_idx])) refTxtFile = 'reference_date.txt' if (inps.save_reference_date and \ ut.update_file(refTxtFile, [inps.timeseries_file, inps.mask_file, inps.template_file], check_readable=False)): f = open(refTxtFile, 'w') f.write(ref_date + '\n') f.close() print('save date to file: ' + refTxtFile) ##### exclude_date.txt print('------------------------------------------------------------') ex_idx_list = [rms_list.index(i) for i in rms_list if i > inps.min_rms] print('date(s) with residual RMS > ' + str(inps.min_rms)) exTxtFile = 'exclude_date.txt' if ex_idx_list: if (inps.save_exclude_date and \ ut.update_file(exTxtFile, [inps.timeseries_file, inps.mask_file, inps.template_file], check_readable=False)): f = open(exTxtFile, 'w') for i in ex_idx_list: print('%s - %.4f' % (date_list[i], rms_list[i])) f.write(date_list[i] + '\n') f.close() print('save date(s) to file: ' + exTxtFile) else: print('None.') ##### Plot if inps.ramp_type != 'no': fig_name = os.path.splitext( inps.timeseries_file)[0] + '_' + inps.ramp_type + '_rms.pdf' else: fig_name = os.path.splitext(inps.timeseries_file)[0] + '_rms.pdf' if ut.update_file(fig_name, [exTxtFile, refTxtFile, inps.template_file], check_readable=False): fig = plt.figure() ax = fig.add_subplot(111) font_size = 12 dates, datevector = ptime.date_list2vector(date_list) bar_width = ut.mode(np.diff(dates).tolist()) * 3 / 4 x_list = [i - bar_width / 2 for i in dates] # Plot all dates ax.bar(x_list, rms_list, bar_width.days) #ax.bar(x_list, rms_list, bar_width.days) # Plot reference date if inps.save_reference_date: ax.bar(x_list[ref_idx], rms_list[ref_idx], bar_width.days, label='Reference date') # Plot exclude dates if ex_idx_list and inps.save_exclude_date: ex_x_list = [x_list[i] for i in ex_idx_list] ex_rms_list = [rms_list[i] for i in ex_idx_list] ax.bar(ex_x_list, ex_rms_list, bar_width.days, color='darkgray', label='Exclude date(s)') # Plot min_rms line ax, xmin, xmax = ptime.auto_adjust_xaxis_date(ax, datevector, font_size) ax.plot(np.array([xmin, xmax]), np.array([inps.min_rms, inps.min_rms]), '-') # axis format ax = pnet.auto_adjust_yaxis(ax, rms_list + [inps.min_rms], font_size, ymin=0.0) ax.set_xlabel('Time [years]', fontsize=font_size) ax.set_ylabel('Root Mean Square (m)', fontsize=font_size) ax.yaxis.set_ticks_position('both') if inps.save_reference_date or inps.save_exclude_date: plt.legend() # save figure fig.savefig(fig_name, bbox_inches='tight') print('save figure to file: ' + fig_name) return
def plot_network(ax, date12_list, date_list, pbase_list, plot_dict={}, date12_list_drop=[]): '''Plot Temporal-Perp baseline Network Inputs ax : matplotlib axes object date12_list : list of string for date12 in YYMMDD-YYMMDD format date_list : list of string, for date in YYYYMMDD/YYMMDD format pbase_list : list of float, perp baseline, len=number of acquisition plot_dict : dictionary with the following items: fontsize linewidth markercolor markersize coherence_list : list of float, coherence value of each interferogram, len = number of ifgrams disp_min/max : float, min/max range of the color display based on coherence_list colormap : string, colormap name coh_thres : float, coherence of where to cut the colormap for display disp_title : bool, show figure title or not, default: True disp_drop: bool, show dropped interferograms or not, default: True Output ax : matplotlib axes object ''' # Figure Setting keyList = list(plot_dict.keys()) if not 'fontsize' in keyList: plot_dict['fontsize'] = 12 if not 'linewidth' in keyList: plot_dict['linewidth'] = 2 if not 'markercolor' in keyList: plot_dict['markercolor'] = 'orange' if not 'markersize' in keyList: plot_dict['markersize'] = 16 # For colorful display of coherence if not 'coherence_list' in keyList: plot_dict['coherence_list'] = None if not 'disp_min' in keyList: plot_dict['disp_min'] = 0.2 if not 'disp_max' in keyList: plot_dict['disp_max'] = 1.0 if not 'colormap' in keyList: plot_dict['colormap'] = 'RdBu' if not 'disp_title' in keyList: plot_dict['disp_title'] = True if not 'coh_thres' in keyList: plot_dict['coh_thres'] = None if not 'disp_drop' in keyList: plot_dict['disp_drop'] = True coh_list = plot_dict['coherence_list'] disp_min = plot_dict['disp_min'] disp_max = plot_dict['disp_max'] coh_thres = plot_dict['coh_thres'] transparency = 0.7 # Date Convert date8_list = ptime.yyyymmdd(sorted(date_list)) date6_list = ptime.yymmdd(date8_list) dates, datevector = ptime.date_list2vector(date8_list) ## Keep/Drop - date12 date12_list_keep = sorted(list(set(date12_list) - set(date12_list_drop))) idx_date12_keep = [date12_list.index(i) for i in date12_list_keep] idx_date12_drop = [date12_list.index(i) for i in date12_list_drop] ## Keep/Drop - date 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))) idx_date_keep = [date8_list.index(i) for i in date8_list_keep] idx_date_drop = [date8_list.index(i) for i in date8_list_drop] # Ploting #ax=fig.add_subplot(111) ## Colorbar when conherence is colored if coh_list: data_min = min(coh_list) data_max = max(coh_list) # Normalize normalization = False if normalization: coh_list = [(coh - data_min) / (data_min - data_min) for coh in coh_list] disp_min = data_min disp_max = data_max print('showing coherence') print('colormap: ' + plot_dict['colormap']) print('display range: ' + str([disp_min, disp_max])) print('data range: ' + str([data_min, data_max])) # Use lower/upper part of colormap to emphasis dropped interferograms if not coh_thres: # Find proper cut percentage so that all keep pairs are blue and drop pairs are red coh_list_keep = [coh_list[i] for i in idx_date12_keep] coh_list_drop = [coh_list[i] for i in idx_date12_drop] if coh_list_drop: coh_thres = max(coh_list_drop) else: coh_thres = min(coh_list_keep) c1_num = np.ceil(200.0 * (coh_thres - disp_min) / (disp_max - disp_min)).astype('int') coh_thres = c1_num / 200.0 * (disp_max - disp_min) + disp_min cmap = plt.get_cmap(plot_dict['colormap']) colors1 = cmap(np.linspace(0.0, 0.3, c1_num)) colors2 = cmap(np.linspace(0.6, 1.0, 200 - c1_num)) cmap = colors.LinearSegmentedColormap.from_list( 'truncate_RdBu', np.vstack((colors1, colors2))) print('color jump at ' + str(coh_thres)) divider = make_axes_locatable(ax) cax = divider.append_axes("right", "5%", pad="3%") norm = mpl.colors.Normalize(vmin=disp_min, vmax=disp_max) cbar = mpl.colorbar.ColorbarBase(cax, cmap=cmap, norm=norm) cbar.set_label('Average Spatial Coherence', fontsize=plot_dict['fontsize']) ## Dot - SAR Acquisition if idx_date_keep: x_list = [dates[i] for i in idx_date_keep] y_list = [pbase_list[i] for i in idx_date_keep] ax.plot(x_list, y_list, 'ko', alpha=0.7, ms=plot_dict['markersize'], mfc=plot_dict['markercolor']) if idx_date_drop: x_list = [dates[i] for i in idx_date_drop] y_list = [pbase_list[i] for i in idx_date_drop] ax.plot(x_list, y_list, 'ko', alpha=0.7, ms=plot_dict['markersize'], mfc='gray') ## Line - Pair/Interferogram # interferograms kept for date12 in date12_list_keep: date1, date2 = date12.split('-') idx1 = date6_list.index(date1) idx2 = date6_list.index(date2) x = np.array([dates[idx1], dates[idx2]]) y = np.array([pbase_list[idx1], pbase_list[idx2]]) if coh_list: coh = coh_list[date12_list.index(date12)] coh_idx = (coh - disp_min) / (disp_max - disp_min) ax.plot(x, y, '-', lw=plot_dict['linewidth'], alpha=transparency, c=cmap(coh_idx)) else: ax.plot(x, y, '-', lw=plot_dict['linewidth'], alpha=transparency, c='k') # interferograms dropped if plot_dict['disp_drop']: for date12 in date12_list_drop: date1, date2 = date12.split('-') idx1 = date6_list.index(date1) idx2 = date6_list.index(date2) x = np.array([dates[idx1], dates[idx2]]) y = np.array([pbase_list[idx1], pbase_list[idx2]]) if coh_list: coh = coh_list[date12_list.index(date12)] coh_idx = (coh - disp_min) / (disp_max - disp_min) ax.plot(x, y, '--', lw=plot_dict['linewidth'], alpha=transparency, c=cmap(coh_idx)) else: ax.plot(x, y, '--', lw=plot_dict['linewidth'], alpha=transparency, c='k') if plot_dict['disp_title']: ax.set_title('Interferogram Network', fontsize=plot_dict['fontsize']) # axis format ax = ptime.auto_adjust_xaxis_date(ax, datevector, plot_dict['fontsize'])[0] ax = auto_adjust_yaxis(ax, pbase_list, plot_dict['fontsize']) ax.set_xlabel('Time [years]', fontsize=plot_dict['fontsize']) ax.set_ylabel('Perp Baseline [m]', fontsize=plot_dict['fontsize']) # Legend solid_line = mlines.Line2D([], [], color='k', ls='solid', label='Interferograms') dash_line = mlines.Line2D([], [], color='k', ls='dashed', label='Interferograms dropped') ax.legend(handles=[solid_line, dash_line]) return ax
def plot_perp_baseline_hist(ax, date8_list, pbase_list, plot_dict={}, date8_list_drop=[]): ''' Plot Perpendicular Spatial Baseline History Inputs ax : matplotlib axes object date8_list : list of string, date in YYYYMMDD format pbase_list : list of float, perp baseline plot_dict : dictionary with the following items: fontsize linewidth markercolor markersize disp_title : bool, show figure title or not, default: True date8_list_drop : list of string, date dropped in YYYYMMDD format e.g. ['20080711', '20081011'] Output: ax : matplotlib axes object ''' # Figure Setting keyList = list(plot_dict.keys()) if not 'fontsize' in keyList: plot_dict['fontsize'] = 12 if not 'linewidth' in keyList: plot_dict['linewidth'] = 2 if not 'markercolor' in keyList: plot_dict['markercolor'] = 'orange' if not 'markersize' in keyList: plot_dict['markersize'] = 16 if not 'disp_title' in keyList: plot_dict['disp_title'] = True transparency = 0.7 # Date Convert dates, datevector = ptime.date_list2vector(date8_list) # Get index of date used and dropped #date8_list_drop = ['20080711', '20081011'] # for debug idx_keep = list(range(len(date8_list))) idx_drop = [] for i in date8_list_drop: idx = date8_list.index(i) idx_keep.remove(idx) idx_drop.append(idx) # Plot #ax=fig.add_subplot(111) # Plot date used if idx_keep: x_list = [dates[i] for i in idx_keep] y_list = [pbase_list[i] for i in idx_keep] ax.plot(x_list, y_list, '-ko', alpha=transparency, lw=plot_dict['linewidth'], \ ms=plot_dict['markersize'], mfc=plot_dict['markercolor']) # Plot date dropped if idx_drop: x_list = [dates[i] for i in idx_drop] y_list = [pbase_list[i] for i in idx_drop] ax.plot(x_list, y_list, 'ko', alpha=transparency, ms=plot_dict['markersize'], mfc='gray') if plot_dict['disp_title']: ax.set_title('Perpendicular Baseline History', fontsize=plot_dict['fontsize']) # axis format ax = ptime.auto_adjust_xaxis_date(ax, datevector, plot_dict['fontsize'])[0] ax = auto_adjust_yaxis(ax, pbase_list, plot_dict['fontsize']) ax.set_xlabel('Time [years]', fontsize=plot_dict['fontsize']) ax.set_ylabel('Perpendicular Baseline [m]', fontsize=plot_dict['fontsize']) return ax
def main(argv): inps = cmdLineParse() #print '\n********** Inversion: Time Series to Velocity ***********' atr = readfile.read_attribute(inps.timeseries_file) k = atr['FILE_TYPE'] print('input ' + k + ' file: ' + inps.timeseries_file) if not k == 'timeseries': sys.exit('ERROR: input file is not timeseries!') h5file = h5py.File(inps.timeseries_file) ##################################### ## Date Info dateListAll = sorted(h5file[k].keys()) print('--------------------------------------------') print('Dates from input file: ' + str(len(dateListAll))) print(dateListAll) inps.ex_date = get_exclude_date(inps, dateListAll) dateList = sorted(list(set(dateListAll) - set(inps.ex_date))) print('--------------------------------------------') if len(dateList) == len(dateListAll): print('using all dates to calculate the velocity') else: print('Dates used to estimate the velocity: ' + str(len(dateList))) print(dateList) print('--------------------------------------------') # Date Aux Info dates, datevector = ptime.date_list2vector(dateList) ##################################### ## Inversion # Design matrix B = np.ones([len(datevector), 2]) B[:, 0] = datevector #B_inv = np.linalg.pinv(B) B_inv = np.dot(np.linalg.inv(np.dot(B.T, B)), B.T) B_inv = np.array(B_inv, np.float32) # Loading timeseries print("Loading time series file: " + inps.timeseries_file + ' ...') width = int(atr['WIDTH']) length = int(atr['FILE_LENGTH']) dateNum = len(dateList) timeseries = np.zeros([dateNum, length * width], np.float32) prog_bar = ptime.progress_bar(maxValue=dateNum, prefix='loading: ') for i in range(dateNum): date = dateList[i] timeseries[i, :] = h5file[k].get(date)[:].flatten() prog_bar.update(i + 1, suffix=date) prog_bar.close() h5file.close() # Velocity Inversion print('Calculating velocity ...') X = np.dot(B_inv, timeseries) velocity = np.reshape(X[0, :], [length, width]) print('Calculating rmse ...') timeseries_linear = np.dot(B, X) timeseries_residual = timeseries - timeseries_linear rmse = np.reshape(np.sqrt((np.sum((timeseries_residual)**2, 0)) / dateNum), [length, width]) print('Calculating the standard deviation of the estimated velocity ...') s1 = np.sqrt(np.sum(timeseries_residual**2, 0) / (dateNum - 2)) s2 = np.sqrt(np.sum((datevector - np.mean(datevector))**2)) std = np.reshape(s1 / s2, [length, width]) # SSt=np.sum((timeseries-np.mean(timeseries,0))**2,0) # SSres=np.sum(residual**2,0) # SS_REG=SSt-SSres # Rsquared=np.reshape(SS_REG/SSt,[length,width]) ###################################################### # covariance of the velocities ##################################### # Output file name if not inps.outfile: inps.outfile = 'velocity.h5' inps.outfile_rmse = os.path.splitext( inps.outfile)[0] + 'Rmse' + os.path.splitext(inps.outfile)[1] inps.outfile_std = os.path.splitext( inps.outfile)[0] + 'Std' + os.path.splitext(inps.outfile)[1] inps.outfile_r2 = os.path.splitext( inps.outfile)[0] + 'R2' + os.path.splitext(inps.outfile)[1] # Attributes atr['date1'] = datevector[0] atr['date2'] = datevector[dateNum - 1] # File Writing print('--------------------------------------') atr['FILE_TYPE'] = 'velocity' print('writing >>> ' + inps.outfile) writefile.write(velocity, atr, inps.outfile) #atr['FILE_TYPE'] = 'rmse' print('writing >>> ' + inps.outfile_rmse) writefile.write(rmse, atr, inps.outfile_rmse) #atr['FILE_TYPE'] = 'rmse' print('writing >>> ' + inps.outfile_std) writefile.write(std, atr, inps.outfile_std) print('Done.\n') return inps.outfile