def geocode_data_roipac(data, geomapFile, outname): print 'writing to roi_pac unw file format' writefile.write_float32(data, outname) f = open(outname + '.rsc', 'w') f.write('FILE_LENGTH ' + str(data.shape[0]) + '\n') f.write('WIDTH ' + str(data.shape[1]) + '\n') f.close() geoCmd = 'geocode.pl ' + geomapFile + ' ' + outname + ' geo_' + outname print geoCmd os.system(geoCmd) print 'reading geocoded file...' amp, unw, unwrsc = readfile.read_float32('geo_' + outname) rmCmd = 'rm ' + outname os.system(rmCmd) print rmCmd rmCmd = 'rm ' + outname + '.rsc' os.system(rmCmd) print rmCmd rmCmd = 'rm geo_' + outname os.system(rmCmd) print rmCmd rmCmd = 'rm geo_' + outname + '.rsc' os.system(rmCmd) print rmCmd return amp, unw, unwrsc
def geomap4subset_radar_file(radar_atr, geomap_file): ''' Add offset value to geomap file if input radar file has been subsetted.''' if 'subset_x0' in radar_atr.keys(): x0 = float(radar_atr['subset_x0']) y0 = float(radar_atr['subset_y0']) print '\nInput radar coord file has been subsetted.\n creating temporary geomap file for it...' rg, az, rsc = readfile.read_float32(geomap_file) rg = rg - x0 az = az - y0 geomap_file = 'temp_' + geomap_file print ' writing >>> ' + geomap_file + '\n' writefile.write_float32(rg, az, geomap_file) f = open(geomap_file + '.rsc', 'w') for key in rsc.keys(): f.write(key + ' ' + rsc[key] + '\n') f.close() return geomap_file
def geocode_one(data,geomapFile,outname): print 'writing to roi_pac unw file format' writefile.write_float32(data,outname) f = open(outname+'.rsc','w') f.write('FILE_LENGTH '+str(data.shape[0])+'\n') f.write('WIDTH '+str(data.shape[1])+'\n') f.close() geoCmd='geocode.pl '+geomapFile+' '+outname+' geo_'+outname print geoCmd os.system(geoCmd) print 'reading geocoded file...' amp,unw,unwrsc = readfile.read_float32('geo_'+outname) rmCmd='rm '+outname; os.system(rmCmd); print rmCmd rmCmd='rm '+outname+'.rsc'; os.system(rmCmd); print rmCmd rmCmd='rm geo_'+outname; os.system(rmCmd); print rmCmd rmCmd='rm geo_'+outname+'.rsc'; os.system(rmCmd); print rmCmd return amp, unw, unwrsc
def main(argv): try: file = argv[0] alks = float(argv[1]) rlks = float(argv[2]) except: try: file = argv[0] alks = float(argv[1]) rlks = float(argv[1]) except: Usage() sys.exit(1) ext = os.path.splitext(file)[1] outName = file.split('.')[0] + '_a' + str(int(alks)) + 'lks_r' + str( int(rlks)) + 'lks' + ext ################################################################################ if ext == '.h5': import h5py h5file_mli = h5py.File(outName, 'w') h5file = h5py.File(file, 'r') k = h5file.keys() if 'interferograms' in k: k[0] = 'interferograms' elif 'coherence' in k: k[0] = 'coherence' elif 'timeseries' in k: k[0] = 'timeseries' if k[0] in ['interferograms', 'coherence', 'wrapped']: print 'Multilooking the interferograms' gg = h5file_mli.create_group(k[0]) igramList = h5file[k[0]].keys() for igram in igramList: print igram unw = h5file[k[0]][igram].get(igram) unwlks = multilook(unw, alks, rlks) group = gg.create_group(igram) dset = group.create_dataset(igram, data=unwlks, compression='gzip') for key, value in h5file[k[0]][igram].attrs.iteritems(): group.attrs[key] = value group.attrs['WIDTH'] = unwlks.shape[1] group.attrs['FILE_LENGTH'] = unwlks.shape[0] try: group.attrs['Y_STEP'] = alks * float(group.attrs['Y_STEP']) group.attrs['X_STEP'] = rlks * float(group.attrs['X_STEP']) except: group.attrs['AZIMUTH_PIXEL_SIZE'] = alks * float( group.attrs['AZIMUTH_PIXEL_SIZE']) group.attrs['RANGE_PIXEL_SIZE'] = rlks * float( group.attrs['RANGE_PIXEL_SIZE']) try: dset1 = h5file['mask'].get('mask') mask = dset1[0:dset1.shape[0], 0:dset1.shape[1]] masklks = multilook(mask, alks, rlks) group = h5file_mli.create_group('mask') dset = group.create_dataset('mask', data=masklks, compression='gzip') except: print 'No mask group found.' elif k[0] in [ 'timeseries', 'temporal_coherence', 'velocity', 'mask', 'rmse' ]: print 'Multilooking ' + k[0] group = h5file_mli.create_group(k[0]) if k[0] == 'timeseries': dateList = h5file[k[0]].keys() for d in dateList: print d unw = h5file[k[0]].get(d) unwlks = multilook(unw, alks, rlks) dset = group.create_dataset(d, data=unwlks, compression='gzip') elif k[0] in ['temporal_coherence', 'velocity', 'mask', 'rmse']: dset1 = h5file[k[0]].get(k[0]) unw = dset1[0:dset1.shape[0], 0:dset1.shape[1]] unwlks = multilook(unw, alks, rlks) dset = group.create_dataset(k[0], data=unwlks, compression='gzip') try: dset1 = h5file['mask'].get('mask') Mask = dset1[0:dset1.shape[0], 0:dset1.shape[1]] Masklks = multilook(Mask, alks, rlks) group = h5file_mli.create_group('mask') dset = group.create_dataset('mask', data=Masklks, compression='gzip') except: print 'No mask group found.' ## Update attributes for key, value in h5file[k[0]].attrs.iteritems(): group.attrs[key] = value group.attrs['WIDTH'] = unwlks.shape[1] group.attrs['FILE_LENGTH'] = unwlks.shape[0] try: group.attrs['Y_STEP'] = alks * float(group.attrs['Y_STEP']) group.attrs['X_STEP'] = rlks * float(group.attrs['X_STEP']) except: group.attrs['AZIMUTH_PIXEL_SIZE'] = alks * float( group.attrs['AZIMUTH_PIXEL_SIZE']) group.attrs['RANGE_PIXEL_SIZE'] = rlks * float( group.attrs['RANGE_PIXEL_SIZE']) h5file.close() h5file_mli.close() ################################################################################ elif ext in ['.unw','.cor','.hgt','.dem','.trans'] or\ ext in ['.jpeg','.jpg','.png','.ras','.bmp'] or\ ext in ['.mli','.slc']: import pysar._readfile as readfile import pysar._writefile as writefile r = readfile.read_rsc_file(file + '.rsc') if ext == '.int' or ext == '.slc': a, p, r = readfile.read_complex64(file) pmli = multilook(p, alks, rlks) amli = multilook(a, alks, rlks) r['FILE_LENGTH'] = str(pmli.shape[0]) r['WIDTH'] = str(pmli.shape[1]) elif ext == '.unw' or ext == '.cor' or ext == '.hgt': a, p, r = readfile.read_float32(file) pmli = multilook(p, alks, rlks) amli = multilook(a, alks, rlks) print 'writing >>>' + outName writefile.write_float32(pmli, outName) r['FILE_LENGTH'] = str(pmli.shape[0]) r['WIDTH'] = str(pmli.shape[1]) elif ext == ('.dem'): d, r = readfile.read_dem(file) dmli = multilook(d, alks, rlks) print 'writing >>>' + outName writefile.write_dem(dmli, outName) r['FILE_LENGTH'] = str(dmli.shape[0]) r['WIDTH'] = str(dmli.shape[1]) elif ext in ['.jpeg', '.jpg', '.png', '.bmp']: import Image im = Image.open(file) width = im.size[0] / int(rlks) height = im.size[1] / int(alks) imlks = im.resize((width, height), Image.NEAREST) print 'writing >>>' + outName imlks.save(outName) r['FILE_LENGTH'] = str(height) r['WIDTH'] = str(width) ## Update attributes r['XMAX'] = str(int(r['WIDTH']) - 1) r['YMAX'] = str(int(r['FILE_LENGTH']) - 1) try: r['Y_STEP'] = str(float(r['Y_STEP']) * alks) r['X_STEP'] = str(float(r['X_STEP']) * rlks) except: r['AZIMUTH_PIXEL_SIZE'] = alks * float(r['AZIMUTH_PIXEL_SIZE']) r['RANGE_PIXEL_SIZE'] = rlks * float(r['RANGE_PIXEL_SIZE']) f = open(outName + '.rsc', 'w') for k in r.keys(): f.write(k + ' ' + r[k] + '\n') f.close()
def main(argv): try: file=argv[0] alks=float(argv[1]) rlks=float(argv[2]) except: try: file=argv[0] alks=float(argv[1]) rlks=float(argv[1]) except: Usage();sys.exit(1) ext = os.path.splitext(file)[1] outName=file.split('.')[0]+'_a'+str(int(alks))+'lks_r'+str(int(rlks))+'lks'+ext ################################################################################ if ext == '.h5': import h5py h5file_mli=h5py.File(outName,'w') h5file=h5py.File(file,'r') k=h5file.keys() if 'interferograms' in k: k[0] = 'interferograms' elif 'coherence' in k: k[0] = 'coherence' elif 'timeseries' in k: k[0] = 'timeseries' if k[0] in ['interferograms','coherence','wrapped']: print 'Multilooking the interferograms' gg = h5file_mli.create_group(k[0]) igramList=h5file[k[0]].keys() for igram in igramList: print igram unw = h5file[k[0]][igram].get(igram) unwlks=multilook(unw,alks,rlks) group = gg.create_group(igram) dset = group.create_dataset(igram, data=unwlks, compression='gzip') for key, value in h5file[k[0]][igram].attrs.iteritems(): group.attrs[key] = value group.attrs['WIDTH'] = unwlks.shape[1] group.attrs['FILE_LENGTH'] = unwlks.shape[0] try: group.attrs['Y_STEP']=alks*float(group.attrs['Y_STEP']) group.attrs['X_STEP']=rlks*float(group.attrs['X_STEP']) except: group.attrs['AZIMUTH_PIXEL_SIZE'] = alks*float(group.attrs['AZIMUTH_PIXEL_SIZE']) group.attrs['RANGE_PIXEL_SIZE'] = rlks*float(group.attrs['RANGE_PIXEL_SIZE']) try: dset1=h5file['mask'].get('mask') mask=dset1[0:dset1.shape[0],0:dset1.shape[1]] masklks=multilook(mask,alks,rlks) group=h5file_mli.create_group('mask') dset = group.create_dataset('mask', data=masklks, compression='gzip') except: print 'No mask group found.' elif k[0] in ['timeseries','temporal_coherence', 'velocity', 'mask', 'rmse']: print 'Multilooking '+k[0] group = h5file_mli.create_group(k[0]) if k[0] == 'timeseries': dateList=h5file[k[0]].keys() for d in dateList: print d unw = h5file[k[0]].get(d) unwlks=multilook(unw,alks,rlks) dset = group.create_dataset(d, data=unwlks, compression='gzip') elif k[0] in ['temporal_coherence', 'velocity', 'mask', 'rmse']: dset1 = h5file[k[0]].get(k[0]) unw = dset1[0:dset1.shape[0],0:dset1.shape[1]] unwlks=multilook(unw,alks,rlks) dset = group.create_dataset(k[0], data=unwlks, compression='gzip') try: dset1 = h5file['mask'].get('mask') Mask = dset1[0:dset1.shape[0],0:dset1.shape[1]] Masklks=multilook(Mask,alks,rlks) group=h5file_mli.create_group('mask') dset = group.create_dataset('mask', data=Masklks, compression='gzip') except: print 'No mask group found.' ## Update attributes for key,value in h5file[k[0]].attrs.iteritems(): group.attrs[key] = value group.attrs['WIDTH'] = unwlks.shape[1] group.attrs['FILE_LENGTH'] = unwlks.shape[0] try: group.attrs['Y_STEP']=alks*float(group.attrs['Y_STEP']) group.attrs['X_STEP']=rlks*float(group.attrs['X_STEP']) except: group.attrs['AZIMUTH_PIXEL_SIZE'] = alks*float(group.attrs['AZIMUTH_PIXEL_SIZE']) group.attrs['RANGE_PIXEL_SIZE'] = rlks*float(group.attrs['RANGE_PIXEL_SIZE']) h5file.close() h5file_mli.close() ################################################################################ elif ext in ['.unw','.cor','.hgt','.dem','.trans'] or\ ext in ['.jpeg','.jpg','.png','.ras','.bmp'] or\ ext in ['.mli','.slc']: import pysar._readfile as readfile import pysar._writefile as writefile r = readfile.read_rsc_file(file + '.rsc') if ext == '.int' or ext == '.slc': a,p,r = readfile.read_complex64(file) pmli=multilook(p,alks,rlks) amli=multilook(a,alks,rlks) r['FILE_LENGTH']=str(pmli.shape[0]) r['WIDTH'] =str(pmli.shape[1]) elif ext == '.unw' or ext == '.cor' or ext == '.hgt': a,p,r = readfile.read_float32(file) pmli=multilook(p,alks,rlks) amli=multilook(a,alks,rlks) print 'writing >>>'+outName writefile.write_float32(pmli,outName) r['FILE_LENGTH']=str(pmli.shape[0]) r['WIDTH'] =str(pmli.shape[1]) elif ext == ('.dem'): d,r = readfile.read_dem(file) dmli=multilook(d,alks,rlks) print 'writing >>>'+outName writefile.write_dem(dmli,outName) r['FILE_LENGTH']=str(dmli.shape[0]) r['WIDTH'] =str(dmli.shape[1]) elif ext in ['.jpeg','.jpg','.png','.bmp']: import Image im = Image.open(file) width = im.size[0] / int(rlks) height = im.size[1] / int(alks) imlks = im.resize((width, height), Image.NEAREST) print 'writing >>>'+outName imlks.save(outName) r['FILE_LENGTH']=str(height) r['WIDTH'] =str(width) ## Update attributes r['XMAX']=str(int(r['WIDTH']) - 1) r['YMAX']=str(int(r['FILE_LENGTH']) - 1) try: r['Y_STEP']=str(float(r['Y_STEP'])*alks) r['X_STEP']=str(float(r['X_STEP'])*rlks) except: r['AZIMUTH_PIXEL_SIZE'] = alks*float(r['AZIMUTH_PIXEL_SIZE']) r['RANGE_PIXEL_SIZE'] = rlks*float(r['RANGE_PIXEL_SIZE']) f = open(outName+'.rsc','w') for k in r.keys(): f.write(k+' '+r[k]+'\n') f.close()
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
def main(argv): if len(sys.argv) > 2: try: opts, args = getopt.getopt(argv, "h:f:x:y:o:l:L:") except getopt.GetoptError: print 'Error while getting args' Usage() sys.exit(1) for opt, arg in opts: if opt in ("-h", "--help"): Usage() sys.exit() elif opt == '-f': File = arg elif opt == '-y': ysub = [int(i) for i in arg.split(':')] ysub.sort() elif opt == '-x': xsub = [int(i) for i in arg.split(':')] xsub.sort() elif opt == '-o': outName = arg elif opt == '-l': Latsub = [float(i) for i in arg.split(':')] Latsub.sort() elif opt == '-L': Lonsub = [float(i) for i in arg.split(':')] Lonsub.sort() else: Usage() sys.exit(1) try: outName except: outName = 'subset_' + File ext = os.path.splitext(File)[1] ############################################################################ ################################# PySAR ################################## if ext == '.h5': try: h5file = h5py.File(File, 'r') except: Usage() sys.exit(1) k = h5file.keys() if 'interferograms' in k: k[0] = 'interferograms' elif 'coherence' in k: k[0] = 'coherence' elif 'timeseries' in k: k[0] = 'timeseries' if k[0] in ('interferograms', 'coherence', 'wrapped'): atr = h5file[k[0]][h5file[k[0]].keys()[0]].attrs elif k[0] in ('dem', 'velocity', 'mask', 'temporal_coherence', 'rmse', 'timeseries'): atr = h5file[k[0]].attrs ############# Subset Option ############# width = int(atr['WIDTH']) length = int(atr['FILE_LENGTH']) try: Latsub try: lat_step = float(atr['Y_STEP']) lat1 = float(atr['Y_FIRST']) lat0 = lat1 + length * lat_step if Latsub[0] < lat0: Latsub[0] = lat0 print 'WARNING: input latitude < min (' + str( lat0) + ')! Set it to min.' if Latsub[1] > lat1: Latsub[1] = lat1 print 'WARNING: input latitude > max (' + str( lat1) + ')! Set it to max.' print 'subset in latitude - ' + str(Latsub[0]) + ':' + str( Latsub[1]) ysub = [0] * 2 ysub[0] = int((Latsub[1] - lat1) / lat_step) ysub[1] = int((Latsub[0] - lat1) / lat_step) except: print 'Not geocoded file, cannot be subseted with LatLon.' Usage() return except: try: ysub if ysub[0] < 0: ysub[0] = 0 print 'WARNING: input y < min (0)! Set it to min.' if ysub[1] > length: ysub[1] = length print 'WARNING: input y > max (' + str( length) + ')! Set it to max.' print 'subset in y direction - ' + str(ysub[0]) + ':' + str( ysub[1]) except: ysub = [0, length] try: Lonsub try: lon_step = float(atr['X_STEP']) lon0 = float(atr['X_FIRST']) lon1 = lon0 + width * lon_step if Lonsub[0] < lon0: Lonsub[0] = lon0 print 'WARNING: input longitude < min (' + str( lon0) + ')! Set it to min.' if Lonsub[1] > lon1: Lonsub[1] = lon1 print 'WARNING: input longitude > max (' + str( lon1) + ')! Set it to max.' print 'subset in longitude - ' + str(Lonsub[0]) + ':' + str( Lonsub[1]) xsub = [0] * 2 xsub[0] = int((Lonsub[0] - lon0) / lon_step) xsub[1] = int((Lonsub[1] - lon0) / lon_step) except: print 'Not geocoded file, cannot be subseted with LatLon.' Usage() return except: try: xsub if xsub[0] < 0: xsub[0] = 0 print 'WARNING: input x < min (0)! Set it to min.' if xsub[1] > width: xsub[1] = width print 'WARNING: input x > max (' + str( width) + ')! Set it to max x.' print 'subset in x direction - ' + str(xsub[0]) + ':' + str( xsub[1]) except: xsub = [0, width] if ysub[0] > length or ysub[1] < 0 or xsub[0] > length or xsub[1] < 0: print 'ERROR: input index is out of data range!' print 'range in rdr: x - 0:' + str(width) + ' y - 0:' + str( length) try: print 'range in geo: lat - ' + str(lat0) + ':' + str( lat1) + ' lon - ' + str(lon0) + ':' + str(lon1) except: Geo = 0 sys.exit(1) ######## Data Read, Crop and Write ####### ##### N dset, N attributes if k[0] in ('interferograms', 'coherence', 'wrapped'): print 'writing >>> ' + outName h5out = h5py.File(outName, 'w') gg = h5out.create_group(k[0]) igramList = h5file[k[0]].keys() for igram in igramList: print igram dset1 = h5file[k[0]][igram].get(igram) group = gg.create_group(igram) dset = group.create_dataset(igram, data=dset1[ysub[0]:ysub[1], xsub[0]:xsub[1]], compression='gzip') for key, value in h5file[k[0]][igram].attrs.iteritems(): group.attrs[key] = value group.attrs['FILE_LENGTH'] = ysub[1] - ysub[0] group.attrs['WIDTH'] = xsub[1] - xsub[0] try: sub_x0_ori = int(group.attrs['subset_x0']) group.attrs['subset_x0'] = xsub[0] + sub_x0_ori group.attrs['subset_x1'] = xsub[1] + sub_x0_ori except: group.attrs['subset_x0'] = xsub[0] group.attrs['subset_x1'] = xsub[1] try: sub_y0_ori = int(group.attrs['subset_y0']) group.attrs['subset_y0'] = ysub[0] + sub_y0_ori group.attrs['subset_y1'] = ysub[1] + sub_y0_ori except: group.attrs['subset_y0'] = ysub[0] group.attrs['subset_y1'] = ysub[1] if 'X_FIRST' in atr.keys(): group.attrs['X_FIRST'] = float( atr['X_FIRST']) + xsub[0] * float(atr['X_STEP']) group.attrs['Y_FIRST'] = float( atr['Y_FIRST']) + ysub[0] * float(atr['Y_STEP']) ## support of old format try: Mset = h5file['mask'].get('mask') gm = h5out.create_group('mask') dset = gm.create_dataset('mask', data=Mset[ysub[0]:ysub[1], xsub[0]:xsub[1]], compression='gzip') except: print 'No group for mask found in the file.' try: Cset = h5file['meanCoherence'].get('meanCoherence') gm = h5out.create_group('meanCoherence') dset = gm.create_dataset('meanCoherence', data=Cset[ysub[0]:ysub[1], xsub[0]:xsub[1]], compression='gzip') except: print 'No group for meanCoherence found in the file' h5file.close() h5out.close() ##### N/1 dset, 1 attributes elif k[0] in [ 'timeseries', 'temporal_coherence', 'velocity', 'mask', 'rmse' ]: print 'writing >>> ' + outName h5out = h5py.File(outName, 'w') group = h5out.create_group(k[0]) if k[0] == 'timeseries': dateList = h5file[k[0]].keys() for d in dateList: print d dset1 = h5file[k[0]].get(d) dset = group.create_dataset(d, data=dset1[ysub[0]:ysub[1], xsub[0]:xsub[1]], compression='gzip') elif k[0] in ['temporal_coherence', 'velocity', 'mask', 'rmse']: dset1 = h5file[k[0]].get(k[0]) dset = group.create_dataset(k[0], data=dset1[ysub[0]:ysub[1], xsub[0]:xsub[1]], compression='gzip') ## Update attributes for key, value in h5file[k[0]].attrs.iteritems(): group.attrs[key] = value group.attrs['FILE_LENGTH'] = ysub[1] - ysub[0] group.attrs['WIDTH'] = xsub[1] - xsub[0] try: sub_x0_ori = int(group.attrs['subset_x0']) group.attrs['subset_x0'] = xsub[0] + sub_x0_ori group.attrs['subset_x1'] = xsub[1] + sub_x0_ori except: group.attrs['subset_x0'] = xsub[0] group.attrs['subset_x1'] = xsub[1] try: sub_y0_ori = int(group.attrs['subset_y0']) group.attrs['subset_y0'] = ysub[0] + sub_y0_ori group.attrs['subset_y1'] = ysub[1] + sub_y0_ori except: group.attrs['subset_y0'] = ysub[0] group.attrs['subset_y1'] = ysub[1] if 'X_FIRST' in atr.keys(): group.attrs['X_FIRST'] = float( atr['X_FIRST']) + xsub[0] * float(atr['X_STEP']) group.attrs['Y_FIRST'] = float( atr['Y_FIRST']) + ysub[0] * float(atr['Y_STEP']) h5file.close() h5out.close() else: print 'Error: group of HDF5 file not recogized!' h5file.close() Usage() sys.exit(1) ############################################################################ ######################### ROI_PAC / Image / GAMMA ######################## elif ext in ['.unw','.cor','.hgt','.dem','.trans'] or\ ext in ['.jpeg','.jpg','.png','.ras','.bmp'] or\ ext in ['.mli','.slc']: try: atr = readfile.read_rsc_file(File + '.rsc') except: try: atr = readfile.read_par_file(File + '.par') except: atr = readfile.read_par_file( os.path.splitext(File)[0] + '.par') ############# Subset Option ############# try: width = int(atr['WIDTH']) length = int(atr['FILE_LENGTH']) except: width = int(atr['range_samples:']) length = int(atr['azimuth_lines:']) try: Latsub try: lat_step = float(atr['Y_STEP']) lat1 = float(atr['Y_FIRST']) lat0 = lat1 + length * lat_step if Latsub[0] < lat0: Latsub[0] = lat0 print 'WARNING: input latitude < min (' + str( lat0) + ')! Set it to min.' if Latsub[1] > lat1: Latsub[1] = lat1 print 'WARNING: input latitude > max (' + str( lat1) + ')! Set it to max.' print 'subset in latitude - ' + str(Latsub[0]) + ':' + str( Latsub[1]) ysub = [0] * 2 ysub[0] = int((Latsub[1] - lat1) / lat_step) ysub[1] = int((Latsub[0] - lat1) / lat_step) except: print 'Not geocoded file, cannot be subseted with LatLon.' Usage() return except: try: ysub if ysub[0] < 0: ysub[0] = 0 print 'WARNING: input y < min (0)! Set it to min.' if ysub[1] > length: ysub[1] = length print 'WARNING: input y > max (' + str( length) + ')! Set it to max.' print 'subset in y direction - ' + str(ysub[0]) + ':' + str( ysub[1]) except: ysub = [0, length] try: Lonsub try: lon_step = float(atr['X_STEP']) lon0 = float(atr['X_FIRST']) lon1 = lon0 + width * lon_step if Lonsub[0] < lon0: Lonsub[0] = lon0 print 'WARNING: input longitude < min (' + str( lon0) + ')! Set it to min.' if Lonsub[1] > lon1: Lonsub[1] = lon1 print 'WARNING: input longitude > max (' + str( lon1) + ')! Set it to max.' print 'subset in longitude - ' + str(Lonsub[0]) + ':' + str( Lonsub[1]) xsub = [0] * 2 xsub[0] = int((Lonsub[0] - lon0) / lon_step) xsub[1] = int((Lonsub[1] - lon0) / lon_step) except: print 'Not geocoded file, cannot be subseted with LatLon.' Usage() return except: try: xsub if xsub[0] < 0: xsub[0] = 0 print 'WARNING: input x < min (0)! Set it to min.' if xsub[1] > width: xsub[1] = width print 'WARNING: input x > max (' + str( width) + ')! Set it to max x.' print 'subset in x direction - ' + str(xsub[0]) + ':' + str( xsub[1]) except: xsub = [0, width] if ysub[0] > length or ysub[1] < 0 or xsub[0] > length or xsub[1] < 0: print 'ERROR: input index is out of data range!' print 'range in rdr: x - 0:' + str(width) + ' y - 0:' + str( length) try: print 'range in geo: lat - ' + str(lat0) + ':' + str( lat1) + ' lon - ' + str(lon0) + ':' + str(lon1) except: Geo = 0 sys.exit(1) ######## Data Read, Crop and Write ####### print 'writing >>> ' + outName box = (xsub[0], ysub[0], xsub[1], ysub[1]) if ext in ['.unw', '.cor', '.hgt']: a, p, r = readfile.read_float32(File, box) #p = p[ysub[0]:ysub[1],xsub[0]:xsub[1]] writefile.write_float32(p, outName) elif ext == '.dem': p, r = readfile.read_dem(File) p = p[ysub[0]:ysub[1], xsub[0]:xsub[1]] writefile.write_dem(p, outName) elif ext == '.trans': a, p, r = readfile.read_float32(File, box) #a = a[ysub[0]:ysub[1],xsub[0]:xsub[1]] #p = p[ysub[0]:ysub[1],xsub[0]:xsub[1]] writefile.write_float32(a, p, outName) elif ext in ['.jpeg', '.jpg', '.png', '.ras', '.bmp']: import Image im = Image.open(File) box = (xsub[0], ysub[0], xsub[1], ysub[1]) output_img = im.crop(box) output_img.save(outName) elif ext == '.mli': d, r = readfile.read_gamma_float(File) d = d[ysub[0]:ysub[1], xsub[0]:xsub[1]] writefile.write_gamma_float(d, outName) elif ext == '.slc': d, r = readfile.read_gamma_scomplex(File, box) writefile.write_gamma_scomplex(d, outName) ########### Update .rsc file ############# atr['FILE_LENGTH'] = str(ysub[1] - ysub[0]) atr['WIDTH'] = str(xsub[1] - xsub[0]) atr['XMAX'] = str(width - 1) atr['YMAX'] = str(length - 1) try: sub_x0_ori = int(atr['subset_x0']) atr['subset_x0'] = str(xsub[0] + sub_x0_ori) atr['subset_x1'] = str(xsub[1] + sub_x0_ori) except: atr['subset_x0'] = str(xsub[0]) atr['subset_x1'] = str(xsub[1]) try: sub_y0_ori = int(atr['subset_y0']) atr['subset_y0'] = str(ysub[0] + sub_y0_ori) atr['subset_y1'] = str(ysub[1] + sub_y0_ori) except: atr['subset_y0'] = str(ysub[0]) atr['subset_y1'] = str(ysub[1]) if 'X_FIRST' in atr.keys(): atr['Y_FIRST'] = str( float(atr['Y_FIRST']) + ysub[0] * float(atr['Y_STEP'])) atr['X_FIRST'] = str( float(atr['X_FIRST']) + xsub[0] * float(atr['X_STEP'])) f = open(outName + '.rsc', 'w') for k in atr.keys(): f.write(k + ' ' + atr[k] + '\n') f.close() ########################################################################### else: print 'File extension not recogized.' Usage() sys.exit(1)
def main(argv): #lineWidth=4 #fontSize=32 #markerColor='orange' #markerSize=20 #if len(sys.argv)>2: try: opts, args = getopt.getopt(argv,"h:f:m:t:x:y:o:") except getopt.GetoptError: Usage() ; sys.exit(1) for opt,arg in opts: if opt in ("-h","--help"): Usage() sys.exit() elif opt == '-f': File = arg elif opt == '-m': maskFile = arg elif opt == '-t': thr = float(arg) elif opt == '-y': ysub = [int(i) for i in arg.split(':')]; ysub.sort() elif opt == '-x': xsub = [int(i) for i in arg.split(':')]; xsub.sort() elif opt == '-o': MaskedFile = arg try: File maskFile except: Usage() ; sys.exit(1) ###################################### ext = os.path.splitext(File)[1] import h5py import numpy as np ################################ PySAR HDF5 ################################# if ext == '.h5': h5file=h5py.File(File,'r') h5mask=h5py.File(maskFile,'r') kf=h5file.keys() if 'coherence' not in h5mask.keys(): Mset=h5mask[h5mask.keys()[0]].get(h5mask.keys()[0]) M=Mset[0:Mset.shape[0],0:Mset.shape[1]] try: MaskedFile except: MaskedFile = File.split('.')[0]+'_masked.h5' h5file2 = h5py.File(MaskedFile,'w') if len(kf)==1 and kf[0] in ('velocity','temporal_coherence','rmse','mask'): Vset=h5file[h5file.keys()[0]].get(h5file.keys()[0]) V=Vset[0:Vset.shape[0],0:Vset.shape[1]] try: xsub ysub M[ysub[0]:ysub[1],xsub[0]:xsub[1]]=0 except: print 'No subset' try: V[M<thr] = np.nan except: V[M==0] = np.nan group=h5file2.create_group(kf[0]) dset = group.create_dataset(os.path.basename(kf[0]), data=V, compression='gzip') for key, value in h5file[kf[0]].attrs.iteritems(): group.attrs[key] = value elif 'timeseries' in h5file.keys(): print 'Masking the time-series' group = h5file2.create_group('timeseries') dateList=h5file['timeseries'].keys() for d in dateList: print d unwset = h5file['timeseries'].get(d) unw=unwset[0:unwset.shape[0],0:unwset.shape[1]] try: unw[M<thr] = np.nan except: unw[M==0] = np.nan dset = group.create_dataset(d, data=unw, compression='gzip') for key,value in h5file['timeseries'].attrs.iteritems(): group.attrs[key] = value elif kf[0] in ('interferograms','wrapped') and 'coherence' in h5mask.keys(): print 'Masking each '+kf[0]+' using its coherence file' igramList=h5file[kf[0]].keys() cohList=h5mask['coherence'].keys() gg = h5file2.create_group(kf[0]) for igram in igramList: print igram date12 = h5file[kf[0]][igram].attrs['DATE12'] for cohFile in cohList: if h5mask['coherence'][cohFile].attrs['DATE12']==date12: igramCoh=cohFile print igramCoh unwset = h5file[kf[0]][igram].get(igram) unw=unwset[0:unwset.shape[0],0:unwset.shape[1]] cohset=h5mask['coherence'][igramCoh].get(igramCoh) coh=cohset[0:cohset.shape[0],0:cohset.shape[1]] unw[coh<thr]=np.nan group = gg.create_group(igram) dset = group.create_dataset(igram, data=unw, compression='gzip') for key, value in h5file[kf[0]][igram].attrs.iteritems(): group.attrs[key] = value try: #if kf[0] == 'interferograms': mask = h5file['mask'].get('mask') gm = h5file2.create_group('mask') dset = gm.create_dataset('mask', data=mask, compression='gzip') except: print 'no mask group found.' elif kf[0] in ('interferograms','wrapped') and 'coherence' not in h5mask.keys(): print 'Masking the '+kf[0] igramList=h5file[kf[0]].keys() gg = h5file2.create_group(kf[0]) for igram in igramList: print igram unwset = h5file[kf[0]][igram].get(igram) unw=unwset[0:unwset.shape[0],0:unwset.shape[1]] try: unw[M<thr] = np.nan except: unw[M==0] = np.nan group = gg.create_group(igram) dset = group.create_dataset(igram, data=unw, compression='gzip') for key, value in h5file[kf[0]][igram].attrs.iteritems(): group.attrs[key] = value try: #if kf[0] == 'interferograms': mask = h5file['mask'].get('mask') gm = h5file2.create_group('mask') dset = gm.create_dataset('mask', data=mask, compression='gzip') except: print 'no mask group found.' h5file.close() h5mask.close() h5file2.close() ################################ ROI_PAC ###################################### elif ext in ['.unw','.int']: import pysar._readfile as readfile import pysar._writefile as writefile Mext = os.path.splitext(maskFile)[1] if Mext == '.cor': a,M,atr = readfile.read_float32(maskFile) elif Mext == '.h5': h5mask=h5py.File(maskFile,'r') Mset=h5mask[h5mask.keys()[0]].get(h5mask.keys()[0]) M=Mset[0:Mset.shape[0],0:Mset.shape[1]] else: sys.exit(['Un-recoganized mask file extension: '+Mext]) try: MaskedFile except: MaskedFile = File.split('.')[0]+'_masked'+ext a,unw,atr = readfile.read_float32(File) try: unw[M<thr] = np.nan except: unw[M==0] = np.nan print 'writting >>> '+MaskedFile writefile.write_float32(unw,MaskedFile) f = open(MaskedFile+'.rsc','w') for k in atr.keys(): f.write(k+' '+atr[k]+'\n') f.close() try: h5mask.close() except: pass
def main(argv): try: file=argv[0] geomap=argv[1] except: Usage();sys.exit(1) ###################################################################################### fileName=os.path.basename(file).split('.')[0] h5file=h5py.File(file,'r') k=h5file.keys() if 'interferograms' in k: k[0] = 'interferograms' elif 'coherence' in k: k[0] = 'coherence' elif 'timeseries' in k: k[0] = 'timeseries' if k[0] in ('interferograms','coherence','wrapped'): atr = h5file[k[0]][h5file[k[0]].keys()[0]].attrs elif k[0] in ('dem','velocity','mask','temporal_coherence','rmse','timeseries'): atr = h5file[k[0]].attrs #### Subsetted radar coded file try: x0 = float(atr['subset_x0']) y0 = float(atr['subset_y0']) print '\nSubsetted radar coded file:\n creating temporary geomap file for it...' rg,az,rsc = readfile.read_float32(geomap) rg = rg - x0 az = az - y0 geomap = 'temp_'+geomap print ' writing '+geomap+'\n' writefile.write_float32(rg,az,geomap) fg = open(geomap+'.rsc','w') for kg in rsc.keys(): fg.write(kg+' '+rsc[kg]+'\n') fg.close() except: pass ###################################################################################### if k[0] in ('velocity','temporal_coherence','mask','rmse','dem'): dset = h5file[k[0]].get(k[0]) data = dset[0:dset.shape[0],0:dset.shape[1]] outname=fileName+'.unw' amp,unw,unwrsc = geocode_one(data,geomap,outname) f = h5py.File('geo_'+file,'w') group=f.create_group(k[0]) dset = group.create_dataset(k[0], data=unw, compression='gzip') for key,value in h5file[k[0]].attrs.iteritems(): group.attrs[key]=value for key,value in unwrsc.iteritems(): group.attrs[key] = value ###################################################################################### elif 'timeseries' in k: print 'geocoding timeseries:' outname='epoch_temp.unw' f = h5py.File('geo_'+file,'w') group = f.create_group('timeseries') epochList=h5file['timeseries'].keys() for epoch in epochList: print 'geocoding '+epoch d = h5file['timeseries'].get(epoch) data = d[0:d.shape[0],0:d.shape[1]] amp,unw,unwrsc = geocode_one(data,geomap,outname) dset = group.create_dataset(epoch, data=unw, compression='gzip') for key,value in unwrsc.iteritems(): group.attrs[key] = value for key,value in h5file[k[0]].attrs.iteritems(): group.attrs[key] = value group.attrs['WIDTH'] = unwrsc['WIDTH'] group.attrs['FILE_LENGTH'] = unwrsc['FILE_LENGTH'] ###################################################################################### elif k[0] in ['interferograms','coherence','wrapped']: print 'geocoding '+k[0]+' ...' if k[0] == 'interferograms': outname = k[0]+'_temp.unw' elif k[0] == 'coherence': outname = k[0]+'_temp.cor' else: outname = k[0]+'_temp.int' f = h5py.File('geo_'+file,'w') gg = f.create_group('interferograms') igramList=h5file[k[0]].keys() for igram in igramList: print 'geocoding '+igram d = h5file[k[0]][igram].get(igram) data = d[0:d.shape[0],0:d.shape[1]] amp,unw,unwrsc = geocode_one(data,geomap,outname) group = gg.create_group('geo_'+igram) dset = group.create_dataset('geo_'+igram, data=unw, compression='gzip') for key,value in unwrsc.iteritems(): group.attrs[key] = value for key,value in h5file[k[0]][igram].attrs.iteritems(): group.attrs[key] = value group.attrs['WIDTH'] = unwrsc['WIDTH'] group.attrs['FILE_LENGTH'] = unwrsc['FILE_LENGTH'] ####################### support of old format ####################### ### mask try: d = h5file['mask'].get('mask') data = d[0:d.shape[0],0:d.shape[1]] amp,unw,unwrsc = geocode_one(data,geomap,'mask_'+outname) gm = f.create_group('mask') dset = gm.create_dataset('mask', data=unw, compression='gzip') except: print 'No group for mask found in the file.' ### meanCoherence try: d = h5file['meanCoherence'].get('meanCoherence') data = d[0:d.shape[0],0:d.shape[1]] amp,unw,unwrsc = geocode_one(data,geomap,'meanCoherence_'+outname) gm = f.create_group('meanCoherence') dset = gm.create_dataset('meanCoherence', data=unw, compression='gzip') except: print 'No group for meanCoherence found in the file' ###################################################################################### try: atr['subset_x0'] rmCmd='rm '+geomap; os.system(rmCmd); print rmCmd rmCmd='rm '+geomap+'.rsc'; os.system(rmCmd); print rmCmd except: pass f.close() h5file.close()
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.'
def main(argv): try: File = argv[0] except: Usage() sys.exit(1) from numpy import pi import h5py import pysar._writefile as writefile h5file = h5py.File(File, 'r') k = h5file.keys() if 'interferograms' in k: k[0] = 'interferograms' elif 'coherence' in k: k[0] = 'coherence' elif 'timeseries' in k: k[0] = 'timeseries' if '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() elif k[0] in ['rmse', 'temporal_coherence', 'mask']: dset = h5file[k[0]].get(k[0]) data = dset[0:dset.shape[0], 0:dset.shape[1]] if k[0] == 'temporal_coherence': outname = File.split('.')[0] + '.cor' else: 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() elif 'timeseries' in k: dateList = h5file['timeseries'].keys() ## Input if len(sys.argv) == 2: print 'No input date specified >>> continue with the last date' dateList = h5file['timeseries'].keys() d = dateList[-1] elif len(sys.argv) == 3: d = sys.argv[2] elif len(sys.argv) == 4: ds = sys.argv[2:4] ds.sort() d_ref = ds[0] d = ds[1] else: Usage() sys.exit(1) ## Data Operation print 'reading ' + d + ' ... ' dset = h5file['timeseries'].get(d) data = dset[0:dset.shape[0], 0:dset.shape[1]] try: dset_ref = h5file['timeseries'].get(d_ref) print 'reading ' + d_ref + ' ... ' data_ref = dset_ref[0:dset_ref.shape[0], 0:dset_ref.shape[1]] data = data - data_ref except: pass wvl = float(h5file[k[0]].attrs['WAVELENGTH']) data = (-4 * pi / wvl) * data ## outName try: master_d = d_ref except: 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] outname = master_d + '_' + d + '.unw' print 'writing >>> ' + outname writefile.write_float32(data, outname) f = open(outname + '.rsc', 'w') for key, value in h5file[k[0]].attrs.iteritems(): if key == 'DATE12': f.write(key + ' ' + master_d + '-' + d + '\n') else: f.write(key + ' ' + str(value) + '\n') f.close() elif k[0] in ['interferograms', 'coherence', 'wrapped']: igramList = h5file[k[0]].keys() try: d = sys.argv[2] except: d = igramList[-1] print 'No input date specified >>> continue with the last date' print 'reading ' + d + ' ... ' dset = h5file[k[0]][d].get(d) data = dset[0:dset.shape[0], 0:dset.shape[1]] outname = d print 'writing >>> ' + outname writefile.write_float32(data, outname) f = open(outname + '.rsc', 'w') for key, value in h5file[k[0]][d].attrs.iteritems(): f.write(key + ' ' + str(value) + '\n') f.close() h5file.close()
def main(argv): try: File=argv[0] except: Usage();sys.exit(1) atr = readfile.read_attributes(File) k = atr['FILE_TYPE'] h5file=h5py.File(File,'r') print '\n************* Output to ROI_PAC format ***************' if k == 'velocity': dset = h5file['velocity'].get('velocity') data = dset[0:dset.shape[0],0:dset.shape[1]] print "converting velocity to a 1 year interferogram." wvl=float(h5file[k].attrs['WAVELENGTH']) data=(-4*pi/wvl)*data outname=File.split('.')[0]+'.unw' writefile.write(data,atr,outname) elif k == 'timeseries': dateList=h5file['timeseries'].keys() ## Input if len(sys.argv)==2: print 'No input date specified >>> continue with the last date' dateList=h5file['timeseries'].keys() d=dateList[-1] elif len(sys.argv)==3: d=sys.argv[2] elif len(sys.argv)==4: ds=sys.argv[2:4]; ds.sort() d_ref = ds[0] d = ds[1] else: Usage(); sys.exit(1) d = ptime.yyyymmdd(d) try: d_ref = ptime.yyyymmdd(d_ref) except: pass ## Data print 'reading '+d+' ... ' data = h5file['timeseries'].get(d)[:] try: print 'reading '+d_ref+' ... ' data_ref = h5file['timeseries'].get(d_ref)[:] data = data - data_ref except: pass wvl=float(atr['WAVELENGTH']) data *= -4*pi/wvl ## outName try: master_d = d_ref except: try: master_d = atr['ref_date'] except: master_d = atr['DATE'] if len(master_d)==8: master_d=master_d[2:8] if len(d)==8: d=d[2:8] outname = master_d+'_'+d+'.unw' ## Attributes atr['FILE_TYPE'] = '.unw' atr['P_BASELINE_TIMESERIES'] = '0.0' atr['UNIT'] = 'radian' atr['DATE'] = master_d atr['DATE12'] = master_d+'-'+d ## Writing writefile.write(data,atr,outname) elif k in ['interferograms','coherence','wrapped']: ## Check input igramList=h5file[k].keys() try: d = sys.argv[2] for i in range(len(igramList)): if d in igramList[i]: igram = igramList[i] except: igram = igramList[-1]; print 'No input date specified >>> continue with the last date' ## Read and Write print 'reading '+igram+' ... ' dset = h5file[k][igram].get(igram) data = dset[0:dset.shape[0],0:dset.shape[1]] outname = igram print 'writing >>> '+ outname writefile.write_float32(data,outname) f = open(outname+'.rsc','w') for key , value in h5file[k][igram].attrs.iteritems(): f.write(key+' '+str(value)+'\n') f.close() else: dset = h5file[k].get(k) data = dset[0:dset.shape[0],0:dset.shape[1]] if k == 'temporal_coherence': outname=File.split('.')[0]+'.cor' else: outname=File.split('.')[0]+'.unw' writefile.write(data,atr,outname) h5file.close()
def main(argv): try: file=argv[0] geomap=argv[1] except: Usage();sys.exit(1) ###################################################################################### fileName=os.path.basename(file).split('.')[0] h5file=h5py.File(file,'r') atr = readfile.read_attributes(file) k = atr['FILE_TYPE'] print '\n***************** Geocoding *******************' print 'input file: '+k #### Subsetted radar coded file try: x0 = float(atr['subset_x0']) y0 = float(atr['subset_y0']) print '\nSubsetted radar coded file:\n creating temporary geomap file for it...' rg,az,rsc = readfile.read_float32(geomap) rg = rg - x0 az = az - y0 geomap = 'temp_'+geomap print ' writing '+geomap+'\n' writefile.write_float32(rg,az,geomap) fg = open(geomap+'.rsc','w') for kg in rsc.keys(): fg.write(kg+' '+rsc[kg]+'\n') fg.close() except: pass ###################################################################################### if k in ['timeseries']: outname='epoch_temp.unw' f = h5py.File('geo_'+file,'w') group = f.create_group('timeseries') epochList = h5file['timeseries'].keys() epochList = sorted(epochList) for epoch in epochList: print 'geocoding '+epoch data = h5file['timeseries'].get(epoch)[:] amp,unw,unwrsc = geocode_one(data,geomap,outname) dset = group.create_dataset(epoch, data=unw, compression='gzip') atr = geocode_attributes(atr,unwrsc) for key,value in atr.iteritems(): group.attrs[key] = value ###################################################################################### elif k in ['interferograms','coherence','wrapped']: if k == 'interferograms': outname = k[0]+'_temp.unw' elif k == 'coherence' : outname = k[0]+'_temp.cor' else: outname = k[0]+'_temp.int' f = h5py.File('geo_'+file,'w') gg = f.create_group('interferograms') igramList = h5file[k].keys() igramList = sorted(igramList) for igram in igramList: print 'geocoding '+igram data = h5file[k][igram].get(igram)[:] amp,unw,unwrsc = geocode_one(data,geomap,outname) group = gg.create_group('geo_'+igram) dset = group.create_dataset('geo_'+igram, data=unw, compression='gzip') atr = geocode_attributes(h5file[k][igram].attrs, unwrsc) for key,value in atr.iteritems(): group.attrs[key] = value ####################### support of old format ####################### ### mask try: data = h5file['mask'].get('mask')[:] amp,unw,unwrsc = geocode_one(data,geomap,'mask_'+outname) gm = f.create_group('mask') dset = gm.create_dataset('mask', data=unw, compression='gzip') except: print 'No group for mask found in the file.' ### meanCoherence try: data = h5file['meanCoherence'].get('meanCoherence')[:] amp,unw,unwrsc = geocode_one(data,geomap,'meanCoherence_'+outname) gm = f.create_group('meanCoherence') dset = gm.create_dataset('meanCoherence', data=unw, compression='gzip') except: print 'No group for meanCoherence found in the file' ###################################################################################### else: data,atr = readfile.read(file) outname=fileName+'.unw' amp,unw,unwrsc = geocode_one(data,geomap,outname) atr = geocode_attributes(atr,unwrsc) writefile.write(unw,atr,'geo_'+file) ###################################################################################### try: atr['subset_x0'] rmCmd='rm '+geomap; os.system(rmCmd); print rmCmd rmCmd='rm '+geomap+'.rsc'; os.system(rmCmd); print rmCmd except: pass try: f.close() h5file.close() except: pass
def main(argv): #lineWidth=4 #fontSize=32 #markerColor='orange' #markerSize=20 #if len(sys.argv)>2: try: opts, args = getopt.getopt(argv, "h:f:m:t:x:y:o:") except getopt.GetoptError: Usage() sys.exit(1) for opt, arg in opts: if opt in ("-h", "--help"): Usage() sys.exit() elif opt == '-f': File = arg elif opt == '-m': maskFile = arg elif opt == '-t': thr = float(arg) elif opt == '-y': ysub = [int(i) for i in arg.split(':')] ysub.sort() elif opt == '-x': xsub = [int(i) for i in arg.split(':')] xsub.sort() elif opt == '-o': MaskedFile = arg try: File maskFile except: Usage() sys.exit(1) ###################################### ext = os.path.splitext(File)[1] import h5py import numpy as np ################################ PySAR HDF5 ################################# if ext == '.h5': h5file = h5py.File(File, 'r') h5mask = h5py.File(maskFile, 'r') kf = h5file.keys() if 'coherence' not in h5mask.keys(): Mset = h5mask[h5mask.keys()[0]].get(h5mask.keys()[0]) M = Mset[0:Mset.shape[0], 0:Mset.shape[1]] try: MaskedFile except: MaskedFile = File.split('.')[0] + '_masked.h5' h5file2 = h5py.File(MaskedFile, 'w') if len(kf) == 1 and kf[0] in ('velocity', 'temporal_coherence', 'rmse', 'mask'): Vset = h5file[h5file.keys()[0]].get(h5file.keys()[0]) V = Vset[0:Vset.shape[0], 0:Vset.shape[1]] try: xsub ysub M[ysub[0]:ysub[1], xsub[0]:xsub[1]] = 0 except: print 'No subset' try: V[M < thr] = np.nan except: V[M == 0] = np.nan group = h5file2.create_group(kf[0]) dset = group.create_dataset(os.path.basename(kf[0]), data=V, compression='gzip') for key, value in h5file[kf[0]].attrs.iteritems(): group.attrs[key] = value elif 'timeseries' in h5file.keys(): print 'Masking the time-series' group = h5file2.create_group('timeseries') dateList = h5file['timeseries'].keys() for d in dateList: print d unwset = h5file['timeseries'].get(d) unw = unwset[0:unwset.shape[0], 0:unwset.shape[1]] try: unw[M < thr] = np.nan except: unw[M == 0] = np.nan dset = group.create_dataset(d, data=unw, compression='gzip') for key, value in h5file['timeseries'].attrs.iteritems(): group.attrs[key] = value elif kf[0] in ('interferograms', 'wrapped') and 'coherence' in h5mask.keys(): print 'Masking each ' + kf[0] + ' using its coherence file' igramList = h5file[kf[0]].keys() cohList = h5mask['coherence'].keys() gg = h5file2.create_group(kf[0]) for igram in igramList: print igram date12 = h5file[kf[0]][igram].attrs['DATE12'] for cohFile in cohList: if h5mask['coherence'][cohFile].attrs['DATE12'] == date12: igramCoh = cohFile print igramCoh unwset = h5file[kf[0]][igram].get(igram) unw = unwset[0:unwset.shape[0], 0:unwset.shape[1]] cohset = h5mask['coherence'][igramCoh].get(igramCoh) coh = cohset[0:cohset.shape[0], 0:cohset.shape[1]] unw[coh < thr] = np.nan group = gg.create_group(igram) dset = group.create_dataset(igram, data=unw, compression='gzip') for key, value in h5file[kf[0]][igram].attrs.iteritems(): group.attrs[key] = value try: #if kf[0] == 'interferograms': mask = h5file['mask'].get('mask') gm = h5file2.create_group('mask') dset = gm.create_dataset('mask', data=mask, compression='gzip') except: print 'no mask group found.' elif kf[0] in ('interferograms', 'wrapped') and 'coherence' not in h5mask.keys(): print 'Masking the ' + kf[0] igramList = h5file[kf[0]].keys() gg = h5file2.create_group(kf[0]) for igram in igramList: print igram unwset = h5file[kf[0]][igram].get(igram) unw = unwset[0:unwset.shape[0], 0:unwset.shape[1]] try: unw[M < thr] = np.nan except: unw[M == 0] = np.nan group = gg.create_group(igram) dset = group.create_dataset(igram, data=unw, compression='gzip') for key, value in h5file[kf[0]][igram].attrs.iteritems(): group.attrs[key] = value try: #if kf[0] == 'interferograms': mask = h5file['mask'].get('mask') gm = h5file2.create_group('mask') dset = gm.create_dataset('mask', data=mask, compression='gzip') except: print 'no mask group found.' h5file.close() h5mask.close() h5file2.close() ################################ ROI_PAC ###################################### elif ext in ['.unw', '.int']: import pysar._readfile as readfile import pysar._writefile as writefile Mext = os.path.splitext(maskFile)[1] if Mext == '.cor': a, M, atr = readfile.read_float32(maskFile) elif Mext == '.h5': h5mask = h5py.File(maskFile, 'r') Mset = h5mask[h5mask.keys()[0]].get(h5mask.keys()[0]) M = Mset[0:Mset.shape[0], 0:Mset.shape[1]] else: sys.exit(['Un-recoganized mask file extension: ' + Mext]) try: MaskedFile except: MaskedFile = File.split('.')[0] + '_masked' + ext a, unw, atr = readfile.read_float32(File) try: unw[M < thr] = np.nan except: unw[M == 0] = np.nan print 'writting >>> ' + MaskedFile writefile.write_float32(unw, MaskedFile) f = open(MaskedFile + '.rsc', 'w') for k in atr.keys(): f.write(k + ' ' + atr[k] + '\n') f.close() try: h5mask.close() except: pass
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.'
def main(argv): if len(sys.argv)>2: try: opts, args = getopt.getopt(argv,"h:f:x:y:o:l:L:") except getopt.GetoptError: print 'Error while getting args' Usage() ; sys.exit(1) for opt,arg in opts: if opt in ("-h","--help"): Usage() ; sys.exit() elif opt == '-f': File = arg elif opt == '-y': ysub = [int(i) for i in arg.split(':')]; ysub.sort() elif opt == '-x': xsub = [int(i) for i in arg.split(':')]; xsub.sort() elif opt == '-o': outName=arg elif opt == '-l': Latsub = [float(i) for i in arg.split(':')]; Latsub.sort() elif opt == '-L': Lonsub = [float(i) for i in arg.split(':')]; Lonsub.sort() else: Usage(); sys.exit(1) try: outName except: outName='subset_'+File ext = os.path.splitext(File)[1] ############################################################################ ################################# PySAR ################################## if ext == '.h5': try: h5file=h5py.File(File,'r') except: Usage() ; sys.exit(1) k=h5file.keys() if 'interferograms' in k: k[0] = 'interferograms' elif 'coherence' in k: k[0] = 'coherence' elif 'timeseries' in k: k[0] = 'timeseries' if k[0] in ('interferograms','coherence','wrapped'): atr = h5file[k[0]][h5file[k[0]].keys()[0]].attrs elif k[0] in ('dem','velocity','mask','temporal_coherence','rmse','timeseries'): atr = h5file[k[0]].attrs ############# Subset Option ############# width=int(atr['WIDTH']) length=int(atr['FILE_LENGTH']) try: Latsub try: lat_step = float(atr['Y_STEP']) lat1 = float(atr['Y_FIRST']) lat0 = lat1 + length*lat_step if Latsub[0]<lat0: Latsub[0]=lat0; print 'WARNING: input latitude < min ('+str(lat0)+')! Set it to min.' if Latsub[1]>lat1: Latsub[1]=lat1; print 'WARNING: input latitude > max ('+str(lat1)+')! Set it to max.' print 'subset in latitude - '+str(Latsub[0])+':'+str(Latsub[1]) ysub=[0]*2 ysub[0] = int((Latsub[1]-lat1)/lat_step) ysub[1] = int((Latsub[0]-lat1)/lat_step) except: print 'Not geocoded file, cannot be subseted with LatLon.' Usage() ; return except: try: ysub if ysub[0]<0: ysub[0]=0; print 'WARNING: input y < min (0)! Set it to min.' if ysub[1]>length: ysub[1]=length; print 'WARNING: input y > max ('+str(length)+')! Set it to max.' print 'subset in y direction - '+str(ysub[0])+':'+str(ysub[1]) except: ysub = [0,length] try: Lonsub try: lon_step = float(atr['X_STEP']) lon0 = float(atr['X_FIRST']) lon1 = lon0 + width*lon_step if Lonsub[0]<lon0: Lonsub[0]=lon0; print 'WARNING: input longitude < min ('+str(lon0)+')! Set it to min.' if Lonsub[1]>lon1: Lonsub[1]=lon1; print 'WARNING: input longitude > max ('+str(lon1)+')! Set it to max.' print 'subset in longitude - '+str(Lonsub[0])+':'+str(Lonsub[1]) xsub=[0]*2 xsub[0]=int((Lonsub[0]-lon0)/lon_step) xsub[1]=int((Lonsub[1]-lon0)/lon_step) except: print 'Not geocoded file, cannot be subseted with LatLon.' Usage() ; return except: try: xsub if xsub[0]<0: xsub[0]=0; print 'WARNING: input x < min (0)! Set it to min.' if xsub[1]>width: xsub[1]=width; print 'WARNING: input x > max ('+str(width)+')! Set it to max x.' print 'subset in x direction - '+str(xsub[0])+':'+str(xsub[1]) except: xsub = [0,width] if ysub[0]>length or ysub[1]<0 or xsub[0]>length or xsub[1]<0: print 'ERROR: input index is out of data range!' print 'range in rdr: x - 0:'+str(width)+' y - 0:'+str(length) try: print 'range in geo: lat - '+str(lat0)+':'+str(lat1)+' lon - '+str(lon0)+':'+str(lon1) except: Geo=0 sys.exit(1) ######## Data Read, Crop and Write ####### ##### N dset, N attributes if k[0] in ('interferograms','coherence','wrapped'): print 'writing >>> ' +outName h5out=h5py.File(outName,'w') gg=h5out.create_group(k[0]) igramList=h5file[k[0]].keys() for igram in igramList: print igram dset1=h5file[k[0]][igram].get(igram) group=gg.create_group(igram) dset=group.create_dataset(igram, data=dset1[ysub[0]:ysub[1],xsub[0]:xsub[1]], compression='gzip') for key, value in h5file[k[0]][igram].attrs.iteritems(): group.attrs[key] = value group.attrs['FILE_LENGTH']=ysub[1]-ysub[0] group.attrs['WIDTH'] =xsub[1]-xsub[0] try: sub_x0_ori = int(group.attrs['subset_x0']) group.attrs['subset_x0'] = xsub[0]+sub_x0_ori group.attrs['subset_x1'] = xsub[1]+sub_x0_ori except: group.attrs['subset_x0']=xsub[0] group.attrs['subset_x1']=xsub[1] try: sub_y0_ori = int(group.attrs['subset_y0']) group.attrs['subset_y0'] = ysub[0]+sub_y0_ori group.attrs['subset_y1'] = ysub[1]+sub_y0_ori except: group.attrs['subset_y0']=ysub[0] group.attrs['subset_y1']=ysub[1] if 'X_FIRST' in atr.keys(): group.attrs['X_FIRST']=float(atr['X_FIRST']) + xsub[0]*float(atr['X_STEP']) group.attrs['Y_FIRST']=float(atr['Y_FIRST']) + ysub[0]*float(atr['Y_STEP']) ## support of old format try: Mset=h5file['mask'].get('mask') gm=h5out.create_group('mask') dset=gm.create_dataset('mask', data=Mset[ysub[0]:ysub[1],xsub[0]:xsub[1]], compression='gzip') except: print 'No group for mask found in the file.' try: Cset=h5file['meanCoherence'].get('meanCoherence') gm=h5out.create_group('meanCoherence') dset=gm.create_dataset('meanCoherence', data=Cset[ysub[0]:ysub[1],xsub[0]:xsub[1]], compression='gzip') except: print 'No group for meanCoherence found in the file' h5file.close() h5out.close() ##### N/1 dset, 1 attributes elif k[0] in ['timeseries', 'temporal_coherence', 'velocity', 'mask', 'rmse']: print 'writing >>> ' +outName h5out=h5py.File(outName,'w') group=h5out.create_group(k[0]) if k[0] == 'timeseries': dateList=h5file[k[0]].keys() for d in dateList: print d dset1=h5file[k[0]].get(d) dset=group.create_dataset(d, data=dset1[ysub[0]:ysub[1],xsub[0]:xsub[1]], compression='gzip') elif k[0] in ['temporal_coherence', 'velocity', 'mask', 'rmse']: dset1=h5file[k[0]].get(k[0]) dset=group.create_dataset(k[0],data=dset1[ysub[0]:ysub[1],xsub[0]:xsub[1]],compression='gzip') ## Update attributes for key, value in h5file[k[0]].attrs.iteritems(): group.attrs[key] = value group.attrs['FILE_LENGTH']=ysub[1]-ysub[0] group.attrs['WIDTH'] =xsub[1]-xsub[0] try: sub_x0_ori = int(group.attrs['subset_x0']) group.attrs['subset_x0'] = xsub[0]+sub_x0_ori group.attrs['subset_x1'] = xsub[1]+sub_x0_ori except: group.attrs['subset_x0']=xsub[0] group.attrs['subset_x1']=xsub[1] try: sub_y0_ori = int(group.attrs['subset_y0']) group.attrs['subset_y0'] = ysub[0]+sub_y0_ori group.attrs['subset_y1'] = ysub[1]+sub_y0_ori except: group.attrs['subset_y0']=ysub[0] group.attrs['subset_y1']=ysub[1] if 'X_FIRST' in atr.keys(): group.attrs['X_FIRST']=float(atr['X_FIRST']) + xsub[0]*float(atr['X_STEP']) group.attrs['Y_FIRST']=float(atr['Y_FIRST']) + ysub[0]*float(atr['Y_STEP']) h5file.close() h5out.close() else: print 'Error: group of HDF5 file not recogized!' h5file.close() Usage() ; sys.exit(1) ############################################################################ ######################### ROI_PAC / Image / GAMMA ######################## elif ext in ['.unw','.cor','.hgt','.dem','.trans'] or\ ext in ['.jpeg','.jpg','.png','.ras','.bmp'] or\ ext in ['.mli','.slc']: try: atr = readfile.read_rsc_file(File + '.rsc') except: try: atr = readfile.read_par_file(File + '.par') except: atr = readfile.read_par_file(os.path.splitext(File)[0] + '.par') ############# Subset Option ############# try: width = int(atr['WIDTH']); length = int(atr['FILE_LENGTH']) except: width = int(atr['range_samples:']); length = int(atr['azimuth_lines:']) try: Latsub try: lat_step = float(atr['Y_STEP']) lat1 = float(atr['Y_FIRST']) lat0 = lat1 + length*lat_step if Latsub[0]<lat0: Latsub[0]=lat0; print 'WARNING: input latitude < min ('+str(lat0)+')! Set it to min.' if Latsub[1]>lat1: Latsub[1]=lat1; print 'WARNING: input latitude > max ('+str(lat1)+')! Set it to max.' print 'subset in latitude - '+str(Latsub[0])+':'+str(Latsub[1]) ysub=[0]*2 ysub[0] = int((Latsub[1]-lat1)/lat_step) ysub[1] = int((Latsub[0]-lat1)/lat_step) except: print 'Not geocoded file, cannot be subseted with LatLon.' Usage() ; return except: try: ysub if ysub[0]<0: ysub[0]=0; print 'WARNING: input y < min (0)! Set it to min.' if ysub[1]>length: ysub[1]=length; print 'WARNING: input y > max ('+str(length)+')! Set it to max.' print 'subset in y direction - '+str(ysub[0])+':'+str(ysub[1]) except: ysub = [0,length] try: Lonsub try: lon_step = float(atr['X_STEP']) lon0 = float(atr['X_FIRST']) lon1 = lon0 + width*lon_step if Lonsub[0]<lon0: Lonsub[0]=lon0; print 'WARNING: input longitude < min ('+str(lon0)+')! Set it to min.' if Lonsub[1]>lon1: Lonsub[1]=lon1; print 'WARNING: input longitude > max ('+str(lon1)+')! Set it to max.' print 'subset in longitude - '+str(Lonsub[0])+':'+str(Lonsub[1]) xsub=[0]*2 xsub[0]=int((Lonsub[0]-lon0)/lon_step) xsub[1]=int((Lonsub[1]-lon0)/lon_step) except: print 'Not geocoded file, cannot be subseted with LatLon.' Usage() ; return except: try: xsub if xsub[0]<0: xsub[0]=0; print 'WARNING: input x < min (0)! Set it to min.' if xsub[1]>width: xsub[1]=width; print 'WARNING: input x > max ('+str(width)+')! Set it to max x.' print 'subset in x direction - '+str(xsub[0])+':'+str(xsub[1]) except: xsub = [0,width] if ysub[0]>length or ysub[1]<0 or xsub[0]>length or xsub[1]<0: print 'ERROR: input index is out of data range!' print 'range in rdr: x - 0:'+str(width)+' y - 0:'+str(length) try: print 'range in geo: lat - '+str(lat0)+':'+str(lat1)+' lon - '+str(lon0)+':'+str(lon1) except: Geo=0 sys.exit(1) ######## Data Read, Crop and Write ####### print 'writing >>> '+outName box = (xsub[0],ysub[0],xsub[1],ysub[1]) if ext in ['.unw','.cor','.hgt']: a,p,r = readfile.read_float32(File,box) #p = p[ysub[0]:ysub[1],xsub[0]:xsub[1]] writefile.write_float32(p,outName) elif ext == '.dem': p,r = readfile.read_dem(File) p = p[ysub[0]:ysub[1],xsub[0]:xsub[1]] writefile.write_dem(p,outName) elif ext == '.trans': a,p,r = readfile.read_float32(File,box) #a = a[ysub[0]:ysub[1],xsub[0]:xsub[1]] #p = p[ysub[0]:ysub[1],xsub[0]:xsub[1]] writefile.write_float32(a,p,outName) elif ext in ['.jpeg','.jpg','.png','.ras','.bmp']: import Image im = Image.open(File) box = (xsub[0],ysub[0],xsub[1],ysub[1]) output_img = im.crop(box) output_img.save(outName) elif ext == '.mli': d,r = readfile.read_gamma_float(File) d = d[ysub[0]:ysub[1],xsub[0]:xsub[1]] writefile.write_gamma_float(d,outName) elif ext == '.slc': d,r = readfile.read_gamma_scomplex(File,box) writefile.write_gamma_scomplex(d,outName) ########### Update .rsc file ############# atr['FILE_LENGTH'] = str(ysub[1]-ysub[0]) atr['WIDTH'] = str(xsub[1]-xsub[0]) atr['XMAX'] = str(width - 1) atr['YMAX'] = str(length - 1) try: sub_x0_ori = int(atr['subset_x0']) atr['subset_x0'] = str(xsub[0] + sub_x0_ori) atr['subset_x1'] = str(xsub[1] + sub_x0_ori) except: atr['subset_x0'] = str(xsub[0]) atr['subset_x1'] = str(xsub[1]) try: sub_y0_ori = int(atr['subset_y0']) atr['subset_y0'] = str(ysub[0] + sub_y0_ori) atr['subset_y1'] = str(ysub[1] + sub_y0_ori) except: atr['subset_y0'] = str(ysub[0]) atr['subset_y1'] = str(ysub[1]) if 'X_FIRST' in atr.keys(): atr['Y_FIRST']=str(float(atr['Y_FIRST'])+ysub[0]*float(atr['Y_STEP'])) atr['X_FIRST']=str(float(atr['X_FIRST'])+xsub[0]*float(atr['X_STEP'])) f = open(outName+'.rsc','w') for k in atr.keys(): f.write(k+' '+atr[k]+'\n') f.close() ########################################################################### else: print 'File extension not recogized.' Usage() ; sys.exit(1)
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
def main(argv): try: File=argv[0] except: Usage();sys.exit(1) from numpy import pi import h5py import pysar._writefile as writefile h5file=h5py.File(File,'r') k=h5file.keys() if 'interferograms' in k: k[0] = 'interferograms' elif 'coherence' in k: k[0] = 'coherence' elif 'timeseries' in k: k[0] = 'timeseries' if '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() elif k[0] in ['rmse','temporal_coherence','mask']: dset = h5file[k[0]].get(k[0]) data = dset[0:dset.shape[0],0:dset.shape[1]] if k[0] == 'temporal_coherence': outname=File.split('.')[0]+'.cor' else: 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() elif 'timeseries' in k: dateList=h5file['timeseries'].keys() ## Input if len(sys.argv)==2: print 'No input date specified >>> continue with the last date' dateList=h5file['timeseries'].keys() d=dateList[-1] elif len(sys.argv)==3: d=sys.argv[2] elif len(sys.argv)==4: ds=sys.argv[2:4]; ds.sort() d_ref = ds[0] d = ds[1] else: Usage(); sys.exit(1) ## Data Operation print 'reading '+d+' ... ' dset = h5file['timeseries'].get(d) data = dset[0:dset.shape[0],0:dset.shape[1]] try: dset_ref = h5file['timeseries'].get(d_ref) print 'reading '+d_ref+' ... ' data_ref = dset_ref[0:dset_ref.shape[0],0:dset_ref.shape[1]] data = data - data_ref except: pass wvl=float(h5file[k[0]].attrs['WAVELENGTH']) data=(-4*pi/wvl)*data ## outName try: master_d = d_ref except: 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] outname = master_d+'_'+d+'.unw' print 'writing >>> '+ outname writefile.write_float32(data,outname) f = open(outname+'.rsc','w') for key , value in h5file[k[0]].attrs.iteritems(): if key=='DATE12': f.write(key+' '+master_d+'-'+d+'\n') else: f.write(key+' '+str(value)+'\n') f.close() elif k[0] in ['interferograms','coherence','wrapped']: igramList=h5file[k[0]].keys() try: d=sys.argv[2] except: d=igramList[-1]; print 'No input date specified >>> continue with the last date' print 'reading '+d + ' ... ' dset=h5file[k[0]][d].get(d) data = dset[0:dset.shape[0],0:dset.shape[1]] outname=d print 'writing >>> '+ outname writefile.write_float32(data,outname) f = open(outname+'.rsc','w') for key , value in h5file[k[0]][d].attrs.iteritems(): f.write(key+' '+str(value)+'\n') f.close() h5file.close()