Beispiel #1
0
def check_size(k, epochList):
    width_list = []
    length_list = []
    epoch_list = []
    for epoch in epochList:
        rscFile = readfile.read_roipac_rsc(epoch + ".rsc")
        width = rscFile["WIDTH"]
        length = rscFile["FILE_LENGTH"]
        width_list.append(width)
        length_list.append(length)
        epoch_list.append(epoch)

    mode_width = mode(width_list)
    mode_length = mode(length_list)

    if width_list.count(mode_width) != len(width_list) or length_list.count(mode_length) != len(length_list):
        print "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n"
        print "WARNING: Some " + k + " may have the wrong dimensions!\n"
        print "All " + k + " should have the same size.\n"
        print "The width and length of the majority of " + k + " are: " + str(mode_width) + ", " + str(
            mode_length
        ) + "\n"
        print "But the following " + k + " have different dimensions and thus not considered in the time-series: \n"
        for epoch in epoch_list:
            rscFile = readfile.read_roipac_rsc(epoch + ".rsc")
            width = rscFile["WIDTH"]
            length = rscFile["FILE_LENGTH"]
            if width != mode_width or length != mode_length:
                print "  " + epoch + "    width: " + width + "  length: " + length
                epochList.remove(epoch)
        print "\nNumber of " + k + " to be loaded: " + str(len(epochList))
        print "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
    return epochList, mode_width, mode_length
Beispiel #2
0
def extract_attribute_dem_geo(fname):
    '''Read/extract attribute for .dem file from Gamma to ROI_PAC
    For example, it read input file, sim_150911-150922.utm.dem, 
    find its associated par file, sim_150911-150922.utm.dem.par, read it, and
    convert to ROI_PAC style and write it to an rsc file, sim_150911-150922.utm.dem.rsc
    '''
    atr = {}
    atr['PROCESSOR'] = 'gamma'
    atr['INSAR_PROCESSOR'] = 'gamma'
    atr['FILE_TYPE'] = os.path.splitext(fname)[1]
    atr['Y_UNIT'] = 'degrees'
    atr['X_UNIT'] = 'degrees'

    par_file = fname + '.par'
    print 'read ' + os.path.basename(par_file)
    print 'convert Gamma attribute to ROI_PAC style'
    par_dict = readfile.read_gamma_par(par_file)
    par_dict = readfile.attribute_gamma2roipac(par_dict)
    atr.update(par_dict)

    ## Write to .rsc file
    rsc_file = fname + '.rsc'
    try:
        atr_orig = readfile.read_roipac_rsc(rsc_file)
    except:
        atr_orig = None
    if atr_orig != atr:
        print 'writing >>> ' + os.path.basename(rsc_file)
        writefile.write_roipac_rsc(atr, rsc_file)
    return rsc_file
Beispiel #3
0
def extract_attribute(fname):
    '''Read/extract attributes for PySAR from ROI_PAC .unw, .int, .cor file.

    For each unwrapped interferogram or spatial coherence file, there are 2 .rsc files:
        basic metadata file and baseline parameter file. 
        e.g. filt_100901-110117-sim_HDR_4rlks_c10.unw
             filt_100901-110117-sim_HDR_4rlks_c10.unw.rsc
             100901-110117_baseline.rsc
    Inputs:
        fname : string, ROI_PAC interferogram filename or path,
                i.e. /KujuT422F650AlosA/filt_100901-110117-sim_HDR_4rlks_c10.unw
    Outputs:
        atr : dict, Attributes dictionary
    '''
    ## 1. Read basic metadata file
    basic_rsc_file = fname + '.rsc'
    basic_dict = readfile.read_roipac_rsc(basic_rsc_file)

    # return if baseline attributes are already there.
    if 'P_BASELINE_TOP_HDR' in basic_dict.keys():
        return basic_rsc_file

    atr = {}
    atr['PROCESSOR'] = 'roipac'
    atr['INSAR_PROCESSOR'] = 'roipac'
    atr['FILE_TYPE'] = os.path.splitext(fname)[1]

    ## 2. Read baseline metadata file
    date1, date2 = basic_dict['DATE12'].split('-')
    baseline_rsc_file = os.path.dirname(
        fname) + '/' + date1 + '_' + date2 + '_baseline.rsc'
    baseline_dict = readfile.read_roipac_rsc(baseline_rsc_file)
    print 'read ' + os.path.basename(
        basic_rsc_file) + ' and ' + os.path.basename(baseline_rsc_file)

    ## 3. Merge
    atr.update(basic_dict)
    atr.update(baseline_dict)

    ## Write to rsc file
    atr_file = fname + '.rsc'
    #print 'writing >>> '+os.path.basename(atr_file)
    writefile.write_roipac_rsc(atr, atr_file)
    return atr_file
Beispiel #4
0
def roipac_nonzero_mask(unwFileList, maskFile='Mask.h5'):
    '''Generate mask for non-zero amplitude pixel of ROI_PAC .unw file list.'''
    unwFileList, width, length = check_file_size(unwFileList)
    if unwFileList:
        # Initial mask value
        if os.path.isfile(maskFile):
            maskZero, atr = readfile.read(maskFile)
            print 'update existing mask file: '+maskFile
        else:
            maskZero = np.ones([int(length), int(width)])
            atr = None
            print 'create initial mask matrix'

        # Update mask from input .unw file list
        fileNum = len(unwFileList)
        for i in range(fileNum):
            file = unwFileList[i]
            amp, unw, rsc = readfile.read_float32(file)
            
            maskZero *= amp
            ut.print_progress(i+1, fileNum, prefix='loading', suffix=os.path.basename(file))
        mask = np.ones([int(length), int(width)])
        mask[maskZero==0] = 0
        
        # write mask hdf5 file
        print 'writing >>> '+maskFile
        h5 = h5py.File(maskFile,'w')
        group = h5.create_group('mask')
        dset = group.create_dataset('mask', data=mask, compression='gzip')
        # Attribute - *.unw.rsc
        for key,value in rsc.iteritems():
            group.attrs[key] = value
        # Attribute - *baseline.rsc
        d1, d2 = rsc['DATE12'].split('-')
        baseline_file = os.path.dirname(file)+'/'+d1+'_'+d2+'_baseline.rsc'
        baseline_rsc = readfile.read_roipac_rsc(baseline_file)
        for key,value in baseline_rsc.iteritems():
            group.attrs[key] = value
        # Attribute - existed file
        if atr:
            for key, value in atr.iteritems():
                group.attrs[key] = value

    return maskFile, unwFileList
Beispiel #5
0
def radar_or_geo(File):
    ext = os.path.splitext(File)[1]
    if ext == '.h5':
        h5file=h5py.File(File,'r')
        k=h5file.keys()
        if 'interferograms' in k: k[0] = 'interferograms'
        elif 'coherence'    in k: k[0] = 'coherence'
        elif 'timeseries'   in k: k[0] = 'timeseries'
        if   k[0] in ('interferograms','coherence','wrapped'):
            atrKey = h5file[k[0]][h5file[k[0]].keys()[0]].attrs.keys()
        elif k[0] in ('dem','velocity','mask','temporal_coherence','rmse','timeseries'):
            atrKey = h5file[k[0]].attrs.keys()
        h5file.close()
    elif ext in ['.unw','.cor','.int','.hgt','.dem','.trans']:
        atrKey = readfile.read_roipac_rsc(File + '.rsc').keys()
    else: print 'Unrecognized extention: '+ext; return
  
    if 'X_FIRST' in atrKey:  rdr_geo='geo'
    else:                    rdr_geo='radar'
    return rdr_geo
