@@ -1,237 +1,238 | |||
|
1 | 1 | ''' |
|
2 | 2 | Created on Jul 9, 2014 |
|
3 | 3 | |
|
4 | 4 | @author: roj-idl71 |
|
5 | 5 | ''' |
|
6 | 6 | import os, sys |
|
7 | 7 | import datetime |
|
8 | 8 | import numpy |
|
9 | 9 | import traceback |
|
10 | 10 | |
|
11 | 11 | from time import sleep |
|
12 | 12 | from Queue import Queue |
|
13 | 13 | from threading import Lock |
|
14 | 14 | # from threading import Thread |
|
15 | 15 | |
|
16 | 16 | import schainpy |
|
17 | 17 | import schainpy.admin |
|
18 | 18 | |
|
19 | 19 | from schainpy.model.proc.jroproc_base import Operation |
|
20 | 20 | from schainpy.model.serializer.data import obj2Dict, dict2Obj |
|
21 | 21 | from jroplot_correlation import * |
|
22 | 22 | from jroplot_heispectra import * |
|
23 | 23 | from jroplot_parameters import * |
|
24 | 24 | from jroplot_spectra import * |
|
25 | 25 | from jroplot_voltage import * |
|
26 | 26 | |
|
27 | 27 | |
|
28 | 28 | class Plotter(Operation): |
|
29 | 29 | |
|
30 | 30 | isConfig = None |
|
31 | 31 | name = None |
|
32 | 32 | __queue = None |
|
33 | 33 | |
|
34 | 34 | def __init__(self, plotter_name, plotter_queue=None): |
|
35 | 35 | |
|
36 | 36 | Operation.__init__(self) |
|
37 | 37 | |
|
38 | 38 | self.isConfig = False |
|
39 | 39 | self.name = plotter_name |
|
40 | 40 | self.__queue = plotter_queue |
|
41 | 41 | |
|
42 | 42 | def getSubplots(self): |
|
43 | 43 | |
|
44 | 44 | nrow = self.nplots |
|
45 | 45 | ncol = 1 |
|
46 | 46 | return nrow, ncol |
|
47 | 47 | |
|
48 | 48 | def setup(self, **kwargs): |
|
49 | 49 | |
|
50 | 50 | print "Initializing ..." |
|
51 | 51 | |
|
52 | 52 | |
|
53 | 53 | def run(self, dataOut, id=None, **kwargs): |
|
54 | 54 | |
|
55 | 55 | """ |
|
56 | 56 | |
|
57 | 57 | Input: |
|
58 | 58 | dataOut : |
|
59 | 59 | id : |
|
60 | 60 | """ |
|
61 | 61 | |
|
62 | 62 | packDict = {} |
|
63 | 63 | |
|
64 | 64 | packDict['id'] = id |
|
65 | 65 | packDict['name'] = self.name |
|
66 | 66 | packDict['kwargs'] = kwargs |
|
67 | 67 | |
|
68 | 68 | packDict['data'] = obj2Dict(dataOut) |
|
69 | 69 | |
|
70 | 70 | self.__queue.put(packDict) |
|
71 | 71 | |
|
72 | 72 | # class PlotManager(Thread): |
|
73 | 73 | class PlotManager(): |
|
74 | 74 | |
|
75 | 75 | __err = False |
|
76 | 76 | __stop = False |
|
77 | 77 | __realtime = False |
|
78 | 78 | |
|
79 | 79 | controllerThreadObj = None |
|
80 | 80 | |
|
81 | 81 | plotterList = ['Scope', |
|
82 | 82 | 'SpectraPlot', 'RTIPlot', |
|
83 | 'SpectraCutPlot', | |
|
83 | 84 | 'CrossSpectraPlot', 'CoherenceMap', |
|
84 | 85 | 'PowerProfilePlot', 'Noise', 'BeaconPhase', |
|
85 | 86 | 'CorrelationPlot', |
|
86 | 87 | 'SpectraHeisScope','RTIfromSpectraHeis'] |
|
87 | 88 | |
|
88 | 89 | def __init__(self, plotter_queue): |
|
89 | 90 | |
|
90 | 91 | # Thread.__init__(self) |
|
91 | 92 | # self.setDaemon(True) |
|
92 | 93 | |
|
93 | 94 | self.__queue = plotter_queue |
|
94 | 95 | self.__lock = Lock() |
|
95 | 96 | |
|
96 | 97 | self.plotInstanceDict = {} |
|
97 | 98 | |
|
98 | 99 | self.__err = False |
|
99 | 100 | self.__stop = False |
|
100 | 101 | self.__realtime = False |
|
101 | 102 | |
|
102 | 103 | def __handleError(self, name="", send_email=False): |
|
103 | 104 | |
|
104 | 105 | err = traceback.format_exception(sys.exc_info()[0], |
|
105 | 106 | sys.exc_info()[1], |
|
106 | 107 | sys.exc_info()[2]) |
|
107 | 108 | |
|
108 | 109 | print "***** Error occurred in PlotManager *****" |
|
109 | 110 | print "***** [%s]: %s" %(name, err[-1]) |
|
110 | 111 | |
|
111 | 112 | message = "\nError ocurred in %s:\n" %name |
|
112 | 113 | message += "".join(err) |
|
113 | 114 | |
|
114 | 115 | sys.stderr.write(message) |
|
115 | 116 | |
|
116 | 117 | if not send_email: |
|
117 | 118 | return |
|
118 | 119 | |
|
119 | 120 | import socket |
|
120 | 121 | |
|
121 | 122 | subject = "SChain v%s: Error running %s\n" %(schainpy.__version__, name) |
|
122 | 123 | |
|
123 | 124 | subtitle = "%s:\n" %(name) |
|
124 | 125 | subtitle += "Hostname: %s\n" %socket.gethostbyname(socket.gethostname()) |
|
125 | 126 | subtitle += "Working directory: %s\n" %os.path.abspath("./") |
|
126 | 127 | # subtitle += "Configuration file: %s\n" %self.filename |
|
127 | 128 | subtitle += "Time: %s\n" %str(datetime.datetime.now()) |
|
128 | 129 | |
|
129 | 130 | adminObj = schainpy.admin.SchainNotify() |
|
130 | 131 | adminObj.sendAlert(message=message, |
|
131 | 132 | subject=subject, |
|
132 | 133 | subtitle=subtitle) |
|
133 | 134 | |
|
134 | 135 | def run(self): |
|
135 | 136 | |
|
136 | 137 | if self.__queue.empty(): |
|
137 | 138 | return |
|
138 | 139 | |
|
139 | 140 | if self.__err: |
|
140 | 141 | serial_data = self.__queue.get() |
|
141 | 142 | self.__queue.task_done() |
|
142 | 143 | return |
|
143 | 144 | |
|
144 | 145 | self.__lock.acquire() |
|
145 | 146 | |
|
146 | 147 | # if self.__queue.full(): |
|
147 | 148 | # for i in range(int(self.__queue.qsize()/2)): |
|
148 | 149 | # serial_data = self.__queue.get() |
|
149 | 150 | # self.__queue.task_done() |
|
150 | 151 | |
|
151 | 152 | n = int(self.__queue.qsize()/3 + 1) |
|
152 | 153 | |
|
153 | 154 | for i in range(n): |
|
154 | 155 | |
|
155 | 156 | if self.__queue.empty(): |
|
156 | 157 | break |
|
157 | 158 | |
|
158 | 159 | serial_data = self.__queue.get() |
|
159 | 160 | self.__queue.task_done() |
|
160 | 161 | |
|
161 | 162 | plot_id = serial_data['id'] |
|
162 | 163 | plot_name = serial_data['name'] |
|
163 | 164 | kwargs = serial_data['kwargs'] |
|
164 | 165 | dataDict = serial_data['data'] |
|
165 | 166 | |
|
166 | 167 | dataPlot = dict2Obj(dataDict) |
|
167 | 168 | |
|
168 | 169 | if plot_id not in self.plotInstanceDict.keys(): |
|
169 | 170 | className = eval(plot_name) |
|
170 | 171 | self.plotInstanceDict[plot_id] = className() |
|
171 | 172 | |
|
172 | 173 | plotter = self.plotInstanceDict[plot_id] |
|
173 | 174 | try: |
|
174 | 175 | plotter.run(dataPlot, plot_id, **kwargs) |
|
175 | 176 | except: |
|
176 | 177 | self.__err = True |
|
177 | 178 | self.__handleError(plot_name, send_email=True) |
|
178 | 179 | break |
|
179 | 180 | |
|
180 | 181 | self.__lock.release() |
|
181 | 182 | |
|
182 | 183 | def isEmpty(self): |
|
183 | 184 | |
|
184 | 185 | return self.__queue.empty() |
|
185 | 186 | |
|
186 | 187 | def stop(self): |
|
187 | 188 | |
|
188 | 189 | self.__lock.acquire() |
|
189 | 190 | |
|
190 | 191 | self.__stop = True |
|
191 | 192 | |
|
192 | 193 | self.__lock.release() |
|
193 | 194 | |
|
194 | 195 | def close(self): |
|
195 | 196 | |
|
196 | 197 | self.__lock.acquire() |
|
197 | 198 | |
|
198 | 199 | for plot_id in self.plotInstanceDict.keys(): |
|
199 | 200 | plotter = self.plotInstanceDict[plot_id] |
|
200 | 201 | plotter.close() |
|
201 | 202 | |
|
202 | 203 | self.__lock.release() |
|
203 | 204 | |
|
204 | 205 | def setController(self, controllerThreadObj): |
|
205 | 206 | |
|
206 | 207 | self.controllerThreadObj = controllerThreadObj |
|
207 | 208 | |
|
208 | 209 | def start(self): |
|
209 | 210 | |
|
210 | 211 | if not self.controllerThreadObj.isRunning(): |
|
211 | 212 | raise RuntimeError, "controllerThreadObj has not been initialized. Use controllerThreadObj.start() before call this method" |
|
212 | 213 | |
|
213 | 214 | self.join() |
|
214 | 215 | |
|
215 | 216 | def join(self): |
|
216 | 217 | |
|
217 | 218 | #Execute plotter while controller is running |
|
218 | 219 | while self.controllerThreadObj.isRunning(): |
|
219 | 220 | self.run() |
|
220 | 221 | |
|
221 | 222 | self.controllerThreadObj.stop() |
|
222 | 223 | |
|
223 | 224 | #Wait until plotter queue is empty |
|
224 | 225 | while not self.isEmpty(): |
|
225 | 226 | self.run() |
|
226 | 227 | |
|
227 | 228 | self.close() |
|
228 | 229 | |
|
229 | 230 | def isErrorDetected(self): |
|
230 | 231 | |
|
231 | 232 | self.__lock.acquire() |
|
232 | 233 | |
|
233 | 234 | err = self.__err |
|
234 | 235 | |
|
235 | 236 | self.__lock.release() |
|
236 | 237 | |
|
237 | 238 | return err No newline at end of file |
General Comments 0
You need to be logged in to leave comments.
Login now