improved speed on writing to file (write instead of writelines)

This commit is contained in:
Marcel Paffrath 2016-05-11 12:02:09 +02:00
parent d8a764db31
commit 46c152b7a1
2 changed files with 375 additions and 200 deletions

View File

@ -6,32 +6,152 @@ import datetime
import numpy as np import numpy as np
class Tomo3d(object): class Tomo3d(object):
def __init__(self, nproc): def __init__(self, nproc, iterations):
self.setCWD()
self.defParas() self.defParas()
self.nproc = nproc self.nproc = nproc
self.iter = iterations # number of iterations
self.citer = 0 # current iteration
self.sources = self.readSrcFile() self.sources = self.readSrcFile()
self.traces = self.readTraces() self.traces = self.readTraces()
self.directories = []
def defParas(self): def defParas(self):
self.fmm = 'fm3d' self.defFMMParas()
self.defInvParas()
def defFMMParas(self):
self.fmm = '{0}/fm3d'.format(self.cwd)
self.frechgen = '{0}/frechgen'.format(self.cwd)
self.cvg = 'vgrids.in' self.cvg = 'vgrids.in'
self.cig = 'interfaces.in' self.cig = 'interfaces.in'
self.csl = 'sources.in' self.csl = 'sources.in'
self.pg = 'propgrid.in' self.pg = 'propgrid.in'
self.rec = 'receivers.in' self.rec = 'receivers.in'
#self.ot = 'otimes.dat'
self.frech = 'frechet.in' self.frech = 'frechet.in'
self.ot = 'otimes.dat'
self.ttim = 'arrivals.dat'
self.mode = 'mode_set.in' self.mode = 'mode_set.in'
self.cwd = subprocess.check_output(['pwd'])[0:-1] + '/' self.folder = '.proc_'
self.folder = 'test_'
def defInvParas(self):
# Name of program for performing inversion
self.inv = '{0}/invert3d'.format(self.cwd)
# Name of file containing current model traveltimes
self.mtrav = 'mtimes.dat'
# Name of file containing reference model traveltimes
self.rtrav = 'rtimes.dat'
# Name of file containing initial velocity grid
self.ivg = 'vgridsref.in'
# Name of file containing initial interface grid
self.iig = 'interfacesref.in'
# Name of file containing initial source locations
self.isl = 'sourcesref.in'
# Name of program for calculating traveltime residuals
self.resid = '{0}/residuals'.format(self.cwd)
# Name of output file for calculating traveltime residuals
self.resout = 'residuals.dat'
def copyRef(self):
# Attention: Copies reference grids to used grids (e.g. sourcesref.in to sources.in)
os.system('cp %s %s'%(self. ivg, self.cvg))
os.system('cp %s %s'%(self.iig, self.cig))
os.system('cp %s %s'%(self.isl, self.csl))
def setCWD(self):
self.cwd = subprocess.check_output(['pwd'])[0:-1]
print('Working directory is pwd: %s'%self.cwd)
def runFrech(self):
os.system(self.frechgen)
def runFmm(self, directory, logfile, processes): def runFmm(self, directory, logfile, processes):
os.chdir(directory) os.chdir(directory)
processes.append(subprocess.Popen('fm3d', stdout = None)) fout = open(logfile, 'w')
# os.system('%s > %s &'%(self.fmm, logfile)) processes.append(subprocess.Popen(self.fmm, stdout = fout))
fout.close()
os.chdir(self.cwd) os.chdir(self.cwd)
return processes return processes
def calcRes(self):
resout = os.path.join(self.cwd, self.resout)
if self.citer == 0:
os.system('%s > %s'%(self.resid, resout))
else:
os.system('%s >> %s'%(self.resid, resout))
with open(resout, 'r') as infile:
residuals = infile.readlines()
RMS, var, chi2 = residuals[-1].split()
print('Residuals: RMS = %s, var = %s, Chi^2 = %s.'%(RMS, var, chi2))
def runTOMO3D(self):
starttime = datetime.datetime.now()
print('Starting TOMO3D on %s parallel processes for %s iteration(s).'
%(self.nproc, self.iter))
if self.citer == 0:
self.startForward()
self.raiseIter()
while self.citer <= self.iter:
self.startInversion()
self.startForward()
self.raiseIter()
tdelta = datetime.datetime.now() - starttime
print('runTOMO3D: Finished %s iterations after %s.'%(self.iter, tdelta))
def _printLine(self):
print('----------------------------------------')
def raiseIter(self):
self.citer +=1
self._printLine()
def startInversion(self):
print('Calling %s...'%self.inv)
os.system(self.inv)
def startForward(self):
self._printLine()
print('Starting forward simulation for iteration %s.'%(self.citer))
self.makeDirectories()
starttime = datetime.datetime.now()
processes = []
if self.citer == 0:
self.copyRef()
self.runFrech()
for procID in range(1, self.nproc + 1):
directory = self.getProcDir(procID)
log_out = self.cwd + '/fm3dlog_' + str(procID) + '.out'
self.writeSrcFile(procID, directory)
self.writeTracesFile(procID, directory)
os.system('cp {cvg} {cig} {mode} {pg} {frechout} {dest}'
.format(cvg=self.cvg, cig=self.cig, frechout=self.frech,
mode=self.mode, pg=self.pg, dest=directory))
processes = self.runFmm(directory, log_out, processes)
for p in processes:
p.wait()
self.mergeOutput()
self.clearDirectories()
self.copyArrivals()
if self.citer == 0:
self.copyArrivals(self.rtrav)
self.calcRes()
tdelta = datetime.datetime.now() - starttime
print('Finished Forward calculation after %s'%tdelta)
def copyArrivals(self, target = None):
if target == None:
target = self.mtrav
os.system('cp %s %s'%(self.ttim, target))
def makeDIR(self, directory): def makeDIR(self, directory):
err = os.system('mkdir %s'%directory) err = os.system('mkdir %s'%directory)
if err is 256: if err is 256:
@ -40,6 +160,21 @@ class Tomo3d(object):
print('Overwriting existing files.') print('Overwriting existing files.')
else: else:
sys.exit('Aborted') sys.exit('Aborted')
self.directories.append(directory)
def clearDIR(self, directory):
err = os.system('rm -r %s'%directory)
def makeDirectories(self):
for procID in range(1, self.nproc + 1):
directory = self.getProcDir(procID)
self.makeDIR(directory)
def clearDirectories(self):
for directory in self.directories:
self.clearDIR(directory)
self.directories = []
def readNsrc(self): def readNsrc(self):
srcfile = open(self.csl, 'r') srcfile = open(self.csl, 'r')
@ -73,29 +208,6 @@ class Tomo3d(object):
start = (srcPK + 1) * remain + srcPK * (proc - remain) + 1 start = (srcPK + 1) * remain + srcPK * (proc - remain) + 1
return range(start, start + srcPK) return range(start, start + srcPK)
def startTomo(self):
starttime = datetime.datetime.now()
processes = []
for procID in range(1, self.nproc + 1):
directory = self.getProcDir(procID)
log_out = self.cwd + 'fm3dlog_' + str(procID) + '.out'
self.makeDIR(directory) # Problem bei Iteration
self.writeSrcFile(procID, directory)
self.writeTracesFile(procID, directory)
os.system('cp %s %s %s %s %s %s %s'
%(self.cvg, self.cig, self.frech,
self.fmm, self.mode, self.pg, directory))
processes = self.runFmm(directory, log_out, processes)
for p in processes:
p.wait()
self.mergeOutput()
tdelta = datetime.datetime.now() - starttime
print('Finished after %s'%tdelta)
def readSrcFile(self): def readSrcFile(self):
nsrc = self.readNsrc() nsrc = self.readNsrc()
srcfile = open(self.csl, 'r') srcfile = open(self.csl, 'r')
@ -150,32 +262,32 @@ class Tomo3d(object):
srcfile = open('%s/sources.in'%directory, 'w') srcfile = open('%s/sources.in'%directory, 'w')
sourceIDs = self.srcIDs4Kernel(procID) sourceIDs = self.srcIDs4Kernel(procID)
srcfile.writelines('%s\n'%len(sourceIDs)) srcfile.write('%s\n'%len(sourceIDs))
for sourceID in sourceIDs: for sourceID in sourceIDs:
source = self.sources[sourceID] source = self.sources[sourceID]
coords = source['coords'] coords = source['coords']
interactions = source['interactions'] interactions = source['interactions']
srcfile.writelines('%s\n'%source['teleflag']) srcfile.write('%s\n'%source['teleflag'])
srcfile.writelines('%s %s %s\n'%(float(coords[0]), float(coords[1]), float(coords[2]))) srcfile.write('%s %s %s\n'%(float(coords[0]), float(coords[1]), float(coords[2])))
srcfile.writelines('%s\n'%source['numpaths']) srcfile.write('%s\n'%source['numpaths'])
srcfile.writelines('%s\n'%source['steps']) srcfile.write('%s\n'%source['steps'])
srcfile.writelines('%s %s\n'%(int(interactions[0]), int(interactions[1]))) srcfile.write('%s %s\n'%(int(interactions[0]), int(interactions[1])))
srcfile.writelines('%s\n'%source['veltype']) srcfile.write('%s\n'%source['veltype'])
def writeTracesFile(self, procID, directory): def writeTracesFile(self, procID, directory):
recfile = open('%s/receivers.in'%directory, 'w') recfile = open('%s/receivers.in'%directory, 'w')
sourceIDs = self.srcIDs4Kernel(procID) sourceIDs = self.srcIDs4Kernel(procID)
traceIDs = self.getTraceIDs4Sources(sourceIDs) traceIDs = self.getTraceIDs4Sources(sourceIDs)
recfile.writelines('%s\n'%len(traceIDs)) recfile.write('%s\n'%len(traceIDs))
for traceID in traceIDs: for traceID in traceIDs:
trace = self.traces[traceID] trace = self.traces[traceID]
coords = trace['coords'] coords = trace['coords']
source = int(trace['source']) - sourceIDs[0] + 1 source = int(trace['source']) - sourceIDs[0] + 1
recfile.writelines('%s %s %s\n'%(float(coords[0]), float(coords[1]), float(coords[2]))) recfile.write('%s %s %s\n'%(float(coords[0]), float(coords[1]), float(coords[2])))
recfile.writelines('%s\n'%trace['paths']) recfile.write('%s\n'%trace['paths'])
recfile.writelines('%s\n'%source) recfile.write('%s\n'%source)
recfile.writelines('%s\n'%trace['path']) recfile.write('%s\n'%trace['path'])
def getTraceIDs4Sources(self, sourceIDs): def getTraceIDs4Sources(self, sourceIDs):
traceIDs = [] traceIDs = []
@ -208,79 +320,139 @@ class Tomo3d(object):
return arrivals return arrivals
def readFrechet(self, procID):
directory = self.getProcDir(procID)
frechfile = open(directory + '/frechet.dat', 'r')
sourceIDs = self.srcIDs4Kernel(procID)
frechet = {}
for sourceID in sourceIDs:
traceIDs = self.getTraceIDs4Source(sourceID)
for traceID in traceIDs:
line = frechfile.readline().split()
if line != []:
# recID and srcID for the individual processor will not be needed
PDEV = []
recID_proc, srcID_proc, ray, normal, NPDEV = line
for i in range(int(NPDEV)):
PDEV.append(frechfile.readline())
frechet[traceID] = {'sourceID': sourceID,
'raypath': ray,
'normal': normal,
'NPDEV': NPDEV,
'PDEV': PDEV
}
return frechet
def mergeArrivals(self): def mergeArrivals(self):
arrivalsOut = open(self.cwd + '/arrivals.dat', 'w') arrivalsOut = open(os.path.join(self.cwd, self.ttim), 'w')
print('Merging arrivals.dat...') print('Merging arrivals.dat...')
for procID in range(1, self.nproc + 1): for procID in range(1, self.nproc + 1):
arrivals = self.readArrivals(procID) arrivals = self.readArrivals(procID)
for line in arrivals: for line in arrivals:
arrivalsOut.writelines('%6s %6s %6s %6s %15s %5s %5s\n'%tuple(line)) arrivalsOut.write('%6s %6s %6s %6s %15s %5s %5s\n'%tuple(line))
def mergeFrechet(self): # def mergeFrechet(self):
print('Merging frechet.dat...') # print('Merging frechet.dat...')
frechetOut = open(self.cwd + '/frechet.dat', 'w') # frechetOut = open(self.cwd + '/frechet.dat', 'w')
for procID in range(1, self.nproc + 1): # for procID in range(1, self.nproc + 1):
frechet = self.readFrechet(procID) # frechet = self.readFrechet(procID)
traceIDs = frechet.keys() # traceIDs = frechet.keys()
traceIDs.sort() # traceIDs.sort()
# for traceID in traceIDs:
# frech = frechet[traceID]
# frechetOut.write('%6s %6s %6s %6s %6s\n'%
# (traceID,
# frech['sourceID'],
# frech['raypath'],
# frech['normal'],
# frech['NPDEV']))
# for pdev in frech['PDEV']:
# frechetOut.writelines(pdev)
def readRays(self, procID):
directory = self.getProcDir(procID)
raysfile = open(directory + '/rays.dat', 'r')
sourceIDs = self.srcIDs4Kernel(procID)
rays = {}
for sourceID in sourceIDs:
traceIDs = self.getTraceIDs4Source(sourceID)
for traceID in traceIDs: for traceID in traceIDs:
frech = frechet[traceID] line1 = raysfile.readline().split()
frechetOut.writelines('%6s %6s %6s %6s %6s\n'% if line1 != []:
(traceID, # recID and srcID for the individual processor will not be needed
frech['sourceID'], recID_proc, srcID_proc, ray, normal, nsec = line1
frech['raypath'], raysecs = {}
frech['normal'],
frech['NPDEV'])) for sec in range(int(nsec)):
for pdev in frech['PDEV']: line2 = raysfile.readline.split()
frechetOut.writelines(pdev) npoints, region, diff, head = line2
raypoints = []
for j in range(int(npoints)):
raypoints.append(raysfile.readline() + '\n')
raysecs[sec] = {'npoints': npoints,
'region': region,
'diff': diff,
'head': head,
'raypoints': raypoints
}
rays[traceID] = {'sourceID': sourceID,
'raypath': ray,
'normal': normal,
'nsec': nsec,
'raysections': raysecs
}
return rays
def mergeRays(self): def mergeRays(self):
print('Merging rays.dat...') print('Merging rays.dat...')
filenames = []
for procID in range(1, self.nproc + 1):
directory = self.getProcDir(procID)
filenames.append(directory + '/rays.dat')
with open(self.cwd + 'rays.dat', 'w') as outfile: with open(self.cwd + 'rays.dat', 'w') as outfile:
for fname in filenames: for procID in range(1, self.nproc + 1):
with open(fname) as infile: rays = self.readRays(procID)
for line in infile: for traceID in rays:
outfile.write(line) ray = rays[traceID]
outfile.write('%6s %6s %6s %6s %6s'%(traceID,
ray['sourceID'],
ray['raypath'],
ray['normal'],
ray['nsec']))
for sec in range(int(ray['nsec'])):
raysec = ray['raysections'][sec]
outfile.write('%6s %6s %6s %6s'%(raysec['npoints'],
raysec['region'],
raysec['diff'],
raysec['head']))
outfile.writelines(raysec['raypoints'])
# def readFrechet(self, procID):
# directory = self.getProcDir(procID)
# frechfile = open(directory + '/frechet.dat', 'r')
# sourceIDs = self.srcIDs4Kernel(procID)
# frechet = {}
# for sourceID in sourceIDs:
# traceIDs = self.getTraceIDs4Source(sourceID)
# for traceID in traceIDs:
# line = frechfile.readline().split()
# if line != []:
# # recID and srcID for the individual processor will not be needed
# PDEV = []
# recID_proc, srcID_proc, ray, normal, NPDEV = line
# for i in range(int(NPDEV)):
# PDEV.append(frechfile.readline())
# frechet[traceID] = {'sourceID': sourceID,
# 'raypath': ray,
# 'normal': normal,
# 'NPDEV': NPDEV,
# 'PDEV': PDEV
# }
# return frechet
def mergeFrechet(self):
print('Merging frechet.dat...')
with open(self.cwd + '/frechet.dat', 'w') as outfile:
for procID in range(1, self.nproc + 1):
filename = self.getProcDir(procID) + '/frechet.dat'
with open(filename) as infile:
for sourceID in self.srcIDs4Kernel(procID):
for traceID in self.getTraceIDs4Source(sourceID):
recID_proc, srcID_proc, ray, normal, NPDEV = infile.readline().split()
outfile.write('%6s %6s %6s %6s %6s\n'%(traceID, sourceID, ray, normal, NPDEV))
for index in range(int(NPDEV)):
outfile.write(infile.readline())
def getProcDir(self, procID): def getProcDir(self, procID):
return self.cwd + self.folder + str(procID) return os.path.join(self.cwd, self.folder) + str(procID)
def mergeOutput(self): def mergeOutput(self):
self.mergeArrivals() self.mergeArrivals()
self.mergeFrechet() self.mergeFrechet()
self.mergeRays() self.mergeRays()
def vgrids2VTK(inputfile='vgrids.in', outputfile='vgrids.vtk', absOrRel='abs', inputfileref='vgridsref.in'): def vgrids2VTK(inputfile='vgrids.in', outputfile='vgrids.vtk', absOrRel='abs', inputfileref='vgridsref.in'):
@ -314,23 +486,23 @@ def vgrids2VTK(inputfile='vgrids.in', outputfile='vgrids.vtk', absOrRel='abs', i
# write header # write header
print("Writing header for VTK file...") print("Writing header for VTK file...")
outfile.writelines('# vtk DataFile Version 3.1\n') outfile.write('# vtk DataFile Version 3.1\n')
outfile.writelines('Velocity on FMTOMO vgrids.in points\n') outfile.write('Velocity on FMTOMO vgrids.in points\n')
outfile.writelines('ASCII\n') outfile.write('ASCII\n')
outfile.writelines('DATASET STRUCTURED_POINTS\n') outfile.write('DATASET STRUCTURED_POINTS\n')
outfile.writelines('DIMENSIONS %d %d %d\n' % (nX, nY, nZ)) outfile.write('DIMENSIONS %d %d %d\n' % (nX, nY, nZ))
outfile.writelines('ORIGIN %f %f %f\n' % (sX, sY, sZ)) outfile.write('ORIGIN %f %f %f\n' % (sX, sY, sZ))
outfile.writelines('SPACING %f %f %f\n' % (dX, dY, dZ)) outfile.write('SPACING %f %f %f\n' % (dX, dY, dZ))
outfile.writelines('POINT_DATA %15d\n' % (nPoints)) outfile.write('POINT_DATA %15d\n' % (nPoints))
if absOrRel == 'abs': if absOrRel == 'abs':
outfile.writelines('SCALARS velocity float %d\n' %(1)) outfile.write('SCALARS velocity float %d\n' %(1))
if absOrRel == 'relDepth': if absOrRel == 'relDepth':
outfile.writelines('SCALARS velocity2depthMean float %d\n' %(1)) outfile.write('SCALARS velocity2depthMean float %d\n' %(1))
elif absOrRel == 'rel': elif absOrRel == 'rel':
outfile.writelines('SCALARS velChangePercent float %d\n' % (1)) outfile.write('SCALARS velChangePercent float %d\n' % (1))
outfile.writelines('LOOKUP_TABLE default\n') outfile.write('LOOKUP_TABLE default\n')
pointsPerR = nTheta * nPhi pointsPerR = nTheta * nPhi
@ -338,7 +510,7 @@ def vgrids2VTK(inputfile='vgrids.in', outputfile='vgrids.vtk', absOrRel='abs', i
if absOrRel == 'abs': if absOrRel == 'abs':
print("Writing velocity values to VTK file...") print("Writing velocity values to VTK file...")
for velocity in vel: for velocity in vel:
outfile.writelines('%10f\n' %velocity) outfile.write('%10f\n' %velocity)
elif absOrRel == 'relDepth': elif absOrRel == 'relDepth':
print("Writing velocity values to VTK file relative to mean of each depth...") print("Writing velocity values to VTK file relative to mean of each depth...")
index = 0; count = 0 index = 0; count = 0
@ -350,7 +522,7 @@ def vgrids2VTK(inputfile='vgrids.in', outputfile='vgrids.vtk', absOrRel='abs', i
velmean = np.mean(veldepth) velmean = np.mean(veldepth)
#print velmean, count, count/pointsPerR #print velmean, count, count/pointsPerR
for vel in veldepth: for vel in veldepth:
outfile.writelines('%10f\n' %(vel - velmean)) outfile.write('%10f\n' %(vel - velmean))
veldepth = [] veldepth = []
elif absOrRel == 'rel': elif absOrRel == 'rel':
nref, dref, sref, velref = _readVgrid(inputfileref) nref, dref, sref, velref = _readVgrid(inputfileref)
@ -372,7 +544,7 @@ def vgrids2VTK(inputfile='vgrids.in', outputfile='vgrids.vtk', absOrRel='abs', i
return return
print("Writing velocity values to VTK file...") print("Writing velocity values to VTK file...")
for velocity in velrel: for velocity in velrel:
outfile.writelines('%10f\n' % velocity) outfile.write('%10f\n' % velocity)
print('Pertubations: min: %s %%, max: %s %%' % (min(velrel), max(velrel))) print('Pertubations: min: %s %%, max: %s %%' % (min(velrel), max(velrel)))
outfile.close() outfile.close()
@ -430,29 +602,29 @@ def rays2VTK(fnin, fdirout='./vtk_files/', nthPoint=50):
# write header # write header
# print("Writing header for VTK file...") # print("Writing header for VTK file...")
print("Writing shot %d to file %s" % (shotnumber, fnameout)) print("Writing shot %d to file %s" % (shotnumber, fnameout))
outfile.writelines('# vtk DataFile Version 3.1\n') outfile.write('# vtk DataFile Version 3.1\n')
outfile.writelines('FMTOMO rays\n') outfile.write('FMTOMO rays\n')
outfile.writelines('ASCII\n') outfile.write('ASCII\n')
outfile.writelines('DATASET POLYDATA\n') outfile.write('DATASET POLYDATA\n')
outfile.writelines('POINTS %15d float\n' % (nPoints)) outfile.write('POINTS %15d float\n' % (nPoints))
# write coordinates # write coordinates
# print("Writing coordinates to VTK file...") # print("Writing coordinates to VTK file...")
for raynumber in rays[shotnumber].keys(): for raynumber in rays[shotnumber].keys():
for raypoint in rays[shotnumber][raynumber]: for raypoint in rays[shotnumber][raynumber]:
outfile.writelines('%10f %10f %10f \n' % (raypoint[0], raypoint[1], raypoint[2])) outfile.write('%10f %10f %10f \n' % (raypoint[0], raypoint[1], raypoint[2]))
outfile.writelines('LINES %15d %15d\n' % (len(rays[shotnumber]), len(rays[shotnumber]) + nPoints)) outfile.write('LINES %15d %15d\n' % (len(rays[shotnumber]), len(rays[shotnumber]) + nPoints))
# write indices # write indices
# print("Writing indices to VTK file...") # print("Writing indices to VTK file...")
count = 0 count = 0
for raynumber in rays[shotnumber].keys(): for raynumber in rays[shotnumber].keys():
outfile.writelines('%d ' % (len(rays[shotnumber][raynumber]))) outfile.write('%d ' % (len(rays[shotnumber][raynumber])))
for index in range(len(rays[shotnumber][raynumber])): for index in range(len(rays[shotnumber][raynumber])):
outfile.writelines('%d ' % (count)) outfile.write('%d ' % (count))
count += 1 count += 1
outfile.writelines('\n') outfile.write('\n')
def _readVgrid(filename): def _readVgrid(filename):
@ -594,10 +766,10 @@ def addCheckerboard(spacing=10., pertubation=0.1, inputfile='vgrids.in',
nPoints = nR * nTheta * nPhi nPoints = nR * nTheta * nPhi
# write header for velocity grid file (in RADIANS) # write header for velocity grid file (in RADIANS)
outfile.writelines('%10s %10s \n' % (1, 1)) outfile.write('%10s %10s \n' % (1, 1))
outfile.writelines('%10s %10s %10s\n' % (nR, nTheta, nPhi)) outfile.write('%10s %10s %10s\n' % (nR, nTheta, nPhi))
outfile.writelines('%10s %10s %10s\n' % (dR, np.deg2rad(dTheta), np.deg2rad(dPhi))) outfile.write('%10s %10s %10s\n' % (dR, np.deg2rad(dTheta), np.deg2rad(dPhi)))
outfile.writelines('%10s %10s %10s\n' % (sR, np.deg2rad(sTheta), np.deg2rad(sPhi))) outfile.write('%10s %10s %10s\n' % (sR, np.deg2rad(sTheta), np.deg2rad(sPhi)))
spacR = correctSpacing(spacing, dR, '[meter], R') spacR = correctSpacing(spacing, dR, '[meter], R')
spacTheta = correctSpacing(_getAngle(spacing), dTheta, '[degree], Theta') spacTheta = correctSpacing(_getAngle(spacing), dTheta, '[degree], Theta')
@ -642,7 +814,7 @@ def addCheckerboard(spacing=10., pertubation=0.1, inputfile='vgrids.in',
evenOdd = evenOddR * evenOddT * evenOddP * ampFactor evenOdd = evenOddR * evenOddT * evenOddP * ampFactor
velocity += evenOdd * pertubation * velocity velocity += evenOdd * pertubation * velocity
outfile.writelines('%10s %10s\n' % (velocity, decm)) outfile.write('%10s %10s\n' % (velocity, decm))
count += 1 count += 1
progress = float(count) / float(nPoints) * 100 progress = float(count) / float(nPoints) * 100
@ -697,10 +869,10 @@ def addBox(x=(None, None), y=(None, None), z=(None, None),
nPoints = nR * nTheta * nPhi nPoints = nR * nTheta * nPhi
# write header for velocity grid file (in RADIANS) # write header for velocity grid file (in RADIANS)
outfile.writelines('%10s %10s \n' % (1, 1)) outfile.write('%10s %10s \n' % (1, 1))
outfile.writelines('%10s %10s %10s\n' % (nR, nTheta, nPhi)) outfile.write('%10s %10s %10s\n' % (nR, nTheta, nPhi))
outfile.writelines('%10s %10s %10s\n' % (dR, np.deg2rad(dTheta), np.deg2rad(dPhi))) outfile.write('%10s %10s %10s\n' % (dR, np.deg2rad(dTheta), np.deg2rad(dPhi)))
outfile.writelines('%10s %10s %10s\n' % (sR, np.deg2rad(sTheta), np.deg2rad(sPhi))) outfile.write('%10s %10s %10s\n' % (sR, np.deg2rad(sTheta), np.deg2rad(sPhi)))
count = 0 count = 0
for radius in rGrid: for radius in rGrid:
@ -722,7 +894,7 @@ def addBox(x=(None, None), y=(None, None), z=(None, None),
if rFlag * thetaFlag * phiFlag is not 0: if rFlag * thetaFlag * phiFlag is not 0:
velocity = boxvelocity velocity = boxvelocity
outfile.writelines('%10s %10s\n' % (velocity, decm)) outfile.write('%10s %10s\n' % (velocity, decm))
count += 1 count += 1
progress = float(count) / float(nPoints) * 100 progress = float(count) / float(nPoints) * 100

View File

@ -134,7 +134,8 @@ class SeisArray(object):
if traceID2 < traceID1: if traceID2 < traceID1:
direction = -1 direction = -1
return direction return direction
print "Error: Same Value for traceID1 = %s and traceID2 = %s" % (traceID1, traceID2) err_msg = "Same Value for traceID1 = %s and traceID2 = %s" % (traceID1, traceID2)
raise RuntimeError(err_msg)
def _checkCoordDirection(self, traceID1, traceID2, coordinate): def _checkCoordDirection(self, traceID1, traceID2, coordinate):
''' '''
@ -146,7 +147,8 @@ class SeisArray(object):
if self._getReceiverValue(traceID1, coordinate) > self._getReceiverValue(traceID2, coordinate): if self._getReceiverValue(traceID1, coordinate) > self._getReceiverValue(traceID2, coordinate):
direction = -1 direction = -1
return direction return direction
print "Error: Same Value for traceID1 = %s and traceID2 = %s" % (traceID1, traceID2) err_msg = "Same Value for traceID1 = %s and traceID2 = %s" % (traceID1, traceID2)
raise RuntimeError(err_msg)
def _interpolateMeanDistances(self, traceID1, traceID2, coordinate): def _interpolateMeanDistances(self, traceID1, traceID2, coordinate):
''' '''
@ -455,7 +457,7 @@ class SeisArray(object):
recx, recy, recz = self.getReceiverLists() recx, recy, recz = self.getReceiverLists()
nsrc = len(self.getSourceLocations()) nsrc = len(self.getSourceLocations())
outfile.writelines('%s\n' % (len(zip(recx, recy, recz)) * nsrc)) outfile.write('%s\n' % (len(zip(recx, recy, recz)) * nsrc))
for index in range(nsrc): for index in range(nsrc):
for point in zip(recx, recy, recz): for point in zip(recx, recy, recz):
@ -463,10 +465,10 @@ class SeisArray(object):
rad = - rz rad = - rz
lat = self._getAngle(ry) lat = self._getAngle(ry)
lon = self._getAngle(rx) lon = self._getAngle(rx)
outfile.writelines('%15s %15s %15s\n' % (rad, lat, lon)) outfile.write('%15s %15s %15s\n' % (rad, lat, lon))
outfile.writelines('%15s\n' % (1)) outfile.write('%15s\n' % (1))
outfile.writelines('%15s\n' % (index + 1)) outfile.write('%15s\n' % (index + 1))
outfile.writelines('%15s\n' % (1)) outfile.write('%15s\n' % (1))
outfile.close() outfile.close()
@ -506,22 +508,22 @@ class SeisArray(object):
deltaPhi = abs(phiE - phiW) / float((nPhi - 1)) deltaPhi = abs(phiE - phiW) / float((nPhi - 1))
# write header for interfaces grid file (in RADIANS) # write header for interfaces grid file (in RADIANS)
outfile.writelines('%10s\n' % (nInterfaces)) outfile.write('%10s\n' % (nInterfaces))
outfile.writelines('%10s %10s\n' % (nTheta + 2, nPhi + 2)) # +2 cushion nodes outfile.write('%10s %10s\n' % (nTheta + 2, nPhi + 2)) # +2 cushion nodes
outfile.writelines('%10s %10s\n' % (np.deg2rad(deltaTheta), np.deg2rad(deltaPhi))) outfile.write('%10s %10s\n' % (np.deg2rad(deltaTheta), np.deg2rad(deltaPhi)))
outfile.writelines('%10s %10s\n' % (np.deg2rad(thetaS - deltaTheta), np.deg2rad(phiW - deltaPhi))) outfile.write('%10s %10s\n' % (np.deg2rad(thetaS - deltaTheta), np.deg2rad(phiW - deltaPhi)))
interface1 = self.interpolateTopography(nTheta, nPhi, thetaSN, phiWE, method=method) interface1 = self.interpolateTopography(nTheta, nPhi, thetaSN, phiWE, method=method)
interface2 = self.interpolateOnRegularGrid(nTheta, nPhi, thetaSN, phiWE, -depthmax, method=method) interface2 = self.interpolateOnRegularGrid(nTheta, nPhi, thetaSN, phiWE, -depthmax, method=method)
for point in interface1: for point in interface1:
z = point[2] z = point[2]
outfile.writelines('%10s\n' % (z + R)) outfile.write('%10s\n' % (z + R))
outfile.writelines('\n') outfile.write('\n')
for point in interface2: for point in interface2:
z = point[2] z = point[2]
outfile.writelines('%10s\n' % (z + R)) outfile.write('%10s\n' % (z + R))
outfile.close() outfile.close()
@ -596,10 +598,10 @@ class SeisArray(object):
deltaPhi = abs(phiE - phiW) / float(nPhi - 1) deltaPhi = abs(phiE - phiW) / float(nPhi - 1)
deltaR = abs(rbot - rtop) / float(nR - 1) deltaR = abs(rbot - rtop) / float(nR - 1)
outfile.writelines('%10s %10s %10s\n' % (nR, nTheta, nPhi)) outfile.write('%10s %10s %10s\n' % (nR, nTheta, nPhi))
outfile.writelines('%10s %10s %10s\n' % (deltaR, deltaTheta, deltaPhi)) outfile.write('%10s %10s %10s\n' % (deltaR, deltaTheta, deltaPhi))
outfile.writelines('%10s %10s %10s\n' % (rtop, thetaS, phiW)) outfile.write('%10s %10s %10s\n' % (rtop, thetaS, phiW))
outfile.writelines('%10s %10s\n' % refinement) outfile.write('%10s %10s\n' % refinement)
outfile.close() outfile.close()
@ -708,10 +710,10 @@ class SeisArray(object):
print("Total number of grid nodes: %s" % nTotal) print("Total number of grid nodes: %s" % nTotal)
# write header for velocity grid file (in RADIANS) # write header for velocity grid file (in RADIANS)
outfile.writelines('%10s %10s \n' % (1, 1)) outfile.write('%10s %10s \n' % (1, 1))
outfile.writelines('%10s %10s %10s\n' % (nR + 2, nTheta + 2, nPhi + 2)) outfile.write('%10s %10s %10s\n' % (nR + 2, nTheta + 2, nPhi + 2))
outfile.writelines('%10s %10s %10s\n' % (deltaR, np.deg2rad(deltaTheta), np.deg2rad(deltaPhi))) outfile.write('%10s %10s %10s\n' % (deltaR, np.deg2rad(deltaTheta), np.deg2rad(deltaPhi)))
outfile.writelines( outfile.write(
'%10s %10s %10s\n' % (rbot - deltaR, np.deg2rad(thetaS - deltaTheta), np.deg2rad(phiW - deltaPhi))) '%10s %10s %10s\n' % (rbot - deltaR, np.deg2rad(thetaS - deltaTheta), np.deg2rad(phiW - deltaPhi)))
surface = self.interpolateTopography(nTheta, nPhi, thetaSN, phiWE, method=method) surface = self.interpolateTopography(nTheta, nPhi, thetaSN, phiWE, method=method)
@ -746,15 +748,16 @@ class SeisArray(object):
vbot[index] - vtop[index]) + vtop[index] vbot[index] - vtop[index]) + vtop[index]
break break
if not (ztop[index]) >= depth > (zbot[index]): if not (ztop[index]) >= depth > (zbot[index]):
print( err_msg = 'ERROR in grid inputfile, could not find velocity'
'ERROR in grid inputfile, could not find velocity for a z-value of %s in the inputfile' % ( 'for a z-value of %s in the inputfile' % (depth - topo)
depth - topo)) raise ValueError(err_msg)
return
count += 1 count += 1
if vel < 0: if not vel >= 0:
print( err_msg = 'vel < 0; z, topo, zbot, vbot, vtop:',
'ERROR, vel <0; z, topo, zbot, vbot, vtop:', depth, topo, zbot[index], vbot[index], vtop[index]) depth, topo, zbot[index], vbot[index], vtop[index]
outfile.writelines('%10s %10s\n' % (vel, decm)) raise ValueError(err_msg)
outfile.write('%10s %10s\n' % (vel, decm))
progress = float(count) / float(nTotal) * 100 progress = float(count) / float(nTotal) * 100
self._update_progress(progress) self._update_progress(progress)
@ -775,7 +778,7 @@ class SeisArray(object):
for traceID in self.getReceiverCoordinates().keys(): for traceID in self.getReceiverCoordinates().keys():
count += 1 count += 1
x, y, z = self.getReceiverCoordinates()[traceID] x, y, z = self.getReceiverCoordinates()[traceID]
recfile_out.writelines('%5s %15s %15s %15s\n' % (traceID, x, y, z)) recfile_out.write('%5s %15s %15s %15s\n' % (traceID, x, y, z))
print "Exported coordinates for %s traces to file > %s" % (count, filename) print "Exported coordinates for %s traces to file > %s" % (count, filename)
recfile_out.close() recfile_out.close()
@ -895,11 +898,11 @@ class SeisArray(object):
# write header # write header
print("Writing header for VTK file...") print("Writing header for VTK file...")
outfile.writelines('# vtk DataFile Version 3.1\n') outfile.write('# vtk DataFile Version 3.1\n')
outfile.writelines('Surface Points\n') outfile.write('Surface Points\n')
outfile.writelines('ASCII\n') outfile.write('ASCII\n')
outfile.writelines('DATASET POLYDATA\n') outfile.write('DATASET POLYDATA\n')
outfile.writelines('POINTS %15d float\n' % (nPoints)) outfile.write('POINTS %15d float\n' % (nPoints))
# write coordinates # write coordinates
print("Writing coordinates to VTK file...") print("Writing coordinates to VTK file...")
@ -908,23 +911,23 @@ class SeisArray(object):
y = point[1] y = point[1]
z = point[2] z = point[2]
outfile.writelines('%10f %10f %10f \n' % (x, y, z)) outfile.write('%10f %10f %10f \n' % (x, y, z))
outfile.writelines('VERTICES %15d %15d\n' % (nPoints, 2 * nPoints)) outfile.write('VERTICES %15d %15d\n' % (nPoints, 2 * nPoints))
# write indices # write indices
print("Writing indices to VTK file...") print("Writing indices to VTK file...")
for index in range(nPoints): for index in range(nPoints):
outfile.writelines('%10d %10d\n' % (1, index)) outfile.write('%10d %10d\n' % (1, index))
# outfile.writelines('POINT_DATA %15d\n' %(nPoints)) # outfile.write('POINT_DATA %15d\n' %(nPoints))
# outfile.writelines('SCALARS traceIDs int %d\n' %(1)) # outfile.write('SCALARS traceIDs int %d\n' %(1))
# outfile.writelines('LOOKUP_TABLE default\n') # outfile.write('LOOKUP_TABLE default\n')
# # write traceIDs # # write traceIDs
# print("Writing traceIDs to VTK file...") # print("Writing traceIDs to VTK file...")
# for traceID in traceIDs: # for traceID in traceIDs:
# outfile.writelines('%10d\n' %traceID) # outfile.write('%10d\n' %traceID)
outfile.close() outfile.close()
print("Wrote %d points to file: %s" % (nPoints, filename)) print("Wrote %d points to file: %s" % (nPoints, filename))
@ -944,11 +947,11 @@ class SeisArray(object):
# write header # write header
print("Writing header for VTK file...") print("Writing header for VTK file...")
outfile.writelines('# vtk DataFile Version 3.1\n') outfile.write('# vtk DataFile Version 3.1\n')
outfile.writelines('Receivers with traceIDs\n') outfile.write('Receivers with traceIDs\n')
outfile.writelines('ASCII\n') outfile.write('ASCII\n')
outfile.writelines('DATASET POLYDATA\n') outfile.write('DATASET POLYDATA\n')
outfile.writelines('POINTS %15d float\n' % (nPoints)) outfile.write('POINTS %15d float\n' % (nPoints))
# write coordinates # write coordinates
print("Writing coordinates to VTK file...") print("Writing coordinates to VTK file...")
@ -957,23 +960,23 @@ class SeisArray(object):
y = self._getYreceiver(traceID) y = self._getYreceiver(traceID)
z = self._getZreceiver(traceID) z = self._getZreceiver(traceID)
outfile.writelines('%10f %10f %10f \n' % (x, y, z)) outfile.write('%10f %10f %10f \n' % (x, y, z))
outfile.writelines('VERTICES %15d %15d\n' % (nPoints, 2 * nPoints)) outfile.write('VERTICES %15d %15d\n' % (nPoints, 2 * nPoints))
# write indices # write indices
print("Writing indices to VTK file...") print("Writing indices to VTK file...")
for index in range(nPoints): for index in range(nPoints):
outfile.writelines('%10d %10d\n' % (1, index)) outfile.write('%10d %10d\n' % (1, index))
outfile.writelines('POINT_DATA %15d\n' % (nPoints)) outfile.write('POINT_DATA %15d\n' % (nPoints))
outfile.writelines('SCALARS traceIDs int %d\n' % (1)) outfile.write('SCALARS traceIDs int %d\n' % (1))
outfile.writelines('LOOKUP_TABLE default\n') outfile.write('LOOKUP_TABLE default\n')
# write traceIDs # write traceIDs
print("Writing traceIDs to VTK file...") print("Writing traceIDs to VTK file...")
for traceID in traceIDs: for traceID in traceIDs:
outfile.writelines('%10d\n' % traceID) outfile.write('%10d\n' % traceID)
outfile.close() outfile.close()
print("Wrote %d receiver for to file: %s" % (nPoints, filename)) print("Wrote %d receiver for to file: %s" % (nPoints, filename))
@ -993,11 +996,11 @@ class SeisArray(object):
# write header # write header
print("Writing header for VTK file...") print("Writing header for VTK file...")
outfile.writelines('# vtk DataFile Version 3.1\n') outfile.write('# vtk DataFile Version 3.1\n')
outfile.writelines('Shots with shotnumbers\n') outfile.write('Shots with shotnumbers\n')
outfile.writelines('ASCII\n') outfile.write('ASCII\n')
outfile.writelines('DATASET POLYDATA\n') outfile.write('DATASET POLYDATA\n')
outfile.writelines('POINTS %15d float\n' % (nPoints)) outfile.write('POINTS %15d float\n' % (nPoints))
# write coordinates # write coordinates
print("Writing coordinates to VTK file...") print("Writing coordinates to VTK file...")
@ -1006,23 +1009,23 @@ class SeisArray(object):
y = self._getYshot(shotnumber) y = self._getYshot(shotnumber)
z = self._getZshot(shotnumber) z = self._getZshot(shotnumber)
outfile.writelines('%10f %10f %10f \n' % (x, y, z)) outfile.write('%10f %10f %10f \n' % (x, y, z))
outfile.writelines('VERTICES %15d %15d\n' % (nPoints, 2 * nPoints)) outfile.write('VERTICES %15d %15d\n' % (nPoints, 2 * nPoints))
# write indices # write indices
print("Writing indices to VTK file...") print("Writing indices to VTK file...")
for index in range(nPoints): for index in range(nPoints):
outfile.writelines('%10d %10d\n' % (1, index)) outfile.write('%10d %10d\n' % (1, index))
outfile.writelines('POINT_DATA %15d\n' % (nPoints)) outfile.write('POINT_DATA %15d\n' % (nPoints))
outfile.writelines('SCALARS shotnumbers int %d\n' % (1)) outfile.write('SCALARS shotnumbers int %d\n' % (1))
outfile.writelines('LOOKUP_TABLE default\n') outfile.write('LOOKUP_TABLE default\n')
# write shotnumber # write shotnumber
print("Writing shotnumbers to VTK file...") print("Writing shotnumbers to VTK file...")
for shotnumber in shotnumbers: for shotnumber in shotnumbers:
outfile.writelines('%10d\n' % shotnumber) outfile.write('%10d\n' % shotnumber)
outfile.close() outfile.close()
print("Wrote %d sources to file: %s" % (nPoints, filename)) print("Wrote %d sources to file: %s" % (nPoints, filename))