예제 #1
0
def read_template2inps(template_file, inps=None):
    '''Read input template file into inps.ex_date'''
    if not inps:
        inps = cmdLineParse()
    template = readfile.read_template(template_file)
    key_list = template.keys()

    # Read template option
    prefix = 'pysar.velocity.'
    key = prefix + 'excludeDate'
    if key in key_list:
        value = template[key]
        if value == 'auto':
            inps.ex_date = ['exclude_date.txt']
        elif value == 'no':
            inps.ex_date = []
        else:
            inps.ex_date = value.replace(',', ' ').split()

    key = prefix + 'startDate'
    if key in key_list:
        value = template[key]
        if value not in ['auto', 'no']:
            inps.min_date = ptime.yyyymmdd(value)

    key = prefix + 'endDate'
    if key in key_list:
        value = template[key]
        if value not in ['auto', 'no']:
            inps.max_date = ptime.yyyymmdd(value)

    return inps
예제 #2
0
def select_pairs_star(date_list, m_date=None, pbase_list=[]):
    '''Select Star-like network/interferograms/pairs, it's a single master network, similar to PS approach.
    Usage:
        m_date : master date, choose it based on the following cretiria:
                 1) near the center in temporal and spatial baseline
                 2) prefer winter season than summer season for less temporal decorrelation
    Reference:
        Ferretti, A., C. Prati, and F. Rocca (2001), Permanent scatterers in SAR interferometry, IEEE TGRS, 39(1), 8-20.
    '''
    date8_list = sorted(ptime.yyyymmdd(date_list))
    date6_list = ptime.yymmdd(date8_list)
    
    # Select master date if not existed
    if not m_date:
        m_date = select_master_date(date8_list, pbase_list)
        print 'auto select master date: '+m_date
    
    # Check input master date
    m_date8 = ptime.yyyymmdd(m_date)
    if m_date8 not in date8_list:
        print 'Input master date is not existed in date list!'
        print 'Input master date: '+str(m_date8)
        print 'Input date list: '+str(date8_list)
        m_date8 = None
    
    # Generate star/ps network
    m_idx = date8_list.index(m_date8)
    date12_idx_list = [sorted([m_idx, s_idx]) for s_idx in range(len(date8_list)) if s_idx is not m_idx]
    date12_list = [date6_list[idx[0]]+'-'+date6_list[idx[1]] for idx in date12_idx_list]
    
    return date12_list
예제 #3
0
def read_template2inps(template_file, inps=None):
    '''Read input template file into inps.ex_date'''
    if not inps:
        inps = cmdLineParse()
    template = readfile.read_template(template_file)
    key_list = template.keys()

    # Read template option
    prefix = 'pysar.topographicResidual.'

    key = prefix+'polyOrder'
    if key in key_list:
        value = template[key]
        if value == 'auto':
            inps.poly_order = 2
        else:
            inps.poly_order = int(value)

    key = prefix+'excludeDate'
    if key in key_list:
        value = template[key]
        if value not in ['auto','no']:
            value = value.replace(',',' ').split()
            value = ptime.yyyymmdd(value)
            inps.ex_date += value

    key = prefix+'stepFuncDate'
    if key in key_list:
        value = template[key]
        if value not in ['auto','no']:
            value = value.replace(',',' ').split()
            value = ptime.yyyymmdd(value)
            inps.step_date += value

    return inps
예제 #4
0
def get_exclude_date(inps, date_list_all):
    '''Get inps.ex_date full list
    Inputs:
        inps          - Namespace, 
        date_list_all - list of string for all available date in YYYYMMDD format
    Output:
        inps.ex_date  - list of string for exclude date in YYYYMMDD format
    '''
    yy_list_all = ptime.yyyymmdd2years(date_list_all)

    # 1. template_file
    if inps.template_file:
        print 'read option from template file: ' + inps.template_file
        inps = read_template2inps(inps.template_file, inps)

    # 2. ex_date
    input_ex_date = list(inps.ex_date)
    inps.ex_date = []
    if input_ex_date:
        for ex_date in input_ex_date:
            if os.path.isfile(ex_date):
                ex_date = ptime.read_date_list(ex_date)
            else:
                ex_date = [ptime.yyyymmdd(ex_date)]
            inps.ex_date += list(set(ex_date) - set(inps.ex_date))
        # delete dates not existed in input file
        inps.ex_date = list(set(inps.ex_date).intersection(date_list_all))
        print 'exclude date:' + str(inps.ex_date)

    # 3. min_date
    if inps.min_date:
        print 'start date: ' + inps.min_date
        yy_min = ptime.yyyymmdd2years(ptime.yyyymmdd(inps.min_date))
        for i in range(len(date_list_all)):
            date = date_list_all[i]
            if yy_list_all[i] < yy_min and date not in inps.ex_date:
                print '  remove date: ' + date
                inps.ex_date.append(date)

    # 4. max_date
    if inps.max_date:
        print 'end date: ' + inps.max_date
        yy_max = ptime.yyyymmdd2years(ptime.yyyymmdd(inps.max_date))
        for i in range(len(date_list_all)):
            date = date_list_all[i]
            if yy_list_all[i] > yy_max and date not in inps.ex_date:
                print '  remove date: ' + date
                inps.ex_date.append(date)

    return inps.ex_date
예제 #5
0
파일: _network.py 프로젝트: yunjunz/PySAR
def read_baseline_file(baselineFile,exDateList=[]):
    ## Read bl_list.txt without dates listed in exDateList
    ##
    ## Examples:
    ##     date8List, perpBaseList, dopList, prfList, slcDirList = read_baseline_file(baselineFile)
    ##     date8List, perpBaseList, dopList, prfList, slcDirList = read_baseline_file(baselineFile,['080520','100726'])
    ##     date8List, perpBaseList = read_baseline_file(baselineFile)[0:2]

    ## Read baseline file into lines
    fb = open(baselineFile)
    lines = []
    for line in fb.xreadlines():
        l = str.replace(line,'\n','').strip()
        lines.append(l)
    fb.close()

    ## Read each line and put the values into arrays
    date6List    = []
    perpBaseList = []
    dopplerList  = []
    prfList      = []
    slcDirList   = []
    for line in lines:
        c = line.split()    # splits on white space
        date = c[0]
        if not date in exDateList:
            date6List.append(date)
            perpBaseList.append(float(c[1]))
            dopplerList.append(np.array([float(c[2]), float(c[3]), float(c[4])]))
            prfList.append(float(c[5]))
            slcDirList.append(c[6])

    date8List = ptime.yyyymmdd(date6List)

    return date8List, perpBaseList, dopplerList, prfList, slcDirList
예제 #6
0
def select_master_date(date_list, pbase_list=[]):
    '''Select super master date based on input temporal and/or perpendicular baseline info.
    Return master date in YYYYMMDD format.
    '''
    date8_list = ptime.yyyymmdd(date_list)
    if not pbase_list:
        # Choose date in the middle
        m_date8 = date8_list[int(len(date8_list)/2)]
    else:
        # Get temporal baseline list
        tbase_list = ptime.date_list2tbase(date8_list)[0]
        # Normalization (Pepe and Lanari, 2006, TGRS)
        temp2perp_scale = (max(pbase_list)-min(pbase_list)) / (max(tbase_list)-min(tbase_list))
        tbase_list = [tbase*temp2perp_scale for tbase in tbase_list]
        # Get distance matrix
        ttMat1, ttMat2 = np.meshgrid(np.array(tbase_list), np.array(tbase_list))
        ppMat1, ppMat2 = np.meshgrid(np.array(pbase_list), np.array(pbase_list))
        ttMat = np.abs(ttMat1 - ttMat2)  # temporal distance matrix
        ppMat = np.abs(ppMat1 - ppMat2)  # spatial distance matrix
        disMat = np.sqrt(np.square(ttMat) + np.square(ppMat))  # 2D distance matrix in temp/perp domain
        
        # Choose date minimize the total distance of temp/perp baseline
        disMean = np.mean(disMat, 0)
        m_idx = np.argmin(disMean)
        m_date8 = date8_list[m_idx]
    return m_date8
