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
|
|
|
|
#--------------------------------------------------------------------------------------------------
|
|
|
|
identifiers = {
|
|
|
|
'grid': ['a','b','c'],
|
|
|
|
'size': ['x','y','z'],
|
|
|
|
'origin': ['x','y','z'],
|
|
|
|
}
|
|
|
|
mappings = {
|
|
|
|
'grid': lambda x: int(x),
|
|
|
|
'size': lambda x: float(x),
|
|
|
|
'origin': lambda x: float(x),
|
|
|
|
'homogenization': lambda x: int(x),
|
|
|
|
'microstructures': lambda x: int(x),
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2014-06-07 23:44:30 +05:30
|
|
|
parser.add_option('-p', '--positions', dest = 'pos', type = 'string',
|
2014-05-26 20:27:39 +05:30
|
|
|
help = 'coordinate label')
|
2014-06-07 23:44:30 +05:30
|
|
|
parser.add_option('--boundingbox', dest = 'box', type = 'float', nargs = 6,
|
|
|
|
help = 'min (x,y,z) and max (x,y,z) to specify bounding box [auto]')
|
|
|
|
parser.add_option('-i', '--index', dest = 'index', type = 'string',
|
2014-05-26 20:27:39 +05:30
|
|
|
help = 'microstructure index label')
|
2014-06-07 23:44:30 +05:30
|
|
|
parser.add_option('-w','--white', dest = 'whitelist', action = 'extend', type = 'string', \
|
|
|
|
help = 'white list of microstructure indices', metavar = '<LIST>')
|
|
|
|
parser.add_option('-b','--black', dest = 'blacklist', action = 'extend', type = 'string', \
|
|
|
|
help = 'black list of microstructure indices', metavar = '<LIST>')
|
2014-05-26 20:27:39 +05:30
|
|
|
|
|
|
|
parser.set_defaults(pos = 'pos')
|
|
|
|
parser.set_defaults(index = 'microstructure')
|
2014-10-13 15:24:01 +05:30
|
|
|
parser.set_defaults(box = ())
|
2014-05-26 20:27:39 +05:30
|
|
|
parser.set_defaults(whitelist = [])
|
|
|
|
parser.set_defaults(blacklist = [])
|
|
|
|
|
|
|
|
(options,filenames) = parser.parse_args()
|
|
|
|
|
|
|
|
datainfo = { # list of requested labels per datatype
|
|
|
|
'scalar': {'len':1,
|
|
|
|
'label':[]},
|
|
|
|
'vector': {'len':3,
|
|
|
|
'label':[]},
|
|
|
|
}
|
|
|
|
|
|
|
|
if options.pos != None: datainfo['vector']['label'] += [options.pos]
|
|
|
|
if options.index != None: datainfo['scalar']['label'] += [options.index]
|
|
|
|
options.whitelist = map(int,options.whitelist)
|
|
|
|
options.blacklist = map(int,options.blacklist)
|
|
|
|
|
|
|
|
#--- setup file handles --------------------------------------------------------------------------
|
|
|
|
files = []
|
|
|
|
if filenames == []:
|
|
|
|
files.append({'name':'STDIN',
|
|
|
|
'input':sys.stdin,
|
|
|
|
'output':sys.stdout,
|
|
|
|
'croak':sys.stderr,
|
|
|
|
})
|
|
|
|
else:
|
|
|
|
for name in filenames:
|
|
|
|
if os.path.exists(name):
|
|
|
|
files.append({'name':name,
|
|
|
|
'input':open(name),
|
|
|
|
'output':open(os.path.splitext(name)[0]+'.seeds','w'),
|
|
|
|
'croak':sys.stdout,
|
|
|
|
})
|
|
|
|
|
|
|
|
#--- loop over input files ------------------------------------------------------------------------
|
|
|
|
for file in files:
|
2014-11-18 21:01:39 +05:30
|
|
|
file['croak'].write('\033[1m' + scriptName + '\033[0m: ' + (file['name'] if file['name'] != 'STDIN' else '') + '\n')
|
2014-05-26 20:27:39 +05:30
|
|
|
|
2014-10-13 15:24:01 +05:30
|
|
|
table = damask.ASCIItable(file['input'],file['output'],buffered = False)
|
|
|
|
table.head_read()
|
2014-05-26 20:27:39 +05:30
|
|
|
|
|
|
|
# --------------- figure out columns to process
|
2014-06-07 23:44:30 +05:30
|
|
|
active = defaultdict(list)
|
|
|
|
column = defaultdict(dict)
|
2014-05-26 20:27:39 +05:30
|
|
|
for datatype,info in datainfo.items():
|
|
|
|
for label in info['label']:
|
|
|
|
foundIt = False
|
|
|
|
for key in ['1_'+label,label]:
|
2014-10-13 15:24:01 +05:30
|
|
|
if key in table.labels:
|
2014-05-26 20:27:39 +05:30
|
|
|
foundIt = True
|
|
|
|
active[datatype].append(label)
|
2014-10-13 15:24:01 +05:30
|
|
|
column[datatype][label] = table.labels.index(key) # remember columns of requested data
|
2014-05-26 20:27:39 +05:30
|
|
|
if not foundIt:
|
|
|
|
file['croak'].write('column %s not found...\n'%label)
|
|
|
|
break
|
|
|
|
|
|
|
|
# ------------------------------------------ process data ---------------------------------------
|
|
|
|
|
2014-10-13 15:24:01 +05:30
|
|
|
table.data_readArray(list(itertools.chain.from_iterable(map(lambda x:[x+i for i in range(datainfo['vector']['len'])],
|
2014-05-26 20:27:39 +05:30
|
|
|
[column['vector'][label] for label in active['vector']]))) +
|
|
|
|
[column['scalar'][label] for label in active['scalar']])
|
|
|
|
|
|
|
|
#--- 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)))
|
2014-06-07 23:44:30 +05:30
|
|
|
if len(options.box) == 6:
|
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
|
|
|
|
|
|
|
#--- 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
|
|
|
|
|
|
|
|
|
|
|
#--- filtering of grain voxels ------------------------------------------------------------------------------------
|
2014-10-13 15:24:01 +05:30
|
|
|
mask = np.logical_and(\
|
|
|
|
np.ones_like(table.data[:,3],bool) \
|
2014-05-26 20:27:39 +05:30
|
|
|
if options.whitelist == [] \
|
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) \
|
2014-05-26 20:27:39 +05:30
|
|
|
if options.blacklist == [] \
|
2014-10-13 15:24:01 +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 = [
|
2014-05-26 20:27:39 +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-04-23 00:14:54 +05:30
|
|
|
table.labels_append(['1_coords','2_coords','3_coords','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()
|
|
|
|
table.output_flush()
|
2014-05-26 20:27:39 +05:30
|
|
|
|
2015-04-23 00:14:54 +05:30
|
|
|
table.input_close() # close input ASCII table
|
2014-05-26 20:27:39 +05:30
|
|
|
if file['name'] != 'STDIN':
|
2015-04-23 00:14:54 +05:30
|
|
|
table.output_close() # close output ASCII table
|