2014-05-26 20:27:39 +05:30
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: UTF-8 no BOM -*-
|
|
|
|
|
2014-10-13 15:24:01 +05:30
|
|
|
import os,sys,string,itertools
|
|
|
|
import numpy as np
|
|
|
|
from optparse import OptionParser
|
2014-06-07 23:44:30 +05:30
|
|
|
from collections import defaultdict
|
2014-10-13 15:24:01 +05:30
|
|
|
import damask
|
2014-05-26 20:27:39 +05:30
|
|
|
|
2014-10-13 15:24:01 +05:30
|
|
|
scriptID = string.replace('$Id$','\n','\\n')
|
2014-11-18 21:01:39 +05:30
|
|
|
scriptName = os.path.splitext(scriptID.split()[1])[0]
|
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
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
parser.add_option('-p', '--positions',
|
|
|
|
dest = 'pos',
|
|
|
|
type = 'string', metavar = 'string',
|
|
|
|
help = 'coordinate label [%default]')
|
|
|
|
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]')
|
|
|
|
parser.add_option('-i', '--index',
|
|
|
|
dest = 'index',
|
|
|
|
type = 'string', metavar = 'string',
|
|
|
|
help = 'microstructure index label [%default]')
|
|
|
|
parser.add_option('-w','--white',
|
|
|
|
dest = 'whitelist',
|
|
|
|
action = 'extend', metavar = '<int LIST>',
|
|
|
|
help = 'whitelist of microstructure indices')
|
|
|
|
parser.add_option('-b','--black',
|
|
|
|
dest = 'blacklist',
|
|
|
|
action = 'extend', metavar = '<int LIST>',
|
|
|
|
help = 'blacklist of microstructure indices')
|
|
|
|
|
2015-06-16 11:12:11 +05:30
|
|
|
parser.set_defaults(pos = 'pos',
|
|
|
|
index ='microstructure',
|
|
|
|
)
|
2014-05-26 20:27:39 +05:30
|
|
|
|
|
|
|
(options,filenames) = parser.parse_args()
|
|
|
|
|
2015-05-14 22:37:50 +05:30
|
|
|
if options.whitelist != None: options.whitelist = map(int,options.whitelist)
|
|
|
|
if options.blacklist != 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-08 00:33:26 +05:30
|
|
|
if filenames == []: filenames = ['STDIN']
|
2015-06-16 11:12:11 +05:30
|
|
|
|
|
|
|
for name in filenames:
|
2015-08-08 00:33:26 +05:30
|
|
|
if not (name == 'STDIN' or os.path.exists(name)): continue
|
|
|
|
table = damask.ASCIItable(name = name, outname = os.path.splitext(name)[0]+'.seeds',
|
|
|
|
buffered = False)
|
|
|
|
table.croak('\033[1m'+scriptName+'\033[0m'+(': '+name if name != 'STDIN' else ''))
|
|
|
|
|
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 ---------------------------------------
|
|
|
|
|
|
|
|
missing_labels = table.data_readArray([options.pos,options.index])
|
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)))
|
|
|
|
for label, dim in {options.pos: 3,
|
2015-08-08 00:33:26 +05:30
|
|
|
options.index: 1}.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-08-08 00:33:26 +05:30
|
|
|
table.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 --------------------------------------------------------------------
|
|
|
|
|
2014-10-13 15:24:01 +05:30
|
|
|
mask = np.logical_and(\
|
|
|
|
np.ones_like(table.data[:,3],bool) \
|
2015-05-14 22:37:50 +05:30
|
|
|
if options.whitelist == None \
|
2014-10-13 15:24:01 +05:30
|
|
|
else np.in1d(table.data[:,3].ravel(), options.whitelist).reshape(table.data[:,3].shape),
|
|
|
|
np.ones_like(table.data[:,3],bool) \
|
2015-05-14 22:37:50 +05:30
|
|
|
if options.blacklist == None \
|
2015-06-16 11:12:11 +05:30
|
|
|
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
|
|
|
|
|
|
|
# ------------------------------------------ output result ---------------------------------------
|
|
|
|
|
|
|
|
# ------------------------------------------ assemble header ---------------------------------------
|
|
|
|
|
2014-10-13 15:24:01 +05:30
|
|
|
table.info = [
|
2015-06-16 11:12:11 +05:30
|
|
|
scriptID,
|
|
|
|
'size %s'%(' '.join(list(itertools.chain.from_iterable(zip(['x','y','z'],
|
|
|
|
map(str,boundingBox[1,:]-boundingBox[0,:])))))),
|
|
|
|
]
|
2014-10-13 15:24:01 +05:30
|
|
|
table.labels_clear()
|
2015-08-08 00:33:26 +05:30
|
|
|
table.labels_append(['1_pos','2_pos','3_pos','microstructure']) # 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
|
|
|
|
2014-10-13 15:24:01 +05:30
|
|
|
table.data_writeArray()
|
2015-06-16 11:12:11 +05:30
|
|
|
table.close() # close ASCII tables
|