2011-10-11 23:05:53 +05:30
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: UTF-8 no BOM -*-
|
|
|
|
|
2014-11-18 13:30:45 +05:30
|
|
|
import os,sys,string,math
|
|
|
|
import numpy as np
|
2013-11-04 19:42:10 +05:30
|
|
|
from scipy import ndimage
|
2014-11-18 13:30:45 +05:30
|
|
|
from optparse import OptionParser
|
|
|
|
import damask
|
2011-10-11 23:05:53 +05:30
|
|
|
|
2014-11-17 03:14:46 +05:30
|
|
|
scriptID = string.replace('$Id$','\n','\\n')
|
2014-11-18 21:01:39 +05:30
|
|
|
scriptName = os.path.splitext(scriptID.split()[1])[0]
|
2013-07-18 18:58:54 +05:30
|
|
|
|
2013-05-14 23:21:53 +05:30
|
|
|
#--------------------------------------------------------------------------------------------------
|
|
|
|
# MAIN
|
|
|
|
#--------------------------------------------------------------------------------------------------
|
2011-10-11 23:05:53 +05:30
|
|
|
|
2014-11-18 13:30:45 +05:30
|
|
|
parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [file[s]]', description = """
|
2011-10-11 23:05:53 +05:30
|
|
|
Offset microstructure index for points which see a microstructure different from themselves within a given (cubic) vicinity,
|
|
|
|
i.e. within the region close to a grain/phase boundary.
|
2014-11-18 13:30:45 +05:30
|
|
|
|
|
|
|
""", version = scriptID)
|
2011-10-11 23:05:53 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
parser.add_option('-v', '--vicinity',
|
|
|
|
dest = 'vicinity',
|
|
|
|
type = 'int', metavar = 'int',
|
|
|
|
help = 'voxel distance checked for presence of other microstructure [%default]')
|
|
|
|
parser.add_option('-m', '--microstructureoffset',
|
|
|
|
dest='offset',
|
|
|
|
type = 'int', metavar = 'int',
|
|
|
|
help = 'offset (positive or negative) for tagged microstructure indices. '+
|
|
|
|
'"0" selects maximum microstructure index [%default]')
|
2011-10-11 23:05:53 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
parser.set_defaults(vicinity = 1,
|
|
|
|
offset = 0,
|
|
|
|
)
|
2011-10-11 23:05:53 +05:30
|
|
|
|
|
|
|
(options, filenames) = parser.parse_args()
|
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
# --- loop over input files -------------------------------------------------------------------------
|
|
|
|
|
|
|
|
if filenames == []: filenames = ['STDIN']
|
|
|
|
|
|
|
|
for name in filenames:
|
|
|
|
if not (name == 'STDIN' or os.path.exists(name)): continue
|
|
|
|
table = damask.ASCIItable(name = name, outname = name+'_tmp',
|
|
|
|
buffered = False, labeled = False)
|
|
|
|
table.croak('\033[1m'+scriptName+'\033[0m'+(': '+name if name != 'STDIN' else ''))
|
|
|
|
|
|
|
|
# --- interpret header ----------------------------------------------------------------------------
|
|
|
|
|
2015-03-02 10:36:29 +05:30
|
|
|
table.head_read()
|
2015-08-08 00:33:26 +05:30
|
|
|
info,extra_header = table.head_getGeom()
|
|
|
|
|
|
|
|
table.croak(['grid a b c: %s'%(' x '.join(map(str,info['grid']))),
|
|
|
|
'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 != []:
|
|
|
|
table.croak(errors)
|
|
|
|
table.close(dismiss = True)
|
|
|
|
continue
|
|
|
|
|
|
|
|
# --- read data ------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
microstructure = table.microstructure_read(info['grid']).reshape(info['grid'],order='F') # read microstructure
|
|
|
|
|
|
|
|
# --- do work ------------------------------------------------------------------------------------
|
2011-10-11 23:05:53 +05:30
|
|
|
|
2013-05-14 23:21:53 +05:30
|
|
|
newInfo = {
|
2015-08-08 00:33:26 +05:30
|
|
|
'microstructures': 0,
|
|
|
|
}
|
2013-05-15 02:39:37 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
if options.offset == 0: options.offset = microstructure.max()
|
2011-10-11 23:05:53 +05:30
|
|
|
|
2015-03-02 10:36:29 +05:30
|
|
|
microstructure = np.where(ndimage.filters.maximum_filter(microstructure,size=1+2*options.vicinity,mode='wrap') ==
|
2015-08-08 00:33:26 +05:30
|
|
|
ndimage.filters.minimum_filter(microstructure,size=1+2*options.vicinity,mode='wrap'),
|
|
|
|
microstructure, microstructure + options.offset)
|
2011-10-11 23:05:53 +05:30
|
|
|
|
2013-05-14 23:21:53 +05:30
|
|
|
newInfo['microstructures'] = microstructure.max()
|
2013-04-25 22:21:32 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
# --- report ---------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
remarks = []
|
|
|
|
if ( newInfo['microstructures'] != info['microstructures']): remarks.append('--> microstructures: %i'%newInfo['microstructures'])
|
2015-08-18 10:03:45 +05:30
|
|
|
if remarks != []: table.croak(remarks)
|
2015-08-08 00:33:26 +05:30
|
|
|
|
|
|
|
# --- write header ---------------------------------------------------------------------------------
|
|
|
|
|
2015-03-02 10:36:29 +05:30
|
|
|
table.labels_clear()
|
|
|
|
table.info_clear()
|
|
|
|
table.info_append(extra_header+[
|
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=info['origin']),
|
|
|
|
"homogenization\t{homog}".format(homog=info['homogenization']),
|
|
|
|
"microstructures\t{microstructures}".format(microstructures=newInfo['microstructures']),
|
2013-11-04 19:42:10 +05:30
|
|
|
])
|
2015-03-02 10:36:29 +05:30
|
|
|
table.head_write()
|
|
|
|
table.output_flush()
|
2013-11-04 19:42:10 +05:30
|
|
|
|
2013-05-14 23:21:53 +05:30
|
|
|
# --- write microstructure information ------------------------------------------------------------
|
2015-08-08 00:33:26 +05:30
|
|
|
|
2013-11-04 19:42:10 +05:30
|
|
|
formatwidth = int(math.floor(math.log10(microstructure.max())+1))
|
2015-03-02 10:36:29 +05:30
|
|
|
table.data = microstructure.reshape((info['grid'][0],info['grid'][1]*info['grid'][2]),order='F').transpose()
|
2015-08-08 00:33:26 +05:30
|
|
|
table.data_writeArray('%%%ii'%(formatwidth),delimiter = ' ')
|
2011-10-11 23:05:53 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
# --- output finalization --------------------------------------------------------------------------
|
2013-11-04 19:42:10 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
table.close() # close ASCII table
|
|
|
|
if name != 'STDIN': os.rename(name+'_tmp',name) # overwrite old one with tmp new
|