Esempio n. 1
0
def geocode_data_roipac(data, geomapFile, outname):

    print 'writing to roi_pac unw file format'
    writefile.write_float32(data, outname)
    f = open(outname + '.rsc', 'w')
    f.write('FILE_LENGTH       ' + str(data.shape[0]) + '\n')
    f.write('WIDTH             ' + str(data.shape[1]) + '\n')
    f.close()

    geoCmd = 'geocode.pl ' + geomapFile + ' ' + outname + ' geo_' + outname
    print geoCmd
    os.system(geoCmd)

    print 'reading geocoded file...'
    amp, unw, unwrsc = readfile.read_float32('geo_' + outname)

    rmCmd = 'rm ' + outname
    os.system(rmCmd)
    print rmCmd
    rmCmd = 'rm ' + outname + '.rsc'
    os.system(rmCmd)
    print rmCmd
    rmCmd = 'rm geo_' + outname
    os.system(rmCmd)
    print rmCmd
    rmCmd = 'rm geo_' + outname + '.rsc'
    os.system(rmCmd)
    print rmCmd

    return amp, unw, unwrsc
Esempio n. 2
0
def main(argv):
    try:
        file = argv[0]
    except:
        usage()
        sys.exit(1)

    print '\n*************** Save to GRD file for GMT ****************'
    print 'Input file: ' + file
    ext = os.path.splitext(file)[1]

    ########## PySAR HDF5 Files ################
    if ext == '.h5':
        h5 = h5py.File(file, 'r')
        k = h5.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 = h5[k[0]][h5[k[0]].keys()[0]].attrs
        elif k[0] in ('dem', 'velocity', 'mask', 'temporal_coherence', 'rmse',
                      'timeseries'):
            atr = h5[k[0]].attrs

        if 'timeseries' in k:
            try:
                d = sys.argv[2]
            except:
                print 'No input date... continue to convert the last date of timeseries'
                dateList = h5['timeseries'].keys()
                d = dateList[-1]
            print 'reading ' + d + ' ... '
            dset = h5['timeseries'].get(d)

        elif k[0] in ['interferograms', 'coherence', 'wrapped']:
            print 'interferograms is not supported currently.'
            sys.exit(1)

        else:
            dset = h5[k[0]].get(k[0])

        z = dset[0:dset.shape[0], 0:dset.shape[1]]

    ########## ROI_PAC Files ##################
    elif ext in ['.unw', '.cor', '.hgt', '.dem']:
        if ext == '.dem': z, atr = readfile.read_real_int16(file)
        if ext in ['.unw', '.cor', '.hgt']:
            a, z, atr = readfile.read_float32(file)

    else:
        print 'Unsupported file extension: ' + ext
        sys.exit(1)

    lats, lons = get_geo_lat_lon(atr)

    #fname='velocity.grd'
    outName = os.path.basename(file).replace(ext, '') + '.grd'
    print 'Output (GMT grd file): ' + outName
    _gmt.write_gmt_simple(lons, flipud(lats), flipud(z), outName, title='default', name='velocity',\
                          scale=1.0, offset=0, units='meters')
Esempio n. 3
0
def main(argv):
    try: file = argv[0]
    except: Usage(); sys.exit(1)
  
    print '\n*************** Save to GRD file for GMT ****************'
    print 'Input file: '+file
    ext = os.path.splitext(file)[1]
  
    ########## PySAR HDF5 Files ################
    if ext == '.h5':
        h5=h5py.File(file,'r')
        k=h5.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  = h5[k[0]][h5[k[0]].keys()[0]].attrs
        elif k[0] in ('dem','velocity','mask','temporal_coherence','rmse','timeseries'):
            atr  = h5[k[0]].attrs
   
        if 'timeseries' in k:
            try:
                d=sys.argv[2]
            except:
                print 'No input date... continue to convert the last date of timeseries'
                dateList=h5['timeseries'].keys()
                d=dateList[-1]
            print 'reading '+d + ' ... '
            dset=h5['timeseries'].get(d)
   
        elif k[0] in ['interferograms','coherence','wrapped']:
           print 'interferograms is not supported currently.'; sys.exit(1)
   
        else:   dset=h5[k[0]].get(k[0])
   
        z=dset[0:dset.shape[0],0:dset.shape[1]]
  
    ########## ROI_PAC Files ##################
    elif ext in ['.unw','.cor','.hgt','.dem']:
        import pysar._readfile as readfile
        if ext == '.dem' :                    z,atr = readfile.read_real_int16(file)
        if ext in ['.unw','.cor','.hgt']:   a,z,atr = readfile.read_float32(file)
  
    else: print 'Unsupported file extension: '+ext;  sys.exit(1)
  
    lats,lons=get_geo_lat_lon(atr)
  
    #fname='velocity.grd'
    outName=os.path.basename(file).replace(ext,'')+'.grd';  print 'Output (GMT grd file): '+outName
    _gmt.write_gmt_simple(lons, flipud(lats), flipud(z), outName, title='default', name='velocity',\
                          scale=1.0, offset=0, units='meters')
Esempio n. 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
Esempio n. 5
0
def geomap4subset_radar_file(radar_atr, geomap_file):
    ''' Add offset value to geomap file if input radar file has been subsetted.'''
    if 'subset_x0' in radar_atr.keys():
        x0 = float(radar_atr['subset_x0'])
        y0 = float(radar_atr['subset_y0'])
        print '\nInput radar coord file has been subsetted.\n    creating temporary geomap file for it...'

        rg, az, rsc = readfile.read_float32(geomap_file)
        rg = rg - x0
        az = az - y0

        geomap_file = 'temp_' + geomap_file
        print '    writing >>> ' + geomap_file + '\n'
        writefile.write_float32(rg, az, geomap_file)
        f = open(geomap_file + '.rsc', 'w')
        for key in rsc.keys():
            f.write(key + '    ' + rsc[key] + '\n')
        f.close()
    return geomap_file
Esempio n. 6
0
def geocode_one(data,geomapFile,outname):

   print 'writing to roi_pac unw file format'
   writefile.write_float32(data,outname)
   f = open(outname+'.rsc','w')
   f.write('FILE_LENGTH       '+str(data.shape[0])+'\n')
   f.write('WIDTH             '+str(data.shape[1])+'\n')
   f.close()

   geoCmd='geocode.pl '+geomapFile+' '+outname+' geo_'+outname
   print geoCmd
   os.system(geoCmd)

   print 'reading geocoded file...'
   amp,unw,unwrsc = readfile.read_float32('geo_'+outname)

   rmCmd='rm '+outname;                 os.system(rmCmd);       print rmCmd
   rmCmd='rm '+outname+'.rsc';          os.system(rmCmd);       print rmCmd
   rmCmd='rm geo_'+outname;             os.system(rmCmd);       print rmCmd
   rmCmd='rm geo_'+outname+'.rsc';      os.system(rmCmd);       print rmCmd

   return amp, unw, unwrsc
Esempio n. 7
0
def main(argv):

    method    = 'triangular_consistency'    ## or 'bonding_point'
    ramp_type = 'plane'
    save_rampCor = 'yes'
    plot_bonding_points = 'yes'
  
    ##### Check Inputs
    if len(sys.argv)>2:
        try: opts, args = getopt.getopt(argv,'h:f:m:x:y:o:t:',['ramp=','no-ramp-save'])
        except getopt.GetoptError:  print 'Error while getting args';  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 '-x':    x = [int(i) for i in arg.split(',')];    method = 'bonding_point'
            elif opt in '-y':    y = [int(i) for i in arg.split(',')];    method = 'bonding_point'
            elif opt in '-t':    templateFile = arg
            elif opt in '--ramp'         :  ramp_type    = arg.lower()
            elif opt in '--no-ramp-save' :  save_rampCor = 'no'
  
    elif len(sys.argv)==2:
        if argv[0] in ['-h','--help']:    Usage();  sys.exit()
        elif os.path.isfile(argv[0]):     File = argv[0];  maskFile = argv[1]
        else:    print 'Input file does not existed: '+argv[0];  sys.exit(1)
  
    else:  Usage(); sys.exit(1)
  
    ##### Check template file
    try:
        templateFile
        templateContents = readfile.read_template(templateFile)
    except: pass
  
    try:
        yx = [int(i) for i in templateContents['pysar.unwrapError.yx'].split(',')]
        x = yx[1::2]
        y = yx[0::2]
        method = 'bonding_point'
    except: pass

    ##### Read Mask File 
    ## Priority:
    ## Input mask file > pysar.mask.file > existed Modified_Mask.h5 > existed Mask.h5
    try:       maskFile
    except:
        try:    maskFile = templateContents['pysar.mask.file']
        except:
            if   os.path.isfile('Modified_Mask.h5'):  maskFile = 'Modified_Mask.h5'
            elif os.path.isfile('Mask.h5'):           maskFile = 'Mask.h5'
            else: print 'No mask found!'; sys.exit(1)
    try:    Mask,Matr = readfile.read(maskFile);   print 'mask: '+maskFile
    except: print 'Can not open mask file: '+maskFile; sys.exit(1)
  
    ##### Output file name
    ext = os.path.splitext(File)[1]
    try:    outName
    except: outName = File.split('.')[0]+'_unwCor'+ext
  
    print '\n**************** Unwrapping Error Correction ******************'

    ####################  Triangular Consistency (Phase Closure)  ####################
    if method == 'triangular_consistency':
        print 'Phase unwrapping error correction using Triangular Consistency / Phase Closure'
  
        h5file=h5py.File(File)
        ifgramList = h5file['interferograms'].keys()
        sx = int(h5file['interferograms'][ifgramList[0]].attrs['WIDTH'])
        sy = int(h5file['interferograms'][ifgramList[0]].attrs['FILE_LENGTH'])
        curls,Triangles,C=ut.get_triangles(h5file)
        A,B = ut.design_matrix(h5file)   
        ligram,lv=np.shape(B)
        lcurls=np.shape(curls)[0]
        print 'Number of all triangles: '+  str(lcurls)
        print 'Number of interferograms: '+ str(ligram)
        #print curls
  
        curlfile='curls.h5'
        if not os.path.isfile(curlfile):
            ut.generate_curls(curlfile,h5file,Triangles,curls)
         
        thr=0.50
        curls=np.array(curls);   n1=curls[:,0];   n2=curls[:,1];   n3=curls[:,2]
  
        numPixels=sy*sx
        print 'reading interferograms...'   
        data = np.zeros((ligram,numPixels),np.float32)
        for ni in range(ligram):
            dset=h5file['interferograms'][ifgramList[ni]].get(ifgramList[ni])
            d = dset[0:dset.shape[0],0:dset.shape[1]]
            data[ni] = d.flatten(1)   
  
        print np.shape(data)
        print 'reading curls ...' 
        h5curl=h5py.File(curlfile)
        curlList=h5curl['interferograms'].keys()
        curlData = np.zeros((lcurls,numPixels),np.float32)
        for ni in range(lcurls):
            dset=h5curl['interferograms'][curlList[ni]].get(curlList[ni])
            d = dset[0:dset.shape[0],0:dset.shape[1]]
            curlData[ni] = d.flatten(1)
        pi=np.pi
        EstUnwrap=np.zeros((ligram,numPixels),np.float32)
  
        #try:
        #    maskFile=argv[1]
        #    h5Mask=h5py.File(maskFile)
        #    dset = h5Mask['mask'].get('mask')
        #    Mask=dset[0:dset.shape[0],0:dset.shape[1]]
        #except:
        #    dset = h5file['mask'].get('mask')
        #    Mask=dset[0:dset.shape[0],0:dset.shape[1]]
        
        Mask=Mask.flatten(1)

        from scipy.linalg import pinv as pinv
        for ni in range(numPixels):
            #dU = np.zeros([ligram,1])
            #print np.shape(dU)
            #print np.shape(data[:,ni])
  
            if Mask[ni]==1:
                dU = data[:,ni]
                #nan_ndx = dataPoint == 0.
                unwCurl = np.array(curlData[:,ni])
                #print unwCurl
  
                ind  = np.abs(unwCurl)>=thr;      N1 =n1[ind];      N2 =n2[ind];      N3 =n3[ind]
                indC = np.abs(unwCurl)< thr;      Nc1=n1[indC];     Nc2=n2[indC];     Nc3=n3[indC]
  
                N =np.hstack([N1, N2, N3]);       UniN =np.unique(N)
                Nc=np.hstack([Nc1,Nc2,Nc3]);      UniNc=np.unique(Nc)
  
                inter=list(set(UniNc) & set(UniN)) # intersetion
                UniNc= list(UniNc)
                for x in inter:
                    UniNc.remove(x)
  
                D=np.zeros([len(UniNc),ligram])
                for i in range(len(UniNc)):
                    D[i,UniNc[i]]=1
  
                AAA=np.vstack([-2*pi*C,D])
                #AAA1=np.hstack([AAA,np.zeros([AAA.shape[0],lv])])
                #AAA2=np.hstack([-2*pi*np.eye(ligram),B]) 
                #AAAA=np.vstack([AAA1,AAA2])
                AAAA=np.vstack([AAA,0.25*np.eye(ligram)])
  
                #print '************************'
                #print np.linalg.matrix_rank(C)
                #print np.linalg.matrix_rank(AAA) 
                #print np.linalg.matrix_rank(AAAA)
                #print '************************'
  
                #LLL=list(np.dot(C,dU)) + list(np.zeros(np.shape(UniNc)[0]))# + list(dU)
                #ind=np.isnan(AAA)
                #M1=pinv(AAA)      
                #M=np.dot(M1,LLL)
                #EstUnwrap[:,ni]=np.round(M[0:ligram])*2.0*np.pi
  
                ##########
                # with Tikhonov regularization:
                AAAA=np.vstack([AAA,0.25*np.eye(ligram)])
                LLL=list(np.dot(C,dU)) + list(np.zeros(np.shape(UniNc)[0])) + list(np.zeros(ligram))
                ind=np.isnan(AAAA)
                M1=pinv(AAAA)
                M=np.dot(M1,LLL)
                EstUnwrap[:,ni]=np.round(M[0:ligram])*2.0*np.pi
                #print M[0:ligram]
                #print np.round(M[0:ligram])
  
            else:
                EstUnwrap[:,ni]=np.zeros([ligram])
                if not np.remainder(ni,10000): print 'Processing point: %7d of %7d ' % (ni,numPixels)

        ##### Output
        dataCor = data+EstUnwrap
        unwCorFile=File.replace('.h5','')+'_unwCor.h5';  print 'writing >>> '+unwCorFile
        h5unwCor=h5py.File(unwCorFile,'w') 
        gg = h5unwCor.create_group('interferograms') 
        for i in range(ligram):
            group = gg.create_group(ifgramList[i])
            dset = group.create_dataset(ifgramList[i], data=np.reshape(dataCor[i,:],[sx,sy]).T, compression='gzip')
            for key, value in h5file['interferograms'][ifgramList[i]].attrs.iteritems():
                group.attrs[key] = value
  
        try:
            MASK=h5file['mask'].get('mask')
            gm = h5unwCor.create_group('mask')
            dset = gm.create_dataset('mask', data=MASK, compression='gzip')
        except: pass
  
        h5unwCor.close()
        h5file.close()
        h5curl.close() 


    ####################  Bonding Points (Spatial Continuity)  ####################
    elif method == 'bonding_point':
        print 'Phase unwrapping error correction using Bonding Points / Spatial Continuity'
  
        ##### Read Bridge Points Info
        try:
            x
            y
            if len(x) != len(y) or np.mod(len(x),2) != 0:
                print 'Wrong number of bridge points input: '+str(len(x))+' for x, '+str(len(y))+' for y'
                Usage();  sys.exit(1)
        except: print 'Error in reading bridge points info!';  Usage();  sys.exit(1)
        for i in range(0,len(x)):
            if Mask[y[i],x[i]] == 0:
                print '\nERROR: Connecting point ('+str(y[i])+','+str(x[i])+') is out of masked area! Select them again!\n'
                sys.exit(1)
  
        print 'Number of bonding point pairs: '+str(len(x)/2)
        print 'Bonding points coordinates:\nx: '+str(x)+'\ny: '+str(y)
  
        ## Plot Connecting Pair of Points
        if plot_bonding_points == 'yes':
            point_yx = ''
            line_yx  = ''
            n_bridge = len(x)/2
            for i in range(n_bridge):
                pair_yx = str(y[2*i])+','+str(x[2*i])+','+str(y[2*i+1])+','+str(x[2*i+1])
                if not i == n_bridge-1:
                    point_yx += pair_yx+','
                    line_yx  += pair_yx+';'
                else:
                    point_yx += pair_yx
                    line_yx  += pair_yx

            try:
                plot_cmd = 'view.py --point-yx="'+point_yx+'" --line-yx="'+line_yx+\
                           '" --nodisplay -o bonding_points.png -f '+maskFile
                print plot_cmd
                os.system(plot_cmd)
            except: pass


        ##### Ramp Info
        ramp_mask = Mask==1
        print 'estimate phase ramp during the correction'
        print 'ramp type: '+ramp_type
        if save_rampCor == 'yes':
            outName_ramp = os.path.basename(outName).split(ext)[0]+'_'+ramp_type+ext
  
        ########## PySAR ##########
        if ext == '.h5':
            ##### Read
            try:     h5file=h5py.File(File,'r')
            except:  print 'ERROR: Cannot open input file: '+File; sys.exit(1)
            k=h5file.keys()
            if 'interferograms' in k: k[0] = 'interferograms';  print 'Input file is '+k[0]
            else: print 'Input file - '+File+' - is not interferograms.';  Usage();  sys.exit(1)
            igramList = h5file[k[0]].keys()
            igramList = sorted(igramList)
  
            #### Write
            h5out = h5py.File(outName,'w')
            gg = h5out.create_group(k[0])
            print 'writing >>> '+outName
  
            if save_rampCor == 'yes':
                h5out_ramp = h5py.File(outName_ramp,'w')
                gg_ramp = h5out_ramp.create_group(k[0])
                print 'writing >>> '+outName_ramp
  
            ##### Loop
            print 'Number of interferograms: '+str(len(igramList))
            for igram in igramList:
                print igram
                data = h5file[k[0]][igram].get(igram)[:]
  
                data_ramp,ramp = rm.remove_data_surface(data,ramp_mask,ramp_type)
                #ramp = data_ramp - data
                data_rampCor = phase_bonding(data_ramp,Mask,x,y)
                dataCor = data_rampCor - ramp
  
                group = gg.create_group(igram)
                dset = group.create_dataset(igram, data=dataCor, compression='gzip')
                for key, value in h5file[k[0]][igram].attrs.iteritems():
                    group.attrs[key]=value
  
                if save_rampCor == 'yes':
                    group_ramp = gg_ramp.create_group(igram)
                    dset = group_ramp.create_dataset(igram, data=data_rampCor, compression='gzip')
                    for key, value in h5file[k[0]][igram].attrs.iteritems():
                        group_ramp.attrs[key]=value
  
            try:
                mask = h5file['mask'].get('mask');
                gm = h5out.create_group('mask')
                dset = gm.create_dataset('mask', data=mask[0:mask.shape[0],0:mask.shape[1]], compression='gzip')
            except: print 'no mask group found.'
  
            h5file.close()
            h5out.close()
            if save_rampCor == 'yes':
                h5out_ramp.close()

        ########## ROI_PAC ##########
        elif ext == '.unw':
            print 'Input file is '+ext
            a,data,atr = readfile.read_float32(File);
  
            data_ramp,ramp = rm.remove_data_surface(data,ramp_mask,ramp_type)
            #ramp = data_ramp - data
            data_rampCor = phase_bonding(data_ramp,Mask,x,y)
            dataCor = data_rampCor - ramp
  
            writefile.write(dataCor, atr, outName)
            if save_rampCor == 'yes':
                writefile.write(data_rampCor,atr,outName_ramp)
  
        else: print 'Un-supported file type: '+ext;  Usage();  sys.exit(1)
Esempio n. 8
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)
Esempio n. 9
0
def main(argv):

    #################  default values  ################
    flip_lr = 'no'
    flip_ud = 'no'
    disp_geo = 'yes'
    #font_size=8
    color_map = 'jet'
    figs_rows = 5
    figs_cols = 8
    rewrapping = 'yes'
    allData2display = 'yes'
    Wspace = 0.1
    Hspace = 0.1
    title = 'out'
    showRef = 'yes'
    ref_color = 'k'
    ref_symbol = 's'
    ref_size = 10
    dip_opposite = 'no'
    saveFig = 'no'
    dispFig = 'yes'
    dispContour = 'only'
    contour_step = 200
    contour_sigma = 3.0
    fig_dpi = 300

    ###################  get input args  ###############
    if len(sys.argv) > 2:
        try:
            opts, args = getopt.getopt(
                argv,
                "h:D:O:G:S:f:m:M:v:u:s:c:e:d:r:p:w:i:j:t:R:a:b:k:x:y:C:V:P:o:g:l:L:"
            )
        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 == '-D':
                demFile = arg
            elif opt == '-w':
                rewrapping = arg
            elif opt == '-m':
                min = float(arg)
                rewrapping = 'no'
            elif opt == '-M':
                max = float(arg)
                rewrapping = 'no'
            elif opt == '-v':
                flip_lr = arg
            elif opt == '-u':
                flip_ud = arg
            elif opt == '-s':
                font_size = int(arg)
            elif opt == '-c':
                color_map = arg
            elif opt == '-e':
                epoch_number = int(arg)
                allData2display = 'no'
            elif opt == '-d':
                epoch_date = arg
                allData2display = 'no'
            elif opt == '-r':
                figs_rows = int(arg)
            elif opt == '-p':
                figs_cols = int(arg)
            elif opt == '-i':
                Wspace = float(arg)
            elif opt == '-j':
                Hspace = float(arg)
            elif opt == '-t':
                title = arg
            elif opt == '-R':
                showRef = arg
            elif opt == '-a':
                ref_color = arg
            elif opt == '-b':
                ref_symbol = arg
            elif opt == '-k':
                ref_size = int(arg)
            elif opt == '-x':
                win_x = [int(i) for i in arg.split(':')]
                win_x.sort()
            elif opt == '-y':
                win_y = [int(i) for i in arg.split(':')]
                win_y.sort()
            elif opt == '-G':
                disp_geo = arg
            elif opt == '-O':
                dip_opposite = arg
            elif opt == '-S':
                saveFig = arg
            elif opt == '-C':
                dispContour = arg
            elif opt == '-V':
                contour_step = float(arg)
            elif opt == '-P':
                dispFig = arg
            elif opt == '-o':
                figName = arg
            elif opt == '-g':
                contour_sigma = float(arg)
            elif opt == '-l':
                win_lat = [float(i) for i in arg.split(':')]
                win_lat.sort()
            elif opt == '-L':
                win_lon = [float(i) for i in arg.split(':')]
                win_lon.sort()

    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)

    if color_map == 'hsv':
        ################################################
        cdict1 = {
            'red': ((0.0, 0.0, 0.0), (0.5, 0.0, 0.0), (0.6, 1.0, 1.0),
                    (0.8, 1.0, 1.0), (1.0, 0.5, 0.5)),
            'green': ((0.0, 0.0, 0.0), (0.2, 0.0, 0.0), (0.4, 1.0, 1.0),
                      (0.6, 1.0, 1.0), (0.8, 0.0, 0.0), (1.0, 0.0, 0.0)),
            'blue': (
                (0.0, 0.5, .5),
                (0.2, 1.0, 1.0),
                (0.4, 1.0, 1.0),
                (0.5, 0.0, 0.0),
                (1.0, 0.0, 0.0),
            )
        }

        from matplotlib.colors import LinearSegmentedColormap
        ccmap = LinearSegmentedColormap('BlueRed1', cdict1)

        ################################################
    else:
        ccmap = plt.get_cmap(color_map)

    ##################################################
    ext = os.path.splitext(File)[1]
    if ext == '.h5':
        import h5py
        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'
        print 'Input: ' + str(k)
        if k[0] in ('dem', 'velocity', 'mask', 'temporal_coherence', 'rmse'):
            allData2display = 'no'

    elif ext in [
            '.unw', '.int', '.cor', '.hgt', '.dem', '.trans', '.mli', '.slc'
    ]:
        import pysar._readfile as readfile
        allData2display = 'no'
        k = [ext]
    else:
        print 'File extension not recogized: ' + ext
        print 'Support file format:\n\
                PySAR HDF5 files: velocity.h5, timeseries.h5, LoadedData.h5, ...\n\
                ROI_PAC    files: .unw .cor .int .hgt .dem .trans .mli'

        sys.exit(1)

