From a2b46c8a36bc63a77cd5d3c7fad2834f7ca82809 Mon Sep 17 00:00:00 2001 From: Philip Eisenlohr Date: Thu, 23 Feb 2012 13:53:12 +0000 Subject: [PATCH] columns can be called by index now pipe-aware... --- processing/post/addCalculation.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/processing/post/addCalculation.py b/processing/post/addCalculation.py index b28e3ee5b..002626799 100755 --- a/processing/post/addCalculation.py +++ b/processing/post/addCalculation.py @@ -29,6 +29,7 @@ class extendableOption(Option): parser = OptionParser(option_class=extendableOption, usage='%prog options [file[s]]', description = """ Add column(s) with derived values according to user defined arithmetic operation between column(s). +Columns can be specified either by label or index. Example: distance to IP coordinates -- "math.sqrt( #ip.x#**2 + #ip.y#**2 + #ip.z#**2 )" """ + string.replace('$Id$','\n','\\n') @@ -81,6 +82,11 @@ for file in files: formula = formula.replace('#'+operand+'#','{%i}'%position) if operand in specials: interpolator += ['specials["%s"]'%operand] + elif operand.isdigit(): + if len(table.labels) > int(operand): + interpolator += ['float(table.data[%i])'%(int(operand))] + else: + parser.error('column %s not present...\n'%operand) else: try: interpolator += ['float(table.data[%i])'%table.labels.index(operand)] @@ -96,17 +102,18 @@ for file in files: # ------------------------------------------ process data --------------------------------------- - while table.data_read(): # read next data line of ASCII table + outputAlive = True + while outputAlive and table.data_read(): # read next data line of ASCII table specials['_row_'] += 1 # count row for label in options.labels: table.data_append(eval(eval(evaluator[label]))) - table.data_write() # output processed line + outputAlive = table.data_write() # output processed line # ------------------------------------------ output result --------------------------------------- - table.output_flush() # just in case of buffered ASCII table + outputAlive and table.output_flush() # just in case of buffered ASCII table file['input'].close() # close input ASCII table if file['name'] != 'STDIN': - file['output'].close # close output ASCII table + file['output'].close() # close output ASCII table os.rename(file['name']+'_tmp',file['name']) # overwrite old one with tmp new