2014-12-13 05:42:55 +05:30
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: UTF-8 no BOM -*-
|
|
|
|
|
|
|
|
import os,sys,string
|
|
|
|
import numpy as np
|
|
|
|
from optparse import OptionParser
|
|
|
|
import damask
|
|
|
|
|
|
|
|
scriptID = string.replace('$Id$','\n','\\n')
|
|
|
|
scriptName = os.path.splitext(scriptID.split()[1])[0]
|
|
|
|
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# 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)
|
|
|
|
|
|
|
|
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]')
|
2014-12-13 05:42:55 +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]')
|
2014-12-13 05:42:55 +05:30
|
|
|
parser.add_option('-e', '--eulers', dest='eulers', metavar='string',
|
2015-05-10 03:02:23 +05:30
|
|
|
help = 'Euler angles label')
|
2014-12-13 05:42:55 +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]')
|
2014-12-13 05:42:55 +05:30
|
|
|
parser.add_option('-m', '--matrix', dest='matrix', metavar='string',
|
2015-05-10 03:02:23 +05:30
|
|
|
help = 'orientation matrix label')
|
2014-12-13 05:42:55 +05:30
|
|
|
parser.add_option('-a', dest='a', metavar='string',
|
2015-05-10 03:02:23 +05:30
|
|
|
help = 'crystal frame a vector label')
|
2014-12-13 05:42:55 +05:30
|
|
|
parser.add_option('-b', dest='b', metavar='string',
|
2015-05-10 03:02:23 +05:30
|
|
|
help = 'crystal frame b vector label')
|
2014-12-13 05:42:55 +05:30
|
|
|
parser.add_option('-c', dest='c', metavar='string',
|
2015-05-10 03:02:23 +05:30
|
|
|
help = 'crystal frame c vector label')
|
2014-12-13 05:42:55 +05:30
|
|
|
parser.add_option('-q', '--quaternion', dest='quaternion', metavar='string',
|
2015-05-10 03:02:23 +05:30
|
|
|
help = 'quaternion label')
|
|
|
|
parser.set_defaults(pole = (1.0,0.0,0.0))
|
2014-12-13 05:42:55 +05:30
|
|
|
parser.set_defaults(degrees = False)
|
|
|
|
parser.set_defaults(polar = False)
|
|
|
|
|
|
|
|
(options, filenames) = parser.parse_args()
|
|
|
|
|
|
|
|
datainfo = { # list of requested labels per datatype
|
|
|
|
'tensor': {'len':9,
|
|
|
|
'label':[]},
|
|
|
|
'vector': {'len':3,
|
|
|
|
'label':[]},
|
|
|
|
'quaternion': {'len':4,
|
|
|
|
'label':[]},
|
|
|
|
}
|
|
|
|
|
|
|
|
input = []
|
|
|
|
if options.eulers != None:
|
|
|
|
datainfo['vector']['label'] += [options.eulers]
|
|
|
|
input += ['eulers']
|
|
|
|
if options.a != None and \
|
|
|
|
options.b != None and \
|
|
|
|
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']
|
|
|
|
|
|
|
|
if len(input) != 1: parser.error('needs exactly one input format...')
|
|
|
|
input = input[0]
|
|
|
|
|
|
|
|
toRadians = np.pi/180.0 if options.degrees else 1.0 # rescale degrees to radians
|
|
|
|
pole = np.array(options.pole)
|
|
|
|
pole /= np.linalg.norm(pole)
|
|
|
|
|
2015-03-06 22:34:44 +05:30
|
|
|
# --- loop over input files -------------------------------------------------------------------------
|
2014-12-13 05:42:55 +05:30
|
|
|
if filenames == []:
|
2015-03-06 22:34:44 +05:30
|
|
|
filenames = ['STDIN']
|
2014-12-13 05:42:55 +05:30
|
|
|
|
2015-03-06 22:34:44 +05:30
|
|
|
for name in filenames:
|
|
|
|
if name == 'STDIN':
|
|
|
|
file = {'name':'STDIN', 'input':sys.stdin, 'output':sys.stdout, 'croak':sys.stderr}
|
|
|
|
file['croak'].write('\033[1m'+scriptName+'\033[0m\n')
|
|
|
|
else:
|
|
|
|
if not os.path.exists(name): continue
|
|
|
|
file = {'name':name, 'input':open(name), 'output':open(name+'_tmp','w'), 'croak':sys.stderr}
|
|
|
|
file['croak'].write('\033[1m'+scriptName+'\033[0m: '+file['name']+'\n')
|
2014-12-13 05:42:55 +05:30
|
|
|
|
|
|
|
table = damask.ASCIItable(file['input'],file['output'],buffered = False) # make unbuffered ASCII_table
|
|
|
|
table.head_read() # read ASCII header info
|
|
|
|
table.info_append(scriptID + '\t' + ' '.join(sys.argv[1:]))
|
|
|
|
|
|
|
|
column = {}
|
|
|
|
missingColumns = False
|
|
|
|
|
|
|
|
for datatype,info in datainfo.items():
|
|
|
|
for label in info['label']:
|
|
|
|
key = list(set([label, '1_'+label]) & set(table.labels))
|
|
|
|
if key == []:
|
|
|
|
file['croak'].write('column %s not found...\n'%label)
|
|
|
|
missingColumns = True # break if label not found
|
|
|
|
else:
|
|
|
|
column[label] = table.labels.index(key[0]) # remember columns of requested data
|
|
|
|
|
|
|
|
if missingColumns:
|
|
|
|
continue
|
|
|
|
|
|
|
|
# ------------------------------------------ assemble header ---------------------------------------
|
|
|
|
table.labels_append(['%i_pole_%g%g%g'%(i+1,options.pole[0],options.pole[1],options.pole[2]) for i in xrange(2)])
|
|
|
|
table.head_write()
|
|
|
|
|
|
|
|
# ------------------------------------------ process data ------------------------------------------
|
|
|
|
outputAlive = True
|
|
|
|
while outputAlive and table.data_read(): # read next data line of ASCII table
|
|
|
|
if input == 'eulers':
|
|
|
|
o = damask.Orientation(Eulers=toRadians*\
|
|
|
|
np.array(map(float,table.data[column[options.eulers]:\
|
|
|
|
column[options.eulers]+datainfo['vector']['len']])),
|
|
|
|
)
|
|
|
|
elif input == 'matrix':
|
|
|
|
o = damask.Orientation(matrix=\
|
|
|
|
np.array([map(float,table.data[column[options.matrix]:\
|
|
|
|
column[options.matrix]+datainfo['tensor']['len']])]).reshape(np.sqrt(datainfo['tensor']['len']),
|
|
|
|
np.sqrt(datainfo['tensor']['len'])).transpose(),
|
|
|
|
)
|
|
|
|
elif input == 'frame':
|
|
|
|
o = damask.Orientation(matrix=\
|
|
|
|
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']]
|
|
|
|
)]).reshape(3,3),
|
|
|
|
)
|
|
|
|
elif input == 'quaternion':
|
|
|
|
o = damask.Orientation(quaternion=\
|
|
|
|
np.array(map(float,table.data[column[options.quaternion]:\
|
|
|
|
column[options.quaternion]+datainfo['quaternion']['len']])),
|
|
|
|
)
|
|
|
|
|
|
|
|
rotatedPole = o.quaternion*pole # rotate pole according to crystal orientation
|
|
|
|
(x,y) = rotatedPole[0:2]/(1.+abs(pole[2])) # stereographic projection
|
|
|
|
|
|
|
|
table.data_append([np.sqrt(x*x+y*y),np.arctan2(y,x)] if options.polar else [x,y]) # cartesian coordinates
|
|
|
|
|
|
|
|
outputAlive = table.data_write() # output processed line
|
|
|
|
|
|
|
|
# ------------------------------------------ output result -----------------------------------------
|
|
|
|
outputAlive and table.output_flush() # just in case of buffered ASCII table
|
|
|
|
|
|
|
|
table.input_close() # close input ASCII table (works for stdin)
|
|
|
|
table.output_close() # close output ASCII table (works for stdout)
|
|
|
|
if file['name'] != 'STDIN':
|
|
|
|
os.rename(file['name']+'_tmp',file['name']) # overwrite old one with tmp new
|