[947] | 1 | from asap import rcParams, print_log, selector
|
---|
[1146] | 2 | from asap import NUM
|
---|
[1153] | 3 | import matplotlib.axes
|
---|
[1217] | 4 | import sre
|
---|
[203] | 5 |
|
---|
| 6 | class asapplotter:
|
---|
[226] | 7 | """
|
---|
| 8 | The ASAP plotter.
|
---|
| 9 | By default the plotter is set up to plot polarisations
|
---|
| 10 | 'colour stacked' and scantables across panels.
|
---|
| 11 | Note:
|
---|
| 12 | Currenly it only plots 'spectra' not Tsys or
|
---|
| 13 | other variables.
|
---|
| 14 | """
|
---|
[734] | 15 | def __init__(self, visible=None):
|
---|
| 16 | self._visible = rcParams['plotter.gui']
|
---|
| 17 | if visible is not None:
|
---|
| 18 | self._visible = visible
|
---|
[710] | 19 | self._plotter = self._newplotter()
|
---|
| 20 |
|
---|
[554] | 21 | self._panelling = None
|
---|
| 22 | self._stacking = None
|
---|
| 23 | self.set_panelling()
|
---|
| 24 | self.set_stacking()
|
---|
[377] | 25 | self._rows = None
|
---|
| 26 | self._cols = None
|
---|
[203] | 27 | self._autoplot = False
|
---|
[525] | 28 | self._minmaxx = None
|
---|
| 29 | self._minmaxy = None
|
---|
[710] | 30 | self._datamask = None
|
---|
[203] | 31 | self._data = None
|
---|
[607] | 32 | self._lmap = None
|
---|
[226] | 33 | self._title = None
|
---|
[257] | 34 | self._ordinate = None
|
---|
| 35 | self._abcissa = None
|
---|
[709] | 36 | self._abcunit = None
|
---|
[920] | 37 | self._usermask = []
|
---|
| 38 | self._maskselection = None
|
---|
| 39 | self._selection = selector()
|
---|
[1023] | 40 | self._hist = rcParams['plotter.histogram']
|
---|
| 41 |
|
---|
[920] | 42 | def _translate(self, instr):
|
---|
| 43 | keys = "s b i p t".split()
|
---|
| 44 | if isinstance(instr, str):
|
---|
| 45 | for key in keys:
|
---|
| 46 | if instr.lower().startswith(key):
|
---|
| 47 | return key
|
---|
| 48 | return None
|
---|
| 49 |
|
---|
[710] | 50 | def _newplotter(self):
|
---|
| 51 | if self._visible:
|
---|
| 52 | from asap.asaplotgui import asaplotgui as asaplot
|
---|
| 53 | else:
|
---|
| 54 | from asap.asaplot import asaplot
|
---|
| 55 | return asaplot()
|
---|
| 56 |
|
---|
| 57 |
|
---|
[935] | 58 | def plot(self, scan=None):
|
---|
[203] | 59 | """
|
---|
[920] | 60 | Plot a scantable.
|
---|
[203] | 61 | Parameters:
|
---|
[920] | 62 | scan: a scantable
|
---|
[203] | 63 | Note:
|
---|
[920] | 64 | If a scantable was specified in a previous call
|
---|
[203] | 65 | to plot, no argument has to be given to 'replot'
|
---|
[920] | 66 | NO checking is done that the abcissas of the scantable
|
---|
[203] | 67 | are consistent e.g. all 'channel' or all 'velocity' etc.
|
---|
| 68 | """
|
---|
[710] | 69 | if self._plotter.is_dead:
|
---|
| 70 | self._plotter = self._newplotter()
|
---|
[600] | 71 | self._plotter.hold()
|
---|
[203] | 72 | self._plotter.clear()
|
---|
[920] | 73 | from asap import scantable
|
---|
[935] | 74 | if not self._data and not scan:
|
---|
[1101] | 75 | msg = "Input is not a scantable"
|
---|
| 76 | if rcParams['verbose']:
|
---|
| 77 | print msg
|
---|
| 78 | return
|
---|
| 79 | raise TypeError(msg)
|
---|
[920] | 80 | if isinstance(scan, scantable):
|
---|
[709] | 81 | if self._data is not None:
|
---|
[920] | 82 | if scan != self._data:
|
---|
| 83 | self._data = scan
|
---|
[710] | 84 | # reset
|
---|
| 85 | self._reset()
|
---|
[525] | 86 | else:
|
---|
[920] | 87 | self._data = scan
|
---|
[710] | 88 | self._reset()
|
---|
[709] | 89 | # ranges become invalid when unit changes
|
---|
[935] | 90 | if self._abcunit and self._abcunit != self._data.get_unit():
|
---|
[709] | 91 | self._minmaxx = None
|
---|
| 92 | self._minmaxy = None
|
---|
[920] | 93 | self._abcunit = self._data.get_unit()
|
---|
[710] | 94 | self._datamask = None
|
---|
[920] | 95 | self._plot(self._data)
|
---|
[709] | 96 | if self._minmaxy is not None:
|
---|
| 97 | self._plotter.set_limits(ylim=self._minmaxy)
|
---|
[203] | 98 | self._plotter.release()
|
---|
[1153] | 99 | self._plotter.tidy()
|
---|
| 100 | self._plotter.show(hardrefresh=False)
|
---|
[753] | 101 | print_log()
|
---|
[203] | 102 | return
|
---|
| 103 |
|
---|
[1153] | 104 |
|
---|
| 105 | # forwards to matplotlib axes
|
---|
| 106 | def text(self, *args, **kwargs):
|
---|
| 107 | self._axes_callback("text", *args, **kwargs)
|
---|
| 108 | text. __doc__ = matplotlib.axes.Axes.text.__doc__
|
---|
| 109 | def arrow(self, *args, **kwargs):
|
---|
| 110 | self._axes_callback("arrow", *args, **kwargs)
|
---|
| 111 | arrow. __doc__ = matplotlib.axes.Axes.arrow.__doc__
|
---|
| 112 | def axvline(self, *args, **kwargs):
|
---|
| 113 | self._axes_callback("axvline", *args, **kwargs)
|
---|
| 114 | axvline. __doc__ = matplotlib.axes.Axes.axvline.__doc__
|
---|
| 115 | def axhline(self, *args, **kwargs):
|
---|
| 116 | self._axes_callback("axhline", *args, **kwargs)
|
---|
| 117 | axhline. __doc__ = matplotlib.axes.Axes.axhline.__doc__
|
---|
| 118 | def axvspan(self, *args, **kwargs):
|
---|
| 119 | self._axes_callback("axvspan", *args, **kwargs)
|
---|
| 120 | # hack to preventy mpl from redrawing the patch
|
---|
| 121 | # it seem to convert the patch into lines on every draw.
|
---|
| 122 | # This doesn't happen in a test script???
|
---|
| 123 | del self._plotter.axes.patches[-1]
|
---|
| 124 | axvspan. __doc__ = matplotlib.axes.Axes.axvspan.__doc__
|
---|
[1232] | 125 |
|
---|
[1153] | 126 | def axhspan(self, *args, **kwargs):
|
---|
[1232] | 127 | self._axes_callback("axhspan", *args, **kwargs)
|
---|
[1153] | 128 | # hack to preventy mpl from redrawing the patch
|
---|
| 129 | # it seem to convert the patch into lines on every draw.
|
---|
| 130 | # This doesn't happen in a test script???
|
---|
| 131 | del self._plotter.axes.patches[-1]
|
---|
| 132 | axhspan. __doc__ = matplotlib.axes.Axes.axhspan.__doc__
|
---|
| 133 |
|
---|
| 134 | def _axes_callback(self, axesfunc, *args, **kwargs):
|
---|
| 135 | panel = 0
|
---|
| 136 | if kwargs.has_key("panel"):
|
---|
| 137 | panel = kwargs.pop("panel")
|
---|
| 138 | coords = None
|
---|
| 139 | if kwargs.has_key("coords"):
|
---|
| 140 | coords = kwargs.pop("coords")
|
---|
| 141 | if coords.lower() == 'world':
|
---|
| 142 | kwargs["transform"] = self._plotter.axes.transData
|
---|
| 143 | elif coords.lower() == 'relative':
|
---|
| 144 | kwargs["transform"] = self._plotter.axes.transAxes
|
---|
| 145 | self._plotter.subplot(panel)
|
---|
| 146 | self._plotter.axes.set_autoscale_on(False)
|
---|
| 147 | getattr(self._plotter.axes, axesfunc)(*args, **kwargs)
|
---|
| 148 | self._plotter.show(False)
|
---|
| 149 | self._plotter.axes.set_autoscale_on(True)
|
---|
| 150 | # end matplotlib.axes fowarding functions
|
---|
| 151 |
|
---|
[226] | 152 | def set_mode(self, stacking=None, panelling=None):
|
---|
[203] | 153 | """
|
---|
[377] | 154 | Set the plots look and feel, i.e. what you want to see on the plot.
|
---|
[203] | 155 | Parameters:
|
---|
| 156 | stacking: tell the plotter which variable to plot
|
---|
[1217] | 157 | as line colour overlays (default 'pol')
|
---|
[203] | 158 | panelling: tell the plotter which variable to plot
|
---|
| 159 | across multiple panels (default 'scan'
|
---|
| 160 | Note:
|
---|
| 161 | Valid modes are:
|
---|
| 162 | 'beam' 'Beam' 'b': Beams
|
---|
| 163 | 'if' 'IF' 'i': IFs
|
---|
| 164 | 'pol' 'Pol' 'p': Polarisations
|
---|
| 165 | 'scan' 'Scan' 's': Scans
|
---|
| 166 | 'time' 'Time' 't': Times
|
---|
| 167 | """
|
---|
[753] | 168 | msg = "Invalid mode"
|
---|
| 169 | if not self.set_panelling(panelling) or \
|
---|
| 170 | not self.set_stacking(stacking):
|
---|
| 171 | if rcParams['verbose']:
|
---|
| 172 | print msg
|
---|
| 173 | return
|
---|
| 174 | else:
|
---|
| 175 | raise TypeError(msg)
|
---|
[920] | 176 | if self._data: self.plot(self._data)
|
---|
[203] | 177 | return
|
---|
| 178 |
|
---|
[554] | 179 | def set_panelling(self, what=None):
|
---|
| 180 | mode = what
|
---|
| 181 | if mode is None:
|
---|
| 182 | mode = rcParams['plotter.panelling']
|
---|
| 183 | md = self._translate(mode)
|
---|
[203] | 184 | if md:
|
---|
[554] | 185 | self._panelling = md
|
---|
[226] | 186 | self._title = None
|
---|
[203] | 187 | return True
|
---|
| 188 | return False
|
---|
| 189 |
|
---|
[377] | 190 | def set_layout(self,rows=None,cols=None):
|
---|
| 191 | """
|
---|
| 192 | Set the multi-panel layout, i.e. how many rows and columns plots
|
---|
| 193 | are visible.
|
---|
| 194 | Parameters:
|
---|
| 195 | rows: The number of rows of plots
|
---|
| 196 | cols: The number of columns of plots
|
---|
| 197 | Note:
|
---|
| 198 | If no argument is given, the potter reverts to its auto-plot
|
---|
| 199 | behaviour.
|
---|
| 200 | """
|
---|
| 201 | self._rows = rows
|
---|
| 202 | self._cols = cols
|
---|
[920] | 203 | if self._data: self.plot(self._data)
|
---|
[377] | 204 | return
|
---|
| 205 |
|
---|
[709] | 206 | def set_stacking(self, what=None):
|
---|
[554] | 207 | mode = what
|
---|
[709] | 208 | if mode is None:
|
---|
| 209 | mode = rcParams['plotter.stacking']
|
---|
[554] | 210 | md = self._translate(mode)
|
---|
[203] | 211 | if md:
|
---|
| 212 | self._stacking = md
|
---|
[226] | 213 | self._lmap = None
|
---|
[203] | 214 | return True
|
---|
| 215 | return False
|
---|
| 216 |
|
---|
[525] | 217 | def set_range(self,xstart=None,xend=None,ystart=None,yend=None):
|
---|
[203] | 218 | """
|
---|
| 219 | Set the range of interest on the abcissa of the plot
|
---|
| 220 | Parameters:
|
---|
[525] | 221 | [x,y]start,[x,y]end: The start and end points of the 'zoom' window
|
---|
[203] | 222 | Note:
|
---|
| 223 | These become non-sensical when the unit changes.
|
---|
| 224 | use plotter.set_range() without parameters to reset
|
---|
| 225 |
|
---|
| 226 | """
|
---|
[525] | 227 | if xstart is None and xend is None:
|
---|
| 228 | self._minmaxx = None
|
---|
[600] | 229 | else:
|
---|
| 230 | self._minmaxx = [xstart,xend]
|
---|
[525] | 231 | if ystart is None and yend is None:
|
---|
| 232 | self._minmaxy = None
|
---|
[600] | 233 | else:
|
---|
[709] | 234 | self._minmaxy = [ystart,yend]
|
---|
[920] | 235 | if self._data: self.plot(self._data)
|
---|
[203] | 236 | return
|
---|
[709] | 237 |
|
---|
[1101] | 238 | def set_legend(self, mp=None, fontsize = None, mode = 0):
|
---|
[203] | 239 | """
|
---|
| 240 | Specify a mapping for the legend instead of using the default
|
---|
| 241 | indices:
|
---|
| 242 | Parameters:
|
---|
[1101] | 243 | mp: a list of 'strings'. This should have the same length
|
---|
| 244 | as the number of elements on the legend and then maps
|
---|
| 245 | to the indeces in order. It is possible to uses latex
|
---|
| 246 | math expression. These have to be enclosed in r'',
|
---|
| 247 | e.g. r'$x^{2}$'
|
---|
| 248 | fontsize: The font size of the label (default None)
|
---|
| 249 | mode: where to display the legend
|
---|
| 250 | Any other value for loc else disables the legend:
|
---|
[1096] | 251 | 0: auto
|
---|
| 252 | 1: upper right
|
---|
| 253 | 2: upper left
|
---|
| 254 | 3: lower left
|
---|
| 255 | 4: lower right
|
---|
| 256 | 5: right
|
---|
| 257 | 6: center left
|
---|
| 258 | 7: center right
|
---|
| 259 | 8: lower center
|
---|
| 260 | 9: upper center
|
---|
| 261 | 10: center
|
---|
[203] | 262 |
|
---|
| 263 | Example:
|
---|
[485] | 264 | If the data has two IFs/rest frequencies with index 0 and 1
|
---|
[203] | 265 | for CO and SiO:
|
---|
| 266 | plotter.set_stacking('i')
|
---|
[710] | 267 | plotter.set_legend(['CO','SiO'])
|
---|
[203] | 268 | plotter.plot()
|
---|
[710] | 269 | plotter.set_legend([r'$^{12}CO$', r'SiO'])
|
---|
[203] | 270 | """
|
---|
| 271 | self._lmap = mp
|
---|
[1096] | 272 | self._plotter.legend(mode)
|
---|
[1101] | 273 | if isinstance(fontsize, int):
|
---|
| 274 | from matplotlib import rc as rcp
|
---|
| 275 | rcp('legend', fontsize=fontsize)
|
---|
[1096] | 276 | if self._data:
|
---|
| 277 | self.plot(self._data)
|
---|
[226] | 278 | return
|
---|
| 279 |
|
---|
[1101] | 280 | def set_title(self, title=None, fontsize=None):
|
---|
[710] | 281 | """
|
---|
| 282 | Set the title of the plot. If multiple panels are plotted,
|
---|
| 283 | multiple titles have to be specified.
|
---|
| 284 | Example:
|
---|
| 285 | # two panels are visible on the plotter
|
---|
| 286 | plotter.set_title(["First Panel","Second Panel"])
|
---|
| 287 | """
|
---|
[226] | 288 | self._title = title
|
---|
[1101] | 289 | if isinstance(fontsize, int):
|
---|
| 290 | from matplotlib import rc as rcp
|
---|
| 291 | rcp('axes', titlesize=fontsize)
|
---|
[920] | 292 | if self._data: self.plot(self._data)
|
---|
[226] | 293 | return
|
---|
| 294 |
|
---|
[1101] | 295 | def set_ordinate(self, ordinate=None, fontsize=None):
|
---|
[710] | 296 | """
|
---|
| 297 | Set the y-axis label of the plot. If multiple panels are plotted,
|
---|
| 298 | multiple labels have to be specified.
|
---|
[1021] | 299 | Parameters:
|
---|
| 300 | ordinate: a list of ordinate labels. None (default) let
|
---|
| 301 | data determine the labels
|
---|
[710] | 302 | Example:
|
---|
| 303 | # two panels are visible on the plotter
|
---|
| 304 | plotter.set_ordinate(["First Y-Axis","Second Y-Axis"])
|
---|
| 305 | """
|
---|
[257] | 306 | self._ordinate = ordinate
|
---|
[1101] | 307 | if isinstance(fontsize, int):
|
---|
| 308 | from matplotlib import rc as rcp
|
---|
| 309 | rcp('axes', labelsize=fontsize)
|
---|
| 310 | rcp('ytick', labelsize=fontsize)
|
---|
[920] | 311 | if self._data: self.plot(self._data)
|
---|
[257] | 312 | return
|
---|
| 313 |
|
---|
[1101] | 314 | def set_abcissa(self, abcissa=None, fontsize=None):
|
---|
[710] | 315 | """
|
---|
| 316 | Set the x-axis label of the plot. If multiple panels are plotted,
|
---|
| 317 | multiple labels have to be specified.
|
---|
[1021] | 318 | Parameters:
|
---|
| 319 | abcissa: a list of abcissa labels. None (default) let
|
---|
| 320 | data determine the labels
|
---|
[710] | 321 | Example:
|
---|
| 322 | # two panels are visible on the plotter
|
---|
| 323 | plotter.set_ordinate(["First X-Axis","Second X-Axis"])
|
---|
| 324 | """
|
---|
[257] | 325 | self._abcissa = abcissa
|
---|
[1101] | 326 | if isinstance(fontsize, int):
|
---|
| 327 | from matplotlib import rc as rcp
|
---|
| 328 | rcp('axes', labelsize=fontsize)
|
---|
| 329 | rcp('xtick', labelsize=fontsize)
|
---|
[920] | 330 | if self._data: self.plot(self._data)
|
---|
[257] | 331 | return
|
---|
| 332 |
|
---|
[1217] | 333 | def set_colors(self, colmap):
|
---|
[377] | 334 | """
|
---|
[1217] | 335 | Set the colours to be used. The plotter will cycle through
|
---|
| 336 | these colours when lines are overlaid (stacking mode).
|
---|
[1021] | 337 | Parameters:
|
---|
[1217] | 338 | colmap: a list of colour names
|
---|
[710] | 339 | Example:
|
---|
| 340 | plotter.set_colors("red green blue")
|
---|
| 341 | # If for example four lines are overlaid e.g I Q U V
|
---|
| 342 | # 'I' will be 'red', 'Q' will be 'green', U will be 'blue'
|
---|
| 343 | # and 'V' will be 'red' again.
|
---|
| 344 | """
|
---|
[1217] | 345 | if isinstance(colmap,str):
|
---|
| 346 | colmap = colmap.split()
|
---|
| 347 | self._plotter.palette(0, colormap=colmap)
|
---|
[920] | 348 | if self._data: self.plot(self._data)
|
---|
[710] | 349 |
|
---|
[1217] | 350 | # alias for english speakers
|
---|
| 351 | set_colours = set_colors
|
---|
| 352 |
|
---|
[1101] | 353 | def set_histogram(self, hist=True, linewidth=None):
|
---|
[1021] | 354 | """
|
---|
| 355 | Enable/Disable histogram-like plotting.
|
---|
| 356 | Parameters:
|
---|
| 357 | hist: True (default) or False. The fisrt default
|
---|
| 358 | is taken from the .asaprc setting
|
---|
| 359 | plotter.histogram
|
---|
| 360 | """
|
---|
[1023] | 361 | self._hist = hist
|
---|
[1101] | 362 | if isinstance(linewidth, float) or isinstance(linewidth, int):
|
---|
| 363 | from matplotlib import rc as rcp
|
---|
| 364 | rcp('lines', linewidth=linewidth)
|
---|
[1021] | 365 | if self._data: self.plot(self._data)
|
---|
[1023] | 366 |
|
---|
[1101] | 367 | def set_linestyles(self, linestyles=None, linewidth=None):
|
---|
[710] | 368 | """
|
---|
[734] | 369 | Set the linestyles to be used. The plotter will cycle through
|
---|
| 370 | these linestyles when lines are overlaid (stacking mode) AND
|
---|
| 371 | only one color has been set.
|
---|
[710] | 372 | Parameters:
|
---|
| 373 | linestyles: a list of linestyles to use.
|
---|
| 374 | 'line', 'dashed', 'dotted', 'dashdot',
|
---|
| 375 | 'dashdotdot' and 'dashdashdot' are
|
---|
| 376 | possible
|
---|
| 377 |
|
---|
| 378 | Example:
|
---|
| 379 | plotter.set_colors("black")
|
---|
| 380 | plotter.set_linestyles("line dashed dotted dashdot")
|
---|
| 381 | # If for example four lines are overlaid e.g I Q U V
|
---|
| 382 | # 'I' will be 'solid', 'Q' will be 'dashed',
|
---|
| 383 | # U will be 'dotted' and 'V' will be 'dashdot'.
|
---|
| 384 | """
|
---|
| 385 | if isinstance(linestyles,str):
|
---|
| 386 | linestyles = linestyles.split()
|
---|
| 387 | self._plotter.palette(color=0,linestyle=0,linestyles=linestyles)
|
---|
[1101] | 388 | if isinstance(linewidth, float) or isinstance(linewidth, int):
|
---|
| 389 | from matplotlib import rc as rcp
|
---|
| 390 | rcp('lines', linewidth=linewidth)
|
---|
[920] | 391 | if self._data: self.plot(self._data)
|
---|
[710] | 392 |
|
---|
[1101] | 393 | def set_font(self, family=None, style=None, weight=None, size=None):
|
---|
| 394 | """
|
---|
| 395 | Set font properties.
|
---|
| 396 | Parameters:
|
---|
| 397 | family: one of 'sans-serif', 'serif', 'cursive', 'fantasy', 'monospace'
|
---|
| 398 | style: one of 'normal' (or 'roman'), 'italic' or 'oblique'
|
---|
| 399 | weight: one of 'normal or 'bold'
|
---|
| 400 | size: the 'general' font size, individual elements can be adjusted
|
---|
| 401 | seperately
|
---|
| 402 | """
|
---|
| 403 | from matplotlib import rc as rcp
|
---|
| 404 | if isinstance(family, str):
|
---|
| 405 | rcp('font', family=family)
|
---|
| 406 | if isinstance(style, str):
|
---|
| 407 | rcp('font', style=style)
|
---|
| 408 | if isinstance(weight, str):
|
---|
| 409 | rcp('font', weight=weight)
|
---|
| 410 | if isinstance(size, float) or isinstance(size, int):
|
---|
| 411 | rcp('font', size=size)
|
---|
| 412 | if self._data: self.plot(self._data)
|
---|
| 413 |
|
---|
[1169] | 414 | def plot_lines(self, linecat=None, doppler=0.0, deltachan=10, rotate=0.0,
|
---|
[1146] | 415 | location=None):
|
---|
| 416 | """
|
---|
[1158] | 417 | Plot a line catalog.
|
---|
| 418 | Parameters:
|
---|
| 419 | linecat: the linecatalog to plot
|
---|
[1168] | 420 | doppler: the velocity shift to apply to the frequencies
|
---|
[1158] | 421 | deltachan: the number of channels to include each side of the
|
---|
| 422 | line to determine a local maximum/minimum
|
---|
| 423 | rotate: the rotation for the text label
|
---|
| 424 | location: the location of the line annotation from the 'top',
|
---|
| 425 | 'bottom' or alternate (None - the default)
|
---|
[1165] | 426 | Notes:
|
---|
| 427 | If the spectrum is flagged no line will be drawn in that location.
|
---|
[1146] | 428 | """
|
---|
| 429 | if not self._data: return
|
---|
| 430 | from asap._asap import linecatalog
|
---|
| 431 | if not isinstance(linecat, linecatalog): return
|
---|
| 432 | if not self._data.get_unit().endswith("GHz"): return
|
---|
[1153] | 433 | #self._plotter.hold()
|
---|
| 434 | from matplotlib.numerix import ma
|
---|
[1146] | 435 | for j in range(len(self._plotter.subplots)):
|
---|
| 436 | self._plotter.subplot(j)
|
---|
| 437 | lims = self._plotter.axes.get_xlim()
|
---|
[1153] | 438 | for row in range(linecat.nrow()):
|
---|
[1165] | 439 | restf = linecat.get_frequency(row)/1000.0
|
---|
| 440 | c = 299792.458
|
---|
[1174] | 441 | freq = restf*(1.0-doppler/c)
|
---|
[1146] | 442 | if lims[0] < freq < lims[1]:
|
---|
| 443 | if location is None:
|
---|
| 444 | loc = 'bottom'
|
---|
[1153] | 445 | if row%2: loc='top'
|
---|
[1146] | 446 | else: loc = location
|
---|
[1153] | 447 | maxys = []
|
---|
| 448 | for line in self._plotter.axes.lines:
|
---|
| 449 | v = line._x
|
---|
| 450 | asc = v[0] < v[-1]
|
---|
| 451 |
|
---|
| 452 | idx = None
|
---|
| 453 | if not asc:
|
---|
| 454 | if v[len(v)-1] <= freq <= v[0]:
|
---|
| 455 | i = len(v)-1
|
---|
| 456 | while i>=0 and v[i] < freq:
|
---|
| 457 | idx = i
|
---|
| 458 | i-=1
|
---|
| 459 | else:
|
---|
| 460 | if v[0] <= freq <= v[len(v)-1]:
|
---|
| 461 | i = 0
|
---|
| 462 | while i<len(v) and v[i] < freq:
|
---|
| 463 | idx = i
|
---|
| 464 | i+=1
|
---|
| 465 | if idx is not None:
|
---|
| 466 | lower = idx - deltachan
|
---|
| 467 | upper = idx + deltachan
|
---|
| 468 | if lower < 0: lower = 0
|
---|
| 469 | if upper > len(v): upper = len(v)
|
---|
| 470 | s = slice(lower, upper)
|
---|
[1167] | 471 | y = line._y[s]
|
---|
[1165] | 472 | maxy = ma.maximum(y)
|
---|
| 473 | if isinstance( maxy, float):
|
---|
| 474 | maxys.append(maxy)
|
---|
[1164] | 475 | if len(maxys):
|
---|
| 476 | peak = max(maxys)
|
---|
[1165] | 477 | if peak > self._plotter.axes.get_ylim()[1]:
|
---|
| 478 | loc = 'bottom'
|
---|
[1164] | 479 | else:
|
---|
| 480 | continue
|
---|
[1157] | 481 | self._plotter.vline_with_label(freq, peak,
|
---|
| 482 | linecat.get_name(row),
|
---|
| 483 | location=loc, rotate=rotate)
|
---|
[1153] | 484 | # self._plotter.release()
|
---|
| 485 | self._plotter.show(hardrefresh=False)
|
---|
[1146] | 486 |
|
---|
[1153] | 487 |
|
---|
[710] | 488 | def save(self, filename=None, orientation=None, dpi=None):
|
---|
| 489 | """
|
---|
[377] | 490 | Save the plot to a file. The know formats are 'png', 'ps', 'eps'.
|
---|
| 491 | Parameters:
|
---|
| 492 | filename: The name of the output file. This is optional
|
---|
| 493 | and autodetects the image format from the file
|
---|
| 494 | suffix. If non filename is specified a file
|
---|
| 495 | called 'yyyymmdd_hhmmss.png' is created in the
|
---|
| 496 | current directory.
|
---|
[709] | 497 | orientation: optional parameter for postscript only (not eps).
|
---|
| 498 | 'landscape', 'portrait' or None (default) are valid.
|
---|
| 499 | If None is choosen for 'ps' output, the plot is
|
---|
| 500 | automatically oriented to fill the page.
|
---|
[710] | 501 | dpi: The dpi of the output non-ps plot
|
---|
[377] | 502 | """
|
---|
[709] | 503 | self._plotter.save(filename,orientation,dpi)
|
---|
[377] | 504 | return
|
---|
[709] | 505 |
|
---|
[257] | 506 |
|
---|
[920] | 507 | def set_mask(self, mask=None, selection=None):
|
---|
[525] | 508 | """
|
---|
[734] | 509 | Set a plotting mask for a specific polarization.
|
---|
| 510 | This is useful for masking out "noise" Pangle outside a source.
|
---|
| 511 | Parameters:
|
---|
[920] | 512 | mask: a mask from scantable.create_mask
|
---|
| 513 | selection: the spectra to apply the mask to.
|
---|
[734] | 514 | Example:
|
---|
[920] | 515 | select = selector()
|
---|
| 516 | select.setpolstrings("Pangle")
|
---|
| 517 | plotter.set_mask(mymask, select)
|
---|
[734] | 518 | """
|
---|
[710] | 519 | if not self._data:
|
---|
[920] | 520 | msg = "Can only set mask after a first call to plot()"
|
---|
[753] | 521 | if rcParams['verbose']:
|
---|
| 522 | print msg
|
---|
[762] | 523 | return
|
---|
[753] | 524 | else:
|
---|
[762] | 525 | raise RuntimeError(msg)
|
---|
[920] | 526 | if len(mask):
|
---|
| 527 | if isinstance(mask, list) or isinstance(mask, tuple):
|
---|
| 528 | self._usermask = array(mask)
|
---|
[710] | 529 | else:
|
---|
[920] | 530 | self._usermask = mask
|
---|
| 531 | if mask is None and selection is None:
|
---|
| 532 | self._usermask = []
|
---|
| 533 | self._maskselection = None
|
---|
| 534 | if isinstance(selection, selector):
|
---|
[947] | 535 | self._maskselection = {'b': selection.get_beams(),
|
---|
| 536 | 's': selection.get_scans(),
|
---|
| 537 | 'i': selection.get_ifs(),
|
---|
| 538 | 'p': selection.get_pols(),
|
---|
[920] | 539 | 't': [] }
|
---|
[710] | 540 | else:
|
---|
[920] | 541 | self._maskselection = None
|
---|
| 542 | self.plot(self._data)
|
---|
[710] | 543 |
|
---|
[709] | 544 | def _slice_indeces(self, data):
|
---|
| 545 | mn = self._minmaxx[0]
|
---|
| 546 | mx = self._minmaxx[1]
|
---|
| 547 | asc = data[0] < data[-1]
|
---|
| 548 | start=0
|
---|
| 549 | end = len(data)-1
|
---|
| 550 | inc = 1
|
---|
| 551 | if not asc:
|
---|
| 552 | start = len(data)-1
|
---|
| 553 | end = 0
|
---|
| 554 | inc = -1
|
---|
| 555 | # find min index
|
---|
[1101] | 556 | while start > 0 and data[start] < mn:
|
---|
[709] | 557 | start+= inc
|
---|
| 558 | # find max index
|
---|
[1101] | 559 | while end > 0 and data[end] > mx:
|
---|
[709] | 560 | end-=inc
|
---|
[1101] | 561 | if end > 0: end +=1
|
---|
[709] | 562 | if start > end:
|
---|
| 563 | return end,start
|
---|
| 564 | return start,end
|
---|
| 565 |
|
---|
[710] | 566 | def _reset(self):
|
---|
[920] | 567 | self._usermask = []
|
---|
[710] | 568 | self._usermaskspectra = None
|
---|
[920] | 569 | self.set_selection(None, False)
|
---|
| 570 |
|
---|
| 571 | def _plot(self, scan):
|
---|
[947] | 572 | savesel = scan.get_selection()
|
---|
| 573 | sel = savesel + self._selection
|
---|
| 574 | d0 = {'s': 'SCANNO', 'b': 'BEAMNO', 'i':'IFNO',
|
---|
| 575 | 'p': 'POLNO', 'c': 'CYCLENO', 't' : 'TIME' }
|
---|
| 576 | order = [d0[self._panelling],d0[self._stacking]]
|
---|
| 577 | sel.set_order(order)
|
---|
| 578 | scan.set_selection(sel)
|
---|
[920] | 579 | d = {'b': scan.getbeam, 's': scan.getscan,
|
---|
| 580 | 'i': scan.getif, 'p': scan.getpol, 't': scan._gettime }
|
---|
| 581 |
|
---|
[1148] | 582 | polmodes = dict(zip(self._selection.get_pols(),
|
---|
| 583 | self._selection.get_poltypes()))
|
---|
| 584 | # this returns either a tuple of numbers or a length (ncycles)
|
---|
| 585 | # convert this into lengths
|
---|
| 586 | n0,nstack0 = self._get_selected_n(scan)
|
---|
| 587 | if isinstance(n0, int): n = n0
|
---|
[1175] | 588 | else: n = len(n0)
|
---|
[1148] | 589 | if isinstance(nstack0, int): nstack = nstack0
|
---|
[1175] | 590 | else: nstack = len(nstack0)
|
---|
[998] | 591 | maxpanel, maxstack = 16,8
|
---|
[920] | 592 | if n > maxpanel or nstack > maxstack:
|
---|
| 593 | from asap import asaplog
|
---|
[1148] | 594 | maxn = 0
|
---|
| 595 | if nstack > maxstack: maxn = maxstack
|
---|
| 596 | if n > maxpanel: maxn = maxpanel
|
---|
[920] | 597 | msg ="Scan to be plotted contains more than %d selections.\n" \
|
---|
[1148] | 598 | "Selecting first %d selections..." % (maxn, maxn)
|
---|
[920] | 599 | asaplog.push(msg)
|
---|
| 600 | print_log()
|
---|
| 601 | n = min(n,maxpanel)
|
---|
[998] | 602 | nstack = min(nstack,maxstack)
|
---|
[920] | 603 | if n > 1:
|
---|
| 604 | ganged = rcParams['plotter.ganged']
|
---|
| 605 | if self._rows and self._cols:
|
---|
| 606 | n = min(n,self._rows*self._cols)
|
---|
| 607 | self._plotter.set_panels(rows=self._rows,cols=self._cols,
|
---|
| 608 | nplots=n,ganged=ganged)
|
---|
| 609 | else:
|
---|
| 610 | self._plotter.set_panels(rows=n,cols=0,nplots=n,ganged=ganged)
|
---|
| 611 | else:
|
---|
| 612 | self._plotter.set_panels()
|
---|
| 613 | r=0
|
---|
| 614 | nr = scan.nrow()
|
---|
| 615 | a0,b0 = -1,-1
|
---|
| 616 | allxlim = []
|
---|
[1018] | 617 | allylim = []
|
---|
[920] | 618 | newpanel=True
|
---|
| 619 | panelcount,stackcount = 0,0
|
---|
[1002] | 620 | while r < nr:
|
---|
[920] | 621 | a = d[self._panelling](r)
|
---|
| 622 | b = d[self._stacking](r)
|
---|
| 623 | if a > a0 and panelcount < n:
|
---|
| 624 | if n > 1:
|
---|
| 625 | self._plotter.subplot(panelcount)
|
---|
| 626 | self._plotter.palette(0)
|
---|
| 627 | #title
|
---|
| 628 | xlab = self._abcissa and self._abcissa[panelcount] \
|
---|
| 629 | or scan._getabcissalabel()
|
---|
| 630 | ylab = self._ordinate and self._ordinate[panelcount] \
|
---|
| 631 | or scan._get_ordinate_label()
|
---|
| 632 | self._plotter.set_axes('xlabel',xlab)
|
---|
| 633 | self._plotter.set_axes('ylabel',ylab)
|
---|
| 634 | lbl = self._get_label(scan, r, self._panelling, self._title)
|
---|
| 635 | if isinstance(lbl, list) or isinstance(lbl, tuple):
|
---|
| 636 | if 0 <= panelcount < len(lbl):
|
---|
| 637 | lbl = lbl[panelcount]
|
---|
| 638 | else:
|
---|
| 639 | # get default label
|
---|
| 640 | lbl = self._get_label(scan, r, self._panelling, None)
|
---|
| 641 | self._plotter.set_axes('title',lbl)
|
---|
| 642 | newpanel = True
|
---|
| 643 | stackcount =0
|
---|
| 644 | panelcount += 1
|
---|
| 645 | if (b > b0 or newpanel) and stackcount < nstack:
|
---|
| 646 | y = []
|
---|
| 647 | if len(polmodes):
|
---|
| 648 | y = scan._getspectrum(r, polmodes[scan.getpol(r)])
|
---|
| 649 | else:
|
---|
| 650 | y = scan._getspectrum(r)
|
---|
| 651 | m = scan._getmask(r)
|
---|
[1146] | 652 | from matplotlib.numerix import logical_not, logical_and
|
---|
[920] | 653 | if self._maskselection and len(self._usermask) == len(m):
|
---|
| 654 | if d[self._stacking](r) in self._maskselection[self._stacking]:
|
---|
| 655 | m = logical_and(m, self._usermask)
|
---|
| 656 | x = scan._getabcissa(r)
|
---|
[1146] | 657 | from matplotlib.numerix import ma, array
|
---|
[1116] | 658 | y = ma.masked_array(y,mask=logical_not(array(m,copy=False)))
|
---|
[920] | 659 | if self._minmaxx is not None:
|
---|
| 660 | s,e = self._slice_indeces(x)
|
---|
| 661 | x = x[s:e]
|
---|
| 662 | y = y[s:e]
|
---|
[1096] | 663 | if len(x) > 1024 and rcParams['plotter.decimate']:
|
---|
| 664 | fac = len(x)/1024
|
---|
[920] | 665 | x = x[::fac]
|
---|
| 666 | y = y[::fac]
|
---|
| 667 | llbl = self._get_label(scan, r, self._stacking, self._lmap)
|
---|
| 668 | if isinstance(llbl, list) or isinstance(llbl, tuple):
|
---|
| 669 | if 0 <= stackcount < len(llbl):
|
---|
| 670 | # use user label
|
---|
| 671 | llbl = llbl[stackcount]
|
---|
| 672 | else:
|
---|
| 673 | # get default label
|
---|
| 674 | llbl = self._get_label(scan, r, self._stacking, None)
|
---|
| 675 | self._plotter.set_line(label=llbl)
|
---|
[1023] | 676 | plotit = self._plotter.plot
|
---|
| 677 | if self._hist: plotit = self._plotter.hist
|
---|
[1146] | 678 | if len(x) > 0:
|
---|
| 679 | plotit(x,y)
|
---|
| 680 | xlim= self._minmaxx or [min(x),max(x)]
|
---|
| 681 | allxlim += xlim
|
---|
| 682 | ylim= self._minmaxy or [ma.minimum(y),ma.maximum(y)]
|
---|
| 683 | allylim += ylim
|
---|
[920] | 684 | stackcount += 1
|
---|
| 685 | # last in colour stack -> autoscale x
|
---|
| 686 | if stackcount == nstack:
|
---|
| 687 | allxlim.sort()
|
---|
| 688 | self._plotter.axes.set_xlim([allxlim[0],allxlim[-1]])
|
---|
| 689 | # clear
|
---|
| 690 | allxlim =[]
|
---|
| 691 |
|
---|
| 692 | newpanel = False
|
---|
| 693 | a0=a
|
---|
| 694 | b0=b
|
---|
| 695 | # ignore following rows
|
---|
| 696 | if (panelcount == n) and (stackcount == nstack):
|
---|
[1018] | 697 | # last panel -> autoscale y if ganged
|
---|
| 698 | if rcParams['plotter.ganged']:
|
---|
| 699 | allylim.sort()
|
---|
| 700 | self._plotter.set_limits(ylim=[allylim[0],allylim[-1]])
|
---|
[998] | 701 | break
|
---|
[920] | 702 | r+=1 # next row
|
---|
[947] | 703 | #reset the selector to the scantable's original
|
---|
| 704 | scan.set_selection(savesel)
|
---|
[920] | 705 |
|
---|
| 706 | def set_selection(self, selection=None, refresh=True):
|
---|
[947] | 707 | self._selection = isinstance(selection,selector) and selection or selector()
|
---|
[920] | 708 | d0 = {'s': 'SCANNO', 'b': 'BEAMNO', 'i':'IFNO',
|
---|
| 709 | 'p': 'POLNO', 'c': 'CYCLENO', 't' : 'TIME' }
|
---|
| 710 | order = [d0[self._panelling],d0[self._stacking]]
|
---|
[947] | 711 | self._selection.set_order(order)
|
---|
[920] | 712 | if self._data and refresh: self.plot(self._data)
|
---|
| 713 |
|
---|
| 714 | def _get_selected_n(self, scan):
|
---|
[1148] | 715 | d1 = {'b': scan.getbeamnos, 's': scan.getscannos,
|
---|
| 716 | 'i': scan.getifnos, 'p': scan.getpolnos, 't': scan.ncycle }
|
---|
| 717 | d2 = { 'b': self._selection.get_beams(),
|
---|
| 718 | 's': self._selection.get_scans(),
|
---|
| 719 | 'i': self._selection.get_ifs(),
|
---|
| 720 | 'p': self._selection.get_pols(),
|
---|
| 721 | 't': self._selection.get_cycles() }
|
---|
[920] | 722 | n = d2[self._panelling] or d1[self._panelling]()
|
---|
| 723 | nstack = d2[self._stacking] or d1[self._stacking]()
|
---|
| 724 | return n,nstack
|
---|
| 725 |
|
---|
| 726 | def _get_label(self, scan, row, mode, userlabel=None):
|
---|
[1153] | 727 | if isinstance(userlabel, list) and len(userlabel) == 0:
|
---|
| 728 | userlabel = " "
|
---|
[947] | 729 | pms = dict(zip(self._selection.get_pols(),self._selection.get_poltypes()))
|
---|
[920] | 730 | if len(pms):
|
---|
| 731 | poleval = scan._getpollabel(scan.getpol(row),pms[scan.getpol(row)])
|
---|
| 732 | else:
|
---|
| 733 | poleval = scan._getpollabel(scan.getpol(row),scan.poltype())
|
---|
| 734 | d = {'b': "Beam "+str(scan.getbeam(row)),
|
---|
| 735 | 's': scan._getsourcename(row),
|
---|
| 736 | 'i': "IF"+str(scan.getif(row)),
|
---|
[964] | 737 | 'p': poleval,
|
---|
[1175] | 738 | 't': str(scan.get_time(row)) }
|
---|
[920] | 739 | return userlabel or d[mode]
|
---|
[1153] | 740 |
|
---|