Beispiel #6
0
def extract_attribute_lookup_table(fname):
    '''Read/extract attribute for .UTM_TO_RDC file from Gamma to ROI_PAC
    For example, it read input file, sim_150911-150922.UTM_TO_RDC, 
    find its associated par file, sim_150911-150922.utm.dem.par, read it, and
    convert to ROI_PAC style and write it to an rsc file, sim_150911-150922.UTM_TO_RDC.rsc'''

    ## Check existed .rsc file
    rsc_file_list = ut.get_file_list(fname + '.rsc')
    if rsc_file_list:
        rsc_file = rsc_file_list[0]
        print rsc_file + ' is existed, no need to re-extract.'
        return rsc_file

    atr = {}
    atr['PROCESSOR'] = 'gamma'
    atr['INSAR_PROCESSOR'] = 'gamma'
    atr['FILE_TYPE'] = os.path.splitext(fname)[1]
    atr['Y_UNIT'] = 'degrees'
    atr['X_UNIT'] = 'degrees'

    par_file = os.path.splitext(fname)[0] + '.utm.dem.par'

    print 'read ' + os.path.basename(par_file)
    par_dict = readfile.read_gamma_par(par_file)

    print 'convert Gamma attribute to ROI_PAC style'
    par_dict = readfile.attribute_gamma2roipac(par_dict)
    atr.update(par_dict)

    ## Write to .rsc file
    rsc_file = fname + '.rsc'
    try:
        atr_orig = readfile.read_roipac_rsc(rsc_file)
    except:
        atr_orig = None
    if atr_orig != atr:
        print 'writing >>> ' + os.path.basename(rsc_file)
        writefile.write_roipac_rsc(atr, rsc_file)
    return rsc_file
Beispiel #7
0
def extract_attribute_dem_radar(fname):
    '''Read/extract attribute for .hgt_sim file from Gamma to ROI_PAC
    Input:
        sim_150911-150922.hgt_sim
        sim_150911-150922.rdc.dem
    Search for:
        sim_150911-150922.diff_par
    Output:
        sim_150911-150922.hgt_sim.rsc
        sim_150911-150922.rdc.dem.rsc
    '''
    atr = {}
    atr['PROCESSOR'] = 'gamma'
    atr['INSAR_PROCESSOR'] = 'gamma'
    atr['FILE_TYPE'] = os.path.splitext(fname)[1]

    # Get basename of file
    fname_base = os.path.splitext(fname)[0]
    for i in range(5):
        fname_base = os.path.splitext(fname_base)[0]

    par_file = fname_base + '.diff_par'
    print 'read ' + os.path.basename(par_file)
    print 'convert Gamma attribute to ROI_PAC style'
    par_dict = readfile.read_gamma_par(par_file)
    par_dict = readfile.attribute_gamma2roipac(par_dict)
    atr.update(par_dict)

    ## Write to .rsc file
    rsc_file = fname + '.rsc'
    try:
        atr_orig = readfile.read_roipac_rsc(rsc_file)
    except:
        atr_orig = None
    if atr_orig != atr:
        print 'writing >>> ' + os.path.basename(rsc_file)
        writefile.write_roipac_rsc(atr, rsc_file)
    return rsc_file
Beispiel #8
0
def main(argv):

    try:    opts, args = getopt.getopt(argv,"h:f:t:p:")
    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
        elif opt == '-t':           filtType = arg
        elif opt == '-p':           par      = arg
  
    ext = os.path.splitext(file)[1]
    outName=file.split('.')[0]+'_'+filtType+ext
    try:    par
    except: par=[]
  
    #print '+++++++++++++++++++++++++++'
    print 'Filter type : '+filtType
    print 'parameters : ' + str(par)
    #print '+++++++++++++++++++++++++++'
    ###############################################
    if ext == '.int' or ext == '.slc':
        a,p,r = readfile.read_complex_float32(file)
        plks=multilook(p,alks,rlks)
        alks=multilook(a,alks,rlks)
    
        r['FILE_LENGTH']=str(dlks.shape[0])
        r['WIDTH']=str(dlks.shape[1])
        r['XMAX']=str(int(r['WIDTH']) - 1)
        r['YMAX']=str(int(r['FILE_LENGTH']) - 1)
        try:
            r['Y_STEP']=str(float(r['Y_STEP'])*alks)
            r['X_STEP']=str(float(r['X_STEP'])*rlks)
        except:
            Geo=0
    
        f = open(outName+'.rsc','w')
        for k in r.keys():
            f.write(k+'    '+r[k]+'\n')
        f.close()   

    elif ext == '.unw' or ext == '.cor' or ext == '.hgt':
        a,p,r = readfile.read_float32(file)
        plks=multilook(p,alks,rlks)
        alks=multilook(a,alks,rlks)
        
        writefile.write_float32(plks,outName)
    
        r['FILE_LENGTH']=str(dlks.shape[0])
        r['WIDTH']=str(dlks.shape[1])
        r['XMAX']=str(int(r['WIDTH']) - 1)
        r['YMAX']=str(int(r['FILE_LENGTH']) - 1)
        
        try:
            r['Y_STEP']=str(float(r['Y_STEP'])*alks)
            r['X_STEP']=str(float(r['X_STEP'])*rlks)
        except:
            Geo=0
    
        f = open(outName+'.rsc','w')
        for k in r.keys():
            f.write(k+'    '+r[k]+'\n')
        f.close()
  
    elif ext == ('.dem'):
        d,r = readfile.read_real_int16(file)
        dlks=multilook(d,alks,rlks)
    
        print 'writing '+outName
        writefile.write_real_int16(dlks,outName)
        
        r['FILE_LENGTH']=str(dlks.shape[0])
        r['WIDTH']=str(dlks.shape[1])
        r['XMAX']=str(int(r['WIDTH']) - 1)
        r['YMAX']=str(int(r['FILE_LENGTH']) - 1)
    
        try:
            r['Y_STEP']=str(float(r['Y_STEP'])*alks)
            r['X_STEP']=str(float(r['X_STEP'])*rlks)
        except:
            Geo=0
    
        f = open(outName+'.rsc','w')
        for k in r.keys():
            f.write(k+'    '+r[k]+'\n')
        f.close()

    elif ext in ['.jpeg','jpg','png']:
  
        import Image
        im = Image.open(file)
    
        width = im.size[0] / int(rlks)
        height = im.size[1] / int(alks)
    
        imlks = im.resize((width, height), Image.NEAREST)
        print 'writing ' + outName
        imlks.save(outName)
    
        try:
            r=readfile.read_roipac_rsc(file+'.rsc')
        except:
            sys.exit(1)
    
        r['FILE_LENGTH']=str(height)
        r['WIDTH']=str(width)
        r['XMAX']=str(int(r['WIDTH']) - 1)
        r['YMAX']=str(int(r['FILE_LENGTH']) - 1)
        try:
            r['Y_STEP']=str(float(r['Y_STEP'])*alks)
            r['X_STEP']=str(float(r['X_STEP'])*rlks)
        except:
            Geo=0
        
        f = open(outName+'.rsc','w')
        for k in r.keys():
            f.write(k+'    '+r[k]+'\n')
        f.close()


    elif ext == ('.h5'):
  
        h5file=h5py.File(file,'r')
        # outName=file.split('.')[0]+'_a'+str(int(alks))+'lks_r'+str(int(rlks))+'lks.h5'
        h5file_lks=h5py.File(outName,'w')
      
        if 'interferograms' in h5file.keys():
            print 'Filtering the interferograms in space'
            gg = h5file_lks.create_group('interferograms')
            igramList=h5file['interferograms'].keys()
            for igram in igramList:
                print igram
                unwSet = h5file['interferograms'][igram].get(igram)
                unw=unwSet[0:unwSet.shape[0],0:unwSet.shape[1]]
                unw=filter(unw,filtType,par)
                group = gg.create_group(igram)
                dset = group.create_dataset(igram, data=unw, compression='gzip')
                for key, value in h5file['interferograms'][igram].attrs.iteritems():
                    group.attrs[key] = value
    
            dset1=h5file['mask'].get('mask')
            mask=dset1[0:dset1.shape[0],0:dset1.shape[1]]
            group=h5file_lks.create_group('mask')
            dset = group.create_dataset('mask', data=mask, compression='gzip')
    
        elif 'timeseries' in h5file.keys():
            print 'Filtering the time-series'
            group = h5file_lks.create_group('timeseries')
            dateList=h5file['timeseries'].keys()
            for d in dateList:
                print d
                dset1 = h5file['timeseries'].get(d)
                data=dset1[0:dset1.shape[0],0:dset1.shape[1]]
                data=filter(data,filtType,par)
                
                dset = group.create_dataset(d, data=data, compression='gzip')      
      
            for key,value in h5file['timeseries'].attrs.iteritems():
                group.attrs[key] = value
      
      
            try:
                dset1 = h5file['mask'].get('mask')
                Mask = dset1[0:dset1.shape[0],0:dset1.shape[1]]
                # Masklks=multilook(Mask,alks,rlks)
                group=h5file_lks.create_group('mask')
                dset = group.create_dataset('mask', data=Mask, compression='gzip')
            except:
                print 'Filterd file does not include the maske'
    
    
        elif 'temporal_coherence' in h5file.keys() or 'velocity' in h5file.keys() or 'mask' in h5file.keys():
            k=h5file.keys()
            print 'filtering the '+ k[0]
         
            group=h5file_lks.create_group(k[0])    
            dset1 = h5file[k[0]].get(k[0])
            data = dset1[0:dset1.shape[0],0:dset1.shape[1]]
            data = filter(data,filtType,par)
            dset = group.create_dataset(k[0], data=data, compression='gzip')
            for key , value in h5file[k[0]].attrs.iteritems():
                group.attrs[key]=value
    
        h5file.close()
        h5file_lks.close()
        print 'writing >>> '+outName
