2016-07-18 23:05:35 +05:30
|
|
|
#!/usr/bin/env python2.7
|
2014-06-06 20:05:37 +05:30
|
|
|
# -*- coding: UTF-8 no BOM -*-
|
|
|
|
|
2016-03-01 22:55:14 +05:30
|
|
|
import os,sys
|
2016-08-11 23:53:29 +05:30
|
|
|
import numpy as np
|
2014-07-25 01:51:18 +05:30
|
|
|
from optparse import OptionParser
|
|
|
|
import damask
|
2014-06-06 20:05:37 +05:30
|
|
|
|
2016-01-27 22:36:00 +05:30
|
|
|
scriptName = os.path.splitext(os.path.basename(__file__))[0]
|
|
|
|
scriptID = ' '.join([scriptName,damask.version])
|
2014-06-06 20:05:37 +05:30
|
|
|
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# MAIN
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
|
2014-07-25 01:51:18 +05:30
|
|
|
parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [file[s]]', description = """
|
2014-06-06 20:05:37 +05:30
|
|
|
Add data in column(s) of second ASCIItable selected from row that is given by the value in a mapping column.
|
|
|
|
|
2014-08-06 18:57:09 +05:30
|
|
|
""", version = scriptID)
|
2014-06-06 20:05:37 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
parser.add_option('-c','--map',
|
|
|
|
dest = 'map',
|
|
|
|
type = 'string', metavar = 'string',
|
|
|
|
help = 'heading of column containing row mapping')
|
|
|
|
parser.add_option('-o','--offset',
|
|
|
|
dest = 'offset',
|
|
|
|
type = 'int', metavar = 'int',
|
|
|
|
help = 'offset between mapping column value and actual row in mapped table [%default]')
|
|
|
|
parser.add_option('-l','--label',
|
|
|
|
dest = 'label',
|
|
|
|
action = 'extend', metavar = '<string LIST>',
|
2016-08-11 23:53:29 +05:30
|
|
|
help='column label(s) to be mapped')
|
2015-08-08 00:33:26 +05:30
|
|
|
parser.add_option('-a','--asciitable',
|
|
|
|
dest = 'asciitable',
|
|
|
|
type = 'string', metavar = 'string',
|
|
|
|
help = 'mapped ASCIItable')
|
|
|
|
|
|
|
|
parser.set_defaults(offset = 0,
|
|
|
|
)
|
2014-06-06 20:05:37 +05:30
|
|
|
|
|
|
|
(options,filenames) = parser.parse_args()
|
|
|
|
|
2016-03-02 01:41:43 +05:30
|
|
|
if options.label is None:
|
2015-08-08 00:33:26 +05:30
|
|
|
parser.error('no data columns specified.')
|
2016-03-02 01:41:43 +05:30
|
|
|
if options.map is None:
|
2015-08-08 00:33:26 +05:30
|
|
|
parser.error('no mapping column given.')
|
|
|
|
|
|
|
|
# ------------------------------------------ process mapping ASCIItable ---------------------------
|
|
|
|
|
2016-03-02 01:41:43 +05:30
|
|
|
if options.asciitable is not None and os.path.isfile(options.asciitable):
|
2014-06-06 20:05:37 +05:30
|
|
|
|
2015-08-21 01:12:05 +05:30
|
|
|
mappedTable = damask.ASCIItable(name = options.asciitable,
|
2016-08-11 23:53:29 +05:30
|
|
|
buffered = False,
|
|
|
|
readonly = True)
|
2015-08-08 00:33:26 +05:30
|
|
|
mappedTable.head_read() # read ASCII header info of mapped table
|
|
|
|
missing_labels = mappedTable.data_readArray(options.label)
|
2014-06-06 20:05:37 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
if len(missing_labels) > 0:
|
2016-08-11 23:53:29 +05:30
|
|
|
damask.util.croak('column{} {} not found...'.format('s' if len(missing_labels) > 1 else '',', '.join(missing_labels)))
|
2014-10-02 10:19:46 +05:30
|
|
|
|
2014-06-06 20:05:37 +05:30
|
|
|
else:
|
2015-08-08 00:33:26 +05:30
|
|
|
parser.error('no mapped ASCIItable given.')
|
2014-06-06 20:05:37 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
# --- loop over input files -------------------------------------------------------------------------
|
|
|
|
|
2015-08-21 01:12:05 +05:30
|
|
|
if filenames == []: filenames = [None]
|
2015-08-08 00:33:26 +05:30
|
|
|
|
|
|
|
for name in filenames:
|
2016-08-11 23:53:29 +05:30
|
|
|
try: table = damask.ASCIItable(name = name,
|
|
|
|
buffered = False)
|
2015-08-21 01:12:05 +05:30
|
|
|
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()
|
|
|
|
|
|
|
|
# ------------------------------------------ sanity checks ----------------------------------------
|
|
|
|
|
|
|
|
errors = []
|
|
|
|
|
|
|
|
mappedColumn = table.label_index(options.map)
|
|
|
|
if mappedColumn < 0: errors.append('mapping column {} not found.'.format(options.map))
|
|
|
|
|
|
|
|
if errors != []:
|
2015-09-24 14:54:42 +05:30
|
|
|
damask.util.croak(errors)
|
2015-08-08 00:33:26 +05:30
|
|
|
table.close(dismiss = True)
|
2014-06-06 20:05:37 +05:30
|
|
|
continue
|
|
|
|
|
2014-08-06 20:55:18 +05:30
|
|
|
# ------------------------------------------ assemble header --------------------------------------
|
2015-08-08 00:33:26 +05:30
|
|
|
|
2015-05-10 16:59:11 +05:30
|
|
|
table.info_append(scriptID + '\t' + ' '.join(sys.argv[1:]))
|
2016-05-18 22:01:54 +05:30
|
|
|
table.labels_append(mappedTable.labels(raw = True)) # extend ASCII header with new labels
|
2014-06-06 20:05:37 +05:30
|
|
|
table.head_write()
|
|
|
|
|
2014-08-06 20:55:18 +05:30
|
|
|
# ------------------------------------------ process data ------------------------------------------
|
2015-08-08 00:33:26 +05:30
|
|
|
|
2014-06-06 20:05:37 +05:30
|
|
|
outputAlive = True
|
2014-07-25 01:51:18 +05:30
|
|
|
while outputAlive and table.data_read(): # read next data line of ASCII table
|
2016-08-11 23:53:29 +05:30
|
|
|
try:
|
|
|
|
table.data_append(mappedTable.data[int(round(float(table.data[mappedColumn])))+options.offset-1]) # add all mapped data types
|
|
|
|
except IndexError:
|
|
|
|
table.data_append(np.nan*np.ones_like(mappedTable.data[0]))
|
2014-07-25 01:51:18 +05:30
|
|
|
outputAlive = table.data_write() # output processed line
|
2014-06-06 20:05:37 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
# ------------------------------------------ output finalization -----------------------------------
|
|
|
|
|
|
|
|
table.close() # close ASCII tables
|
2014-06-06 20:05:37 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
mappedTable.close() # close mapped input ASCII table
|