@@ -1,167 +1,237 | |||
|
1 | 1 | ''' |
|
2 | 2 | Created on Jul 9, 2014 |
|
3 | 3 | |
|
4 | 4 | @author: roj-idl71 |
|
5 | 5 | ''' |
|
6 | import os | |
|
6 | import os, sys | |
|
7 | 7 | import datetime |
|
8 | 8 | import numpy |
|
9 | import traceback | |
|
9 | 10 | |
|
10 | 11 | from time import sleep |
|
11 | 12 | from Queue import Queue |
|
12 | 13 | from threading import Lock |
|
13 | 14 | # from threading import Thread |
|
14 | 15 | |
|
16 | import schainpy | |
|
17 | import schainpy.admin | |
|
18 | ||
|
15 | 19 | from schainpy.model.proc.jroproc_base import Operation |
|
16 | 20 | from schainpy.model.serializer.data import obj2Dict, dict2Obj |
|
17 | 21 | from jroplot_correlation import * |
|
18 | 22 | from jroplot_heispectra import * |
|
19 | 23 | from jroplot_parameters import * |
|
20 | 24 | from jroplot_spectra import * |
|
21 | 25 | from jroplot_voltage import * |
|
22 | 26 | |
|
23 | 27 | |
|
24 | 28 | class Plotter(Operation): |
|
25 | 29 | |
|
26 | 30 | isConfig = None |
|
27 | 31 | name = None |
|
28 | 32 | __queue = None |
|
29 | 33 | |
|
30 | 34 | def __init__(self, plotter_name, plotter_queue=None): |
|
31 | 35 | |
|
32 | 36 | Operation.__init__(self) |
|
33 | 37 | |
|
34 | 38 | self.isConfig = False |
|
35 | 39 | self.name = plotter_name |
|
36 | 40 | self.__queue = plotter_queue |
|
37 | 41 | |
|
38 | 42 | def getSubplots(self): |
|
39 | 43 | |
|
40 | 44 | nrow = self.nplots |
|
41 | 45 | ncol = 1 |
|
42 | 46 | return nrow, ncol |
|
43 | 47 | |
|
44 | 48 | def setup(self, **kwargs): |
|
45 | 49 | |
|
46 | 50 | print "Initializing ..." |
|
47 | 51 | |
|
48 | 52 | |
|
49 | 53 | def run(self, dataOut, id=None, **kwargs): |
|
50 | 54 | |
|
51 | 55 | """ |
|
52 | 56 | |
|
53 | 57 | Input: |
|
54 | 58 | dataOut : |
|
55 | 59 | id : |
|
56 | 60 | """ |
|
57 | 61 | |
|
58 | 62 | packDict = {} |
|
59 | 63 | |
|
60 | 64 | packDict['id'] = id |
|
61 | 65 | packDict['name'] = self.name |
|
62 | 66 | packDict['kwargs'] = kwargs |
|
63 | 67 | |
|
64 | 68 | packDict['data'] = obj2Dict(dataOut) |
|
65 | 69 | |
|
66 | 70 | self.__queue.put(packDict) |
|
67 | 71 | |
|
68 | 72 | # class PlotManager(Thread): |
|
69 | 73 | class PlotManager(): |
|
74 | ||
|
75 | __err = False | |
|
70 | 76 | __stop = False |
|
77 | __realtime = False | |
|
78 | ||
|
71 | 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 | 88 | def __init__(self, plotter_queue): |
|
74 | 89 | |
|
75 | 90 | # Thread.__init__(self) |
|
76 | 91 | # self.setDaemon(True) |
|
77 | 92 | |
|
78 | 93 | self.__queue = plotter_queue |
|
79 | 94 | self.__lock = Lock() |
|
80 | 95 | |
|
81 | 96 | self.plotInstanceDict = {} |
|
97 | ||
|
98 | self.__err = False | |
|
82 | 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 | 134 | def run(self): |
|
85 | 135 | |
|
86 | 136 | if self.__queue.empty(): |
|
87 | 137 | return |
|
88 | 138 | |
|
139 | if self.__err: | |
|
140 | serial_data = self.__queue.get() | |
|
141 | self.__queue.task_done() | |
|
142 | return | |
|
143 | ||
|
89 | 144 | self.__lock.acquire() |
|
90 | 145 | |
|
91 | 146 | # if self.__queue.full(): |
|
92 | 147 | # for i in range(int(self.__queue.qsize()/2)): |
|
93 | 148 | # serial_data = self.__queue.get() |
|
94 | 149 | # self.__queue.task_done() |
|
95 | 150 | |
|
96 | 151 | n = int(self.__queue.qsize()/3 + 1) |
|
97 | 152 | |
|
98 | 153 | for i in range(n): |
|
99 | 154 | |
|
100 | 155 | if self.__queue.empty(): |
|
101 | 156 | break |
|
102 | 157 | |
|
103 | 158 | serial_data = self.__queue.get() |
|
104 | 159 | self.__queue.task_done() |
|
105 | 160 | |
|
106 | 161 | plot_id = serial_data['id'] |
|
107 | 162 | plot_name = serial_data['name'] |
|
108 | 163 | kwargs = serial_data['kwargs'] |
|
109 | 164 | dataDict = serial_data['data'] |
|
110 | 165 | |
|
111 | 166 | dataPlot = dict2Obj(dataDict) |
|
112 | 167 | |
|
113 | 168 | if plot_id not in self.plotInstanceDict.keys(): |
|
114 | 169 | className = eval(plot_name) |
|
115 | 170 | self.plotInstanceDict[plot_id] = className() |
|
116 | 171 | |
|
117 | 172 | plotter = self.plotInstanceDict[plot_id] |
|
173 | try: | |
|
118 | 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 | 180 | self.__lock.release() |
|
121 | 181 | |
|
122 | 182 | def isEmpty(self): |
|
123 | 183 | |
|
124 | 184 | return self.__queue.empty() |
|
125 | 185 | |
|
126 | 186 | def stop(self): |
|
127 | 187 | |
|
128 | 188 | self.__lock.acquire() |
|
129 | 189 | |
|
130 | 190 | self.__stop = True |
|
131 | 191 | |
|
132 | 192 | self.__lock.release() |
|
133 | 193 | |
|
134 | 194 | def close(self): |
|
135 | 195 | |
|
136 | 196 | self.__lock.acquire() |
|
137 | 197 | |
|
138 | 198 | for plot_id in self.plotInstanceDict.keys(): |
|
139 | 199 | plotter = self.plotInstanceDict[plot_id] |
|
140 | 200 | plotter.close() |
|
141 | 201 | |
|
142 | 202 | self.__lock.release() |
|
143 | 203 | |
|
144 | 204 | def setController(self, controllerThreadObj): |
|
145 | 205 | |
|
146 | 206 | self.controllerThreadObj = controllerThreadObj |
|
147 | 207 | |
|
148 | 208 | def start(self): |
|
149 | 209 | |
|
150 | 210 | if not self.controllerThreadObj.isRunning(): |
|
151 | 211 | raise RuntimeError, "controllerThreadObj has not been initialized. Use controllerThreadObj.start() before call this method" |
|
152 | 212 | |
|
153 | 213 | self.join() |
|
154 | 214 | |
|
155 | 215 | def join(self): |
|
156 | 216 | |
|
157 | 217 | #Execute plotter while controller is running |
|
158 | 218 | while self.controllerThreadObj.isRunning(): |
|
159 | 219 | self.run() |
|
160 | 220 | |
|
161 | 221 | self.controllerThreadObj.stop() |
|
162 | 222 | |
|
163 | 223 | #Wait until plotter queue is empty |
|
164 | 224 | while not self.isEmpty(): |
|
165 | 225 | self.run() |
|
166 | 226 | |
|
167 | 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