Beispiel #9
0
def radar2glob(x,y,rdrRefFile='radar*.hgt',igramNum=1):
    ## Convert radar coordinates into geo coordinates.
    ##     This function use radar*.hgt or input reference file's 4 corners'
    ##     lat/lon info for a simple 2D linear transformation.
    ##     Accuracy: rough, not accurate
    ##
    ## Usage: lat,lon,lat_res,lon_res = glob2radar(x, y [,rdrRefFile] [,igramNum])
    ##
    ##     x (np.array)     : Array of x/range pixel number
    ##     y (np.array)     : Array of y/azimuth pixel number
    ##     rdrRefFile       : radar coded file (not subseted), optional.
    ##                        radar*.hgt by default, support all PySAR / ROI_PAC format
    ##     igramNum         : used interferogram number, i.e. 1 or 56, optional
    ##
    ##     lat/lon          : Array of geo coordinate
    ##     lat_res/lon_res  : residul/uncertainty of coordinate conversion
    ##
    ## Exmaple: lat,lon,lat_res,lon_res = glob2radar(np.array([202,808,...]), np.array([404,303,...]))
    ##          lat,lon,lat_res,lon_res = glob2radar(np.array([202,808,...]), np.array([404,303,...]),'Mask.h5')
    ##          lat,lon,lat_res,lon_res = glob2radar(np.array([202,808,...]), np.array([404,303,...]),'LoadedData.h5',1)
  
    ### find and read radar coded reference file
    rdrRefFile = check_variable_name(rdrRefFile)  
    rdrRefFile = glob.glob(rdrRefFile)[0]
    ext = os.path.splitext(rdrRefFile)[1]
    if ext == '.h5':
        h5file=h5py.File(rdrRefFile,'r')
        k=h5file.keys()
        if 'interferograms' in k: k[0] = 'interferograms'
        elif 'coherence'    in k: k[0] = 'coherence'
        elif 'timeseries'   in k: k[0] = 'timeseries'
        if   k[0] in ('interferograms','coherence','wrapped'):
            atr = h5file[k[0]][h5file[k[0]].keys()[igramNum-1]].attrs
        elif k[0] in ('dem','velocity','mask','temporal_coherence','rmse','timeseries'):
            atr = h5file[k[0]].attrs
    elif ext in ['.unw','.cor','.int','.hgt','.dem']:
        atr = readfile.read_roipac_rsc(rdrRefFile + '.rsc')
    else: print 'Unrecognized file extention: '+ext; return
  
    LAT_REF1=float(atr['LAT_REF1'])
    LAT_REF2=float(atr['LAT_REF2'])
    LAT_REF3=float(atr['LAT_REF3'])
    LAT_REF4=float(atr['LAT_REF4'])
    LON_REF1=float(atr['LON_REF1'])
    LON_REF2=float(atr['LON_REF2'])
    LON_REF3=float(atr['LON_REF3'])
    LON_REF4=float(atr['LON_REF4'])
    W =      float(atr['WIDTH'])
    L =      float(atr['FILE_LENGTH'])
    if ext == '.h5':  h5file.close()

    try:
        atr['subset_x0']
        print 'WARNING: Cannot use subset file as input! No coordinate converted.'
        return
    except: pass
  
    LAT_REF = np.array([LAT_REF1,LAT_REF2,LAT_REF3,LAT_REF4]).reshape(4,1)
    LON_REF = np.array([LON_REF1,LON_REF2,LON_REF3,LON_REF4]).reshape(4,1)
    X = np.array([1,W,1,W]).reshape(4,1)
    Y = np.array([1,1,L,L]).reshape(4,1)
  
    ### estimate 2D tranformation from Lease Square
    A = np.hstack([X,Y,np.ones((4,1))])
    B = np.hstack([LAT_REF,LON_REF])
    affine_par = np.linalg.lstsq(A,B)[0]
    res = B - np.dot(A,affine_par)
    res_mean = np.mean(np.abs(res),0)
    lat_res = res_mean[0]
    lon_res = res_mean[1]
    print 'Residul - lat: '+str(lat_res)+', lon: '+str(lon_res)
  
    ### calculate geo coordinate of inputs
    N = len(x)
    A = np.hstack([x.reshape(N,1), y.reshape(N,1), np.ones((N,1))])
    lat = np.dot(A, affine_par[:,0])
    lon = np.dot(A, affine_par[:,1])
  
    return lat, lon, lat_res, lon_res
