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 plot_coherence_history(ax, date12_list, coherence_list, plot_dict={}): '''Plot min/max Coherence of all interferograms for each date''' # Figure Setting keyList = 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 read_dis_xy(xsub, ysub, dateList, h5file, unit='cm'): global ref_date ## Unit and Scale if unit == 'm': unitFac = 1.0 elif unit == 'mm': unitFac = 1000.0 elif unit == 'km': unitFac = 0.001 else: unit = 'cm' unitFac = 100.0 # cm by default ## read displacement try: ref_date dis_ref = h5file['timeseries'].get(ref_date)[ysub[0]:ysub[1] + 1, xsub[0]:xsub[1] + 1] except: pass dis = [] for date in dateList: dis0 = h5file['timeseries'].get(date)[ysub[0]:ysub[1] + 1, xsub[0]:xsub[1] + 1] try: dis0 -= dis_ref except: pass dis.append(dis0) dis = np.array(dis) * unitFac dis = np.reshape(dis, (len(dateList), -1)) ## calculate mean dis_mean = np.nanmean(dis, 1) ## calculate standard deviation if (xsub[1] - xsub[0]) * (ysub[1] - ysub[0]) == 1: dis_std = np.array([0] * len(dateList)) else: dis_std = np.nanstd(dis, 1) ## display print 'spatial averaged displacement [' + unit + ']:' print dis_mean print 'standard deviation [' + unit + ']:' print dis_std ## calculate linear velocity try: dates, datevector = ptime.date_list2vector(dateList) dis_slope = stats.linregress(np.array(datevector), dis_mean) print 'linear velocity [' + unit + '/yr]:' print str(dis_slope[0]) + '+/-' + str(dis_slope[4]) except: pass try: return dis, dis_mean, dis_std, dis_slope except: return dis, dis_mean, dis_std
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) pairs_idx = pnet.read_igram_pairs(File) bperp_list = ut.Baseline_timeseries(File) date8_list = ptime.igram_date_list(File) ax = pnet.plot_network(ax, pairs_idx, date8_list, bperp_list) print 'display the network of interferogram of file: ' + File date12_orig = pnet.get_date12_list(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_dis(xsub, ysub, dateList, h5file, unit="cm"): global ref_date ## Unit and Scale if unit == "m": unitFac = 1.0 elif unit == "mm": unitFac = 1000.0 elif unit == "km": unitFac = 0.001 else: unit = "cm" unitFac = 100.0 # cm by default ## read displacement try: ref_date dis_ref = h5file["timeseries"].get(ref_date)[ysub[0] : ysub[1] + 1, xsub[0] : xsub[1] + 1] except: pass dis = [] for date in dateList: dis0 = h5file["timeseries"].get(date)[ysub[0] : ysub[1] + 1, xsub[0] : xsub[1] + 1] try: dis0 -= dis_ref except: pass dis.append(dis0) dis = np.array(dis) * unitFac dis = np.reshape(dis, (len(dateList), -1)) ## calculate mean dis_mean = stats.nanmean(dis, 1) ## calculate standard deviation if (xsub[1] - xsub[0]) * (ysub[1] - ysub[0]) == 1: dis_std = np.array([0] * len(dateList)) else: dis_std = stats.nanstd(dis, 1) ## calculate linear velocity dates, datevector = ptime.date_list2vector(dateList) dis_slope = stats.linregress(np.array(datevector), dis_mean) ## display print "spatial averaged displacement [" + unit + "]:" print dis_mean print "standard deviation [" + unit + "]:" print dis_std print "linear velocity [" + unit + "/yr]:" print str(dis_slope[0]) + "+/-" + str(dis_slope[4]) return dis, dis_mean, dis_std, dis_slope
def print_timseries_date_info(dateList): datevector = ptime.date_list2vector(dateList)[1] print '*************** Date Info ***************' print 'Start Date: '+dateList[0] print 'End Date: '+dateList[-1] print 'Number of acquisitions : '+str(len(dateList)) print 'Standard deviation of acquisition times : '+str(std(datevector))+' years' print '----------------------' print 'List of dates:' print dateList print '----------------------' print 'List of dates in years' print datevector return
def print_timseries_date_info(dateList): datevector = ptime.date_list2vector(dateList)[1] print('*************** Date Info ***************') print('Start Date: ' + dateList[0]) print('End Date: ' + dateList[-1]) print('Number of acquisitions : %d' % len(dateList)) print('Std. of acquisition times : %.2f yeras' % std(datevector)) print('----------------------') print('List of dates:') print(dateList) print('----------------------') print('List of dates in years') print(datevector) return
def axis_adjust_date_length(ax, dateList, lengthArray): fontSize = 12 ## Date Convert dates, datevector = ptime.date_list2vector(dateList) ## Date Display years = mdates.YearLocator() # every year months = mdates.MonthLocator() # every month yearsFmt = mdates.DateFormatter("%Y") ## X axis format ax.fmt_xdata = DateFormatter("%Y-%m-%d %H:%M:%S") ts = datevector[0] - 0.2 ys = int(ts) ms = int((ts - ys) * 12) te = datevector[-1] + 0.3 ye = int(te) me = int((te - ye) * 12) if ms > 12: ys = ys + 1 ms = 1 if me > 12: ye = ye + 1 me = 1 if ms < 1: ys = ys - 1 ms = 12 if me < 1: ye = ye - 1 me = 12 dss = datetime.date(ys, ms, 1) dee = datetime.date(ye, me, 1) ax.set_xlim(dss, dee) # using the same xlim with the previous one ax.xaxis.set_major_locator(years) ax.xaxis.set_major_formatter(yearsFmt) ax.xaxis.set_minor_locator(months) ### Y axis format length = max(lengthArray) - min(lengthArray) ax.set_ylim(min(lengthArray) - 0.1 * length, max(lengthArray) + 0.1 * length) xticklabels = getp(gca(), "xticklabels") yticklabels = getp(gca(), "yticklabels") setp(yticklabels, "color", "k", fontsize=fontSize) setp(xticklabels, "color", "k", fontsize=fontSize) return ax
def plot_bperp_hist(fig, dateList, bperp): ## Figure Setting fontSize = 12 markerColor = "orange" markerSize = 16 lineWidth = 2 ## Date Convert dates, datevector = ptime.date_list2vector(dateList) ## Plot ax = fig.add_subplot(111) ax.plot(dates, bperp, "-ko", ms=markerSize, lw=lineWidth, alpha=0.7, mfc=markerColor) ax.set_title("Perpendicular Baseline History", fontsize=fontSize) ## axis format ax = axis_adjust_date_length(ax, dateList, bperp) ax.set_xlabel("Time [years]", fontsize=fontSize) ax.set_ylabel("Perpendicular Baseline [m]", fontsize=fontSize) return fig
def plot_network(fig, pairs_idx, dateList, bperp): ## Plot Temporal-Bperp Network ## ## Inputs ## fig : matplotlib figure ## pairs_idx : pairs info in int index ## dateList : date list in 8 digit string ## bperp : perp baseline array ## Output ## fig : matplotlib figure ## Figure Setting fontSize = 12 markerColor = "orange" markerSize = 16 lineWidth = 2 ## Date Convert dates, datevector = ptime.date_list2vector(dateList) ## Ploting ax = fig.add_subplot(111) ax.plot(dates, bperp, "o", ms=markerSize, lw=lineWidth, alpha=0.7, mfc=markerColor) for i in range(len(pairs_idx)): ax.plot( array([dates[pairs_idx[i][0]], dates[pairs_idx[i][1]]]), array([bperp[pairs_idx[i][0]], bperp[pairs_idx[i][1]]]), "k", lw=lineWidth, ) ax.set_title("Interferogram Network", fontsize=fontSize) ## axis format ax = axis_adjust_date_length(ax, dateList, bperp) ax.set_xlabel("Time [years]", fontsize=fontSize) ax.set_ylabel("Perpendicular Baseline [m]", fontsize=fontSize) return fig
def main(argv): inps = cmdLineParse() print '\n*************** Spatial Average ******************' if inps.mask_file: print 'reading mask file: ' + inps.mask_file mask, mask_atr = readfile.read(inps.mask_file) else: mask = None for File in inps.file: mean_list = ut.spatial_average(File, mask, saveList=True) atr = readfile.read_attribute(File) k = atr['FILE_TYPE'] if inps.disp_fig and k == 'timeseries': # Get date list h5file = h5py.File(File) dateList = sorted(h5file[k].keys()) h5file.close() dates, datevector = ptime.date_list2vector(dateList) # 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 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()) dateListAll = ptime.yyyymmdd(dateListAll) yyListAll = ptime.yyyymmdd2years(dateListAll) print '--------------------------------------------' print 'Dates from input file: '+str(len(dateListAll)) print dateListAll # Extrac exclude dates from input arguments inps.datesNot2include = [] # 1. template_file if inps.template_file: inps = update_inps_from_template(inps, inps.template_file) # 2. ex_date if inps.ex_date: for ex_date in inps.ex_date: if os.path.isfile(ex_date): ex_date = ptime.read_date_list(ex_date) else: ex_date = [ptime.yyyymmdd(ex_date)] inps.datesNot2include += list(set(ex_date) - set(inps.datesNot2include)) # delete dates not existed in input file inps.datesNot2include = list(set(inps.datesNot2include).intersection(dateListAll)) print 'date excluded:'+str(inps.datesNot2include) # 3. min_date if inps.min_date: inps.min_date = ptime.yyyymmdd(inps.min_date) print 'minimum date: '+inps.min_date yy_min = ptime.yyyymmdd2years(inps.min_date) for i in range(len(dateListAll)): date = dateListAll[i] if yyListAll[i] < yy_min and date not in inps.datesNot2include: print ' remove date: '+date inps.datesNot2include.append(date) # 4. max_date if inps.max_date: inps.max_date = ptime.yyyymmdd(inps.max_date) print 'minimum date: '+inps.max_date yy_max = ptime.yyyymmdd2years(inps.max_date) for i in range(len(dateListAll)): date = dateListAll[i] if yyListAll[i] > yy_max and date not in inps.datesNot2include: print ' remove date: '+date inps.datesNot2include.append(date) # Summary dateList = sorted(list(set(dateListAll) - set(inps.datesNot2include))) 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 #B1 = np.linalg.pinv(B) B1 = np.dot(np.linalg.inv(np.dot(B.T,B)),B.T) B1 = np.array(B1,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) start_time = time.time() for i in range(dateNum): date = dateList[i] ut.print_progress(i+1, dateNum, prefix='loading:', suffix=date, elapsed_time=time.time()-start_time) timeseries[i,:] = h5file[k].get(date)[:].flatten() h5file.close() # Velocity Inversion print 'Calculating velocity ...' x = np.dot(B1,timeseries) velocity = np.reshape(x[0,:],[length,width]) print 'Calculating rmse ...' timeseries_linear = np.dot(B,x) rmse = np.reshape(np.sqrt((np.sum((timeseries_linear-timeseries)**2,0))/dateNum),[length,width]) print 'Calculating the standard deviation of the estimated velocity ...' residual = timeseries_linear - timeseries s1 = np.sqrt(np.sum(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' if inps.datesNot2include: inps.outfile = os.path.splitext(inps.outfile)[0]+'_ex'+os.path.splitext(inps.outfile)[1] inps.outfile_rmse = 'rmse_'+inps.outfile inps.outfile_std = 'std_'+inps.outfile inps.outfile_r2 = 'R2_'+inps.outfile # 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.' return inps.outfile
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 = 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 = 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 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 coh_date12_list: list of date, corresponding to coherence_list 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 Output ax : matplotlib axes object ''' # Figure Setting keyList = 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 transparency = 0.7 # Date Convert date8_list = ptime.yyyymmdd(date_list) date6_list = ptime.yymmdd(date8_list) dates, datevector = ptime.date_list2vector(date8_list) # Index of date12 used and dropped idx_date12_keep = range(len(date12_list)) idx_date12_drop = [] for i in date12_list_drop: idx = date12_list.index(i) idx_date12_keep.remove(idx) idx_date12_drop.append(idx) # Index of date used and dropped date12_list_keep = sorted(list(set(date12_list) - set(date12_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))) idx_date_keep = range(len(date8_list)) idx_date_drop = [] for i in date8_list_drop: idx = date8_list.index(i) idx_date_keep.remove(idx) idx_date_drop.append(idx) # Ploting #ax=fig.add_subplot(111) ## Colorbar when conherence is colored if plot_dict['coherence_list']: data_min = min(plot_dict['coherence_list']) data_max = max(plot_dict['coherence_list']) # Normalize normalization = False if normalization: plot_dict['coherence_list'] = [(coh-data_min) / (data_min-data_min) for coh in plot_dict['coherence_list']] plot_dict['disp_min'] = data_min plot_dict['disp_max'] = data_max print 'showing coherence' print 'colormap: '+plot_dict['colormap'] print 'display range: '+str([plot_dict['disp_min'], plot_dict['disp_max']]) print 'data range: '+str([data_min, data_max]) # Use lower/upper part of colormap to emphasis dropped interferograms if 'coh_thres' not in plot_dict.keys() or not plot_dict['coh_thres']: # Find proper cut percentage so that all keep pairs are blue and drop pairs are red coh_list_keep = [plot_dict['coherence_list'][i] for i in idx_date12_keep] coh_list_drop = [plot_dict['coherence_list'][i] for i in idx_date12_drop] plot_dict['coh_thres'] = min(coh_list_keep) if coh_list_drop: plot_dict['coh_thres'] += max(coh_list_drop) plot_dict['coh_thres'] /= 2 plot_dict['coh_thres'] = round(plot_dict['coh_thres'], -int(np.floor(np.log10(abs(plot_dict['coh_thres']))))) print 'color jump at '+str(plot_dict['coh_thres']) c1_num = (plot_dict['coh_thres'] - plot_dict['disp_min']) / (plot_dict['disp_max'] - plot_dict['disp_min']) c1_num = int(c1_num * 200) 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))) divider = make_axes_locatable(ax) cax = divider.append_axes("right", "5%", pad="3%") norm = mpl.colors.Normalize(vmin=plot_dict['disp_min'], vmax=plot_dict['disp_max']) cbar = mpl.colorbar.ColorbarBase(cax, cmap=cmap, norm=norm) cbar.set_label('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 plot_dict['coherence_list']: coh = plot_dict['coherence_list'][plot_dict['coh_date12_list'].index(date12)] coh_idx = (coh - plot_dict['disp_min']) / (plot_dict['disp_max'] - plot_dict['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 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 plot_dict['coherence_list']: coh_idx = (plot_dict['coherence_list'][date12_list.index(date12)] - plot_dict['disp_min']) /\ (plot_dict['disp_max'] - plot_dict['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']) return ax
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 = 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] if not date12_list_drop: plot_dict['disp_drop'] = False ## 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) if coh_thres < disp_min: print 'data range exceed orginal display range, set new display range to: [0.0, %f]' % ( disp_max) disp_min = 0.0 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 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') # 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') 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 if plot_dict['disp_drop']: 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 main(argv): ## Default settings contour_step = 200.0 contour_sigma = 3.0 demShade = "yes" demContour = "yes" global markerSize, markderSize2, markerColor, markerColor2, rectColor global lineWidth, lineWidth2, edgeWidth, fontSize # global markerColor_ref, markerColor_ref2 markerSize = 16 markerSize2 = 16 markerColor = "crimson" # g markerColor2 = "lightgray" markerColor_ref = "white" markerColor_ref2 = "lightgray" rectColor = "black" lineWidth = 0 lineWidth2 = 0 edgeWidth = 1.5 fontSize = 16 global unit, radius, saveFig, dispFig, fig_dpi fig_dpi = 300 radius = 0 saveFig = "no" dispFig = "yes" unit = "cm" dispDisplacement = "no" dispOpposite = "no" dispContour = "only" smoothContour = "no" contour_step = 200 showRef = "yes" vel_alpha = 1.0 zero_start = "yes" global ref_xsub, ref_ysub, ref_date global h5timeseries_2, dates_2, dateList_2 global lbound, hbound ############### Check Inputs ################## if len(sys.argv) < 2: Usage() sys.exit(1) elif len(sys.argv) == 2: if argv[0] == "-h": Usage() sys.exit(1) elif os.path.isfile(argv[0]): timeSeriesFile = argv[0] h5timeseries = h5py.File(timeSeriesFile) k = h5timeseries.keys() if not "timeseries" in k: print "ERROR: Input file is " + k[0] + ".\n\tOnly timeseries is supported.\n" sys.exit(1) else: Usage() sys.exit(1) elif len(sys.argv) > 2: try: opts, args = getopt.getopt( argv, "f:F:v:a:b:s:m:c:w:u:l:h:D:V:t:T:d:r:x:y:X:Y:o:E:", [ "save", "nodisplay", "unit=", "exclude=", "ref-date=", "rect-color=", "zero-start=", "zoom-x=", "zoom-y=", "zoom-lon", "zoom-lat", "lalo=", "opposite", "dem-nocontour", "dem-noshade", "displacement", "contour-step=", "contour-smooth=", "LALO=", ], ) except getopt.GetoptError: Usage() sys.exit(1) for opt, arg in opts: if opt == "-f": timeSeriesFile = arg elif opt == "-F": timeSeriesFile_2 = arg elif opt == "-v": velocityFile = arg elif opt == "-a": vmin = float(arg) elif opt == "-b": vmax = float(arg) elif opt == "-s": fontSize = int(arg) elif opt == "-m": markerSize = int(arg) markerSize2 = int(arg) elif opt == "-c": markerColor = arg elif opt == "-w": lineWidth = int(arg) elif opt == "-u": unit = arg elif opt == "-l": lbound = float(arg) elif opt == "-h": hbound = float(arg) elif opt == "-D": demFile = arg elif opt == "-V": contour_step = float(arg) elif opt == "-t": minDate = arg elif opt == "-T": maxDate = arg elif opt == "-r": radius = abs(int(arg)) elif opt == "-x": xsub = [int(i) for i in arg.split(":")] xsub.sort() # dispVelFig='no' elif opt == "-y": ysub = [int(i) for i in arg.split(":")] ysub.sort() # dispVelFig='no' elif opt == "-X": ref_xsub = [int(i) for i in arg.split(":")] ref_xsub.sort() elif opt == "-Y": ref_ysub = [int(i) for i in arg.split(":")] ref_ysub.sort() # dispVelFig='no' elif opt == "--contour-step": contour_step = float(arg) elif opt == "--contour-smooth": contour_sigma = float(arg) elif opt == "--dem-nocontour": demContour = "no" elif opt == "--dem-noshade": demShade = "no" elif opt == "--displacement": dispDisplacement = "yes" elif opt in ["-E", "--exclude"]: datesNot2show = arg.split(",") elif opt in "--lalo": lalosub = [float(i) for i in arg.split(",")] elif opt in "--LALO": ref_lalosub = [float(i) for i in arg.split(",")] elif opt in ["--rect-color"]: rectColor = arg elif opt in ["--ref-date"]: ref_date = ptime.yyyymmdd(arg) elif opt in ["-u", "--unit"]: unit = arg.lower() elif opt == "--save": saveFig = "yes" elif opt == "--nodisplay": dispFig = "no" saveFig = "yes" elif opt == "--opposite": dispOpposite = "yes" elif opt == "--zero-start": zero_start = arg.lower() elif opt == "--zoom-x": win_x = [int(i) for i in arg.split(":")] win_x.sort() elif opt == "--zoom-y": win_y = [int(i) for i in arg.split(":")] win_y.sort() elif opt == "--zoom-lon": win_lon = [float(i) for i in arg.split(":")] win_lon.sort() elif opt == "--zoom-lat": win_lat = [float(i) for i in arg.split(":")] win_lat.sort() ############################################################## ## Read time series file info if not os.path.isfile(timeSeriesFile): print "\nERROR: Input time series file does not exist: " + timeSeriesFile + "\n" sys.exit(1) h5timeseries = h5py.File(timeSeriesFile) k = h5timeseries.keys() # read h5 file and its group type if not "timeseries" in k: print "ERROR: Input file is " + k[0] + ".\n\tOnly timeseries is supported.\n" sys.exit(1) atr = readfile.read_attributes(timeSeriesFile) dateList1 = h5timeseries["timeseries"].keys() dateList1 = sorted(dateList1) dates1, datevector1 = ptime.date_list2vector(dateList1) print "\n************ Time Series Display - Point *************" ##### Select Check try: lalosub xsub = subset.coord_geo2radar([lalosub[1]], atr, "longitude") ysub = subset.coord_geo2radar([lalosub[0]], atr, "latitude") xsub = [xsub] ysub = [ysub] if radius == 0: radius = 3 except: pass try: ref_lalosub ref_xsub = subset.coord_geo2radar([ref_lalosub[1]], atr, "longitude") ref_ysub = subset.coord_geo2radar([ref_lalosub[0]], atr, "latitude") ref_xsub = [ref_xsub] ref_ysub = [ref_ysub] if radius == 0: radius = 3 except: pass ############################################################## global dates, dateList, datevector_all, dateListMinMax print "*******************" print "All dates existed:" print dateList1 print "*******************" ## Check exclude date input try: datesNot2show if os.path.isfile(datesNot2show[0]): try: datesNot2show = ptime.read_date_list(datesNot2show[0]) except: print "Can not read date list file: " + datesNot2show[0] print "dates not to show: " + str(datesNot2show) except: datesNot2show = [] ## Check Min / Max Date dateListMinMax = [] try: minDate minDate = ptime.yyyymmdd(minDate) dateListMinMax.append(minDate) minDateyy = ptime.yyyymmdd2years(minDate) print "minimum date: " + minDate for date in dateList1: yy = ptime.yyyymmdd2years(date) if yy < minDateyy: datesNot2show.append(date) except: pass try: maxDate maxDate = ptime.yyyymmdd(maxDate) dateListMinMax.append(maxDate) maxDateyy = ptime.yyyymmdd2years(maxDate) print "maximum date: " + maxDate for date in dateList1: yy = ptime.yyyymmdd2years(date) if yy > maxDateyy: datesNot2show.append(date) except: pass dateListMinMax = sorted(dateListMinMax) if not dateListMinMax: print "no min/max date input." else: datesMinMax, dateVecMinMax = ptime.date_list2vector(dateListMinMax) ## Finalize Date List try: dateList = [] for date in dateList1: if date not in datesNot2show: dateList.append(date) print "--------------------------------------------" print "dates used to show time series displacements:" print dateList print "--------------------------------------------" except: dateList = dateList1 print "using all dates to show time series displacement" ## Read Date Info (x axis for time series display) dates, datevector = ptime.date_list2vector(dateList) datevector_all = list(datevector) ## Check reference date input try: ref_date if not ref_date in dateList: print "Reference date - " + ref_date + " - is not included in date list to show." sys.exit(1) else: print "reference date: " + ref_date except: if zero_start == "yes": ref_date = dateList[0] print "set the 1st date as reference for displacement display." else: pass ############################################################## ##### Plot Fig 1 - Velocity / last epoch of time series / DEM fig = plt.figure(1) ax = fig.add_subplot(111) ##### Check subset range width = int(atr["WIDTH"]) length = int(atr["FILE_LENGTH"]) print "file size: " + str(length) + ", " + str(width) try: win_y = subset.coord_geo2radar(win_lat, atr, "latitude") except: try: win_y except: win_y = [0, length] try: win_x = subset.coord_geo2radar(win_lon, atr, "longitude") except: try: win_x except: win_x = [0, width] win_y, win_x = subset.check_subset_range(win_y, win_x, atr) try: velocityFile try: vel, vel_atr = readfile.read(velocityFile) except: vel, vel_atr = readfile.read(timeSeriesFile, velocityFile) ax.set_title(velocityFile) print "display: " + velocityFile except: vel, vel_atr = readfile.read(timeSeriesFile, dateList1[-1]) ax.set_title("epoch: " + dateList1[-1]) print "display last epoch" ##### show displacement instead of phase if vel_atr["FILE_TYPE"] in ["interferograms", ".unw"] and dispDisplacement == "yes": print "show displacement" phase2range = -float(vel_atr["WAVELENGTH"]) / (4 * np.pi) vel *= phase2range else: dispDisplacement = "no" ## Reference Point if showRef == "yes": try: ax.plot(int(atr["ref_x"]), int(atr["ref_y"]), "ks", ms=6) except: pass if dispOpposite == "yes": print "show opposite value in figure/map 1" vel *= -1 ## Flip try: flip_lr except: try: flip_ud except: flip_lr, flip_ud = view.auto_flip_check(atr) ## Status bar ## Geo coordinate try: ullon = float(atr["X_FIRST"]) ullat = float(atr["Y_FIRST"]) lon_step = float(atr["X_STEP"]) lat_step = float(atr["Y_STEP"]) lon_unit = atr["Y_UNIT"] lat_unit = atr["X_UNIT"] geocoord = "yes" print "Input file is Geocoded" except: geocoord = "no" def format_coord(x, y): col = int(x + 0.5) row = int(y + 0.5) if col >= 0 and col <= width and row >= 0 and row <= length: z = vel[row, col] try: lon = ullon + x * lon_step lat = ullat + y * lat_step return "x=%.1f, y=%.1f, value=%.4f, lon=%.4f, lat=%.4f" % (x, y, z, lon, lat) except: return "x=%.1f, y=%.1f, value=%.4f" % (x, y, z) ax.format_coord = format_coord ## DEM try: demFile dem, demRsc = readfile.read(demFile) ax = view.plot_dem_yx(ax, dem, demShade, demContour, contour_step, contour_sigma) vel_alpha = 0.8 except: print "No DEM file" try: img = ax.imshow(vel, vmin=vmin, vmax=vmax, alpha=vel_alpha) except: img = ax.imshow(vel, alpha=vel_alpha) plt.colorbar(img) ## Zoom In (subset) if flip_lr == "yes": ax.set_xlim(win_x[1], win_x[0]) else: ax.set_xlim(win_x[0], win_x[1]) if flip_ud == "yes": ax.set_ylim(win_y[0], win_y[1]) else: ax.set_ylim(win_y[1], win_y[0]) ## Flip # if flip_lr == 'yes': fig.gca().invert_xaxis() # if flip_ud == 'yes': fig.gca().invert_yaxis() ########################################## ##### Plot Fig 2 - Time series plot # fig2 = plt.figure(num=2,figsize=(12,6)) fig2 = plt.figure(2, figsize=(12, 6)) ax2 = fig2.add_subplot(111) try: timeSeriesFile_2 h5timeseries_2 = h5py.File(timeSeriesFile_2) dateList_2 = h5timeseries_2["timeseries"].keys() dateList_2 = sorted(dateList_2) dates_2, datevector_2 = ptime.date_list2vector(dateList_2) datevector_all += list(set(datevector_2) - set(datevector_all)) datevector_all = sorted(datevector_all) except: pass ################################ Plot Code Package <start> ################################# def plot_ts(ax, ax2, fig2, xsub, ysub, h5timeseries): ax2.cla() print "\n-------------------------------------------------------------------------------" disp_min = 0 disp_max = 0 ############################# Plot Time Series ############################## global ref_xsub, ref_ysub ##### 1.1 Plot Reference time series try: ref_xsub ref_ysub ref_xsub, ref_ysub = check_yx(ref_xsub, ref_ysub, radius, ax, rectColor) print "----------------------------------------------------" print "Reference Point:" print "ref_x=" + str(ref_xsub[0]) + ":" + str(ref_xsub[1]) print "ref_y=" + str(ref_ysub[0]) + ":" + str(ref_ysub[1]) print "-----------------------------" print "Time series with all dates:" dis1, dis1_mean, dis1_std, dis1_vel = read_dis(ref_xsub, ref_ysub, dateList1, h5timeseries, unit) (_, caps, _) = ax2.errorbar( dates1, dis1_mean, yerr=dis1_std, fmt="-ks", ms=markerSize2, lw=0, alpha=1, mfc=markerColor_ref, mew=edgeWidth, elinewidth=edgeWidth, ecolor="black", capsize=markerSize * 0.5, ) for cap in caps: cap.set_markeredgewidth(edgeWidth) disp_min, disp_max = update_lim(disp_min, disp_max, dis1_mean, dis1_std) if not len(dateList) == len(dateList1): print "-----------------------------" print "Time series with dates of interest:" dis12, dis12_mean, dis12_std, dis12_vel = read_dis(ref_xsub, ref_ysub, dateList, h5timeseries, unit) (_, caps, _) = ax2.errorbar( dates, dis12_mean, yerr=dis12_std, fmt="-ks", ms=markerSize2, lw=0, alpha=1, mfc=markerColor_ref2, mew=edgeWidth, elinewidth=edgeWidth, ecolor="black", capsize=markerSize * 0.5, ) for cap in caps: cap.set_markeredgewidth(edgeWidth) disp_min, disp_max = update_lim(disp_min, disp_max, dis12_mean, dis12_std) except: pass ##### 1.2.0 Read y/x print "\n----------------------------------------------------" print "Point of Interest:" xsub, ysub = check_yx(xsub, ysub, radius, ax, rectColor) print "x=" + str(xsub[0]) + ":" + str(xsub[1]) print "y=" + str(ysub[0]) + ":" + str(ysub[1]) ##### 1.2.1 Plot 2nd time series try: timeSeriesFile_2 print "-----------------------------" print "2nd Time Series:" dis2, dis2_mean, dis2_std, dis2_vel = read_dis(xsub, ysub, dateList_2, h5timeseries_2, unit) (_, caps, _) = ax2.errorbar( dates_2, dis2_mean, yerr=dis2_std, fmt="-ko", ms=markerSize2, lw=0, alpha=1, mfc=markerColor2, elinewidth=0, ecolor="black", capsize=0, ) for cap in caps: cap.set_markeredgewidth(edgeWidth) disp_min, disp_max = update_lim(disp_min, disp_max, dis2_mean, dis2_std) except: pass ##### 1.2.2 Plot 1st time series print "-----------------------------" print "Time Series:" dis, dis_mean, dis_std, dis_vel = read_dis(xsub, ysub, dateList, h5timeseries, unit) (_, caps, _) = ax2.errorbar( dates, dis_mean, yerr=dis_std, fmt="-ko", ms=markerSize, lw=lineWidth, alpha=1, mfc=markerColor, elinewidth=edgeWidth, ecolor="black", capsize=markerSize * 0.5, ) for cap in caps: cap.set_markeredgewidth(edgeWidth) disp_min, disp_max = update_lim(disp_min, disp_max, dis_mean, dis_std) ####################### Figure Format ####################### ## x axis format try: ax2 = ptime.adjust_xaxis_date(ax2, dateVecMinMax, fontSize) except: ax2 = ptime.adjust_xaxis_date(ax2, datevector_all, fontSize) ## y axis format ax2.set_ylabel("Displacement [" + unit + "]", fontsize=fontSize) try: lbound hbound ax2.set_ylim(lbound, hbound) except: disp_buf = 0.2 * (disp_max - disp_min) ax2.set_ylim(disp_min - disp_buf, disp_max + disp_buf) for tick in ax2.yaxis.get_major_ticks(): tick.label.set_fontsize(fontSize) ## title figTitle = "x=" + str(xsub[0]) + ":" + str(xsub[1]) + ", y=" + str(ysub[0]) + ":" + str(ysub[1]) try: lonc = ullon + (xsub[0] + xsub[1]) / 2.0 * lon_step latc = ullat + (ysub[0] + ysub[1]) / 2.0 * lat_step figTitle += ", lalo=" + "%.4f,%.4f" % (latc, lonc) except: pass ax2.set_title(figTitle) ################## Save and Output ##################### if saveFig == "yes": print "-----------------------------" Delay = {} Delay["displacement"] = dis Delay["unit"] = unit Delay["time"] = datevector Delay["velocity"] = dis_vel[0] Delay["velocity_unit"] = unit + "/yr" Delay["velocity_std"] = dis_vel[4] figBase = "x" + str(xsub[0]) + "_" + str(xsub[1] - 1) + "y" + str(ysub[0]) + "_" + str(ysub[1] - 1) sio.savemat(figBase + "_ts.mat", {"displacement": Delay}) print "saved " + figBase + "_ts.mat" fig2.savefig(figBase + "_ts.pdf", bbox_inches="tight", transparent=True, dpi=fig_dpi) print "saved " + figBase + "_ts.pdf" if dispFig == "no": fig.savefig(figBase + "_vel.png", bbox_inches="tight", transparent=True, dpi=fig_dpi) print "saved " + figBase + "_vel.png" ################################ Plot Code Package <end> ################################# ########### 1. Plot Time Series with x/y ########## try: xsub ysub plot_ts(ax, ax2, fig2, xsub, ysub, h5timeseries) except: print "No x/y input" pass ########### 2. Plot Time Series with Click ########## ## similar to 1. Plot Time Series with x/y def onclick(event): ax2.cla() xsub = [int(event.xdata)] ysub = [int(event.ydata)] plot_ts(ax, ax2, fig2, xsub, ysub, h5timeseries) if dispFig == "yes": plt.show() try: cid = fig.canvas.mpl_connect("button_press_event", onclick) except: pass if dispFig == "yes": plt.show()
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 fig_name = os.path.dirname(os.path.abspath(inps.timeseries_file))+\ '/rms_'+os.path.splitext(inps.timeseries_file)[0] if inps.ramp_type != 'no': fig_name += '_' + inps.ramp_type fig_name += '.pdf' if ut.update_file(fig_name, [exTxtFile, refTxtFile, inps.template_file], check_readable=False): if inps.fig_size: fig = plt.figure(figsize=inps.fig_size) else: fig = plt.figure() ax = fig.add_subplot(111) font_size = 12 dates, datevector = ptime.date_list2vector(date_list) try: bar_width = ut.mode(np.diff(dates).tolist()) * 3 / 4 except: bar_width = np.min(np.diff(dates).tolist()) * 3 / 4 x_list = [i - bar_width / 2 for i in dates] rms_list = [i * 1000. for i in rms_list] min_rms = inps.min_rms * 1000. # 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: if ex_idx_list: 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, every_year=inps.tick_year_num) ax.plot(np.array([xmin, xmax]), np.array([min_rms, min_rms]), '--k') # axis format ax = pnet.auto_adjust_yaxis(ax, rms_list + [min_rms], font_size, ymin=0.0) ax.set_xlabel('Time [years]', fontsize=font_size) ax.set_ylabel('Root Mean Square [mm]', fontsize=font_size) ax.yaxis.set_ticks_position('both') ax.tick_params(labelsize=font_size) if inps.save_reference_date or inps.save_exclude_date: plt.legend(fontsize=font_size) # save figure fig.savefig(fig_name, bbox_inches='tight', transparent=True) print 'save figure to file: ' + fig_name return
def main(argv): ##### Default fontSize = 12 lineWidth = 2 markerColor = 'crimson' markerSize = 16 disp_fig = 'no' save_fig = 'yes' save_list = 'yes' ref_file = 'reference_date.txt' drop_file = 'drop_date.txt' ##### Check Inputs if len(sys.argv)>3: try: opts, args = getopt.getopt(argv,'h:f:m:o:x:y:',['help','circle=']) except getopt.GetoptError: print 'Error in reading input options!'; usage() ; sys.exit(1) for opt,arg in opts: if opt in ("-h","--help"): usage() ; sys.exit() elif opt == '-f': File = arg elif opt == '-m': maskFile = arg elif opt == '-x': xsub = [int(i) for i in arg.split(':')]; xsub.sort() elif opt == '-y': ysub = [int(i) for i in arg.split(':')]; ysub.sort() elif opt == '--circle' : cir_par = [i for i in arg.split(';')] #elif opt == '-o': outName = arg else: try: File = argv[0] except: usage(); sys.exit(1) try: maskFile = argv[1] except: pass try: atr = readfile.read_attribute(File) except: usage(); sys.exit(1) ext = os.path.splitext(File)[1].lower() FileBase = os.path.basename(File).split(ext)[0] outNameBase = 'spatialMean_'+FileBase print '\n*************** Spatial Average ******************' ##### Input File Info k = atr['FILE_TYPE'] print 'Input file is '+k width = int(atr['WIDTH']) length = int(atr['FILE_LENGTH']) h5file = h5py.File(File) epochList = h5file[k].keys() epochList = sorted(epochList) epochNum = len(epochList) print 'number of epoch: '+str(epochNum) if dates,datevector = ptime.date_list2vector(epochList) ##### Mask Info try: Mask_orig,Matr = readfile.read(maskFile) print 'mask file: '+maskFile Masking = 'yes' except: print 'No mask. Use the whole area for ramp estimation.' Masking = 'no' Mask_orig=np.ones((length,width)) Mask = np.zeros((length,width)) Mask[:] = Mask_orig[:] ## Bounding Subset try: xsub ysub ysub,xsub = subset.check_subset_range(ysub,xsub,atr) Mask[ysub[0]:ysub[1],xsub[0]:xsub[1]] = Mask_orig[ysub[0]:ysub[1],xsub[0]:xsub[1]]*2 #Mask[0:ysub[0],:] = 0 #Mask[ysub[1]:length,:] = 0 #Mask[:,0:xsub[0]] = 0 #Mask[:,xsub[1]:width] = 0 except: Mask = Mask_orig*2 print 'No subset input.' ## Circle Inputs try: cir_par for i in range(len(cir_par)): cir_idx = circle_index(atr,cir_par[i]) Mask[cir_idx] = Mask_orig[cir_idx] print 'Circle '+str(i)+': '+cir_par[i] except: print 'No circle of interest input.' ## Mask output idx = Mask == 2 idxNum = float(sum(sum(idx))) fig = plt.figure() plt.imshow(Mask,cmap='gray') plt.savefig(outNameBase+'_mask.png',bbox_inches='tight') print 'save mask to '+outNameBase+'_mask.png' #fig.clf() ##### Calculation meanList = np.zeros(epochNum) pixPercent = np.zeros(epochNum) pixT = 0.7 print 'calculating ...' print ' Date Mean Percentage' for i in range(epochNum): epoch = epochList[i] d = h5file[k].get(epoch)[:] #d[Mask==0] = np.nan meanList[i] = np.nanmean(d[idx]) pixPercent[i] = np.sum(d[idx] >= pixT)/idxNum print epoch+' : %.2f %.1f%%'%(meanList[i],pixPercent[i]*100) del d h5file.close() ##### Reference date - Max Value top3 = sorted(zip(meanList,epochList), reverse=True)[:3] print '------------ Top 3 Mean ------------------' print top3 ## Write to txt file fref = open(ref_file,'w') fref.write(str(top3[0][1])+'\n') fref.close() print 'write optimal reference date to '+ref_file idxMean = meanList == np.nanmax(meanList) ##### Drop dates - mean threshold #meanT = 0.7 #idxMean = meanList < meanT #print '------------ Mean Value < '+str(meanT)+' --------' #print np.array(epochList)[idxMean] #print meanList[idxMean] ##### Drop dates - good pixel percentage pixNumT = 0.7 print '------------ Good Pixel Percentage < %.0f%% -------'%(pixNumT*100) idxPix = pixPercent < pixNumT dropEpochList = np.array(epochList)[idxPix] print dropEpochList print pixPercent[idxPix] ## Write to txt file fdrop = open(drop_file,'w') for i in range(len(dropEpochList)): fdrop.write(str(dropEpochList[i])+'\n') fdrop.close() print 'write drop dates to '+drop_file print '-------------------------------------------' ##### Display fig = plt.figure(figsize=(12,12)) ax = fig.add_subplot(211) ax.plot(dates, meanList, '-ko', ms=markerSize, lw=lineWidth, alpha=0.7, mfc=markerColor) #ax.plot([dates[0],dates[-1]],[meanT,meanT], '--b', lw=lineWidth) #sc = ax.scatter(dates, np.tile(0.5,epochNum), c=meanList, s=22**2, alpha=0.3, vmin=0.0, vmax=1.0) #ax.scatter(np.array(dates)[idxMean], 0.5, c=meanList[idxMean], s=22**2, alpha=1.0, vmin=0.0, vmax=1.0) ax = ptime.auto_adjust_xaxis_date(ax,datevector) ax.set_ylim(0,1) ax.set_title('Spatial Average Value', fontsize=fontSize) ax.set_xlabel('Time [years]', fontsize=fontSize) #cbar = plt.colorbar(sc) #cbar.set_label('Spatial Mean of Normalized Sum Epochs') ax = fig.add_subplot(212) ax.plot(dates, pixPercent, '-ko', ms=markerSize, lw=lineWidth, alpha=0.7, mfc=markerColor) ax.plot([dates[0],dates[-1]],[pixNumT,pixNumT], '--b', lw=lineWidth) ax = ptime.auto_adjust_xaxis_date(ax,datevector) ax.set_ylim(0,1) ax.set_title('Percenrage of Pixels with Value > '+str(pixNumT), fontsize=fontSize) ax.set_xlabel('Time [years]', fontsize=fontSize) vals = ax.get_yticks() ax.set_yticklabels(['{:3.0f}%'.format(i*100) for i in vals]) if save_fig == 'yes': plt.savefig(outNameBase+'.png',bbox_inches='tight') print 'save figure to '+outNameBase+'.png' if disp_fig == 'yes': plt.show() ##### Output if save_list == 'yes': epochList6 = ptime.yymmdd(epochList) fl = open(outNameBase+'.txt','w') for i in range(epochNum): str_line = epochList6[i]+' %.2f %.2f\n'%(meanList[i],pixPercent[i]) fl.write(str_line) fl.close() print 'write data to '+outNameBase+'.txt\n'
########################################################################################### if __name__ == '__main__': #######Actual code. inps = cmdLineParse() # Time Series Info 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) # Read exclude dates 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(
def main(argv): lineWidth = 2 fontSize = 12 markerColor = 'orange' markerSize = 16 networkDisplay = 'no' if len(sys.argv)>2: try: opts, args = getopt.getopt(argv,"h:f:C:s:w:m:c:t:b:d:l:n:N:T:l:") except getopt.GetoptError: Usage() ; sys.exit(1) for opt,arg in opts: if opt in ("-h","--help"): Usage(); sys.exit() elif opt == '-f': File = arg elif opt == '-C': corFile = arg elif opt == '-s': fontSize = int(arg) elif opt == '-w': lineWidth = int(arg) elif opt == '-m': markerSize = int(arg) elif opt == '-c': markerColor = arg elif opt == '-t': temp_thr = float(arg) elif opt == '-b': base_thr = float(arg) elif opt == '-d': dates2Rmv = arg elif opt == '-l': ifgrams_to_rmv = arg elif opt == '-n': networkDisplay = arg elif opt == '-N': ifgrams_Number_to_rmv = arg.split() elif opt == '-T': templateFile = arg elif opt == '-l': list_fileList = arg.split(',') try: File except: Usage() ; sys.exit(1) elif len(sys.argv)==2: File = argv[0] networkDisplay = 'yes' else: Usage() ; sys.exit(1) ## display network for modification, if no other limit setted try: temp_thr base_trh dates2Rmv ifgrams_to_rmv ifgrams_Number_to_rmv networkDisplay = 'yes' except: pass ########################################################### atr = readfile.read_attributes(File) k = atr['FILE_TYPE'] print '\n*************** Modify Network ****************' print 'Input file is '+k #if h5file.keys()[0] != 'interferograms': # print 'Input file should be interferograms'; sys.exit(1) h5file = h5py.File(File) ifgramList = h5file[k].keys() ifgramList = sorted(ifgramList) try: ifgrams_to_rmv except: ifgrams_to_rmv=[] ########################################################### ##### L - list file of interferograms try: ifgrams_to_rmv = list_file.read() except: pass ##### T - templateFile, pysar.dropIfgIndex try: templateFile template = readfile.read_template(templateFile) drop_ifg_index = template['pysar.drop.ifgIndex'].split(',') print 'drop interferogram index:' print drop_ifg_index try: ifgrams_Number_to_rmv except: ifgrams_Number_to_rmv = [] for index in drop_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): ifgrams_Number_to_rmv.append(str(j)) elif len(index_temp)==1: ifgrams_Number_to_rmv.append(index) else: print 'Unrecoganized input: '+index except: pass ##### N - interferogram number list try: for i in ifgrams_Number_to_rmv: print i+' '+ifgramList[int(i)-1] ifgrams_to_rmv.append(ifgramList[int(i)-1]) except: pass ##### b - perpendicular baseline limit try: base_thr print 'interferograms with the spatial baseline longer than '+ str(base_thr)+' m is removed' for ifgram in ifgramList: Baseline = (float(h5file[k][ifgram].attrs['P_BASELINE_BOTTOM_HDR'])+\ float(h5file[k][ifgram].attrs['P_BASELINE_TOP_HDR']))/2 if abs(Baseline) > base_thr: if not ifgram in ifgrams_to_rmv: ifgrams_to_rmv.append(ifgram) except: print 'No Spatial Baseline threshold applied' ##### d - dates to remove try: dates2Rmv print 'interferograms with any of following dates will be removed: '+ dates2Rmv for ifgram in ifgramList: date1,date2 = h5file[k][ifgram].attrs['DATE12'].split('-') if (date1 in dates2Rmv) or (date2 in dates2Rmv): if not ifgram in ifgrams_to_rmv: ifgrams_to_rmv.append(ifgram) except: print 'No specific dates selected to remove' ##### t - temporal baseline limit tbase,dateList,dateDict,dateList6=ut.date_list(h5file) try: temp_thr print 'Applying the temporal baseline threshold with threshold of '+str(temp_thr)+' days' for ifgram in ifgramList: date1,date2 = h5file[k][ifgram].attrs['DATE12'].split('-') ind1 = dateList6.index(date1) ind2 = dateList6.index(date2) dt=tbase[ind2]-tbase[ind1] if dt>temp_thr: if not ifgram in ifgrams_to_rmv: ifgrams_to_rmv.append(ifgram) except: print 'No Temporal Baseline threshold applied' ############################################################ ############################################################ if networkDisplay=='yes': dates,datevector = ptime.date_list2vector(dateList) ################################################## Bp = ut.Baseline_timeseries(File) ############################################################# ifgramList = h5file[k].keys() igram_pairs=np.zeros([len(ifgramList),2],np.int) i=0 for ifgram in ifgramList: date1,date2 = h5file[k][ifgram].attrs['DATE12'].split('-') igram_pairs[i][0]=dateList6.index(date1) igram_pairs[i][1]=dateList6.index(date2) i=i+1 ############################################################ import matplotlib.pyplot as plt fig1 = plt.figure(1) ax1=fig1.add_subplot(111) ax1.cla() # ax1.plot(dates,Bp, 'o',ms=markerSize, lw=lineWidth, alpha=0.7, mfc=markerColor) print tbase ax1.plot(tbase,Bp, 'o',ms=markerSize, lw=lineWidth, alpha=0.7, mfc=markerColor) for ni in range(len(ifgramList)): ax1.plot(array([tbase[igram_pairs[ni][0]],tbase[igram_pairs[ni][1]]]),\ array([Bp[igram_pairs[ni][0]],Bp[igram_pairs[ni][1]]]),'k',lw=4) # ax1.fmt_xdata = DateFormatter('%Y-%m-%d %H:%M:%S') ax1.set_ylabel('Bperp [m]',fontsize=fontSize) ax1.set_xlabel('Time [years]',fontsize=fontSize) ts=datevector[0]+0.2 te=datevector[-1]+0.2 ys=int(ts) ye=int(te) ms=int((ts-ys)*12) me=int((te-ye)*12) if ms>12: ys =ys+1; ms=1 if me>12: ye =ye+1; me=1 if ms<1: ys =ys-1; ms=12 if me<1: ye =ye-1; me=12 dss=datetime.datetime(ys,ms,1,0,0) dee=datetime.datetime(ye,me,1,0,0) ax1.set_ylim(min(Bp)-0.4*abs(min(Bp)),max(Bp)+0.4*max(Bp)) xticklabels = getp(gca(), 'xticklabels') yticklabels = getp(gca(), 'yticklabels') setp(yticklabels, 'color', 'k', fontsize=fontSize) setp(xticklabels, 'color', 'k', fontsize=fontSize) ########################################## x=[] y=[] Master_index_torremove=[] Slave_index_torremove=[] a_tbase=array(tbase) a_Bp=array(Bp) def onclick(event): if event.button==1: print 'click' xClick = event.xdata yClick = event.ydata idx=nearest_neighbor(xClick,yClick, a_tbase, a_Bp) xr = a_tbase[idx] yr = a_Bp[idx] ix=tbase.index(xr)+1 print ix x.append(xr) y.append(yr) if mod(len(x),2)==0: Master_index_torremove.append(tbase.index(xr)) ax1.plot([x[len(x)-1],x[len(x)-2]],[y[len(x)-1],y[len(x)-2]],'r',lw=4) else: Slave_index_torremove.append(tbase.index(xr)) plt.show() cid = fig1.canvas.mpl_connect('button_press_event', onclick) plt.show() print Master_index_torremove print Slave_index_torremove if len(Master_index_torremove) == len(Slave_index_torremove): R=np.vstack((Master_index_torremove,Slave_index_torremove)) else: R=np.vstack((Master_index_torremove[:-1],Slave_index_torremove)) R=np.vstack((Master_index_torremove,Slave_index_torremove)) R.sort(0) print R print dateList6 numIgrams_rmv=np.shape(R)[1] for ifgram in ifgramList: date1,date2 = h5file[k][ifgram].attrs['DATE12'].split('-') for i in range(numIgrams_rmv): if dateList6[R[0][i]]==date1 and dateList6[R[1][i]]==date2: ifgrams_to_rmv.append(ifgram) else: print 'No network display.' ############################################################ ############################################################ print 'Number of interferograms to remove: '+str(len(ifgrams_to_rmv)) print 'List of interferograms to remove:' print ifgrams_to_rmv file_modified='Modified_'+File h5filem = h5py.File(file_modified,'w') gg = h5filem.create_group(k) ifgram=ifgramList[0] unw = h5file[k][ifgram].get(ifgram) MaskZero=np.ones([unw.shape[0],unw.shape[1]]) print 'writing >>> modified interferogram file ...' print 'Number of interferograms: '+str(len(ifgramList)-len(ifgrams_to_rmv)) for ifgram in ifgramList: if not ifgram in ifgrams_to_rmv: print ifgram unwSet = h5file[k][ifgram].get(ifgram) unw = unwSet[0:unwSet.shape[0],0:unwSet.shape[1]] MaskZero=unw*MaskZero group = gg.create_group(ifgram) dset = group.create_dataset(ifgram, data=unw, compression='gzip') for key, value in h5file[k][ifgram].attrs.iteritems(): group.attrs[key] = value Mask=np.ones([unwSet.shape[0],unwSet.shape[1]]) Mask[MaskZero==0]=0 h5file.close() h5filem.close() ####################### Coherence ######################## # updating Coherence file # convert ifgrams_to_rmv to cor_to_rmv date12_to_rmv=[] for igram in ifgrams_to_rmv: date12_to_rmv.append(igram.split('-sim')[0].split('filt_')[-1]) try: corFile h5fileCor=h5py.File(corFile) corList=h5fileCor['coherence'].keys() corFile_modified='Modified_'+corFile h5fileCorm=h5py.File(corFile_modified,'w') gc = h5fileCorm.create_group('coherence') print 'writing >>> modified coherence file ...' for cor in corList: date12=cor.split('-sim')[0].split('filt_')[-1] if not date12 in date12_to_rmv: print cor unwSet = h5fileCor['coherence'][cor].get(cor) unw = unwSet[0:unwSet.shape[0],0:unwSet.shape[1]] group = gc.create_group(cor) dset = group.create_dataset(cor, data=unw, compression='gzip') for key, value in h5fileCor['coherence'][cor].attrs.iteritems(): group.attrs[key] = value h5fileCor.close() h5fileCorm.close() except: print 'No coherence file to be updated.' ############################################################ print 'writing >>> Modified_Mask.h5' h5mask = h5py.File('Modified_Mask.h5','w') group = h5mask.create_group('mask') dset = group.create_dataset(os.path.basename('mask'), data=Mask, compression='gzip') for key, value in atr.iteritems(): group.attrs[key] = value h5mask.close()
def main(argv): ## Default settings contour_step = 200.0 contour_sigma = 3.0 demShade = 'yes' demContour = 'yes' global markerSize, markderSize2, markerColor, markerColor2, rectColor global lineWidth, lineWidth2, edgeWidth, fontSize #global markerColor_ref, markerColor_ref2 markerSize = 16 markerSize2 = 16 markerColor = 'crimson' # g markerColor2 = 'lightgray' markerColor_ref = 'white' markerColor_ref2 = 'lightgray' rectColor = 'black' lineWidth = 0 lineWidth2 = 0 edgeWidth = 1.5 fontSize = 16 global unit, radius, saveFig, dispFig, fig_dpi fig_dpi = 300 radius = 0 saveFig = 'no' dispFig = 'yes' unit = 'cm' dispDisplacement = 'no' dispOpposite = 'no' dispContour = 'only' smoothContour = 'no' contour_step = 200 showRef = 'yes' vel_alpha = 1.0 zero_start = 'yes' global ref_xsub, ref_ysub, ref_date global h5timeseries_2, dates_2, dateList_2 global lbound, hbound ############### Check Inputs ################## if len(sys.argv) < 2: usage() sys.exit(1) elif len(sys.argv) == 2: if argv[0] == '-h': usage() sys.exit(1) elif os.path.isfile(argv[0]): timeSeriesFile = argv[0] h5timeseries = h5py.File(timeSeriesFile) k = h5timeseries.keys() if not 'timeseries' in k: print 'ERROR: Input file is ' + k[ 0] + '.\n\tOnly timeseries is supported.\n' sys.exit(1) else: usage() sys.exit(1) elif len(sys.argv) > 2: try: opts, args = getopt.getopt(argv,"f:F:v:a:b:s:m:c:w:u:l:h:D:V:t:T:d:r:x:y:X:Y:o:E:", ['save','nodisplay','unit=','exclude=','ref-date=','rect-color=',\ 'zero-start=','zoom-x=','zoom-y=','zoom-lon','zoom-lat','lalo=',\ 'opposite','dem-nocontour','dem-noshade','displacement','contour-step=',\ 'contour-smooth=','LALO=']) except getopt.GetoptError: usage() sys.exit(1) for opt, arg in opts: if opt == '-f': timeSeriesFile = arg elif opt == '-F': timeSeriesFile_2 = arg elif opt == '-v': velocityFile = arg elif opt == '-a': vmin = float(arg) elif opt == '-b': vmax = float(arg) elif opt == '-s': fontSize = int(arg) elif opt == '-m': markerSize = int(arg) markerSize2 = int(arg) elif opt == '-c': markerColor = arg elif opt == '-w': lineWidth = int(arg) elif opt == '-u': unit = arg elif opt == '-l': lbound = float(arg) elif opt == '-h': hbound = float(arg) elif opt == '-D': demFile = arg elif opt == '-V': contour_step = float(arg) elif opt == '-t': minDate = arg elif opt == '-T': maxDate = arg elif opt == '-r': radius = abs(int(arg)) elif opt == '-x': xsub = sorted([int(i) for i in arg.split(':')]) elif opt == '-y': ysub = sorted([int(i) for i in arg.split(':')]) elif opt == '-X': ref_xsub = sorted([int(i) for i in arg.split(':')]) elif opt == '-Y': ref_ysub = sorted([int(i) for i in arg.split(':')]) elif opt == '--contour-step': contour_step = float(arg) elif opt == '--contour-smooth': contour_sigma = float(arg) elif opt == '--dem-nocontour': demContour = 'no' elif opt == '--dem-noshade': demShade = 'no' elif opt == '--displacement': dispDisplacement = 'yes' elif opt in ['-E', '--exclude']: datesNot2show = arg.split(',') elif opt in '--lalo': lalosub = [float(i) for i in arg.split(',')] elif opt in '--LALO': ref_lalosub = [float(i) for i in arg.split(',')] elif opt in ['--rect-color']: rectColor = arg elif opt in ['--ref-date']: ref_date = ptime.yyyymmdd(arg) elif opt in ['-u', '--unit']: unit = arg.lower() elif opt == '--save': saveFig = 'yes' elif opt == '--nodisplay': dispFig = 'no' saveFig = 'yes' elif opt == '--opposite': dispOpposite = 'yes' elif opt == '--zero-start': zero_start = arg.lower() elif opt == '--zoom-x': win_x = sorted([int(i) for i in arg.split(':')]) elif opt == '--zoom-y': win_y = sorted([int(i) for i in arg.split(':')]) elif opt == '--zoom-lon': win_lon = sorted([float(i) for i in arg.split(':')]) elif opt == '--zoom-lat': win_lat = sorted([float(i) for i in arg.split(':')]) ############################################################## ## Read time series file info if not os.path.isfile(timeSeriesFile): print '\nERROR: Input time series file does not exist: ' + timeSeriesFile + '\n' sys.exit(1) h5timeseries = h5py.File(timeSeriesFile, 'r') k = h5timeseries.keys() # read h5 file and its group type if not 'timeseries' in k: print 'ERROR: Input file is ' + k[ 0] + '.\n\tOnly timeseries is supported.\n' sys.exit(1) atr = readfile.read_attribute(timeSeriesFile) dateList1 = sorted(h5timeseries['timeseries'].keys()) dates1, datevector1 = ptime.date_list2vector(dateList1) print '\n************ Time Series Display - Point *************' ##### Select Check try: lalosub xsub = subset.coord_geo2radar([lalosub[1]], atr, 'longitude') ysub = subset.coord_geo2radar([lalosub[0]], atr, 'latitude') xsub = [xsub] ysub = [ysub] if radius == 0: radius = 3 except: pass try: ref_lalosub ref_xsub = subset.coord_geo2radar([ref_lalosub[1]], atr, 'longitude') ref_ysub = subset.coord_geo2radar([ref_lalosub[0]], atr, 'latitude') ref_xsub = [ref_xsub] ref_ysub = [ref_ysub] if radius == 0: radius = 3 except: pass ############################################################## global dates, dateList, datevector_all, dateListMinMax print '*******************' print 'All dates existed:' print dateList1 print '*******************' ## Check exclude date input try: datesNot2show if os.path.isfile(datesNot2show[0]): try: datesNot2show = ptime.read_date_list(datesNot2show[0]) except: print 'Can not read date list file: ' + datesNot2show[0] print 'dates not to show: ' + str(datesNot2show) except: datesNot2show = [] ## Check Min / Max Date dateListMinMax = [] try: minDate minDate = ptime.yyyymmdd(minDate) dateListMinMax.append(minDate) minDateyy = ptime.yyyymmdd2years(minDate) print 'minimum date: ' + minDate for date in dateList1: yy = ptime.yyyymmdd2years(date) if yy < minDateyy: datesNot2show.append(date) except: pass try: maxDate maxDate = ptime.yyyymmdd(maxDate) dateListMinMax.append(maxDate) maxDateyy = ptime.yyyymmdd2years(maxDate) print 'maximum date: ' + maxDate for date in dateList1: yy = ptime.yyyymmdd2years(date) if yy > maxDateyy: datesNot2show.append(date) except: pass dateListMinMax = sorted(dateListMinMax) if not dateListMinMax: print 'no min/max date input.' else: datesMinMax, dateVecMinMax = ptime.date_list2vector(dateListMinMax) ## Finalize Date List try: dateList = [] for date in dateList1: if date not in datesNot2show: dateList.append(date) print '--------------------------------------------' print 'dates used to show time series displacements:' print dateList print '--------------------------------------------' except: dateList = dateList1 print 'using all dates to show time series displacement' ## Read Date Info (x axis for time series display) dates, datevector = ptime.date_list2vector(dateList) datevector_all = list(datevector) ## Check reference date input try: ref_date if not ref_date in dateList: print 'Reference date - ' + ref_date + ' - is not included in date list to show.' sys.exit(1) else: print 'reference date: ' + ref_date except: if zero_start == 'yes': ref_date = dateList[0] print 'set the 1st date as reference for displacement display.' else: pass ############################################################## ##### Plot Fig 1 - Velocity / last epoch of time series / DEM fig = plt.figure(1) ax = fig.add_subplot(111) ##### Check subset range width = int(atr['WIDTH']) length = int(atr['FILE_LENGTH']) print 'file size: ' + str(length) + ', ' + str(width) try: win_y = subset.coord_geo2radar(win_lat, atr, 'latitude') except: try: win_y except: win_y = [0, length] try: win_x = subset.coord_geo2radar(win_lon, atr, 'longitude') except: try: win_x except: win_x = [0, width] win_box = (win_x[0], win_y[0], win_x[1], win_y[1]) win_box = subset.check_box_within_data_coverage(win_box, atr) try: velocityFile try: vel, vel_atr = readfile.read(velocityFile) except: vel, vel_atr = readfile.read(timeSeriesFile, velocityFile) ax.set_title(velocityFile) print 'display: ' + velocityFile except: vel, vel_atr = readfile.read(timeSeriesFile, dateList1[-1]) ax.set_title('epoch: ' + dateList1[-1]) print 'display last epoch' ##### show displacement instead of phase if vel_atr['FILE_TYPE'] in ['interferograms', '.unw' ] and dispDisplacement == 'yes': print 'show displacement' phase2range = -float(vel_atr['WAVELENGTH']) / (4 * np.pi) vel *= phase2range else: dispDisplacement = 'no' ## Reference Point if showRef == 'yes': try: ax.plot(int(atr['ref_x']), int(atr['ref_y']), 'ks', ms=6) except: pass if dispOpposite == 'yes': print 'show opposite value in figure/map 1' vel *= -1 ## Flip try: flip_lr except: try: flip_ud except: flip_lr, flip_ud = view.auto_flip_direction(atr) ## Status bar ## Geo coordinate try: ullon = float(atr['X_FIRST']) ullat = float(atr['Y_FIRST']) lon_step = float(atr['X_STEP']) lat_step = float(atr['Y_STEP']) lon_unit = atr['Y_UNIT'] lat_unit = atr['X_UNIT'] geocoord = 'yes' print 'Input file is Geocoded' except: geocoord = 'no' def format_coord(x, y): col = int(x + 0.5) row = int(y + 0.5) if col >= 0 and col <= width and row >= 0 and row <= length: z = vel[row, col] try: lon = ullon + x * lon_step lat = ullat + y * lat_step return 'x=%.1f, y=%.1f, value=%.4f, lon=%.4f, lat=%.4f' % ( x, y, z, lon, lat) except: return 'x=%.1f, y=%.1f, value=%.4f' % (x, y, z) ax.format_coord = format_coord ## DEM try: demFile dem, demRsc = readfile.read(demFile) ax = view.plot_dem_yx(ax, dem, demShade, demContour, contour_step, contour_sigma) vel_alpha = 0.8 except: print 'No DEM file' try: img = ax.imshow(vel, vmin=vmin, vmax=vmax, alpha=vel_alpha) except: img = ax.imshow(vel, alpha=vel_alpha) plt.colorbar(img) ## Zoom In (subset) if flip_lr == 'yes': ax.set_xlim(win_box[2], win_box[0]) else: ax.set_xlim(win_box[0], win_box[2]) if flip_ud == 'yes': ax.set_ylim(win_box[1], win_box[3]) else: ax.set_ylim(win_box[3], win_box[1]) ## Flip #if flip_lr == 'yes': fig.gca().invert_xaxis() #if flip_ud == 'yes': fig.gca().invert_yaxis() ########################################## ##### Plot Fig 2 - Time series plot #fig2 = plt.figure(num=2,figsize=(12,6)) fig2 = plt.figure(2, figsize=(12, 6)) ax2 = fig2.add_subplot(111) try: timeSeriesFile_2 h5timeseries_2 = h5py.File(timeSeriesFile_2) dateList_2 = sorted(h5timeseries_2['timeseries'].keys()) dates_2, datevector_2 = ptime.date_list2vector(dateList_2) datevector_all += list(set(datevector_2) - set(datevector_all)) datevector_all = sorted(datevector_all) except: pass ################################ Plot Code Package <start> ################################# def plot_ts(ax, ax2, fig2, xsub, ysub, h5timeseries): ax2.cla() print '\n-------------------------------------------------------------------------------' disp_min = 0 disp_max = 0 ############################# Plot Time Series ############################## global ref_xsub, ref_ysub ##### 1.1 Plot Reference time series try: ref_xsub ref_ysub ref_xsub, ref_ysub = check_yx(ref_xsub, ref_ysub, radius, ax, rectColor) print '----------------------------------------------------' print 'Reference Point:' print 'ref_x=' + str(ref_xsub[0]) + ':' + str(ref_xsub[1]) print 'ref_y=' + str(ref_ysub[0]) + ':' + str(ref_ysub[1]) print '-----------------------------' print 'Time series with all dates:' dis1, dis1_mean, dis1_std, dis1_vel = read_dis_xy( ref_xsub, ref_ysub, dateList1, h5timeseries, unit) (_, caps, _)=ax2.errorbar(dates1,dis1_mean,yerr=dis1_std,fmt='-ks',\ ms=markerSize2, lw=0, alpha=1,mfc=markerColor_ref,mew=edgeWidth,\ elinewidth=edgeWidth,ecolor='black',capsize=markerSize*0.5) for cap in caps: cap.set_markeredgewidth(edgeWidth) disp_min, disp_max = update_lim(disp_min, disp_max, dis1_mean, dis1_std) if not len(dateList) == len(dateList1): print '-----------------------------' print 'Time series with dates of interest:' dis12, dis12_mean, dis12_std, dis12_vel = read_dis_xy( ref_xsub, ref_ysub, dateList, h5timeseries, unit) (_, caps, _)=ax2.errorbar(dates,dis12_mean,yerr=dis12_std,fmt='-ks',\ ms=markerSize2, lw=0, alpha=1,mfc=markerColor_ref2,mew=edgeWidth,\ elinewidth=edgeWidth,ecolor='black',capsize=markerSize*0.5) for cap in caps: cap.set_markeredgewidth(edgeWidth) disp_min, disp_max = update_lim(disp_min, disp_max, dis12_mean, dis12_std) except: pass ##### 1.2.0 Read y/x print '\n----------------------------------------------------' print 'Point of Interest:' xsub, ysub = check_yx(xsub, ysub, radius, ax, rectColor) print 'x=' + str(xsub[0]) + ':' + str(xsub[1]) print 'y=' + str(ysub[0]) + ':' + str(ysub[1]) ##### 1.2.1 Plot 2nd time series try: timeSeriesFile_2 print '-----------------------------' print '2nd Time Series:' dis2, dis2_mean, dis2_std, dis2_vel = read_dis_xy( xsub, ysub, dateList_2, h5timeseries_2, unit) (_, caps, _)=ax2.errorbar(dates_2,dis2_mean,yerr=dis2_std,fmt='-ko',\ ms=markerSize2, lw=0, alpha=1, mfc=markerColor2,\ elinewidth=0,ecolor='black',capsize=0) for cap in caps: cap.set_markeredgewidth(edgeWidth) disp_min, disp_max = update_lim(disp_min, disp_max, dis2_mean, dis2_std) except: pass ##### 1.2.2 Plot 1st time series print '-----------------------------' print 'Time Series:' dis, dis_mean, dis_std, dis_vel = read_dis_xy(xsub, ysub, dateList, h5timeseries, unit) (_, caps, _)=ax2.errorbar(dates,dis_mean,yerr=dis_std,fmt='-ko',\ ms=markerSize, lw=lineWidth, alpha=1, mfc=markerColor,\ elinewidth=edgeWidth,ecolor='black',capsize=markerSize*0.5) for cap in caps: cap.set_markeredgewidth(edgeWidth) disp_min, disp_max = update_lim(disp_min, disp_max, dis_mean, dis_std) ####################### Figure Format ####################### ## x axis format try: ax2 = ptime.auto_adjust_xaxis_date(ax2, dateVecMinMax, fontSize) except: ax2 = ptime.auto_adjust_xaxis_date(ax2, datevector_all, fontSize) ## y axis format ax2.set_ylabel('Displacement [' + unit + ']', fontsize=fontSize) try: lbound hbound ax2.set_ylim(lbound, hbound) except: disp_buf = 0.2 * (disp_max - disp_min) ax2.set_ylim(disp_min - disp_buf, disp_max + disp_buf) for tick in ax2.yaxis.get_major_ticks(): tick.label.set_fontsize(fontSize) ## title figTitle = 'x=' + str(xsub[0]) + ':' + str(xsub[1]) + ', y=' + str( ysub[0]) + ':' + str(ysub[1]) try: lonc = ullon + (xsub[0] + xsub[1]) / 2.0 * lon_step latc = ullat + (ysub[0] + ysub[1]) / 2.0 * lat_step figTitle += ', lalo=' + '%.4f,%.4f' % (latc, lonc) except: pass ax2.set_title(figTitle) ################## Save and Output ##################### if saveFig == 'yes': print '-----------------------------' Delay = {} Delay['displacement'] = dis Delay['unit'] = unit Delay['time'] = datevector Delay['velocity'] = dis_vel[0] Delay['velocity_unit'] = unit + '/yr' Delay['velocity_std'] = dis_vel[4] figBase = 'x' + str(xsub[0]) + '_' + str(xsub[1] - 1) + 'y' + str( ysub[0]) + '_' + str(ysub[1] - 1) sio.savemat(figBase + '_ts.mat', {'displacement': Delay}) print 'saved ' + figBase + '_ts.mat' fig2.savefig(figBase + '_ts.pdf', bbox_inches='tight', transparent=True, dpi=fig_dpi) print 'saved ' + figBase + '_ts.pdf' if dispFig == 'no': fig.savefig(figBase + '_vel.png', bbox_inches='tight', transparent=True, dpi=fig_dpi) print 'saved ' + figBase + '_vel.png' ################################ Plot Code Package <end> ################################# ########### 1. Plot Time Series with x/y ########## try: xsub ysub plot_ts(ax, ax2, fig2, xsub, ysub, h5timeseries) except: print 'No x/y input' pass ########### 2. Plot Time Series with Click ########## ## similar to 1. Plot Time Series with x/y def onclick(event): ax2.cla() xsub = [int(event.xdata)] ysub = [int(event.ydata)] plot_ts(ax, ax2, fig2, xsub, ysub, h5timeseries) if dispFig == 'yes': plt.show() try: cid = fig.canvas.mpl_connect('button_press_event', onclick) except: pass if dispFig == 'yes': plt.show()
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
def main(argv): ##### Default fontSize = 12 lineWidth = 2 markerColor = 'crimson' markerSize = 16 disp_fig = 'no' save_fig = 'yes' save_list = 'yes' ref_file = 'reference_date.txt' drop_file = 'drop_date.txt' ##### Check Inputs if len(sys.argv)>3: try: opts, args = getopt.getopt(argv,'h:f:m:o:x:y:',['help','circle=']) except getopt.GetoptError: print 'Error in reading input options!'; Usage() ; sys.exit(1) for opt,arg in opts: if opt in ("-h","--help"): Usage() ; sys.exit() elif opt == '-f': File = arg elif opt == '-m': maskFile = arg elif opt == '-x': xsub = [int(i) for i in arg.split(':')]; xsub.sort() elif opt == '-y': ysub = [int(i) for i in arg.split(':')]; ysub.sort() elif opt == '--circle' : cir_par = [i for i in arg.split(';')] #elif opt == '-o': outName = arg else: try: File = argv[0] except: Usage(); sys.exit(1) try: maskFile = argv[1] except: pass try: atr = readfile.read_attributes(File) except: Usage(); sys.exit(1) ext = os.path.splitext(File)[1].lower() FileBase = os.path.basename(File).split(ext)[0] outNameBase = 'spatialMean_'+FileBase print '\n*************** Spatial Average ******************' ##### Input File Info k = atr['FILE_TYPE'] print 'Input file is '+k width = int(atr['WIDTH']) length = int(atr['FILE_LENGTH']) h5file = h5py.File(File) epochList = h5file[k].keys(); epochList = sorted(epochList) epochNum = len(epochList) print 'number of epoch: '+str(epochNum) dates,datevector = ptime.date_list2vector(epochList) ##### Mask Info try: Mask_orig,Matr = readfile.read(maskFile) print 'mask file: '+maskFile Masking = 'yes' except: print 'No mask. Use the whole area for ramp estimation.' Masking = 'no' Mask_orig=np.ones((length,width)) Mask = np.zeros((length,width)) Mask[:] = Mask_orig[:] ## Bounding Subset try: xsub ysub ysub,xsub = subset.check_subset_range(ysub,xsub,atr) Mask[ysub[0]:ysub[1],xsub[0]:xsub[1]] = Mask_orig[ysub[0]:ysub[1],xsub[0]:xsub[1]]*2 #Mask[0:ysub[0],:] = 0 #Mask[ysub[1]:length,:] = 0 #Mask[:,0:xsub[0]] = 0 #Mask[:,xsub[1]:width] = 0 except: Mask = Mask_orig*2 print 'No subset input.' ## Circle Inputs try: cir_par for i in range(len(cir_par)): cir_idx = circle_index(atr,cir_par[i]) Mask[cir_idx] = Mask_orig[cir_idx] print 'Circle '+str(i)+': '+cir_par[i] except: print 'No circle of interest input.' ## Mask output idx = Mask == 2 idxNum = float(sum(sum(idx))) fig = plt.figure() plt.imshow(Mask,cmap='gray') plt.savefig(outNameBase+'_mask.png',bbox_inches='tight') print 'save mask to '+outNameBase+'_mask.png' #fig.clf() ##### Calculation meanList = np.zeros(epochNum) pixPercent = np.zeros(epochNum) pixT = 0.7 print 'calculating ...' print ' Date Mean Percentage' for i in range(epochNum): epoch = epochList[i] d = h5file[k].get(epoch)[:] #d[Mask==0] = np.nan meanList[i] = np.nanmean(d[idx]) pixPercent[i] = np.sum(d[idx] >= pixT)/idxNum print epoch+' : %.2f %.1f%%'%(meanList[i],pixPercent[i]*100) del d h5file.close() ##### Reference date - Max Value top3 = sorted(zip(meanList,epochList), reverse=True)[:3] print '------------ Top 3 Mean ------------------' print top3 ## Write to txt file fref = open(ref_file,'w') fref.write(str(top3[0][1])+'\n') fref.close() print 'write optimal reference date to '+ref_file idxMean = meanList == np.nanmax(meanList) ##### Drop dates - mean threshold #meanT = 0.7 #idxMean = meanList < meanT #print '------------ Mean Value < '+str(meanT)+' --------' #print np.array(epochList)[idxMean] #print meanList[idxMean] ##### Drop dates - good pixel percentage pixNumT = 0.7 print '------------ Good Pixel Percentage < %.0f%% -------'%(pixNumT*100) idxPix = pixPercent < pixNumT dropEpochList = np.array(epochList)[idxPix] print dropEpochList print pixPercent[idxPix] ## Write to txt file fdrop = open(drop_file,'w') for i in range(len(dropEpochList)): fdrop.write(str(dropEpochList[i])+'\n') fdrop.close() print 'write drop dates to '+drop_file print '-------------------------------------------' ##### Display fig = plt.figure(figsize=(12,12)) ax = fig.add_subplot(211) ax.plot(dates, meanList, '-ko', ms=markerSize, lw=lineWidth, alpha=0.7, mfc=markerColor) #ax.plot([dates[0],dates[-1]],[meanT,meanT], '--b', lw=lineWidth) #sc = ax.scatter(dates, np.tile(0.5,epochNum), c=meanList, s=22**2, alpha=0.3, vmin=0.0, vmax=1.0) #ax.scatter(np.array(dates)[idxMean], 0.5, c=meanList[idxMean], s=22**2, alpha=1.0, vmin=0.0, vmax=1.0) ax = ptime.adjust_xaxis_date(ax,datevector) ax.set_ylim(0,1) ax.set_title('Spatial Average Value', fontsize=fontSize) ax.set_xlabel('Time [years]', fontsize=fontSize) #cbar = plt.colorbar(sc) #cbar.set_label('Spatial Mean of Normalized Sum Epochs') ax = fig.add_subplot(212) ax.plot(dates, pixPercent, '-ko', ms=markerSize, lw=lineWidth, alpha=0.7, mfc=markerColor) ax.plot([dates[0],dates[-1]],[pixNumT,pixNumT], '--b', lw=lineWidth) ax = ptime.adjust_xaxis_date(ax,datevector) ax.set_ylim(0,1) ax.set_title('Percenrage of Pixels with Value > '+str(pixNumT), fontsize=fontSize) ax.set_xlabel('Time [years]', fontsize=fontSize) vals = ax.get_yticks() ax.set_yticklabels(['{:3.0f}%'.format(i*100) for i in vals]) if save_fig == 'yes': plt.savefig(outNameBase+'.png',bbox_inches='tight') print 'save figure to '+outNameBase+'.png' if disp_fig == 'yes': plt.show() ##### Output if save_list == 'yes': epochList6 = ptime.yymmdd(epochList) fl = open(outNameBase+'.txt','w') for i in range(epochNum): str_line = epochList6[i]+' %.2f %.2f\n'%(meanList[i],pixPercent[i]) fl.write(str_line) fl.close() print 'write data to '+outNameBase+'.txt\n'