2018-11-17 12:42:12 +05:30
|
|
|
#!/usr/bin/env python3
|
2014-04-02 00:11:14 +05:30
|
|
|
# -*- coding: UTF-8 no BOM -*-
|
2013-11-26 00:41:02 +05:30
|
|
|
|
2018-12-09 22:35:37 +05:30
|
|
|
import os,sys
|
2014-08-06 20:55:18 +05:30
|
|
|
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
|
|
|
|
2016-01-27 22:36:00 +05:30
|
|
|
scriptName = os.path.splitext(os.path.basename(__file__))[0]
|
|
|
|
scriptID = ' '.join([scriptName,damask.version])
|
2013-11-26 00:41:02 +05:30
|
|
|
|
2018-12-08 23:52:22 +05:30
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# convention conformity checks
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
|
|
|
|
def check_Eulers(eulers):
|
2018-12-09 22:35:37 +05:30
|
|
|
if np.any(eulers < 0.0) or np.any(eulers > 2.0*np.pi) or eulers[1] > np.pi: # Euler angles within valid range?
|
2018-12-08 23:52:22 +05:30
|
|
|
raise ValueError('Euler angles outside of [0..2π],[0..π],[0..2π].\n{} {} {}.'.format(*eulers))
|
|
|
|
return eulers
|
|
|
|
|
|
|
|
def check_quaternion(q):
|
2018-12-09 17:05:32 +05:30
|
|
|
if q[0] < 0.0: # positive first quaternion component?
|
2018-12-09 22:35:37 +05:30
|
|
|
raise ValueError('quaternion has negative first component.\n{}'.format(q[0]))
|
|
|
|
if not np.isclose(np.linalg.norm(q), 1.0): # unit quaternion?
|
2018-12-09 17:05:32 +05:30
|
|
|
raise ValueError('quaternion is not of unit length.\n{} {} {} {}'.format(*q))
|
2018-12-08 23:52:22 +05:30
|
|
|
return q
|
|
|
|
|
|
|
|
def check_matrix(M):
|
2018-12-09 22:35:37 +05:30
|
|
|
if not np.isclose(np.linalg.det(M),1.0): # proper rotation?
|
2018-12-08 23:52:22 +05:30
|
|
|
raise ValueError('matrix is not a proper rotation.\n{}'.format(M))
|
2018-12-10 00:49:17 +05:30
|
|
|
if not np.isclose(np.dot(M[0],M[1]), 0.0) \
|
|
|
|
or not np.isclose(np.dot(M[1],M[2]), 0.0) \
|
|
|
|
or not np.isclose(np.dot(M[2],M[0]), 0.0): # all orthogonal?
|
2018-12-08 23:52:22 +05:30
|
|
|
raise ValueError('matrix is not orthogonal.\n{}'.format(M))
|
|
|
|
return M
|
|
|
|
|
2014-07-25 01:51:18 +05:30
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# MAIN
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
|
2019-02-17 02:50:10 +05:30
|
|
|
parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [ASCIItable(s)]', description = """
|
2013-11-26 00:41:02 +05:30
|
|
|
Add quaternion and/or Bunge Euler angle representation of crystal lattice orientation.
|
2016-03-02 01:44:06 +05:30
|
|
|
Orientation is given by quaternion, Euler angles, rotation matrix, or crystal frame coordinates
|
|
|
|
(i.e. component vectors of rotation matrix).
|
2016-08-01 05:05:10 +05:30
|
|
|
Additional (globally fixed) rotations of the lab frame and/or crystal frame can be applied.
|
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
|
|
|
|
2018-12-08 08:33:14 +05:30
|
|
|
outputChoices = {
|
|
|
|
'quaternion': ['quat',4],
|
|
|
|
'rodrigues': ['rodr',3],
|
|
|
|
'eulers': ['eulr',3],
|
|
|
|
'matrix': ['mtrx',9],
|
|
|
|
'angleaxis': ['aaxs',4],
|
|
|
|
}
|
|
|
|
|
2018-12-10 13:57:39 +05:30
|
|
|
parser.add_option('-o',
|
|
|
|
'--output',
|
2015-08-08 00:33:26 +05:30
|
|
|
dest = 'output',
|
|
|
|
action = 'extend', metavar = '<string LIST>',
|
2016-07-31 00:37:44 +05:30
|
|
|
help = 'output orientation formats {{{}}}'.format(', '.join(outputChoices)))
|
2018-12-10 13:57:39 +05:30
|
|
|
parser.add_option('-d',
|
|
|
|
'--degrees',
|
2016-07-31 00:37:44 +05:30
|
|
|
dest = 'degrees',
|
|
|
|
action = 'store_true',
|
2018-12-08 08:33:14 +05:30
|
|
|
help = 'all angles in degrees')
|
2018-12-10 13:57:39 +05:30
|
|
|
parser.add_option('-R',
|
|
|
|
'--labrotation',
|
2016-08-01 05:05:10 +05:30
|
|
|
dest='labrotation',
|
2016-07-31 00:37:44 +05:30
|
|
|
type = 'float', nargs = 4, metavar = ' '.join(['float']*4),
|
2019-02-16 19:23:56 +05:30
|
|
|
help = 'angle and axis of additional lab frame rotation [%default]')
|
2018-12-10 13:57:39 +05:30
|
|
|
parser.add_option('-r',
|
|
|
|
'--crystalrotation',
|
2016-08-01 05:05:10 +05:30
|
|
|
dest='crystalrotation',
|
|
|
|
type = 'float', nargs = 4, metavar = ' '.join(['float']*4),
|
2019-02-16 19:23:56 +05:30
|
|
|
help = 'angle and axis of additional crystal frame rotation [%default]')
|
2018-12-10 13:57:39 +05:30
|
|
|
parser.add_option('--eulers',
|
2015-08-08 00:33:26 +05:30
|
|
|
dest = 'eulers',
|
2018-12-10 13:57:39 +05:30
|
|
|
metavar = 'string',
|
2015-05-10 03:02:23 +05:30
|
|
|
help = 'Euler angles label')
|
2018-12-10 13:57:39 +05:30
|
|
|
parser.add_option('--rodrigues',
|
2017-08-23 23:40:56 +05:30
|
|
|
dest = 'rodrigues',
|
2018-12-10 13:57:39 +05:30
|
|
|
metavar = 'string',
|
2017-08-23 23:40:56 +05:30
|
|
|
help = 'Rodrigues vector label')
|
2018-12-10 13:57:39 +05:30
|
|
|
parser.add_option('--matrix',
|
2015-08-08 00:33:26 +05:30
|
|
|
dest = 'matrix',
|
2018-12-10 13:57:39 +05:30
|
|
|
metavar = 'string',
|
2015-05-10 03:02:23 +05:30
|
|
|
help = 'orientation matrix label')
|
2018-12-10 13:57:39 +05:30
|
|
|
parser.add_option('--quaternion',
|
2017-08-23 23:40:56 +05:30
|
|
|
dest = 'quaternion',
|
2018-12-10 13:57:39 +05:30
|
|
|
metavar = 'string',
|
2017-08-23 23:40:56 +05:30
|
|
|
help = 'quaternion label')
|
2018-12-08 08:33:14 +05:30
|
|
|
parser.add_option('-x',
|
|
|
|
dest = 'x',
|
2018-12-10 13:57:39 +05:30
|
|
|
metavar = 'string',
|
2018-12-08 08:33:14 +05:30
|
|
|
help = 'label of lab x vector (expressed in crystal coords)')
|
|
|
|
parser.add_option('-y',
|
|
|
|
dest = 'y',
|
2018-12-10 13:57:39 +05:30
|
|
|
metavar = 'string',
|
2018-12-08 08:33:14 +05:30
|
|
|
help = 'label of lab y vector (expressed in crystal coords)')
|
|
|
|
parser.add_option('-z',
|
|
|
|
dest = 'z',
|
2018-12-10 13:57:39 +05:30
|
|
|
metavar = 'string',
|
2018-12-08 08:33:14 +05:30
|
|
|
help = 'label of lab z vector (expressed in crystal coords)')
|
2013-11-26 00:41:02 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
parser.set_defaults(output = [],
|
2016-08-01 20:13:14 +05:30
|
|
|
labrotation = (0.,1.,1.,1.), # no rotation about 1,1,1
|
|
|
|
crystalrotation = (0.,1.,1.,1.), # no rotation about 1,1,1
|
2015-08-08 00:33:26 +05:30
|
|
|
)
|
2013-11-26 00:41:02 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
(options, filenames) = parser.parse_args()
|
2014-08-04 23:23:41 +05:30
|
|
|
|
2018-07-19 20:23:48 +05:30
|
|
|
options.output = list(map(lambda x: x.lower(), options.output))
|
2015-08-08 00:33:26 +05:30
|
|
|
if options.output == [] or (not set(options.output).issubset(set(outputChoices))):
|
|
|
|
parser.error('output must be chosen from {}.'.format(', '.join(outputChoices)))
|
|
|
|
|
2016-03-02 01:41:43 +05:30
|
|
|
input = [options.eulers is not None,
|
2017-08-23 23:40:56 +05:30
|
|
|
options.rodrigues is not None,
|
2018-12-08 08:33:14 +05:30
|
|
|
options.x is not None and \
|
|
|
|
options.y is not None and \
|
|
|
|
options.z is not None,
|
2016-03-02 01:41:43 +05:30
|
|
|
options.matrix is not None,
|
|
|
|
options.quaternion is not None,
|
2015-08-08 00:33:26 +05:30
|
|
|
]
|
|
|
|
|
|
|
|
if np.sum(input) != 1: parser.error('needs exactly one input format.')
|
|
|
|
|
|
|
|
(label,dim,inputtype) = [(options.eulers,3,'eulers'),
|
2017-08-23 23:40:56 +05:30
|
|
|
(options.rodrigues,3,'rodrigues'),
|
2018-12-08 08:33:14 +05:30
|
|
|
([options.x,options.y,options.z],[3,3,3],'frame'),
|
2015-08-08 00:33:26 +05:30
|
|
|
(options.matrix,9,'matrix'),
|
|
|
|
(options.quaternion,4,'quaternion'),
|
|
|
|
][np.where(input)[0][0]] # select input label that was requested
|
2018-12-08 23:52:22 +05:30
|
|
|
|
2018-12-09 22:35:37 +05:30
|
|
|
toRadians = np.pi/180.0 if options.degrees else 1.0 # rescale degrees to radians
|
2018-12-08 08:33:14 +05:30
|
|
|
r = damask.Quaternion.fromAngleAxis(toRadians*options.crystalrotation[0],options.crystalrotation[1:]) # crystal frame rotation
|
|
|
|
R = damask.Quaternion.fromAngleAxis(toRadians*options. labrotation[0],options. labrotation[1:]) # lab frame rotation
|
2013-11-26 00:41:02 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
# --- loop over input files ------------------------------------------------------------------------
|
2014-05-26 20:01:42 +05:30
|
|
|
|
2015-08-21 01:12:05 +05:30
|
|
|
if filenames == []: filenames = [None]
|
2015-06-05 17:17:27 +05:30
|
|
|
|
|
|
|
for name in filenames:
|
2016-07-31 00:37:44 +05:30
|
|
|
try: table = damask.ASCIItable(name = name,
|
|
|
|
buffered = False)
|
2015-08-21 01:12:05 +05:30
|
|
|
except: continue
|
2015-09-24 14:54:42 +05:30
|
|
|
damask.util.report(scriptName,name)
|
2013-11-26 00:41:02 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
# ------------------------------------------ read header ------------------------------------------
|
2013-11-26 00:41:02 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
table.head_read()
|
2014-08-04 23:23:41 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
# ------------------------------------------ sanity checks -----------------------------------------
|
|
|
|
|
|
|
|
errors = []
|
|
|
|
remarks = []
|
|
|
|
|
2016-03-09 22:58:36 +05:30
|
|
|
if not np.all(table.label_dimension(label) == dim): errors.append('input {} does not have dimension {}.'.format(label,dim))
|
2015-08-08 00:33:26 +05:30
|
|
|
else: column = table.label_index(label)
|
|
|
|
|
2015-09-24 14:54:42 +05:30
|
|
|
if remarks != []: damask.util.croak(remarks)
|
2015-08-08 00:33:26 +05:30
|
|
|
if errors != []:
|
2015-09-24 14:54:42 +05:30
|
|
|
damask.util.croak(errors)
|
2015-08-08 00:33:26 +05:30
|
|
|
table.close(dismiss = True)
|
2014-08-04 23:23:41 +05:30
|
|
|
continue
|
2013-11-26 00:41:02 +05:30
|
|
|
|
2014-08-06 20:55:18 +05:30
|
|
|
# ------------------------------------------ assemble header ---------------------------------------
|
2015-08-08 00:33:26 +05:30
|
|
|
|
|
|
|
table.info_append(scriptID + '\t' + ' '.join(sys.argv[1:]))
|
2013-11-26 00:41:02 +05:30
|
|
|
for output in options.output:
|
2018-12-08 08:33:14 +05:30
|
|
|
if output in outputChoices:
|
|
|
|
table.labels_append(['{}_{}({})'.format(i+1,outputChoices[output][0],label) \
|
|
|
|
for i in range(outputChoices[output][1])])
|
2013-11-26 00:41:02 +05:30
|
|
|
table.head_write()
|
|
|
|
|
2014-08-06 20:55:18 +05:30
|
|
|
# ------------------------------------------ process data ------------------------------------------
|
2015-08-08 00:33:26 +05:30
|
|
|
|
2014-07-25 01:51:18 +05:30
|
|
|
outputAlive = True
|
|
|
|
while outputAlive and table.data_read(): # read next data line of ASCII table
|
2018-12-08 08:33:14 +05:30
|
|
|
if inputtype == 'eulers':
|
2018-12-08 23:52:22 +05:30
|
|
|
|
|
|
|
o = damask.Orientation(Eulers = check_Eulers(np.array(list(map(float,table.data[column:column+3])))*toRadians))
|
2017-08-23 23:40:56 +05:30
|
|
|
elif inputtype == 'rodrigues':
|
2018-12-08 23:52:22 +05:30
|
|
|
o = damask.Orientation(Rodrigues = np.array(list(map(float,table.data[column:column+3]))))
|
2015-08-08 00:33:26 +05:30
|
|
|
elif inputtype == 'matrix':
|
2018-12-08 23:52:22 +05:30
|
|
|
|
|
|
|
o = damask.Orientation(matrix = check_matrix(np.array(list(map(float,table.data[column:column+9]))).reshape(3,3)))
|
2015-08-08 00:33:26 +05:30
|
|
|
elif inputtype == 'frame':
|
2018-12-08 08:33:14 +05:30
|
|
|
M = np.array(list(map(float,table.data[column[0]:column[0]+3] + \
|
|
|
|
table.data[column[1]:column[1]+3] + \
|
|
|
|
table.data[column[2]:column[2]+3]))).reshape(3,3).T
|
2018-12-08 23:52:22 +05:30
|
|
|
o = damask.Orientation(matrix = check_matrix(M/np.linalg.norm(M,axis=0)))
|
2015-08-08 00:33:26 +05:30
|
|
|
elif inputtype == 'quaternion':
|
2018-12-08 23:52:22 +05:30
|
|
|
|
|
|
|
o = damask.Orientation(quaternion = check_quaternion(np.array(list(map(float,table.data[column:column+4])))))
|
2013-11-26 00:41:02 +05:30
|
|
|
|
2016-08-01 05:05:10 +05:30
|
|
|
o.quaternion = r*o.quaternion*R # apply additional lab and crystal frame rotations
|
2014-05-26 20:01:42 +05:30
|
|
|
|
2013-11-26 00:41:02 +05:30
|
|
|
for output in options.output:
|
2016-07-31 00:37:44 +05:30
|
|
|
if output == 'quaternion': table.data_append(o.asQuaternion())
|
|
|
|
elif output == 'rodrigues': table.data_append(o.asRodrigues())
|
2018-11-22 22:21:53 +05:30
|
|
|
elif output == 'eulers': table.data_append(o.asEulers(degrees=options.degrees))
|
2018-12-08 08:33:14 +05:30
|
|
|
elif output == 'matrix': table.data_append(o.asMatrix())
|
|
|
|
elif output == 'angleaxis': table.data_append(o.asAngleAxis(degrees=options.degrees,flat=True))
|
2014-07-25 01:51:18 +05:30
|
|
|
outputAlive = table.data_write() # output processed line
|
2013-11-26 00:41:02 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
# ------------------------------------------ output finalization -----------------------------------
|
2013-11-26 00:41:02 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
table.close() # close ASCII tables
|