####################################################################
########################## Plot One ################################

    if allData2display == 'no':
        try:
            font_size
        except:
            font_size = 12

        ################# File Reading ##################
        ##### PySAR HDF5
        if k[0] in ('dem', 'velocity', 'mask', 'temporal_coherence', 'rmse'):
            atr = h5file[k[0]].attrs
            dset = h5file[k[0]].get(k[0])
            data = dset[0:dset.shape[0], 0:dset.shape[1]]
            # rewrapping
            if rewrapping in ('yes', 'Yes', 'Y', 'y', 'YES'):
                print 'Rewrapping disabled for ' + k[0]

        elif k[0] == 'timeseries':
            dateList = h5file[k[0]].keys()
            try:
                epoch_number
            except:
                try:
                    epoch_date
                    if len(epoch_date) == 6:
                        epoch_date = yymmdd2yyyymmdd(epoch_date)
                    epoch_number = dateList.index(epoch_date)
                except:
                    print 'Unrecognized epoch input!'
                    sys.exit(1)
            print 'Displaying date: ' + dateList[epoch_number]
            atr = h5file[k[0]].attrs
            dset = h5file[k[0]].get(dateList[epoch_number])
            data = dset[0:dset.shape[0], 0:dset.shape[1]]
            # rewrapping
            if rewrapping in ('yes', 'Yes', 'y', 'Y', 'YES'):
                print 'Rewrapping. Set min/max to -pi/pi. Showing phase'
                range2phase = 4 * np.pi / float(
                    atr['WAVELENGTH'])  #double-way, 2*2*pi/lamda
                data = range2phase * data
                data = rewrap(data)
                min = -np.pi
                max = np.pi
                figUnit = '(radian)'
            elif rewrapping in ('no', 'No', 'N', 'n', 'NO'):
                print 'No rewrapping. Showing displacement.'
                figUnit = '(m)'

        elif k[0] in ('interferograms', 'coherence', 'wrapped'):
            ifgramList = h5file[k[0]].keys()
            try:
                epoch_number
            except:
                for i in range(len(ifgramList)):
                    if epoch_date in ifgramList[i]: epoch_number = i
            print 'Displaying: ' + ifgramList[epoch_number]
            atr = h5file[k[0]][ifgramList[epoch_number]].attrs
            dset = h5file[k[0]][ifgramList[epoch_number]].get(
                ifgramList[epoch_number])
            data = dset[0:dset.shape[0], 0:dset.shape[1]]

            # rewrapping
            if k[0] in ('coherence',
                        'wrapped') and rewrapping in ('yes', 'Yes', 'y', 'Y',
                                                      'YES'):
                print 'No rewrapping for coherence/wrapped files, set to "no"'
                rewrapping = 'no'
            if rewrapping in ('yes', 'Yes', 'y', 'Y', 'YES'):
                print 'Rewrapping. Set min/max to -pi/pi.'
                data = np.angle(np.exp(1j * data))
                min = -np.pi
                max = np.pi

        ##### ROI_PAC product
        elif k[0] in ['.slc', '.mli']:
            data, p, atr = readfile.read_complex64(File)
            data = np.nanlog10(data)
            figUnit = '(dB)'
        elif k[0] == '.int':
            a, data, atr = readfile.read_complex64(File)
            min = -np.pi
            max = np.pi
            rewrapping = 'no'
            figUnit = '(radian)'
        elif k[0] in ['.unw', '.cor', '.hgt', '.trans']:
            a, data, atr = readfile.read_float32(File)
            if k[0] == '.unw': figUnit = '(radian)'
            elif k[0] == '.hgt': figUnit = '(m)'
            if k[0] in ['.cor', '.hgt', '.trans']: rewrapping = 'no'
            if rewrapping in ('yes', 'Yes', 'y', 'Y', 'True', 'true'):
                print 'Rewrapping. Set min/max to -pi/pi.'
                data = rewrap(data)
                min = -np.pi
                max = np.pi
        elif k[0] == '.dem':
            data, atr = readfile.read_dem(File)
            figUnit = '(m)'

        ############## Data Option ##################
        # Opposite Sign
        if dip_opposite in ('yes', 'Yes', 'Y', 'y', 'YES'):
            data = -1 * data
        # Subset
        try:  # y/latitude direction
            win_lat
            try:
                atr['Y_FIRST']
                win_y = [0] * 2
                win_y[0] = int((win_lat[1] - float(atr['Y_FIRST'])) /
                               float(atr['Y_STEP']))
                win_y[1] = int((win_lat[0] - float(atr['Y_FIRST'])) /
                               float(atr['Y_STEP']))
                if win_y[0] < 0:
                    win_y[0] = 0
                    print 'input latitude > max latitude! Set to max'
                print 'subset in latitude  - ' + str(win_lat[0]) + ':' + str(
                    win_lat[1])
            except:
                print 'Non-geocoded file, cannot use LatLon option'
                Usage()
                sys.exit(1)
        except:
            try:
                win_y
                print 'subset in y direction - ' + str(win_y[0]) + ':' + str(
                    win_y[1])
            except:
                win_y = [0, int(atr['FILE_LENGTH'])]
        try:  # x/longitude direction
            win_lon
            try:
                atr['X_FIRST']
                win_x = [0] * 2
                win_x[0] = int((win_lon[0] - float(atr['X_FIRST'])) /
                               float(atr['X_STEP']))
                win_x[1] = int((win_lon[1] - float(atr['X_FIRST'])) /
                               float(atr['X_STEP']))
                if win_x[0] < 0:
                    win_x[0] = 0
                    print 'input longitude > max longitude! Set to max'
                print 'subset in longitude - ' + str(win_lon[0]) + ':' + str(
                    win_lon[1])
            except:
                print 'Non-geocoded file, cannot use LatLon option'
                Usage()
                sys.exit(1)
        except:
            try:
                win_x
                print 'subset in x direction - ' + str(win_x[0]) + ':' + str(
                    win_x[1])
            except:
                win_x = [0, int(atr['WIDTH'])]

        data = data[win_y[0]:win_y[1], win_x[0]:win_x[1]]

        # Reference Point
        try:
            xref = atr['ref_x'] - win_x[0]
            yref = atr['ref_y'] - win_y[0]
        except:
            print 'No reference point'
        try:
            xref = xref - atr['subset_x0']
            yref = yref - atr['subset_y0']
        except:
            print 'No subset'
        # Geo coordinate
        try:
            lon_step = float(atr['X_STEP'])
            lat_step = float(atr['Y_STEP'])
            lon_unit = atr['Y_UNIT']
            lat_unit = atr['X_UNIT']
            ullon = float(atr['X_FIRST']) + win_x[0] * lon_step
            ullat = float(atr['Y_FIRST']) + win_y[0] * lat_step
            llcrnrlon = ullon
            llcrnrlat = ullat + lat_step * data.shape[0]
            urcrnrlon = ullon + lon_step * data.shape[1]
            urcrnrlat = ullat
            geocoord = 'yes'
            print 'Input file is Geocoded'
        except:
            geocoord = 'no'
        # Flip
        if flip_lr in ('yes', 'Yes', 'Y', 'y', 'YES'):
            data = np.fliplr(data)
            xref = np.shape(data)[1] - xref - 1
        if flip_ud in ('yes', 'Yes', 'Y', 'y', 'YES'):
            data = np.flipud(data)
            yref = np.shape(data)[0] - yref - 1

        # Colorbar Extend
        data_min = np.nanmin(data)
        data_max = np.nanmax(data)
        try:
            min
        except:
            min = data_min
        try:
            max
        except:
            max = data_max
        if min <= data_min and max >= data_max: cb_extend = 'neither'
        elif min > data_min and max >= data_max: cb_extend = 'min'
        elif min <= data_min and max < data_max: cb_extend = 'max'
        else: cb_extend = 'both'

        ############## DEM Option ##################
        try:
            demFile
            print 'Show topography'
            import pysar._readfile as readfile
            if os.path.basename(demFile).split('.')[1] == 'hgt':
                amp, dem, demRsc = readfile.read_float32(demFile)
            elif os.path.basename(demFile).split('.')[1] == 'dem':
                dem, demRsc = readfile.read_dem(demFile)

            # Subset
            dem = dem[win_y[0]:win_y[1], win_x[0]:win_x[1]]
            # Flip
            if flip_lr in ('yes', 'Yes', 'Y', 'y', 'YES'): dem = np.fliplr(dem)
            if flip_ud in ('yes', 'Yes', 'Y', 'y', 'YES'): dem = np.flipud(dem)

            # DEM data preparation
            if dispContour in ('no', 'No', 'n', 'N', 'NO', 'yes', 'Yes', 'y',
                               'Y', 'YES'):  #DEM basemap
                print 'plot DEM as basemap'
                cmap_dem = plt.get_cmap('gray')
                import pysar._pysar_utilities as ut
            if dispContour in ('only', 'Only', 'o', 'O', 'ONLY', 'yes', 'Yes',
                               'y', 'Y', 'YES'):  #contour
                print 'plot contour'
                #if smoothContour in ('yes','Yes','y','Y','YES'):
                import scipy.ndimage as ndimage
                dem = ndimage.gaussian_filter(dem,
                                              sigma=contour_sigma,
                                              order=0)
                contour_sequence = np.arange(-6000, 9000, contour_step)
        except:
            print 'No DEM file'

        ############## Data Plot and Output  ################
        # Figure Title
        if k[0] == 'velocity': figTitle = 'Velocity (m/yr)'
        elif k[0] == 'temporal_coherence': figTitle = 'Temporal coherence'
        elif k[0] == 'dem': figTitle = 'DEM error'
        elif k[0] == 'rmse': figTitle = 'RMSE (m/yr)'
        elif k[0] == 'mask': figTitle = 'Pixels with no valid value.'
        elif k[0] == 'coherence': figTitle = ifgramList[epoch_number]
        elif k[0] in ('interferograms', 'wrapped'):
            figTitle = ifgramList[epoch_number] + ' (radian)'
        elif k[0] == 'timeseries':
            try:
                master_date = atr['ref_date']
            except:
                master_date = atr['DATE']
            if len(master_date) == 6:
                master_date = yymmdd2yyyymmdd(master_date)
            if dip_opposite in ('yes', 'Yes', 'Y', 'y', 'YES'):
                figTitle = dateList[
                    epoch_number] + '_' + master_date + ' ' + figUnit
            else:
                figTitle = master_date + '_' + dateList[
                    epoch_number] + ' ' + figUnit
        elif k[0] in [
                '.unw', '.cor', '.hgt', '.dem', '.trans', '.mli', '.slc'
        ]:
            try:
                figTitle = File + ' ' + figUnit
            except:
                figTitle = File

        # Plot in Geo-coordinate: plot in map
        if geocoord == 'yes' and disp_geo in ('yes', 'Yes', 'y', 'Y', 'YES'):
            print 'display Lat/Lon'
            fig = plt.figure()
            ax = fig.add_axes([0.1, 0.1, 0.8, 0.8])
            try:
                plt.title(figTitle, fontsize=font_size)
            except:
                pass

            # Map - DEM - Data
            from mpl_toolkits.basemap import Basemap
            m = Basemap(llcrnrlon=llcrnrlon,
                        llcrnrlat=llcrnrlat,
                        urcrnrlon=urcrnrlon,
                        urcrnrlat=urcrnrlat,
                        resolution='l',
                        area_thresh=1.,
                        projection='cyl',
                        suppress_ticks=False,
                        ax=ax)
            try:
                demFile
                if dispContour in ('no', 'No', 'n', 'N', 'NO', 'yes', 'Yes',
                                   'y', 'Y', 'YES'):
                    m.imshow(ut.hillshade(np.flipud(dem), 50.0), cmap=cmap_dem)
                if dispContour in ('only', 'Only', 'o', 'O', 'ONLY', 'yes',
                                   'Yes', 'y', 'Y', 'YES'):
                    import numpy.matlib
                    c_x = np.linspace(llcrnrlon,
                                      urcrnrlon,
                                      num=dem.shape[1],
                                      endpoint='FALSE').reshape(
                                          1, dem.shape[1])
                    c_xx = np.matlib.repmat(c_x, dem.shape[0], 1)
                    c_y = np.linspace(llcrnrlat,
                                      urcrnrlat,
                                      num=dem.shape[0],
                                      endpoint='FALSE').reshape(
                                          dem.shape[0], 1)
                    c_yy = np.matlib.repmat(c_y, 1, dem.shape[1])
                    m.contour(c_xx,
                              c_yy,
                              np.flipud(dem),
                              contour_sequence,
                              origin='lower',
                              colors='black',
                              alpha=0.5,
                              latlon='FALSE')
            except:
                pass
            try:
                im = m.imshow(np.flipud(data), cmap=ccmap, vmin=min, vmax=max)
            except:
                im = m.imshow(np.flipud(data), cmap=ccmap)

            # Reference Point
            if showRef in ('yes', 'Yes', 'Y', 'y', 'YES'):
                try:
                    refPoint = ref_color + ref_symbol
                    ref_lon = llcrnrlon + xref * lon_step
                    ref_lat = urcrnrlat + yref * lat_step
                    plt.plot(ref_lon, ref_lat, refPoint, ms=ref_size)
                except:
                    print 'No reference point'

            # Colorbar
            from mpl_toolkits.axes_grid1 import make_axes_locatable
            divider = make_axes_locatable(ax)
            cax = divider.append_axes("right", size="5%", pad=0.30)
            plt.colorbar(im, cax=cax, extend=cb_extend)

            #plt.colorbar(im,cax=cax)

            # Status bar
            def format_coord(x, y):
                col = int((x - ullon) / lon_step + 0.5)
                row = int((y - ullat) / lat_step + 0.5)
                if col >= 0 and col <= data.shape[
                        1] and row >= 0 and row <= data.shape[0]:
                    z = data[row, col]
                    return 'lat=%.4f,  lon=%.4f,  value=%.4f' % (x, y, z)
                else:
                    return 'lat=%.4f,  lon=%.4f' % (x, y)

            ax.format_coord = format_coord

        # Plot in x/y coordinate: row and column
        else:
            fig = plt.figure()
            ax = fig.add_axes([0.1, 0.1, 0.8, 0.8])
            try:
                plt.title(figTitle, fontsize=font_size)
            except:
                pass

            # Plot
            try:
                demFile
                if dispContour in ('no', 'No', 'n', 'N', 'NO', 'yes', 'Yes',
                                   'y', 'Y', 'YES'):
                    ax.imshow(ut.hillshade(dem, 50.0), cmap=cmap_dem)
                if dispContour in ('only', 'Only', 'o', 'O', 'ONLY', 'yes',
                                   'Yes', 'y', 'Y', 'YES'):
                    ax.contour(dem,
                               contour_sequence,
                               origin='lower',
                               colors='black',
                               alpha=0.5)
            except:
                pass
            try:
                im = ax.imshow(data, cmap=ccmap, vmin=min, vmax=max)
            except:
                im = ax.imshow(data, cmap=ccmap)
            cbar = plt.colorbar(im, extend=cb_extend)
            #cbar.set_label('m/yr')

            # Reference Point
            if showRef in ('yes', 'Yes', 'Y', 'y', 'YES'):
                try:
                    refPoint = ref_color + ref_symbol
                    ax.plot(xref, yref, refPoint, ms=ref_size)
                except:
                    print 'No reference point'

            plt.xlim(0, np.shape(data)[1])
            plt.ylim(np.shape(data)[0], 0)

            # Status bar
            def format_coord(x, y):
                col = int(x + 0.5)
                row = int(y + 0.5)
                if col >= 0 and col <= data.shape[
                        1] and row >= 0 and row <= data.shape[0]:
                    z = data[row, col]
                    return 'x=%.4f,  y=%.4f,  value=%.4f' % (x, y, z)
                else:
                    return 'x=%.4f,  y=%.4f' % (x, y)

            ax.format_coord = format_coord

        # Save Figure
        if saveFig in ('yes', 'Yes', 'Y', 'y', 'YES'):
            try:
                figName
            except:
                if k[0] == 'velocity': figName = 'velocity.pdf'
                elif k[0] == 'temporal_coherence':
                    figName = 'temporal_coherence.pdf'
                elif k[0] == 'dem':
                    figName = 'DEM_error.pdf'
                elif k[0] == 'rmse':
                    figName = 'rmse.pdf'
                elif k[0] == 'mask':
                    figName = 'mask.pdf'
                elif k[0] == 'timeseries':
                    figName = os.path.basename(File).split(
                        '.')[0] + '_' + dateList[epoch_number] + '.pdf'
                elif k[0] in ('interferograms', 'coherence', 'wrapped'):
                    figName = ifgramList[epoch_number] + '.pdf'
                elif k[0] in [
                        '.unw', '.cor', '.hgt', '.dem', '.trans', '.mli',
                        '.slc'
                ]:
                    figName = File + '.pdf'
            plt.savefig(figName, dpi=fig_dpi)
            print 'Saved figure to ' + figName

        # Show Figure
        if dispFig in ('yes', 'Yes', 'Y', 'y', 'YES'):
            plt.show()

####################################################################
########################## Plot All ################################

    elif allData2display == 'yes':
        try:
            font_size
        except:
            font_size = 8

        if k[0] == 'timeseries':
            atr = h5file[k[0]].attrs
            # rewrapping
            if rewrapping in ('yes', 'Yes', 'Y', 'y', 'YES'):
                print 'Rewrapping. Set min/max to -pi/pi. Showing phase.'
                range2phase = 4 * np.pi / float(
                    h5file['timeseries'].attrs['WAVELENGTH'])
                min = -np.pi
                max = np.pi
            else:
                print 'No rewrapping. Showing displacement.'

        elif k[0] in ('interferograms', 'coherence', 'wrapped'):
            atr = h5file[k[0]][h5file[k[0]].keys()[0]].attrs
            # rewrapping
            if k[0] in ('coherence',
                        'wrapped') and rewrapping in ('yes', 'Yes', 'y', 'Y',
                                                      'YES'):
                print 'No rewrapping for coherence/wrapped files, set to "no"'
                rewrapping = 'no'
            if rewrapping in ('yes', 'Yes', 'Y', 'y', 'YES'):
                print 'Rewrapping. Set min/max to -pi/pi.'
                min = -np.pi
                max = np.pi
            else:
                print 'No rewrapping'

        ### Subset Option ###
        try:  # y/latitude direction
            win_lat
            try:
                atr['Y_FIRST']
                win_y = [0] * 2
                win_y[0] = int((win_lat[1] - float(atr['Y_FIRST'])) /
                               float(atr['Y_STEP']))
                win_y[1] = int((win_lat[0] - float(atr['Y_FIRST'])) /
                               float(atr['Y_STEP']))
                if win_y[0] < 0:
                    win_y[0] = 0
                    print 'input latitude > max latitude! Set to max'
                print 'subset in latitude  - ' + str(win_lat[0]) + ':' + str(
                    win_lat[1])
            except:
                print 'Non-geocoded file, cannot use LatLon option'
                Usage()
                sys.exit(1)
        except:
            try:
                win_y
                print 'subset in y direction - ' + str(win_y[0]) + ':' + str(
                    win_y[1])
            except:
                win_y = [0, int(atr['FILE_LENGTH'])]
        try:  # x/longitude direction
            win_lon
            try:
                atr['X_FIRST']
                win_x = [0] * 2
                win_x[0] = int((win_lon[0] - float(atr['X_FIRST'])) /
                               float(atr['X_STEP']))
                win_x[1] = int((win_lon[1] - float(atr['X_FIRST'])) /
                               float(atr['X_STEP']))
                if win_x[0] < 0:
                    win_x[0] = 0
                    print 'input longitude > max longitude! Set to max'
                print 'subset in longitude - ' + str(win_lon[0]) + ':' + str(
                    win_lon[1])
            except:
                print 'Non-geocoded file, cannot use LatLon option'
                Usage()
                sys.exit(1)
        except:
            try:
                win_x
                print 'subset in x direction - ' + str(win_x[0]) + ':' + str(
                    win_x[1])
            except:
                win_x = [0, int(atr['WIDTH'])]

        # Figure Name
        if saveFig in ('yes', 'Yes', 'Y', 'y', 'YES'):
            try:
                figName
                figNameBase = os.path.basename(figName).split('.')[0]
                figNameExt = os.path.basename(figName).split('.')[1]
            except:
                figNameBase = os.path.basename(File).split('.')[0]
                figNameExt = '.pdf'

        ################## DEM Options ####################
        try:
            demFile
            print 'Show topography'
            import pysar._readfile as readfile
            if os.path.basename(demFile).split('.')[1] == 'hgt':
                amp, dem, demRsc = readfile.read_float32(demFile)
            elif os.path.basename(demFile).split('.')[1] == 'dem':
                dem, demRsc = readfile.read_dem(demFile)

            # Subset
            dem = dem[win_y[0]:win_y[1], win_x[0]:win_x[1]]

            if dispContour in ('no', 'No', 'n', 'N', 'NO', 'yes', 'Yes', 'y',
                               'Y', 'YES'):  #DEM basemap
                print 'plot DEM as basemap'
                cmap_dem = plt.get_cmap('gray')
                import pysar._pysar_utilities as ut
                hillshade_dem = ut.hillshade(dem, 50.0)
            if dispContour in ('only', 'Only', 'o', 'O', 'ONLY', 'yes', 'Yes',
                               'y', 'Y', 'YES'):  #contour
                print 'plot contour'
                #if smoothContour in ('yes','Yes','y','Y','YES'):
                import scipy.ndimage as ndimage
                dem = ndimage.gaussian_filter(dem,
                                              sigma=contour_sigma,
                                              order=0)
                contour_sequence = np.arange(-6000, 9000, contour_step)
        except:
            print 'No DEM file'

        ################## Plot Loop ####################
        ifgramList = h5file[k[0]].keys()
        nfigs = figs_rows * figs_cols
        lifgram = len(ifgramList)
        print 'number of  epochs/interferograms to display:' + str(lifgram)
        kk = int(lifgram / nfigs) + 1
        ii = 0

        # plot (1,end-1) figures
        for j in range(1, kk):
            fig = plt.figure(j)
            ii = (j - 1) * nfigs + 1
            for i in range(ii, ii + nfigs):
                print 'loading ' + ifgramList[i - 1]
                ax = fig.add_subplot(figs_rows, figs_cols, i - ii + 1)

                # Data option
                if k[0] == 'timeseries':
                    figTitle = ifgramList[i - 1]
                    dset = h5file[k[0]].get(ifgramList[i - 1])
                    data = dset[0:dset.shape[0], 0:dset.shape[1]]
                    if rewrapping in ('yes', 'Yes', 'Y', 'y', 'YES'):
                        data = range2phase * data
                        data = rewrap(data)
                elif k[0] in ('interferograms', 'coherence', 'wrapped'):
                    figTitle = str(i) + ' : ' + h5file[k[0]][ifgramList[
                        i - 1]].attrs['DATE12']
                    dset = h5file[k[0]][ifgramList[i - 1]].get(ifgramList[i -
                                                                          1])
                    data = dset[0:dset.shape[0], 0:dset.shape[1]]
                    if rewrapping in ('yes', 'Yes', 'Y', 'y', 'YES'):
                        data = np.angle(np.exp(1j * data))

                data = data[win_y[0]:win_y[1], win_x[0]:win_x[1]]

                # Plot
                try:
                    demFile
                    if dispContour in ('no', 'No', 'n', 'N', 'NO', 'yes',
                                       'Yes', 'y', 'Y', 'YES'):
                        plt.imshow(hillshade_dem, cmap=cmap_dem)
                    if dispContour in ('only', 'Only', 'o', 'O', 'ONLY', 'yes',
                                       'Yes', 'y', 'Y', 'YES'):
                        plt.contour(dem,
                                    contour_sequence,
                                    origin='lower',
                                    colors='black',
                                    alpha=0.5)
                except:
                    pass
                try:
                    ax.imshow(data, cmap=ccmap, vmin=min, vmax=max)
                except:
                    ax.imshow(data, cmap=ccmap)

                ax.set_yticklabels([])
                ax.set_xticklabels([])
                ax.set_xticks([])
                ax.set_yticks([])
                if title == 'out': ax.set_title(figTitle, fontsize=font_size)
                elif title == 'in': add_inner_title(ax, figTitle, loc=1)
            fig.subplots_adjust(wspace=Wspace, hspace=Hspace)
            if saveFig in ('yes', 'Yes', 'Y', 'y', 'YES'):
                figName = figNameBase + '_' + str(j) + figNameExt
                plt.savefig(figName, dpi=fig_dpi)

        # plot the last figure
        fig = plt.figure(kk)
        ii = (kk - 1) * nfigs + 1
        for i in range(ii, lifgram + 1):
            print 'loading ' + ifgramList[i - 1]
            ax = fig.add_subplot(figs_rows, figs_cols, i - ii + 1)

            # Data option
            if k[0] == 'timeseries':
                figTitle = ifgramList[i - 1]
                dset = h5file[k[0]].get(ifgramList[i - 1])
                data = dset[0:dset.shape[0], 0:dset.shape[1]]
                if rewrapping in ('yes', 'Yes', 'Y', 'y', 'YES'):
                    data = range2phase * data
                    data = rewrap(data)
            elif k[0] in ('interferograms', 'coherence', 'wrapped'):
                figTitle = str(i) + ' : ' + h5file[k[0]][ifgramList[
                    i - 1]].attrs['DATE12']
                dset = h5file[k[0]][ifgramList[i - 1]].get(ifgramList[i - 1])
                data = dset[0:dset.shape[0], 0:dset.shape[1]]
                if rewrapping in ('yes', 'Yes', 'Y', 'y', 'YES'):
                    data = np.angle(np.exp(1j * data))

            data = data[win_y[0]:win_y[1], win_x[0]:win_x[1]]

            # Plot
            try:
                demFile
                if dispContour in ('no', 'No', 'n', 'N', 'NO', 'yes', 'Yes',
                                   'y', 'Y', 'YES'):
                    plt.imshow(hillshade_dem, cmap=cmap_dem)
                if dispContour in ('only', 'Only', 'o', 'O', 'ONLY', 'yes',
                                   'Yes', 'y', 'Y', 'YES'):
                    plt.contour(dem,
                                contour_sequence,
                                origin='lower',
                                colors='black',
                                alpha=0.5)
            except:
                pass

            try:
                ax.imshow(data, cmap=ccmap, vmin=min, vmax=max)
            except:
                ax.imshow(data, cmap=ccmap)

            ax.xaxis.label.set_fontsize(20)
            ax.set_yticklabels([])
            ax.set_xticklabels([])
            ax.set_xticks([])
            ax.set_yticks([])
            if title == 'out': ax.set_title(figTitle, fontsize=font_size)
            elif title == 'in': add_inner_title(ax, figTitle, loc=1)
        fig.subplots_adjust(wspace=Wspace, hspace=Hspace)
        if saveFig in ('yes', 'Yes', 'Y', 'y', 'YES'):
            figName = figNameBase + '_' + str(kk) + figNameExt
            plt.savefig(figName, dpi=fig_dpi)
            print 'Saved figure to ' + figNameBase + '_*' + figNameExt

        if dispFig in ('yes', 'Yes', 'Y', 'y', 'YES'):
            plt.show()


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

    try:
        h5file.close()
    except:
        pass