Beispiel #10
0
def glob2radar(lat,lon,rdrRefFile='radar*.hgt',igramNum=1):
    ## Convert geo coordinates into radar coordinates.
    ##     If geomap*.trans file exists, use it for precise conversion;
    ##     If not, use radar*.hgt or input reference file's 4 corners' lat/lon
    ##          info for a simple 2D linear transformation.
    ##
    ## Usage: x,y,x_res,y_res = glob2radar(lat,lon [,rdrRefFile] [,igramNum])
    ##
    ##     lat (np.array) : Array of latitude
    ##     lon (np.array) : Array of longitude
    ##     rdrRefFile     : radar coded file (not subseted), optional.
    ##                      radar*.hgt by default, support all PySAR / ROI_PAC format
    ##     igramNum       : used interferogram number, i.e. 1 or 56, optional
    ##
    ##     x/y            : Array of radar coordinate - range/azimuth
    ##     x_res/y_res    : residul/uncertainty of coordinate conversion
    ##
    ## Exmaple: x,y,x_res,y_res = glob2radar(np.array([31.1,31.2,...]), np.array([130.1,130.2,...]))
    ##          x,y,x_res,y_res = glob2radar(np.array([31.1,31.2,...]), np.array([130.1,130.2,...]),'Mask.h5')
    ##          x,y,x_res,y_res = glob2radar(np.array([31.1,31.2,...]), np.array([130.1,130.2,...]),'LoadedData.h5',1)
  
    ########## Precise conversion using geomap.trans file, if it exists.
    try:
        geomapFile = glob.glob('geomap*.trans')[0]
        atr = readfile.read_roipac_rsc(geomapFile+'.rsc')
        print 'finding precise radar coordinate from '+geomapFile+' file.'
    
        width  = int(atr['WIDTH'])
        row = (lat - float(atr['Y_FIRST'])) / float(atr['Y_STEP']);  row = (row+0.5).astype(int)
        col = (lon - float(atr['X_FIRST'])) / float(atr['X_STEP']);  col = (col+0.5).astype(int)
        row_read = np.max(row)+1
        data = np.fromfile(geomapFile,np.float32,row_read*2*width).reshape(row_read,2*width)
        x = data[row, col];       x = (x+0.5).astype(int)
        y = data[row, col+width]; y = (y+0.5).astype(int)
        x_res = 2
        y_res = 2

    ########## Simple conversion using 2D linear transformation, with 4 corners' lalo info
    except:
        rdrRefFile = check_variable_name(rdrRefFile)
        rdrRefFile = glob.glob(rdrRefFile)[0]
        print 'finding approximate radar coordinate with 2D linear transformation estimation.'
        print '    using four corner lat/lon info from '+rdrRefFile+' file.'
    
        ext = os.path.splitext(rdrRefFile)[1]
        if ext == '.h5':
            h5file=h5py.File(rdrRefFile,'r')
            k=h5file.keys()
            if 'interferograms' in k: k[0] = 'interferograms'
            elif 'coherence'    in k: k[0] = 'coherence'
            elif 'timeseries'   in k: k[0] = 'timeseries'
            if   k[0] in ('interferograms','coherence','wrapped'):
                atr = h5file[k[0]][h5file[k[0]].keys()[igramNum-1]].attrs
            elif k[0] in ('dem','velocity','mask','temporal_coherence','rmse','timeseries'):
                atr = h5file[k[0]].attrs
        elif ext in ['.unw','.cor','.int','.hgt','.dem']:
            atr = readfile.read_roipac_rsc(rdrRefFile + '.rsc')
        else: print 'Unrecognized reference file extention: '+ext; return
    
        LAT_REF1=float(atr['LAT_REF1'])
        LAT_REF2=float(atr['LAT_REF2'])
        LAT_REF3=float(atr['LAT_REF3'])
        LAT_REF4=float(atr['LAT_REF4'])
        LON_REF1=float(atr['LON_REF1'])
        LON_REF2=float(atr['LON_REF2'])
        LON_REF3=float(atr['LON_REF3'])
        LON_REF4=float(atr['LON_REF4'])
        W =      float(atr['WIDTH'])
        L =      float(atr['FILE_LENGTH'])
        if ext == '.h5':  h5file.close()
    
        ## subset radar image has different WIDTH and FILE_LENGTH info
        try:
            atr['subset_x0']
            print 'WARNING: Cannot use subset file as input! No coordinate converted.'
            return
        except: pass

        LAT_REF = np.array([LAT_REF1,LAT_REF2,LAT_REF3,LAT_REF4]).reshape(4,1)
        LON_REF = np.array([LON_REF1,LON_REF2,LON_REF3,LON_REF4]).reshape(4,1)
        X = np.array([1,W,1,W]).reshape(4,1)
        Y = np.array([1,1,L,L]).reshape(4,1)
    
        ### estimate 2D tranformation from Lease Square
        A = np.hstack([LAT_REF,LON_REF,np.ones((4,1))])
        B = np.hstack([X,Y])
        affine_par = np.linalg.lstsq(A,B)[0]
        res = B - np.dot(A,affine_par)
        res_mean = np.mean(np.abs(res),0)
        x_res = (res_mean[0]+0.5).astype(int)
        y_res = (res_mean[1]+0.5).astype(int)
        print 'Residul - x: '+str(x_res)+', y: '+str(y_res)
    
        ### calculate radar coordinate of inputs
        N = len(lat)
        A = np.hstack([lat.reshape(N,1), lon.reshape(N,1), np.ones((N,1))])
        x = np.dot(A, affine_par[:,0]);   x = (x+0.5).astype(int)
        y = np.dot(A, affine_par[:,1]);   y = (y+0.5).astype(int)
  
  
    return x, y, x_res, y_res
