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 # try: # file=argv[0] # alks=float(argv[1]) # rlks=float(argv[2]) # except: # Usage();sys.exit(1) 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_complex64(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_dem(file) dlks = multilook(d, alks, rlks) print "writing " + outName writefile.write_dem(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_rsc_file(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()
def main(argv): try: file = argv[0] alks = float(argv[1]) rlks = float(argv[2]) 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 == '.int' or ext == '.slc': a, p, r = readfile.read_complex64(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_dem(file) dlks = multilook(d, alks, rlks) print 'writing ' + outName writefile.write_dem(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_rsc_file(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 'Multilooking the interferograms' gg = h5file_lks.create_group('interferograms') igramList = h5file['interferograms'].keys() for igram in igramList: print igram unw = h5file['interferograms'][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['interferograms'][ 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']) 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=masklks, compression='gzip') elif 'timeseries' in h5file.keys(): print 'Multilooking the time-series' group = h5file_lks.create_group('timeseries') dateList = h5file['timeseries'].keys() for d in dateList: print d unw = h5file['timeseries'].get(d) unwlks = multilook(unw, alks, rlks) dset = group.create_dataset(d, data=unwlks, compression='gzip') for key, value in h5file['timeseries'].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_lks.create_group('mask') dset = group.create_dataset('mask', data=Masklks, compression='gzip') except: print 'Multilooked 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 'multi looking the ' + k[0] group = h5file_lks.create_group(k[0]) dset1 = h5file[k[0]].get(k[0]) Mask = dset1[0:dset1.shape[0], 0:dset1.shape[1]] Masklks = multilook(Mask, alks, rlks) dset = group.create_dataset(k[0], data=Masklks, compression='gzip') for key, value in h5file[k[0]].attrs.iteritems(): group.attrs[key] = value 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']) group.attrs['WIDTH'] = Masklks.shape[1] group.attrs['FILE_LENGTH'] = Masklks.shape[0] h5file.close() h5file_lks.close()
def main(argv): try: timeSeriesFile = argv[0] igramsFile = argv[1] except: Usage() ; sys.exit(1) # projectName = os.path.basename(templateFile.partition('.')[0]) # tssarDirectory = os.getenv('TSSARDIR') # projectDir=tssarDirectory+'/'+projectName # os.chdir(projectDir) # timeSeriesFile='timeseries_'+projectName+'.h5' ######################################################## print "Loading time series: " + timeSeriesFile h5timeseries = h5py.File(timeSeriesFile) dateList = h5timeseries['timeseries'].keys() dateIndex={} for ni in range(len(dateList)): dateIndex[dateList[ni]]=ni dset = h5timeseries['timeseries'].get(h5timeseries['timeseries'].keys()[0]) nrows,ncols=np.shape(dset) timeseries = np.zeros((len(h5timeseries['timeseries'].keys()),np.shape(dset)[0]*np.shape(dset)[1]),np.float32) for date in dateList: dset = h5timeseries['timeseries'].get(date) d = dset[0:dset.shape[0],0:dset.shape[1]] timeseries[dateIndex[date]][:]=d.flatten(0) del d # h5timeseries.close() lt,numpixels=np.shape(timeseries) ###################################################### tbase=np.zeros((lt,1)) d1 = datetime.datetime(*time.strptime(dateList[0],"%Y%m%d")[0:5]) for ni in range(len(dateList)): d2 = datetime.datetime(*time.strptime(dateList[ni],"%Y%m%d")[0:5]) diff = d2-d1 tbase[ni]=diff.days ##################################################### Bperp=np.zeros([lt,1]) #Bh,Bv = ut.Bh_Bv_timeseries(igramsFile) #Theta_Near = float(h5timeseries['timeseries'].attrs['LOOK_REF1'])*np.pi/180. #Theta_Far = float(h5timeseries['timeseries'].attrs['LOOK_REF2'])*np.pi/180. #Theta=(Theta_Near+Theta_Far)/2 #Bp=Bh*np.sin(Theta)-Bv*np.cos(Theta) Bp = ut.Baseline_timeseries(igramsFile) for i in range(lt): Bperp[i][0]=Bp[i] #################################################### # bl_list=np.loadtxt(baselineFile,usecols = (0,1),dtype=str) # Bperp=np.zeros([lt,1]) # print np.shape(bl_list) # for i in range(np.shape(bl_list)[0]): # Bperp[i]=float(bl_list[i][1]) # Bperp=Bperp-Bperp[0][0] # print Bperp # print Heresh #################################################### # teta = (tetaN+tetaF)/2 # r = (rN+rF)/2 # teta=19.658799999999999*np.pi/180 # r=846848.2 # Bperp=1000*np.random.random((lt,1)) h5file = h5py.File(igramsFile) igramList = h5file['interferograms'].keys() try: r=float(h5file['interferograms'][igramList[0]].attrs['STARTING_RANGE1']) theta=(float(h5file['interferograms'][igramList[0]].attrs['LOOK_REF1'])+float(h5file['interferograms'][igramList[0]].attrs['LOOK_REF2']))/2 theta=theta*np.pi/180 C1=Bperp/r/np.sin(theta) except: C1=Bperp # tbase=np.array(tbase) ################################################# # print np.shape(C1) M=np.hstack((.5*tbase**2,tbase,np.ones((lt,1)))) C=np.hstack((M,C1)) print 'rank of the design matrix : '+str(np.linalg.matrix_rank(C)) if np.linalg.matrix_rank(C) ==4: print 'Design matrix has full rank' Cinv=np.linalg.pinv(C) par=np.dot(Cinv,timeseries) dz=np.zeros([1,numpixels]) dz[0][:]=par[3][:] # print np.shape(dz) timeseries=timeseries-np.dot(C1,dz) dz=np.reshape(dz,[nrows,ncols]) print '**************************************' print 'writing DEM_error.hgt' writefile.write_float32(dz,'DEM_error.hgt') f = open('DEM_error.hgt.rsc','w') f.write('FILE_LENGTH '+str(int(nrows))+'\n') f.write('WIDTH '+str(int(ncols))+'\n') print '**************************************' ################################################### print 'writing DEM_error.h5' h5fileDEM = 'DEM_error.h5' h5rmse = h5py.File(h5fileDEM,'w') group=h5rmse.create_group('dem') dset = group.create_dataset(os.path.basename('dem'), data=dz, compression='gzip') for key , value in h5timeseries['timeseries'].attrs.iteritems(): group.attrs[key]=value print '**************************************' ################################################## try: outname=argv[2] except: outname=timeSeriesFile.replace('.h5','')+'_demCor.h5' print 'writing '+outname h5timeseriesDEMcor = h5py.File(outname,'w') group = h5timeseriesDEMcor.create_group('timeseries') for date in dateList: print date if not date in h5timeseriesDEMcor['timeseries']: dset = group.create_dataset(date, data=np.reshape(timeseries[dateIndex[date]][:],[nrows,ncols]), compression='gzip') # group = h5timeseriesDEMcor.create_group('timeseries') for key,value in h5timeseries['timeseries'].attrs.iteritems(): group.attrs[key] = value dset1 = h5timeseries['mask'].get('mask') Mask = dset1[0:dset1.shape[0],0:dset1.shape[1]] group=h5timeseriesDEMcor.create_group('mask') dset = group.create_dataset('mask', data=Mask, compression='gzip') h5timeseriesDEMcor.close() h5timeseries.close()
def main(argv): try: timeSeriesFile = argv[0] igramsFile = argv[1] except: Usage() sys.exit(1) # projectName = os.path.basename(templateFile.partition('.')[0]) # tssarDirectory = os.getenv('TSSARDIR') # projectDir=tssarDirectory+'/'+projectName # os.chdir(projectDir) # timeSeriesFile='timeseries_'+projectName+'.h5' ######################################################## print "Loading time series: " + timeSeriesFile h5timeseries = h5py.File(timeSeriesFile) dateList = h5timeseries['timeseries'].keys() dateIndex = {} for ni in range(len(dateList)): dateIndex[dateList[ni]] = ni dset = h5timeseries['timeseries'].get(h5timeseries['timeseries'].keys()[0]) nrows, ncols = np.shape(dset) timeseries = np.zeros((len(h5timeseries['timeseries'].keys()), np.shape(dset)[0] * np.shape(dset)[1]), np.float32) for date in dateList: dset = h5timeseries['timeseries'].get(date) d = dset[0:dset.shape[0], 0:dset.shape[1]] timeseries[dateIndex[date]][:] = d.flatten(0) del d # h5timeseries.close() lt, numpixels = np.shape(timeseries) ###################################################### tbase = np.zeros((lt, 1)) d1 = datetime.datetime(*time.strptime(dateList[0], "%Y%m%d")[0:5]) for ni in range(len(dateList)): d2 = datetime.datetime(*time.strptime(dateList[ni], "%Y%m%d")[0:5]) diff = d2 - d1 tbase[ni] = diff.days ##################################################### Bperp = np.zeros([lt, 1]) #Bh,Bv = ut.Bh_Bv_timeseries(igramsFile) #Theta_Near = float(h5timeseries['timeseries'].attrs['LOOK_REF1'])*np.pi/180. #Theta_Far = float(h5timeseries['timeseries'].attrs['LOOK_REF2'])*np.pi/180. #Theta=(Theta_Near+Theta_Far)/2 #Bp=Bh*np.sin(Theta)-Bv*np.cos(Theta) Bp = ut.Baseline_timeseries(igramsFile) for i in range(lt): Bperp[i][0] = Bp[i] #################################################### # bl_list=np.loadtxt(baselineFile,usecols = (0,1),dtype=str) # Bperp=np.zeros([lt,1]) # print np.shape(bl_list) # for i in range(np.shape(bl_list)[0]): # Bperp[i]=float(bl_list[i][1]) # Bperp=Bperp-Bperp[0][0] # print Bperp # print Heresh #################################################### # teta = (tetaN+tetaF)/2 # r = (rN+rF)/2 # teta=19.658799999999999*np.pi/180 # r=846848.2 # Bperp=1000*np.random.random((lt,1)) h5file = h5py.File(igramsFile) igramList = h5file['interferograms'].keys() try: r = float( h5file['interferograms'][igramList[0]].attrs['STARTING_RANGE1']) theta = (float( h5file['interferograms'][igramList[0]].attrs['LOOK_REF1']) + float( h5file['interferograms'][igramList[0]].attrs['LOOK_REF2'])) / 2 theta = theta * np.pi / 180 C1 = Bperp / r / np.sin(theta) except: C1 = Bperp # tbase=np.array(tbase) ################################################# # print np.shape(C1) M = np.hstack((.5 * tbase**2, tbase, np.ones((lt, 1)))) C = np.hstack((M, C1)) print 'rank of the design matrix : ' + str(np.linalg.matrix_rank(C)) if np.linalg.matrix_rank(C) == 4: print 'Design matrix has full rank' Cinv = np.linalg.pinv(C) par = np.dot(Cinv, timeseries) dz = np.zeros([1, numpixels]) dz[0][:] = par[3][:] # print np.shape(dz) timeseries = timeseries - np.dot(C1, dz) dz = np.reshape(dz, [nrows, ncols]) print '**************************************' print 'writing DEM_error.hgt' writefile.write_float32(dz, 'DEM_error.hgt') f = open('DEM_error.hgt.rsc', 'w') f.write('FILE_LENGTH ' + str(int(nrows)) + '\n') f.write('WIDTH ' + str(int(ncols)) + '\n') print '**************************************' ################################################### print 'writing DEM_error.h5' h5fileDEM = 'DEM_error.h5' h5rmse = h5py.File(h5fileDEM, 'w') group = h5rmse.create_group('dem') dset = group.create_dataset(os.path.basename('dem'), data=dz, compression='gzip') for key, value in h5timeseries['timeseries'].attrs.iteritems(): group.attrs[key] = value print '**************************************' ################################################## try: outname = argv[2] except: outname = timeSeriesFile.replace('.h5', '') + '_demCor.h5' print 'writing ' + outname h5timeseriesDEMcor = h5py.File(outname, 'w') group = h5timeseriesDEMcor.create_group('timeseries') for date in dateList: print date if not date in h5timeseriesDEMcor['timeseries']: dset = group.create_dataset(date, data=np.reshape( timeseries[dateIndex[date]][:], [nrows, ncols]), compression='gzip') # group = h5timeseriesDEMcor.create_group('timeseries') for key, value in h5timeseries['timeseries'].attrs.iteritems(): group.attrs[key] = value dset1 = h5timeseries['mask'].get('mask') Mask = dset1[0:dset1.shape[0], 0:dset1.shape[1]] group = h5timeseriesDEMcor.create_group('mask') dset = group.create_dataset('mask', data=Mask, compression='gzip') h5timeseriesDEMcor.close() h5timeseries.close()
def main(argv): #outName='subsetIgrams.h5' 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() ##################################################### try: File xsub ysub except: try: File Latsub Lonsub except: Usage();sys.exit(1) try: outName except: outName='subset_'+File ext = os.path.splitext(File)[1] if ext == '.h5': try: h5file=h5py.File(File,'r') except: Usage() ; sys.exit(1) k=h5file.keys() # convert LatLon to xy for geocoded file try: Latsub Lonsub if 'X_FIRST' in h5file[k[0]].attrs.keys(): xsub=[0]*2 ysub=[0]*2 xsub[0]=int((Lonsub[0]-float(h5file[k[0]].attrs['X_FIRST']))/float(h5file[k[0]].attrs['X_STEP'])) xsub[1]=int((Lonsub[1]-float(h5file[k[0]].attrs['X_FIRST']))/float(h5file[k[0]].attrs['X_STEP'])) ysub[0]=int((Latsub[1]-float(h5file[k[0]].attrs['Y_FIRST']))/float(h5file[k[0]].attrs['Y_STEP'])) ysub[1]=int((Latsub[0]-float(h5file[k[0]].attrs['Y_FIRST']))/float(h5file[k[0]].attrs['Y_STEP'])) print 'Subseting geocoded',ext,' file with Latitude and Longitude...' elif 'X_FIRST' in h5file[k[0]][h5file[k[0]].keys()[0]].attrs.keys(): # for geocoded interferograms/coherence igramList=h5file[k[0]].keys() xsub=[0]*2 ysub=[0]*2 xsub[0]=int((Lonsub[0]-float(h5file[k[0]][igramList[0]].attrs['X_FIRST']))/float(h5file[k[0]][igramList[0]].attrs['X_STEP'])) xsub[1]=int((Lonsub[1]-float(h5file[k[0]][igramList[0]].attrs['X_FIRST']))/float(h5file[k[0]][igramList[0]].attrs['X_STEP'])) ysub[0]=int((Latsub[1]-float(h5file[k[0]][igramList[0]].attrs['Y_FIRST']))/float(h5file[k[0]][igramList[0]].attrs['Y_STEP'])) ysub[1]=int((Latsub[0]-float(h5file[k[0]][igramList[0]].attrs['Y_FIRST']))/float(h5file[k[0]][igramList[0]].attrs['Y_STEP'])) print 'Subseting geocoded',ext,' file with Latitude and Longitude...' else: print 'Not geocoded file, cannot be subseted with LatLon.' Usage() ; sys.exit(1) except: Geo=0 # k=h5file.keys() if 'interferograms' in k: igramList=h5file['interferograms'].keys() h5out=h5py.File(outName,'w') gg=h5out.create_group('interferograms') for igram in igramList: print igram dset1=h5file['interferograms'][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['interferograms'][igram].attrs.iteritems(): group.attrs[key] = value group.attrs['FILE_LENGTH']=shape(dset1[ysub[0]:ysub[1],xsub[0]:xsub[1]])[0] group.attrs['WIDTH']=shape(dset1[ysub[0]:ysub[1],xsub[0]:xsub[1]])[1] group.attrs['subset_x0']=xsub[0] group.attrs['subset_x1']=xsub[1] group.attrs['subset_y0']=ysub[0] group.attrs['subset_y1']=ysub[1] if 'X_FIRST' in h5file['interferograms'][igram].attrs.keys(): group.attrs['X_FIRST']=float(h5file['interferograms'][igram].attrs['X_FIRST']) + xsub[0]*float(h5file['interferograms'][igram].attrs['X_STEP']) group.attrs['Y_FIRST']=float(h5file['interferograms'][igram].attrs['Y_FIRST']) + ysub[0]*float(h5file['interferograms'][igram].attrs['Y_STEP']) gm=h5out.create_group('mask') try: Mset=h5file['mask'].get('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! It may cause problem in other processing steps.' 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 average coherence found in the File' elif k[0] in ('coherence','wrapped'): corList=h5file[k[0]].keys() h5out=h5py.File(outName,'w') gg=h5out.create_group(k[0]) for cor in corList: print cor dset1=h5file[k[0]][cor].get(cor) group=gg.create_group(cor) dset=group.create_dataset(cor, data=dset1[ysub[0]:ysub[1],xsub[0]:xsub[1]], compression='gzip') for key, value in h5file[k[0]][cor].attrs.iteritems(): group.attrs[key] = value group.attrs['FILE_LENGTH']=shape(dset1[ysub[0]:ysub[1],xsub[0]:xsub[1]])[0] group.attrs['WIDTH']=shape(dset1[ysub[0]:ysub[1],xsub[0]:xsub[1]])[1] group.attrs['subset_x0']=xsub[0] group.attrs['subset_x1']=xsub[1] group.attrs['subset_y0']=ysub[0] group.attrs['subset_y1']=ysub[1] if 'X_FIRST' in h5file[k[0]][cor].attrs.keys(): group.attrs['X_FIRST']=float(h5file[k[0]][cor].attrs['X_FIRST']) + xsub[0]*float(h5file[k[0]][cor].attrs['X_STEP']) group.attrs['Y_FIRST']=float(h5file[k[0]][cor].attrs['Y_FIRST']) + ysub[0]*float(h5file[k[0]][cor].attrs['Y_STEP']) elif 'timeseries' in h5file.keys(): dateList=h5file['timeseries'].keys() h5out=h5py.File(outName,'w') group=h5out.create_group('timeseries') for d in dateList: print d dset1=h5file['timeseries'].get(d) dset=group.create_dataset(d, data=dset1[ysub[0]:ysub[1],xsub[0]:xsub[1]], compression='gzip') for key, value in h5file['timeseries'].attrs.iteritems(): group.attrs[key] = value group.attrs['FILE_LENGTH']=shape(dset1[ysub[0]:ysub[1],xsub[0]:xsub[1]])[0] group.attrs['WIDTH']=shape(dset1[ysub[0]:ysub[1],xsub[0]:xsub[1]])[1] group.attrs['subset_x0']=xsub[0] group.attrs['subset_x1']=xsub[1] group.attrs['subset_y0']=ysub[0] group.attrs['subset_y1']=ysub[1] if 'X_FIRST' in h5file['timeseries'].attrs.keys(): group.attrs['X_FIRST']=float(h5file['timeseries'].attrs['X_FIRST']) + xsub[0]*float(h5file['timeseries'].attrs['X_STEP']) group.attrs['Y_FIRST']=float(h5file['timeseries'].attrs['Y_FIRST']) + ysub[0]*float(h5file['timeseries'].attrs['Y_STEP']) h5file.close() h5out.close() elif 'temporal_coherence' in h5file.keys() or 'velocity' in h5file.keys() or 'mask' in h5file.keys() or 'rmse' in h5file.keys(): print 'writing >>> ' +outName dset=h5file[k[0]].get(k[0]) data=dset[ysub[0]:ysub[1],xsub[0]:xsub[1]] hfout=h5py.File(outName,'w') group= hfout.create_group(k[0]) group.create_dataset(k[0],data=data,compression='gzip') for key,value in h5file[k[0]].attrs.iteritems(): group.attrs[key]=value group.attrs['FILE_LENGTH']=data.shape[0] group.attrs['WIDTH']=data.shape[1] group.attrs['XMIN']=0 group.attrs['XMAX']=data.shape[1]-1 group.attrs['YMIN']=0 group.attrs['YMAX']=data.shape[0]-1 group.attrs['subset_x0']=xsub[0] group.attrs['subset_x1']=xsub[1] group.attrs['subset_y0']=ysub[0] group.attrs['subset_y1']=ysub[1] if 'X_FIRST' in h5file[k[0]].attrs.keys(): group.attrs['X_FIRST']=float(h5file[k[0]].attrs['X_FIRST']) + xsub[0]*float(h5file[k[0]].attrs['X_STEP']) group.attrs['Y_FIRST']=float(h5file[k[0]].attrs['Y_FIRST']) + ysub[0]*float(h5file[k[0]].attrs['Y_STEP']) h5file.close() hfout.close() elif ext in ['.unw','.cor','.hgt']: a,p,r = readfile.read_float32(File) try: Latsub Lonsub try: r['X_FIRST'] xsub=[0]*2 ysub=[0]*2 xsub[0]=int((Lonsub[0]-float(r['X_FIRST']))/float(r['X_STEP'])) xsub[1]=int((Lonsub[1]-float(r['X_FIRST']))/float(r['X_STEP'])) ysub[0]=int((Latsub[1]-float(r['Y_FIRST']))/float(r['Y_STEP'])) ysub[1]=int((Latsub[0]-float(r['Y_FIRST']))/float(r['Y_STEP'])) print 'Subseting geocoded',ext,' file with Latitude and Longitude...' except: print 'Not geocoded file, cannot be subseted with LatLon.' Usage() ; sys.exit(1) except: Geo=0 a=a[ysub[0]:ysub[1],xsub[0]:xsub[1]] p=p[ysub[0]:ysub[1],xsub[0]:xsub[1]] print 'writing >>> '+outName writefile.write_float32(p,outName) r['FILE_LENGTH']=str(p.shape[0]) r['WIDTH']=str(p.shape[1]) r['XMAX']=str(int(r['WIDTH']) - 1) r['YMAX']=str(int(r['FILE_LENGTH']) - 1) r['subset_x0']=str(xsub[0]) r['subset_x1']=str(xsub[1]) r['subset_y0']=str(ysub[0]) r['subset_y1']=str(ysub[1]) try: r['Y_FIRST']=str(float(r['Y_FIRST'])+ysub[0]*float(r['Y_STEP'])) r['X_FIRST']=str(float(r['X_FIRST'])+xsub[0]*float(r['X_STEP'])) 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_dem(File) try: Latsub Lonsub # print Latsub try: r['X_FIRST'] xsub=[0]*2 ysub=[0]*2 xsub[0]=int((Lonsub[0]-float(r['X_FIRST']))/float(r['X_STEP'])) xsub[1]=int((Lonsub[1]-float(r['X_FIRST']))/float(r['X_STEP'])) ysub[0]=int((Latsub[1]-float(r['Y_FIRST']))/float(r['Y_STEP'])) ysub[1]=int((Latsub[0]-float(r['Y_FIRST']))/float(r['Y_STEP'])) print 'Subseting',ext,' file with Latitude and Longitude...' except: print 'Not geocoded file, cannot be subseted with LatLon.' Usage() ; sys.exit(1) except: Geo=0 d=d[ysub[0]:ysub[1],xsub[0]:xsub[1]] print 'writing >>> '+outName writefile.write_dem(d,outName) r['FILE_LENGTH']=str(d.shape[0]) r['WIDTH']=str(d.shape[1]) r['XMAX']=str(int(r['WIDTH']) - 1) r['YMAX']=str(int(r['FILE_LENGTH']) - 1) r['subset_x0']=str(xsub[0]) r['subset_x1']=str(xsub[1]) r['subset_y0']=str(ysub[0]) r['subset_y1']=str(ysub[1]) try: r['Y_FIRST']=str(float(r['Y_FIRST'])+ysub[0]*float(r['Y_STEP'])) r['X_FIRST']=str(float(r['X_FIRST'])+xsub[0]*float(r['X_STEP'])) 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) try: r=readfile.read_rsc_file(File+'.rsc') except: sys.exit(1) try: Latsub Lonsub try: r['X_FIRST'] xsub=[0]*2 ysub=[0]*2 xsub[0]=int((Lonsub[0]-float(r['X_FIRST']))/float(r['X_STEP'])) xsub[1]=int((Lonsub[1]-float(r['X_FIRST']))/float(r['X_STEP'])) ysub[0]=int((Latsub[1]-float(r['Y_FIRST']))/float(r['Y_STEP'])) ysub[1]=int((Latsub[0]-float(r['Y_FIRST']))/float(r['Y_STEP'])) print 'Subseting geocoded',ext,' file with Latitude and Longitude...' except: print 'Not geocoded file, cannot be subseted with LatLon.' Usage() ; sys.exit(1) except: Geo=0 box = (xsub[0],ysub[0],xsub[1],ysub[1]) output_img = im.crop(box) print 'writing >>> '+outName output_img.save(outName) # try: # r=readfile.read_rsc_file(File+'.rsc') # except: # sys.exit(1) r['FILE_LENGTH']=str(ysub[1]-ysub[0]) r['WIDTH']=str(xsub[1]-xsub[0]) r['XMAX']=str(int(r['WIDTH']) - 1) r['YMAX']=str(int(r['FILE_LENGTH']) - 1) r['subset_x0']=str(xsub[0]) r['subset_x1']=str(xsub[1]) r['subset_y0']=str(ysub[0]) r['subset_y1']=str(ysub[1]) try: r['Y_FIRST']=str(float(r['Y_FIRST'])+ysub[0]*float(r['Y_STEP'])) r['X_FIRST']=str(float(r['X_FIRST'])+xsub[0]*float(r['X_STEP'])) except: Geo=0 f = open(outName+'.rsc','w') for k in r.keys(): f.write(k+' '+r[k]+'\n') f.close()
def main(argv): try: File=argv[0] except: Usage();sys.exit(1) h5file=h5py.File(File,'r') k=h5file.keys() if 'velocity' in k: dset = h5file['velocity'].get('velocity') data = dset[0:dset.shape[0],0:dset.shape[1]] print "converting velocity to a 1 year interferogram." wvl=float(h5file[k[0]].attrs['WAVELENGTH']) data=(-4*pi/wvl)*data outname=File.split('.')[0]+'.unw' print 'writing >>> '+ outname writefile.write_float32(data,outname) f = open(outname+'.rsc','w') for key , value in h5file[k[0]].attrs.iteritems(): f.write(key+' '+str(value)+'\n') f.close() if k[0] =='rmse' or k[0] =='temporal_coherence' or k[0]=='mask': dset = h5file[k[0]].get(k[0]) data = dset[0:dset.shape[0],0:dset.shape[1]] outname=File.split('.')[0]+'.unw' print 'writing >>> '+ outname writefile.write_float32(data,outname) f = open(outname+'.rsc','w') for key , value in h5file[k[0]].attrs.iteritems(): f.write(key+' '+str(value)+'\n') f.close() # outname=File.split('.')[0]+'.unw' # writefile.write_float32(data,outname) # f = open(outname+'.rsc','w') # for key , value in h5file[k[0]].attrs.iteritems(): # f.write(key+' '+str(value)+'\n') # f.close() elif 'timeseries' in k: dateList=h5file['timeseries'].keys() try: d=sys.argv[2] except: print 'No input date specified >>> continue with the last date' dateList=h5file['timeseries'].keys() d=dateList[-1] print 'reading '+d + ' ... ' dset=h5file['timeseries'].get(d) data = dset[0:dset.shape[0],0:dset.shape[1]] wvl=float(h5file[k[0]].attrs['WAVELENGTH']) data=(-4*pi/wvl)*data outname=File.split('.')[0]+'.unw' print 'writing >>> '+ outname writefile.write_float32(data,outname) f = open(outname+'.rsc','w') for key , value in h5file[k[0]].attrs.iteritems(): # update 'DATE12', Yunjun, Aug 3rd 2015 if key=='DATE12': try: master_d=h5file[k[0]].attrs['ref_date'] except: master_d=h5file[k[0]].attrs['DATE'] if len(master_d)==8: master_d=master_d[2:8] if len(d)==8: d=d[2:8] f.write(key+' '+master_d+'-'+d+'\n') else: f.write(key+' '+str(value)+'\n') f.close() elif 'interferograms' in k: igramList=h5file['interferograms'].keys() try: d=sys.argv[2] except: print 'No input date specified >>> continue with the last date' d=igramList[-1] print 'reading '+d + ' ... ' dset=h5file['interferograms'][d].get(d) data = dset[0:dset.shape[0],0:dset.shape[1]] # wvl=float(h5file['interferograms'][d].attrs['WAVELENGTH']) # data=(-4*pi/wvl)*data # outname=File.split('.')[0]+'.unw' outname=d print 'writing >>> '+ outname writefile.write_float32(data,outname) f = open(outname+'.rsc','w') for key , value in h5file['interferograms'][d].attrs.iteritems(): f.write(key+' '+str(value)+'\n') f.close() h5file.close()
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 k[0] in ('velocity','temporal_coherence','mask','rmse') and 'timeseries' not in k: dset = h5file[k[0]].get(k[0]) data = dset[0:dset.shape[0],0:dset.shape[1]] outname=fileName+'.unw' 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 '+geomap+' '+outname+' geo_'+outname print geoCmd os.system(geoCmd) print 'reading geocoded file and write it to h5 format' 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 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 f.close() h5file.close() 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]] 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 '+geomap+' '+outname+' geo_'+outname print geoCmd; os.system(geoCmd) print 'reading geocoded file and add it to '+'geo_'+file amp,unw,unwrsc = readfile.read_float32('geo_'+outname) dset = group.create_dataset(epoch, data=unw, compression='gzip') 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 for key,value in unwrsc.iteritems(): group.attrs[key] = value for key,value in h5file['timeseries'].attrs.iteritems(): group.attrs[key] = value group.attrs['WIDTH'] =unwrsc['WIDTH'] group.attrs['FILE_LENGTH'] =unwrsc['FILE_LENGTH'] f.close() h5file.close() elif k[0] == 'interferograms': print 'geocoding interferograms:' outname='igram_temp.unw' f = h5py.File('geo_'+file,'w') gg = f.create_group('interferograms') igramList=h5file[k[0]].keys() for igram in igramList: print 'geocoding '+igram group = gg.create_group('geo_'+igram) d = h5file['interferograms'][igram].get(igram) data = d[0:d.shape[0],0:d.shape[1]] writefile.write_float32(data,outname) f_temp = open(outname+'.rsc','w') f_temp.write('FILE_LENGTH '+str(data.shape[0])+'\n') f_temp.write('WIDTH '+str(data.shape[1])+'\n') f_temp.close() geoCmd='geocode.pl '+geomap+' '+outname+' geo_'+outname print geoCmd; os.system(geoCmd) print 'reading geocoded file and add it to '+'geo_'+file amp,unw,unwrsc = readfile.read_float32('geo_'+outname) if igram==igramList[0]: MaskZero=np.ones([unw.shape[0],unw.shape[1]]) MaskZero=amp*MaskZero dset = group.create_dataset('geo_'+igram, data=unw, compression='gzip') 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 for key,value in unwrsc.iteritems(): group.attrs[key] = value for key,value in h5file['interferograms'][igram].attrs.iteritems(): group.attrs[key] = value # for key,value in unwrsc.iteritems(): # group.attrs[key] = value group.attrs['WIDTH'] = unwrsc['WIDTH'] group.attrs['FILE_LENGTH'] = unwrsc['FILE_LENGTH'] Mask=np.ones(MaskZero.shape) Mask[MaskZero==0]=0 gm = f.create_group('mask') dset = gm.create_dataset('mask', data=Mask, compression='gzip') f.close() h5file.close() elif k[0] == 'coherence': print 'geocoding coherence:' outname='cor_temp.unw' f = h5py.File('geo_'+file,'w') gg = f.create_group(k[0]) corList=h5file[k[0]].keys() for cor in corList: print 'geocoding '+cor group = gg.create_group('geo_'+cor) d = h5file[k[0]][cor].get(cor) data = d[0:d.shape[0],0:d.shape[1]] writefile.write_float32(data,outname) f_temp = open(outname+'.rsc','w') f_temp.write('FILE_LENGTH '+str(data.shape[0])+'\n') f_temp.write('WIDTH '+str(data.shape[1])+'\n') f_temp.close() geoCmd='geocode.pl '+geomap+' '+outname+' geo_'+outname print geoCmd; os.system(geoCmd) print 'reading geocoded file and add it to '+'geo_'+file amp,unw,unwrsc = readfile.read_float32('geo_'+outname) dset = group.create_dataset('geo_'+cor, data=unw, compression='gzip') 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 for key,value in unwrsc.iteritems(): group.attrs[key] = value for key,value in h5file[k[0]][cor].attrs.iteritems(): group.attrs[key] = value group.attrs['WIDTH'] = unwrsc['WIDTH'] group.attrs['FILE_LENGTH'] = unwrsc['FILE_LENGTH'] f.close() h5file.close() elif k[0] == 'wrapped': print 'geocoding wrapped interferograms:' outname='wrap_temp.int' f = h5py.File('geo_'+file,'w') gg = f.create_group(k[0]) wrapList=h5file[k[0]].keys() for wrap in wrapList: print 'geocoding '+wrap group = gg.create_group('geo_'+wrap) d = h5file[k[0]][wrap].get(wrap) data = d[0:d.shape[0],0:d.shape[1]] writefile.write_complex64(data,outname) f_temp = open(outname+'.rsc','w') f_temp.write('FILE_LENGTH '+str(data.shape[0])+'\n') f_temp.write('WIDTH '+str(data.shape[1])+'\n') f_temp.close() geoCmd='geocode.pl '+geomap+' '+outname+' geo_'+outname print geoCmd; os.system(geoCmd) print 'reading geocoded file and add it to '+'geo_'+file amp,unw,unwrsc = readfile.read_complex64('geo_'+outname) dset = group.create_dataset('geo_'+wrap, data=unw, compression='gzip') 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 for key,value in unwrsc.iteritems(): group.attrs[key] = value for key,value in h5file[k[0]][wrap].attrs.iteritems(): group.attrs[key] = value group.attrs['WIDTH'] = unwrsc['WIDTH'] group.attrs['FILE_LENGTH'] = unwrsc['FILE_LENGTH'] f.close() h5file.close()
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 # try: # file=argv[0] # alks=float(argv[1]) # rlks=float(argv[2]) # except: # Usage();sys.exit(1) 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_complex64(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_dem(file) dlks = multilook(d, alks, rlks) print 'writing ' + outName writefile.write_dem(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_rsc_file(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()
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 k[0] in ('velocity', 'temporal_coherence', 'mask', 'rmse') and 'timeseries' not in k: dset = h5file[k[0]].get(k[0]) data = dset[0:dset.shape[0], 0:dset.shape[1]] outname = fileName + '.unw' 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 ' + geomap + ' ' + outname + ' geo_' + outname print geoCmd os.system(geoCmd) print 'reading geocoded file and write it to h5 format' 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 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 f.close() h5file.close() 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]] 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 ' + geomap + ' ' + outname + ' geo_' + outname print geoCmd os.system(geoCmd) print 'reading geocoded file and add it to ' + 'geo_' + file amp, unw, unwrsc = readfile.read_float32('geo_' + outname) dset = group.create_dataset(epoch, data=unw, compression='gzip') 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 for key, value in unwrsc.iteritems(): group.attrs[key] = value for key, value in h5file['timeseries'].attrs.iteritems(): group.attrs[key] = value group.attrs['WIDTH'] = unwrsc['WIDTH'] group.attrs['FILE_LENGTH'] = unwrsc['FILE_LENGTH'] f.close() h5file.close() elif k[0] == 'interferograms': print 'geocoding interferograms:' outname = 'igram_temp.unw' f = h5py.File('geo_' + file, 'w') gg = f.create_group('interferograms') igramList = h5file[k[0]].keys() for igram in igramList: print 'geocoding ' + igram group = gg.create_group('geo_' + igram) d = h5file['interferograms'][igram].get(igram) data = d[0:d.shape[0], 0:d.shape[1]] writefile.write_float32(data, outname) f_temp = open(outname + '.rsc', 'w') f_temp.write('FILE_LENGTH ' + str(data.shape[0]) + '\n') f_temp.write('WIDTH ' + str(data.shape[1]) + '\n') f_temp.close() geoCmd = 'geocode.pl ' + geomap + ' ' + outname + ' geo_' + outname print geoCmd os.system(geoCmd) print 'reading geocoded file and add it to ' + 'geo_' + file amp, unw, unwrsc = readfile.read_float32('geo_' + outname) if igram == igramList[0]: MaskZero = np.ones([unw.shape[0], unw.shape[1]]) MaskZero = amp * MaskZero dset = group.create_dataset('geo_' + igram, data=unw, compression='gzip') 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 for key, value in unwrsc.iteritems(): group.attrs[key] = value for key, value in h5file['interferograms'][igram].attrs.iteritems( ): group.attrs[key] = value # for key,value in unwrsc.iteritems(): # group.attrs[key] = value group.attrs['WIDTH'] = unwrsc['WIDTH'] group.attrs['FILE_LENGTH'] = unwrsc['FILE_LENGTH'] Mask = np.ones(MaskZero.shape) Mask[MaskZero == 0] = 0 gm = f.create_group('mask') dset = gm.create_dataset('mask', data=Mask, compression='gzip') f.close() h5file.close() elif k[0] == 'coherence': print 'geocoding coherence:' outname = 'cor_temp.unw' f = h5py.File('geo_' + file, 'w') gg = f.create_group(k[0]) corList = h5file[k[0]].keys() for cor in corList: print 'geocoding ' + cor group = gg.create_group('geo_' + cor) d = h5file[k[0]][cor].get(cor) data = d[0:d.shape[0], 0:d.shape[1]] writefile.write_float32(data, outname) f_temp = open(outname + '.rsc', 'w') f_temp.write('FILE_LENGTH ' + str(data.shape[0]) + '\n') f_temp.write('WIDTH ' + str(data.shape[1]) + '\n') f_temp.close() geoCmd = 'geocode.pl ' + geomap + ' ' + outname + ' geo_' + outname print geoCmd os.system(geoCmd) print 'reading geocoded file and add it to ' + 'geo_' + file amp, unw, unwrsc = readfile.read_float32('geo_' + outname) dset = group.create_dataset('geo_' + cor, data=unw, compression='gzip') 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 for key, value in unwrsc.iteritems(): group.attrs[key] = value for key, value in h5file[k[0]][cor].attrs.iteritems(): group.attrs[key] = value group.attrs['WIDTH'] = unwrsc['WIDTH'] group.attrs['FILE_LENGTH'] = unwrsc['FILE_LENGTH'] f.close() h5file.close() elif k[0] == 'wrapped': print 'geocoding wrapped interferograms:' outname = 'wrap_temp.int' f = h5py.File('geo_' + file, 'w') gg = f.create_group(k[0]) wrapList = h5file[k[0]].keys() for wrap in wrapList: print 'geocoding ' + wrap group = gg.create_group('geo_' + wrap) d = h5file[k[0]][wrap].get(wrap) data = d[0:d.shape[0], 0:d.shape[1]] writefile.write_complex64(data, outname) f_temp = open(outname + '.rsc', 'w') f_temp.write('FILE_LENGTH ' + str(data.shape[0]) + '\n') f_temp.write('WIDTH ' + str(data.shape[1]) + '\n') f_temp.close() geoCmd = 'geocode.pl ' + geomap + ' ' + outname + ' geo_' + outname print geoCmd os.system(geoCmd) print 'reading geocoded file and add it to ' + 'geo_' + file amp, unw, unwrsc = readfile.read_complex64('geo_' + outname) dset = group.create_dataset('geo_' + wrap, data=unw, compression='gzip') 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 for key, value in unwrsc.iteritems(): group.attrs[key] = value for key, value in h5file[k[0]][wrap].attrs.iteritems(): group.attrs[key] = value group.attrs['WIDTH'] = unwrsc['WIDTH'] group.attrs['FILE_LENGTH'] = unwrsc['FILE_LENGTH'] f.close() h5file.close()
def main(argv): #outName='subsetIgrams.h5' 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() ##################################################### try: File xsub ysub except: try: File Latsub Lonsub except: Usage() sys.exit(1) try: outName except: outName = 'subset_' + File ext = os.path.splitext(File)[1] if ext == '.h5': try: h5file = h5py.File(File, 'r') except: Usage() sys.exit(1) k = h5file.keys() # convert LatLon to xy for geocoded file try: Latsub Lonsub if 'X_FIRST' in h5file[k[0]].attrs.keys(): xsub = [0] * 2 ysub = [0] * 2 xsub[0] = int( (Lonsub[0] - float(h5file[k[0]].attrs['X_FIRST'])) / float(h5file[k[0]].attrs['X_STEP'])) xsub[1] = int( (Lonsub[1] - float(h5file[k[0]].attrs['X_FIRST'])) / float(h5file[k[0]].attrs['X_STEP'])) ysub[0] = int( (Latsub[1] - float(h5file[k[0]].attrs['Y_FIRST'])) / float(h5file[k[0]].attrs['Y_STEP'])) ysub[1] = int( (Latsub[0] - float(h5file[k[0]].attrs['Y_FIRST'])) / float(h5file[k[0]].attrs['Y_STEP'])) print 'Subseting geocoded', ext, ' file with Latitude and Longitude...' elif 'X_FIRST' in h5file[k[0]][h5file[k[0]].keys()[0]].attrs.keys( ): # for geocoded interferograms/coherence igramList = h5file[k[0]].keys() xsub = [0] * 2 ysub = [0] * 2 xsub[0] = int( (Lonsub[0] - float(h5file[k[0]][igramList[0]].attrs['X_FIRST'])) / float(h5file[k[0]][igramList[0]].attrs['X_STEP'])) xsub[1] = int( (Lonsub[1] - float(h5file[k[0]][igramList[0]].attrs['X_FIRST'])) / float(h5file[k[0]][igramList[0]].attrs['X_STEP'])) ysub[0] = int( (Latsub[1] - float(h5file[k[0]][igramList[0]].attrs['Y_FIRST'])) / float(h5file[k[0]][igramList[0]].attrs['Y_STEP'])) ysub[1] = int( (Latsub[0] - float(h5file[k[0]][igramList[0]].attrs['Y_FIRST'])) / float(h5file[k[0]][igramList[0]].attrs['Y_STEP'])) print 'Subseting geocoded', ext, ' file with Latitude and Longitude...' else: print 'Not geocoded file, cannot be subseted with LatLon.' Usage() sys.exit(1) except: Geo = 0 # k=h5file.keys() if 'interferograms' in k: igramList = h5file['interferograms'].keys() h5out = h5py.File(outName, 'w') gg = h5out.create_group('interferograms') for igram in igramList: print igram dset1 = h5file['interferograms'][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['interferograms'][ igram].attrs.iteritems(): group.attrs[key] = value group.attrs['FILE_LENGTH'] = shape(dset1[ysub[0]:ysub[1], xsub[0]:xsub[1]])[0] group.attrs['WIDTH'] = shape(dset1[ysub[0]:ysub[1], xsub[0]:xsub[1]])[1] group.attrs['subset_x0'] = xsub[0] group.attrs['subset_x1'] = xsub[1] group.attrs['subset_y0'] = ysub[0] group.attrs['subset_y1'] = ysub[1] if 'X_FIRST' in h5file['interferograms'][igram].attrs.keys(): group.attrs['X_FIRST'] = float( h5file['interferograms'] [igram].attrs['X_FIRST']) + xsub[0] * float( h5file['interferograms'][igram].attrs['X_STEP']) group.attrs['Y_FIRST'] = float( h5file['interferograms'] [igram].attrs['Y_FIRST']) + ysub[0] * float( h5file['interferograms'][igram].attrs['Y_STEP']) gm = h5out.create_group('mask') try: Mset = h5file['mask'].get('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! It may cause problem in other processing steps.' 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 average coherence found in the File' elif k[0] in ('coherence', 'wrapped'): corList = h5file[k[0]].keys() h5out = h5py.File(outName, 'w') gg = h5out.create_group(k[0]) for cor in corList: print cor dset1 = h5file[k[0]][cor].get(cor) group = gg.create_group(cor) dset = group.create_dataset(cor, data=dset1[ysub[0]:ysub[1], xsub[0]:xsub[1]], compression='gzip') for key, value in h5file[k[0]][cor].attrs.iteritems(): group.attrs[key] = value group.attrs['FILE_LENGTH'] = shape(dset1[ysub[0]:ysub[1], xsub[0]:xsub[1]])[0] group.attrs['WIDTH'] = shape(dset1[ysub[0]:ysub[1], xsub[0]:xsub[1]])[1] group.attrs['subset_x0'] = xsub[0] group.attrs['subset_x1'] = xsub[1] group.attrs['subset_y0'] = ysub[0] group.attrs['subset_y1'] = ysub[1] if 'X_FIRST' in h5file[k[0]][cor].attrs.keys(): group.attrs['X_FIRST'] = float( h5file[k[0]][cor].attrs['X_FIRST']) + xsub[0] * float( h5file[k[0]][cor].attrs['X_STEP']) group.attrs['Y_FIRST'] = float( h5file[k[0]][cor].attrs['Y_FIRST']) + ysub[0] * float( h5file[k[0]][cor].attrs['Y_STEP']) elif 'timeseries' in h5file.keys(): dateList = h5file['timeseries'].keys() h5out = h5py.File(outName, 'w') group = h5out.create_group('timeseries') for d in dateList: print d dset1 = h5file['timeseries'].get(d) dset = group.create_dataset(d, data=dset1[ysub[0]:ysub[1], xsub[0]:xsub[1]], compression='gzip') for key, value in h5file['timeseries'].attrs.iteritems(): group.attrs[key] = value group.attrs['FILE_LENGTH'] = shape(dset1[ysub[0]:ysub[1], xsub[0]:xsub[1]])[0] group.attrs['WIDTH'] = shape(dset1[ysub[0]:ysub[1], xsub[0]:xsub[1]])[1] group.attrs['subset_x0'] = xsub[0] group.attrs['subset_x1'] = xsub[1] group.attrs['subset_y0'] = ysub[0] group.attrs['subset_y1'] = ysub[1] if 'X_FIRST' in h5file['timeseries'].attrs.keys(): group.attrs['X_FIRST'] = float( h5file['timeseries'].attrs['X_FIRST']) + xsub[0] * float( h5file['timeseries'].attrs['X_STEP']) group.attrs['Y_FIRST'] = float( h5file['timeseries'].attrs['Y_FIRST']) + ysub[0] * float( h5file['timeseries'].attrs['Y_STEP']) h5file.close() h5out.close() elif 'temporal_coherence' in h5file.keys( ) or 'velocity' in h5file.keys() or 'mask' in h5file.keys( ) or 'rmse' in h5file.keys(): print 'writing >>> ' + outName dset = h5file[k[0]].get(k[0]) data = dset[ysub[0]:ysub[1], xsub[0]:xsub[1]] hfout = h5py.File(outName, 'w') group = hfout.create_group(k[0]) group.create_dataset(k[0], data=data, compression='gzip') for key, value in h5file[k[0]].attrs.iteritems(): group.attrs[key] = value group.attrs['FILE_LENGTH'] = data.shape[0] group.attrs['WIDTH'] = data.shape[1] group.attrs['XMIN'] = 0 group.attrs['XMAX'] = data.shape[1] - 1 group.attrs['YMIN'] = 0 group.attrs['YMAX'] = data.shape[0] - 1 group.attrs['subset_x0'] = xsub[0] group.attrs['subset_x1'] = xsub[1] group.attrs['subset_y0'] = ysub[0] group.attrs['subset_y1'] = ysub[1] if 'X_FIRST' in h5file[k[0]].attrs.keys(): group.attrs['X_FIRST'] = float( h5file[k[0]].attrs['X_FIRST']) + xsub[0] * float( h5file[k[0]].attrs['X_STEP']) group.attrs['Y_FIRST'] = float( h5file[k[0]].attrs['Y_FIRST']) + ysub[0] * float( h5file[k[0]].attrs['Y_STEP']) h5file.close() hfout.close() elif ext in ['.unw', '.cor', '.hgt']: a, p, r = readfile.read_float32(File) try: Latsub Lonsub try: r['X_FIRST'] xsub = [0] * 2 ysub = [0] * 2 xsub[0] = int( (Lonsub[0] - float(r['X_FIRST'])) / float(r['X_STEP'])) xsub[1] = int( (Lonsub[1] - float(r['X_FIRST'])) / float(r['X_STEP'])) ysub[0] = int( (Latsub[1] - float(r['Y_FIRST'])) / float(r['Y_STEP'])) ysub[1] = int( (Latsub[0] - float(r['Y_FIRST'])) / float(r['Y_STEP'])) print 'Subseting geocoded', ext, ' file with Latitude and Longitude...' except: print 'Not geocoded file, cannot be subseted with LatLon.' Usage() sys.exit(1) except: Geo = 0 a = a[ysub[0]:ysub[1], xsub[0]:xsub[1]] p = p[ysub[0]:ysub[1], xsub[0]:xsub[1]] print 'writing >>> ' + outName writefile.write_float32(p, outName) r['FILE_LENGTH'] = str(p.shape[0]) r['WIDTH'] = str(p.shape[1]) r['XMAX'] = str(int(r['WIDTH']) - 1) r['YMAX'] = str(int(r['FILE_LENGTH']) - 1) r['subset_x0'] = str(xsub[0]) r['subset_x1'] = str(xsub[1]) r['subset_y0'] = str(ysub[0]) r['subset_y1'] = str(ysub[1]) try: r['Y_FIRST'] = str( float(r['Y_FIRST']) + ysub[0] * float(r['Y_STEP'])) r['X_FIRST'] = str( float(r['X_FIRST']) + xsub[0] * float(r['X_STEP'])) 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_dem(File) try: Latsub Lonsub # print Latsub try: r['X_FIRST'] xsub = [0] * 2 ysub = [0] * 2 xsub[0] = int( (Lonsub[0] - float(r['X_FIRST'])) / float(r['X_STEP'])) xsub[1] = int( (Lonsub[1] - float(r['X_FIRST'])) / float(r['X_STEP'])) ysub[0] = int( (Latsub[1] - float(r['Y_FIRST'])) / float(r['Y_STEP'])) ysub[1] = int( (Latsub[0] - float(r['Y_FIRST'])) / float(r['Y_STEP'])) print 'Subseting', ext, ' file with Latitude and Longitude...' except: print 'Not geocoded file, cannot be subseted with LatLon.' Usage() sys.exit(1) except: Geo = 0 d = d[ysub[0]:ysub[1], xsub[0]:xsub[1]] print 'writing >>> ' + outName writefile.write_dem(d, outName) r['FILE_LENGTH'] = str(d.shape[0]) r['WIDTH'] = str(d.shape[1]) r['XMAX'] = str(int(r['WIDTH']) - 1) r['YMAX'] = str(int(r['FILE_LENGTH']) - 1) r['subset_x0'] = str(xsub[0]) r['subset_x1'] = str(xsub[1]) r['subset_y0'] = str(ysub[0]) r['subset_y1'] = str(ysub[1]) try: r['Y_FIRST'] = str( float(r['Y_FIRST']) + ysub[0] * float(r['Y_STEP'])) r['X_FIRST'] = str( float(r['X_FIRST']) + xsub[0] * float(r['X_STEP'])) 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) try: r = readfile.read_rsc_file(File + '.rsc') except: sys.exit(1) try: Latsub Lonsub try: r['X_FIRST'] xsub = [0] * 2 ysub = [0] * 2 xsub[0] = int( (Lonsub[0] - float(r['X_FIRST'])) / float(r['X_STEP'])) xsub[1] = int( (Lonsub[1] - float(r['X_FIRST'])) / float(r['X_STEP'])) ysub[0] = int( (Latsub[1] - float(r['Y_FIRST'])) / float(r['Y_STEP'])) ysub[1] = int( (Latsub[0] - float(r['Y_FIRST'])) / float(r['Y_STEP'])) print 'Subseting geocoded', ext, ' file with Latitude and Longitude...' except: print 'Not geocoded file, cannot be subseted with LatLon.' Usage() sys.exit(1) except: Geo = 0 box = (xsub[0], ysub[0], xsub[1], ysub[1]) output_img = im.crop(box) print 'writing >>> ' + outName output_img.save(outName) # try: # r=readfile.read_rsc_file(File+'.rsc') # except: # sys.exit(1) r['FILE_LENGTH'] = str(ysub[1] - ysub[0]) r['WIDTH'] = str(xsub[1] - xsub[0]) r['XMAX'] = str(int(r['WIDTH']) - 1) r['YMAX'] = str(int(r['FILE_LENGTH']) - 1) r['subset_x0'] = str(xsub[0]) r['subset_x1'] = str(xsub[1]) r['subset_y0'] = str(ysub[0]) r['subset_y1'] = str(ysub[1]) try: r['Y_FIRST'] = str( float(r['Y_FIRST']) + ysub[0] * float(r['Y_STEP'])) r['X_FIRST'] = str( float(r['X_FIRST']) + xsub[0] * float(r['X_STEP'])) except: Geo = 0 f = open(outName + '.rsc', 'w') for k in r.keys(): f.write(k + ' ' + r[k] + '\n') f.close()
def main(argv): try: file=argv[0] alks=float(argv[1]) rlks=float(argv[2]) 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 == '.int' or ext == '.slc': a,p,r = readfile.read_complex64(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_dem(file) dlks=multilook(d,alks,rlks) print 'writing '+outName writefile.write_dem(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_rsc_file(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 'Multilooking the interferograms' gg = h5file_lks.create_group('interferograms') igramList=h5file['interferograms'].keys() for igram in igramList: print igram unw = h5file['interferograms'][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['interferograms'][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']) 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=masklks, compression='gzip') elif 'timeseries' in h5file.keys(): print 'Multilooking the time-series' group = h5file_lks.create_group('timeseries') dateList=h5file['timeseries'].keys() for d in dateList: print d unw = h5file['timeseries'].get(d) unwlks=multilook(unw,alks,rlks) dset = group.create_dataset(d, data=unwlks, compression='gzip') for key,value in h5file['timeseries'].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_lks.create_group('mask') dset = group.create_dataset('mask', data=Masklks, compression='gzip') except: print 'Multilooked 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 'multi looking the '+ k[0] group=h5file_lks.create_group(k[0]) dset1 = h5file[k[0]].get(k[0]) Mask = dset1[0:dset1.shape[0],0:dset1.shape[1]] Masklks=multilook(Mask,alks,rlks) dset = group.create_dataset(k[0], data=Masklks, compression='gzip') for key , value in h5file[k[0]].attrs.iteritems(): group.attrs[key]=value 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']) group.attrs['WIDTH']=Masklks.shape[1] group.attrs['FILE_LENGTH']=Masklks.shape[0] h5file.close() h5file_lks.close()