2015-08-29 06:24:45 +05:30
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: UTF-8 no BOM -*-
|
|
|
|
|
2015-10-09 19:52:41 +05:30
|
|
|
import os,re,sys,string,fnmatch,math,random
|
|
|
|
import numpy as np
|
2015-08-29 06:24:45 +05:30
|
|
|
from optparse import OptionParser
|
|
|
|
import damask
|
|
|
|
|
2016-01-27 22:36:00 +05:30
|
|
|
scriptName = os.path.splitext(os.path.basename(__file__))[0]
|
|
|
|
scriptID = ' '.join([scriptName,damask.version])
|
2015-08-29 06:24:45 +05:30
|
|
|
|
|
|
|
def unravel(item):
|
|
|
|
if hasattr(item,'__contains__'): return ' '.join(map(unravel,item))
|
|
|
|
else: return str(item)
|
|
|
|
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# MAIN
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
|
|
|
|
parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [file[s]]', description = """
|
|
|
|
Update existing value(s) to expression(s) respecting condition.
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
Replace the 2nd value of column x by value "z - y" --> fillTable -l x -c "#_row_#==2" -f "#z#-#y#"
|
|
|
|
|
|
|
|
""", version = scriptID)
|
|
|
|
|
|
|
|
parser.add_option('-l','--label',
|
|
|
|
dest = 'labels',
|
|
|
|
action = 'extend', metavar = '<string LIST>',
|
|
|
|
help = '(list) of columns to be filled with formula(e)')
|
|
|
|
parser.add_option('-f','--formula',
|
|
|
|
dest = 'formulae',
|
|
|
|
action = 'extend', metavar = '<string LIST>',
|
|
|
|
help = '(list of) formulae corresponding to labels')
|
|
|
|
parser.add_option('-c','--condition',
|
|
|
|
dest = 'condition', metavar='string',
|
|
|
|
help = 'condition to filter rows')
|
|
|
|
|
|
|
|
parser.set_defaults(condition = '',
|
|
|
|
)
|
|
|
|
|
|
|
|
(options,filenames) = parser.parse_args()
|
|
|
|
|
|
|
|
if options.labels == None or options.formulae == None:
|
|
|
|
parser.error('no formulae specified.')
|
|
|
|
if len(options.labels) != len(options.formulae):
|
|
|
|
parser.error('number of labels ({}) and formulae ({}) do not match.'.format(len(options.labels),len(options.formulae)))
|
|
|
|
|
|
|
|
for i in xrange(len(options.formulae)):
|
|
|
|
options.formulae[i] = options.formulae[i].replace(';',',')
|
|
|
|
|
|
|
|
# --- loop over input files -------------------------------------------------------------------------
|
|
|
|
|
|
|
|
if filenames == []: filenames = [None]
|
|
|
|
|
|
|
|
for name in filenames:
|
|
|
|
try:
|
|
|
|
table = damask.ASCIItable(name = name,
|
|
|
|
buffered = False)
|
|
|
|
except: continue
|
2015-09-24 14:54:42 +05:30
|
|
|
damask.util.report(scriptName,name)
|
2015-08-29 06:24:45 +05:30
|
|
|
|
2015-08-29 20:40:43 +05:30
|
|
|
# ------------------------------------------ assemble info -----------------------------------------
|
2015-08-29 06:24:45 +05:30
|
|
|
|
|
|
|
table.head_read()
|
|
|
|
# ------------------------------------------ Evaluate condition ---------------------------------------
|
|
|
|
|
|
|
|
specials = { \
|
|
|
|
'_row_': 0,
|
|
|
|
}
|
|
|
|
|
|
|
|
interpolator = []
|
2015-10-09 19:52:41 +05:30
|
|
|
condition = options.condition # copy per file, since might be altered inline
|
|
|
|
breaker = False
|
|
|
|
|
2015-08-29 06:24:45 +05:30
|
|
|
for position,operand in enumerate(set(re.findall(r'#(([s]#)?(.+?))#',condition))): # find three groups
|
|
|
|
condition = condition.replace('#'+operand[0]+'#',
|
2015-10-09 19:52:41 +05:30
|
|
|
{ '': '{%i}'%position,
|
|
|
|
's#':'"{%i}"'%position}[operand[1]])
|
2015-08-29 06:24:45 +05:30
|
|
|
if operand[2] in specials: # special label
|
|
|
|
interpolator += ['specials["%s"]'%operand[2]]
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
interpolator += ['%s(table.data[%i])'%({ '':'float',
|
|
|
|
's#':'str'}[operand[1]],
|
2015-10-09 19:52:41 +05:30
|
|
|
table.labels.index(operand[2]))] # ccould be generalized to indexrange as array lookup
|
2015-08-29 06:24:45 +05:30
|
|
|
except:
|
2015-10-09 19:52:41 +05:30
|
|
|
damask.util.croak('column %s not found.'%operand[2])
|
|
|
|
breaker = True
|
|
|
|
|
|
|
|
if breaker: continue # found mistake in condition evaluation --> next file
|
|
|
|
|
2015-08-29 06:24:45 +05:30
|
|
|
evaluator_condition = "'" + condition + "'.format(" + ','.join(interpolator) + ")"
|
|
|
|
|
|
|
|
#----------------------------------- Formula -------------------------------------------------------
|
|
|
|
|
|
|
|
evaluator = {}
|
|
|
|
brokenFormula = {}
|
|
|
|
for label,formula in zip(options.labels,options.formulae):
|
|
|
|
for column in re.findall(r'#(.+?)#',formula): # loop over column labels in formula
|
|
|
|
idx = table.label_index(column)
|
|
|
|
dim = table.label_dimension(column)
|
|
|
|
if column in specials:
|
|
|
|
replacement = 'specials["{}"]'.format(column)
|
|
|
|
elif dim == 1: # scalar input
|
|
|
|
replacement = 'float(table.data[{}])'.format(idx) # take float value of data column
|
|
|
|
elif dim > 1: # multidimensional input (vector, tensor, etc.)
|
|
|
|
replacement = 'np.array(table.data[{}:{}],dtype=float)'.format(idx,idx+dim) # use (flat) array representation
|
|
|
|
else:
|
2015-09-24 14:54:42 +05:30
|
|
|
damask.util.croak('column {} not found...'.format(column))
|
2015-08-29 06:24:45 +05:30
|
|
|
brokenFormula[label] = True
|
|
|
|
break
|
|
|
|
formula = formula.replace('#'+column+'#',replacement)
|
|
|
|
if label not in brokenFormula:
|
|
|
|
evaluator[label] = formula
|
2015-10-09 19:52:41 +05:30
|
|
|
|
2015-08-29 06:24:45 +05:30
|
|
|
# ------------------------------------------ assemble header ---------------------------------------
|
2015-10-09 19:52:41 +05:30
|
|
|
|
2015-08-29 20:40:43 +05:30
|
|
|
table.info_append(scriptID + '\t' + ' '.join(sys.argv[1:])) # read ASCII header info
|
|
|
|
table.labels # update with label set
|
2015-08-29 06:24:45 +05:30
|
|
|
table.head_write()
|
|
|
|
|
|
|
|
# ------------------------------------------ process data ------------------------------------------
|
|
|
|
|
|
|
|
outputAlive = True
|
|
|
|
while outputAlive and table.data_read(): # read next data line of ASCII table
|
|
|
|
specials['_row_'] += 1
|
2015-10-09 19:52:41 +05:30
|
|
|
if condition == '' or eval(eval(evaluator_condition)): # test row for condition
|
2015-08-29 06:24:45 +05:30
|
|
|
for label in [x for x in options.labels if x not in set(brokenFormula)]:
|
2015-10-09 19:52:41 +05:30
|
|
|
indices = table.label_indexrange(label) # affected columns
|
|
|
|
probe = np.array(eval(evaluator[label])) # get formula result (scalar, array, etc.)
|
|
|
|
container = np.tile(probe,np.ceil(float(len(indices))/probe.size))[:len(indices)] # spread formula result into given number of columns
|
|
|
|
for i,ind in enumerate(indices): # copy one by one as table.data is NOT a numpy array
|
|
|
|
table.data[ind] = container[i]
|
2015-08-29 06:24:45 +05:30
|
|
|
|
|
|
|
outputAlive = table.data_write() # output processed line
|
|
|
|
|
|
|
|
# ------------------------------------------ output finalization -----------------------------------
|
|
|
|
|
|
|
|
table.close() # close ASCII tables
|
|
|
|
|