dc55d89161a719e27bb81d8076d2186c2a4420bd
[hooke.git] / hooke / plugin / cut.py
1 """Defines :class:`CutPlugin` and :class:`CutCommand`.
2 """
3
4 from ..command import Command, Argument
5 from ..plugin import Plugin
6
7
8 class CutPlugin (Plugin):
9     def __init__(self):
10         super(CutPlugin, self).__init__(name='cut')
11
12     def commands(self):
13         return [CutCommand()]
14
15 class CutCommand (Command):
16     """Cut the selected signal between two points and write it to a file.
17
18     The data is saved in TAB-delimited ASCII text, where the first column
19     is "x" and the second is "y".  There is no header row.
20     """
21     def __init__(self):
22         super(CutCommand, self).__init__(
23             name='cut',
24             arguments=[
25                 Argument(name='curve', type='curve', help="""
26 :class:`hooke.curve.Curve` to cut from.  Defaults to the current curve.
27 """.strip()),
28                 Argument(name='block', aliases=['set'], type='int', default=0,
29                     help="""
30 Data block to save.  For an approach/retract force curve, `0` selects
31 the approacing curve and `1` selects the retracting curve.
32 """.strip()),
33                 Argument(name='bounds', type='point', optional=False, count=2,
34                          help="""
35 Indicies of points bounding the selected data.
36 """.strip()),
37                 Argument(name='output', type='file', default='cut.dat',
38                          help="""
39 File name for the output data.
40 """.strip()),
41                 ],
42             help=self.__doc__)
43
44     def _run(self, hooke, inqueue, outqueue, params):
45         if params['curve'] == None:
46             params['curve'] = hooke.playlists.current().current()
47
48         i_min = min([p.index for p in params['points']])
49         i_max = max([p.index for p in params['points']])
50
51         data = params['curve'][params['bound']]
52         cut_data = data[i_min:i_max+1,:] # slice rows from row-major data
53         # +1 to include data[i_max] row
54
55         f = open(params['output'], 'w')
56         cut_data.tofile(f, sep='\t')
57         f.close()