now based on damask.ASCIItable object

continue with next if one file is broken (used to stop!)
switched to numpy functions for geom reshaping and writing --> 10x faster
This commit is contained in:
Philip Eisenlohr 2013-06-30 00:34:16 +00:00
parent 7d6035dcfa
commit f204c38d4d
1 changed files with 52 additions and 56 deletions

View File

@ -2,6 +2,7 @@
# -*- coding: UTF-8 no BOM -*- # -*- coding: UTF-8 no BOM -*-
import os,sys,string,re,math,numpy import os,sys,string,re,math,numpy
import damask
from optparse import OptionParser, OptionGroup, Option, SUPPRESS_HELP from optparse import OptionParser, OptionGroup, Option, SUPPRESS_HELP
#-------------------------------------------------------------------------------------------------- #--------------------------------------------------------------------------------------------------
@ -50,12 +51,9 @@ parser.add_option('-o', '--offset', dest='offset', type='int', nargs = 3, \
help='a,b,c offset from old to new origin of grid %default') help='a,b,c offset from old to new origin of grid %default')
parser.add_option('-f', '--fill', dest='fill', type='int', \ parser.add_option('-f', '--fill', dest='fill', type='int', \
help='(background) canvas grain index. "0" selects maximum microstructure index + 1 [%default]') help='(background) canvas grain index. "0" selects maximum microstructure index + 1 [%default]')
parser.add_option('-2', '--twodimensional', dest='twoD', action='store_true', \
help='output geom file with two-dimensional data arrangement [%default]')
parser.set_defaults(grid = ['0','0','0']) parser.set_defaults(grid = ['0','0','0'])
parser.set_defaults(offset = [0,0,0]) parser.set_defaults(offset = [0,0,0])
parser.set_defaults(twoD = False)
parser.set_defaults(fill = 0) parser.set_defaults(fill = 0)
(options, filenames) = parser.parse_args() (options, filenames) = parser.parse_args()
@ -81,19 +79,10 @@ else:
for file in files: for file in files:
if file['name'] != 'STDIN': file['croak'].write(file['name']+'\n') if file['name'] != 'STDIN': file['croak'].write(file['name']+'\n')
firstline = file['input'].readline() theTable = damask.ASCIItable(file['input'],file['output'],labels=False)
m = re.search('(\d+)\s*head', firstline.lower()) theTable.head_read()
if m:
headerlines = int(m.group(1))
headers = [file['input'].readline() for i in range(headerlines)]
else:
headerlines = 1
headers = firstline
content = file['input'].readlines() #--- interpret header ----------------------------------------------------------------------------
file['input'].close()
#--- interprete header ----------------------------------------------------------------------------
info = { info = {
'grid': numpy.zeros(3,'i'), 'grid': numpy.zeros(3,'i'),
'size': numpy.zeros(3,'d'), 'size': numpy.zeros(3,'d'),
@ -102,13 +91,15 @@ for file in files:
'homogenization': 0, 'homogenization': 0,
} }
newInfo = { newInfo = {
'grid': numpy.zeros(3,'i'),
'origin': numpy.zeros(3,'d'), 'origin': numpy.zeros(3,'d'),
'microstructures': 0, 'microstructures': 0,
} }
extra_header = []
new_header = [] for header in theTable.info:
for header in headers:
headitems = map(str.lower,header.split()) headitems = map(str.lower,header.split())
if len(headitems) == 0: continue # skip blank lines
if headitems[0] == 'resolution': headitems[0] = 'grid' if headitems[0] == 'resolution': headitems[0] = 'grid'
if headitems[0] == 'dimension': headitems[0] = 'size' if headitems[0] == 'dimension': headitems[0] = 'size'
if headitems[0] in mappings.keys(): if headitems[0] in mappings.keys():
@ -118,6 +109,8 @@ for file in files:
mappings[headitems[0]](headitems[headitems.index(identifiers[headitems[0]][i])+1]) mappings[headitems[0]](headitems[headitems.index(identifiers[headitems[0]][i])+1])
else: else:
info[headitems[0]] = mappings[headitems[0]](headitems[1]) info[headitems[0]] = mappings[headitems[0]](headitems[1])
else:
extra_header.append(header)
file['croak'].write('grid a b c: %s\n'%(' x '.join(map(str,info['grid']))) + \ file['croak'].write('grid a b c: %s\n'%(' x '.join(map(str,info['grid']))) + \
'size x y z: %s\n'%(' x '.join(map(str,info['size']))) + \ 'size x y z: %s\n'%(' x '.join(map(str,info['size']))) + \
@ -127,31 +120,32 @@ for file in files:
if numpy.any(info['grid'] < 1): if numpy.any(info['grid'] < 1):
file['croak'].write('invalid grid a b c.\n') file['croak'].write('invalid grid a b c.\n')
sys.exit() continue
if numpy.any(info['size'] <= 0.0): if numpy.any(info['size'] <= 0.0):
file['croak'].write('invalid size x y z.\n') file['croak'].write('invalid size x y z.\n')
sys.exit() continue
newInfo['grid'] = numpy.array([{True:int(o*float(n.translate(None,'xX'))), False: int(n.translate(None,'xX'))}[n[-1].lower() == 'x'] for o,n in zip(info['grid'],options.grid)],'i') #--- read data ------------------------------------------------------------------------------------
newInfo['grid'] = numpy.where(newInfo['grid'] <= 0 , info['grid'],newInfo['grid']) microstructure = numpy.zeros(info['grid'].prod(),'i') # initialize as flat array
#--- read data ------------------------------------------------------------------------------------
microstructure = numpy.zeros(info['grid'],'i')
i = 0 i = 0
for line in content: theTable.data_rewind()
items = line.split() while theTable.data_read():
items = theTable.data
if len(items) > 2: if len(items) > 2:
if items[1].lower() == 'of': items = [int(items[2])]*int(items[0]) if items[1].lower() == 'of': items = [int(items[2])]*int(items[0])
elif items[1].lower() == 'to': items = xrange(int(items[0]),1+int(items[2])) elif items[1].lower() == 'to': items = xrange(int(items[0]),1+int(items[2]))
else: items = map(int,items) else: items = map(int,items)
else: items = map(int,items) else: items = map(int,items)
for item in items: s = len(items)
microstructure[i%info['grid'][0], microstructure[i:i+s] = items
(i/info['grid'][0])%info['grid'][1], i += s
i/info['grid'][0] /info['grid'][1]] = item
i += 1
#--- do work ------------------------------------------------------------------------------------
newInfo['grid'] = numpy.array([{True:int(o*float(n.translate(None,'xX'))), False: int(n.translate(None,'xX'))}[n[-1].lower() == 'x'] for o,n in zip(info['grid'],options.grid)],'i')
newInfo['grid'] = numpy.where(newInfo['grid'] <= 0 , info['grid'],newInfo['grid'])
microstructure = microstructure.reshape(info['grid'],order='F')
microstructure_cropped = numpy.zeros(newInfo['grid'],'i') microstructure_cropped = numpy.zeros(newInfo['grid'],'i')
microstructure_cropped.fill({True:options.fill,False:microstructure.max()+1}[options.fill>0]) microstructure_cropped.fill({True:options.fill,False:microstructure.max()+1}[options.fill>0])
xindex = list(set(xrange(options.offset[0],options.offset[0]+newInfo['grid'][0])) & \ xindex = list(set(xrange(options.offset[0],options.offset[0]+newInfo['grid'][0])) & \
@ -169,11 +163,11 @@ for file in files:
= microstructure[min(xindex):(max(xindex)+1),\ = microstructure[min(xindex):(max(xindex)+1),\
min(yindex):(max(yindex)+1),\ min(yindex):(max(yindex)+1),\
min(zindex):(max(zindex)+1)] min(zindex):(max(zindex)+1)]
formatwidth = int(math.floor(math.log10(microstructure.max())+1))
newInfo['microstructures'] = microstructure_cropped.max()
newInfo['size'] = info['size']/info['grid']*newInfo['grid'] newInfo['size'] = info['size']/info['grid']*newInfo['grid']
newInfo['origin'] = info['origin']+info['size']/info['grid']*options.offset newInfo['origin'] = info['origin']+info['size']/info['grid']*options.offset
newInfo['microstructures'] = microstructure_cropped.max()
#--- report --------------------------------------------------------------------------------------- #--- report ---------------------------------------------------------------------------------------
@ -188,30 +182,32 @@ for file in files:
if numpy.any(newInfo['grid'] < 1): if numpy.any(newInfo['grid'] < 1):
file['croak'].write('invalid new grid a b c.\n') file['croak'].write('invalid new grid a b c.\n')
sys.exit() continue
if numpy.any(newInfo['size'] <= 0.0): if numpy.any(newInfo['size'] <= 0.0):
file['croak'].write('invalid new size x y z.\n') file['croak'].write('invalid new size x y z.\n')
sys.exit() continue
# --- assemble header ----------------------------------------------------------------------------- #--- write header ---------------------------------------------------------------------------------
new_header.append('$Id$\n') theTable.labels_clear()
new_header.append("grid\ta %i\tb %i\tc %i\n"%( theTable.info_clear()
newInfo['grid'][0],newInfo['grid'][1],newInfo['grid'][2])) theTable.info_append(extra_header+[
new_header.append("size\tx %f\ty %f\tz %f\n"%( "$Id$",
newInfo['size'][0],newInfo['size'][1],newInfo['size'][2])) "grid\ta %i\tb %i\tc %i"%(newInfo['grid'][0],newInfo['grid'][1],newInfo['grid'][2],),
new_header.append("origin\tx %f\ty %f\tz %f\n"%( "size\tx %f\ty %f\tz %f"%(newInfo['size'][0],newInfo['size'][1],newInfo['size'][2],),
newInfo['origin'][0],newInfo['origin'][1],newInfo['origin'][2])) "origin\tx %f\ty %f\tz %f"%(newInfo['origin'][0],newInfo['origin'][1],newInfo['origin'][2],),
new_header.append("homogenization\t%i\n"%info['homogenization']) "microstructures\t%i"%(newInfo['microstructures']),
new_header.append("microstructures\t%i\n"%newInfo['microstructures']) "homogenization\t%i"%info['homogenization'],
file['output'].write('%i\theader\n'%(len(new_header))+''.join(new_header)) ])
theTable.head_write()
theTable.output_flush()
# --- write microstructure information ------------------------------------------------------------ # --- write microstructure information ------------------------------------------------------------
for z in xrange(newInfo['grid'][2]): formatwidth = int(math.floor(math.log10(microstructure_cropped.max())+1))
for y in xrange(newInfo['grid'][1]): theTable.data = microstructure_cropped.reshape((newInfo['grid'][0],newInfo['grid'][1]*newInfo['grid'][2]),order='F').transpose()
file['output'].write({True:' ',False:'\n'}[options.twoD].join(map(lambda x: \ theTable.data_writeArray('%%%ii'%(formatwidth))
('%%%ii'%formatwidth)%x, microstructure_cropped[:,y,z])) + '\n')
#--- output finalization -------------------------------------------------------------------------- #--- output finalization --------------------------------------------------------------------------
if file['name'] != 'STDIN': if file['name'] != 'STDIN':
file['input'].close()
file['output'].close() file['output'].close()
os.rename(file['name']+'_tmp',file['name']) os.rename(file['name']+'_tmp',file['name'])