Exemplo n.º 1
0
def main(argv):
    inps = cmdLineParse()
    if not inps.disp_fig:
        plt.switch_backend('Agg')
    #print '\n******************** Plot Network **********************'

    ##### 1. Read Info
    # Read dateList and bperpList
    ext = os.path.splitext(inps.file)[1]
    if ext in ['.h5']:
        atr = readfile.read_attribute(inps.file)
        k = atr['FILE_TYPE']
        print 'reading date and perpendicular baseline from '+k+' file: '+os.path.basename(inps.file)
        if not k in multi_group_hdf5_file:
            raise ValueError('only the following file type are supported:\n'+str(multi_group_hdf5_file))
        pbase_list = ut.perp_baseline_ifgram2timeseries(inps.file)[0]
        date8_list = ptime.ifgram_date_list(inps.file)
    else:
        print 'reading date and perpendicular baseline from baseline list file: '+inps.bl_list_file
        date8_list, pbase_list = pnet.read_baseline_file(inps.bl_list_file)[0:2]
    print 'number of acquisitions  : '+str(len(date8_list))

    # Read Pairs Info
    print 'reading pairs info from file: '+inps.file
    date12_list = pnet.get_date12_list(inps.file)
    print 'number of interferograms: '+str(len(date12_list))

    # Read drop_ifgram 
    date8_list_drop = []
    date12_list_drop = []
    if ext in ['.h5','.he5']:
        h5 = h5py.File(inps.file, 'r')
        ifgram_list_all = sorted(h5[k].keys())
        ifgram_list_keep = ut.check_drop_ifgram(h5, atr, ifgram_list_all)
        date12_list_keep = ptime.list_ifgram2date12(ifgram_list_keep)
        # Get date12_list_drop
        date12_list_drop = sorted(list(set(date12_list) - set(date12_list_keep)))
        print 'number of interferograms marked as dropped: '+str(len(date12_list_drop))

        # Get date_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)))
        print 'number of acquisitions marked as dropped: '+str(len(date8_list_drop))

    # Read Coherence List
    inps.coherence_list = None
    if inps.coherence_file and os.path.isfile(inps.coherence_file):
        ext = os.path.splitext(inps.coherence_file)[1]
        if ext in ['.h5']:
            listFile = os.path.splitext(inps.coherence_file)[0]+'_spatialAverage.txt'
            if os.path.isfile(listFile):
                print 'reading coherence value from existed '+listFile
                fcoh = np.loadtxt(listFile, dtype=str)
                inps.coherence_list  = [float(i) for i in fcoh[:,1]]
                inps.coh_date12_list = [i        for i in fcoh[:,0]]
            else:
                print 'calculating average coherence value from '+inps.coherence_file
                if inps.mask_file:
                    mask = readfile.read(inps.mask_file)[0]
                else:
                    mask = None
                inps.coherence_list  = ut.spatial_average(inps.coherence_file, mask, saveList=True)
                inps.coh_date12_list = pnet.get_date12_list(inps.coherence_file)
        else:
            print 'reading coherence value from '+inps.coherence_file
            fcoh = np.loadtxt(inps.coherence_file, dtype=str)
            inps.coherence_list  = [float(i) for i in fcoh[:,1]]
            inps.coh_date12_list = [i        for i in fcoh[:,0]]

        # Check length of coherence file and input file
        if not set(inps.coh_date12_list) == set(date12_list):
            print 'WARNING: input coherence list has different pairs/date12 from input file'
            print 'turn off the color plotting of interferograms based on coherence'
            inps.coherence_list = None

    #inps.coh_thres = 0.7
    ##### 2. Plot
    # Fig 1 - Baseline History
    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax = pnet.plot_perp_baseline_hist(ax, date8_list, pbase_list, vars(inps), date8_list_drop)

    figName = 'BperpHistory'+inps.fig_ext
    if inps.save_fig:
        fig.savefig(figName,bbox_inches='tight')
        print 'save figure to '+figName

    # Fig 2 - Coherence Matrix
    if inps.coherence_list:
        figName = 'CoherenceMatrix'+inps.fig_ext
        if inps.fig_size:
            fig = plt.figure(figsize=inps.fig_size)
        else:
            fig = plt.figure()
        ax = fig.add_subplot(111)
        ax = pnet.plot_coherence_matrix(ax, date12_list, inps.coherence_list)

        if inps.save_fig:
            fig.savefig(figName, bbox_inches='tight')
            print 'save figure to '+figName

    # Fig 3 - Min/Max Coherence History
    if inps.coherence_list:
        figName = 'CoherenceHistory'+inps.fig_ext
        fig = plt.figure()
        ax = fig.add_subplot(111)
        ax = pnet.plot_coherence_history(ax, date12_list, inps.coherence_list)

        if inps.save_fig:
            fig.savefig(figName, bbox_inches='tight')
            print 'save figure to '+figName

    # Fig 4 - Interferogram Network
    if inps.fig_size:
        fig = plt.figure(figsize=inps.fig_size)
    else:
        fig = plt.figure()
    ax = fig.add_subplot(111)
    ax = pnet.plot_network(ax, date12_list, date8_list, pbase_list, vars(inps), date12_list_drop)

    figName = 'Network'+inps.fig_ext
    if inps.save_fig:
        fig.savefig(figName,bbox_inches='tight')
        print 'save figure to '+figName

    if inps.save_list:
        txtFile = os.path.splitext(inps.file)[0]+'_date12_list.txt'
        np.savetxt(txtFile, date12_list, fmt='%s')
        print 'save pairs/date12 info to file: '+txtFile


    if inps.disp_fig:
        plt.show() 
