Exemplo n.º 1
0
def stacking(File):
    ## Stack multi-temporal dataset into one
    ##    equivalent to temporal sum

    ## File Info
    atr = readfile.read_attributes(File)
    k = atr['FILE_TYPE']
    length = int(atr['FILE_LENGTH'])
    width  = int(atr['WIDTH'])

    ## Calculation
    stack  = np.zeros([length,width])
    if k in ['timeseries','interferograms','wrapped','coherence']:
        ##### Input File Info
        h5file = h5py.File(File,'r')
        epochList = h5file[k].keys()
        epochList = sorted(epochList)
        epochNum  = len(epochList)
        for i in range(epochNum):
            epoch = epochList[i]
            if k == 'timeseries':  data = h5file[k].get(epoch)[:]
            else:                  data = h5file[k][epoch].get(epoch)[:]
            stack += data
            printProgress(i+1,epochNum)
        h5file.close()

    else:
        try: stack, atrStack = readfile.read(File)
        except: print 'Cannot read file: '+File; sys.exit(1)

    return stack
Exemplo n.º 2
0
def main(argv):
    try: timeSeriesFile=argv[0]
    except: Usage() ; sys.exit(1)

    try:    outname=argv[1]
    except: outname='sum_'+timeSeriesFile

    ##################################################
    print "\n*************** Calculating Sum of Epochs ****************"
    atr = readfile.read_attributes(timeSeriesFile)
    k = atr['FILE_TYPE']
    print "Loading time series: " + timeSeriesFile
    h5timeseries=h5py.File(timeSeriesFile)
    dateList = h5timeseries['timeseries'].keys()
    dateList = sorted(dateList)

    dateIndex={}
    for ni in range(len(dateList)):
        dateIndex[dateList[ni]]=ni

    length = int(atr['FILE_LENGTH'])
    width  = int(atr['WIDTH'])
    D = np.zeros((len(dateList),length*width),np.float32)

    for date in dateList:
        print date
        d = h5timeseries['timeseries'].get(date)[:]
        D[dateIndex[date]][:]=d.flatten(0)

    ##################################################
    ## Calculate Sum
    lt=len(dateList)
    sumD=np.zeros(D.shape)
    print 'calculating epochs sum ...'
    for j in range(lt):
        print j
        sumD[j,:] = np.sum(np.abs(D-D[j,:]),0)/lt

    ## Normalize to 0 and 1
    ## with high atmosphere equal to 0 and no atmosphere equal to 1
    sumD -= np.max(sumD,0)
    sumD *= -1
    sumD /= np.max(sumD,0)
    sumD[np.isnan(sumD)] = 1

    ##################################################
    print 'writing to >>> '+outname
    h5sum = h5py.File(outname,'w')
    group = h5sum.create_group('timeseries')
    for date in dateList:
        print date
        d = np.reshape(sumD[dateIndex[date]][:],[length,width])
        dset = group.create_dataset(date, data=d, compression='gzip')

    for key,value in atr.iteritems():
        group.attrs[key] = value

    print 'Done.'
Exemplo n.º 3
0
def main(argv):

    inversion_method = 'l2'
    #maskFile = 'Mask.h5'
  
    if len(sys.argv)>2:
        try:   opts, args = getopt.getopt(argv,"h:f:l:o:")
        except getopt.GetoptError:
            Usage() ; sys.exit(1)
  
        for opt,arg in opts:
            if opt in ("-h","--help"):  Usage();   sys.exit()
            elif opt == '-f':           igramsFile        = arg
            elif opt == '-l':           inversion_method  = arg.lower()
            elif opt == '-o':           timeseriesFile    = arg
  
    elif len(sys.argv)==2:
        if os.path.isfile(argv[0]):     igramsFile = argv[0]
        else:  Usage(); sys.exit(1)
    else:  Usage(); sys.exit(1)
    
    #try:     igramsFile = argv[0]
    #except:  Usage() ; sys.exit(1)
    #try:     inversion_method = argv[1]
    #except:  inversion_method = 'L2'
  
    try:    timeseriesFile
    except: timeseriesFile = 'timeseries.h5'
  
    #h5file = h5py.File(igramsFile,'r')
    atr = readfile.read_attributes(igramsFile)
    if not atr['FILE_TYPE'] == 'interferograms':
        print '**********************************************************************'
        print 'ERROR:'
        print '       '+igramsFile+ '  was not found or the file is not readable!'
        print '**********************************************************************'
        Usage();sys.exit(1)

    #numIfgrams = len(h5file['interferograms'].keys())
    #if numIfgrams == 0.:
    #    print "load interferograms first by running: load_data.py TEMPLATEFILE"
    #    sys.exit(1)
    #h5timeseries = h5py.File(timeseriesFile,'w')
  
    print '\n************** Inverse Time Series ****************'
    if not inversion_method == 'l1':
        print 'Inverse time series using L2 norm minimization'
        ut.timeseries_inversion(igramsFile,timeseriesFile)
    else:
        print 'Inverse time series using L1 norm minimization'
        ut.timeseries_inversion_L1(igramsFile,timeseriesFile)
Exemplo n.º 4
0
def main(argv):

    try:
        File = argv[0]
        atr  = readfile.read_attributes(File)
    except: Usage(); sys.exit(1)

    try:    outName = argv[1]
    except: outName = 'tempMean_'+File
    #except: outName = 'average_spatial_coherence.h5'

    print '\n*************** Temporal Average ******************'

    ##### Input File Info
    k = atr['FILE_TYPE']
    print 'Input file is '+k
    width  = int(atr['WIDTH'])
    length = int(atr['FILE_LENGTH'])

    h5file = h5py.File(File)
    epochList = h5file[k].keys()
    epochList = sorted(epochList)
    print 'number of epoch: '+str(len(epochList))

    ##### Calculation
    dMean = np.zeros((length,width))
    print 'calculating ...'
    for epoch in epochList:
        print epoch
        if k in ['interferogram','coherence','wrapped']:
            d = h5file[k][epoch].get(epoch)[:]
        elif k in ['timeseries']:
            d = h5file[k].get(epoch)[:]
        else: print k+' type is not supported currently.'; sys.exit(1)
        dMean += d
    dMean /= len(epochList)
    del d
    h5file.close()

    ##### Output
    print 'writing >>> '+outName
    h5mean = h5py.File(outName,'w')
    group  = h5mean.create_group('mask')
    dset = group.create_dataset(os.path.basename('mask'), data=dMean, compression='gzip')
    for key,value in atr.iteritems():
        group.attrs[key] = value
    h5mean.close()
Exemplo n.º 5
0
def remove_multiple_surface(File, surf_type, Mask, ysub, outName):
    start = time.time()
    ##### Output File Name
    if outName == "":
        ext = os.path.splitext(File)[1].lower()
        outName = os.path.basename(File).split(ext)[0] + "_" + surf_type + ext

    atr = readfile.read_attributes(File)
    k = atr["FILE_TYPE"]
    print "Input file is " + atr["PROCESSOR"] + " " + k

    if k == "timeseries":
        h5file = h5py.File(File, "r")
        ifgramList = h5file[k].keys()
        ifgramList = sorted(ifgramList)
        print "number of epochs: " + str(len(ifgramList))

        h5flat = h5py.File(outName, "w")
        group = h5flat.create_group(k)
        print "writing >>> " + outName

        for ifgram in ifgramList:
            print "Removing " + surf_type + " from " + ifgram
            dataIn = h5file[k].get(ifgram)[:]

            dataOut = remove_data_multiple_surface(dataIn, surf_type, Mask, ysub)

            dset = group.create_dataset(ifgram, data=dataOut, compression="gzip")
        for key, value in h5file[k].attrs.iteritems():
            group.attrs[key] = value

    else:
        try:
            dataIn, atr = readfile.read(File)
        except:
            print "Input file type is not supported: " + atr["FILE_TYPE"]

        dataOut = remove_data_multiple_surface(dataIn, surf_type, Mask, ysub)
        ramp = dataIn - dataOut

        writefile.write(dataOut, atr, outName)
        # atr['FILE_TYPE']='mask'
        # writefile.write(ramp,atr,'2quadratic.h5')

    print "Remove " + surf_type + " took " + str(time.time() - start) + " secs"
Exemplo n.º 6
0
def main(argv):
    try:  opts, args = getopt.getopt(argv,"f:h")
    except getopt.GetoptError:  Usage() ; sys.exit(1)
  
    if  opts==[]:  Usage() ; sys.exit(1)
    for opt,arg in opts:
        if opt in ("-h","--help"):   Usage();  sys.exit()
        elif opt == '-f':            File = arg
    
        ##### Read attributes
        atr = readfile.read_attributes(File)
        print '\n*************** Generate Incidence Angle *****************'
    
        ##### Calculate look angle
        angle = look_angle(atr)
    
        ##### Output
        atr['FILE_TYPE'] = 'mask'
        outName = 'incidence_angle.h5'
        writefile.write(angle,atr,outName)
Exemplo n.º 7
0
def seed_xy(File,x,y,outName=''):
    ## Seed Input File with reference on point (y,x)
    print 'Referencing input file to pixel: (%d, %d)'%(y,x)
    ##4-tuple defining the left, upper, right, and lower pixel coordinate [optional]
    box = (x,y,x+1,y+1)

    #####  IO Info
    atr = readfile.read_attributes(File)
    k = atr['FILE_TYPE']
    if outName == '':  outName = 'Seeded_'+os.path.basename(File)

    ##### Mask
    length = int(atr['FILE_LENGTH'])
    width  = int(atr['WIDTH'])
    mask = np.ones((length,width))
    
    ## Read refernce value
    refList = ut.spatial_mean(File,mask,box)

    ## Seeding
    seed_file(File,outName,refList,x,y)

    return 1
Exemplo n.º 8
0
def main(argv):

    ## Default value
    phase_velocity = "no"  # 'no' means use 'phase history'
    update_timeseries = "yes"

    if len(sys.argv) > 2:
        try:
            opts, args = getopt.getopt(argv, "h:f:F:o:v:", ["phase-velocity", "no-timeseries-update"])
        except getopt.GetoptError:
            print "Error in reading input options!"
            Usage()
            sys.exit(1)

        for opt, arg in opts:
            if opt in ["-h", "--help"]:
                Usage()
                sys.exit()
            elif opt == "-f":
                timeSeriesFile = arg
            elif opt == "-F":
                igramsFile = arg
            elif opt == "-o":
                outname = arg
            elif opt == "--phase-velocity":
                phase_velocity = "yes"
            elif opt == "--no-timeseries-update":
                update_timeseries = "no"

    elif len(sys.argv) == 2:
        if argv[0] in ["-h", "--help"]:
            Usage()
            sys.exit(1)
        else:
            timeSeriesFile = argv[0]
    else:
        Usage()
        sys.exit(1)

    try:
        outname
    except:
        outname = timeSeriesFile.replace(".h5", "") + "_demCor.h5"

    ##### Read Time Series
    print "\n*************** Topographic Error Correction ****************"
    print "Loading time series: " + timeSeriesFile
    atr = readfile.read_attributes(timeSeriesFile)
    h5timeseries = h5py.File(timeSeriesFile)
    dateList = h5timeseries["timeseries"].keys()
    dateList = sorted(dateList)
    lt = len(dateList)
    print "number of epochs: " + str(lt)

    dateIndex = {}
    for ni in range(len(dateList)):
        dateIndex[dateList[ni]] = ni

    nrows = int(atr["FILE_LENGTH"])
    ncols = int(atr["WIDTH"])
    timeseries = np.zeros((len(dateList), nrows * ncols), np.float32)
    for date in dateList:
        print date
        d = h5timeseries["timeseries"].get(date)[:]
        timeseries[dateIndex[date]][:] = d.flatten("F")
    del d
    h5timeseries.close()
    print "**************************************"

    ##### Temporal Baseline
    print "read temporal baseline"
    tbase, date_dict = ptime.date_list2tbase(dateList)
    tbase = np.array(tbase).reshape(lt, 1)

    ##### Perpendicular Baseline
    try:
        Bp = [float(i) for i in atr["P_BASELINE_TIMESERIES"].split()]
        Bp = np.array(Bp).reshape(lt, 1)
    except:
        print "Cannot find P_BASELINE_TIMESERIES from timeseries file."
        print "Trying to calculate it from interferograms file"
        try:
            Bp = ut.Baseline_timeseries(igramsFile)
            Bp = np.array(Bp).reshape(lt, 1)
        except:
            print "Error in calculating baseline time series!"
            sys.exit(1)
    Bp_v = (Bp[1:lt] - Bp[0 : lt - 1]) / (tbase[1:lt] - tbase[0 : lt - 1])

    ##### Cubic Temporal Deformation Model
    ## Formula (10) in (Fattahi and Amelung, 2013, TGRS)
    if phase_velocity == "yes":
        print "using phase velocity history"
        M1 = np.ones((lt - 1, 1))
        M2 = (tbase[1:lt] + tbase[0 : lt - 1]) / 2
        M3 = (tbase[1:lt] ** 2 + tbase[1:lt] * tbase[0 : lt - 1] + tbase[0 : lt - 1] ** 2) / 6
        M = np.hstack((M1, M2, M3))
    else:
        print "using phase history"
        M = np.hstack((0.5 * tbase ** 2, tbase, np.ones((lt, 1))))

    ## Testing
    # teta = (tetaN+tetaF)/2
    # r = (rN+rF)/2
    # teta=19.658799999999999*np.pi/180
    # r=846848.2
    # Bperp=1000*np.random.random((lt,1))

    ##### Range and Look Angle
    near_range = float(atr["STARTING_RANGE1"])
    dR = float(atr["RANGE_PIXEL_SIZE"])
    r = float(atr["EARTH_RADIUS"])
    H = float(atr["HEIGHT"])
    far_range = near_range + dR * (ncols - 1)
    incidence_n = np.pi - np.arccos((r ** 2 + near_range ** 2 - (r + H) ** 2) / (2 * r * near_range))
    incidence_f = np.pi - np.arccos((r ** 2 + far_range ** 2 - (r + H) ** 2) / (2 * r * far_range))

    various_range = "yes"
    if various_range == "yes":
        range_x = np.linspace(near_range, far_range, num=ncols, endpoint="FALSE")
        look_angle_x = np.linspace(incidence_n, incidence_f, num=ncols, endpoint="FALSE")
    else:
        print "using center range and look angle to represent the whole area"
        center_range = (near_range + far_range) / 2
        center_look_angle = np.pi - np.arccos((r ** 2 + center_range ** 2 - (r + H) ** 2) / (2 * r * center_range))
        range_x = np.tile(center_range, ncols)
        look_angle_x = np.tile(center_look_angle, ncols)
    # C1_v = Bp_v / (center_range * np.sin(center_look_angle))
    # C1   = Bp   / (center_range * np.sin(center_look_angle))
    # timeseries_v = (timeseries[1:lt,:] - timeseries[0:lt-1,:]) / (tbase[1:lt] - tbase[0:lt-1])

    ##### Inversion column by column
    print "inversing using L2-norm minimization (unweighted least squares)..."
    dz = np.zeros([1, nrows * ncols])

    for i in range(ncols):
        ## Design Matrix Inversion
        C1_v = Bp_v / (range_x[i] * np.sin(look_angle_x[i]))
        C1 = Bp / (range_x[i] * np.sin(look_angle_x[i]))
        if phase_velocity == "yes":
            C = np.hstack((M, C1_v))
        else:
            C = np.hstack((M, C1))

        # print '    rank of the design matrix : '+str(np.linalg.matrix_rank(C))
        # if np.linalg.matrix_rank(C) == 4:  print '    design matrix has full rank'
        Cinv = np.linalg.pinv(C)

        ## (Phase) Velocity History
        ts_x = timeseries[:, i * nrows : (i + 1) * nrows]
        ts_xv = (ts_x[1:lt, :] - ts_x[0 : lt - 1, :]) / (tbase[1:lt] - tbase[0 : lt - 1])

        ## DEM error
        if phase_velocity == "yes":
            par = np.dot(Cinv, ts_xv)
        else:
            par = np.dot(Cinv, ts_x)
        dz_x = par[3].reshape((1, nrows))

        ## Update DEM error matrix and timeseries matrix
        dz[0][i * nrows : (i + 1) * nrows] = dz_x
        timeseries[:, i * nrows : (i + 1) * nrows] -= np.dot(C1, dz_x)

        ut.printProgress(i + 1, ncols)

    # dz[0][:] = par[3][:]
    dz = np.reshape(dz, [nrows, ncols], order="F")

    ########## Output - DEM error #######################
    # print '**************************************'
    # print 'writing DEM_error.hgt'
    # writefile.write_float32(dz,'DEM_error.hgt')
    # f = open('DEM_error.hgt.rsc','w')
    # f.write('FILE_LENGTH       '+str(int(nrows))+'\n')
    # f.write('WIDTH             '+str(int(ncols))+'\n')
    # print '**************************************'

    h5fileDEM = "DEM_error.h5"
    print "writing >>> " + h5fileDEM
    h5rmse = h5py.File(h5fileDEM, "w")
    group = h5rmse.create_group("dem")
    dset = group.create_dataset(os.path.basename("dem"), data=dz, compression="gzip")
    for key, value in atr.iteritems():
        group.attrs[key] = value
    group.attrs["UNIT"] = "m"
    print "**************************************"

    ########### Output - Corrected Time Series ##########
    if update_timeseries == "yes":
        print "writing >>> " + outname
        print "number of dates: " + str(len(dateList))

        h5timeseriesDEMcor = h5py.File(outname, "w")
        group = h5timeseriesDEMcor.create_group("timeseries")
        for i in range(len(dateList)):
            print dateList[i]
            d = np.reshape(timeseries[i][:], [nrows, ncols], order="F")
            dset = group.create_dataset(dateList[i], data=d, compression="gzip")
        # for date in dateList:
        #    print date
        #    if not date in h5timeseriesDEMcor['timeseries']:
        #        d = np.reshape(timeseries[dateIndex[date]][:],[nrows,ncols],order='F')
        #        dset = group.create_dataset(date, data=d, compression='gzip')
        for key, value in atr.iteritems():
            group.attrs[key] = value
        h5timeseriesDEMcor.close()
