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