1 # Copyright (C) 2009-2010 Fabrizio Benedetti
2 # Massimo Sandal <devicerandom@gmail.com>
3 # W. Trevor King <wking@drexel.edu>
5 # This file is part of Hooke.
7 # Hooke is free software: you can redistribute it and/or
8 # modify it under the terms of the GNU Lesser General Public
9 # License as published by the Free Software Foundation, either
10 # version 3 of the License, or (at your option) any later version.
12 # Hooke is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Lesser General Public License for more details.
17 # You should have received a copy of the GNU Lesser General Public
18 # License along with Hooke. If not, see
19 # <http://www.gnu.org/licenses/>.
21 """The `cut` module provides :class:`CutPlugin` and
25 from ..command import Command, Argument, Failure
26 from ..plugin import Plugin
29 class CutPlugin (Plugin):
31 super(CutPlugin, self).__init__(name='cut')
32 self._commands = [CutCommand(self)]
35 # Define common or complicated arguments
37 def current_curve_callback(hooke, command, argument, value):
38 playlist = hooke.playlists.current()
40 raise Failure('No playlists loaded')
41 curve = playlist.current()
43 raise Failure('No curves in playlist %s' % playlist.name)
46 CurveArgument = Argument(
47 name='curve', type='curve', callback=current_curve_callback,
49 :class:`hooke.curve.Curve` to cut from. Defaults to the current curve.
55 class CutCommand (Command):
56 """Cut the selected signal between two points and write it to a file.
58 The data is saved in TAB-delimited ASCII text, where the first column
59 is "x" and the second is "y". There is no header row.
61 def __init__(self, plugin):
62 super(CutCommand, self).__init__(
66 Argument(name='block', aliases=['set'], type='int', default=0,
68 Data block to save. For an approach/retract force curve, `0` selects
69 the approacing curve and `1` selects the retracting curve.
71 Argument(name='bounds', type='point', optional=False, count=2,
73 Indicies of points bounding the selected data.
75 Argument(name='output', type='file', default='cut.dat',
77 File name for the output data.
80 help=self.__doc__, plugin=plugin)
82 def _run(self, hooke, inqueue, outqueue, params):
83 if params['curve'] == None:
84 params['curve'] = hooke.playlists.current().current()
86 i_min = min([p.index for p in params['points']])
87 i_max = max([p.index for p in params['points']])
89 data = params['curve'][params['bound']]
90 cut_data = data[i_min:i_max+1,:] # slice rows from row-major data
91 # +1 to include data[i_max] row
93 f = open(params['output'], 'w')
94 cut_data.tofile(f, sep='\t')