Esempio n. 10
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
Esempio n. 11
0
def main(argv):

    method = 'triangular_consistency'  ## or 'bonding_point'
    ramp_type = 'plane'
    save_rampCor = 'yes'
    plot_bonding_points = 'yes'

    ##### Check Inputs
    if len(sys.argv) > 2:
        try:
            opts, args = getopt.getopt(argv, 'h:f:m:x:y:o:t:',
                                       ['ramp=', 'no-ramp-save'])
        except getopt.GetoptError:
            print 'Error while getting args'
            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 '-x':
                x = [int(i) for i in arg.split(',')]
                method = 'bonding_point'
            elif opt in '-y':
                y = [int(i) for i in arg.split(',')]
                method = 'bonding_point'
            elif opt in '-t':
                templateFile = arg
            elif opt in '--ramp':
                ramp_type = arg.lower()
            elif opt in '--no-ramp-save':
                save_rampCor = 'no'

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

    else:
        usage()
        sys.exit(1)

    ##### Check template file
    try:
        templateFile
        templateContents = readfile.read_template(templateFile)
    except:
        pass

    try:
        yx = [
            int(i) for i in templateContents['pysar.unwrapError.yx'].split(',')
        ]
        x = yx[1::2]
        y = yx[0::2]
        method = 'bonding_point'
    except:
        pass

    ##### Read Mask File
    ## Priority:
    ## Input mask file > pysar.mask.file > existed Modified_Mask.h5 > existed Mask.h5
    try:
        maskFile
    except:
        try:
            maskFile = templateContents['pysar.mask.file']
        except:
            if os.path.isfile('Modified_Mask.h5'):
                maskFile = 'Modified_Mask.h5'
            elif os.path.isfile('Mask.h5'):
                maskFile = 'Mask.h5'
            else:
                print 'No mask found!'
                sys.exit(1)
    try:
        Mask, Matr = readfile.read(maskFile)
        print 'mask: ' + maskFile
    except:
        print 'Can not open mask file: ' + maskFile
        sys.exit(1)

    ##### Output file name
    ext = os.path.splitext(File)[1]
    try:
        outName
    except:
        outName = File.split('.')[0] + '_unwCor' + ext

    print '\n**************** Unwrapping Error Correction ******************'

    ####################  Triangular Consistency (Phase Closure)  ####################
    if method == 'triangular_consistency':
        print 'Phase unwrapping error correction using Triangular Consistency / Phase Closure'

        h5file = h5py.File(File)
        ifgramList = h5file['interferograms'].keys()
        sx = int(h5file['interferograms'][ifgramList[0]].attrs['WIDTH'])
        sy = int(h5file['interferograms'][ifgramList[0]].attrs['FILE_LENGTH'])
        curls, Triangles, C = ut.get_triangles(h5file)
        A, B = ut.design_matrix(h5file)
        ligram, lv = np.shape(B)
        lcurls = np.shape(curls)[0]
        print 'Number of all triangles: ' + str(lcurls)
        print 'Number of interferograms: ' + str(ligram)
        #print curls

        curlfile = 'curls.h5'
        if not os.path.isfile(curlfile):
            ut.generate_curls(curlfile, h5file, Triangles, curls)

        thr = 0.50
        curls = np.array(curls)
        n1 = curls[:, 0]
        n2 = curls[:, 1]
        n3 = curls[:, 2]

        numPixels = sy * sx
        print 'reading interferograms...'
        data = np.zeros((ligram, numPixels), np.float32)
        for ni in range(ligram):
            dset = h5file['interferograms'][ifgramList[ni]].get(ifgramList[ni])
            d = dset[0:dset.shape[0], 0:dset.shape[1]]
            data[ni] = d.flatten(1)

        print np.shape(data)
        print 'reading curls ...'
        h5curl = h5py.File(curlfile)
        curlList = h5curl['interferograms'].keys()
        curlData = np.zeros((lcurls, numPixels), np.float32)
        for ni in range(lcurls):
            dset = h5curl['interferograms'][curlList[ni]].get(curlList[ni])
            d = dset[0:dset.shape[0], 0:dset.shape[1]]
            curlData[ni] = d.flatten(1)
        pi = np.pi
        EstUnwrap = np.zeros((ligram, numPixels), np.float32)

        #try:
        #    maskFile=argv[1]
        #    h5Mask=h5py.File(maskFile)
        #    dset = h5Mask['mask'].get('mask')
        #    Mask=dset[0:dset.shape[0],0:dset.shape[1]]
        #except:
        #    dset = h5file['mask'].get('mask')
        #    Mask=dset[0:dset.shape[0],0:dset.shape[1]]

        Mask = Mask.flatten(1)

        for ni in range(numPixels):
            #dU = np.zeros([ligram,1])
            #print np.shape(dU)
            #print np.shape(data[:,ni])

            if Mask[ni] == 1:
                dU = data[:, ni]
                #nan_ndx = dataPoint == 0.
                unwCurl = np.array(curlData[:, ni])
                #print unwCurl

                ind = np.abs(unwCurl) >= thr
                N1 = n1[ind]
                N2 = n2[ind]
                N3 = n3[ind]
                indC = np.abs(unwCurl) < thr
                Nc1 = n1[indC]
                Nc2 = n2[indC]
                Nc3 = n3[indC]

                N = np.hstack([N1, N2, N3])
                UniN = np.unique(N)
                Nc = np.hstack([Nc1, Nc2, Nc3])
                UniNc = np.unique(Nc)

                inter = list(set(UniNc) & set(UniN))  # intersetion
                UniNc = list(UniNc)
                for x in inter:
                    UniNc.remove(x)

                D = np.zeros([len(UniNc), ligram])
                for i in range(len(UniNc)):
                    D[i, UniNc[i]] = 1

                AAA = np.vstack([-2 * pi * C, D])
                #AAA1=np.hstack([AAA,np.zeros([AAA.shape[0],lv])])
                #AAA2=np.hstack([-2*pi*np.eye(ligram),B])
                #AAAA=np.vstack([AAA1,AAA2])
                AAAA = np.vstack([AAA, 0.25 * np.eye(ligram)])

                #print '************************'
                #print np.linalg.matrix_rank(C)
                #print np.linalg.matrix_rank(AAA)
                #print np.linalg.matrix_rank(AAAA)
                #print '************************'

                #LLL=list(np.dot(C,dU)) + list(np.zeros(np.shape(UniNc)[0]))# + list(dU)
                #ind=np.isnan(AAA)
                #M1=pinv(AAA)
                #M=np.dot(M1,LLL)
                #EstUnwrap[:,ni]=np.round(M[0:ligram])*2.0*np.pi

                ##########
                # with Tikhonov regularization:
                AAAA = np.vstack([AAA, 0.25 * np.eye(ligram)])
                LLL = list(np.dot(C, dU)) + list(np.zeros(
                    np.shape(UniNc)[0])) + list(np.zeros(ligram))
                ind = np.isnan(AAAA)
                M1 = pinv(AAAA)
                M = np.dot(M1, LLL)
                EstUnwrap[:, ni] = np.round(M[0:ligram]) * 2.0 * np.pi
                #print M[0:ligram]
                #print np.round(M[0:ligram])

            else:
                EstUnwrap[:, ni] = np.zeros([ligram])
                if not np.remainder(ni, 10000):
                    print 'Processing point: %7d of %7d ' % (ni, numPixels)

        ##### Output
        dataCor = data + EstUnwrap
        unwCorFile = File.replace('.h5', '') + '_unwCor.h5'
        print 'writing >>> ' + unwCorFile
        h5unwCor = h5py.File(unwCorFile, 'w')
        gg = h5unwCor.create_group('interferograms')
        for i in range(ligram):
            group = gg.create_group(ifgramList[i])
            dset = group.create_dataset(ifgramList[i],
                                        data=np.reshape(
                                            dataCor[i, :], [sx, sy]).T,
                                        compression='gzip')
            for key, value in h5file['interferograms'][
                    ifgramList[i]].attrs.iteritems():
                group.attrs[key] = value

        try:
            MASK = h5file['mask'].get('mask')
            gm = h5unwCor.create_group('mask')
            dset = gm.create_dataset('mask', data=MASK, compression='gzip')
        except:
            pass

        h5unwCor.close()
        h5file.close()
        h5curl.close()

    ####################  Bonding Points (Spatial Continuity)  ####################
    elif method == 'bonding_point':
        print 'Phase unwrapping error correction using Bonding Points / Spatial Continuity'

        ##### Read Bridge Points Info
        try:
            x
            y
            if len(x) != len(y) or np.mod(len(x), 2) != 0:
                print 'Wrong number of bridge points input: ' + str(
                    len(x)) + ' for x, ' + str(len(y)) + ' for y'
                usage()
                sys.exit(1)
        except:
            print 'Error in reading bridge points info!'
            usage()
            sys.exit(1)
        for i in range(0, len(x)):
            if Mask[y[i], x[i]] == 0:
                print '\nERROR: Connecting point (' + str(y[i]) + ',' + str(
                    x[i]) + ') is out of masked area! Select them again!\n'
                sys.exit(1)

        print 'Number of bonding point pairs: ' + str(len(x) / 2)
        print 'Bonding points coordinates:\nx: ' + str(x) + '\ny: ' + str(y)

        ## Plot Connecting Pair of Points
        if plot_bonding_points == 'yes':
            point_yx = ''
            line_yx = ''
            n_bridge = len(x) / 2
            for i in range(n_bridge):
                pair_yx = str(y[2 * i]) + ',' + str(x[2 * i]) + ',' + str(
                    y[2 * i + 1]) + ',' + str(x[2 * i + 1])
                if not i == n_bridge - 1:
                    point_yx += pair_yx + ','
                    line_yx += pair_yx + ';'
                else:
                    point_yx += pair_yx
                    line_yx += pair_yx

            try:
                plot_cmd = 'view.py --point-yx="'+point_yx+'" --line-yx="'+line_yx+\
                           '" --nodisplay -o bonding_points.png -f '+maskFile
                print plot_cmd
                os.system(plot_cmd)
            except:
                pass

        ##### Ramp Info
        ramp_mask = Mask == 1
        print 'estimate phase ramp during the correction'
        print 'ramp type: ' + ramp_type
        if save_rampCor == 'yes':
            outName_ramp = os.path.basename(outName).split(
                ext)[0] + '_' + ramp_type + ext

        ########## PySAR ##########
        if ext == '.h5':
            ##### Read
            try:
                h5file = h5py.File(File, 'r')
            except:
                print 'ERROR: Cannot open input file: ' + File
                sys.exit(1)
            k = h5file.keys()
            if 'interferograms' in k:
                k[0] = 'interferograms'
                print 'Input file is ' + k[0]
            else:
                print 'Input file - ' + File + ' - is not interferograms.'
                usage()
                sys.exit(1)
            igramList = sorted(h5file[k[0]].keys())

            #### Write
            h5out = h5py.File(outName, 'w')
            gg = h5out.create_group(k[0])
            print 'writing >>> ' + outName

            if save_rampCor == 'yes':
                h5out_ramp = h5py.File(outName_ramp, 'w')
                gg_ramp = h5out_ramp.create_group(k[0])
                print 'writing >>> ' + outName_ramp

            ##### Loop
            print 'Number of interferograms: ' + str(len(igramList))
            for igram in igramList:
                print igram
                data = h5file[k[0]][igram].get(igram)[:]

                data_ramp, ramp = rm.remove_data_surface(
                    data, ramp_mask, ramp_type)
                #ramp = data_ramp - data
                data_rampCor = phase_bonding(data_ramp, Mask, x, y)
                dataCor = data_rampCor - ramp

                group = gg.create_group(igram)
                dset = group.create_dataset(igram,
                                            data=dataCor,
                                            compression='gzip')
                for key, value in h5file[k[0]][igram].attrs.iteritems():
                    group.attrs[key] = value

                if save_rampCor == 'yes':
                    group_ramp = gg_ramp.create_group(igram)
                    dset = group_ramp.create_dataset(igram,
                                                     data=data_rampCor,
                                                     compression='gzip')
                    for key, value in h5file[k[0]][igram].attrs.iteritems():
                        group_ramp.attrs[key] = value

            try:
                mask = h5file['mask'].get('mask')
                gm = h5out.create_group('mask')
                dset = gm.create_dataset('mask',
                                         data=mask[0:mask.shape[0],
                                                   0:mask.shape[1]],
                                         compression='gzip')
            except:
                print 'no mask group found.'

            h5file.close()
            h5out.close()
            if save_rampCor == 'yes':
                h5out_ramp.close()

        ########## ROI_PAC ##########
        elif ext == '.unw':
            print 'Input file is ' + ext
            a, data, atr = readfile.read_float32(File)

            data_ramp, ramp = rm.remove_data_surface(data, ramp_mask,
                                                     ramp_type)
            #ramp = data_ramp - data
            data_rampCor = phase_bonding(data_ramp, Mask, x, y)
            dataCor = data_rampCor - ramp

            writefile.write(dataCor, atr, outName)
            if save_rampCor == 'yes':
                writefile.write(data_rampCor, atr, outName_ramp)

        else:
            print 'Un-supported file type: ' + ext
            usage()
            sys.exit(1)
Esempio n. 12
0
     correlation_with_dem.py radar_8rlks.hgt  velocity_masked.h5

