2018-11-17 12:42:12 +05:30
|
|
|
#!/usr/bin/env python3
|
2015-11-12 01:21:17 +05:30
|
|
|
# -*- coding: UTF-8 no BOM -*-
|
|
|
|
|
2019-05-25 02:01:34 +05:30
|
|
|
import os
|
|
|
|
import sys
|
2015-11-12 01:21:17 +05:30
|
|
|
import numpy as np
|
|
|
|
import damask
|
2019-05-25 02:01:34 +05:30
|
|
|
from io import StringIO
|
2015-11-12 01:21:17 +05:30
|
|
|
from scipy import ndimage
|
|
|
|
from optparse import OptionParser
|
|
|
|
|
2016-01-27 22:36:00 +05:30
|
|
|
scriptName = os.path.splitext(os.path.basename(__file__))[0]
|
|
|
|
scriptID = ' '.join([scriptName,damask.version])
|
2015-11-12 01:21:17 +05:30
|
|
|
|
|
|
|
#--------------------------------------------------------------------------------------------------
|
|
|
|
# MAIN
|
|
|
|
#--------------------------------------------------------------------------------------------------
|
|
|
|
|
2016-05-12 12:24:34 +05:30
|
|
|
parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [geomfile(s)]', description = """
|
2015-11-12 01:21:17 +05:30
|
|
|
Rotates spectral geometry description.
|
|
|
|
|
|
|
|
""", version=scriptID)
|
|
|
|
|
|
|
|
parser.add_option('-r', '--rotation',
|
|
|
|
dest='rotation',
|
|
|
|
type = 'float', nargs = 4, metavar = ' '.join(['float']*4),
|
|
|
|
help = 'rotation given as angle and axis')
|
|
|
|
parser.add_option('-e', '--eulers',
|
|
|
|
dest = 'eulers',
|
|
|
|
type = 'float', nargs = 3, metavar = ' '.join(['float']*3),
|
|
|
|
help = 'rotation given as Euler angles')
|
|
|
|
parser.add_option('-d', '--degrees',
|
|
|
|
dest = 'degrees',
|
|
|
|
action = 'store_true',
|
|
|
|
help = 'Euler angles are given in degrees [%default]')
|
|
|
|
parser.add_option('-m', '--matrix',
|
|
|
|
dest = 'matrix',
|
|
|
|
type = 'float', nargs = 9, metavar = ' '.join(['float']*9),
|
|
|
|
help = 'rotation given as matrix')
|
|
|
|
parser.add_option('-q', '--quaternion',
|
|
|
|
dest = 'quaternion',
|
|
|
|
type = 'float', nargs = 4, metavar = ' '.join(['float']*4),
|
|
|
|
help = 'rotation given as quaternion')
|
|
|
|
parser.add_option('-f', '--fill',
|
|
|
|
dest = 'fill',
|
|
|
|
type = 'int', metavar = 'int',
|
2019-05-25 02:01:34 +05:30
|
|
|
help = 'background grain index, defaults to max + 1')
|
2015-11-12 01:21:17 +05:30
|
|
|
|
|
|
|
parser.set_defaults(degrees = False,
|
2019-05-23 22:33:24 +05:30
|
|
|
)
|
2015-11-12 01:21:17 +05:30
|
|
|
|
|
|
|
(options, filenames) = parser.parse_args()
|
|
|
|
|
2017-08-23 23:39:33 +05:30
|
|
|
if sum(x is not None for x in [options.rotation,options.eulers,options.matrix,options.quaternion]) != 1:
|
2015-11-12 01:21:17 +05:30
|
|
|
parser.error('not exactly one rotation specified...')
|
|
|
|
|
2019-02-24 02:43:24 +05:30
|
|
|
if options.quaternion is not None:
|
|
|
|
eulers = damask.Rotation.fromQuaternion(np.array(options.quaternion)).asEulers(degrees=True)
|
|
|
|
if options.rotation is not None:
|
|
|
|
eulers = damask.Rotation.fromAxisAngle(np.array(options.rotation,degrees=True)).asEulers(degrees=True)
|
|
|
|
if options.matrix is not None:
|
|
|
|
eulers = damask.Rotation.fromMatrix(np.array(options.Matrix)).asEulers(degrees=True)
|
|
|
|
if options.eulers is not None:
|
|
|
|
eulers = damask.Rotation.fromEulers(np.array(options.eulers),degrees=True).asEulers(degrees=True)
|
2017-08-23 23:39:33 +05:30
|
|
|
|
2015-11-12 01:21:17 +05:30
|
|
|
# --- loop over input files -------------------------------------------------------------------------
|
|
|
|
|
|
|
|
if filenames == []: filenames = [None]
|
|
|
|
|
|
|
|
for name in filenames:
|
2019-05-25 02:01:34 +05:30
|
|
|
if name is None:
|
|
|
|
virt_file = StringIO(''.join(sys.stdin.read()))
|
|
|
|
geom = damask.Geom.from_file(virt_file)
|
|
|
|
else:
|
2019-05-25 11:54:32 +05:30
|
|
|
geom = damask.Geom.from_file(name)
|
|
|
|
|
2015-11-12 01:21:17 +05:30
|
|
|
damask.util.report(scriptName,name)
|
|
|
|
|
2019-05-25 02:01:34 +05:30
|
|
|
microstructure = geom.microstructure
|
|
|
|
spacing = geom.get_size()/geom.get_grid()
|
|
|
|
|
|
|
|
newGrainID = options.fill if options.fill is not None else np.nanmax(microstructure)+1
|
|
|
|
microstructure = ndimage.rotate(microstructure,eulers[2],(0,1),order=0,
|
|
|
|
prefilter=False,output=microstructure.dtype,cval=newGrainID) # rotation around Z
|
|
|
|
microstructure = ndimage.rotate(microstructure,eulers[1],(1,2),order=0,
|
|
|
|
prefilter=False,output=microstructure.dtype,cval=newGrainID) # rotation around X
|
|
|
|
microstructure = ndimage.rotate(microstructure,eulers[0],(0,1),order=0,
|
|
|
|
prefilter=False,output=microstructure.dtype,cval=newGrainID) # rotation around Z
|
|
|
|
|
|
|
|
geom.microstructure = microstructure
|
|
|
|
geom.set_size(microstructure.shape*spacing)
|
|
|
|
geom.add_comment(scriptID + ' ' + ' '.join(sys.argv[1:]))
|
|
|
|
|
|
|
|
damask.util.croak('\n'.join(geom.info()))
|
|
|
|
|
|
|
|
if name is None:
|
|
|
|
sys.stdout.write(str(geom))
|
|
|
|
else:
|
|
|
|
geom.to_file(name)
|