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