Exemplo n.º 1
0
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()
Exemplo n.º 2
0
def main(argv):
    ##### Read Inputs
    inps = cmdLineParse()
    inps.file = ut.get_file_list(inps.file)
    date12_orig = pnet.get_date12_list(inps.file[0])
    print 'input file(s) to be modified: ' + str(inps.file)
    print 'number of interferograms: ' + str(len(date12_orig))
    atr = readfile.read_attribute(inps.file[0])

    # Update inps if template is input
    if inps.template_file:
        inps = read_template2inps(inps.template_file, inps)

    if all(not i for i in [inps.reference_file, inps.max_temp_baseline, inps.max_perp_baseline,\
                           inps.exclude_ifg_index, inps.exclude_date, inps.coherence_based,\
                           inps.start_date, inps.end_date, inps.reset]):
        # Display network for manually modification when there is no other modification input.
        print 'No input option found to remove interferogram'
        if inps.template_file:
            print 'Keep all interferograms by enable --reset option'
            inps.reset = True
        else:
            print 'To manually modify network, please use --manual option '
            return

    if inps.reset:
        print '----------------------------------------------------------------------------'
        for file in inps.file:
            reset_pairs(file)
        mean_coh_txt_file = os.path.splitext(
            inps.coherence_file)[0] + '_spatialAverage.txt'
        if os.path.isfile(mean_coh_txt_file):
            rmCmd = 'rm ' + mean_coh_txt_file
            print rmCmd
            os.system(rmCmd)
        return

    # Convert index : input to continous index list
    if inps.exclude_ifg_index:
        ifg_index = list(inps.exclude_ifg_index)
        inps.exclude_ifg_index = []
        for index in ifg_index:
            index_temp = [int(i) for i in index.split(':')]
            index_temp.sort()
            if len(index_temp) == 2:
                for j in range(index_temp[0], index_temp[1] + 1):
                    inps.exclude_ifg_index.append(j)
            elif len(index_temp) == 1:
                inps.exclude_ifg_index.append(int(index))
            else:
                print 'Unrecoganized input: ' + index
        inps.exclude_ifg_index = sorted(inps.exclude_ifg_index)
        if max(inps.exclude_ifg_index) > len(date12_orig):
            raise Exception('Input index out of range!\n'+\
                            'input index:'+str(inps.exclude_ifg_index)+'\n'+\
                            'index range of file: '+str(len(date12_orig)))

    ##### Get date12_to_rmv
    date12_to_rmv = []

    # 1. Update date12_to_rmv from reference file
    if inps.reference_file:
        date12_to_keep = pnet.get_date12_list(inps.reference_file,
                                              check_drop_ifgram=True)
        print '----------------------------------------------------------------------------'
        print 'use reference pairs info from file: ' + inps.reference_file
        print 'number of interferograms in reference: ' + str(
            len(date12_to_keep))
        print 'date12 not in reference file:'
        date12_to_rmv_temp = []
        for date12 in date12_orig:
            if date12 not in date12_to_keep:
                date12_to_rmv.append(date12)
                date12_to_rmv_temp.append(date12)
        print date12_to_rmv_temp

    # 2.1 Update date12_to_rmv from coherence file
    if inps.coherence_based and os.path.isfile(inps.coherence_file):
        print '----------------------------------------------------------------------------'
        print 'use coherence-based network modification from coherence file: ' + inps.coherence_file
        # check mask AOI in lalo
        if inps.aoi_geo_box and inps.lookup_file:
            print 'input AOI in (lon0, lat1, lon1, lat0): ' + str(
                inps.aoi_geo_box)
            inps.aoi_pix_box = subset.bbox_geo2radar(inps.aoi_geo_box, atr,
                                                     inps.lookup_file)
        if inps.aoi_pix_box:
            # check mask AOI within the data coverage
            inps.aoi_pix_box = subset.check_box_within_data_coverage(
                inps.aoi_pix_box, atr)
            print 'input AOI in (x0,y0,x1,y1): ' + str(inps.aoi_pix_box)

        # Calculate spatial average coherence
        coh_list, coh_date12_list = ut.spatial_average(inps.coherence_file, inps.mask_file,\
                                                           inps.aoi_pix_box, saveList=True)

        # MST network
        if inps.keep_mst:
            print 'Get minimum spanning tree (MST) of interferograms with inverse of coherence.'
            print 'date12 with 1) average coherence < ' + str(
                inps.min_coherence) + ' AND 2) not in MST network: '
            mst_date12_list = pnet.threshold_coherence_based_mst(
                coh_date12_list, coh_list)
        else:
            print 'date12 with average coherence < ' + str(inps.min_coherence)
            mst_date12_list = []

        date12_to_rmv_temp = []
        for i in range(len(coh_date12_list)):
            date12 = coh_date12_list[i]
            if coh_list[
                    i] < inps.min_coherence and date12 not in mst_date12_list:
                date12_to_rmv.append(date12)
                date12_to_rmv_temp.append(date12)
        print date12_to_rmv_temp

    # 2.2 Update date12_to_rmv from temp baseline threshold
    if inps.max_temp_baseline:
        print '----------------------------------------------------------------------------'
        print 'Drop pairs with temporal baseline > ' + str(
            inps.max_temp_baseline) + ' days'
        date8_list = ptime.ifgram_date_list(inps.file[0])
        date6_list = ptime.yymmdd(date8_list)
        tbase_list = ptime.date_list2tbase(date8_list)[0]
        date12_to_rmv_temp = []
        for i in range(len(date12_orig)):
            date1, date2 = date12_orig[i].split('-')
            idx1 = date6_list.index(date1)
            idx2 = date6_list.index(date2)
            t_diff = tbase_list[idx2] - tbase_list[idx1]
            if t_diff > inps.max_temp_baseline:
                date12 = date12_orig[i]
                date12_to_rmv.append(date12)
                date12_to_rmv_temp.append(date12)
        print 'number of pairs to drop: %d' % (len(date12_to_rmv_temp))
        print date12_to_rmv_temp

    # 2.3 Update date12_to_rmv from perp baseline threshold
    if inps.max_perp_baseline:
        print '----------------------------------------------------------------------------'
        print 'Drop pairs with perpendicular spatial baseline > ' + str(
            inps.max_perp_baseline) + ' meters'
        ifg_bperp_list = pnet.igram_perp_baseline_list(inps.file[0])
        date12_to_rmv_temp = []
        for i in range(len(ifg_bperp_list)):
            if abs(ifg_bperp_list[i]) > inps.max_perp_baseline:
                date12 = date12_orig[i]
                date12_to_rmv.append(date12)
                date12_to_rmv_temp.append(date12)
        print 'number of pairs to drop: %d' % (len(date12_to_rmv_temp))
        print date12_to_rmv_temp

    # 2.4 Update date12_to_rmv from exclude_ifg_index
    if inps.exclude_ifg_index:
        print '----------------------------------------------------------------------------'
        print 'drop date12/pair with the following index number:'
        for index in inps.exclude_ifg_index:
            date12 = date12_orig[index - 1]
            date12_to_rmv.append(date12)
            print str(index) + '    ' + date12

    # 2.5 Update date12_to_rmv from exclude_date
    if inps.exclude_date:
        inps.exclude_date = ptime.yymmdd(inps.exclude_date)
        print '----------------------------------------------------------------------------'
        print 'Drop pairs including the following dates: \n' + str(
            inps.exclude_date)
        date12_to_rmv_temp = []
        for i in range(len(date12_orig)):
            date1, date2 = date12_orig[i].split('-')
            if (date1 in inps.exclude_date) or (date2 in inps.exclude_date):
                date12 = date12_orig[i]
                date12_to_rmv.append(date12)
                date12_to_rmv_temp.append(date12)
        print date12_to_rmv_temp

    # 2.6 Update date12_to_rmv from start_date
    if inps.start_date:
        inps.start_date = ptime.yymmdd(inps.start_date)
        print '----------------------------------------------------------------------------'
        print 'Drop pairs with date earlier than start-date: ' + inps.start_date
        min_date = int(ptime.yyyymmdd(inps.start_date))
        date12_to_rmv_temp = []
        for i in range(len(date12_orig)):
            date12 = date12_orig[i]
            if any(
                    int(j) < min_date
                    for j in ptime.yyyymmdd(date12.split('-'))):
                date12_to_rmv.append(date12)
                date12_to_rmv_temp.append(date12)
        print date12_to_rmv_temp

    # 2.7 Update date12_to_rmv from end_date
    if inps.end_date:
        inps.end_date = ptime.yymmdd(inps.end_date)
        print '----------------------------------------------------------------------------'
        print 'Drop pairs with date earlier than end-date: ' + inps.end_date
        max_date = int(ptime.yyyymmdd(inps.end_date))
        date12_to_rmv_temp = []
        for i in range(len(date12_orig)):
            date12 = date12_orig[i]
            if any(
                    int(j) > max_date
                    for j in ptime.yyyymmdd(date12.split('-'))):
                date12_to_rmv.append(date12)
                date12_to_rmv_temp.append(date12)
        print date12_to_rmv_temp

    # 3. Manually drop pairs
    if inps.disp_network:
        date12_click = manual_select_pairs_to_remove(inps.file[0])
        for date12 in list(date12_click):
            if date12 not in date12_orig:
                date12_click.remove(date12)
        print 'date12 selected to remove:'
        print date12_click
        date12_to_rmv += date12_click

    # 4. drop duplicate date12 and sort in order
    date12_to_rmv = sorted(list(set(date12_to_rmv)))
    date12_keep = sorted(list(set(date12_orig) - set(date12_to_rmv)))
    print '----------------------------------------------------------------------------'
    print 'number of interferograms to remove: ' + str(len(date12_to_rmv))
    print 'number of interferograms kept     : ' + str(len(date12_keep))

    ##### Calculated date12_to_drop v.s. existing date12_to_drop
    # Get list of date12 of interferograms already been marked to drop
    k = readfile.read_attribute(inps.file[0])['FILE_TYPE']
    h5 = h5py.File(inps.file[0], 'r')
    ifgram_list_all = sorted(h5[k].keys())
    ifgram_list_keep = ut.check_drop_ifgram(h5, print_msg=False)
    ifgram_list_dropped = sorted(
        list(set(ifgram_list_all) - set(ifgram_list_keep)))
    date12_list_dropped = ptime.list_ifgram2date12(ifgram_list_dropped)
    h5.close()

    if date12_to_rmv == date12_list_dropped and inps.mark_attribute:
        print 'Calculated date12 to drop is the same as exsiting marked input file, skip update file attributes.'
        date12_to_rmv = []

    ##### Update date12 to drop
    if date12_to_rmv:
        ##### Update Input Files with date12_to_rmv
        Modified_CoherenceFile = 'Modified_coherence.h5'
        for File in inps.file:
            Modified_File = modify_file_date12_list(File, date12_to_rmv,
                                                    inps.mark_attribute)

            k = readfile.read_attribute(File)['FILE_TYPE']
            # Update Mask File
            if k == 'interferograms' and inps.update_aux:
                print 'update mask file for input ' + k + ' file based on ' + Modified_File
                inps.mask_file = 'mask.h5'
                print 'writing >>> ' + inps.mask_file
                ut.nonzero_mask(Modified_File, inps.mask_file)

            elif k == 'coherence' and inps.update_aux:
                inps.coherence_file = Modified_File
                print 'update average spatial coherence for input ' + k + ' file based on: ' + Modified_File
                outFile = 'averageSpatialCoherence.h5'
                print 'writing >>> ' + outFile
                ut.temporal_average(Modified_File, outFile)

                # Touch spatial average txt file of coherence if it's existed
                coh_spatialAverage_file = os.path.splitext(
                    Modified_File)[0] + '_spatialAverage.txt'
                if os.path.isfile(coh_spatialAverage_file):
                    touchCmd = 'touch ' + coh_spatialAverage_file
                    print touchCmd
                    os.system(touchCmd)

    # Plot result
    if inps.plot:
        print '\nplot modified network and save to file.'
        plotCmd = 'plot_network.py ' + inps.coherence_file + ' --coherence ' + inps.coherence_file + ' --nodisplay'
        if inps.template_file:
            plotCmd += ' --template ' + inps.template_file
        print plotCmd
        os.system(plotCmd)

    print 'Done.'
    return
Exemplo n.º 3
0
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