DAMASK_EICMD/processing/legacy/addCumulative.py

49 lines
1.6 KiB
Python
Raw Normal View History

2019-02-15 03:26:39 +05:30
#!/usr/bin/env python3
import os
import sys
2019-12-22 00:48:33 +05:30
from io import StringIO
from optparse import OptionParser
import numpy as np
import damask
scriptName = os.path.splitext(os.path.basename(__file__))[0]
scriptID = ' '.join([scriptName,damask.version])
# --------------------------------------------------------------------
# MAIN
# --------------------------------------------------------------------
2019-02-16 22:11:56 +05:30
parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [ASCIItable(s)]', description = """
Add cumulative (sum of first to current row) values for given label(s).
""", version = scriptID)
parser.add_option('-l','--label',
2019-12-22 00:48:33 +05:30
dest='labels',
action = 'extend', metavar = '<string LIST>',
help = 'columns to cumulate')
parser.add_option('-p','--product',
dest='product', action = 'store_true',
help = 'product of values instead of sum')
(options,filenames) = parser.parse_args()
2019-12-22 00:48:33 +05:30
if filenames == []: filenames = [None]
2019-12-22 00:48:33 +05:30
if options.labels is None:
parser.error('no data column(s) specified.')
for name in filenames:
2019-12-22 00:48:33 +05:30
damask.util.report(scriptName,name)
2019-12-22 00:48:33 +05:30
table = damask.Table.from_ASCII(StringIO(''.join(sys.stdin.read())) if name is None else name)
for label in options.labels:
table.add('cum_{}({})'.format('prod' if options.product else 'sum',label),
np.cumprod(table.get(label),0) if options.product else np.cumsum(table.get(label),0),
scriptID+' '+' '.join(sys.argv[1:]))
2020-09-03 20:49:19 +05:30
table.to_file(sys.stdout if name is None else name)