##// END OF EJS Templates
Spectrum class added
Miguel Valdez -
r4:bbac27f532a1
parent child
Show More
@@ -1,990 +1,1129
1 1 '''
2 2 Created on Jan 25, 2012
3 3
4 4 @author: Miguel Urco
5 5 '''
6 6
7 7 import plplot
8 8 import numpy
9 9
10 10 def cmap1_init(colormap="gray"):
11 11
12 ncolor = None
12 13 rgb_lvl = None
13 14
14 15 # Routine for defining a specific color map 1 in HLS space.
15 16 # if gray is true, use basic grayscale variation from half-dark to light.
16 17 # otherwise use false color variation from blue (240 deg) to red (360 deg).
17 18
18 19 # Independent variable of control points.
19 20 i = numpy.array((0., 1.))
20 21 if colormap=="gray":
22 ncolor = 256
21 23 # Hue for control points. Doesn't matter since saturation is zero.
22 24 h = numpy.array((0., 0.))
23 25 # Lightness ranging from half-dark (for interest) to light.
24 26 l = numpy.array((0.5, 1.))
25 27 # Gray scale has zero saturation
26 28 s = numpy.array((0., 0.))
27 29
28 30 # number of cmap1 colours is 256 in this case.
29 plplot.plscmap1n(256)
31 plplot.plscmap1n(ncolor)
30 32 # Interpolate between control points to set up cmap1.
31 33 plplot.plscmap1l(0, i, h, l, s)
32 34
33 35 return None
34 36
35 37 if colormap=="br_black":
38 ncolor = 256
36 39 # Hue ranges from blue (240 deg) to red (0 or 360 deg)
37 40 h = numpy.array((240., 0.))
38 41 # Lightness and saturation are constant (values taken from C example).
39 42 l = numpy.array((0.6, 0.6))
40 43 s = numpy.array((0.8, 0.8))
41 44
42 45 # number of cmap1 colours is 256 in this case.
43 plplot.plscmap1n(256)
46 plplot.plscmap1n(ncolor)
44 47 # Interpolate between control points to set up cmap1.
45 48 plplot.plscmap1l(0, i, h, l, s)
46 49
47 50 return None
48 51
49 52 if colormap=="tricolor":
53 ncolor = 3
50 54 # Hue ranges from blue (240 deg) to red (0 or 360 deg)
51 55 h = numpy.array((240., 0.))
52 56 # Lightness and saturation are constant (values taken from C example).
53 57 l = numpy.array((0.6, 0.6))
54 58 s = numpy.array((0.8, 0.8))
55 59
56 60 # number of cmap1 colours is 256 in this case.
57 plplot.plscmap1n(3)
61 plplot.plscmap1n(ncolor)
58 62 # Interpolate between control points to set up cmap1.
59 63 plplot.plscmap1l(0, i, h, l, s)
60 64
61 65 return None
62 66
63 67 if colormap == 'rgb' or colormap == 'rgb666':
64 68
65 69 color_sz = 6
66 70 ncolor = color_sz*color_sz*color_sz
67 71 pos = numpy.zeros((ncolor))
68 72 r = numpy.zeros((ncolor))
69 73 g = numpy.zeros((ncolor))
70 74 b = numpy.zeros((ncolor))
71 75 ind = 0
72 76 for ri in range(color_sz):
73 77 for gi in range(color_sz):
74 78 for bi in range(color_sz):
75 79 r[ind] = ri/(color_sz-1.0)
76 80 g[ind] = gi/(color_sz-1.0)
77 81 b[ind] = bi/(color_sz-1.0)
78 82 pos[ind] = ind/(ncolor-1.0)
79 83 ind += 1
80 84 rgb_lvl = [6,6,6] #Levels for RGB colors
81 85
82 86 if colormap == 'rgb676':
83 87 ncolor = 6*7*6
84 88 pos = numpy.zeros((ncolor))
85 89 r = numpy.zeros((ncolor))
86 90 g = numpy.zeros((ncolor))
87 91 b = numpy.zeros((ncolor))
88 92 ind = 0
89 93 for ri in range(8):
90 94 for gi in range(8):
91 95 for bi in range(4):
92 96 r[ind] = ri/(6-1.0)
93 97 g[ind] = gi/(7-1.0)
94 98 b[ind] = bi/(6-1.0)
95 99 pos[ind] = ind/(ncolor-1.0)
96 100 ind += 1
97 101 rgb_lvl = [6,7,6] #Levels for RGB colors
98 102
99 103 if colormap == 'rgb685':
100 104 ncolor = 6*8*5
101 105 pos = numpy.zeros((ncolor))
102 106 r = numpy.zeros((ncolor))
103 107 g = numpy.zeros((ncolor))
104 108 b = numpy.zeros((ncolor))
105 109 ind = 0
106 110 for ri in range(8):
107 111 for gi in range(8):
108 112 for bi in range(4):
109 113 r[ind] = ri/(6-1.0)
110 114 g[ind] = gi/(8-1.0)
111 115 b[ind] = bi/(5-1.0)
112 116 pos[ind] = ind/(ncolor-1.0)
113 117 ind += 1
114 118 rgb_lvl = [6,8,5] #Levels for RGB colors
115 119
116 120 if colormap == 'rgb884':
117 121 ncolor = 8*8*4
118 122 pos = numpy.zeros((ncolor))
119 123 r = numpy.zeros((ncolor))
120 124 g = numpy.zeros((ncolor))
121 125 b = numpy.zeros((ncolor))
122 126 ind = 0
123 127 for ri in range(8):
124 128 for gi in range(8):
125 129 for bi in range(4):
126 130 r[ind] = ri/(8-1.0)
127 131 g[ind] = gi/(8-1.0)
128 132 b[ind] = bi/(4-1.0)
129 133 pos[ind] = ind/(ncolor-1.0)
130 134 ind += 1
131 135 rgb_lvl = [8,8,4] #Levels for RGB colors
132
136
137 if ncolor == None:
138 raise ValueError, "The colormap selected is not valid"
139
133 140 plplot.plscmap1n(ncolor)
134 141 plplot.plscmap1l(1, pos, r, g, b)
135 142
136 143 return rgb_lvl
137 144
138 145 class BasicGraph():
139 146 """
140 147
141 148 """
149
150 hasRange = False
151
142 152 xrange = None
143 153 yrange = None
144 154 zrange = None
145 155
146 156 xlabel = None
147 157 ylabel = None
148 158 title = None
149 159
150 160 legends = None
151 161
152 162 __name = None
153 163 __subpage = None
154 164 __szchar = None
155 165
156 166 __colormap = None
157 167 __colbox = None
158 168 __colleg = None
159 169
160 170 __xpos = None
161 171 __ypos = None
162 172
163 173 __xopt = None #"bcnst"
164 174 __yopt = None #"bcnstv"
165 175
166 176 __xlpos = None
167 177 __ylpos = None
168 178
169 179 __xrangeIsTime = None
170 180
171 181 #Advanced
172 182 __xg = None
173 183 __yg = None
174 184
175 185 def __init__(self):
176 186 """
177 187
178 188 """
179 189 pass
180
190
191 def hasNotXrange(self):
192
193 if self.xrange == None:
194 return 1
195
196 return 0
197
198 def hasNotYrange(self):
199
200 if self.yrange == None:
201 return 1
202
203 return 0
204
205 def hasNotZrange(self):
206
207 if self.zrange == None:
208 return 1
209
210 return 0
181 211 def setName(self, name):
182 212 self.__name = name
183 213
184 214 def setScreenPos(self, xpos, ypos):
185 215 self.__xpos = xpos
186 216 self.__ypos = ypos
187 217
188 def setScreenPos(self, xoff, yoff, xw, yw):
218 def setScreenPosbyWidth(self, xoff, yoff, xw, yw):
189 219 self.__xpos = [xoff, xoff + xw]
190 220 self.__ypos = [yoff, yoff + yw]
191 221
192 222 def setSubpage(self, subpage):
193 223 self.__subpage = subpage
224
225 def setSzchar(self, szchar):
226 self.__szchar = szchar
227
228 def setOpt(self, xopt, yopt):
229 self.__xopt = xopt
230 self.__yopt = yopt
194 231
195 def setRanges(self, xrange, yrange, zrange):
232 def setRanges(self, xrange, yrange, zrange=None):
196 233 """
197 234 """
198 235 self.xrange = xrange
236
199 237 self.yrange = yrange
200 self.zrange = zrange
238
239 if zrange != None:
240 self.zrange = zrange
201 241
202 def __setColormap(self, colormap=None):
242 def setColormap(self, colormap=None):
203 243
204 244 if colormap == None:
205 245 colormap = self.__colormap
206 246
207 247 cmap1_init(colormap)
208 248
209 def __setBox(self):
249 def plotBox(self):
210 250 """
211 251
212 252 """
213 253 plplot.plvpor(self.__xpos[0], self.__xpos[1], self.__ypos[0], self.__ypos[1])
214 254 plplot.plwind(self.xrange[0], self.xrange[1], self.yrange[0], self.yrange[1])
215 255 plplot.plbox(self.__xopt, 0.0, 0, self.__yopt, 0.0, 0)
216 256 plplot.pllab(self.xlabel, self.ylabel, self.title)
217 257
218 258 def setup(self, title=None, xlabel=None, ylabel=None, colormap=None):
219 259 """
220 260 """
221 261 self.title = title
222 262 self.xlabel = xlabel
223 263 self.ylabel = ylabel
224 self.colormap = colormap
264 self.__colormap = colormap
225 265
226 266 def initSubpage(self):
267
268 if plplot.plgdev() == '':
269 raise ValueError, "Plot device has not been initialize"
270
227 271 plplot.pladv(self.__subpage)
228 272 plplot.plschr(0.0, self.__szchar)
229 273
230 274 if self.__xrangeIsTime:
231 275 plplot.pltimefmt("%H:%M")
232 276
233 self.__setColormap()
277 self.setColormap()
234 278 self.initPlot()
235 279
236 280 def initPlot(self):
237 281 """
238 282
239 283 """
284 if plplot.plgdev() == '':
285 raise ValueError, "Plot device has not been initialize"
286
287 xrange = self.xrange
288 if xrange == None:
289 xrange = [0., 1.]
290
291 yrange = self.yrange
292 if yrange == None:
293 yrange = [0., 1.]
294
240 295 plplot.plvpor(self.__xpos[0], self.__xpos[1], self.__ypos[0], self.__ypos[1])
241 plplot.plwind(self.xrange[0], self.xrange[1], self.yrange[0], self.yrange[1])
296 plplot.plwind(xrange[0], xrange[1], yrange[0], yrange[1])
242 297 plplot.plbox(self.__xopt, 0.0, 0, self.__yopt, 0.0, 0)
243 298 plplot.pllab(self.xlabel, self.ylabel, self.title)
244
245
246 def basicXYPlot(self):
247 pass
299
300 def colorbarPlot(self):
301 data = numpy.arange(256)
302 data = numpy.reshape(data, (1,-1))
303
304 self.plotBox()
305 plplot.plimage(data,
306 self.xrange[0],
307 self.xrange[1],
308 self.yrange[0],
309 self.yrange[1],
310 0.,
311 255.,
312 self.xrange[0],
313 self.xrange[1],
314 self.yrange[0],
315 self.yrange[1],)
316
317 def basicXYPlot(self, x, y):
318 self.plotBox()
319 plplot.plline(x, y)
248 320
249 321 def basicXYwithErrorPlot(self):
250 322 pass
251 323
252 324 def basicLineTimePlot(self):
253 325 pass
254 326
255 327 def basicPcolorPlot(self, data, xmin, xmax, ymin, ymax, zmin, zmax):
256 328 """
257 329 """
258 330
259 self.__setBox()
331 self.plotBox()
260 332 plplot.plimage(data, xmin, xmax, ymin, ymax, zmin, zmax, xmin, xmax, ymin, ymax)
261 333
262 334
263 335 def __getBoxpltr(self, x, y, deltax=None, deltay=None):
264 336
265 337 if not(len(x)>1 and len(y)>1):
266 338 raise ValueError, "x axis and y axis are empty"
267 339
268 340 if deltax == None: deltax = x[-1] - x[-2]
269 341 if deltay == None: deltay = y[-1] - y[-2]
270 342
271 343 x1 = numpy.append(x, x[-1] + deltax)
272 344 y1 = numpy.append(y, y[-1] + deltay)
273 345
274 346 xg = (numpy.multiply.outer(x1, numpy.ones(len(y1))))
275 347 yg = (numpy.multiply.outer(numpy.ones(len(x1)), y1))
276 348
277 349 self.__xg = xg
278 350 self.__yg = yg
279 351
280 352 def advPcolorPlot(self, data, x, y, zmin=0., zmax=0.):
281 353 """
282 354 """
283 355
284 356 if self.__xg == None and self.__yg == None:
285 357 self.__getBoxpltr(x, y)
286 358
287 359 plplot.plimagefr(data, x[0], x[-1], y[0], y[-1], 0., 0., zmin, zmax, plplot.pltr2, self.__xg, self.__yg)
288 360
289 361
290 362 class Graph():
291 363 """
292 364 """
293 365
294 366 graphObjDict = {}
295 367
296 368 def __init__(self):
297 369 raise
298
299 def setup(self):
300 raise
301
302 def plotData(self):
303 raise
304 370
305 371 class Spectrum(Graph):
306 372
373
307 374 showColorbar = False
308 375 showPowerProfile = True
309 376
377 __szchar = 0.7
310 378
311 379 def __init__(self):
312 380
313 381 key = "spec"
314 382
315 383 specObj = BasicGraph()
316 384 specObj.setName(key)
317 385
318 386 self.graphObjDict[key] = specObj
319 387
320 388
321 def setup(self, title="", xlabel="", ylabel="", colormap="jet", showColorbar=False, showPowerProfile=False):
322
323 xi = 0.12
324 xw = 0.86
325 xf = xi + xw
389 def setup(self, subpage, title="", xlabel="", ylabel="", colormap="jet", showColorbar=False, showPowerProfile=False):
390 """
391 """
326 392
327 yi = 0.14
328 yw = 0.80
329 yf = yi + yw
393 xi = 0.12; xw = 0.78; xf = xi + xw
394 yi = 0.14; yw = 0.80; yf = yi + yw
330 395
331 xcmapi = xcmapf = 0.
332 xpowi = xpowf = 0.
396 xcmapi = xcmapf = 0.; xpowi = xpowf = 0.
333 397
334 specObj = self.graphObjDict[0]
398 key = "spec"
399 specObj = self.graphObjDict[key]
400 specObj.setSubpage(subpage)
401 specObj.setSzchar(self.__szchar)
402 specObj.setOpt("bcnts","bcnts")
335 403 specObj.setup(title,
336 404 xlabel,
337 405 ylabel,
338 406 colormap)
339 407
340 408 if showColorbar:
341 409 key = "colorbar"
342 410
343 411 cmapObj = BasicGraph()
344 412 cmapObj.setName(key)
413 cmapObj.setSubpage(subpage)
414 cmapObj.setSzchar(self.__szchar)
415 cmapObj.setOpt("bc","bcmt")
345 416 cmapObj.setup(title="dBs",
346 417 xlabel="",
347 418 ylabel="",
348 419 colormap=colormap)
349 420
350 self.graphObjDict[key] = cmapObject
421 self.graphObjDict[key] = cmapObj
351 422
352 423 xcmapi = 0.
353 xcmapw = 0.16
424 xcmapw = 0.05
354 425 xw -= xcmapw
355 426
356 427 if showPowerProfile:
357 428 key = "powerprof"
358 429
359 430 powObj = BasicGraph()
360 431 powObj.setName(key)
432 powObj.setSubpage(subpage)
433 powObj.setSzchar(self.__szchar)
434 plplot.pllsty(2)
435 powObj.setOpt("bcntg","bc")
436 plplot.pllsty(1)
361 437 powObj.setup(title="Power Profile",
362 438 xlabel="dBs",
363 439 ylabel="")
364 440
365 self.graphObjDict[key] = powObject
441 self.graphObjDict[key] = powObj
366 442
367 443 xpowi = 0.
368 xpoww = 0.23
444 xpoww = 0.24
369 445 xw -= xpoww
370 446
371 447 xf = xi + xw
372 448 yf = yi + yw
373 449 xcmapf = xf
374 450
375 specObj.setScreenPos([xi, yf], [yi, yf])
451 specObj.setScreenPos([xi, xf], [yi, yf])
376 452
377 453 if showColorbar:
378 xcmapi = xf + 0.2
454 xcmapi = xf + 0.02
379 455 xcmapf = xcmapi + xcmapw
380 cmapObj.setScreenPos([xcmapi, ycmapf], [ycmapi, ycmapf])
456 cmapObj.setScreenPos([xcmapi, xcmapf], [yi, yf])
381 457
382 458 if showPowerProfile:
383 xpowi = xcmapf + 0.3
459 xpowi = xcmapf + 0.06
384 460 xpowf = xpowi + xpoww
385 powObj.setScreenPos([xpowi, ypowf], [ypowi, ypowf])
461 powObj.setScreenPos([xpowi, xpowf], [yi, yf])
386 462
387 463
388 specObj.initSubpage()
389
390 if showColorbar:
391 cmapObj.initPlot()
392
393 if showPowerProfile:
394 powObj.initPlot()
464 # specObj.initSubpage()
465 #
466 # if showColorbar:
467 # cmapObj.initPlot()
468 #
469 # if showPowerProfile:
470 # powObj.initPlot()
395 471
396 472 self.showColorbar = showColorbar
397 473 self.showPowerProfile = showPowerProfile
398 474
399 def setRanges(self, xrange=None, yrange=None, zrange=None):
475 def setRanges(self, xrange, yrange, zrange):
400 476
401 477 key = "spec"
402 478 specObj = self.graphObjDict[key]
403 479 specObj.setRanges(xrange, yrange, zrange)
404 480
405 481 keyList = self.graphObjDict.keys()
406 482
407 483 key = "colorbar"
408 484 if key in keyList:
409 485 cmapObj = self.graphObjDict[key]
410 cmapObj.setRanges([0., 1.], zrange, [0., 1.])
486 cmapObj.setRanges([0., 1.], zrange)
411 487
412 488 key = "powerprof"
413 489 if key in keyList:
414 490 powObj = self.graphObjDict[key]
415 491 powObj.setRanges(zrange, yrange)
416 492
417 def plotData(self, data , xmin, xmax, ymin, ymax):
493 def plotData(self, data , xmin=None, xmax=None, ymin=None, ymax=None, zmin=None, zmax=None):
418 494
419 pass
495 key = "spec"
496 specObj = self.graphObjDict[key]
497 specObj.initSubpage()
498
499 if xmin == None:
500 xmin = 0.
501
502 if xmax == None:
503 xmax = 1.
420 504
505 if ymin == None:
506 ymin = 0.
507
508 if ymax == None:
509 ymax = 1.
510
511 if zmin == None:
512 zmin = numpy.nanmin(data)
513
514 if zmax == None:
515 zmax = numpy.nanmax(data)
516
517 if not(specObj.hasRange):
518 self.setRanges([xmin, xmax], [ymin,ymax], [zmin,zmax])
519
520 specObj.basicPcolorPlot(data, xmin, xmax, ymin, ymax, specObj.zrange[0], specObj.zrange[1])
521
522 if self.showColorbar:
523 key = "colorbar"
524 cmapObj = self.graphObjDict[key]
525 cmapObj.colorbarPlot()
526
527 if self.showPowerProfile:
528 power = numpy.average(data, axis=1)
529
530 step = (ymax - ymin)/(power.shape[0]-1)
531 heis = numpy.arange(ymin, ymax + step, step)
532
533 key = "powerprof"
534 powObj = self.graphObjDict[key]
535 powObj.basicXYPlot(power, heis)
536
421 537 class CrossSpectrum(Graph):
422 538
423 539 def __init__(self):
424 540 pass
425 541
426 542 def setup(self):
427 543 pass
428 544
429 545 def plotData(self):
430 546 pass
431 547
432 548 class Graph2():
433 549
434 550 def __init__(self):
435 551 """
436 552 Initiation of variables
437 553
438 554 Variables:
439 555
440 556 type:
441 557 windowsize:
442 558 cmap: colormap
443 559 showcmap: show the colormap selected on the graphic
444 560
445 561 """
446 562
447 563 self.id = None
448 564 self.subpage = None
449 565 self.type = None
450 566 self.windowsize = None
451 567
452 568 self.szchar = 0.6
453 569 self.title = None
454 570 self.xlabel = None
455 571 self.ylabel = None
456 572
457 573 self.showGraph2 = None
458 574 self.cmap = None
459 575 self.showcmap = None
460 576
461 577 self.xrangeIsTime = False
462 578
463 579 self.xrange = ()
464 580 self.yrange = ()
465 581 self.zrange = ()
466 582
467 583 self.xscreen = ()
468 584 self.yscreen = ()
469 585
470 586 self.xcmapscreen = ()
471 587 self.ycmapscreen = ()
472 588
473 589 self.x2screen = ()
474 590 self.y2screen = ()
475 591
476 592
477 593 def setup(self, id, type=0, windowsize=1., title="", xlabel="", ylabel="", showGraph2=None, cmap="jet", showcmap=False, xrangeIsTime=False):
478 594 """
479 595 Inputs:
480 596 type: This variable indicates the kind of graphic. Instantaneous data or background data
481 597
482 598 0: real instantaneous data, like spectrum
483 599 1: background data, like spectrogram (RTI)
484 600 2: complex instantaneous data, like cross-spectrum
485 601
486 602 windowsize : Float. Size of window. It can be full window (1), half window (0.5) or 1 1/2 window (1.5)
487 603 cmap : Set the colormap to use
488 604 showcmap : Show the colormap used on the graphic.
489 605
490 606
491 607 Variables affected:
492 608
493 609 """
494 610
495 611 # if windowsize == 1.:
496 612 # self.xscreen = (0.12, 0.96)
497 613 # self.yscreen = (0.14, 0.94)
498 614 #
499 615 # elif windowsize == 0.5:
500 616 # self.xscreen = (0.12, 0.52)
501 617 # self.yscreen = (0.14, 0.94)
502 618 #
503 619 # elif windowsize == 1.5:
504 620 # self.xscreen = (-0.44, 0.96)
505 621 # self.yscreen = (0.14, 0.94)
506 622 #
507 623 # else:
508 624 # raise ValueError, "type of graphic has not been properly set"
509 625
510 626 if showGraph2 == None:
511 627 if type == 0:
512 628 showGraph2 = True
513 629
514 630 if type == 1:
515 631 showGraph2 = True
516 632
517 633 if type == 2:
518 634 showGraph2 = True
519 635
520 636 xscreen = (0.12, 0.98)
521 637 yscreen = (0.14, 0.94)
522 638 xcmapscreen = (0., 0.)
523 639 ycmapscreen = (0.14, 0.94)
524 640 x2screen = (0., 0.)
525 641 y2screen = (0.14, 0.94)
526 642
527 643 if type == 0:
528 644
529 645 #showGraph2 <> PowerProfile
530 646 if showGraph2 and showcmap:
531 647 xscreen = (0.12, 0.62)
532 648 xcmapscreen = (0.64, 0.70)
533 649 x2screen = (0.75, 0.98)
534 650
535 651 elif showGraph2:
536 652 xscreen = (0.12, 0.67)
537 653 xcmapscreen = (0., 0.)
538 654 x2screen = (0.7, 0.98)
539 655
540 656 elif showcmap:
541 657 xscreen = (0.12, 0.85)
542 658 xcmapscreen = (0.87, 0.93)
543 659 x2screen = (0., 0.)
544 660
545 661 if type == 1:
546 662 xscreen = (0.06, 0.98)
547 663 yscreen = (0.16, 0.84)
548 664 #showGraph2 <> Phase
549 665 if showGraph2 and showcmap:
550 666 xscreen = (0.06, 0.75)
551 667 xcmapscreen = (0.76, 0.80)
552 668 x2screen = (0.82, 0.98)
553 669
554 670 elif showGraph2:
555 671 xscreen = (0.06, 0.80)
556 672 xcmapscreen = (0., 0.)
557 673 x2screen = (0.82, 0.98)
558 674
559 675 elif showcmap:
560 676 xscreen = (0.06, 0.92)
561 677 xcmapscreen = (0.93, 0.96)
562 678 x2screen = (0., 0.)
563 679
564 680 if type == 2:
565 681 if showGraph2 and showcmap:
566 682 xscreen = (0.12, 0.46)
567 683 xcmapscreen = (0.48, 0.54)
568 684 x2screen = (0.56, 0.98)
569 685
570 686 elif showGraph2:
571 687 xscreen = (0.12, 0.54)
572 688 xcmapscreen = (0., 0.)
573 689 x2screen = (0.56, 0.98)
574 690
575 691 elif showcmap:
576 692 xscreen = (0.12, 0.85)
577 693 xcmapscreen = (0.87, 0.93)
578 694 x2screen = (0., 0.)
579 695
580 696 if type == 3:
581 697 xscreen = (0.12, 0.52)
582 698 x2screen = (0.76, 0.96)
583 699
584 700 if type == 4:
585 701 xscreen = (-0.44, 0.96)
586 702 x2screen = (0.76, 0.96)
587 703
588 704
589 705 self.id = id
590 706 self.subpage = id + 1
591 707 self.type = type
592 708 self.windowsize = windowsize
593 709 self.title = title
594 710 self.xlabel = xlabel
595 711 self.ylabel = ylabel
596 712 self.showGraph2 = showGraph2
597 713 self.cmap = cmap
598 714 self.showcmap = showcmap
599 715 self.xrangeIsTime = xrangeIsTime
600 716
601 717 self.xscreen = xscreen
602 718 self.yscreen = yscreen
603 719 self.x2screen = x2screen
604 720 self.y2screen = y2screen
605 721 self.xcmapscreen = xcmapscreen
606 722 self.ycmapscreen = ycmapscreen
607 723
608 724 def setRanges(self, xrange=(), yrange=(), zrange=()):
609 725 """
610 726 Inputs:
611 727 xrange
612 728 yrange
613 729 zrange
614 730
615 731 Variables affected:
616 732 self.xrange
617 733 self.yrange
618 734 self.zrange
619 735
620 736 """
621 737
622 738 self.xrange = xrange
623 739 self.yrange = yrange
624 740 self.zrange = zrange
625 741
626 742 def setsubPage(self, subpage):
627 743 """
628 744 """
629 745 self.subpage = subpage
630 746
631 747 def plotColorbar(self):
632 748
633 749 if not(self.showcmap):
634 750 return 0
635 751
636 752 colors = numpy.arange(255)
637 753 colors = numpy.reshape(colors, (1,255))
638 754
639 755 plplot.plvpor(self.xcmapscreen[0], self.xcmapscreen[1], self.ycmapscreen[0], self.ycmapscreen[1])
640 756 plplot.plwind(0., 1., self.zrange[0], self.zrange[1])
641 757 plplot.plbox("bc",0.0,0,"bc",0.0,0)
642 758 plplot.pllab("", "", "dBs")
643 759 plplot.plimage(colors, 0., 1., self.zrange[0], self.zrange[1], 0., 255., 0., 1., self.zrange[0], self.zrange[1])
644 760
645 761 return 1
646 762
647 763 def plotPowerProfile(self, power, ymin, ymax):
648 764
649 765 if not(self.showGraph2):
650 766 return 0
651 767
652 768 ny = power.shape[0]
653 769 yscale = (ymax - ymin) / ny
654 770 y = ymin + yscale*numpy.arange(ny)
655 771
656 772 plplot.plvpor(self.x2screen[0], self.x2screen[1], self.y2screen[0], self.y2screen[1])
657 773 plplot.plwind(self.zrange[0], self.zrange[1], self.yrange[0], self.yrange[1])
658 774 plplot.plbox("bcnst",0.0,0,"bc",0.0,0)
659 775 plplot.pllsty(2)
660 776 plplot.plbox("bcnstg",0.0,0,"bc",0.0,0)
661 777 plplot.pllsty(1)
662 778 plplot.pllab("dB", "", "Power Profile")
663 779 plplot.plline(power, y)
664 780 #plplot.plflush()
665 781
666 782 return 1
667 783
668 784 def plotSpectrum(self, data, xmin=0.0, xmax=1.0, ymin=0.0, ymax=1.0, zmin=None, zmax=None):
669 785 """
670 786
671 787 """
672 788 if zmin == None: zmin = numpy.nanmin(data)
673 789 if zmax == None: zmax = numpy.nanmax(data)
674 790
675 791 if self.xrange == (): self.xrange = (xmin, xmax)
676 792 if self.yrange == (): self.yrange = (ymin, ymax)
677 793 if self.zrange == (): self.zrange = (zmin, zmax)
678 794
679 795 plplot.pladv(self.subpage)
680 796 plplot.plschr(0.0,self.szchar)
681 797
682 798 power = numpy.average(data, axis=0)
683 799
684 800 self.plotPowerProfile(power, ymin, ymax)
685 801 self.plotColorbar()
686 802
687 803 if self.xrangeIsTime:
688 804 plplot.pltimefmt("%H:%M")
689 805
690 806 plplot.plvpor(self.xscreen[0], self.xscreen[1], self.yscreen[0], self.yscreen[1])
691 807 plplot.plwind(self.xrange[0], self.xrange[1], self.yrange[0], self.yrange[1])
692 808 plplot.plbox("bcnst",0.0,0,"bcnstv",0.0,0)
693 809 plplot.pllab(self.xlabel, self.ylabel, self.title)
694 810 plplot.plimage(data, xmin, xmax, ymin, ymax, zmin, zmax, xmin, xmax, ymin, ymax)
695 811 #plplot.plflush()
696 812
697 813 def plotSpectrogram(self, data, xmin=0.0, xmax=1.0, ymin=0.0, ymax=1.0, zmin=0.0, zmax=1.0):
698 814 """
699 815
700 816 """
701 817
702 818 if zmin == None: zmin = numpy.nanmin(data)
703 819 if zmax == None: zmax = numpy.nanmax(data)
704 820
705 821 if self.xrange == (): self.xrange = (xmin, xmax)
706 822 if self.yrange == (): self.yrange = (ymin, ymax)
707 823 if self.zrange == (): self.zrange = (zmin, zmax)
708 824
709 825 plplot.pladv(self.subpage)
710 826
711 827 plplot.plschr(0.0,self.szchar+0.3)
712 828
713 829 power = numpy.average(data, axis=0)
714 830
715 831 self.plotPowerProfile(power, ymin, ymax)
716 832 self.plotColorbar()
717 833
718 834 if self.xrangeIsTime:
719 835 plplot.pltimefmt("%H:%M")
720 836
721 837 plplot.plvpor(self.xscreen[0], self.xscreen[1], self.yscreen[0], self.yscreen[1])
722 838 plplot.plwind(self.xrange[0], self.xrange[1], self.yrange[0], self.yrange[1])
723 839 plplot.plbox("bcnst", 0.0, 0, "bcnstv", 0.0, 0)
724 840
725 841 plplot.pllab(self.xlabel, self.ylabel, self.title)
726 842 plplot.plimage(data, xmin, xmax, ymin, ymax, zmin, zmax, xmin, xmax, ymin, ymax)
727 843 #plplot.plflush()
728 844
729 845
730 846 class PlotData():
731 847 '''
732 848 classdocs
733 849 '''
734 850
735 851 __INST_XSIZE = 300
736 852 __INST_YSIZE = 280
737 853 __BACKGR_XSIZE = 900
738 854 __BACKGR_YSIZE = 150
739 855 __SPACE = 100
740 856
741 857 def __init__(self):
742 858 '''
743 859 Constructor
744 860 '''
745 861
746 862 self.nx = None
747 863 self.ny = None
748 864 self.xsize = None
749 865 self.ysize = None
750 866
751 867 self.objGraphList = []
752 868
753 869 def getNumSubPages(self):
754 870
755 871 nT0 = 0
756 872 nT1 = 0
757 873 nT2 = 0
758 874 nT10 = 0
759 875 nT11 = 0
760 876
761 877 for thisObj in self.objGraphList:
762 878 if thisObj.type == 0:
763 879 nT0 += 1
764 880 continue
765 881
766 882 if thisObj.type == 1:
767 883 nT1 += 1
768 884 continue
769 885
770 886 if thisObj.type == 2:
771 887 nT2 += 1
772 888 continue
773 889
774 890 if thisObj.type == 10:
775 891 nT10 += 1
776 892 continue
777 893
778 894 if thisObj.type == 11:
779 895 nT11 += 1
780 896 continue
781 897
782 898 nSpectrum = nT0 + nT2
783 899
784 900 if (nSpectrum > 0) and nT1*nT10*nT11 == 0:
785 901
786 902 if nSpectrum in [1,2]: nx = 1
787 903 elif nSpectrum in [3,4,5,6]: nx = 2
788 904 else: nx = 3
789 905
790 906 if nSpectrum in [1]: ny = 1
791 907 elif nSpectrum in [2,3,4]: ny = 2
792 908 else: ny = 3
793 909
794 910 elif nT1 > 0 and nT0*nT10*nT11 == 0:
795 911 nx = 1
796 912 ny = nT1
797 913
798 914 elif nT10 == nT11 and nT0*nT1 == 0:
799 915 nx = nT10
800 916 ny = 2
801 917
802 918 else:
803 919 raise ValueError, "number of instantaneous and background graphics are not consistent"
804 920
805 921 self.nx = nx
806 922 self.ny = ny
807 923
808 924 return nx, ny
809 925
810 926 def getSizeScreen(self):
811 927
812 928 nx, ny = self.nx, self.ny
813 929
814 930 if nx == None or ny == None:
815 931 raise ValueError, "The number of subpages have been set yet, please use the getNumSubPages method for this."
816 932
817 933 nT0 = 0
818 934 nT1 = 0
819 935 nT2 = 0
820 936 nT10 = 0
821 937 nT11 = 0
822 938
823 939 for thisObj in self.objGraphList:
824 940 if thisObj.type == 0:
825 941 nT0 += 1
826 942 continue
827 943
828 944 if thisObj.type == 1:
829 945 nT1 += 1
830 946 continue
831 947
832 948 if thisObj.type == 2:
833 949 nT2 += 1
834 950 continue
835 951
836 952 if thisObj.type == 10:
837 953 nT10 += 1
838 954 continue
839 955
840 956 if thisObj.type == 11:
841 957 nT11 += 1
842 958 continue
843 959
844 960 if (nT0 > 0 or nT2 > 0) and nT1 > 0:
845 961 raise ValueError, "Different type of graphics have been selected"
846 962
847 963 if nT0 > 0 or nT2 > 0:
848 964 xsize = ny*self.__INST_XSIZE
849 965 ysize = nx*self.__INST_YSIZE
850 966
851 967 elif nT1 > 0:
852 968 xsize = nx*self.__BACKGR_XSIZE
853 969 ysize = ny*self.__BACKGR_YSIZE
854 970
855 971 elif nT10 == nT11:
856 972 xsize = self.__INST_XSIZE + self.__BACKGR_XSIZE + self.__SPACE
857 973 ysize = nx*self.__BACKGR_YSIZE
858 974 else:
859 975 raise ValueError, "number of instantaneous and background graphics are not consistent"
860 976
861 977 self.xsize = xsize
862 978 self.ysize = ysize
863 979
864 980 return xsize, ysize
865 981
866 982 def setup(self, sizescreen="800x600", save=False, gpath="", filename=""):
867 983 """
868 984 """
869 985
870 986 self.sizecreen = sizescreen
871 987 self.save = save
872 988 self.gpath = gpath
873 989 self.filename = filename
874 990
875 991 def addGraph(self, type=0, xlabel="", ylabel="", title="", showGraph2=False, showcmap=False, cmap="jet", windowsize=1.):
876 992 """
877 993 type: This variable indicates the kind of graphics. Instantaneous data or background data
878 994
879 995 0: real instantaneous data, like a spectrum
880 996 1: background data, like a spectrogram (RTI)
881 997 2: complex instantaneous data, like cross-spectrum
882 998
883 999
884 1000 windowsize: Float. Size of window. It can be::
885 1001 1.0: full window (1)
886 1002 0.5: half window
887 1003 1.5: 1 1/2 window (1.5)
888 1004
889 1005 If some graps have already been set with one graphic type the next ones should be of the same type
890 1006 """
891 1007
892 1008 id = len(self.objGraphList)
893 1009
894 1010 objGraph = Graph2()
895 1011 objGraph.setup(id, type, windowsize, title, xlabel, ylabel, showGraph2, cmap, showcmap)
896 1012
897 1013 self.objGraphList.append(objGraph)
898 1014
899 1015 return id
900 1016
901 1017 def getGraphFromId(self, id):
902 1018 """
903 1019
904 1020 """
905 1021 if id >= len(self.objGraphList):
906 1022 return None
907 1023
908 1024 return self.objGraphList[id]
909 1025
910 1026 def setRanges(self, id=0, xrange=(), yrange=(), zrange=()):
911 1027 """
912 1028
913 1029 """
914 1030 thisGraphObj = self.getGraphFromId(id)
915 1031 thisGraphObj.setmargins(xrange, yrange, zrange)
916 1032
917 1033
918 1034 def addText(self, id, xpos=0, ypos=0, text=""):
919 1035 """
920 1036
921 1037 """
922 1038 thisGraphObj = self.getGraphFromId(id)
923 1039
924 1040 plplot.pladv(thisGraphObj.subpage)
925 1041 plplot.plmtex("b", 5, xpos, ypos, text)
926 1042
927 1043 def plotData(self, id, data, xmin=0.0, xmax=1.0, ymin=0.0, ymax=1.0, zmin=None, zmax=None):
928 1044
929 1045 thisGraphObj = self.getGraphFromId(id)
930 1046
931 1047 if thisGraphObj == None:
932 1048 return 0
933 1049
934 1050 plplot.plcol0(1)
935 1051 if thisGraphObj.type in [0,2]:
936 1052 thisGraphObj.plotSpectrum(data, xmin, xmax, ymin, ymax, zmin, zmax)
937 1053 return 1
938 1054
939 1055 if thisGraphObj.type in [1]:
940 1056 thisGraphObj.plotSpectrogram(data, xmin, xmax, ymin, ymax, zmin, zmax)
941 1057 return 1
942 1058
943 1059 return 0
944 1060
945 1061 def iniPlot(self, nx=None, ny=None):
946 1062 """
947 1063
948 1064 """
949 1065 if nx == None or ny == None:
950 1066 nx, ny = self.getNumSubPages()
951 1067
952 1068 self.getSizeScreen()
953 1069
954 1070 plplot.plsetopt("geometry", "%dx%d" %(self.xsize, self.ysize))
955 1071 plplot.plsdev("xcairo")
956 1072 plplot.plscolbg(255,255,255)
957 1073 plplot.plscol0(1,0,0,0)
958 1074 plplot.plinit()
959 1075 plplot.plssub(nx, ny)
960 1076 plplot.pladv(0)
961 1077
962 1078 #plplot.plspause(0)
963 1079
964 1080 def end(self):
965 1081 plplot.plflush()
966 1082 plplot.plend()
967 1083
968 1084 if __name__ == '__main__':
969 1085
970 1086 import numpy
971 data = numpy.random.uniform(-50,50,(150,250))
1087 plplot.plsetopt("geometry", "%dx%d" %(350*2, 300*2))
1088 plplot.plsdev("xcairo")
1089 plplot.plscolbg(255,255,255)
1090 plplot.plscol0(1,0,0,0)
1091 plplot.plinit()
1092 plplot.plssub(2, 2)
1093
1094 nx = 64
1095 ny = 100
1096
1097 data = numpy.random.uniform(-50,50,(nx,ny))
1098
1099 specObj = Spectrum()
1100 specObj.setup(1, "Spectrum", "Frequency", "Range", "br_black", True, True)
1101 specObj.plotData(data)
1102
1103 data = numpy.random.uniform(-50,50,(nx,ny))
1104
1105 spec2Obj = Spectrum()
1106 spec2Obj.setup(2, "Spectrum", "Frequency", "Range", "br_black", True, True)
1107 spec2Obj.plotData(data)
1108
1109 plplot.plend()
1110 exit(0)
972 1111
973 1112 objPlot = PlotData()
974 1113 objPlot.addGraph(1, "Frequency", "Height", "Channel A")
975 1114 objPlot.addGraph(1, "Frequency", "Height", "Channel B", showGraph2=True)
976 1115 objPlot.addGraph(1, "Frequency", "Height", "Channel C", showcmap=True)
977 1116 #
978 1117 # objPlot.addGraph(1, "Frequency", "Height", "Cross A-B")
979 1118 # objPlot.addGraph(1, "Frequency", "Height", "Cross A-C", showGraph2=True)
980 1119 # objPlot.addGraph(1, "Frequency", "Height", "Cross A-D", showcmap=True)
981 1120 #
982 1121 objPlot.addGraph(1, "Frequency", "Height", "Channel D", showcmap=True, showGraph2=True)
983 1122 objPlot.addGraph(1, "Frequency", "Height", "Cross A-E", showcmap=True, showGraph2=True)
984 1123
985 1124 objPlot.iniPlot()
986 1125
987 1126 for id in range(10):
988 1127 objPlot.plotData(id, data)
989 1128
990 1129 objPlot.end() No newline at end of file
General Comments 0
You need to be logged in to leave comments. Login now