2019-02-15 04:54:09 +05:30
|
|
|
#!/usr/bin/env python3
|
2014-06-06 20:05:37 +05:30
|
|
|
|
2019-06-14 16:33:30 +05:30
|
|
|
import os
|
|
|
|
import sys
|
2014-07-25 01:51:18 +05:30
|
|
|
from optparse import OptionParser
|
2019-06-14 16:33:30 +05:30
|
|
|
|
|
|
|
import numpy as np
|
|
|
|
|
2014-07-25 01:51:18 +05:30
|
|
|
import damask
|
2014-06-06 20:05:37 +05:30
|
|
|
|
2019-06-14 16:33:30 +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
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
|
2019-02-17 02:50:10 +05:30
|
|
|
parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [ASCIItable(s)]', description = """
|
2017-08-16 01:42:06 +05:30
|
|
|
Add data of selected column(s) from (first) row of linked 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',
|
2019-02-15 04:54:09 +05:30
|
|
|
help = 'column labels of table and linked table containing linking values')
|
2015-08-08 00:33:26 +05:30
|
|
|
parser.add_option('-l','--label',
|
|
|
|
dest = 'label',
|
|
|
|
action = 'extend', metavar = '<string LIST>',
|
2017-08-16 01:42:06 +05:30
|
|
|
help = 'column label(s) to add from linked ASCIItable')
|
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
|
|
|
|
2020-02-20 19:35:38 +05:30
|
|
|
linkedTable = damask.ASCIItable(name = options.asciitable, 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)))
|
2017-08-24 03:23:30 +05:30
|
|
|
if len(missing_labels) >= len(options.label):
|
|
|
|
damask.util.croak('aborting...')
|
|
|
|
sys.exit()
|
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:
|
2020-02-20 19:35:38 +05:30
|
|
|
try:
|
|
|
|
table = damask.ASCIItable(name = name)
|
|
|
|
except IOError:
|
|
|
|
continue
|
2017-08-23 23:40:22 +05:30
|
|
|
damask.util.report(scriptName,"{} {} <== {} {}".format(name,damask.util.deemph('@ '+options.link[0]),
|
|
|
|
options.asciitable,damask.util.deemph('@ '+options.link[1])))
|
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:
|
2019-02-15 04:56:07 +05:30
|
|
|
table.data_append(data[np.argwhere(np.all((list(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
|