make the script consistent with other scripts:

1. use new file handles
2. add scriptID and version
This commit is contained in:
Haiming Zhang 2015-04-28 17:47:06 +00:00
parent 906c3f63a1
commit f8e06b488a
1 changed files with 76 additions and 74 deletions

View File

@ -1,8 +1,12 @@
#!/usr/bin/env python #!/usr/bin/env python
import os,sys,math,re import damask
import os,sys,math,re,string
from optparse import OptionParser from optparse import OptionParser
scriptID = string.replace('$Id$','\n','\\n')
scriptName = scriptID.split()[1]
def integerFactorization(i): def integerFactorization(i):
j = int(math.floor(math.sqrt(float(i)))) j = int(math.floor(math.sqrt(float(i))))
@ -21,75 +25,83 @@ def positiveRadians(angle):
def getHeader(sizeX,sizeY,step): def getHeader(sizeX,sizeY,step):
return [ \ return [
'# TEM_PIXperUM 1.000000', \ '# TEM_PIXperUM 1.000000',
'# x-star 0.509548', \ '# x-star 0.509548',
'# y-star 0.795272', \ '# y-star 0.795272',
'# z-star 0.611799', \ '# z-star 0.611799',
'# WorkingDistance 18.000000', \ '# WorkingDistance 18.000000',
'#', \ '#',
'# Phase 1', \ '# Phase 1',
'# MaterialName Al', \ '# MaterialName Al',
'# Formula Fe', \ '# Formula Fe',
'# Info', \ '# Info',
'# Symmetry 43', \ '# Symmetry 43',
'# LatticeConstants 2.870 2.870 2.870 90.000 90.000 90.000', \ '# LatticeConstants 2.870 2.870 2.870 90.000 90.000 90.000',
'# NumberFamilies 4', \ '# NumberFamilies 4',
'# hklFamilies 1 1 0 1 0.000000 1', \ '# hklFamilies 1 1 0 1 0.000000 1',
'# hklFamilies 2 0 0 1 0.000000 1', \ '# hklFamilies 2 0 0 1 0.000000 1',
'# hklFamilies 2 1 1 1 0.000000 1', \ '# hklFamilies 2 1 1 1 0.000000 1',
'# hklFamilies 3 1 0 1 0.000000 1', \ '# hklFamilies 3 1 0 1 0.000000 1',
'# Categories 0 0 0 0 0 ', \ '# Categories 0 0 0 0 0 ',
'#', \ '#',
'# GRID: SquareGrid', \ '# GRID: SquareGrid',
'# XSTEP: ' + str(step), \ '# XSTEP: ' + str(step),
'# YSTEP: ' + str(step), \ '# YSTEP: ' + str(step),
'# NCOLS_ODD: ' + str(sizeX), \ '# NCOLS_ODD: ' + str(sizeX),
'# NCOLS_EVEN: ' + str(sizeX), \ '# NCOLS_EVEN: ' + str(sizeX),
'# NROWS: ' + str(sizeY), \ '# NROWS: ' + str(sizeY),
'#', \ '#',
'# OPERATOR: ODFsammpling', \ '# OPERATOR: ODFsammpling',
'#', \ '#',
'# SAMPLEID: ', \ '# SAMPLEID: ',
'#', \ '#',
'# SCANID: ', \ '# SCANID: ',
'#', \ '#'
] ]
parser = OptionParser(usage='%prog [options] datafile(s)') # --------------------------------------------------------------------
parser.add_option("-c", "--column", type="int",\ # MAIN
dest="column",\ # --------------------------------------------------------------------
parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [file[s]]', description = """
Transform linear binned data into Euler angles.
""", version = scriptID)
parser.add_option("-c", "--column", type="int", dest="column",
help="starting column of Euler triplet") help="starting column of Euler triplet")
parser.add_option("-s", "--skip", type="int",\ parser.add_option("-s", "--skip", type="int", dest="skip",
dest="skip",\
help="skip this many lines of heading info [%default]") help="skip this many lines of heading info [%default]")
parser.set_defaults (column = 1) parser.set_defaults (column = 1)
parser.set_defaults (skip = 0) parser.set_defaults (skip = 1)
(options, files) = parser.parse_args() (options,filenames) = parser.parse_args()
options.column -= 1 options.column -= 1
if files == []: #--- setup file handles ---------------------------------------------------------------------------
parser.error('no input file specified...') files = []
sys.exit(1) if filenames == []:
if options.column < 0: files.append({'name':'STDIN','input':sys.stdin,'output':sys.stdout,'croak':sys.stderr})
parser.error('column needs to be 1,2,...') else:
sys.exit(1) for name in filenames:
if os.path.exists(name):
files.append({'name':name,'input':open(name),'output':open(name+'_tmp','w'),'croak':sys.stdout})
else:
print('No such file or directory: '+name)
while len(files) > 0: #--- loop over input files ------------------------------------------------------------------------
textureFilename = files.pop() for file in files:
baseName = os.path.splitext(textureFilename)[0] file['croak'].write('\033[1m' + scriptName + '\033[0m: ' + (file['name'] if file['name'] != 'STDIN' else '') + '\n')
# open texture file and read content # open texture file and read content
textureFile = open(textureFilename) textureFile = open(file['name'])
content = textureFile.readlines() content = textureFile.readlines()
textureFile.close() textureFile.close()
m = re.match('(\d+)\s+head',content[0],re.I) m = re.match('(\d+)\s+head',content[0],re.I)
if m != None and options.skip == 0: if m != None and options.skip == 0: options.skip = int(m.group(1))+1
options.skip = int(m.group(1))+1
# extract orientation angles # extract orientation angles
angles = [map(positiveRadians,line.split()[options.column:options.column+3]) for line in content[options.skip:]] angles = [map(positiveRadians,line.split()[options.column:options.column+3]) for line in content[options.skip:]]
@ -97,25 +109,15 @@ while len(files) > 0:
nPoints = len(angles) nPoints = len(angles)
sizeY = integerFactorization(nPoints) sizeY = integerFactorization(nPoints)
sizeX = nPoints / sizeY sizeX = nPoints / sizeY
print '%s: %i * %i = %i (== %i)'%(baseName,sizeX,sizeY,sizeX*sizeY,nPoints) file['croak'].write('%s: %i*%i = %i (== %i)\n'%(file['name'],sizeX,sizeY,sizeX*sizeY,nPoints) )
# write ang file # write ang file
for line in getHeader(sizeX,sizeY,1.0):
try: file['output'].write(line + '\n')
for counter,point in enumerate(angles):
# write header file['output'].write(''.join(['%10.5f'%angle for angle in point])+
angFile = open(baseName + '.ang','w') ''.join(['%10.5f'%coord for coord in [counter%sizeX,counter//sizeX]])+
for line in getHeader(sizeX,sizeY,1.0): ' 100.0 1.0 0 1 1.0\n')
angFile.write(line + '\n') if file['name'] != 'STDIN':
file['output'].close()
# write data os.rename(file['name']+'_tmp', os.path.splitext(file['name'])[0]+'.ang' )
counter = 0
for point in angles:
angFile.write(''.join(['%10.5f'%angle for angle in point])+
''.join(['%10.5f'%coord for coord in [counter%sizeX,counter//sizeX]])+
' 100.0 1.0 0 1 1.0\n')
counter += 1
angFile.close()
except:
print 'unable to write',baseName