@@ -1,801 +1,803 | |||
|
1 | 1 | |
|
2 | 2 | import os |
|
3 | 3 | import sys |
|
4 | 4 | import zmq |
|
5 | 5 | import time |
|
6 | 6 | import datetime |
|
7 | 7 | from functools import wraps |
|
8 | 8 | import numpy |
|
9 | 9 | import matplotlib |
|
10 | 10 | |
|
11 | 11 | if 'BACKEND' in os.environ: |
|
12 | 12 | matplotlib.use(os.environ['BACKEND']) |
|
13 | 13 | elif 'linux' in sys.platform: |
|
14 | 14 | matplotlib.use("TkAgg") |
|
15 | 15 | elif 'darwin' in sys.platform: |
|
16 | 16 | matplotlib.use('TkAgg') |
|
17 | 17 | else: |
|
18 | 18 | from schainpy.utils import log |
|
19 | 19 | log.warning('Using default Backend="Agg"', 'INFO') |
|
20 | 20 | matplotlib.use('Agg') |
|
21 | 21 | |
|
22 | 22 | import matplotlib.pyplot as plt |
|
23 | 23 | from matplotlib.patches import Polygon |
|
24 | 24 | from mpl_toolkits.axes_grid1 import make_axes_locatable |
|
25 | 25 | from matplotlib.ticker import FuncFormatter, LinearLocator, MultipleLocator |
|
26 | 26 | |
|
27 | 27 | from schainpy.model.data.jrodata import PlotterData |
|
28 | 28 | from schainpy.model.proc.jroproc_base import ProcessingUnit, Operation, MPDecorator |
|
29 | 29 | from schainpy.utils import log |
|
30 | 30 | |
|
31 | 31 | jet_values = matplotlib.pyplot.get_cmap('jet', 100)(numpy.arange(100))[10:90] |
|
32 | 32 | blu_values = matplotlib.pyplot.get_cmap( |
|
33 | 33 | 'seismic_r', 20)(numpy.arange(20))[10:15] |
|
34 | 34 | ncmap = matplotlib.colors.LinearSegmentedColormap.from_list( |
|
35 | 35 | 'jro', numpy.vstack((blu_values, jet_values))) |
|
36 | 36 | matplotlib.pyplot.register_cmap(cmap=ncmap) |
|
37 | 37 | |
|
38 | 38 | CMAPS = [plt.get_cmap(s) for s in ('jro', 'jet', 'viridis', |
|
39 | 39 | 'plasma', 'inferno', 'Greys', 'seismic', 'bwr', 'coolwarm')] |
|
40 | 40 | |
|
41 | 41 | EARTH_RADIUS = 6.3710e3 |
|
42 | 42 | |
|
43 | 43 | |
|
44 | 44 | def ll2xy(lat1, lon1, lat2, lon2): |
|
45 | 45 | |
|
46 | 46 | p = 0.017453292519943295 |
|
47 | 47 | a = 0.5 - numpy.cos((lat2 - lat1) * p)/2 + numpy.cos(lat1 * p) * \ |
|
48 | 48 | numpy.cos(lat2 * p) * (1 - numpy.cos((lon2 - lon1) * p)) / 2 |
|
49 | 49 | r = 12742 * numpy.arcsin(numpy.sqrt(a)) |
|
50 | 50 | theta = numpy.arctan2(numpy.sin((lon2-lon1)*p)*numpy.cos(lat2*p), numpy.cos(lat1*p) |
|
51 | 51 | * numpy.sin(lat2*p)-numpy.sin(lat1*p)*numpy.cos(lat2*p)*numpy.cos((lon2-lon1)*p)) |
|
52 | 52 | theta = -theta + numpy.pi/2 |
|
53 | 53 | return r*numpy.cos(theta), r*numpy.sin(theta) |
|
54 | 54 | |
|
55 | 55 | |
|
56 | 56 | def km2deg(km): |
|
57 | 57 | ''' |
|
58 | 58 | Convert distance in km to degrees |
|
59 | 59 | ''' |
|
60 | 60 | |
|
61 | 61 | return numpy.rad2deg(km/EARTH_RADIUS) |
|
62 | 62 | |
|
63 | 63 | |
|
64 | 64 | def figpause(interval): |
|
65 | 65 | backend = plt.rcParams['backend'] |
|
66 | 66 | if backend in matplotlib.rcsetup.interactive_bk: |
|
67 | 67 | figManager = matplotlib._pylab_helpers.Gcf.get_active() |
|
68 | 68 | if figManager is not None: |
|
69 | 69 | canvas = figManager.canvas |
|
70 | 70 | if canvas.figure.stale: |
|
71 | 71 | canvas.draw() |
|
72 | 72 | try: |
|
73 | 73 | canvas.start_event_loop(interval) |
|
74 | 74 | except: |
|
75 | 75 | pass |
|
76 | 76 | return |
|
77 | 77 | |
|
78 | 78 | |
|
79 | 79 | def popup(message): |
|
80 | 80 | ''' |
|
81 | 81 | ''' |
|
82 | 82 | |
|
83 | 83 | fig = plt.figure(figsize=(12, 8), facecolor='r') |
|
84 | 84 | text = '\n'.join([s.strip() for s in message.split(':')]) |
|
85 | 85 | fig.text(0.01, 0.5, text, ha='left', va='center', |
|
86 | 86 | size='20', weight='heavy', color='w') |
|
87 | 87 | fig.show() |
|
88 | 88 | figpause(1000) |
|
89 | 89 | |
|
90 | 90 | |
|
91 | 91 | class Throttle(object): |
|
92 | 92 | ''' |
|
93 | 93 | Decorator that prevents a function from being called more than once every |
|
94 | 94 | time period. |
|
95 | 95 | To create a function that cannot be called more than once a minute, but |
|
96 | 96 | will sleep until it can be called: |
|
97 | 97 | @Throttle(minutes=1) |
|
98 | 98 | def foo(): |
|
99 | 99 | pass |
|
100 | 100 | |
|
101 | 101 | for i in range(10): |
|
102 | 102 | foo() |
|
103 | 103 | print "This function has run %s times." % i |
|
104 | 104 | ''' |
|
105 | 105 | |
|
106 | 106 | def __init__(self, seconds=0, minutes=0, hours=0): |
|
107 | 107 | self.throttle_period = datetime.timedelta( |
|
108 | 108 | seconds=seconds, minutes=minutes, hours=hours |
|
109 | 109 | ) |
|
110 | 110 | |
|
111 | 111 | self.time_of_last_call = datetime.datetime.min |
|
112 | 112 | |
|
113 | 113 | def __call__(self, fn): |
|
114 | 114 | @wraps(fn) |
|
115 | 115 | def wrapper(*args, **kwargs): |
|
116 | 116 | coerce = kwargs.pop('coerce', None) |
|
117 | 117 | if coerce: |
|
118 | 118 | self.time_of_last_call = datetime.datetime.now() |
|
119 | 119 | return fn(*args, **kwargs) |
|
120 | 120 | else: |
|
121 | 121 | now = datetime.datetime.now() |
|
122 | 122 | time_since_last_call = now - self.time_of_last_call |
|
123 | 123 | time_left = self.throttle_period - time_since_last_call |
|
124 | 124 | |
|
125 | 125 | if time_left > datetime.timedelta(seconds=0): |
|
126 | 126 | return |
|
127 | 127 | |
|
128 | 128 | self.time_of_last_call = datetime.datetime.now() |
|
129 | 129 | return fn(*args, **kwargs) |
|
130 | 130 | |
|
131 | 131 | return wrapper |
|
132 | 132 | |
|
133 | 133 | def apply_throttle(value): |
|
134 | 134 | |
|
135 | 135 | @Throttle(seconds=value) |
|
136 | 136 | def fnThrottled(fn): |
|
137 | 137 | fn() |
|
138 | 138 | |
|
139 | 139 | return fnThrottled |
|
140 | 140 | |
|
141 | 141 | @MPDecorator |
|
142 | 142 | class Plotter(ProcessingUnit): |
|
143 | 143 | ''' |
|
144 | 144 | Proccessing unit to handle plot operations |
|
145 | 145 | ''' |
|
146 | 146 | |
|
147 | 147 | def __init__(self): |
|
148 | 148 | |
|
149 | 149 | ProcessingUnit.__init__(self) |
|
150 | 150 | |
|
151 | 151 | def setup(self, **kwargs): |
|
152 | 152 | |
|
153 | 153 | self.connections = 0 |
|
154 | 154 | self.web_address = kwargs.get('web_server', False) |
|
155 | 155 | self.realtime = kwargs.get('realtime', False) |
|
156 | 156 | self.localtime = kwargs.get('localtime', True) |
|
157 | 157 | self.buffering = kwargs.get('buffering', True) |
|
158 | 158 | self.throttle = kwargs.get('throttle', 2) |
|
159 | 159 | self.exp_code = kwargs.get('exp_code', None) |
|
160 | 160 | self.set_ready = apply_throttle(self.throttle) |
|
161 | 161 | self.dates = [] |
|
162 | 162 | self.data = PlotterData( |
|
163 | 163 | self.plots, self.throttle, self.exp_code, self.buffering) |
|
164 | 164 | self.isConfig = True |
|
165 | 165 | |
|
166 | 166 | def ready(self): |
|
167 | 167 | ''' |
|
168 | 168 | Set dataOut ready |
|
169 | 169 | ''' |
|
170 | 170 | |
|
171 | 171 | self.data.ready = True |
|
172 | 172 | self.dataOut.data_plt = self.data |
|
173 | 173 | |
|
174 | 174 | def run(self, realtime=True, localtime=True, buffering=True, |
|
175 | 175 | throttle=2, exp_code=None, web_server=None): |
|
176 | 176 | |
|
177 | 177 | if not self.isConfig: |
|
178 | 178 | self.setup(realtime=realtime, localtime=localtime, |
|
179 | 179 | buffering=buffering, throttle=throttle, exp_code=exp_code, |
|
180 | 180 | web_server=web_server) |
|
181 | 181 | |
|
182 | 182 | if self.web_address: |
|
183 | 183 | log.success( |
|
184 | 184 | 'Sending to web: {}'.format(self.web_address), |
|
185 | 185 | self.name |
|
186 | 186 | ) |
|
187 | 187 | self.context = zmq.Context() |
|
188 | 188 | self.sender_web = self.context.socket(zmq.REQ) |
|
189 | 189 | self.sender_web.connect(self.web_address) |
|
190 | 190 | self.poll = zmq.Poller() |
|
191 | 191 | self.poll.register(self.sender_web, zmq.POLLIN) |
|
192 | 192 | time.sleep(1) |
|
193 | 193 | |
|
194 | 194 | # t = Thread(target=self.event_monitor, args=(monitor,)) |
|
195 | 195 | # t.start() |
|
196 | 196 | |
|
197 | 197 | self.dataOut = self.dataIn |
|
198 | 198 | self.data.ready = False |
|
199 | 199 | |
|
200 | 200 | if self.dataOut.flagNoData: |
|
201 | 201 | coerce = True |
|
202 | 202 | else: |
|
203 | 203 | coerce = False |
|
204 | 204 | |
|
205 | 205 | if self.dataOut.type == 'Parameters': |
|
206 | 206 | tm = self.dataOut.utctimeInit |
|
207 | 207 | else: |
|
208 | 208 | tm = self.dataOut.utctime |
|
209 | 209 | if self.dataOut.useLocalTime: |
|
210 | 210 | if not self.localtime: |
|
211 | 211 | tm += time.timezone |
|
212 | 212 | dt = datetime.datetime.fromtimestamp(tm).date() |
|
213 | 213 | else: |
|
214 | 214 | if self.localtime: |
|
215 | 215 | tm -= time.timezone |
|
216 | 216 | dt = datetime.datetime.utcfromtimestamp(tm).date() |
|
217 | 217 | if dt not in self.dates: |
|
218 | 218 | if self.data: |
|
219 | 219 | self.ready() |
|
220 | 220 | self.data.setup() |
|
221 | 221 | self.dates.append(dt) |
|
222 | 222 | |
|
223 | 223 | self.data.update(self.dataOut, tm) |
|
224 | 224 | |
|
225 | 225 | if False: # TODO check when publishers ends |
|
226 | 226 | self.connections -= 1 |
|
227 | 227 | if self.connections == 0 and dt in self.dates: |
|
228 | 228 | self.data.ended = True |
|
229 | 229 | self.ready() |
|
230 | 230 | time.sleep(1) |
|
231 | 231 | else: |
|
232 | 232 | if self.realtime: |
|
233 | 233 | self.ready() |
|
234 | 234 | if self.web_address: |
|
235 | 235 | retries = 5 |
|
236 | 236 | while True: |
|
237 | 237 | self.sender_web.send(self.data.jsonify()) |
|
238 | 238 | socks = dict(self.poll.poll(5000)) |
|
239 | 239 | if socks.get(self.sender_web) == zmq.POLLIN: |
|
240 | 240 | reply = self.sender_web.recv_string() |
|
241 | 241 | if reply == 'ok': |
|
242 | 242 | log.log("Response from server ok", self.name) |
|
243 | 243 | break |
|
244 | 244 | else: |
|
245 | 245 | log.warning( |
|
246 | 246 | "Malformed reply from server: {}".format(reply), self.name) |
|
247 | 247 | |
|
248 | 248 | else: |
|
249 | 249 | log.warning( |
|
250 | 250 | "No response from server, retrying...", self.name) |
|
251 | 251 | self.sender_web.setsockopt(zmq.LINGER, 0) |
|
252 | 252 | self.sender_web.close() |
|
253 | 253 | self.poll.unregister(self.sender_web) |
|
254 | 254 | retries -= 1 |
|
255 | 255 | if retries == 0: |
|
256 | 256 | log.error( |
|
257 | 257 | "Server seems to be offline, abandoning", self.name) |
|
258 | 258 | self.sender_web = self.context.socket(zmq.REQ) |
|
259 | 259 | self.sender_web.connect(self.web_address) |
|
260 | 260 | self.poll.register(self.sender_web, zmq.POLLIN) |
|
261 | 261 | time.sleep(1) |
|
262 | 262 | break |
|
263 | 263 | self.sender_web = self.context.socket(zmq.REQ) |
|
264 | 264 | self.sender_web.connect(self.web_address) |
|
265 | 265 | self.poll.register(self.sender_web, zmq.POLLIN) |
|
266 | 266 | time.sleep(1) |
|
267 | 267 | else: |
|
268 | 268 | self.set_ready(self.ready, coerce=coerce) |
|
269 | 269 | |
|
270 | 270 | return |
|
271 | 271 | |
|
272 | 272 | def close(self): |
|
273 | 273 | pass |
|
274 | 274 | |
|
275 | 275 | |
|
276 | 276 | @MPDecorator |
|
277 | 277 | class Plot(Operation): |
|
278 | 278 | ''' |
|
279 | 279 | Base class for Schain plotting operations |
|
280 | 280 | ''' |
|
281 | 281 | |
|
282 | 282 | CODE = 'Figure' |
|
283 | 283 | colormap = 'jro' |
|
284 | 284 | bgcolor = 'white' |
|
285 | 285 | __missing = 1E30 |
|
286 | 286 | |
|
287 | 287 | __attrs__ = ['show', 'save', 'xmin', 'xmax', 'ymin', 'ymax', 'zmin', 'zmax', |
|
288 | 288 | 'zlimits', 'xlabel', 'ylabel', 'xaxis', 'cb_label', 'title', |
|
289 | 289 | 'colorbar', 'bgcolor', 'width', 'height', 'localtime', 'oneFigure', |
|
290 | 290 | 'showprofile', 'decimation', 'pause'] |
|
291 | 291 | |
|
292 | 292 | def __init__(self): |
|
293 | 293 | |
|
294 | 294 | Operation.__init__(self) |
|
295 | 295 | self.isConfig = False |
|
296 | 296 | self.isPlotConfig = False |
|
297 | 297 | |
|
298 | 298 | def __fmtTime(self, x, pos): |
|
299 | 299 | ''' |
|
300 | 300 | ''' |
|
301 | 301 | |
|
302 | 302 | return '{}'.format(self.getDateTime(x).strftime('%H:%M')) |
|
303 | 303 | |
|
304 | 304 | def __setup(self, **kwargs): |
|
305 | 305 | ''' |
|
306 | 306 | Initialize variables |
|
307 | 307 | ''' |
|
308 | 308 | |
|
309 | 309 | self.figures = [] |
|
310 | 310 | self.axes = [] |
|
311 | 311 | self.cb_axes = [] |
|
312 | 312 | self.localtime = kwargs.pop('localtime', True) |
|
313 | 313 | self.show = kwargs.get('show', True) |
|
314 | 314 | self.save = kwargs.get('save', False) |
|
315 | 315 | self.ftp = kwargs.get('ftp', False) |
|
316 | 316 | self.colormap = kwargs.get('colormap', self.colormap) |
|
317 | 317 | self.colormap_coh = kwargs.get('colormap_coh', 'jet') |
|
318 | 318 | self.colormap_phase = kwargs.get('colormap_phase', 'RdBu_r') |
|
319 | 319 | self.colormaps = kwargs.get('colormaps', None) |
|
320 | 320 | self.bgcolor = kwargs.get('bgcolor', self.bgcolor) |
|
321 | 321 | self.showprofile = kwargs.get('showprofile', False) |
|
322 | 322 | self.title = kwargs.get('wintitle', self.CODE.upper()) |
|
323 | 323 | self.cb_label = kwargs.get('cb_label', None) |
|
324 | 324 | self.cb_labels = kwargs.get('cb_labels', None) |
|
325 | 325 | self.labels = kwargs.get('labels', None) |
|
326 | 326 | self.xaxis = kwargs.get('xaxis', 'frequency') |
|
327 | 327 | self.zmin = kwargs.get('zmin', None) |
|
328 | 328 | self.zmax = kwargs.get('zmax', None) |
|
329 | 329 | self.zlimits = kwargs.get('zlimits', None) |
|
330 | 330 | self.xmin = kwargs.get('xmin', None) |
|
331 | 331 | self.xmax = kwargs.get('xmax', None) |
|
332 | 332 | self.xrange = kwargs.get('xrange', 12) |
|
333 | 333 | self.xscale = kwargs.get('xscale', None) |
|
334 | 334 | self.ymin = kwargs.get('ymin', None) |
|
335 | 335 | self.ymax = kwargs.get('ymax', None) |
|
336 | 336 | self.yscale = kwargs.get('yscale', None) |
|
337 | 337 | self.xlabel = kwargs.get('xlabel', None) |
|
338 | 338 | self.decimation = kwargs.get('decimation', None) |
|
339 | 339 | self.showSNR = kwargs.get('showSNR', False) |
|
340 | 340 | self.oneFigure = kwargs.get('oneFigure', True) |
|
341 | 341 | self.width = kwargs.get('width', None) |
|
342 | 342 | self.height = kwargs.get('height', None) |
|
343 | 343 | self.colorbar = kwargs.get('colorbar', True) |
|
344 | 344 | self.factors = kwargs.get('factors', [1, 1, 1, 1, 1, 1, 1, 1]) |
|
345 | 345 | self.channels = kwargs.get('channels', None) |
|
346 | 346 | self.titles = kwargs.get('titles', []) |
|
347 | 347 | self.polar = False |
|
348 | 348 | self.type = kwargs.get('type', 'iq') |
|
349 | 349 | self.grid = kwargs.get('grid', False) |
|
350 | 350 | self.pause = kwargs.get('pause', False) |
|
351 | 351 | self.save_labels = kwargs.get('save_labels', None) |
|
352 | 352 | self.realtime = kwargs.get('realtime', True) |
|
353 | 353 | self.buffering = kwargs.get('buffering', True) |
|
354 | 354 | self.throttle = kwargs.get('throttle', 2) |
|
355 | 355 | self.exp_code = kwargs.get('exp_code', None) |
|
356 | 356 | self.__throttle_plot = apply_throttle(self.throttle) |
|
357 | 357 | self.data = PlotterData( |
|
358 | 358 | self.CODE, self.throttle, self.exp_code, self.buffering) |
|
359 | 359 | |
|
360 | 360 | def __setup_plot(self): |
|
361 | 361 | ''' |
|
362 | 362 | Common setup for all figures, here figures and axes are created |
|
363 | 363 | ''' |
|
364 | 364 | |
|
365 | 365 | self.setup() |
|
366 | 366 | |
|
367 | 367 | self.time_label = 'LT' if self.localtime else 'UTC' |
|
368 | 368 | if self.data.localtime: |
|
369 | 369 | self.getDateTime = datetime.datetime.fromtimestamp |
|
370 | 370 | else: |
|
371 | 371 | self.getDateTime = datetime.datetime.utcfromtimestamp |
|
372 | 372 | |
|
373 | 373 | if self.width is None: |
|
374 | 374 | self.width = 8 |
|
375 | 375 | |
|
376 | 376 | self.figures = [] |
|
377 | 377 | self.axes = [] |
|
378 | 378 | self.cb_axes = [] |
|
379 | 379 | self.pf_axes = [] |
|
380 | 380 | self.cmaps = [] |
|
381 | 381 | |
|
382 | 382 | size = '15%' if self.ncols == 1 else '30%' |
|
383 | 383 | pad = '4%' if self.ncols == 1 else '8%' |
|
384 | 384 | |
|
385 | 385 | if self.oneFigure: |
|
386 | 386 | if self.height is None: |
|
387 | 387 | self.height = 1.4 * self.nrows + 1 |
|
388 | 388 | fig = plt.figure(figsize=(self.width, self.height), |
|
389 | 389 | edgecolor='k', |
|
390 | 390 | facecolor='w') |
|
391 | 391 | self.figures.append(fig) |
|
392 | 392 | for n in range(self.nplots): |
|
393 | 393 | ax = fig.add_subplot(self.nrows, self.ncols, |
|
394 | 394 | n + 1, polar=self.polar) |
|
395 | 395 | ax.tick_params(labelsize=8) |
|
396 | 396 | ax.firsttime = True |
|
397 | 397 | ax.index = 0 |
|
398 | 398 | ax.press = None |
|
399 | 399 | self.axes.append(ax) |
|
400 | 400 | if self.showprofile: |
|
401 | 401 | cax = self.__add_axes(ax, size=size, pad=pad) |
|
402 | 402 | cax.tick_params(labelsize=8) |
|
403 | 403 | self.pf_axes.append(cax) |
|
404 | 404 | else: |
|
405 | 405 | if self.height is None: |
|
406 | 406 | self.height = 3 |
|
407 | 407 | for n in range(self.nplots): |
|
408 | 408 | fig = plt.figure(figsize=(self.width, self.height), |
|
409 | 409 | edgecolor='k', |
|
410 | 410 | facecolor='w') |
|
411 | 411 | ax = fig.add_subplot(1, 1, 1, polar=self.polar) |
|
412 | 412 | ax.tick_params(labelsize=8) |
|
413 | 413 | ax.firsttime = True |
|
414 | 414 | ax.index = 0 |
|
415 | 415 | ax.press = None |
|
416 | 416 | self.figures.append(fig) |
|
417 | 417 | self.axes.append(ax) |
|
418 | 418 | if self.showprofile: |
|
419 | 419 | cax = self.__add_axes(ax, size=size, pad=pad) |
|
420 | 420 | cax.tick_params(labelsize=8) |
|
421 | 421 | self.pf_axes.append(cax) |
|
422 | 422 | |
|
423 | 423 | for n in range(self.nrows): |
|
424 | 424 | if self.colormaps is not None: |
|
425 | 425 | cmap = plt.get_cmap(self.colormaps[n]) |
|
426 | 426 | else: |
|
427 | 427 | cmap = plt.get_cmap(self.colormap) |
|
428 | 428 | cmap.set_bad(self.bgcolor, 1.) |
|
429 | 429 | self.cmaps.append(cmap) |
|
430 | 430 | |
|
431 | 431 | for fig in self.figures: |
|
432 | 432 | fig.canvas.mpl_connect('key_press_event', self.OnKeyPress) |
|
433 | 433 | fig.canvas.mpl_connect('scroll_event', self.OnBtnScroll) |
|
434 | 434 | fig.canvas.mpl_connect('button_press_event', self.onBtnPress) |
|
435 | 435 | fig.canvas.mpl_connect('motion_notify_event', self.onMotion) |
|
436 | 436 | fig.canvas.mpl_connect('button_release_event', self.onBtnRelease) |
|
437 | 437 | if self.show: |
|
438 | 438 | fig.show() |
|
439 | 439 | |
|
440 | 440 | def OnKeyPress(self, event): |
|
441 | 441 | ''' |
|
442 | 442 | Event for pressing keys (up, down) change colormap |
|
443 | 443 | ''' |
|
444 | 444 | ax = event.inaxes |
|
445 | 445 | if ax in self.axes: |
|
446 | 446 | if event.key == 'down': |
|
447 | 447 | ax.index += 1 |
|
448 | 448 | elif event.key == 'up': |
|
449 | 449 | ax.index -= 1 |
|
450 | 450 | if ax.index < 0: |
|
451 | 451 | ax.index = len(CMAPS) - 1 |
|
452 | 452 | elif ax.index == len(CMAPS): |
|
453 | 453 | ax.index = 0 |
|
454 | 454 | cmap = CMAPS[ax.index] |
|
455 | 455 | ax.cbar.set_cmap(cmap) |
|
456 | 456 | ax.cbar.draw_all() |
|
457 | 457 | ax.plt.set_cmap(cmap) |
|
458 | 458 | ax.cbar.patch.figure.canvas.draw() |
|
459 | 459 | self.colormap = cmap.name |
|
460 | 460 | |
|
461 | 461 | def OnBtnScroll(self, event): |
|
462 | 462 | ''' |
|
463 | 463 | Event for scrolling, scale figure |
|
464 | 464 | ''' |
|
465 | 465 | cb_ax = event.inaxes |
|
466 | 466 | if cb_ax in [ax.cbar.ax for ax in self.axes if ax.cbar]: |
|
467 | 467 | ax = [ax for ax in self.axes if cb_ax == ax.cbar.ax][0] |
|
468 | 468 | pt = ax.cbar.ax.bbox.get_points()[:, 1] |
|
469 | 469 | nrm = ax.cbar.norm |
|
470 | 470 | vmin, vmax, p0, p1, pS = ( |
|
471 | 471 | nrm.vmin, nrm.vmax, pt[0], pt[1], event.y) |
|
472 | 472 | scale = 2 if event.step == 1 else 0.5 |
|
473 | 473 | point = vmin + (vmax - vmin) / (p1 - p0) * (pS - p0) |
|
474 | 474 | ax.cbar.norm.vmin = point - scale * (point - vmin) |
|
475 | 475 | ax.cbar.norm.vmax = point - scale * (point - vmax) |
|
476 | 476 | ax.plt.set_norm(ax.cbar.norm) |
|
477 | 477 | ax.cbar.draw_all() |
|
478 | 478 | ax.cbar.patch.figure.canvas.draw() |
|
479 | 479 | |
|
480 | 480 | def onBtnPress(self, event): |
|
481 | 481 | ''' |
|
482 | 482 | Event for mouse button press |
|
483 | 483 | ''' |
|
484 | 484 | cb_ax = event.inaxes |
|
485 | 485 | if cb_ax is None: |
|
486 | 486 | return |
|
487 | 487 | |
|
488 | 488 | if cb_ax in [ax.cbar.ax for ax in self.axes if ax.cbar]: |
|
489 | 489 | cb_ax.press = event.x, event.y |
|
490 | 490 | else: |
|
491 | 491 | cb_ax.press = None |
|
492 | 492 | |
|
493 | 493 | def onMotion(self, event): |
|
494 | 494 | ''' |
|
495 | 495 | Event for move inside colorbar |
|
496 | 496 | ''' |
|
497 | 497 | cb_ax = event.inaxes |
|
498 | 498 | if cb_ax is None: |
|
499 | 499 | return |
|
500 | 500 | if cb_ax not in [ax.cbar.ax for ax in self.axes if ax.cbar]: |
|
501 | 501 | return |
|
502 | 502 | if cb_ax.press is None: |
|
503 | 503 | return |
|
504 | 504 | |
|
505 | 505 | ax = [ax for ax in self.axes if cb_ax == ax.cbar.ax][0] |
|
506 | 506 | xprev, yprev = cb_ax.press |
|
507 | 507 | dx = event.x - xprev |
|
508 | 508 | dy = event.y - yprev |
|
509 | 509 | cb_ax.press = event.x, event.y |
|
510 | 510 | scale = ax.cbar.norm.vmax - ax.cbar.norm.vmin |
|
511 | 511 | perc = 0.03 |
|
512 | 512 | |
|
513 | 513 | if event.button == 1: |
|
514 | 514 | ax.cbar.norm.vmin -= (perc * scale) * numpy.sign(dy) |
|
515 | 515 | ax.cbar.norm.vmax -= (perc * scale) * numpy.sign(dy) |
|
516 | 516 | elif event.button == 3: |
|
517 | 517 | ax.cbar.norm.vmin -= (perc * scale) * numpy.sign(dy) |
|
518 | 518 | ax.cbar.norm.vmax += (perc * scale) * numpy.sign(dy) |
|
519 | 519 | |
|
520 | 520 | ax.cbar.draw_all() |
|
521 | 521 | ax.plt.set_norm(ax.cbar.norm) |
|
522 | 522 | ax.cbar.patch.figure.canvas.draw() |
|
523 | 523 | |
|
524 | 524 | def onBtnRelease(self, event): |
|
525 | 525 | ''' |
|
526 | 526 | Event for mouse button release |
|
527 | 527 | ''' |
|
528 | 528 | cb_ax = event.inaxes |
|
529 | 529 | if cb_ax is not None: |
|
530 | 530 | cb_ax.press = None |
|
531 | 531 | |
|
532 | 532 | def __add_axes(self, ax, size='30%', pad='8%'): |
|
533 | 533 | ''' |
|
534 | 534 | Add new axes to the given figure |
|
535 | 535 | ''' |
|
536 | 536 | divider = make_axes_locatable(ax) |
|
537 | 537 | nax = divider.new_horizontal(size=size, pad=pad) |
|
538 | 538 | ax.figure.add_axes(nax) |
|
539 | 539 | return nax |
|
540 | 540 | |
|
541 | 541 | def setup(self): |
|
542 | 542 | ''' |
|
543 | 543 | This method should be implemented in the child class, the following |
|
544 | 544 | attributes should be set: |
|
545 | 545 | |
|
546 | 546 | self.nrows: number of rows |
|
547 | 547 | self.ncols: number of cols |
|
548 | 548 | self.nplots: number of plots (channels or pairs) |
|
549 | 549 | self.ylabel: label for Y axes |
|
550 | 550 | self.titles: list of axes title |
|
551 | 551 | |
|
552 | 552 | ''' |
|
553 | 553 | raise NotImplementedError |
|
554 | 554 | |
|
555 | 555 | def fill_gaps(self, x_buffer, y_buffer, z_buffer): |
|
556 | 556 | ''' |
|
557 | 557 | Create a masked array for missing data |
|
558 | 558 | ''' |
|
559 | 559 | if x_buffer.shape[0] < 2: |
|
560 | 560 | return x_buffer, y_buffer, z_buffer |
|
561 | 561 | |
|
562 | 562 | deltas = x_buffer[1:] - x_buffer[0:-1] |
|
563 | 563 | x_median = numpy.median(deltas) |
|
564 | 564 | |
|
565 | 565 | index = numpy.where(deltas > 5 * x_median) |
|
566 | 566 | |
|
567 | 567 | if len(index[0]) != 0: |
|
568 | 568 | z_buffer[::, index[0], ::] = self.__missing |
|
569 | 569 | z_buffer = numpy.ma.masked_inside(z_buffer, |
|
570 | 570 | 0.99 * self.__missing, |
|
571 | 571 | 1.01 * self.__missing) |
|
572 | 572 | |
|
573 | 573 | return x_buffer, y_buffer, z_buffer |
|
574 | 574 | |
|
575 | 575 | def decimate(self): |
|
576 | 576 | |
|
577 | 577 | # dx = int(len(self.x)/self.__MAXNUMX) + 1 |
|
578 | 578 | dy = int(len(self.y) / self.decimation) + 1 |
|
579 | 579 | |
|
580 | 580 | # x = self.x[::dx] |
|
581 | 581 | x = self.x |
|
582 | 582 | y = self.y[::dy] |
|
583 | 583 | z = self.z[::, ::, ::dy] |
|
584 | 584 | |
|
585 | 585 | return x, y, z |
|
586 | 586 | |
|
587 | 587 | def format(self): |
|
588 | 588 | ''' |
|
589 | 589 | Set min and max values, labels, ticks and titles |
|
590 | 590 | ''' |
|
591 | 591 | |
|
592 | 592 | if self.xmin is None: |
|
593 | 593 | xmin = self.data.min_time |
|
594 | 594 | else: |
|
595 | 595 | if self.xaxis is 'time': |
|
596 | 596 | dt = self.getDateTime(self.data.min_time) |
|
597 | 597 | xmin = (dt.replace(hour=int(self.xmin), minute=0, second=0) - |
|
598 | 598 | datetime.datetime(1970, 1, 1)).total_seconds() |
|
599 | 599 | if self.data.localtime: |
|
600 | 600 | xmin += time.timezone |
|
601 | 601 | else: |
|
602 | 602 | xmin = self.xmin |
|
603 | 603 | |
|
604 | 604 | if self.xmax is None: |
|
605 | 605 | xmax = xmin + self.xrange * 60 * 60 |
|
606 | 606 | else: |
|
607 | 607 | if self.xaxis is 'time': |
|
608 | 608 | dt = self.getDateTime(self.data.max_time) |
|
609 | 609 | xmax = (dt.replace(hour=int(self.xmax), minute=59, second=59) - |
|
610 | 610 | datetime.datetime(1970, 1, 1) + datetime.timedelta(seconds=1)).total_seconds() |
|
611 | 611 | if self.data.localtime: |
|
612 | 612 | xmax += time.timezone |
|
613 | 613 | else: |
|
614 | 614 | xmax = self.xmax |
|
615 | 615 | |
|
616 | 616 | ymin = self.ymin if self.ymin else numpy.nanmin(self.y) |
|
617 | 617 | ymax = self.ymax if self.ymax else numpy.nanmax(self.y) |
|
618 | 618 | Y = numpy.array([1, 2, 5, 10, 20, 50, 100, 200, 500, 1000, 2000, 5000, 10000, 20000, 50000]) |
|
619 | 619 | #i = 1 if numpy.where( |
|
620 | 620 | # abs(ymax-ymin) <= Y)[0][0] < 0 else numpy.where(abs(ymax-ymin) <= Y)[0][0] |
|
621 | 621 | #ystep = Y[i] / 10. |
|
622 | ystep = round(ymax,-1)//5 | |
|
622 | dig = int(numpy.log10(ymax)) | |
|
623 | ystep = ((ymax + (10**(dig)))//10**(dig))*(10**(dig)) | |
|
624 | ystep = ystep//10 | |
|
623 | 625 | if self.xaxis is not 'time': |
|
624 | 626 | X = numpy.array([0.02, 0.05, 0.1, 0.2, 0.5, 1, 2, 5, 10, 20, 50, 100, |
|
625 | 627 | 200, 500, 1000, 2000, 5000, 10000, 20000, 50000])/2. |
|
626 | 628 | |
|
627 | 629 | i = 1 if numpy.where( |
|
628 | 630 | abs(xmax-xmin) <= X)[0][0] < 0 else numpy.where(abs(xmax-xmin) <= X)[0][0] |
|
629 | 631 | xstep = X[i] / 5. |
|
630 | 632 | |
|
631 | 633 | for n, ax in enumerate(self.axes): |
|
632 | 634 | if ax.firsttime: |
|
633 | 635 | ax.set_facecolor(self.bgcolor) |
|
634 | 636 | ax.yaxis.set_major_locator(MultipleLocator(ystep)) |
|
635 | 637 | if self.xscale: |
|
636 | 638 | ax.xaxis.set_major_formatter(FuncFormatter( |
|
637 | 639 | lambda x, pos: '{0:g}'.format(x*self.xscale))) |
|
638 | 640 | if self.xscale: |
|
639 | 641 | ax.yaxis.set_major_formatter(FuncFormatter( |
|
640 | 642 | lambda x, pos: '{0:g}'.format(x*self.yscale))) |
|
641 | 643 | if self.xaxis is 'time': |
|
642 | 644 | ax.xaxis.set_major_formatter(FuncFormatter(self.__fmtTime)) |
|
643 | 645 | ax.xaxis.set_major_locator(LinearLocator(9)) |
|
644 | 646 | else: |
|
645 | 647 | ax.xaxis.set_major_locator(MultipleLocator(xstep)) |
|
646 | 648 | if self.xlabel is not None: |
|
647 | 649 | ax.set_xlabel(self.xlabel) |
|
648 | 650 | ax.set_ylabel(self.ylabel) |
|
649 | 651 | ax.firsttime = False |
|
650 | 652 | if self.showprofile: |
|
651 | 653 | self.pf_axes[n].set_ylim(ymin, ymax) |
|
652 | 654 | self.pf_axes[n].set_xlim(self.zmin, self.zmax) |
|
653 | 655 | self.pf_axes[n].set_xlabel('dB') |
|
654 | 656 | self.pf_axes[n].grid(b=True, axis='x') |
|
655 | 657 | [tick.set_visible(False) |
|
656 | 658 | for tick in self.pf_axes[n].get_yticklabels()] |
|
657 | 659 | if self.colorbar: |
|
658 | 660 | ax.cbar = plt.colorbar( |
|
659 | 661 | ax.plt, ax=ax, fraction=0.05, pad=0.02, aspect=10) |
|
660 | 662 | ax.cbar.ax.tick_params(labelsize=8) |
|
661 | 663 | ax.cbar.ax.press = None |
|
662 | 664 | if self.cb_label: |
|
663 | 665 | ax.cbar.set_label(self.cb_label, size=8) |
|
664 | 666 | elif self.cb_labels: |
|
665 | 667 | ax.cbar.set_label(self.cb_labels[n], size=8) |
|
666 | 668 | else: |
|
667 | 669 | ax.cbar = None |
|
668 | 670 | if self.grid: |
|
669 | 671 | ax.grid(True) |
|
670 | 672 | |
|
671 | 673 | if not self.polar: |
|
672 | 674 | ax.set_xlim(xmin, xmax) |
|
673 | 675 | ax.set_ylim(ymin, ymax) |
|
674 | 676 | ax.set_title('{} {} {}'.format( |
|
675 | 677 | self.titles[n], |
|
676 | 678 | self.getDateTime(self.data.max_time).strftime( |
|
677 | 679 | '%H:%M:%S'), |
|
678 | 680 | self.time_label), |
|
679 | 681 | size=8) |
|
680 | 682 | else: |
|
681 | 683 | ax.set_title('{}'.format(self.titles[n]), size=8) |
|
682 | 684 | ax.set_ylim(0, 90) |
|
683 | 685 | ax.set_yticks(numpy.arange(0, 90, 20)) |
|
684 | 686 | ax.yaxis.labelpad = 40 |
|
685 | 687 | |
|
686 | 688 | def clear_figures(self): |
|
687 | 689 | ''' |
|
688 | 690 | Reset axes for redraw plots |
|
689 | 691 | ''' |
|
690 | 692 | |
|
691 | 693 | for ax in self.axes: |
|
692 | 694 | ax.clear() |
|
693 | 695 | ax.firsttime = True |
|
694 | 696 | if ax.cbar: |
|
695 | 697 | ax.cbar.remove() |
|
696 | 698 | |
|
697 | 699 | def __plot(self): |
|
698 | 700 | ''' |
|
699 | 701 | Main function to plot, format and save figures |
|
700 | 702 | ''' |
|
701 | 703 | |
|
702 | 704 | #try: |
|
703 | 705 | self.plot() |
|
704 | 706 | self.format() |
|
705 | 707 | #except Exception as e: |
|
706 | 708 | # log.warning('{} Plot could not be updated... check data'.format( |
|
707 | 709 | # self.CODE), self.name) |
|
708 | 710 | # log.error(str(e), '') |
|
709 | 711 | # return |
|
710 | 712 | |
|
711 | 713 | for n, fig in enumerate(self.figures): |
|
712 | 714 | if self.nrows == 0 or self.nplots == 0: |
|
713 | 715 | log.warning('No data', self.name) |
|
714 | 716 | fig.text(0.5, 0.5, 'No Data', fontsize='large', ha='center') |
|
715 | 717 | fig.canvas.manager.set_window_title(self.CODE) |
|
716 | 718 | continue |
|
717 | 719 | |
|
718 | 720 | fig.tight_layout() |
|
719 | 721 | fig.canvas.manager.set_window_title('{} - {}'.format(self.title, |
|
720 | 722 | self.getDateTime(self.data.max_time).strftime('%Y/%m/%d'))) |
|
721 | 723 | fig.canvas.draw() |
|
722 | 724 | |
|
723 | 725 | if self.save: |
|
724 | 726 | |
|
725 | 727 | if self.save_labels: |
|
726 | 728 | labels = self.save_labels |
|
727 | 729 | else: |
|
728 | 730 | labels = list(range(self.nrows)) |
|
729 | 731 | |
|
730 | 732 | if self.oneFigure: |
|
731 | 733 | label = '' |
|
732 | 734 | else: |
|
733 | 735 | label = '-{}'.format(labels[n]) |
|
734 | 736 | figname = os.path.join( |
|
735 | 737 | self.save, |
|
736 | 738 | self.CODE, |
|
737 | 739 | '{}{}_{}.png'.format( |
|
738 | 740 | self.CODE, |
|
739 | 741 | label, |
|
740 | 742 | self.getDateTime(self.data.max_time).strftime( |
|
741 | 743 | '%Y%m%d_%H%M%S'), |
|
742 | 744 | ) |
|
743 | 745 | ) |
|
744 | 746 | log.log('Saving figure: {}'.format(figname), self.name) |
|
745 | 747 | if not os.path.isdir(os.path.dirname(figname)): |
|
746 | 748 | os.makedirs(os.path.dirname(figname)) |
|
747 | 749 | fig.savefig(figname) |
|
748 | 750 | |
|
749 | 751 | def plot(self): |
|
750 | 752 | ''' |
|
751 | 753 | Must be defined in the child class |
|
752 | 754 | ''' |
|
753 | 755 | raise NotImplementedError |
|
754 | 756 | |
|
755 | 757 | def run(self, dataOut, **kwargs): |
|
756 | 758 | |
|
757 | 759 | if dataOut.error: |
|
758 | 760 | coerce = True |
|
759 | 761 | else: |
|
760 | 762 | coerce = False |
|
761 | 763 | |
|
762 | 764 | if self.isConfig is False: |
|
763 | 765 | self.__setup(**kwargs) |
|
764 | 766 | self.data.setup() |
|
765 | 767 | self.isConfig = True |
|
766 | 768 | |
|
767 | 769 | if dataOut.type == 'Parameters': |
|
768 | 770 | tm = dataOut.utctimeInit |
|
769 | 771 | else: |
|
770 | 772 | tm = dataOut.utctime |
|
771 | 773 | |
|
772 | 774 | if dataOut.useLocalTime: |
|
773 | 775 | if not self.localtime: |
|
774 | 776 | tm += time.timezone |
|
775 | 777 | else: |
|
776 | 778 | if self.localtime: |
|
777 | 779 | tm -= time.timezone |
|
778 | 780 | |
|
779 | 781 | if self.data and (tm - self.data.min_time) >= self.xrange*60*60: |
|
780 | 782 | self.__plot() |
|
781 | 783 | self.data.setup() |
|
782 | 784 | self.clear_figures() |
|
783 | 785 | |
|
784 | 786 | self.data.update(dataOut, tm) |
|
785 | 787 | |
|
786 | 788 | if self.isPlotConfig is False: |
|
787 | 789 | self.__setup_plot() |
|
788 | 790 | self.isPlotConfig = True |
|
789 | 791 | |
|
790 | 792 | if self.realtime: |
|
791 | 793 | self.__plot() |
|
792 | 794 | else: |
|
793 | 795 | self.__throttle_plot(self.__plot, coerce=coerce) |
|
794 | 796 | |
|
795 | 797 | figpause(0.001) |
|
796 | 798 | |
|
797 | 799 | def close(self): |
|
798 | 800 | |
|
799 | 801 | if self.data and self.pause: |
|
800 | 802 | figpause(10) |
|
801 | 803 |
General Comments 0
You need to be logged in to leave comments.
Login now