예제 #7
0
def coherence_matrix(date12_list, coh_list):
    '''Return coherence matrix based on input date12 list and its coherence
    Inputs:
        date12_list - list of string in YYMMDD-YYMMDD format
        coh_list    - list of float, average coherence for each interferograms
    Output:
        coh_matrix  - 2D np.array with dimension length = date num
                      np.nan value for interferograms non-existed.
                      1.0 for diagonal elements
    '''
    # Get date list
    m_dates = [date12.split('-')[0] for date12 in date12_list]
    s_dates = [date12.split('-')[1] for date12 in date12_list]
    date6_list = ptime.yymmdd(sorted(ptime.yyyymmdd(list(set(m_dates + s_dates)))))
    date_num = len(date6_list)

    coh_mat = np.zeros([date_num, date_num])
    coh_mat[:] = np.nan
    for date12 in date12_list:
        date1, date2 = date12.split('-')
        idx1 = date6_list.index(date1)
        idx2 = date6_list.index(date2)
        coh = coh_list[date12_list.index(date12)]
        coh_mat[idx1, idx2] = coh    #symmetric
        coh_mat[idx2, idx1] = coh

    #for i in range(date_num):    # diagonal value
    #    coh_mat[i, i] = 1.0

    return coh_mat
예제 #8
0
def threshold_doppler_overlap(date12_list, date_list, dop_list, bandwidth_az, dop_overlap_min=0.15):
    '''Remove pairs/interoferogram with doppler overlap larger than critical value
    Inputs:
        date12_list : list of string, for date12 in YYMMDD-YYMMDD format
        date_list   : list of string, for date in YYMMDD/YYYYMMDD format, optional
        dop_list    : list of list of 3 float, for centroid Doppler frequency
        bandwidth_az    : float, bandwidth in azimuth direction
        dop_overlap_min : float, minimum overlap of azimuth Doppler frequency
    Outputs:
        date12_list : list of string, for date12 in YYMMDD-YYMMDD format
    '''
    if not date12_list:  return []
    # Get date6_list
    if not date_list:
        m_dates = [date12.split('-')[0] for date12 in date12_list]
        s_dates = [date12.split('-')[1] for date12 in date12_list]
        date_list = sorted(ptime.yyyymmdd(list(set(m_dates + s_dates))))
        if not len(date_list) == len(pbase_list):
            print 'ERROR: number of existing dates is not equal to number of perp baseline!'
            print 'date list is needed for threshold filtering!'
            print 'skip filtering.'
            return date12_list
    date6_list = ptime.yymmdd(date_list)

    # Threshold
    date12_list_out = []
    for date12 in date12_list:
        date1, date2 = date12.split('-')
        idx1 = date6_list.index(date1)
        idx2 = date6_list.index(date2)
        dop_overlap = calculate_doppler_overlap(dop_list[idx1], dop_list[idx2], bandwidth_az)
        if dop_overlap >= dop_overlap_min:
            date12_list_out.append(date12)
    return date12_list_out
예제 #9
0
def threshold_temporal_baseline(date12_list, btemp_max, keep_seasonal=True, btemp_min=0.0):
    '''Remove pairs/interferograms out of min/max/seasonal temporal baseline limits
    Inputs:
        date12_list : list of string for date12 in YYMMDD-YYMMDD format
        btemp_max   : float, maximum temporal baseline
        btemp_min   : float, minimum temporal baseline
        keep_seasonal : keep interferograms with seasonal temporal baseline
    Output:
        date12_list_out : list of string for date12 in YYMMDD-YYMMDD format
    Example:
        date12_list = threshold_temporal_baseline(date12_list, 200)
        date12_list = threshold_temporal_baseline(date12_list, 200, False)
    '''
    if not date12_list:  return []
    # Get date list and tbase 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))))
    date6_list = ptime.yymmdd(date8_list)
    tbase_list = ptime.date_list2tbase(date8_list)[0]

    # Threshold
    date12_list_out = []
    for date12 in date12_list:
        date1, date2 = date12.split('-')
        idx1 = date6_list.index(date1)
        idx2 = date6_list.index(date2)
        tbase = int(abs(tbase_list[idx1] - tbase_list[idx2]))
        if btemp_min <= tbase <= btemp_max:
            date12_list_out.append(date12)
        elif keep_seasonal and tbase/30 in [11,12]:
            date12_list_out.append(date12)    
    return date12_list_out
예제 #10
0
def select_pairs_delaunay(date_list, pbase_list, norm=True):
    '''Select Pairs using Delaunay Triangulation based on temporal/perpendicular baselines
    Inputs:
        date_list  : list of date in YYMMDD/YYYYMMDD format
        pbase_list : list of float, perpendicular spatial baseline
        norm       : normalize temporal baseline to perpendicular baseline
    Key points
        1. Define a ratio between perpendicular and temporal baseline axis units (Pepe and Lanari, 2006, TGRS).
        2. Pairs with too large perpendicular / temporal baseline or Doppler centroid difference should be removed
           after this, using a threshold, to avoid strong decorrelations (Zebker and Villasenor, 1992, TGRS).
    Reference:
        Pepe, A., and R. Lanari (2006), On the extension of the minimum cost flow algorithm for phase unwrapping
        of multitemporal differential SAR interferograms, IEEE TGRS, 44(9), 2374-2383.
        Zebker, H. A., and J. Villasenor (1992), Decorrelation in interferometric radar echoes, IEEE TGRS, 30(5), 950-959.
    '''
    # Get temporal baseline in days
    date6_list = ptime.yymmdd(date_list)
    date8_list = ptime.yyyymmdd(date_list)
    tbase_list = ptime.date_list2tbase(date8_list)[0]

    # Normalization (Pepe and Lanari, 2006, TGRS)
    if norm:
        temp2perp_scale = (max(pbase_list)-min(pbase_list)) / (max(tbase_list)-min(tbase_list))
        tbase_list = [tbase*temp2perp_scale for tbase in tbase_list]
    
    # Generate Delaunay Triangulation
    date12_idx_list = Triangulation(tbase_list, pbase_list).edges.tolist()
    date12_idx_list = [sorted(idx) for idx in sorted(date12_idx_list)]

    # Convert index into date12
    date12_list = [date6_list[idx[0]]+'-'+date6_list[idx[1]] for idx in date12_idx_list]
    return date12_list
예제 #11
0
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
예제 #12
0
def select_pairs_sequential(date_list, increment_num=2):
    '''Select Pairs in a Sequential way:
        For each acquisition, find its increment_num nearest acquisitions in the past time.
    Inputs:
        date_list  : list of date in YYMMDD/YYYYMMDD format
    Reference:
        Fattahi, H., and F. Amelung (2013), DEM Error Correction in InSAR Time Series, IEEE TGRS, 51(7), 4249-4259.
    '''
    date8_list = sorted(ptime.yyyymmdd(date_list))
    date6_list = ptime.yymmdd(date8_list)
    date_idx_list = list(range(len(date6_list)))

    # Get pairs index list
    date12_idx_list = []
    for date_idx in date_idx_list:
        for i in range(increment_num):
            if date_idx - i - 1 >= 0:
                date12_idx_list.append([date_idx - i - 1, date_idx])
    date12_idx_list = [sorted(idx) for idx in sorted(date12_idx_list)]

    # Convert index into date12
    date12_list = [
        date6_list[idx[0]] + '-' + date6_list[idx[1]]
        for idx in date12_idx_list
    ]
    return date12_list
예제 #13
0
def threshold_coherence_based_mst(date12_list, coh_list):
    '''Return a minimum spanning tree of network based on the coherence inverse.
    Inputs:
        date12_list - list of string in YYMMDD-YYMMDD format
        coh_list    - list of float, average coherence for each interferogram
    Output:
        mst_date12_list - list of string in YYMMDD-YYMMDD format, for MST network of interferograms 
    '''
    # coh_list --> coh_mat --> weight_mat
    coh_mat = coherence_matrix(date12_list, coh_list)
    mask = ~np.isnan(coh_mat)
    wei_mat = np.zeros(coh_mat.shape)
    wei_mat[:] = np.inf
    wei_mat[mask] = 1/coh_mat[mask]

    # MST path based on weight matrix
    wei_mat_csr = csr_matrix(wei_mat)
    mst_mat_csr = minimum_spanning_tree(wei_mat_csr)

    # Get date6_list
    m_dates = [date12.split('-')[0] for date12 in date12_list]
    s_dates = [date12.split('-')[1] for date12 in date12_list]
    date6_list = ptime.yymmdd(sorted(ptime.yyyymmdd(list(set(m_dates + s_dates)))))

    # Convert MST index matrix into date12 list
    [s_idx_list, m_idx_list] = [date_idx_array.tolist() for date_idx_array in find(mst_mat_csr)[0:2]]
    mst_date12_list = []
    for i in range(len(m_idx_list)):
        idx = sorted([m_idx_list[i], s_idx_list[i]])
        date12 = date6_list[idx[0]]+'-'+date6_list[idx[1]]
        mst_date12_list.append(date12)
    return mst_date12_list
