can deal with "veterans" and "newbies" meaning over ride existing with new
This commit is contained in:
parent
920cf2c849
commit
304fdf1ebe
|
@ -1,4 +1,4 @@
|
|||
#!/usr/bin/env python2.7
|
||||
#!/usr/bin/env python2
|
||||
# -*- coding: UTF-8 no BOM -*-
|
||||
|
||||
import os,re,sys
|
||||
|
@ -6,10 +6,15 @@ import math
|
|||
import numpy as np
|
||||
from optparse import OptionParser
|
||||
import damask
|
||||
import collections
|
||||
|
||||
scriptName = os.path.splitext(os.path.basename(__file__))[0]
|
||||
scriptID = ' '.join([scriptName,damask.version])
|
||||
|
||||
def listify(x):
|
||||
return (x if isinstance(x, collections.Iterable) else [x])
|
||||
|
||||
|
||||
# --------------------------------------------------------------------
|
||||
# MAIN
|
||||
# --------------------------------------------------------------------
|
||||
|
@ -55,13 +60,9 @@ for i in xrange(len(options.formulas)):
|
|||
if filenames == []: filenames = [None]
|
||||
|
||||
for name in filenames:
|
||||
try:
|
||||
table = damask.ASCIItable(name = name,
|
||||
buffered = False)
|
||||
output = damask.ASCIItable(name = name,
|
||||
buffered = False)
|
||||
except:
|
||||
continue
|
||||
try: table = damask.ASCIItable(name = name,
|
||||
buffered = False)
|
||||
except: continue
|
||||
damask.util.report(scriptName,name)
|
||||
|
||||
# ------------------------------------------ read header -------------------------------------------
|
||||
|
@ -129,53 +130,46 @@ for name in filenames:
|
|||
|
||||
while outputAlive and table.data_read(): # read next data line of ASCII table
|
||||
specials['_row_'] += 1 # count row
|
||||
output.data_clear()
|
||||
|
||||
# ------------------------------------------ calculate one result to get length of labels ---------
|
||||
|
||||
if firstLine:
|
||||
firstLine = False
|
||||
labelDim = {}
|
||||
for label in [x for x in options.labels]:
|
||||
labelDim[label] = np.size(eval(evaluator[label]))
|
||||
if labelDim[label] == 0: options.labels.remove(label)
|
||||
resultDim = {}
|
||||
for label in list(options.labels): # iterate over stable copy
|
||||
resultDim[label] = np.size(eval(evaluator[label])) # get dimension of formula[label]
|
||||
if resultDim[label] == 0: options.labels.remove(label) # remove label if invalid result
|
||||
|
||||
veterans = list(set(options.labels)&set(table.labels(raw=False)+table.labels(raw=True)) ) # intersection of requested and existing
|
||||
newbies = list(set(options.labels)-set(table.labels(raw=False)+table.labels(raw=True)) ) # requested but not existing
|
||||
|
||||
for veteran in list(veterans):
|
||||
if resultDim[veteran] != table.label_dimension(veteran):
|
||||
damask.util.croak('{} is ignored due to inconsistent dimension...'.format(veteran))
|
||||
veterans.remove(veteran) # ignore culprit
|
||||
for newby in newbies:
|
||||
table.labels_append(['{}_{}'.format(i+1,newby) for i in xrange(resultDim[newby])]
|
||||
if resultDim[newby] > 1 else newby)
|
||||
|
||||
# ------------------------------------------ assemble header ---------------------------------------
|
||||
|
||||
output.labels_clear()
|
||||
tabLabels = table.labels()
|
||||
for label in tabLabels:
|
||||
dim = labelDim[label] if label in options.labels \
|
||||
else table.label_dimension(label)
|
||||
output.labels_append(['{}_{}'.format(i+1,label) for i in xrange(dim)] if dim > 1 else label)
|
||||
|
||||
for label in options.labels:
|
||||
if label in tabLabels: continue
|
||||
output.labels_append(['{}_{}'.format(i+1,label) for i in xrange(labelDim[label])]
|
||||
if labelDim[label] > 1
|
||||
else label)
|
||||
|
||||
output.info = table.info
|
||||
output.info_append(scriptID + '\t' + ' '.join(sys.argv[1:]))
|
||||
output.head_write()
|
||||
table.info_append(scriptID + '\t' + ' '.join(sys.argv[1:]))
|
||||
table.head_write()
|
||||
|
||||
# ------------------------------------------ process data ------------------------------------------
|
||||
|
||||
for label in output.labels():
|
||||
oldIndices = table.label_indexrange(label)
|
||||
Nold = max(1,len(oldIndices)) # Nold could be zero for new columns
|
||||
Nnew = len(output.label_indexrange(label))
|
||||
output.data_append(eval(evaluator[label]) if label in options.labels and
|
||||
(options.condition is None or eval(eval(evaluator_condition)))
|
||||
else np.tile([table.data[i] for i in oldIndices]
|
||||
if label in tabLabels
|
||||
else np.nan,
|
||||
np.ceil(float(Nnew)/Nold))[:Nnew]) # spread formula result into given number of columns
|
||||
if options.condition is None or eval(eval(evaluator_condition)): # condition for veteran replacement fulfilled
|
||||
for veteran in veterans: # evaluate formulae that overwrite
|
||||
table.data[table.label_index(veteran):
|
||||
table.label_index(veteran)+table.label_dimension(veteran)] = \
|
||||
listify(eval(evaluator[veteran]))
|
||||
|
||||
for newby in newbies: # evaluate formulae that append
|
||||
table.data_append(listify(eval(evaluator[newby])))
|
||||
|
||||
outputAlive = output.data_write() # output processed line
|
||||
outputAlive = table.data_write() # output processed line
|
||||
|
||||
# ------------------------------------------ output finalization -----------------------------------
|
||||
|
||||
table.input_close() # close ASCII tables
|
||||
output.close() # close ASCII tables
|
||||
|
||||
table.close() # close ASCII table
|
||||
|
||||
|
|
Loading…
Reference in New Issue