now based on damask.ASCIItable object

continue with next if one file is broken (used to stop!)
improved index substitution and adopted more numpy functions --> notably faster
This commit is contained in:
Philip Eisenlohr 2013-06-30 00:37:03 +00:00
parent f204c38d4d
commit fcc2736431
1 changed files with 51 additions and 55 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
#-------------------------------------------------------------------------------------------------- #--------------------------------------------------------------------------------------------------
@ -83,19 +84,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'),
@ -104,12 +96,14 @@ for file in files:
'homogenization': 0 'homogenization': 0
} }
newInfo = { newInfo = {
'origin': numpy.zeros(3,'d'), 'origin': numpy.zeros(3,'d'),
'microstructures': 0, 'microstructures': 0,
} }
new_header = [] extra_header = []
for header in headers:
for header in theTable.info:
headitems = map(str.lower,header.split()) headitems = map(str.lower,header.split())
if len(headitems) == 0: continue
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():
@ -120,8 +114,7 @@ for file in files:
else: else:
info[headitems[0]] = mappings[headitems[0]](headitems[1]) info[headitems[0]] = mappings[headitems[0]](headitems[1])
else: else:
new_header.append(header) 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']))) + \
@ -131,63 +124,66 @@ 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
#--- process input --------------------------------------------------------------------------------
N = info['grid'].prod()
microstructure = numpy.zeros(N,'i')
#--- read data ------------------------------------------------------------------------------------
microstructure = numpy.zeros(info['grid'].prod(),'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)
s = len(items) s = len(items)
microstructure[i:i+s] = items # read microstructure indices microstructure[i:i+s] = items
i += s i += s
for i in xrange(N): #--- do work ------------------------------------------------------------------------------------
if microstructure[i] in sub: microstructure[i] = sub[microstructure[i]] # substitute microstructure indices substituted = numpy.copy(microstructure)
for k, v in sub.iteritems(): substituted[microstructure==k] = v # substitute microstructure indices
# for i in xrange(N):
# if microstructure[i] in sub: microstructure[i] = sub[microstructure[i]] # substitute microstructure indices
microstructure += options.microstructure # shift microstructure indices substituted += options.microstructure # shift microstructure indices
formatwidth = int(math.floor(math.log10(microstructure.max())+1))
#--- assemble header and report changes -----------------------------------------------------------
newInfo['origin'] = info['origin'] + options.origin newInfo['origin'] = info['origin'] + options.origin
newInfo['microstructures'] = microstructure.max() newInfo['microstructures'] = substituted.max()
#--- report ---------------------------------------------------------------------------------------
if (any(newInfo['origin'] != info['origin'])): if (any(newInfo['origin'] != info['origin'])):
file['croak'].write('--> origin x y z: %s\n'%(' : '.join(map(str,newInfo['origin'])))) file['croak'].write('--> origin x y z: %s\n'%(' : '.join(map(str,newInfo['origin']))))
if (newInfo['microstructures'] != info['microstructures']): if (newInfo['microstructures'] != info['microstructures']):
file['croak'].write('--> microstructures: %i\n'%newInfo['microstructures']) file['croak'].write('--> microstructures: %i\n'%newInfo['microstructures'])
new_header.append('$Id$\n') #--- write header ---------------------------------------------------------------------------------
new_header.append("grid\ta %i\tb %i\tc %i\n"%(info['grid'][0],info['grid'][1],info['grid'][2],)) theTable.labels_clear()
new_header.append("size\tx %f\ty %f\tz %f\n"%(info['size'][0],info['size'][1],info['size'][2],)) theTable.info_clear()
new_header.append("origin\tx %f\ty %f\tz %f\n"%( theTable.info_append(extra_header+[
newInfo['origin'][0],newInfo['origin'][1],newInfo['origin'][2],)) "$Id$",
new_header.append("microstructures\t%i\n"%newInfo['microstructures']) "grid\ta %i\tb %i\tc %i"%(info['grid'][0],info['grid'][1],info['grid'][2],),
new_header.append("homogenization\t%i\n"%info['homogenization']) "size\tx %f\ty %f\tz %f"%(info['size'][0],info['size'][1],info['size'][2],),
file['output'].write('%i\theader\n'%(len(new_header))+''.join(new_header)) "origin\tx %f\ty %f\tz %f"%(newInfo['origin'][0],newInfo['origin'][1],newInfo['origin'][2],),
"microstructures\t%i"%(newInfo['microstructures']),
#--- write new data ------------------------------------------------------------------------------- "homogenization\t%i"%info['homogenization'],
i = 0 ])
for z in xrange(info['grid'][2]): theTable.head_write()
for y in xrange(info['grid'][1]): theTable.output_flush()
output = {True:' ',False:'\n'}[options.twoD].join(map(lambda x: ('%%%ii'%formatwidth)%x,
microstructure[i:i+info['grid'][0]])) + '\n' # --- write microstructure information ------------------------------------------------------------
file['output'].write(output) formatwidth = int(math.floor(math.log10(substituted.max())+1))
i += info['grid'][0] theTable.data = substituted.reshape((info['grid'][0],info['grid'][1]*info['grid'][2]),order='F').transpose()
theTable.data_writeArray('%%%ii'%(formatwidth))
#--- 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'])