Beispiel #11
0
def main(argv):

    try:
        opts, args = getopt.getopt(argv, "h:f:t:p:")
    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
        elif opt == '-t':
            filtType = arg
        elif opt == '-p':
            par = arg

    ext = os.path.splitext(file)[1]
    outName = file.split('.')[0] + '_' + filtType + ext
    try:
        par
    except:
        par = []

    #print '+++++++++++++++++++++++++++'
    print 'Filter type : ' + filtType
    print 'parameters : ' + str(par)
    #print '+++++++++++++++++++++++++++'
    ###############################################
    if ext == '.int' or ext == '.slc':
        a, p, r = readfile.read_complex_float32(file)
        plks = multilook(p, alks, rlks)
        alks = multilook(a, alks, rlks)

        r['FILE_LENGTH'] = str(dlks.shape[0])
        r['WIDTH'] = str(dlks.shape[1])
        r['XMAX'] = str(int(r['WIDTH']) - 1)
        r['YMAX'] = str(int(r['FILE_LENGTH']) - 1)
        try:
            r['Y_STEP'] = str(float(r['Y_STEP']) * alks)
            r['X_STEP'] = str(float(r['X_STEP']) * rlks)
        except:
            Geo = 0

        f = open(outName + '.rsc', 'w')
        for k in r.keys():
            f.write(k + '    ' + r[k] + '\n')
        f.close()

    elif ext == '.unw' or ext == '.cor' or ext == '.hgt':
        a, p, r = readfile.read_float32(file)
        plks = multilook(p, alks, rlks)
        alks = multilook(a, alks, rlks)

        writefile.write_float32(plks, outName)

        r['FILE_LENGTH'] = str(dlks.shape[0])
        r['WIDTH'] = str(dlks.shape[1])
        r['XMAX'] = str(int(r['WIDTH']) - 1)
        r['YMAX'] = str(int(r['FILE_LENGTH']) - 1)

        try:
            r['Y_STEP'] = str(float(r['Y_STEP']) * alks)
            r['X_STEP'] = str(float(r['X_STEP']) * rlks)
        except:
            Geo = 0

        f = open(outName + '.rsc', 'w')
        for k in r.keys():
            f.write(k + '    ' + r[k] + '\n')
        f.close()

    elif ext == ('.dem'):
        d, r = readfile.read_real_int16(file)
        dlks = multilook(d, alks, rlks)

        print 'writing ' + outName
        writefile.write_real_int16(dlks, outName)

        r['FILE_LENGTH'] = str(dlks.shape[0])
        r['WIDTH'] = str(dlks.shape[1])
        r['XMAX'] = str(int(r['WIDTH']) - 1)
        r['YMAX'] = str(int(r['FILE_LENGTH']) - 1)

        try:
            r['Y_STEP'] = str(float(r['Y_STEP']) * alks)
            r['X_STEP'] = str(float(r['X_STEP']) * rlks)
        except:
            Geo = 0

        f = open(outName + '.rsc', 'w')
        for k in r.keys():
            f.write(k + '    ' + r[k] + '\n')
        f.close()

    elif ext in ['.jpeg', 'jpg', 'png']:
        im = Image.open(file)

        width = im.size[0] / int(rlks)
        height = im.size[1] / int(alks)

        imlks = im.resize((width, height), Image.NEAREST)
        print 'writing ' + outName
        imlks.save(outName)

        try:
            r = readfile.read_roipac_rsc(file + '.rsc')
        except:
            sys.exit(1)

        r['FILE_LENGTH'] = str(height)
        r['WIDTH'] = str(width)
        r['XMAX'] = str(int(r['WIDTH']) - 1)
        r['YMAX'] = str(int(r['FILE_LENGTH']) - 1)
        try:
            r['Y_STEP'] = str(float(r['Y_STEP']) * alks)
            r['X_STEP'] = str(float(r['X_STEP']) * rlks)
        except:
            Geo = 0

        f = open(outName + '.rsc', 'w')
        for k in r.keys():
            f.write(k + '    ' + r[k] + '\n')
        f.close()

    elif ext == ('.h5'):

        h5file = h5py.File(file, 'r')
        # outName=file.split('.')[0]+'_a'+str(int(alks))+'lks_r'+str(int(rlks))+'lks.h5'
        h5file_lks = h5py.File(outName, 'w')

        if 'interferograms' in h5file.keys():
            print 'Filtering the interferograms in space'
            gg = h5file_lks.create_group('interferograms')
            igramList = h5file['interferograms'].keys()
            for igram in igramList:
                print igram
                unwSet = h5file['interferograms'][igram].get(igram)
                unw = unwSet[0:unwSet.shape[0], 0:unwSet.shape[1]]
                unw = filter(unw, filtType, par)
                group = gg.create_group(igram)
                dset = group.create_dataset(igram,
                                            data=unw,
                                            compression='gzip')
                for key, value in h5file['interferograms'][
                        igram].attrs.iteritems():
                    group.attrs[key] = value

            dset1 = h5file['mask'].get('mask')
            mask = dset1[0:dset1.shape[0], 0:dset1.shape[1]]
            group = h5file_lks.create_group('mask')
            dset = group.create_dataset('mask', data=mask, compression='gzip')

        elif 'timeseries' in h5file.keys():
            print 'Filtering the time-series'
            group = h5file_lks.create_group('timeseries')
            dateList = h5file['timeseries'].keys()
            for d in dateList:
                print d
                dset1 = h5file['timeseries'].get(d)
                data = dset1[0:dset1.shape[0], 0:dset1.shape[1]]
                data = filter(data, filtType, par)

                dset = group.create_dataset(d, data=data, compression='gzip')

            for key, value in h5file['timeseries'].attrs.iteritems():
                group.attrs[key] = value

            try:
                dset1 = h5file['mask'].get('mask')
                Mask = dset1[0:dset1.shape[0], 0:dset1.shape[1]]
                # Masklks=multilook(Mask,alks,rlks)
                group = h5file_lks.create_group('mask')
                dset = group.create_dataset('mask',
                                            data=Mask,
                                            compression='gzip')
            except:
                print 'Filterd file does not include the maske'

        elif 'temporal_coherence' in h5file.keys(
        ) or 'velocity' in h5file.keys() or 'mask' in h5file.keys():
            k = h5file.keys()
            print 'filtering the ' + k[0]

            group = h5file_lks.create_group(k[0])
            dset1 = h5file[k[0]].get(k[0])
            data = dset1[0:dset1.shape[0], 0:dset1.shape[1]]
            data = filter(data, filtType, par)
            dset = group.create_dataset(k[0], data=data, compression='gzip')
            for key, value in h5file[k[0]].attrs.iteritems():
                group.attrs[key] = value

        h5file.close()
        h5file_lks.close()
        print 'writing >>> ' + outName
Beispiel #12
0
def extract_attribute_interferogram(fname):
    '''Read/extract attributes for PySAR from Gamma .unw, .cor and .int file
    Inputs:
        fname : str, Gamma interferogram filename or path, i.e. /PopoSLT143TsxD/diff_filt_HDR_130118-130129_4rlks.unw
    Output:
        atr : dict, Attributes dictionary
    '''
    file_dir = os.path.dirname(fname)
    file_basename = os.path.basename(fname)

    rsc_file = fname + '.rsc'
    #if os.path.isfile(rsc_file):
    #    return rsc_file

    atr = {}
    atr['PROCESSOR'] = 'gamma'
    atr['INSAR_PROCESSOR'] = 'gamma'
    atr['FILE_TYPE'] = os.path.splitext(fname)[1]

    ## Get info: date12, num of loooks
    try:
        date12 = str(re.findall('\d{8}[-_]\d{8}', file_basename)[0])
    except:
        date12 = str(re.findall('\d{6}[-_]\d{6}', file_basename)[0])
    m_date, s_date = date12.replace('_', '-').split('-')
    atr['DATE12'] = ptime.yymmdd(m_date) + '-' + ptime.yymmdd(s_date)
    lks = os.path.splitext(file_basename.split(date12)[1])[0]

    ## Read .off and .par file
    off_file = file_dir + '/*' + date12 + lks + '.off'
    m_par_file = [
        file_dir + '/*' + m_date + lks + i for i in ['.amp.par', '.ramp.par']
    ]
    s_par_file = [
        file_dir + '/*' + s_date + lks + i for i in ['.amp.par', '.ramp.par']
    ]

    try:
        off_file = ut.get_file_list(off_file)[0]
    except:
        print '\nERROR: Can not find .off file, it supposed to be like: ' + off_file
    try:
        m_par_file = ut.get_file_list(m_par_file)[0]
    except:
        print '\nERROR: Can not find master date .par file, it supposed to be like: ' + m_par_file
    try:
        s_par_file = ut.get_file_list(s_par_file)[0]
    except:
        print '\nERROR: Can not find slave date .par file, it supposed to be like: ' + s_par_file

    #print 'read '+m_par_file
    #print 'read '+off_file
    par_dict = readfile.read_gamma_par(m_par_file)
    off_dict = readfile.read_gamma_par(off_file)

    #print 'convert Gamma attribute to ROI_PAC style'
    atr.update(par_dict)
    atr.update(off_dict)
    atr = readfile.attribute_gamma2roipac(atr)

    ## Perp Baseline Info
    #print 'extract baseline info from %s, %s and %s file' % (m_par_file, s_par_file, off_file)
    atr = get_perp_baseline(m_par_file, s_par_file, off_file, atr)

    ## LAT/LON_REF1/2/3/4
    #print 'extract LAT/LON_REF1/2/3/4 from '+m_par_file
    atr = get_lalo_ref(m_par_file, atr)

    ## Write to .rsc file
    #print 'writing >>> '+rsc_file
    try:
        atr_orig = readfile.read_roipac_rsc(rsc_file)
    except:
        atr_orig = None
    if atr_orig != atr:
        print 'merge %s, %s and %s into %s' % (os.path.basename(m_par_file), os.path.basename(s_par_file),\
                                               os.path.basename(off_file), os.path.basename(rsc_file))
        writefile.write_roipac_rsc(atr, rsc_file)

    return rsc_file
