From c9ec2e0f460cb624613a8936f9eaabfc1a798d23 Mon Sep 17 00:00:00 2001 From: Philip Eisenlohr Date: Tue, 29 Nov 2016 09:29:26 -0500 Subject: [PATCH] New table mapping based on shared column values renamed old addMapped to addRowMapped to better reflect true functionality. --- .../post/{addMapped.py => addRowMapped.py} | 11 +- processing/post/addValueMapped.py | 108 ++++++++++++++++++ 2 files changed, 113 insertions(+), 6 deletions(-) rename processing/post/{addMapped.py => addRowMapped.py} (92%) create mode 100755 processing/post/addValueMapped.py diff --git a/processing/post/addMapped.py b/processing/post/addRowMapped.py similarity index 92% rename from processing/post/addMapped.py rename to processing/post/addRowMapped.py index f67d88d15..16250aa1f 100755 --- a/processing/post/addMapped.py +++ b/processing/post/addRowMapped.py @@ -14,14 +14,14 @@ scriptID = ' '.join([scriptName,damask.version]) # -------------------------------------------------------------------- parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [file[s]]', description = """ -Add data in column(s) of second ASCIItable selected from row that is given by the value in a mapping column. +Add data in column(s) of second ASCIItable selected from the row indexed by the value in a mapping column. """, version = scriptID) parser.add_option('-c','--map', dest = 'map', type = 'string', metavar = 'string', - help = 'heading of column containing row mapping') + help = 'column label containing row mapping') parser.add_option('-o','--offset', dest = 'offset', type = 'int', metavar = 'int', @@ -29,11 +29,11 @@ parser.add_option('-o','--offset', parser.add_option('-l','--label', dest = 'label', action = 'extend', metavar = '', - help='column label(s) to be mapped') + help='column label(s) to be appended') parser.add_option('-a','--asciitable', dest = 'asciitable', type = 'string', metavar = 'string', - help = 'mapped ASCIItable') + help = 'indexed ASCIItable') parser.set_defaults(offset = 0, ) @@ -54,6 +54,7 @@ if options.asciitable is not None and os.path.isfile(options.asciitable): readonly = True) mappedTable.head_read() # read ASCII header info of mapped table missing_labels = mappedTable.data_readArray(options.label) + mappedTable.close() # close mapped input ASCII table if len(missing_labels) > 0: damask.util.croak('column{} {} not found...'.format('s' if len(missing_labels) > 1 else '',', '.join(missing_labels))) @@ -106,5 +107,3 @@ for name in filenames: # ------------------------------------------ output finalization ----------------------------------- table.close() # close ASCII tables - -mappedTable.close() # close mapped input ASCII table diff --git a/processing/post/addValueMapped.py b/processing/post/addValueMapped.py new file mode 100755 index 000000000..cd0f50d0d --- /dev/null +++ b/processing/post/addValueMapped.py @@ -0,0 +1,108 @@ +#!/usr/bin/env python2.7 +# -*- coding: UTF-8 no BOM -*- + +import os,sys +import numpy as np +from optparse import OptionParser +import damask + +scriptName = os.path.splitext(os.path.basename(__file__))[0] +scriptID = ' '.join([scriptName,damask.version]) + +# -------------------------------------------------------------------- +# MAIN +# -------------------------------------------------------------------- + +parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [file[s]]', description = """ +Add data of selected column(s) from (first) row of second ASCIItable that shares the mapping column value. + +""", version = scriptID) + +parser.add_option('-c','--map', + dest = 'map', nargs = 2, + type = 'string', metavar = 'string string', + help = 'column labels containing linked values') +parser.add_option('-l','--label', + dest = 'label', + action = 'extend', metavar = '', + help='column label(s) to be appended') +parser.add_option('-a','--asciitable', + dest = 'asciitable', + type = 'string', metavar = 'string', + help = 'indexed ASCIItable') + +parser.set_defaults() + +(options,filenames) = parser.parse_args() + +if options.label is None: + parser.error('no data columns specified.') +if options.map is None: + parser.error('no mapping columns given.') + +# ------------------------------------------ process mapping ASCIItable --------------------------- + +if options.asciitable is not None and os.path.isfile(options.asciitable): + + mappedTable = damask.ASCIItable(name = options.asciitable, + buffered = False, + readonly = True) + mappedTable.head_read() # read ASCII header info of mapped table + if mappedTable.label_dimension(options.map[1]) != 1: + parser.error('mapping column {} needs to be scalar valued.'.format(options.map[1])) + + missing_labels = mappedTable.data_readArray([options.map[1]]+options.label) + mappedTable.close() # close mapped input ASCII table + + if len(missing_labels) > 0: + damask.util.croak('column{} {} not found...'.format('s' if len(missing_labels) > 1 else '',', '.join(missing_labels))) + +else: + parser.error('no indexed ASCIItable given.') + +# --- loop over input files ------------------------------------------------------------------------- + +if filenames == []: filenames = [None] + +for name in filenames: + try: table = damask.ASCIItable(name = name, + buffered = False) + except: continue + damask.util.report(scriptName,name) + +# ------------------------------------------ read header ------------------------------------------ + + table.head_read() + +# ------------------------------------------ sanity checks ---------------------------------------- + + errors = [] + + mappedColumn = table.label_index(options.map[0]) + if mappedColumn < 0: errors.append('mapping column {} not found.'.format(options.map[0])) + + if errors != []: + damask.util.croak(errors) + table.close(dismiss = True) + continue + +# ------------------------------------------ assemble header -------------------------------------- + + table.info_append(scriptID + '\t' + ' '.join(sys.argv[1:])) + table.labels_append(mappedTable.labels(raw = True)[1:]) # extend with new labels (except for mapped column) + table.head_write() + +# ------------------------------------------ process data ------------------------------------------ + + outputAlive = True + while outputAlive and table.data_read(): # read next data line of ASCII table + try: + table.data_append(mappedTable.data[np.argwhere(mappedTable.data[:,0] == + float(table.data[mappedColumn]))[0]]) # add data from first matching line + except IndexError: + table.data_append(np.nan*np.ones_like(mappedTable.data[0])) # or add NaNs + outputAlive = table.data_write() # output processed line + +# ------------------------------------------ output finalization ----------------------------------- + + table.close() # close ASCII tables