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 = """
|
2016-11-30 03:00:07 +05:30
|
|
|
Add data of selected column(s) from (first) row of second ASCIItable that shares the linking column value.
|
2014-06-06 20:05:37 +05:30
|
|
|
|
2014-08-06 18:57:09 +05:30
|
|
|
""", version = scriptID)
|
2014-06-06 20:05:37 +05:30
|
|
|
|
2016-11-30 03:00:07 +05:30
|
|
|
parser.add_option('--link',
|
|
|
|
dest = 'link', nargs = 2,
|
2016-11-29 19:59:26 +05:30
|
|
|
type = 'string', metavar = 'string string',
|
|
|
|
help = 'column labels containing linked values')
|
2015-08-08 00:33:26 +05:30
|
|
|
parser.add_option('-l','--label',
|
|
|
|
dest = 'label',
|
|
|
|
action = 'extend', metavar = '<string LIST>',
|
2016-11-30 01:14:34 +05:30
|
|
|
help = 'column label(s) to be appended')
|
2015-08-08 00:33:26 +05:30
|
|
|
parser.add_option('-a','--asciitable',
|
|
|
|
dest = 'asciitable',
|
|
|
|
type = 'string', metavar = 'string',
|
2016-11-30 03:00:07 +05:30
|
|
|
help = 'linked ASCIItable')
|
2015-08-08 00:33:26 +05:30
|
|
|
|
2016-11-29 19:59:26 +05:30
|
|
|
parser.set_defaults()
|
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-11-30 03:00:07 +05:30
|
|
|
if options.link is None:
|
|
|
|
parser.error('no linking columns given.')
|
2015-08-08 00:33:26 +05:30
|
|
|
|
2016-11-30 03:00:07 +05:30
|
|
|
# -------------------------------------- process linked ASCIItable --------------------------------
|
2015-08-08 00:33:26 +05:30
|
|
|
|
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
|
|
|
|
2016-11-30 03:00:07 +05:30
|
|
|
linkedTable = damask.ASCIItable(name = options.asciitable,
|
2016-08-11 23:53:29 +05:30
|
|
|
buffered = False,
|
|
|
|
readonly = True)
|
2016-11-30 03:00:07 +05:30
|
|
|
linkedTable.head_read() # read ASCII header info of linked table
|
2017-07-31 19:04:30 +05:30
|
|
|
linkDim = linkedTable.label_dimension(options.link[1]) # dimension of linking column
|
2016-11-29 19:59:26 +05:30
|
|
|
|
2017-07-31 19:04:30 +05:30
|
|
|
missing_labels = linkedTable.data_readArray([options.link[1]]+options.label) # try reading linked ASCII table
|
2016-11-30 03:00:07 +05:30
|
|
|
linkedTable.close() # close linked ASCII table
|
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
|
|
|
|
2017-07-31 19:04:30 +05:30
|
|
|
index = linkedTable.data[:,:linkDim]
|
|
|
|
data = linkedTable.data[:,linkDim:]
|
2014-06-06 20:05:37 +05:30
|
|
|
else:
|
2016-11-30 03:00:07 +05:30
|
|
|
parser.error('no linked ASCIItable given.')
|
2014-06-06 20:05:37 +05:30
|
|
|
|
2016-11-30 03:00:07 +05:30
|
|
|
# --- loop over input files -----------------------------------------------------------------------
|
2015-08-08 00:33:26 +05:30
|
|
|
|
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 = []
|
|
|
|
|
2017-07-31 19:04:30 +05:30
|
|
|
myLink = table.label_index (options.link[0])
|
|
|
|
myLinkDim = table.label_dimension(options.link[0])
|
|
|
|
if myLink < 0: errors.append('linking column {} not found.'.format(options.link[0]))
|
|
|
|
if myLinkDim != linkDim: errors.append('dimension mismatch for column {}.'.format(options.link[0]))
|
2015-08-08 00:33:26 +05:30
|
|
|
|
|
|
|
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:]))
|
2017-07-31 19:04:30 +05:30
|
|
|
table.labels_append(linkedTable.labels(raw = True)[linkDim:]) # extend with new labels (except for linked column)
|
2016-11-29 21:46:50 +05:30
|
|
|
|
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:
|
2017-07-31 19:32:58 +05:30
|
|
|
table.data_append(data[np.argwhere(np.all((map(float,table.data[myLink:myLink+myLinkDim]) - index)==0,axis=1))[0]]) # add data of first matching line
|
2016-08-11 23:53:29 +05:30
|
|
|
except IndexError:
|
2016-11-29 21:46:50 +05:30
|
|
|
table.data_append(np.nan*np.ones_like(data[0])) # or add NaNs
|
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
|