1 | from asap.asaplot import ASAPlot |
---|
2 | |
---|
3 | class asapplotter: |
---|
4 | def __init__(self): |
---|
5 | """ |
---|
6 | The ASAP plotter. |
---|
7 | By default the plotter is set up to plot polarisations |
---|
8 | 'colour stacked' and scantables across panels. |
---|
9 | The defaul plotter is called 'plotter'. |
---|
10 | Note: |
---|
11 | Currenly it only plots 'spectra' not Tsys or |
---|
12 | other variables. |
---|
13 | |
---|
14 | """ |
---|
15 | self._plotter = ASAPlot() |
---|
16 | |
---|
17 | self._tdict = {'Time':'t','time':'t','t':'t','T':'t'} |
---|
18 | self._bdict = {'Beam':'b','beam':'b','b':'b','B':'b'} |
---|
19 | self._idict = {'IF':'i','if':'i','i':'i','I':'i'} |
---|
20 | self._pdict = {'Pol':'p','pol':'p','p':'p'} |
---|
21 | self._sdict = {'scan':'s','Scan':'s','s':'s','S':'s'} |
---|
22 | self._cdict = {'t':'scan.nrow()', |
---|
23 | 'b':'scan.nbeam()', |
---|
24 | 'i':'scan.nif()', |
---|
25 | 'p':'scan.npol()', |
---|
26 | 's':'len(scans)'} |
---|
27 | self._ldict = {'b':'Beam', |
---|
28 | 'i':'IF', |
---|
29 | 'p':'Pol', |
---|
30 | 's':'Scan'} |
---|
31 | self._dicts = [self._tdict,self._bdict, |
---|
32 | self._idict,self._pdict, |
---|
33 | self._sdict] |
---|
34 | self._panels = 's' |
---|
35 | self._stacking = 'p' |
---|
36 | self._autoplot = False |
---|
37 | self._minmax = None |
---|
38 | self._data = None |
---|
39 | self._lmap = [] |
---|
40 | |
---|
41 | def _translate(self, name): |
---|
42 | for d in self._dicts: |
---|
43 | if d.has_key(name): |
---|
44 | return d[name] |
---|
45 | return None |
---|
46 | |
---|
47 | def plot(self,*args): |
---|
48 | """ |
---|
49 | Plot a (list of) scantables. |
---|
50 | Parameters: |
---|
51 | one or more comma separated scantables |
---|
52 | Note: |
---|
53 | If a (list) of scantables was specified in a previous call |
---|
54 | to plot, no argument has to be given to 'replot' |
---|
55 | NO checking is done that the abscissas of the scantables |
---|
56 | are consistent e.g. all 'channel' or all 'velocity' etc. |
---|
57 | """ |
---|
58 | if self._plotter.is_dead: |
---|
59 | self._plotter = ASAPlot() |
---|
60 | self._plotter.clear() |
---|
61 | self._plotter.hold() |
---|
62 | if len(args) > 0: |
---|
63 | self._data = tuple(args) |
---|
64 | if self._panels == 't': |
---|
65 | if self._data[0].nrow() > 25: |
---|
66 | print "Scan to be plotted contains more than 25 rows.\nCan't plot that many panels..." |
---|
67 | return |
---|
68 | self._plot_time(self._data[0], self._stacking) |
---|
69 | elif self._panels == 's': |
---|
70 | self._plot_scans(self._data, self._stacking) |
---|
71 | else: |
---|
72 | self._plot_other(self._data, self._stacking) |
---|
73 | if self._minmax is not None: |
---|
74 | self._plotter.set_limits(xlim=self._minmax) |
---|
75 | self._plotter.release() |
---|
76 | return |
---|
77 | |
---|
78 | def _plot_time(self, scan, colmode): |
---|
79 | if colmode == 't': |
---|
80 | return |
---|
81 | n = scan.nrow() |
---|
82 | cdict = {'b':'scan.setbeam(j)', |
---|
83 | 'i':'scan.setif(j)', |
---|
84 | 'p':'scan.setpol(j)'} |
---|
85 | if self._stacking is not None: |
---|
86 | ncol = eval(self._cdict.get(colmode)) |
---|
87 | self._plotter.set_panels() |
---|
88 | if n > 1: |
---|
89 | self._plotter.set_panels(rows=n) |
---|
90 | for i in range(n): |
---|
91 | if n > 1: |
---|
92 | self._plotter.palette(0) |
---|
93 | self._plotter.subplot(i) |
---|
94 | for j in range(ncol): |
---|
95 | eval(cdict.get(colmode)) |
---|
96 | x = None |
---|
97 | y = None |
---|
98 | m = None |
---|
99 | tlab = scan._getsourcename(i) |
---|
100 | x,xlab = scan.get_abcissa(i) |
---|
101 | y = scan.getspectrum(i) |
---|
102 | ylab = 'Flux ('+scan.get_fluxunit()+')' |
---|
103 | m = scan.getmask(i) |
---|
104 | if len(self._lmap) > 0: |
---|
105 | llab = self._lmap[j] |
---|
106 | else: |
---|
107 | llab = self._ldict.get(colmode)+' '+str(j) |
---|
108 | self._plotter.set_line(label=llab) |
---|
109 | self._plotter.plot(x,y,m) |
---|
110 | xlim=[min(x),max(x)] |
---|
111 | self._plotter.axes.set_xlim(xlim) |
---|
112 | self._plotter.set_axes('xlabel',xlab) |
---|
113 | self._plotter.set_axes('ylabel',ylab) |
---|
114 | self._plotter.set_axes('title',tlab) |
---|
115 | return |
---|
116 | |
---|
117 | def _plot_scans(self, scans, colmode): |
---|
118 | if colmode == 's': |
---|
119 | return |
---|
120 | cdict = {'b':'scan.setbeam(j)', |
---|
121 | 'i':'scan.setif(j)', |
---|
122 | 'p':'scan.setpol(j)'} |
---|
123 | n = len(scans) |
---|
124 | if self._stacking is not None: |
---|
125 | scan = scans[0] |
---|
126 | ncol = eval(self._cdict.get(colmode)) |
---|
127 | self._plotter.set_panels() |
---|
128 | if n > 1: |
---|
129 | self._plotter.set_panels(rows=n) |
---|
130 | i = 0 |
---|
131 | for scan in scans: |
---|
132 | if n > 1: |
---|
133 | self._plotter.subplot(i) |
---|
134 | self._plotter.palette(0) |
---|
135 | for j in range(ncol): |
---|
136 | eval(cdict.get(colmode)) |
---|
137 | x = None |
---|
138 | y = None |
---|
139 | m = None |
---|
140 | tlab = scan._getsourcename() |
---|
141 | x,xlab = scan.get_abcissa() |
---|
142 | y = scan.getspectrum() |
---|
143 | ylab = 'Flux ('+scan.get_fluxunit()+')' |
---|
144 | m = scan.getmask() |
---|
145 | if len(self._lmap) > 0: |
---|
146 | llab = self._lmap[j] |
---|
147 | else: |
---|
148 | llab = self._ldict.get(colmode)+' '+str(j) |
---|
149 | self._plotter.set_line(label=llab) |
---|
150 | self._plotter.plot(x,y,m) |
---|
151 | xlim=[min(x),max(x)] |
---|
152 | self._plotter.axes.set_xlim(xlim) |
---|
153 | |
---|
154 | self._plotter.set_axes('xlabel',xlab) |
---|
155 | self._plotter.set_axes('ylabel',ylab) |
---|
156 | self._plotter.set_axes('title',tlab) |
---|
157 | i += 1 |
---|
158 | return |
---|
159 | |
---|
160 | def _plot_other(self,scans,colmode): |
---|
161 | if colmode == self._panels: |
---|
162 | return |
---|
163 | cdict = {'b':'scan.setbeam(j)', |
---|
164 | 'i':'scan.setif(j)', |
---|
165 | 'p':'scan.setpol(j)', |
---|
166 | 's':'scans[j]'} |
---|
167 | scan = scans[0] |
---|
168 | n = eval(self._cdict.get(self._panels)) |
---|
169 | if self._stacking is not None: |
---|
170 | ncol = eval(self._cdict.get(colmode)) |
---|
171 | self._plotter.set_panels() |
---|
172 | if n > 1: |
---|
173 | self._plotter.set_panels(rows=n) |
---|
174 | for i in range(n): |
---|
175 | if n>1: |
---|
176 | self._plotter.subplot(i) |
---|
177 | self._plotter.palette(0) |
---|
178 | k=0 |
---|
179 | j=i |
---|
180 | eval(cdict.get(self._panels)) |
---|
181 | for j in range(ncol): |
---|
182 | if colmode == 's': |
---|
183 | scan = eval(cdict.get(colmode)) |
---|
184 | elif colmode == 't': |
---|
185 | k = j |
---|
186 | else: |
---|
187 | eval(cdict.get(colmode)) |
---|
188 | x = None |
---|
189 | y = None |
---|
190 | m = None |
---|
191 | x,xlab = scan.get_abcissa(k) |
---|
192 | y = scan.getspectrum(k) |
---|
193 | ylab = 'Flux ('+scan.get_fluxunit()+')' |
---|
194 | m = scan.getmask(k) |
---|
195 | if colmode == 's' or colmode == 't': |
---|
196 | tlab = self._ldict.get(self._panels)+' '+str(i) |
---|
197 | llab = scan._getsourcename(k) |
---|
198 | else: |
---|
199 | tlab = scan._getsourcename(k) |
---|
200 | if len(self._lmap) > 0: |
---|
201 | llab = self._lmap[j] |
---|
202 | else: |
---|
203 | llab = self._ldict.get(colmode)+' '+str(j) |
---|
204 | self._plotter.set_line(label=llab) |
---|
205 | self._plotter.plot(x,y,m) |
---|
206 | xlim=[min(x),max(x)] |
---|
207 | self._plotter.axes.set_xlim(xlim) |
---|
208 | |
---|
209 | self._plotter.set_axes('xlabel',xlab) |
---|
210 | self._plotter.set_axes('ylabel',ylab) |
---|
211 | self._plotter.set_axes('title',tlab) |
---|
212 | |
---|
213 | return |
---|
214 | |
---|
215 | |
---|
216 | def set_mode(self, stacking='pol', panelling='scan'): |
---|
217 | """ |
---|
218 | Parameters: |
---|
219 | stacking: tell the plotter which variable to plot |
---|
220 | as line colour overlays (default 'pol') |
---|
221 | panelling: tell the plotter which variable to plot |
---|
222 | across multiple panels (default 'scan' |
---|
223 | Note: |
---|
224 | Valid modes are: |
---|
225 | 'beam' 'Beam' 'b': Beams |
---|
226 | 'if' 'IF' 'i': IFs |
---|
227 | 'pol' 'Pol' 'p': Polarisations |
---|
228 | 'scan' 'Scan' 's': Scans |
---|
229 | 'time' 'Time' 't': Times |
---|
230 | """ |
---|
231 | if not self.set_panels(panelling): |
---|
232 | print "Invalid mode" |
---|
233 | if not self.set_stacking(stacking): |
---|
234 | print "Invalid mode" |
---|
235 | return |
---|
236 | |
---|
237 | def set_panels(self, what='scan'): |
---|
238 | md = self._translate(what) |
---|
239 | if md: |
---|
240 | self._panels = md |
---|
241 | return True |
---|
242 | return False |
---|
243 | |
---|
244 | def set_stacking(self, what='pol'): |
---|
245 | md = self._translate(what) |
---|
246 | if md: |
---|
247 | self._stacking = md |
---|
248 | return True |
---|
249 | return False |
---|
250 | |
---|
251 | def set_range(self,start=None,end=None): |
---|
252 | """ |
---|
253 | Set the range of interest on the abcissa of the plot |
---|
254 | Parameters: |
---|
255 | start,end: The start an end point of the 'zoom' window |
---|
256 | Note: |
---|
257 | These become non-sensical when the unit changes. |
---|
258 | use plotter.set_range() without parameters to reset |
---|
259 | |
---|
260 | """ |
---|
261 | if start is None and end is None: |
---|
262 | self._minmax = None |
---|
263 | if self._data is not None: |
---|
264 | self.plot() |
---|
265 | else: |
---|
266 | self._minmax = [start,end] |
---|
267 | if self._data is not None: |
---|
268 | self.plot() |
---|
269 | return |
---|
270 | |
---|
271 | def set_legend_map(self,mp=[]): |
---|
272 | """ |
---|
273 | Specify a mapping for the legend instead of using the default |
---|
274 | indices: |
---|
275 | Parameters: |
---|
276 | mp: a list of 'strings'. This should have the same length |
---|
277 | as the number of elements on the legend and then maps |
---|
278 | to the indeces in order |
---|
279 | |
---|
280 | Example: |
---|
281 | If the data has to IFs/rest frequencies with index 0 and 1 |
---|
282 | for CO and SiO: |
---|
283 | plotter.set_stacking('i') |
---|
284 | plotter.set_legend_map(['CO','SiO']) |
---|
285 | plotter.plot() |
---|
286 | """ |
---|
287 | self._lmap = mp |
---|
288 | |
---|
289 | if __name__ == '__main__': |
---|
290 | plotter = asapplotter() |
---|