Exemplo n.º 2
0
def main(argv):
    inps = cmdLineParse()
    if not inps.disp_fig:
        plt.switch_backend('Agg')
    if inps.template_file:
        inps = read_template2inps(inps.template_file, inps)

    ##### 1. Read Info
    # Read dateList and bperpList
    ext = os.path.splitext(inps.file)[1]
    if ext in ['.h5']:
        atr = readfile.read_attribute(inps.file)
        k = atr['FILE_TYPE']
        print 'reading date and perpendicular baseline from ' + k + ' file: ' + os.path.basename(
            inps.file)
        if not k in multi_group_hdf5_file:
            raise ValueError('only the following file type are supported:\n' +
                             str(multi_group_hdf5_file))
        if not inps.coherence_file and k == 'coherence':
            inps.coherence_file = inps.file
        pbase_list = ut.perp_baseline_ifgram2timeseries(inps.file)[0]
        date8_list = ptime.ifgram_date_list(inps.file)
    else:
        print 'reading date and perpendicular baseline from baseline list file: ' + inps.bl_list_file
        date8_list, pbase_list = pnet.read_baseline_file(
            inps.bl_list_file)[0:2]
    print 'number of acquisitions  : ' + str(len(date8_list))

    # Read Pairs Info
    print 'reading pairs info from file: ' + inps.file
    date12_list = pnet.get_date12_list(inps.file)
    print 'number of interferograms: ' + str(len(date12_list))

    # Read drop_ifgram
    date8_list_drop = []
    date12_list_drop = []
    if ext in ['.h5', '.he5']:
        h5 = h5py.File(inps.file, 'r')
        ifgram_list_all = sorted(h5[k].keys())
        ifgram_list_keep = ut.check_drop_ifgram(h5)
        date12_list_keep = ptime.list_ifgram2date12(ifgram_list_keep)
        # Get date12_list_drop
        date12_list_drop = sorted(
            list(set(date12_list) - set(date12_list_keep)))
        print 'number of interferograms marked as dropped: ' + str(
            len(date12_list_drop))
        print 'number of interferograms marked as kept   : ' + str(
            len(date12_list_keep))

        # Get date_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)))
        print 'number of acquisitions marked as dropped: ' + str(
            len(date8_list_drop))

    # Read Coherence List
    inps.coherence_list = None
    if inps.coherence_file and os.path.isfile(inps.coherence_file):
        if inps.mask_file and not os.path.isfile(inps.mask_file):
            inps.mask_file = None
        inps.coherence_list, inps.coh_date12_list = ut.spatial_average(inps.coherence_file, inps.mask_file, \
                                                                       saveList=True, checkAoi=False)

        if all(np.isnan(inps.coherence_list)):
            print 'WARNING: all coherence value are nan! Do not use this and continue.'
            inps.coherence_list = None

        # Check subset of date12 info between input file and coherence file
        if not set(inps.coh_date12_list) >= set(date12_list):
            print 'WARNING: not every pair/date12 from input file is in coherence file'
            print 'turn off the color plotting of interferograms based on coherence'
            inps.coherence_list = None
        elif set(inps.coh_date12_list) > set(date12_list):
            print 'extract coherence value for all pair/date12 in input file'
            inps.coherence_list = [
                inps.coherence_list[inps.coh_date12_list.index(i)]
                for i in date12_list
            ]

    #inps.coh_thres = 0.7
    ##### 2. Plot
    inps.cbar_label = 'Average spatial coherence'

    # Fig 1 - Baseline History
    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax = pnet.plot_perp_baseline_hist(ax, date8_list, pbase_list, vars(inps),
                                      date8_list_drop)

    figName = 'BperpHistory' + inps.fig_ext
    if inps.save_fig:
        fig.savefig(figName, bbox_inches='tight')
        print 'save figure to ' + figName

    # Fig 2 - Coherence Matrix
    if inps.coherence_list:
        figName = 'CoherenceMatrix' + inps.fig_ext
        if inps.fig_size:
            fig = plt.figure(figsize=inps.fig_size)
        else:
            fig = plt.figure()
        ax = fig.add_subplot(111)
        ax = pnet.plot_coherence_matrix(ax, date12_list, inps.coherence_list,\
                                        date12_list_drop, plot_dict=vars(inps))

        if inps.save_fig:
            fig.savefig(figName, bbox_inches='tight', dpi=150)
            print 'save figure to ' + figName

    # Fig 3 - Min/Max Coherence History
    if inps.coherence_list:
        figName = 'CoherenceHistory' + inps.fig_ext
        fig = plt.figure()
        ax = fig.add_subplot(111)
        ax = pnet.plot_coherence_history(ax, date12_list, inps.coherence_list)

        if inps.save_fig:
            fig.savefig(figName, bbox_inches='tight')
            print 'save figure to ' + figName

    # Fig 4 - Interferogram Network
    if inps.fig_size:
        fig = plt.figure(figsize=inps.fig_size)
    else:
        fig = plt.figure()
    ax = fig.add_subplot(111)
    ax = pnet.plot_network(ax, date12_list, date8_list, pbase_list, vars(inps),
                           date12_list_drop)

    figName = 'Network' + inps.fig_ext
    if inps.save_fig:
        fig.savefig(figName, bbox_inches='tight')
        print 'save figure to ' + figName

    if inps.save_list:
        txtFile = os.path.splitext(inps.file)[0] + '_date12_list.txt'
        np.savetxt(txtFile, date12_list, fmt='%s')
        print 'save pairs/date12 info to file: ' + txtFile

    if inps.disp_fig:
        plt.show()
