fixed small bug ('theTable' instead of 'table') and simplified
This commit is contained in:
parent
1ba7cbb046
commit
d93c40a3dd
|
@ -9,7 +9,6 @@ import damask
|
||||||
scriptID = '$Id$'
|
scriptID = '$Id$'
|
||||||
scriptName = scriptID.split()[1]
|
scriptName = scriptID.split()[1]
|
||||||
|
|
||||||
|
|
||||||
#--------------------------------------------------------------------------------------------------
|
#--------------------------------------------------------------------------------------------------
|
||||||
# MAIN
|
# MAIN
|
||||||
#--------------------------------------------------------------------------------------------------
|
#--------------------------------------------------------------------------------------------------
|
||||||
|
@ -32,11 +31,12 @@ mappings = {
|
||||||
|
|
||||||
parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [file[s]]', description = """
|
parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [file[s]]', description = """
|
||||||
compress geometry files with ranges "a to b" and/or multiples "n of x".
|
compress geometry files with ranges "a to b" and/or multiples "n of x".
|
||||||
|
|
||||||
""", version = scriptID)
|
""", version = scriptID)
|
||||||
|
|
||||||
(options, filenames) = parser.parse_args()
|
(options, filenames) = parser.parse_args()
|
||||||
|
|
||||||
# ------------------------------------------ setup file handles ---------------------------------------
|
# ------------------------------------------ setup file handles ------------------------------------
|
||||||
files = []
|
files = []
|
||||||
if filenames == []:
|
if filenames == []:
|
||||||
files.append({'name':'STDIN', 'input':sys.stdin, 'output':sys.stdout, 'croak':sys.stderr})
|
files.append({'name':'STDIN', 'input':sys.stdin, 'output':sys.stdout, 'croak':sys.stderr})
|
||||||
|
@ -45,14 +45,13 @@ else:
|
||||||
if os.path.exists(name):
|
if os.path.exists(name):
|
||||||
files.append({'name':name, 'input':open(name), 'output':open(name+'_tmp','w'), 'croak':sys.stderr})
|
files.append({'name':name, 'input':open(name), 'output':open(name+'_tmp','w'), 'croak':sys.stderr})
|
||||||
|
|
||||||
#--- loop over input files ------------------------------------------------------------------------
|
# ------------------------------------------ loop over input files ---------------------------------
|
||||||
for file in files:
|
for file in files:
|
||||||
if file['name'] != 'STDIN': file['croak'].write('\033[1m'+scriptName+'\033[0m: '+file['name']+'\n')
|
if file['name'] != 'STDIN': file['croak'].write('\033[1m'+scriptName+'\033[0m: '+file['name']+'\n')
|
||||||
else: file['croak'].write('\033[1m'+scriptName+'\033[0m\n')
|
else: file['croak'].write('\033[1m'+scriptName+'\033[0m\n')
|
||||||
|
|
||||||
theTable = damask.ASCIItable(file['input'],file['output'],labels = False,buffered = False)
|
table = damask.ASCIItable(file['input'],file['output'],labels = False,buffered = False) # make unbuffered ASCII_table
|
||||||
theTable.head_read()
|
table.head_read() # read ASCII header info
|
||||||
|
|
||||||
|
|
||||||
#--- interpret header ----------------------------------------------------------------------------
|
#--- interpret header ----------------------------------------------------------------------------
|
||||||
info = {
|
info = {
|
||||||
|
@ -64,7 +63,7 @@ for file in files:
|
||||||
}
|
}
|
||||||
extra_header = []
|
extra_header = []
|
||||||
|
|
||||||
for header in theTable.info:
|
for header in table.info:
|
||||||
headitems = map(str.lower,header.split())
|
headitems = map(str.lower,header.split())
|
||||||
if len(headitems) == 0: continue
|
if len(headitems) == 0: continue
|
||||||
for synonym,alternatives in synonyms.iteritems():
|
for synonym,alternatives in synonyms.iteritems():
|
||||||
|
@ -93,9 +92,9 @@ for file in files:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
#--- write header ---------------------------------------------------------------------------------
|
#--- write header ---------------------------------------------------------------------------------
|
||||||
theTable.labels_clear()
|
table.labels_clear()
|
||||||
theTable.info_clear()
|
table.info_clear()
|
||||||
theTable.info_append(extra_header+[
|
table.info_append(extra_header+[
|
||||||
scriptID + ' ' + ' '.join(sys.argv[1:]),
|
scriptID + ' ' + ' '.join(sys.argv[1:]),
|
||||||
"grid\ta %i\tb %i\tc %i"%(info['grid'][0],info['grid'][1],info['grid'][2],),
|
"grid\ta %i\tb %i\tc %i"%(info['grid'][0],info['grid'][1],info['grid'][2],),
|
||||||
"size\tx %e\ty %e\tz %e"%(info['size'][0],info['size'][1],info['size'][2],),
|
"size\tx %e\ty %e\tz %e"%(info['size'][0],info['size'][1],info['size'][2],),
|
||||||
|
@ -103,7 +102,7 @@ for file in files:
|
||||||
"homogenization\t%i"%info['homogenization'],
|
"homogenization\t%i"%info['homogenization'],
|
||||||
"microstructures\t%i"%(info['microstructures']),
|
"microstructures\t%i"%(info['microstructures']),
|
||||||
])
|
])
|
||||||
theTable.head_write()
|
table.head_write()
|
||||||
|
|
||||||
# --- write packed microstructure information -----------------------------------------------------
|
# --- write packed microstructure information -----------------------------------------------------
|
||||||
type = ''
|
type = ''
|
||||||
|
@ -112,8 +111,8 @@ for file in files:
|
||||||
reps = 0
|
reps = 0
|
||||||
|
|
||||||
outputAlive = True
|
outputAlive = True
|
||||||
while outputAlive and theTable.data_read(): # read next data line of ASCII table
|
while outputAlive and table.data_read(): # read next data line of ASCII table
|
||||||
items = theTable.data
|
items = table.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]))
|
||||||
|
@ -129,31 +128,31 @@ for file in files:
|
||||||
reps += 1
|
reps += 1
|
||||||
else:
|
else:
|
||||||
if type == '':
|
if type == '':
|
||||||
theTable.data = []
|
table.data = []
|
||||||
elif type == '.':
|
elif type == '.':
|
||||||
theTable.data = [str(former)]
|
table.data = [str(former)]
|
||||||
elif type == 'to':
|
elif type == 'to':
|
||||||
theTable.data = ['%i to %i'%(former-reps+1,former)]
|
table.data = ['%i to %i'%(former-reps+1,former)]
|
||||||
elif type == 'of':
|
elif type == 'of':
|
||||||
theTable.data = ['%i of %i'%(reps,former)]
|
table.data = ['%i of %i'%(reps,former)]
|
||||||
|
|
||||||
outputAlive = theTable.data_write(delimiter = ' ') # output processed line
|
outputAlive = table.data_write(delimiter = ' ') # output processed line
|
||||||
type = '.'
|
type = '.'
|
||||||
start = current
|
start = current
|
||||||
reps = 1
|
reps = 1
|
||||||
|
|
||||||
former = current
|
former = current
|
||||||
|
|
||||||
theTable.data = {
|
table.data = {
|
||||||
'.' : [str(former)],
|
'.' : [str(former)],
|
||||||
'to': ['%i to %i'%(former-reps+1,former)],
|
'to': ['%i to %i'%(former-reps+1,former)],
|
||||||
'of': ['%i of %i'%(reps,former)],
|
'of': ['%i of %i'%(reps,former)],
|
||||||
}[type]
|
}[type]
|
||||||
outputAlive = theTable.data_write(delimiter = ' ') # output processed line
|
outputAlive = table.data_write(delimiter = ' ') # output processed line
|
||||||
|
|
||||||
|
|
||||||
# ------------------------------------------ output result ---------------------------------------
|
# ------------------------------------------ output result ---------------------------------------
|
||||||
outputAlive and theTable.output_flush() # just in case of buffered ASCII table
|
outputAlive and table.output_flush() # just in case of buffered ASCII table
|
||||||
|
|
||||||
#--- output finalization --------------------------------------------------------------------------
|
#--- output finalization --------------------------------------------------------------------------
|
||||||
if file['name'] != 'STDIN':
|
if file['name'] != 'STDIN':
|
||||||
|
|
|
@ -1,32 +1,14 @@
|
||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
# -*- coding: UTF-8 no BOM -*-
|
# -*- coding: UTF-8 no BOM -*-
|
||||||
|
|
||||||
import os,sys,string,re,math,numpy
|
import os,sys,string,re,math
|
||||||
|
import numpy as np
|
||||||
|
from optparse import OptionParser
|
||||||
import damask
|
import damask
|
||||||
from optparse import OptionParser, OptionGroup, Option, SUPPRESS_HELP
|
|
||||||
|
|
||||||
scriptID = '$Id$'
|
scriptID = '$Id$'
|
||||||
scriptName = scriptID.split()[1]
|
scriptName = scriptID.split()[1]
|
||||||
|
|
||||||
#--------------------------------------------------------------------------------------------------
|
|
||||||
class extendedOption(Option):
|
|
||||||
#--------------------------------------------------------------------------------------------------
|
|
||||||
# used for definition of new option parser action 'extend', which enables to take multiple option arguments
|
|
||||||
# taken from online tutorial http://docs.python.org/library/optparse.html
|
|
||||||
|
|
||||||
ACTIONS = Option.ACTIONS + ("extend",)
|
|
||||||
STORE_ACTIONS = Option.STORE_ACTIONS + ("extend",)
|
|
||||||
TYPED_ACTIONS = Option.TYPED_ACTIONS + ("extend",)
|
|
||||||
ALWAYS_TYPED_ACTIONS = Option.ALWAYS_TYPED_ACTIONS + ("extend",)
|
|
||||||
|
|
||||||
def take_action(self, action, dest, opt, value, values, parser):
|
|
||||||
if action == "extend":
|
|
||||||
lvalue = value.split(",")
|
|
||||||
values.ensure_value(dest, []).extend(lvalue)
|
|
||||||
else:
|
|
||||||
Option.take_action(self, action, dest, opt, value, values, parser)
|
|
||||||
|
|
||||||
|
|
||||||
#--------------------------------------------------------------------------------------------------
|
#--------------------------------------------------------------------------------------------------
|
||||||
# MAIN
|
# MAIN
|
||||||
#--------------------------------------------------------------------------------------------------
|
#--------------------------------------------------------------------------------------------------
|
||||||
|
@ -47,10 +29,10 @@ mappings = {
|
||||||
'microstructures': lambda x: int(x),
|
'microstructures': lambda x: int(x),
|
||||||
}
|
}
|
||||||
|
|
||||||
parser = OptionParser(option_class=extendedOption, usage='%prog options [file[s]]', description = """
|
parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [file[s]]', description = """
|
||||||
Unpack geometry files containing ranges "a to b" and/or "n of x" multiples (exclusively in one line).
|
Unpack geometry files containing ranges "a to b" and/or "n of x" multiples (exclusively in one line).
|
||||||
""" + string.replace(scriptID,'\n','\\n')
|
|
||||||
)
|
""", version = scriptID)
|
||||||
|
|
||||||
parser.add_option('-1', '--onedimensional', dest='oneD', action='store_true', \
|
parser.add_option('-1', '--onedimensional', dest='oneD', action='store_true', \
|
||||||
help='output geom file with one-dimensional data arrangement [%default]')
|
help='output geom file with one-dimensional data arrangement [%default]')
|
||||||
|
@ -59,43 +41,34 @@ parser.set_defaults(oneD = False)
|
||||||
|
|
||||||
(options, filenames) = parser.parse_args()
|
(options, filenames) = parser.parse_args()
|
||||||
|
|
||||||
#--- setup file handles ---------------------------------------------------------------------------
|
# ------------------------------------------ setup file handles ------------------------------------
|
||||||
files = []
|
files = []
|
||||||
if filenames == []:
|
if filenames == []:
|
||||||
files.append({'name':'STDIN',
|
files.append({'name':'STDIN', 'input':sys.stdin, 'output':sys.stdout, 'croak':sys.stderr})
|
||||||
'input':sys.stdin,
|
|
||||||
'output':sys.stdout,
|
|
||||||
'croak':sys.stderr,
|
|
||||||
})
|
|
||||||
else:
|
else:
|
||||||
for name in filenames:
|
for name in filenames:
|
||||||
if os.path.exists(name):
|
if os.path.exists(name):
|
||||||
files.append({'name':name,
|
files.append({'name':name, 'input':open(name), 'output':open(name+'_tmp','w'), 'croak':sys.stderr})
|
||||||
'input':open(name),
|
|
||||||
'output':open(name+'_tmp','w'),
|
|
||||||
'croak':sys.stdout,
|
|
||||||
})
|
|
||||||
|
|
||||||
#--- loop over input files ------------------------------------------------------------------------
|
# ------------------------------------------ loop over input files ---------------------------------
|
||||||
for file in files:
|
for file in files:
|
||||||
if file['name'] != 'STDIN': file['croak'].write('\033[1m'+scriptName+'\033[0m: '+file['name']+'\n')
|
if file['name'] != 'STDIN': file['croak'].write('\033[1m'+scriptName+'\033[0m: '+file['name']+'\n')
|
||||||
else: file['croak'].write('\033[1m'+scriptName+'\033[0m\n')
|
else: file['croak'].write('\033[1m'+scriptName+'\033[0m\n')
|
||||||
|
|
||||||
theTable = damask.ASCIItable(file['input'],file['output'],labels = False,buffered = False)
|
table = damask.ASCIItable(file['input'],file['output'],labels = False,buffered = False) # make unbuffered ASCII_table
|
||||||
theTable.head_read()
|
table.head_read() # read ASCII header info
|
||||||
|
|
||||||
|
|
||||||
#--- interpret header ----------------------------------------------------------------------------
|
#--- interpret header ----------------------------------------------------------------------------
|
||||||
info = {
|
info = {
|
||||||
'grid': numpy.zeros(3,'i'),
|
'grid': np.zeros(3,'i'),
|
||||||
'size': numpy.zeros(3,'d'),
|
'size': np.zeros(3,'d'),
|
||||||
'origin': numpy.zeros(3,'d'),
|
'origin': np.zeros(3,'d'),
|
||||||
'homogenization': 0,
|
'homogenization': 0,
|
||||||
'microstructures': 0,
|
'microstructures': 0,
|
||||||
}
|
}
|
||||||
extra_header = []
|
extra_header = []
|
||||||
|
|
||||||
for header in theTable.info:
|
for header in table.info:
|
||||||
headitems = map(str.lower,header.split())
|
headitems = map(str.lower,header.split())
|
||||||
if len(headitems) == 0: continue
|
if len(headitems) == 0: continue
|
||||||
for synonym,alternatives in synonyms.iteritems():
|
for synonym,alternatives in synonyms.iteritems():
|
||||||
|
@ -116,19 +89,19 @@ for file in files:
|
||||||
'homogenization: %i\n'%info['homogenization'] + \
|
'homogenization: %i\n'%info['homogenization'] + \
|
||||||
'microstructures: %i\n'%info['microstructures'])
|
'microstructures: %i\n'%info['microstructures'])
|
||||||
|
|
||||||
if numpy.any(info['grid'] < 1):
|
if np.any(info['grid'] < 1):
|
||||||
file['croak'].write('invalid grid a b c.\n')
|
file['croak'].write('invalid grid a b c.\n')
|
||||||
continue
|
continue
|
||||||
if numpy.any(info['size'] <= 0.0):
|
if np.any(info['size'] <= 0.0):
|
||||||
file['croak'].write('invalid size x y z.\n')
|
file['croak'].write('invalid size x y z.\n')
|
||||||
continue
|
continue
|
||||||
|
|
||||||
#--- read data ------------------------------------------------------------------------------------
|
#--- read data ------------------------------------------------------------------------------------
|
||||||
microstructure = numpy.zeros(info['grid'].prod(),'i')
|
microstructure = np.zeros(info['grid'].prod(),'i')
|
||||||
i = 0
|
i = 0
|
||||||
|
|
||||||
while theTable.data_read(): # read next data line of ASCII table
|
while table.data_read(): # read next data line of ASCII table
|
||||||
items = theTable.data
|
items = table.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]))
|
||||||
|
@ -140,9 +113,9 @@ for file in files:
|
||||||
i += s
|
i += s
|
||||||
|
|
||||||
#--- write header ---------------------------------------------------------------------------------
|
#--- write header ---------------------------------------------------------------------------------
|
||||||
theTable.labels_clear()
|
table.labels_clear()
|
||||||
theTable.info_clear()
|
table.info_clear()
|
||||||
theTable.info_append(extra_header+[
|
table.info_append(extra_header+[
|
||||||
scriptID + ' ' + ' '.join(sys.argv[1:]),
|
scriptID + ' ' + ' '.join(sys.argv[1:]),
|
||||||
"grid\ta %i\tb %i\tc %i"%(info['grid'][0],info['grid'][1],info['grid'][2],),
|
"grid\ta %i\tb %i\tc %i"%(info['grid'][0],info['grid'][1],info['grid'][2],),
|
||||||
"size\tx %e\ty %e\tz %e"%(info['size'][0],info['size'][1],info['size'][2],),
|
"size\tx %e\ty %e\tz %e"%(info['size'][0],info['size'][1],info['size'][2],),
|
||||||
|
@ -150,15 +123,15 @@ for file in files:
|
||||||
"homogenization\t%i"%info['homogenization'],
|
"homogenization\t%i"%info['homogenization'],
|
||||||
"microstructures\t%i"%(info['microstructures']),
|
"microstructures\t%i"%(info['microstructures']),
|
||||||
])
|
])
|
||||||
theTable.head_write()
|
table.head_write()
|
||||||
|
|
||||||
# --- write microstructure information ------------------------------------------------------------
|
# --- write microstructure information ------------------------------------------------------------
|
||||||
formatwidth = int(math.floor(math.log10(microstructure.max())+1))
|
formatwidth = int(math.floor(math.log10(microstructure.max())+1))
|
||||||
if options.oneD:
|
if options.oneD:
|
||||||
theTable.data = microstructure
|
table.data = microstructure
|
||||||
else:
|
else:
|
||||||
theTable.data = microstructure.reshape((info['grid'][0],info['grid'][1]*info['grid'][2]),order='F').transpose()
|
table.data = microstructure.reshape((info['grid'][0],info['grid'][1]*info['grid'][2]),order='F').transpose()
|
||||||
theTable.data_writeArray('%%%ii'%(formatwidth),delimiter = ' ')
|
table.data_writeArray('%%%ii'%(formatwidth),delimiter = ' ')
|
||||||
|
|
||||||
#--- output finalization --------------------------------------------------------------------------
|
#--- output finalization --------------------------------------------------------------------------
|
||||||
if file['name'] != 'STDIN':
|
if file['name'] != 'STDIN':
|
||||||
|
|
Loading…
Reference in New Issue