2012-02-07 18:39:10 +05:30
|
|
|
#!/usr/bin/env python
|
2014-04-02 00:11:14 +05:30
|
|
|
# -*- coding: UTF-8 no BOM -*-
|
2011-12-02 20:45:36 +05:30
|
|
|
|
2015-09-24 14:54:42 +05:30
|
|
|
import os,re,sys,string
|
2015-12-01 22:07:20 +05:30
|
|
|
import math # flake8: noqa
|
2014-06-17 12:40:10 +05:30
|
|
|
import numpy as np
|
|
|
|
from optparse import OptionParser
|
|
|
|
import damask
|
2011-12-02 20:45:36 +05:30
|
|
|
|
2016-01-27 22:36:00 +05:30
|
|
|
scriptName = os.path.splitext(os.path.basename(__file__))[0]
|
|
|
|
scriptID = ' '.join([scriptName,damask.version])
|
2013-12-09 21:24:47 +05:30
|
|
|
|
2013-07-17 03:18:23 +05:30
|
|
|
def unravel(item):
|
|
|
|
if hasattr(item,'__contains__'): return ' '.join(map(unravel,item))
|
|
|
|
else: return str(item)
|
|
|
|
|
2011-12-02 20:45:36 +05:30
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# MAIN
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
|
2014-06-17 12:40:10 +05:30
|
|
|
parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [file[s]]', description = """
|
2015-08-08 00:33:26 +05:30
|
|
|
Add column(s) with derived values according to user-defined arithmetic operation between column(s).
|
|
|
|
Column labels are tagged by '#label#' in formulas. Use ';' for ',' in functions.
|
2014-08-06 18:57:09 +05:30
|
|
|
Numpy is available as np.
|
2011-12-02 20:45:36 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
Special variables: #_row_# -- row index
|
|
|
|
Examples: (1) magnitude of vector -- "np.linalg.norm(#vec#)" (2) rounded root of row number -- "round(math.sqrt(#_row_#);3)"
|
2014-08-06 18:57:09 +05:30
|
|
|
|
|
|
|
""", version = scriptID)
|
2011-12-02 20:45:36 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
parser.add_option('-l','--label',
|
|
|
|
dest = 'labels',
|
|
|
|
action = 'extend', metavar = '<string LIST>',
|
|
|
|
help = '(list of) new column labels')
|
|
|
|
parser.add_option('-f','--formula',
|
|
|
|
dest = 'formulas',
|
|
|
|
action = 'extend', metavar = '<string LIST>',
|
|
|
|
help = '(list of) formulas corresponding to labels')
|
2011-12-02 20:45:36 +05:30
|
|
|
|
|
|
|
(options,filenames) = parser.parse_args()
|
|
|
|
|
2015-05-09 18:15:30 +05:30
|
|
|
if options.labels == None or options.formulas == None:
|
2015-08-08 00:33:26 +05:30
|
|
|
parser.error('no formulas and/or labels specified.')
|
|
|
|
if len(options.labels) != len(options.formulas):
|
|
|
|
parser.error('number of labels ({}) and formulas ({}) do not match.'.format(len(options.labels),len(options.formulas)))
|
2015-05-09 18:15:30 +05:30
|
|
|
|
2012-08-22 23:17:34 +05:30
|
|
|
for i in xrange(len(options.formulas)):
|
2015-08-08 00:33:26 +05:30
|
|
|
options.formulas[i] = options.formulas[i].replace(';',',')
|
|
|
|
|
|
|
|
# --- loop over input files -------------------------------------------------------------------------
|
|
|
|
|
2015-08-13 02:29:10 +05:30
|
|
|
if filenames == []: filenames = [None]
|
2015-08-08 00:33:26 +05:30
|
|
|
|
|
|
|
for name in filenames:
|
2015-08-13 02:29:10 +05:30
|
|
|
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-08 00:33:26 +05:30
|
|
|
|
|
|
|
# ------------------------------------------ read header -------------------------------------------
|
|
|
|
|
|
|
|
table.head_read()
|
|
|
|
|
|
|
|
# ------------------------------------------ build formulae ----------------------------------------
|
2011-12-02 20:45:36 +05:30
|
|
|
|
2012-02-16 17:26:16 +05:30
|
|
|
specials = { \
|
|
|
|
'_row_': 0,
|
|
|
|
}
|
|
|
|
|
2011-12-02 20:45:36 +05:30
|
|
|
evaluator = {}
|
2013-12-09 21:24:47 +05:30
|
|
|
brokenFormula = {}
|
|
|
|
|
2011-12-02 20:45:36 +05:30
|
|
|
for label,formula in zip(options.labels,options.formulas):
|
2014-07-10 14:57:51 +05:30
|
|
|
for column in re.findall(r'#(.+?)#',formula): # loop over column labels in formula
|
2015-08-08 00:33:26 +05:30
|
|
|
idx = table.label_index(column)
|
|
|
|
dim = table.label_dimension(column)
|
2013-12-09 21:24:47 +05:30
|
|
|
if column in specials:
|
2015-08-08 00:33:26 +05:30
|
|
|
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
|
2012-02-16 17:26:16 +05:30
|
|
|
else:
|
2015-09-24 14:54:42 +05:30
|
|
|
damask.util.croak('column {} not found...'.format(column))
|
2015-08-08 00:33:26 +05:30
|
|
|
brokenFormula[label] = True
|
|
|
|
break
|
|
|
|
|
|
|
|
formula = formula.replace('#'+column+'#',replacement)
|
2014-05-03 01:26:56 +05:30
|
|
|
|
|
|
|
if label not in brokenFormula:
|
2015-08-08 00:33:26 +05:30
|
|
|
evaluator[label] = formula
|
2013-07-17 03:18:23 +05:30
|
|
|
|
2014-07-22 01:25:05 +05:30
|
|
|
# ------------------------------------------ process data ------------------------------------------
|
2015-08-08 00:33:26 +05:30
|
|
|
|
|
|
|
firstLine = True
|
2014-07-22 01:25:05 +05:30
|
|
|
outputAlive = True
|
2015-08-08 00:33:26 +05:30
|
|
|
|
2014-07-22 01:25:05 +05:30
|
|
|
while outputAlive and table.data_read(): # read next data line of ASCII table
|
|
|
|
specials['_row_'] += 1 # count row
|
2015-08-08 00:33:26 +05:30
|
|
|
|
2014-07-10 14:57:51 +05:30
|
|
|
# ------------------------------------------ calculate one result to get length of labels ---------
|
2015-08-08 00:33:26 +05:30
|
|
|
|
2014-07-22 01:25:05 +05:30
|
|
|
if firstLine:
|
2015-08-08 00:33:26 +05:30
|
|
|
firstLine = False
|
|
|
|
labelDim = {}
|
|
|
|
for label in [x for x in options.labels if x not in set(brokenFormula)]:
|
|
|
|
labelDim[label] = np.size(eval(evaluator[label]))
|
|
|
|
if labelDim[label] == 0: brokenFormula[label] = True
|
2011-12-02 20:45:36 +05:30
|
|
|
|
2014-08-06 20:55:18 +05:30
|
|
|
# ------------------------------------------ assemble header ---------------------------------------
|
2015-08-08 00:33:26 +05:30
|
|
|
|
2014-07-22 01:25:05 +05:30
|
|
|
if label not in brokenFormula:
|
2015-08-08 00:33:26 +05:30
|
|
|
table.labels_append(['{}_{}'.format(i+1,label) for i in xrange(labelDim[label])] if labelDim[label] > 1
|
2015-05-09 18:15:30 +05:30
|
|
|
else label)
|
2015-08-08 00:33:26 +05:30
|
|
|
|
|
|
|
table.info_append(scriptID + '\t' + ' '.join(sys.argv[1:]))
|
2014-07-22 01:25:05 +05:30
|
|
|
table.head_write()
|
2011-12-02 20:45:36 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
# ------------------------------------------ process data ------------------------------------------
|
|
|
|
|
|
|
|
for label in [x for x in options.labels if x not in set(brokenFormula)]:
|
|
|
|
table.data_append(unravel(eval(evaluator[label])))
|
2015-08-13 02:29:10 +05:30
|
|
|
|
2014-07-10 14:57:51 +05:30
|
|
|
outputAlive = table.data_write() # output processed line
|
2011-12-02 20:45:36 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
# ------------------------------------------ output finalization -----------------------------------
|
2011-12-02 20:45:36 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
table.close() # close ASCII tables
|