source: trunk/python/asapplotter.py @ 371

Last change on this file since 371 was 282, checked in by mar637, 19 years ago

removed comment on j=i

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.9 KB
Line 
1from asap.asaplot import ASAPlot
2from asap import rcParams
3
4class 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    The defaul plotter is called 'plotter'.
10    Note:
11        Currenly it only plots 'spectra' not Tsys or
12        other variables.
13    """
14    def __init__(self):
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 = rcParams['plotter.stacking']
36        self._autoplot = False
37        self._minmax = None
38        self._data = None
39        self._lmap = []
40        self._title = None
41        self._ordinate = None
42        self._abcissa = None
43
44    def _translate(self, name):
45        for d in self._dicts:
46            if d.has_key(name):
47                return d[name]
48        return None
49       
50    def plot(self,*args):
51        """
52        Plot a (list of) scantables.
53        Parameters:
54            one or more comma separated scantables
55        Note:
56            If a (list) of scantables was specified in a previous call
57            to plot, no argument has to be given to 'replot'
58            NO checking is done that the abscissas of the scantables
59            are consistent e.g. all 'channel' or all 'velocity' etc.
60        """
61        if self._plotter.is_dead:
62            self._plotter = ASAPlot()
63        self._plotter.clear()
64        self._plotter.hold()
65        if len(args) > 0:
66            self._data = tuple(args)           
67        if self._panels == 't':
68            if self._data[0].nrow() > 25:
69                print "Scan to be plotted contains more than 25 rows.\nCan't plot that many panels..."
70                return
71            self._plot_time(self._data[0], self._stacking)
72        elif self._panels == 's':
73            self._plot_scans(self._data, self._stacking)
74        else:
75            self._plot_other(self._data, self._stacking)
76        if self._minmax is not None:
77            self._plotter.set_limits(xlim=self._minmax)
78        self._plotter.release()
79        return
80
81    def _plot_time(self, scan, colmode):
82        if colmode == 't':
83            return
84        n = scan.nrow()
85        cdict = {'b':'scan.setbeam(j)',
86                 'i':'scan.setif(j)',
87                 'p':'scan.setpol(j)'}
88        if self._stacking is not None:
89            ncol = eval(self._cdict.get(colmode))
90        self._plotter.set_panels()
91        if n > 1:
92            self._plotter.set_panels(rows=n)
93        for i in range(n):
94            if n > 1:
95                self._plotter.palette(0)
96                self._plotter.subplot(i)
97            for j in range(ncol):
98                eval(cdict.get(colmode))
99                x = None
100                y = None
101                m = None
102                if not self._title:
103                    tlab = scan._getsourcename(i)                   
104                else:
105                    if len(self._title) == n:
106                        tlab = self._title[i]
107                    else:
108                        tlab = scan._getsourcename(i)                   
109                x,xlab = scan.get_abcissa(i)
110                if self._abcissa: xlab = self._abcissa
111                y = scan._getspectrum(i)
112                if self._ordinate:
113                    ylab = self._ordinate
114                else:
115                    ylab = 'Flux ('+scan.get_fluxunit()+')'
116                m = scan._getmask(i)
117                if self._lmap and len(self._lmap) > 0:
118                    llab = self._lmap[j]
119                else:
120                    llab = self._ldict.get(colmode)+' '+str(j)
121                self._plotter.set_line(label=llab)
122                self._plotter.plot(x,y,m)
123                xlim=[min(x),max(x)]
124                self._plotter.axes.set_xlim(xlim)
125            self._plotter.set_axes('xlabel',xlab)
126            self._plotter.set_axes('ylabel',ylab)
127            self._plotter.set_axes('title',tlab)           
128        return
129
130    def _plot_scans(self, scans, colmode):       
131        if colmode == 's':
132            return
133        cdict = {'b':'scan.setbeam(j)',
134                 'i':'scan.setif(j)',
135                 'p':'scan.setpol(j)'}
136        n = len(scans)
137        if self._stacking is not None:
138            scan = scans[0]
139            ncol = eval(self._cdict.get(colmode))
140        self._plotter.set_panels()
141        if n > 1:
142            self._plotter.set_panels(rows=n)
143        i = 0
144        for scan in scans:
145            if n > 1:
146                self._plotter.subplot(i)
147                self._plotter.palette(0)
148            for j in range(ncol):
149                eval(cdict.get(colmode))
150                x = None
151                y = None
152                m = None
153                tlab = self._title
154                if not self._title:
155                    tlab = scan._getsourcename()
156                x,xlab = scan.get_abcissa()
157                if self._abcissa: xlab = self._abcissa
158                y = scan._getspectrum()
159                if self._ordinate:
160                    ylab = self._ordinate
161                else:
162                    ylab = 'Flux ('+scan.get_fluxunit()+')'
163                m = scan._getmask()
164                if self._lmap and len(self._lmap) > 0:
165                    llab = self._lmap[j]
166                else:
167                    llab = self._ldict.get(colmode)+' '+str(j)
168                self._plotter.set_line(label=llab)
169                self._plotter.plot(x,y,m)
170                xlim=[min(x),max(x)]
171                self._plotter.axes.set_xlim(xlim)
172
173            self._plotter.set_axes('xlabel',xlab)
174            self._plotter.set_axes('ylabel',ylab)
175            self._plotter.set_axes('title',tlab)
176            i += 1
177        return
178   
179    def _plot_other(self,scans,colmode):
180        if colmode == self._panels:
181            return
182        cdict = {'b':'scan.setbeam(j)',
183                 'i':'scan.setif(j)',
184                 'p':'scan.setpol(j)',
185                 's':'scans[j]'}
186        scan = scans[0]
187        n = eval(self._cdict.get(self._panels))
188        if self._stacking is not None:           
189            ncol = eval(self._cdict.get(colmode))
190        self._plotter.set_panels()
191        if n > 1:
192            self._plotter.set_panels(rows=n)
193        for i in range(n):
194            if n>1:
195                self._plotter.subplot(i)
196                self._plotter.palette(0)
197            k=0
198            j=i
199            eval(cdict.get(self._panels))
200            for j in range(ncol):
201                if colmode == 's':
202                    scan = eval(cdict.get(colmode))
203                elif colmode == 't':
204                    k = j
205                else:
206                    eval(cdict.get(colmode))
207                x = None
208                y = None
209                m = None
210                x,xlab = scan.get_abcissa(k)
211                if self._abcissa: xlab = self._abcissa
212                y = scan._getspectrum(k)
213                if self._ordinate:
214                    ylab = self._ordinate
215                else:
216                    ylab = 'Flux ('+scan.get_fluxunit()+')'
217                m = scan._getmask(k)
218                if colmode == 's' or colmode == 't':
219                    if not self._title:
220                        tlab = self._ldict.get(self._panels)+' '+str(i)
221                    else:
222                        if len(self.title) == n:
223                            tlab = self._title[i]
224                        else:
225                            tlab = self._ldict.get(self._panels)+' '+str(i)
226                    llab = scan._getsourcename(k)
227                else:
228                    if self._title and len(self._title) > 0:
229                        tlab = self._title[i]
230                    else:
231                        tlab = self._ldict.get(self._panels)+' '+str(i)
232                    if self._lmap and len(self._lmap) > 0:
233                        llab = self._lmap[j]
234                    else:
235                        llab = self._ldict.get(colmode)+' '+str(j)
236                self._plotter.set_line(label=llab)
237                self._plotter.plot(x,y,m)
238                xlim=[min(x),max(x)]
239                self._plotter.axes.set_xlim(xlim)
240
241            self._plotter.set_axes('xlabel',xlab)
242            self._plotter.set_axes('ylabel',ylab)
243            self._plotter.set_axes('title',tlab)
244       
245        return
246
247
248    def set_mode(self, stacking=None, panelling=None):
249        """
250        Parameters:
251            stacking:     tell the plotter which variable to plot
252                          as line colour overlays (default 'pol')
253            panelling:    tell the plotter which variable to plot
254                          across multiple panels (default 'scan'
255        Note:
256            Valid modes are:
257                 'beam' 'Beam' 'b':     Beams
258                 'if' 'IF' 'i':         IFs
259                 'pol' 'Pol' 'p':       Polarisations
260                 'scan' 'Scan' 's':     Scans
261                 'time' 'Time' 't':     Times
262        """
263        if not self.set_panels(panelling):
264            print "Invalid mode"
265            return
266        if not self.set_stacking(stacking):
267            print "Invalid mode"
268            return
269        if self._data: self.plot()
270        return
271
272    def set_panels(self, what=None):       
273        if not what:
274             what = rcParams['plotter.panelling']
275        md = self._translate(what)
276        if md:
277            self._panels = md
278            self._title = None
279            return True
280        return False
281
282    def set_stacking(self, what=None): 
283        if not what:
284             what = rcParams['plotter.stacking']       
285        md = self._translate(what)
286        if md:
287            self._stacking = md
288            self._lmap = None
289            return True
290        return False
291
292    def set_range(self,start=None,end=None):
293        """
294        Set the range of interest on the abcissa of the plot
295        Parameters:
296            start,end:    The start an end point of the 'zoom' window
297        Note:
298            These become non-sensical when the unit changes.
299            use plotter.set_range() without parameters to reset
300
301        """
302        if start is None and end is None:
303            self._minmax = None
304            if self._data: self.plot()
305        else:
306            self._minmax = [start,end]
307            if self._data: self.plot()
308        return
309   
310    def set_legend(self, mp=None):
311        """
312        Specify a mapping for the legend instead of using the default
313        indices:
314        Parameters:
315             mp:    a list of 'strings'. This should have the same length
316                    as the number of elements on the legend and then maps
317                    to the indeces in order
318
319        Example:
320             If the data has to IFs/rest frequencies with index 0 and 1
321             for CO and SiO:
322             plotter.set_stacking('i')
323             plotter.set_legend_map(['CO','SiO'])
324             plotter.plot()
325        """
326        self._lmap = mp
327        if self._data: self.plot()
328        return
329
330    def set_title(self, title=None):
331        self._title = title
332        if self._data: self.plot()
333        return
334
335    def set_ordinate(self, ordinate=None):
336        self._ordinate = ordinate
337        if self._data: self.plot()
338        return
339
340    def set_abcissa(self, abcissa=None):
341        self._abcissa = abcissa
342        if self._data: self.plot()
343        return
344
345       
346
347if __name__ == '__main__':
348    plotter = asapplotter()
Note: See TracBrowser for help on using the repository browser.