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