예제 #14
0
def select_master_interferogram(date12_list, date_list, pbase_list, m_date=None):
    '''Select reference interferogram based on input temp/perp baseline info
    If master_date is specified, select its closest slave_date; otherwise, choose the closest pair
    among all pairs as master interferogram.
    Example:
        master_date12 = pnet.select_master_ifgram(date12_list, date_list, pbase_list)
    '''
    pbase_array = np.array(pbase_list, dtype='float64')
    # Get temporal baseline
    date8_list = ptime.yyyymmdd(date_list)
    date6_list = ptime.yymmdd(date8_list)
    tbase_array = np.array(ptime.date_list2tbase(date8_list)[0], dtype='float64')
    # Normalization (Pepe and Lanari, 2006, TGRS)
    temp2perp_scale = (max(pbase_array)-min(pbase_array)) / (max(tbase_array)-min(tbase_array))
    tbase_array *= temp2perp_scale
    
    # Calculate sqrt of temp/perp baseline for input pairs
    idx1 = np.array([date6_list.index(date12.split('-')[0]) for date12 in date12_list])
    idx2 = np.array([date6_list.index(date12.split('-')[1]) for date12 in date12_list])
    base_distance = np.sqrt((tbase_array[idx2] - tbase_array[idx1])**2 + (pbase_array[idx2] - pbase_array[idx1])**2)
    
    # Get master interferogram index
    if not m_date:
        # Choose pair with shortest temp/perp baseline
        m_date12_idx = np.argmin(base_distance)        
    else:
        m_date = ptime.yymmdd(m_date)
        # Choose pair contains m_date with shortest temp/perp baseline
        m_date12_idx_array = np.array([date12_list.index(date12) for date12 in date12_list if m_date in date12])
        min_base_distance = np.min(base_distance[m_date12_idx_array])
        m_date12_idx = np.where(base_distance == min_base_distance)[0][0]
    
    m_date12 = date12_list[m_date12_idx]
    return m_date12
예제 #15
0
def ref_date_file(inFile, ref_date, outFile=None):
    '''Change input file reference date to a different one.'''
    if not outFile:
        outFile = os.path.splitext(inFile)[0] + '_refDate.h5'

    # Input file type
    atr = readfile.read_attribute(inFile)
    k = atr['FILE_TYPE']
    if not k in ['timeseries']:
        print 'Input file is ' + k + ', only timeseries is supported.'
        return None

    # Input reference date
    h5 = h5py.File(inFile, 'r')
    date_list = sorted(h5[k].keys())
    h5.close()
    date_num = len(date_list)
    try:
        ref_date_orig = atr['ref_date']
    except:
        ref_date_orig = date_list[0]

    ref_date = ptime.yyyymmdd(ref_date)
    print 'input reference date: ' + ref_date
    if not ref_date in date_list:
        print 'Input reference date was not found!\nAll dates available: ' + str(
            date_list)
        return None
    if ref_date == ref_date_orig:
        print 'Same reference date chosen as existing reference date.'
        print 'Copy %s to %s' % (inFile, outFile)
        shutil.copy2(inFile, outFile)
        return outFile

    # Referencing in time
    h5 = h5py.File(inFile, 'r')
    ref_data = h5[k].get(ref_date)[:]

    print 'writing >>> ' + outFile
    h5out = h5py.File(outFile, 'w')
    group = h5out.create_group(k)
    prog_bar = ptime.progress_bar(maxValue=date_num)
    for i in range(date_num):
        date = date_list[i]
        data = h5[k].get(date)[:]
        dset = group.create_dataset(date,
                                    data=data - ref_data,
                                    compression='gzip')
        prog_bar.update(i + 1, suffix=date)
    prog_bar.close()
    h5.close()

    ## Update attributes
    atr = ref_date_attribute(atr, ref_date, date_list)
    for key, value in atr.iteritems():
        group.attrs[key] = value
    h5out.close()

    return outFile
예제 #16
0
def ref_date_attribute(atr_in, ref_date, date_list):
    '''Update attribute dictionary for reference date'''
    ref_date = ptime.yyyymmdd(ref_date)
    date_list = ptime.yyyymmdd(date_list)
    ref_index = date_list.index(ref_date)

    atr = dict()
    for key, value in atr_in.iteritems():
        atr[key] = str(value)

    # Update ref_date
    atr['ref_date'] = ref_date
    print 'update ref_date'

    # Update Bperp time series
    try:
        pbase = np.array(
            [float(i) for i in atr['P_BASELINE_TIMESERIES'].split()])
        pbase -= pbase[ref_index]
        atr['P_BASELINE_TIMESERIES'] = str(pbase.tolist()).translate(
            None, '[],')
        print 'update P_BASELINE_TIMESERIES'
    except:
        pass

    try:
        pbase_top = np.array(
            [float(i) for i in atr['P_BASELINE_TOP_TIMESERIES'].split()])
        pbase_bottom = np.array(
            [float(i) for i in atr['P_BASELINE_BOTTOM_TIMESERIES'].split()])
        pbase_top -= pbase_top[ref_index]
        pbase_bottom -= pbase_bottom[ref_index]
        atr['P_BASELINE_TOP_TIMESERIES'] = str(pbase_top.tolist()).translate(
            None, '[],')
        atr['P_BASELINE_BOTTOM_TIMESERIES'] = str(
            pbase_bottom.tolist()).translate(None, '[],')
        print 'update P_BASELINE_TOP/BOTTOM_TIMESERIES'
    except:
        pass

    return atr
예제 #17
0
def select_pairs_all(date_list):
    '''Select All Possible Pairs/Interferograms
    Input : date_list   - list of date in YYMMDD/YYYYMMDD format
    Output: date12_list - list date12 in YYMMDD-YYMMDD format
    Reference:
        Berardino, P., G. Fornaro, R. Lanari, and E. Sansosti (2002), A new algorithm for surface deformation monitoring
        based on small baseline differential SAR interferograms, IEEE TGRS, 40(11), 2375-2383.
    '''
    date8_list = sorted(ptime.yyyymmdd(date_list))
    date6_list = ptime.yymmdd(date8_list)
    date12_list = list(itertools.combinations(date6_list, 2))
    date12_list = [date12[0]+'-'+date12[1] for date12 in date12_list]
    return date12_list
예제 #18
0
def date12_list2index(date12_list, date_list=[]):
    '''Convert list of date12 string into list of index'''
    # Get dateList from date12List
    if not date_list:
        m_dates = [date12.split('-')[0] for date12 in date12_list]
        s_dates = [date12.split('-')[1] for date12 in date12_list]
        date_list = list(set(m_dates + s_dates))
    date6_list = ptime.yymmdd(sorted(ptime.yyyymmdd(date_list)))
    
    # Get pair index 
    pairs_idx = []
    for date12 in date12_list:
        dates = date12.split('-')
        pair_idx = [date6_list.index(dates[0]), date6_list.index(dates[1])]
        pairs_idx.append(pair_idx)

    return pairs_idx
예제 #19
0
def get_exclude_date(inps, date_list_all):
    input_ex_date = list(inps.ex_date)
    inps.ex_date = []
    if input_ex_date:
        for ex_date in input_ex_date:
            if os.path.isfile(ex_date):
                ex_date = ptime.read_date_list(ex_date)
            else:
                ex_date = [ptime.yyyymmdd(ex_date)]
            inps.ex_date += list(set(ex_date) - set(inps.ex_date))
        # delete dates not existed in input file
        inps.ex_date = sorted(
            list(set(inps.ex_date).intersection(date_list_all)))
        print 'Exclude date for DEM error estimation:'
        print inps.ex_date

    return inps
