DAMASK_EICMD/processing/post/addIndexed.py

65 lines
2.2 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
import os
import sys
2020-01-15 11:07:53 +05:30
from io import StringIO
2014-07-25 01:51:18 +05:30
from optparse import OptionParser
import numpy as np
2014-07-25 01:51:18 +05:30
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 data in column(s) of mapped ASCIItable selected from the row indexed by the value in a mapping column.
Row numbers start at 1.
""", version = scriptID)
parser.add_option('--index',
dest = 'index',
type = 'string', metavar = 'string',
help = 'column label containing row index')
parser.add_option('-o','--offset',
dest = 'offset',
type = 'int', metavar = 'int',
help = 'constant offset for index column value [%default]')
parser.add_option('-l','--label',
dest = 'label',
action = 'extend', metavar = '<string LIST>',
help = 'column label(s) to be appended')
parser.add_option('-a','--asciitable',
dest = 'asciitable',
type = 'string', metavar = 'string',
help = 'indexed ASCIItable')
parser.set_defaults(offset = 0,
)
(options,filenames) = parser.parse_args()
2020-01-15 11:07:53 +05:30
if filenames == []: filenames = [None]
if options.label is None:
parser.error('no data columns specified.')
if options.index is None:
parser.error('no index column given.')
for name in filenames:
2020-01-15 11:07:53 +05:30
damask.util.report(scriptName,name)
2020-01-15 11:07:53 +05:30
table = damask.Table.from_ASCII(StringIO(''.join(sys.stdin.read())) if name is None else name)
indexedTable = damask.Table.from_ASCII(options.asciitable)
idx = np.reshape(table.get(options.index).astype(int) + options.offset,(-1))-1
2020-09-03 20:49:19 +05:30
2020-01-15 11:07:53 +05:30
for data in options.label:
2020-03-19 01:19:39 +05:30
table.add(data+'_addIndexed',indexedTable.get(data)[idx],scriptID+' '+' '.join(sys.argv[1:]))
2020-09-03 20:49:19 +05:30
table.to_file(sys.stdout if name is None else name)