***********************************************************************
***********************************************************************
    '''

try:
    demFile=sys.argv[1]
    File=sys.argv[2]
except:
    Usage()
    sys.exit(1)


if os.path.basename(demFile).split('.')[1]=='hgt':
    amp,dem,demRsc = readfile.read_float32(demFile)

elif os.path.basename(demFile).split('.')[1]=='dem':
    dem,demRsc = readfile.read_real_int16(demFile)

#amp,dem,demRsc = readfile.read_float32(demFile)
h5data = h5py.File(File)
dset = h5data['velocity'].get('velocity')
data = dset[0:dset.shape[0],0:dset.shape[1]]

try:
    suby=sys.argv[3].split(':')
    subx=sys.argv[4].split(':')
    data = data[int(suby[0]):int(suby[1]),int(subx[0]):int(subx[1])]
    dem = dem[int(suby[0]):int(suby[1]),int(subx[0]):int(subx[1])]
except:
Esempio n. 13
0
def main(argv):

    if len(sys.argv) > 2:
        try:
            opts, args = getopt.getopt(argv, "h:f:x:y:o:l:L:")
        except getopt.GetoptError:
            print 'Error while getting args'
            Usage()
            sys.exit(1)

        for opt, arg in opts:
            if opt in ("-h", "--help"):
                Usage()
                sys.exit()
            elif opt == '-f':
                File = 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 == '-l':
                Latsub = [float(i) for i in arg.split(':')]
                Latsub.sort()
            elif opt == '-L':
                Lonsub = [float(i) for i in arg.split(':')]
                Lonsub.sort()

    else:
        Usage()
        sys.exit(1)

    try:
        outName
    except:
        outName = 'subset_' + File

    ext = os.path.splitext(File)[1]

    ############################################################################
    #################################  PySAR  ##################################

    if ext == '.h5':
        try:
            h5file = h5py.File(File, 'r')
        except:
            Usage()
            sys.exit(1)
        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()[0]].attrs
        elif k[0] in ('dem', 'velocity', 'mask', 'temporal_coherence', 'rmse',
                      'timeseries'):
            atr = h5file[k[0]].attrs

        ############# Subset Option #############
        width = int(atr['WIDTH'])
        length = int(atr['FILE_LENGTH'])

        try:
            Latsub
            try:
                lat_step = float(atr['Y_STEP'])
                lat1 = float(atr['Y_FIRST'])
                lat0 = lat1 + length * lat_step
                if Latsub[0] < lat0:
                    Latsub[0] = lat0
                    print 'WARNING: input latitude < min (' + str(
                        lat0) + ')! Set it to min.'
                if Latsub[1] > lat1:
                    Latsub[1] = lat1
                    print 'WARNING: input latitude > max (' + str(
                        lat1) + ')! Set it to max.'
                print 'subset in latitude - ' + str(Latsub[0]) + ':' + str(
                    Latsub[1])
                ysub = [0] * 2
                ysub[0] = int((Latsub[1] - lat1) / lat_step)
                ysub[1] = int((Latsub[0] - lat1) / lat_step)
            except:
                print 'Not geocoded file, cannot be subseted with LatLon.'
                Usage()
                return
        except:
            try:
                ysub
                if ysub[0] < 0:
                    ysub[0] = 0
                    print 'WARNING: input y < min (0)! Set it to min.'
                if ysub[1] > length:
                    ysub[1] = length
                    print 'WARNING: input y > max (' + str(
                        length) + ')! Set it to max.'
                print 'subset in y direction - ' + str(ysub[0]) + ':' + str(
                    ysub[1])
            except:
                ysub = [0, length]

        try:
            Lonsub
            try:
                lon_step = float(atr['X_STEP'])
                lon0 = float(atr['X_FIRST'])
                lon1 = lon0 + width * lon_step
                if Lonsub[0] < lon0:
                    Lonsub[0] = lon0
                    print 'WARNING: input longitude < min (' + str(
                        lon0) + ')! Set it to min.'
                if Lonsub[1] > lon1:
                    Lonsub[1] = lon1
                    print 'WARNING: input longitude > max (' + str(
                        lon1) + ')! Set it to max.'
                print 'subset in longitude - ' + str(Lonsub[0]) + ':' + str(
                    Lonsub[1])
                xsub = [0] * 2
                xsub[0] = int((Lonsub[0] - lon0) / lon_step)
                xsub[1] = int((Lonsub[1] - lon0) / lon_step)
            except:
                print 'Not geocoded file, cannot be subseted with LatLon.'
                Usage()
                return
        except:
            try:
                xsub
                if xsub[0] < 0:
                    xsub[0] = 0
                    print 'WARNING: input x < min (0)! Set it to min.'
                if xsub[1] > width:
                    xsub[1] = width
                    print 'WARNING: input x > max (' + str(
                        width) + ')! Set it to max x.'
                print 'subset in x direction - ' + str(xsub[0]) + ':' + str(
                    xsub[1])
            except:
                xsub = [0, width]

        if ysub[0] > length or ysub[1] < 0 or xsub[0] > length or xsub[1] < 0:
            print 'ERROR: input index is out of data range!'
            print 'range in rdr: x - 0:' + str(width) + '    y - 0:' + str(
                length)
            try:
                print 'range in geo: lat - ' + str(lat0) + ':' + str(
                    lat1) + '    lon - ' + str(lon0) + ':' + str(lon1)
            except:
                Geo = 0
            sys.exit(1)

        ######## Data Read, Crop and Write #######
        ##### N dset, N attributes
        if k[0] in ('interferograms', 'coherence', 'wrapped'):
            print 'writing  >>>  ' + outName
            h5out = h5py.File(outName, 'w')
            gg = h5out.create_group(k[0])

            igramList = h5file[k[0]].keys()
            for igram in igramList:
                print igram
                dset1 = h5file[k[0]][igram].get(igram)
                group = gg.create_group(igram)
                dset = group.create_dataset(igram,
                                            data=dset1[ysub[0]:ysub[1],
                                                       xsub[0]:xsub[1]],
                                            compression='gzip')

                for key, value in h5file[k[0]][igram].attrs.iteritems():
                    group.attrs[key] = value
                group.attrs['FILE_LENGTH'] = ysub[1] - ysub[0]
                group.attrs['WIDTH'] = xsub[1] - xsub[0]
                try:
                    sub_x0_ori = int(group.attrs['subset_x0'])
                    group.attrs['subset_x0'] = xsub[0] + sub_x0_ori
                    group.attrs['subset_x1'] = xsub[1] + sub_x0_ori
                except:
                    group.attrs['subset_x0'] = xsub[0]
                    group.attrs['subset_x1'] = xsub[1]
                try:
                    sub_y0_ori = int(group.attrs['subset_y0'])
                    group.attrs['subset_y0'] = ysub[0] + sub_y0_ori
                    group.attrs['subset_y1'] = ysub[1] + sub_y0_ori
                except:
                    group.attrs['subset_y0'] = ysub[0]
                    group.attrs['subset_y1'] = ysub[1]
                if 'X_FIRST' in atr.keys():
                    group.attrs['X_FIRST'] = float(
                        atr['X_FIRST']) + xsub[0] * float(atr['X_STEP'])
                    group.attrs['Y_FIRST'] = float(
                        atr['Y_FIRST']) + ysub[0] * float(atr['Y_STEP'])

            ## support of old format
            try:
                Mset = h5file['mask'].get('mask')
                gm = h5out.create_group('mask')
                dset = gm.create_dataset('mask',
                                         data=Mset[ysub[0]:ysub[1],
                                                   xsub[0]:xsub[1]],
                                         compression='gzip')
            except:
                print 'No group for mask found in the file.'
            try:
                Cset = h5file['meanCoherence'].get('meanCoherence')
                gm = h5out.create_group('meanCoherence')
                dset = gm.create_dataset('meanCoherence',
                                         data=Cset[ysub[0]:ysub[1],
                                                   xsub[0]:xsub[1]],
                                         compression='gzip')
            except:
                print 'No group for meanCoherence found in the file'

            h5file.close()
            h5out.close()

        ##### N/1 dset, 1 attributes
        elif k[0] in [
                'timeseries', 'temporal_coherence', 'velocity', 'mask', 'rmse'
        ]:
            print 'writing  >>>  ' + outName
            h5out = h5py.File(outName, 'w')
            group = h5out.create_group(k[0])

            if k[0] == 'timeseries':
                dateList = h5file[k[0]].keys()
                for d in dateList:
                    print d
                    dset1 = h5file[k[0]].get(d)
                    dset = group.create_dataset(d,
                                                data=dset1[ysub[0]:ysub[1],
                                                           xsub[0]:xsub[1]],
                                                compression='gzip')
            elif k[0] in ['temporal_coherence', 'velocity', 'mask', 'rmse']:
                dset1 = h5file[k[0]].get(k[0])
                dset = group.create_dataset(k[0],
                                            data=dset1[ysub[0]:ysub[1],
                                                       xsub[0]:xsub[1]],
                                            compression='gzip')

            ## Update attributes
            for key, value in h5file[k[0]].attrs.iteritems():
                group.attrs[key] = value
            group.attrs['FILE_LENGTH'] = ysub[1] - ysub[0]
            group.attrs['WIDTH'] = xsub[1] - xsub[0]
            try:
                sub_x0_ori = int(group.attrs['subset_x0'])
                group.attrs['subset_x0'] = xsub[0] + sub_x0_ori
                group.attrs['subset_x1'] = xsub[1] + sub_x0_ori
            except:
                group.attrs['subset_x0'] = xsub[0]
                group.attrs['subset_x1'] = xsub[1]
            try:
                sub_y0_ori = int(group.attrs['subset_y0'])
                group.attrs['subset_y0'] = ysub[0] + sub_y0_ori
                group.attrs['subset_y1'] = ysub[1] + sub_y0_ori
            except:
                group.attrs['subset_y0'] = ysub[0]
                group.attrs['subset_y1'] = ysub[1]
            if 'X_FIRST' in atr.keys():
                group.attrs['X_FIRST'] = float(
                    atr['X_FIRST']) + xsub[0] * float(atr['X_STEP'])
                group.attrs['Y_FIRST'] = float(
                    atr['Y_FIRST']) + ysub[0] * float(atr['Y_STEP'])

            h5file.close()
            h5out.close()

        else:
            print 'Error: group of HDF5 file not recogized!'
            h5file.close()
            Usage()
            sys.exit(1)

############################################################################
#########################  ROI_PAC / Image / GAMMA  ########################

    elif ext in ['.unw','.cor','.hgt','.dem','.trans'] or\
         ext in ['.jpeg','.jpg','.png','.ras','.bmp'] or\
         ext in ['.mli','.slc']:

        try:
            atr = readfile.read_rsc_file(File + '.rsc')
        except:
            try:
                atr = readfile.read_par_file(File + '.par')
            except:
                atr = readfile.read_par_file(
                    os.path.splitext(File)[0] + '.par')

        ############# Subset Option #############
        try:
            width = int(atr['WIDTH'])
            length = int(atr['FILE_LENGTH'])
        except:
            width = int(atr['range_samples:'])
            length = int(atr['azimuth_lines:'])

        try:
            Latsub
            try:
                lat_step = float(atr['Y_STEP'])
                lat1 = float(atr['Y_FIRST'])
                lat0 = lat1 + length * lat_step
                if Latsub[0] < lat0:
                    Latsub[0] = lat0
                    print 'WARNING: input latitude < min (' + str(
                        lat0) + ')! Set it to min.'
                if Latsub[1] > lat1:
                    Latsub[1] = lat1
                    print 'WARNING: input latitude > max (' + str(
                        lat1) + ')! Set it to max.'
                print 'subset in latitude - ' + str(Latsub[0]) + ':' + str(
                    Latsub[1])
                ysub = [0] * 2
                ysub[0] = int((Latsub[1] - lat1) / lat_step)
                ysub[1] = int((Latsub[0] - lat1) / lat_step)
            except:
                print 'Not geocoded file, cannot be subseted with LatLon.'
                Usage()
                return
        except:
            try:
                ysub
                if ysub[0] < 0:
                    ysub[0] = 0
                    print 'WARNING: input y < min (0)! Set it to min.'
                if ysub[1] > length:
                    ysub[1] = length
                    print 'WARNING: input y > max (' + str(
                        length) + ')! Set it to max.'
                print 'subset in y direction - ' + str(ysub[0]) + ':' + str(
                    ysub[1])
            except:
                ysub = [0, length]

        try:
            Lonsub
            try:
                lon_step = float(atr['X_STEP'])
                lon0 = float(atr['X_FIRST'])
                lon1 = lon0 + width * lon_step
                if Lonsub[0] < lon0:
                    Lonsub[0] = lon0
                    print 'WARNING: input longitude < min (' + str(
                        lon0) + ')! Set it to min.'
                if Lonsub[1] > lon1:
                    Lonsub[1] = lon1
                    print 'WARNING: input longitude > max (' + str(
                        lon1) + ')! Set it to max.'
                print 'subset in longitude - ' + str(Lonsub[0]) + ':' + str(
                    Lonsub[1])
                xsub = [0] * 2
                xsub[0] = int((Lonsub[0] - lon0) / lon_step)
                xsub[1] = int((Lonsub[1] - lon0) / lon_step)
            except:
                print 'Not geocoded file, cannot be subseted with LatLon.'
                Usage()
                return
        except:
            try:
                xsub
                if xsub[0] < 0:
                    xsub[0] = 0
                    print 'WARNING: input x < min (0)! Set it to min.'
                if xsub[1] > width:
                    xsub[1] = width
                    print 'WARNING: input x > max (' + str(
                        width) + ')! Set it to max x.'
                print 'subset in x direction - ' + str(xsub[0]) + ':' + str(
                    xsub[1])
            except:
                xsub = [0, width]

        if ysub[0] > length or ysub[1] < 0 or xsub[0] > length or xsub[1] < 0:
            print 'ERROR: input index is out of data range!'
            print 'range in rdr: x - 0:' + str(width) + '    y - 0:' + str(
                length)
            try:
                print 'range in geo: lat - ' + str(lat0) + ':' + str(
                    lat1) + '    lon - ' + str(lon0) + ':' + str(lon1)
            except:
                Geo = 0
            sys.exit(1)

        ######## Data Read, Crop and Write #######
        print 'writing >>> ' + outName
        box = (xsub[0], ysub[0], xsub[1], ysub[1])
        if ext in ['.unw', '.cor', '.hgt']:
            a, p, r = readfile.read_float32(File, box)
            #p = p[ysub[0]:ysub[1],xsub[0]:xsub[1]]
            writefile.write_float32(p, outName)
        elif ext == '.dem':
            p, r = readfile.read_dem(File)
            p = p[ysub[0]:ysub[1], xsub[0]:xsub[1]]
            writefile.write_dem(p, outName)
        elif ext == '.trans':
            a, p, r = readfile.read_float32(File, box)
            #a = a[ysub[0]:ysub[1],xsub[0]:xsub[1]]
            #p = p[ysub[0]:ysub[1],xsub[0]:xsub[1]]
            writefile.write_float32(a, p, outName)
        elif ext in ['.jpeg', '.jpg', '.png', '.ras', '.bmp']:
            import Image
            im = Image.open(File)
            box = (xsub[0], ysub[0], xsub[1], ysub[1])
            output_img = im.crop(box)
            output_img.save(outName)
        elif ext == '.mli':
            d, r = readfile.read_gamma_float(File)
            d = d[ysub[0]:ysub[1], xsub[0]:xsub[1]]
            writefile.write_gamma_float(d, outName)
        elif ext == '.slc':
            d, r = readfile.read_gamma_scomplex(File, box)
            writefile.write_gamma_scomplex(d, outName)

        ########### Update .rsc file #############
        atr['FILE_LENGTH'] = str(ysub[1] - ysub[0])
        atr['WIDTH'] = str(xsub[1] - xsub[0])
        atr['XMAX'] = str(width - 1)
        atr['YMAX'] = str(length - 1)
        try:
            sub_x0_ori = int(atr['subset_x0'])
            atr['subset_x0'] = str(xsub[0] + sub_x0_ori)
            atr['subset_x1'] = str(xsub[1] + sub_x0_ori)
        except:
            atr['subset_x0'] = str(xsub[0])
            atr['subset_x1'] = str(xsub[1])
        try:
            sub_y0_ori = int(atr['subset_y0'])
            atr['subset_y0'] = str(ysub[0] + sub_y0_ori)
            atr['subset_y1'] = str(ysub[1] + sub_y0_ori)
        except:
            atr['subset_y0'] = str(ysub[0])
            atr['subset_y1'] = str(ysub[1])
        if 'X_FIRST' in atr.keys():
            atr['Y_FIRST'] = str(
                float(atr['Y_FIRST']) + ysub[0] * float(atr['Y_STEP']))
            atr['X_FIRST'] = str(
                float(atr['X_FIRST']) + xsub[0] * float(atr['X_STEP']))

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

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

    else:
        print 'File extension not recogized.'
        Usage()
        sys.exit(1)
Esempio n. 14
0
def main(argv):
    plt.switch_backend('Agg')

    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_attribute(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 = mpl.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)
Esempio n. 15
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
Esempio n. 16
0
def main(argv):

    #lineWidth=4
    #fontSize=32
    #markerColor='orange'
    #markerSize=20
    #if len(sys.argv)>2:

    try:
        opts, args = getopt.getopt(argv, "h:f:m:t:x:y:o:")
    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 == '-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':
            MaskedFile = arg

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

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

    ext = os.path.splitext(File)[1]
    import h5py
    import numpy as np

    ################################  PySAR HDF5  #################################

    if ext == '.h5':
        h5file = h5py.File(File, 'r')
        h5mask = h5py.File(maskFile, 'r')
        kf = h5file.keys()

        if 'coherence' not in h5mask.keys():
            Mset = h5mask[h5mask.keys()[0]].get(h5mask.keys()[0])
            M = Mset[0:Mset.shape[0], 0:Mset.shape[1]]

        try:
            MaskedFile
        except:
            MaskedFile = File.split('.')[0] + '_masked.h5'
        h5file2 = h5py.File(MaskedFile, 'w')

        if len(kf) == 1 and kf[0] in ('velocity', 'temporal_coherence', 'rmse',
                                      'mask'):
            Vset = h5file[h5file.keys()[0]].get(h5file.keys()[0])
            V = Vset[0:Vset.shape[0], 0:Vset.shape[1]]

            try:
                xsub
                ysub
                M[ysub[0]:ysub[1], xsub[0]:xsub[1]] = 0
            except:
                print 'No subset'

            try:
                V[M < thr] = np.nan
            except:
                V[M == 0] = np.nan

            group = h5file2.create_group(kf[0])
            dset = group.create_dataset(os.path.basename(kf[0]),
                                        data=V,
                                        compression='gzip')

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

        elif 'timeseries' in h5file.keys():
            print 'Masking the time-series'
            group = h5file2.create_group('timeseries')
            dateList = h5file['timeseries'].keys()
            for d in dateList:
                print d
                unwset = h5file['timeseries'].get(d)
                unw = unwset[0:unwset.shape[0], 0:unwset.shape[1]]

                try:
                    unw[M < thr] = np.nan
                except:
                    unw[M == 0] = np.nan

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

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

        elif kf[0] in ('interferograms',
                       'wrapped') and 'coherence' in h5mask.keys():
            print 'Masking each ' + kf[0] + ' using its coherence file'
            igramList = h5file[kf[0]].keys()
            cohList = h5mask['coherence'].keys()
            gg = h5file2.create_group(kf[0])
            for igram in igramList:
                print igram
                date12 = h5file[kf[0]][igram].attrs['DATE12']
                for cohFile in cohList:
                    if h5mask['coherence'][cohFile].attrs['DATE12'] == date12:
                        igramCoh = cohFile
                print igramCoh

                unwset = h5file[kf[0]][igram].get(igram)
                unw = unwset[0:unwset.shape[0], 0:unwset.shape[1]]

                cohset = h5mask['coherence'][igramCoh].get(igramCoh)
                coh = cohset[0:cohset.shape[0], 0:cohset.shape[1]]
                unw[coh < thr] = np.nan
                group = gg.create_group(igram)
                dset = group.create_dataset(igram,
                                            data=unw,
                                            compression='gzip')
                for key, value in h5file[kf[0]][igram].attrs.iteritems():
                    group.attrs[key] = value
            try:
                #if kf[0] == 'interferograms':
                mask = h5file['mask'].get('mask')
                gm = h5file2.create_group('mask')
                dset = gm.create_dataset('mask', data=mask, compression='gzip')
            except:
                print 'no mask group found.'

        elif kf[0] in ('interferograms',
                       'wrapped') and 'coherence' not in h5mask.keys():
            print 'Masking the ' + kf[0]
            igramList = h5file[kf[0]].keys()
            gg = h5file2.create_group(kf[0])
            for igram in igramList:
                print igram
                unwset = h5file[kf[0]][igram].get(igram)
                unw = unwset[0:unwset.shape[0], 0:unwset.shape[1]]
                try:
                    unw[M < thr] = np.nan
                except:
                    unw[M == 0] = np.nan

                group = gg.create_group(igram)
                dset = group.create_dataset(igram,
                                            data=unw,
                                            compression='gzip')
                for key, value in h5file[kf[0]][igram].attrs.iteritems():
                    group.attrs[key] = value

            try:
                #if kf[0] == 'interferograms':
                mask = h5file['mask'].get('mask')
                gm = h5file2.create_group('mask')
                dset = gm.create_dataset('mask', data=mask, compression='gzip')
            except:
                print 'no mask group found.'

        h5file.close()
        h5mask.close()
        h5file2.close()

    ################################  ROI_PAC  ######################################

    elif ext in ['.unw', '.int']:
        import pysar._readfile as readfile
        import pysar._writefile as writefile
        Mext = os.path.splitext(maskFile)[1]
        if Mext == '.cor':
            a, M, atr = readfile.read_float32(maskFile)
        elif Mext == '.h5':
            h5mask = h5py.File(maskFile, 'r')
            Mset = h5mask[h5mask.keys()[0]].get(h5mask.keys()[0])
            M = Mset[0:Mset.shape[0], 0:Mset.shape[1]]
        else:
            sys.exit(['Un-recoganized mask file extension: ' + Mext])

        try:
            MaskedFile
        except:
            MaskedFile = File.split('.')[0] + '_masked' + ext

        a, unw, atr = readfile.read_float32(File)
        try:
            unw[M < thr] = np.nan
        except:
            unw[M == 0] = np.nan

        print 'writting >>> ' + MaskedFile
        writefile.write_float32(unw, MaskedFile)
        f = open(MaskedFile + '.rsc', 'w')
        for k in atr.keys():
            f.write(k + '    ' + atr[k] + '\n')
        f.close()

        try:
            h5mask.close()
        except:
            pass
Esempio n. 17
0
def main(argv):

  #################  default values  ################
  flip_lr='no'
  flip_ud='no'
  disp_geo = 'yes'
  #font_size=8
  color_map='jet'
  figs_rows=5
  figs_cols=8
  rewrapping='yes'
  allData2display='yes'
  Wspace = 0.1
  Hspace = 0.1
  title = 'out'
  showRef = 'yes'
  ref_color='k'
  ref_symbol='s'
  ref_size =10
  dip_opposite = 'no'
  saveFig='no'
  dispFig='yes'
  dispContour='only'
  contour_step=200
  contour_sigma=3.0
  fig_dpi=300

  ###################  get input args  ###############
  if len(sys.argv)>2:
     try:
        opts, args = getopt.getopt(argv,"h:D:O:G:S:f:m:M:v:u:s:c:e:d:r:p:w:i:j:t:R:a:b:k:x:y:C:V:P:o:g:l:L:")
     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 == '-D': demFile=arg
        elif opt == '-w': rewrapping = arg
        elif opt == '-m': min = float(arg);         rewrapping='no'
        elif opt == '-M': max = float(arg);         rewrapping='no'
        elif opt == '-v': flip_lr = arg
        elif opt == '-u': flip_ud = arg
        elif opt == '-s': font_size = int(arg)
        elif opt == '-c': color_map = arg
        elif opt == '-e': epoch_number = int(arg);  allData2display='no'
        elif opt == '-d': epoch_date = arg;         allData2display='no'
        elif opt == '-r': figs_rows = int(arg)
        elif opt == '-p': figs_cols = int(arg)
        elif opt == '-i': Wspace = float(arg)
        elif opt == '-j': Hspace = float(arg)
        elif opt == '-t': title = arg
        elif opt == '-R': showRef = arg
        elif opt == '-a': ref_color = arg
        elif opt == '-b': ref_symbol = arg
        elif opt == '-k': ref_size=int(arg)
        elif opt == '-x': win_x = [int(i) for i in arg.split(':')];      win_x.sort()
        elif opt == '-y': win_y = [int(i) for i in arg.split(':')];      win_y.sort()
        elif opt == '-G': disp_geo = arg
        elif opt == '-O': dip_opposite=arg
        elif opt == '-S': saveFig=arg 
        elif opt == '-C': dispContour=arg
        elif opt == '-V': contour_step=float(arg)
        elif opt == '-P': dispFig=arg
        elif opt == '-o': figName=arg
        elif opt == '-g': contour_sigma = float(arg)
        elif opt == '-l': win_lat = [float(i) for i in arg.split(':')];  win_lat.sort()
        elif opt == '-L': win_lon = [float(i) for i in arg.split(':')];  win_lon.sort()

  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)

  if color_map == 'hsv':
     ################################################
     cdict1 = {'red':   ((0.0, 0.0, 0.0),
                   (0.5, 0.0, 0.0),
                   (0.6, 1.0, 1.0),
                   (0.8, 1.0, 1.0),
                   (1.0, 0.5, 0.5)),
        
         'green': ((0.0, 0.0, 0.0),
                   (0.2, 0.0, 0.0),
                   (0.4, 1.0, 1.0),
                   (0.6, 1.0, 1.0),
                   (0.8, 0.0, 0.0),
                   (1.0, 0.0, 0.0)),
      
         'blue':  ((0.0, 0.5, .5),
                   (0.2, 1.0, 1.0),
                   (0.4, 1.0, 1.0),
                   (0.5, 0.0, 0.0),
                   (1.0, 0.0, 0.0),)
        }

     from matplotlib.colors import LinearSegmentedColormap
     ccmap = LinearSegmentedColormap('BlueRed1', cdict1)
        
     ################################################
  else:  ccmap=plt.get_cmap(color_map)


  ##################################################
  ext = os.path.splitext(File)[1]
  if ext == '.h5':
     import h5py
     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'
     print 'Input: '+str(k)
     if k[0] in ('dem','velocity','mask','temporal_coherence','rmse'):
        allData2display = 'no'

  elif ext in ['.unw','.int','.cor','.hgt','.dem','.trans','.mli','.slc']:
     import pysar._readfile as readfile
     allData2display = 'no'
     k = [ext]
  else: 
     print 'File extension not recogized: '+ext
     print 'Support file format:\n\
                PySAR HDF5 files: velocity.h5, timeseries.h5, LoadedData.h5, ...\n\
                ROI_PAC    files: .unw .cor .int .hgt .dem .trans .mli'
     sys.exit(1)


####################################################################
########################## Plot One ################################

  if allData2display == 'no':
    try:    font_size
    except: font_size=12

    ################# File Reading ##################
    ##### PySAR HDF5
    if k[0] in ('dem','velocity','mask','temporal_coherence','rmse'):
       atr  = h5file[k[0]].attrs
       dset = h5file[k[0]].get(k[0])
       data = dset[0:dset.shape[0],0:dset.shape[1]]
       # rewrapping
       if rewrapping in ('yes','Yes','Y','y','YES'):
          print 'Rewrapping disabled for '+k[0]

    elif k[0] == 'timeseries':
       dateList=h5file[k[0]].keys()
       try:
          epoch_number
       except:
          try:
             epoch_date
             if len(epoch_date)==6:  epoch_date=yymmdd2yyyymmdd(epoch_date)
             epoch_number=dateList.index(epoch_date)
          except:
             print 'Unrecognized epoch input!';  sys.exit(1)
       print 'Displaying date: '+dateList[epoch_number]
       atr  = h5file[k[0]].attrs
       dset = h5file[k[0]].get(dateList[epoch_number])
       data = dset[0:dset.shape[0],0:dset.shape[1]]
       # rewrapping
       if rewrapping in ('yes','Yes','y','Y','YES'):
          print 'Rewrapping. Set min/max to -pi/pi. Showing phase'
          range2phase=4*np.pi/float(atr['WAVELENGTH'])         #double-way, 2*2*pi/lamda
          data=range2phase*data
          data=rewrap(data)
          min = -np.pi
          max = np.pi
          figUnit='(radian)'
       elif rewrapping in ('no','No','N','n','NO'):
          print 'No rewrapping. Showing displacement.'
          figUnit='(m)'

    elif k[0] in ('interferograms','coherence','wrapped'):
       ifgramList=h5file[k[0]].keys()
       try:
          epoch_number
       except:
          for i in range(len(ifgramList)):
             if epoch_date in ifgramList[i]:   epoch_number = i
       print 'Displaying: '+ifgramList[epoch_number]
       atr  = h5file[k[0]][ifgramList[epoch_number]].attrs
       dset = h5file[k[0]][ifgramList[epoch_number]].get(ifgramList[epoch_number])
       data = dset[0:dset.shape[0],0:dset.shape[1]]

       # rewrapping
       if k[0] in ('coherence','wrapped') and rewrapping in ('yes','Yes','y','Y','YES'):
          print 'No rewrapping for coherence/wrapped files, set to "no"'
          rewrapping='no'
       if rewrapping in ('yes','Yes','y','Y','YES'):
          print 'Rewrapping. Set min/max to -pi/pi.'
          data = np.angle(np.exp(1j*data))
          min  = -np.pi
          max  = np.pi

    ##### ROI_PAC product
    elif k[0] in ['.slc','.mli']:
       data,p,atr = readfile.read_complex64(File)
       data= np.nanlog10(data)
       figUnit = '(dB)'
    elif k[0] == '.int':
       a,data,atr = readfile.read_complex64(File)
       min = -np.pi
       max =  np.pi
       rewrapping = 'no'
       figUnit = '(radian)'
    elif k[0] in ['.unw', '.cor', '.hgt', '.trans']:
       a,data,atr = readfile.read_float32(File)
       if   k[0] == '.unw':   figUnit = '(radian)'
       elif k[0] == '.hgt':   figUnit = '(m)'
       if k[0] in ['.cor','.hgt','.trans']: rewrapping='no'
       if rewrapping in ('yes','Yes','y','Y','True','true'):
          print 'Rewrapping. Set min/max to -pi/pi.'
          data = rewrap(data)
          min = -np.pi
          max =  np.pi
    elif k[0] == '.dem':
       data,atr = readfile.read_dem(File)
       figUnit = '(m)'


    ############## Data Option ##################
    # Opposite Sign
    if dip_opposite in ('yes','Yes','Y','y','YES'):
       data=-1*data
    # Subset
    try:      # y/latitude direction
      win_lat
      try:
        atr['Y_FIRST']
        win_y=[0]*2
        win_y[0]=int((win_lat[1]-float(atr['Y_FIRST']))/float(atr['Y_STEP']))
        win_y[1]=int((win_lat[0]-float(atr['Y_FIRST']))/float(atr['Y_STEP']))
        if win_y[0]<0: win_y[0]=0; print 'input latitude > max latitude! Set to max'
        print 'subset in latitude  - '+str(win_lat[0])+':'+str(win_lat[1])
      except:  print 'Non-geocoded file, cannot use LatLon option';   Usage(); sys.exit(1)
    except:
      try:
        win_y
        print 'subset in y direction - '+str(win_y[0])+':'+str(win_y[1])
      except: win_y = [0,int(atr['FILE_LENGTH'])]
    try:      # x/longitude direction
      win_lon
      try:
        atr['X_FIRST']
        win_x=[0]*2
        win_x[0]=int((win_lon[0]-float(atr['X_FIRST']))/float(atr['X_STEP']))
        win_x[1]=int((win_lon[1]-float(atr['X_FIRST']))/float(atr['X_STEP']))
        if win_x[0]<0: win_x[0]=0; print 'input longitude > max longitude! Set to max'
        print 'subset in longitude - '+str(win_lon[0])+':'+str(win_lon[1])
      except:  print 'Non-geocoded file, cannot use LatLon option';   Usage(); sys.exit(1)
    except:
      try:
        win_x
        print 'subset in x direction - '+str(win_x[0])+':'+str(win_x[1])
      except: win_x = [0,int(atr['WIDTH'])]

    data = data[win_y[0]:win_y[1],win_x[0]:win_x[1]]

    # Reference Point
    try:
       xref=atr['ref_x']-win_x[0]
       yref=atr['ref_y']-win_y[0]
    except:  print 'No reference point'
    try:
       xref=xref-atr['subset_x0']
       yref=yref-atr['subset_y0']
    except:  print 'No subset'
    # Geo coordinate
    try:
       lon_step = float(atr['X_STEP'])
       lat_step = float(atr['Y_STEP'])
       lon_unit = atr['Y_UNIT']
       lat_unit = atr['X_UNIT']
       ullon     = float(atr['X_FIRST'])+win_x[0]*lon_step
       ullat     = float(atr['Y_FIRST'])+win_y[0]*lat_step
       llcrnrlon = ullon
       llcrnrlat = ullat+lat_step*data.shape[0]
       urcrnrlon = ullon+lon_step*data.shape[1]
       urcrnrlat = ullat
       geocoord='yes'
       print 'Input file is Geocoded'
    except:  geocoord='no'
    # Flip
    if flip_lr in ('yes','Yes','Y','y','YES'):  data=np.fliplr(data);  xref=np.shape(data)[1]-xref-1 
    if flip_ud in ('yes','Yes','Y','y','YES'):  data=np.flipud(data);  yref=np.shape(data)[0]-yref-1

    # Colorbar Extend
    data_min = np.nanmin(data)
    data_max = np.nanmax(data)
    try:    min
    except: min = data_min
    try:    max
    except: max = data_max
    if   min <= data_min and max >= data_max: cb_extend='neither'
    elif min >  data_min and max >= data_max: cb_extend='min'
    elif min <= data_min and max <  data_max: cb_extend='max'
    else:                                     cb_extend='both'

    ############## DEM Option ##################
    try:
       demFile
       print 'Show topography'
       import pysar._readfile as readfile
       if   os.path.basename(demFile).split('.')[1]=='hgt':  amp,dem,demRsc = readfile.read_float32(demFile)
       elif os.path.basename(demFile).split('.')[1]=='dem':      dem,demRsc = readfile.read_dem(demFile)

       # Subset
       dem = dem[win_y[0]:win_y[1],win_x[0]:win_x[1]]
       # Flip
       if flip_lr in ('yes','Yes','Y','y','YES'):  dem=np.fliplr(dem)
       if flip_ud in ('yes','Yes','Y','y','YES'):  dem=np.flipud(dem)

       # DEM data preparation
       if dispContour in ('no','No','n','N','NO','yes','Yes','y','Y','YES'):           #DEM basemap
          print 'plot DEM as basemap'
          cmap_dem=plt.get_cmap('gray')
          import pysar._pysar_utilities as ut
       if dispContour in ('only','Only','o','O','ONLY','yes','Yes','y','Y','YES'):     #contour
          print 'plot contour'
          #if smoothContour in ('yes','Yes','y','Y','YES'):
          import scipy.ndimage as ndimage
          dem=ndimage.gaussian_filter(dem,sigma=contour_sigma,order=0)
          contour_sequence=np.arange(-6000,9000,contour_step)
    except:  print 'No DEM file'

    ############## Data Plot and Output  ################
    # Figure Title
    if   k[0]=='velocity':                    figTitle = 'Velocity (m/yr)'
    elif k[0]=='temporal_coherence':          figTitle = 'Temporal coherence'
    elif k[0]=='dem':                         figTitle = 'DEM error'
    elif k[0]=='rmse':                        figTitle = 'RMSE (m/yr)'
    elif k[0]=='mask':                        figTitle = 'Pixels with no valid value.'
    elif k[0]=='coherence':                   figTitle = ifgramList[epoch_number]
    elif k[0]in('interferograms','wrapped'):  figTitle = ifgramList[epoch_number]+' (radian)'
    elif k[0]=='timeseries':
       try:    master_date=atr['ref_date']
       except: master_date=atr['DATE']
       if len(master_date)==6:     master_date=yymmdd2yyyymmdd(master_date)
       if dip_opposite in ('yes','Yes','Y','y','YES'): figTitle = dateList[epoch_number]+'_'+master_date+' '+figUnit
       else:                                           figTitle = master_date+'_'+dateList[epoch_number]+' '+figUnit
    elif k[0] in ['.unw','.cor','.hgt','.dem','.trans','.mli','.slc']:
       try:    figTitle = File+' '+figUnit
       except: figTitle = File

    # Plot in Geo-coordinate: plot in map
    if geocoord == 'yes' and disp_geo in ('yes','Yes','y','Y','YES'):
       print 'display Lat/Lon'
       fig = plt.figure()
       ax = fig.add_axes([0.1,0.1,0.8,0.8])
       try: plt.title(figTitle,fontsize=font_size)
       except: pass

       # Map - DEM - Data
       from mpl_toolkits.basemap import Basemap
       m = Basemap(llcrnrlon=llcrnrlon, llcrnrlat=llcrnrlat, urcrnrlon=urcrnrlon, urcrnrlat=urcrnrlat,
                   resolution='l', area_thresh=1., projection='cyl',suppress_ticks=False,ax=ax)
       try:
          demFile
          if dispContour in ('no','No','n','N','NO','yes','Yes','y','Y','YES'):
             m.imshow(ut.hillshade(np.flipud(dem),50.0),cmap=cmap_dem)
          if dispContour in ('only','Only','o','O','ONLY','yes','Yes','y','Y','YES'):
             import numpy.matlib
             c_x = np.linspace(llcrnrlon,urcrnrlon,num=dem.shape[1],endpoint='FALSE').reshape(1,dem.shape[1])
             c_xx= np.matlib.repmat(c_x,dem.shape[0],1)
             c_y = np.linspace(llcrnrlat,urcrnrlat,num=dem.shape[0],endpoint='FALSE').reshape(dem.shape[0],1)
             c_yy= np.matlib.repmat(c_y,1,dem.shape[1])
             m.contour(c_xx,c_yy,np.flipud(dem),contour_sequence,origin='lower',colors='black',alpha=0.5,latlon='FALSE')
       except:  pass
       try:     im = m.imshow(np.flipud(data),cmap=ccmap,vmin=min,vmax=max)
       except:  im = m.imshow(np.flipud(data),cmap=ccmap)

       # Reference Point
       if showRef in ('yes','Yes','Y','y','YES'):
          try:
             refPoint=ref_color+ref_symbol
             ref_lon = llcrnrlon + xref*lon_step
             ref_lat = urcrnrlat + yref*lat_step
             plt.plot(ref_lon,ref_lat,refPoint,ms=ref_size)
          except:  print 'No reference point'

       # Colorbar
       from mpl_toolkits.axes_grid1 import make_axes_locatable
       divider = make_axes_locatable(ax)
       cax = divider.append_axes("right",size="5%", pad=0.30)
       plt.colorbar(im,cax=cax,extend=cb_extend)
       #plt.colorbar(im,cax=cax)

       # Status bar
       def format_coord(x,y):
         col = int((x-ullon)/lon_step+0.5)
         row = int((y-ullat)/lat_step+0.5)
         if col>=0 and col<=data.shape[1] and row >=0 and row<=data.shape[0]:
            z = data[row,col]
            return 'lat=%.4f,  lon=%.4f,  value=%.4f'%(x,y,z)
         else:
            return 'lat=%.4f,  lon=%.4f'%(x,y)
       ax.format_coord = format_coord


    # Plot in x/y coordinate: row and column
    else:
       fig = plt.figure()
       ax = fig.add_axes([0.1,0.1,0.8,0.8])
       try: plt.title(figTitle,fontsize=font_size)
       except: pass

       # Plot
       try:
          demFile
          if dispContour in ('no','No','n','N','NO','yes','Yes','y','Y','YES'):
             ax.imshow(ut.hillshade(dem,50.0),cmap=cmap_dem)
          if dispContour in ('only','Only','o','O','ONLY','yes','Yes','y','Y','YES'):
             ax.contour(dem,contour_sequence,origin='lower',colors='black',alpha=0.5)
       except:  pass
       try:     im = ax.imshow(data,cmap=ccmap, vmin=min, vmax=max)
       except:  im = ax.imshow(data,cmap=ccmap)
       cbar = plt.colorbar(im,extend=cb_extend)
       #cbar.set_label('m/yr')

       # Reference Point
       if showRef in ('yes','Yes','Y','y','YES'):
          try:
             refPoint=ref_color+ref_symbol
             ax.plot(xref,yref,refPoint,ms=ref_size)
          except:  print 'No reference point'

       plt.xlim(0,np.shape(data)[1])
       plt.ylim(  np.shape(data)[0],0)

       # Status bar
       def format_coord(x,y):
         col = int(x+0.5)
         row = int(y+0.5)
         if col>=0 and col<=data.shape[1] and row >=0 and row<=data.shape[0]:
            z = data[row,col]
            return 'x=%.4f,  y=%.4f,  value=%.4f'%(x,y,z)
         else:
            return 'x=%.4f,  y=%.4f'%(x,y)
       ax.format_coord = format_coord

    # Save Figure
    if saveFig in ('yes','Yes','Y','y','YES'):
       try:  figName
       except:
          if   k[0]=='velocity':            figName='velocity.pdf'
          elif k[0]=='temporal_coherence':  figName='temporal_coherence.pdf'
          elif k[0]=='dem':                 figName='DEM_error.pdf'
          elif k[0]=='rmse':                figName='rmse.pdf'
          elif k[0]=='mask':                figName='mask.pdf'
          elif k[0]=='timeseries':
             figName=os.path.basename(File).split('.')[0]+'_'+dateList[epoch_number]+'.pdf'
          elif k[0] in ('interferograms','coherence','wrapped'):
             figName=ifgramList[epoch_number]+'.pdf'
          elif k[0] in ['.unw','.cor','.hgt','.dem','.trans','.mli','.slc']:
             figName=File+'.pdf'
       plt.savefig(figName,dpi=fig_dpi)
       print 'Saved figure to '+figName

    # Show Figure
    if dispFig in ('yes','Yes','Y','y','YES'):  
       plt.show()
    
####################################################################
########################## Plot All ################################  

  elif allData2display == 'yes':
    try:    font_size
    except: font_size=8

    if k[0] == 'timeseries':
       atr = h5file[k[0]].attrs
       # rewrapping
       if rewrapping in ('yes','Yes','Y','y','YES'):
          print 'Rewrapping. Set min/max to -pi/pi. Showing phase.' 
          range2phase=4*np.pi/float(h5file['timeseries'].attrs['WAVELENGTH'])
          min=-np.pi
          max=np.pi
       else:  print 'No rewrapping. Showing displacement.'

    elif k[0] in ('interferograms','coherence','wrapped'):
       atr = h5file[k[0]][h5file[k[0]].keys()[0]].attrs
       # rewrapping
       if k[0] in ('coherence','wrapped') and rewrapping in ('yes','Yes','y','Y','YES'):
          print 'No rewrapping for coherence/wrapped files, set to "no"'
          rewrapping='no'
       if rewrapping in ('yes','Yes','Y','y','YES'):
          print 'Rewrapping. Set min/max to -pi/pi.'
          min=-np.pi
          max=np.pi
       else:  print 'No rewrapping'

    ### Subset Option ###
    try:      # y/latitude direction
      win_lat
      try:
        atr['Y_FIRST']
        win_y=[0]*2
        win_y[0]=int((win_lat[1]-float(atr['Y_FIRST']))/float(atr['Y_STEP']))
        win_y[1]=int((win_lat[0]-float(atr['Y_FIRST']))/float(atr['Y_STEP']))
        if win_y[0]<0: win_y[0]=0; print 'input latitude > max latitude! Set to max'
        print 'subset in latitude  - '+str(win_lat[0])+':'+str(win_lat[1])
      except:  print 'Non-geocoded file, cannot use LatLon option';   Usage(); sys.exit(1)
    except:
      try:
        win_y
        print 'subset in y direction - '+str(win_y[0])+':'+str(win_y[1])
      except: win_y = [0,int(atr['FILE_LENGTH'])]
    try:      # x/longitude direction
      win_lon
      try:
        atr['X_FIRST']
        win_x=[0]*2
        win_x[0]=int((win_lon[0]-float(atr['X_FIRST']))/float(atr['X_STEP']))
        win_x[1]=int((win_lon[1]-float(atr['X_FIRST']))/float(atr['X_STEP']))
        if win_x[0]<0: win_x[0]=0; print 'input longitude > max longitude! Set to max'
        print 'subset in longitude - '+str(win_lon[0])+':'+str(win_lon[1])
      except:  print 'Non-geocoded file, cannot use LatLon option';   Usage(); sys.exit(1)
    except:
      try:
        win_x
        print 'subset in x direction - '+str(win_x[0])+':'+str(win_x[1])
      except: win_x = [0,int(atr['WIDTH'])]

    # Figure Name
    if saveFig in ('yes','Yes','Y','y','YES'):
       try:
          figName
          figNameBase = os.path.basename(figName).split('.')[0]
          figNameExt  = os.path.basename(figName).split('.')[1]
       except:
          figNameBase = os.path.basename(File).split('.')[0]
          figNameExt  = '.pdf'

    ################## DEM Options ####################
    try:
       demFile
       print 'Show topography'
       import pysar._readfile as readfile
       if   os.path.basename(demFile).split('.')[1]=='hgt':  amp,dem,demRsc = readfile.read_float32(demFile)
       elif os.path.basename(demFile).split('.')[1]=='dem':      dem,demRsc = readfile.read_dem(demFile)

       # Subset
       dem = dem[win_y[0]:win_y[1],win_x[0]:win_x[1]]

       if dispContour in ('no','No','n','N','NO','yes','Yes','y','Y','YES'):           #DEM basemap
          print 'plot DEM as basemap'
          cmap_dem=plt.get_cmap('gray')
          import pysar._pysar_utilities as ut
          hillshade_dem=ut.hillshade(dem,50.0)
       if dispContour in ('only','Only','o','O','ONLY','yes','Yes','y','Y','YES'):     #contour
          print 'plot contour'
          #if smoothContour in ('yes','Yes','y','Y','YES'):
          import scipy.ndimage as ndimage
          dem=ndimage.gaussian_filter(dem,sigma=contour_sigma,order=0)
          contour_sequence=np.arange(-6000,9000,contour_step)
    except:  print 'No DEM file'

    ################## Plot Loop ####################
    ifgramList=h5file[k[0]].keys()
    nfigs   = figs_rows*figs_cols
    lifgram = len(ifgramList)
    print 'number of  epochs/interferograms to display:'+ str(lifgram)
    kk=int(lifgram/nfigs)+1
    ii=0

    # plot (1,end-1) figures
    for j in range(1,kk):
       fig = plt.figure(j)
       ii=(j-1)*nfigs+1
       for i in range(ii,ii+nfigs):
           print 'loading '+ifgramList[i-1]
           ax = fig.add_subplot(figs_rows,figs_cols,i-ii+1) 

           # Data option
           if k[0] == 'timeseries':
              figTitle = ifgramList[i-1]
              dset = h5file[k[0]].get(ifgramList[i-1])
              data = dset[0:dset.shape[0],0:dset.shape[1]]
              if rewrapping in ('yes','Yes','Y','y','YES'):
                 data=range2phase*data
                 data=rewrap(data)
           elif k[0] in ('interferograms','coherence','wrapped'):
              figTitle = str(i)+' : '+h5file[k[0]][ifgramList[i-1]].attrs['DATE12']
              dset = h5file[k[0]][ifgramList[i-1]].get(ifgramList[i-1])
              data = dset[0:dset.shape[0],0:dset.shape[1]]
              if rewrapping in ('yes','Yes','Y','y','YES'):
                 data=np.angle(np.exp(1j*data))

           data = data[win_y[0]:win_y[1],win_x[0]:win_x[1]]

           # Plot
           try:
              demFile
              if dispContour in ('no','No','n','N','NO','yes','Yes','y','Y','YES'):
                 plt.imshow(hillshade_dem,cmap=cmap_dem)
              if dispContour in ('only','Only','o','O','ONLY','yes','Yes','y','Y','YES'):
                 plt.contour(dem,contour_sequence,origin='lower',colors='black',alpha=0.5)
           except:  pass
           try:     ax.imshow(data,cmap=ccmap,vmin=min,vmax=max)
           except:  ax.imshow(data,cmap=ccmap)

           ax.set_yticklabels([])
           ax.set_xticklabels([])
           ax.set_xticks([])
           ax.set_yticks([])
           if   title=='out':  ax.set_title(figTitle,fontsize=font_size)
           elif title=='in':   add_inner_title(ax, figTitle, loc=1)
       fig.subplots_adjust(wspace=Wspace,hspace=Hspace)
       if saveFig in ('yes','Yes','Y','y','YES'):   
           figName=figNameBase+'_'+str(j)+figNameExt
           plt.savefig(figName,dpi=fig_dpi)

    # plot the last figure
    fig = plt.figure(kk)
    ii=(kk-1)*nfigs+1
    for i in range(ii,lifgram+1):
           print 'loading '+ifgramList[i-1]
           ax = fig.add_subplot(figs_rows,figs_cols,i-ii+1)

           # Data option
           if k[0] == 'timeseries':
              figTitle = ifgramList[i-1]
              dset = h5file[k[0]].get(ifgramList[i-1])
              data = dset[0:dset.shape[0],0:dset.shape[1]]
              if rewrapping in ('yes','Yes','Y','y','YES'):
                 data=range2phase*data
                 data=rewrap(data)
           elif k[0] in ('interferograms','coherence','wrapped'):
              figTitle = str(i)+' : '+h5file[k[0]][ifgramList[i-1]].attrs['DATE12']
              dset = h5file[k[0]][ifgramList[i-1]].get(ifgramList[i-1])
              data = dset[0:dset.shape[0],0:dset.shape[1]]
              if rewrapping in ('yes','Yes','Y','y','YES'):
                 data=np.angle(np.exp(1j*data))

           data = data[win_y[0]:win_y[1],win_x[0]:win_x[1]]

           # Plot
           try:
              demFile
              if dispContour in ('no','No','n','N','NO','yes','Yes','y','Y','YES'):
                 plt.imshow(hillshade_dem,cmap=cmap_dem)
              if dispContour in ('only','Only','o','O','ONLY','yes','Yes','y','Y','YES'):
                 plt.contour(dem,contour_sequence,origin='lower',colors='black',alpha=0.5)
           except:  pass

           try:     ax.imshow(data,cmap=ccmap,vmin=min,vmax=max)
           except:  ax.imshow(data,cmap=ccmap)

           ax.xaxis.label.set_fontsize(20)
           ax.set_yticklabels([])
           ax.set_xticklabels([])
           ax.set_xticks([])
           ax.set_yticks([])
           if    title=='out':  ax.set_title(figTitle,fontsize=font_size)
           elif title =='in':   add_inner_title(ax, figTitle, loc=1)
    fig.subplots_adjust(wspace=Wspace,hspace=Hspace)
    if saveFig in ('yes','Yes','Y','y','YES'):
       figName=figNameBase+'_'+str(kk)+figNameExt
       plt.savefig(figName,dpi=fig_dpi)
       print 'Saved figure to '+figNameBase+'_*'+figNameExt

    if dispFig in ('yes','Yes','Y','y','YES'):
       plt.show()
   
####################################################################
####################################################################  

  try: h5file.close()
  except: pass
Esempio n. 18
0
  load_dem.py SanAndreas.dem 
  
  load_dem.py SanAndreas.dem SanAndreas.h5

  load_dem.py  radar_8rlks.hgt radar_8rlks.h5

  ********************************
  ********************************
    '''
    sys.exit(1)