Beispiel #13
0
def main(argv):

    disRas = 'no'
  
    if len(sys.argv)>2:
  
        try:  opts, args = getopt.getopt(argv,"h:f:x:y:l:L:r:a:o:P:")
        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 == '-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 == '-l':   latsub = [float(i) for i in arg.split(':')];  latsub.sort()
            elif opt == '-L':   lonsub = [float(i) for i in arg.split(':')];  lonsub.sort()
            elif opt == '-r':   mli_rg = int(arg)
            elif opt == '-a':   mli_az = int(arg)
            elif opt == '-o':   outname = arg
            elif opt == '-P':   disRas = arg
    
        try:     file
        except:  Usage(); sys.exit(1)
  
    elif len(sys.argv)==2:   file = argv[0]
    else:                    Usage(); sys.exit(1)
        
    ############################################################

    ext = os.path.splitext(file)[1]
    outname='subset_'+file
  
    try:
        parContents = readfile.read_roipac_rsc(file + '.rsc')
        width  = int(parContents['WIDTH'])
        length = int(parContents['FILE_LENGTH'])
    except:
        parContents = readfile.read_par_file(file + '.par')
        width  = int(parContents['range_samples:'])
        length = int(parContents['azimuth_lines:'])
  
    # subset
    try:
        ysub
        if ysub[1] > length: ysub[1]=length;   print 'ysub[1] > length! Set ysub[1]=length='+str(length)
    except:
        ysub=[0,length]
        print 'no subset in y direction'
    try:
        xsub
        if xsub[1] > width:  xsub[1]=width;  print 'xsub[1] > width! Set xsub[1]=width='+str(width)
    except:
        xsub=[0,width]
        print 'no subset in x direction'
  
    if (ysub[1]-ysub[0])*(xsub[1]-xsub[0]) < length*width:
        subsetCmd='subset.py -f '+file+' -x '+str(xsub[0])+':'+str(xsub[1])+' -y '+str(ysub[0])+':'+str(ysub[1])+' -o '+outname
        print subsetCmd
        os.system(subsetCmd)
    else:
        outname = file
        print 'No subset.'
  
    # generate .ras file
    if ext == '.mli':
        try:    mli_rg
        except: mli_rg=1
        try:    mli_az
        except: mli_az=1
        rasCmd='raspwr '+outname+' '+str(xsub[1]-xsub[0])+' 1 0 '+str(mli_rg)+' '+str(mli_az)+' 1. .35 1 - 0'
        print rasCmd
        os.system(rasCmd)
    elif ext in ('.slc','.SLC'):
        try:    mli_rg
        except: mli_rg=1
        try:    mli_az
        except: mli_az=2
        rasCmd='rasSLC '+outname+' '+str(xsub[1]-xsub[0])+' 1 0 '+str(mli_rg)+' '+str(mli_az)+' 1. .35 1 1'
        print rasCmd
        os.system(rasCmd)
    else:
        print 'Not recognized file extension!'
        Usage(); sys.exit(1)

    # display .ras file
    if disRas in ('yes','Yes','Y','y','YES'):
        disCmd = 'display '+outname+'.ras'
        print disCmd
        os.system(disCmd)
Beispiel #14
0
def load_roipac2multi_group_h5(fileType, fileList, hdf5File='unwrapIfgram.h5', pysar_meta_dict=None):
    '''Load multiple ROI_PAC product into (Multi-group, one dataset and one attribute dict per group) HDF5 file.
    Inputs:
        fileType : string, i.e. interferograms, coherence, snaphu_connect_component, etc.
        fileList : list of path, ROI_PAC .unw/.cor/.int/.byt file
        hdf5File : string, file name/path of the multi-group hdf5 PySAR file
        pysar_meta_dict : dict, extra attribute dictionary 
    Outputs:
        hdf5File

    '''
    ext = os.path.splitext(fileList[0])[1]
    print '--------------------------------------------'
    print 'loading ROI_PAC '+ext+' files into '+fileType+' HDF5 file ...'
    print 'number of '+ext+' input: '+str(len(fileList))

    # Check width/length mode of input files
    fileList, mode_width, mode_length = check_file_size(fileList)
    if not fileList:
        return None, None

    # Check conflict with existing hdf5 file
    fileList2 = check_existed_hdf5_file(fileList, hdf5File)
    
    # Open(Create) HDF5 file with r+/w mode based on fileList2
    
    if fileList2 == fileList:
        # Create and open new hdf5 file with w mode
        print 'number of '+ext+' to add: '+str(len(fileList))
        print 'open '+hdf5File+' with w mode'
        h5file = h5py.File(hdf5File, 'w')
    elif fileList2:
        # Open existed hdf5 file with r+ mode
        print 'Continue by adding the following new epochs ...'
        print 'number of '+ext+' to add: '+str(len(fileList))
        print 'open '+hdf5File+' with r+ mode'
        h5file = h5py.File(hdf5File, 'r+')
        fileList = list(fileList2)
    else:
        print 'All input '+ext+' are included, no need to re-load.'
        fileList = None

    # Loop - Writing ROI_PAC files into hdf5 file
    if fileList:
        # Unwraped Interferograms
        if not fileType in h5file.keys():
            gg = h5file.create_group(fileType)     # new hdf5 file
        else:
            gg = h5file[fileType]                  # existing hdf5 file
        
        for file in fileList:
            print 'Adding ' + file
            data, rsc = readfile.read(file)
            
            # Dataset
            group = gg.create_group(os.path.basename(file))
            dset = group.create_dataset(os.path.basename(file), data=data, compression='gzip')
            
            # Attribute - *.unw.rsc
            for key,value in rsc.iteritems():
                group.attrs[key] = value
            # Attribute - *baseline.rsc
            d1, d2 = rsc['DATE12'].split('-')
            baseline_file = os.path.dirname(file)+'/'+d1+'_'+d2+'_baseline.rsc'
            baseline_rsc = readfile.read_roipac_rsc(baseline_file)
            for key,value in baseline_rsc.iteritems():
                group.attrs[key] = value
            # Attribute - PySAR
            if pysar_meta_dict:
                group.attrs['PROJECT_NAME'] = pysar_meta_dict['project_name']
        
        # End of Loop
        h5file.close()
        print 'finished writing to '+hdf5File

    return hdf5File, fileList
