now based on damask.ASCIItable object

continue with next if one file is broken (used to stop!)
packing of already packed works now
overall speed improvement
standard unpacked output is 2D: use -1/--onedimensional for linear list
This commit is contained in:
Philip Eisenlohr 2013-06-30 00:39:48 +00:00
parent fcc2736431
commit 77aa1c27b0
2 changed files with 111 additions and 79 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
#-------------------------------------------------------------------------------------------------- #--------------------------------------------------------------------------------------------------
@ -67,30 +68,23 @@ 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()
file['input'].close()
#--- interprete header ---------------------------------------------------------------------------- #--- interpret header ----------------------------------------------------------------------------
info = { info = {
'grid': numpy.zeros(3,'i'), 'grid': numpy.zeros(3,'i'),
'size': numpy.zeros(3,'d'), 'size': numpy.zeros(3,'d'),
'origin': numpy.zeros(3,'d'), 'origin': numpy.zeros(3,'d'),
'microstructures': 0, 'microstructures': 0,
'homogenization': 0, 'homogenization': 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
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():
@ -100,31 +94,52 @@ 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])
new_header.append(header) 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']))) + \
'origin x y z: %s\n'%(' : '.join(map(str,info['origin']))) + \ 'origin x y z: %s\n'%(' : '.join(map(str,info['origin']))) + \
'homogenization: %i\n'%info['homogenization'] + \ 'homogenization: %i\n'%info['homogenization'] + \
'microstructures: %i\n'%info['microstructures']) 'microstructures: %i\n\n'%info['microstructures'])
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
file['output'].write('%i\theader\n'%(len(new_header))+''.join(new_header)) #--- 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"%(info['origin'][0],info['origin'][1],info['origin'][2],),
"microstructures\t%i"%(info['microstructures']),
"homogenization\t%i"%info['homogenization'],
])
theTable.head_write()
theTable.output_flush()
#--- pack input ----------------------------------------------------------------------------------- # --- write packed microstructure information ------------------------------------------------------------
type = '' type = ''
former = -1 former = -1
start = -1 start = -1
reps = 0 reps = 0
for line in content: theTable.data_rewind()
for current in map(int,line.split()): 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)
for current in items:
if current == former+1 and start+reps == former+1: if current == former+1 and start+reps == former+1:
type = 'to' type = 'to'
reps += 1 reps += 1
@ -132,24 +147,30 @@ for file in files:
type = 'of' type = 'of'
reps += 1 reps += 1
else: else:
output = {'': '', theTable.data = {
'.': str(former)+'\n', '' : [],
'to': '%i to %i\n'%(former-reps+1,former), '.' : [str(former)],
'of': '%i of %i\n'%(reps,former), 'to': ['%i to %i'%(former-reps+1,former)],
'of': ['%i of %i'%(reps,former)],
}[type] }[type]
file['output'].write(output) theTable.data_write()
# file['output'].write(output)
type = '.' type = '.'
start = current start = current
reps = 1 reps = 1
former = current former = current
output = {'.': str(former), theTable.data = {
'to': '%i to %i'%(former-reps+1,former), '.' : [str(former)],
'of': '%i of %i'%(reps,former), 'to': ['%i to %i'%(former-reps+1,former)],
'of': ['%i of %i'%(reps,former)],
}[type] }[type]
file['output'].write(output+'\n') theTable.data_write()
theTable.output_flush()
# file['output'].write(output+'\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'])

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
#-------------------------------------------------------------------------------------------------- #--------------------------------------------------------------------------------------------------
@ -44,10 +45,10 @@ Unpack geometry files containing ranges "a to b" and/or "n of x" multiples (excl
""" + string.replace('$Id$','\n','\\n') """ + string.replace('$Id$','\n','\\n')
) )
parser.add_option('-2', '--twodimensional', dest='twoD', action='store_true', \ parser.add_option('-1', '--onedimensional', dest='oneD', action='store_true', \
help='output geom file with two-dimensional data arrangement [%default]') help='output geom file with one-dimensional data arrangement [%default]')
parser.set_defaults(twoD = False) parser.set_defaults(oneD = False)
(options, filenames) = parser.parse_args() (options, filenames) = parser.parse_args()
@ -72,30 +73,23 @@ 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()
file['input'].close()
#--- interprete header ---------------------------------------------------------------------------- #--- interpret header ----------------------------------------------------------------------------
info = { info = {
'grid': numpy.zeros(3,'i'), 'grid': numpy.zeros(3,'i'),
'size': numpy.zeros(3,'d'), 'size': numpy.zeros(3,'d'),
'origin': numpy.zeros(3,'d'), 'origin': numpy.zeros(3,'d'),
'microstructures': 0, 'microstructures': 0,
'homogenization': 0, 'homogenization': 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
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():
@ -105,45 +99,62 @@ 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])
new_header.append(header) else:
extra_header.append(header)
format = {True: info['grid'][0],
False: 1}[options.twoD]
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']))) + \
'origin x y z: %s\n'%(' : '.join(map(str,info['origin']))) + \ 'origin x y z: %s\n'%(' : '.join(map(str,info['origin']))) + \
'homogenization: %i\n'%info['homogenization'] + \ 'homogenization: %i\n'%info['homogenization'] + \
'microstructures: %i\n'%info['microstructures']) 'microstructures: %i\n\n'%info['microstructures'])
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
file['output'].write('%i\theader\n'%(len(new_header))+''.join(new_header)) #--- read data ------------------------------------------------------------------------------------
microstructure = numpy.zeros(info['grid'].prod(),'i')
i = 0
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)
if info['microstructures'] > 0: s = len(items)
digits = 1+int(math.log10(int(info['microstructures']))) microstructure[i:i+s] = items
i += s
#--- 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"%(info['origin'][0],info['origin'][1],info['origin'][2],),
"microstructures\t%i"%(info['microstructures']),
"homogenization\t%i"%info['homogenization'],
])
theTable.head_write()
theTable.output_flush()
# --- write microstructure information ------------------------------------------------------------
formatwidth = int(math.floor(math.log10(microstructure.max())+1))
if options.oneD:
theTable.data = microstructure
else: else:
digits = 1+int(math.log10(int(info['grid'][0]*info['grid'][1]*info['grid'][2]))) theTable.data = microstructure.reshape((info['grid'][0],info['grid'][1]*info['grid'][2]),order='F').transpose()
theTable.data_writeArray('%%%ii'%(formatwidth))
#--- unpack input ---------------------------------------------------------------------------------
wordsWritten = 0
for line in content:
words = line.split()
if len(words) > 2: # any packing keywords?
if words[1].lower() == 'to': words = map(str,range(int(words[0]),int(words[2])+1))
elif words[1].lower() == 'of': words = [words[2]]*int(words[0])
for word in words:
wordsWritten += 1
file['output'].write(word.rjust(digits)+{True:'\n',False:' '}[wordsWritten%format == 0]) # newline every format words
#--- 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'])