2014-05-26 20:27:39 +05:30
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: UTF-8 no BOM -*-
|
|
|
|
|
2016-03-02 17:13:09 +05:30
|
|
|
import os,itertools
|
2014-10-13 15:24:01 +05:30
|
|
|
import numpy as np
|
|
|
|
from optparse import OptionParser
|
|
|
|
import damask
|
2014-05-26 20:27:39 +05:30
|
|
|
|
2016-01-27 22:36:00 +05:30
|
|
|
scriptName = os.path.splitext(os.path.basename(__file__))[0]
|
|
|
|
scriptID = ' '.join([scriptName,damask.version])
|
2014-05-26 20:27:39 +05:30
|
|
|
|
|
|
|
#--------------------------------------------------------------------------------------------------
|
|
|
|
# MAIN
|
|
|
|
#--------------------------------------------------------------------------------------------------
|
|
|
|
|
2014-10-13 15:24:01 +05:30
|
|
|
parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [file[s]]', description = """
|
2014-05-26 20:27:39 +05:30
|
|
|
Create seed file by taking microstructure indices from given ASCIItable column.
|
|
|
|
White and black-listing of microstructure indices is possible.
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
--white 1,2,5 --index grainID isolates grainID entries of value 1, 2, and 5;
|
|
|
|
--black 1 --index grainID takes all grainID entries except for value 1.
|
|
|
|
|
2014-10-13 15:24:01 +05:30
|
|
|
""", version = scriptID)
|
2014-05-26 20:27:39 +05:30
|
|
|
|
2016-04-24 21:52:54 +05:30
|
|
|
parser.add_option('-p',
|
|
|
|
'--pos', '--seedposition',
|
|
|
|
dest = 'pos',
|
|
|
|
type = 'string', metavar = 'string',
|
|
|
|
help = 'label of coordinates [%default]')
|
2015-08-08 00:33:26 +05:30
|
|
|
parser.add_option('--boundingbox',
|
|
|
|
dest = 'box',
|
|
|
|
type = 'float', nargs = 6, metavar = ' '.join(['float']*6),
|
|
|
|
help = 'min (x,y,z) and max (x,y,z) coordinates of bounding box [tight]')
|
2016-04-24 21:52:54 +05:30
|
|
|
parser.add_option('-m',
|
|
|
|
'--microstructure',
|
|
|
|
dest = 'microstructure',
|
|
|
|
type = 'string', metavar = 'string',
|
|
|
|
help = 'label of microstructures [%default]')
|
|
|
|
parser.add_option('--weight',
|
|
|
|
dest = 'weight',
|
2015-08-08 00:33:26 +05:30
|
|
|
type = 'string', metavar = 'string',
|
2016-04-24 21:52:54 +05:30
|
|
|
help = 'label of weights [%default]')
|
|
|
|
parser.add_option('-w',
|
|
|
|
'--white',
|
2015-08-08 00:33:26 +05:30
|
|
|
dest = 'whitelist',
|
|
|
|
action = 'extend', metavar = '<int LIST>',
|
|
|
|
help = 'whitelist of microstructure indices')
|
2016-04-24 21:52:54 +05:30
|
|
|
parser.add_option('-b',
|
|
|
|
'--black',
|
2015-08-08 00:33:26 +05:30
|
|
|
dest = 'blacklist',
|
|
|
|
action = 'extend', metavar = '<int LIST>',
|
|
|
|
help = 'blacklist of microstructure indices')
|
|
|
|
|
2016-04-24 21:52:54 +05:30
|
|
|
parser.set_defaults(pos = 'pos',
|
|
|
|
microstructure = 'microstructure',
|
|
|
|
weight = None,
|
2015-06-16 11:12:11 +05:30
|
|
|
)
|
2014-05-26 20:27:39 +05:30
|
|
|
|
|
|
|
(options,filenames) = parser.parse_args()
|
|
|
|
|
2016-03-02 17:13:09 +05:30
|
|
|
if options.whitelist is not None: options.whitelist = map(int,options.whitelist)
|
|
|
|
if options.blacklist is not None: options.blacklist = map(int,options.blacklist)
|
2014-05-26 20:27:39 +05:30
|
|
|
|
2015-06-16 11:12:11 +05:30
|
|
|
# --- loop over input files -------------------------------------------------------------------------
|
|
|
|
|
2015-08-21 01:12:05 +05:30
|
|
|
if filenames == []: filenames = [None]
|
2015-06-16 11:12:11 +05:30
|
|
|
|
|
|
|
for name in filenames:
|
2016-04-24 21:52:54 +05:30
|
|
|
try: table = damask.ASCIItable(name = name,
|
|
|
|
outname = os.path.splitext(name)[0]+'.seeds' if name else name,
|
|
|
|
buffered = False)
|
2015-08-13 02:58:07 +05:30
|
|
|
except: continue
|
2015-09-24 18:51:44 +05:30
|
|
|
damask.util.report(scriptName,name)
|
2015-08-08 00:33:26 +05:30
|
|
|
|
2015-06-16 11:12:11 +05:30
|
|
|
table.head_read() # read ASCII header info
|
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
# ------------------------------------------ sanity checks ---------------------------------------
|
|
|
|
|
2016-04-24 21:52:54 +05:30
|
|
|
missing_labels = table.data_readArray([options.pos,options.microstructure] +
|
|
|
|
([options.weight] if options.weight else []))
|
2014-05-26 20:27:39 +05:30
|
|
|
|
2015-06-16 11:12:11 +05:30
|
|
|
errors = []
|
|
|
|
if len(missing_labels) > 0:
|
2015-08-08 00:33:26 +05:30
|
|
|
errors.append('column{} {} not found'.format('s' if len(missing_labels) > 1 else '',
|
2015-06-16 11:12:11 +05:30
|
|
|
', '.join(missing_labels)))
|
2016-04-24 21:52:54 +05:30
|
|
|
input = {options.pos: 3,
|
|
|
|
options.microstructure: 1,}
|
|
|
|
if options.weight: input.update({options.weight: 1})
|
|
|
|
for label, dim in input.iteritems():
|
2015-06-16 11:12:11 +05:30
|
|
|
if table.label_dimension(label) != dim:
|
2015-08-08 00:33:26 +05:30
|
|
|
errors.append('column {} has wrong dimension'.format(label))
|
2015-06-16 11:12:11 +05:30
|
|
|
|
|
|
|
if errors != []:
|
2015-09-24 18:51:44 +05:30
|
|
|
damask.util.croak(errors)
|
2015-06-16 11:12:11 +05:30
|
|
|
table.close(dismiss = True) # close ASCII table file handles and delete output file
|
|
|
|
continue
|
2015-08-08 00:33:26 +05:30
|
|
|
|
|
|
|
# ------------------------------------------ process data ------------------------------------------
|
2015-06-16 11:12:11 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
# --- finding bounding box -------------------------------------------------------------------------
|
|
|
|
|
2014-10-13 15:24:01 +05:30
|
|
|
boundingBox = np.array((np.amin(table.data[:,0:3],axis = 0),np.amax(table.data[:,0:3],axis = 0)))
|
2015-05-18 14:12:14 +05:30
|
|
|
if options.box:
|
2014-10-13 15:24:01 +05:30
|
|
|
boundingBox[0,:] = np.minimum(options.box[0:3],boundingBox[0,:])
|
|
|
|
boundingBox[1,:] = np.maximum(options.box[3:6],boundingBox[1,:])
|
2014-05-26 20:27:39 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
# --- rescaling coordinates ------------------------------------------------------------------------
|
|
|
|
|
2014-10-13 15:24:01 +05:30
|
|
|
table.data[:,0:3] -= boundingBox[0,:]
|
|
|
|
table.data[:,0:3] /= boundingBox[1,:]-boundingBox[0,:]
|
2014-05-26 20:27:39 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
# --- filtering of grain voxels --------------------------------------------------------------------
|
|
|
|
|
2016-03-02 17:13:09 +05:30
|
|
|
mask = np.logical_and(
|
|
|
|
np.ones_like(table.data[:,3],bool) if options.whitelist is None \
|
|
|
|
else np.in1d(table.data[:,3].ravel(), options.whitelist).reshape(table.data[:,3].shape),
|
|
|
|
np.ones_like(table.data[:,3],bool) if options.blacklist is None \
|
|
|
|
else np.invert(np.in1d(table.data[:,3].ravel(), options.blacklist).reshape(table.data[:,3].shape))
|
2014-05-26 20:27:39 +05:30
|
|
|
)
|
2014-10-13 15:24:01 +05:30
|
|
|
table.data = table.data[mask]
|
2014-05-26 20:27:39 +05:30
|
|
|
|
|
|
|
# ------------------------------------------ assemble header ---------------------------------------
|
|
|
|
|
2014-10-13 15:24:01 +05:30
|
|
|
table.info = [
|
2015-06-16 11:12:11 +05:30
|
|
|
scriptID,
|
2016-04-24 21:52:54 +05:30
|
|
|
'size {}'.format(' '.join(list(itertools.chain.from_iterable(zip(['x','y','z'],
|
2015-06-16 11:12:11 +05:30
|
|
|
map(str,boundingBox[1,:]-boundingBox[0,:])))))),
|
|
|
|
]
|
2014-10-13 15:24:01 +05:30
|
|
|
table.labels_clear()
|
2016-04-24 21:52:54 +05:30
|
|
|
table.labels_append(['1_pos','2_pos','3_pos','microstructure'] +
|
|
|
|
['weight'] if options.weight else []) # implicitly switching label processing/writing on
|
2014-10-13 15:24:01 +05:30
|
|
|
table.head_write()
|
2014-05-26 20:27:39 +05:30
|
|
|
|
2015-08-13 02:58:07 +05:30
|
|
|
# ------------------------------------------ output result ---------------------------------------
|
|
|
|
|
2014-10-13 15:24:01 +05:30
|
|
|
table.data_writeArray()
|
2015-06-16 11:12:11 +05:30
|
|
|
table.close() # close ASCII tables
|