DAMASK_EICMD/processing/post/rotateData.py

105 lines
3.7 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
import os
import sys
from optparse import OptionParser
import numpy as np
import damask
scriptName = os.path.splitext(os.path.basename(__file__))[0]
scriptID = ' '.join([scriptName,damask.version])
# --------------------------------------------------------------------
# MAIN
# --------------------------------------------------------------------
2019-02-17 12:30:26 +05:30
parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [ASCIItable(s)]', description = """
2015-03-29 21:07:40 +05:30
Rotate vector and/or tensor column data by given angle around given axis.
""", version = scriptID)
parser.add_option('-d', '--data',
dest = 'data',
action = 'extend', metavar = '<string LIST>',
help = 'vector/tensor value(s) label(s)')
parser.add_option('-r', '--rotation',
dest = 'rotation',
type = 'float', nargs = 4, metavar = ' '.join(['float']*4),
2019-03-10 00:28:17 +05:30
help = 'axis and angle to rotate data [%default]')
parser.add_option('--degrees',
dest = 'degrees',
action = 'store_true',
2019-02-16 19:23:56 +05:30
help = 'angles are given in degrees')
2019-03-10 00:28:17 +05:30
parser.set_defaults(rotation = (1.,1.,1.,0), # no rotation about (1,1,1)
degrees = False,
)
(options,filenames) = parser.parse_args()
if options.data is None:
parser.error('no data column specified.')
2019-03-10 00:28:17 +05:30
r = damask.Rotation.fromAxisAngle(np.array(options.rotation),options.degrees,normalise=True)
# --- loop over input files -------------------------------------------------------------------------
2015-08-18 20:07:32 +05:30
if filenames == []: filenames = [None]
for name in filenames:
2015-08-18 20:07:32 +05:30
try:
table = damask.ASCIItable(name = name,
buffered = False)
except: continue
damask.util.report(scriptName,name)
# --- interpret header ----------------------------------------------------------------------------
table.head_read()
errors = []
remarks = []
active = {'vector':[],'tensor':[]}
for i,dim in enumerate(table.label_dimension(options.data)):
label = options.data[i]
if dim == -1:
remarks.append('"{}" not found...'.format(label))
elif dim == 3:
remarks.append('adding vector "{}"...'.format(label))
active['vector'].append(label)
elif dim == 9:
remarks.append('adding tensor "{}"...'.format(label))
active['tensor'].append(label)
if remarks != []: damask.util.croak(remarks)
if errors != []:
damask.util.croak(errors)
table.close(dismiss = True)
continue
# ------------------------------------------ assemble header --------------------------------------
table.info_append(scriptID + '\t' + ' '.join(sys.argv[1:]))
table.head_write()
# ------------------------------------------ process data ------------------------------------------
outputAlive = True
while outputAlive and table.data_read(): # read next data line of ASCII table
for v in active['vector']:
column = table.label_index(v)
table.data[column:column+3] = r * np.array(list(map(float,table.data[column:column+3])))
for t in active['tensor']:
2018-12-09 12:28:42 +05:30
column = table.label_index(t)
table.data[column:column+9] = (r * np.array(list(map(float,table.data[column:column+9]))).reshape((3,3))).reshape(9)
outputAlive = table.data_write() # output processed line
# ------------------------------------------ output finalization -----------------------------------
table.close() # close ASCII tables