Beispiel #15
0
def main(argv):

    disRas = 'no'

    if len(sys.argv) > 2:

        try:
            opts, args = getopt.getopt(argv, "h:f:x:y:l:L:r:a:o:P:")
        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 == '-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 == '-l':
                latsub = [float(i) for i in arg.split(':')]
                latsub.sort()
            elif opt == '-L':
                lonsub = [float(i) for i in arg.split(':')]
                lonsub.sort()
            elif opt == '-r':
                mli_rg = int(arg)
            elif opt == '-a':
                mli_az = int(arg)
            elif opt == '-o':
                outname = arg
            elif opt == '-P':
                disRas = arg

        try:
            file
        except:
            usage()
            sys.exit(1)

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

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

    ext = os.path.splitext(file)[1]
    outname = 'subset_' + file

    try:
        parContents = readfile.read_roipac_rsc(file + '.rsc')
        width = int(parContents['WIDTH'])
        length = int(parContents['FILE_LENGTH'])
    except:
        parContents = readfile.read_par_file(file + '.par')
        width = int(parContents['range_samples:'])
        length = int(parContents['azimuth_lines:'])

    # subset
    try:
        ysub
        if ysub[1] > length:
            ysub[1] = length
            print 'ysub[1] > length! Set ysub[1]=length=' + str(length)
    except:
        ysub = [0, length]
        print 'no subset in y direction'
    try:
        xsub
        if xsub[1] > width:
            xsub[1] = width
            print 'xsub[1] > width! Set xsub[1]=width=' + str(width)
    except:
        xsub = [0, width]
        print 'no subset in x direction'

    if (ysub[1] - ysub[0]) * (xsub[1] - xsub[0]) < length * width:
        subsetCmd = 'subset.py -f ' + file + ' -x ' + str(xsub[0]) + ':' + str(
            xsub[1]) + ' -y ' + str(ysub[0]) + ':' + str(
                ysub[1]) + ' -o ' + outname
        print subsetCmd
        os.system(subsetCmd)
    else:
        outname = file
        print 'No subset.'

    # generate .ras file
    if ext == '.mli':
        try:
            mli_rg
        except:
            mli_rg = 1
        try:
            mli_az
        except:
            mli_az = 1
        rasCmd = 'raspwr ' + outname + ' ' + str(
            xsub[1] - xsub[0]) + ' 1 0 ' + str(mli_rg) + ' ' + str(
                mli_az) + ' 1. .35 1 - 0'
        print rasCmd
        os.system(rasCmd)
    elif ext in ('.slc', '.SLC'):
        try:
            mli_rg
        except:
            mli_rg = 1
        try:
            mli_az
        except:
            mli_az = 2
        rasCmd = 'rasSLC ' + outname + ' ' + str(
            xsub[1] - xsub[0]) + ' 1 0 ' + str(mli_rg) + ' ' + str(
                mli_az) + ' 1. .35 1 1'
        print rasCmd
        os.system(rasCmd)
    else:
        print 'Not recognized file extension!'
        usage()
        sys.exit(1)

    # display .ras file
    if disRas in ('yes', 'Yes', 'Y', 'y', 'YES'):
        disCmd = 'display ' + outname + '.ras'
        print disCmd
        os.system(disCmd)
