def main(iargs=None): inps = cmd_line_parse(iargs) print('\n*************** Spatial Average ******************') mean_list, date_list = ut.spatial_average(inps.file, datasetName=inps.datasetName, maskFile=inps.mask_file, saveList=True) atr = readfile.read_attribute(inps.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, '-o')#, lw=2, ms=16, alpha=0.7) #, mfc='crimson') ax.set_title('Spatial Average', fontsize=12) ax = pp.auto_adjust_xaxis_date(ax, datevector)[0] ax.set_xlabel('Time [years]', fontsize=12) ax.set_ylabel('Mean', fontsize=12) plt.show() return
def read_network_info(inps): ext = os.path.splitext(inps.file)[1] # 1. Read dateList and pbaseList if ext in ['.h5', '.he5']: k = readfile.read_attribute(inps.file)['FILE_TYPE'] print('reading temporal/spatial baselines from {} file: {}'.format( k, inps.file)) if k == 'ifgramStack': inps.dateList = ifgramStack( inps.file).get_date_list(dropIfgram=False) inps.pbaseList = ifgramStack( inps.file).get_perp_baseline_timeseries(dropIfgram=False) elif k == 'timeseries': obj = timeseries(inps.file) obj.open(print_msg=False) inps.dateList = obj.dateList inps.pbaseList = obj.pbase else: raise ValueError( 'input file is not ifgramStack/timeseries, can not read temporal/spatial baseline info.' ) else: print('reading temporal/spatial baselines from list file: ' + inps.bl_list_file) inps.dateList, inps.pbaseList = pnet.read_baseline_file( inps.bl_list_file)[0:2] print('number of acquisitions: {}'.format(len(inps.dateList))) # 2. Read All Date12/Ifgrams/Pairs inps.date12List = pnet.get_date12_list(inps.file) print('reading interferograms info from file: {}'.format(inps.file)) print('number of interferograms: {}'.format(len(inps.date12List))) if inps.save_list: txtFile = os.path.splitext(os.path.basename( inps.file))[0] + '_date12List.txt' np.savetxt(txtFile, inps.date12List, fmt='%s') print('save pairs/date12 info to file: ' + txtFile) # Optional: Read dropped date12 / date inps.dateList_drop = [] inps.date12List_drop = [] if ext in ['.h5', '.he5'] and k == 'ifgramStack': inps.date12List_keep = ifgramStack( inps.file).get_date12_list(dropIfgram=True) inps.date12List_drop = sorted( list(set(inps.date12List) - set(inps.date12List_keep))) print('-' * 50) print('number of interferograms marked as drop: {}'.format( len(inps.date12List_drop))) print('number of interferograms marked as keep: {}'.format( len(inps.date12List_keep))) mDates = [i.split('_')[0] for i in inps.date12List_keep] sDates = [i.split('_')[1] for i in inps.date12List_keep] inps.dateList_keep = sorted(list(set(mDates + sDates))) inps.dateList_drop = sorted( list(set(inps.dateList) - set(inps.dateList_keep))) print('number of acquisitions marked as drop: {}'.format( len(inps.dateList_drop))) # Optional: Read Coherence List inps.cohList = None if ext in ['.h5', '.he5'] and k == 'ifgramStack': inps.cohList, cohDate12List = ut.spatial_average( inps.file, datasetName='coherence', maskFile=inps.maskFile, saveList=True, checkAoi=False) if all(np.isnan(inps.cohList)): inps.cohList = None print( 'WARNING: all coherence value are nan! Do not use this and continue.' ) if set(cohDate12List) > set(inps.date12List): print('extract coherence value for all pair/date12 in input file') inps.cohList = [ inps.cohList[cohDate12List.index(i)] for i in inps.date12List ] elif set(cohDate12List) < set(inps.date12List): inps.cohList = None print( 'WARNING: not every pair/date12 from input file is in coherence file' ) print( 'turn off the color plotting of interferograms based on coherence' ) return inps
def get_date12_to_drop(inps): """Get date12 list to dropped Return [] if no ifgram to drop, thus KEEP ALL ifgrams; None if nothing to change, exit without doing anything. """ obj = ifgramStack(inps.file) obj.open() date12ListAll = obj.date12List dateList = obj.dateList print('number of interferograms: {}'.format(len(date12ListAll))) # Get date12_to_drop date12_to_drop = [] # reference file if inps.referenceFile: date12_to_keep = ifgramStack(inps.referenceFile).get_date12_list(dropIfgram=True) print('--------------------------------------------------') print('use reference pairs info from file: {}'.format(inps.referenceFile)) print('number of interferograms in reference: {}'.format(len(date12_to_keep))) tempList = sorted(list(set(date12ListAll) - set(date12_to_keep))) date12_to_drop += tempList print('date12 not in reference file: ({})\n{}'.format(len(tempList), tempList)) # coherence file if inps.coherenceBased: print('--------------------------------------------------') print('use coherence-based network modification') coord = ut.coordinate(obj.metadata, lookup_file=inps.lookupFile) if inps.aoi_geo_box and inps.lookupFile: print('input AOI in (lon0, lat1, lon1, lat0): {}'.format(inps.aoi_geo_box)) inps.aoi_pix_box = coord.bbox_geo2radar(inps.aoi_geo_box) if inps.aoi_pix_box: inps.aoi_pix_box = coord.check_box_within_data_coverage(inps.aoi_pix_box) print('input AOI in (x0,y0,x1,y1): {}'.format(inps.aoi_pix_box)) # Calculate spatial average coherence cohList = ut.spatial_average(inps.file, datasetName='coherence', maskFile=inps.maskFile, box=inps.aoi_pix_box, saveList=True)[0] coh_date12_list = list(np.array(date12ListAll)[np.array(cohList) >= inps.minCoherence]) # MST network if inps.keepMinSpanTree: print('Get minimum spanning tree (MST) of interferograms with inverse of coherence.') msg = ('Drop ifgrams with ' '1) average coherence < {} AND ' '2) not in MST network: '.format(inps.minCoherence)) mst_date12_list = pnet.threshold_coherence_based_mst(date12ListAll, cohList) mst_date12_list = ptime.yyyymmdd_date12(mst_date12_list) else: msg = 'Drop ifgrams with average coherence < {}: '.format(inps.minCoherence) mst_date12_list = [] tempList = sorted(list(set(date12ListAll) - set(coh_date12_list + mst_date12_list))) date12_to_drop += tempList msg += '({})'.format(len(tempList)) if len(tempList) <= 200: msg += '\n{}'.format(tempList) print(msg) # temp baseline threshold if inps.tempBaseMax: tempIndex = np.abs(obj.tbaseIfgram) > inps.tempBaseMax tempList = list(np.array(date12ListAll)[tempIndex]) date12_to_drop += tempList print('--------------------------------------------------') print('Drop ifgrams with temporal baseline > {} days: ({})\n{}'.format( inps.tempBaseMax, len(tempList), tempList)) # perp baseline threshold if inps.perpBaseMax: tempIndex = np.abs(obj.pbaseIfgram) > inps.perpBaseMax tempList = list(np.array(date12ListAll)[tempIndex]) date12_to_drop += tempList print('--------------------------------------------------') print('Drop ifgrams with perp baseline > {} meters: ({})\n{}'.format( inps.perpBaseMax, len(tempList), tempList)) # connection number threshold if inps.connNumMax: seq_date12_list = pnet.select_pairs_sequential(dateList, inps.connNumMax) seq_date12_list = ptime.yyyymmdd_date12(seq_date12_list) tempList = [i for i in date12ListAll if i not in seq_date12_list] date12_to_drop += tempList print('--------------------------------------------------') msg = 'Drop ifgrams with temporal baseline beyond {} neighbors: ({})'.format( inps.connNumMax, len(tempList)) if len(tempList) <= 200: msg += '\n{}'.format(tempList) print(msg) # excludeIfgIndex if inps.excludeIfgIndex: tempList = [date12ListAll[i] for i in inps.excludeIfgIndex] date12_to_drop += tempList print('--------------------------------------------------') print('Drop ifgrams with the following index number: {}'.format(len(tempList))) for i in range(len(tempList)): print('{} : {}'.format(i, tempList[i])) #len(tempList), zip(inps.excludeIfgIndex, tempList))) # excludeDate if inps.excludeDate: tempList = [i for i in date12ListAll if any(j in inps.excludeDate for j in i.split('_'))] date12_to_drop += tempList print('-'*50+'\nDrop ifgrams including the following dates: ({})\n{}'.format( len(tempList), inps.excludeDate)) print('-'*30+'\n{}'.format(tempList)) # startDate if inps.startDate: minDate = int(inps.startDate) tempList = [i for i in date12ListAll if any(int(j) < minDate for j in i.split('_'))] date12_to_drop += tempList print('--------------------------------------------------') print('Drop ifgrams with date earlier than: {} ({})\n{}'.format( inps.startDate, len(tempList), tempList)) # endDate if inps.endDate: maxDate = int(inps.endDate) tempList = [i for i in date12ListAll if any(int(j) > maxDate for j in i.split('_'))] date12_to_drop += tempList print('--------------------------------------------------') print('Drop ifgrams with date later than: {} ({})\n{}'.format( inps.endDate, len(tempList), tempList)) # Manually drop pairs if inps.manual: tempList = manual_select_pairs_to_remove(inps.file) if tempList is None: return None tempList = [i for i in tempList if i in date12ListAll] print('date12 selected to remove: ({})\n{}'.format(len(tempList), tempList)) date12_to_drop += tempList # drop duplicate date12 and sort in order date12_to_drop = sorted(list(set(date12_to_drop))) date12_to_keep = sorted(list(set(date12ListAll) - set(date12_to_drop))) print('--------------------------------------------------') print('number of interferograms to remove: {}'.format(len(date12_to_drop))) print('number of interferograms to keep : {}'.format(len(date12_to_keep))) date_to_keep = [d for date12 in date12_to_keep for d in date12.split('_')] date_to_keep = sorted(list(set(date_to_keep))) date_to_drop = sorted(list(set(dateList) - set(date_to_keep))) if len(date_to_drop) > 0: print('number of acquisitions to remove: {}\n{}'.format(len(date_to_drop), date_to_drop)) date12ListKept = obj.get_date12_list(dropIfgram=True) date12ListDropped = sorted(list(set(date12ListAll) - set(date12ListKept))) if date12_to_drop == date12ListDropped: print('Calculated date12 to drop is the same as exsiting marked input file, skip updating file.') date12_to_drop = None elif date12_to_drop == date12ListAll: raise Exception('Zero interferogram left! Please adjust your setting and try again.') return date12_to_drop
def get_date12_to_drop(inps): """Get date12 list to dropped Return [] if no ifgram to drop, thus KEEP ALL ifgrams; None if nothing to change, exit without doing anything. """ obj = ifgramStack(inps.file) obj.open() date12ListAll = obj.date12List print('number of interferograms: {}'.format(len(date12ListAll))) # Get date12_to_drop date12_to_drop = [] # 1. Update date12_to_drop from reference file if inps.referenceFile: date12_to_keep = ifgramStack( inps.referenceFile).get_date12_list(dropIfgram=True) print('--------------------------------------------------') print('use reference pairs info from file: {}'.format( inps.referenceFile)) print('number of interferograms in reference: {}'.format( len(date12_to_keep))) tempList = sorted(list(set(date12ListAll) - set(date12_to_keep))) date12_to_drop += tempList print('date12 not in reference file: ({})\n{}'.format( len(tempList), tempList)) # 2.1 Update date12_to_drop from coherence file if inps.coherenceBased: print('--------------------------------------------------') print('use coherence-based network modification') if inps.aoi_geo_box and inps.lookupFile: print('input AOI in (lon0, lat1, lon1, lat0): {}'.format( inps.aoi_geo_box)) inps.aoi_pix_box = subset.bbox_geo2radar(inps.aoi_geo_box, obj.metadata, inps.lookupFile) if inps.aoi_pix_box: inps.aoi_pix_box = subset.check_box_within_data_coverage( inps.aoi_pix_box, obj.metadata) print('input AOI in (x0,y0,x1,y1): {}'.format(inps.aoi_pix_box)) # Calculate spatial average coherence cohList = ut.spatial_average(inps.file, datasetName='coherence', maskFile=inps.maskFile, box=inps.aoi_pix_box, saveList=True)[0] coh_date12_list = list( np.array(date12ListAll)[np.array(cohList) >= inps.minCoherence]) # MST network if inps.keepMinSpanTree: print( 'Get minimum spanning tree (MST) of interferograms with inverse of coherence.' ) msg = 'Drop ifgrams with 1) average coherence < {} AND 2) not in MST network: '.format( inps.minCoherence) mst_date12_list = pnet.threshold_coherence_based_mst( date12ListAll, cohList) else: msg = 'Drop ifgrams with average coherence < {}: '.format( inps.minCoherence) mst_date12_list = [] tempList = [ i for i in date12ListAll if (i in coh_date12_list and i not in mst_date12_list) ] #tempList = sorted(list(set(date12ListAll) - set(coh_date12_list) - set(mst_date12_list))) date12_to_drop += tempList print(msg + '({})\n{}'.format(len(tempList), tempList)) # 2.2 Update date12_to_drop from temp baseline threshold if inps.tempBaseMax: tempIndex = np.abs(obj.tbaseIfgram) > inps.tempBaseMax tempList = list(np.array(date12ListAll)[tempIndex]) date12_to_drop += tempList print('--------------------------------------------------') print('Drop ifgrams with temporal baseline > {} days: ({})\n{}'.format( inps.tempBaseMax, len(tempList), tempList)) # 2.3 Update date12_to_drop from perp baseline threshold if inps.perpBaseMax: tempIndex = np.abs(obj.pbaseIfgram) > inps.perpBaseMax tempList = list(np.array(date12ListAll)[tempIndex]) date12_to_drop += tempList print('--------------------------------------------------') print('Drop ifgrams with perp baseline > {} meters: ({})\n{}'.format( inps.perpBaseMax, len(tempList), tempList)) # 2.4 Update date12_to_drop from excludeIfgIndex if inps.excludeIfgIndex: tempList = [date12ListAll[i] for i in inps.excludeIfgIndex] date12_to_drop += tempList print('--------------------------------------------------') print('Drop ifgrams with the following index number: {}\n{}'.format( len(tempList), zip(inps.excludeIfgIndex, tempList))) # 2.5 Update date12_to_drop from excludeDate if inps.excludeDate: tempList = [ i for i in date12ListAll if any(j in inps.excludeDate for j in i.split('_')) ] date12_to_drop += tempList print('-' * 50 + '\nDrop ifgrams including the following dates:\n{}'.format( inps.excludeDate)) print('-' * 20 + 'Ifgrams dropped: ({})\n{}'.format(len(templist), tempList)) # 2.6 Update date12_to_drop from startDate if inps.startDate: minDate = int(inps.startDate) tempList = [ i for i in date12ListAll if any( int(j) < minDate for j in i.split('_')) ] date12_to_drop += tempList print('--------------------------------------------------') print('Drop ifgrams with date earlier than: {} ({})\n{}'.format( inps.startDate, len(tempList), tempList)) # 2.7 Update date12_to_drop from endDate if inps.endDate: maxDate = int(inps.endDate) tempList = [ i for i in date12ListAll if any( int(j) > maxDate for j in i.split('_')) ] date12_to_drop += tempList print('--------------------------------------------------') print('Drop ifgrams with date later than: {} ({})\n{}'.format( inps.endDate, len(tempList), tempList)) # 3. Manually drop pairs if inps.manual: tempList = manual_select_pairs_to_remove(inps.file) if tempList is None: return None tempList = [i for i in tempList if i in date12ListAll] print('date12 selected to remove: ({})\n{}'.format( len(tempList), tempList)) date12_to_drop += tempList # 4. drop duplicate date12 and sort in order date12_to_drop = sorted(list(set(date12_to_drop))) date12_to_keep = sorted(list(set(date12ListAll) - set(date12_to_drop))) print('--------------------------------------------------') print('number of interferograms to remove: {}'.format(len(date12_to_drop))) print('number of interferograms to keep : {}'.format(len(date12_to_keep))) date12ListKept = obj.get_date12_list(dropIfgram=True) date12ListDropped = sorted(list(set(date12ListAll) - set(date12ListKept))) if date12_to_drop == date12ListDropped: print( 'Calculated date12 to drop is the same as exsiting marked input file, skip updating file.' ) date12_to_drop = None return date12_to_drop