use OptionParse to parse the command-line options, add help information,

add the output of material.config file.
This commit is contained in:
Haiming Zhang 2015-03-05 19:01:31 +00:00
parent a4ac2eae96
commit 4579a696e8
1 changed files with 79 additions and 21 deletions

View File

@ -1,8 +1,13 @@
#!/usr/bin/env python
import os,sys,math,re,random
random.seed()
from optparse import OptionParser
import damask
import os,sys,math,re,random,string
scriptID = string.replace('$Id$','\n','\\n')
scriptName = scriptID.split()[1]
random.seed()
# --- helper functions ---
@ -195,17 +200,50 @@ def TothVanHoutteSTAT (ODF,nSamples):
return orientations, reconstructedODF
# --------------------------------------------------------------------
# MAIN
# --------------------------------------------------------------------
parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [file[s]]', description = """
Transform linear binned data into Euler angles.
""", version=string.replace(scriptID,'\n','\\n')
)
parser.add_option('-f', '--file', dest='file', type='string', metavar = 'string', \
help='file name, the input file is generated by the script "OIMlinear2linearODF.py"')
parser.add_option('-o', '--output', dest='output', type='string', metavar = 'string', \
help='the prefix of output file name.')
parser.add_option('-n', '--number', dest='number', type='int', metavar = 'int', \
help='the number of orientations needed to be generated, the default is [%default]')
parser.add_option('-a','--algorithm', dest='algorithm', type='string', metavar = 'string', \
help='''The algorithm adopted, three algorithms are provided,
that is:
[IA]: direct inversion,
[STAT]: Van Houtte,
[MC]: Monte Carlo.
the default is [%default].''')
parser.add_option('-p','--phase', dest='phase', type='int', metavar = 'int',
help='phase index to be used [%default]')
parser.add_option('--crystallite', dest='crystallite', type='int', metavar = 'int',
help='crystallite index to be used [%default]')
parser.add_option('-c', '--configuration', dest='config', action='store_true',
help='output material configuration [%default]')
parser.set_defaults(number = 500)
parser.set_defaults(output = 'texture')
parser.set_defaults(algorithm = 'IA')
parser.set_defaults(phase = 1)
parser.set_defaults(crystallite = 1)
options = parser.parse_args()[0]
# check usage
try:
nameBinnedODF = sys.argv[1]
nSamples = int(float(sys.argv[2]))
except:
print "\nusage:",os.path.basename(sys.argv[0]),"nameLinearODF nSamples [nameSampledODF]\n"
sys.exit(1);
if not os.path.exists(options.file):
parser.error('binnedODF file does not exist'); sys.exit()
methods = ['IA','STAT']
nameBinnedODF = options.file
nameSampledODF = options.output
nSamples = options.number
methods = [options.algorithm]
#open binned ODF
try:
@ -216,14 +254,15 @@ except:
# process header info
ODF = {}
ODF['limits'] = [math.radians(float(limit)) for limit in fileBinnedODF.readline().split()]
ODF['deltas'] = [math.radians(float(delta)) for delta in fileBinnedODF.readline().split()]
ODF['intervals'] = [int(round(limit/delta)) for limit,delta in zip(ODF['limits'],ODF['deltas'])]
ODF['limits'] = [math.radians(float(limit)) for limit in fileBinnedODF.readline().split()[0:3]]
ODF['deltas'] = [math.radians(float(delta)) for delta in fileBinnedODF.readline().split()[0:3]]
ODF['intervals'] = [int(interval) for interval in fileBinnedODF.readline().split()[0:3]]
#ODF['intervals'] = [int(round(limit/delta)) for limit,delta in zip(ODF['limits'],ODF['deltas'])]
nBins = ODF['intervals'][0]*ODF['intervals'][1]*ODF['intervals'][2]
print 'Limit:', [math.degrees(limit) for limit in ODF['limits']]
print 'Delta:', [math.degrees(delta) for delta in ODF['deltas']]
print 'Interval:', ODF['intervals']
print 'Limit: ', [math.degrees(limit) for limit in ODF['limits']]
print 'Delta: ', [math.degrees(delta) for delta in ODF['deltas']]
print 'Interval: ', ODF['intervals']
centering = fileBinnedODF.readline()
if re.search('cell',centering.lower()):
@ -304,12 +343,6 @@ print 'nNonZero correlation slope:\t', [(ODF['nNonZero']*mutualProd[method]-indi
print 'nNonZero correlation confidence:\t', [(mutualProd[method]-indivSum['orig']*indivSum[method]/ODF['nNonZero'])/\
(ODF['nNonZero']*math.sqrt((indivSquaredSum['orig']/ODF['nNonZero']-(indivSum['orig']/ODF['nNonZero'])**2)*(indivSquaredSum[method]/ODF['nNonZero']-(indivSum[method]/ODF['nNonZero'])**2))) for method in methods]
# write result
try:
nameSampledODF = sys.argv[3]
except:
sys.exit(0) # that's it folks
for method in methods:
if method == 'IA' and nSamples < ODF['nNonZero']:
strOpt = '(%i)'%ODF['nNonZero']
@ -329,3 +362,28 @@ for method in methods:
fileRegressionODF.close()
except:
print 'unable to write RegressionODF:', nameSampledODF+'.'+method+'regression_'+str(nSamples)+strOpt
if options.config:
try:
fileConfig = open(nameSampledODF+'.'+method+str(nSamples)+'.config', 'w') # write config file
formatwidth = 1
fileConfig.write('#-------------------#')
fileConfig.write('\n<microstructure>\n')
fileConfig.write('#-------------------#\n')
for i,ID in enumerate(xrange(nSamples)):
fileConfig.write('[Grain%s]\n'%(str(ID).zfill(formatwidth)) + \
'crystallite %i\n'%options.crystallite + \
'(constituent) phase %i texture %s fraction 1.0\n'%(options.phase,str(ID).rjust(formatwidth)))
fileConfig.write('\n#-------------------#')
fileConfig.write('\n<texture>\n')
fileConfig.write('#-------------------#\n')
for ID in xrange(nSamples):
eulers = re.split(r'[\t]', Orientations[method][ID].strip())
fileConfig.write('[Grain%s]\n'%(str(ID).zfill(formatwidth)) + \
'(gauss) phi1 %10.5f Phi %10.5f phi2 %10.6f scatter 0.0 fraction 1.0\n'\
%(float(eulers[0]),float(eulers[1]),float(eulers[2])))
except:
print 'unable to write material .config file:', nameSampledODF+'.'+method+str(nSamples)+'.config'