2016-07-18 23:05:35 +05:30
|
|
|
#!/usr/bin/env python2.7
|
2014-05-27 13:38:10 +05:30
|
|
|
# -*- coding: UTF-8 no BOM -*-
|
|
|
|
|
2016-03-01 22:55:14 +05:30
|
|
|
import os,sys,math
|
2014-08-07 22:21:26 +05:30
|
|
|
import numpy as np
|
|
|
|
from optparse import OptionParser
|
|
|
|
import damask
|
2014-05-27 13:38:10 +05:30
|
|
|
|
2016-01-27 22:36:00 +05:30
|
|
|
scriptName = os.path.splitext(os.path.basename(__file__))[0]
|
|
|
|
scriptID = ' '.join([scriptName,damask.version])
|
2014-05-27 13:38:10 +05:30
|
|
|
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# MAIN
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
|
2014-08-07 22:21:26 +05:30
|
|
|
parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [file[s]]', description = """
|
2015-03-29 21:07:40 +05:30
|
|
|
Rotate vector and/or tensor column data by given angle around given axis.
|
2014-05-27 13:38:10 +05:30
|
|
|
|
2014-08-07 22:21:26 +05:30
|
|
|
""", version = scriptID)
|
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
parser.add_option('-v','--vector',
|
|
|
|
dest = 'vector',
|
|
|
|
action = 'extend', metavar = '<string LIST>',
|
|
|
|
help = 'column heading of vector(s) to rotate')
|
|
|
|
parser.add_option('-t','--tensor',
|
|
|
|
dest = 'tensor',
|
|
|
|
action = 'extend', metavar = '<string LIST>',
|
|
|
|
help = 'column heading of tensor(s) to rotate')
|
|
|
|
parser.add_option('-r', '--rotation',
|
|
|
|
dest = 'rotation',
|
|
|
|
type = 'float', nargs = 4, metavar = ' '.join(['float']*4),
|
2015-05-10 16:28:32 +05:30
|
|
|
help = 'angle and axis to rotate data [%default]')
|
2015-08-08 00:33:26 +05:30
|
|
|
parser.add_option('-d', '--degrees',
|
|
|
|
dest = 'degrees',
|
|
|
|
action = 'store_true',
|
2015-05-10 16:28:32 +05:30
|
|
|
help = 'angles are given in degrees [%default]')
|
2014-05-27 13:38:10 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
parser.set_defaults(rotation = (0.,1.,1.,1.), # no rotation about 1,1,1
|
|
|
|
degrees = False,
|
|
|
|
)
|
|
|
|
|
2014-05-27 13:38:10 +05:30
|
|
|
(options,filenames) = parser.parse_args()
|
|
|
|
|
2016-03-02 15:22:24 +05:30
|
|
|
if options.vector is None and options.tensor is None:
|
2015-08-08 00:33:26 +05:30
|
|
|
parser.error('no data column specified.')
|
2014-05-27 13:38:10 +05:30
|
|
|
|
2014-08-07 22:21:26 +05:30
|
|
|
toRadians = math.pi/180.0 if options.degrees else 1.0 # rescale degrees to radians
|
2015-08-08 00:33:26 +05:30
|
|
|
q = damask.Quaternion().fromAngleAxis(toRadians*options.rotation[0],options.rotation[1:])
|
|
|
|
R = q.asMatrix()
|
2014-05-27 13:38:10 +05:30
|
|
|
|
2015-02-11 22:52:47 +05:30
|
|
|
# --- loop over input files -------------------------------------------------------------------------
|
2015-08-08 00:33:26 +05:30
|
|
|
|
2015-08-18 20:07:32 +05:30
|
|
|
if filenames == []: filenames = [None]
|
2015-02-11 22:52:47 +05:30
|
|
|
|
|
|
|
for name in filenames:
|
2015-08-18 20:07:32 +05:30
|
|
|
try:
|
|
|
|
table = damask.ASCIItable(name = name,
|
|
|
|
buffered = False)
|
|
|
|
except: continue
|
2015-09-24 14:54:42 +05:30
|
|
|
damask.util.report(scriptName,name)
|
2015-08-08 00:33:26 +05:30
|
|
|
|
|
|
|
# ------------------------------------------ read header ------------------------------------------
|
|
|
|
|
|
|
|
table.head_read()
|
|
|
|
|
|
|
|
# ------------------------------------------ sanity checks ----------------------------------------
|
|
|
|
|
|
|
|
items = {
|
|
|
|
'tensor': {'dim': 9, 'shape': [3,3], 'labels':options.tensor, 'active':[], 'column': []},
|
|
|
|
'vector': {'dim': 3, 'shape': [3], 'labels':options.vector, 'active':[], 'column': []},
|
|
|
|
}
|
|
|
|
errors = []
|
|
|
|
remarks = []
|
|
|
|
column = {}
|
|
|
|
|
2018-07-19 19:46:10 +05:30
|
|
|
for type, data in items.items():
|
2015-08-08 00:33:26 +05:30
|
|
|
for what in data['labels']:
|
|
|
|
dim = table.label_dimension(what)
|
|
|
|
if dim != data['dim']: remarks.append('column {} is not a {}.'.format(what,type))
|
|
|
|
else:
|
|
|
|
items[type]['active'].append(what)
|
|
|
|
items[type]['column'].append(table.label_index(what))
|
2014-05-27 13:38:10 +05:30
|
|
|
|
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)
|
|
|
|
continue
|
2014-05-27 13:38:10 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
# ------------------------------------------ assemble header --------------------------------------
|
|
|
|
|
|
|
|
table.info_append(scriptID + '\t' + ' '.join(sys.argv[1:]))
|
2014-05-27 13:38:10 +05:30
|
|
|
table.head_write()
|
|
|
|
|
2014-08-07 22:21:26 +05:30
|
|
|
# ------------------------------------------ process data ------------------------------------------
|
2014-05-27 13:38:10 +05:30
|
|
|
outputAlive = True
|
2014-08-07 22:21:26 +05:30
|
|
|
while outputAlive and table.data_read(): # read next data line of ASCII table
|
2014-08-22 21:02:48 +05:30
|
|
|
|
2014-05-27 13:38:10 +05:30
|
|
|
datatype = 'vector'
|
2014-08-22 21:02:48 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
for column in items[datatype]['column']: # loop over all requested labels
|
|
|
|
table.data[column:column+items[datatype]['dim']] = \
|
2015-09-09 22:15:49 +05:30
|
|
|
q * np.array(map(float,table.data[column:column+items[datatype]['dim']]))
|
2014-05-27 13:38:10 +05:30
|
|
|
|
|
|
|
datatype = 'tensor'
|
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
for column in items[datatype]['column']: # loop over all requested labels
|
|
|
|
table.data[column:column+items[datatype]['dim']] = \
|
|
|
|
np.dot(R,np.dot(np.array(map(float,table.data[column:column+items[datatype]['dim']])).\
|
2016-03-02 15:22:24 +05:30
|
|
|
reshape(items[datatype]['shape']),R.transpose())).reshape(items[datatype]['dim'])
|
2015-08-08 00:33:26 +05:30
|
|
|
|
2014-08-07 22:21:26 +05:30
|
|
|
outputAlive = table.data_write() # output processed line
|
2014-05-27 13:38:10 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
# ------------------------------------------ output finalization -----------------------------------
|
2014-05-27 13:38:10 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
table.close() # close ASCII tables
|