source: trunk/python/asapplotter.py@ 2625

Last change on this file since 2625 was 2608, checked in by Kana Sugimoto, 12 years ago

New Development: No

JIRA Issue: No (removal of debug comments)

Ready for Test: Yes

Interface Changes: No

What Interface Changed:

Test Programs:

Put in Release Notes: No

Module(s): asapplotter, flagplotter, sdplot, and sdflag

Description:

Removed debug comments and destructor of asapplotter.


  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 73.4 KB
RevLine 
[1824]1from asap.parameters import rcParams
2from asap.selector import selector
3from asap.scantable import scantable
[1862]4from asap.logging import asaplog, asaplog_post_dec
[1153]5import matplotlib.axes
[1556]6from matplotlib.font_manager import FontProperties
7from matplotlib.text import Text
[2535]8from matplotlib import _pylab_helpers
[1556]9
[1317]10import re
[203]11
[2150]12def new_asaplot(visible=None,**kwargs):
13 """
14 Returns a new asaplot instance based on the backend settings.
15 """
16 if visible == None:
17 visible = rcParams['plotter.gui']
18
19 backend=matplotlib.get_backend()
20 if not visible:
21 from asap.asaplot import asaplot
22 elif backend == 'TkAgg':
23 from asap.asaplotgui import asaplotgui as asaplot
24 elif backend == 'Qt4Agg':
25 from asap.asaplotgui_qt4 import asaplotgui as asaplot
26 elif backend == 'GTkAgg':
27 from asap.asaplotgui_gtk import asaplotgui as asaplot
28 else:
29 from asap.asaplot import asaplot
30 return asaplot(**kwargs)
31
[203]32class asapplotter:
[226]33 """
34 The ASAP plotter.
35 By default the plotter is set up to plot polarisations
36 'colour stacked' and scantables across panels.
[1858]37
38 .. note::
39
[226]40 Currenly it only plots 'spectra' not Tsys or
41 other variables.
[1858]42
[226]43 """
[1563]44 def __init__(self, visible=None , **kwargs):
[734]45 self._visible = rcParams['plotter.gui']
46 if visible is not None:
47 self._visible = visible
[2451]48 self._plotter = None
49 self._inikwg = kwargs
[710]50
[554]51 self._panelling = None
52 self._stacking = None
53 self.set_panelling()
54 self.set_stacking()
[377]55 self._rows = None
56 self._cols = None
[525]57 self._minmaxx = None
58 self._minmaxy = None
[710]59 self._datamask = None
[203]60 self._data = None
[607]61 self._lmap = None
[226]62 self._title = None
[257]63 self._ordinate = None
64 self._abcissa = None
[709]65 self._abcunit = None
[920]66 self._usermask = []
67 self._maskselection = None
68 self._selection = selector()
[1023]69 self._hist = rcParams['plotter.histogram']
[1556]70 self._fp = FontProperties()
[2037]71 self._margins = self.set_margin(refresh=False)
[1897]72 self._offset = None
[1981]73 self._startrow = 0
74 self._ipanel = -1
75 self._panelrows = []
[2053]76 self._headtext={'string': None, 'textobj': None}
[2451]77 self._colormap = None
78 self._linestyles = None
79 self._legendloc = None
[1023]80
[920]81 def _translate(self, instr):
[1910]82 keys = "s b i p t r".split()
[920]83 if isinstance(instr, str):
84 for key in keys:
85 if instr.lower().startswith(key):
86 return key
87 return None
88
[2535]89 @asaplog_post_dec
[2451]90 def _reload_plotter(self):
91 if self._plotter is not None:
[2535]92 #if not self._plotter.is_dead:
93 # # clear lines and axes
94 # try:
95 # self._plotter.clear()
96 # except: # Can't remove when already killed.
97 # pass
[2451]98 if self.casabar_exists():
99 del self._plotter.figmgr.casabar
100 self._plotter.quit()
101 del self._plotter
[2535]102 asaplog.push('Loading new plotter')
[2451]103 self._plotter = new_asaplot(self._visible,**self._inikwg)
104 self._plotter.figmgr.casabar=self._new_custombar()
105 # just to make sure they're set
106 self._plotter.palette(color=0,colormap=self._colormap,
107 linestyle=0,linestyles=self._linestyles)
108 self._plotter.legend(self._legendloc)
[710]109
[2173]110 def _new_custombar(self):
[1819]111 backend=matplotlib.get_backend()
[2168]112 if not self._visible:
113 return None
114 elif backend == "TkAgg":
[2155]115 from asap.customgui_tkagg import CustomToolbarTkAgg
[1819]116 return CustomToolbarTkAgg(self)
[2168]117 elif backend == "Qt4Agg":
118 from asap.customgui_qt4agg import CustomToolbarQT4Agg
119 return CustomToolbarQT4Agg(self)
[1995]120 return None
[1819]121
[2147]122 def casabar_exists(self):
123 if not hasattr(self._plotter.figmgr,'casabar'):
124 return False
125 elif self._plotter.figmgr.casabar:
126 return True
127 return False
128
[2453]129 def _assert_plotter(self,action="status",errmsg=None):
[2451]130 """
[2453]131 Check plot window status. Returns True if plot window is alive.
[2451]132 Parameters
[2453]133 action: An action to take if the plotter window is not alive.
134 ['status'|'reload'|'halt']
135 The action 'status' simply returns False if asaplot
136 is not alive. When action='reload', plot window is
137 reloaded and the method returns True. Finally, an
138 error is raised when action='halt'.
[2451]139 errmsg: An error (warning) message to send to the logger,
[2453]140 when plot window is not alive.
[2451]141 """
[2538]142 isAlive = (self._plotter is not None) and self._plotter._alive()
[2535]143 # More tests
[2538]144 #if isAlive:
145 # if self._plotter.figmgr:
146 # figmgr = self._plotter.figmgr
147 # figid = figmgr.num
148 # # Make sure figid=0 is what asapplotter expects.
149 # # It might be already destroied/overridden by matplotlib
150 # # commands or other plotting methods using asaplot.
151 # isAlive = _pylab_helpers.Gcf.has_fignum(figid) and \
152 # (figmgr == \
153 # _pylab_helpers.Gcf.get_fig_manager(figid))
154 # else:
155 # isAlive = False
[2535]156
157 if isAlive:
[2451]158 return True
159 # Plotter is not alive.
160 haltmsg = "Plotter window has not yet been loaded or is closed."
161 if type(errmsg)==str and len(errmsg) > 0:
162 haltmsg = errmsg
163
[2453]164 if action.upper().startswith("R"):
[2451]165 # reload plotter
166 self._reload_plotter()
167 return True
[2453]168 elif action.upper().startswith("H"):
[2451]169 # halt
170 asaplog.push(haltmsg)
171 asaplog.post("ERROR")
172 raise RuntimeError(haltmsg)
173 else:
174 if errmsg:
175 asaplog.push(errmsg)
176 asaplog.post("WARN")
177 return False
178
179
[1862]180 @asaplog_post_dec
[935]181 def plot(self, scan=None):
[203]182 """
[920]183 Plot a scantable.
[203]184 Parameters:
[920]185 scan: a scantable
[203]186 Note:
[920]187 If a scantable was specified in a previous call
[203]188 to plot, no argument has to be given to 'replot'
[920]189 NO checking is done that the abcissas of the scantable
[203]190 are consistent e.g. all 'channel' or all 'velocity' etc.
191 """
[2451]192 if not self._data and not scan:
193 msg = "Input is not a scantable"
194 raise TypeError(msg)
[1981]195 self._startrow = 0
196 self._ipanel = -1
[2056]197 self._reset_header()
[2451]198 self._panelrows = []
199
[2453]200 self._assert_plotter(action="reload")
[2147]201 if self.casabar_exists():
[1984]202 self._plotter.figmgr.casabar.set_pagecounter(1)
[2451]203
[600]204 self._plotter.hold()
[1945]205 #self._plotter.clear()
[1897]206 if scan:
207 self.set_data(scan, refresh=False)
[2451]208 self._plotter.palette(color=0,colormap=self._colormap,
209 linestyle=0,linestyles=self._linestyles)
210 self._plotter.legend(self._legendloc)
211
[920]212 self._plot(self._data)
[709]213 if self._minmaxy is not None:
214 self._plotter.set_limits(ylim=self._minmaxy)
[2147]215 if self.casabar_exists(): self._plotter.figmgr.casabar.enable_button()
[203]216 self._plotter.release()
[1153]217 self._plotter.tidy()
218 self._plotter.show(hardrefresh=False)
[203]219 return
220
[1572]221 def gca(self):
[2451]222 errmsg = "No axis to retun. Need to plot first."
[2453]223 if not self._assert_plotter(action="status",errmsg=errmsg):
[2451]224 return None
[1572]225 return self._plotter.figure.gca()
226
[1550]227 def refresh(self):
[1572]228 """Do a soft refresh"""
[2451]229 errmsg = "No figure to re-plot. Need to plot first."
[2453]230 self._assert_plotter(action="halt",errmsg=errmsg)
[2451]231
[1550]232 self._plotter.figure.show()
233
[1555]234 def create_mask(self, nwin=1, panel=0, color=None):
[1597]235 """
[1927]236 Interactively define a mask. It retruns a mask that is equivalent to
[1597]237 the one created manually with scantable.create_mask.
238 Parameters:
239 nwin: The number of mask windows to create interactively
240 default is 1.
241 panel: Which panel to use for mask selection. This is useful
242 if different IFs are spread over panels (default 0)
243 """
[2451]244 ## this method relies on already plotted figure
[2453]245 if not self._assert_plotter(action="status") or (self._data is None):
[2451]246 msg = "Cannot create mask interactively on plot. Can only create mask after plotting."
247 asaplog.push( msg )
248 asaplog.post( "ERROR" )
[1555]249 return []
[1547]250 outmask = []
[1549]251 self._plotter.subplot(panel)
252 xmin, xmax = self._plotter.axes.get_xlim()
[1548]253 marg = 0.05*(xmax-xmin)
[1549]254 self._plotter.axes.set_xlim(xmin-marg, xmax+marg)
[1550]255 self.refresh()
[1695]256
[1555]257 def cleanup(lines=False, texts=False, refresh=False):
258 if lines:
259 del self._plotter.axes.lines[-1]
260 if texts:
261 del self._plotter.axes.texts[-1]
262 if refresh:
263 self.refresh()
264
265 for w in xrange(nwin):
[1547]266 wpos = []
[1695]267 self.text(0.05,1.0, "Add start boundary",
[1555]268 coords="relative", fontsize=10)
269 point = self._plotter.get_point()
270 cleanup(texts=True)
271 if point is None:
272 continue
273 wpos.append(point[0])
[1695]274 self.axvline(wpos[0], color=color)
[1551]275 self.text(0.05,1.0, "Add end boundary", coords="relative", fontsize=10)
[1555]276 point = self._plotter.get_point()
277 cleanup(texts=True, lines=True)
278 if point is None:
279 self.refresh()
280 continue
281 wpos.append(point[0])
282 self.axvspan(wpos[0], wpos[1], alpha=0.1,
283 edgecolor=color, facecolor=color)
284 ymin, ymax = self._plotter.axes.get_ylim()
[1547]285 outmask.append(wpos)
[1153]286
[1555]287 self._plotter.axes.set_xlim(xmin, xmax)
288 self.refresh()
289 if len(outmask) > 0:
290 return self._data.create_mask(*outmask)
291 return []
292
[1153]293 # forwards to matplotlib axes
294 def text(self, *args, **kwargs):
[2453]295 self._assert_plotter(action="reload")
[1547]296 if kwargs.has_key("interactive"):
297 if kwargs.pop("interactive"):
298 pos = self._plotter.get_point()
299 args = tuple(pos)+args
[1153]300 self._axes_callback("text", *args, **kwargs)
[1547]301
[1358]302 text.__doc__ = matplotlib.axes.Axes.text.__doc__
[1559]303
[1153]304 def arrow(self, *args, **kwargs):
[2453]305 self._assert_plotter(action="reload")
[1547]306 if kwargs.has_key("interactive"):
307 if kwargs.pop("interactive"):
308 pos = self._plotter.get_region()
309 dpos = (pos[0][0], pos[0][1],
310 pos[1][0]-pos[0][0],
311 pos[1][1] - pos[0][1])
312 args = dpos + args
[1153]313 self._axes_callback("arrow", *args, **kwargs)
[1547]314
[1358]315 arrow.__doc__ = matplotlib.axes.Axes.arrow.__doc__
[1559]316
317 def annotate(self, text, xy=None, xytext=None, **kwargs):
[2453]318 self._assert_plotter(action="reload")
[1559]319 if kwargs.has_key("interactive"):
320 if kwargs.pop("interactive"):
321 xy = self._plotter.get_point()
322 xytext = self._plotter.get_point()
323 if not kwargs.has_key("arrowprops"):
324 kwargs["arrowprops"] = dict(arrowstyle="->")
325 self._axes_callback("annotate", text, xy, xytext, **kwargs)
326
327 annotate.__doc__ = matplotlib.axes.Axes.annotate.__doc__
328
[1153]329 def axvline(self, *args, **kwargs):
[2453]330 self._assert_plotter(action="reload")
[1547]331 if kwargs.has_key("interactive"):
332 if kwargs.pop("interactive"):
333 pos = self._plotter.get_point()
334 args = (pos[0],)+args
[1153]335 self._axes_callback("axvline", *args, **kwargs)
[1559]336
[1358]337 axvline.__doc__ = matplotlib.axes.Axes.axvline.__doc__
[1547]338
[1153]339 def axhline(self, *args, **kwargs):
[2453]340 self._assert_plotter(action="reload")
[1547]341 if kwargs.has_key("interactive"):
342 if kwargs.pop("interactive"):
343 pos = self._plotter.get_point()
344 args = (pos[1],)+args
[1153]345 self._axes_callback("axhline", *args, **kwargs)
[1559]346
[1358]347 axhline.__doc__ = matplotlib.axes.Axes.axhline.__doc__
[1547]348
[1153]349 def axvspan(self, *args, **kwargs):
[2453]350 self._assert_plotter(action="reload")
[1547]351 if kwargs.has_key("interactive"):
352 if kwargs.pop("interactive"):
353 pos = self._plotter.get_region()
354 dpos = (pos[0][0], pos[1][0])
355 args = dpos + args
[1153]356 self._axes_callback("axvspan", *args, **kwargs)
357 # hack to preventy mpl from redrawing the patch
358 # it seem to convert the patch into lines on every draw.
359 # This doesn't happen in a test script???
[1547]360 #del self._plotter.axes.patches[-1]
361
[1358]362 axvspan.__doc__ = matplotlib.axes.Axes.axvspan.__doc__
[1232]363
[1153]364 def axhspan(self, *args, **kwargs):
[2453]365 self._assert_plotter(action="reload")
[1547]366 if kwargs.has_key("interactive"):
367 if kwargs.pop("interactive"):
368 pos = self._plotter.get_region()
369 dpos = (pos[0][1], pos[1][1])
370 args = dpos + args
[1232]371 self._axes_callback("axhspan", *args, **kwargs)
[1153]372 # hack to preventy mpl from redrawing the patch
373 # it seem to convert the patch into lines on every draw.
374 # This doesn't happen in a test script???
[1547]375 #del self._plotter.axes.patches[-1]
[1559]376
[1358]377 axhspan.__doc__ = matplotlib.axes.Axes.axhspan.__doc__
[1153]378
379 def _axes_callback(self, axesfunc, *args, **kwargs):
[2453]380 self._assert_plotter(action="reload")
[1153]381 panel = 0
382 if kwargs.has_key("panel"):
383 panel = kwargs.pop("panel")
384 coords = None
385 if kwargs.has_key("coords"):
386 coords = kwargs.pop("coords")
387 if coords.lower() == 'world':
388 kwargs["transform"] = self._plotter.axes.transData
389 elif coords.lower() == 'relative':
390 kwargs["transform"] = self._plotter.axes.transAxes
391 self._plotter.subplot(panel)
392 self._plotter.axes.set_autoscale_on(False)
393 getattr(self._plotter.axes, axesfunc)(*args, **kwargs)
394 self._plotter.show(False)
395 self._plotter.axes.set_autoscale_on(True)
396 # end matplotlib.axes fowarding functions
397
[1862]398 @asaplog_post_dec
[1819]399 def set_data(self, scan, refresh=True):
400 """
[1824]401 Set a scantable to plot.
[1819]402 Parameters:
403 scan: a scantable
404 refresh: True (default) or False. If True, the plot is
[1824]405 replotted based on the new parameter setting(s).
[1819]406 Otherwise,the parameter(s) are set without replotting.
407 Note:
408 The user specified masks and data selections will be reset
409 if a new scantable is set. This method should be called before
[1824]410 setting data selections (set_selection) and/or masks (set_mask).
[1819]411 """
412 from asap import scantable
413 if isinstance(scan, scantable):
[2604]414 if (self._data is not None) and (scan != self._data):
415 del self._data
416 msg = "A new scantable is set to the plotter. "\
417 "The masks and data selections are reset."
418 asaplog.push( msg )
419 self._data = scan
420 # reset
421 self._reset()
[1819]422 else:
423 msg = "Input is not a scantable"
424 raise TypeError(msg)
[1547]425
[1819]426 # ranges become invalid when unit changes
427 if self._abcunit and self._abcunit != self._data.get_unit():
428 self._minmaxx = None
429 self._minmaxy = None
430 self._abcunit = self._data.get_unit()
431 self._datamask = None
432 if refresh: self.plot()
433
[1862]434 @asaplog_post_dec
[1819]435 def set_mode(self, stacking=None, panelling=None, refresh=True):
[203]436 """
[377]437 Set the plots look and feel, i.e. what you want to see on the plot.
[203]438 Parameters:
439 stacking: tell the plotter which variable to plot
[1217]440 as line colour overlays (default 'pol')
[203]441 panelling: tell the plotter which variable to plot
442 across multiple panels (default 'scan'
[1819]443 refresh: True (default) or False. If True, the plot is
[1824]444 replotted based on the new parameter setting(s).
[1819]445 Otherwise,the parameter(s) are set without replotting.
[203]446 Note:
447 Valid modes are:
448 'beam' 'Beam' 'b': Beams
449 'if' 'IF' 'i': IFs
450 'pol' 'Pol' 'p': Polarisations
451 'scan' 'Scan' 's': Scans
452 'time' 'Time' 't': Times
[1989]453 'row' 'Row' 'r': Rows
454 When either 'stacking' or 'panelling' is set to 'row',
455 the other parameter setting is ignored.
[203]456 """
[753]457 msg = "Invalid mode"
458 if not self.set_panelling(panelling) or \
459 not self.set_stacking(stacking):
[1859]460 raise TypeError(msg)
[1989]461 #if self._panelling == 'r':
462 # self._stacking = '_r'
463 #if self._stacking == 'r':
464 # self._panelling = '_r'
[1819]465 if refresh and self._data: self.plot(self._data)
[203]466 return
467
[554]468 def set_panelling(self, what=None):
[1858]469 """Set the 'panelling' mode i.e. which type of spectra should be
470 spread across different panels.
471 """
472
[554]473 mode = what
474 if mode is None:
475 mode = rcParams['plotter.panelling']
476 md = self._translate(mode)
[203]477 if md:
[554]478 self._panelling = md
[226]479 self._title = None
[1989]480 #if md == 'r':
481 # self._stacking = '_r'
[1981]482 # you need to reset counters for multi page plotting
483 self._reset_counters()
[203]484 return True
485 return False
486
[1819]487 def set_layout(self,rows=None,cols=None,refresh=True):
[377]488 """
489 Set the multi-panel layout, i.e. how many rows and columns plots
490 are visible.
491 Parameters:
492 rows: The number of rows of plots
493 cols: The number of columns of plots
[1819]494 refresh: True (default) or False. If True, the plot is
[1824]495 replotted based on the new parameter setting(s).
[1819]496 Otherwise,the parameter(s) are set without replotting.
[377]497 Note:
498 If no argument is given, the potter reverts to its auto-plot
499 behaviour.
500 """
501 self._rows = rows
502 self._cols = cols
[1819]503 if refresh and self._data: self.plot(self._data)
[377]504 return
505
[709]506 def set_stacking(self, what=None):
[1858]507 """Set the 'stacking' mode i.e. which type of spectra should be
508 overlayed.
509 """
[554]510 mode = what
[709]511 if mode is None:
512 mode = rcParams['plotter.stacking']
[554]513 md = self._translate(mode)
[203]514 if md:
515 self._stacking = md
[226]516 self._lmap = None
[1989]517 #if md == 'r':
518 # self._panelling = '_r'
[1981]519 # you need to reset counters for multi page plotting
520 self._reset_counters()
[203]521 return True
522 return False
523
[1981]524 def _reset_counters(self):
525 self._startrow = 0
526 self._ipanel = -1
527 self._panelrows = []
528
[1897]529 def set_range(self,xstart=None,xend=None,ystart=None,yend=None,refresh=True, offset=None):
[203]530 """
531 Set the range of interest on the abcissa of the plot
532 Parameters:
[525]533 [x,y]start,[x,y]end: The start and end points of the 'zoom' window
[1819]534 refresh: True (default) or False. If True, the plot is
[1824]535 replotted based on the new parameter setting(s).
[1819]536 Otherwise,the parameter(s) are set without replotting.
[1897]537 offset: shift the abcissa by the given amount. The abcissa label will
538 have '(relative)' appended to it.
[203]539 Note:
540 These become non-sensical when the unit changes.
541 use plotter.set_range() without parameters to reset
542
543 """
[1897]544 self._offset = offset
[525]545 if xstart is None and xend is None:
546 self._minmaxx = None
[600]547 else:
548 self._minmaxx = [xstart,xend]
[525]549 if ystart is None and yend is None:
550 self._minmaxy = None
[600]551 else:
[709]552 self._minmaxy = [ystart,yend]
[1819]553 if refresh and self._data: self.plot(self._data)
[203]554 return
[709]555
[1819]556 def set_legend(self, mp=None, fontsize = None, mode = 0, refresh=True):
[203]557 """
558 Specify a mapping for the legend instead of using the default
559 indices:
560 Parameters:
[1101]561 mp: a list of 'strings'. This should have the same length
562 as the number of elements on the legend and then maps
563 to the indeces in order. It is possible to uses latex
564 math expression. These have to be enclosed in r'',
565 e.g. r'$x^{2}$'
566 fontsize: The font size of the label (default None)
567 mode: where to display the legend
568 Any other value for loc else disables the legend:
[1096]569 0: auto
570 1: upper right
571 2: upper left
572 3: lower left
573 4: lower right
574 5: right
575 6: center left
576 7: center right
577 8: lower center
578 9: upper center
579 10: center
[1819]580 refresh: True (default) or False. If True, the plot is
[1824]581 replotted based on the new parameter setting(s).
[1819]582 Otherwise,the parameter(s) are set without replotting.
[203]583
584 Example:
[485]585 If the data has two IFs/rest frequencies with index 0 and 1
[203]586 for CO and SiO:
587 plotter.set_stacking('i')
[710]588 plotter.set_legend(['CO','SiO'])
[203]589 plotter.plot()
[710]590 plotter.set_legend([r'$^{12}CO$', r'SiO'])
[203]591 """
592 self._lmap = mp
[2451]593 #self._plotter.legend(mode)
594 self._legendloc = mode
[1101]595 if isinstance(fontsize, int):
596 from matplotlib import rc as rcp
597 rcp('legend', fontsize=fontsize)
[1819]598 if refresh and self._data: self.plot(self._data)
[226]599 return
600
[1819]601 def set_title(self, title=None, fontsize=None, refresh=True):
[710]602 """
[2451]603 Set the title of sub-plots. If multiple sub-plots are plotted,
[710]604 multiple titles have to be specified.
[1819]605 Parameters:
[2451]606 title: a list of titles of sub-plots.
607 fontsize: a font size of titles (integer)
[1819]608 refresh: True (default) or False. If True, the plot is
[1824]609 replotted based on the new parameter setting(s).
[1819]610 Otherwise,the parameter(s) are set without replotting.
[710]611 Example:
612 # two panels are visible on the plotter
[2451]613 plotter.set_title(['First Panel','Second Panel'])
[710]614 """
[226]615 self._title = title
[1101]616 if isinstance(fontsize, int):
617 from matplotlib import rc as rcp
618 rcp('axes', titlesize=fontsize)
[1819]619 if refresh and self._data: self.plot(self._data)
[226]620 return
621
[1819]622 def set_ordinate(self, ordinate=None, fontsize=None, refresh=True):
[710]623 """
624 Set the y-axis label of the plot. If multiple panels are plotted,
625 multiple labels have to be specified.
[1021]626 Parameters:
627 ordinate: a list of ordinate labels. None (default) let
628 data determine the labels
[2451]629 fontsize: a font size of vertical axis labels (integer)
[1819]630 refresh: True (default) or False. If True, the plot is
[1824]631 replotted based on the new parameter setting(s).
[1819]632 Otherwise,the parameter(s) are set without replotting.
[710]633 Example:
634 # two panels are visible on the plotter
[2451]635 plotter.set_ordinate(['First Y-Axis','Second Y-Axis'])
[710]636 """
[257]637 self._ordinate = ordinate
[1101]638 if isinstance(fontsize, int):
639 from matplotlib import rc as rcp
640 rcp('axes', labelsize=fontsize)
641 rcp('ytick', labelsize=fontsize)
[1819]642 if refresh and self._data: self.plot(self._data)
[257]643 return
644
[1819]645 def set_abcissa(self, abcissa=None, fontsize=None, refresh=True):
[710]646 """
647 Set the x-axis label of the plot. If multiple panels are plotted,
648 multiple labels have to be specified.
[1021]649 Parameters:
650 abcissa: a list of abcissa labels. None (default) let
651 data determine the labels
[2451]652 fontsize: a font size of horizontal axis labels (integer)
[1819]653 refresh: True (default) or False. If True, the plot is
[1824]654 replotted based on the new parameter setting(s).
[1819]655 Otherwise,the parameter(s) are set without replotting.
[710]656 Example:
657 # two panels are visible on the plotter
[2451]658 plotter.set_ordinate(['First X-Axis','Second X-Axis'])
[710]659 """
[257]660 self._abcissa = abcissa
[1101]661 if isinstance(fontsize, int):
662 from matplotlib import rc as rcp
663 rcp('axes', labelsize=fontsize)
664 rcp('xtick', labelsize=fontsize)
[1819]665 if refresh and self._data: self.plot(self._data)
[257]666 return
667
[1819]668 def set_colors(self, colmap, refresh=True):
[377]669 """
[1217]670 Set the colours to be used. The plotter will cycle through
671 these colours when lines are overlaid (stacking mode).
[1021]672 Parameters:
[1217]673 colmap: a list of colour names
[1819]674 refresh: True (default) or False. If True, the plot is
[1824]675 replotted based on the new parameter setting(s).
[1819]676 Otherwise,the parameter(s) are set without replotting.
[710]677 Example:
[2451]678 plotter.set_colors('red green blue')
[710]679 # If for example four lines are overlaid e.g I Q U V
680 # 'I' will be 'red', 'Q' will be 'green', U will be 'blue'
681 # and 'V' will be 'red' again.
682 """
[2451]683 #if isinstance(colmap,str):
684 # colmap = colmap.split()
685 #self._plotter.palette(0, colormap=colmap)
686 self._colormap = colmap
[1819]687 if refresh and self._data: self.plot(self._data)
[710]688
[1217]689 # alias for english speakers
690 set_colours = set_colors
691
[1819]692 def set_histogram(self, hist=True, linewidth=None, refresh=True):
[1021]693 """
694 Enable/Disable histogram-like plotting.
695 Parameters:
696 hist: True (default) or False. The fisrt default
697 is taken from the .asaprc setting
698 plotter.histogram
[2451]699 linewidth: a line width
[1819]700 refresh: True (default) or False. If True, the plot is
[1824]701 replotted based on the new parameter setting(s).
[1819]702 Otherwise,the parameter(s) are set without replotting.
[1021]703 """
[1023]704 self._hist = hist
[1101]705 if isinstance(linewidth, float) or isinstance(linewidth, int):
706 from matplotlib import rc as rcp
707 rcp('lines', linewidth=linewidth)
[1819]708 if refresh and self._data: self.plot(self._data)
[1023]709
[1819]710 def set_linestyles(self, linestyles=None, linewidth=None, refresh=True):
[710]711 """
[734]712 Set the linestyles to be used. The plotter will cycle through
713 these linestyles when lines are overlaid (stacking mode) AND
714 only one color has been set.
[710]715 Parameters:
[2451]716 linestyles: a list of linestyles to use.
[710]717 'line', 'dashed', 'dotted', 'dashdot',
718 'dashdotdot' and 'dashdashdot' are
719 possible
[2451]720 linewidth: a line width
[1819]721 refresh: True (default) or False. If True, the plot is
[1824]722 replotted based on the new parameter setting(s).
[1819]723 Otherwise,the parameter(s) are set without replotting.
[710]724 Example:
[2451]725 plotter.set_colors('black')
726 plotter.set_linestyles('line dashed dotted dashdot')
[710]727 # If for example four lines are overlaid e.g I Q U V
728 # 'I' will be 'solid', 'Q' will be 'dashed',
729 # U will be 'dotted' and 'V' will be 'dashdot'.
730 """
[2451]731 #if isinstance(linestyles,str):
732 # linestyles = linestyles.split()
733 #self._plotter.palette(color=0,linestyle=0,linestyles=linestyles)
734 self._linestyles = linestyles
[1101]735 if isinstance(linewidth, float) or isinstance(linewidth, int):
736 from matplotlib import rc as rcp
737 rcp('lines', linewidth=linewidth)
[1819]738 if refresh and self._data: self.plot(self._data)
[710]739
[1819]740 def set_font(self, refresh=True,**kwargs):
[1101]741 """
742 Set font properties.
743 Parameters:
744 family: one of 'sans-serif', 'serif', 'cursive', 'fantasy', 'monospace'
745 style: one of 'normal' (or 'roman'), 'italic' or 'oblique'
746 weight: one of 'normal or 'bold'
747 size: the 'general' font size, individual elements can be adjusted
748 seperately
[1819]749 refresh: True (default) or False. If True, the plot is
[1824]750 replotted based on the new parameter setting(s).
[1819]751 Otherwise,the parameter(s) are set without replotting.
[1101]752 """
753 from matplotlib import rc as rcp
[1547]754 fdict = {}
755 for k,v in kwargs.iteritems():
756 if v:
757 fdict[k] = v
[1556]758 self._fp = FontProperties(**fdict)
[1819]759 if refresh and self._data: self.plot(self._data)
[1101]760
[2037]761 def set_margin(self,margin=[],refresh=True):
[1819]762 """
[2037]763 Set margins between subplots and plot edges.
[1819]764 Parameters:
[2037]765 margin: a list of margins in figure coordinate (0-1),
[1824]766 i.e., fraction of the figure width or height.
[1819]767 The order of elements should be:
768 [left, bottom, right, top, horizontal space btw panels,
[1824]769 vertical space btw panels].
[1819]770 refresh: True (default) or False. If True, the plot is
[1824]771 replotted based on the new parameter setting(s).
[1819]772 Otherwise,the parameter(s) are set without replotting.
773 Note
[2037]774 * When margin is not specified, the values are reset to the defaults
[1819]775 of matplotlib.
[1824]776 * If any element is set to be None, the current value is adopted.
[1819]777 """
[2037]778 if margin == []: self._margins=self._reset_margin()
[1824]779 else:
[2037]780 self._margins=[None]*6
781 self._margins[0:len(margin)]=margin
782 #print "panel margin set to ",self._margins
[1819]783 if refresh and self._data: self.plot(self._data)
784
[2037]785 def _reset_margin(self):
[1819]786 ks=map(lambda x: 'figure.subplot.'+x,
787 ['left','bottom','right','top','hspace','wspace'])
788 return map(matplotlib.rcParams.get,ks)
789
[1259]790 def plot_lines(self, linecat=None, doppler=0.0, deltachan=10, rotate=90.0,
[1146]791 location=None):
792 """
[1158]793 Plot a line catalog.
794 Parameters:
795 linecat: the linecatalog to plot
[1168]796 doppler: the velocity shift to apply to the frequencies
[1158]797 deltachan: the number of channels to include each side of the
798 line to determine a local maximum/minimum
[1927]799 rotate: the rotation (in degrees) for the text label (default 90.0)
[1158]800 location: the location of the line annotation from the 'top',
801 'bottom' or alternate (None - the default)
[1165]802 Notes:
803 If the spectrum is flagged no line will be drawn in that location.
[1146]804 """
[2451]805 errmsg = "Cannot plot spectral lines. Need to plot scantable first."
[2453]806 self._assert_plotter(action="halt",errmsg=errmsg)
[1259]807 if not self._data:
808 raise RuntimeError("No scantable has been plotted yet.")
[1146]809 from asap._asap import linecatalog
[1259]810 if not isinstance(linecat, linecatalog):
811 raise ValueError("'linecat' isn't of type linecatalog.")
812 if not self._data.get_unit().endswith("Hz"):
813 raise RuntimeError("Can only overlay linecatalogs when data is in frequency.")
[1739]814 from numpy import ma
[1146]815 for j in range(len(self._plotter.subplots)):
816 self._plotter.subplot(j)
817 lims = self._plotter.axes.get_xlim()
[1153]818 for row in range(linecat.nrow()):
[1259]819 # get_frequency returns MHz
820 base = { "GHz": 1000.0, "MHz": 1.0, "Hz": 1.0e-6 }
821 restf = linecat.get_frequency(row)/base[self._data.get_unit()]
[1165]822 c = 299792.458
[1174]823 freq = restf*(1.0-doppler/c)
[1146]824 if lims[0] < freq < lims[1]:
825 if location is None:
826 loc = 'bottom'
[1153]827 if row%2: loc='top'
[1146]828 else: loc = location
[1153]829 maxys = []
830 for line in self._plotter.axes.lines:
831 v = line._x
832 asc = v[0] < v[-1]
833
834 idx = None
835 if not asc:
836 if v[len(v)-1] <= freq <= v[0]:
837 i = len(v)-1
838 while i>=0 and v[i] < freq:
839 idx = i
840 i-=1
841 else:
842 if v[0] <= freq <= v[len(v)-1]:
843 i = 0
844 while i<len(v) and v[i] < freq:
845 idx = i
846 i+=1
847 if idx is not None:
848 lower = idx - deltachan
849 upper = idx + deltachan
850 if lower < 0: lower = 0
851 if upper > len(v): upper = len(v)
852 s = slice(lower, upper)
[1167]853 y = line._y[s]
[1165]854 maxy = ma.maximum(y)
855 if isinstance( maxy, float):
856 maxys.append(maxy)
[1164]857 if len(maxys):
858 peak = max(maxys)
[1165]859 if peak > self._plotter.axes.get_ylim()[1]:
860 loc = 'bottom'
[1164]861 else:
862 continue
[1157]863 self._plotter.vline_with_label(freq, peak,
864 linecat.get_name(row),
865 location=loc, rotate=rotate)
[1153]866 self._plotter.show(hardrefresh=False)
[1146]867
[1153]868
[710]869 def save(self, filename=None, orientation=None, dpi=None):
870 """
[1927]871 Save the plot to a file. The known formats are 'png', 'ps', 'eps'.
[377]872 Parameters:
873 filename: The name of the output file. This is optional
874 and autodetects the image format from the file
875 suffix. If non filename is specified a file
876 called 'yyyymmdd_hhmmss.png' is created in the
877 current directory.
[709]878 orientation: optional parameter for postscript only (not eps).
879 'landscape', 'portrait' or None (default) are valid.
880 If None is choosen for 'ps' output, the plot is
881 automatically oriented to fill the page.
[710]882 dpi: The dpi of the output non-ps plot
[377]883 """
[2451]884 errmsg = "Cannot save figure. Need to plot first."
[2453]885 self._assert_plotter(action="halt",errmsg=errmsg)
[2451]886
[709]887 self._plotter.save(filename,orientation,dpi)
[377]888 return
[709]889
[1862]890 @asaplog_post_dec
[1819]891 def set_mask(self, mask=None, selection=None, refresh=True):
[525]892 """
[734]893 Set a plotting mask for a specific polarization.
[2451]894 This is useful for masking out 'noise' Pangle outside a source.
[734]895 Parameters:
[920]896 mask: a mask from scantable.create_mask
897 selection: the spectra to apply the mask to.
[1819]898 refresh: True (default) or False. If True, the plot is
[1824]899 replotted based on the new parameter setting(s).
[1819]900 Otherwise,the parameter(s) are set without replotting.
[734]901 Example:
[920]902 select = selector()
[2451]903 select.setpolstrings('Pangle')
[920]904 plotter.set_mask(mymask, select)
[734]905 """
[710]906 if not self._data:
[920]907 msg = "Can only set mask after a first call to plot()"
[1859]908 raise RuntimeError(msg)
[920]909 if len(mask):
910 if isinstance(mask, list) or isinstance(mask, tuple):
911 self._usermask = array(mask)
[710]912 else:
[920]913 self._usermask = mask
914 if mask is None and selection is None:
915 self._usermask = []
916 self._maskselection = None
917 if isinstance(selection, selector):
[947]918 self._maskselection = {'b': selection.get_beams(),
919 's': selection.get_scans(),
920 'i': selection.get_ifs(),
921 'p': selection.get_pols(),
[920]922 't': [] }
[710]923 else:
[920]924 self._maskselection = None
[1819]925 if refresh: self.plot(self._data)
[710]926
[709]927 def _slice_indeces(self, data):
928 mn = self._minmaxx[0]
929 mx = self._minmaxx[1]
930 asc = data[0] < data[-1]
931 start=0
932 end = len(data)-1
933 inc = 1
934 if not asc:
935 start = len(data)-1
936 end = 0
937 inc = -1
938 # find min index
[1819]939 #while start > 0 and data[start] < mn:
940 # start+= inc
941 minind=start
942 for ind in xrange(start,end+inc,inc):
943 if data[ind] > mn: break
944 minind=ind
[709]945 # find max index
[1819]946 #while end > 0 and data[end] > mx:
947 # end-=inc
948 #if end > 0: end +=1
949 maxind=end
950 for ind in xrange(end,start-inc,-inc):
951 if data[ind] < mx: break
952 maxind=ind
953 start=minind
954 end=maxind
[709]955 if start > end:
[1819]956 return end,start+1
957 elif start < end:
958 return start,end+1
959 else:
960 return start,end
[709]961
[710]962 def _reset(self):
[920]963 self._usermask = []
[710]964 self._usermaskspectra = None
[1897]965 self._offset = None
[920]966 self.set_selection(None, False)
[2051]967 self._reset_header()
[920]968
[2051]969 def _reset_header(self):
[2053]970 self._headtext={'string': None, 'textobj': None}
[2051]971
[920]972 def _plot(self, scan):
[947]973 savesel = scan.get_selection()
974 sel = savesel + self._selection
[1910]975 order = self._get_sortstring([self._panelling,self._stacking])
976 if order:
977 sel.set_order(order)
[947]978 scan.set_selection(sel)
[920]979 d = {'b': scan.getbeam, 's': scan.getscan,
[1949]980 'i': scan.getif, 'p': scan.getpol, 't': scan.get_time,
[1989]981 'r': int}#, '_r': int}
[920]982
[1148]983 polmodes = dict(zip(self._selection.get_pols(),
984 self._selection.get_poltypes()))
985 # this returns either a tuple of numbers or a length (ncycles)
986 # convert this into lengths
987 n0,nstack0 = self._get_selected_n(scan)
988 if isinstance(n0, int): n = n0
[1175]989 else: n = len(n0)
[1148]990 if isinstance(nstack0, int): nstack = nstack0
[1175]991 else: nstack = len(nstack0)
[1989]992 # In case of row stacking
993 rowstack = False
994 titlemode = self._panelling
995 if self._stacking == "r" and self._panelling != "r":
996 rowstack = True
997 titlemode = '_r'
[1913]998 nptot = n
[1582]999 maxpanel, maxstack = 16,16
[1913]1000 if nstack > maxstack:
1001 msg ="Scan to be overlayed contains more than %d selections.\n" \
1002 "Selecting first %d selections..." % (maxstack, maxstack)
[920]1003 asaplog.push(msg)
[1861]1004 asaplog.post('WARN')
[998]1005 nstack = min(nstack,maxstack)
[2038]1006 #n = min(n-self._ipanel-1,maxpanel)
1007 n = n-self._ipanel-1
[2011]1008
1009 ganged = False
[920]1010 if n > 1:
1011 ganged = rcParams['plotter.ganged']
[1819]1012 if self._panelling == 'i':
1013 ganged = False
[920]1014 if self._rows and self._cols:
1015 n = min(n,self._rows*self._cols)
1016 self._plotter.set_panels(rows=self._rows,cols=self._cols,
[2277]1017 nplots=n,margin=self._margins,ganged=ganged)
[920]1018 else:
[2038]1019 n = min(n,maxpanel)
[2277]1020 self._plotter.set_panels(rows=n,cols=0,nplots=n,margin=self._margins,ganged=ganged)
[920]1021 else:
[2037]1022 self._plotter.set_panels(margin=self._margins)
[1913]1023 #r = 0
[1981]1024 r = self._startrow
[920]1025 nr = scan.nrow()
1026 a0,b0 = -1,-1
1027 allxlim = []
[1018]1028 allylim = []
[1981]1029 #newpanel=True
1030 newpanel=False
[920]1031 panelcount,stackcount = 0,0
[1981]1032 # If this is not the first page
1033 if r > 0:
1034 # panelling value of the prev page
1035 a0 = d[self._panelling](r-1)
1036 # set the initial stackcount large not to plot
1037 # the start row automatically
1038 stackcount = nstack
1039
[1002]1040 while r < nr:
[920]1041 a = d[self._panelling](r)
1042 b = d[self._stacking](r)
1043 if a > a0 and panelcount < n:
1044 if n > 1:
1045 self._plotter.subplot(panelcount)
1046 self._plotter.palette(0)
1047 #title
1048 xlab = self._abcissa and self._abcissa[panelcount] \
1049 or scan._getabcissalabel()
[1897]1050 if self._offset and not self._abcissa:
1051 xlab += " (relative)"
[920]1052 ylab = self._ordinate and self._ordinate[panelcount] \
1053 or scan._get_ordinate_label()
[1547]1054 self._plotter.set_axes('xlabel', xlab)
1055 self._plotter.set_axes('ylabel', ylab)
[1989]1056 #lbl = self._get_label(scan, r, self._panelling, self._title)
1057 lbl = self._get_label(scan, r, titlemode, self._title)
[920]1058 if isinstance(lbl, list) or isinstance(lbl, tuple):
1059 if 0 <= panelcount < len(lbl):
1060 lbl = lbl[panelcount]
1061 else:
1062 # get default label
[1989]1063 #lbl = self._get_label(scan, r, self._panelling, None)
1064 lbl = self._get_label(scan, r, titlemode, None)
[920]1065 self._plotter.set_axes('title',lbl)
1066 newpanel = True
[1913]1067 stackcount = 0
[920]1068 panelcount += 1
[1981]1069 # save the start row to plot this panel for future revisit.
1070 if self._panelling != 'r' and \
1071 len(self._panelrows) < self._ipanel+1+panelcount:
1072 self._panelrows += [r]
1073
[1944]1074 #if (b > b0 or newpanel) and stackcount < nstack:
[2277]1075 if stackcount < nstack and (newpanel or rowstack or (a == a0 and b > b0)):
[920]1076 y = []
1077 if len(polmodes):
1078 y = scan._getspectrum(r, polmodes[scan.getpol(r)])
1079 else:
1080 y = scan._getspectrum(r)
[1995]1081 # flag application
1082 mr = scan._getflagrow(r)
[1739]1083 from numpy import ma, array
[1995]1084 if mr:
1085 y = ma.masked_array(y,mask=mr)
1086 else:
1087 m = scan._getmask(r)
1088 from numpy import logical_not, logical_and
1089 if self._maskselection and len(self._usermask) == len(m):
[2277]1090 if d[self._stacking](r) in self._maskselection[self._stacking]:
[1995]1091 m = logical_and(m, self._usermask)
[2277]1092 y = ma.masked_array(y,mask=logical_not(array(m,copy=False)))
[1995]1093
[1897]1094 x = array(scan._getabcissa(r))
1095 if self._offset:
1096 x += self._offset
[920]1097 if self._minmaxx is not None:
1098 s,e = self._slice_indeces(x)
1099 x = x[s:e]
1100 y = y[s:e]
[1096]1101 if len(x) > 1024 and rcParams['plotter.decimate']:
1102 fac = len(x)/1024
[920]1103 x = x[::fac]
1104 y = y[::fac]
1105 llbl = self._get_label(scan, r, self._stacking, self._lmap)
1106 if isinstance(llbl, list) or isinstance(llbl, tuple):
1107 if 0 <= stackcount < len(llbl):
1108 # use user label
1109 llbl = llbl[stackcount]
1110 else:
1111 # get default label
1112 llbl = self._get_label(scan, r, self._stacking, None)
1113 self._plotter.set_line(label=llbl)
[1023]1114 plotit = self._plotter.plot
1115 if self._hist: plotit = self._plotter.hist
[1995]1116 if len(x) > 0 and not mr:
[1146]1117 plotit(x,y)
1118 xlim= self._minmaxx or [min(x),max(x)]
1119 allxlim += xlim
1120 ylim= self._minmaxy or [ma.minimum(y),ma.maximum(y)]
1121 allylim += ylim
[1819]1122 else:
1123 xlim = self._minmaxx or []
1124 allxlim += xlim
1125 ylim= self._minmaxy or []
1126 allylim += ylim
[920]1127 stackcount += 1
[1981]1128 a0=a
1129 b0=b
[920]1130 # last in colour stack -> autoscale x
[1819]1131 if stackcount == nstack and len(allxlim) > 0:
[920]1132 allxlim.sort()
[1819]1133 self._plotter.subplots[panelcount-1]['axes'].set_xlim([allxlim[0],allxlim[-1]])
[1989]1134 if ganged:
1135 allxlim = [allxlim[0],allxlim[-1]]
1136 else:
1137 # clear
1138 allxlim =[]
[920]1139
1140 newpanel = False
[1981]1141 #a0=a
1142 #b0=b
[920]1143 # ignore following rows
[1981]1144 if (panelcount == n and stackcount == nstack) or (r == nr-1):
[1018]1145 # last panel -> autoscale y if ganged
[1989]1146 #if rcParams['plotter.ganged'] and len(allylim) > 0:
1147 if ganged and len(allylim) > 0:
[1018]1148 allylim.sort()
1149 self._plotter.set_limits(ylim=[allylim[0],allylim[-1]])
[998]1150 break
[920]1151 r+=1 # next row
[1981]1152
1153 # save the current counter for multi-page plotting
1154 self._startrow = r+1
1155 self._ipanel += panelcount
[2147]1156 if self.casabar_exists():
[1981]1157 if self._ipanel >= nptot-1:
[1913]1158 self._plotter.figmgr.casabar.disable_next()
1159 else:
1160 self._plotter.figmgr.casabar.enable_next()
[1981]1161 if self._ipanel + 1 - panelcount > 0:
1162 self._plotter.figmgr.casabar.enable_prev()
1163 else:
1164 self._plotter.figmgr.casabar.disable_prev()
1165
[947]1166 #reset the selector to the scantable's original
1167 scan.set_selection(savesel)
[1824]1168
[1819]1169 #temporary switch-off for older matplotlib
1170 #if self._fp is not None:
1171 if self._fp is not None and getattr(self._plotter.figure,'findobj',False):
[1556]1172 for o in self._plotter.figure.findobj(Text):
1173 o.set_fontproperties(self._fp)
[920]1174
[1910]1175 def _get_sortstring(self, lorders):
1176 d0 = {'s': 'SCANNO', 'b': 'BEAMNO', 'i':'IFNO',
1177 'p': 'POLNO', 'c': 'CYCLENO', 't' : 'TIME', 'r':None, '_r':None }
[1944]1178 if not (type(lorders) == list) and not (type(lorders) == tuple):
[1910]1179 return None
1180 if len(lorders) > 0:
1181 lsorts = []
1182 for order in lorders:
[1989]1183 if order == "r":
1184 # don't sort if row panelling/stacking
1185 return None
[1910]1186 ssort = d0[order]
1187 if ssort:
1188 lsorts.append(ssort)
1189 return lsorts
1190 return None
1191
[1582]1192 def set_selection(self, selection=None, refresh=True, **kw):
[1819]1193 """
1194 Parameters:
1195 selection: a selector object (default unset the selection)
1196 refresh: True (default) or False. If True, the plot is
[1824]1197 replotted based on the new parameter setting(s).
[1819]1198 Otherwise,the parameter(s) are set without replotting.
1199 """
[1582]1200 if selection is None:
1201 # reset
1202 if len(kw) == 0:
1203 self._selection = selector()
1204 else:
1205 # try keywords
1206 for k in kw:
1207 if k not in selector.fields:
1208 raise KeyError("Invalid selection key '%s', valid keys are %s" % (k, selector.fields))
1209 self._selection = selector(**kw)
1210 elif isinstance(selection, selector):
1211 self._selection = selection
1212 else:
1213 raise TypeError("'selection' is not of type selector")
1214
[1910]1215 order = self._get_sortstring([self._panelling,self._stacking])
1216 if order:
1217 self._selection.set_order(order)
[1819]1218 if refresh and self._data: self.plot(self._data)
[920]1219
1220 def _get_selected_n(self, scan):
[1148]1221 d1 = {'b': scan.getbeamnos, 's': scan.getscannos,
[1910]1222 'i': scan.getifnos, 'p': scan.getpolnos, 't': scan.ncycle,
[1989]1223 'r': scan.nrow}#, '_r': False}
[1148]1224 d2 = { 'b': self._selection.get_beams(),
1225 's': self._selection.get_scans(),
1226 'i': self._selection.get_ifs(),
1227 'p': self._selection.get_pols(),
[1910]1228 't': self._selection.get_cycles(),
[1989]1229 'r': False}#, '_r': 1}
[920]1230 n = d2[self._panelling] or d1[self._panelling]()
1231 nstack = d2[self._stacking] or d1[self._stacking]()
[1989]1232 # handle row panelling/stacking
1233 if self._panelling == 'r':
1234 nstack = 1
1235 elif self._stacking == 'r':
1236 n = 1
[920]1237 return n,nstack
1238
1239 def _get_label(self, scan, row, mode, userlabel=None):
[1153]1240 if isinstance(userlabel, list) and len(userlabel) == 0:
1241 userlabel = " "
[947]1242 pms = dict(zip(self._selection.get_pols(),self._selection.get_poltypes()))
[920]1243 if len(pms):
1244 poleval = scan._getpollabel(scan.getpol(row),pms[scan.getpol(row)])
1245 else:
1246 poleval = scan._getpollabel(scan.getpol(row),scan.poltype())
1247 d = {'b': "Beam "+str(scan.getbeam(row)),
[1819]1248 #'s': scan._getsourcename(row),
1249 's': "Scan "+str(scan.getscan(row))+\
1250 " ("+str(scan._getsourcename(row))+")",
[920]1251 'i': "IF"+str(scan.getif(row)),
[964]1252 'p': poleval,
[1910]1253 't': str(scan.get_time(row)),
1254 'r': "row "+str(row),
[1913]1255 #'_r': str(scan.get_time(row))+",\nIF"+str(scan.getif(row))+", "+poleval+", Beam"+str(scan.getbeam(row)) }
1256 '_r': "" }
[920]1257 return userlabel or d[mode]
[1153]1258
[1819]1259 def plotazel(self, scan=None, outfile=None):
[1391]1260 """
[1696]1261 plot azimuth and elevation versus time of a scantable
[1391]1262 """
[1923]1263 visible = rcParams['plotter.gui']
[1696]1264 from matplotlib import pylab as PL
[2586]1265 from matplotlib.dates import DateFormatter
1266 from pytz import timezone
[1696]1267 from matplotlib.dates import HourLocator, MinuteLocator,SecondLocator, DayLocator
[1391]1268 from matplotlib.ticker import MultipleLocator
[1739]1269 from numpy import array, pi
[1923]1270 if not visible or not self._visible:
1271 PL.ioff()
1272 from matplotlib.backends.backend_agg import FigureCanvasAgg
1273 PL.gcf().canvas.switch_backends(FigureCanvasAgg)
[1819]1274 self._data = scan
[1556]1275 dates = self._data.get_time(asdatetime=True)
[1391]1276 t = PL.date2num(dates)
1277 tz = timezone('UTC')
1278 PL.cla()
1279 PL.ioff()
1280 PL.clf()
[2037]1281 # Adjust subplot margins
[2576]1282 if not self._margins or len(self._margins) != 6:
[2037]1283 self.set_margin(refresh=False)
1284 lef, bot, rig, top, wsp, hsp = self._margins
[1819]1285 PL.gcf().subplots_adjust(left=lef,bottom=bot,right=rig,top=top,
1286 wspace=wsp,hspace=hsp)
[1824]1287
[1391]1288 tdel = max(t) - min(t)
1289 ax = PL.subplot(2,1,1)
1290 el = array(self._data.get_elevation())*180./pi
1291 PL.ylabel('El [deg.]')
1292 dstr = dates[0].strftime('%Y/%m/%d')
1293 if tdel > 1.0:
1294 dstr2 = dates[len(dates)-1].strftime('%Y/%m/%d')
1295 dstr = dstr + " - " + dstr2
1296 majloc = DayLocator()
1297 minloc = HourLocator(range(0,23,12))
1298 timefmt = DateFormatter("%b%d")
[1696]1299 elif tdel > 24./60.:
1300 timefmt = DateFormatter('%H:%M')
1301 majloc = HourLocator()
1302 minloc = MinuteLocator(30)
[1391]1303 else:
[1696]1304 timefmt = DateFormatter('%H:%M')
1305 majloc = MinuteLocator(interval=5)
1306 minloc = SecondLocator(30)
1307
[1391]1308 PL.title(dstr)
[1819]1309 if tdel == 0.0:
1310 th = (t - PL.floor(t))*24.0
1311 PL.plot(th,el,'o',markersize=2, markerfacecolor='b', markeredgecolor='b')
1312 else:
1313 PL.plot_date(t,el,'o', markersize=2, markerfacecolor='b', markeredgecolor='b',tz=tz)
1314 #ax.grid(True)
1315 ax.xaxis.set_major_formatter(timefmt)
1316 ax.xaxis.set_major_locator(majloc)
1317 ax.xaxis.set_minor_locator(minloc)
[1391]1318 ax.yaxis.grid(True)
[1819]1319 yloc = MultipleLocator(30)
1320 ax.set_ylim(0,90)
1321 ax.yaxis.set_major_locator(yloc)
[1391]1322 if tdel > 1.0:
1323 labels = ax.get_xticklabels()
1324 # PL.setp(labels, fontsize=10, rotation=45)
1325 PL.setp(labels, fontsize=10)
[1819]1326
[1391]1327 # Az plot
1328 az = array(self._data.get_azimuth())*180./pi
1329 if min(az) < 0:
1330 for irow in range(len(az)):
1331 if az[irow] < 0: az[irow] += 360.0
1332
[1819]1333 ax2 = PL.subplot(2,1,2)
1334 #PL.xlabel('Time (UT [hour])')
1335 PL.ylabel('Az [deg.]')
1336 if tdel == 0.0:
1337 PL.plot(th,az,'o',markersize=2, markeredgecolor='b',markerfacecolor='b')
1338 else:
1339 PL.plot_date(t,az,'o', markersize=2,markeredgecolor='b',markerfacecolor='b',tz=tz)
1340 ax2.xaxis.set_major_formatter(timefmt)
1341 ax2.xaxis.set_major_locator(majloc)
1342 ax2.xaxis.set_minor_locator(minloc)
1343 #ax2.grid(True)
1344 ax2.set_ylim(0,360)
[1696]1345 ax2.yaxis.grid(True)
[1819]1346 #hfmt = DateFormatter('%H')
1347 #hloc = HourLocator()
1348 yloc = MultipleLocator(60)
1349 ax2.yaxis.set_major_locator(yloc)
1350 if tdel > 1.0:
1351 labels = ax2.get_xticklabels()
1352 PL.setp(labels, fontsize=10)
1353 PL.xlabel('Time (UT [day])')
1354 else:
1355 PL.xlabel('Time (UT [hour])')
1356
[1391]1357 PL.ion()
1358 PL.draw()
[2416]1359 if matplotlib.get_backend() == 'Qt4Agg': PL.gcf().show()
[2576]1360 if (outfile is not None):
1361 PL.savefig(outfile)
[1391]1362
[1819]1363 def plotpointing(self, scan=None, outfile=None):
[1391]1364 """
1365 plot telescope pointings
1366 """
[1923]1367 visible = rcParams['plotter.gui']
[1696]1368 from matplotlib import pylab as PL
[1819]1369 from numpy import array, pi
[1923]1370 if not visible or not self._visible:
1371 PL.ioff()
1372 from matplotlib.backends.backend_agg import FigureCanvasAgg
1373 PL.gcf().canvas.switch_backends(FigureCanvasAgg)
[1819]1374 self._data = scan
[1391]1375 dir = array(self._data.get_directionval()).transpose()
1376 ra = dir[0]*180./pi
1377 dec = dir[1]*180./pi
1378 PL.cla()
[1819]1379 #PL.ioff()
[1391]1380 PL.clf()
[2037]1381 # Adjust subplot margins
[2576]1382 if not self._margins or len(self._margins) != 6:
[2037]1383 self.set_margin(refresh=False)
1384 lef, bot, rig, top, wsp, hsp = self._margins
[1819]1385 PL.gcf().subplots_adjust(left=lef,bottom=bot,right=rig,top=top,
1386 wspace=wsp,hspace=hsp)
1387 ax = PL.gca()
1388 #ax = PL.axes([0.1,0.1,0.8,0.8])
1389 #ax = PL.axes([0.1,0.1,0.8,0.8])
[1391]1390 ax.set_aspect('equal')
[1696]1391 PL.plot(ra, dec, 'b,')
[1391]1392 PL.xlabel('RA [deg.]')
1393 PL.ylabel('Declination [deg.]')
1394 PL.title('Telescope pointings')
1395 [xmin,xmax,ymin,ymax] = PL.axis()
1396 PL.axis([xmax,xmin,ymin,ymax])
[2416]1397 PL.ion()
[1391]1398 PL.draw()
[2416]1399 if matplotlib.get_backend() == 'Qt4Agg': PL.gcf().show()
[2576]1400 if (outfile is not None):
1401 PL.savefig(outfile)
[1819]1402
1403 # plot total power data
1404 # plotting in time is not yet implemented..
[1862]1405 @asaplog_post_dec
[2576]1406 def plottp(self, scan=None):
[2453]1407 self._assert_plotter(action="reload")
[1819]1408 self._plotter.hold()
1409 self._plotter.clear()
1410 from asap import scantable
1411 if not self._data and not scan:
1412 msg = "Input is not a scantable"
1413 raise TypeError(msg)
1414 if isinstance(scan, scantable):
1415 if self._data is not None:
1416 if scan != self._data:
1417 self._data = scan
1418 # reset
1419 self._reset()
1420 else:
1421 self._data = scan
1422 self._reset()
1423 # ranges become invalid when abcissa changes?
1424 #if self._abcunit and self._abcunit != self._data.get_unit():
1425 # self._minmaxx = None
1426 # self._minmaxy = None
1427 # self._abcunit = self._data.get_unit()
1428 # self._datamask = None
1429
[2037]1430 # Adjust subplot margins
[2576]1431 if not self._margins or len(self._margins) !=6:
1432 self.set_margin(refresh=False)
[2037]1433 lef, bot, rig, top, wsp, hsp = self._margins
[1819]1434 self._plotter.figure.subplots_adjust(
1435 left=lef,bottom=bot,right=rig,top=top,wspace=wsp,hspace=hsp)
[2147]1436 if self.casabar_exists(): self._plotter.figmgr.casabar.disable_button()
[1819]1437 self._plottp(self._data)
1438 if self._minmaxy is not None:
1439 self._plotter.set_limits(ylim=self._minmaxy)
1440 self._plotter.release()
1441 self._plotter.tidy()
1442 self._plotter.show(hardrefresh=False)
1443 return
1444
1445 def _plottp(self,scan):
1446 """
1447 private method for plotting total power data
1448 """
1449 from numpy import ma, array, arange, logical_not
1450 r=0
1451 nr = scan.nrow()
1452 a0,b0 = -1,-1
1453 allxlim = []
1454 allylim = []
1455 y=[]
1456 self._plotter.set_panels()
1457 self._plotter.palette(0)
1458 #title
1459 #xlab = self._abcissa and self._abcissa[panelcount] \
1460 # or scan._getabcissalabel()
1461 #ylab = self._ordinate and self._ordinate[panelcount] \
1462 # or scan._get_ordinate_label()
1463 xlab = self._abcissa or 'row number' #or Time
1464 ylab = self._ordinate or scan._get_ordinate_label()
1465 self._plotter.set_axes('xlabel',xlab)
1466 self._plotter.set_axes('ylabel',ylab)
1467 lbl = self._get_label(scan, r, 's', self._title)
1468 if isinstance(lbl, list) or isinstance(lbl, tuple):
1469 # if 0 <= panelcount < len(lbl):
1470 # lbl = lbl[panelcount]
1471 # else:
1472 # get default label
1473 lbl = self._get_label(scan, r, self._panelling, None)
1474 self._plotter.set_axes('title',lbl)
1475 y=array(scan._get_column(scan._getspectrum,-1))
1476 m = array(scan._get_column(scan._getmask,-1))
1477 y = ma.masked_array(y,mask=logical_not(array(m,copy=False)))
1478 x = arange(len(y))
1479 # try to handle spectral data somewhat...
1480 l,m = y.shape
1481 if m > 1:
1482 y=y.mean(axis=1)
1483 plotit = self._plotter.plot
1484 llbl = self._get_label(scan, r, self._stacking, None)
1485 self._plotter.set_line(label=llbl)
1486 if len(x) > 0:
1487 plotit(x,y)
1488
1489
1490 # forwards to matplotlib.Figure.text
1491 def figtext(self, *args, **kwargs):
1492 """
1493 Add text to figure at location x,y (relative 0-1 coords).
1494 This method forwards *args and **kwargs to a Matplotlib method,
1495 matplotlib.Figure.text.
1496 See the method help for detailed information.
1497 """
[2453]1498 self._assert_plotter(action="reload")
[1819]1499 self._plotter.text(*args, **kwargs)
1500 # end matplotlib.Figure.text forwarding function
1501
1502
1503 # printing header information
[1862]1504 @asaplog_post_dec
[2053]1505 def print_header(self, plot=True, fontsize=9, logger=False, selstr='', extrastr=''):
[1819]1506 """
1507 print data (scantable) header on the plot and/or logger.
[2056]1508 To plot the header on the plot, this method should be called after
1509 plotting spectra by the method, asapplotter.plot.
[1819]1510 Parameters:
[1824]1511 plot: whether or not print header info on the plot.
[2053]1512 fontsize: header font size (valid only plot=True)
[1819]1513 logger: whether or not print header info on the logger.
1514 selstr: additional selection string (not verified)
[2053]1515 extrastr: additional string to print at the beginning (not verified)
[1819]1516 """
[1859]1517 if not plot and not logger:
1518 return
1519 if not self._data:
1520 raise RuntimeError("No scantable has been set yet.")
[1824]1521 # Now header will be printed on plot and/or logger.
1522 # Get header information and format it.
[2112]1523 ssum=self._data._list_header()
[1819]1524 # Print Observation header to the upper-left corner of plot
[2290]1525 headstr=[ssum[0:ssum.find('Obs. Type:')]]
1526 headstr.append(ssum[ssum.find('Obs. Type:'):ssum.find('Flux Unit:')])
[2053]1527 if extrastr != '':
1528 headstr[0]=extrastr+'\n'+headstr[0]
1529 self._headtext['extrastr'] = extrastr
[2112]1530 if selstr != '':
1531 selstr += '\n'
1532 self._headtext['selstr'] = selstr
[2056]1533 ssel=(selstr+self._data.get_selection().__str__()+self._selection.__str__() or 'none')
[2053]1534 headstr.append('***Selections***\n'+ssel)
[1824]1535
[2051]1536 if plot:
[2451]1537 errmsg = "Can plot header only after the first call to plot()."
[2453]1538 self._assert_plotter(action="halt",errmsg=errmsg)
[1819]1539 self._plotter.hold()
[2053]1540 self._header_plot(headstr,fontsize=fontsize)
[1819]1541 import time
[2106]1542 self._plotter.figure.text(0.99,0.01,
[1819]1543 time.strftime("%a %d %b %Y %H:%M:%S %Z"),
1544 horizontalalignment='right',
1545 verticalalignment='bottom',fontsize=8)
1546 self._plotter.release()
1547 if logger:
[2053]1548 selstr = "Selections: "+ssel
[1819]1549 asaplog.push("----------------\n Plot Summary\n----------------")
[2053]1550 asaplog.push(extrastr)
[2290]1551 asaplog.push(ssum[0:ssum.find('Selection:')]\
[2112]1552 + selstr)
[2053]1553 self._headtext['string'] = headstr
1554 del ssel, ssum, headstr
[2051]1555
[2053]1556 def _header_plot(self, texts, fontsize=9):
1557 self._headtext['textobj']=[]
1558 nstcol=len(texts)
1559 for i in range(nstcol):
1560 self._headtext['textobj'].append(
1561 self._plotter.figure.text(0.03+float(i)/nstcol,0.98,
1562 texts[i],
1563 horizontalalignment='left',
1564 verticalalignment='top',
1565 fontsize=fontsize))
1566
1567 def clear_header(self):
1568 if not self._headtext['textobj']:
1569 asaplog.push("No header has been plotted. Exit without any operation")
1570 asaplog.post("WARN")
[2453]1571 elif self._assert_plotter(action="status"):
[2053]1572 self._plotter.hold()
1573 for textobj in self._headtext['textobj']:
1574 #if textobj.get_text() in self._headstring:
1575 try:
1576 textobj.remove()
1577 except NotImplementedError:
1578 self._plotter.figure.texts.pop(self._plotter.figure.texts.index(textobj))
1579 self._plotter.release()
1580 self._reset_header()
[2576]1581
1582 # plot spectra by pointing
1583 @asaplog_post_dec
1584 def plotgrid(self, scan=None,center=None,spacing=None,rows=None,cols=None):
1585 """
1586 Plot spectra based on direction.
1587
1588 Parameters:
1589 scan: a scantable to plot
1590 center: the grid center direction (a list) of plots in the
1591 unit of DIRECTION column.
1592 (default) the center of map region
1593 spacing: a list of horizontal (R.A.) and vertical (Dec.)
1594 spacing in the unit of DIRECTION column.
1595 (default) Calculated by the extent of map region and
1596 the number of rows and cols to cover
1597 rows: number of panels (grid points) in horizontal direction
1598 cols: number of panels (grid points) in vertical direction
1599
1600 Note:
1601 - Only the first IFNO, POLNO, and BEAM in the scantable will be
1602 plotted.
1603 - This method doesn't re-grid and average spectra in scantable. Use
1604 asapgrid module to re-grid spectra before plotting with this method.
1605 Only the first spectrum is plotted in case there are multiple
1606 spectra which belong to a grid.
1607 """
1608 from asap import scantable
[2607]1609 from numpy import array, ma, cos
[2576]1610 if not self._data and not scan:
1611 msg = "No scantable is specified to plot"
1612 raise TypeError(msg)
[2604]1613 if scan:
1614 self.set_data(scan, refresh=False)
1615 del scan
1616
[2576]1617 # Rows and cols
1618 if rows:
1619 self._rows = int(rows)
1620 else:
1621 msg = "Number of rows to plot are not specified. "
1622 if self._rows:
1623 msg += "Using previous value = %d" % (self._rows)
1624 asaplog.push(msg)
1625 else:
1626 self._rows = 1
1627 msg += "Setting rows = %d" % (self._rows)
1628 asaplog.post()
1629 asaplog.push(msg)
1630 asaplog.post("WARN")
1631 if cols:
1632 self._cols = int(cols)
1633 else:
1634 msg = "Number of cols to plot are not specified. "
1635 if self._cols:
1636 msg += "Using previous value = %d" % (self._cols)
1637 asaplog.push(msg)
1638 else:
1639 self._cols = 1
1640 msg += "Setting cols = %d" % (self._cols)
1641 asaplog.post()
1642 asaplog.push(msg)
1643 asaplog.post("WARN")
1644
1645 # Center and spacing
[2607]1646 if center is None:
1647 #asaplog.post()
[2576]1648 asaplog.push("Grid center is not specified. Automatically calculated from pointing center.")
[2607]1649 #asaplog.post("WARN")
[2576]1650 dirarr = array(self._data.get_directionval()).transpose()
1651 #center = [dirarr[0].mean(), dirarr[1].mean()]
1652 center = [0.5*(dirarr[0].max() + dirarr[0].min()),
1653 0.5*(dirarr[1].max() + dirarr[1].min())]
1654 del dirarr
[2607]1655 elif (type(center) in (list, tuple)) and len(center) > 1:
1656 center = center[0:2]
1657 else:
1658 msg = "Direction of grid center should be a list of float (R.A., Dec.)"
1659 raise ValueError, msg
[2576]1660 asaplog.push("Grid center: (%f, %f) " % (center[0],center[1]))
1661
1662 if spacing is None:
[2607]1663 #asaplog.post()
[2576]1664 asaplog.push("Grid spacing not specified. Automatically calculated from map coverage")
[2607]1665 #asaplog.post("WARN")
[2576]1666 # automatically get spacing
1667 dirarr = array(self._data.get_directionval()).transpose()
1668 wx = 2. * max(abs(dirarr[0].max()-center[0]),
1669 abs(dirarr[0].min()-center[0]))
1670 wy = 2. * max(abs(dirarr[1].max()-center[1]),
1671 abs(dirarr[1].min()-center[1]))
[2607]1672 ## slightly expand area to plot the edges
1673 #wx *= 1.01
1674 #wy *= 1.01
1675 #xgrid = wx/self._cols
1676 #ygrid = wy/self._rows
1677 xgrid = wx/max(self._cols-1.,1.)
1678 ygrid = wy/max(self._rows-1.,1.)
[2602]1679 #print "Pointing range: (x, y) = (%f - %f, %f - %f)" %\
1680 # (dirarr[0].min(),dirarr[0].max(),dirarr[1].min(),dirarr[1].max())
[2576]1681 # identical R.A. and/or Dec. for all spectra.
1682 if xgrid == 0:
1683 xgrid = 1.
1684 if ygrid == 0:
1685 ygrid = 1.
1686 # spacing should be negative to transpose plot
1687 spacing = [- xgrid, - ygrid]
1688 del dirarr, xgrid, ygrid
1689 #elif isinstance(spacing, str):
1690 # # spacing is a quantity
[2607]1691 elif (type(spacing) in (list, tuple)) and len(spacing) > 1:
[2602]1692 for i in xrange(2):
1693 val = spacing[i]
1694 if not isinstance(val, float):
[2576]1695 raise TypeError("spacing should be a list of float")
[2602]1696 if val > 0.:
1697 spacing[i] = -val
[2576]1698 spacing = spacing[0:2]
[2607]1699 # Correction of Dec. effect
1700 spacing[0] /= cos(center[1])
[2576]1701 else:
1702 msg = "Invalid spacing."
1703 raise TypeError(msg)
[2607]1704 asaplog.push("Spacing: (%f, %f) (projected)" % (spacing[0],spacing[1]))
[2576]1705
1706 ntotpl = self._rows * self._cols
1707 minpos = [center[0]-spacing[0]*self._cols/2.,
1708 center[1]-spacing[1]*self._rows/2.]
[2602]1709 #print "Plot range: (x, y) = (%f - %f, %f - %f)" %\
1710 # (minpos[0],minpos[0]+spacing[0]*self._cols,
1711 # minpos[1],minpos[1]+spacing[1]*self._rows)
[2576]1712 ifs = self._data.getifnos()
1713 if len(ifs) > 1:
1714 msg = "Found multiple IFs in scantable. Only the first IF (IFNO=%d) will be plotted." % ifs[0]
1715 asaplog.post()
1716 asaplog.push(msg)
1717 asaplog.post("WARN")
1718 pols = self._data.getpolnos()
1719 if len(pols) > 1:
1720 msg = "Found multiple POLs in scantable. Only the first POL (POLNO=%d) will be plotted." % pols[0]
1721 asaplog.post()
1722 asaplog.push(msg)
1723 asaplog.post("WARN")
1724 beams = self._data.getbeamnos()
1725 if len(beams) > 1:
1726 msg = "Found multiple BEAMs in scantable. Only the first BEAM (BEAMNO=%d) will be plotted." % beams[0]
1727 asaplog.post()
1728 asaplog.push(msg)
1729 asaplog.post("WARN")
1730 self._data.set_selection(ifs=[ifs[0]],pols=[pols[0]],beams=[beams[0]])
1731 if self._data.nrow() > ntotpl:
1732 msg = "Scantable is finely sampled than plotting grids. "\
1733 + "Only the first spectrum is plotted in each grid."
1734 asaplog.post()
1735 asaplog.push(msg)
1736 asaplog.post("WARN")
1737
1738 self._assert_plotter(action="reload")
1739 self._plotter.hold()
1740 self._plotter.clear()
[2604]1741 self._plotter.legend()
[2576]1742
1743 # Adjust subplot margins
1744 if not self._margins or len(self._margins) !=6:
1745 self.set_margin(refresh=False)
1746 self._plotter.set_panels(rows=self._rows,cols=self._cols,
1747 nplots=ntotpl,margin=self._margins,ganged=True)
[2603]1748 if self.casabar_exists():
1749 self._plotter.figmgr.casabar.set_pagecounter(1)
1750 self._plotter.figmgr.casabar.enable_button()
[2576]1751 # Actual plot
1752 npl = 0
1753 for irow in range(self._data.nrow()):
1754 pos = self._data.get_directionval(irow)
1755 ix = int((pos[0] - minpos[0])/spacing[0])
1756 if ix < 0 or ix >= self._cols:
[2602]1757 #print "Row %d : Out of X-range (x = %f) ... skipped" % (irow, pos[0])
[2576]1758 continue
1759 iy = int((pos[1]- minpos[1])/spacing[1])
1760 if iy < 0 or iy >= self._cols:
[2602]1761 #print "Row %d : Out of Y-range (y = %f) ... skipped" % (irow,pos[1])
[2576]1762 continue
1763 ipanel = ix + iy*self._cols
1764 if len(self._plotter.subplots[ipanel]['lines']) > 0:
[2602]1765 #print "Row %d : panel %d lready plotted ... skipped" % (irow,ipanel)
[2576]1766 # a spectrum already plotted in the panel
1767 continue
1768 # Plotting this row
[2602]1769 #print "PLOTTING row %d (panel=%d)" % (irow, ipanel)
[2576]1770 npl += 1
1771 self._plotter.subplot(ipanel)
[2602]1772 self._plotter.palette(0,colormap=self._colormap, \
1773 linestyle=0,linestyles=self._linestyles)
[2576]1774 xlab = self._abcissa and self._abcissa[ipanel] \
[2603]1775 or self._data._getabcissalabel(irow)
[2576]1776 if self._offset and not self._abcissa:
1777 xlab += " (relative)"
1778 ylab = self._ordinate and self._ordinate[ipanel] \
[2603]1779 or self._data._get_ordinate_label()
[2576]1780 self._plotter.set_axes('xlabel', xlab)
1781 self._plotter.set_axes('ylabel', ylab)
1782 #from numpy import pi
1783 #lbl = "(%f, %f)" % (self._data.get_directionval(irow)[0]*180/pi,self._data.get_directionval(irow)[1]*180./pi)
1784 lbl = self._data.get_direction(irow)
1785 self._plotter.set_axes('title',lbl)
1786
[2603]1787 y = self._data._getspectrum(irow)
[2576]1788 # flag application
[2603]1789 mr = self._data._getflagrow(irow)
[2576]1790 if mr: # FLAGROW=True
1791 y = ma.masked_array(y,mask=mr)
1792 else:
[2603]1793 m = self._data._getmask(irow)
[2576]1794 from numpy import logical_not, logical_and
1795 ### user mask is not available so far
1796 #if self._maskselection and len(self._usermask) == len(m):
1797 # if d[self._stacking](irow) in self._maskselection[self._stacking]:
1798 # m = logical_and(m, self._usermask)
1799 y = ma.masked_array(y,mask=logical_not(array(m,copy=False)))
1800
[2603]1801 x = array(self._data._getabcissa(irow))
[2576]1802 if self._offset:
1803 x += self._offset
1804 if self._minmaxx is not None:
1805 s,e = self._slice_indeces(x)
1806 x = x[s:e]
1807 y = y[s:e]
1808 if len(x) > 1024 and rcParams['plotter.decimate']:
1809 fac = len(x)/1024
1810 x = x[::fac]
1811 y = y[::fac]
1812 self._plotter.set_line(label=lbl)
1813 plotit = self._plotter.plot
1814 if self._hist: plotit = self._plotter.hist
1815 if len(x) > 0 and not mr:
1816 plotit(x,y)
1817# xlim= self._minmaxx or [min(x),max(x)]
1818# allxlim += xlim
1819# ylim= self._minmaxy or [ma.minimum(y),ma.maximum(y)]
1820# allylim += ylim
1821# else:
1822# xlim = self._minmaxx or []
1823# allxlim += xlim
1824# ylim= self._minmaxy or []
1825# allylim += ylim
1826
1827 if npl >= ntotpl:
1828 break
1829
1830 if self._minmaxy is not None:
1831 self._plotter.set_limits(ylim=self._minmaxy)
1832 self._plotter.release()
1833 self._plotter.tidy()
1834 self._plotter.show(hardrefresh=False)
1835 return
Note: See TracBrowser for help on using the repository browser.