예제 #20
0
def read_baseline_file(baselineFile, exDateList=[]):
    '''Read bl_list.txt without dates listed in exDateList
    # Date  Bperp    dop0/PRF  dop1/PRF   dop2/PRF      PRF    slcDir
    070106     0.0   0.03      0.0000000  0.00000000000 2155.2 /scratch/KyushuT422F650AlosA/SLC/070106/
    070709  2631.9   0.07      0.0000000  0.00000000000 2155.2 /scratch/KyushuT422F650AlosA/SLC/070709/
    070824  2787.3   0.07      0.0000000  0.00000000000 2155.2 /scratch/KyushuT422F650AlosA/SLC/070824/
    ...
    
    Examples:
        date8List, perpBaseList, dopList, prfList, slcDirList = read_baseline_file(baselineFile)
        date8List, perpBaseList, dopList, prfList, slcDirList = read_baseline_file(baselineFile,['080520','100726'])
        date8List, perpBaseList = read_baseline_file(baselineFile)[0:2]
    '''
    exDateList = ptime.yymmdd(exDateList)
    if not exDateList:  exDateList = []

    ## Read baseline file into lines
    fb = open(baselineFile)
    lines = []
    for line in fb.xreadlines():
        l = str.replace(line,'\n','').strip()
        lines.append(l)
    fb.close()

    ## Read each line and put the values into arrays
    date6List    = []
    perpBaseList = []
    dopplerList  = []
    slcDirList   = []
    for line in lines:
        c = line.split()    # splits on white space
        date = c[0]
        if not date in exDateList:
            date6List.append(date)
            perpBaseList.append(float(c[1]))
            dop = np.array([float(c[2]), float(c[3]), float(c[4])])
            prf = float(c[5])
            dop *= prf
            dopplerList.append(dop)
            slcDirList.append(c[6])

    date8List = ptime.yyyymmdd(date6List)
    return date8List, perpBaseList, dopplerList, slcDirList
예제 #21
0
def select_pairs_mst(date_list, pbase_list):
    '''Select Pairs using Minimum Spanning Tree technique
        Connection Cost is calculated using the baseline distance in perp and scaled temporal baseline (Pepe and Lanari,
        2006, TGRS) plane.
    Inputs:
        date_list  : list of date in YYMMDD/YYYYMMDD format
        pbase_list : list of float, perpendicular spatial baseline
    References:
        Pepe, A., and R. Lanari (2006), On the extension of the minimum cost flow algorithm for phase unwrapping
        of multitemporal differential SAR interferograms, IEEE TGRS, 44(9), 2374-2383.
        Perissin D., Wang T. (2012), Repeat-pass SAR interferometry with partially coherent targets. IEEE TGRS. 271-280
    '''
    # Get temporal baseline in days
    date6_list = ptime.yymmdd(date_list)
    date8_list = ptime.yyyymmdd(date_list)
    tbase_list = ptime.date_list2tbase(date8_list)[0]
    # Normalization (Pepe and Lanari, 2006, TGRS)
    temp2perp_scale = (max(pbase_list) - min(pbase_list)) / (max(tbase_list) -
                                                             min(tbase_list))
    tbase_list = [tbase * temp2perp_scale for tbase in tbase_list]

    # Get weight matrix
    ttMat1, ttMat2 = np.meshgrid(np.array(tbase_list), np.array(tbase_list))
    ppMat1, ppMat2 = np.meshgrid(np.array(pbase_list), np.array(pbase_list))
    ttMat = np.abs(ttMat1 - ttMat2)  # temporal distance matrix
    ppMat = np.abs(ppMat1 - ppMat2)  # spatial distance matrix

    weightMat = np.sqrt(
        np.square(ttMat) +
        np.square(ppMat))  # 2D distance matrix in temp/perp domain
    weightMat = csr_matrix(weightMat)  # compress sparse row matrix

    # MST path based on weight matrix
    mstMat = minimum_spanning_tree(weightMat)

    # Convert MST index matrix into date12 list
    [s_idx_list, m_idx_list
     ] = [date_idx_array.tolist() for date_idx_array in find(mstMat)[0:2]]
    date12_list = [
        date6_list[m_idx_list[i]] + '-' + date6_list[s_idx_list[i]]
        for i in range(len(m_idx_list))
    ]
    return date12_list
예제 #22
0
def main(argv):
    try:
        File = argv[0]
        atr = readfile.read_attribute(File)
        epoch = argv[1]
    except:
        usage()
        sys.exit(1)

    try:
        outFile = argv[2]
    except:
        outFile = None

    # Calculate look angle
    pbase = ut.perp_baseline_timeseries(atr, dimension=1)

    if pbase.shape[1] == 1:
        print pbase
        return pbase

    k = atr['FILE_TYPE']
    width = int(atr['WIDTH'])
    length = int(atr['FILE_LENGTH'])

    h5 = h5py.File(File, 'r')
    epochList = sorted(h5[k].keys())
    epoch = ptime.yyyymmdd(epoch)
    epoch_idx = epochList.index(epoch)

    pbase_y = pbase[epoch_idx, :].reshape(length, 1)
    pbase_xy = np.tile(pbase_y, (1, width))

    if not outFile:
        outFile = 'perpBaseline_' + epoch + '.h5'

    print 'writing >>> ' + outFile
    atr['FILE_TYPE'] = 'mask'
    atr['UNIT'] = 'm'
    writefile.write(pbase_xy, atr, outFile)
    return outFile
예제 #23
0
def threshold_perp_baseline(date12_list,
                            date_list,
                            pbase_list,
                            pbase_max,
                            pbase_min=0.0):
    '''Remove pairs/interoferogram out of [pbase_min, pbase_max]
    Inputs:
        date12_list : list of string for date12 in YYMMDD-YYMMDD format
        date_list   : list of string for date in YYMMDD/YYYYMMDD format, optional
        pbase_list  : list of float for perpendicular spatial baseline
        pbase_max   : float, maximum perpendicular baseline
        pbase_min   : float, minimum perpendicular baseline
    Output:
        date12_list_out : list of string for date12 in YYMMDD-YYMMDD format
    Example:
        date12_list = threshold_perp_baseline(date12_list, date_list, pbase_list, 500)
    '''
    if not date12_list: return []
    # Get date6_list
    if not date_list:
        m_dates = [date12.split('-')[0] for date12 in date12_list]
        s_dates = [date12.split('-')[1] for date12 in date12_list]
        date_list = sorted(ptime.yyyymmdd(list(set(m_dates + s_dates))))
        if not len(date_list) == len(pbase_list):
            print 'ERROR: number of existing dates is not equal to number of perp baseline!'
            print 'date list is needed for threshold filtering!'
            print 'skip filtering.'
            return date12_list
    date6_list = ptime.yymmdd(date_list)

    # Threshold
    date12_list_out = []
    for date12 in date12_list:
        date1, date2 = date12.split('-')
        idx1 = date6_list.index(date1)
        idx2 = date6_list.index(date2)
        pbase = abs(pbase_list[idx1] - pbase_list[idx2])
        if pbase_min <= pbase <= pbase_max:
            date12_list_out.append(date12)
    return date12_list_out
예제 #24
0
def check_exclude_date(exDateIn, dateList):
    '''Read exclude dates info
    Inputs:
        exDateIn - list of string, date in YYMMDD or YYYYMMDD format,
                   or text file with date in it
        dateList - list of string, date in YYYYMMDD format
    Output:
        exDateOut - list of string, date in YYYYMMDD format
    '''
    if not exDateIn:
        return []

    exDateOut = []
    for exDate in exDateIn:
        if os.path.isfile(exDate):
            exDate = ptime.read_date_list(exDate)
        else:
            exDate = [ptime.yyyymmdd(exDate)]
        exDateOut += exDate
    exDateOut = sorted(list(set(exDateOut).intersection(dateList)))
    print 'Exclude date for DEM error estimation:'
    print exDateOut
    return exDateOut