Exemplo n.º 3
0
def main(argv):

    # Read inputs
    inps = cmdLineParse()
    inps = read_template2inps(inps.template_file, inps)
    log(os.path.basename(sys.argv[0]) + ' ' + inps.template_file)

    project_name = os.path.splitext(os.path.basename(inps.template_file))[0]
    print 'project name: ' + project_name
    if not inps.sensor:
        inps.sensor = project_name2sensor(project_name)

    # Auto path setting for Miami user
    if not inps.baseline_file and pysar.miami_path and 'SCRATCHDIR' in os.environ:
        if pysar.miami_path and 'SCRATCHDIR' in os.environ:
            try:
                inps.baseline_file = glob.glob(
                    os.getenv('SCRATCHDIR') + '/' + project_name +
                    '/SLC/bl_list.txt')[0]
            except:
                inps.baseline_file = None

    # Pair selection from reference
    if inps.reference_file:
        print 'Use pairs info from reference file: ' + inps.reference_file
        date12_list = pnet.get_date12_list(inps.reference_file)
        date12_list = [i.replace('_', '-') for i in date12_list]

        if inps.baseline_file:
            date8_list, pbase_list, dop_list = pnet.read_baseline_file(
                inps.baseline_file)[0:3]
            date6_list = ptime.yymmdd(date8_list)
            tbase_list = ptime.date_list2tbase(date8_list)[0]

    # Pair selection from temp/perp/dop baseline info
    else:
        if not inps.baseline_file:
            raise Exception('ERROR: No baseline file found!')

        # Check start/end/exclude date
        date8_list = pnet.read_baseline_file(inps.baseline_file)[0]
        inps.exclude_date = ptime.yyyymmdd(inps.exclude_date)
        if not inps.exclude_date:
            inps.exclude_date = []
        else:
            print 'input exclude dates: ' + str(inps.exclude_date)
        if inps.start_date:
            print 'input start date: ' + inps.start_date
            inps.exclude_date += [
                i for i in date8_list
                if float(i) < float(ptime.yyyymmdd(inps.start_date))
            ]
            inps.exclude_date = sorted(inps.exclude_date)
        if inps.end_date:
            print 'input end   date: ' + inps.end_date
            inps.exclude_date += [
                i for i in date8_list
                if float(i) > float(ptime.yyyymmdd(inps.end_date))
            ]
            inps.exclude_date = sorted(inps.exclude_date)
        if inps.exclude_date:
            print 'exclude    dates: '
            print inps.exclude_date

        # Read baseline list file: bl_list.txt
        inps.exclude_date = ptime.yymmdd(inps.exclude_date)
        date8_list, pbase_list, dop_list = pnet.read_baseline_file(
            inps.baseline_file, inps.exclude_date)[0:3]
        date6_list = ptime.yymmdd(date8_list)
        tbase_list = ptime.date_list2tbase(date8_list)[0]

        # Initial network using input methods
        inps.method = inps.method.lower().replace('-', '_')
        if inps.method in ['star', 'ps']: inps.method = 'star'
        elif inps.method.startswith('seq'): inps.method = 'sequential'
        elif inps.method.startswith('hierar'): inps.method = 'hierarchical'
        elif inps.method in [
                'mst', 'min_spanning_tree', 'minimum_spanning_tree'
        ]:
            inps.method = 'mst'
        print 'select method: ' + inps.method

        if inps.method == 'all':
            date12_list = pnet.select_pairs_all(date6_list)
        elif inps.method == 'delaunay':
            date12_list = pnet.select_pairs_delaunay(date6_list, pbase_list,
                                                     inps.norm)
        elif inps.method == 'star':
            date12_list = pnet.select_pairs_star(date6_list)
        elif inps.method == 'sequential':
            date12_list = pnet.select_pairs_sequential(date6_list,
                                                       inps.increment_num)
        elif inps.method == 'hierarchical':
            date12_list = pnet.select_pairs_hierarchical(
                date6_list, pbase_list, inps.temp_perp_list)
        elif inps.method == 'mst':
            date12_list = pnet.select_pairs_mst(date6_list, pbase_list)
        else:
            raise Exception('Unrecoganized select method: ' + inps.method)
        print 'initial number of interferograms: ' + str(len(date12_list))

        # Filter pairs (optional) using temp/perp/doppler baseline threshold
        if inps.method in ['star', 'hierarchical', 'mst']:
            inps.threshold = False
        if inps.threshold:
            # Temporal baseline
            date12_list = pnet.threshold_temporal_baseline(date12_list, inps.temp_base_max,\
                                                           inps.keep_seasonal, inps.temp_base_min)
            print 'number of interferograms after filtering of <%d, %d> days in temporal baseline: %d'\
                  % (inps.temp_base_min, inps.temp_base_max, len(date12_list))
            if inps.keep_seasonal:
                print '\tkeep seasonal pairs, i.e. pairs with temporal baseline == N*years +/- one month'

            # Perpendicular spatial baseline
            date12_list = pnet.threshold_perp_baseline(date12_list, date6_list,
                                                       pbase_list,
                                                       inps.perp_base_max)
            print 'number of interferograms after filtering of max %d meters in perpendicular baseline: %d'\
                  % (inps.perp_base_max, len(date12_list))

            # Doppler Overlap Percentage
            if inps.sensor:
                bandwidth_az = pnet.azimuth_bandwidth(inps.sensor)
                date12_list = pnet.threshold_doppler_overlap(date12_list, date6_list, dop_list,\
                                                             bandwidth_az, inps.dop_overlap_min/100.0)
                print 'number of interferograms after filtering of min '+str(inps.dop_overlap_min)+'%'+\
                      ' overlap in azimuth Doppler frequency: '+str(len(date12_list))

    # Write ifgram_list.txt
    if not date12_list:
        print 'WARNING: No interferogram selected!'
        return None

    # date12_list to date_list
    m_dates = [
        date12.replace('_', '-').split('-')[0] for date12 in date12_list
    ]
    s_dates = [
        date12.replace('_', '-').split('-')[1] for date12 in date12_list
    ]
    try:
        print 'number of acquisitions   input   : ' + str(len(date6_list))
    except:
        pass
    print 'number of acquisitions   selected: ' + str(
        len(list(set(m_dates + s_dates))))
    print 'number of interferograms selected: ' + str(len(date12_list))

    # Output directory/filename
    if not inps.outfile:
        if pysar.miami_path and 'SCRATCHDIR' in os.environ:
            inps.out_dir = os.getenv(
                'SCRATCHDIR') + '/' + project_name + '/PROCESS'
        else:
            try:
                inps.out_dir = os.path.dirname(
                    os.path.abspath(inps.reference_file))
            except:
                inps.out_dir = os.path.dirname(
                    os.path.abspath(inps.baseline_file))
        inps.outfile = inps.out_dir + '/ifgram_list.txt'
    inps.outfile = os.path.abspath(inps.outfile)
    inps.out_dir = os.path.dirname(inps.outfile)
    if not os.path.isdir(inps.out_dir):
        os.makedirs(inps.out_dir)

    print 'writing >>> ' + inps.outfile
    if not inps.baseline_file:
        np.savetxt(inps.outfile, date12_list, fmt='%s')
        return inps.outfile

    ## Calculate Bperp, Btemp and predicted coherence
    ifgram_num = len(date12_list)
    ifgram_pbase_list = []
    ifgram_tbase_list = []

    for i in range(ifgram_num):
        m_date, s_date = date12_list[i].split('-')
        m_idx = date6_list.index(m_date)
        s_idx = date6_list.index(s_date)
        pbase = pbase_list[s_idx] - pbase_list[m_idx]
        tbase = tbase_list[s_idx] - tbase_list[m_idx]
        ifgram_pbase_list.append(pbase)
        ifgram_tbase_list.append(tbase)

    try:
        inps.coherence_list = pnet.simulate_coherence(
            date12_list, inps.baseline_file,
            sensor=inps.sensor).flatten().tolist()
        inps.cbar_label = 'Simulated coherence'
    except:
        inps.coherence_list = None

    ##### Write txt file
    fl = open(inps.outfile, 'w')
    fl.write('#Interferograms configuration generated by select_network.py\n')
    fl.write('#   Date12      Btemp(days)    Bperp(m)    sim_coherence\n')
    for i in range(len(date12_list)):
        line = '%s   %6.0f         %6.1f' % (
            date12_list[i], ifgram_tbase_list[i], ifgram_pbase_list[i])
        if inps.coherence_list:
            line += '       %1.4f' % (inps.coherence_list[i])
        fl.write(line + '\n')
    fl.close()

    ##### Plot network info
    if not inps.disp_fig:
        plt.switch_backend('Agg')

    out_fig_name = 'BperpHistory.pdf'
    print 'plotting baseline history in temp/perp baseline domain to file: ' + out_fig_name
    fig2, ax2 = plt.subplots()
    ax2 = pnet.plot_perp_baseline_hist(ax2, date8_list, pbase_list)
    plt.savefig(inps.out_dir + '/' + out_fig_name, bbox_inches='tight')

    out_fig_name = 'Network.pdf'
    print 'plotting network / pairs  in temp/perp baseline domain to file: ' + out_fig_name
    fig1, ax1 = plt.subplots()
    ax1 = pnet.plot_network(ax1,
                            date12_list,
                            date8_list,
                            pbase_list,
                            plot_dict=vars(inps),
                            print_msg=False)
    plt.savefig(inps.out_dir + '/' + out_fig_name, bbox_inches='tight')

    out_fig_name = 'CoherenceMatrix.pdf'
    if inps.coherence_list:
        print 'plotting predicted coherence matrix to file: ' + out_fig_name
        fig3, ax3 = plt.subplots()
        ax3 = pnet.plot_coherence_matrix(ax3,
                                         date12_list,
                                         inps.coherence_list,
                                         plot_dict=vars(inps))
        plt.savefig(inps.out_dir + '/' + out_fig_name, bbox_inches='tight')

    if inps.disp_fig:
        plt.show()

    return inps.outfile