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