DAMASK_EICMD/processing/pre/geom_translate.py

65 lines
2.3 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
2019-05-25 13:44:53 +05:30
import os
import sys
from optparse import OptionParser
2019-05-25 13:44:53 +05:30
from io import StringIO
2019-05-27 00:06:41 +05:30
2019-05-25 13:44:53 +05:30
import damask
2019-05-27 00:06:41 +05:30
scriptName = os.path.splitext(os.path.basename(__file__))[0]
scriptID = ' '.join([scriptName,damask.version])
2019-05-27 00:06:41 +05:30
#--------------------------------------------------------------------------------------------------
# MAIN
#--------------------------------------------------------------------------------------------------
2019-05-27 00:06:41 +05:30
parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [geomfile(s)]', description = """
Translate microstructure indices (shift or substitute) and/or geometry origin.
""", version=scriptID)
parser.add_option('-o', '--origin',
dest = 'origin',
type = 'float', nargs = 3, metavar = ' '.join(['float']*3),
help = 'offset from old to new origin of grid')
parser.add_option('-m', '--microstructure',
dest = 'microstructure',
type = 'int', metavar = 'int',
2019-05-27 00:06:41 +05:30
help = 'offset from old to new microstructure indices (after substitution)')
parser.add_option('-s', '--substitute',
dest = 'substitute',
action = 'extend', metavar = '<string LIST>',
help = 'substitutions of microstructure indices from,to,from,to,...')
parser.set_defaults(origin = (0.0,0.0,0.0),
microstructure = 0,
2019-05-25 13:44:53 +05:30
substitute = []
)
(options, filenames) = parser.parse_args()
sub = list(map(int,options.substitute))
2019-05-30 14:56:47 +05:30
if filenames == []: filenames = [None]
for name in filenames:
2015-09-24 22:22:58 +05:30
damask.util.report(scriptName,name)
2019-05-30 17:37:49 +05:30
geom = damask.Geom.from_file(StringIO(''.join(sys.stdin.read())) if name is None else name)
2019-05-30 17:37:49 +05:30
2019-05-30 14:56:47 +05:30
substituted = geom.get_microstructure()
for old,new in zip(sub[0::2],sub[1::2]): substituted[substituted==old] = new # substitute microstructure indices
substituted += options.microstructure # constant shift
2019-05-30 17:37:49 +05:30
damask.util.croak(geom.update(substituted,origin=geom.get_origin()+options.origin))
geom.add_comments(scriptID + ' ' + ' '.join(sys.argv[1:]))
2019-05-27 00:06:41 +05:30
2019-05-25 13:44:53 +05:30
if name is None:
sys.stdout.write(str(geom.show()))
else:
geom.to_file(name)