@@ -1,707 +1,709 | |||
|
1 | 1 | |
|
2 | 2 | import os |
|
3 | 3 | import zmq |
|
4 | 4 | import time |
|
5 | 5 | import numpy |
|
6 | 6 | import datetime |
|
7 | 7 | import numpy as np |
|
8 | 8 | import matplotlib |
|
9 | 9 | matplotlib.use('TkAgg') |
|
10 | 10 | import matplotlib.pyplot as plt |
|
11 | 11 | from mpl_toolkits.axes_grid1 import make_axes_locatable |
|
12 | 12 | from matplotlib.ticker import FuncFormatter, LinearLocator |
|
13 | 13 | from multiprocessing import Process |
|
14 | 14 | |
|
15 | 15 | from schainpy.model.proc.jroproc_base import Operation |
|
16 | 16 | |
|
17 |
plt.io |
|
|
17 | plt.ion() | |
|
18 | 18 | |
|
19 | 19 | func = lambda x, pos: ('%s') %(datetime.datetime.fromtimestamp(x).strftime('%H:%M')) |
|
20 | 20 | |
|
21 | 21 | d1970 = datetime.datetime(1970,1,1) |
|
22 | 22 | |
|
23 | 23 | class PlotData(Operation, Process): |
|
24 | 24 | |
|
25 | 25 | CODE = 'Figure' |
|
26 | 26 | colormap = 'jro' |
|
27 | 27 | CONFLATE = False |
|
28 | 28 | __MAXNUMX = 80 |
|
29 | 29 | __missing = 1E30 |
|
30 | 30 | |
|
31 | 31 | def __init__(self, **kwargs): |
|
32 | 32 | |
|
33 | 33 | Operation.__init__(self, plot=True, **kwargs) |
|
34 | 34 | Process.__init__(self) |
|
35 | 35 | self.kwargs['code'] = self.CODE |
|
36 | 36 | self.mp = False |
|
37 | 37 | self.dataOut = None |
|
38 | 38 | self.isConfig = False |
|
39 | 39 | self.figure = None |
|
40 | 40 | self.axes = [] |
|
41 | 41 | self.localtime = kwargs.pop('localtime', True) |
|
42 | 42 | self.show = kwargs.get('show', True) |
|
43 | 43 | self.save = kwargs.get('save', False) |
|
44 | 44 | self.colormap = kwargs.get('colormap', self.colormap) |
|
45 | 45 | self.colormap_coh = kwargs.get('colormap_coh', 'jet') |
|
46 | 46 | self.colormap_phase = kwargs.get('colormap_phase', 'RdBu_r') |
|
47 | 47 | self.showprofile = kwargs.get('showprofile', True) |
|
48 | 48 | self.title = kwargs.get('wintitle', '') |
|
49 | 49 | self.xaxis = kwargs.get('xaxis', 'frequency') |
|
50 | 50 | self.zmin = kwargs.get('zmin', None) |
|
51 | 51 | self.zmax = kwargs.get('zmax', None) |
|
52 | 52 | self.xmin = kwargs.get('xmin', None) |
|
53 | 53 | self.xmax = kwargs.get('xmax', None) |
|
54 | 54 | self.xrange = kwargs.get('xrange', 24) |
|
55 | 55 | self.ymin = kwargs.get('ymin', None) |
|
56 | 56 | self.ymax = kwargs.get('ymax', None) |
|
57 | 57 | self.__MAXNUMY = kwargs.get('decimation', 80) |
|
58 | 58 | self.throttle_value = 5 |
|
59 | 59 | self.times = [] |
|
60 | #self.interactive = self.kwargs['parent'] | |
|
61 | ||
|
60 | 62 | |
|
61 | 63 | def fill_gaps(self, x_buffer, y_buffer, z_buffer): |
|
62 | 64 | |
|
63 | 65 | if x_buffer.shape[0] < 2: |
|
64 | 66 | return x_buffer, y_buffer, z_buffer |
|
65 | 67 | |
|
66 | 68 | deltas = x_buffer[1:] - x_buffer[0:-1] |
|
67 | 69 | x_median = np.median(deltas) |
|
68 | 70 | |
|
69 | 71 | index = np.where(deltas > 5*x_median) |
|
70 | 72 | |
|
71 | 73 | if len(index[0]) != 0: |
|
72 | 74 | z_buffer[::, index[0], ::] = self.__missing |
|
73 | 75 | z_buffer = np.ma.masked_inside(z_buffer, |
|
74 | 76 | 0.99*self.__missing, |
|
75 | 77 | 1.01*self.__missing) |
|
76 | 78 | |
|
77 | 79 | return x_buffer, y_buffer, z_buffer |
|
78 | 80 | |
|
79 | 81 | def decimate(self): |
|
80 | 82 | |
|
81 | 83 | # dx = int(len(self.x)/self.__MAXNUMX) + 1 |
|
82 | 84 | dy = int(len(self.y)/self.__MAXNUMY) + 1 |
|
83 | 85 | |
|
84 | 86 | # x = self.x[::dx] |
|
85 | 87 | x = self.x |
|
86 | 88 | y = self.y[::dy] |
|
87 | 89 | z = self.z[::, ::, ::dy] |
|
88 | 90 | |
|
89 | 91 | return x, y, z |
|
90 | 92 | |
|
91 | 93 | def __plot(self): |
|
92 | 94 | |
|
93 | 95 | print 'plotting...{}'.format(self.CODE) |
|
94 | 96 | |
|
95 | 97 | if self.show: |
|
96 | 98 | print 'showing' |
|
97 | 99 | self.figure.show() |
|
98 | 100 | |
|
99 | 101 | self.plot() |
|
100 | 102 | plt.tight_layout() |
|
101 | 103 | self.figure.canvas.manager.set_window_title('{} {} - Date:{}'.format(self.title, self.CODE.upper(), |
|
102 | 104 | datetime.datetime.fromtimestamp(self.max_time).strftime('%y/%m/%d %H:%M:%S'))) |
|
103 | 105 | |
|
104 | 106 | if self.save: |
|
105 | 107 | figname = os.path.join(self.save, '{}_{}.png'.format(self.CODE, |
|
106 | 108 | datetime.datetime.fromtimestamp(self.saveTime).strftime('%y%m%d_%H%M%S'))) |
|
107 | 109 | print 'Saving figure: {}'.format(figname) |
|
108 | 110 | self.figure.savefig(figname) |
|
109 | 111 | |
|
110 | 112 | self.figure.canvas.draw() |
|
111 | 113 | |
|
112 | 114 | def plot(self): |
|
113 | 115 | |
|
114 | 116 | print 'plotting...{}'.format(self.CODE.upper()) |
|
115 | 117 | return |
|
116 | 118 | |
|
117 | 119 | def run(self): |
|
118 | 120 | |
|
119 | 121 | print '[Starting] {}'.format(self.name) |
|
120 | 122 | context = zmq.Context() |
|
121 | 123 | receiver = context.socket(zmq.SUB) |
|
122 | 124 | receiver.setsockopt(zmq.SUBSCRIBE, '') |
|
123 | 125 | receiver.setsockopt(zmq.CONFLATE, self.CONFLATE) |
|
124 | 126 | receiver.connect("ipc:///tmp/zmq.plots") |
|
125 | 127 | seconds_passed = 0 |
|
126 | 128 | while True: |
|
127 | 129 | try: |
|
128 | 130 | self.data = receiver.recv_pyobj(flags=zmq.NOBLOCK)#flags=zmq.NOBLOCK |
|
129 | 131 | self.started = self.data['STARTED'] |
|
130 | 132 | self.dataOut = self.data['dataOut'] |
|
131 | 133 | |
|
132 | 134 | if (len(self.times) < len(self.data['times']) and not self.started and self.data['ENDED']): |
|
133 | 135 | continue |
|
134 | 136 | |
|
135 | 137 | self.times = self.data['times'] |
|
136 | 138 | self.times.sort() |
|
137 | 139 | self.throttle_value = self.data['throttle'] |
|
138 | 140 | self.min_time = self.times[0] |
|
139 | 141 | self.max_time = self.times[-1] |
|
140 | 142 | |
|
141 | 143 | if self.isConfig is False: |
|
142 | 144 | print 'setting up' |
|
143 | 145 | self.setup() |
|
144 | 146 | self.isConfig = True |
|
145 | 147 | self.__plot() |
|
146 | 148 | |
|
147 | 149 | if self.data['ENDED'] is True: |
|
148 | 150 | print '********GRAPHIC ENDED********' |
|
149 | 151 | self.ended = True |
|
150 | 152 | self.isConfig = False |
|
151 | 153 | self.__plot() |
|
152 | 154 | elif seconds_passed >= self.data['throttle']: |
|
153 | 155 | print 'passed', seconds_passed |
|
154 | 156 | self.__plot() |
|
155 | 157 | seconds_passed = 0 |
|
156 | 158 | |
|
157 | 159 | except zmq.Again as e: |
|
158 | 160 | print 'Waiting for data...' |
|
159 | 161 | plt.pause(2) |
|
160 | 162 | seconds_passed += 2 |
|
161 | 163 | |
|
162 | 164 | def close(self): |
|
163 | 165 | if self.dataOut: |
|
164 | 166 | self.__plot() |
|
165 | 167 | |
|
166 | 168 | |
|
167 | 169 | class PlotSpectraData(PlotData): |
|
168 | 170 | |
|
169 | 171 | CODE = 'spc' |
|
170 | 172 | colormap = 'jro' |
|
171 | 173 | CONFLATE = False |
|
172 | 174 | |
|
173 | 175 | def setup(self): |
|
174 | 176 | |
|
175 | 177 | ncolspan = 1 |
|
176 | 178 | colspan = 1 |
|
177 | 179 | self.ncols = int(numpy.sqrt(self.dataOut.nChannels)+0.9) |
|
178 | 180 | self.nrows = int(self.dataOut.nChannels*1./self.ncols + 0.9) |
|
179 | 181 | self.width = 3.6*self.ncols |
|
180 | 182 | self.height = 3.2*self.nrows |
|
181 | 183 | if self.showprofile: |
|
182 | 184 | ncolspan = 3 |
|
183 | 185 | colspan = 2 |
|
184 | 186 | self.width += 1.2*self.ncols |
|
185 | 187 | |
|
186 | 188 | self.ylabel = 'Range [Km]' |
|
187 | 189 | self.titles = ['Channel {}'.format(x) for x in self.dataOut.channelList] |
|
188 | 190 | |
|
189 | 191 | if self.figure is None: |
|
190 | 192 | self.figure = plt.figure(figsize=(self.width, self.height), |
|
191 | 193 | edgecolor='k', |
|
192 | 194 | facecolor='w') |
|
193 | 195 | else: |
|
194 | 196 | self.figure.clf() |
|
195 | 197 | |
|
196 | 198 | n = 0 |
|
197 | 199 | for y in range(self.nrows): |
|
198 | 200 | for x in range(self.ncols): |
|
199 | 201 | if n >= self.dataOut.nChannels: |
|
200 | 202 | break |
|
201 | 203 | ax = plt.subplot2grid((self.nrows, self.ncols*ncolspan), (y, x*ncolspan), 1, colspan) |
|
202 | 204 | if self.showprofile: |
|
203 | 205 | ax.ax_profile = plt.subplot2grid((self.nrows, self.ncols*ncolspan), (y, x*ncolspan+colspan), 1, 1) |
|
204 | 206 | |
|
205 | 207 | ax.firsttime = True |
|
206 | 208 | self.axes.append(ax) |
|
207 | 209 | n += 1 |
|
208 | 210 | |
|
209 | 211 | def plot(self): |
|
210 | 212 | |
|
211 | 213 | if self.xaxis == "frequency": |
|
212 | 214 | x = self.dataOut.getFreqRange(1)/1000. |
|
213 | 215 | xlabel = "Frequency (kHz)" |
|
214 | 216 | elif self.xaxis == "time": |
|
215 | 217 | x = self.dataOut.getAcfRange(1) |
|
216 | 218 | xlabel = "Time (ms)" |
|
217 | 219 | else: |
|
218 | 220 | x = self.dataOut.getVelRange(1) |
|
219 | 221 | xlabel = "Velocity (m/s)" |
|
220 | 222 | |
|
221 | 223 | y = self.dataOut.getHeiRange() |
|
222 | 224 | z = self.data[self.CODE] |
|
223 | 225 | |
|
224 | 226 | for n, ax in enumerate(self.axes): |
|
225 | 227 | |
|
226 | 228 | if ax.firsttime: |
|
227 | 229 | self.xmax = self.xmax if self.xmax else np.nanmax(x) |
|
228 | 230 | self.xmin = self.xmin if self.xmin else -self.xmax |
|
229 | 231 | self.ymin = self.ymin if self.ymin else np.nanmin(y) |
|
230 | 232 | self.ymax = self.ymax if self.ymax else np.nanmax(y) |
|
231 | 233 | self.zmin = self.zmin if self.zmin else np.nanmin(z) |
|
232 | 234 | self.zmax = self.zmax if self.zmax else np.nanmax(z) |
|
233 | 235 | ax.plot = ax.pcolormesh(x, y, z[n].T, |
|
234 | 236 | vmin=self.zmin, |
|
235 | 237 | vmax=self.zmax, |
|
236 | 238 | cmap=plt.get_cmap(self.colormap) |
|
237 | 239 | ) |
|
238 | 240 | divider = make_axes_locatable(ax) |
|
239 | 241 | cax = divider.new_horizontal(size='3%', pad=0.05) |
|
240 | 242 | self.figure.add_axes(cax) |
|
241 | 243 | plt.colorbar(ax.plot, cax) |
|
242 | 244 | |
|
243 | 245 | ax.set_xlim(self.xmin, self.xmax) |
|
244 | 246 | ax.set_ylim(self.ymin, self.ymax) |
|
245 | 247 | |
|
246 | 248 | ax.set_ylabel(self.ylabel) |
|
247 | 249 | ax.set_xlabel(xlabel) |
|
248 | 250 | |
|
249 | 251 | ax.firsttime = False |
|
250 | 252 | |
|
251 | 253 | if self.showprofile: |
|
252 | 254 | ax.plot_profile= ax.ax_profile.plot(self.data['rti'][self.max_time][n], y)[0] |
|
253 | 255 | ax.ax_profile.set_xlim(self.zmin, self.zmax) |
|
254 | 256 | ax.ax_profile.set_ylim(self.ymin, self.ymax) |
|
255 | 257 | ax.ax_profile.set_xlabel('dB') |
|
256 | 258 | ax.ax_profile.grid(b=True, axis='x') |
|
257 | 259 | ax.plot_noise = ax.ax_profile.plot(numpy.repeat(self.data['noise'][self.max_time][n], len(y)), y, |
|
258 | 260 | color="k", linestyle="dashed", lw=2)[0] |
|
259 | 261 | [tick.set_visible(False) for tick in ax.ax_profile.get_yticklabels()] |
|
260 | 262 | else: |
|
261 | 263 | ax.plot.set_array(z[n].T.ravel()) |
|
262 | 264 | if self.showprofile: |
|
263 | 265 | ax.plot_profile.set_data(self.data['rti'][self.max_time][n], y) |
|
264 | 266 | ax.plot_noise.set_data(numpy.repeat(self.data['noise'][self.max_time][n], len(y)), y) |
|
265 | 267 | |
|
266 | 268 | ax.set_title('{} - Noise: {:.2f} dB'.format(self.titles[n], self.data['noise'][self.max_time][n]), |
|
267 | 269 | size=8) |
|
268 | 270 | self.saveTime = self.max_time |
|
269 | 271 | |
|
270 | 272 | |
|
271 | 273 | class PlotCrossSpectraData(PlotData): |
|
272 | 274 | |
|
273 | 275 | CODE = 'cspc' |
|
274 | 276 | zmin_coh = None |
|
275 | 277 | zmax_coh = None |
|
276 | 278 | zmin_phase = None |
|
277 | 279 | zmax_phase = None |
|
278 | 280 | CONFLATE = False |
|
279 | 281 | |
|
280 | 282 | def setup(self): |
|
281 | 283 | |
|
282 | 284 | ncolspan = 1 |
|
283 | 285 | colspan = 1 |
|
284 | 286 | self.ncols = 2 |
|
285 | 287 | self.nrows = self.dataOut.nPairs |
|
286 | 288 | self.width = 3.6*self.ncols |
|
287 | 289 | self.height = 3.2*self.nrows |
|
288 | 290 | |
|
289 | 291 | self.ylabel = 'Range [Km]' |
|
290 | 292 | self.titles = ['Channel {}'.format(x) for x in self.dataOut.channelList] |
|
291 | 293 | |
|
292 | 294 | if self.figure is None: |
|
293 | 295 | self.figure = plt.figure(figsize=(self.width, self.height), |
|
294 | 296 | edgecolor='k', |
|
295 | 297 | facecolor='w') |
|
296 | 298 | else: |
|
297 | 299 | self.figure.clf() |
|
298 | 300 | |
|
299 | 301 | for y in range(self.nrows): |
|
300 | 302 | for x in range(self.ncols): |
|
301 | 303 | ax = plt.subplot2grid((self.nrows, self.ncols), (y, x), 1, 1) |
|
302 | 304 | ax.firsttime = True |
|
303 | 305 | self.axes.append(ax) |
|
304 | 306 | |
|
305 | 307 | def plot(self): |
|
306 | 308 | |
|
307 | 309 | if self.xaxis == "frequency": |
|
308 | 310 | x = self.dataOut.getFreqRange(1)/1000. |
|
309 | 311 | xlabel = "Frequency (kHz)" |
|
310 | 312 | elif self.xaxis == "time": |
|
311 | 313 | x = self.dataOut.getAcfRange(1) |
|
312 | 314 | xlabel = "Time (ms)" |
|
313 | 315 | else: |
|
314 | 316 | x = self.dataOut.getVelRange(1) |
|
315 | 317 | xlabel = "Velocity (m/s)" |
|
316 | 318 | |
|
317 | 319 | y = self.dataOut.getHeiRange() |
|
318 | 320 | z_coh = self.data['cspc_coh'] |
|
319 | 321 | z_phase = self.data['cspc_phase'] |
|
320 | 322 | |
|
321 | 323 | for n in range(self.nrows): |
|
322 | 324 | ax = self.axes[2*n] |
|
323 | 325 | ax1 = self.axes[2*n+1] |
|
324 | 326 | if ax.firsttime: |
|
325 | 327 | self.xmax = self.xmax if self.xmax else np.nanmax(x) |
|
326 | 328 | self.xmin = self.xmin if self.xmin else -self.xmax |
|
327 | 329 | self.ymin = self.ymin if self.ymin else np.nanmin(y) |
|
328 | 330 | self.ymax = self.ymax if self.ymax else np.nanmax(y) |
|
329 | 331 | self.zmin_coh = self.zmin_coh if self.zmin_coh else 0.0 |
|
330 | 332 | self.zmax_coh = self.zmax_coh if self.zmax_coh else 1.0 |
|
331 | 333 | self.zmin_phase = self.zmin_phase if self.zmin_phase else -180 |
|
332 | 334 | self.zmax_phase = self.zmax_phase if self.zmax_phase else 180 |
|
333 | 335 | |
|
334 | 336 | ax.plot = ax.pcolormesh(x, y, z_coh[n].T, |
|
335 | 337 | vmin=self.zmin_coh, |
|
336 | 338 | vmax=self.zmax_coh, |
|
337 | 339 | cmap=plt.get_cmap(self.colormap_coh) |
|
338 | 340 | ) |
|
339 | 341 | divider = make_axes_locatable(ax) |
|
340 | 342 | cax = divider.new_horizontal(size='3%', pad=0.05) |
|
341 | 343 | self.figure.add_axes(cax) |
|
342 | 344 | plt.colorbar(ax.plot, cax) |
|
343 | 345 | |
|
344 | 346 | ax.set_xlim(self.xmin, self.xmax) |
|
345 | 347 | ax.set_ylim(self.ymin, self.ymax) |
|
346 | 348 | |
|
347 | 349 | ax.set_ylabel(self.ylabel) |
|
348 | 350 | ax.set_xlabel(xlabel) |
|
349 | 351 | ax.firsttime = False |
|
350 | 352 | |
|
351 | 353 | ax1.plot = ax1.pcolormesh(x, y, z_phase[n].T, |
|
352 | 354 | vmin=self.zmin_phase, |
|
353 | 355 | vmax=self.zmax_phase, |
|
354 | 356 | cmap=plt.get_cmap(self.colormap_phase) |
|
355 | 357 | ) |
|
356 | 358 | divider = make_axes_locatable(ax1) |
|
357 | 359 | cax = divider.new_horizontal(size='3%', pad=0.05) |
|
358 | 360 | self.figure.add_axes(cax) |
|
359 | 361 | plt.colorbar(ax1.plot, cax) |
|
360 | 362 | |
|
361 | 363 | ax1.set_xlim(self.xmin, self.xmax) |
|
362 | 364 | ax1.set_ylim(self.ymin, self.ymax) |
|
363 | 365 | |
|
364 | 366 | ax1.set_ylabel(self.ylabel) |
|
365 | 367 | ax1.set_xlabel(xlabel) |
|
366 | 368 | ax1.firsttime = False |
|
367 | 369 | else: |
|
368 | 370 | ax.plot.set_array(z_coh[n].T.ravel()) |
|
369 | 371 | ax1.plot.set_array(z_phase[n].T.ravel()) |
|
370 | 372 | |
|
371 | 373 | ax.set_title('Coherence Ch{} * Ch{}'.format(self.dataOut.pairsList[n][0], self.dataOut.pairsList[n][1]), size=8) |
|
372 | 374 | ax1.set_title('Phase Ch{} * Ch{}'.format(self.dataOut.pairsList[n][0], self.dataOut.pairsList[n][1]), size=8) |
|
373 | 375 | self.saveTime = self.max_time |
|
374 | 376 | |
|
375 | 377 | |
|
376 | 378 | class PlotSpectraMeanData(PlotSpectraData): |
|
377 | 379 | |
|
378 | 380 | CODE = 'spc_mean' |
|
379 | 381 | colormap = 'jet' |
|
380 | 382 | |
|
381 | 383 | def plot(self): |
|
382 | 384 | |
|
383 | 385 | if self.xaxis == "frequency": |
|
384 | 386 | x = self.dataOut.getFreqRange(1)/1000. |
|
385 | 387 | xlabel = "Frequency (kHz)" |
|
386 | 388 | elif self.xaxis == "time": |
|
387 | 389 | x = self.dataOut.getAcfRange(1) |
|
388 | 390 | xlabel = "Time (ms)" |
|
389 | 391 | else: |
|
390 | 392 | x = self.dataOut.getVelRange(1) |
|
391 | 393 | xlabel = "Velocity (m/s)" |
|
392 | 394 | |
|
393 | 395 | y = self.dataOut.getHeiRange() |
|
394 | 396 | z = self.data['spc'] |
|
395 | 397 | mean = self.data['mean'][self.max_time] |
|
396 | 398 | |
|
397 | 399 | for n, ax in enumerate(self.axes): |
|
398 | 400 | |
|
399 | 401 | if ax.firsttime: |
|
400 | 402 | self.xmax = self.xmax if self.xmax else np.nanmax(x) |
|
401 | 403 | self.xmin = self.xmin if self.xmin else -self.xmax |
|
402 | 404 | self.ymin = self.ymin if self.ymin else np.nanmin(y) |
|
403 | 405 | self.ymax = self.ymax if self.ymax else np.nanmax(y) |
|
404 | 406 | self.zmin = self.zmin if self.zmin else np.nanmin(z) |
|
405 | 407 | self.zmax = self.zmax if self.zmax else np.nanmax(z) |
|
406 | 408 | ax.plt = ax.pcolormesh(x, y, z[n].T, |
|
407 | 409 | vmin=self.zmin, |
|
408 | 410 | vmax=self.zmax, |
|
409 | 411 | cmap=plt.get_cmap(self.colormap) |
|
410 | 412 | ) |
|
411 | 413 | ax.plt_dop = ax.plot(mean[n], y, |
|
412 | 414 | color='k')[0] |
|
413 | 415 | |
|
414 | 416 | divider = make_axes_locatable(ax) |
|
415 | 417 | cax = divider.new_horizontal(size='3%', pad=0.05) |
|
416 | 418 | self.figure.add_axes(cax) |
|
417 | 419 | plt.colorbar(ax.plt, cax) |
|
418 | 420 | |
|
419 | 421 | ax.set_xlim(self.xmin, self.xmax) |
|
420 | 422 | ax.set_ylim(self.ymin, self.ymax) |
|
421 | 423 | |
|
422 | 424 | ax.set_ylabel(self.ylabel) |
|
423 | 425 | ax.set_xlabel(xlabel) |
|
424 | 426 | |
|
425 | 427 | ax.firsttime = False |
|
426 | 428 | |
|
427 | 429 | if self.showprofile: |
|
428 | 430 | ax.plt_profile= ax.ax_profile.plot(self.data['rti'][self.max_time][n], y)[0] |
|
429 | 431 | ax.ax_profile.set_xlim(self.zmin, self.zmax) |
|
430 | 432 | ax.ax_profile.set_ylim(self.ymin, self.ymax) |
|
431 | 433 | ax.ax_profile.set_xlabel('dB') |
|
432 | 434 | ax.ax_profile.grid(b=True, axis='x') |
|
433 | 435 | ax.plt_noise = ax.ax_profile.plot(numpy.repeat(self.data['noise'][self.max_time][n], len(y)), y, |
|
434 | 436 | color="k", linestyle="dashed", lw=2)[0] |
|
435 | 437 | [tick.set_visible(False) for tick in ax.ax_profile.get_yticklabels()] |
|
436 | 438 | else: |
|
437 | 439 | ax.plt.set_array(z[n].T.ravel()) |
|
438 | 440 | ax.plt_dop.set_data(mean[n], y) |
|
439 | 441 | if self.showprofile: |
|
440 | 442 | ax.plt_profile.set_data(self.data['rti'][self.max_time][n], y) |
|
441 | 443 | ax.plt_noise.set_data(numpy.repeat(self.data['noise'][self.max_time][n], len(y)), y) |
|
442 | 444 | |
|
443 | 445 | ax.set_title('{} - Noise: {:.2f} dB'.format(self.titles[n], self.data['noise'][self.max_time][n]), |
|
444 | 446 | size=8) |
|
445 | 447 | self.saveTime = self.max_time |
|
446 | 448 | |
|
447 | 449 | |
|
448 | 450 | class PlotRTIData(PlotData): |
|
449 | 451 | |
|
450 | 452 | CODE = 'rti' |
|
451 | 453 | colormap = 'jro' |
|
452 | 454 | |
|
453 | 455 | def setup(self): |
|
454 | 456 | self.ncols = 1 |
|
455 | 457 | self.nrows = self.dataOut.nChannels |
|
456 | 458 | self.width = 10 |
|
457 | 459 | self.height = 2.2*self.nrows if self.nrows<6 else 12 |
|
458 | 460 | if self.nrows==1: |
|
459 | 461 | self.height += 1 |
|
460 | 462 | self.ylabel = 'Range [Km]' |
|
461 | 463 | self.titles = ['Channel {}'.format(x) for x in self.dataOut.channelList] |
|
462 | 464 | |
|
463 | 465 | if self.figure is None: |
|
464 | 466 | self.figure = plt.figure(figsize=(self.width, self.height), |
|
465 | 467 | edgecolor='k', |
|
466 | 468 | facecolor='w') |
|
467 | 469 | else: |
|
468 | 470 | self.figure.clf() |
|
469 | 471 | self.axes = [] |
|
470 | 472 | |
|
471 | 473 | for n in range(self.nrows): |
|
472 | 474 | ax = self.figure.add_subplot(self.nrows, self.ncols, n+1) |
|
473 | 475 | ax.firsttime = True |
|
474 | 476 | self.axes.append(ax) |
|
475 | 477 | |
|
476 | 478 | def plot(self): |
|
477 | 479 | |
|
478 | 480 | self.x = np.array(self.times) |
|
479 | 481 | self.y = self.dataOut.getHeiRange() |
|
480 | 482 | self.z = [] |
|
481 | 483 | |
|
482 | 484 | for ch in range(self.nrows): |
|
483 | 485 | self.z.append([self.data[self.CODE][t][ch] for t in self.times]) |
|
484 | 486 | |
|
485 | 487 | self.z = np.array(self.z) |
|
486 | 488 | for n, ax in enumerate(self.axes): |
|
487 | 489 | x, y, z = self.fill_gaps(*self.decimate()) |
|
488 | 490 | xmin = self.min_time |
|
489 | 491 | xmax = xmin+self.xrange*60*60 |
|
490 | 492 | self.zmin = self.zmin if self.zmin else np.min(self.z) |
|
491 | 493 | self.zmax = self.zmax if self.zmax else np.max(self.z) |
|
492 | 494 | if ax.firsttime: |
|
493 | 495 | self.ymin = self.ymin if self.ymin else np.nanmin(self.y) |
|
494 | 496 | self.ymax = self.ymax if self.ymax else np.nanmax(self.y) |
|
495 | 497 | plot = ax.pcolormesh(x, y, z[n].T, |
|
496 | 498 | vmin=self.zmin, |
|
497 | 499 | vmax=self.zmax, |
|
498 | 500 | cmap=plt.get_cmap(self.colormap) |
|
499 | 501 | ) |
|
500 | 502 | divider = make_axes_locatable(ax) |
|
501 | 503 | cax = divider.new_horizontal(size='2%', pad=0.05) |
|
502 | 504 | self.figure.add_axes(cax) |
|
503 | 505 | plt.colorbar(plot, cax) |
|
504 | 506 | ax.set_ylim(self.ymin, self.ymax) |
|
505 | 507 | |
|
506 | 508 | ax.xaxis.set_major_formatter(FuncFormatter(func)) |
|
507 | 509 | ax.xaxis.set_major_locator(LinearLocator(6)) |
|
508 | 510 | |
|
509 | 511 | ax.set_ylabel(self.ylabel) |
|
510 | 512 | |
|
511 | 513 | # if self.xmin is None: |
|
512 | 514 | # xmin = self.min_time |
|
513 | 515 | # else: |
|
514 | 516 | # xmin = (datetime.datetime.combine(self.dataOut.datatime.date(), |
|
515 | 517 | # datetime.time(self.xmin, 0, 0))-d1970).total_seconds() |
|
516 | 518 | |
|
517 | 519 | ax.set_xlim(xmin, xmax) |
|
518 | 520 | ax.firsttime = False |
|
519 | 521 | else: |
|
520 | 522 | ax.collections.remove(ax.collections[0]) |
|
521 | 523 | ax.set_xlim(xmin, xmax) |
|
522 | 524 | plot = ax.pcolormesh(x, y, z[n].T, |
|
523 | 525 | vmin=self.zmin, |
|
524 | 526 | vmax=self.zmax, |
|
525 | 527 | cmap=plt.get_cmap(self.colormap) |
|
526 | 528 | ) |
|
527 | 529 | ax.set_title('{} {}'.format(self.titles[n], |
|
528 | 530 | datetime.datetime.fromtimestamp(self.max_time).strftime('%y/%m/%d %H:%M:%S')), |
|
529 | 531 | size=8) |
|
530 | 532 | |
|
531 | 533 | self.saveTime = self.min_time |
|
532 | 534 | |
|
533 | 535 | |
|
534 | 536 | class PlotCOHData(PlotRTIData): |
|
535 | 537 | |
|
536 | 538 | CODE = 'coh' |
|
537 | 539 | |
|
538 | 540 | def setup(self): |
|
539 | 541 | |
|
540 | 542 | self.ncols = 1 |
|
541 | 543 | self.nrows = self.dataOut.nPairs |
|
542 | 544 | self.width = 10 |
|
543 | 545 | self.height = 2.2*self.nrows if self.nrows<6 else 12 |
|
544 | 546 | if self.nrows==1: |
|
545 | 547 | self.height += 1 |
|
546 | 548 | self.ylabel = 'Range [Km]' |
|
547 | 549 | self.titles = ['{} Ch{} * Ch{}'.format(self.CODE.upper(), x[0], x[1]) for x in self.dataOut.pairsList] |
|
548 | 550 | |
|
549 | 551 | if self.figure is None: |
|
550 | 552 | self.figure = plt.figure(figsize=(self.width, self.height), |
|
551 | 553 | edgecolor='k', |
|
552 | 554 | facecolor='w') |
|
553 | 555 | else: |
|
554 | 556 | self.figure.clf() |
|
555 | 557 | self.axes = [] |
|
556 | 558 | |
|
557 | 559 | for n in range(self.nrows): |
|
558 | 560 | ax = self.figure.add_subplot(self.nrows, self.ncols, n+1) |
|
559 | 561 | ax.firsttime = True |
|
560 | 562 | self.axes.append(ax) |
|
561 | 563 | |
|
562 | 564 | |
|
563 | 565 | class PlotNoiseData(PlotData): |
|
564 | 566 | CODE = 'noise' |
|
565 | 567 | |
|
566 | 568 | def setup(self): |
|
567 | 569 | |
|
568 | 570 | self.ncols = 1 |
|
569 | 571 | self.nrows = 1 |
|
570 | 572 | self.width = 10 |
|
571 | 573 | self.height = 3.2 |
|
572 | 574 | self.ylabel = 'Intensity [dB]' |
|
573 | 575 | self.titles = ['Noise'] |
|
574 | 576 | |
|
575 | 577 | if self.figure is None: |
|
576 | 578 | self.figure = plt.figure(figsize=(self.width, self.height), |
|
577 | 579 | edgecolor='k', |
|
578 | 580 | facecolor='w') |
|
579 | 581 | else: |
|
580 | 582 | self.figure.clf() |
|
581 | 583 | self.axes = [] |
|
582 | 584 | |
|
583 | 585 | self.ax = self.figure.add_subplot(self.nrows, self.ncols, 1) |
|
584 | 586 | self.ax.firsttime = True |
|
585 | 587 | |
|
586 | 588 | def plot(self): |
|
587 | 589 | |
|
588 | 590 | x = self.times |
|
589 | 591 | xmin = self.min_time |
|
590 | 592 | xmax = xmin+self.xrange*60*60 |
|
591 | 593 | if self.ax.firsttime: |
|
592 | 594 | for ch in self.dataOut.channelList: |
|
593 | 595 | y = [self.data[self.CODE][t][ch] for t in self.times] |
|
594 | 596 | self.ax.plot(x, y, lw=1, label='Ch{}'.format(ch)) |
|
595 | 597 | self.ax.firsttime = False |
|
596 | 598 | self.ax.xaxis.set_major_formatter(FuncFormatter(func)) |
|
597 | 599 | self.ax.xaxis.set_major_locator(LinearLocator(6)) |
|
598 | 600 | self.ax.set_ylabel(self.ylabel) |
|
599 | 601 | plt.legend() |
|
600 | 602 | else: |
|
601 | 603 | for ch in self.dataOut.channelList: |
|
602 | 604 | y = [self.data[self.CODE][t][ch] for t in self.times] |
|
603 | 605 | self.ax.lines[ch].set_data(x, y) |
|
604 | 606 | |
|
605 | 607 | self.ax.set_xlim(xmin, xmax) |
|
606 | 608 | self.ax.set_ylim(min(y)-5, max(y)+5) |
|
607 | 609 | self.saveTime = self.min_time |
|
608 | 610 | |
|
609 | 611 | |
|
610 | 612 | class PlotWindProfilerData(PlotRTIData): |
|
611 | 613 | CODE = 'wind' |
|
612 | 614 | colormap = 'seismic' |
|
613 | 615 | |
|
614 | 616 | def setup(self): |
|
615 | 617 | self.ncols = 1 |
|
616 | 618 | self.nrows = self.dataOut.data_output.shape[0] |
|
617 | 619 | self.width = 10 |
|
618 | 620 | self.height = 2.2*self.nrows |
|
619 | 621 | self.ylabel = 'Height [Km]' |
|
620 | 622 | self.titles = ['Zonal' ,'Meridional', 'Vertical'] |
|
621 | 623 | self.clabels = ['Velocity (m/s)','Velocity (m/s)','Velocity (cm/s)'] |
|
622 | 624 | self.windFactor = [1, 1, 100] |
|
623 | 625 | |
|
624 | 626 | if self.figure is None: |
|
625 | 627 | self.figure = plt.figure(figsize=(self.width, self.height), |
|
626 | 628 | edgecolor='k', |
|
627 | 629 | facecolor='w') |
|
628 | 630 | else: |
|
629 | 631 | self.figure.clf() |
|
630 | 632 | self.axes = [] |
|
631 | 633 | |
|
632 | 634 | for n in range(self.nrows): |
|
633 | 635 | ax = self.figure.add_subplot(self.nrows, self.ncols, n+1) |
|
634 | 636 | ax.firsttime = True |
|
635 | 637 | self.axes.append(ax) |
|
636 | 638 | |
|
637 | 639 | def plot(self): |
|
638 | 640 | |
|
639 | 641 | self.x = np.array(self.times) |
|
640 | 642 | self.y = self.dataOut.heightList |
|
641 | 643 | self.z = [] |
|
642 | 644 | |
|
643 | 645 | for ch in range(self.nrows): |
|
644 | 646 | self.z.append([self.data[self.CODE][t][ch] for t in self.times]) |
|
645 | 647 | |
|
646 | 648 | self.z = np.array(self.z) |
|
647 | 649 | self.z = numpy.ma.masked_invalid(self.z) |
|
648 | 650 | |
|
649 | 651 | cmap=plt.get_cmap(self.colormap) |
|
650 | 652 | cmap.set_bad('white', 1.) |
|
651 | 653 | |
|
652 | 654 | for n, ax in enumerate(self.axes): |
|
653 | 655 | x, y, z = self.fill_gaps(*self.decimate()) |
|
654 | 656 | xmin = self.min_time |
|
655 | 657 | xmax = xmin+self.xrange*60*60 |
|
656 | 658 | if ax.firsttime: |
|
657 | 659 | self.ymin = self.ymin if self.ymin else np.nanmin(self.y) |
|
658 | 660 | self.ymax = self.ymax if self.ymax else np.nanmax(self.y) |
|
659 | 661 | self.zmax = self.zmax if self.zmax else numpy.nanmax(abs(self.z[:-1, :])) |
|
660 | 662 | self.zmin = self.zmin if self.zmin else -self.zmax |
|
661 | 663 | |
|
662 | 664 | plot = ax.pcolormesh(x, y, z[n].T*self.windFactor[n], |
|
663 | 665 | vmin=self.zmin, |
|
664 | 666 | vmax=self.zmax, |
|
665 | 667 | cmap=cmap |
|
666 | 668 | ) |
|
667 | 669 | divider = make_axes_locatable(ax) |
|
668 | 670 | cax = divider.new_horizontal(size='2%', pad=0.05) |
|
669 | 671 | cax.set_ylabel(self.clabels[n]) |
|
670 | 672 | self.figure.add_axes(cax) |
|
671 | 673 | plt.colorbar(plot, cax) |
|
672 | 674 | ax.set_ylim(self.ymin, self.ymax) |
|
673 | 675 | |
|
674 | 676 | ax.xaxis.set_major_formatter(FuncFormatter(func)) |
|
675 | 677 | ax.xaxis.set_major_locator(LinearLocator(6)) |
|
676 | 678 | |
|
677 | 679 | ax.set_ylabel(self.ylabel) |
|
678 | 680 | |
|
679 | 681 | ax.set_xlim(xmin, xmax) |
|
680 | 682 | ax.firsttime = False |
|
681 | 683 | else: |
|
682 | 684 | ax.collections.remove(ax.collections[0]) |
|
683 | 685 | ax.set_xlim(xmin, xmax) |
|
684 | 686 | plot = ax.pcolormesh(x, y, z[n].T*self.windFactor[n], |
|
685 | 687 | vmin=self.zmin, |
|
686 | 688 | vmax=self.zmax, |
|
687 | 689 | cmap=plt.get_cmap(self.colormap) |
|
688 | 690 | ) |
|
689 | 691 | ax.set_title('{} {}'.format(self.titles[n], |
|
690 | 692 | datetime.datetime.fromtimestamp(self.max_time).strftime('%y/%m/%d %H:%M:%S')), |
|
691 | 693 | size=8) |
|
692 | 694 | |
|
693 | 695 | self.saveTime = self.min_time |
|
694 | 696 | |
|
695 | 697 | |
|
696 | 698 | class PlotSNRData(PlotRTIData): |
|
697 | 699 | CODE = 'snr' |
|
698 | 700 | colormap = 'jet' |
|
699 | 701 | |
|
700 | 702 | class PlotDOPData(PlotRTIData): |
|
701 | 703 | CODE = 'dop' |
|
702 | 704 | colormap = 'jet' |
|
703 | 705 | |
|
704 | 706 | |
|
705 | 707 | class PlotPHASEData(PlotCOHData): |
|
706 | 708 | CODE = 'phase' |
|
707 | 709 | colormap = 'seismic' |
@@ -1,457 +1,456 | |||
|
1 | 1 | ''' |
|
2 | 2 | @author: Juan C. Espinoza |
|
3 | 3 | ''' |
|
4 | 4 | |
|
5 | 5 | import time |
|
6 | 6 | import json |
|
7 | 7 | import numpy |
|
8 | 8 | import paho.mqtt.client as mqtt |
|
9 | 9 | import zmq |
|
10 | 10 | from profilehooks import profile |
|
11 | 11 | import datetime |
|
12 | 12 | from zmq.utils.monitor import recv_monitor_message |
|
13 | 13 | from functools import wraps |
|
14 | 14 | from threading import Thread |
|
15 | 15 | from multiprocessing import Process |
|
16 | 16 | |
|
17 | 17 | from schainpy.model.proc.jroproc_base import Operation, ProcessingUnit |
|
18 | 18 | |
|
19 | 19 | MAXNUMX = 100 |
|
20 | 20 | MAXNUMY = 100 |
|
21 | 21 | |
|
22 | 22 | class PrettyFloat(float): |
|
23 | 23 | def __repr__(self): |
|
24 | 24 | return '%.2f' % self |
|
25 | 25 | |
|
26 | 26 | def roundFloats(obj): |
|
27 | 27 | if isinstance(obj, list): |
|
28 | 28 | return map(roundFloats, obj) |
|
29 | 29 | elif isinstance(obj, float): |
|
30 | 30 | return round(obj, 2) |
|
31 | 31 | |
|
32 | 32 | def decimate(z, MAXNUMY): |
|
33 | 33 | # dx = int(len(self.x)/self.__MAXNUMX) + 1 |
|
34 | 34 | |
|
35 | 35 | dy = int(len(z[0])/MAXNUMY) + 1 |
|
36 | 36 | |
|
37 | 37 | return z[::, ::dy] |
|
38 | 38 | |
|
39 | 39 | class throttle(object): |
|
40 | 40 | """Decorator that prevents a function from being called more than once every |
|
41 | 41 | time period. |
|
42 | 42 | To create a function that cannot be called more than once a minute, but |
|
43 | 43 | will sleep until it can be called: |
|
44 | 44 | @throttle(minutes=1) |
|
45 | 45 | def foo(): |
|
46 | 46 | pass |
|
47 | 47 | |
|
48 | 48 | for i in range(10): |
|
49 | 49 | foo() |
|
50 | 50 | print "This function has run %s times." % i |
|
51 | 51 | """ |
|
52 | 52 | |
|
53 | 53 | def __init__(self, seconds=0, minutes=0, hours=0): |
|
54 | 54 | self.throttle_period = datetime.timedelta( |
|
55 | 55 | seconds=seconds, minutes=minutes, hours=hours |
|
56 | 56 | ) |
|
57 | 57 | |
|
58 | 58 | self.time_of_last_call = datetime.datetime.min |
|
59 | 59 | |
|
60 | 60 | def __call__(self, fn): |
|
61 | 61 | @wraps(fn) |
|
62 | 62 | def wrapper(*args, **kwargs): |
|
63 | 63 | now = datetime.datetime.now() |
|
64 | 64 | time_since_last_call = now - self.time_of_last_call |
|
65 | 65 | time_left = self.throttle_period - time_since_last_call |
|
66 | 66 | |
|
67 | 67 | if time_left > datetime.timedelta(seconds=0): |
|
68 | 68 | return |
|
69 | 69 | |
|
70 | 70 | self.time_of_last_call = datetime.datetime.now() |
|
71 | 71 | return fn(*args, **kwargs) |
|
72 | 72 | |
|
73 | 73 | return wrapper |
|
74 | 74 | |
|
75 | 75 | |
|
76 | 76 | class PublishData(Operation): |
|
77 | 77 | """Clase publish.""" |
|
78 | 78 | |
|
79 | 79 | def __init__(self, **kwargs): |
|
80 | 80 | """Inicio.""" |
|
81 | 81 | Operation.__init__(self, **kwargs) |
|
82 | 82 | self.isConfig = False |
|
83 | 83 | self.client = None |
|
84 | 84 | self.zeromq = None |
|
85 | 85 | self.mqtt = None |
|
86 | 86 | |
|
87 | 87 | def on_disconnect(self, client, userdata, rc): |
|
88 | 88 | if rc != 0: |
|
89 | 89 | print("Unexpected disconnection.") |
|
90 | 90 | self.connect() |
|
91 | 91 | |
|
92 | 92 | def connect(self): |
|
93 | 93 | print 'trying to connect' |
|
94 | 94 | try: |
|
95 | 95 | self.client.connect( |
|
96 | 96 | host=self.host, |
|
97 | 97 | port=self.port, |
|
98 | 98 | keepalive=60*10, |
|
99 | 99 | bind_address='') |
|
100 | 100 | self.client.loop_start() |
|
101 | 101 | # self.client.publish( |
|
102 | 102 | # self.topic + 'SETUP', |
|
103 | 103 | # json.dumps(setup), |
|
104 | 104 | # retain=True |
|
105 | 105 | # ) |
|
106 | 106 | except: |
|
107 | 107 | print "MQTT Conection error." |
|
108 | 108 | self.client = False |
|
109 | 109 | |
|
110 | 110 | def setup(self, port=1883, username=None, password=None, clientId="user", zeromq=1, verbose=True, **kwargs): |
|
111 | 111 | self.counter = 0 |
|
112 | 112 | self.topic = kwargs.get('topic', 'schain') |
|
113 | 113 | self.delay = kwargs.get('delay', 0) |
|
114 | 114 | self.plottype = kwargs.get('plottype', 'spectra') |
|
115 | 115 | self.host = kwargs.get('host', "10.10.10.82") |
|
116 | 116 | self.port = kwargs.get('port', 3000) |
|
117 | 117 | self.clientId = clientId |
|
118 | 118 | self.cnt = 0 |
|
119 | 119 | self.zeromq = zeromq |
|
120 | 120 | self.mqtt = kwargs.get('plottype', 0) |
|
121 | 121 | self.client = None |
|
122 | 122 | self.verbose = verbose |
|
123 | 123 | self.dataOut.firstdata = True |
|
124 | 124 | setup = [] |
|
125 | 125 | if mqtt is 1: |
|
126 | 126 | self.client = mqtt.Client( |
|
127 | 127 | client_id=self.clientId + self.topic + 'SCHAIN', |
|
128 | 128 | clean_session=True) |
|
129 | 129 | self.client.on_disconnect = self.on_disconnect |
|
130 | 130 | self.connect() |
|
131 | 131 | for plot in self.plottype: |
|
132 | 132 | setup.append({ |
|
133 | 133 | 'plot': plot, |
|
134 | 134 | 'topic': self.topic + plot, |
|
135 | 135 | 'title': getattr(self, plot + '_' + 'title', False), |
|
136 | 136 | 'xlabel': getattr(self, plot + '_' + 'xlabel', False), |
|
137 | 137 | 'ylabel': getattr(self, plot + '_' + 'ylabel', False), |
|
138 | 138 | 'xrange': getattr(self, plot + '_' + 'xrange', False), |
|
139 | 139 | 'yrange': getattr(self, plot + '_' + 'yrange', False), |
|
140 | 140 | 'zrange': getattr(self, plot + '_' + 'zrange', False), |
|
141 | 141 | }) |
|
142 | 142 | if zeromq is 1: |
|
143 | 143 | context = zmq.Context() |
|
144 | 144 | self.zmq_socket = context.socket(zmq.PUSH) |
|
145 | 145 | server = kwargs.get('server', 'zmq.pipe') |
|
146 | 146 | |
|
147 | 147 | if 'tcp://' in server: |
|
148 | 148 | address = server |
|
149 | 149 | else: |
|
150 | 150 | address = 'ipc:///tmp/%s' % server |
|
151 | 151 | |
|
152 | 152 | self.zmq_socket.connect(address) |
|
153 | 153 | time.sleep(1) |
|
154 | 154 | |
|
155 | 155 | |
|
156 | 156 | def publish_data(self): |
|
157 | 157 | self.dataOut.finished = False |
|
158 | 158 | if self.mqtt is 1: |
|
159 | 159 | yData = self.dataOut.heightList[:2].tolist() |
|
160 | 160 | if self.plottype == 'spectra': |
|
161 | 161 | data = getattr(self.dataOut, 'data_spc') |
|
162 | 162 | z = data/self.dataOut.normFactor |
|
163 | 163 | zdB = 10*numpy.log10(z) |
|
164 | 164 | xlen, ylen = zdB[0].shape |
|
165 | 165 | dx = int(xlen/MAXNUMX) + 1 |
|
166 | 166 | dy = int(ylen/MAXNUMY) + 1 |
|
167 | 167 | Z = [0 for i in self.dataOut.channelList] |
|
168 | 168 | for i in self.dataOut.channelList: |
|
169 | 169 | Z[i] = zdB[i][::dx, ::dy].tolist() |
|
170 | 170 | payload = { |
|
171 | 171 | 'timestamp': self.dataOut.utctime, |
|
172 | 172 | 'data': roundFloats(Z), |
|
173 | 173 | 'channels': ['Ch %s' % ch for ch in self.dataOut.channelList], |
|
174 | 174 | 'interval': self.dataOut.getTimeInterval(), |
|
175 | 175 | 'type': self.plottype, |
|
176 | 176 | 'yData': yData |
|
177 | 177 | } |
|
178 | 178 | # print payload |
|
179 | 179 | |
|
180 | 180 | elif self.plottype in ('rti', 'power'): |
|
181 | 181 | data = getattr(self.dataOut, 'data_spc') |
|
182 | 182 | z = data/self.dataOut.normFactor |
|
183 | 183 | avg = numpy.average(z, axis=1) |
|
184 | 184 | avgdB = 10*numpy.log10(avg) |
|
185 | 185 | xlen, ylen = z[0].shape |
|
186 | 186 | dy = numpy.floor(ylen/self.__MAXNUMY) + 1 |
|
187 | 187 | AVG = [0 for i in self.dataOut.channelList] |
|
188 | 188 | for i in self.dataOut.channelList: |
|
189 | 189 | AVG[i] = avgdB[i][::dy].tolist() |
|
190 | 190 | payload = { |
|
191 | 191 | 'timestamp': self.dataOut.utctime, |
|
192 | 192 | 'data': roundFloats(AVG), |
|
193 | 193 | 'channels': ['Ch %s' % ch for ch in self.dataOut.channelList], |
|
194 | 194 | 'interval': self.dataOut.getTimeInterval(), |
|
195 | 195 | 'type': self.plottype, |
|
196 | 196 | 'yData': yData |
|
197 | 197 | } |
|
198 | 198 | elif self.plottype == 'noise': |
|
199 | 199 | noise = self.dataOut.getNoise()/self.dataOut.normFactor |
|
200 | 200 | noisedB = 10*numpy.log10(noise) |
|
201 | 201 | payload = { |
|
202 | 202 | 'timestamp': self.dataOut.utctime, |
|
203 | 203 | 'data': roundFloats(noisedB.reshape(-1, 1).tolist()), |
|
204 | 204 | 'channels': ['Ch %s' % ch for ch in self.dataOut.channelList], |
|
205 | 205 | 'interval': self.dataOut.getTimeInterval(), |
|
206 | 206 | 'type': self.plottype, |
|
207 | 207 | 'yData': yData |
|
208 | 208 | } |
|
209 | 209 | elif self.plottype == 'snr': |
|
210 | 210 | data = getattr(self.dataOut, 'data_SNR') |
|
211 | 211 | avgdB = 10*numpy.log10(data) |
|
212 | 212 | |
|
213 | 213 | ylen = data[0].size |
|
214 | 214 | dy = numpy.floor(ylen/self.__MAXNUMY) + 1 |
|
215 | 215 | AVG = [0 for i in self.dataOut.channelList] |
|
216 | 216 | for i in self.dataOut.channelList: |
|
217 | 217 | AVG[i] = avgdB[i][::dy].tolist() |
|
218 | 218 | payload = { |
|
219 | 219 | 'timestamp': self.dataOut.utctime, |
|
220 | 220 | 'data': roundFloats(AVG), |
|
221 | 221 | 'channels': ['Ch %s' % ch for ch in self.dataOut.channelList], |
|
222 | 222 | 'type': self.plottype, |
|
223 | 223 | 'yData': yData |
|
224 | 224 | } |
|
225 | 225 | else: |
|
226 | 226 | print "Tipo de grafico invalido" |
|
227 | 227 | payload = { |
|
228 | 228 | 'data': 'None', |
|
229 | 229 | 'timestamp': 'None', |
|
230 | 230 | 'type': None |
|
231 | 231 | } |
|
232 | 232 | # print 'Publishing data to {}'.format(self.host) |
|
233 | 233 | self.client.publish(self.topic + self.plottype, json.dumps(payload), qos=0) |
|
234 | 234 | |
|
235 | 235 | if self.zeromq is 1: |
|
236 | 236 | if self.verbose: |
|
237 | 237 | print '[Sending] {} - {}'.format(self.dataOut.type, self.dataOut.datatime) |
|
238 | 238 | self.zmq_socket.send_pyobj(self.dataOut) |
|
239 | 239 | self.dataOut.firstdata = False |
|
240 | 240 | |
|
241 | 241 | |
|
242 | 242 | def run(self, dataOut, **kwargs): |
|
243 | 243 | self.dataOut = dataOut |
|
244 | 244 | if not self.isConfig: |
|
245 | 245 | self.setup(**kwargs) |
|
246 | 246 | self.isConfig = True |
|
247 | 247 | |
|
248 | 248 | self.publish_data() |
|
249 | 249 | time.sleep(self.delay) |
|
250 | 250 | |
|
251 | 251 | def close(self): |
|
252 | 252 | if self.zeromq is 1: |
|
253 | 253 | self.dataOut.finished = True |
|
254 | 254 | self.zmq_socket.send_pyobj(self.dataOut) |
|
255 | 255 | self.zmq_socket.close() |
|
256 | 256 | if self.client: |
|
257 | 257 | self.client.loop_stop() |
|
258 | 258 | self.client.disconnect() |
|
259 | 259 | |
|
260 | 260 | class ReceiverData(ProcessingUnit, Process): |
|
261 | 261 | |
|
262 | 262 | throttle_value = 5 |
|
263 | 263 | |
|
264 | 264 | def __init__(self, **kwargs): |
|
265 | 265 | |
|
266 | 266 | ProcessingUnit.__init__(self, **kwargs) |
|
267 | 267 | Process.__init__(self) |
|
268 | 268 | self.mp = False |
|
269 | 269 | self.isConfig = False |
|
270 | 270 | self.isWebConfig = False |
|
271 | 271 | self.plottypes =[] |
|
272 | 272 | self.connections = 0 |
|
273 | 273 | server = kwargs.get('server', 'zmq.pipe') |
|
274 | 274 | plot_server = kwargs.get('plot_server', 'zmq.web') |
|
275 | 275 | if 'tcp://' in server: |
|
276 | 276 | address = server |
|
277 | 277 | else: |
|
278 | 278 | address = 'ipc:///tmp/%s' % server |
|
279 | 279 | |
|
280 | 280 | if 'tcp://' in plot_server: |
|
281 | 281 | plot_address = plot_server |
|
282 | 282 | else: |
|
283 | 283 | plot_address = 'ipc:///tmp/%s' % plot_server |
|
284 | 284 | |
|
285 | 285 | self.address = address |
|
286 | 286 | self.plot_address = plot_address |
|
287 | 287 | self.plottypes = [s.strip() for s in kwargs.get('plottypes', 'rti').split(',')] |
|
288 | 288 | self.realtime = kwargs.get('realtime', False) |
|
289 | 289 | self.throttle_value = kwargs.get('throttle', 5) |
|
290 | 290 | self.sendData = self.initThrottle(self.throttle_value) |
|
291 | 291 | self.setup() |
|
292 | 292 | |
|
293 | 293 | def setup(self): |
|
294 | 294 | |
|
295 | 295 | self.data = {} |
|
296 | 296 | self.data['times'] = [] |
|
297 | 297 | for plottype in self.plottypes: |
|
298 | 298 | self.data[plottype] = {} |
|
299 | 299 | self.data['noise'] = {} |
|
300 | 300 | self.data['throttle'] = self.throttle_value |
|
301 | 301 | self.data['ENDED'] = False |
|
302 | 302 | self.isConfig = True |
|
303 | 303 | self.data_web = {} |
|
304 | 304 | |
|
305 | 305 | def event_monitor(self, monitor): |
|
306 | 306 | |
|
307 | 307 | events = {} |
|
308 | 308 | |
|
309 | 309 | for name in dir(zmq): |
|
310 | 310 | if name.startswith('EVENT_'): |
|
311 | 311 | value = getattr(zmq, name) |
|
312 | 312 | events[value] = name |
|
313 | 313 | |
|
314 | 314 | while monitor.poll(): |
|
315 | 315 | evt = recv_monitor_message(monitor) |
|
316 | 316 | if evt['event'] == 32: |
|
317 | 317 | self.connections += 1 |
|
318 | 318 | if evt['event'] == 512: |
|
319 | 319 | pass |
|
320 | 320 | if self.connections == 0 and self.started is True: |
|
321 | 321 | self.ended = True |
|
322 | 322 | |
|
323 | 323 | evt.update({'description': events[evt['event']]}) |
|
324 | 324 | |
|
325 | 325 | if evt['event'] == zmq.EVENT_MONITOR_STOPPED: |
|
326 | 326 | break |
|
327 | 327 | monitor.close() |
|
328 | 328 | print("event monitor thread done!") |
|
329 | 329 | |
|
330 | 330 | def initThrottle(self, throttle_value): |
|
331 | 331 | |
|
332 | 332 | @throttle(seconds=throttle_value) |
|
333 | 333 | def sendDataThrottled(fn_sender, data): |
|
334 | 334 | fn_sender(data) |
|
335 | 335 | |
|
336 | 336 | return sendDataThrottled |
|
337 | 337 | |
|
338 | 338 | |
|
339 | 339 | def send(self, data): |
|
340 | 340 | # print '[sending] data=%s size=%s' % (data.keys(), len(data['times'])) |
|
341 | 341 | self.sender.send_pyobj(data) |
|
342 | 342 | |
|
343 | 343 | |
|
344 | 344 | def update(self): |
|
345 | 345 | t = self.dataOut.utctime |
|
346 | 346 | |
|
347 | 347 | if t in self.data['times']: |
|
348 | 348 | return |
|
349 | 349 | |
|
350 | 350 | self.data['times'].append(t) |
|
351 | 351 | self.data['dataOut'] = self.dataOut |
|
352 | 352 | |
|
353 | 353 | for plottype in self.plottypes: |
|
354 | 354 | if plottype == 'spc': |
|
355 | 355 | z = self.dataOut.data_spc/self.dataOut.normFactor |
|
356 | 356 | self.data[plottype] = 10*numpy.log10(z) |
|
357 | 357 | self.data['noise'][t] = 10*numpy.log10(self.dataOut.getNoise()/self.dataOut.normFactor) |
|
358 | 358 | if plottype == 'cspc': |
|
359 | 359 | jcoherence = self.dataOut.data_cspc/numpy.sqrt(self.dataOut.data_spc*self.dataOut.data_spc) |
|
360 | 360 | self.data['cspc_coh'] = numpy.abs(jcoherence) |
|
361 | 361 | self.data['cspc_phase'] = numpy.arctan2(jcoherence.imag, jcoherence.real)*180/numpy.pi |
|
362 | 362 | if plottype == 'rti': |
|
363 | 363 | self.data[plottype][t] = self.dataOut.getPower() |
|
364 | 364 | if plottype == 'snr': |
|
365 | 365 | self.data[plottype][t] = 10*numpy.log10(self.dataOut.data_SNR) |
|
366 | 366 | if plottype == 'dop': |
|
367 | 367 | self.data[plottype][t] = 10*numpy.log10(self.dataOut.data_DOP) |
|
368 | 368 | if plottype == 'mean': |
|
369 | 369 | self.data[plottype][t] = self.dataOut.data_MEAN |
|
370 | 370 | if plottype == 'std': |
|
371 | 371 | self.data[plottype][t] = self.dataOut.data_STD |
|
372 | 372 | if plottype == 'coh': |
|
373 | 373 | self.data[plottype][t] = self.dataOut.getCoherence() |
|
374 | 374 | if plottype == 'phase': |
|
375 | 375 | self.data[plottype][t] = self.dataOut.getCoherence(phase=True) |
|
376 | 376 | if plottype == 'wind': |
|
377 | 377 | self.data[plottype][t] = self.dataOut.data_output |
|
378 | 378 | if self.realtime: |
|
379 | 379 | self.data_web['timestamp'] = t |
|
380 | 380 | if plottype == 'spc': |
|
381 | 381 | self.data_web[plottype] = roundFloats(decimate(self.data[plottype]).tolist()) |
|
382 | 382 | elif plottype == 'cspc': |
|
383 | 383 | self.data_web['cspc_coh'] = roundFloats(decimate(self.data['cspc_coh']).tolist()) |
|
384 | 384 | self.data_web['cspc_phase'] = roundFloats(decimate(self.data['cspc_phase']).tolist()) |
|
385 | 385 | elif plottype == 'noise': |
|
386 | 386 | self.data_web['noise'] = roundFloats(self.data['noise'][t].tolist()) |
|
387 | 387 | else: |
|
388 | 388 | self.data_web[plottype] = roundFloats(decimate(self.data[plottype][t]).tolist()) |
|
389 | 389 | self.data_web['interval'] = self.dataOut.getTimeInterval() |
|
390 | 390 | self.data_web['type'] = plottype |
|
391 | 391 | |
|
392 | 392 | def run(self): |
|
393 | 393 | |
|
394 | 394 | print '[Starting] {} from {}'.format(self.name, self.address) |
|
395 | 395 | |
|
396 | 396 | self.context = zmq.Context() |
|
397 | 397 | self.receiver = self.context.socket(zmq.PULL) |
|
398 | 398 | self.receiver.bind(self.address) |
|
399 | 399 | monitor = self.receiver.get_monitor_socket() |
|
400 | 400 | self.sender = self.context.socket(zmq.PUB) |
|
401 | 401 | if self.realtime: |
|
402 | 402 | self.sender_web = self.context.socket(zmq.PUB) |
|
403 | 403 | self.sender_web.connect(self.plot_address) |
|
404 | 404 | time.sleep(1) |
|
405 | 405 | self.sender.bind("ipc:///tmp/zmq.plots") |
|
406 | 406 | time.sleep(3) |
|
407 | 407 | t = Thread(target=self.event_monitor, args=(monitor,)) |
|
408 | 408 | t.start() |
|
409 | 409 | |
|
410 | 410 | while True: |
|
411 | 411 | self.dataOut = self.receiver.recv_pyobj() |
|
412 | 412 | # print '[Receiving] {} - {}'.format(self.dataOut.type, |
|
413 | 413 | # self.dataOut.datatime.ctime()) |
|
414 | 414 | |
|
415 | 415 | self.update() |
|
416 | 416 | |
|
417 | 417 | if self.dataOut.firstdata is True: |
|
418 | 418 | self.data['STARTED'] = True |
|
419 | 419 | |
|
420 | ||
|
421 | 420 | if self.dataOut.finished is True: |
|
422 | 421 | self.send(self.data) |
|
423 | 422 | self.connections -= 1 |
|
424 | 423 | if self.connections == 0 and self.started: |
|
425 | 424 | self.ended = True |
|
426 | 425 | self.data['ENDED'] = True |
|
427 | 426 | self.send(self.data) |
|
428 | 427 | self.setup() |
|
429 | 428 | self.started = False |
|
430 | 429 | else: |
|
431 | 430 | if self.realtime: |
|
432 | 431 | self.send(self.data) |
|
433 | 432 | self.sender_web.send_string(json.dumps(self.data_web)) |
|
434 | 433 | else: |
|
435 | 434 | self.sendData(self.send, self.data) |
|
436 | 435 | self.started = True |
|
437 | 436 | |
|
438 | 437 | self.data['STARTED'] = False |
|
439 | 438 | return |
|
440 | 439 | |
|
441 | 440 | def sendToWeb(self): |
|
442 | 441 | |
|
443 | 442 | if not self.isWebConfig: |
|
444 | 443 | context = zmq.Context() |
|
445 | 444 | sender_web_config = context.socket(zmq.PUB) |
|
446 | 445 | if 'tcp://' in self.plot_address: |
|
447 | 446 | dum, address, port = self.plot_address.split(':') |
|
448 | 447 | conf_address = '{}:{}:{}'.format(dum, address, int(port)+1) |
|
449 | 448 | else: |
|
450 | 449 | conf_address = self.plot_address + '.config' |
|
451 | 450 | sender_web_config.bind(conf_address) |
|
452 | 451 | time.sleep(1) |
|
453 | 452 | for kwargs in self.operationKwargs.values(): |
|
454 | 453 | if 'plot' in kwargs: |
|
455 | 454 | print '[Sending] Config data to web for {}'.format(kwargs['code'].upper()) |
|
456 | 455 | sender_web_config.send_string(json.dumps(kwargs)) |
|
457 | 456 | self.isWebConfig = True |
@@ -1,58 +1,59 | |||
|
1 | 1 | #!/usr/bin/env python |
|
2 | 2 | ''' |
|
3 | 3 | Created on Jul 7, 2014 |
|
4 | 4 | |
|
5 | 5 | @author: roj-idl71 |
|
6 | 6 | ''' |
|
7 | 7 | import os, sys |
|
8 | 8 | |
|
9 | 9 | from schainpy.controller import Project |
|
10 | 10 | |
|
11 | 11 | if __name__ == '__main__': |
|
12 | 12 | desc = "Segundo Test" |
|
13 | 13 | |
|
14 | 14 | controllerObj = Project() |
|
15 | 15 | controllerObj.setup(id='191', name='test01', description=desc) |
|
16 | 16 | |
|
17 | 17 | proc1 = controllerObj.addProcUnit(name='ReceiverData') |
|
18 | 18 | proc1.addParameter(name='realtime', value='0', format='bool') |
|
19 | 19 | proc1.addParameter(name='plottypes', value='rti,coh,phase', format='str') |
|
20 | 20 | proc1.addParameter(name='throttle', value='10', format='int') |
|
21 | proc1.addParameter(name='interactive', value='0', format='bool') | |
|
21 | 22 | # proc1.addParameter(name='server', value='tcp://10.10.10.82:7000', format='str') |
|
22 | 23 | ## TODO Agregar direccion de server de publicacion a graficos como variable |
|
23 | 24 | |
|
24 | 25 | op1 = proc1.addOperation(name='PlotRTIData', optype='other') |
|
25 | 26 | op1.addParameter(name='wintitle', value='Julia 150Km', format='str') |
|
26 | 27 | op1.addParameter(name='save', value='/home/nanosat/Pictures', format='str') |
|
27 | 28 | op1.addParameter(name='show', value='0', format='bool') |
|
28 | 29 | op1.addParameter(name='colormap', value='jet', format='str') |
|
29 | 30 | # |
|
30 | 31 | op2 = proc1.addOperation(name='PlotCOHData', optype='other') |
|
31 | 32 | op2.addParameter(name='wintitle', value='Julia 150Km', format='str') |
|
32 | 33 | op2.addParameter(name='save', value='/home/nanosat/Pictures', format='str') |
|
33 | 34 | op2.addParameter(name='colormap', value='jet', format='str') |
|
34 | 35 | op2.addParameter(name='show', value='0', format='bool') |
|
35 | 36 | # # # |
|
36 | 37 | op6 = proc1.addOperation(name='PlotPHASEData', optype='other') |
|
37 | 38 | op6.addParameter(name='wintitle', value='Julia 150Km', format='str') |
|
38 | 39 | op6.addParameter(name='save', value='/home/nanosat/Pictures', format='str') |
|
39 | 40 | op6.addParameter(name='show', value='1', format='bool') |
|
40 | 41 | # # |
|
41 | 42 | # # proc2 = controllerObj.addProcUnit(name='ReceiverData') |
|
42 | 43 | # # proc2.addParameter(name='server', value='juanca', format='str') |
|
43 | 44 | # # proc2.addParameter(name='plottypes', value='snr,dop', format='str') |
|
44 | 45 | # # |
|
45 | 46 | # op3 = proc1.addOperation(name='PlotSNRData', optype='other') |
|
46 | 47 | # op3.addParameter(name='wintitle', value='Julia 150Km', format='str') |
|
47 | 48 | # op3.addParameter(name='save', value='/home/nanosat/Pictures', format='str') |
|
48 | 49 | # op3.addParameter(name='show', value='0', format='bool') |
|
49 | 50 | # # |
|
50 | 51 | # op4 = proc1.addOperation(name='PlotDOPData', optype='other') |
|
51 | 52 | # op4.addParameter(name='wintitle', value='Julia 150Km', format='str') |
|
52 | 53 | # op4.addParameter(name='save', value='/home/nanosat/Pictures', format='str') |
|
53 | 54 | # op4.addParameter(name='show', value='0', format='bool') |
|
54 | 55 | # op4.addParameter(name='colormap', value='jet', format='str') |
|
55 | 56 | |
|
56 | 57 | |
|
57 | 58 | |
|
58 | 59 | controllerObj.start() |
@@ -1,1 +1,1 | |||
|
1 |
<Project description="HF_EXAMPLE" id="191" name="test01"><ReadUnit datatype="SpectraReader" id="1911" inputId="0" name="SpectraReader"><Operation id="19111" name="run" priority="1" type="self"><Parameter format="str" id="191111" name="datatype" value="SpectraReader" /><Parameter format="str" id="191112" name="path" value="/home/nanosat/data/sp1_f0" /><Parameter format="date" id="191113" name="startDate" value="2017/01/28" /><Parameter format="date" id="191114" name="endDate" value="2017/01/28" /><Parameter format="time" id="191115" name="startTime" value="00:00:00" /><Parameter format="time" id="191116" name="endTime" value="23:59:59" /><Parameter format="int" id="191118" name="cursor" value=" |
|
|
1 | <Project description="HF_EXAMPLE" id="191" name="test01"><ReadUnit datatype="SpectraReader" id="1911" inputId="0" name="SpectraReader"><Operation id="19111" name="run" priority="1" type="self"><Parameter format="str" id="191111" name="datatype" value="SpectraReader" /><Parameter format="str" id="191112" name="path" value="/home/nanosat/data/sp1_f0" /><Parameter format="date" id="191113" name="startDate" value="2017/01/28" /><Parameter format="date" id="191114" name="endDate" value="2017/01/28" /><Parameter format="time" id="191115" name="startTime" value="00:00:00" /><Parameter format="time" id="191116" name="endTime" value="23:59:59" /><Parameter format="int" id="191118" name="cursor" value="5" /><Parameter format="int" id="191119" name="skip" value="177" /><Parameter format="int" id="191120" name="walk" value="1" /><Parameter format="int" id="191121" name="verbose" value="1" /><Parameter format="int" id="191122" name="online" value="0" /></Operation></ReadUnit><ProcUnit datatype="ParametersProc" id="1913" inputId="1911" name="ParametersProc"><Operation id="19131" name="run" priority="1" type="self" /><Operation id="19132" name="SpectralMoments" priority="2" type="other" /><Operation id="19133" name="PublishData" priority="3" type="other"><Parameter format="int" id="191331" name="zeromq" value="1" /><Parameter format="bool" id="191332" name="verbose" value="0" /><Parameter format="int" id="191333" name="delay" value="0" /></Operation></ProcUnit><ProcUnit datatype="Spectra" id="1912" inputId="1911" name="SpectraProc"><Operation id="19121" name="run" priority="1" type="self" /></ProcUnit></Project> No newline at end of file |
General Comments 0
You need to be logged in to leave comments.
Login now