ext = os.path.splitext(demFile)[1]

if ext == '.hgt':  amp,dem,demRsc = readfile.read_float32(demFile)
elif ext == '.dem':    dem,demRsc = readfile.read_real_int16(demFile)


try:     outName = sys.argv[2]
except:  outName = 'dem.h5'


h5=h5py.File(outName,'w')
group=h5.create_group('dem')

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

for key , value in demRsc.iteritems():
     group.attrs[key]=value
Esempio n. 19
0
def main(argv):

    ########### Check Inputs #############
    try:
        file     = sys.argv[1]
        operator = sys.argv[2]
        operand  = float(sys.argv[3])
    except:
        Usage();sys.exit(1)
  
    if   operator in ['+','plus',  'add',      'addition']:        operator = 'plus'
    elif operator in ['-','minus', 'substract','substraction']:    operator = 'minus'
    elif operator in ['*','times', 'multiply', 'multiplication']:  operator = 'multiply'
    elif operator in ['/','obelus','divide',   'division']:        operator = 'divide'
    elif operator in ['^','exp',   'exponential']:                 operator = 'exp'
    else:  print 'ERROR: Unrecognized operator: '+operator;  sys.exit(1)
    print '\n*************** Image Math ******************'
    print 'operation: '+operator+' '+str(operand)
  
    ext = os.path.splitext(file)[1]
    try:    outName = sys.argv[4]
    except: outName = file.split('.')[0]+'_'+operator+str(operand)+ext

    ########### Read - Calculate - Write  ###########
    ##### PySAR HDF5 files ######
    if ext == '.h5':
        import h5py
        try: h5file=h5py.File(file,'r')
        except: print 'ERROR: can not open file: '+file; sys.exit(1)
        k=h5file.keys()
        if 'interferograms' in k: k[0] = 'interferograms'
        elif 'coherence'    in k: k[0] = 'coherence'
        elif 'timeseries'   in k: k[0] = 'timeseries'
        print 'Input file is '+k[0]
      
        h5fileOut = h5py.File(outName,'w'); print 'writing >>> '+outName
        group = h5fileOut.create_group(k[0])
   
        if k[0] in ('velocity','temporal_coherence','rmse','mask','dem'):
            dset = h5file[k[0]].get(k[0])
            data = dset[0:dset.shape[0],0:dset.shape[1]]
       
            dataOut = operation(data,operator,operand)
       
            dset = group.create_dataset(k[0], data=dataOut, compression='gzip')
            for key , value in h5file[k[0]].attrs.iteritems():
                group.attrs[key]=value
   
        elif k[0] == 'timeseries':
            dateList = h5file[k[0]].keys();  print 'number of dates: '+str(len(dateList))
            for date in dateList:
                print date
                dset = h5file[k[0]].get(date)
                data = dset[0:dset.shape[0],0:dset.shape[1]]
       
                dataOut = operation(data,operator,operand)
       
                dset = group.create_dataset(date, data=dataOut, compression='gzip')
            for key,value in h5file[k[0]].attrs.iteritems():
                group.attrs[key] = value
   
        elif k[0] in ['interferograms','coherence','wrapped']:
            ifgramList = h5file[k[0]].keys();  print 'number of epochs: '+str(len(ifgramList))
            for igram in ifgramList:
                print igram
                dset = h5file[k[0]][igram].get(igram)
                data = dset[0:dset.shape[0],0:dset.shape[1]]
        
                dataOut = operation(data,operator,operand)
        
                group2 = group.create_group(igram)
                dset = group2.create_dataset(igram, data=dataOut, compression='gzip')
                for key, value in h5file[k[0]][igram].attrs.iteritems():
                    group2.attrs[key] = value
       
            try:
                mask = h5file['mask'].get('mask')
                gm = h5fileOut.create_group('mask')
                dset = gm.create_dataset('mask', data=mask, compression='gzip')
            except:  print 'No group for mask found in the file.'
       
            try:
                Cset = h5file['meanCoherence'].get('meanCoherence')
                gm = h5fileOut.create_group('meanCoherence')
                dset = gm.create_dataset('meanCoherence', data=Cset, compression='gzip')
            except:  print 'No group for meanCoherence found in the file'

        else: print 'ERROR: Unrecognized HDF5 file type: '+k[0]; sys.exit(1)
   
        h5file.close()
        h5fileOut.close()

    ##### ROI_PAC files #######
    elif ext in ['.unw','.cor','.hgt','.dem','.trans']:
        import pysar._readfile as readfile
        import pysar._writefile as writefile
        print 'Input file is '+ext+'\nwriting >>> '+outName
        if ext in ['.unw','.cor','.hgt']:
            a,p,r = readfile.read_float32(file)
            p2 = operation(p,operator,operand)
            writefile.write_float32(p2,outName)
        elif ext == '.dem':
            p,r = readfile.read_real_int16(file)
            p2 = operation(p,operator,operand)
            writefile.write_real_int16(p2,outName)
        elif ext == '.trans':
            a,p,r = readfile.read_float32(file)
            a2 = operation(a,operator,operand)
            p2 = operation(p,operator,operand)
            writefile.write_float32(a2,p2,outName)
   
        ## Write atrributes file
        f = open(outName+'.rsc','w')
        for k in r.keys():    f.write(k+'    '+r[k]+'\n')
        f.close()
  
  
    else: print 'ERROR: Unrecognized file extension: '+ext; sys.exit(1)
  
    print 'Done.'
