2019-02-15 03:26:39 +05:30
|
|
|
#!/usr/bin/env python3
|
2015-08-21 01:10:45 +05:30
|
|
|
|
2019-06-14 16:33:30 +05:30
|
|
|
import os
|
|
|
|
import sys
|
2015-08-21 01:10:45 +05:30
|
|
|
from optparse import OptionParser
|
2019-06-14 16:33:30 +05:30
|
|
|
|
|
|
|
import numpy as np
|
|
|
|
|
2015-08-21 01:10:45 +05:30
|
|
|
import damask
|
|
|
|
|
2019-06-14 16:33:30 +05:30
|
|
|
|
2016-01-27 22:36:00 +05:30
|
|
|
scriptName = os.path.splitext(os.path.basename(__file__))[0]
|
|
|
|
scriptID = ' '.join([scriptName,damask.version])
|
2015-08-21 01:10:45 +05:30
|
|
|
|
2019-06-14 16:33:30 +05:30
|
|
|
|
2015-08-21 01:10:45 +05:30
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# MAIN
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
|
2019-02-16 22:11:56 +05:30
|
|
|
parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [ASCIItable(s)]', description = """
|
2015-08-21 01:10:45 +05:30
|
|
|
Add cumulative (sum of first to current row) values for given label(s).
|
|
|
|
""", version = scriptID)
|
|
|
|
|
|
|
|
parser.add_option('-l','--label',
|
|
|
|
dest='label',
|
|
|
|
action = 'extend', metavar = '<string LIST>',
|
|
|
|
help = 'columns to cumulate')
|
|
|
|
|
2019-10-17 22:49:51 +05:30
|
|
|
parser.add_option('-p','--product',
|
|
|
|
dest='product', action = 'store_true',
|
|
|
|
help = 'product of values instead of sum')
|
|
|
|
|
2015-08-21 01:10:45 +05:30
|
|
|
(options,filenames) = parser.parse_args()
|
|
|
|
|
2019-02-16 19:23:56 +05:30
|
|
|
if options.label is None:
|
2015-08-21 01:10:45 +05:30
|
|
|
parser.error('no data column(s) specified.')
|
|
|
|
|
|
|
|
# --- loop over input files -------------------------------------------------------------------------
|
|
|
|
|
|
|
|
if filenames == []: filenames = [None]
|
|
|
|
|
|
|
|
for name in filenames:
|
|
|
|
try:
|
2015-09-12 22:04:31 +05:30
|
|
|
table = damask.ASCIItable(name = name,
|
2019-10-17 22:49:51 +05:30
|
|
|
buffered = False)
|
|
|
|
except IOError: continue
|
2015-09-24 14:54:42 +05:30
|
|
|
damask.util.report(scriptName,name)
|
2015-08-21 01:10:45 +05:30
|
|
|
|
|
|
|
# ------------------------------------------ read header ------------------------------------------
|
|
|
|
|
|
|
|
table.head_read()
|
|
|
|
|
|
|
|
# ------------------------------------------ sanity checks ----------------------------------------
|
|
|
|
|
|
|
|
errors = []
|
|
|
|
remarks = []
|
|
|
|
columns = []
|
|
|
|
dims = []
|
|
|
|
|
|
|
|
for what in options.label:
|
|
|
|
dim = table.label_dimension(what)
|
|
|
|
if dim < 0: remarks.append('column {} not found...'.format(what))
|
|
|
|
else:
|
|
|
|
dims.append(dim)
|
|
|
|
columns.append(table.label_index(what))
|
|
|
|
table.labels_append('cum({})'.format(what) if dim == 1 else
|
2016-10-25 00:46:29 +05:30
|
|
|
['{}_cum({})'.format(i+1,what) for i in range(dim)] ) # extend ASCII header with new labels
|
2015-08-21 01:10:45 +05:30
|
|
|
|
2015-09-24 14:54:42 +05:30
|
|
|
if remarks != []: damask.util.croak(remarks)
|
2015-08-21 01:10:45 +05:30
|
|
|
if errors != []:
|
2015-09-24 14:54:42 +05:30
|
|
|
damask.util.croak(errors)
|
2015-08-21 01:10:45 +05:30
|
|
|
table.close(dismiss = True)
|
|
|
|
continue
|
|
|
|
|
|
|
|
# ------------------------------------------ assemble header ---------------------------------------
|
|
|
|
|
|
|
|
table.info_append(scriptID + '\t' + ' '.join(sys.argv[1:]))
|
|
|
|
table.head_write()
|
|
|
|
|
|
|
|
# ------------------------------------------ process data ------------------------------------------
|
|
|
|
mask = []
|
|
|
|
for col,dim in zip(columns,dims): mask += range(col,col+dim) # isolate data columns to cumulate
|
2019-10-18 02:11:18 +05:30
|
|
|
cumulated = np.ones(len(mask)) if options.product else np.zeros(len(mask)) # prepare output field
|
2015-08-21 01:10:45 +05:30
|
|
|
|
2018-01-28 04:15:53 +05:30
|
|
|
outputAlive = True
|
|
|
|
while outputAlive and table.data_read(): # read next data line of ASCII table
|
2019-10-17 22:49:51 +05:30
|
|
|
if options.product:
|
|
|
|
for i,col in enumerate(mask):
|
|
|
|
cumulated[i] *= float(table.data[col]) # cumulate values (multiplication)
|
|
|
|
else:
|
|
|
|
for i,col in enumerate(mask):
|
|
|
|
cumulated[i] += float(table.data[col]) # cumulate values (addition)
|
2018-01-28 04:15:53 +05:30
|
|
|
table.data_append(cumulated)
|
2015-08-21 01:10:45 +05:30
|
|
|
|
2018-01-28 04:15:53 +05:30
|
|
|
outputAlive = table.data_write() # output processed line
|
2015-08-21 01:10:45 +05:30
|
|
|
|
|
|
|
# ------------------------------------------ output finalization -----------------------------------
|
|
|
|
|
|
|
|
table.close() # close ASCII tables
|