예제 #25
0
def main(argv):
    inps = cmdLineParse()

    atr = readfile.read_attribute(inps.velocity_file)
    length = int(atr['FILE_LENGTH'])
    width = int(atr['WIDTH'])

    # Check subset input
    if inps.subset_y:
        inps.subset_y = sorted(inps.subset_y)
        print 'subset in y/azimuth direction: '+str(inps.subset_y)
    else:
        inps.subset_y = [0, length]

    if inps.subset_x:
        inps.subset_x = sorted(inps.subset_x)
        print 'subset in x/range direction: '+str(inps.subset_x)
    else:
        inps.subset_x = [0, width]
    y0, y1 = inps.subset_y
    x0, x1 = inps.subset_x

    # Read velocity/rate
    velocity = readfile.read(inps.velocity_file)[0]
    print 'read velocity file: '+inps.velocity_file

    k = 'interferograms'
    h5 = h5py.File(inps.ifgram_file, 'r')
    ifgram_list = sorted(h5[k].keys())
    ifgram_num = len(ifgram_list)
    date12_list = ptime.list_ifgram2date12(ifgram_list)
    print 'number of interferograms: '+str(ifgram_num)

    ##### Select interferograms with unwrapping error
    if inps.percentage > 0.0:
        mask = readfile.read(inps.mask_file, epoch='mask')[0]
        print 'read mask for pixels with unwrapping error from file: '+inps.mask_file

        unw_err_ifgram_num = int(np.rint(inps.percentage*ifgram_num))
        unw_err_ifgram_idx = random.sample(range(ifgram_num), unw_err_ifgram_num)
        unw_err_ifgram_list = [ifgram_list[i] for i in unw_err_ifgram_idx]
        unw_err_date12_list = [date12_list[i] for i in unw_err_ifgram_idx]
        print 'randomly choose the following %d interferograms with unwrapping error' % unw_err_ifgram_num
        print unw_err_date12_list

        unit_unw_err = 2.0*np.pi*mask
    else:
        unw_err_ifgram_list = []

    ###### Generate simulated interferograms
    m_dates = ptime.yyyymmdd([i.split('-')[0] for i in date12_list])
    s_dates = ptime.yyyymmdd([i.split('-')[1] for i in date12_list])
    range2phase = -4.0*np.pi/float(atr['WAVELENGTH'])

    print 'writing simulated interferograms file: '+inps.outfile
    h5out=h5py.File(inps.outfile,'w') 
    group = h5out.create_group('interferograms')
    for i in range(ifgram_num):
        ifgram = ifgram_list[i]
        # Get temporal baseline in years
        t1 = datetime.datetime(*time.strptime(m_dates[i],"%Y%m%d")[0:5])
        t2 = datetime.datetime(*time.strptime(s_dates[i],"%Y%m%d")[0:5])
        dt = (t2-t1)
        dt = float(dt.days)/365.25

        # Simuated interferograms with unwrap error
        unw = velocity*dt*range2phase
        if ifgram in unw_err_ifgram_list:
            rand_int = random.sample(range(1,10),1)[0]
            unw += rand_int * unit_unw_err
            print ifgram+'  - add unwrapping error of %d*2*pi' % rand_int
        else:
            print ifgram

        gg = group.create_group(ifgram)
        dset = gg.create_dataset(ifgram, data=unw[y0:y1,x0:x1], compression='gzip')

        for key, value in h5[k][ifgram].attrs.iteritems():
            gg.attrs[key] = value
        if ifgram in unw_err_ifgram_list:
            gg.attrs['unwrap_error'] = 'yes'
        else:
            gg.attrs['unwrap_error'] = 'no'
        gg.attrs['FILE_LENGTH'] = y1-y0
        gg.attrs['WIDTH']       = x1-x0
    h5.close()
    h5out.close()
    print 'Done.'
    return inps.outfile
예제 #26
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()
예제 #27
0
def main(argv):
    try:
        File = argv[0]
    except:
        usage()
        sys.exit(1)

    atr = readfile.read_attribute(File)
    k = atr['FILE_TYPE']

    h5file = h5py.File(File, 'r')
    print '\n************* Output to ROI_PAC format ***************'

    if k == 'velocity':
        dset = h5file['velocity'].get('velocity')
        data = dset[0:dset.shape[0], 0:dset.shape[1]]
        print "converting velocity to a 1 year interferogram."
        wvl = float(h5file[k].attrs['WAVELENGTH'])
        data = (-4 * pi / wvl) * data

        outname = File.split('.')[0] + '.unw'
        writefile.write(data, atr, outname)

    elif k == 'timeseries':
        dateList = h5file['timeseries'].keys()
        ## Input
        if len(sys.argv) == 2:
            print 'No input date specified >>> continue with the last date'
            dateList = h5file['timeseries'].keys()
            d = dateList[-1]
        elif len(sys.argv) == 3:
            d = sys.argv[2]
        elif len(sys.argv) == 4:
            ds = sorted(sys.argv[2:4])
            d_ref = ds[0]
            d = ds[1]
        else:
            usage()
            sys.exit(1)
        d = ptime.yyyymmdd(d)
        try:
            d_ref = ptime.yyyymmdd(d_ref)
        except:
            pass

        ## Data
        print 'reading ' + d + ' ... '
        data = h5file['timeseries'].get(d)[:]
        try:
            print 'reading ' + d_ref + ' ... '
            data_ref = h5file['timeseries'].get(d_ref)[:]
            data = data - data_ref
        except:
            pass
        wvl = float(atr['WAVELENGTH'])
        data *= -4 * pi / wvl

        ## outName
        try:
            master_d = d_ref
        except:
            try:
                master_d = atr['ref_date']
            except:
                master_d = atr['DATE']
        if len(master_d) == 8: master_d = master_d[2:8]
        if len(d) == 8: d = d[2:8]
        outname = master_d + '_' + d + '.unw'

        ## Attributes
        atr['FILE_TYPE'] = '.unw'
        atr['P_BASELINE_TIMESERIES'] = '0.0'
        atr['UNIT'] = 'radian'
        atr['DATE'] = master_d
        atr['DATE12'] = master_d + '-' + d

        ## Writing
        writefile.write(data, atr, outname)

    elif k in ['interferograms', 'coherence', 'wrapped']:
        ## Check input
        igramList = h5file[k].keys()
        try:
            d = sys.argv[2]
            for i in range(len(igramList)):
                if d in igramList[i]:
                    igram = igramList[i]
        except:
            igram = igramList[-1]
            print 'No input date specified >>> continue with the last date'
        ## Read and Write
        print 'reading ' + igram + ' ... '
        data = h5file[k][igram].get(igram)[:]
        atr = h5file[k][igram].attrs
        outname = igram

        print 'writing >>> ' + outname
        writefile.write(data, atr, outname)

    else:
        dset = h5file[k].get(k)
        data = dset[0:dset.shape[0], 0:dset.shape[1]]
        if k == 'temporal_coherence': outname = File.split('.')[0] + '.cor'
        else: outname = File.split('.')[0] + '.unw'

        writefile.write(data, atr, outname)

    h5file.close()
    return