Esempio n. 20
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')
   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()[0]].attrs
   elif k[0] in ('dem','velocity','mask','temporal_coherence','rmse','timeseries'):
       atr  = h5file[k[0]].attrs

   #### 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[0] in ('velocity','temporal_coherence','mask','rmse','dem'):

       dset = h5file[k[0]].get(k[0])
       data = dset[0:dset.shape[0],0:dset.shape[1]]
       outname=fileName+'.unw'

       amp,unw,unwrsc = geocode_one(data,geomap,outname)

       f = h5py.File('geo_'+file,'w')
       group=f.create_group(k[0])
       dset = group.create_dataset(k[0], data=unw, compression='gzip')
       for key,value in h5file[k[0]].attrs.iteritems():   group.attrs[key]=value
       for key,value in unwrsc.iteritems():               group.attrs[key] = value


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

   elif  'timeseries' in k:
       print 'geocoding timeseries:'
       outname='epoch_temp.unw'

       f = h5py.File('geo_'+file,'w')
       group = f.create_group('timeseries')
       epochList=h5file['timeseries'].keys()
       for epoch in epochList:
           print 'geocoding '+epoch
           d = h5file['timeseries'].get(epoch)
           data = d[0:d.shape[0],0:d.shape[1]]

           amp,unw,unwrsc = geocode_one(data,geomap,outname)
           dset = group.create_dataset(epoch, data=unw, compression='gzip')

       for key,value in unwrsc.iteritems():              group.attrs[key] = value
       for key,value in h5file[k[0]].attrs.iteritems():  group.attrs[key] = value
       group.attrs['WIDTH']       = unwrsc['WIDTH'] 
       group.attrs['FILE_LENGTH'] = unwrsc['FILE_LENGTH']
    

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

   elif k[0] in ['interferograms','coherence','wrapped']:
       print 'geocoding '+k[0]+' ...'
       if   k[0] == 'interferograms': outname = k[0]+'_temp.unw'
       elif k[0] == '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[0]].keys()
       for igram in igramList:
           print 'geocoding '+igram
           d = h5file[k[0]][igram].get(igram)
           data = d[0:d.shape[0],0:d.shape[1]]

           amp,unw,unwrsc = geocode_one(data,geomap,outname)

           group = gg.create_group('geo_'+igram)
           dset = group.create_dataset('geo_'+igram, data=unw, compression='gzip')
           for key,value in unwrsc.iteritems():                         group.attrs[key] = value
           for key,value in h5file[k[0]][igram].attrs.iteritems():      group.attrs[key] = value
           group.attrs['WIDTH'] = unwrsc['WIDTH']
           group.attrs['FILE_LENGTH'] = unwrsc['FILE_LENGTH']

       #######################  support of old format  #######################
       ### mask
       try:
           d = h5file['mask'].get('mask')
           data = d[0:d.shape[0],0:d.shape[1]]
           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:
           d = h5file['meanCoherence'].get('meanCoherence')
           data = d[0:d.shape[0],0:d.shape[1]]
           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'


######################################################################################
   try:
       atr['subset_x0']
       rmCmd='rm '+geomap;            os.system(rmCmd);       print rmCmd
       rmCmd='rm '+geomap+'.rsc';     os.system(rmCmd);       print rmCmd
   except: pass

   f.close()
   h5file.close()
Esempio n. 21
0
  Usage:
      load_dem.py input [output]

  Example:
      load_dem.py  SanAndreas.dem 
      load_dem.py  SanAndreas.dem    SanAndreas.h5
      load_dem.py  radar_8rlks.hgt   radar_8rlks.h5

