Ran update_copyright.py, updating all the copyright blurbs and adding AUTHORS.
[hooke.git] / hooke / ui / gui / driver.py
1 # Copyright (C) 2010 W. Trevor King <wking@drexel.edu>
2 #
3 # This file is part of Hooke.
4 #
5 # Hooke is free software: you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation, either
8 # version 3 of the License, or (at your option) any later version.
9 #
10 # Hooke is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU Lesser General Public License for more details.
14 #
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with Hooke.  If not, see
17 # <http://www.gnu.org/licenses/>.
18
19
20     def _generate_vectors(self):
21         """
22         Here we parse the data and generate the raw vectors. This
23         method has only to do with the peculiar file format here, so
24         it's of no big interest (I just wrote it to present a
25         functional driver).
26
27         Only thing to remember, it can be nice to call methods that
28         are used only "internally" by the driver (or by plugins) with
29         a "_" prefix, so to have a visual remark. But it's just an
30         advice.
31         """
32         vectors={'PLOT1':[[],[],[],[]] , 'PLOT2':[[],[],[],[]]}
33         positions={'X1':0,'Y1':1,'X2':2,'Y2':3}
34         whatplot=''
35         pos=0
36         for item in self.data:
37             try:
38                 num=float(item)
39                 vectors[whatplot][pos].append(num)
40             except ValueError:
41                 if item[:-1]=='PLOT1':
42                     whatplot=item[:-1]
43                 elif item[:-1]=='PLOT2':
44                     whatplot=item[:-1]
45                 elif item[0]=='X' or item[0]=='Y':
46                     pos=positions[item[:-1]]
47                 else:
48                     pass
49
50         return vectors
51
52     def default_plots(self):
53         """
54         THIS METHOD MUST BE DEFINED.
55         RETURNS: [ lhc.PlotObject ] or [ lhc.PlotObject, lhc.PlotObject]
56
57         This is the method that returns the plots to Hooke.
58         It must return a list with *one* or *two* PlotObjects.
59
60         See the curve.py source code to see how PlotObjects are defined and work in detail.
61         """
62         gen_vectors=self._generate_vectors()
63
64         #Here we create the main plot PlotObject and initialize its vectors.
65         main_plot=lhc.PlotObject()
66         main_plot.vectors=[]
67         #The same for the other plot.
68         other_plot=lhc.PlotObject()
69         other_plot.vectors=[]
70
71         """
72         Now we fill the plot vectors with our data.
73                                                            set 1                           set 2
74         The "correct" shape of the vector is [ [[x1,x2,x3...],[y1,y2,y3...]] , [[x1,x2,x3...],[y1,y2,y3...]] ], so we have to put stuff in this way into it.
75
76         The add_set() method takes care of this , just use plot.add_set(x,y).
77         """
78         main_plot.add_set(gen_vectors['PLOT1'][0],gen_vectors['PLOT1'][1])
79         main_plot.add_set(gen_vectors['PLOT1'][2],gen_vectors['PLOT1'][3])
80
81         other_plot.add_set(gen_vectors['PLOT2'][0],gen_vectors['PLOT2'][1])
82         other_plot.add_set(gen_vectors['PLOT2'][2],gen_vectors['PLOT2'][3])
83
84         """
85         normalize_vectors() trims the vectors, so that if two x/y couples are of different lengths, the latest
86         points are trimmed (otherwise we have a python error). Always a good idea to run it, to avoid crashes.
87         """
88         main_plot.normalize_vectors()
89         other_plot.normalize_vectors()
90
91         """
92         Here we define:
93         - units: [string, string], define the measure units of X and Y axes
94         - destination: 0/1 , defines where to plot the plot (0=top, 1=bottom), default=0
95         - title: string , the plot title.
96
97         for each plot.
98         Again, see curve.py comments for details.
99         """
100         main_plot.units=['unit of x','unit of y']
101         main_plot.destination=0
102         main_plot.title=self.filename+' main'
103
104         other_plot.units=['unit of x','unit of y']
105         other_plot.destination=1
106         other_plot.title=self.filename+' other'
107
108         return [main_plot, other_plot]