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 update_timeseries(y, x): '''Plot point time series displacement at pixel [y, x]''' global fig_ts, ax_ts, inps d_ts = [] for date in dateList: d = h5[k].get(date)[y, x] if inps.ref_yx: d -= h5[k].get(date)[inps.ref_yx[0], inps.ref_yx[1]] d_ts.append(d * inps.unit_fac) if inps.zero_first: d_ts -= d_ts[0] ax_ts.cla() if inps.error_file: ax_ts = plot_timeseries_errorbar(ax_ts, d_ts, inps) else: ax_ts = plot_timeseries_scatter(ax_ts, d_ts, inps) ax_ts.set_ylim(inps.ylim) # Title title_ts = 'Y = %d, X = %d' % (y, x) try: lat = ullat + y * lat_step lon = ullon + x * lon_step title_ts += ', lat = %.4f, lon = %.4f' % (lat, lon) except: pass if inps.disp_title: ax_ts.set_title(title_ts) ax_ts = ptime.auto_adjust_xaxis_date(ax_ts, tims, fontSize=inps.font_size)[0] ax_ts.set_xlabel('Time', fontsize=inps.font_size) ax_ts.set_ylabel('Displacement [%s]' % inps.disp_unit, fontsize=inps.font_size) fig_ts.canvas.draw() # Print to terminal print '\n---------------------------------------' print title_ts print d_ts return d_ts
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): ##### 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'
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): 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 update_timeseries(y, x): '''Plot point time series displacement at pixel [y, x]''' global fig_ts, ax_ts, inps d_ts = [] for date in dateList: d = h5[k].get(date)[y, x] if inps.ref_yx: d -= h5[k].get(date)[inps.ref_yx[0], inps.ref_yx[1]] d_ts.append(d * inps.unit_fac) if inps.zero_first: d_ts -= d_ts[inps.zero_idx] ax_ts.cla() if inps.error_file: ax_ts = plot_timeseries_errorbar(ax_ts, d_ts, inps) else: ax_ts = plot_timeseries_scatter(ax_ts, d_ts, inps) ax_ts.set_ylim(inps.ylim) for tick in ax_ts.yaxis.get_major_ticks(): tick.label.set_fontsize(inps.font_size) # Title title_ts = 'Y = %d, X = %d' % (y, x) try: lat = ullat + y * lat_step lon = ullon + x * lon_step title_ts += ', lat = %.4f, lon = %.4f' % (lat, lon) except: pass if inps.disp_title: ax_ts.set_title(title_ts) ax_ts = ptime.auto_adjust_xaxis_date(ax_ts, tims, fontSize=inps.font_size)[0] ax_ts.set_xlabel('Time', fontsize=inps.font_size) ax_ts.set_ylabel('Displacement [%s]' % inps.disp_unit, fontsize=inps.font_size) fig_ts.canvas.draw() # Print to terminal print '\n---------------------------------------' print title_ts print d_ts # Slope estimation if inps.ex_date_list: tims_kept = [ tims[i] for i in range(date_num) if i not in inps.ex_idx_list ] d_ts_kept = [ d_ts[i] for i in range(date_num) if i not in inps.ex_idx_list ] d_slope = stats.linregress(np.array(tims_kept), np.array(d_ts_kept)) else: d_slope = stats.linregress(np.array(tims), np.array(d_ts)) print 'linear velocity: %.2f +/- %.2f [%s/yr]' % ( d_slope[0], d_slope[4], inps.disp_unit) return d_ts
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'