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