예제 #28
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() 
예제 #29
0
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
예제 #30
0
파일: tsviewer.py 프로젝트: yunjunz/PySAR
def main(argv):

    ## Default settings
    contour_step = 200.0
    contour_sigma = 3.0
    demShade = "yes"
    demContour = "yes"

    global markerSize, markderSize2, markerColor, markerColor2, rectColor
    global lineWidth, lineWidth2, edgeWidth, fontSize
    # global markerColor_ref, markerColor_ref2

    markerSize = 16
    markerSize2 = 16
    markerColor = "crimson"  # g
    markerColor2 = "lightgray"
    markerColor_ref = "white"
    markerColor_ref2 = "lightgray"
    rectColor = "black"
    lineWidth = 0
    lineWidth2 = 0
    edgeWidth = 1.5
    fontSize = 16

    global unit, radius, saveFig, dispFig, fig_dpi

    fig_dpi = 300
    radius = 0
    saveFig = "no"
    dispFig = "yes"
    unit = "cm"

    dispDisplacement = "no"
    dispOpposite = "no"
    dispContour = "only"
    smoothContour = "no"
    contour_step = 200
    showRef = "yes"
    vel_alpha = 1.0
    zero_start = "yes"

    global ref_xsub, ref_ysub, ref_date
    global h5timeseries_2, dates_2, dateList_2
    global lbound, hbound

    ############### Check Inputs ##################
    if len(sys.argv) < 2:
        Usage()
        sys.exit(1)
    elif len(sys.argv) == 2:
        if argv[0] == "-h":
            Usage()
            sys.exit(1)
        elif os.path.isfile(argv[0]):
            timeSeriesFile = argv[0]
            h5timeseries = h5py.File(timeSeriesFile)
            k = h5timeseries.keys()
            if not "timeseries" in k:
                print "ERROR: Input file is " + k[0] + ".\n\tOnly timeseries is supported.\n"
                sys.exit(1)
        else:
            Usage()
            sys.exit(1)

    elif len(sys.argv) > 2:
        try:
            opts, args = getopt.getopt(
                argv,
                "f:F:v:a:b:s:m:c:w:u:l:h:D:V:t:T:d:r:x:y:X:Y:o:E:",
                [
                    "save",
                    "nodisplay",
                    "unit=",
                    "exclude=",
                    "ref-date=",
                    "rect-color=",
                    "zero-start=",
                    "zoom-x=",
                    "zoom-y=",
                    "zoom-lon",
                    "zoom-lat",
                    "lalo=",
                    "opposite",
                    "dem-nocontour",
                    "dem-noshade",
                    "displacement",
                    "contour-step=",
                    "contour-smooth=",
                    "LALO=",
                ],
            )
        except getopt.GetoptError:
            Usage()
            sys.exit(1)

        for opt, arg in opts:
            if opt == "-f":
                timeSeriesFile = arg
            elif opt == "-F":
                timeSeriesFile_2 = arg
            elif opt == "-v":
                velocityFile = arg
            elif opt == "-a":
                vmin = float(arg)
            elif opt == "-b":
                vmax = float(arg)
            elif opt == "-s":
                fontSize = int(arg)
            elif opt == "-m":
                markerSize = int(arg)
                markerSize2 = int(arg)
            elif opt == "-c":
                markerColor = arg
            elif opt == "-w":
                lineWidth = int(arg)
            elif opt == "-u":
                unit = arg
            elif opt == "-l":
                lbound = float(arg)
            elif opt == "-h":
                hbound = float(arg)
            elif opt == "-D":
                demFile = arg
            elif opt == "-V":
                contour_step = float(arg)
            elif opt == "-t":
                minDate = arg
            elif opt == "-T":
                maxDate = arg
            elif opt == "-r":
                radius = abs(int(arg))
            elif opt == "-x":
                xsub = [int(i) for i in arg.split(":")]
                xsub.sort()
                # dispVelFig='no'
            elif opt == "-y":
                ysub = [int(i) for i in arg.split(":")]
                ysub.sort()
                # dispVelFig='no'
            elif opt == "-X":
                ref_xsub = [int(i) for i in arg.split(":")]
                ref_xsub.sort()
            elif opt == "-Y":
                ref_ysub = [int(i) for i in arg.split(":")]
                ref_ysub.sort()
                # dispVelFig='no'

            elif opt == "--contour-step":
                contour_step = float(arg)
            elif opt == "--contour-smooth":
                contour_sigma = float(arg)
            elif opt == "--dem-nocontour":
                demContour = "no"
            elif opt == "--dem-noshade":
                demShade = "no"
            elif opt == "--displacement":
                dispDisplacement = "yes"
            elif opt in ["-E", "--exclude"]:
                datesNot2show = arg.split(",")
            elif opt in "--lalo":
                lalosub = [float(i) for i in arg.split(",")]
            elif opt in "--LALO":
                ref_lalosub = [float(i) for i in arg.split(",")]
            elif opt in ["--rect-color"]:
                rectColor = arg
            elif opt in ["--ref-date"]:
                ref_date = ptime.yyyymmdd(arg)
            elif opt in ["-u", "--unit"]:
                unit = arg.lower()
            elif opt == "--save":
                saveFig = "yes"
            elif opt == "--nodisplay":
                dispFig = "no"
                saveFig = "yes"
            elif opt == "--opposite":
                dispOpposite = "yes"
            elif opt == "--zero-start":
                zero_start = arg.lower()
            elif opt == "--zoom-x":
                win_x = [int(i) for i in arg.split(":")]
                win_x.sort()
            elif opt == "--zoom-y":
                win_y = [int(i) for i in arg.split(":")]
                win_y.sort()
            elif opt == "--zoom-lon":
                win_lon = [float(i) for i in arg.split(":")]
                win_lon.sort()
            elif opt == "--zoom-lat":
                win_lat = [float(i) for i in arg.split(":")]
                win_lat.sort()

    ##############################################################
    ## Read time series file info
    if not os.path.isfile(timeSeriesFile):
        print "\nERROR: Input time series file does not exist: " + timeSeriesFile + "\n"
        sys.exit(1)
    h5timeseries = h5py.File(timeSeriesFile)
    k = h5timeseries.keys()
    # read h5 file and its group type
    if not "timeseries" in k:
        print "ERROR: Input file is " + k[0] + ".\n\tOnly timeseries is supported.\n"
        sys.exit(1)

    atr = readfile.read_attributes(timeSeriesFile)
    dateList1 = h5timeseries["timeseries"].keys()
    dateList1 = sorted(dateList1)
    dates1, datevector1 = ptime.date_list2vector(dateList1)
    print "\n************ Time Series Display - Point *************"

    ##### Select Check
    try:
        lalosub
        xsub = subset.coord_geo2radar([lalosub[1]], atr, "longitude")
        ysub = subset.coord_geo2radar([lalosub[0]], atr, "latitude")
        xsub = [xsub]
        ysub = [ysub]
        if radius == 0:
            radius = 3
    except:
        pass

    try:
        ref_lalosub
        ref_xsub = subset.coord_geo2radar([ref_lalosub[1]], atr, "longitude")
        ref_ysub = subset.coord_geo2radar([ref_lalosub[0]], atr, "latitude")
        ref_xsub = [ref_xsub]
        ref_ysub = [ref_ysub]
        if radius == 0:
            radius = 3
    except:
        pass

    ##############################################################
    global dates, dateList, datevector_all, dateListMinMax

    print "*******************"
    print "All dates existed:"
    print dateList1
    print "*******************"

    ## Check exclude date input
    try:
        datesNot2show
        if os.path.isfile(datesNot2show[0]):
            try:
                datesNot2show = ptime.read_date_list(datesNot2show[0])
            except:
                print "Can not read date list file: " + datesNot2show[0]
        print "dates not to show: " + str(datesNot2show)
    except:
        datesNot2show = []

    ## Check Min / Max Date
    dateListMinMax = []
    try:
        minDate
        minDate = ptime.yyyymmdd(minDate)
        dateListMinMax.append(minDate)
        minDateyy = ptime.yyyymmdd2years(minDate)
        print "minimum date: " + minDate
        for date in dateList1:
            yy = ptime.yyyymmdd2years(date)
            if yy < minDateyy:
                datesNot2show.append(date)
    except:
        pass
    try:
        maxDate
        maxDate = ptime.yyyymmdd(maxDate)
        dateListMinMax.append(maxDate)
        maxDateyy = ptime.yyyymmdd2years(maxDate)
        print "maximum date: " + maxDate
        for date in dateList1:
            yy = ptime.yyyymmdd2years(date)
            if yy > maxDateyy:
                datesNot2show.append(date)
    except:
        pass

    dateListMinMax = sorted(dateListMinMax)
    if not dateListMinMax:
        print "no min/max date input."
    else:
        datesMinMax, dateVecMinMax = ptime.date_list2vector(dateListMinMax)

    ## Finalize Date List
    try:
        dateList = []
        for date in dateList1:
            if date not in datesNot2show:
                dateList.append(date)
        print "--------------------------------------------"
        print "dates used to show time series displacements:"
        print dateList
        print "--------------------------------------------"
    except:
        dateList = dateList1
        print "using all dates to show time series displacement"

    ## Read Date Info (x axis for time series display)
    dates, datevector = ptime.date_list2vector(dateList)
    datevector_all = list(datevector)

    ## Check reference date input
    try:
        ref_date
        if not ref_date in dateList:
            print "Reference date - " + ref_date + " - is not included in date list to show."
            sys.exit(1)
        else:
            print "reference date: " + ref_date
    except:
        if zero_start == "yes":
            ref_date = dateList[0]
            print "set the 1st date as reference for displacement display."
        else:
            pass

    ##############################################################
    ##### Plot Fig 1 - Velocity / last epoch of time series / DEM
    fig = plt.figure(1)
    ax = fig.add_subplot(111)

    ##### Check subset range
    width = int(atr["WIDTH"])
    length = int(atr["FILE_LENGTH"])
    print "file size: " + str(length) + ", " + str(width)
    try:
        win_y = subset.coord_geo2radar(win_lat, atr, "latitude")
    except:
        try:
            win_y
        except:
            win_y = [0, length]
    try:
        win_x = subset.coord_geo2radar(win_lon, atr, "longitude")
    except:
        try:
            win_x
        except:
            win_x = [0, width]
    win_y, win_x = subset.check_subset_range(win_y, win_x, atr)

    try:
        velocityFile
        try:
            vel, vel_atr = readfile.read(velocityFile)
        except:
            vel, vel_atr = readfile.read(timeSeriesFile, velocityFile)
        ax.set_title(velocityFile)
        print "display: " + velocityFile
    except:
        vel, vel_atr = readfile.read(timeSeriesFile, dateList1[-1])
        ax.set_title("epoch: " + dateList1[-1])
        print "display last epoch"

    ##### show displacement instead of phase
    if vel_atr["FILE_TYPE"] in ["interferograms", ".unw"] and dispDisplacement == "yes":
        print "show displacement"
        phase2range = -float(vel_atr["WAVELENGTH"]) / (4 * np.pi)
        vel *= phase2range
    else:
        dispDisplacement = "no"

    ## Reference Point
    if showRef == "yes":
        try:
            ax.plot(int(atr["ref_x"]), int(atr["ref_y"]), "ks", ms=6)
        except:
            pass

    if dispOpposite == "yes":
        print "show opposite value in figure/map 1"
        vel *= -1

    ## Flip
    try:
        flip_lr
    except:
        try:
            flip_ud
        except:
            flip_lr, flip_ud = view.auto_flip_check(atr)

    ## Status bar
    ## Geo coordinate
    try:
        ullon = float(atr["X_FIRST"])
        ullat = float(atr["Y_FIRST"])
        lon_step = float(atr["X_STEP"])
        lat_step = float(atr["Y_STEP"])
        lon_unit = atr["Y_UNIT"]
        lat_unit = atr["X_UNIT"]
        geocoord = "yes"
        print "Input file is Geocoded"
    except:
        geocoord = "no"

    def format_coord(x, y):
        col = int(x + 0.5)
        row = int(y + 0.5)
        if col >= 0 and col <= width and row >= 0 and row <= length:
            z = vel[row, col]
            try:
                lon = ullon + x * lon_step
                lat = ullat + y * lat_step
                return "x=%.1f, y=%.1f, value=%.4f, lon=%.4f, lat=%.4f" % (x, y, z, lon, lat)
            except:
                return "x=%.1f, y=%.1f, value=%.4f" % (x, y, z)

    ax.format_coord = format_coord

    ## DEM
    try:
        demFile
        dem, demRsc = readfile.read(demFile)
        ax = view.plot_dem_yx(ax, dem, demShade, demContour, contour_step, contour_sigma)
        vel_alpha = 0.8
    except:
        print "No DEM file"

    try:
        img = ax.imshow(vel, vmin=vmin, vmax=vmax, alpha=vel_alpha)
    except:
        img = ax.imshow(vel, alpha=vel_alpha)
    plt.colorbar(img)

    ## Zoom In (subset)
    if flip_lr == "yes":
        ax.set_xlim(win_x[1], win_x[0])
    else:
        ax.set_xlim(win_x[0], win_x[1])
    if flip_ud == "yes":
        ax.set_ylim(win_y[0], win_y[1])
    else:
        ax.set_ylim(win_y[1], win_y[0])

    ## Flip
    # if flip_lr == 'yes':  fig.gca().invert_xaxis()
    # if flip_ud == 'yes':  fig.gca().invert_yaxis()

    ##########################################
    ##### Plot Fig 2 - Time series plot
    # fig2 = plt.figure(num=2,figsize=(12,6))
    fig2 = plt.figure(2, figsize=(12, 6))
    ax2 = fig2.add_subplot(111)

    try:
        timeSeriesFile_2
        h5timeseries_2 = h5py.File(timeSeriesFile_2)
        dateList_2 = h5timeseries_2["timeseries"].keys()
        dateList_2 = sorted(dateList_2)
        dates_2, datevector_2 = ptime.date_list2vector(dateList_2)
        datevector_all += list(set(datevector_2) - set(datevector_all))
        datevector_all = sorted(datevector_all)
    except:
        pass

    ################################  Plot Code Package <start> #################################
    def plot_ts(ax, ax2, fig2, xsub, ysub, h5timeseries):
        ax2.cla()
        print "\n-------------------------------------------------------------------------------"
        disp_min = 0
        disp_max = 0

        ############################# Plot Time Series ##############################
        global ref_xsub, ref_ysub
        ##### 1.1 Plot Reference time series
        try:
            ref_xsub
            ref_ysub
            ref_xsub, ref_ysub = check_yx(ref_xsub, ref_ysub, radius, ax, rectColor)
            print "----------------------------------------------------"
            print "Reference Point:"
            print "ref_x=" + str(ref_xsub[0]) + ":" + str(ref_xsub[1])
            print "ref_y=" + str(ref_ysub[0]) + ":" + str(ref_ysub[1])

            print "-----------------------------"
            print "Time series with all dates:"
            dis1, dis1_mean, dis1_std, dis1_vel = read_dis(ref_xsub, ref_ysub, dateList1, h5timeseries, unit)
            (_, caps, _) = ax2.errorbar(
                dates1,
                dis1_mean,
                yerr=dis1_std,
                fmt="-ks",
                ms=markerSize2,
                lw=0,
                alpha=1,
                mfc=markerColor_ref,
                mew=edgeWidth,
                elinewidth=edgeWidth,
                ecolor="black",
                capsize=markerSize * 0.5,
            )
            for cap in caps:
                cap.set_markeredgewidth(edgeWidth)
            disp_min, disp_max = update_lim(disp_min, disp_max, dis1_mean, dis1_std)

            if not len(dateList) == len(dateList1):
                print "-----------------------------"
                print "Time series with dates of interest:"
                dis12, dis12_mean, dis12_std, dis12_vel = read_dis(ref_xsub, ref_ysub, dateList, h5timeseries, unit)
                (_, caps, _) = ax2.errorbar(
                    dates,
                    dis12_mean,
                    yerr=dis12_std,
                    fmt="-ks",
                    ms=markerSize2,
                    lw=0,
                    alpha=1,
                    mfc=markerColor_ref2,
                    mew=edgeWidth,
                    elinewidth=edgeWidth,
                    ecolor="black",
                    capsize=markerSize * 0.5,
                )
                for cap in caps:
                    cap.set_markeredgewidth(edgeWidth)
                disp_min, disp_max = update_lim(disp_min, disp_max, dis12_mean, dis12_std)

        except:
            pass

        ##### 1.2.0 Read y/x
        print "\n----------------------------------------------------"
        print "Point of Interest:"
        xsub, ysub = check_yx(xsub, ysub, radius, ax, rectColor)
        print "x=" + str(xsub[0]) + ":" + str(xsub[1])
        print "y=" + str(ysub[0]) + ":" + str(ysub[1])

        ##### 1.2.1 Plot 2nd time series
        try:
            timeSeriesFile_2
            print "-----------------------------"
            print "2nd Time Series:"
            dis2, dis2_mean, dis2_std, dis2_vel = read_dis(xsub, ysub, dateList_2, h5timeseries_2, unit)
            (_, caps, _) = ax2.errorbar(
                dates_2,
                dis2_mean,
                yerr=dis2_std,
                fmt="-ko",
                ms=markerSize2,
                lw=0,
                alpha=1,
                mfc=markerColor2,
                elinewidth=0,
                ecolor="black",
                capsize=0,
            )
            for cap in caps:
                cap.set_markeredgewidth(edgeWidth)
            disp_min, disp_max = update_lim(disp_min, disp_max, dis2_mean, dis2_std)
        except:
            pass

        ##### 1.2.2 Plot 1st time series
        print "-----------------------------"
        print "Time Series:"
        dis, dis_mean, dis_std, dis_vel = read_dis(xsub, ysub, dateList, h5timeseries, unit)
        (_, caps, _) = ax2.errorbar(
            dates,
            dis_mean,
            yerr=dis_std,
            fmt="-ko",
            ms=markerSize,
            lw=lineWidth,
            alpha=1,
            mfc=markerColor,
            elinewidth=edgeWidth,
            ecolor="black",
            capsize=markerSize * 0.5,
        )
        for cap in caps:
            cap.set_markeredgewidth(edgeWidth)
        disp_min, disp_max = update_lim(disp_min, disp_max, dis_mean, dis_std)

        ####################### Figure Format #######################
        ## x axis format
        try:
            ax2 = ptime.adjust_xaxis_date(ax2, dateVecMinMax, fontSize)
        except:
            ax2 = ptime.adjust_xaxis_date(ax2, datevector_all, fontSize)

        ## y axis format
        ax2.set_ylabel("Displacement [" + unit + "]", fontsize=fontSize)
        try:
            lbound
            hbound
            ax2.set_ylim(lbound, hbound)
        except:
            disp_buf = 0.2 * (disp_max - disp_min)
            ax2.set_ylim(disp_min - disp_buf, disp_max + disp_buf)
        for tick in ax2.yaxis.get_major_ticks():
            tick.label.set_fontsize(fontSize)

        ## title
        figTitle = "x=" + str(xsub[0]) + ":" + str(xsub[1]) + ", y=" + str(ysub[0]) + ":" + str(ysub[1])
        try:
            lonc = ullon + (xsub[0] + xsub[1]) / 2.0 * lon_step
            latc = ullat + (ysub[0] + ysub[1]) / 2.0 * lat_step
            figTitle += ", lalo=" + "%.4f,%.4f" % (latc, lonc)
        except:
            pass
        ax2.set_title(figTitle)

        ################## Save and Output #####################
        if saveFig == "yes":
            print "-----------------------------"
            Delay = {}
            Delay["displacement"] = dis
            Delay["unit"] = unit
            Delay["time"] = datevector
            Delay["velocity"] = dis_vel[0]
            Delay["velocity_unit"] = unit + "/yr"
            Delay["velocity_std"] = dis_vel[4]
            figBase = "x" + str(xsub[0]) + "_" + str(xsub[1] - 1) + "y" + str(ysub[0]) + "_" + str(ysub[1] - 1)
            sio.savemat(figBase + "_ts.mat", {"displacement": Delay})
            print "saved " + figBase + "_ts.mat"
            fig2.savefig(figBase + "_ts.pdf", bbox_inches="tight", transparent=True, dpi=fig_dpi)
            print "saved " + figBase + "_ts.pdf"
            if dispFig == "no":
                fig.savefig(figBase + "_vel.png", bbox_inches="tight", transparent=True, dpi=fig_dpi)
                print "saved " + figBase + "_vel.png"

    ################################  Plot Code Package <end> #################################

    ########### 1. Plot Time Series with x/y ##########
    try:
        xsub
        ysub
        plot_ts(ax, ax2, fig2, xsub, ysub, h5timeseries)
    except:
        print "No x/y input"
        pass

    ########### 2. Plot Time Series with Click ##########
    ## similar to 1. Plot Time Series with x/y

    def onclick(event):
        ax2.cla()
        xsub = [int(event.xdata)]
        ysub = [int(event.ydata)]
        plot_ts(ax, ax2, fig2, xsub, ysub, h5timeseries)

        if dispFig == "yes":
            plt.show()

    try:
        cid = fig.canvas.mpl_connect("button_press_event", onclick)
    except:
        pass

    if dispFig == "yes":
        plt.show()
