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