2016-05-11 14:31:39 +05:30
|
|
|
#!/usr/bin/env python2
|
2014-12-13 05:42:55 +05:30
|
|
|
# -*- coding: UTF-8 no BOM -*-
|
|
|
|
|
2016-03-01 22:55:14 +05:30
|
|
|
import os,sys,math
|
2014-12-13 05:42:55 +05:30
|
|
|
import numpy as np
|
|
|
|
from optparse import OptionParser
|
|
|
|
import damask
|
|
|
|
|
2016-01-27 22:36:00 +05:30
|
|
|
scriptName = os.path.splitext(os.path.basename(__file__))[0]
|
|
|
|
scriptID = ' '.join([scriptName,damask.version])
|
2014-12-13 05:42:55 +05:30
|
|
|
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# MAIN
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
|
|
|
|
parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [file[s]]', description = """
|
|
|
|
Add x,y coordinates of stereographic projection of given direction (pole) in crystal frame.
|
|
|
|
|
|
|
|
""", version = scriptID)
|
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
parser.add_option('-p', '--pole',
|
|
|
|
dest = 'pole',
|
|
|
|
type = 'float', nargs = 3, metavar = 'float float float',
|
2015-05-10 03:02:23 +05:30
|
|
|
help = 'crystal frame direction for pole figure [%default]')
|
2015-08-08 00:33:26 +05:30
|
|
|
parser.add_option('--polar',
|
|
|
|
dest = 'polar',
|
|
|
|
action = 'store_true',
|
2015-05-10 03:02:23 +05:30
|
|
|
help = 'output polar coordinates r,phi [%default]')
|
2015-08-08 00:33:26 +05:30
|
|
|
parser.add_option('-e', '--eulers',
|
|
|
|
dest = 'eulers',
|
|
|
|
type = 'string', metavar = 'string',
|
2015-05-10 03:02:23 +05:30
|
|
|
help = 'Euler angles label')
|
2015-08-08 00:33:26 +05:30
|
|
|
parser.add_option('-d', '--degrees',
|
|
|
|
dest = 'degrees',
|
|
|
|
action = 'store_true',
|
2015-05-10 03:02:23 +05:30
|
|
|
help = 'Euler angles are given in degrees [%default]')
|
2015-08-08 00:33:26 +05:30
|
|
|
parser.add_option('-m', '--matrix',
|
|
|
|
dest = 'matrix',
|
|
|
|
type = 'string', metavar = 'string',
|
2015-05-10 03:02:23 +05:30
|
|
|
help = 'orientation matrix label')
|
2015-08-08 00:33:26 +05:30
|
|
|
parser.add_option('-a',
|
|
|
|
dest = 'a',
|
|
|
|
type = 'string', metavar = 'string',
|
2015-05-10 03:02:23 +05:30
|
|
|
help = 'crystal frame a vector label')
|
2015-08-08 00:33:26 +05:30
|
|
|
parser.add_option('-b',
|
|
|
|
dest = 'b',
|
|
|
|
type = 'string', metavar = 'string',
|
2015-05-10 03:02:23 +05:30
|
|
|
help = 'crystal frame b vector label')
|
2015-08-08 00:33:26 +05:30
|
|
|
parser.add_option('-c',
|
|
|
|
dest = 'c',
|
|
|
|
type = 'string', metavar = 'string',
|
2015-05-10 03:02:23 +05:30
|
|
|
help = 'crystal frame c vector label')
|
2015-08-08 00:33:26 +05:30
|
|
|
parser.add_option('-q', '--quaternion',
|
|
|
|
dest = 'quaternion',
|
|
|
|
type = 'string', metavar = 'string',
|
2015-05-10 03:02:23 +05:30
|
|
|
help = 'quaternion label')
|
2015-08-08 00:33:26 +05:30
|
|
|
|
|
|
|
parser.set_defaults(pole = (1.0,0.0,0.0),
|
|
|
|
degrees = False,
|
|
|
|
polar = False,
|
|
|
|
)
|
2014-12-13 05:42:55 +05:30
|
|
|
|
|
|
|
(options, filenames) = parser.parse_args()
|
|
|
|
|
2016-03-02 01:41:43 +05:30
|
|
|
input = [options.eulers is not None,
|
|
|
|
options.a is not None and \
|
|
|
|
options.b is not None and \
|
|
|
|
options.c is not None,
|
|
|
|
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'),
|
|
|
|
([options.a,options.b,options.c],[3,3,3],'frame'),
|
|
|
|
(options.matrix,9,'matrix'),
|
|
|
|
(options.quaternion,4,'quaternion'),
|
|
|
|
][np.where(input)[0][0]] # select input label that was requested
|
|
|
|
toRadians = math.pi/180.0 if options.degrees else 1.0 # rescale degrees to radians
|
2014-12-13 05:42:55 +05:30
|
|
|
pole = np.array(options.pole)
|
|
|
|
pole /= np.linalg.norm(pole)
|
|
|
|
|
2015-08-21 01:12:05 +05:30
|
|
|
# --- loop over input files -------------------------------------------------------------------------
|
2015-08-08 00:33:26 +05:30
|
|
|
|
2015-08-21 01:12:05 +05:30
|
|
|
if filenames == []: filenames = [None]
|
2014-12-13 05:42:55 +05:30
|
|
|
|
2015-03-06 22:34:44 +05:30
|
|
|
for name in filenames:
|
2015-08-21 01:12:05 +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 ------------------------------------------
|
2014-12-13 05:42:55 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
table.head_read()
|
2014-12-13 05:42:55 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
# ------------------------------------------ sanity checks ----------------------------------------
|
2014-12-13 05:42:55 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
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-12-13 05:42:55 +05:30
|
|
|
continue
|
|
|
|
|
|
|
|
# ------------------------------------------ assemble header ---------------------------------------
|
2015-08-08 00:33:26 +05:30
|
|
|
|
|
|
|
table.info_append(scriptID + '\t' + ' '.join(sys.argv[1:]))
|
|
|
|
table.labels_append(['{}_pole_{}{}{}'.format(i+1,*options.pole) for i in xrange(2)])
|
2014-12-13 05:42:55 +05:30
|
|
|
table.head_write()
|
|
|
|
|
|
|
|
# ------------------------------------------ process data ------------------------------------------
|
|
|
|
outputAlive = True
|
|
|
|
while outputAlive and table.data_read(): # read next data line of ASCII table
|
2015-08-08 00:33:26 +05:30
|
|
|
if inputtype == 'eulers':
|
|
|
|
o = damask.Orientation(Eulers = np.array(map(float,table.data[column:column+3]))*toRadians)
|
|
|
|
elif inputtype == 'matrix':
|
|
|
|
o = damask.Orientation(matrix = np.array(map(float,table.data[column:column+9])).reshape(3,3).transpose())
|
|
|
|
elif inputtype == 'frame':
|
|
|
|
o = damask.Orientation(matrix = np.array(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))
|
|
|
|
elif inputtype == 'quaternion':
|
|
|
|
o = damask.Orientation(quaternion = np.array(map(float,table.data[column:column+4])))
|
2014-12-13 05:42:55 +05:30
|
|
|
|
|
|
|
rotatedPole = o.quaternion*pole # rotate pole according to crystal orientation
|
|
|
|
(x,y) = rotatedPole[0:2]/(1.+abs(pole[2])) # stereographic projection
|
|
|
|
|
2016-03-02 01:41:43 +05:30
|
|
|
table.data_append([np.sqrt(x*x+y*y),np.arctan2(y,x)] if options.polar else [x,y]) # cartesian coordinates
|
2014-12-13 05:42:55 +05:30
|
|
|
|
|
|
|
outputAlive = table.data_write() # output processed line
|
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
# ------------------------------------------ output finalization -----------------------------------
|
2014-12-13 05:42:55 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
table.close() # close ASCII tables
|