From ef5c42872164611bd5b42a43544d27eb9a53e843 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Thu, 3 Jun 2010 12:13:00 -0400 Subject: [PATCH] Add headers to output of 'cut' and 'export block'. Both controlled by new 'header' arguments (optional, True by default). --- hooke/plugin/curve.py | 14 +++++++++++++- hooke/plugin/cut.py | 15 ++++++++++++--- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/hooke/plugin/curve.py b/hooke/plugin/curve.py index dc86978..9471318 100644 --- a/hooke/plugin/curve.py +++ b/hooke/plugin/curve.py @@ -126,6 +126,9 @@ class InfoCommand (Command): class ExportCommand (Command): """Export a :class:`hooke.curve.Curve` data block as TAB-delimeted ASCII text. + + A "#" prefixed header will optionally appear at the beginning of + the file naming the columns. """ def __init__(self, plugin): super(ExportCommand, self).__init__( @@ -140,13 +143,22 @@ the approaching curve and `1` selects the retracting curve. Argument(name='output', type='file', default='curve.dat', help=""" File name for the output data. Defaults to 'curve.dat' +""".strip()), + Argument(name='header', type='bool', default=True, + help=""" +True if you want the column-naming header line. """.strip()), ], help=self.__doc__, plugin=plugin) def _run(self, hooke, inqueue, outqueue, params): data = params['curve'].data[int(params['block'])] # HACK, int() should be handled by ui - numpy.savetxt(params['output'], data, delimiter='\t') + + f = open(params['output'], 'w') + if params['header'] == True: + f.write('# %s \n' % ('\t'.join(data.info['columns']))) + numpy.savetxt(f, data, delimiter='\t') + f.close() class DifferenceCommand (Command): """Calculate the derivative (actually, the discrete differentiation) diff --git a/hooke/plugin/cut.py b/hooke/plugin/cut.py index d42338d..9eb112b 100644 --- a/hooke/plugin/cut.py +++ b/hooke/plugin/cut.py @@ -57,8 +57,9 @@ CurveArgument = Argument( class CutCommand (Command): """Cut the selected signal between two points and write it to a file. - The data is saved in TAB-delimited ASCII text, where the first column - is "x" and the second is "y". There is no header row. + The data is saved in TAB-delimited ASCII text. A "#" prefixed + header will optionally appear at the beginning of the file naming + the columns. """ def __init__(self, plugin): super(CutCommand, self).__init__( @@ -77,6 +78,10 @@ Indicies of points bounding the selected data. Argument(name='output', type='file', default='cut.dat', help=""" File name for the output data. +""".strip()), + Argument(name='header', type='bool', default=True, + help=""" +True if you want the column-naming header line. """.strip()), ], help=self.__doc__, plugin=plugin) @@ -92,4 +97,8 @@ File name for the output data. cut_data = data[i_min:i_max+1,:] # slice rows from row-major data # +1 to include data[i_max] row - numpy.savetxt(params['output'], cut_data, delimiter='\t') + f = open(params['output'], 'w') + if params['header'] == True: + f.write('# %s \n' % ('\t'.join(cut_data.info['columns']))) + numpy.savetxt(f, cut_data, delimiter='\t') + f.close() -- 2.26.2