##// END OF EJS Templates
Spectrum class added
Miguel Valdez -
r4:bbac27f532a1
parent child
Show More
@@ -9,6 +9,7 import numpy
9
9
10 def cmap1_init(colormap="gray"):
10 def cmap1_init(colormap="gray"):
11
11
12 ncolor = None
12 rgb_lvl = None
13 rgb_lvl = None
13
14
14 # Routine for defining a specific color map 1 in HLS space.
15 # Routine for defining a specific color map 1 in HLS space.
@@ -18,6 +19,7 def cmap1_init(colormap="gray"):
18 # Independent variable of control points.
19 # Independent variable of control points.
19 i = numpy.array((0., 1.))
20 i = numpy.array((0., 1.))
20 if colormap=="gray":
21 if colormap=="gray":
22 ncolor = 256
21 # Hue for control points. Doesn't matter since saturation is zero.
23 # Hue for control points. Doesn't matter since saturation is zero.
22 h = numpy.array((0., 0.))
24 h = numpy.array((0., 0.))
23 # Lightness ranging from half-dark (for interest) to light.
25 # Lightness ranging from half-dark (for interest) to light.
@@ -26,13 +28,14 def cmap1_init(colormap="gray"):
26 s = numpy.array((0., 0.))
28 s = numpy.array((0., 0.))
27
29
28 # number of cmap1 colours is 256 in this case.
30 # number of cmap1 colours is 256 in this case.
29 plplot.plscmap1n(256)
31 plplot.plscmap1n(ncolor)
30 # Interpolate between control points to set up cmap1.
32 # Interpolate between control points to set up cmap1.
31 plplot.plscmap1l(0, i, h, l, s)
33 plplot.plscmap1l(0, i, h, l, s)
32
34
33 return None
35 return None
34
36
35 if colormap=="br_black":
37 if colormap=="br_black":
38 ncolor = 256
36 # Hue ranges from blue (240 deg) to red (0 or 360 deg)
39 # Hue ranges from blue (240 deg) to red (0 or 360 deg)
37 h = numpy.array((240., 0.))
40 h = numpy.array((240., 0.))
38 # Lightness and saturation are constant (values taken from C example).
41 # Lightness and saturation are constant (values taken from C example).
@@ -40,13 +43,14 def cmap1_init(colormap="gray"):
40 s = numpy.array((0.8, 0.8))
43 s = numpy.array((0.8, 0.8))
41
44
42 # number of cmap1 colours is 256 in this case.
45 # number of cmap1 colours is 256 in this case.
43 plplot.plscmap1n(256)
46 plplot.plscmap1n(ncolor)
44 # Interpolate between control points to set up cmap1.
47 # Interpolate between control points to set up cmap1.
45 plplot.plscmap1l(0, i, h, l, s)
48 plplot.plscmap1l(0, i, h, l, s)
46
49
47 return None
50 return None
48
51
49 if colormap=="tricolor":
52 if colormap=="tricolor":
53 ncolor = 3
50 # Hue ranges from blue (240 deg) to red (0 or 360 deg)
54 # Hue ranges from blue (240 deg) to red (0 or 360 deg)
51 h = numpy.array((240., 0.))
55 h = numpy.array((240., 0.))
52 # Lightness and saturation are constant (values taken from C example).
56 # Lightness and saturation are constant (values taken from C example).
@@ -54,7 +58,7 def cmap1_init(colormap="gray"):
54 s = numpy.array((0.8, 0.8))
58 s = numpy.array((0.8, 0.8))
55
59
56 # number of cmap1 colours is 256 in this case.
60 # number of cmap1 colours is 256 in this case.
57 plplot.plscmap1n(3)
61 plplot.plscmap1n(ncolor)
58 # Interpolate between control points to set up cmap1.
62 # Interpolate between control points to set up cmap1.
59 plplot.plscmap1l(0, i, h, l, s)
63 plplot.plscmap1l(0, i, h, l, s)
60
64
@@ -129,7 +133,10 def cmap1_init(colormap="gray"):
129 pos[ind] = ind/(ncolor-1.0)
133 pos[ind] = ind/(ncolor-1.0)
130 ind += 1
134 ind += 1
131 rgb_lvl = [8,8,4] #Levels for RGB colors
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 plplot.plscmap1n(ncolor)
140 plplot.plscmap1n(ncolor)
134 plplot.plscmap1l(1, pos, r, g, b)
141 plplot.plscmap1l(1, pos, r, g, b)
135
142
@@ -139,6 +146,9 class BasicGraph():
139 """
146 """
140
147
141 """
148 """
149
150 hasRange = False
151
142 xrange = None
152 xrange = None
143 yrange = None
153 yrange = None
144 zrange = None
154 zrange = None
@@ -177,7 +187,27 class BasicGraph():
177
187
178 """
188 """
179 pass
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 def setName(self, name):
211 def setName(self, name):
182 self.__name = name
212 self.__name = name
183
213
@@ -185,28 +215,38 class BasicGraph():
185 self.__xpos = xpos
215 self.__xpos = xpos
186 self.__ypos = ypos
216 self.__ypos = ypos
187
217
188 def setScreenPos(self, xoff, yoff, xw, yw):
218 def setScreenPosbyWidth(self, xoff, yoff, xw, yw):
189 self.__xpos = [xoff, xoff + xw]
219 self.__xpos = [xoff, xoff + xw]
190 self.__ypos = [yoff, yoff + yw]
220 self.__ypos = [yoff, yoff + yw]
191
221
192 def setSubpage(self, subpage):
222 def setSubpage(self, subpage):
193 self.__subpage = subpage
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 self.xrange = xrange
235 self.xrange = xrange
236
199 self.yrange = yrange
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 if colormap == None:
244 if colormap == None:
205 colormap = self.__colormap
245 colormap = self.__colormap
206
246
207 cmap1_init(colormap)
247 cmap1_init(colormap)
208
248
209 def __setBox(self):
249 def plotBox(self):
210 """
250 """
211
251
212 """
252 """
@@ -221,30 +261,62 class BasicGraph():
221 self.title = title
261 self.title = title
222 self.xlabel = xlabel
262 self.xlabel = xlabel
223 self.ylabel = ylabel
263 self.ylabel = ylabel
224 self.colormap = colormap
264 self.__colormap = colormap
225
265
226 def initSubpage(self):
266 def initSubpage(self):
267
268 if plplot.plgdev() == '':
269 raise ValueError, "Plot device has not been initialize"
270
227 plplot.pladv(self.__subpage)
271 plplot.pladv(self.__subpage)
228 plplot.plschr(0.0, self.__szchar)
272 plplot.plschr(0.0, self.__szchar)
229
273
230 if self.__xrangeIsTime:
274 if self.__xrangeIsTime:
231 plplot.pltimefmt("%H:%M")
275 plplot.pltimefmt("%H:%M")
232
276
233 self.__setColormap()
277 self.setColormap()
234 self.initPlot()
278 self.initPlot()
235
279
236 def initPlot(self):
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 plplot.plvpor(self.__xpos[0], self.__xpos[1], self.__ypos[0], self.__ypos[1])
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 plplot.plbox(self.__xopt, 0.0, 0, self.__yopt, 0.0, 0)
297 plplot.plbox(self.__xopt, 0.0, 0, self.__yopt, 0.0, 0)
243 plplot.pllab(self.xlabel, self.ylabel, self.title)
298 plplot.pllab(self.xlabel, self.ylabel, self.title)
244
299
245
300 def colorbarPlot(self):
246 def basicXYPlot(self):
301 data = numpy.arange(256)
247 pass
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 def basicXYwithErrorPlot(self):
321 def basicXYwithErrorPlot(self):
250 pass
322 pass
@@ -256,7 +328,7 class BasicGraph():
256 """
328 """
257 """
329 """
258
330
259 self.__setBox()
331 self.plotBox()
260 plplot.plimage(data, xmin, xmax, ymin, ymax, zmin, zmax, xmin, xmax, ymin, ymax)
332 plplot.plimage(data, xmin, xmax, ymin, ymax, zmin, zmax, xmin, xmax, ymin, ymax)
261
333
262
334
@@ -295,18 +367,14 class Graph():
295
367
296 def __init__(self):
368 def __init__(self):
297 raise
369 raise
298
299 def setup(self):
300 raise
301
302 def plotData(self):
303 raise
304
370
305 class Spectrum(Graph):
371 class Spectrum(Graph):
306
372
373
307 showColorbar = False
374 showColorbar = False
308 showPowerProfile = True
375 showPowerProfile = True
309
376
377 __szchar = 0.7
310
378
311 def __init__(self):
379 def __init__(self):
312
380
@@ -318,20 +386,20 class Spectrum(Graph):
318 self.graphObjDict[key] = specObj
386 self.graphObjDict[key] = specObj
319
387
320
388
321 def setup(self, title="", xlabel="", ylabel="", colormap="jet", showColorbar=False, showPowerProfile=False):
389 def setup(self, subpage, title="", xlabel="", ylabel="", colormap="jet", showColorbar=False, showPowerProfile=False):
322
390 """
323 xi = 0.12
391 """
324 xw = 0.86
325 xf = xi + xw
326
392
327 yi = 0.14
393 xi = 0.12; xw = 0.78; xf = xi + xw
328 yw = 0.80
394 yi = 0.14; yw = 0.80; yf = yi + yw
329 yf = yi + yw
330
395
331 xcmapi = xcmapf = 0.
396 xcmapi = xcmapf = 0.; xpowi = xpowf = 0.
332 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 specObj.setup(title,
403 specObj.setup(title,
336 xlabel,
404 xlabel,
337 ylabel,
405 ylabel,
@@ -342,15 +410,18 class Spectrum(Graph):
342
410
343 cmapObj = BasicGraph()
411 cmapObj = BasicGraph()
344 cmapObj.setName(key)
412 cmapObj.setName(key)
413 cmapObj.setSubpage(subpage)
414 cmapObj.setSzchar(self.__szchar)
415 cmapObj.setOpt("bc","bcmt")
345 cmapObj.setup(title="dBs",
416 cmapObj.setup(title="dBs",
346 xlabel="",
417 xlabel="",
347 ylabel="",
418 ylabel="",
348 colormap=colormap)
419 colormap=colormap)
349
420
350 self.graphObjDict[key] = cmapObject
421 self.graphObjDict[key] = cmapObj
351
422
352 xcmapi = 0.
423 xcmapi = 0.
353 xcmapw = 0.16
424 xcmapw = 0.05
354 xw -= xcmapw
425 xw -= xcmapw
355
426
356 if showPowerProfile:
427 if showPowerProfile:
@@ -358,45 +429,50 class Spectrum(Graph):
358
429
359 powObj = BasicGraph()
430 powObj = BasicGraph()
360 powObj.setName(key)
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 powObj.setup(title="Power Profile",
437 powObj.setup(title="Power Profile",
362 xlabel="dBs",
438 xlabel="dBs",
363 ylabel="")
439 ylabel="")
364
440
365 self.graphObjDict[key] = powObject
441 self.graphObjDict[key] = powObj
366
442
367 xpowi = 0.
443 xpowi = 0.
368 xpoww = 0.23
444 xpoww = 0.24
369 xw -= xpoww
445 xw -= xpoww
370
446
371 xf = xi + xw
447 xf = xi + xw
372 yf = yi + yw
448 yf = yi + yw
373 xcmapf = xf
449 xcmapf = xf
374
450
375 specObj.setScreenPos([xi, yf], [yi, yf])
451 specObj.setScreenPos([xi, xf], [yi, yf])
376
452
377 if showColorbar:
453 if showColorbar:
378 xcmapi = xf + 0.2
454 xcmapi = xf + 0.02
379 xcmapf = xcmapi + xcmapw
455 xcmapf = xcmapi + xcmapw
380 cmapObj.setScreenPos([xcmapi, ycmapf], [ycmapi, ycmapf])
456 cmapObj.setScreenPos([xcmapi, xcmapf], [yi, yf])
381
457
382 if showPowerProfile:
458 if showPowerProfile:
383 xpowi = xcmapf + 0.3
459 xpowi = xcmapf + 0.06
384 xpowf = xpowi + xpoww
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()
464 # specObj.initSubpage()
389
465 #
390 if showColorbar:
466 # if showColorbar:
391 cmapObj.initPlot()
467 # cmapObj.initPlot()
392
468 #
393 if showPowerProfile:
469 # if showPowerProfile:
394 powObj.initPlot()
470 # powObj.initPlot()
395
471
396 self.showColorbar = showColorbar
472 self.showColorbar = showColorbar
397 self.showPowerProfile = showPowerProfile
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 key = "spec"
477 key = "spec"
402 specObj = self.graphObjDict[key]
478 specObj = self.graphObjDict[key]
@@ -407,17 +483,57 class Spectrum(Graph):
407 key = "colorbar"
483 key = "colorbar"
408 if key in keyList:
484 if key in keyList:
409 cmapObj = self.graphObjDict[key]
485 cmapObj = self.graphObjDict[key]
410 cmapObj.setRanges([0., 1.], zrange, [0., 1.])
486 cmapObj.setRanges([0., 1.], zrange)
411
487
412 key = "powerprof"
488 key = "powerprof"
413 if key in keyList:
489 if key in keyList:
414 powObj = self.graphObjDict[key]
490 powObj = self.graphObjDict[key]
415 powObj.setRanges(zrange, yrange)
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 class CrossSpectrum(Graph):
537 class CrossSpectrum(Graph):
422
538
423 def __init__(self):
539 def __init__(self):
@@ -968,7 +1084,30 class PlotData():
968 if __name__ == '__main__':
1084 if __name__ == '__main__':
969
1085
970 import numpy
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 objPlot = PlotData()
1112 objPlot = PlotData()
974 objPlot.addGraph(1, "Frequency", "Height", "Channel A")
1113 objPlot.addGraph(1, "Frequency", "Height", "Channel A")
General Comments 0
You need to be logged in to leave comments. Login now