2014-06-06 20:05:37 +05:30
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: UTF-8 no BOM -*-
|
|
|
|
|
2014-08-06 20:55:18 +05:30
|
|
|
import os,sys,string
|
2014-07-25 01:51:18 +05:30
|
|
|
from optparse import OptionParser
|
|
|
|
import damask
|
2014-06-06 20:05:37 +05:30
|
|
|
|
2014-09-26 03:24:42 +05:30
|
|
|
scriptID = string.replace('$Id$','\n','\\n')
|
2014-12-19 00:56:52 +05:30
|
|
|
scriptName = os.path.splitext(scriptID.split()[1])[0]
|
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>',
|
|
|
|
help='heading of column(s) to be mapped')
|
|
|
|
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()
|
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
if options.label == None:
|
|
|
|
parser.error('no data columns specified.')
|
2014-07-25 01:51:18 +05:30
|
|
|
if options.map == None:
|
2015-08-08 00:33:26 +05:30
|
|
|
parser.error('no mapping column given.')
|
|
|
|
|
|
|
|
# ------------------------------------------ process mapping ASCIItable ---------------------------
|
|
|
|
|
2014-06-06 20:05:37 +05:30
|
|
|
if options.asciitable != None and os.path.isfile(options.asciitable):
|
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
mappedTable = damask.ASCIItable(name = options.asciitable,buffered = False, readonly = True)
|
|
|
|
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:
|
|
|
|
mappedTable.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 -------------------------------------------------------------------------
|
|
|
|
|
|
|
|
if filenames == []: filenames = ['STDIN']
|
|
|
|
|
|
|
|
for name in filenames:
|
|
|
|
if not (name == 'STDIN' or os.path.exists(name)): continue
|
|
|
|
table = damask.ASCIItable(name = name, outname = name+'_tmp',
|
|
|
|
buffered = False)
|
|
|
|
table.croak('\033[1m'+scriptName+'\033[0m'+(': '+name if name != 'STDIN' else ''))
|
|
|
|
|
|
|
|
# ------------------------------------------ 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 != []:
|
|
|
|
table.croak(errors)
|
|
|
|
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:]))
|
2015-08-08 00:33:26 +05:30
|
|
|
table.labels_append(mappedTable.labels) # 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
|
|
|
|
table.data_append(mappedTable.data[int(table.data[mappedColumn])+options.offset-1]) # add all mapped data types
|
|
|
|
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
|
|
|
|
if name != 'STDIN': os.rename(name+'_tmp',name) # overwrite old one with tmp new
|
2014-06-06 20:05:37 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
mappedTable.close() # close mapped input ASCII table
|