[226] | 1 | from asap import rcParams |
---|
[709] | 2 | from numarray import logical_and |
---|
[203] | 3 | |
---|
| 4 | class asapplotter: |
---|
[226] | 5 | """ |
---|
| 6 | The ASAP plotter. |
---|
| 7 | By default the plotter is set up to plot polarisations |
---|
| 8 | 'colour stacked' and scantables across panels. |
---|
| 9 | Note: |
---|
| 10 | Currenly it only plots 'spectra' not Tsys or |
---|
| 11 | other variables. |
---|
| 12 | """ |
---|
[734] | 13 | def __init__(self, visible=None): |
---|
| 14 | self._visible = rcParams['plotter.gui'] |
---|
| 15 | if visible is not None: |
---|
| 16 | self._visible = visible |
---|
[710] | 17 | self._plotter = self._newplotter() |
---|
| 18 | |
---|
[203] | 19 | self._tdict = {'Time':'t','time':'t','t':'t','T':'t'} |
---|
| 20 | self._bdict = {'Beam':'b','beam':'b','b':'b','B':'b'} |
---|
| 21 | self._idict = {'IF':'i','if':'i','i':'i','I':'i'} |
---|
| 22 | self._pdict = {'Pol':'p','pol':'p','p':'p'} |
---|
| 23 | self._sdict = {'scan':'s','Scan':'s','s':'s','S':'s'} |
---|
[525] | 24 | self._cdict = {'t':'len(self._cursor["t"])', |
---|
| 25 | 'b':'len(self._cursor["b"])', |
---|
| 26 | 'i':'len(self._cursor["i"])', |
---|
| 27 | 'p':'len(self._cursor["p"])', |
---|
[203] | 28 | 's':'len(scans)'} |
---|
| 29 | self._ldict = {'b':'Beam', |
---|
| 30 | 'i':'IF', |
---|
| 31 | 'p':'Pol', |
---|
| 32 | 's':'Scan'} |
---|
| 33 | self._dicts = [self._tdict,self._bdict, |
---|
| 34 | self._idict,self._pdict, |
---|
| 35 | self._sdict] |
---|
[554] | 36 | self._panelling = None |
---|
| 37 | self._stacking = None |
---|
| 38 | self.set_panelling() |
---|
| 39 | self.set_stacking() |
---|
[377] | 40 | self._rows = None |
---|
| 41 | self._cols = None |
---|
[203] | 42 | self._autoplot = False |
---|
[525] | 43 | self._minmaxx = None |
---|
| 44 | self._minmaxy = None |
---|
[710] | 45 | self._datamask = None |
---|
[203] | 46 | self._data = None |
---|
[607] | 47 | self._lmap = None |
---|
[226] | 48 | self._title = None |
---|
[257] | 49 | self._ordinate = None |
---|
| 50 | self._abcissa = None |
---|
[709] | 51 | self._abcunit = None |
---|
[525] | 52 | self._cursor = {'t':None, 'b':None, |
---|
| 53 | 'i':None, 'p':None |
---|
| 54 | } |
---|
[710] | 55 | self._usermask = None |
---|
| 56 | self._usermaskspectra = None |
---|
[203] | 57 | |
---|
[710] | 58 | def _newplotter(self): |
---|
| 59 | if self._visible: |
---|
| 60 | from asap.asaplotgui import asaplotgui as asaplot |
---|
| 61 | else: |
---|
| 62 | from asap.asaplot import asaplot |
---|
| 63 | return asaplot() |
---|
| 64 | |
---|
| 65 | |
---|
[203] | 66 | def _translate(self, name): |
---|
| 67 | for d in self._dicts: |
---|
| 68 | if d.has_key(name): |
---|
| 69 | return d[name] |
---|
| 70 | return None |
---|
[709] | 71 | |
---|
[525] | 72 | def plot(self, *args): |
---|
[203] | 73 | """ |
---|
| 74 | Plot a (list of) scantables. |
---|
| 75 | Parameters: |
---|
[709] | 76 | one or more comma separated scantables |
---|
[203] | 77 | Note: |
---|
| 78 | If a (list) of scantables was specified in a previous call |
---|
| 79 | to plot, no argument has to be given to 'replot' |
---|
[525] | 80 | NO checking is done that the abcissas of the scantables |
---|
[203] | 81 | are consistent e.g. all 'channel' or all 'velocity' etc. |
---|
| 82 | """ |
---|
[710] | 83 | if self._plotter.is_dead: |
---|
| 84 | self._plotter = self._newplotter() |
---|
[600] | 85 | self._plotter.hold() |
---|
[203] | 86 | self._plotter.clear() |
---|
| 87 | if len(args) > 0: |
---|
[709] | 88 | if self._data is not None: |
---|
[525] | 89 | if list(args) != self._data: |
---|
| 90 | self._data = list(args) |
---|
[710] | 91 | # reset |
---|
| 92 | self._reset() |
---|
[525] | 93 | else: |
---|
[710] | 94 | if isinstance(args[0], list): |
---|
| 95 | self._data = args[0] |
---|
| 96 | else: |
---|
| 97 | self._data = list(args) |
---|
| 98 | self._reset() |
---|
[709] | 99 | # ranges become invalid when unit changes |
---|
| 100 | if self._abcunit != self._data[0].get_unit(): |
---|
| 101 | self._minmaxx = None |
---|
| 102 | self._minmaxy = None |
---|
| 103 | self._abcunit = self._data[0].get_unit() |
---|
[710] | 104 | self._datamask = None |
---|
[554] | 105 | if self._panelling == 't': |
---|
[626] | 106 | maxrows = 25 |
---|
[541] | 107 | if self._data[0].nrow() > maxrows: |
---|
[603] | 108 | if self._cursor["t"] is None or \ |
---|
| 109 | (isinstance(self._cursor["t"],list) and \ |
---|
| 110 | len(self._cursor["t"]) > maxrows ): |
---|
| 111 | print "Scan to be plotted contains more than %d rows.\n" \ |
---|
| 112 | "Selecting first %d rows..." % (maxrows,maxrows) |
---|
| 113 | self._cursor["t"] = range(maxrows) |
---|
[203] | 114 | self._plot_time(self._data[0], self._stacking) |
---|
[554] | 115 | elif self._panelling == 's': |
---|
[203] | 116 | self._plot_scans(self._data, self._stacking) |
---|
| 117 | else: |
---|
| 118 | self._plot_other(self._data, self._stacking) |
---|
[709] | 119 | if self._minmaxy is not None: |
---|
| 120 | self._plotter.set_limits(ylim=self._minmaxy) |
---|
[203] | 121 | self._plotter.release() |
---|
| 122 | return |
---|
| 123 | |
---|
| 124 | def _plot_time(self, scan, colmode): |
---|
| 125 | if colmode == 't': |
---|
| 126 | return |
---|
[525] | 127 | n = len(self._cursor["t"]) |
---|
[203] | 128 | cdict = {'b':'scan.setbeam(j)', |
---|
| 129 | 'i':'scan.setif(j)', |
---|
| 130 | 'p':'scan.setpol(j)'} |
---|
[525] | 131 | cdict2 = {'b':'self._cursor["b"]', |
---|
| 132 | 'i':'self._cursor["i"]', |
---|
| 133 | 'p':'self._cursor["p"]'} |
---|
| 134 | ncol = 1 |
---|
[203] | 135 | if self._stacking is not None: |
---|
| 136 | ncol = eval(self._cdict.get(colmode)) |
---|
| 137 | if n > 1: |
---|
[710] | 138 | ganged = rcParams['plotter.ganged'] |
---|
[377] | 139 | if self._rows and self._cols: |
---|
| 140 | n = min(n,self._rows*self._cols) |
---|
| 141 | self._plotter.set_panels(rows=self._rows,cols=self._cols, |
---|
[710] | 142 | nplots=n,ganged=ganged) |
---|
[377] | 143 | else: |
---|
[710] | 144 | self._plotter.set_panels(rows=n,cols=0,nplots=n,ganged=ganged) |
---|
[600] | 145 | else: |
---|
| 146 | self._plotter.set_panels() |
---|
[525] | 147 | rows = self._cursor["t"] |
---|
[652] | 148 | self._plotter.palette(0) |
---|
[525] | 149 | for rowsel in rows: |
---|
| 150 | i = self._cursor["t"].index(rowsel) |
---|
[203] | 151 | if n > 1: |
---|
[652] | 152 | self._plotter.palette(0) |
---|
[203] | 153 | self._plotter.subplot(i) |
---|
[525] | 154 | colvals = eval(cdict2.get(colmode)) |
---|
| 155 | for j in colvals: |
---|
| 156 | polmode = "raw" |
---|
| 157 | jj = colvals.index(j) |
---|
| 158 | savej = j |
---|
| 159 | for k in cdict.keys(): |
---|
[709] | 160 | sel = eval(cdict2.get(k)) |
---|
[525] | 161 | j = sel[0] |
---|
| 162 | if k == "p": |
---|
| 163 | which = self._cursor["p"].index(j) |
---|
| 164 | polmode = self._polmode[which] |
---|
| 165 | j = which |
---|
| 166 | eval(cdict.get(k)) |
---|
| 167 | j = savej |
---|
| 168 | if colmode == "p": |
---|
| 169 | polmode = self._polmode[self._cursor["p"].index(j)] |
---|
[709] | 170 | #j = jj |
---|
[203] | 171 | eval(cdict.get(colmode)) |
---|
| 172 | x = None |
---|
| 173 | y = None |
---|
| 174 | m = None |
---|
[626] | 175 | if self._title is None: |
---|
[709] | 176 | tlab = scan._getsourcename(rowsel) |
---|
[226] | 177 | else: |
---|
[626] | 178 | if len(self._title) >= n: |
---|
[525] | 179 | tlab = self._title[rowsel] |
---|
[226] | 180 | else: |
---|
[525] | 181 | tlab = scan._getsourcename(rowsel) |
---|
| 182 | x,xlab = scan.get_abcissa(rowsel) |
---|
[257] | 183 | if self._abcissa: xlab = self._abcissa |
---|
[525] | 184 | y = None |
---|
[710] | 185 | m = scan._getmask(rowsel) |
---|
| 186 | if self._usermask and self._usermask.count(j): |
---|
| 187 | m = logical_and(self._usermask, m) |
---|
[525] | 188 | if polmode == "stokes": |
---|
| 189 | y = scan._getstokesspectrum(rowsel) |
---|
| 190 | elif polmode == "stokes2": |
---|
| 191 | y = scan._getstokesspectrum(rowsel,True) |
---|
[541] | 192 | elif polmode == "circular": |
---|
| 193 | y = scan._stokestopolspectrum(rowsel,False,-1) |
---|
[525] | 194 | else: |
---|
| 195 | y = scan._getspectrum(rowsel) |
---|
[257] | 196 | if self._ordinate: |
---|
| 197 | ylab = self._ordinate |
---|
| 198 | else: |
---|
[626] | 199 | ylab = scan._get_ordinate_label() |
---|
[525] | 200 | m = scan._getmask(rowsel) |
---|
[710] | 201 | if self._datamask is not None: |
---|
| 202 | if len(m) == len(self._datamask): |
---|
| 203 | m = logical_and(m,self._datamask) |
---|
[226] | 204 | if self._lmap and len(self._lmap) > 0: |
---|
[525] | 205 | llab = self._lmap[jj] |
---|
[203] | 206 | else: |
---|
[525] | 207 | if colmode == 'p': |
---|
[603] | 208 | llab = self._get_pollabel(scan, polmode) |
---|
[709] | 209 | else: |
---|
[525] | 210 | llab = self._ldict.get(colmode)+' '+str(j) |
---|
[203] | 211 | self._plotter.set_line(label=llab) |
---|
[709] | 212 | if self._minmaxx is not None: |
---|
| 213 | s,e = self._slice_indeces(x) |
---|
| 214 | x = x[s:e] |
---|
| 215 | y = y[s:e] |
---|
| 216 | m = m[s:e] |
---|
[710] | 217 | if len(x) > 1024 and rcParams['plotter.decimate']: |
---|
| 218 | fac = len(x)/1024 |
---|
| 219 | x = x[::fac] |
---|
| 220 | m = m[::fac] |
---|
| 221 | y = y[::fac] |
---|
[203] | 222 | self._plotter.plot(x,y,m) |
---|
| 223 | xlim=[min(x),max(x)] |
---|
[709] | 224 | if self._minmaxx is not None: |
---|
[710] | 225 | xlim = self._minmaxx |
---|
| 226 | self._plotter.axes.set_xlim(xlim) |
---|
[203] | 227 | self._plotter.set_axes('xlabel',xlab) |
---|
| 228 | self._plotter.set_axes('ylabel',ylab) |
---|
[709] | 229 | self._plotter.set_axes('title',tlab) |
---|
[203] | 230 | return |
---|
| 231 | |
---|
[525] | 232 | def _plot_scans(self, scans, colmode): |
---|
[710] | 233 | print "Plotting mode is scans across panels. Can only plot one row per scan." |
---|
[203] | 234 | if colmode == 's': |
---|
| 235 | return |
---|
| 236 | cdict = {'b':'scan.setbeam(j)', |
---|
| 237 | 'i':'scan.setif(j)', |
---|
| 238 | 'p':'scan.setpol(j)'} |
---|
[525] | 239 | cdict2 = {'b':'self._cursor["b"]', |
---|
| 240 | 'i':'self._cursor["i"]', |
---|
| 241 | 'p':'self._cursor["p"]'} |
---|
[709] | 242 | |
---|
[203] | 243 | n = len(scans) |
---|
[525] | 244 | ncol = 1 |
---|
[203] | 245 | if self._stacking is not None: |
---|
| 246 | scan = scans[0] |
---|
| 247 | ncol = eval(self._cdict.get(colmode)) |
---|
| 248 | if n > 1: |
---|
[710] | 249 | ganged = rcParams['plotter.ganged'] |
---|
[377] | 250 | if self._rows and self._cols: |
---|
| 251 | n = min(n,self._rows*self._cols) |
---|
[626] | 252 | self._plotter.set_panels(rows=self._rows,cols=self._cols, |
---|
[710] | 253 | nplots=n,ganged=ganged) |
---|
[377] | 254 | else: |
---|
[710] | 255 | self._plotter.set_panels(rows=n,cols=0,nplots=n,ganged=ganged) |
---|
[600] | 256 | else: |
---|
| 257 | self._plotter.set_panels() |
---|
[709] | 258 | |
---|
[203] | 259 | for scan in scans: |
---|
[652] | 260 | self._plotter.palette(0) |
---|
[203] | 261 | if n > 1: |
---|
[525] | 262 | self._plotter.subplot(scans.index(scan)) |
---|
| 263 | colvals = eval(cdict2.get(colmode)) |
---|
| 264 | rowsel = self._cursor["t"][0] |
---|
| 265 | for j in colvals: |
---|
| 266 | polmode = "raw" |
---|
| 267 | jj = colvals.index(j) |
---|
| 268 | savej = j |
---|
| 269 | for k in cdict.keys(): |
---|
[709] | 270 | sel = eval(cdict2.get(k)) |
---|
[525] | 271 | j = sel[0] |
---|
| 272 | eval(cdict.get(k)) |
---|
| 273 | if k == "p": |
---|
| 274 | which = self._cursor["p"].index(j) |
---|
| 275 | polmode = self._polmode[which] |
---|
| 276 | j = which |
---|
| 277 | j = savej |
---|
| 278 | if colmode == "p": |
---|
| 279 | polmode = self._polmode[self._cursor["p"].index(j)] |
---|
[709] | 280 | #j = jj |
---|
[203] | 281 | eval(cdict.get(colmode)) |
---|
| 282 | x = None |
---|
| 283 | y = None |
---|
| 284 | m = None |
---|
[226] | 285 | tlab = self._title |
---|
| 286 | if not self._title: |
---|
[525] | 287 | tlab = scan._getsourcename(rowsel) |
---|
| 288 | x,xlab = scan.get_abcissa(rowsel) |
---|
[257] | 289 | if self._abcissa: xlab = self._abcissa |
---|
[525] | 290 | if polmode == "stokes": |
---|
| 291 | y = scan._getstokesspectrum(rowsel) |
---|
| 292 | elif polmode == "stokes2": |
---|
| 293 | y = scan._getstokesspectrum(rowsel,True) |
---|
[541] | 294 | elif polmode == "circular": |
---|
| 295 | y = scan._stokestopolspectrum(rowsel,False,-1) |
---|
[525] | 296 | else: |
---|
| 297 | y = scan._getspectrum(rowsel) |
---|
[257] | 298 | if self._ordinate: |
---|
| 299 | ylab = self._ordinate |
---|
| 300 | else: |
---|
[626] | 301 | ylab = scan._get_ordinate_label() |
---|
[525] | 302 | m = scan._getmask(rowsel) |
---|
[710] | 303 | if self._usermask and self._usermask.count(j): |
---|
| 304 | m = logical_and(self._usermask, m) |
---|
[257] | 305 | if self._lmap and len(self._lmap) > 0: |
---|
[525] | 306 | llab = self._lmap[jj] |
---|
[203] | 307 | else: |
---|
[525] | 308 | if colmode == 'p': |
---|
[603] | 309 | llab = self._get_pollabel(scan, polmode) |
---|
[525] | 310 | else: |
---|
| 311 | llab = self._ldict.get(colmode)+' '+str(j) |
---|
[203] | 312 | self._plotter.set_line(label=llab) |
---|
[709] | 313 | if self._minmaxx is not None: |
---|
| 314 | s,e = self._slice_indeces(x) |
---|
| 315 | x = x[s:e] |
---|
| 316 | y = y[s:e] |
---|
| 317 | m = m[s:e] |
---|
[710] | 318 | if len(x) > 1024 and rcParams['plotter.decimate']: |
---|
| 319 | fac = len(x)/1024 |
---|
| 320 | x = x[::fac] |
---|
| 321 | m = m[::fac] |
---|
| 322 | y = y[::fac] |
---|
[203] | 323 | self._plotter.plot(x,y,m) |
---|
| 324 | xlim=[min(x),max(x)] |
---|
[709] | 325 | if self._minmaxx is not None: |
---|
[710] | 326 | xlim = self._minmaxx |
---|
[203] | 327 | self._plotter.axes.set_xlim(xlim) |
---|
| 328 | |
---|
| 329 | self._plotter.set_axes('xlabel',xlab) |
---|
| 330 | self._plotter.set_axes('ylabel',ylab) |
---|
| 331 | self._plotter.set_axes('title',tlab) |
---|
| 332 | return |
---|
[709] | 333 | |
---|
[203] | 334 | def _plot_other(self,scans,colmode): |
---|
[554] | 335 | if colmode == self._panelling: |
---|
[203] | 336 | return |
---|
[525] | 337 | cdict = {'b':'scan.setbeam(i)', |
---|
| 338 | 'i':'scan.setif(i)', |
---|
| 339 | 'p':'scan.setpol(i)'} |
---|
| 340 | cdict2 = {'b':'self._cursor["b"]', |
---|
| 341 | 'i':'self._cursor["i"]', |
---|
| 342 | 'p':'self._cursor["p"]', |
---|
| 343 | 's': 'scans', |
---|
| 344 | 't': 'self._cursor["t"]'} |
---|
[203] | 345 | scan = scans[0] |
---|
[554] | 346 | n = eval(self._cdict.get(self._panelling)) |
---|
[525] | 347 | ncol=1 |
---|
[709] | 348 | if self._stacking is not None: |
---|
[203] | 349 | ncol = eval(self._cdict.get(colmode)) |
---|
| 350 | if n > 1: |
---|
[710] | 351 | ganged = rcParams['plotter.ganged'] |
---|
[377] | 352 | if self._rows and self._cols: |
---|
| 353 | n = min(n,self._rows*self._cols) |
---|
| 354 | self._plotter.set_panels(rows=self._rows,cols=self._cols, |
---|
[710] | 355 | nplots=n,ganged=ganged) |
---|
[377] | 356 | else: |
---|
[710] | 357 | self._plotter.set_panels(rows=n,cols=0,nplots=n,ganged=ganged) |
---|
[600] | 358 | else: |
---|
[709] | 359 | self._plotter.set_panels() |
---|
| 360 | panels = self._cursor[self._panelling] |
---|
[525] | 361 | for i in panels: |
---|
[652] | 362 | self._plotter.palette(0) |
---|
[525] | 363 | polmode = "raw" |
---|
[554] | 364 | ii = self._cursor[self._panelling].index(i) |
---|
[203] | 365 | if n>1: |
---|
[525] | 366 | self._plotter.subplot(ii) |
---|
[554] | 367 | if self._panelling == "p": |
---|
[525] | 368 | polmode = self._polmode[ii] |
---|
[554] | 369 | eval(cdict.get(self._panelling)) |
---|
[525] | 370 | else: |
---|
[554] | 371 | eval(cdict.get(self._panelling)) |
---|
[525] | 372 | colvals = eval(cdict2.get(colmode)) |
---|
| 373 | for j in colvals: |
---|
| 374 | rowsel = self._cursor["t"][0] |
---|
| 375 | jj = colvals.index(j) |
---|
| 376 | savei = i |
---|
| 377 | for k in cdict.keys(): |
---|
[554] | 378 | if k != self._panelling: |
---|
[525] | 379 | sel = eval(cdict2.get(k)) |
---|
| 380 | i = sel[0] |
---|
| 381 | if k == "p": |
---|
[557] | 382 | which = self._cursor["p"].index(i) |
---|
[525] | 383 | polmode = self._polmode[which] |
---|
[709] | 384 | i = which |
---|
[525] | 385 | eval(cdict.get(k)) |
---|
| 386 | i = savei |
---|
[203] | 387 | if colmode == 's': |
---|
[525] | 388 | scan = j |
---|
[203] | 389 | elif colmode == 't': |
---|
[709] | 390 | rowsel = j |
---|
[203] | 391 | else: |
---|
[525] | 392 | savei = i |
---|
| 393 | if colmode == 'p': |
---|
| 394 | polmode = self._polmode[self._cursor["p"].index(j)] |
---|
| 395 | i = j |
---|
[203] | 396 | eval(cdict.get(colmode)) |
---|
[525] | 397 | i = savei |
---|
[203] | 398 | x = None |
---|
| 399 | y = None |
---|
| 400 | m = None |
---|
[525] | 401 | x,xlab = scan.get_abcissa(rowsel) |
---|
[257] | 402 | if self._abcissa: xlab = self._abcissa |
---|
[525] | 403 | if polmode == "stokes": |
---|
| 404 | y = scan._getstokesspectrum(rowsel) |
---|
| 405 | elif polmode == "stokes2": |
---|
| 406 | y = scan._getstokesspectrum(rowsel,True) |
---|
[541] | 407 | elif polmode == "circular": |
---|
| 408 | y = scan._stokestopolspectrum(rowsel,False,-1) |
---|
[525] | 409 | else: |
---|
| 410 | y = scan._getspectrum(rowsel) |
---|
| 411 | |
---|
[257] | 412 | if self._ordinate: |
---|
| 413 | ylab = self._ordinate |
---|
| 414 | else: |
---|
[626] | 415 | ylab = scan._get_ordinate_label() |
---|
[525] | 416 | m = scan._getmask(rowsel) |
---|
[710] | 417 | if self._usermask and self._usermask.count(j): |
---|
| 418 | m = logical_and(self._usermask, m) |
---|
| 419 | |
---|
[203] | 420 | if colmode == 's' or colmode == 't': |
---|
[525] | 421 | if self._title and len(self._title) > 0: |
---|
| 422 | tlab = self._title[ii] |
---|
[709] | 423 | else: |
---|
| 424 | if self._panelling == 'p': |
---|
| 425 | tlab = self._get_pollabel(scan, polmode) |
---|
| 426 | else: |
---|
| 427 | tlab = self._ldict.get(self._panelling)+' '+str(i) |
---|
[607] | 428 | if self._lmap and len(self._lmap) > 0: |
---|
| 429 | llab = self._lmap[jj] |
---|
| 430 | else: |
---|
| 431 | llab = scan._getsourcename(rowsel) |
---|
[203] | 432 | else: |
---|
[226] | 433 | if self._title and len(self._title) > 0: |
---|
[525] | 434 | tlab = self._title[ii] |
---|
[226] | 435 | else: |
---|
[603] | 436 | if self._panelling == 'p': |
---|
| 437 | tlab = self._get_pollabel(scan, polmode) |
---|
| 438 | else: |
---|
| 439 | tlab = self._ldict.get(self._panelling)+' '+str(i) |
---|
[226] | 440 | if self._lmap and len(self._lmap) > 0: |
---|
[525] | 441 | llab = self._lmap[jj] |
---|
[203] | 442 | else: |
---|
[525] | 443 | if colmode == 'p': |
---|
[603] | 444 | llab = self._get_pollabel(scan, polmode) |
---|
[525] | 445 | else: |
---|
| 446 | llab = self._ldict.get(colmode)+' '+str(j) |
---|
[203] | 447 | self._plotter.set_line(label=llab) |
---|
[709] | 448 | if self._minmaxx is not None: |
---|
| 449 | s,e = self._slice_indeces(x) |
---|
| 450 | x = x[s:e] |
---|
| 451 | y = y[s:e] |
---|
| 452 | m = m[s:e] |
---|
[710] | 453 | if len(x) > 1024 and rcParams['plotter.decimate']: |
---|
| 454 | fac = len(x)/1024 |
---|
| 455 | x = x[::fac] |
---|
| 456 | m = m[::fac] |
---|
| 457 | y = y[::fac] |
---|
[203] | 458 | self._plotter.plot(x,y,m) |
---|
| 459 | xlim=[min(x),max(x)] |
---|
[709] | 460 | if self._minmaxx is not None: |
---|
[710] | 461 | xlim = self._minmaxx |
---|
[203] | 462 | self._plotter.axes.set_xlim(xlim) |
---|
| 463 | |
---|
| 464 | self._plotter.set_axes('xlabel',xlab) |
---|
| 465 | self._plotter.set_axes('ylabel',ylab) |
---|
| 466 | self._plotter.set_axes('title',tlab) |
---|
[709] | 467 | |
---|
[203] | 468 | return |
---|
| 469 | |
---|
| 470 | |
---|
[226] | 471 | def set_mode(self, stacking=None, panelling=None): |
---|
[203] | 472 | """ |
---|
[377] | 473 | Set the plots look and feel, i.e. what you want to see on the plot. |
---|
[203] | 474 | Parameters: |
---|
| 475 | stacking: tell the plotter which variable to plot |
---|
[710] | 476 | as line color overlays (default 'pol') |
---|
[203] | 477 | panelling: tell the plotter which variable to plot |
---|
| 478 | across multiple panels (default 'scan' |
---|
| 479 | Note: |
---|
| 480 | Valid modes are: |
---|
| 481 | 'beam' 'Beam' 'b': Beams |
---|
| 482 | 'if' 'IF' 'i': IFs |
---|
| 483 | 'pol' 'Pol' 'p': Polarisations |
---|
| 484 | 'scan' 'Scan' 's': Scans |
---|
| 485 | 'time' 'Time' 't': Times |
---|
| 486 | """ |
---|
[554] | 487 | if not self.set_panelling(panelling): |
---|
[203] | 488 | print "Invalid mode" |
---|
[226] | 489 | return |
---|
[203] | 490 | if not self.set_stacking(stacking): |
---|
| 491 | print "Invalid mode" |
---|
[226] | 492 | return |
---|
| 493 | if self._data: self.plot() |
---|
[203] | 494 | return |
---|
| 495 | |
---|
[554] | 496 | def set_panelling(self, what=None): |
---|
| 497 | mode = what |
---|
| 498 | if mode is None: |
---|
| 499 | mode = rcParams['plotter.panelling'] |
---|
| 500 | md = self._translate(mode) |
---|
[203] | 501 | if md: |
---|
[554] | 502 | self._panelling = md |
---|
[226] | 503 | self._title = None |
---|
[203] | 504 | return True |
---|
| 505 | return False |
---|
| 506 | |
---|
[377] | 507 | def set_layout(self,rows=None,cols=None): |
---|
| 508 | """ |
---|
| 509 | Set the multi-panel layout, i.e. how many rows and columns plots |
---|
| 510 | are visible. |
---|
| 511 | Parameters: |
---|
| 512 | rows: The number of rows of plots |
---|
| 513 | cols: The number of columns of plots |
---|
| 514 | Note: |
---|
| 515 | If no argument is given, the potter reverts to its auto-plot |
---|
| 516 | behaviour. |
---|
| 517 | """ |
---|
| 518 | self._rows = rows |
---|
| 519 | self._cols = cols |
---|
| 520 | if self._data: self.plot() |
---|
| 521 | return |
---|
| 522 | |
---|
[709] | 523 | def set_stacking(self, what=None): |
---|
[554] | 524 | mode = what |
---|
[709] | 525 | if mode is None: |
---|
| 526 | mode = rcParams['plotter.stacking'] |
---|
[554] | 527 | md = self._translate(mode) |
---|
[203] | 528 | if md: |
---|
| 529 | self._stacking = md |
---|
[226] | 530 | self._lmap = None |
---|
[203] | 531 | return True |
---|
| 532 | return False |
---|
| 533 | |
---|
[525] | 534 | def set_range(self,xstart=None,xend=None,ystart=None,yend=None): |
---|
[203] | 535 | """ |
---|
| 536 | Set the range of interest on the abcissa of the plot |
---|
| 537 | Parameters: |
---|
[525] | 538 | [x,y]start,[x,y]end: The start and end points of the 'zoom' window |
---|
[203] | 539 | Note: |
---|
| 540 | These become non-sensical when the unit changes. |
---|
| 541 | use plotter.set_range() without parameters to reset |
---|
| 542 | |
---|
| 543 | """ |
---|
[525] | 544 | if xstart is None and xend is None: |
---|
| 545 | self._minmaxx = None |
---|
[600] | 546 | else: |
---|
| 547 | self._minmaxx = [xstart,xend] |
---|
[525] | 548 | if ystart is None and yend is None: |
---|
| 549 | self._minmaxy = None |
---|
[600] | 550 | else: |
---|
[709] | 551 | self._minmaxy = [ystart,yend] |
---|
[525] | 552 | if self._data: self.plot() |
---|
[203] | 553 | return |
---|
[709] | 554 | |
---|
[257] | 555 | def set_legend(self, mp=None): |
---|
[203] | 556 | """ |
---|
| 557 | Specify a mapping for the legend instead of using the default |
---|
| 558 | indices: |
---|
| 559 | Parameters: |
---|
| 560 | mp: a list of 'strings'. This should have the same length |
---|
| 561 | as the number of elements on the legend and then maps |
---|
[710] | 562 | to the indeces in order. It is possible to uses latex |
---|
| 563 | math expression. These have to be enclosed in r'', e.g. r'$x^{2}$' |
---|
[203] | 564 | |
---|
| 565 | Example: |
---|
[485] | 566 | If the data has two IFs/rest frequencies with index 0 and 1 |
---|
[203] | 567 | for CO and SiO: |
---|
| 568 | plotter.set_stacking('i') |
---|
[710] | 569 | plotter.set_legend(['CO','SiO']) |
---|
[203] | 570 | plotter.plot() |
---|
[710] | 571 | plotter.set_legend([r'$^{12}CO$', r'SiO']) |
---|
[203] | 572 | """ |
---|
| 573 | self._lmap = mp |
---|
[226] | 574 | if self._data: self.plot() |
---|
| 575 | return |
---|
| 576 | |
---|
| 577 | def set_title(self, title=None): |
---|
[710] | 578 | """ |
---|
| 579 | Set the title of the plot. If multiple panels are plotted, |
---|
| 580 | multiple titles have to be specified. |
---|
| 581 | Example: |
---|
| 582 | # two panels are visible on the plotter |
---|
| 583 | plotter.set_title(["First Panel","Second Panel"]) |
---|
| 584 | """ |
---|
[226] | 585 | self._title = title |
---|
| 586 | if self._data: self.plot() |
---|
| 587 | return |
---|
| 588 | |
---|
[257] | 589 | def set_ordinate(self, ordinate=None): |
---|
[710] | 590 | """ |
---|
| 591 | Set the y-axis label of the plot. If multiple panels are plotted, |
---|
| 592 | multiple labels have to be specified. |
---|
| 593 | Example: |
---|
| 594 | # two panels are visible on the plotter |
---|
| 595 | plotter.set_ordinate(["First Y-Axis","Second Y-Axis"]) |
---|
| 596 | """ |
---|
[257] | 597 | self._ordinate = ordinate |
---|
| 598 | if self._data: self.plot() |
---|
| 599 | return |
---|
| 600 | |
---|
| 601 | def set_abcissa(self, abcissa=None): |
---|
[710] | 602 | """ |
---|
| 603 | Set the x-axis label of the plot. If multiple panels are plotted, |
---|
| 604 | multiple labels have to be specified. |
---|
| 605 | Example: |
---|
| 606 | # two panels are visible on the plotter |
---|
| 607 | plotter.set_ordinate(["First X-Axis","Second X-Axis"]) |
---|
| 608 | """ |
---|
[257] | 609 | self._abcissa = abcissa |
---|
| 610 | if self._data: self.plot() |
---|
| 611 | return |
---|
| 612 | |
---|
[710] | 613 | def set_colors(self, colormap): |
---|
[377] | 614 | """ |
---|
[710] | 615 | Set the colors to be used. The plotter will cycle through |
---|
| 616 | these colors when lines are overlaid (stacking mode). |
---|
| 617 | Example: |
---|
| 618 | plotter.set_colors("red green blue") |
---|
| 619 | # If for example four lines are overlaid e.g I Q U V |
---|
| 620 | # 'I' will be 'red', 'Q' will be 'green', U will be 'blue' |
---|
| 621 | # and 'V' will be 'red' again. |
---|
| 622 | """ |
---|
| 623 | if isinstance(colormap,str): |
---|
| 624 | colormap = colormap.split() |
---|
| 625 | self._plotter.palette(0,colormap=colormap) |
---|
| 626 | if self._data: self.plot() |
---|
| 627 | |
---|
| 628 | def set_linestyles(self, linestyles): |
---|
| 629 | """ |
---|
[734] | 630 | Set the linestyles to be used. The plotter will cycle through |
---|
| 631 | these linestyles when lines are overlaid (stacking mode) AND |
---|
| 632 | only one color has been set. |
---|
[710] | 633 | Parameters: |
---|
| 634 | linestyles: a list of linestyles to use. |
---|
| 635 | 'line', 'dashed', 'dotted', 'dashdot', |
---|
| 636 | 'dashdotdot' and 'dashdashdot' are |
---|
| 637 | possible |
---|
| 638 | |
---|
| 639 | Example: |
---|
| 640 | plotter.set_colors("black") |
---|
| 641 | plotter.set_linestyles("line dashed dotted dashdot") |
---|
| 642 | # If for example four lines are overlaid e.g I Q U V |
---|
| 643 | # 'I' will be 'solid', 'Q' will be 'dashed', |
---|
| 644 | # U will be 'dotted' and 'V' will be 'dashdot'. |
---|
| 645 | """ |
---|
| 646 | if isinstance(linestyles,str): |
---|
| 647 | linestyles = linestyles.split() |
---|
| 648 | self._plotter.palette(color=0,linestyle=0,linestyles=linestyles) |
---|
| 649 | if self._data: self.plot() |
---|
| 650 | |
---|
| 651 | def save(self, filename=None, orientation=None, dpi=None): |
---|
| 652 | """ |
---|
[377] | 653 | Save the plot to a file. The know formats are 'png', 'ps', 'eps'. |
---|
| 654 | Parameters: |
---|
| 655 | filename: The name of the output file. This is optional |
---|
| 656 | and autodetects the image format from the file |
---|
| 657 | suffix. If non filename is specified a file |
---|
| 658 | called 'yyyymmdd_hhmmss.png' is created in the |
---|
| 659 | current directory. |
---|
[709] | 660 | orientation: optional parameter for postscript only (not eps). |
---|
| 661 | 'landscape', 'portrait' or None (default) are valid. |
---|
| 662 | If None is choosen for 'ps' output, the plot is |
---|
| 663 | automatically oriented to fill the page. |
---|
[710] | 664 | dpi: The dpi of the output non-ps plot |
---|
[377] | 665 | """ |
---|
[709] | 666 | self._plotter.save(filename,orientation,dpi) |
---|
[377] | 667 | return |
---|
[709] | 668 | |
---|
[541] | 669 | def set_cursor(self, row=None,beam=None,IF=None,pol=None, refresh=True): |
---|
[525] | 670 | """ |
---|
| 671 | Specify a 'cursor' for plotting selected spectra. Time (rows), |
---|
| 672 | Beam, IF, Polarisation ranges can be specified. |
---|
| 673 | Parameters: |
---|
| 674 | Default for all paramaters is to select all available |
---|
| 675 | row: selects the rows (time stamps) to be plotted, this has |
---|
| 676 | to be a vector of row indices, e.g. row=[0,2,5] or row=[2] |
---|
| 677 | beam: select a range of beams |
---|
| 678 | IF: select a range of IFs |
---|
| 679 | pol: select Polarisations for plotting these can be by index |
---|
| 680 | (raw polarisations (default)) or by names any of: |
---|
| 681 | ["I", "Q", "U", "V"] or |
---|
| 682 | ["I", "Plinear", "Pangle", "V"] or |
---|
[541] | 683 | ["XX", "YY", "Real(XY)", "Imag(XY)"] or |
---|
| 684 | ["RR", "LL"] |
---|
[525] | 685 | Example: |
---|
| 686 | plotter.set_mode('pol','time') |
---|
| 687 | plotter.plot(myscan) # plots all raw polarisations colour stacked |
---|
| 688 | plotter.set_cursor(pol=["I"]) # plot "I" only for all rows |
---|
| 689 | # plot "I" only for two time stamps row=0 and row=2 |
---|
| 690 | plotter.set_cursor(row=[0,2],pol=["I"]) |
---|
[257] | 691 | |
---|
[525] | 692 | Note: |
---|
[709] | 693 | Be careful to select only exisiting polarisations. |
---|
[525] | 694 | """ |
---|
| 695 | if not self._data: |
---|
| 696 | print "Can only set cursor after a first call to plot()" |
---|
| 697 | return |
---|
[709] | 698 | |
---|
[525] | 699 | n = self._data[0].nrow() |
---|
| 700 | if row is None: |
---|
| 701 | self._cursor["t"] = range(n) |
---|
| 702 | else: |
---|
| 703 | for i in row: |
---|
[554] | 704 | if i < 0 or i >= n: |
---|
[525] | 705 | print "Row index '%d' out of range" % i |
---|
| 706 | return |
---|
| 707 | self._cursor["t"] = row |
---|
| 708 | |
---|
| 709 | n = self._data[0].nbeam() |
---|
| 710 | if beam is None: |
---|
| 711 | self._cursor["b"] = range(n) |
---|
| 712 | else: |
---|
| 713 | for i in beam: |
---|
[554] | 714 | if i < 0 or i >= n: |
---|
[525] | 715 | print "Beam index '%d' out of range" % i |
---|
[709] | 716 | return |
---|
[525] | 717 | self._cursor["b"] = beam |
---|
| 718 | |
---|
| 719 | n = self._data[0].nif() |
---|
| 720 | if IF is None: |
---|
| 721 | self._cursor["i"] = range(n) |
---|
| 722 | else: |
---|
| 723 | for i in IF: |
---|
[554] | 724 | if i < 0 or i >= n: |
---|
[525] | 725 | print "IF index '%d' out of range" %i |
---|
[709] | 726 | return |
---|
| 727 | self._cursor["i"] = IF |
---|
[525] | 728 | |
---|
| 729 | n = self._data[0].npol() |
---|
| 730 | dstokes = {"I":0,"Q":1,"U":2,"V":3} |
---|
| 731 | dstokes2 = {"I":0,"Plinear":1,"Pangle":2,"V":3} |
---|
| 732 | draw = {"XX":0, "YY":1,"Real(XY)":2, "Imag(XY)":3} |
---|
[710] | 733 | dcirc = { "RR":0,"LL":1}#,"Real(RL)":2,"Imag(RL)":3} |
---|
[709] | 734 | |
---|
[525] | 735 | if pol is None: |
---|
| 736 | self._cursor["p"] = range(n) |
---|
| 737 | self._polmode = ["raw" for i in range(n)] |
---|
| 738 | else: |
---|
| 739 | if isinstance(pol,str): |
---|
| 740 | pol = pol.split() |
---|
| 741 | polmode = [] |
---|
| 742 | pols = [] |
---|
| 743 | for i in pol: |
---|
| 744 | if isinstance(i,str): |
---|
| 745 | if draw.has_key(i): |
---|
| 746 | pols.append(draw.get(i)) |
---|
| 747 | polmode.append("raw") |
---|
| 748 | elif dstokes.has_key(i): |
---|
| 749 | pols.append(dstokes.get(i)) |
---|
| 750 | polmode.append("stokes") |
---|
| 751 | elif dstokes2.has_key(i): |
---|
| 752 | pols.append(dstokes2.get(i)) |
---|
| 753 | polmode.append("stokes2") |
---|
| 754 | elif dcirc.has_key(i): |
---|
| 755 | pols.append(dcirc.get(i)) |
---|
[541] | 756 | polmode.append("circular") |
---|
[525] | 757 | else: |
---|
[652] | 758 | print "Pol type '%s' not valid" %i |
---|
[525] | 759 | return |
---|
| 760 | elif 0 > i >= n: |
---|
| 761 | print "Pol index '%d' out of range" %i |
---|
| 762 | return |
---|
| 763 | else: |
---|
| 764 | pols.append(i) |
---|
| 765 | polmode.append("raw") |
---|
| 766 | self._cursor["p"] = pols |
---|
| 767 | self._polmode = polmode |
---|
[541] | 768 | if self._data and refresh: self.plot() |
---|
[525] | 769 | |
---|
[710] | 770 | def set_mask(self, mask=None, pol=None): |
---|
[734] | 771 | """ |
---|
| 772 | Set a plotting mask for a specific polarization. |
---|
| 773 | This is useful for masking out "noise" Pangle outside a source. |
---|
| 774 | Parameters: |
---|
| 775 | mask: a mask from scantable.create_mask |
---|
| 776 | pol: the polarisation to apply the mask to, e.g |
---|
| 777 | "Pangle" or "XX" etc. |
---|
| 778 | Example: |
---|
| 779 | """ |
---|
[710] | 780 | if not self._data: |
---|
| 781 | print "Can only set cursor after a first call to plot()" |
---|
| 782 | return |
---|
| 783 | if isinstance(mask, array): |
---|
| 784 | self._usermask = mask |
---|
| 785 | if isinstance(mask, list): |
---|
| 786 | self._usermask = array(mask) |
---|
| 787 | if mask is None and pol is None: |
---|
| 788 | self._usermask = None |
---|
| 789 | self._usermaskspectra = None |
---|
| 790 | |
---|
| 791 | dstokes = {"I":0,"Q":1,"U":2,"V":3} |
---|
| 792 | dstokes2 = {"I":0,"Plinear":1,"Pangle":2,"V":3} |
---|
| 793 | draw = {"XX":0, "YY":1,"Real(XY)":2, "Imag(XY)":3} |
---|
| 794 | dcirc = { "RR":0,"LL":1}#,"Real(RL)":2,"Imag(RL)":3} |
---|
| 795 | if isinstance(pol, str): |
---|
| 796 | pol = pol.split() |
---|
| 797 | if isinstance(pol, list): |
---|
| 798 | if isinstance(pol[0], str): |
---|
| 799 | pass |
---|
| 800 | else: |
---|
| 801 | cpos = self._cursor[self._stacking] |
---|
| 802 | self._usermaskspectra =filter(lambda i: filter(lambda j: j==i ,cpos),pol) |
---|
| 803 | else: |
---|
| 804 | return |
---|
| 805 | self.plot() |
---|
| 806 | |
---|
[603] | 807 | def _get_pollabel(self, scan, polmode): |
---|
| 808 | tlab = "" |
---|
| 809 | if polmode == "stokes": |
---|
| 810 | tlab = scan._getpolarizationlabel(0,1,0) |
---|
| 811 | elif polmode == "stokes2": |
---|
| 812 | tlab = scan._getpolarizationlabel(0,1,1) |
---|
| 813 | elif polmode == "circular": |
---|
| 814 | tlab = scan._getpolarizationlabel(0,0,0) |
---|
| 815 | else: |
---|
| 816 | tlab = scan._getpolarizationlabel(1,0,0) |
---|
| 817 | return tlab |
---|
[709] | 818 | |
---|
| 819 | def _slice_indeces(self, data): |
---|
| 820 | mn = self._minmaxx[0] |
---|
| 821 | mx = self._minmaxx[1] |
---|
| 822 | asc = data[0] < data[-1] |
---|
| 823 | start=0 |
---|
| 824 | end = len(data)-1 |
---|
| 825 | inc = 1 |
---|
| 826 | if not asc: |
---|
| 827 | start = len(data)-1 |
---|
| 828 | end = 0 |
---|
| 829 | inc = -1 |
---|
| 830 | # find min index |
---|
| 831 | while data[start] < mn: |
---|
| 832 | start+= inc |
---|
| 833 | # find max index |
---|
| 834 | while data[end] > mx: |
---|
| 835 | end-=inc |
---|
| 836 | end +=1 |
---|
| 837 | if start > end: |
---|
| 838 | return end,start |
---|
| 839 | return start,end |
---|
| 840 | |
---|
[710] | 841 | def _reset(self): |
---|
| 842 | self._usermask = None |
---|
| 843 | self._usermaskspectra = None |
---|
| 844 | self.set_cursor(refresh=False) |
---|