コード例 #1
0
ファイル: save_gmt.py プロジェクト: nillei/PySAR
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')
コード例 #2
0
def main(argv):
    try:
        dem_file = argv[1]
        dem_error = argv[2]
    except:
        usage()
        sys.exit(1)

    dem, demrsc = _readfile.read_real_int16(dem_file)
    g = h5py.File(dem_error, 'r')
    dset = g['dem'].get('dem')
    dem_error = dset[0:dset.shape[0]]

    print 'Correcting the DEM'
    sum = dem + dem_error
    print 'Creating the new DEM'
    _writefile.write_real_int16(sum, 'DEM_w_error.dem')

    rsc_file = open('DEM_w_error.dem.rsc', 'w')
    for k in demrsc.keys():
        rsc_file.write(k + '	' + demrsc[k] + '\n')
        rsc_file.close

    date12_file = open('111111-222222_baseline.rsc', 'w')
    date12_file.write('P_BASELINE_TOP_ODR' + '     ' + '000')
    date12_file.close
コード例 #3
0
ファイル: save_gmt.py プロジェクト: yunjunz/PySAR
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')
コード例 #4
0
ファイル: correct_dem.py プロジェクト: yunjunz/PySAR
def main(argv):
    try:
        dem_file = argv[1]
        dem_error = argv[2]
    except:
        print '''
        *******************************************
           
           Usage: correct_dem.py demFile geo_demErrorFile
           Example:
                  correct_dem.py $DEMDIR/Socorro-30/Socorro_30.dem geo_DEM_error.h5
                  correct_dem.py $DEMDIR/Socorro-30/Socorro_30.dem geo_DEM_error.h5
    
        *******************************************         
        '''
        sys.exit(1)
  
    
  
    dem, demrsc = _readfile.read_real_int16(dem_file)
    g = h5py.File(dem_error,'r')
    dset  = g['dem'].get('dem')
    dem_error = dset[0:dset.shape[0]]

    print 'Correcting the DEM'
    sum = dem + dem_error
    print 'Creating the new DEM'
    _writefile.write_real_int16(sum,'DEM_w_error.dem')
          
    rsc_file = open('DEM_w_error.dem.rsc','w')
    for k in demrsc.keys():
        rsc_file.write(k+'	'+demrsc[k]+'\n')
        rsc_file.close
          
    date12_file=open('111111-222222_baseline.rsc','w')
    date12_file.write('P_BASELINE_TOP_ODR'+'     '+ '000')
    date12_file.close
コード例 #5
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()
コード例 #6
0
ファイル: filter_spatial.py プロジェクト: yunjunz/PySAR
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
コード例 #7
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.'
コード例 #8
0
ファイル: filter_spatial.py プロジェクト: nillei/PySAR
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
コード例 #9
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
コード例 #10
0
ファイル: baseline_trop.py プロジェクト: yunjunz/PySAR
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()
コード例 #11
0
ファイル: save_kml.py プロジェクト: yunjunz/PySAR
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)
コード例 #12
0
***********************************************************************
    '''


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:
    print 'no subset'

dem = dem.flatten(1)
コード例 #13
0
ファイル: load_dem.py プロジェクト: yunjunz/PySAR
  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

group.attrs['ref_y']=0
コード例 #14
0
ファイル: image_math.py プロジェクト: yunjunz/PySAR
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.'
コード例 #15
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)
コード例 #16
0
ファイル: correlation_with_dem.py プロジェクト: yunjunz/PySAR
***********************************************************************
    '''

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:
    print 'no subset'

dem=dem.flatten(1)