##// END OF EJS Templates
Miguel Valdez -
r814:a59f12dc9ae4
parent child
Show More
@@ -1,659 +1,661
1 1 import os
2 2 import numpy
3 3 import time, datetime
4 4 import mpldriver
5 5
6 6 from schainpy.model.proc.jroproc_base import Operation
7 7
8 8 def isTimeInHourRange(datatime, xmin, xmax):
9 9
10 10 if xmin == None or xmax == None:
11 11 return 1
12 12 hour = datatime.hour + datatime.minute/60.0
13 13
14 14 if xmin < (xmax % 24):
15 15
16 16 if hour >= xmin and hour <= xmax:
17 17 return 1
18 18 else:
19 19 return 0
20 20
21 21 else:
22 22
23 23 if hour >= xmin or hour <= (xmax % 24):
24 24 return 1
25 25 else:
26 26 return 0
27 27
28 28 return 0
29 29
30 30 def isRealtime(utcdatatime):
31 31
32 32 utcnow = time.mktime(time.localtime())
33 33 delta = abs(utcnow - utcdatatime) # abs
34 34 if delta >= 30.:
35 35 return False
36 36 return True
37 37
38 38 class Figure(Operation):
39 39
40 40 __driver = mpldriver
41 41 fig = None
42 42
43 43 id = None
44 44 wintitle = None
45 45 width = None
46 46 height = None
47 47 nplots = None
48 48 timerange = None
49 49
50 50 axesObjList = []
51 51
52 52 WIDTH = 300
53 53 HEIGHT = 200
54 54 PREFIX = 'fig'
55 55
56 56 xmin = None
57 57 xmax = None
58 58
59 59 counter_imagwr = 0
60 60
61 61 figfile = None
62 62
63 63 created = False
64 64
65 65 def __init__(self):
66 66
67 67 raise NotImplementedError
68 68
69 69 def __del__(self):
70 70
71 71 self.__driver.closeFigure()
72 72
73 73 def getFilename(self, name, ext='.png'):
74 74
75 75 path = '%s%03d' %(self.PREFIX, self.id)
76 76 filename = '%s_%s%s' %(self.PREFIX, name, ext)
77 77 return os.path.join(path, filename)
78 78
79 79 def getAxesObjList(self):
80 80
81 81 return self.axesObjList
82 82
83 83 def getSubplots(self):
84 84
85 85 raise NotImplementedError
86 86
87 87 def getScreenDim(self, widthplot, heightplot):
88 88
89 89 nrow, ncol = self.getSubplots()
90 90
91 91 widthscreen = widthplot*ncol
92 92 heightscreen = heightplot*nrow
93 93
94 94 return widthscreen, heightscreen
95 95
96 96 def getTimeLim(self, x, xmin=None, xmax=None, timerange=None):
97 97
98 98 # if self.xmin != None and self.xmax != None:
99 99 # if timerange == None:
100 100 # timerange = self.xmax - self.xmin
101 101 # xmin = self.xmin + timerange
102 102 # xmax = self.xmax + timerange
103 103 #
104 104 # return xmin, xmax
105 105
106 106 if timerange == None and (xmin==None or xmax==None):
107 107 timerange = 14400 #seconds
108 108
109 109 if timerange != None:
110 110 txmin = x[0] #- x[0] % min(timerange/10, 10*60)
111 111 else:
112 112 txmin = x[0] #- x[0] % 10*60
113 113
114 114 thisdatetime = datetime.datetime.utcfromtimestamp(txmin)
115 115 thisdate = datetime.datetime.combine(thisdatetime.date(), datetime.time(0,0,0))
116 116
117 117 if timerange != None:
118 118 xmin = (thisdatetime - thisdate).seconds/(60*60.)
119 119 xmax = xmin + timerange/(60*60.)
120 120
121 mindt = thisdate + datetime.timedelta(hours=xmin) - datetime.timedelta(seconds=time.timezone)
122 xmin_sec = time.mktime(mindt.timetuple())
121 d1970 = datetime.datetime(1970,1,1)
123 122
124 maxdt = thisdate + datetime.timedelta(hours=xmax) - datetime.timedelta(seconds=time.timezone)
125 xmax_sec = time.mktime(maxdt.timetuple())
123 mindt = thisdate + datetime.timedelta(hours=xmin) #- datetime.timedelta(seconds=time.timezone)
124 xmin_sec = (mindt - d1970).total_seconds() #time.mktime(mindt.timetuple()) - time.timezone
125
126 maxdt = thisdate + datetime.timedelta(hours=xmax) #- datetime.timedelta(seconds=time.timezone)
127 xmax_sec = (maxdt - d1970).total_seconds() #time.mktime(maxdt.timetuple()) - time.timezone
126 128
127 129 return xmin_sec, xmax_sec
128 130
129 131 def init(self, id, nplots, wintitle):
130 132
131 133 raise NotImplementedError, "This method has been replaced with createFigure"
132 134
133 135 def createFigure(self, id, wintitle, widthplot=None, heightplot=None, show=True):
134 136
135 137 """
136 138 Crea la figura de acuerdo al driver y parametros seleccionados seleccionados.
137 139 Las dimensiones de la pantalla es calculada a partir de los atributos self.WIDTH
138 140 y self.HEIGHT y el numero de subplots (nrow, ncol)
139 141
140 142 Input:
141 143 id : Los parametros necesarios son
142 144 wintitle :
143 145
144 146 """
145 147
146 148 if widthplot == None:
147 149 widthplot = self.WIDTH
148 150
149 151 if heightplot == None:
150 152 heightplot = self.HEIGHT
151 153
152 154 self.id = id
153 155
154 156 self.wintitle = wintitle
155 157
156 158 self.widthscreen, self.heightscreen = self.getScreenDim(widthplot, heightplot)
157 159
158 160 # if self.created:
159 161 # self.__driver.closeFigure(self.fig)
160 162
161 163 if not self.created:
162 164 self.fig = self.__driver.createFigure(id=self.id,
163 165 wintitle=self.wintitle,
164 166 width=self.widthscreen,
165 167 height=self.heightscreen,
166 168 show=show)
167 169 else:
168 170 self.__driver.clearFigure(self.fig)
169 171
170 172 self.axesObjList = []
171 173 self.counter_imagwr = 0
172 174
173 175 self.created = True
174 176
175 177 def setDriver(self, driver=mpldriver):
176 178
177 179 self.__driver = driver
178 180
179 181 def setTitle(self, title):
180 182
181 183 self.__driver.setTitle(self.fig, title)
182 184
183 185 def setWinTitle(self, title):
184 186
185 187 self.__driver.setWinTitle(self.fig, title=title)
186 188
187 189 def setTextFromAxes(self, text):
188 190
189 191 raise NotImplementedError, "This method has been replaced with Axes.setText"
190 192
191 193 def makeAxes(self, nrow, ncol, xpos, ypos, colspan, rowspan):
192 194
193 195 raise NotImplementedError, "This method has been replaced with Axes.addAxes"
194 196
195 197 def addAxes(self, *args):
196 198 """
197 199
198 200 Input:
199 201 *args : Los parametros necesarios son
200 202 nrow, ncol, xpos, ypos, colspan, rowspan
201 203 """
202 204
203 205 axesObj = Axes(self.fig, *args)
204 206 self.axesObjList.append(axesObj)
205 207
206 208 def saveFigure(self, figpath, figfile, *args):
207 209
208 210 filename = os.path.join(figpath, figfile)
209 211
210 212 fullpath = os.path.split(filename)[0]
211 213
212 214 if not os.path.exists(fullpath):
213 215 subpath = os.path.split(fullpath)[0]
214 216
215 217 if not os.path.exists(subpath):
216 218 os.mkdir(subpath)
217 219
218 220 os.mkdir(fullpath)
219 221
220 222 self.__driver.saveFigure(self.fig, filename, *args)
221 223
222 224 def save(self, figpath, figfile=None, save=True, ftp=False, wr_period=1, thisDatetime=None, update_figfile=True):
223 225
224 226 self.counter_imagwr += 1
225 227 if self.counter_imagwr < wr_period:
226 228 return
227 229
228 230 self.counter_imagwr = 0
229 231
230 232 if save:
231 233
232 234 if not figfile:
233 235
234 236 if not thisDatetime:
235 237 raise ValueError, "Saving figure: figfile or thisDatetime should be defined"
236 238 return
237 239
238 240 str_datetime = thisDatetime.strftime("%Y%m%d_%H%M%S")
239 241 figfile = self.getFilename(name = str_datetime)
240 242
241 243 if self.figfile == None:
242 244 self.figfile = figfile
243 245
244 246 if update_figfile:
245 247 self.figfile = figfile
246 248
247 249 # store png plot to local folder
248 250 self.saveFigure(figpath, self.figfile)
249 251
250 252
251 253 if not ftp:
252 254 return
253 255
254 256 if not thisDatetime:
255 257 return
256 258
257 259 # store png plot to FTP server according to RT-Web format
258 260 ftp_filename = self.getNameToFtp(thisDatetime, self.FTP_WEI, self.EXP_CODE, self.SUB_EXP_CODE, self.PLOT_CODE, self.PLOT_POS)
259 261 # ftp_filename = os.path.join(figpath, name)
260 262 self.saveFigure(figpath, ftp_filename)
261 263
262 264 def getNameToFtp(self, thisDatetime, FTP_WEI, EXP_CODE, SUB_EXP_CODE, PLOT_CODE, PLOT_POS):
263 265 YEAR_STR = '%4.4d'%thisDatetime.timetuple().tm_year
264 266 DOY_STR = '%3.3d'%thisDatetime.timetuple().tm_yday
265 267 FTP_WEI = '%2.2d'%FTP_WEI
266 268 EXP_CODE = '%3.3d'%EXP_CODE
267 269 SUB_EXP_CODE = '%2.2d'%SUB_EXP_CODE
268 270 PLOT_CODE = '%2.2d'%PLOT_CODE
269 271 PLOT_POS = '%2.2d'%PLOT_POS
270 272 name = YEAR_STR + DOY_STR + FTP_WEI + EXP_CODE + SUB_EXP_CODE + PLOT_CODE + PLOT_POS
271 273 return name
272 274
273 275 def draw(self):
274 276
275 277 self.__driver.draw(self.fig)
276 278
277 279 def run(self):
278 280
279 281 raise NotImplementedError
280 282
281 283 def close(self, show=False):
282 284
283 285 self.__driver.closeFigure(show=show, fig=self.fig)
284 286
285 287 axesList = property(getAxesObjList)
286 288
287 289
288 290 class Axes:
289 291
290 292 __driver = mpldriver
291 293 fig = None
292 294 ax = None
293 295 plot = None
294 296 __missing = 1E30
295 297 __firsttime = None
296 298
297 299 __showprofile = False
298 300
299 301 xmin = None
300 302 xmax = None
301 303 ymin = None
302 304 ymax = None
303 305 zmin = None
304 306 zmax = None
305 307
306 308 x_buffer = None
307 309 z_buffer = None
308 310
309 311 decimationx = None
310 312 decimationy = None
311 313
312 314 __MAXNUMX = 200
313 315 __MAXNUMY = 400
314 316
315 317 __MAXNUMTIME = 500
316 318
317 319 def __init__(self, *args):
318 320
319 321 """
320 322
321 323 Input:
322 324 *args : Los parametros necesarios son
323 325 fig, nrow, ncol, xpos, ypos, colspan, rowspan
324 326 """
325 327
326 328 ax = self.__driver.createAxes(*args)
327 329 self.fig = args[0]
328 330 self.ax = ax
329 331 self.plot = None
330 332
331 333 self.__firsttime = True
332 334 self.idlineList = []
333 335
334 336 self.x_buffer = numpy.array([])
335 337 self.z_buffer = numpy.array([])
336 338
337 339 def setText(self, text):
338 340
339 341 self.__driver.setAxesText(self.ax, text)
340 342
341 343 def setXAxisAsTime(self):
342 344 pass
343 345
344 346 def pline(self, x, y,
345 347 xmin=None, xmax=None,
346 348 ymin=None, ymax=None,
347 349 xlabel='', ylabel='',
348 350 title='',
349 351 **kwargs):
350 352
351 353 """
352 354
353 355 Input:
354 356 x :
355 357 y :
356 358 xmin :
357 359 xmax :
358 360 ymin :
359 361 ymax :
360 362 xlabel :
361 363 ylabel :
362 364 title :
363 365 **kwargs : Los parametros aceptados son
364 366
365 367 ticksize
366 368 ytick_visible
367 369 """
368 370
369 371 if self.__firsttime:
370 372
371 373 if xmin == None: xmin = numpy.nanmin(x)
372 374 if xmax == None: xmax = numpy.nanmax(x)
373 375 if ymin == None: ymin = numpy.nanmin(y)
374 376 if ymax == None: ymax = numpy.nanmax(y)
375 377
376 378 self.plot = self.__driver.createPline(self.ax, x, y,
377 379 xmin, xmax,
378 380 ymin, ymax,
379 381 xlabel=xlabel,
380 382 ylabel=ylabel,
381 383 title=title,
382 384 **kwargs)
383 385
384 386 self.idlineList.append(0)
385 387 self.__firsttime = False
386 388 return
387 389
388 390 self.__driver.pline(self.plot, x, y, xlabel=xlabel,
389 391 ylabel=ylabel,
390 392 title=title)
391 393
392 394 # self.__driver.pause()
393 395
394 396 def addpline(self, x, y, idline, **kwargs):
395 397 lines = self.ax.lines
396 398
397 399 if idline in self.idlineList:
398 400 self.__driver.set_linedata(self.ax, x, y, idline)
399 401
400 402 if idline not in(self.idlineList):
401 403 self.__driver.addpline(self.ax, x, y, **kwargs)
402 404 self.idlineList.append(idline)
403 405
404 406 return
405 407
406 408 def pmultiline(self, x, y,
407 409 xmin=None, xmax=None,
408 410 ymin=None, ymax=None,
409 411 xlabel='', ylabel='',
410 412 title='',
411 413 **kwargs):
412 414
413 415 if self.__firsttime:
414 416
415 417 if xmin == None: xmin = numpy.nanmin(x)
416 418 if xmax == None: xmax = numpy.nanmax(x)
417 419 if ymin == None: ymin = numpy.nanmin(y)
418 420 if ymax == None: ymax = numpy.nanmax(y)
419 421
420 422 self.plot = self.__driver.createPmultiline(self.ax, x, y,
421 423 xmin, xmax,
422 424 ymin, ymax,
423 425 xlabel=xlabel,
424 426 ylabel=ylabel,
425 427 title=title,
426 428 **kwargs)
427 429 self.__firsttime = False
428 430 return
429 431
430 432 self.__driver.pmultiline(self.plot, x, y, xlabel=xlabel,
431 433 ylabel=ylabel,
432 434 title=title)
433 435
434 436 # self.__driver.pause()
435 437
436 438 def pmultilineyaxis(self, x, y,
437 439 xmin=None, xmax=None,
438 440 ymin=None, ymax=None,
439 441 xlabel='', ylabel='',
440 442 title='',
441 443 **kwargs):
442 444
443 445 if self.__firsttime:
444 446
445 447 if xmin == None: xmin = numpy.nanmin(x)
446 448 if xmax == None: xmax = numpy.nanmax(x)
447 449 if ymin == None: ymin = numpy.nanmin(y)
448 450 if ymax == None: ymax = numpy.nanmax(y)
449 451
450 452 self.plot = self.__driver.createPmultilineYAxis(self.ax, x, y,
451 453 xmin, xmax,
452 454 ymin, ymax,
453 455 xlabel=xlabel,
454 456 ylabel=ylabel,
455 457 title=title,
456 458 **kwargs)
457 459 if self.xmin == None: self.xmin = xmin
458 460 if self.xmax == None: self.xmax = xmax
459 461 if self.ymin == None: self.ymin = ymin
460 462 if self.ymax == None: self.ymax = ymax
461 463
462 464 self.__firsttime = False
463 465 return
464 466
465 467 self.__driver.pmultilineyaxis(self.plot, x, y, xlabel=xlabel,
466 468 ylabel=ylabel,
467 469 title=title)
468 470
469 471 # self.__driver.pause()
470 472
471 473 def pcolor(self, x, y, z,
472 474 xmin=None, xmax=None,
473 475 ymin=None, ymax=None,
474 476 zmin=None, zmax=None,
475 477 xlabel='', ylabel='',
476 478 title='', colormap='jet',
477 479 **kwargs):
478 480
479 481 """
480 482 Input:
481 483 x :
482 484 y :
483 485 x :
484 486 xmin :
485 487 xmax :
486 488 ymin :
487 489 ymax :
488 490 zmin :
489 491 zmax :
490 492 xlabel :
491 493 ylabel :
492 494 title :
493 495 **kwargs : Los parametros aceptados son
494 496 ticksize=9,
495 497 cblabel=''
496 498 """
497 499
498 500 #Decimating data
499 501 xlen = len(x)
500 502 ylen = len(y)
501 503
502 504 decimationx = numpy.floor(xlen/self.__MAXNUMX) + 1
503 505 decimationy = numpy.floor(ylen/self.__MAXNUMY) + 1
504 506
505 507 x_buffer = x[::decimationx]
506 508 y_buffer = y[::decimationy]
507 509 z_buffer = z[::decimationx, ::decimationy]
508 510 #===================================================
509 511
510 512 if self.__firsttime:
511 513
512 514 if xmin == None: xmin = numpy.nanmin(x)
513 515 if xmax == None: xmax = numpy.nanmax(x)
514 516 if ymin == None: ymin = numpy.nanmin(y)
515 517 if ymax == None: ymax = numpy.nanmax(y)
516 518 if zmin == None: zmin = numpy.nanmin(z)
517 519 if zmax == None: zmax = numpy.nanmax(z)
518 520
519 521
520 522 self.plot = self.__driver.createPcolor(self.ax, x_buffer,
521 523 y_buffer,
522 524 z_buffer,
523 525 xmin, xmax,
524 526 ymin, ymax,
525 527 zmin, zmax,
526 528 xlabel=xlabel,
527 529 ylabel=ylabel,
528 530 title=title,
529 531 colormap=colormap,
530 532 **kwargs)
531 533
532 534 if self.xmin == None: self.xmin = xmin
533 535 if self.xmax == None: self.xmax = xmax
534 536 if self.ymin == None: self.ymin = ymin
535 537 if self.ymax == None: self.ymax = ymax
536 538 if self.zmin == None: self.zmin = zmin
537 539 if self.zmax == None: self.zmax = zmax
538 540
539 541 self.__firsttime = False
540 542 return
541 543
542 544 self.__driver.pcolor(self.plot,
543 545 z_buffer,
544 546 xlabel=xlabel,
545 547 ylabel=ylabel,
546 548 title=title)
547 549
548 550 # self.__driver.pause()
549 551
550 552 def pcolorbuffer(self, x, y, z,
551 553 xmin=None, xmax=None,
552 554 ymin=None, ymax=None,
553 555 zmin=None, zmax=None,
554 556 xlabel='', ylabel='',
555 557 title='', rti = True, colormap='jet',
556 558 maxNumX = None, maxNumY = None,
557 559 **kwargs):
558 560
559 561 if maxNumX == None:
560 562 maxNumX = self.__MAXNUMTIME
561 563
562 564 if maxNumY == None:
563 565 maxNumY = self.__MAXNUMY
564 566
565 567 if self.__firsttime:
566 568 self.z_buffer = z
567 569 self.x_buffer = numpy.hstack((self.x_buffer, x))
568 570
569 571 if xmin == None: xmin = numpy.nanmin(x)
570 572 if xmax == None: xmax = numpy.nanmax(x)
571 573 if ymin == None: ymin = numpy.nanmin(y)
572 574 if ymax == None: ymax = numpy.nanmax(y)
573 575 if zmin == None: zmin = numpy.nanmin(z)
574 576 if zmax == None: zmax = numpy.nanmax(z)
575 577
576 578
577 579 self.plot = self.__driver.createPcolor(self.ax, self.x_buffer, y, z,
578 580 xmin, xmax,
579 581 ymin, ymax,
580 582 zmin, zmax,
581 583 xlabel=xlabel,
582 584 ylabel=ylabel,
583 585 title=title,
584 586 colormap=colormap,
585 587 **kwargs)
586 588
587 589 if self.xmin == None: self.xmin = xmin
588 590 if self.xmax == None: self.xmax = xmax
589 591 if self.ymin == None: self.ymin = ymin
590 592 if self.ymax == None: self.ymax = ymax
591 593 if self.zmin == None: self.zmin = zmin
592 594 if self.zmax == None: self.zmax = zmax
593 595
594 596 self.__firsttime = False
595 597 return
596 598
597 599 self.x_buffer = numpy.hstack((self.x_buffer[:-1], x[0], x[-1]))
598 600 self.z_buffer = numpy.hstack((self.z_buffer, z))
599 601 z_buffer = self.z_buffer.reshape(-1,len(y))
600 602
601 603 #Decimating data
602 604 xlen = len(self.x_buffer)
603 605 ylen = len(y)
604 606
605 607 decimationx = numpy.floor(xlen/maxNumX) + 1
606 608 decimationy = numpy.floor(ylen/maxNumY) + 1
607 609
608 610 x_buffer = self.x_buffer[::decimationx]
609 611 y_buffer = y[::decimationy]
610 612 z_buffer = z_buffer[::decimationx, ::decimationy]
611 613 #===================================================
612 614
613 615 x_buffer, y_buffer, z_buffer = self.__fillGaps(x_buffer, y_buffer, z_buffer)
614 616
615 617 self.__driver.addpcolorbuffer(self.ax, x_buffer, y_buffer, z_buffer, self.zmin, self.zmax,
616 618 xlabel=xlabel,
617 619 ylabel=ylabel,
618 620 title=title,
619 621 colormap=colormap)
620 622
621 623 # self.__driver.pause()
622 624
623 625 def polar(self, x, y,
624 626 title='', xlabel='',ylabel='',**kwargs):
625 627
626 628 if self.__firsttime:
627 629 self.plot = self.__driver.createPolar(self.ax, x, y, title = title, xlabel = xlabel, ylabel = ylabel)
628 630 self.__firsttime = False
629 631 self.x_buffer = x
630 632 self.y_buffer = y
631 633 return
632 634
633 635 self.x_buffer = numpy.hstack((self.x_buffer,x))
634 636 self.y_buffer = numpy.hstack((self.y_buffer,y))
635 637 self.__driver.polar(self.plot, self.x_buffer, self.y_buffer, xlabel=xlabel,
636 638 ylabel=ylabel,
637 639 title=title)
638 640
639 641 # self.__driver.pause()
640 642
641 643 def __fillGaps(self, x_buffer, y_buffer, z_buffer):
642 644
643 645 if x_buffer.shape[0] < 2:
644 646 return x_buffer, y_buffer, z_buffer
645 647
646 648 deltas = x_buffer[1:] - x_buffer[0:-1]
647 649 x_median = numpy.median(deltas)
648 650
649 651 index = numpy.where(deltas > 5*x_median)
650 652
651 653 if len(index[0]) != 0:
652 654 z_buffer[index[0],::] = self.__missing
653 655 z_buffer = numpy.ma.masked_inside(z_buffer,0.99*self.__missing,1.01*self.__missing)
654 656
655 657 return x_buffer, y_buffer, z_buffer
656 658
657 659
658 660
659 661 No newline at end of file
General Comments 0
You need to be logged in to leave comments. Login now