corrected handling of STDIN|STDOUT
adopted ASCII_TABLE class
This commit is contained in:
parent
267b8ac30c
commit
18a09d7cff
|
@ -1,6 +1,6 @@
|
||||||
#!/usr/bin/python
|
#!/usr/bin/python
|
||||||
|
|
||||||
import os,re,sys,math,numpy,string
|
import os,re,sys,math,numpy,string,damask_tools
|
||||||
from optparse import OptionParser, Option
|
from optparse import OptionParser, Option
|
||||||
|
|
||||||
# -----------------------------
|
# -----------------------------
|
||||||
|
@ -22,13 +22,6 @@ class extendableOption(Option):
|
||||||
Option.take_action(self, action, dest, opt, value, values, parser)
|
Option.take_action(self, action, dest, opt, value, values, parser)
|
||||||
|
|
||||||
|
|
||||||
def prefixMultiply(what,len):
|
|
||||||
|
|
||||||
return {True: ['%i_%s'%(i+1,what) for i in range(len)],
|
|
||||||
False:[what]}[len>1]
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# --------------------------------------------------------------------
|
# --------------------------------------------------------------------
|
||||||
# MAIN
|
# MAIN
|
||||||
|
@ -42,14 +35,11 @@ deformation gradient and first Piola--Kirchhoff stress.
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
parser.add_option('-m','--memory', dest='memory', action='store_true', \
|
|
||||||
help='load complete file into memory [%default]')
|
|
||||||
parser.add_option('-f','--defgrad', dest='defgrad', type='string', \
|
parser.add_option('-f','--defgrad', dest='defgrad', type='string', \
|
||||||
help='heading of columns containing deformation gradient [%default]')
|
help='heading of columns containing deformation gradient [%default]')
|
||||||
parser.add_option('-p','--stress', dest='stress', type='string', \
|
parser.add_option('-p','--stress', dest='stress', type='string', \
|
||||||
help='heading of columns containing first Piola--Kirchhoff stress [%default]')
|
help='heading of columns containing first Piola--Kirchhoff stress [%default]')
|
||||||
|
|
||||||
parser.set_defaults(memory = False)
|
|
||||||
parser.set_defaults(defgrad = 'f')
|
parser.set_defaults(defgrad = 'f')
|
||||||
parser.set_defaults(stress = 'p')
|
parser.set_defaults(stress = 'p')
|
||||||
|
|
||||||
|
@ -74,7 +64,7 @@ datainfo['stress']['label'].append(options.stress)
|
||||||
|
|
||||||
files = []
|
files = []
|
||||||
if filenames == []:
|
if filenames == []:
|
||||||
files.append({'name':'STDIN', 'handle':sys.stdin})
|
files.append({'name':'STDIN', 'input':sys.stdin, 'output':sys.stdout})
|
||||||
else:
|
else:
|
||||||
for name in filenames:
|
for name in filenames:
|
||||||
if os.path.exists(name):
|
if os.path.exists(name):
|
||||||
|
@ -83,30 +73,12 @@ else:
|
||||||
# ------------------------------------------ loop over input files ---------------------------------------
|
# ------------------------------------------ loop over input files ---------------------------------------
|
||||||
|
|
||||||
for file in files:
|
for file in files:
|
||||||
print file['name']
|
if file['name'] != 'STDIN': print file['name']
|
||||||
|
|
||||||
# get labels by either read the first row, or - if keyword header is present - the last line of the header
|
table = damask_tools.ASCII_TABLE(file['input'],file['output'],False) # make unbuffered ASCII_table
|
||||||
|
table.head_read() # read ASCII header info
|
||||||
firstline = file['input'].readline()
|
table.info_append(string.replace('$Id$','\n','\\n') + \
|
||||||
m = re.search('(\d+)\s*head', firstline.lower())
|
'\t' + ' '.join(sys.argv[1:]))
|
||||||
if m:
|
|
||||||
headerlines = int(m.group(1))
|
|
||||||
passOn = [file['input'].readline() for i in range(1,headerlines)]
|
|
||||||
headers = file['input'].readline().split()
|
|
||||||
else:
|
|
||||||
headerlines = 1
|
|
||||||
passOn = []
|
|
||||||
headers = firstline.split()
|
|
||||||
|
|
||||||
if options.memory:
|
|
||||||
data = file['input'].readlines()
|
|
||||||
else:
|
|
||||||
data = []
|
|
||||||
|
|
||||||
for i,l in enumerate(headers):
|
|
||||||
if l.startswith('1_'):
|
|
||||||
if re.match('\d+_',l[2:]) or i == len(headers)-1 or not headers[i+1].endswith(l[2:]):
|
|
||||||
headers[i] = l[2:]
|
|
||||||
|
|
||||||
active = {}
|
active = {}
|
||||||
column = {}
|
column = {}
|
||||||
|
@ -116,56 +88,38 @@ for file in files:
|
||||||
for label in info['label']:
|
for label in info['label']:
|
||||||
key = {True :'1_%s',
|
key = {True :'1_%s',
|
||||||
False:'%s' }[info['len']>1]%label
|
False:'%s' }[info['len']>1]%label
|
||||||
if key not in headers:
|
if key not in table.labels:
|
||||||
sys.stderr.write('column %s not found...\n'%key)
|
sys.stderr.write('column %s not found...\n'%key)
|
||||||
else:
|
else:
|
||||||
if datatype not in active: active[datatype] = []
|
if datatype not in active: active[datatype] = []
|
||||||
if datatype not in column: column[datatype] = {}
|
if datatype not in column: column[datatype] = {}
|
||||||
active[datatype].append(label)
|
active[datatype].append(label)
|
||||||
column[datatype][label] = headers.index(key)
|
column[datatype][label] = table.labels.index(key) # remember columns of requested data
|
||||||
head += prefixMultiply('Cauchy',datainfo[datatype]['len'])
|
|
||||||
|
table.labels_append(['%i_Cauchy'%(i+1)
|
||||||
|
for i in xrange(datainfo['defgrad']['len'])]) # extend ASCII header with new labels
|
||||||
|
|
||||||
# ------------------------------------------ assemble header ---------------------------------------
|
# ------------------------------------------ assemble header ---------------------------------------
|
||||||
|
|
||||||
output = '%i\theader'%(headerlines+1) + '\n' + \
|
table.head_write()
|
||||||
''.join(passOn) + \
|
|
||||||
string.replace('$Id$','\n','\\n')+ '\t' + \
|
|
||||||
' '.join(sys.argv[1:]) + '\n' + \
|
|
||||||
'\t'.join(headers + head) + '\n' # build extended header
|
|
||||||
|
|
||||||
if not options.memory:
|
# ------------------------------------------ process data ---------------------------------------
|
||||||
file['output'].write(output)
|
|
||||||
output = ''
|
|
||||||
|
|
||||||
# ------------------------------------------ read file ---------------------------------------
|
while table.data_read(): # read next data line of ASCII table
|
||||||
|
|
||||||
for line in {True : data,
|
|
||||||
False : file['input']}[options.memory]:
|
|
||||||
items = line.split()[:len(headers)]
|
|
||||||
if len(items) < len(headers):
|
|
||||||
continue
|
|
||||||
|
|
||||||
output += '\t'.join(items)
|
F = numpy.array(map(float,table.data[column['defgrad'][active['defgrad'][0]]:
|
||||||
|
column['defgrad'][active['defgrad'][0]]+datainfo['defgrad']['len']]),'d').reshape(3,3)
|
||||||
|
P = numpy.array(map(float,table.data[column['stress'][active['stress'][0]]:
|
||||||
|
column['stress'][active['stress'][0]]+datainfo['stress']['len']]),'d').reshape(3,3)
|
||||||
|
|
||||||
F = numpy.array(map(float,items[column['defgrad'][active['defgrad'][0]]:
|
table.data_append(list(1.0/numpy.linalg.det(F)*numpy.dot(P,F.T).reshape(9))) # [Cauchy] = (1/det(F)) * [P].[F_transpose]
|
||||||
column['defgrad'][active['defgrad'][0]]+datainfo['defgrad']['len']]),'d').reshape(3,3)
|
table.data_write() # output processed line
|
||||||
P = numpy.array(map(float,items[column['stress'][active['stress'][0]]:
|
|
||||||
column['stress'][active['stress'][0]]+datainfo['stress']['len']]),'d').reshape(3,3)
|
|
||||||
output += '\t'+'\t'.join(map(str,1.0/numpy.linalg.det(F)*numpy.dot(P,F.T).reshape(9))) # [Cauchy] = (1/det(F)) * [P].[F_transpose]
|
|
||||||
|
|
||||||
output += '\n'
|
|
||||||
|
|
||||||
if not options.memory:
|
|
||||||
file['output'].write(output)
|
|
||||||
output = ''
|
|
||||||
|
|
||||||
file['input'].close()
|
|
||||||
|
|
||||||
# ------------------------------------------ output result ---------------------------------------
|
# ------------------------------------------ output result ---------------------------------------
|
||||||
|
|
||||||
if options.memory:
|
table.output_flush() # just in case of buffered ASCII table
|
||||||
file['output'].write(output)
|
|
||||||
|
|
||||||
|
file['input'].close() # close input ASCII table
|
||||||
if file['name'] != 'STDIN':
|
if file['name'] != 'STDIN':
|
||||||
file['output'].close
|
file['output'].close # close output ASCII table
|
||||||
os.rename(file['name']+'_tmp',file['name'])
|
os.rename(file['name']+'_tmp',file['name']) # overwrite old one with tmp new
|
||||||
|
|
Loading…
Reference in New Issue