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