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