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