예제 #31
0
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
예제 #32
0
def correct_lod_file(File, outFile=None):
    # Check Sensor Type
    print 'input file: ' + File
    atr = readfile.read_attribute(File)
    k = atr['FILE_TYPE']
    platform = atr['PLATFORM']
    print 'platform: ' + platform
    if not platform.lower() in ['env', 'envisat']:
        print 'No need to correct LOD for ' + platform
        sys.exit(1)

    # Output Filename
    if not outFile:
        ext = os.path.splitext(File)[1]
        outFile = os.path.splitext(File)[0] + '_LODcor' + ext

    # Get LOD phase ramp from empirical model
    width = int(atr['WIDTH'])
    length = int(atr['FILE_LENGTH'])
    range_resolution = float(atr['RANGE_PIXEL_SIZE'])

    r = np.linspace(0, width - 1, width)
    R = range_resolution * r * (3.87e-7)
    Ramp = np.tile(R, [length, 1])

    yref = int(atr['ref_y'])
    xref = int(atr['ref_x'])
    Ramp -= Ramp[yref][xref]

    # Correct LOD Ramp for Input File
    if k in multi_group_hdf5_file + multi_dataset_hdf5_file:
        h5 = h5py.File(File, 'r')
        epochList = sorted(h5[k].keys())

        h5out = h5py.File(outFile, 'w')
        group = h5out.create_group(k)

        if k in ['interferograms', 'wrapped']:
            print 'number of interferograms: ' + str(len(epochList))
            wvl = float(atr['WAVELENGTH'])
            Ramp *= -4 * np.pi / wvl
            for epoch in epochList:
                print epoch
                data = h5[k][epoch].get(epoch)[:]
                atr = h5[k][epoch].attrs

                dates = ptime.yyyymmdd(atr['DATE12'].split('-'))
                dates = ptime.yyyymmdd2years(dates)
                dt = date[1] - date[0]
                data -= Ramp * dt

                gg = group.create_group(epoch)
                dset = gg.create_dataset(epoch, data=data, compression='gzip')
                for key, value in atr.iteritems():
                    gg.attrs[key] = value

        elif k == 'timeseries':
            print 'number of acquisitions: ' + str(len(epochList))
            tbase = [
                float(dy) / 365.25
                for dy in ptime.date_list2tbase(epochList)[0]
            ]
            for i in range(len(epochList)):
                epoch = epochList[i]
                print epoch
                data = h5[k].get(epoch)[:]

                data -= Ramp * tbase[i]

                dset = group.create_dataset(epoch,
                                            data=data,
                                            compression='gzip')
            for key, value in atr.iteritems():
                group.attrs[key] = value
        else:
            print 'No need to correct for LOD for ' + k + ' file'
            sys.exit(1)

        h5.close()
        h5out.close()

    else:
        data, atr = readfile.read(File)
        data -= Ramp
        writefile.write(data, atr, outFile)

    return outFile
