Added modular directory structure.
[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                 return True
19         print 'Not a recognizable curve format: ', self.path
20         return False
21
22
23 class Driver(object):
24     '''
25     Base class for file format drivers.
26
27     To be overridden
28     '''
29     def __init__(self):
30         self.experiment=''
31         self.filetype=''
32
33     def is_me(self):
34         '''
35         This method must read the file and return True if the filetype can be managed by the driver, False if not.
36         '''
37         return False
38
39     def close_all(self):
40         '''
41         This method must close all the open files of the driver, explicitly.
42         '''
43         return None
44
45     def default_plots(self):
46         dummy_default=PlotObject()
47         dummy_default.vectors.append([[[0]],[[0]]])
48         return [dummy_default]
49
50
51 class PlotObject(object):
52
53     def __init__(self):
54
55         '''
56         the plot destination
57         0=top
58         1=bottom
59         '''
60         self.destination=0
61
62         '''
63         self.vectors is a multidimensional array:
64         self.vectors[0]=plot1
65         self.vectors[1]=plot2
66         self.vectors[2]=plot3
67         etc.
68
69         2 curves in a x,y plot are:
70         [[[x1],[y1]],[[x2],[y2]]]
71         for example:
72             x1          y1              x2         y2
73         [[[1,2,3,4],[10,20,30,40]],[[3,6,9,12],[30,60,90,120]]]
74         x1 = self.vectors[0][0]
75         y1 = self.vectors[0][1]
76         x2 = self.vectors[1][0]
77         y2 = self.vectors[1][1]
78         '''
79         self.vectors=[]
80
81         '''
82         self.units is simpler. for each plot with N axes (x,y,z...) only N labels
83         can be made, regardless of the number of superimposed plots
84         so units for the double plot above is: [unitx, unity]
85
86         units are strings
87         '''
88         self.units=['','']
89
90         '''
91         xaxes and yaxes directions. 0,0 means the common +X=right, +Y=top directions
92         '''
93         self.xaxes=0
94         self.yaxes=0
95
96         self.title='' #title
97
98         '''
99         styles: defines what is the style of the current plots. If undefined or None, it is line plot.
100         If an element of the list is 'scatter', the corresponding dataset
101         is drawn with scattered points and not a continuous line.
102         '''
103         self.styles=[]
104
105         '''
106         colors: define what is the colour of the current plots
107         '''
108         self.colors=[]
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