Beispiel #16
0
def main(argv):
    try:
        templateFile = argv[1]
    except:
        Usage()
        sys.exit(1)

    from pysar._pysar_utilities import check_variable_name

    templateContents = readfile.read_template(templateFile)
    projectName = os.path.basename(templateFile).partition(".")[0]

    try:
        processProjectDir = argv[2]
        tssarProjectDir = argv[3]
    except:
        if os.getenv("PARENTDIR"):
            processProjectDir = os.getenv("SCRATCHDIR") + "/" + projectName + "/PROCESS"
            tssarProjectDir = os.getenv("SCRATCHDIR") + "/" + projectName + "/TSSAR"
        else:
            processProjectDir = os.getenv("PROCESSDIR") + "/" + projectName
            tssarProjectDir = os.getenv("TSSARDIR") + "/" + projectName
    print "\n*************** Loading Data into PySAR ****************"
    print "PROCESS directory: " + processProjectDir
    print "TSSAR   directory: " + tssarProjectDir
    if not os.path.isdir(tssarProjectDir):
        os.mkdir(tssarProjectDir)

    ########### Use defaults if paths not given in template file #########
    import h5py
    import numpy as np

    optionName = {}
    optionName["interferograms"] = "pysar.inputFiles"
    optionName["coherence"] = "pysar.corFiles"
    optionName["wrapped"] = "pysar.wrappedFiles"
    optionName["geomap"] = "pysar.geomap"
    optionName["demGeo"] = "pysar.dem.geoCoord"
    optionName["demRdr"] = "pysar.dem.radarCoord"

    try:
        igramPath = templateContents["pysar.inputFiles"]
        igramPath = check_variable_name(igramPath)
    except:
        igramPath = processProjectDir + "/DONE/IFGRAM*/filt_*.unw"
    print "Path pattern for unwrapped interferogram: " + igramPath
    # except: igramPath = os.getenv('SCRATCHDIR') + '/' + projectName + '/PROCESS/DONE/IFGRAM*/filt_*.unw'

    try:
        corPath = templateContents["pysar.corFiles"]
        corPath = check_variable_name(corPath)
    except:
        corPath = processProjectDir + "/DONE/IFGRAM*/filt_*rlks.cor"
    print "Path pattern for coherence:               " + corPath

    try:
        wrapPath = templateContents["pysar.wrappedFiles"]
        wrapPath = check_variable_name(wrapPath)
    except:
        wrapPath = processProjectDir + "/DONE/IFGRAM*/filt_*rlks.int"
    print "Path pattern for wrapped interferogram:   " + wrapPath

    # try:    demRdrPath = templateContents['pysar.dem.radarCoord'];  demRdrPath = check_variable_name(demRdrPath)
    # except:
    #  demRdrList=glob.glob(demRdrPath)

    ###########################################################################
    ######################### Unwrapped Interferograms ########################

    try:
        if os.path.isfile(tssarProjectDir + "/LoadedData.h5"):
            print "\nLoadedData.h5" + "  already exists."
            sys.exit(1)
        igramList = glob.glob(igramPath)
        igramList = sorted(igramList)
        k = "interferograms"
        check_number(k, optionName[k], igramList)  # number check
        print "loading interferograms ..."
        igramList, mode_width, mode_length = check_size(k, igramList)  # size check
        igramList = sorted(igramList)

        h5file = tssarProjectDir + "/LoadedData.h5"
        f = h5py.File(h5file, "w")
        gg = f.create_group("interferograms")
        MaskZero = np.ones([int(mode_length), int(mode_width)])
        for igram in igramList:
            if not os.path.basename(igram) in f:
                print "Adding " + igram
                group = gg.create_group(os.path.basename(igram))
                amp, unw, unwrsc = readfile.read_float32(igram)

                MaskZero *= amp

                dset = group.create_dataset(os.path.basename(igram), data=unw, compression="gzip")
                for key, value in unwrsc.iteritems():
                    group.attrs[key] = value

                d1, d2 = unwrsc["DATE12"].split("-")
                baseline_file = os.path.dirname(igram) + "/" + d1 + "_" + d2 + "_baseline.rsc"
                baseline = readfile.read_roipac_rsc(baseline_file)
                for key, value in baseline.iteritems():
                    group.attrs[key] = value
                group.attrs["PROJECT_NAME"] = projectName
                group.attrs["UNIT"] = "radian"
            else:
                print os.path.basename(h5file) + " already contains " + os.path.basename(igram)

        Mask = np.ones([int(mode_length), int(mode_width)])
        Mask[MaskZero == 0] = 0
        # gm = f.create_group('mask')
        # dset = gm.create_dataset('mask', data=Mask, compression='gzip')
        f.close()

        ############## Mask file ###############
        print "writing to Mask.h5\n"
        # Mask=np.ones([int(mode_length),int(mode_width)])
        # Mask[MaskZero==0]=0
        h5file = tssarProjectDir + "/Mask.h5"
        h5mask = h5py.File(h5file, "w")
        group = h5mask.create_group("mask")
        dset = group.create_dataset(os.path.basename("mask"), data=Mask, compression="gzip")
        for key, value in unwrsc.iteritems():
            group.attrs[key] = value
        h5mask.close()

    except:
        print "No unwrapped interferogram is loaded.\n"

    ########################################################################
    ############################# Coherence ################################
    try:
        if os.path.isfile(tssarProjectDir + "/Coherence.h5"):
            print "\nCoherence.h5" + "  already exists."
            sys.exit(1)
        corList = glob.glob(corPath)
        corList = sorted(corList)
        k = "coherence"
        check_number(k, optionName[k], corList)  # number check
        print "loading coherence files ..."
        corList, mode_width, mode_length = check_size(k, corList)  # size check
        corList = sorted(corList)

        h5file = tssarProjectDir + "/Coherence.h5"
        fcor = h5py.File(h5file, "w")
        gg = fcor.create_group("coherence")
        meanCoherence = np.zeros([int(mode_length), int(mode_width)])
        for cor in corList:
            if not os.path.basename(cor) in fcor:
                print "Adding " + cor
                group = gg.create_group(os.path.basename(cor))
                amp, unw, unwrsc = readfile.read_float32(cor)

                meanCoherence += unw
                dset = group.create_dataset(os.path.basename(cor), data=unw, compression="gzip")
                for key, value in unwrsc.iteritems():
                    group.attrs[key] = value

                d1, d2 = unwrsc["DATE12"].split("-")
                baseline_file = os.path.dirname(cor) + "/" + d1 + "_" + d2 + "_baseline.rsc"
                baseline = readfile.read_roipac_rsc(baseline_file)
                for key, value in baseline.iteritems():
                    group.attrs[key] = value
                group.attrs["PROJECT_NAME"] = projectName
                group.attrs["UNIT"] = "1"
            else:
                print os.path.basename(h5file) + " already contains " + os.path.basename(cor)
        # fcor.close()

        ########### mean coherence file ###############
        meanCoherence = meanCoherence / (len(corList))
        print "writing meanCoherence group to the coherence h5 file"
        gc = fcor.create_group("meanCoherence")
        dset = gc.create_dataset("meanCoherence", data=meanCoherence, compression="gzip")

        print "writing average_spatial_coherence.h5\n"
        h5file_CorMean = tssarProjectDir + "/average_spatial_coherence.h5"
        fcor_mean = h5py.File(h5file_CorMean, "w")
        group = fcor_mean.create_group("mask")
        dset = group.create_dataset(os.path.basename("mask"), data=meanCoherence, compression="gzip")
        for key, value in unwrsc.iteritems():
            group.attrs[key] = value
        fcor_mean.close()

        fcor.close()

    except:
        print "No correlation file is loaded.\n"

    ##############################################################################
    ########################## Wrapped Interferograms ############################

    try:
        if os.path.isfile(tssarProjectDir + "/Wrapped.h5"):
            print "\nWrapped.h5" + "  already exists."
            sys.exit(1)
        wrapList = glob.glob(wrapPath)
        wrapList = sorted(wrapList)
        k = "wrapped"
        check_number(k, optionName[k], wrapList)  # number check
        print "loading wrapped phase ..."
        wrapList, mode_width, mode_length = check_size(k, wrapList)  # size check
        wrapList = sorted(wrapList)

        h5file = tssarProjectDir + "/Wrapped.h5"
        fw = h5py.File(h5file, "w")
        gg = fw.create_group("wrapped")
        for wrap in wrapList:
            if not os.path.basename(wrap) in fw:
                print "Adding " + wrap
                group = gg.create_group(os.path.basename(wrap))
                amp, unw, unwrsc = readfile.read_complex_float32(wrap)

                dset = group.create_dataset(os.path.basename(wrap), data=unw, compression="gzip")
                for key, value in unwrsc.iteritems():
                    group.attrs[key] = value

                d1, d2 = unwrsc["DATE12"].split("-")
                baseline_file = os.path.dirname(wrap) + "/" + d1 + "_" + d2 + "_baseline.rsc"
                baseline = readfile.read_roipac_rsc(baseline_file)
                for key, value in baseline.iteritems():
                    group.attrs[key] = value
                group.attrs["PROJECT_NAME"] = projectName
                group.attrs["UNIT"] = "radian"
            else:
                print os.path.basename(h5file) + " already contains " + os.path.basename(wrap)
        fw.close()
        print "Writed " + str(len(wrapList)) + " wrapped interferograms to " + h5file + "\n"

    except:
        print "No wrapped interferogram is loaded.\n"

    ##############################################################################
    ################################# geomap.trans ###############################

    try:
        geomapPath = tssarProjectDir + "/geomap*.trans"
        geomapList = glob.glob(geomapPath)
        if len(geomapList) > 0:
            print "\ngeomap*.trans" + "  already exists."
            sys.exit(1)

        geomapPath = templateContents["pysar.geomap"]
        geomapPath = check_variable_name(geomapPath)
        geomapList = glob.glob(geomapPath)

        cpCmd = "cp " + geomapList[0] + " " + tssarProjectDir
        print cpCmd
        os.system(cpCmd)
        cpCmd = "cp " + geomapList[0] + ".rsc " + tssarProjectDir
        print cpCmd + "\n"
        os.system(cpCmd)
    except:
        # print "*********************************"
        print "no geomap file is loaded.\n"
        # print "*********************************\n"

    ##############################################################################
    ##################################  DEM  #####################################

    try:
        demRdrPath = tssarProjectDir + "/radar*.hgt"
        demRdrList = glob.glob(demRdrPath)
        if len(demRdrList) > 0:
            print "\nradar*.hgt" + "  already exists."
            sys.exit(1)

        demRdrPath = templateContents["pysar.dem.radarCoord"]
        demRdrPath = check_variable_name(demRdrPath)
        demRdrList = glob.glob(demRdrPath)

        cpCmd = "cp " + demRdrList[0] + " " + tssarProjectDir
        print cpCmd
        os.system(cpCmd)
        cpCmd = "cp " + demRdrList[0] + ".rsc " + tssarProjectDir
        print cpCmd + "\n"
        os.system(cpCmd)
    except:
        # print "*********************************"
        print "no DEM (radar coordinate) file is loaded.\n"
        # print "*********************************"

    try:
        demGeoPath = tssarProjectDir + "/*.dem"
        demGeoList = glob.glob(demGeoPath)
        if len(demGeoList) > 0:
            print "\n*.dem" + "  already exists."
            sys.exit(1)

        demGeoPath = templateContents["pysar.dem.geoCoord"]
        demGeoPath = check_variable_name(demGeoPath)
        demGeoList = glob.glob(demGeoPath)

        cpCmd = "cp " + demGeoList[0] + " " + tssarProjectDir
        print cpCmd
        os.system(cpCmd)
        cpCmd = "cp " + demGeoList[0] + ".rsc " + tssarProjectDir
        print cpCmd + "\n"
        os.system(cpCmd)
    except:
        # print "*********************************"
        print "no DEM (geo coordinate) file is loaded.\n"