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 -*-
import os,sys,string,re,math,numpy
import damask
from optparse import OptionParser, OptionGroup, Option, SUPPRESS_HELP
#--------------------------------------------------------------------------------------------------
@ -83,19 +84,10 @@ else:
for file in files:
if file['name'] != 'STDIN': file['croak'].write(file['name']+'\n')
firstline = file['input'].readline()
m = re.search('(\d+)\s*head', firstline.lower())
if m:
headerlines = int(m.group(1))
headers = [file['input'].readline() for i in range(headerlines)]
else:
headerlines = 1
headers = firstline
theTable = damask.ASCIItable(file['input'],file['output'],labels=False)
theTable.head_read()
content = file['input'].readlines()
file['input'].close()
#--- interprete header ----------------------------------------------------------------------------
#--- interpret header ----------------------------------------------------------------------------
info = {
'grid': numpy.zeros(3,'i'),
'size': numpy.zeros(3,'d'),
@ -104,12 +96,14 @@ for file in files:
'homogenization': 0
}
newInfo = {
'origin': numpy.zeros(3,'d'),
'origin': numpy.zeros(3,'d'),
'microstructures': 0,
}
new_header = []
for header in headers:
extra_header = []
for header in theTable.info:
headitems = map(str.lower,header.split())
if len(headitems) == 0: continue
if headitems[0] == 'resolution': headitems[0] = 'grid'
if headitems[0] == 'dimension': headitems[0] = 'size'
if headitems[0] in mappings.keys():
@ -120,8 +114,7 @@ for file in files:
else:
info[headitems[0]] = mappings[headitems[0]](headitems[1])
else:
new_header.append(header)
extra_header.append(header)
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']))) + \
@ -131,63 +124,66 @@ for file in files:
if numpy.any(info['grid'] < 1):
file['croak'].write('invalid grid a b c.\n')
sys.exit()
continue
if numpy.any(info['size'] <= 0.0):
file['croak'].write('invalid size x y z.\n')
sys.exit()
#--- process input --------------------------------------------------------------------------------
N = info['grid'].prod()
microstructure = numpy.zeros(N,'i')
continue
#--- read data ------------------------------------------------------------------------------------
microstructure = numpy.zeros(info['grid'].prod(),'i')
i = 0
for line in content:
items = line.split()
theTable.data_rewind()
while theTable.data_read():
items = theTable.data
if len(items) > 2:
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]))
else: items = map(int,items)
else: items = map(int,items)
else: items = map(int,items)
else: items = map(int,items)
s = len(items)
microstructure[i:i+s] = items # read microstructure indices
microstructure[i:i+s] = items
i += s
for i in xrange(N):
if microstructure[i] in sub: microstructure[i] = sub[microstructure[i]] # substitute microstructure indices
#--- do work ------------------------------------------------------------------------------------
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
formatwidth = int(math.floor(math.log10(microstructure.max())+1))
substituted += options.microstructure # shift microstructure indices
#--- assemble header and report changes -----------------------------------------------------------
newInfo['origin'] = info['origin'] + options.origin
newInfo['microstructures'] = microstructure.max()
newInfo['microstructures'] = substituted.max()
#--- report ---------------------------------------------------------------------------------------
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']):
file['croak'].write('--> microstructures: %i\n'%newInfo['microstructures'])
new_header.append('$Id$\n')
new_header.append("grid\ta %i\tb %i\tc %i\n"%(info['grid'][0],info['grid'][1],info['grid'][2],))
new_header.append("size\tx %f\ty %f\tz %f\n"%(info['size'][0],info['size'][1],info['size'][2],))
new_header.append("origin\tx %f\ty %f\tz %f\n"%(
newInfo['origin'][0],newInfo['origin'][1],newInfo['origin'][2],))
new_header.append("microstructures\t%i\n"%newInfo['microstructures'])
new_header.append("homogenization\t%i\n"%info['homogenization'])
file['output'].write('%i\theader\n'%(len(new_header))+''.join(new_header))
#--- write new data -------------------------------------------------------------------------------
i = 0
for z in xrange(info['grid'][2]):
for y in xrange(info['grid'][1]):
output = {True:' ',False:'\n'}[options.twoD].join(map(lambda x: ('%%%ii'%formatwidth)%x,
microstructure[i:i+info['grid'][0]])) + '\n'
file['output'].write(output)
i += info['grid'][0]
#--- output finalization --------------------------------------------------------------------------
#--- write header ---------------------------------------------------------------------------------
theTable.labels_clear()
theTable.info_clear()
theTable.info_append(extra_header+[
"$Id$",
"grid\ta %i\tb %i\tc %i"%(info['grid'][0],info['grid'][1],info['grid'][2],),
"size\tx %f\ty %f\tz %f"%(info['size'][0],info['size'][1],info['size'][2],),
"origin\tx %f\ty %f\tz %f"%(newInfo['origin'][0],newInfo['origin'][1],newInfo['origin'][2],),
"microstructures\t%i"%(newInfo['microstructures']),
"homogenization\t%i"%info['homogenization'],
])
theTable.head_write()
theTable.output_flush()
# --- write microstructure information ------------------------------------------------------------
formatwidth = int(math.floor(math.log10(substituted.max())+1))
theTable.data = substituted.reshape((info['grid'][0],info['grid'][1]*info['grid'][2]),order='F').transpose()
theTable.data_writeArray('%%%ii'%(formatwidth))
#--- output finalization --------------------------------------------------------------------------
if file['name'] != 'STDIN':
file['input'].close()
file['output'].close()
os.rename(file['name']+'_tmp',file['name'])