Rework Plugin.commands() to include _setup_commands().
[hooke.git] / hooke / plugin / cut.py
1 # Copyright (C) 2009-2010 Fabrizio Benedetti
2 #                         Massimo Sandal <devicerandom@gmail.com>
3 #                         W. Trevor King <wking@drexel.edu>
4 #
5 # This file is part of Hooke.
6 #
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.
11 #
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.
16 #
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/>.
20
21 """The `cut` module provides :class:`CutPlugin` and
22 :class:`CutCommand`.
23 """
24
25 from ..command import Command, Argument, Failure
26 from ..plugin import Plugin
27
28
29 class CutPlugin (Plugin):
30     def __init__(self):
31         super(CutPlugin, self).__init__(name='cut')
32         self._commands = [CutCommand()]
33         self._setup_commands()
34
35
36 # Define common or complicated arguments
37
38 def current_curve_callback(hooke, command, argument, value):
39     playlist = hooke.playlists.current()
40     if playlist == None:
41         raise Failure('No playlists loaded')
42     curve = playlist.current()
43     if curve == None:
44         raise Failure('No curves in playlist %s' % playlist.name)
45     return curve
46
47 CurveArgument = Argument(
48     name='curve', type='curve', callback=current_curve_callback,
49     help="""
50 :class:`hooke.curve.Curve` to cut from.  Defaults to the current curve.
51 """.strip())
52
53
54 # Define commands
55
56 class CutCommand (Command):
57     """Cut the selected signal between two points and write it to a file.
58
59     The data is saved in TAB-delimited ASCII text, where the first column
60     is "x" and the second is "y".  There is no header row.
61     """
62     def __init__(self):
63         super(CutCommand, self).__init__(
64             name='cut',
65             arguments=[
66                 CurveArgument,
67                 Argument(name='block', aliases=['set'], type='int', default=0,
68                     help="""
69 Data block to save.  For an approach/retract force curve, `0` selects
70 the approacing curve and `1` selects the retracting curve.
71 """.strip()),
72                 Argument(name='bounds', type='point', optional=False, count=2,
73                          help="""
74 Indicies of points bounding the selected data.
75 """.strip()),
76                 Argument(name='output', type='file', default='cut.dat',
77                          help="""
78 File name for the output data.
79 """.strip()),
80                 ],
81             help=self.__doc__)
82
83     def _run(self, hooke, inqueue, outqueue, params):
84         if params['curve'] == None:
85             params['curve'] = hooke.playlists.current().current()
86
87         i_min = min([p.index for p in params['points']])
88         i_max = max([p.index for p in params['points']])
89
90         data = params['curve'][params['bound']]
91         cut_data = data[i_min:i_max+1,:] # slice rows from row-major data
92         # +1 to include data[i_max] row
93
94         f = open(params['output'], 'w')
95         cut_data.tofile(f, sep='\t')
96         f.close()