2016-07-18 23:05:35 +05:30
|
|
|
#!/usr/bin/env python2.7
|
2012-09-13 15:42:00 +05:30
|
|
|
# -*- coding: UTF-8 no BOM -*-
|
|
|
|
|
2016-03-02 02:53:30 +05:30
|
|
|
import os,sys,math
|
2014-11-18 13:30:45 +05:30
|
|
|
import numpy as np
|
2013-06-30 06:07:03 +05:30
|
|
|
import damask
|
2014-11-18 13:30:45 +05:30
|
|
|
from optparse import OptionParser
|
2012-09-13 15:42:00 +05:30
|
|
|
|
2016-01-27 22:36:00 +05:30
|
|
|
scriptName = os.path.splitext(os.path.basename(__file__))[0]
|
|
|
|
scriptID = ' '.join([scriptName,damask.version])
|
2013-07-10 14:45:42 +05:30
|
|
|
|
2013-05-13 16:57:59 +05:30
|
|
|
#--------------------------------------------------------------------------------------------------
|
|
|
|
# MAIN
|
|
|
|
#--------------------------------------------------------------------------------------------------
|
2012-09-13 15:42:00 +05:30
|
|
|
|
2014-11-18 13:30:45 +05:30
|
|
|
parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [file[s]]', description = """
|
2012-09-13 15:42:00 +05:30
|
|
|
translate microstructure indices (shift or substitute) and/or geometry origin.
|
2014-11-18 13:30:45 +05:30
|
|
|
|
|
|
|
""", version=scriptID)
|
2012-09-13 15:42:00 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
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',
|
|
|
|
help = 'offset from old to new microstructure indices')
|
|
|
|
parser.add_option('-s', '--substitute',
|
|
|
|
dest = 'substitute',
|
|
|
|
action = 'extend', metavar = '<string LIST>',
|
|
|
|
help = 'substitutions of microstructure indices from,to,from,to,...')
|
2016-08-25 01:35:50 +05:30
|
|
|
parser.add_option('--float',
|
|
|
|
dest = 'real',
|
|
|
|
action = 'store_true',
|
|
|
|
help = 'use float input')
|
2015-08-08 00:33:26 +05:30
|
|
|
|
|
|
|
parser.set_defaults(origin = (0.0,0.0,0.0),
|
|
|
|
microstructure = 0,
|
|
|
|
substitute = [],
|
2016-08-25 01:35:50 +05:30
|
|
|
real = False,
|
2015-08-08 00:33:26 +05:30
|
|
|
)
|
2012-09-13 15:42:00 +05:30
|
|
|
|
|
|
|
(options, filenames) = parser.parse_args()
|
|
|
|
|
2016-08-25 01:35:50 +05:30
|
|
|
datatype = 'f' if options.real else 'i'
|
|
|
|
|
2012-09-13 15:42:00 +05:30
|
|
|
sub = {}
|
2016-10-25 00:46:29 +05:30
|
|
|
for i in range(len(options.substitute)/2): # split substitution list into "from" -> "to"
|
2012-09-13 15:42:00 +05:30
|
|
|
sub[int(options.substitute[i*2])] = int(options.substitute[i*2+1])
|
|
|
|
|
2016-08-25 01:35:50 +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]
|
2015-08-08 00:33:26 +05:30
|
|
|
|
|
|
|
for name in filenames:
|
2016-08-25 01:35:50 +05:30
|
|
|
try: table = damask.ASCIItable(name = name,
|
|
|
|
buffered = False,
|
|
|
|
labeled = False)
|
2015-08-21 01:12:05 +05:30
|
|
|
except: continue
|
2015-09-24 22:22:58 +05:30
|
|
|
damask.util.report(scriptName,name)
|
2015-08-08 00:33:26 +05:30
|
|
|
|
2016-08-25 01:35:50 +05:30
|
|
|
# --- interpret header ---------------------------------------------------------------------------
|
2015-08-08 00:33:26 +05:30
|
|
|
|
|
|
|
table.head_read()
|
|
|
|
info,extra_header = table.head_getGeom()
|
2015-08-26 22:09:33 +05:30
|
|
|
|
2015-09-24 22:22:58 +05:30
|
|
|
damask.util.croak(['grid a b c: %s'%(' x '.join(map(str,info['grid']))),
|
2015-08-08 00:33:26 +05:30
|
|
|
'size x y z: %s'%(' x '.join(map(str,info['size']))),
|
|
|
|
'origin x y z: %s'%(' : '.join(map(str,info['origin']))),
|
|
|
|
'homogenization: %i'%info['homogenization'],
|
|
|
|
'microstructures: %i'%info['microstructures'],
|
|
|
|
])
|
|
|
|
|
|
|
|
errors = []
|
|
|
|
if np.any(info['grid'] < 1): errors.append('invalid grid a b c.')
|
|
|
|
if np.any(info['size'] <= 0.0): errors.append('invalid size x y z.')
|
|
|
|
if errors != []:
|
2015-09-24 22:22:58 +05:30
|
|
|
damask.util.croak(errors)
|
2015-08-08 00:33:26 +05:30
|
|
|
table.close(dismiss = True)
|
2013-06-30 06:07:03 +05:30
|
|
|
continue
|
2012-09-13 15:42:00 +05:30
|
|
|
|
2016-08-25 01:35:50 +05:30
|
|
|
# --- read data ----------------------------------------------------------------------------------
|
2015-08-08 00:33:26 +05:30
|
|
|
|
2016-10-25 00:46:29 +05:30
|
|
|
microstructure = table.microstructure_read(info['grid'],datatype) # read microstructure
|
2015-08-08 00:33:26 +05:30
|
|
|
|
|
|
|
# --- do work ------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
newInfo = {
|
|
|
|
'origin': np.zeros(3,'d'),
|
|
|
|
'microstructures': 0,
|
|
|
|
}
|
|
|
|
|
2014-11-18 13:30:45 +05:30
|
|
|
substituted = np.copy(microstructure)
|
2015-08-08 00:33:26 +05:30
|
|
|
for k, v in sub.iteritems(): substituted[microstructure==k] = v # substitute microstructure indices
|
2013-06-30 06:07:03 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
substituted += options.microstructure # shift microstructure indices
|
2013-05-13 16:57:59 +05:30
|
|
|
|
|
|
|
newInfo['origin'] = info['origin'] + options.origin
|
2016-08-25 01:35:50 +05:30
|
|
|
newInfo['microstructures'] = len(np.unique(substituted))
|
2013-05-13 16:57:59 +05:30
|
|
|
|
2016-08-25 01:35:50 +05:30
|
|
|
# --- report -------------------------------------------------------------------------------------
|
2013-05-15 02:39:37 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
remarks = []
|
2016-03-02 02:53:30 +05:30
|
|
|
if (any(newInfo['origin'] != info['origin'])):
|
|
|
|
remarks.append('--> origin x y z: %s'%(' : '.join(map(str,newInfo['origin']))))
|
|
|
|
if ( newInfo['microstructures'] != info['microstructures']):
|
|
|
|
remarks.append('--> microstructures: %i'%newInfo['microstructures'])
|
2015-09-24 22:22:58 +05:30
|
|
|
if remarks != []: damask.util.croak(remarks)
|
2015-08-08 00:33:26 +05:30
|
|
|
|
2016-08-25 01:35:50 +05:30
|
|
|
# --- write header -------------------------------------------------------------------------------
|
2015-08-08 00:33:26 +05:30
|
|
|
|
|
|
|
table.labels_clear()
|
|
|
|
table.info_clear()
|
2015-09-24 22:22:58 +05:30
|
|
|
table.info_append([
|
2014-01-20 20:11:56 +05:30
|
|
|
scriptID + ' ' + ' '.join(sys.argv[1:]),
|
2015-08-08 00:33:26 +05:30
|
|
|
"grid\ta {grid[0]}\tb {grid[1]}\tc {grid[2]}".format(grid=info['grid']),
|
|
|
|
"size\tx {size[0]}\ty {size[1]}\tz {size[2]}".format(size=info['size']),
|
|
|
|
"origin\tx {origin[0]}\ty {origin[1]}\tz {origin[2]}".format(origin=newInfo['origin']),
|
|
|
|
"homogenization\t{homog}".format(homog=info['homogenization']),
|
|
|
|
"microstructures\t{microstructures}".format(microstructures=newInfo['microstructures']),
|
2015-09-24 22:22:58 +05:30
|
|
|
extra_header
|
2013-06-30 06:07:03 +05:30
|
|
|
])
|
2015-08-08 00:33:26 +05:30
|
|
|
table.head_write()
|
2015-08-26 22:09:33 +05:30
|
|
|
|
2016-08-25 01:35:50 +05:30
|
|
|
# --- write microstructure information -----------------------------------------------------------
|
2015-08-08 00:33:26 +05:30
|
|
|
|
2016-08-25 01:35:50 +05:30
|
|
|
format = '%g' if options.real else '%{}i'.format(int(math.floor(math.log10(microstructure.max())+1)))
|
2015-08-26 22:09:33 +05:30
|
|
|
table.data = substituted.reshape((info['grid'][0],info['grid'][1]*info['grid'][2]),order='F').transpose()
|
2016-08-25 01:35:50 +05:30
|
|
|
table.data_writeArray(format,delimiter = ' ')
|
2015-08-26 22:09:33 +05:30
|
|
|
|
2016-08-25 01:35:50 +05:30
|
|
|
# --- output finalization ------------------------------------------------------------------------
|
2015-08-08 00:33:26 +05:30
|
|
|
|
2016-08-25 01:35:50 +05:30
|
|
|
table.close() # close ASCII table
|