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