*****************************************************************
    '''
    sys.exit(1)

ext = os.path.splitext(demFile)[1]

if ext == '.hgt': amp, dem, demRsc = readfile.read_float32(demFile)
elif ext == '.dem': dem, demRsc = readfile.read_real_int16(demFile)

try:
    outName = sys.argv[2]
except:
    outName = 'dem.h5'

print 'writing >>> ' + outName
h5 = h5py.File(outName, 'w')
group = h5.create_group('dem')

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

for key, value in demRsc.iteritems():
    group.attrs[key] = value
Esempio n. 22
0
def main(argv):

  #lineWidth=4
  #fontSize=32
  #markerColor='orange'
  #markerSize=20
  #if len(sys.argv)>2:

  try:
    opts, args = getopt.getopt(argv,"h:f:m:t:x:y:o:")
  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 == '-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':        MaskedFile = arg

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

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

  ext = os.path.splitext(File)[1]
  import h5py
  import numpy as np

  ################################  PySAR HDF5  #################################

  if ext == '.h5':
    h5file=h5py.File(File,'r')
    h5mask=h5py.File(maskFile,'r')
    kf=h5file.keys()

    if 'coherence' not in h5mask.keys():
       Mset=h5mask[h5mask.keys()[0]].get(h5mask.keys()[0])
       M=Mset[0:Mset.shape[0],0:Mset.shape[1]]

    try:     MaskedFile
    except:  MaskedFile = File.split('.')[0]+'_masked.h5'
    h5file2 = h5py.File(MaskedFile,'w')

    if len(kf)==1 and kf[0] in ('velocity','temporal_coherence','rmse','mask'):
      Vset=h5file[h5file.keys()[0]].get(h5file.keys()[0])
      V=Vset[0:Vset.shape[0],0:Vset.shape[1]]
 
      try:
        xsub
        ysub
        M[ysub[0]:ysub[1],xsub[0]:xsub[1]]=0
      except:   print 'No subset'

      try:     V[M<thr] = np.nan
      except:  V[M==0]  = np.nan
    
      group=h5file2.create_group(kf[0])
      dset = group.create_dataset(os.path.basename(kf[0]), data=V, compression='gzip')

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


    elif 'timeseries' in h5file.keys():
      print 'Masking the time-series'
      group = h5file2.create_group('timeseries')
      dateList=h5file['timeseries'].keys()
      for d in dateList:
        print d
        unwset = h5file['timeseries'].get(d)
        unw=unwset[0:unwset.shape[0],0:unwset.shape[1]]

        try:     unw[M<thr] = np.nan
        except:  unw[M==0]  = np.nan

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

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


    elif kf[0] in ('interferograms','wrapped') and 'coherence' in h5mask.keys():
      print 'Masking each '+kf[0]+' using its coherence file'
      igramList=h5file[kf[0]].keys()
      cohList=h5mask['coherence'].keys()
      gg = h5file2.create_group(kf[0])
      for igram in igramList:
         print igram
         date12 = h5file[kf[0]][igram].attrs['DATE12']
         for cohFile in cohList:
             if h5mask['coherence'][cohFile].attrs['DATE12']==date12:
                 igramCoh=cohFile
         print igramCoh

         unwset = h5file[kf[0]][igram].get(igram)
         unw=unwset[0:unwset.shape[0],0:unwset.shape[1]]

         cohset=h5mask['coherence'][igramCoh].get(igramCoh)      
         coh=cohset[0:cohset.shape[0],0:cohset.shape[1]]
         unw[coh<thr]=np.nan
         group = gg.create_group(igram)
         dset = group.create_dataset(igram, data=unw, compression='gzip')
         for key, value in h5file[kf[0]][igram].attrs.iteritems():
            group.attrs[key] = value
      try:
      #if kf[0] == 'interferograms':
         mask = h5file['mask'].get('mask')
         gm = h5file2.create_group('mask')
         dset = gm.create_dataset('mask', data=mask, compression='gzip')
      except: print 'no mask group found.'


    elif kf[0] in ('interferograms','wrapped') and 'coherence' not in h5mask.keys():
      print 'Masking the '+kf[0]
      igramList=h5file[kf[0]].keys()     
      gg = h5file2.create_group(kf[0])
      for igram in igramList:
         print igram
         unwset = h5file[kf[0]][igram].get(igram)
         unw=unwset[0:unwset.shape[0],0:unwset.shape[1]]
         try:     unw[M<thr] = np.nan
         except:  unw[M==0]  = np.nan

         group = gg.create_group(igram)
         dset = group.create_dataset(igram, data=unw, compression='gzip')
         for key, value in h5file[kf[0]][igram].attrs.iteritems():
            group.attrs[key] = value

      try:
      #if kf[0] == 'interferograms':
         mask = h5file['mask'].get('mask')
         gm = h5file2.create_group('mask')
         dset = gm.create_dataset('mask', data=mask, compression='gzip') 
      except: print 'no mask group found.'


    h5file.close()
    h5mask.close()
    h5file2.close()    


  ################################  ROI_PAC  ######################################

  elif ext in ['.unw','.int']:
    import pysar._readfile as readfile
    import pysar._writefile as writefile
    Mext = os.path.splitext(maskFile)[1]
    if Mext == '.cor':
       a,M,atr = readfile.read_float32(maskFile)
    elif Mext == '.h5':
       h5mask=h5py.File(maskFile,'r')
       Mset=h5mask[h5mask.keys()[0]].get(h5mask.keys()[0])
       M=Mset[0:Mset.shape[0],0:Mset.shape[1]]
    else: sys.exit(['Un-recoganized mask file extension: '+Mext])

    try:     MaskedFile
    except:  MaskedFile = File.split('.')[0]+'_masked'+ext

    a,unw,atr = readfile.read_float32(File)
    try:     unw[M<thr] = np.nan
    except:  unw[M==0]  = np.nan

    print 'writting >>> '+MaskedFile
    writefile.write_float32(unw,MaskedFile)
    f = open(MaskedFile+'.rsc','w')
    for k in atr.keys():  f.write(k+'    '+atr[k]+'\n')
    f.close()

    try: h5mask.close()
    except: pass
Esempio n. 23
0
def main(argv):

  try:  
    file=argv[0]
    alks=float(argv[1])
    rlks=float(argv[2])
  except:
    try:
       file=argv[0]
       alks=float(argv[1])
       rlks=float(argv[1])
    except:  Usage();sys.exit(1)

  ext = os.path.splitext(file)[1]
  outName=file.split('.')[0]+'_a'+str(int(alks))+'lks_r'+str(int(rlks))+'lks'+ext

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

  if ext == '.h5':
    import h5py
    h5file_mli=h5py.File(outName,'w')
    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']:
      print 'Multilooking the interferograms'
      gg = h5file_mli.create_group(k[0])
      igramList=h5file[k[0]].keys()
      for igram in igramList:
        print igram
        unw = h5file[k[0]][igram].get(igram)
        unwlks=multilook(unw,alks,rlks)
        group = gg.create_group(igram)
        dset = group.create_dataset(igram, data=unwlks, compression='gzip')
        for key, value in h5file[k[0]][igram].attrs.iteritems():
           group.attrs[key] = value
        group.attrs['WIDTH']       = unwlks.shape[1]
        group.attrs['FILE_LENGTH'] = unwlks.shape[0]
        try:
           group.attrs['Y_STEP']=alks*float(group.attrs['Y_STEP'])
           group.attrs['X_STEP']=rlks*float(group.attrs['X_STEP'])
        except:
           group.attrs['AZIMUTH_PIXEL_SIZE'] = alks*float(group.attrs['AZIMUTH_PIXEL_SIZE'])
           group.attrs['RANGE_PIXEL_SIZE']   = rlks*float(group.attrs['RANGE_PIXEL_SIZE'])

      try:
        dset1=h5file['mask'].get('mask')
        mask=dset1[0:dset1.shape[0],0:dset1.shape[1]]
        masklks=multilook(mask,alks,rlks)
        group=h5file_mli.create_group('mask')
        dset = group.create_dataset('mask', data=masklks, compression='gzip')
      except: print 'No mask group found.'

    elif k[0] in ['timeseries','temporal_coherence', 'velocity', 'mask', 'rmse']:
      print 'Multilooking '+k[0]
      group = h5file_mli.create_group(k[0])

      if k[0] == 'timeseries':
        dateList=h5file[k[0]].keys()
        for d in dateList:
          print d
          unw = h5file[k[0]].get(d)
          unwlks=multilook(unw,alks,rlks)
          dset = group.create_dataset(d, data=unwlks, compression='gzip')
      elif k[0] in ['temporal_coherence', 'velocity', 'mask', 'rmse']:
        dset1 = h5file[k[0]].get(k[0])
        unw = dset1[0:dset1.shape[0],0:dset1.shape[1]]
        unwlks=multilook(unw,alks,rlks)
        dset = group.create_dataset(k[0], data=unwlks, compression='gzip')

      try:
        dset1 = h5file['mask'].get('mask')
        Mask = dset1[0:dset1.shape[0],0:dset1.shape[1]]
        Masklks=multilook(Mask,alks,rlks)
        group=h5file_mli.create_group('mask')
        dset = group.create_dataset('mask', data=Masklks, compression='gzip')
      except:  print 'No mask group found.'

      ## Update attributes
      for key,value in h5file[k[0]].attrs.iteritems():      group.attrs[key] = value
      group.attrs['WIDTH']       = unwlks.shape[1]
      group.attrs['FILE_LENGTH'] = unwlks.shape[0]

      try:
        group.attrs['Y_STEP']=alks*float(group.attrs['Y_STEP'])
        group.attrs['X_STEP']=rlks*float(group.attrs['X_STEP'])
      except:
        group.attrs['AZIMUTH_PIXEL_SIZE'] = alks*float(group.attrs['AZIMUTH_PIXEL_SIZE'])
        group.attrs['RANGE_PIXEL_SIZE']   = rlks*float(group.attrs['RANGE_PIXEL_SIZE'])


    h5file.close()
    h5file_mli.close()

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

  elif ext in ['.unw','.cor','.hgt','.dem','.trans'] or\
       ext in ['.jpeg','.jpg','.png','.ras','.bmp'] or\
       ext in ['.mli','.slc']:
    import pysar._readfile  as readfile 
    import pysar._writefile as writefile
    r = readfile.read_rsc_file(file + '.rsc')

    if ext == '.int' or ext == '.slc':
       a,p,r = readfile.read_complex64(file)
       pmli=multilook(p,alks,rlks)
       amli=multilook(a,alks,rlks)
       r['FILE_LENGTH']=str(pmli.shape[0])
       r['WIDTH']      =str(pmli.shape[1])
    elif ext == '.unw' or ext == '.cor' or ext == '.hgt':
       a,p,r = readfile.read_float32(file)
       pmli=multilook(p,alks,rlks)
       amli=multilook(a,alks,rlks)
       print 'writing >>>'+outName 
       writefile.write_float32(pmli,outName)
       r['FILE_LENGTH']=str(pmli.shape[0])
       r['WIDTH']      =str(pmli.shape[1])
    elif ext == ('.dem'):
       d,r = readfile.read_dem(file)
       dmli=multilook(d,alks,rlks)
       print 'writing >>>'+outName
       writefile.write_dem(dmli,outName)
       r['FILE_LENGTH']=str(dmli.shape[0])
       r['WIDTH']      =str(dmli.shape[1])
    elif ext in ['.jpeg','.jpg','.png','.bmp']:
       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)
       r['FILE_LENGTH']=str(height)
       r['WIDTH']      =str(width)

    ## Update attributes
    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:  
       r['AZIMUTH_PIXEL_SIZE'] = alks*float(r['AZIMUTH_PIXEL_SIZE'])
       r['RANGE_PIXEL_SIZE']   = rlks*float(r['RANGE_PIXEL_SIZE'])

    f = open(outName+'.rsc','w')
    for k in r.keys():
       f.write(k+'    '+r[k]+'\n')
    f.close()
Esempio n. 24
0
def main(argv):
  
    try:
        File = argv[0]
        demFile=argv[1]
        p=int(argv[2])
    except:
        Usage() ; sys.exit(1)
  
    try:    baseline_error=argv[3]
    except: baseline_error='range_and_azimuth'
    print baseline_error  
    ##################################
    h5file = h5py.File(File)
    dateList = h5file['timeseries'].keys()
    ##################################
  
    try: maskFile=argv[4]
    except:
        if   os.path.isfile('Modified_Mask.h5'):  maskFile = 'Modified_Mask.h5'
        elif os.path.isfile('Mask.h5'):           maskFile = 'Mask.h5'
        else: print 'No mask found!'; sys.exit(1)
    try:  Mask,Matr = readfile.read(maskFile);   print 'mask: '+maskFile
    except: print 'Can not open mask file: '+maskFile; sys.exit(1)
  
    #try:
    #  maskFile=argv[4]
    #  h5Mask = h5py.File(maskFile,'r')
    #  kMask=h5Mask.keys()
    #  dset1 = h5Mask[kMask[0]].get(kMask[0])
    #  Mask = dset1[0:dset1.shape[0],0:dset1.shape[1]]
    #except:
    #  dset1 = h5file['mask'].get('mask')
    #  Mask = dset1[0:dset1.shape[0],0:dset1.shape[1]]
  
    ##################################
    Mask=Mask.flatten(1)
    ndx= Mask !=0
    ##################################
    # h5file = h5py.File(File)
    # dateList = h5file['timeseries'].keys() 
    ##################################
    nt=float(h5file['timeseries'].attrs['LOOK_REF1'])
    ft=float(h5file['timeseries'].attrs['LOOK_REF2'])
    sy,sx=np.shape(dset1)
    npixel=sx*sy
    lookangle=np.tile(np.linspace(nt,ft,sx),[sy,1])
    lookangle=lookangle.flatten(1)*np.pi/180.0
    Fh=-np.sin(lookangle)
    Fv=-np.cos(lookangle)  
  
    print 'Looking for azimuth pixel size'
    try:
        daz=float(h5file['timeseries'].attrs['AZIMUTH_PIXEL_SIZE'])
    except:
        print'''
        ERROR!
        The attribute AZIMUTH_PIXEL_SIZE was not found!
        Possible cause of error: Geo coordinate.
        This function works only in radar coordinate system.
        '''   
        sys.exit(1)
  
    lines=np.tile(np.arange(0,sy,1),[1,sx])
    lines=lines.flatten(1)
    rs=lines*daz
   
    if baseline_error=='range_and_azimuth': 
        A = np.zeros([npixel,4])
   
        A[:,0]=Fh
        A[:,1]=Fh*rs
        A[:,2]=Fv
        A[:,3]=Fv*rs 
        num_base_par=4
    elif baseline_error=='range':
        A = np.zeros([npixel,2])
   
        A[:,0]=Fh
        A[:,1]=Fv
        num_base_par=2

    ###########################################
    yref=int(h5file['timeseries'].attrs['ref_y'])
    xref=int(h5file['timeseries'].attrs['ref_x'])
    ###########################################
    if os.path.basename(demFile).split('.')[1]=='hgt':
         amp,dem,demRsc = readfile.read_float32(demFile)
    elif os.path.basename(demFile).split('.')[1]=='dem':
         dem,demRsc = readfile.read_real_int16(demFile)
  
    dem=dem-dem[yref][xref]
    dem=dem.flatten(1)
    ###################################################
    if p==1:
        # A=np.vstack((dem[ndx],np.ones(len(dem[ndx])))).T
        B = np.vstack((dem,np.ones(len(dem)))).T
    elif p==2:
        # A=np.vstack((dem[ndx]**2,dem[ndx],np.ones(len(dem[ndx])))).T
        B = np.vstack((dem**2,dem,np.ones(len(dem)))).T
    elif p==3:
        #  A = np.vstack((dem[ndx]**3,dem[ndx]**2,dem[ndx],np.ones(len(dem[ndx])))).T
        B = np.vstack((dem**3,dem**2,dem,np.ones(len(dem)))).T
    print np.shape(A)
  
    Ainv=np.linalg.pinv(A)
    ###################################################
 

    Bh=[]
    Bv=[]
    Bhrate=[]
    Bvrate=[]
    Be=np.zeros([len(dateList),num_base_par+p+1])  
    print '%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%'
    for i in range(1,len(dateList)):
        dset = h5file['timeseries'].get(dateList[i])
        data = dset[0:dset.shape[0],0:dset.shape[1]]
        L = data.flatten(1)
        M=np.hstack((A,B))
        Berror=np.dot(np.linalg.pinv(M[ndx]),L[ndx])
        Bh.append(Berror[0])
        Bhrate.append(Berror[1])
        Bv.append(Berror[2])
        Bvrate.append(Berror[3])
        Be[i,:]=Berror
        print Berror
    print '%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%' 
    print 'baseline error           mean                          std'   
    print '       bh     :  ' +str(np.mean(Bh)) + '     ,  '+str(np.std(Bh))
    print '     bh rate  :  ' +str(np.mean(Bhrate)) + '     ,  '+str(np.std(Bhrate))
    print '       bv     :  ' +str(np.mean(Bv)) + '     ,  '+str(np.std(Bv))
    print '     bv rate  :  ' +str(np.mean(Bvrate)) + '     ,  '+str(np.std(Bvrate))
    print '%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%'       
    # plt.hist(Bh,bins=8,normed=True)
    # formatter = FuncFormatter(to_percent)
    # Set the formatter
    # plt.gca().yaxis.set_major_formatter(formatter)    
    # plt.show()
    print '%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%'
    # print 'Estimating Baseline error from each differences ...'

    orbEffect=np.zeros([len(dateList),sy,sx])
    for i in range(1,len(dateList)):
        effect=np.dot(M,Be[i,:])
        effect=np.reshape(effect,[sx,sy]).T
        # orbEffect[i,:,:]=orbEffect[i-1,:,:]+effect     
        # orbEffect[i,:,:]=orbEffect[i,:,:]-orbEffect[i,yref,xref]
        orbEffect[i,:,:]=effect - effect[yref,xref]
        del effect
  
    print 'Correctiing the time series '
    outName=File.replace('.h5','')+'_BaseTropCor.h5'
    h5orbCor=h5py.File(outName,'w')
    group = h5orbCor.create_group('timeseries')
    for i in range(len(dateList)):
        dset1 = h5file['timeseries'].get(dateList[i])
        data = dset1[0:dset1.shape[0],0:dset1.shape[1]] - orbEffect[i,:,:]
        dset = group.create_dataset(dateList[i], data=data, compression='gzip')      
  
    for key,value in h5file['timeseries'].attrs.iteritems():
        group.attrs[key] = value
  
  
    dset1 = h5file['mask'].get('mask')
    group=h5orbCor.create_group('mask')
    dset = group.create_dataset('mask', data=dset1, compression='gzip')
  
    h5file.close()
    h5orbCor.close()
Esempio n. 25
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"
Esempio n. 26
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
Esempio n. 27
0
def main(argv):

  if len(sys.argv)>2:
    try:
      opts, args = getopt.getopt(argv,"h:f:x:y:o:l:L:")
    except getopt.GetoptError:
      print 'Error while getting args'
      Usage() ; sys.exit(1)

    for opt,arg in opts:
      if   opt in ("-h","--help"):    Usage() ; sys.exit()
      elif opt == '-f':   File = 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 == '-l':   Latsub = [float(i) for i in arg.split(':')];  Latsub.sort()
      elif opt == '-L':   Lonsub = [float(i) for i in arg.split(':')];  Lonsub.sort()

  else:   Usage(); sys.exit(1)

  try:     outName
  except:  outName='subset_'+File

  ext = os.path.splitext(File)[1]

############################################################################
#################################  PySAR  ##################################

  if ext == '.h5':
    try:      h5file=h5py.File(File,'r')
    except:   Usage() ; sys.exit(1)
    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()[0]].attrs
    elif k[0] in ('dem','velocity','mask','temporal_coherence','rmse','timeseries'):
       atr  = h5file[k[0]].attrs

    ############# Subset Option #############
    width=int(atr['WIDTH'])
    length=int(atr['FILE_LENGTH'])

    try:
      Latsub
      try:
        lat_step = float(atr['Y_STEP'])
        lat1 = float(atr['Y_FIRST'])
        lat0 = lat1 + length*lat_step
        if Latsub[0]<lat0:  Latsub[0]=lat0; print 'WARNING: input latitude < min ('+str(lat0)+')! Set it to min.'
        if Latsub[1]>lat1:  Latsub[1]=lat1; print 'WARNING: input latitude > max ('+str(lat1)+')! Set it to max.'
        print 'subset in latitude - '+str(Latsub[0])+':'+str(Latsub[1])
        ysub=[0]*2
        ysub[0] = int((Latsub[1]-lat1)/lat_step)
        ysub[1] = int((Latsub[0]-lat1)/lat_step)
      except:
        print 'Not geocoded file, cannot be subseted with LatLon.'
        Usage() ; return
    except:
      try:
        ysub
        if ysub[0]<0:       ysub[0]=0;      print 'WARNING: input y < min (0)! Set it to min.'
        if ysub[1]>length:  ysub[1]=length; print 'WARNING: input y > max ('+str(length)+')! Set it to max.'
        print 'subset in y direction - '+str(ysub[0])+':'+str(ysub[1])
      except: ysub = [0,length]

    try:
      Lonsub
      try:
        lon_step = float(atr['X_STEP'])
        lon0 = float(atr['X_FIRST'])
        lon1 = lon0 + width*lon_step
        if Lonsub[0]<lon0:  Lonsub[0]=lon0; print 'WARNING: input longitude < min ('+str(lon0)+')! Set it to min.'
        if Lonsub[1]>lon1:  Lonsub[1]=lon1; print 'WARNING: input longitude > max ('+str(lon1)+')! Set it to max.'
        print 'subset in longitude - '+str(Lonsub[0])+':'+str(Lonsub[1])
        xsub=[0]*2
        xsub[0]=int((Lonsub[0]-lon0)/lon_step)
        xsub[1]=int((Lonsub[1]-lon0)/lon_step)
      except:
        print 'Not geocoded file, cannot be subseted with LatLon.'
        Usage() ; return
    except:
      try:
        xsub
        if xsub[0]<0:      xsub[0]=0;     print 'WARNING: input x < min (0)! Set it to min.'
        if xsub[1]>width:  xsub[1]=width; print 'WARNING: input x > max ('+str(width)+')! Set it to max x.'
        print 'subset in x direction - '+str(xsub[0])+':'+str(xsub[1])
      except: xsub = [0,width]

    if ysub[0]>length or ysub[1]<0 or xsub[0]>length or xsub[1]<0:
      print 'ERROR: input index is out of data range!'
      print 'range in rdr: x - 0:'+str(width)+'    y - 0:'+str(length)
      try: print 'range in geo: lat - '+str(lat0)+':'+str(lat1)+'    lon - '+str(lon0)+':'+str(lon1)
      except: Geo=0
      sys.exit(1)

    ######## Data Read, Crop and Write #######
    ##### N dset, N attributes
    if k[0] in ('interferograms','coherence','wrapped'):
      print 'writing  >>>  ' +outName
      h5out=h5py.File(outName,'w')
      gg=h5out.create_group(k[0])

      igramList=h5file[k[0]].keys()
      for igram in igramList:
        print igram
        dset1=h5file[k[0]][igram].get(igram)
        group=gg.create_group(igram)
        dset=group.create_dataset(igram, data=dset1[ysub[0]:ysub[1],xsub[0]:xsub[1]], compression='gzip')

        for key, value in h5file[k[0]][igram].attrs.iteritems():    group.attrs[key] = value
        group.attrs['FILE_LENGTH']=ysub[1]-ysub[0]
        group.attrs['WIDTH']      =xsub[1]-xsub[0]
        try:
          sub_x0_ori = int(group.attrs['subset_x0'])
          group.attrs['subset_x0'] = xsub[0]+sub_x0_ori
          group.attrs['subset_x1'] = xsub[1]+sub_x0_ori
        except:
          group.attrs['subset_x0']=xsub[0]
          group.attrs['subset_x1']=xsub[1]
        try:
          sub_y0_ori = int(group.attrs['subset_y0'])
          group.attrs['subset_y0'] = ysub[0]+sub_y0_ori
          group.attrs['subset_y1'] = ysub[1]+sub_y0_ori
        except:
          group.attrs['subset_y0']=ysub[0]
          group.attrs['subset_y1']=ysub[1]
        if 'X_FIRST' in atr.keys():
          group.attrs['X_FIRST']=float(atr['X_FIRST']) + xsub[0]*float(atr['X_STEP'])
          group.attrs['Y_FIRST']=float(atr['Y_FIRST']) + ysub[0]*float(atr['Y_STEP'])  

      ## support of old format
      try:
        Mset=h5file['mask'].get('mask')
        gm=h5out.create_group('mask')
        dset=gm.create_dataset('mask', data=Mset[ysub[0]:ysub[1],xsub[0]:xsub[1]], compression='gzip')
      except:  print 'No group for mask found in the file.'
      try:    
        Cset=h5file['meanCoherence'].get('meanCoherence')                  
        gm=h5out.create_group('meanCoherence')
        dset=gm.create_dataset('meanCoherence', data=Cset[ysub[0]:ysub[1],xsub[0]:xsub[1]], compression='gzip')
      except:  print 'No group for meanCoherence found in the file'

      h5file.close()
      h5out.close()

    ##### N/1 dset, 1 attributes
    elif k[0] in ['timeseries', 'temporal_coherence', 'velocity', 'mask', 'rmse']:
      print 'writing  >>>  ' +outName
      h5out=h5py.File(outName,'w')
      group=h5out.create_group(k[0])

      if k[0] == 'timeseries':      
        dateList=h5file[k[0]].keys()
        for d in dateList:
          print d
          dset1=h5file[k[0]].get(d)
          dset=group.create_dataset(d, data=dset1[ysub[0]:ysub[1],xsub[0]:xsub[1]], compression='gzip')
      elif k[0] in ['temporal_coherence', 'velocity', 'mask', 'rmse']:
        dset1=h5file[k[0]].get(k[0]) 
        dset=group.create_dataset(k[0],data=dset1[ysub[0]:ysub[1],xsub[0]:xsub[1]],compression='gzip')

      ## Update attributes
      for key, value in h5file[k[0]].attrs.iteritems():        group.attrs[key] = value
      group.attrs['FILE_LENGTH']=ysub[1]-ysub[0]
      group.attrs['WIDTH']      =xsub[1]-xsub[0]
      try:
        sub_x0_ori = int(group.attrs['subset_x0'])
        group.attrs['subset_x0'] = xsub[0]+sub_x0_ori
        group.attrs['subset_x1'] = xsub[1]+sub_x0_ori
      except:
        group.attrs['subset_x0']=xsub[0]
        group.attrs['subset_x1']=xsub[1]
      try:
        sub_y0_ori = int(group.attrs['subset_y0'])
        group.attrs['subset_y0'] = ysub[0]+sub_y0_ori
        group.attrs['subset_y1'] = ysub[1]+sub_y0_ori
      except:
        group.attrs['subset_y0']=ysub[0]
        group.attrs['subset_y1']=ysub[1]
      if 'X_FIRST' in atr.keys():
        group.attrs['X_FIRST']=float(atr['X_FIRST']) + xsub[0]*float(atr['X_STEP'])
        group.attrs['Y_FIRST']=float(atr['Y_FIRST']) + ysub[0]*float(atr['Y_STEP'])

      h5file.close()
      h5out.close()


    else:
      print 'Error: group of HDF5 file not recogized!'
      h5file.close()
      Usage() ; sys.exit(1)


############################################################################
#########################  ROI_PAC / Image / GAMMA  ########################

  elif ext in ['.unw','.cor','.hgt','.dem','.trans'] or\
       ext in ['.jpeg','.jpg','.png','.ras','.bmp'] or\
       ext in ['.mli','.slc']:

    try:     atr = readfile.read_rsc_file(File + '.rsc')
    except:
      try:     atr = readfile.read_par_file(File + '.par')
      except:  atr = readfile.read_par_file(os.path.splitext(File)[0] + '.par')

    ############# Subset Option #############
    try:     width  = int(atr['WIDTH']);          length = int(atr['FILE_LENGTH'])
    except:  width  = int(atr['range_samples:']); length = int(atr['azimuth_lines:'])

    try:
      Latsub
      try:
        lat_step = float(atr['Y_STEP'])
        lat1 = float(atr['Y_FIRST'])
        lat0 = lat1 + length*lat_step
        if Latsub[0]<lat0:  Latsub[0]=lat0; print 'WARNING: input latitude < min ('+str(lat0)+')! Set it to min.'
        if Latsub[1]>lat1:  Latsub[1]=lat1; print 'WARNING: input latitude > max ('+str(lat1)+')! Set it to max.'
        print 'subset in latitude - '+str(Latsub[0])+':'+str(Latsub[1])
        ysub=[0]*2
        ysub[0] = int((Latsub[1]-lat1)/lat_step)
        ysub[1] = int((Latsub[0]-lat1)/lat_step)
      except:
        print 'Not geocoded file, cannot be subseted with LatLon.'
        Usage() ; return
    except:
      try:
        ysub
        if ysub[0]<0:       ysub[0]=0;      print 'WARNING: input y < min (0)! Set it to min.'
        if ysub[1]>length:  ysub[1]=length; print 'WARNING: input y > max ('+str(length)+')! Set it to max.'
        print 'subset in y direction - '+str(ysub[0])+':'+str(ysub[1])
      except: ysub = [0,length]

    try:
      Lonsub
      try:
        lon_step = float(atr['X_STEP'])
        lon0 = float(atr['X_FIRST'])
        lon1 = lon0 + width*lon_step
        if Lonsub[0]<lon0:  Lonsub[0]=lon0; print 'WARNING: input longitude < min ('+str(lon0)+')! Set it to min.'
        if Lonsub[1]>lon1:  Lonsub[1]=lon1; print 'WARNING: input longitude > max ('+str(lon1)+')! Set it to max.'
        print 'subset in longitude - '+str(Lonsub[0])+':'+str(Lonsub[1])
        xsub=[0]*2
        xsub[0]=int((Lonsub[0]-lon0)/lon_step)
        xsub[1]=int((Lonsub[1]-lon0)/lon_step)
      except:
        print 'Not geocoded file, cannot be subseted with LatLon.'
        Usage() ; return
    except:
      try:
        xsub
        if xsub[0]<0:      xsub[0]=0;     print 'WARNING: input x < min (0)! Set it to min.'
        if xsub[1]>width:  xsub[1]=width; print 'WARNING: input x > max ('+str(width)+')! Set it to max x.'
        print 'subset in x direction - '+str(xsub[0])+':'+str(xsub[1])
      except: xsub = [0,width]

    if ysub[0]>length or ysub[1]<0 or xsub[0]>length or xsub[1]<0:
      print 'ERROR: input index is out of data range!'
      print 'range in rdr: x - 0:'+str(width)+'    y - 0:'+str(length)
      try: print 'range in geo: lat - '+str(lat0)+':'+str(lat1)+'    lon - '+str(lon0)+':'+str(lon1)
      except: Geo=0
      sys.exit(1)

    ######## Data Read, Crop and Write #######
    print 'writing >>> '+outName
    box = (xsub[0],ysub[0],xsub[1],ysub[1])
    if ext in ['.unw','.cor','.hgt']:
       a,p,r = readfile.read_float32(File,box)
       #p = p[ysub[0]:ysub[1],xsub[0]:xsub[1]]
       writefile.write_float32(p,outName)
    elif ext == '.dem':
       p,r = readfile.read_dem(File)
       p = p[ysub[0]:ysub[1],xsub[0]:xsub[1]]
       writefile.write_dem(p,outName)
    elif ext == '.trans':
       a,p,r = readfile.read_float32(File,box)
       #a = a[ysub[0]:ysub[1],xsub[0]:xsub[1]]
       #p = p[ysub[0]:ysub[1],xsub[0]:xsub[1]]
       writefile.write_float32(a,p,outName)
    elif ext in ['.jpeg','.jpg','.png','.ras','.bmp']: 
       import Image
       im  = Image.open(File)
       box = (xsub[0],ysub[0],xsub[1],ysub[1])
       output_img = im.crop(box)
       output_img.save(outName)
    elif ext == '.mli':
       d,r = readfile.read_gamma_float(File)
       d = d[ysub[0]:ysub[1],xsub[0]:xsub[1]]
       writefile.write_gamma_float(d,outName)
    elif ext == '.slc':
       d,r = readfile.read_gamma_scomplex(File,box)
       writefile.write_gamma_scomplex(d,outName)

    ########### Update .rsc file #############
    atr['FILE_LENGTH'] = str(ysub[1]-ysub[0])
    atr['WIDTH']       = str(xsub[1]-xsub[0])
    atr['XMAX'] = str(width - 1)
    atr['YMAX'] = str(length - 1)
    try:
       sub_x0_ori = int(atr['subset_x0'])
       atr['subset_x0'] = str(xsub[0] + sub_x0_ori)
       atr['subset_x1'] = str(xsub[1] + sub_x0_ori)
    except:
       atr['subset_x0'] = str(xsub[0])
       atr['subset_x1'] = str(xsub[1])
    try:
       sub_y0_ori = int(atr['subset_y0'])
       atr['subset_y0'] = str(ysub[0] + sub_y0_ori)
       atr['subset_y1'] = str(ysub[1] + sub_y0_ori)
    except:
       atr['subset_y0'] = str(ysub[0])
       atr['subset_y1'] = str(ysub[1])
    if 'X_FIRST' in atr.keys():
       atr['Y_FIRST']=str(float(atr['Y_FIRST'])+ysub[0]*float(atr['Y_STEP']))
       atr['X_FIRST']=str(float(atr['X_FIRST'])+xsub[0]*float(atr['X_STEP']))

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

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

  else:
    print 'File extension not recogized.'
    Usage() ; sys.exit(1)
Esempio n. 28
0
def main(argv):

    try:
        file = argv[0]
        alks = float(argv[1])
        rlks = float(argv[2])
    except:
        try:
            file = argv[0]
            alks = float(argv[1])
            rlks = float(argv[1])
        except:
            Usage()
            sys.exit(1)

    ext = os.path.splitext(file)[1]
    outName = file.split('.')[0] + '_a' + str(int(alks)) + 'lks_r' + str(
        int(rlks)) + 'lks' + ext

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

    if ext == '.h5':
        import h5py
        h5file_mli = h5py.File(outName, 'w')
        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']:
            print 'Multilooking the interferograms'
            gg = h5file_mli.create_group(k[0])
            igramList = h5file[k[0]].keys()
            for igram in igramList:
                print igram
                unw = h5file[k[0]][igram].get(igram)
                unwlks = multilook(unw, alks, rlks)
                group = gg.create_group(igram)
                dset = group.create_dataset(igram,
                                            data=unwlks,
                                            compression='gzip')
                for key, value in h5file[k[0]][igram].attrs.iteritems():
                    group.attrs[key] = value
                group.attrs['WIDTH'] = unwlks.shape[1]
                group.attrs['FILE_LENGTH'] = unwlks.shape[0]
                try:
                    group.attrs['Y_STEP'] = alks * float(group.attrs['Y_STEP'])
                    group.attrs['X_STEP'] = rlks * float(group.attrs['X_STEP'])
                except:
                    group.attrs['AZIMUTH_PIXEL_SIZE'] = alks * float(
                        group.attrs['AZIMUTH_PIXEL_SIZE'])
                    group.attrs['RANGE_PIXEL_SIZE'] = rlks * float(
                        group.attrs['RANGE_PIXEL_SIZE'])

            try:
                dset1 = h5file['mask'].get('mask')
                mask = dset1[0:dset1.shape[0], 0:dset1.shape[1]]
                masklks = multilook(mask, alks, rlks)
                group = h5file_mli.create_group('mask')
                dset = group.create_dataset('mask',
                                            data=masklks,
                                            compression='gzip')
            except:
                print 'No mask group found.'

        elif k[0] in [
                'timeseries', 'temporal_coherence', 'velocity', 'mask', 'rmse'
        ]:
            print 'Multilooking ' + k[0]
            group = h5file_mli.create_group(k[0])

            if k[0] == 'timeseries':
                dateList = h5file[k[0]].keys()
                for d in dateList:
                    print d
                    unw = h5file[k[0]].get(d)
                    unwlks = multilook(unw, alks, rlks)
                    dset = group.create_dataset(d,
                                                data=unwlks,
                                                compression='gzip')
            elif k[0] in ['temporal_coherence', 'velocity', 'mask', 'rmse']:
                dset1 = h5file[k[0]].get(k[0])
                unw = dset1[0:dset1.shape[0], 0:dset1.shape[1]]
                unwlks = multilook(unw, alks, rlks)
                dset = group.create_dataset(k[0],
                                            data=unwlks,
                                            compression='gzip')

            try:
                dset1 = h5file['mask'].get('mask')
                Mask = dset1[0:dset1.shape[0], 0:dset1.shape[1]]
                Masklks = multilook(Mask, alks, rlks)
                group = h5file_mli.create_group('mask')
                dset = group.create_dataset('mask',
                                            data=Masklks,
                                            compression='gzip')
            except:
                print 'No mask group found.'

            ## Update attributes
            for key, value in h5file[k[0]].attrs.iteritems():
                group.attrs[key] = value
            group.attrs['WIDTH'] = unwlks.shape[1]
            group.attrs['FILE_LENGTH'] = unwlks.shape[0]

            try:
                group.attrs['Y_STEP'] = alks * float(group.attrs['Y_STEP'])
                group.attrs['X_STEP'] = rlks * float(group.attrs['X_STEP'])
            except:
                group.attrs['AZIMUTH_PIXEL_SIZE'] = alks * float(
                    group.attrs['AZIMUTH_PIXEL_SIZE'])
                group.attrs['RANGE_PIXEL_SIZE'] = rlks * float(
                    group.attrs['RANGE_PIXEL_SIZE'])

        h5file.close()
        h5file_mli.close()

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

    elif ext in ['.unw','.cor','.hgt','.dem','.trans'] or\
         ext in ['.jpeg','.jpg','.png','.ras','.bmp'] or\
         ext in ['.mli','.slc']:
        import pysar._readfile as readfile
        import pysar._writefile as writefile
        r = readfile.read_rsc_file(file + '.rsc')

        if ext == '.int' or ext == '.slc':
            a, p, r = readfile.read_complex64(file)
            pmli = multilook(p, alks, rlks)
            amli = multilook(a, alks, rlks)
            r['FILE_LENGTH'] = str(pmli.shape[0])
            r['WIDTH'] = str(pmli.shape[1])
        elif ext == '.unw' or ext == '.cor' or ext == '.hgt':
            a, p, r = readfile.read_float32(file)
            pmli = multilook(p, alks, rlks)
            amli = multilook(a, alks, rlks)
            print 'writing >>>' + outName
            writefile.write_float32(pmli, outName)
            r['FILE_LENGTH'] = str(pmli.shape[0])
            r['WIDTH'] = str(pmli.shape[1])
        elif ext == ('.dem'):
            d, r = readfile.read_dem(file)
            dmli = multilook(d, alks, rlks)
            print 'writing >>>' + outName
            writefile.write_dem(dmli, outName)
            r['FILE_LENGTH'] = str(dmli.shape[0])
            r['WIDTH'] = str(dmli.shape[1])
        elif ext in ['.jpeg', '.jpg', '.png', '.bmp']:
            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)
            r['FILE_LENGTH'] = str(height)
            r['WIDTH'] = str(width)

        ## Update attributes
        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:
            r['AZIMUTH_PIXEL_SIZE'] = alks * float(r['AZIMUTH_PIXEL_SIZE'])
            r['RANGE_PIXEL_SIZE'] = rlks * float(r['RANGE_PIXEL_SIZE'])

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

    color_map = 'jet'
    disp_opposite = 'no'
    disp_colorbar = 'yes'
    rewrapping = 'no'
    dpi = 500

    if len(sys.argv) > 2:
        try:
            opts, args = getopt.getopt(argv, "f:m:M:d:c:w:i:r:")
        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': dpi = int(arg)

    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

    import matplotlib.pyplot as plt
    ext = os.path.splitext(File)[1]
    map = plt.get_cmap(color_map)

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

        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()[0]].attrs
        elif k[0] in ('dem', 'velocity', 'mask', 'temporal_coherence', 'rmse',
                      'timeseries'):
            atr = h5file[k[0]].attrs
        print 'Input file is ' + k[0]

        if k[0] in ('interferograms', 'wrapped', 'coherence'):
            ifgramList = h5file[k[0]].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[0]][ifgramList[epoch_number]].get(
                ifgramList[epoch_number])
            data = dset[0:dset.shape[0], 0:dset.shape[1]]

            if k[0] == '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
            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[0]].get(k[0])
            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[0]].attrs['ref_x']
                yref = h5file[k[0]].attrs['ref_y']
            except:
                pass

    elif ext in ['.unw', '.cor', '.hgt', '.trans', '.dem']:
        import pysar._readfile as readfile
        if ext in ['.unw', '.cor', '.hgt', '.trans']:
            a, data, atr = readfile.read_float32(File)
            outName = File
        elif ext == '.dem':
            data, atr = readfile.read_dem(File)
            outName = File
    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 'Input file is 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]
    fig = plt.figure()
    fig = plt.figure(frameon=False)
    # fig.set_size_inches(width/1000,length/1000)
    ax = plt.Axes(
        fig,
        [0., 0., 1., 1.],
    )
    ax.set_axis_off()
    fig.add_axes(ax)

    aspect = width / (length * 1.0)
    # ax.imshow(data,aspect='normal')

    try:
        ax.imshow(data, aspect='normal', vmax=Vmax, vmin=Vmin)
    except:
        ax.imshow(data, aspect='normal')

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

    # figName = k[0]+'.png'
    figName = outName + '.png'
    plt.savefig(figName, pad_inches=0.0, dpi=dpi)
    # plt.show()

    ############### Making colorbar
    pc = plt.figure(figsize=(1, 4))
    axc = pc.add_subplot(111)
    cmap = mpl.cm.jet
    norm = mpl.colors.Normalize(vmin=Vmin * 1000, vmax=Vmax * 1000)
    clb = mpl.colorbar.ColorbarBase(axc,
                                    cmap=cmap,
                                    norm=norm,
                                    orientation='vertical')
    clb.set_label('mm/yr')
    pc.subplots_adjust(left=0.25, bottom=0.1, right=0.4, top=0.9)
    pc.savefig('colorbar.png', transparent=True, dpi=300)

    ############## Generate KMZ file
    print 'generating kml file'
    doc = KML.kml(KML.Folder(KML.name('PySAR product')))
    slc = KML.GroundOverlay(KML.name(figName),KML.Icon(KML.href(figName)),\
                            KML.TimeSpan(KML.begin('2003'),KML.end('2010')),\
                            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'
    latdel = North - South
    londel = East - West
    slc1   = KML.GroundOverlay(KML.name('colorbar'),KML.Icon(KML.href('colorbar.png')),\
                               KML.altitude('9000'),KML.altitudeMode('absolute'),\
                               KML.LatLonBox(KML.north(str(North-latdel/2.+0.5)),KML.south(str(South+latdel/2.0-0.5)),\
                                             KML.east(str(West-0.2*londel)),     KML.west(str(West-0.4*londel))))
    doc.Folder.append(slc1)

    #############################
    from lxml import etree
    kmlstr = etree.tostring(doc, pretty_print=True)
    # kmlname=k[0]+'.kml'
    kmlname = outName + '.kml'
    print 'writing ' + kmlname
    kmlfile = open(kmlname, 'w')
    kmlfile.write(kmlstr)
    kmlfile.close()

    # kmzName = k[0]+'.kmz'
    kmzName = outName + '.kmz'
    print 'writing ' + kmzName
    # cmdKMZ = 'zip ' + kmzName +' '+ kmlname +' ' + figName
    cmdKMZ = 'zip ' + kmzName + ' ' + kmlname + ' ' + figName + ' colorbar.png'
    os.system(cmdKMZ)

    cmdClean = 'rm ' + kmlname
    os.system(cmdClean)
    cmdClean = 'rm ' + figName
    os.system(cmdClean)
    cmdClean = 'rm colorbar.png'
    os.system(cmdClean)
Esempio n. 30
0
def main(argv):

    ########### Check Inputs #############
    try:
        file = sys.argv[1]
        operator = sys.argv[2]
        operand = float(sys.argv[3])
    except:
        usage()
        sys.exit(1)

    if operator in ['+', 'plus', 'add', 'addition']: operator = 'plus'
    elif operator in ['-', 'minus', 'substract', 'substraction']:
        operator = 'minus'
    elif operator in ['*', 'times', 'multiply', 'multiplication']:
        operator = 'multiply'
    elif operator in ['/', 'obelus', 'divide', 'division']:
        operator = 'divide'
    elif operator in ['^', 'exp', 'exponential']:
        operator = 'exp'
    else:
        print 'ERROR: Unrecognized operator: ' + operator
        sys.exit(1)
    print '\n*************** Image Math ******************'
    print 'operation: ' + operator + ' ' + str(operand)

    ext = os.path.splitext(file)[1]
    try:
        outName = sys.argv[4]
    except:
        outName = file.split('.')[0] + '_' + operator + str(operand) + ext

    ########### Read - Calculate - Write  ###########
    ##### PySAR HDF5 files ######
    if ext in ['.h5', '.he5']:
        try:
            h5file = h5py.File(file, 'r')
        except:
            print 'ERROR: can not open file: ' + file
            sys.exit(1)
        k = h5file.keys()
        if 'interferograms' in k: k[0] = 'interferograms'
        elif 'coherence' in k: k[0] = 'coherence'
        elif 'timeseries' in k: k[0] = 'timeseries'
        print 'Input file is ' + k[0]

        h5fileOut = h5py.File(outName, 'w')
        print 'writing >>> ' + outName
        group = h5fileOut.create_group(k[0])

        if k[0] in ('velocity', 'temporal_coherence', 'rmse', 'mask', 'dem'):
            dset = h5file[k[0]].get(k[0])
            data = dset[0:dset.shape[0], 0:dset.shape[1]]

            dataOut = operation(data, operator, operand)

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

        elif k[0] == 'timeseries':
            dateList = h5file[k[0]].keys()
            print 'number of dates: ' + str(len(dateList))
            for date in dateList:
                print date
                dset = h5file[k[0]].get(date)
                data = dset[0:dset.shape[0], 0:dset.shape[1]]

                dataOut = operation(data, operator, operand)

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

        elif k[0] in ['interferograms', 'coherence', 'wrapped']:
            ifgramList = h5file[k[0]].keys()
            print 'number of epochs: ' + str(len(ifgramList))
            for igram in ifgramList:
                print igram
                dset = h5file[k[0]][igram].get(igram)
                data = dset[0:dset.shape[0], 0:dset.shape[1]]

                dataOut = operation(data, operator, operand)

                group2 = group.create_group(igram)
                dset = group2.create_dataset(igram,
                                             data=dataOut,
                                             compression='gzip')
                for key, value in h5file[k[0]][igram].attrs.iteritems():
                    group2.attrs[key] = value

            try:
                mask = h5file['mask'].get('mask')
                gm = h5fileOut.create_group('mask')
                dset = gm.create_dataset('mask', data=mask, compression='gzip')
            except:
                print 'No group for mask found in the file.'

            try:
                Cset = h5file['meanCoherence'].get('meanCoherence')
                gm = h5fileOut.create_group('meanCoherence')
                dset = gm.create_dataset('meanCoherence',
                                         data=Cset,
                                         compression='gzip')
            except:
                print 'No group for meanCoherence found in the file'

        else:
            print 'ERROR: Unrecognized HDF5 file type: ' + k[0]
            sys.exit(1)

        h5file.close()
        h5fileOut.close()

    ##### ROI_PAC files #######
    elif ext in ['.unw', '.cor', '.hgt', '.dem', '.trans']:
        print 'Input file is ' + ext + '\nwriting >>> ' + outName
        if ext in ['.unw', '.cor', '.hgt']:
            a, p, r = readfile.read_float32(file)
            p2 = operation(p, operator, operand)
            writefile.write_float32(p2, outName)
        elif ext == '.dem':
            p, r = readfile.read_real_int16(file)
            p2 = operation(p, operator, operand)
            writefile.write_real_int16(p2, outName)
        elif ext == '.trans':
            a, p, r = readfile.read_float32(file)
            a2 = operation(a, operator, operand)
            p2 = operation(p, operator, operand)
            writefile.write_float32(a2, p2, outName)

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

    else:
        print 'ERROR: Unrecognized file extension: ' + ext
        sys.exit(1)

    print 'Done.'
Esempio n. 31
0
  Example:
      correlation_with_dem.py radar_8rlks.hgt  velocity_masked.h5

***********************************************************************
    '''