예제 #33
0
파일: save_unw.py 프로젝트: yunjunz/PySAR
def main(argv):
    try:    File=argv[0]
    except: Usage();sys.exit(1)
  
    atr = readfile.read_attributes(File)
    k = atr['FILE_TYPE']
  
    h5file=h5py.File(File,'r')
    print '\n************* Output to ROI_PAC format ***************'
  
    if k == 'velocity':
        dset = h5file['velocity'].get('velocity')
        data = dset[0:dset.shape[0],0:dset.shape[1]]
        print "converting velocity to a 1 year interferogram."
        wvl=float(h5file[k].attrs['WAVELENGTH'])
        data=(-4*pi/wvl)*data
    
        outname=File.split('.')[0]+'.unw'
        writefile.write(data,atr,outname)
  
    elif k == 'timeseries':
        dateList=h5file['timeseries'].keys() 
        ## Input
        if   len(sys.argv)==2:
            print 'No input date specified >>> continue with the last date'
            dateList=h5file['timeseries'].keys()
            d=dateList[-1]
        elif len(sys.argv)==3:
            d=sys.argv[2]
        elif len(sys.argv)==4:
            ds=sys.argv[2:4]; ds.sort()
            d_ref = ds[0]
            d     = ds[1]
        else: Usage(); sys.exit(1)
        d = ptime.yyyymmdd(d)
        try: d_ref = ptime.yyyymmdd(d_ref)
        except: pass
    
        ## Data
        print 'reading '+d+' ... '
        data = h5file['timeseries'].get(d)[:]
        try:
            print 'reading '+d_ref+' ... '
            data_ref = h5file['timeseries'].get(d_ref)[:]
            data = data - data_ref
        except: pass
        wvl=float(atr['WAVELENGTH'])
        data *= -4*pi/wvl
    
        ## outName
        try:      master_d = d_ref
        except:
            try:    master_d = atr['ref_date']
            except: master_d = atr['DATE']
        if len(master_d)==8:  master_d=master_d[2:8]
        if len(d)==8:         d=d[2:8]
        outname = master_d+'_'+d+'.unw'
    
        ## Attributes
        atr['FILE_TYPE']             = '.unw'
        atr['P_BASELINE_TIMESERIES'] = '0.0'
        atr['UNIT']                  = 'radian'
        atr['DATE']                  = master_d
        atr['DATE12']                = master_d+'-'+d
        
        ## Writing
        writefile.write(data,atr,outname)

    elif k in ['interferograms','coherence','wrapped']:
        ## Check input
        igramList=h5file[k].keys()
        try:
            d = sys.argv[2]
            for i in range(len(igramList)):
                if d in igramList[i]:
                    igram = igramList[i]
        except:
            igram = igramList[-1];   print 'No input date specified >>> continue with the last date'
        ## Read and Write
        print 'reading '+igram+' ... '
        dset = h5file[k][igram].get(igram)
        data = dset[0:dset.shape[0],0:dset.shape[1]]
        outname = igram
        print 'writing >>> '+ outname
        writefile.write_float32(data,outname)
        f = open(outname+'.rsc','w')
        for key , value in h5file[k][igram].attrs.iteritems():
            f.write(key+'    '+str(value)+'\n')
        f.close()    
  
  
    else:
        dset = h5file[k].get(k)
        data = dset[0:dset.shape[0],0:dset.shape[1]]
        if k == 'temporal_coherence': outname=File.split('.')[0]+'.cor'
        else:                         outname=File.split('.')[0]+'.unw'
    
        writefile.write(data,atr,outname)
  
  
    h5file.close()