##// END OF EJS Templates
add NoisePlot
jespinoza -
r907:4034368d1421
parent child
Show More
@@ -1,377 +1,427
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.pyplot as plt
9 9 from mpl_toolkits.axes_grid1 import make_axes_locatable
10 10 from matplotlib.ticker import FuncFormatter, LinearLocator
11 11 from multiprocessing import Process
12 12
13 13 from schainpy.model.proc.jroproc_base import Operation
14 14
15 15 #plt.ion()
16 16
17 17 func = lambda x, pos: ('%s') %(datetime.datetime.utcfromtimestamp(x).strftime('%H:%M'))
18 18
19 19 d1970 = datetime.datetime(1970,1,1)
20 20
21 21 class PlotData(Operation, Process):
22 22
23 23 CODE = 'Figure'
24 24 colormap = 'jet'
25 25 CONFLATE = True
26 26 __MAXNUMX = 80
27 27 __MAXNUMY = 80
28 28 __missing = 1E30
29 29
30 30 def __init__(self, **kwargs):
31 31
32 32 Operation.__init__(self, **kwargs)
33 33 Process.__init__(self)
34 34 self.mp = False
35 35 self.dataOut = None
36 36 self.isConfig = False
37 37 self.figure = None
38 38 self.axes = []
39 39 self.localtime = kwargs.pop('localtime', True)
40 40 self.show = kwargs.get('show', True)
41 41 self.save = kwargs.get('save', False)
42 42 self.colormap = kwargs.get('colormap', self.colormap)
43 self.showprofile = kwargs.get('showprofile', False)
43 self.showprofile = kwargs.get('showprofile', True)
44 44 self.title = kwargs.get('wintitle', '')
45 45 self.xaxis = kwargs.get('xaxis', 'time')
46 46 self.zmin = kwargs.get('zmin', None)
47 47 self.zmax = kwargs.get('zmax', None)
48 48 self.xmin = kwargs.get('xmin', None)
49 49 self.xmax = kwargs.get('xmax', None)
50 50 self.xrange = kwargs.get('xrange', 24)
51 51 self.ymin = kwargs.get('ymin', None)
52 52 self.ymax = kwargs.get('ymax', None)
53 53 self.throttle_value = 1
54 54 def fill_gaps(self, x_buffer, y_buffer, z_buffer):
55 55
56 56 if x_buffer.shape[0] < 2:
57 57 return x_buffer, y_buffer, z_buffer
58 58
59 59 deltas = x_buffer[1:] - x_buffer[0:-1]
60 60 x_median = np.median(deltas)
61 61
62 62 index = np.where(deltas > 5*x_median)
63 63
64 64 if len(index[0]) != 0:
65 65 z_buffer[::, index[0], ::] = self.__missing
66 66 z_buffer = np.ma.masked_inside(z_buffer,
67 67 0.99*self.__missing,
68 68 1.01*self.__missing)
69 69
70 70 return x_buffer, y_buffer, z_buffer
71 71
72 72 def decimate(self):
73 73
74 74 # dx = int(len(self.x)/self.__MAXNUMX) + 1
75 75 dy = int(len(self.y)/self.__MAXNUMY) + 1
76 76
77 77 # x = self.x[::dx]
78 78 x = self.x
79 79 y = self.y[::dy]
80 80 z = self.z[::, ::, ::dy]
81 81
82 82 return x, y, z
83 83
84 84 def __plot(self):
85 85
86 86 print 'plotting...{}'.format(self.CODE)
87 87
88 88 self.plot()
89 89 self.figure.suptitle('{} {} - Date:{}'.format(self.title, self.CODE.upper(),
90 90 datetime.datetime.utcfromtimestamp(self.max_time).strftime('%y/%m/%d %H:%M:%S')))
91 91
92 92 if self.save:
93 93 figname = os.path.join(self.save, '{}_{}.png'.format(self.CODE,
94 94 datetime.datetime.utcfromtimestamp(self.times[0]).strftime('%y%m%d_%H%M%S')))
95 95 print 'Saving figure: {}'.format(figname)
96 96 self.figure.savefig(figname)
97 97
98 98 self.figure.canvas.draw()
99 99
100 100 def plot(self):
101 101
102 102 print 'plotting...{}'.format(self.CODE.upper())
103 103 return
104 104
105 105 def run(self):
106 106
107 107 print '[Starting] {}'.format(self.name)
108 108 context = zmq.Context()
109 109 receiver = context.socket(zmq.SUB)
110 110 receiver.setsockopt(zmq.SUBSCRIBE, '')
111 111 receiver.setsockopt(zmq.CONFLATE, self.CONFLATE)
112 112 receiver.connect("ipc:///tmp/zmq.plots")
113 113
114 114 while True:
115 115 try:
116 116 #if True:
117 117 self.data = receiver.recv_pyobj(flags=zmq.NOBLOCK)
118 118 self.dataOut = self.data['dataOut']
119 119 self.times = self.data['times']
120 120 self.times.sort()
121 121 self.throttle_value = self.data['throttle']
122 122 self.min_time = self.times[0]
123 123 self.max_time = self.times[-1]
124 124
125 125 if self.isConfig is False:
126 126 self.setup()
127 127 self.isConfig = True
128 128 self.__plot()
129 129
130 130 if self.data['ENDED'] is True:
131 131 # self.__plot()
132 132 self.isConfig = False
133 133
134 134 except zmq.Again as e:
135 135 print 'Waiting for data...'
136 136 plt.pause(self.throttle_value)
137 137 # time.sleep(3)
138 138
139 139 def close(self):
140 140 if self.dataOut:
141 141 self._plot()
142 142
143 143
144 144 class PlotSpectraData(PlotData):
145 145
146 146 CODE = 'spc'
147 147 colormap = 'jro'
148 148 CONFLATE = False
149 149 def setup(self):
150 150
151 151 ncolspan = 1
152 152 colspan = 1
153 153 self.ncols = int(numpy.sqrt(self.dataOut.nChannels)+0.9)
154 154 self.nrows = int(self.dataOut.nChannels*1./self.ncols + 0.9)
155 155 self.width = 3.6*self.ncols
156 156 self.height = 3.2*self.nrows
157 157 if self.showprofile:
158 158 ncolspan = 3
159 159 colspan = 2
160 160 self.width += 1.2*self.ncols
161 161
162 162 self.ylabel = 'Range [Km]'
163 163 self.titles = ['Channel {}'.format(x) for x in self.dataOut.channelList]
164 164
165 165 if self.figure is None:
166 166 self.figure = plt.figure(figsize=(self.width, self.height),
167 167 edgecolor='k',
168 168 facecolor='w')
169 169 else:
170 170 self.figure.clf()
171 171
172 172 n = 0
173 173 for y in range(self.nrows):
174 174 for x in range(self.ncols):
175 175 if n >= self.dataOut.nChannels:
176 176 break
177 177 ax = plt.subplot2grid((self.nrows, self.ncols*ncolspan), (y, x*ncolspan), 1, colspan)
178 178 if self.showprofile:
179 179 ax.ax_profile = plt.subplot2grid((self.nrows, self.ncols*ncolspan), (y, x*ncolspan+colspan), 1, 1)
180 180
181 181 ax.firsttime = True
182 182 self.axes.append(ax)
183 183 n += 1
184 184
185 self.figure.subplots_adjust(wspace=0.9, hspace=0.5)
185 self.figure.subplots_adjust(left=0.1, right=0.95, bottom=0.15, top=0.85, wspace=0.9, hspace=0.5)
186 186 self.figure.show()
187 187
188 188 def plot(self):
189 189
190 190 if self.xaxis == "frequency":
191 191 x = self.dataOut.getFreqRange(1)/1000.
192 192 xlabel = "Frequency (kHz)"
193 193 elif self.xaxis == "time":
194 194 x = self.dataOut.getAcfRange(1)
195 195 xlabel = "Time (ms)"
196 196 else:
197 197 x = self.dataOut.getVelRange(1)
198 198 xlabel = "Velocity (m/s)"
199 199
200 200 y = self.dataOut.getHeiRange()
201 201 z = self.data[self.CODE]
202 202
203 203 for n, ax in enumerate(self.axes):
204 204
205 205 if ax.firsttime:
206 206 self.xmax = self.xmax if self.xmax else np.nanmax(x)
207 207 self.xmin = self.xmin if self.xmin else -self.xmax
208 208 self.ymin = self.ymin if self.ymin else np.nanmin(y)
209 209 self.ymax = self.ymax if self.ymax else np.nanmax(y)
210 210 self.zmin = self.zmin if self.zmin else np.nanmin(z)
211 211 self.zmax = self.zmax if self.zmax else np.nanmax(z)
212 212 ax.plot = ax.pcolormesh(x, y, z[n].T,
213 213 vmin=self.zmin,
214 214 vmax=self.zmax,
215 215 cmap=plt.get_cmap(self.colormap)
216 216 )
217 217 divider = make_axes_locatable(ax)
218 218 cax = divider.new_horizontal(size='3%', pad=0.05)
219 219 self.figure.add_axes(cax)
220 220 plt.colorbar(ax.plot, cax)
221 221
222 222 ax.set_xlim(self.xmin, self.xmax)
223 223 ax.set_ylim(self.ymin, self.ymax)
224 224
225 225 ax.xaxis.set_major_locator(LinearLocator(5))
226 226 #ax.yaxis.set_major_locator(LinearLocator(4))
227 227
228 228 ax.set_ylabel(self.ylabel)
229 229 ax.set_xlabel(xlabel)
230 230
231 231 ax.firsttime = False
232 232
233 233 if self.showprofile:
234 234 ax.plot_profile= ax.ax_profile.plot(self.data['rti'][self.max_time][n], y)[0]
235 235 ax.ax_profile.set_xlim(self.zmin, self.zmax)
236 236 ax.ax_profile.set_ylim(self.ymin, self.ymax)
237 237 ax.ax_profile.set_xlabel('dB')
238 238 ax.ax_profile.grid(b=True, axis='x')
239 239 ax.plot_noise = ax.ax_profile.plot(numpy.repeat(self.data['noise'][self.max_time][n], len(y)), y,
240 240 color="k", linestyle="dashed", lw=2)[0]
241 241 [tick.set_visible(False) for tick in ax.ax_profile.get_yticklabels()]
242 242 else:
243 243 ax.plot.set_array(z[n].T.ravel())
244 244 if self.showprofile:
245 245 ax.plot_profile.set_data(self.data['rti'][self.max_time][n], y)
246 246 ax.plot_noise.set_data(numpy.repeat(self.data['noise'][self.max_time][n], len(y)), y)
247 247
248 248 ax.set_title('{} - Noise: {:.2f} dB'.format(self.titles[n], self.data['noise'][self.max_time][n]),
249 249 size=8)
250 250
251 251 class PlotRTIData(PlotData):
252 252
253 253 CODE = 'rti'
254 254 colormap = 'jro'
255 255
256 256 def setup(self):
257 257 self.ncols = 1
258 258 self.nrows = self.dataOut.nChannels
259 259 self.width = 10
260 260 self.height = 2.2*self.nrows
261 if self.nrows==1:
262 self.height += 1
261 263 self.ylabel = 'Range [Km]'
262 264 self.titles = ['Channel {}'.format(x) for x in self.dataOut.channelList]
263 265
264 266 if self.figure is None:
265 267 self.figure = plt.figure(figsize=(self.width, self.height),
266 268 edgecolor='k',
267 269 facecolor='w')
268 270 else:
269 271 self.figure.clf()
270 272 self.axes = []
271 273
272 274 for n in range(self.nrows):
273 275 ax = self.figure.add_subplot(self.nrows, self.ncols, n+1)
274 276 ax.firsttime = True
275 277 self.axes.append(ax)
276 278 self.figure.subplots_adjust(hspace=0.5)
277 279 self.figure.show()
278 280
279 281 def plot(self):
280 282
281 283 self.x = np.array(self.times)
282 284 self.y = self.dataOut.getHeiRange()
283 285 self.z = []
284 286
285 287 for ch in range(self.nrows):
286 288 self.z.append([self.data[self.CODE][t][ch] for t in self.times])
287 289
288 290 self.z = np.array(self.z)
289 291 for n, ax in enumerate(self.axes):
290 292
291 293 x, y, z = self.fill_gaps(*self.decimate())
292 294 xmin = self.min_time
293 295 xmax = xmin+self.xrange*60*60
294 296 if ax.firsttime:
295 297 self.ymin = self.ymin if self.ymin else np.nanmin(self.y)
296 298 self.ymax = self.ymax if self.ymax else np.nanmax(self.y)
297 299 self.zmin = self.zmin if self.zmin else np.nanmin(self.z)
298 300 self.zmax = self.zmax if self.zmax else np.nanmax(self.z)
299 301 plot = ax.pcolormesh(x, y, z[n].T,
300 302 vmin=self.zmin,
301 303 vmax=self.zmax,
302 304 cmap=plt.get_cmap(self.colormap)
303 305 )
304 306 divider = make_axes_locatable(ax)
305 307 cax = divider.new_horizontal(size='2%', pad=0.05)
306 308 self.figure.add_axes(cax)
307 309 plt.colorbar(plot, cax)
308 310 ax.set_ylim(self.ymin, self.ymax)
309 311 if self.xaxis == 'time':
310 312 ax.xaxis.set_major_formatter(FuncFormatter(func))
311 313 ax.xaxis.set_major_locator(LinearLocator(6))
312 314
313 315 # ax.yaxis.set_major_locator(LinearLocator(4))
314 316
315 317 ax.set_ylabel(self.ylabel)
316 318
317 319 # if self.xmin is None:
318 320 # xmin = self.min_time
319 321 # else:
320 322 # xmin = (datetime.datetime.combine(self.dataOut.datatime.date(),
321 323 # datetime.time(self.xmin, 0, 0))-d1970).total_seconds()
322 324
323 325 ax.set_xlim(xmin, xmax)
324 326 ax.firsttime = False
325 327 else:
326 328 ax.collections.remove(ax.collections[0])
327 329 ax.set_xlim(xmin, xmax)
328 330 plot = ax.pcolormesh(x, y, z[n].T,
329 331 vmin=self.zmin,
330 332 vmax=self.zmax,
331 333 cmap=plt.get_cmap(self.colormap)
332 334 )
333 335 ax.set_title('{} {}'.format(self.titles[n],
334 336 datetime.datetime.utcfromtimestamp(self.max_time).strftime('%y/%m/%d %H:%M:%S')),
335 337 size=8)
336 338
337 339
338 340 class PlotCOHData(PlotRTIData):
339 341
340 342 CODE = 'coh'
341 343
342 344 def setup(self):
343 345
344 346 self.ncols = 1
345 347 self.nrows = self.dataOut.nPairs
346 348 self.width = 10
347 349 self.height = 2.2*self.nrows
350 if self.nrows==1:
351 self.height += 1
348 352 self.ylabel = 'Range [Km]'
349 353 self.titles = ['Channels {}'.format(x) for x in self.dataOut.pairsList]
350 354
351 355 if self.figure is None:
352 356 self.figure = plt.figure(figsize=(self.width, self.height),
353 357 edgecolor='k',
354 358 facecolor='w')
355 359 else:
356 360 self.figure.clf()
361 self.axes = []
357 362
358 363 for n in range(self.nrows):
359 364 ax = self.figure.add_subplot(self.nrows, self.ncols, n+1)
360 365 ax.firsttime = True
361 366 self.axes.append(ax)
362 367
363 368 self.figure.subplots_adjust(hspace=0.5)
364 369 self.figure.show()
365 370
366 class PlotSNRData(PlotRTIData):
371 class PlotNoiseData(PlotData):
372 CODE = 'noise'
373
374 def setup(self):
375
376 self.ncols = 1
377 self.nrows = 1
378 self.width = 10
379 self.height = 3.2
380 self.ylabel = 'Intensity [dB]'
381 self.titles = ['Noise']
367 382
383 if self.figure is None:
384 self.figure = plt.figure(figsize=(self.width, self.height),
385 edgecolor='k',
386 facecolor='w')
387 else:
388 self.figure.clf()
389 self.axes = []
390
391 self.ax = self.figure.add_subplot(self.nrows, self.ncols, 1)
392 self.ax.firsttime = True
393
394 self.figure.show()
395
396 def plot(self):
397
398 x = self.times
399 xmin = self.min_time
400 xmax = xmin+self.xrange*60*60
401 if self.ax.firsttime:
402 for ch in self.dataOut.channelList:
403 y = [self.data[self.CODE][t][ch] for t in self.times]
404 self.ax.plot(x, y, lw=1, label='Ch{}'.format(ch))
405 self.ax.firsttime = False
406 self.ax.xaxis.set_major_formatter(FuncFormatter(func))
407 self.ax.xaxis.set_major_locator(LinearLocator(6))
408 self.ax.set_ylabel(self.ylabel)
409 plt.legend()
410 else:
411 for ch in self.dataOut.channelList:
412 y = [self.data[self.CODE][t][ch] for t in self.times]
413 self.ax.lines[ch].set_data(x, y)
414
415 self.ax.set_xlim(xmin, xmax)
416 self.ax.set_ylim(min(y)-5, max(y)+5)
417
418 class PlotSNRData(PlotRTIData):
368 419 CODE = 'snr'
369 420
370 421 class PlotDOPData(PlotRTIData):
371 422 CODE = 'dop'
372 423 colormap = 'jet'
373 424
374 425 class PlotPHASEData(PlotCOHData):
375
376 426 CODE = 'phase'
377 427 colormap = 'seismic'
General Comments 0
You need to be logged in to leave comments. Login now