Exemplo n.º 9
0
def main(argv):

    ## Global Variables
    global fontSize, lineWidth, markerColor, markerSize

    ## Default Values
    fontSize = 12
    lineWidth = 2
    markerColor = "orange"
    markerSize = 16
    saveFig = "no"
    dispFig = "yes"
    saveList = "no"

    if len(sys.argv) > 2:
        try:
            opts, args = getopt.getopt(argv, "h:b:f:s:w:l:m:c:o:", ["save", "nodisplay", "list"])
        except getopt.GetoptError:
            Usage()
            sys.exit(1)

        for opt, arg in opts:
            if opt in ("-h", "--help"):
                Usage()
                sys.exit()
            elif opt == "-b":
                baselineFile = arg
            elif opt == "-f":
                igramsFile = arg
            elif opt == "-l":
                listFile = arg
            elif opt == "-s":
                fontSize = int(arg)
            elif opt == "-w":
                lineWidth = int(arg)
            elif opt == "-m":
                markerSize = int(arg)
            elif opt == "-c":
                markerColor = arg
            # elif opt == '-o':        figName2  = arg;   saveFig = 'yes'
            elif opt == "--save":
                saveFig = "yes"
            elif opt == "--nodisplay":
                dispFig = "no"
                saveFig = "yes"
            elif opt == "--list":
                saveList = "yes"

        try:
            igramsFile
        except:
            try:
                baselineFile
            except:
                Usage()
                sys.exit(1)

    elif len(sys.argv) == 2:
        igramsFile = argv[0]
    else:
        Usage()
        sys.exit(1)

    ##### Output figure name
    figName1 = "BperpHist.pdf"
    figName2 = "Network.pdf"
    try:
        igramsFile
        if "Modified" in igramsFile:
            figName1 = "BperpHist_Modified.pdf"
            figName2 = "Network_Modified.pdf"
    except:
        pass

    ############# Read Time and Bperp ################
    print "\n******************** Plot Network **********************"
    try:
        igramsFile
        atr = readfile.read_attributes(igramsFile)
        k = atr["FILE_TYPE"]
        if k not in ["interferograms", "coherence", "wrapped"]:
            print "Only interferograms / coherence / wrapped are supported."
            sys.exit(1)

        print "reading date and perpendicular baseline from " + k
        dateList = ptime.date_list(igramsFile)
        dateList6 = ptime.yymmdd(dateList)
        print "number of acquisitions: " + str(len(dateList))
        Bp = ut.Baseline_timeseries(igramsFile)
    except:
        baselineFile
        print "reading date and perpendicular baseline from " + baselineFile
        dateList, Bp = pnet.read_baseline_file(baselineFile)[0:2]
        dateList6 = ptime.yymmdd(dateList)

    ############# Read Pairs Info ####################
    print "reading pairs info"
    try:
        listFile
        pairs = pnet.read_pairs_list(listFile, dateList)
    except:
        pairs = pnet.read_igram_pairs(igramsFile)
    print "number of pairs       : " + str(len(pairs))

    if saveList == "yes":
        pnet.write_pairs_list(pairs, dateList, "Pairs.list")
        print "save pairs info to Pairs.list"

    ############# Read Unwrapping Error Info #######################
    ## For simulated interferograms only
    ## To plot the interferograms with unwrapping errors with a different color
    # N_unw_err=0
    # try:
    #  for ifgram in  ifgramList:
    #    if h5file[k[0]][ifgram].attrs['unwrap_error']=='yes':
    #       N_unw_err=N_unw_err+1
    # except: pass
    #
    # if N_unw_err>0:
    #   pairs_ue=np.zeros([N_unw_err,2],np.int)
    #   i=0
    #   for ifgram in  ifgramList:
    #     if h5file[k[0]][ifgram].attrs['unwrap_error']=='yes':
    #       date1,date2 = h5file[k[0]][ifgram].attrs['DATE12'].split('-')
    #       pairs_ue[i][0]=dateList6.index(date1)
    #       pairs_ue[i][1]=dateList6.index(date2)
    #       i=i+1
    #

    ############### Fig 1 - Interferogram Network ##################
    fig1 = plt.figure(1)
    fig1 = plot_network(fig1, pairs, dateList, Bp)

    if saveFig == "yes":
        plt.savefig(figName2, bbox_inches="tight")
        print "save figure to " + figName2

    ############## Fig 2 - Baseline History ###################
    fig2 = plt.figure(2)
    fig2 = plot_bperp_hist(fig2, dateList, Bp)

    if saveFig == "yes":
        plt.savefig(figName1, bbox_inches="tight")
        print "save figure to " + figName1

    if dispFig == "yes":
        plt.show()
Exemplo n.º 10
0
def main(argv):

    cbar_bin_num  = 9
    cbar_label    = 'Mean LOS velocity'
    color_map     = 'jet'
    data_alpha    = 0.7
    disp_opposite = 'no'
    disp_colorbar = 'yes'
    rewrapping    = 'no'
    fig_dpi       = 500
    #fig_size      = [6.0,9.0]
    fig_unit      = 'mm/yr'
    disp_ref      = 'yes'
    ref_size      = 5
    dispDisplacement = 'no'

    if len(sys.argv)>2:
        try:   opts, args = getopt.getopt(argv,"f:m:M:d:c:w:i:r:",['noreference','fig-size',\
                                               'ref-size=','cbar-label=','displacement','cbar-bin-num='])
        except getopt.GetoptError:  Usage() ; sys.exit(1)

        for opt,arg in opts:
            if   opt == '-f':        File = arg
            elif opt == '-m':        Vmin = float(arg)
            elif opt == '-M':        Vmax = float(arg)
            elif opt == '-d':        epoch_date    = arg
            elif opt == '-c':        color_map     = arg
            elif opt == '-i':        disp_opposite = arg
            elif opt == '-w':        rewrapping    = arg
            elif opt == '-r':        fig_dpi = int(arg)
            elif opt == '--cbar-bin-num' :   cbar_bin_num     = int(arg)
            elif opt == '--cbar-label'   :   cbar_label       = arg
            elif opt == '--displacement' :   dispDisplacement = 'yes'
            elif opt == '--fig-size'     :   fig_size = [float(i) for i in arg.split(',')][0:2]
            elif opt == '--ref-size'     :   ref_size = int(arg)
            elif opt == '--noreference'  :   disp_ref = 'no'

    elif len(sys.argv)==2:
        if argv[0]=='-h':               Usage(); sys.exit(1)
        elif os.path.isfile(argv[0]):   File = argv[0]
        else:                           Usage(); sys.exit(1)
    else:                             Usage(); sys.exit(1)

    #######################################################
    ###################  Prepare Data  ####################
    ## prepare: data, North, East, South, West

    ext = os.path.splitext(File)[1].lower()
    atr = readfile.read_attributes(File)
    k = atr['FILE_TYPE']
    print '\n*************** Output to KMZ file ****************'
    print 'Input file is '+k

    if ext == '.h5':
        try:      h5file=h5py.File(File,'r')
        except:   Usage() ; sys.exit(1)
        outName=File.split('.')[0]

        if k in ('interferograms','wrapped','coherence'):
            ifgramList=h5file[k].keys()
            for i in range(len(ifgramList)):
                if epoch_date in ifgramList[i]:
                    epoch_number = i
            print ifgramList[epoch_number]
            outName = ifgramList[epoch_number]
            #outName=epoch_date

            dset = h5file[k][ifgramList[epoch_number]].get(ifgramList[epoch_number])
            data = dset[0:dset.shape[0],0:dset.shape[1]]

            if k == 'wrapped':
                print 'No wrapping for wrapped interferograms. Set rewrapping=no'
                rewrapping = 'no'
                Vmin = -np.pi    
                Vmax = np.pi

        elif 'timeseries' in k:
            epochList=h5file['timeseries'].keys()
            for i in range(len(epochList)):
                if epoch_date in epochList[i]:
                    epoch_number = i

            #### Out name
            try:    ref_date = atr['ref_date']
            except: ref_date = ut.yyyymmdd(atr['DATE'])[0]
            #ref_date=h5file['timeseries'].attrs['ref_date']
            if len(epoch_date)==8:  outName=ref_date[2:]+'-'+epoch_date[2:]
            else:                   outName=ref_date[2:]+'-'+epoch_date

            dset = h5file['timeseries'].get(epochList[epoch_number])
            data = dset[0:dset.shape[0],0:dset.shape[1]]

        ### one dataset format: velocity, mask, temporal_coherence, rmse, std, etc.
        else:
            dset = h5file[k].get(k)
            data=dset[0:dset.shape[0],0:dset.shape[1]]
            if disp_opposite in('yes','Yes','Y','y','YES'):
                data=-1*data

            try:
                xref=h5file[k].attrs['ref_x']
                yref=h5file[k].attrs['ref_y']
            except: pass

    elif ext in ['.unw','.cor','.hgt','.trans','.dem']:
        if   ext in ['.unw','.cor','.hgt','.trans']:
            a,data,atr = readfile.read_float32(File)
            outName = File
            if ext in ['.unw']:
                if dispDisplacement == 'yes':
                    print 'show displacement'
                    phase2range = -float(atr['WAVELENGTH']) / (4*np.pi)
                    data *= phase2range
                    atr['UNIT'] = 'm'
                    rewrapping == 'no'
                    fig_unit = 'mm'
                if rewrapping == 'yes':
                    data = rewrap(data,atr)
                    fig_unit = 'radian'
        elif ext == '.dem':
            data,atr = readfile.read_real_int16(File)
            outName = File
        if   ext in ['.hgt','.dem']:     fig_unit = 'm'
        elif ext in ['.cor','.trans']:   fig_unit = ' '
    else: sys.exit('Do not support '+ext+' file!')


    ########################################################
    if rewrapping=='yes':
        data=rewrap(data)
        Vmin = -np.pi    #[-pi,pi] for wrapped interferograms
        Vmax =  np.pi
    else:
        try:     Vmin
        except:  Vmin = np.nanmin(data)
        try:     Vmax
        except:  Vmax = np.nanmax(data)

    try:
        lon_step = float(atr['X_STEP'])
        lat_step = float(atr['Y_STEP'])
        lon_unit = atr['Y_UNIT']
        lat_unit = atr['X_UNIT']
        West     = float(atr['X_FIRST'])
        North    = float(atr['Y_FIRST'])
        South    = North+lat_step*(data.shape[0]-1)
        East     = West +lon_step*(data.shape[1]-1)
        geocoord = 'yes'
        print 'Geocoded'
    except:
        print '%%%%%%%%%%'
        print 'Error:\nThe input file is not geocoded\n'
        print '%%%%%%%%%%'
        Usage();sys.exit(1)


    #######################################################
    ###################  Output KMZ  ######################

    ############### Make PNG file
    print 'Making png file ...'   
    length = data.shape[0]
    width  = data.shape[1]
    try:fig_size
    except:
        fig_size_0 = 6.0           ## min figure dimension: 6.0
        ratio = float(length)/float(width)
        fig_size = [fig_size_0,fig_size_0*ratio]
    print 'figure size:  %.1f, %.1f'%(fig_size[0],fig_size[1])
    ccmap = plt.get_cmap(color_map)
    fig = plt.figure(figsize=fig_size,frameon=False)
    ax = fig.add_axes([0., 0., 1., 1.])
    ax.set_axis_off()

    aspect = width/(length*1.0)
    try:     ax.imshow(data,aspect='auto',cmap=ccmap,vmax=Vmax,vmin=Vmin)
    except:  ax.imshow(data,aspect='auto',cmap=ccmap)

    if disp_ref == 'yes':
        try:
            xref = int(atr['ref_x'])
            yref = int(atr['ref_y'])
            ax.plot(xref,yref,'ks',ms=ref_size)
            print 'show reference point'
        except: print 'Cannot find reference point info!'

    ax.set_xlim([0,width])
    ax.set_ylim([length,0])

    figName = outName + '.png'
    print 'writing '+figName
    plt.savefig(figName, pad_inches=0.0, transparent=True, dpi=fig_dpi)

    ############### Making colorbar
    pc = plt.figure(figsize=(1,8))
    axc = pc.add_subplot(111)
    if   fig_unit in ['mm','mm/yr']: v_scale = 1000
    elif fig_unit in ['cm','cm/yr']: v_scale = 100
    elif fig_unit in ['m',  'm/yr']: v_scale = 1
    norm = mpl.colors.Normalize(vmin=Vmin*v_scale, vmax=Vmax*v_scale)
    clb  = mpl.colorbar.ColorbarBase(axc,cmap=ccmap,norm=norm, orientation='vertical')

    #clb.set_label(fig_unit)
    clb.set_label(cbar_label+' ['+fig_unit+']')
    clb.locator = ticker.MaxNLocator(nbins=cbar_bin_num)
    clb.update_ticks()

    pc.subplots_adjust(left=0.2,bottom=0.3,right=0.4,top=0.7)
    pc.patch.set_facecolor('white')
    pc.patch.set_alpha(0.7)
    pc.savefig('colorbar.png',bbox_inches='tight',facecolor=pc.get_facecolor(),dpi=300)

    ############## Generate KMZ file
    print 'generating kml file ...'
    try:     doc = KML.kml(KML.Folder(KML.name(atr['PROJECT_NAME'])))
    except:  doc = KML.kml(KML.Folder(KML.name('PySAR product')))
    slc = KML.GroundOverlay(KML.name(figName),KML.Icon(KML.href(figName)),\
                            KML.altitudeMode('clampToGround'),\
                            KML.LatLonBox(KML.north(str(North)),KML.south(str(South)),\
                                          KML.east( str(East)), KML.west( str(West))))
    doc.Folder.append(slc)

    #############################
    print 'adding colorscale ...'
    cb_rg = min(North-South, East-West)
    cb_N = (North+South)/2.0 + 0.5*0.5*cb_rg
    cb_W = East  + 0.1*cb_rg
    slc1 = KML.GroundOverlay(KML.name('colorbar'),KML.Icon(KML.href('colorbar.png')),\
                             KML.altitude('2000'),KML.altitudeMode('absolute'),\
                             KML.LatLonBox(KML.north(str(cb_N)),KML.south(str(cb_N-0.5*cb_rg)),\
                                           KML.west( str(cb_W)),KML.east( str(cb_W+0.14*cb_rg))))
    doc.Folder.append(slc1)

    #############################
    kmlstr = etree.tostring(doc, pretty_print=True) 
    kmlname = outName + '.kml'
    print 'writing '+kmlname
    kmlfile = open(kmlname,'w')
    kmlfile.write(kmlstr)
    kmlfile.close()

    kmzName = outName + '.kmz'
    print 'writing '+kmzName
    cmdKMZ = 'zip ' + kmzName +' '+ kmlname +' ' + figName + ' colorbar.png'
    os.system(cmdKMZ)

    cmdClean = 'rm '+kmlname;      print cmdClean;    os.system(cmdClean)
    cmdClean = 'rm '+figName;      print cmdClean;    os.system(cmdClean)
    cmdClean = 'rm colorbar.png';  print cmdClean;    os.system(cmdClean)
