source: trunk/python/asapplotter.py @ 226

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

added rcParams to support rc style default parameters, read from .asaprc

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