Run update-copyright.py.
[hooke.git] / hooke / plugin / cut.py
1 # Copyright (C) 2009-2012 Massimo Sandal <devicerandom@gmail.com>
2 #                         W. Trevor King <wking@drexel.edu>
3 #
4 # This file is part of Hooke.
5 #
6 # Hooke is free software: you can redistribute it and/or modify it under the
7 # terms of the GNU Lesser General Public License as published by the Free
8 # Software Foundation, either version 3 of the License, or (at your option) any
9 # later version.
10 #
11 # Hooke is distributed in the hope that it will be useful, but WITHOUT ANY
12 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13 # A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
14 # details.
15 #
16 # You should have received a copy of the GNU Lesser General Public License
17 # along with Hooke.  If not, see <http://www.gnu.org/licenses/>.
18
19 """The `cut` module provides :class:`CutPlugin` and
20 :class:`CutCommand`.
21 """
22
23 import os.path
24
25 import numpy
26
27 from ..command import Command, Argument, Failure
28 from . import Plugin
29
30
31 class CutPlugin (Plugin):
32     def __init__(self):
33         super(CutPlugin, self).__init__(name='cut')
34         self._commands = [CutCommand(self)]
35
36
37 # Define common or complicated arguments
38
39 def current_curve_callback(hooke, command, argument, value):
40     playlist = hooke.playlists.current()
41     if playlist == None:
42         raise Failure('No playlists loaded')
43     curve = playlist.current()
44     if curve == None:
45         raise Failure('No curves in playlist %s' % playlist.name)
46     return curve
47
48 CurveArgument = Argument(
49     name='curve', type='curve', callback=current_curve_callback,
50     help="""
51 :class:`hooke.curve.Curve` to cut from.  Defaults to the current curve.
52 """.strip())
53
54
55 # Define commands
56
57 class CutCommand (Command):
58     """Cut the selected signal between two points and write it to a file.
59
60     The data is saved in TAB-delimited ASCII text.  A "#" prefixed
61     header will optionally appear at the beginning of the file naming
62     the columns.
63     """
64     def __init__(self, plugin):
65         super(CutCommand, self).__init__(
66             name='cut',
67             arguments=[
68                 CurveArgument,
69                 Argument(name='block', aliases=['set'], type='int', default=0,
70                     help="""
71 Data block to save.  For an approach/retract force curve, `0` selects
72 the approaching curve and `1` selects the retracting curve.
73 """.strip()),
74                 Argument(name='bounds', type='point', optional=False, count=2,
75                          help="""
76 Indicies of points bounding the selected data.
77 """.strip()),
78                 Argument(name='output', type='file', default='cut.dat',
79                          help="""
80 File name for the output data.
81 """.strip()),
82                 Argument(name='header', type='bool', default=True,
83                          help="""
84 True if you want the column-naming header line.
85 """.strip()),
86                 ],
87             help=self.__doc__, plugin=plugin)
88
89     def _run(self, hooke, inqueue, outqueue, params):
90         if params['curve'] == None:
91             params['curve'] = hooke.playlists.current().current()
92
93         i_min = min([p.index for p in params['points']])
94         i_max = max([p.index for p in params['points']])
95
96         data = params['curve'][params['bound']]
97         cut_data = data[i_min:i_max+1,:] # slice rows from row-major data
98         # +1 to include data[i_max] row
99
100         f = open(os.path.expanduser(params['output']), 'w')
101         if params['header'] == True:
102             f.write('# %s \n' % ('\t'.join(cut_data.info['columns'])))
103         numpy.savetxt(f, cut_data, delimiter='\t')
104         f.close()