2013-11-26 00:41:02 +05:30
|
|
|
#!/usr/bin/env python
|
2014-04-02 00:11:14 +05:30
|
|
|
# -*- coding: UTF-8 no BOM -*-
|
2013-11-26 00:41:02 +05:30
|
|
|
|
2014-08-06 20:55:18 +05:30
|
|
|
import os,sys,string,itertools
|
|
|
|
import numpy as np
|
2014-07-25 01:51:18 +05:30
|
|
|
from optparse import OptionParser
|
|
|
|
import damask
|
2013-11-26 00:41:02 +05:30
|
|
|
|
2014-08-06 18:57:09 +05:30
|
|
|
scriptID = string.replace('$Id$','\n','\\n')
|
2014-08-07 00:36:33 +05:30
|
|
|
scriptName = scriptID.split()[1][:-3]
|
2013-11-26 00:41:02 +05:30
|
|
|
|
2014-07-25 01:51:18 +05:30
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# MAIN
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
|
|
|
|
parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [file[s]]', description = """
|
2013-11-26 00:41:02 +05:30
|
|
|
Add quaternion and/or Bunge Euler angle representation of crystal lattice orientation.
|
2014-08-06 18:57:09 +05:30
|
|
|
Orientation is given by quaternion, Euler angles, rotation matrix, or crystal frame coordinates (i.e. component vectors of rotation matrix).
|
2013-11-26 00:41:02 +05:30
|
|
|
|
2014-08-06 18:57:09 +05:30
|
|
|
""", version = scriptID)
|
2013-11-26 00:41:02 +05:30
|
|
|
|
2014-07-25 01:51:18 +05:30
|
|
|
outputChoices = ['quaternion','eulers']
|
2014-09-12 19:44:55 +05:30
|
|
|
parser.add_option('-o', '--output', dest='output', action='extend', metavar='<string LIST>',
|
2014-07-25 01:51:18 +05:30
|
|
|
help = 'output orientation formats (%s)'%(','.join(outputChoices)))
|
2014-09-12 19:44:55 +05:30
|
|
|
parser.add_option('-s', '--symmetry', dest='symmetry', type='choice',
|
2014-07-25 01:51:18 +05:30
|
|
|
choices=damask.Symmetry.lattices[1:], metavar='string',
|
|
|
|
help = 'crystal symmetry (%s) [cubic]'%(', '.join(damask.Symmetry.lattices[1:])))
|
2014-09-12 19:44:55 +05:30
|
|
|
parser.add_option('-r', '--rotation', dest='rotation', type='float', nargs=4, metavar='float float float float',
|
2014-07-25 01:51:18 +05:30
|
|
|
help = 'angle and axis to (pre)rotate orientation')
|
2014-09-12 19:44:55 +05:30
|
|
|
parser.add_option('-e', '--eulers', dest='eulers', metavar='string',
|
2014-07-25 01:51:18 +05:30
|
|
|
help = 'Euler angles label')
|
|
|
|
parser.add_option('-d', '--degrees', dest='degrees', action='store_true',
|
|
|
|
help = 'Euler angles are given in degrees [%default]')
|
2014-09-12 19:44:55 +05:30
|
|
|
parser.add_option('-m', '--matrix', dest='matrix', metavar='string',
|
2014-07-25 01:51:18 +05:30
|
|
|
help = 'orientation matrix label')
|
2014-09-12 19:44:55 +05:30
|
|
|
parser.add_option('-a', dest='a', metavar='string',
|
2014-07-25 01:51:18 +05:30
|
|
|
help = 'crystal frame a vector label')
|
2014-09-12 19:44:55 +05:30
|
|
|
parser.add_option('-b', dest='b', metavar='string',
|
2014-07-25 01:51:18 +05:30
|
|
|
help = 'crystal frame b vector label')
|
2014-09-12 19:44:55 +05:30
|
|
|
parser.add_option('-c', dest='c', metavar='string',
|
2014-07-25 01:51:18 +05:30
|
|
|
help = 'crystal frame c vector label')
|
2014-09-12 19:44:55 +05:30
|
|
|
parser.add_option('-q', '--quaternion', dest='quaternion', metavar='string',
|
2014-07-25 01:51:18 +05:30
|
|
|
help = 'quaternion label')
|
2013-11-26 00:41:02 +05:30
|
|
|
parser.set_defaults(output = [])
|
|
|
|
parser.set_defaults(symmetry = 'cubic')
|
2014-05-26 20:01:42 +05:30
|
|
|
parser.set_defaults(rotation = [0.,1.,1.,1.]) # no rotation about 1,1,1
|
2014-02-10 23:31:21 +05:30
|
|
|
parser.set_defaults(degrees = False)
|
2013-11-26 00:41:02 +05:30
|
|
|
|
|
|
|
(options, filenames) = parser.parse_args()
|
|
|
|
|
|
|
|
datainfo = { # list of requested labels per datatype
|
|
|
|
'tensor': {'len':9,
|
|
|
|
'label':[]},
|
|
|
|
'vector': {'len':3,
|
|
|
|
'label':[]},
|
|
|
|
'quaternion': {'len':4,
|
|
|
|
'label':[]},
|
|
|
|
}
|
|
|
|
|
2014-07-25 01:51:18 +05:30
|
|
|
if not set(options.output).issubset(set(outputChoices)):
|
|
|
|
parser.error('output must be chosen from %s...'%(', '.join(outputChoices)))
|
|
|
|
|
2014-08-06 20:55:18 +05:30
|
|
|
if options.eulers != None:
|
|
|
|
datainfo['vector']['label'] += [options.eulers]
|
|
|
|
input = 'eulers'
|
2013-11-26 00:41:02 +05:30
|
|
|
if options.a != None and \
|
|
|
|
options.b != None and \
|
2014-08-06 20:55:18 +05:30
|
|
|
options.c != None:
|
|
|
|
datainfo['vector']['label'] += [options.a,options.b,options.c]
|
|
|
|
input = 'frame'
|
|
|
|
if options.matrix != None:
|
|
|
|
datainfo['tensor']['label'] += [options.matrix]
|
|
|
|
input = 'matrix'
|
|
|
|
if options.quaternion != None:
|
|
|
|
datainfo['quaternion']['label'] += [options.quaternion]
|
|
|
|
input = 'quaternion'
|
2013-11-26 00:41:02 +05:30
|
|
|
|
2014-08-04 23:23:41 +05:30
|
|
|
inputGiven = 0
|
|
|
|
for datatype,info in datainfo.items():
|
|
|
|
inputGiven += len(info['label'])
|
|
|
|
if inputGiven != 1: parser.error('select exactly one input format...')
|
|
|
|
|
|
|
|
toRadians = math.pi/180.0 if options.degrees else 1.0 # rescale degrees to radians
|
2013-11-26 00:41:02 +05:30
|
|
|
options.output = map(lambda x: x.lower(), options.output)
|
|
|
|
|
2014-05-26 20:01:42 +05:30
|
|
|
r = damask.Quaternion().fromAngleAxis(toRadians*options.rotation[0],options.rotation[1:])
|
|
|
|
|
2014-08-06 20:55:18 +05:30
|
|
|
# ------------------------------------------ setup file handles ------------------------------------
|
2013-11-26 00:41:02 +05:30
|
|
|
|
|
|
|
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.stderr})
|
|
|
|
|
2014-08-06 20:55:18 +05:30
|
|
|
# ------------------------------------------ loop over input files ---------------------------------
|
2013-11-26 00:41:02 +05:30
|
|
|
for file in files:
|
|
|
|
if file['name'] != 'STDIN': file['croak'].write('\033[1m'+scriptName+'\033[0m: '+file['name']+'\n')
|
|
|
|
else: file['croak'].write('\033[1m'+scriptName+'\033[0m\n')
|
|
|
|
|
2014-07-25 01:51:18 +05:30
|
|
|
table = damask.ASCIItable(file['input'],file['output'],False) # make unbuffered ASCII_table
|
|
|
|
table.head_read() # read ASCII header info
|
2014-08-06 18:57:09 +05:30
|
|
|
table.info_append(scriptID + '\t' + ' '.join(sys.argv[1:]))
|
2013-11-26 00:41:02 +05:30
|
|
|
|
2014-08-07 00:36:33 +05:30
|
|
|
column = {}
|
2014-08-04 23:23:41 +05:30
|
|
|
missingColumns = False
|
2013-11-26 00:41:02 +05:30
|
|
|
|
|
|
|
for datatype,info in datainfo.items():
|
|
|
|
for label in info['label']:
|
2014-08-04 23:23:41 +05:30
|
|
|
key = '1_%s'%label
|
|
|
|
if key not in table.labels:
|
|
|
|
file['croak'].write('column %s not found...\n'%key)
|
|
|
|
missingColumns = True # break if label not found
|
|
|
|
else:
|
2014-08-07 00:36:33 +05:30
|
|
|
column[label] = table.labels.index(key) # remember columns of requested data
|
2014-08-04 23:23:41 +05:30
|
|
|
|
|
|
|
if missingColumns:
|
|
|
|
continue
|
2013-11-26 00:41:02 +05:30
|
|
|
|
2014-08-06 20:55:18 +05:30
|
|
|
# ------------------------------------------ assemble header ---------------------------------------
|
2013-11-26 00:41:02 +05:30
|
|
|
for output in options.output:
|
|
|
|
if output == 'quaternion':
|
|
|
|
table.labels_append(['%i_quaternion_%s'%(i+1,options.symmetry) for i in xrange(4)])
|
|
|
|
if output == 'eulers':
|
|
|
|
table.labels_append(['%i_eulers_%s'%(i+1,options.symmetry) for i in xrange(3)])
|
|
|
|
table.head_write()
|
|
|
|
|
2014-08-06 20:55:18 +05:30
|
|
|
# ------------------------------------------ process data ------------------------------------------
|
2014-07-25 01:51:18 +05:30
|
|
|
outputAlive = True
|
|
|
|
while outputAlive and table.data_read(): # read next data line of ASCII table
|
2013-11-26 00:41:02 +05:30
|
|
|
if input == 'eulers':
|
2014-08-06 20:55:18 +05:30
|
|
|
o = damask.Orientation(Eulers=toRadians*\
|
2014-08-07 00:36:33 +05:30
|
|
|
np.array(map(float,table.data[column[options.eulers]:\
|
|
|
|
column[options.eulers]+datainfo['vector']['len']])),
|
2013-11-26 00:41:02 +05:30
|
|
|
symmetry=options.symmetry).reduced()
|
|
|
|
elif input == 'matrix':
|
2014-08-06 20:55:18 +05:30
|
|
|
o = damask.Orientation(matrix=\
|
2014-08-07 00:36:33 +05:30
|
|
|
np.array([map(float,table.data[column[options.matrix]:\
|
|
|
|
column[options.matrix]+datainfo['tensor']['len']])]),
|
2013-11-26 00:41:02 +05:30
|
|
|
symmetry=options.symmetry).reduced()
|
|
|
|
elif input == 'frame':
|
2014-08-06 20:55:18 +05:30
|
|
|
o = damask.Orientation(matrix=\
|
2014-08-07 00:36:33 +05:30
|
|
|
np.array([map(float,table.data[column[options.a]:\
|
|
|
|
column[options.a]+datainfo['vector']['len']] + \
|
|
|
|
table.data[column[options.b]:\
|
|
|
|
column[options.b]+datainfo['vector']['len']] + \
|
|
|
|
table.data[column[options.c]:\
|
|
|
|
column[options.c]+datainfo['vector']['len']]
|
2013-11-26 00:41:02 +05:30
|
|
|
)]).reshape(3,3),
|
|
|
|
symmetry=options.symmetry).reduced()
|
|
|
|
elif input == 'quaternion':
|
2014-08-06 20:55:18 +05:30
|
|
|
o = damask.Orientation(quaternion=\
|
2014-08-07 00:36:33 +05:30
|
|
|
np.array(map(float,table.data[column[options.quaternion]:\
|
|
|
|
column[options.quaternion]+datainfo['quaternion']['len']])),
|
2013-11-26 00:41:02 +05:30
|
|
|
symmetry=options.symmetry).reduced()
|
|
|
|
|
2014-05-26 20:01:42 +05:30
|
|
|
o.quaternion = r*o.quaternion
|
|
|
|
|
2013-11-26 00:41:02 +05:30
|
|
|
for output in options.output:
|
|
|
|
if output == 'quaternion':
|
|
|
|
table.data_append(o.asQuaternion())
|
|
|
|
if output == 'eulers':
|
|
|
|
table.data_append(o.asEulers('Bunge'))
|
2014-07-25 01:51:18 +05:30
|
|
|
outputAlive = table.data_write() # output processed line
|
2013-11-26 00:41:02 +05:30
|
|
|
|
2014-08-06 20:55:18 +05:30
|
|
|
# ------------------------------------------ output result -----------------------------------------
|
2014-07-25 01:51:18 +05:30
|
|
|
outputAlive and table.output_flush() # just in case of buffered ASCII table
|
2013-11-26 00:41:02 +05:30
|
|
|
|
2014-08-22 22:28:53 +05:30
|
|
|
table.input_close() # close input ASCII table (works for stdin)
|
|
|
|
table.output_close() # close output ASCII table (works for stdout)
|
2013-11-26 00:41:02 +05:30
|
|
|
if file['name'] != 'STDIN':
|
2014-07-25 01:51:18 +05:30
|
|
|
os.rename(file['name']+'_tmp',file['name']) # overwrite old one with tmp new
|