change reading in to standard loop over files

getting bins now from header information
format of linearODF now standard ASCII table style with header/keyword/label
This commit is contained in:
Martin Diehl 2015-04-20 14:22:45 +00:00
parent f43afa13d8
commit 4581e22a0f
1 changed files with 71 additions and 72 deletions

View File

@ -3,6 +3,7 @@
import os,string,sys,re
from optparse import OptionParser
import numpy as np
import damask
scriptID = string.replace('$Id$','\n','\\n')
@ -17,90 +18,88 @@ sampleSym = { 'Orthotropic' : (90,90,90),
# --------------------------------------------------------------------
parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [file[s]]', description = """
Transform the OIM texture data into linear ODF data, the input file can be generated by software "TSL OIM Analysis",
and the output file can be used by the script: "hybridIA_linODFsampling.py"
""", version=string.replace(scriptID,'\n','\\n')
)
Transform the binned texture data from "TSL OIM Analysis" into linear ODF data,
parser.add_option('-f', '--file', dest='file', type='string', metavar = 'string', \
help='file name')
parser.add_option('-s', '--symmetry', dest='symmetry', choices=sampleSym.keys(),\
""", version = scriptID)
parser.add_option('-s', '--symmetry', dest='symmetry', choices=sampleSym.keys(),
help='Sample symmetry, the default is [%default]')
parser.add_option('--step', dest='step', type='int', nargs=3,
help='''Angle steps of: phi1, Phi, phi2. For example, if the
range of phi1 is 360 degree, and the desired interval
is 5 degree, then the angle step of phi1 is 72, but
we will discard the 72th step, because 360 is
identical with 0, the default depends on the sample
symmetry. ''', \
metavar='int int int')
parser.set_defaults(symmetry = 'Triclinic')
parser.set_defaults(step = None)
options = parser.parse_args()[0]
if options.step is None:
options.step= [sampleSym[options.symmetry][i]/5 for i in xrange(3)]
(options,filenames) = parser.parse_args()
if not os.path.exists(options.file):
parser.error('texture file does not exist'); sys.exit()
#--- setup file handles ---------------------------------------------------------------------------
files = []
if filenames == []:
files.append({'name':'STDIN',
'input':sys.stdin,
'output':sys.stdout,
'croak':sys.stderr,
})
else:
for name in filenames:
if os.path.exists(name):
files.append({'name':name,
'input':open(name),
'output':open(name+'_tmp','w'),
'croak':sys.stdout,
})
inName = options.file
outName = os.path.splitext(inName)[0]+'.linearODF'
nPhi1,nPHI,nPhi2 = options.step
dPhi1,dPHI,dPhi2 = [sampleSym[options.symmetry][i]/float(options.step[i]) for i in xrange(3)]
#--- loop over input files ------------------------------------------------------------------------
for file in files:
file['croak'].write('\033[1m' + scriptName + '\033[0m: ' + (file['name'] if file['name'] != 'STDIN' else '') + '\n')
N = (nPhi1-1)*(nPHI-1)*(nPhi2-1)
while True: # read header (forward and get bin Size)
line = file['input'].readline()
words = line.split()
if len(words)>=3:
if words[1]=='Bin' and words[2]=='Size:': binSize=float(words[3][:-1])
if not line.startswith('#'): break
try:
inFile = open(inName,'r')
content = inFile.readlines()
except:
print 'unable to read:',inName
sys.exit(1)
try:
outFile = open(outName,'w')
except:
print 'unable to write:',outName
sys.exit(1)
delta = [sampleSym[options.symmetry][i]/binSize for i in xrange(3)]
ODF = [[[[None] for k in range(nPhi2)] for j in range(nPHI)] for i in range(nPhi1)]
linear = [None]*N
line = 0
nPhi1,nPHI,nPhi2 = map(int,delta)
dPhi1,dPHI,dPhi2 = [sampleSym[options.symmetry][i]/delta[i] for i in xrange(3)]
while (content[line].startswith('#')): # skip comments at start of file
line += 1
for iPhi1 in range(nPhi1):
for iPHI in range(nPHI):
for iPhi2 in range(nPhi2):
words = content[line].split()
ODF[iPhi1][iPHI][iPhi2] = float(words[3]) # extract intensity (in column 4)
line += 1
for iPhi1 in range(nPhi1-1):
for iPHI in range(nPHI-1):
for iPhi2 in range(nPhi2-1):
linear[iPhi1*(nPHI-1)*(nPhi2-1)+iPHI*(nPhi2-1)+iPhi2] = (\
ODF[iPhi1 ][iPHI ][iPhi2 ] + \
ODF[iPhi1 ][iPHI ][iPhi2+1] + \
ODF[iPhi1 ][iPHI+1][iPhi2 ] + \
ODF[iPhi1 ][iPHI+1][iPhi2+1] + \
ODF[iPhi1+1][iPHI ][iPhi2 ] + \
ODF[iPhi1+1][iPHI ][iPhi2+1] + \
ODF[iPhi1+1][iPHI+1][iPhi2 ] + \
ODF[iPhi1+1][iPHI+1][iPhi2+1] \
) / 8.0
N = (nPhi1-1)*(nPHI-1)*(nPhi2-1)
inFile.close()
outFile.write('%-6i%-6i%-6i #Ranges of phi1, Phi, phi2\n'%sampleSym[options.symmetry])
outFile.write('%-6.2f%-6.2f%-6.2f #Deltas of phi1, Phi, phi2\n'%(dPhi1,dPHI,dPhi2))
#outFile.write('%-6i%-6i%-6i #Angular steps needed to be converted\n'%(nPhi1-1,nPHI-1,nPhi2-1))
outFile.write('cell-centered data\n')
outFile.write('\n')
ODF = [[[[None] for k in range(nPhi2)] for j in range(nPHI)] for i in range(nPhi1)]
linear = [None]*N
for i in range(N):
outFile.write('%g\n'%(linear[i]))
ODF = np.empty([nPhi1,nPHI,nPhi2],'d')
for iPhi1 in range(nPhi1):
for iPHI in range(nPHI):
for iPhi2 in range(nPhi2):
ODF[iPhi1,iPHI,iPhi2] = float(line.split()[3])*0.125 # extract intensity (in column 4) and weight by 1/8 (since we convert from the 8 corners to the center later on)
line = file['input'].readline()
for iPhi1 in range(nPhi1-1):
for iPHI in range(nPHI-1):
for iPhi2 in range(nPhi2-1):
linear[iPhi1*(nPHI-1)*(nPhi2-1)+iPHI*(nPhi2-1)+iPhi2] =\
ODF[iPhi1 ,iPHI ,iPhi2 ] +\
ODF[iPhi1 ,iPHI ,iPhi2+1] +\
ODF[iPhi1 ,iPHI+1,iPhi2 ] +\
ODF[iPhi1 ,iPHI+1,iPhi2+1] +\
ODF[iPhi1+1,iPHI ,iPhi2 ] +\
ODF[iPhi1+1,iPHI ,iPhi2+1] +\
ODF[iPhi1+1,iPHI+1,iPhi2 ] +\
ODF[iPhi1+1,iPHI+1,iPhi2+1]
file['output'].write('4 header\n')
file['output'].write('limit phi1 %-6.2f Phi %-6.2f phi2 %-6.2f\n'%sampleSym[options.symmetry])
file['output'].write('delta phi1 %-6.2f Phi %-6.2f phi2 %-6.2f\n'%(dPhi1,dPHI,dPhi2))
file['output'].write('centration cell-centered\n')
file['output'].write('density\n')
for i in range(N):
file['output'].write('%g\n'%(linear[i]))
outFile.close()
#--- output finalization --------------------------------------------------------------------------
if file['name'] != 'STDIN':
file['output'].close()
os.rename(file['name']+'_tmp',os.path.splitext(file['name'])[0] +'.linearODF')