5b83e0cacdb6792ac295cd62fe0a4845a3ef6d3f
[hooke.git] / hooke / libhookecurve.py
1 class HookeCurve(object):
2
3     def __init__(self,path):
4         self.path=path
5         self.curve=Driver()
6         self.notes=''
7
8     def identify(self, drivers):
9         '''
10         identifies a curve and returns the corresponding object
11         '''
12         for driver in drivers:
13             tempcurve=driver(self.path)
14             if tempcurve.is_me():
15                 #bring on all the driver, with his load of methods etc.
16                 #so we can access the whole of it.
17                 self.curve=tempcurve
18                 del tempcurve
19                 return True
20
21         print 'Not a recognizable curve format.'
22         return False
23
24
25 class Driver:
26     '''
27     Base class for file format drivers.
28
29     To be overridden
30     '''
31     def __init__(self):
32         self.experiment=''
33         self.filetype=''
34
35     def is_me(self):
36         '''
37         This method must read the file and return True if the filetype can be managed by the driver, False if not.
38         '''
39         return False
40
41     def close_all(self):
42         '''
43         This method must close all the open files of the driver, explicitly.
44         '''
45         return None
46
47     def default_plots(self):
48         dummy_default=PlotObject()
49         dummy_default.vectors.append([[[0]],[[0]]])
50         return [dummy_default]
51
52
53 class PlotObject:
54
55     def __init__(self):
56
57         '''
58         the plot destination
59         0=top
60         1=bottom
61         '''
62         self.destination=0
63
64         '''
65         self.vectors is a multidimensional array:
66         self.vectors[0]=plot1
67         self.vectors[1]=plot2
68         self.vectors[2]=plot3
69         etc.
70
71         2 curves in a x,y plot are:
72         [[[x1],[y1]],[[x2],[y2]]]
73         for example:
74             x1          y1              x2         y2
75         [[[1,2,3,4],[10,20,30,40]],[[3,6,9,12],[30,60,90,120]]]
76         x1 = self.vectors[0][0]
77         y1 = self.vectors[0][1]
78         x2 = self.vectors[1][0]
79         y2 = self.vectors[1][1]
80         '''
81         self.vectors=[]
82
83         '''
84         self.units is simpler. for each plot with N axes (x,y,z...) only N labels
85         can be made, regardless of the number of superimposed plots
86         so units for the double plot above is: [unitx, unity]
87
88         units are strings
89         '''
90         self.units=['','']
91
92         '''
93         xaxes and yaxes directions. 0,0 means the common +X=right, +Y=top directions
94         '''
95         self.xaxes=0
96         self.yaxes=0
97
98         self.title='' #title
99
100         '''
101         styles: defines what is the style of the current plots. If undefined or None, it is line plot.
102         If an element of the list is 'scatter', the corresponding dataset
103         is drawn with scattered points and not a continuous line.
104         '''
105         self.styles=[]
106
107         '''
108         colors: define what is the colour of the current plots
109         '''
110         self.colors=[]
111
112     def add_set(self,x,y):
113         '''
114         Adds an x,y data set to the vectors.
115         '''
116         self.vectors.append([])
117         self.vectors[-1].append(x)
118         self.vectors[-1].append(y)
119         return
120
121     def remove_set(self,whichset):
122         '''
123         Removes a set
124         '''
125         waste=self.vectors.pop(whichset)
126         return
127
128     def normalize_vectors(self):
129         '''
130         Trims the vector lengths as to be equal in a plot.
131         '''
132
133         for index in range(0,len(self.vectors)):
134             vectors_to_plot=self.vectors[index]
135             lengths=[len(vector) for vector in vectors_to_plot]
136             if min(lengths) != max(lengths):
137                 for indexplot in range(0,len(vectors_to_plot)):
138                     self.vectors[index][indexplot] = self.vectors[index][indexplot][0:min(lengths)]
139