try:
    demFile = sys.argv[1]
    File = sys.argv[2]
except:
    usage()
    sys.exit(1)

if os.path.basename(demFile).split('.')[1] == 'hgt':
    amp, dem, demRsc = readfile.read_float32(demFile)

elif os.path.basename(demFile).split('.')[1] == 'dem':
    dem, demRsc = readfile.read_real_int16(demFile)

#amp,dem,demRsc = readfile.read_float32(demFile)
h5data = h5py.File(File)
dset = h5data['velocity'].get('velocity')
data = dset[0:dset.shape[0], 0:dset.shape[1]]

try:
    suby = sys.argv[3].split(':')
    subx = sys.argv[4].split(':')
    data = data[int(suby[0]):int(suby[1]), int(subx[0]):int(subx[1])]
    dem = dem[int(suby[0]):int(suby[1]), int(subx[0]):int(subx[1])]
except:
Esempio n. 32
0
def main(argv):

    try:
        File = argv[0]
        demFile = argv[1]
        p = int(argv[2])
    except:
        usage()
        sys.exit(1)

    try:
        baseline_error = argv[3]
    except:
        baseline_error = 'range_and_azimuth'
    print baseline_error
    ##################################
    h5file = h5py.File(File)
    dateList = h5file['timeseries'].keys()
    ##################################

    try:
        maskFile = argv[4]
    except:
        if os.path.isfile('Modified_Mask.h5'): maskFile = 'Modified_Mask.h5'
        elif os.path.isfile('Mask.h5'): maskFile = 'Mask.h5'
        else:
            print 'No mask found!'
            sys.exit(1)
    try:
        Mask, Matr = readfile.read(maskFile)
        print 'mask: ' + maskFile
    except:
        print 'Can not open mask file: ' + maskFile
        sys.exit(1)

    #try:
    #  maskFile=argv[4]
    #  h5Mask = h5py.File(maskFile,'r')
    #  kMask=h5Mask.keys()
    #  dset1 = h5Mask[kMask[0]].get(kMask[0])
    #  Mask = dset1[0:dset1.shape[0],0:dset1.shape[1]]
    #except:
    #  dset1 = h5file['mask'].get('mask')
    #  Mask = dset1[0:dset1.shape[0],0:dset1.shape[1]]

    ##################################
    Mask = Mask.flatten(1)
    ndx = Mask != 0
    ##################################
    # h5file = h5py.File(File)
    # dateList = h5file['timeseries'].keys()
    ##################################
    nt = float(h5file['timeseries'].attrs['LOOK_REF1'])
    ft = float(h5file['timeseries'].attrs['LOOK_REF2'])
    sy, sx = np.shape(dset1)
    npixel = sx * sy
    lookangle = np.tile(np.linspace(nt, ft, sx), [sy, 1])
    lookangle = lookangle.flatten(1) * np.pi / 180.0
    Fh = -np.sin(lookangle)
    Fv = -np.cos(lookangle)

    print 'Looking for azimuth pixel size'
    try:
        daz = float(h5file['timeseries'].attrs['AZIMUTH_PIXEL_SIZE'])
    except:
        print '''
        ERROR!
        The attribute AZIMUTH_PIXEL_SIZE was not found!
        Possible cause of error: Geo coordinate.
        This function works only in radar coordinate system.
        '''
        sys.exit(1)

    lines = np.tile(np.arange(0, sy, 1), [1, sx])
    lines = lines.flatten(1)
    rs = lines * daz

    if baseline_error == 'range_and_azimuth':
        A = np.zeros([npixel, 4])

        A[:, 0] = Fh
        A[:, 1] = Fh * rs
        A[:, 2] = Fv
        A[:, 3] = Fv * rs
        num_base_par = 4
    elif baseline_error == 'range':
        A = np.zeros([npixel, 2])

        A[:, 0] = Fh
        A[:, 1] = Fv
        num_base_par = 2

    ###########################################
    yref = int(h5file['timeseries'].attrs['ref_y'])
    xref = int(h5file['timeseries'].attrs['ref_x'])
    ###########################################
    if os.path.basename(demFile).split('.')[1] == 'hgt':
        amp, dem, demRsc = readfile.read_float32(demFile)
    elif os.path.basename(demFile).split('.')[1] == 'dem':
        dem, demRsc = readfile.read_real_int16(demFile)

    dem = dem - dem[yref][xref]
    dem = dem.flatten(1)
    ###################################################
    if p == 1:
        # A=np.vstack((dem[ndx],np.ones(len(dem[ndx])))).T
        B = np.vstack((dem, np.ones(len(dem)))).T
    elif p == 2:
        # A=np.vstack((dem[ndx]**2,dem[ndx],np.ones(len(dem[ndx])))).T
        B = np.vstack((dem**2, dem, np.ones(len(dem)))).T
    elif p == 3:
        #  A = np.vstack((dem[ndx]**3,dem[ndx]**2,dem[ndx],np.ones(len(dem[ndx])))).T
        B = np.vstack((dem**3, dem**2, dem, np.ones(len(dem)))).T
    print np.shape(A)

    Ainv = np.linalg.pinv(A)
    ###################################################

    Bh = []
    Bv = []
    Bhrate = []
    Bvrate = []
    Be = np.zeros([len(dateList), num_base_par + p + 1])
    print '%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%'
    for i in range(1, len(dateList)):
        dset = h5file['timeseries'].get(dateList[i])
        data = dset[0:dset.shape[0], 0:dset.shape[1]]
        L = data.flatten(1)
        M = np.hstack((A, B))
        Berror = np.dot(np.linalg.pinv(M[ndx]), L[ndx])
        Bh.append(Berror[0])
        Bhrate.append(Berror[1])
        Bv.append(Berror[2])
        Bvrate.append(Berror[3])
        Be[i, :] = Berror
        print Berror
    print '%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%'
    print 'baseline error           mean                          std'
    print '       bh     :  ' + str(np.mean(Bh)) + '     ,  ' + str(np.std(Bh))
    print '     bh rate  :  ' + str(np.mean(Bhrate)) + '     ,  ' + str(
        np.std(Bhrate))
    print '       bv     :  ' + str(np.mean(Bv)) + '     ,  ' + str(np.std(Bv))
    print '     bv rate  :  ' + str(np.mean(Bvrate)) + '     ,  ' + str(
        np.std(Bvrate))
    print '%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%'
    # plt.hist(Bh,bins=8,normed=True)
    # formatter = FuncFormatter(to_percent)
    # Set the formatter
    # plt.gca().yaxis.set_major_formatter(formatter)
    # plt.show()
    print '%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%'
    # print 'Estimating Baseline error from each differences ...'

    orbEffect = np.zeros([len(dateList), sy, sx])
    for i in range(1, len(dateList)):
        effect = np.dot(M, Be[i, :])
        effect = np.reshape(effect, [sx, sy]).T
        # orbEffect[i,:,:]=orbEffect[i-1,:,:]+effect
        # orbEffect[i,:,:]=orbEffect[i,:,:]-orbEffect[i,yref,xref]
        orbEffect[i, :, :] = effect - effect[yref, xref]
        del effect

    print 'Correctiing the time series '
    outName = File.replace('.h5', '') + '_baseTropCor.h5'
    h5orbCor = h5py.File(outName, 'w')
    group = h5orbCor.create_group('timeseries')
    for i in range(len(dateList)):
        dset1 = h5file['timeseries'].get(dateList[i])
        data = dset1[0:dset1.shape[0], 0:dset1.shape[1]] - orbEffect[i, :, :]
        dset = group.create_dataset(dateList[i], data=data, compression='gzip')

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

    dset1 = h5file['mask'].get('mask')
    group = h5orbCor.create_group('mask')
    dset = group.create_dataset('mask', data=dset1, compression='gzip')

    h5file.close()
    h5orbCor.close()
Esempio n. 33
0
def main(argv):

    color_map = "jet"
    disp_opposite = "no"
    disp_colorbar = "yes"
    rewrapping = "no"
    dpi = 500

    if len(sys.argv) > 2:
        try:
            opts, args = getopt.getopt(argv, "f:m:M:d:c:w:i:r:")
        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":
                dpi = int(arg)

    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

    import matplotlib.pyplot as plt

    ext = os.path.splitext(File)[1]
    map = plt.get_cmap(color_map)

    if ext == ".h5":
        import h5py

        try:
            h5file = h5py.File(File, "r")
        except:
            Usage()
            sys.exit(1)
        outName = File.split(".")[0]

        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()[0]].attrs
        elif k[0] in ("dem", "velocity", "mask", "temporal_coherence", "rmse", "timeseries"):
            atr = h5file[k[0]].attrs
        print "Input file is " + k[0]

        if k[0] in ("interferograms", "wrapped", "coherence"):
            ifgramList = h5file[k[0]].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[0]][ifgramList[epoch_number]].get(ifgramList[epoch_number])
            data = dset[0 : dset.shape[0], 0 : dset.shape[1]]

            if k[0] == "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
            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[0]].get(k[0])
            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[0]].attrs["ref_x"]
                yref = h5file[k[0]].attrs["ref_y"]
            except:
                pass

    elif ext in [".unw", ".cor", ".hgt", ".trans", ".dem"]:
        import pysar._readfile as readfile

        if ext in [".unw", ".cor", ".hgt", ".trans"]:
            a, data, atr = readfile.read_float32(File)
            outName = File
        elif ext == ".dem":
            data, atr = readfile.read_dem(File)
            outName = File
    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 "Input file is 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]
    fig = plt.figure()
    fig = plt.figure(frameon=False)
    # fig.set_size_inches(width/1000,length/1000)
    ax = plt.Axes(fig, [0.0, 0.0, 1.0, 1.0])
    ax.set_axis_off()
    fig.add_axes(ax)

    aspect = width / (length * 1.0)
    # ax.imshow(data,aspect='normal')

    try:
        ax.imshow(data, aspect="normal", vmax=Vmax, vmin=Vmin)
    except:
        ax.imshow(data, aspect="normal")

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

    # figName = k[0]+'.png'
    figName = outName + ".png"
    plt.savefig(figName, pad_inches=0.0, dpi=dpi)
    # plt.show()

    ############### Making colorbar
    pc = plt.figure(figsize=(1, 4))
    axc = pc.add_subplot(111)
    cmap = mpl.cm.jet
    norm = mpl.colors.Normalize(vmin=Vmin * 1000, vmax=Vmax * 1000)
    clb = mpl.colorbar.ColorbarBase(axc, cmap=cmap, norm=norm, orientation="vertical")
    clb.set_label("mm/yr")
    pc.subplots_adjust(left=0.25, bottom=0.1, right=0.4, top=0.9)
    pc.savefig("colorbar.png", transparent=True, dpi=300)

    ############## Generate KMZ file
    print "generating kml file"
    doc = KML.kml(KML.Folder(KML.name("PySAR product")))
    slc = KML.GroundOverlay(
        KML.name(figName),
        KML.Icon(KML.href(figName)),
        KML.TimeSpan(KML.begin("2003"), KML.end("2010")),
        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"
    latdel = North - South
    londel = East - West
    slc1 = KML.GroundOverlay(
        KML.name("colorbar"),
        KML.Icon(KML.href("colorbar.png")),
        KML.altitude("9000"),
        KML.altitudeMode("absolute"),
        KML.LatLonBox(
            KML.north(str(North - latdel / 2.0 + 0.5)),
            KML.south(str(South + latdel / 2.0 - 0.5)),
            KML.east(str(West - 0.2 * londel)),
            KML.west(str(West - 0.4 * londel)),
        ),
    )
    doc.Folder.append(slc1)

    #############################
    from lxml import etree

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

    # kmzName = k[0]+'.kmz'
    kmzName = outName + ".kmz"
    print "writing " + kmzName
    # cmdKMZ = 'zip ' + kmzName +' '+ kmlname +' ' + figName
    cmdKMZ = "zip " + kmzName + " " + kmlname + " " + figName + " colorbar.png"
    os.system(cmdKMZ)

    cmdClean = "rm " + kmlname
    os.system(cmdClean)
    cmdClean = "rm " + figName
    os.system(cmdClean)
    cmdClean = "rm colorbar.png"
    os.system(cmdClean)