Exemplo n.º 11
0
def main(argv):

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

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

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

    global unit, radius, saveFig, dispFig, fig_dpi

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

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

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

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

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

        for opt, arg in opts:
            if opt == "-f":
                timeSeriesFile = arg
            elif opt == "-F":
                timeSeriesFile_2 = arg
            elif opt == "-v":
                velocityFile = arg
            elif opt == "-a":
                vmin = float(arg)
            elif opt == "-b":
                vmax = float(arg)
            elif opt == "-s":
                fontSize = int(arg)
            elif opt == "-m":
                markerSize = int(arg)
                markerSize2 = int(arg)
            elif opt == "-c":
                markerColor = arg
            elif opt == "-w":
                lineWidth = int(arg)
            elif opt == "-u":
                unit = arg
            elif opt == "-l":
                lbound = float(arg)
            elif opt == "-h":
                hbound = float(arg)
            elif opt == "-D":
                demFile = arg
            elif opt == "-V":
                contour_step = float(arg)
            elif opt == "-t":
                minDate = arg
            elif opt == "-T":
                maxDate = arg
            elif opt == "-r":
                radius = abs(int(arg))
            elif opt == "-x":
                xsub = [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()
Exemplo n.º 12
0
def main(argv):

    try:
        file=argv[0]
        geomap=argv[1]
    except:
        Usage();sys.exit(1)
 
    ######################################################################################
    fileName=os.path.basename(file).split('.')[0]
    h5file=h5py.File(file,'r')
    atr = readfile.read_attributes(file)
    k = atr['FILE_TYPE']
    print '\n***************** Geocoding *******************'
    print 'input file: '+k
 
    #### Subsetted radar coded file
    try:
        x0 = float(atr['subset_x0'])
        y0 = float(atr['subset_y0'])
        print '\nSubsetted radar coded file:\n    creating temporary geomap file for it...'
        rg,az,rsc = readfile.read_float32(geomap)
        rg = rg - x0
        az = az - y0
        geomap = 'temp_'+geomap
        print '    writing '+geomap+'\n'
        writefile.write_float32(rg,az,geomap)
        fg = open(geomap+'.rsc','w')
        for kg in rsc.keys():    fg.write(kg+'    '+rsc[kg]+'\n')
        fg.close()
    except: pass


    ######################################################################################
    if k in ['timeseries']:
        outname='epoch_temp.unw'
 
        f = h5py.File('geo_'+file,'w')
        group = f.create_group('timeseries')
        epochList = h5file['timeseries'].keys()
        epochList = sorted(epochList)
        for epoch in epochList:
            print 'geocoding '+epoch
            data = h5file['timeseries'].get(epoch)[:]
 
            amp,unw,unwrsc = geocode_one(data,geomap,outname)
            dset = group.create_dataset(epoch, data=unw, compression='gzip')
 
        atr = geocode_attributes(atr,unwrsc)
        for key,value in atr.iteritems():
            group.attrs[key] = value

    ######################################################################################
    elif k in ['interferograms','coherence','wrapped']:
        if   k == 'interferograms': outname = k[0]+'_temp.unw'
        elif k == 'coherence'     : outname = k[0]+'_temp.cor'
        else:                       outname = k[0]+'_temp.int'
 
        f = h5py.File('geo_'+file,'w')
        gg = f.create_group('interferograms')
        igramList = h5file[k].keys()
        igramList = sorted(igramList)
        for igram in igramList:
            print 'geocoding '+igram
            data = h5file[k][igram].get(igram)[:]
 
            amp,unw,unwrsc = geocode_one(data,geomap,outname)
 
            group = gg.create_group('geo_'+igram)
            dset = group.create_dataset('geo_'+igram, data=unw, compression='gzip')
 
            atr = geocode_attributes(h5file[k][igram].attrs, unwrsc)
            for key,value in atr.iteritems():
                group.attrs[key] = value
 
        #######################  support of old format  #######################
        ### mask
        try:
            data = h5file['mask'].get('mask')[:]
            amp,unw,unwrsc = geocode_one(data,geomap,'mask_'+outname)
            gm = f.create_group('mask')
            dset = gm.create_dataset('mask', data=unw, compression='gzip')
        except:  print 'No group for mask found in the file.'
        ### meanCoherence
        try:
            data = h5file['meanCoherence'].get('meanCoherence')[:]
            amp,unw,unwrsc = geocode_one(data,geomap,'meanCoherence_'+outname)
            gm = f.create_group('meanCoherence')
            dset = gm.create_dataset('meanCoherence', data=unw, compression='gzip')
        except:  print 'No group for meanCoherence found in the file'

    ######################################################################################
    else:
        data,atr = readfile.read(file)
        outname=fileName+'.unw'
 
        amp,unw,unwrsc = geocode_one(data,geomap,outname)
        atr = geocode_attributes(atr,unwrsc)
 
        writefile.write(unw,atr,'geo_'+file)
 
 
    ######################################################################################
    try:
        atr['subset_x0']
        rmCmd='rm '+geomap;            os.system(rmCmd);       print rmCmd
        rmCmd='rm '+geomap+'.rsc';     os.system(rmCmd);       print rmCmd
    except: pass
 
    try:
        f.close()
        h5file.close()
    except: pass
Exemplo n.º 13
0
def main(argv):

    outName = 'mask.h5'
    method  = 'threshold'

    ##### Check Inputs
    if len(sys.argv)>2:
        try:   opts, args = getopt.getopt(argv,'h:f:m:M:x:y:o:d:e:',['nonzero'])
        except getopt.GetoptError:      Usage() ; sys.exit(1)
  
        for opt,arg in opts:
            if opt in ("-h","--help"):   Usage();   sys.exit()
            elif opt == '-f':         File = arg
            elif opt == '-m':         minV = float(arg)
            elif opt == '-M':         maxV = float(arg)
            elif opt == '-y':         ysub = [int(i) for i in arg.split(':')];        ysub.sort()
            elif opt == '-x':         xsub = [int(i) for i in arg.split(':')];        xsub.sort()
            elif opt == '-o':         outName    = arg
            elif opt == '-d':         epoch_date = arg
            elif opt == '-e':         epoch_num  = int(arg) - 1
            elif opt == '--nonzero':  method     = 'nonzero'

    elif len(sys.argv)==2:
        if   argv[0] in ['-h','--help']:    Usage(); sys.exit(1)
        elif os.path.isfile(argv[0]):       File = argv[0]
        else:    print 'Input file does not existed: '+argv[0];  sys.exit(1)
    else:                                   Usage(); sys.exit(1)

    ##### Input File Info
    atr = readfile.read_attributes(File)
    print '\n****************** Generate Mask *******************'
    print 'Input file is '+atr['PROCESSOR']+' '+atr['FILE_TYPE']+': '+File
    mask = np.ones([int(atr['FILE_LENGTH']),int(atr['WIDTH'])])
    print 'Create initial mask with the same size as the input file and all = 1'

    ##### Non-zero Mask #######
    if method == 'nonzero':
        k = atr['FILE_TYPE']
        MaskZero = np.ones([int(atr['FILE_LENGTH']),int(atr['WIDTH'])])
  
        ext = os.path.splitext(File)[1].lower()
        if ext == '.h5' and k in ['interferograms','coherence','wrapped','timeseries']:
            h5file = h5py.File(File,'r')
            epochList = h5file[k].keys()
  
            for epoch in epochList:
                print epoch
                if k in ['interferograms','coherence','wrapped']:
                    data = h5file[k][epoch].get(epoch)[:]
                elif k in ['timeseries']:
                    data = h5file[k].get(epoch)
                MaskZero *= data
                MaskZero[np.isnan(data)] = 0
            h5file.close()
  
        else:
            data,atr = readfile.read(File)
            MaskZero *= data
            MaskZero[np.isnan(data)] = 0
  
        mask = np.ones([int(atr['FILE_LENGTH']),int(atr['WIDTH'])])
        mask[MaskZero==0] = 0


    ##### Threshold ##########
    else:
        ##### Read and Initiate Mask
        try:        V, atr = readfile.read(File,epoch_date)
        except:
            try:    V, atr = readfile.read(File,epoch_num)
            except: V, atr = readfile.read(File)
  
        ##### Calculating Mask
        ## threshold
        try:
            mask[V<minV]=0
            print 'all value < '+str(minV)+' = 0'
        except:  print 'No min threshold'
        try:
            mask[V>maxV]=0
            print 'all value > '+str(maxV)+' = 0'
        except:  print 'No max threshold'  
        ## nan value
        mask[np.isnan(V)]=0
  
    ## subset
    try:
        mask[0:ysub[0],:]=0
        mask[ysub[1]:mask.shape[0],:]=0
        print 'all y in [0,'+str(ysub[0])+'] and ['+str(ysub[1])+',end] = 0'
    except:  print 'No subset in y direction'
    try:
        mask[:,0:xsub[0]]=0
        mask[:,xsub[1]:mask.shape[1]]=0
        print 'all x in [0,'+str(xsub[0])+'] and ['+str(xsub[1])+',end] = 0'
    except:  print 'No subset in x direction'
   
  
    ##### Writing mask file
    atr['FILE_TYPE'] = 'mask'
    writefile.write(mask,atr,outName)
Exemplo n.º 14
0
Arquivo: add.py Projeto: yunjunz/PySAR
def main(argv):

    ####################### Inputs Check ########################
    try:    opts, args = getopt.getopt(argv,"h:f:o:",['help'])
    except getopt.GetoptError:    Usage() ; sys.exit(1)
  
    if len(sys.argv) > 4:
        for opt,arg in opts:
            if opt in ("-h","--help"):  Usage();  sys.exit()
            elif opt == '-f':   fileList = arg.split(',')
            elif opt == '-o':   outName  = arg
  
    elif len(sys.argv) <= 4 and len(sys.argv) >= 3:
        fileList = [sys.argv[1],sys.argv[2]]
        try: outName = sys.argv[3]
        except: pass
    else: Usage();  sys.exit(1)
  
    print '\n****************** Add **********************'
    print 'Input files: '
    print fileList
  
    ext = os.path.splitext(fileList[0])[1].lower()
    try:     outName
    except:  outName = File1.split('.')[0]+'_plus_'+File2.split('.')[0]+ext
  
  
    ##### Read File Info / Attributes
    atr  = readfile.read_attributes(fileList[0])
    print 'Input file is '+atr['PROCESSOR']+' '+atr['FILE_TYPE']
    k = atr['FILE_TYPE']
  
    ##### File Type Check
    if k in ['timeseries','interferograms','coherence','wrapped']:
        for i in range(1,len(fileList)):
            File = fileList[i]
            r = readfile.read_attributes(File)
            if not r['FILE_TYPE'] == k:
                print 'Input file type is not the same: '+r['FILE_TYPE']
                sys.exit(1)
  
        h5out = h5py.File(outName,'w')
        group = h5out.create_group(k)
  
        h5in  = h5py.File(fileList[0])
        epochList = h5in[k].keys()

    ########################### Add file by file ########################
    if k in ['timeseries']:
        for epoch in epochList:
            print epoch
            data = np.zeros((int(atr['FILE_LENGTH']),int(atr['WIDTH'])))
            for File in fileList:
                print File
                h5file = h5py.File(File,'r')
                d = h5file[k].get(epoch)[:]
  
                data = add(data,d)
  
            dset = group.create_dataset(epoch, data=data, compression='gzip')
        for key,value in atr.iteritems():   group.attrs[key] = value
  
        h5out.close()
        h5in.close()
  
    elif k in ['timeseries','interferograms','coherence','wrapped']:
        for epoch in epochList:
            print epoch
            data = np.zeros((int(atr['FILE_LENGTH']),int(atr['WIDTH'])))
            for File in fileList:
                print File
                h5file = h5py.File(File,'r')
                d = h5file[k][epoch].get(epoch)[:]
  
                data = add(data,d)
  
            gg = group.create_group(epoch)
            dset = gg.create_dataset(epoch, data=data, compression='gzip')
            for key, value in h5in[k][epoch].attrs.iteritems():
                gg.attrs[key] = value
  
        h5out.close()
        h5in.close()
  
    ## All the other file types
    else:
        data = np.zeros((int(atr['FILE_LENGTH']),int(atr['WIDTH'])))
        for File in fileList:
            print 'loading '+File
            d,r = readfile.read(File)
            data = add(data,d)
        writefile.write(data,atr,outName)
Exemplo n.º 15
0
def spatial_mean(File,mask_orig,box):
    ## Calculate the Spatial Average of all non-nan pixels for each epoch
    ##     and return the mean value.
    ## 
    ## Inputs:
    ##     File      :
    ##     mask_orig : mask, same size as File
    ##     box       : 4-tuple defining the left, upper, right, and lower pixel coordinate [optional]
    ## Output: list for multi-dataset file, and float for single-dataset file

    print 'calculating spatial average of '+File+' within '+str(box)+' ...'
    ##### Input File Info
    atr  = readfile.read_attributes(File)
    k = atr['FILE_TYPE']
    width  = int(atr['WIDTH'])
    length = int(atr['FILE_LENGTH'])

    ##### Bounding Box
    #if box       == None:    box = [0,0,width,length]
    ##### Mask Info
    #if mask_orig == None:    mask_orig = np.ones((length,width))

    mask = mask_orig[box[1]:box[3],box[0]:box[2]]
    idx = mask != 0

    ##### Calculation
    if k in ['timeseries','interferograms','coherence','wrapped']:
        h5file = h5py.File(File,'r')
        epochList = h5file[k].keys();
        epochList = sorted(epochList)
        epochNum  = len(epochList)
        #print 'number of epoch: '+str(epochNum)

        meanList   = np.zeros(epochNum)
        for i in range(epochNum):
            epoch = epochList[i]
            if k in ['interferograms','coherence','wrapped']:
                dset = h5file[k][epoch].get(epoch)
            elif k == 'timeseries':
                dset = h5file[k].get(epoch)
            else:  print 'Unrecognized group type: '+k
            
            d = dset[box[1]:box[3],box[0]:box[2]]
            ## supress warning 
            ## url - http://stackoverflow.com/questions/29688168/mean-nanmean-and-warning-mean-of-empty-slice
            with warnings.catch_warnings():
                warnings.simplefilter("ignore", category=RuntimeWarning)
                meanList[i] = np.nanmean(d[idx])
            printProgress(i+1,epochNum)
        del d
        h5file.close()
        
        if epochNum == 1:   meanList = float(meanList)

    else:
        data,atr = readfile.read(File,box)
        with warnings.catch_warnings():
            warnings.simplefilter("ignore", category=RuntimeWarning)
            meanList = np.nanmean(data[idx])

    return meanList
Exemplo n.º 16
0
def mask_file(in_file,M,out_file=''):
    ## Mask input file with mask matrix M

    atr = readfile.read_attributes(in_file)
    k = atr['FILE_TYPE']
    print 'file type: '+k

    if out_file == '':
        ext      = os.path.splitext(in_file)[1]
        out_file = os.path.basename(in_file).split('.')[0]+'_masked'+ext

    if k in ['timeseries','interferograms','wrapped','coherence']:
        h5file = h5py.File(in_file,'r')
        epochList = h5file[k].keys()
        epochList = sorted(epochList)
        print 'number of epochs: '+str(len(epochList))

        h5out = h5py.File(out_file,'w')
        print 'writing >>> '+out_file

    ##### Multiple Dataset File
    if k == 'timeseries':
        group = h5out.create_group(k)
        for d in epochList:
            print d
            unwset = h5file[k].get(d)
            unw=unwset[0:unwset.shape[0],0:unwset.shape[1]]

            unw = mask_data(unw,M)

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

    elif k in ['interferograms','wrapped','coherence']:
        gg = h5out.create_group(k)
        for igram in epochList:
            print igram
            unwset = h5file[kf[0]][igram].get(igram)
            unw=unwset[0:unwset.shape[0],0:unwset.shape[1]]

            unw = mask_data(unw,M)

            group = gg.create_group(igram)
            dset = group.create_dataset(igram, data=unw, compression='gzip')
            for key, value in h5file[k][igram].attrs.iteritems():
                group.attrs[key] = value
        try:
            mask = h5file['mask'].get('mask')
            gm = h5out.create_group('mask')
            dset = gm.create_dataset('mask', data=mask, compression='gzip')
        except: print 'no mask group found.'

    ##### Single Dataset File
    else:
        import pysar._writefile as writefile
        unw,atr = readfile.read(in_file)
        unw     = mask_data(unw,M)
        writefile.write(unw,atr,out_file)

    try:
        h5file.close()
        h5out.close()
    except: pass
Exemplo n.º 17
0
def main(argv):

    if len(sys.argv)>2:
        try:   opts, args = getopt.getopt(argv,"f:E:m:M:h:o:t:")
        except getopt.GetoptError:   Usage() ; sys.exit(1)
    
        for opt,arg in opts:
            if   opt == '-f':    timeSeriesFile   = arg
            elif opt == '-E':    datesNot2include = arg.replace(' ','').split(',')
            elif opt == '-m':    minDate          = arg
            elif opt == '-M':    maxDate          = arg
            elif opt == '-o':    outName          = arg
            elif opt == '-t':    templateFile     = arg
  
    elif len(sys.argv)==2:
        if   argv[0]=='-h':  Usage(); sys.exit(1)
        elif os.path.isfile(argv[0]):   timeSeriesFile = argv[0]
        else:  Usage(); sys.exit(1)
    else:  Usage(); sys.exit(1)    
  
    ##### Read excluded date list Input
    try:  datesNot2include
    except:
        try:
            templateFile
            templateContents = readfile.read_template(templateFile)
            datesNot2include = templateContents['pysar.drop.date'].replace(' ','').split(',')
        except: pass

    ##############################################################
    print '\n********** Inversion: Time Series to Velocity ***********'
    atr = readfile.read_attributes(timeSeriesFile)
    k = atr['FILE_TYPE']
    print 'input file: '+k
    if not k == 'timeseries':  print 'Input file is not timeseries!'; sys.exit(1)
    print "Loading time series file: " + timeSeriesFile
    h5timeseries = h5py.File(timeSeriesFile)
    dateList1 = h5timeseries[k].keys()
    dateList1 = sorted(dateList1)
  
    ##############################################################
    print '--------------------------------------------'
    print 'Dates from input file: '+str(len(dateList1))
    print dateList1
  
    try:
        datesNot2include
        if os.path.isfile(datesNot2include[0]):
            try:  datesNot2include = ptime.read_date_list(datesNot2include[0])
            except:  print 'Can not read date list file: '+datesNot2include[0]
        print '--------------------------------------------'
        print 'Date excluded: '+str(len(datesNot2include))
        print datesNot2include
    except:
        datesNot2include=[]
  
    try:
        minDate
        minDateyy=yyyymmdd2years(minDate)
        print 'minimum date: '+minDate
        for date in dateList1:
            yy=yyyymmdd2years(date)
            if yy < minDateyy:
                print '  remove date: '+date
                datesNot2include.append(date)
    except: pass
  
    try:
        maxDate
        maxDateyy=yyyymmdd2years(maxDate) 
        print 'maximum date: '+maxDate
        for date in dateList1:
            yy=yyyymmdd2years(date)
            if yy > maxDateyy:
                print '  remove date: '+date
                datesNot2include.append(date)
    except: pass

    try:
        dateList=[]
        for date in dateList1:
            if date not in datesNot2include:
                dateList.append(date)
    except:  pass

    print '--------------------------------------------'
    if len(dateList) == len(dateList1):
        print 'using all dates to calculate the vlocity'
    else:
        print 'Dates used to estimate the velocity: '+str(len(dateList))
        print dateList
    print '--------------------------------------------'

    ##############################################################
    dateIndex={}
    for ni in range(len(dateList)):
        dateIndex[dateList[ni]]=ni
    tbase=[]
    d1 = datetime.datetime(*time.strptime(dateList[0],"%Y%m%d")[0:5])
    
    for ni in range(len(dateList)):
        d2 = datetime.datetime(*time.strptime(dateList[ni],"%Y%m%d")[0:5])
        diff = d2-d1
        tbase.append(diff.days)
  
    dates=[]
    for ni in range(len(dateList)):
        d = datetime.datetime(*time.strptime(dateList[ni],"%Y%m%d")[0:5])
        dates.append(d)
  
    ###########################################
    print 'Calculating Velocity'
  
    datevector=[]
    for i in range(len(dates)):
        datevector.append(np.float(dates[i].year) + np.float(dates[i].month-1)/12 + np.float(dates[i].day-1)/365)
  
    B=np.ones([len(datevector),2])
    B[:,0]=datevector
    #B1 = np.linalg.pinv(B)
    B1 = np.dot(np.linalg.inv(np.dot(B.T,B)),B.T)
    B1 = np.array(B1,np.float32)

    #########################################
    width  = int(atr['WIDTH'])
    length = int(atr['FILE_LENGTH'])
    lt     = len(dateList)
    timeseries = np.zeros((lt,length,width),np.float32)
    for date in dateList:
        timeseries[dateIndex[date]] = h5timeseries[k].get(date)
  
    numpixels=length*width
    
    Data=np.zeros([lt,numpixels])
    for i in range(lt):
        Data[i,:]=np.reshape(timeseries[i],[1,numpixels])
  
    x=np.dot(B1,Data)
    velocity=np.reshape(x[0,:],[length,width])

    #####################################################
    print 'Calculating rmse'
    Data_linear=np.dot(B,x)
    rmse=np.reshape(np.sqrt((np.sum((Data_linear-Data)**2,0))/lt),[length,width])
    # se=np.reshape((np.sum(np.abs(Data_linear-Data),0)/lt),[length,width])
    # rmse=np.reshape((np.sum((Data_linear-Data)**2,0))/lt,[length,width])
    ######################################################
    print 'Calculating the standard deviation of the estimated velocities'
    residual=Data_linear-Data
    s1=np.sqrt(np.sum(residual**2,0)/(lt-2))
    s2=np.sqrt(np.sum((datevector-np.mean(datevector))**2))
    se=np.reshape(s1/s2,[length,width])
    ######################################################
     
    # SSt=np.sum((Data-np.mean(Data,0))**2,0)
    # SSres=np.sum(residual**2,0)
    # SS_REG=SSt-SSres
    # Rsquared=np.reshape(SS_REG/SSt,[length,width])
    ######################################################  
    # covariance of the velocities
  
    ##### Output File Name
    try:    outName
    except:
        if not datesNot2include == []: outName = 'velocity_ex.h5'
        else:                          outName = 'velocity.h5'
    outName_rmse='rmse_'+outName
    outName_se='std_'+outName
    outName_Rsquared='R2_'+outName
  
    #####################################
    print '--------------------------------------'
    print 'writing to '+outName
    h5velocity = h5py.File(outName,'w')
    group=h5velocity.create_group('velocity')
    dset = group.create_dataset('velocity', data=velocity, compression='gzip')
    group.attrs['date1'] = datevector[0]
    group.attrs['date2'] = datevector[lt-1]
    
    for key , value in atr.iteritems():
        group.attrs[key]=value
    h5velocity.close()  
  
    #####################################
    print 'writing to '+outName_rmse
    h5file = outName_rmse
    h5rmse = h5py.File(h5file,'w')
    group=h5rmse.create_group('rmse')
    dset = group.create_dataset(os.path.basename('rmse'), data=rmse, compression='gzip')
    group.attrs['date1'] = datevector[0]
    group.attrs['date2'] = datevector[lt-1]
  
    for key , value in atr.iteritems():
        group.attrs[key]=value  

    #####################################
    print 'writing to '+outName_se
    h5se = h5py.File(outName_se,'w')
    group=h5se.create_group('rmse')
    dset = group.create_dataset('rmse', data=se, compression='gzip')
    group.attrs['date1'] = datevector[0]
    group.attrs['date2'] = datevector[lt-1]
  
    for key , value in atr.iteritems():
        group.attrs[key]=value
  
    # print 'writing to '+outName_Rsquared
    # h5rsquared = h5py.File(outName_Rsquared,'w')
    # group=h5rsquared.create_group('rmse')
    # dset = group.create_dataset('rmse', data=Rsquared, compression='gzip')
    # group.attrs['date1'] = datevector[0]
    # group.attrs['date2'] = datevector[lt-1]
  
  
    # h5rsquared.close()
    h5se.close()
    h5rmse.close()
    h5timeseries.close()
    print 'Done.'
Exemplo n.º 18
0
def main(argv):

    global xsub, ysub, thr
    parallel = 'yes'     ## Use parallel by default for multiple input files

    ######################################
    try:    opts, args = getopt.getopt(argv,"h:f:m:t:x:y:o:",['no-parallel'])
    except getopt.GetoptError:    Usage() ; sys.exit(1)

    if len(sys.argv) > 3:
        for opt,arg in opts:
            if opt in ("-h","--help"):     Usage();  sys.exit()
            elif opt == '-f':        File     = arg.split(',')
            elif opt == '-m':        maskFile = arg
            elif opt == '-t':        thr  = float(arg)
            elif opt == '-y':        ysub = [int(i) for i in arg.split(':')];     ysub.sort()
            elif opt == '-x':        xsub = [int(i) for i in arg.split(':')];     xsub.sort()
            elif opt == '-o':        outFile = arg
            elif opt == '--no-parallel':   parallel = 'no'

    elif len(sys.argv)==3:
        if os.path.isfile(argv[0]) and os.path.isfile(argv[1]):
            File     = argv[0].split(',')
            maskFile = argv[1]
        else:  print 'Input file does not existed: '+argv[0]+' / '+argv[1];  sys.exit(1)
    else:   Usage();  sys.exit(1)

    try:
        File
        maskFile
    except:    Usage() ; sys.exit(1)

    ##### Check Input File List
    print '\n****************** Masking *********************'
    fileList = ut.get_file_list(File)
    print 'number of file to mask: '+str(len(fileList))
    print fileList

    if len(fileList) == 1:
        parallel = 'no'
        try: outFile          ## Customized output file name for one input file only
        except:
            ext     = os.path.splitext(fileList[0])[1]
            outFile = os.path.basename(fileList[0]).split('.')[0]+'_masked'+ext
    elif len(fileList) > 1:
        try:
            del outFile
            print 'Disabled customized output name for multiple input files, continue with automatic naming insread.'
        except: pass
    else: print 'ERROR: No input file!'; sys.exit(1)

    ##### Check parallel computing requirement
    if parallel == 'yes':
        try:
            from joblib import Parallel, delayed
            import multiprocessing
        except:
            print '/nCannot import joblib or multiprocessing!'
            print 'Disabled parallel masking.'
            print 'Continue with masking file by file ...'

    ###### Read Mask File
    atr_mask = readfile.read_attributes(maskFile)
    k_mask = atr_mask['FILE_TYPE']
    if not k_mask == 'coherence':    ## Read mask file once 
        M,Matr = readfile.read(maskFile)
        print 'mask file: '+maskFile

    ##### Masking - file by file
    if parallel == 'no':
        ##### Single Mask
        if not k_mask == 'coherence':
            for in_file in fileList:
                print '-------------------------------------------'
                print 'masking  : '+in_file
                try:    mask_file(in_file,M,outFile)
                except: mask_file(in_file,M)
        ##### Multiple Mask
        else:
            try:    mask_with_multi_masks(fileList[0],maskFile,outFile)
            except: mask_with_multi_masks(fileList[0],maskFile)

    ##### Masking - parallel
    else:
        print '-----------------------'
        print 'parallel masking ...'
        print '-----------------------'
        num_cores = multiprocessing.cpu_count()
        Parallel(n_jobs=num_cores)(delayed(mask_file)(in_file,M) for in_file in fileList)
Exemplo n.º 19
0
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()
Exemplo n.º 20
0
def main(argv):

    lineWidth   = 2
    fontSize    = 12
    markerColor = 'orange'
    markerSize  = 16
    networkDisplay = 'no'
  
    if len(sys.argv)>2:
  
        try:  opts, args = getopt.getopt(argv,"h:f:C:s:w:m:c:t:b:d:l:n:N:T:l:")
        except getopt.GetoptError: Usage() ; sys.exit(1)
    
        for opt,arg in opts:
            if opt in ("-h","--help"):   Usage();  sys.exit()
            elif opt == '-f':        File           = arg
            elif opt == '-C':        corFile        = arg
            elif opt == '-s':        fontSize       = int(arg)
            elif opt == '-w':        lineWidth      = int(arg)
            elif opt == '-m':        markerSize     = int(arg)
            elif opt == '-c':        markerColor    = arg
            elif opt == '-t':        temp_thr       = float(arg)
            elif opt == '-b':        base_thr       = float(arg)
            elif opt == '-d':        dates2Rmv      = arg
            elif opt == '-l':        ifgrams_to_rmv = arg
            elif opt == '-n':        networkDisplay = arg
            elif opt == '-N':        ifgrams_Number_to_rmv = arg.split()
            elif opt == '-T':        templateFile   = arg
            elif opt == '-l':        list_fileList  = arg.split(',')
    
    
        try:  File
        except:  Usage() ; sys.exit(1)
  
    elif len(sys.argv)==2:
        File = argv[0]
        networkDisplay = 'yes'
    else:   Usage() ; sys.exit(1)
  
    ## display network for modification, if no other limit setted
    try:
        temp_thr
        base_trh
        dates2Rmv
        ifgrams_to_rmv
        ifgrams_Number_to_rmv
        networkDisplay = 'yes'
    except: pass

    ###########################################################
    atr = readfile.read_attributes(File)
    k = atr['FILE_TYPE']
    print '\n*************** Modify Network ****************'
    print 'Input file is '+k
    #if h5file.keys()[0] != 'interferograms':
    #    print 'Input file should be interferograms'; sys.exit(1)
    h5file = h5py.File(File)
    ifgramList = h5file[k].keys()
    ifgramList = sorted(ifgramList)
  
    try:     ifgrams_to_rmv
    except:  ifgrams_to_rmv=[]

    ###########################################################
    ##### L - list file of interferograms
    try: ifgrams_to_rmv = list_file.read()
    except: pass 
  
    #####  T - templateFile, pysar.dropIfgIndex
    try:
        templateFile
        template = readfile.read_template(templateFile)
        drop_ifg_index = template['pysar.drop.ifgIndex'].split(',')
        print 'drop interferogram index:'
        print drop_ifg_index
        try:    ifgrams_Number_to_rmv
        except: ifgrams_Number_to_rmv = []
        for index in drop_ifg_index:
            index_temp = [int(i) for i in index.split(':')];    index_temp.sort()
            if   len(index_temp)==2:
                for j in range(index_temp[0],index_temp[1]+1):  ifgrams_Number_to_rmv.append(str(j))
            elif len(index_temp)==1:                            ifgrams_Number_to_rmv.append(index)
            else: print 'Unrecoganized input: '+index
    except: pass
  
    #####  N - interferogram number list
    try:
        for i in ifgrams_Number_to_rmv:
            print i+'    '+ifgramList[int(i)-1]
            ifgrams_to_rmv.append(ifgramList[int(i)-1])
    except: pass
  
    #####  b - perpendicular baseline limit
    try:
        base_thr
        print 'interferograms with the spatial baseline longer than '+ str(base_thr)+' m is removed'
        for ifgram in  ifgramList:
            Baseline = (float(h5file[k][ifgram].attrs['P_BASELINE_BOTTOM_HDR'])+\
                        float(h5file[k][ifgram].attrs['P_BASELINE_TOP_HDR']))/2
            if abs(Baseline) > base_thr:
                if not ifgram in ifgrams_to_rmv:   ifgrams_to_rmv.append(ifgram)
    except:    print 'No Spatial Baseline threshold applied'
  
    ##### d - dates to remove
    try:
        dates2Rmv
        print 'interferograms with any of following dates will be removed: '+ dates2Rmv
        for ifgram in  ifgramList:
            date1,date2 = h5file[k][ifgram].attrs['DATE12'].split('-')
            if (date1 in dates2Rmv) or (date2 in dates2Rmv):
                if not ifgram in ifgrams_to_rmv:   ifgrams_to_rmv.append(ifgram)
    except:   print 'No specific dates selected to remove'

    ##### t - temporal baseline limit
    tbase,dateList,dateDict,dateList6=ut.date_list(h5file)
    try:
        temp_thr
        print 'Applying the temporal baseline threshold with threshold of '+str(temp_thr)+' days'
        for ifgram in  ifgramList:
            date1,date2 = h5file[k][ifgram].attrs['DATE12'].split('-')      
            ind1 = dateList6.index(date1)
            ind2 = dateList6.index(date2)
            dt=tbase[ind2]-tbase[ind1]
            if dt>temp_thr:
                if not ifgram in ifgrams_to_rmv:
                    ifgrams_to_rmv.append(ifgram)
    except:
        print 'No Temporal Baseline threshold applied'

    ############################################################
    ############################################################
    if networkDisplay=='yes':
  
        dates,datevector = ptime.date_list2vector(dateList)
    
        ##################################################  
        Bp = ut.Baseline_timeseries(File)
        #############################################################
     
        ifgramList = h5file[k].keys()
        igram_pairs=np.zeros([len(ifgramList),2],np.int)
        i=0
        for ifgram in  ifgramList:
            date1,date2 = h5file[k][ifgram].attrs['DATE12'].split('-')
            igram_pairs[i][0]=dateList6.index(date1)
            igram_pairs[i][1]=dateList6.index(date2)
            i=i+1
    
        ############################################################
        import matplotlib.pyplot as plt
        fig1 = plt.figure(1)
        ax1=fig1.add_subplot(111)
    
        ax1.cla()
        # ax1.plot(dates,Bp, 'o',ms=markerSize, lw=lineWidth, alpha=0.7, mfc=markerColor)
        print tbase
        ax1.plot(tbase,Bp, 'o',ms=markerSize, lw=lineWidth, alpha=0.7, mfc=markerColor)
        for ni in range(len(ifgramList)):
            ax1.plot(array([tbase[igram_pairs[ni][0]],tbase[igram_pairs[ni][1]]]),\
                     array([Bp[igram_pairs[ni][0]],Bp[igram_pairs[ni][1]]]),'k',lw=4) 
        # ax1.fmt_xdata = DateFormatter('%Y-%m-%d %H:%M:%S')
        ax1.set_ylabel('Bperp [m]',fontsize=fontSize)
        ax1.set_xlabel('Time [years]',fontsize=fontSize)
        ts=datevector[0]+0.2
        te=datevector[-1]+0.2
        ys=int(ts)
        ye=int(te)
        ms=int((ts-ys)*12)
        me=int((te-ye)*12)
        if ms>12:       ys =ys+1;       ms=1
        if me>12:       ye =ye+1;       me=1
        if ms<1:        ys =ys-1;       ms=12
        if me<1:        ye =ye-1;       me=12
    
        dss=datetime.datetime(ys,ms,1,0,0)
        dee=datetime.datetime(ye,me,1,0,0)
        ax1.set_ylim(min(Bp)-0.4*abs(min(Bp)),max(Bp)+0.4*max(Bp))
    
        xticklabels = getp(gca(), 'xticklabels')
        yticklabels = getp(gca(), 'yticklabels')
        setp(yticklabels, 'color', 'k', fontsize=fontSize)
        setp(xticklabels, 'color', 'k', fontsize=fontSize)

        ##########################################  
        x=[]
        y=[]
        Master_index_torremove=[]
        Slave_index_torremove=[]
        a_tbase=array(tbase)
        a_Bp=array(Bp)
        def onclick(event):
            if event.button==1:
                print 'click'
                xClick = event.xdata
                yClick = event.ydata
                idx=nearest_neighbor(xClick,yClick, a_tbase, a_Bp)       
                xr = a_tbase[idx]
                yr = a_Bp[idx]
                ix=tbase.index(xr)+1
                print ix
                x.append(xr)
                y.append(yr)
                if mod(len(x),2)==0:
                    Master_index_torremove.append(tbase.index(xr))
                    ax1.plot([x[len(x)-1],x[len(x)-2]],[y[len(x)-1],y[len(x)-2]],'r',lw=4)
                else:
                    Slave_index_torremove.append(tbase.index(xr))
            plt.show()
        cid = fig1.canvas.mpl_connect('button_press_event', onclick)
    
    
        plt.show()
        print Master_index_torremove
        print Slave_index_torremove
    
        if len(Master_index_torremove) == len(Slave_index_torremove):
            R=np.vstack((Master_index_torremove,Slave_index_torremove))
        else:
            R=np.vstack((Master_index_torremove[:-1],Slave_index_torremove))
    
        R=np.vstack((Master_index_torremove,Slave_index_torremove)) 
        R.sort(0)
        print R
        print dateList6
        numIgrams_rmv=np.shape(R)[1]
        for ifgram in  ifgramList:
            date1,date2 = h5file[k][ifgram].attrs['DATE12'].split('-')
            for i in range(numIgrams_rmv):
                if dateList6[R[0][i]]==date1 and dateList6[R[1][i]]==date2:
                    ifgrams_to_rmv.append(ifgram)
    
    else:
        print 'No network display.'

    ############################################################
    ############################################################
    print 'Number of interferograms to remove: '+str(len(ifgrams_to_rmv))
    print 'List   of interferograms to remove:' 
    print ifgrams_to_rmv
    file_modified='Modified_'+File
    h5filem = h5py.File(file_modified,'w')
    gg = h5filem.create_group(k)
    ifgram=ifgramList[0]
    unw = h5file[k][ifgram].get(ifgram)
    MaskZero=np.ones([unw.shape[0],unw.shape[1]])
  
    print 'writing >>> modified interferogram file ...'
    print 'Number of interferograms: '+str(len(ifgramList)-len(ifgrams_to_rmv))
    for ifgram in  ifgramList:
        if not ifgram in ifgrams_to_rmv:
            print ifgram
            unwSet = h5file[k][ifgram].get(ifgram)
            unw = unwSet[0:unwSet.shape[0],0:unwSet.shape[1]]        
            MaskZero=unw*MaskZero
            group = gg.create_group(ifgram)
            dset = group.create_dataset(ifgram, data=unw, compression='gzip')
            for key, value in h5file[k][ifgram].attrs.iteritems():
                group.attrs[key] = value
  
    Mask=np.ones([unwSet.shape[0],unwSet.shape[1]])
    Mask[MaskZero==0]=0
  
    h5file.close()
    h5filem.close()


    ####################### Coherence ########################
    # updating Coherence file
    # convert ifgrams_to_rmv to cor_to_rmv
    date12_to_rmv=[]
    for igram in ifgrams_to_rmv:
        date12_to_rmv.append(igram.split('-sim')[0].split('filt_')[-1])
  
    try:
        corFile
        h5fileCor=h5py.File(corFile)
        corList=h5fileCor['coherence'].keys()
   
        corFile_modified='Modified_'+corFile
        h5fileCorm=h5py.File(corFile_modified,'w')
        gc = h5fileCorm.create_group('coherence')
        print 'writing >>> modified coherence file ...'
        for cor in corList:
            date12=cor.split('-sim')[0].split('filt_')[-1]
            if not date12 in date12_to_rmv:
                print cor
                unwSet = h5fileCor['coherence'][cor].get(cor)
                unw = unwSet[0:unwSet.shape[0],0:unwSet.shape[1]]
                group = gc.create_group(cor)
                dset = group.create_dataset(cor, data=unw, compression='gzip')
                for key, value in h5fileCor['coherence'][cor].attrs.iteritems():
                    group.attrs[key] = value  
        h5fileCor.close()
        h5fileCorm.close()
    except:
        print 'No coherence file to be updated.'

    ############################################################
  
    print 'writing >>> Modified_Mask.h5'
    
    h5mask = h5py.File('Modified_Mask.h5','w')
    group  = h5mask.create_group('mask')
    dset   = group.create_dataset(os.path.basename('mask'), data=Mask, compression='gzip')
    for key, value in atr.iteritems():
        group.attrs[key] = value
    h5mask.close()      
Exemplo n.º 21
0
def remove_surface(File, surf_type, Mask, outName=""):
    start = time.time()
    ##### Output File Name
    if outName == "":
        ext = os.path.splitext(File)[1].lower()
        outName = os.path.basename(File).split(ext)[0] + "_" + surf_type + ext

    ##### Input File Info
    atr = readfile.read_attributes(File)
    k = atr["FILE_TYPE"]
    print "Input file is " + atr["PROCESSOR"] + " " + k

    ## Multiple Datasets File
    if k in ["interferograms", "coherence", "wrapped", "timeseries"]:
        h5file = h5py.File(File, "r")
        ifgramList = h5file[k].keys()
        ifgramList = sorted(ifgramList)
        print "number of epochs: " + str(len(ifgramList))

        h5flat = h5py.File(outName, "w")
        group = h5flat.create_group(k)
        print "writing >>> " + outName

    if k in ["timeseries"]:
        for ifgram in ifgramList:
            print "Removing " + surf_type + " from " + ifgram
            data = h5file[k].get(ifgram)[:]

            data_n, ramp = remove_data_surface(data, Mask, surf_type)

            dset = group.create_dataset(ifgram, data=data_n, compression="gzip")
        for key, value in h5file[k].attrs.iteritems():
            group.attrs[key] = value

    elif k in ["interferograms", "wrapped", "coherence"]:
        for ifgram in ifgramList:
            print "Removing " + surf_type + " from " + ifgram
            data = h5file[k][ifgram].get(ifgram)[:]

            data_n, ramp = remove_data_surface(data, Mask, surf_type)

            gg = group.create_group(ifgram)
            dset = gg.create_dataset(ifgram, data=data_n, compression="gzip")
            for key, value in h5file[k][ifgram].attrs.iteritems():
                gg.attrs[key] = value

    ## Single Dataset File
    else:
        try:
            data, atr = readfile.read(File)
        except:
            pass
        print "Removing " + surf_type + " from " + k

        data_n, ramp = remove_data_surface(data, Mask, surf_type)

        writefile.write(data_n, atr, outName)

    try:
        h5file.close()
        h5flat.close()
    except:
        pass

    print "Remove " + surf_type + " took " + str(time.time() - start) + " secs"
Exemplo n.º 22
0
def main(argv):

    ##### Default
    fontSize    = 12
    lineWidth   = 2
    markerColor = 'crimson'
    markerSize  = 16

    disp_fig  = 'no'
    save_fig  = 'yes'
    save_list = 'yes'

    ref_file  = 'reference_date.txt'
    drop_file = 'drop_date.txt'

    ##### Check Inputs
    if len(sys.argv)>3:
        try:
            opts, args = getopt.getopt(argv,'h:f:m:o:x:y:',['help','circle='])
        except getopt.GetoptError:
            print 'Error in reading input options!';  Usage() ; sys.exit(1)

        for opt,arg in opts:
            if opt in ("-h","--help"):    Usage() ; sys.exit()
            elif opt == '-f':  File      = arg
            elif opt == '-m':  maskFile  = arg
            elif opt == '-x':  xsub = [int(i) for i in arg.split(':')];  xsub.sort()
            elif opt == '-y':  ysub = [int(i) for i in arg.split(':')];  ysub.sort()
            elif opt == '--circle'   :  cir_par   = [i for i in arg.split(';')]
            #elif opt == '-o':  outName   = arg
            
    else:
        try:  File = argv[0]
        except: Usage(); sys.exit(1)
        try:  maskFile = argv[1]
        except: pass

    try:  atr  = readfile.read_attributes(File)
    except: Usage(); sys.exit(1)
    ext      = os.path.splitext(File)[1].lower()
    FileBase = os.path.basename(File).split(ext)[0]
    outNameBase = 'spatialMean_'+FileBase
    print '\n*************** Spatial Average ******************'

    ##### Input File Info
    k = atr['FILE_TYPE']
    print 'Input file is '+k
    width  = int(atr['WIDTH'])
    length = int(atr['FILE_LENGTH'])

    h5file = h5py.File(File)
    epochList = h5file[k].keys();
    epochList = sorted(epochList)
    epochNum  = len(epochList)
    print 'number of epoch: '+str(epochNum)
    dates,datevector = ptime.date_list2vector(epochList)

    ##### Mask Info
    try:
        Mask_orig,Matr = readfile.read(maskFile)
        print 'mask file: '+maskFile
        Masking = 'yes'
    except:
        print 'No mask. Use the whole area for ramp estimation.'
        Masking = 'no'
        Mask_orig=np.ones((length,width))
    Mask = np.zeros((length,width))
    Mask[:] = Mask_orig[:]

    ## Bounding Subset
    try:
        xsub
        ysub
        ysub,xsub = subset.check_subset_range(ysub,xsub,atr)
        Mask[ysub[0]:ysub[1],xsub[0]:xsub[1]] = Mask_orig[ysub[0]:ysub[1],xsub[0]:xsub[1]]*2
        #Mask[0:ysub[0],:]      = 0
        #Mask[ysub[1]:length,:] = 0
        #Mask[:,0:xsub[0]]      = 0
        #Mask[:,xsub[1]:width]  = 0
    except:
        Mask = Mask_orig*2
        print 'No subset input.'

    ## Circle Inputs
    try:
        cir_par
        for i in range(len(cir_par)):
            cir_idx = circle_index(atr,cir_par[i])
            Mask[cir_idx] = Mask_orig[cir_idx]
            print 'Circle '+str(i)+': '+cir_par[i]
    except: print 'No circle of interest input.'
    
    ## Mask output
    idx = Mask == 2
    idxNum = float(sum(sum(idx)))
    
    fig = plt.figure()
    plt.imshow(Mask,cmap='gray')
    plt.savefig(outNameBase+'_mask.png',bbox_inches='tight')
    print 'save mask to '+outNameBase+'_mask.png'
    #fig.clf()

    ##### Calculation
    meanList   = np.zeros(epochNum)
    pixPercent = np.zeros(epochNum)
    pixT = 0.7
    print 'calculating ...'
    print '  Date       Mean   Percentage'
    for i in range(epochNum):
        epoch = epochList[i]
        d      = h5file[k].get(epoch)[:]
        #d[Mask==0]  = np.nan
        
        meanList[i]   = np.nanmean(d[idx])
        pixPercent[i] = np.sum(d[idx] >= pixT)/idxNum
        
        print epoch+' :   %.2f    %.1f%%'%(meanList[i],pixPercent[i]*100)
    del d
    h5file.close()

    ##### Reference date - Max Value
    top3 = sorted(zip(meanList,epochList), reverse=True)[:3]
    print '------------ Top 3 Mean ------------------'
    print top3
    ## Write to txt file
    fref = open(ref_file,'w')
    fref.write(str(top3[0][1])+'\n')
    fref.close()
    print 'write optimal reference date to '+ref_file
    idxMean = meanList == np.nanmax(meanList)

    ##### Drop dates - mean threshold
    #meanT = 0.7
    #idxMean  = meanList < meanT
    #print '------------ Mean Value < '+str(meanT)+' --------'
    #print np.array(epochList)[idxMean]
    #print meanList[idxMean]

    ##### Drop dates - good pixel percentage
    pixNumT = 0.7
    print '------------ Good Pixel Percentage < %.0f%% -------'%(pixNumT*100)
    idxPix = pixPercent < pixNumT
    dropEpochList = np.array(epochList)[idxPix]
    print dropEpochList
    print pixPercent[idxPix]
    ## Write to txt file
    fdrop = open(drop_file,'w')
    for i in range(len(dropEpochList)):
        fdrop.write(str(dropEpochList[i])+'\n')
    fdrop.close()
    print 'write drop dates to '+drop_file
    print '-------------------------------------------'

    ##### Display
    fig = plt.figure(figsize=(12,12))
    ax  = fig.add_subplot(211)
    ax.plot(dates, meanList, '-ko', ms=markerSize, lw=lineWidth, alpha=0.7, mfc=markerColor)
    #ax.plot([dates[0],dates[-1]],[meanT,meanT], '--b', lw=lineWidth)
    #sc = ax.scatter(dates, np.tile(0.5,epochNum), c=meanList, s=22**2, alpha=0.3, vmin=0.0, vmax=1.0)
    #ax.scatter(np.array(dates)[idxMean], 0.5, c=meanList[idxMean], s=22**2, alpha=1.0, vmin=0.0, vmax=1.0)
    ax = ptime.adjust_xaxis_date(ax,datevector)
    ax.set_ylim(0,1)
    ax.set_title('Spatial Average Value', fontsize=fontSize)
    ax.set_xlabel('Time [years]',         fontsize=fontSize)
    #cbar = plt.colorbar(sc)
    #cbar.set_label('Spatial Mean of Normalized Sum Epochs')

    ax  = fig.add_subplot(212)
    ax.plot(dates, pixPercent, '-ko', ms=markerSize, lw=lineWidth, alpha=0.7, mfc=markerColor)
    ax.plot([dates[0],dates[-1]],[pixNumT,pixNumT], '--b', lw=lineWidth)
    ax = ptime.adjust_xaxis_date(ax,datevector)
    ax.set_ylim(0,1)
    ax.set_title('Percenrage of Pixels with Value > '+str(pixNumT), fontsize=fontSize)
    ax.set_xlabel('Time [years]',         fontsize=fontSize)
    vals = ax.get_yticks()
    ax.set_yticklabels(['{:3.0f}%'.format(i*100) for i in vals])

    if save_fig == 'yes':
        plt.savefig(outNameBase+'.png',bbox_inches='tight')
        print 'save figure to '+outNameBase+'.png'

    if disp_fig == 'yes':
        plt.show()

    ##### Output
    if save_list == 'yes':
        epochList6 = ptime.yymmdd(epochList)
        fl = open(outNameBase+'.txt','w')
        for i in range(epochNum):
            str_line = epochList6[i]+'    %.2f    %.2f\n'%(meanList[i],pixPercent[i])
            fl.write(str_line)
        fl.close()
        print 'write data to '+outNameBase+'.txt\n'
Exemplo n.º 23
0
def timeseries_inversion(igramsFile,timeseriesFile):
    ## modified from sbas.py written by scott baker, 2012 
    '''Implementation of the SBAS algorithm.
    
    Usage:
    timeseries_inversion(h5flat,h5timeseries)
      h5flat: hdf5 file with the interferograms 
      h5timeseries: hdf5 file with the output from the inversion
    '''
    total = time.time()
  
    global B1, dt, numDates
    h5flat = h5py.File(igramsFile,'r')
    A,B = design_matrix(h5flat)
    tbase,dateList,dateDict,dateDict2 = date_list(h5flat)
    dt = np.diff(tbase)
    B1 = np.linalg.pinv(B)
    B1 = np.array(B1,np.float32)
    numDates = len(dateList)
  
    ##### Basic Info
    ifgramList = h5flat['interferograms'].keys()
    numIfgrams = len(ifgramList)
    atr = readfile.read_attributes(igramsFile)
    length = int(atr['FILE_LENGTH'])
    width  = int(atr['WIDTH'])
    numPixels = length * width
    print 'number of interferograms: '+str(numIfgrams)
    print 'number of pixels        : '+str(numPixels)
    #numPixels_step = int(numPixels/10)

    ##### Inversion Function
    def ts_inverse_point(dataPoint):
        nan_ndx = dataPoint == 0.
        fin_ndx = dataPoint != 0.
        nan_fin = dataPoint.copy()
        nan_fin[nan_ndx] = 1
        if not nan_fin.sum() == len(nan_fin):
            B1tmp = np.dot(B1,np.diag(fin_ndx))
            tmp_rate = np.dot(B1tmp,dataPoint)
            zero = np.array([0.],np.float32)
            defo = np.concatenate((zero,np.cumsum([tmp_rate*dt])))
        else: defo = np.zeros((modelDimension+1,1),np.float32)
        return defo
  
    def ts_inverse(dataLine):
        numPoint = dataLine.shape[1]
        tmp_rate = np.dot(B1,dataLine)
        defo1 = tmp_rate * np.tile(dt.reshape((numDates-1,1)),(1,numPoint))
        defo0 = np.array([0.]*numPoint,np.float32)
        defo  = np.vstack((defo0, np.cumsum(defo1,axis=0)))
        return defo
  
    ##### Read Interferograms
    print 'Reading interferograms ...'
    data = np.zeros((numIfgrams,numPixels),np.float32)
    for j in range(numIfgrams):
        print ifgramList[j]+'   '+str(j+1)
        d = h5flat['interferograms'][ifgramList[j]].get(ifgramList[j])[:]
        data[j] = d.flatten(1)
    h5flat.close()

    ##### Inversion
    print 'Inversing time series ...'
    dataPoint = np.zeros((numIfgrams,1),np.float32)
    dataLine  = np.zeros((numIfgrams,width),np.float32)
    modelDimension  = np.shape(B)[1]
    tempDeformation = np.zeros((modelDimension+1,numPixels),np.float32)
    for i in range(length):
        dataLine = data[:,i*width:(i+1)*width]
        defoLine = ts_inverse(dataLine)
        tempDeformation[:,i*width:(i+1)*width] = defoLine
  
        #for j in range(width):
        #    dataPoint = data[:,j]
        #    try: tempDeformation[:,i*length+j] = point_inverse(dataPoint)
        #    except: pass
  
        printProgress(i+1,length,prefix='calculating:')
    del data
  
    ##### Time Series Data Preparation
    print 'converting phase to range'
    timeseries = np.zeros((modelDimension+1,length,width),np.float32)
    phase2range = -1*float(atr['WAVELENGTH'])/(4.*np.pi)
    for ni in range(modelDimension+1):
        timeseries[ni] = tempDeformation[ni].reshape(width,length).T
        timeseries[ni] = timeseries[ni]*phase2range
    del tempDeformation
  
    ##### Output Time Series File
    print 'writing >>> '+timeseriesFile
    print 'number of dates: '+str(numDates)
    h5timeseries = h5py.File(timeseriesFile,'w')
    group = h5timeseries.create_group('timeseries')
    dateIndex = ptime.date_index(dateList)
    for date in dateList:
        if not date in h5timeseries['timeseries']:
            print date
            dset = group.create_dataset(date, data=timeseries[dateIndex[date]], compression='gzip')
    ## Attributes
    print 'calculating perpendicular baseline timeseries'
    Bperp = Baseline_timeseries(igramsFile)
    Bperp = str(Bperp.tolist()).translate(None,'[],')
    atr['P_BASELINE_TIMESERIES'] = Bperp
    for key,value in atr.iteritems():   group.attrs[key] = value
    h5timeseries.close()
  
    print 'Done.\nTime series inversion took ' + str(time.time()-total) +' secs'
Exemplo n.º 24
0
def main(argv):

    ########################## Check Inputs ################################################
    ## Default value
    Masking   = 'no'
    save_mask = 'no'
  
    if len(sys.argv) > 4:
        try: opts, args = getopt.getopt(argv,'h:f:m:o:s:t:y:',['help','save-mask'])
        except getopt.GetoptError:  print 'Error while getting args!\n';  Usage(); sys.exit(1)
  
        for opt,arg in opts:
            if   opt in ['-h','--help']:    Usage(); sys.exit()
            elif opt in '-f':    File     = arg
            elif opt in '-m':    maskFile = arg
            elif opt in '-o':    outName  = arg
            elif opt in '-s':    surfType = arg.lower()
            elif opt in '-t':    templateFile = arg
            elif opt in '-y':    ysub = [int(i) for i in arg.split(',')]
            elif opt in '--save-mask'  :    save_mask = 'yes'
  
    elif len(sys.argv) in [3,4]:
        File          = argv[0]
        surfType      = argv[1].lower()
        try: maskFile = argv[2]
        except: pass
    else: Usage(); sys.exit(1)
  
    print '\n*************** Phase Ramp Removal ***********************'
    ## Multiple Surfaces
    try:
        ysub
        if not len(ysub)%2 == 0:
            print 'ERROR: -y input has to have even length!'
            sys.exit(1)
        surfNum = len(ysub)/2
    except:
        surfNum = 1
    print 'phase ramp number: '+str(surfNum)
  
    ## Tempate File
    try:
        templateFile
        templateContents = readfile.read_template(templateFile)
    except: pass

    try:        surfType
    except:
        try:    surfType = templateContents['pysar.orbitError.method']
        except: surfType = 'plane'; print 'No ramp type input, use plane as default'
    print 'phase ramp type  : '+surfType
  
    ## Input File(s)
    fileList = glob.glob(File)
    fileList = sorted(fileList)
    print 'input file(s): '+str(len(fileList))
    print fileList
  
    atr = readfile.read_attributes(fileList[0])
    length = int(atr['FILE_LENGTH'])
    width  = int(atr['WIDTH'])

    ## Output File(s)
    if   len(fileList) >  1:    outName = ''
    elif len(fileList) == 0:    print 'ERROR: Cannot find input file(s)!';  sys.exit(1)
    else:    ## Customized output name only works for single file input
Exemplo n.º 25
0
def main(argv):

    global method_default
    ##### Referencing methods
    method_default = 'max_coherence'
    #method = 'manual'
    #method = 'max_coherence'        ## Use phase on point with max coherence [default]
    #method = 'global_average'       ## Use Nan Mean of phase on all pixels
    #method = 'random'
    #maskFile = 'Mask.h5'

    global SeedingDone
    
    ############################## Check Inputs ##############################
    if len(sys.argv) > 2:
        try:  opts, args = getopt.getopt(argv,"h:c:f:m:y:x:l:L:t:o:r:",\
                                         ['manual','max-coherence','global-average','random'])
        except getopt.GetoptError:  Usage() ; sys.exit(1)

        for opt,arg in opts:
            if   opt in ("-h","--help"):   Usage();  sys.exit()
            elif opt == '-f':        File     = arg
            elif opt == '-m':        maskFile = arg
            elif opt == '-c':        corFile  = arg
            elif opt == '-o':        outFile  = arg

            elif opt == '-y':        ry       = int(arg)
            elif opt == '-x':        rx       = int(arg)
            elif opt == '-l':        rlat     = float(arg)
            elif opt == '-L':        rlon     = float(arg)
            elif opt == '-r':        refFile  = arg
            elif opt == '-t':        templateFile = arg

            elif opt == '--global-average' :  method = 'global_average'
            elif opt == '--manual'         :  method = 'manual'
            elif opt == '--max-coherence'  :  method = 'max_coherence'
            elif opt == '--random'         :  method = 'random'

    elif len(sys.argv)==2:
        if   argv[0]=='-h':            Usage(); sys.exit(1)
        elif os.path.isfile(argv[0]):  File = argv[0]
        else:  print 'Input file does not existed: '+argv[0];  sys.exit(1)
    elif len(sys.argv)<2:             Usage(); sys.exit(1)

    ##### Input File Info
    try:
        File
        atr = readfile.read_attributes(File)
        k = atr['FILE_TYPE']
        length = int(atr['FILE_LENGTH'])
        width  = int(atr['WIDTH'])
    except:  Usage() ; sys.exit(1)
    ext = os.path.splitext(File)[1].lower()

    try:    outFile
    except: outFile = 'Seeded_'+File
  
    ############################## Reference Point Input ####################
    try:
        refFile
        atr_ref = readfile.read_attributes(refFile)
    except: pass
  
    try:
        templateFile
        templateContents = readfile.read_template(templateFile)
    except: pass

    ### Priority
    ## lat/lon > y/x
    ## Direct Input > Reference File > Template File
    try:
        rlat
        rlon
    except:
        try:
            rlat = float(atr_ref['ref_lat'])
            rlon = float(atr_ref['ref_lon'])
        except:
            try: rlat,rlon = [float(i) for i in templateContents['pysar.seed.lalo'].split(',')]
            except: pass

    try:
        ry
        rx
    except:
        try:
            ry = int(atr_ref['ref_y'])
            rx = int(atr_ref['ref_x'])
        except:
            try: ry,rx       = [int(i)   for i in templateContents['pysar.seed.yx'].split(',')]
            except: pass

    ##### Check lalo / YX
    print '\n************** Reference Point ******************'
    try:
        rlat
        rlon
        y = sub.coord_geo2radar(rlat,atr,'lat')
        x = sub.coord_geo2radar(rlon,atr,'lon')
        0<= x <= width
        0<= y <= length
        rx = x
        ry = y
        print 'Reference point: lat = %.4f,   lon = %.4f'%(rlat,rlon)
        print '                 y   = %d,     x   = %d'%(ry,rx)
    except:
        print 'Skip input lat/lon reference point.'
        print 'Continue with the y/x reference point.'


    ######################### a. Read Mask File #########################
    ## Priority: Input mask file > pysar.mask.file 
    try:     maskFile
    except:
        try: maskFile = templateContents['pysar.mask.file']
        except:  print 'No mask found!';
    try:
        M,Matr = readfile.read(maskFile);
        print 'mask: '+maskFile
    except:
        print '---------------------------------------------------------'
        print 'WARNING: No mask, use the whole area as mask'
        print '---------------------------------------------------------'
        M = np.ones((length,width))

    ## Message
    try:
        rx
        ry
        0<= rx <= width
        0<= ry <= length
        if M[ry,rx] == 0:
            print 'Input point has 0 value in mask.'
    except: pass

    ######################### b. Stack ##################################
    stackFile = os.path.basename(File).split(ext)[0] + '_stack.h5'
    stack_file_exist = 'no'
    try:
        os.path.isfile(stackFile)
        stack,atrStack = readfile.read(stackFile)
        if width == int(atrStack['WIDTH']) and length == int(atrStack['FILE_LENGTH']):
            stack_file_exist = 'yes'
            print 'read stack from file: '+stackFile
    except: pass

    if stack_file_exist == 'no':
        print 'calculating stack of input file ...'
        stack = ut.stacking(File)
        atrStack = atr.copy()
        atrStack['FILE_TYPE'] = 'mask'
        writefile.write(stack,atrStack,stackFile)

    ## Message
    try:
        rx
        ry
        if stack[ry,rx] == 0:
            print 'Input point has nan value in data.'
    except: pass

    stack[M==0] = 0
    if np.nansum(M) == 0.0:
        print '\n*****************************************************'
        print   'ERROR:'
        print   'There is no pixel that has valid phase value in all datasets.' 
        print   'Check the file!'
        print   'Seeding failed'
        sys.exit(1)

    ######################### Check Method ##############################
    try:
        not stack[ry,rx] == 0
        method = 'input_coord'
    except:
        try:    method
        except: method = method_default
        print 'Skip input y/x reference point.'
        print 'Continue with '+method

    #h5file = h5py.File(File)

    ######################### Seeding ###################################
    ##### Sub-function
    def seed_method(method,File,stack,outFile,corFile=''):
        SeedingDone = 'no'
        next_method = method_default
        M = stack != 0

        if   method == 'manual':
            SeedingDone = seed_manual(File,stack,outFile)
            if SeedingDone == 'no':
                next_method = method_default
                print_warning(next_method)

        elif method == 'max_coherence':
            try:    SeedingDone = seed_max_coherence(File,M,outFile,corFile)
            except: SeedingDone = seed_max_coherence(File,M,outFile)
            if SeedingDone == 'no':
                next_method = 'random'
                print_warning(next_method)

        elif method == 'random':
            y,x = random_selection(stack)
            seed_xy(File,x,y,outFile)
            SeedingDone = 'yes'

        elif method == 'global_average':
            print '\n---------------------------------------------------------'
            print 'Automatically Seeding using Global Spatial Average Value '
            print '---------------------------------------------------------'
            print 'Calculating the global spatial average value for each epoch'+\
                  ' of all valid pixels ...'
            box = (0,0,width,length)
            meanList = ut.spatial_mean(File,M,box)
            seed_file(File,outFile,meanList,'','')
            SeedingDone = 'yes'

        return SeedingDone, next_method

    ##### Seeding
    SeedingDone = 'no'

    if method == 'input_coord':
        seed_xy(File,rx,ry,outFile)
        SeedingDone = 'yes'

    else:
        i = 0
        while SeedingDone == 'no' and i < 5:
            try:    SeedingDone,method = seed_method(method,File,stack,outFile,corFile)
            except: SeedingDone,method = seed_method(method,File,stack,outFile)
            i += 1
        if i >= 5:
            print 'ERROR: Seeding failed after more than '+str(i)+' times try ...'
            sys.exit(1)
Exemplo n.º 26
0
def main(argv):
    try:
        igramsFile = argv[0]
        timeSeriesFile = argv[1]
    except:
        Usage()
        sys.exit(1)

    try:
        tempCohFile = argv[2]
    except:
        tempCohFile = "temporal_coherence.h5"

    ########################################################
    print "\n********** Temporal Coherence ****************"
    print "load time series: " + timeSeriesFile
    atr_ts = readfile.read_attributes(timeSeriesFile)
    h5timeseries = h5py.File(timeSeriesFile)
    dateList = h5timeseries["timeseries"].keys()
    numDates = len(dateList)

    print "number of epoch: " + str(numDates)
    dateIndex = {}
    for ni in range(numDates):
        dateIndex[dateList[ni]] = ni

    dset = h5timeseries["timeseries"].get(h5timeseries["timeseries"].keys()[0])
    nrows, ncols = np.shape(dset)
    timeseries = np.zeros((len(h5timeseries["timeseries"].keys()), np.shape(dset)[0] * np.shape(dset)[1]), np.float32)

    for i in range(numDates):
        date = dateList[i]
        dset = h5timeseries["timeseries"].get(date)
        d = dset[0 : dset.shape[0], 0 : dset.shape[1]]
        timeseries[dateIndex[date]][:] = d.flatten(0)
        ut.printProgress(i + 1, numDates, "loading:", date)
    del d
    h5timeseries.close()

    lt, numpixels = np.shape(timeseries)
    range2phase = -4 * np.pi / float(atr_ts["WAVELENGTH"])
    timeseries = range2phase * timeseries

    ######################################################
    print "load interferograms: " + igramsFile
    h5igrams = h5py.File(igramsFile)
    ifgramList = h5igrams["interferograms"].keys()
    numIfgrams = len(ifgramList)
    print "number of epochs: " + str(numIfgrams)
    A, B = design_matrix(h5igrams)
    p = -1 * np.ones([A.shape[0], 1])
    Ap = np.hstack((p, A))

    print "calculating temporal coherence ..."
    # data = np.zeros((numIfgrams,numpixels),np.float32)
    qq = np.zeros(numpixels) + 0j
    for ni in range(numIfgrams):
        ## read interferogram
        igram = ifgramList[ni]
        dset = h5igrams["interferograms"][igram].get(igram)
        data = dset[0 : dset.shape[0], 0 : dset.shape[1]]
        data = data.flatten(0)

        ## calculate difference between observed and estimated data
        ## interferogram by interferogram, less memory, Yunjun - 2016.06.10
        dataEst = np.dot(Ap[ni, :], timeseries)
        dataDiff = data - dataEst
        qq += np.exp(1j * dataDiff)

        ## progress bar
        ut.printProgress(ni + 1, numIfgrams, "calculating:", igram)
    del timeseries, data, dataEst, dataDiff
    h5igrams.close()

    # qq=np.absolute(np.sum(np.exp(1j*dataDiff),0))/numIfgrams
    qq = np.absolute(qq) / numIfgrams
    Temp_Coh = np.reshape(qq, [nrows, ncols])

    ##### write temporal coherence file ####################
    print "writing >>> " + tempCohFile
    h5TempCoh = h5py.File(tempCohFile, "w")
    group = h5TempCoh.create_group("temporal_coherence")
    dset = group.create_dataset(os.path.basename("temporal_coherence"), data=Temp_Coh, compression="gzip")
    for key, value in atr_ts.iteritems():
        group.attrs[key] = value
    group.attrs["UNIT"] = "1"
    h5TempCoh.close()
Exemplo n.º 27
0
def main(argv):
    dp             = 1.0
    ntrans         = 1
    save_to_mat    = 'off'
    flip_profile   = 'no'
    which_gps      = 'all'
    flip_updown    = 'yes'
    incidence_file ='incidence_file'
    display_InSAR              = 'on'
    display_Average            = 'on'
    disp_std = 'on'

    ##### Input Args
    try:  opts, args = getopt.getopt(argv,"f:s:e:n:d:g:l:h:r:L:F:p:u:G:S:i:I:A:U:E:")
    except getopt.GetoptError:   Usage() ; sys.exit(1)

    for opt,arg in opts:
        if   opt == '-f':   velocityFile = arg
        elif opt == '-s':   y0,x0  = [int(i) for i in arg.split(',')]
        elif opt == '-e':   y1,x1  = [int(i) for i in arg.split(',')]
        elif opt == '-n':   ntrans = int(arg)
        elif opt == '-d':   dp     = float(arg)
        elif opt == '-g':   gpsFile=arg
        elif opt == '-r':   refStation=arg
        elif opt == '-i':   incidence_file=arg
        elif opt == '-L':   stationsList = arg.split(',')
        elif opt == '-F':   FaultCoords  = arg.split(',')
        elif opt == '-p':   flip_profile = arg
        elif opt == '-u':   flip_updown  = arg; print flip_updown
        elif opt == '-G':   which_gps =arg
        elif opt == '-S':   gps_source=arg
        elif opt == '-h':   hbound=float(arg) 
        elif opt == '-l':   lbound=float(arg)
        elif opt == '-I':   display_InSAR   = arg
        elif opt == '-A':   display_Average = arg
        elif opt == '-U':   disp_std        = arg
        elif opt == '-E':   save_to_mat     = arg

    ##### Input File Info
    try: atr = readfile.read_attributes(velocityFile)
    except:  Usage(); sys.exit(1)
    k = atr['FILE_TYPE']
    print 'input file is '+k

    h5file = h5py.File(velocityFile,'r')
    z= h5file[k].get(k)[:]

    width  = int(atr['WIDTH'])
    length = int(atr['FILE_LENGTH'])
    try: lat,lon,lat_step,lon_step,lat_all,lon_all = get_lat_lon(atr)
    except:  print 'radar coordinate'

    ##### Fault Coordinates
    try:
        Lat0 = dms2d(FaultCoords[0]); Lon0 = dms2d(FaultCoords[1])
        Lat1 = dms2d(FaultCoords[2]); Lon1 = dms2d(FaultCoords[3])
        Length,Width=np.shape(z)
        Yf0,Xf0=find_row_column(Lon0,Lat0,lon,lat,lon_step,lat_step)
        Yf1,Xf1=find_row_column(Lon1,Lat1,lon,lat,lon_step,lat_step)
  
        print '*********************************************'
        print ' Fault Coordinates:'
        print '   --------------------------  '
        print '    Lat          Lon'
        print str(Lat0) + ' , ' +str(Lon0)
        print str(Lat1) + ' , ' +str(Lon1)
        print '   --------------------------  '
        print '    row          column'
        print str(Yf0) + ' , ' +str(Xf0)
        print str(Yf1) + ' , ' +str(Xf1)
        print '*********************************************'
        #mf=float(Yf1-Yf0)/float((Xf1-Xf0))  # slope of the fault line
        #cf=float(Yf0-mf*Xf0)   # intercept of the fault line
        #df0=dist_point_from_line(mf,cf,x0,y0,1,1)   #distance of the profile start point from the Fault line
        #df1=dist_point_from_line(mf,cf,x1,y1,1,1)  #distance of the profile end point from the Fault line
  
        #mp=-1./mf  # slope of profile which is perpendicualr to the fault line 
        #x1=int((df0+df1)/np.sqrt(1+mp**2)+x0)    # correcting the end point of the profile to be on a line perpendicular to the Fault
        #y1=int(mp*(x1-x0)+y0)
    except:
        print '*********************************************'
        print 'No information about the Fault coordinates!'
        print '*********************************************'

#############################################################################
    try:
        x0;y0;x1;y1
    except:
        fig = plt.figure()
        ax=fig.add_subplot(111)
        ax.imshow(z)
        try: ax.plot([Xf0,Xf1],[Yf0,Yf1],'k-')
        except: print 'Fault line is not specified'
  
        xc=[]
        yc=[]
        print 'please click on start and end point of the desired profile'
        def onclick(event):
            if event.button==1:
                print 'click'
                xc.append(int(event.xdata))
                yc.append(int(event.ydata))
        cid = fig.canvas.mpl_connect('button_press_event', onclick)
        plt.show();
        x0=xc[0];x1=xc[1]
        y0=yc[0];y1=yc[1]
##############################################################################
    try:
        mf=float(Yf1-Yf0)/float((Xf1-Xf0))  # slope of the fault line
        cf=float(Yf0-mf*Xf0)   # intercept of the fault line
        df0=dist_point_from_line(mf,cf,x0,y0,1,1)   #distance of the profile start point from the Fault line
        df1=dist_point_from_line(mf,cf,x1,y1,1,1)  #distance of the profile end point from the Fault line
  
        mp=-1./mf  # slope of profile which is perpendicualr to the fault line 
        x1=int((df0+df1)/np.sqrt(1+mp**2)+x0)    # correcting the end point of the profile to be on a line perpendicular to the Fault
        y1=int(mp*(x1-x0)+y0)
    except:
        Info_aboutFault='No'

##############################################################################
    print '******************************************************'
    print 'First profile coordinates:'
    print 'Start point:  y = '+str(y0)+', x = '+str(x0) 
    print 'End   point:  y = '+str(y1)+', x = '+str(x1)   
    print '******************************************************'
    length = int(np.hypot(x1-x0, y1-y0))
    x, y = np.linspace(x0, x1, length), np.linspace(y0, y1, length)
    zi = z[y.astype(np.int), x.astype(np.int)]
    try:
        lat_transect=lat_all[y.astype(np.int), x.astype(np.int)]
        lon_transect=lon_all[y.astype(np.int), x.astype(np.int)] 
    except:
        lat_transect='Nan'
        lon_transect='Nan'
 
    earth_radius = 6371e3;    # in meter
    try:
        dx=float(atr['X_STEP'])*np.pi/180.0*earth_radius*np.sin(np.mean(lat)*np.pi/180)
        dy=float(atr['Y_STEP'])*np.pi/180.0*earth_radius
        DX=(x-x0)*dx
        DY=(y-y0)*dy
        D =np.hypot(DX, DY)
        print 'geo coordinate:'
        print 'profile length = ' +str(D[-1]/1000.0) + ' km'
        # df0_km=dist_point_from_line(mf,cf,x0,y0,dx,dy)
    except:
        dx=float(atr['RANGE_PIXEL_SIZE'])
        dy=float(atr['AZIMUTH_PIXEL_SIZE'])
        DX=(x-x0)*dx
        DY=(y-y0)*dy
        D=np.hypot(DX, DY)
        print 'radar coordinate:'
        print 'profile length = ' +str(D[-1]/1000.0) + ' km'       
        # df0_km=dist_point_from_line(mf,cf,x0,y0,dx,dy)

    try: df0_km=dist_point_from_line(mf,cf,x0,y0,dx,dy)
    except: print 'Fault line is not specified'

    import pdb; pdb.set_trace()


    transect      = np.zeros([len(D),ntrans])
    transect[:,0] = zi
    XX0=[];XX1=[]
    YY0=[];YY1=[]
    XX0.append(x0);XX1.append(x1)
    YY0.append(y0);YY1.append(y1)

    if ntrans >1:
        m  = float(y1-y0)/float((x1-x0))
        c  = float(y0-m*x0)       
        m1 = -1.0/m
        if lat_transect=='Nan':
            for i in range(1,ntrans):
                X0=i*dp/np.sqrt(1+m1**2)+x0  
                Y0=m1*(X0-x0)+y0
                X1=i*dp/np.sqrt(1+m1**2)+x1
                Y1=m1*(X1-x1)+y1
                zi=get_transect(z,X0,Y0,X1,Y1)         
                transect[:,i]=zi
                XX0.append(X0);XX1.append(X1);
                YY0.append(Y0);YY1.append(Y1);
        else:
            transect_lat      = np.zeros([len(D),ntrans])
            transect_lat[:,0] = lat_transect
            transect_lon      = np.zeros([len(D),ntrans])
            transect_lon[:,0] = lon_transect
    
            for i in range(1,ntrans):
                X0=i*dp/np.sqrt(1+m1**2)+x0
                Y0=m1*(X0-x0)+y0
                X1=i*dp/np.sqrt(1+m1**2)+x1
                Y1=m1*(X1-x1)+y1
                zi=get_transect(z,X0,Y0,X1,Y1)
                lat_transect=get_transect(lat_all,X0,Y0,X1,Y1)
                lon_transect=get_transect(lon_all,X0,Y0,X1,Y1)       
                transect[:,i]=zi
                transect_lat[:,i]=lat_transect
                transect_lon[:,i]=lon_transect
                XX0.append(X0);XX1.append(X1);
                YY0.append(Y0);YY1.append(Y1);


    #############################################
    try:  m_prof_edge,c_prof_edge=line(XX0[0],YY0[0],XX0[-1],YY0[-1])    
    except:  print 'Plotting one profile'    

    ###############################################################################    
    if flip_profile=='yes':
        transect=np.flipud(transect);
        try:         df0_km=np.max(D)-df0_km;
        except:    print '';
    

    print '******************************************************'
    try:    gpsFile
    except: gpsFile='Nogps'
    print 'GPS velocity file:'
    print gpsFile
    print '*******************************************************'
    if os.path.isfile(gpsFile):
       insarData=z
       del z
       fileName, fileExtension = os.path.splitext(gpsFile)
     #  print fileExtension
     #  if fileExtension =='.cmm4':
     #      print 'reading cmm4 velocities'
     #      Stations, gpsData = redGPSfile_cmm4(gpsFile)
     #      idxRef=Stations.index(refStation)
     #      Lon,Lat,Ve,Vn,Se,Sn,Corr,Hrate,H12=gpsData[idxRef,:]
     #      Lon=Lon-360.0
          # Lat,Lon,Ve,Se,Vn,Sn,Corr,NumEpochs,timeSpan,AvgEpochTimes = gpsData[idxRef,:]
     #      Vu=0
     #  else:
     #      Stations, gpsData = redGPSfile(gpsFile)
     #      idxRef=Stations.index(refStation)
     #      Lat,Lon,Vn,Ve,Sn,Se,Corr,Vu,Su = gpsData[idxRef,:]
      
       Stations,Lat,Lon,Ve,Se,Vn,Sn=readGPSfile(gpsFile,gps_source)
       idxRef=Stations.index(refStation)
       Length,Width=np.shape(insarData)
      # lat,lon,lat_step,lon_step = get_lat_lon(h5file,Length,Width)
       lat,lon,lat_step,lon_step,lat_all,lon_all=get_lat_lon(h5file)
       IDYref,IDXref=find_row_column(Lon[idxRef],Lat[idxRef],lon,lat,lon_step,lat_step)
       if (not np.isnan(IDYref)) and (not np.isnan(IDXref)):
         print 'referencing InSAR data to the GPS station at : ' + str(IDYref) + ' , '+ str(IDXref)
         if not np.isnan(insarData[IDYref][IDXref]):
             transect = transect - insarData[IDYref][IDXref]
             insarData=insarData - insarData[IDYref][IDXref]
            
         else:
            
             print ''' 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
      
      WARNING: nan value for InSAR data at the refernce pixel!
               reference station should be a pixel with valid value in InSAR data.
                               
               please select another GPS station as the reference station.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%                       
                   '''
             sys.exit(1)
       else:
         print 'WARNING:'
         print 'Reference GPS station is out of the area covered by InSAR data'
         print 'please select another GPS station as the reference station.'
         sys.exit(1)
       
       try:
         stationsList
       except:
         stationsList = Stations

        
      # theta=23.0*np.pi/180.0
       if os.path.isfile(incidence_file):
           print 'Using exact look angle for each pixel'
           h5file_theta=h5py.File(incidence_file,'r')
           dset=h5file_theta['mask'].get('mask')
           theta=dset[0:dset.shape[0],0:dset.shape[1]]
           theta=theta*np.pi/180.0
       else:
           print 'Using average look angle'
           theta=np.ones(np.shape(insarData))*23.0*np.pi/180.0

       heading=193.0*np.pi/180.0
       
     #  unitVec=[-np.sin(theta)*np.sin(heading),-np.cos(heading)*np.sin(theta),-np.cos(theta)]
       unitVec=[np.cos(heading)*np.sin(theta),-np.sin(theta)*np.sin(heading),0]#-np.cos(theta)]
       
      #  [0.0806152480932643, 0.34918300221540616, -0.93358042649720174]
       # print unitVec 
       # unitVec=[0.3,-0.09,0.9]
      # unitVec=[-0.3,0.09,-0.9]
      # unitVec=[-0.3,0.09,0]

      # print '*******************************************'
      # print 'unit vector to project GPS to InSAR LOS:'
      # print unitVec
      # print '*******************************************'
      # gpsLOS_ref=unitVec[0]*Ve[idxRef]+unitVec[1]*Vn[idxRef]#+unitVec[2]*Vu[idxRef]       

#       print np.shape(theta)
#       print IDYref
#       print IDXref
#       print theta[IDYref,IDXref]

       gpsLOS_ref = gps_to_LOS(Ve[idxRef],Vn[idxRef],theta[IDYref,IDXref],heading)
       print '%%%%%%^^^^^^^%%%%%%%%'
       print gpsLOS_ref/1000.0
      # insarData=insarData -gpsLOS_ref/1000.0 
      # transect = transect -gpsLOS_ref/1000.0

       GPS=[]
       GPS_station=[]
       GPSx=[]
       GPSy=[]
       GPS_lat=[]
       GPS_lon=[]
       for st in stationsList:
         try :
           idx=Stations.index(st)
          
          # gpsLOS = unitVec[0]*Ve[idx]+unitVec[1]*Vn[idx]#+unitVec[2]*Vu[idx]
            
         #  gpsLOS = gps_to_LOS(Ve[idx],Vn[idx],theta[idx],heading)
         #  gpsLOS=gpsLOS-gpsLOS_ref

           IDY,IDX=find_row_column(Lon[idx],Lat[idx],lon,lat,lon_step,lat_step)
           print theta[IDY,IDX]
           gpsLOS = gps_to_LOS(Ve[idx],Vn[idx],theta[IDY,IDX],heading)
         #  gpsLOS = gpsLOS-gpsLOS_ref

           if which_gps =='all':
             if theta[IDY,IDX]!=0.0:
               GPS.append(gpsLOS-gpsLOS_ref)
               GPS_station.append(st)
               GPSx.append(IDX)
               GPSy.append(IDY)
               GPS_lat.append(Lat[idx])
               GPS_lon.append(Lon[idx])   
           elif not np.isnan(insarData[IDY][IDX]):
             if theta[IDY,IDX]!=0.0:
               GPS.append(gpsLOS-gpsLOS_ref)
               GPS_station.append(st)
               GPSx.append(IDX)
               GPSy.append(IDY)
               GPS_lat.append(Lat[idx])
               GPS_lon.append(Lon[idx])
         except:
           NoInSAR='yes'   
       
      # print GPS_station
      # print gpsLOS 
       DistGPS=[]
       GPS_in_bound=[]
       GPS_in_bound_st=[] 
       GPSxx=[]
       GPSyy=[]
       for i in range(len(GPS_station)):
         gx=GPSx[i]
         gy=GPSy[i]
 #        print '******************'
      #   print gx
      #   print gy
         if which_gps in ['all','insar']:
             check_result = 'True'
         else:
             check_result=check_st_in_box(gx,gy,x0,y0,x1,y1,X0,Y0,X1,Y1)

         if check_result=='True':
           check_result2=check_st_in_box2(gx,gy,x0,y0,x1,y1,X0,Y0,X1,Y1)
           GPS_in_bound_st.append(GPS_station[i])
           GPS_in_bound.append(GPS[i])
           GPSxx.append(GPSx[i])
           GPSyy.append(GPSy[i])   
          # gy=y0+1
          # gx=x0+1
          # gxp,gyp=get_intersect(m,c,gx,gy)
          # Dx=dx*(gx-gxp);Dy=dy*(gy-gyp)
          # print gxp
          # print gyp
           dg = dist_point_from_line(m,c,gx,gy,1,1) # distance of GPS station from the first profile line
          # DistGPS.append(np.hypot(Dx,Dy))
          # X0=dg/np.sqrt(1+m1**2)+x0
          # Y0=m1*(X0-x0)+y0
          # DistGPS.append(np.hypot(dx*(gx-X0), dy*(gy-Y0)))
          
           DistGPS.append(dist_point_from_line(m_prof_edge,c_prof_edge,GPSx[i],GPSy[i],dx,dy))
           

       print '****************************************************'
       print 'GPS stations in the profile area:' 
       print GPS_in_bound_st
       print '****************************************************'
       GPS_in_bound = np.array(GPS_in_bound)
       DistGPS = np.array(DistGPS)
   #    axes[1].plot(DistGPS/1000.0, -1*GPS_in_bound/1000, 'bo')

    if gpsFile=='Nogps':

        insarData=z
        GPSxx=[]
        GPSyy=[]
        GPSx=[];GPSy=[]
        GPS=[]
        XX0[0]=x0;XX1[0]=x1;YY0[0]=y0;YY1[0]=y1

   # else:

    print '****************'
    print 'flip up-down'
    print flip_updown

    if flip_updown=='yes' and gpsFile!='Nogps':
       print 'Flipping up-down'
       transect=-1*transect
       GPS_in_bound=-1*GPS_in_bound
    elif flip_updown=='yes':
       print 'Flipping up-down'
       transect=-1*transect


    if flip_profile=='yes' and gpsFile!='Nogps':
       
       GPS=np.flipud(GPS)
       GPS_in_bound=np.flipud(GPS_in_bound)
       DistGPS=np.flipud(max(D)-DistGPS)


    fig, axes = plt.subplots(nrows=2)
    axes[0].imshow(insarData)
    for i in range(ntrans):
        axes[0].plot([XX0[i], XX1[i]], [YY0[i], YY1[i]], 'r-')

    axes[0].plot(GPSx,GPSy,'b^')
    axes[0].plot(GPSxx,GPSyy,'k^')
    if gpsFile!='Nogps':
        axes[0].plot(IDXref,IDYref,'r^')       
    axes[0].axis('image')
    axes[1].plot(D/1000.0,transect,'ko',ms=1)

    avgInSAR=np.array(nanmean(transect,axis=1))
    stdInSAR=np.array(nanstd(transect,axis=1))
  #  print avgInSAR
  #  print stdInSAR
    
      #std=np.std(transect,1)
   # axes[1].plot(D/1000.0, avgInSAR, 'r-')
    try:
      axes[1].plot(DistGPS/1000.0, -1*GPS_in_bound/1000, 'b^',ms=10)
    except:
      print ''
   # pl.fill_between(x, y-error, y+error,alpha=0.6, facecolor='0.20')
   # print transect
#############################################################################

    fig2, axes2 = plt.subplots(nrows=1)
    axes2.imshow(insarData)
    #for i in range(ntrans):
    axes2.plot([XX0[0], XX1[0]], [YY0[0], YY1[0]], 'k-')
    axes2.plot([XX0[-1], XX1[-1]], [YY0[-1], YY1[-1]], 'k-')
    axes2.plot([XX0[0], XX0[-1]], [YY0[0], YY0[-1]], 'k-')
    axes2.plot([XX1[0], XX1[-1]], [YY1[0], YY1[-1]], 'k-')

    try:
       axes2.plot([Xf0,Xf1],[Yf0,Yf1], 'k-')
    except:
       FaultLine='None'
    

    axes2.plot(GPSx,GPSy,'b^')
    axes2.plot(GPSxx,GPSyy,'k^')
    if gpsFile!='Nogps':
        axes2.plot(IDXref,IDYref,'r^')
    axes2.axis('image')

    figName = 'transect_area.png'
    print 'writing '+figName
    plt.savefig(figName)    

#############################################################################
    fig = plt.figure()
    fig.set_size_inches(10,4)
    ax = plt.Axes(fig, [0., 0., 1., 1.], )
    ax=fig.add_subplot(111)
    if display_InSAR in ['on','On','ON']:
       ax.plot(D/1000.0,transect*1000,'o',ms=1,mfc='Black', linewidth='0')


############################################################################
# save the profile data:     
    if save_to_mat in ['ON','on','On','yes','y','YES','Yes']:
       import scipy.io as sio
       matFile='transect.mat'
       dataset={}
       dataset['datavec']=transect
       try:
         dataset['lat']=transect_lat
         dataset['lon']=transect_lon
       except:
         dataset['lat']='Nan'
         dataset['lon']='Nan'
       dataset['Unit']='m'
       dataset['Distance_along_profile']=D
       print '*****************************************'
       print ''
       print 'writing transect to >>> '+matFile
       sio.savemat(matFile, {'dataset': dataset})
       print ''
       print '*****************************************'
############################################################################
 #   ax.plot(D/1000.0, avgInSAR*1000, 'r-')
 
#    ax.plot(D/1000.0,transect*1000/(np.sin(23.*np.pi/180.)*np.cos(38.*np.pi/180.0)),'o',ms=1,mfc='Black', linewidth='0')
#    ax.plot(D/1000.0, avgInSAR*1000/(np.sin(23.*np.pi/180.)*np.cos(38.*np.pi/180.0)), 'r-')

#############################################################################
    if disp_std in ['on','On','ON']:
 
       for i in np.arange(0.0,1.01,0.01):
          ax.plot(D/1000.0, (avgInSAR-i*stdInSAR)*1000, '-',color='#DCDCDC',alpha=0.5)#,color='#DCDCDC')#'LightGrey')
       for i in np.arange(0.0,1.01,0.01):
          ax.plot(D/1000.0, (avgInSAR+i*stdInSAR)*1000, '-',color='#DCDCDC',alpha=0.5)#'LightGrey')
#############################################################################
    if display_Average in ['on','On','ON']:
       ax.plot(D/1000.0, avgInSAR*1000, 'r-')
########### 
  # ax.fill_between(D/1000.0, (avgInSAR-stdInSAR)*1000, (avgInSAR+stdInSAR)*1000,where=(avgInSAR+stdInSAR)*1000>=(avgInSAR-stdInSAR)*1000,alpha=1, facecolor='Red')

    try:      
        ax.plot(DistGPS/1000.0, -1*GPS_in_bound, '^',ms=10,mfc='Cyan')
    except:
        print ''
    ax.set_ylabel('LOS velocity [mm/yr]',fontsize=26)
    ax.set_xlabel('Distance along profile [km]',fontsize=26)


   # print '******************'
   # print 'Dsitance of fault from the beginig of profile(km):'
   # print df0_km/1000.0


    ###################################################################
    #lower and higher bounds for diplaying the profile

    try:
       lbound
       hbound
    except:
       lbound=np.nanmin(transect)*1000
       hbound=np.nanmax(transect)*1000


    ###################################################################
    #To plot the Fault location on the profile
    try:
       ax.plot([df0_km/1000.0,df0_km/1000.0], [lbound,hbound], '--',color='black',linewidth='2')
    except:
       fault_loc='None'

    ###################################################################
    

    try: 
         ax.set_ylim(lbound,hbound)
    except:
         ylim='no'

   # try: 
   #      ax.set_xlim(-10,300)
   # except:
    #     xlim='no'


##########
#Temporary To plot DEM
   # try:
#    majorLocator = MultipleLocator(5)
#    ax.yaxis.set_major_locator(majorLocator)
#    minorLocator   = MultipleLocator(1)
#    ax.yaxis.set_minor_locator(minorLocator)

#    plt.tick_params(which='major', length=15,width=2)
#    plt.tick_params(which='minor', length=6,width=2)

#    try:
#       for tick in ax.xaxis.get_major_ticks():
#                tick.label.set_fontsize(26)
#       for tick in ax.yaxis.get_major_ticks():
#                tick.label.set_fontsize(26)
#    
#       plt.tick_params(which='major', length=15,width=2)
#       plt.tick_params(which='minor', length=6,width=2)
#    except:
#       print 'couldn not fix the ticks! '


    figName = 'transect.png'
    print 'writing '+figName
    plt.savefig(figName)
    print ''
    print '________________________________'
#############################################################################
    plt.show()
Exemplo n.º 28
0
def seed_file(File,outName,refList,ref_x='',ref_y=''):
    ## Seed Input File with reference value in refList
    print 'Reference value: '
    print refList

    #####  IO Info
    atr = readfile.read_attributes(File)
    k = atr['FILE_TYPE']
    print 'file type: '+k

    ##### Multiple Dataset File
    if k in ['timeseries','interferograms','wrapped','coherence']:
        ##### Input File Info
        h5file = h5py.File(File,'r')
        epochList = h5file[k].keys()
        epochList = sorted(epochList)
        epochNum  = len(epochList)
        print 'number of epochs: '+str(epochNum)
        
        ##### Check Epoch Number
        if not epochNum == len(refList):
            print '\nERROR: Reference value has different epoch number'+\
                  'from input file.'
            print 'Reference List epoch number: '+str(refList)
            print 'Input file     epoch number: '+str(epochNum)
            sys.exit(1)
  
        ##### Output File Info
        h5out = h5py.File(outName,'w')
        group = h5out.create_group(k)
        print 'writing >>> '+outName

    ## Loop
    if k == 'timeseries':
        for i in range(epochNum):
            epoch = epochList[i]
            print epoch
            data = h5file[k].get(epoch)[:]
            
            data -= refList[i]
  
            dset = group.create_dataset(epoch, data=data, compression='gzip')

        atr  = seed_attributes(atr,ref_x,ref_y)
        for key,value in atr.iteritems():   group.attrs[key] = value

    elif k in ['interferograms','wrapped','coherence']:
        for i in range(epochNum):
            epoch = epochList[i]
            #print epoch
            data = h5file[k][epoch].get(epoch)[:]
            atr  = h5file[k][epoch].attrs

            data -= refList[i]
            atr  = seed_attributes(atr,ref_x,ref_y)

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

            ut.printProgress(i+1,epochNum,'seeding:',epoch)
  
    ##### Single Dataset File
    else:
        data,atr = readfile.read(File)

        data -= refList
        atr  = seed_attributes(atr,ref_x,ref_y)

        writefile.write(data,atr,outName)
  
    ##### End & Cleaning
    try:
        h5file.close()
        h5out.close()
    except: pass

    return 1
Exemplo n.º 29
0
def main(argv):

    try:  
        File = argv[0]
        alks = int(argv[1])
        rlks = int(argv[2])
    except:
        Usage();sys.exit(1)
  
    ext = os.path.splitext(File)[1]
    try:     outName = argv[3]
    except:  outName = File.split('.')[0]+'_a'+str(int(alks))+'lks_r'+str(int(rlks))+'lks'+ext
  
    ################################################################################
    atr = readfile.read_attributes(File)
    k = atr['FILE_TYPE']
    print '\n***************** Multilooking *********************'
    print 'number of multilooking in azimuth / latitude  direction: '+str(alks)
    print 'number of multilooking in range   / longitude direction: '+str(rlks)
    print 'input file: '+k
  
    if k in ['interferograms','coherence','wrapped','timeseries']:
        h5file     = h5py.File(File,'r')
        h5file_mli = h5py.File(outName,'w')
  
        print 'writing >>> '+outName 
  
        if k in ['interferograms','coherence','wrapped']:
            gg = h5file_mli.create_group(k)
            igramList = h5file[k].keys()
            igramList = sorted(igramList)
  
            for igram in igramList:
                print igram
                unw = h5file[k][igram].get(igram)[:]
                unwlks = multilook(unw,alks,rlks)
                group = gg.create_group(igram)
                dset = group.create_dataset(igram, data=unwlks, compression='gzip')
  
                atr = h5file[k][igram].attrs
                atr = multilook_attributes(atr,alks,rlks)
                for key, value in atr.iteritems():   group.attrs[key] = value
  
        elif k == 'timeseries':
            dateList=h5file[k].keys()
            dateList = sorted(dateList)
  
            group = h5file_mli.create_group(k)
            for d in dateList:
                print d
                unw = h5file[k].get(d)[:]
                unwlks=multilook(unw,alks,rlks)
                dset = group.create_dataset(d, data=unwlks, compression='gzip')
  
            ## Update attributes
            atr = h5file[k].attrs
            atr = multilook_attributes(atr,alks,rlks)
            for key, value in atr.iteritems():   group.attrs[key] = value
  
        h5file.close()
        h5file_mli.close()

    ################################################################################
    else:
        ####### To multi_look geomap*.trans file, both its file size and value need to be reduced.
        if k == '.trans':
            rg,az,atr = readfile.read(File)
            rgmli = multilook(rg,alks,rlks);    #rgmli = rgmli/float(rlks)
            azmli = multilook(az,alks,rlks);    #azmli = azmli/float(alks)
            atr = multilook_attributes(atr,alks,rlks)
            writefile.write(rgmli,azmli,atr,outName)
        else:
            data,atr = readfile.read(File)
            data_mli = multilook(data,alks,rlks)
            atr = multilook_attributes(atr,alks,rlks)
            writefile.write(data_mli,atr,outName)
Exemplo n.º 30
0
def main(argv):

    global outName
    parallel = 'no'

    ############### Check Inputs ###############
    if len(sys.argv)>3:
        try:
            opts, args = getopt.getopt(argv,'f:l:L:o:t:x:y:r:',['lat=','lon=','row=','col=',\
                                            'parallel','outfill=','outfill-nan','outfill-zero'])
        except getopt.GetoptError:
            print 'Error while getting args'
            Usage() ; sys.exit(1)

        for opt,arg in opts:
            if   opt == '-f':   File         = arg.split(',')
            elif opt == '-o':   outName      = arg
            elif opt == '-t':   templateFile = arg
            elif opt == '-r':   refFile      = arg
            elif opt in ['-x','--col']  :   sub_x   = [int(i)   for i in arg.split(':')];    sub_x.sort()
            elif opt in ['-y','--row']  :   sub_y   = [int(i)   for i in arg.split(':')];    sub_y.sort()
            elif opt in ['-l','--lat']  :   sub_lat = [float(i) for i in arg.split(':')];  sub_lat.sort()
            elif opt in ['-L','--lon']  :   sub_lon = [float(i) for i in arg.split(':')];  sub_lon.sort()
            elif opt in '--parallel'    :   parallel = 'yes'
            elif opt in '--outfill'     :   out_fill = float(arg)
            elif opt in '--outfill-nan' :   out_fill = np.nan
            elif opt in '--outfill-zero':   out_fill = 0.0

    elif len(sys.argv)==3:
        File         = argv[0].split(',')
        templateFile = argv[1]
    elif len(sys.argv)==2:
        if argv[0] in ['-h','--help']:  Usage(); sys.exit()
        else: print '\nERROR: A minimum of 3 inputs is needed.\n'; Usage(); sys.exit()
    else: Usage(); sys.exit(1)

    ##### Check Input file Info
    print '\n**************** Subset *********************'
    fileList = ut.get_file_list(File)
    print 'number of file: '+str(len(fileList))
    print fileList
    atr = readfile.read_attributes(fileList[0])

    if len(fileList) == 1 and parallel == 'yes':
        print 'parallel is disabled for one input file.'
        parallel = 'no'

    ################## Subset Setting ###########
    try:
        atr['X_FIRST']
        print 'geo coordinate'
    except:
        print 'radar coordinate'
    ## Read Subset Inputs
    try:
        templateFile
        template = readfile.read_template(templateFile)
    except: pass

    try:
        refFile
        atr_ref = readfile.read_attributes(refFile)
        box_ref = geo_box(atr_ref)
        lat_ref = [box_ref[3],box_ref[1]]
        lon_ref = [box_ref[0],box_ref[2]]
    except: pass

    try:
        sub_lat
        sub_lon
    except:
        try:
            sub_lat = lat_ref
            sub_lon = lon_ref
        except:
            try:
                sub = template['pysar.subset.lalo'].split(',')
                sub_lat = [float(i) for i in sub[0].split(':')];  sub_lat.sort()
                sub_lon = [float(i) for i in sub[1].split(':')];  sub_lon.sort()
            except: pass; #print 'No pysar.subset.lalo option found in template file!'

    try:
        sub_y
        sub_x
    except:
        try:
            sub = template['pysar.subset.yx'].split(',')
            sub_y = [int(i) for i in sub[0].split(':')];  sub_y.sort()
            sub_x = [int(i) for i in sub[1].split(':')];  sub_x.sort()
        except: pass; #print 'No pysar.subset.yx option found in template file!'

    ## Check Subset Inputs Existed or not
    try:     sub_y
    except:
        try: sub_x
        except:
            try: sub_lat
            except:
                try: sub_lon
                except: print 'ERROR: no subset is setted.'; Usage(); sys.exit(1)

    ##### Subset range radar to geo
    width  = int(atr['WIDTH'])
    length = int(atr['FILE_LENGTH'])
    print 'input file length: '+str(length)
    print 'input file width : '+str(width)

    try: sub_y = coord_geo2radar(sub_lat,atr,'latitude')
    except:
        try:    sub_y
        except: sub_y = [0,length]
    try: sub_x = coord_geo2radar(sub_lon,atr,'longitude')
    except:
        try:    sub_x
        except: sub_x = [0,width]

    ##### Check subset range
    try:
        out_fill
    except:
        sub_y,sub_x = check_subset_range(sub_y,sub_x,atr)
        out_fill = np.nan
        if sub_y[1]-sub_y[0] == length and sub_x[1]-sub_x[0] == width:
            print 'Input subset range == data size, no need to subset.'
            sys.exit(0)

    ################### Subset #######################
    if parallel == 'no':
        for file in fileList:
            print '-------------------------------------------'
            print 'subseting  : '+file
            try:    subset_file(file,sub_x,sub_y,out_fill,outName)
            except: subset_file(file,sub_x,sub_y,out_fill)

    else:
        print '-------------------------'
        print 'parallel subseting ...'
        print '-------------------------'
        from joblib import Parallel, delayed
        import multiprocessing
        num_cores = multiprocessing.cpu_count()
        Parallel(n_jobs=num_cores)(delayed(subset_file)(file,sub_x,sub_y,out_fill) for file in fileList)

    print 'Done.'