Changeset 525


Ignore:
Timestamp:
03/01/05 14:17:19 (19 years ago)
Author:
mar637
Message:

added handling of stokes!!!

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/python/asapplotter.py

    r485 r525  
    1919        self._pdict = {'Pol':'p','pol':'p','p':'p'}
    2020        self._sdict = {'scan':'s','Scan':'s','s':'s','S':'s'}
    21         self._cdict = {'t':'scan.nrow()',
    22                        'b':'scan.nbeam()',
    23                        'i':'scan.nif()',
    24                        'p':'scan.npol()',
     21        self._cdict = {'t':'len(self._cursor["t"])',
     22                       'b':'len(self._cursor["b"])',
     23                       'i':'len(self._cursor["i"])',
     24                       'p':'len(self._cursor["p"])',
    2525                       's':'len(scans)'}
    2626        self._ldict = {'b':'Beam',
     
    3636        self._cols = None
    3737        self._autoplot = False
    38         self._minmax = None
     38        self._minmaxx = None
     39        self._minmaxy = None
    3940        self._data = None
    4041        self._lmap = []
     
    4243        self._ordinate = None
    4344        self._abcissa = None
     45        self._cursor = {'t':None, 'b':None,
     46                        'i':None, 'p':None
     47                        }
    4448
    4549    def _translate(self, name):
     
    4953        return None
    5054       
    51     def plot(self,*args):
     55    def plot(self, *args):
    5256        """
    5357        Plot a (list of) scantables.
     
    5761            If a (list) of scantables was specified in a previous call
    5862            to plot, no argument has to be given to 'replot'
    59             NO checking is done that the abscissas of the scantables
     63            NO checking is done that the abcissas of the scantables
    6064            are consistent e.g. all 'channel' or all 'velocity' etc.
    6165        """
     
    6569        self._plotter.hold()
    6670        if len(args) > 0:
    67             self._data = tuple(args)           
     71            if self._data is not None:               
     72                if list(args) != self._data:
     73                    self._data = list(args)
     74                    # reset cursor
     75                    self.set_cursor()
     76            else:
     77                self._data = list(args)
     78                self.set_cursor()
    6879        if self._panels == 't':
    69             if self._data[0].nrow() > 25:
    70                 print "Scan to be plotted contains more than 25 rows.\nCan't plot that many panels..."
     80            if self._data[0].nrow() > 49:
     81                print "Scan to be plotted contains more than 25 rows.\n \
     82                Can't plot that many panels..."
    7183                return
    7284            self._plot_time(self._data[0], self._stacking)
     
    7587        else:
    7688            self._plot_other(self._data, self._stacking)
    77         if self._minmax is not None:
    78             self._plotter.set_limits(xlim=self._minmax)
     89        if self._minmaxx is not None or self._minmaxy is not None:
     90            self._plotter.set_limits(xlim=self._minmaxx,ylim=self._minmaxy)
    7991        self._plotter.release()
    8092        return
     
    8395        if colmode == 't':
    8496            return
    85         n = scan.nrow()
     97        n = len(self._cursor["t"])
    8698        cdict = {'b':'scan.setbeam(j)',
    8799                 'i':'scan.setif(j)',
    88100                 'p':'scan.setpol(j)'}
     101        cdict2 = {'b':'self._cursor["b"]',
     102                  'i':'self._cursor["i"]',
     103                  'p':'self._cursor["p"]'}
     104        ncol = 1
    89105        if self._stacking is not None:
    90106            ncol = eval(self._cdict.get(colmode))
     
    97113            else:
    98114                self._plotter.set_panels(rows=n,cols=0,nplots=n)
    99         for i in range(n):
     115        rows = self._cursor["t"]
     116        self._plotter.palette(1)
     117        for rowsel in rows:
     118            i = self._cursor["t"].index(rowsel)
    100119            if n > 1:
    101120                self._plotter.palette(1)
    102121                self._plotter.subplot(i)
    103             for j in range(ncol):
     122            colvals = eval(cdict2.get(colmode))
     123            for j in colvals:
     124                polmode = "raw"
     125                jj = colvals.index(j)
     126                savej = j
     127                for k in cdict.keys():
     128                    sel = eval(cdict2.get(k))                   
     129                    j = sel[0]
     130                    if k == "p":
     131                        which = self._cursor["p"].index(j)
     132                        polmode = self._polmode[which]
     133                        j = which
     134                    eval(cdict.get(k))
     135                j = savej
     136                if colmode == "p":
     137                    polmode = self._polmode[self._cursor["p"].index(j)]
     138                    j = jj
    104139                eval(cdict.get(colmode))
    105140                x = None
     
    107142                m = None
    108143                if not self._title:
    109                     tlab = scan._getsourcename(i)                   
     144                    tlab = scan._getsourcename(rowsel)                   
    110145                else:
    111146                    if len(self._title) == n:
    112                         tlab = self._title[i]
     147                        tlab = self._title[rowsel]
    113148                    else:
    114                         tlab = scan._getsourcename(i)                   
    115                 x,xlab = scan.get_abcissa(i)
     149                        tlab = scan._getsourcename(rowsel)
     150                x,xlab = scan.get_abcissa(rowsel)
    116151                if self._abcissa: xlab = self._abcissa
    117                 y = scan._getspectrum(i)
     152                y = None
     153                if polmode == "stokes":
     154                    y = scan._getstokesspectrum(rowsel)
     155                elif polmode == "stokes2":
     156                    y = scan._getstokesspectrum(rowsel,True)
     157                else:
     158                    y = scan._getspectrum(rowsel)
    118159                if self._ordinate:
    119160                    ylab = self._ordinate
    120161                else:
    121162                    ylab = 'Flux ('+scan.get_fluxunit()+')'
    122                 m = scan._getmask(i)
     163                m = scan._getmask(rowsel)
    123164                if self._lmap and len(self._lmap) > 0:
    124                     llab = self._lmap[j]
    125                 else:
    126                     llab = self._ldict.get(colmode)+' '+str(j)
     165                    llab = self._lmap[jj]
     166                else:
     167                    if colmode == 'p':
     168                        if polmode == "stokes":
     169                            llab = scan._getpolarizationlabel(0,1,0)
     170                        elif polmode == "stokes2":
     171                            llab = scan._getpolarizationlabel(0,1,1)
     172                        else:
     173                            llab = scan._getpolarizationlabel(1,0,0)
     174                    else:                   
     175                        llab = self._ldict.get(colmode)+' '+str(j)
    127176                self._plotter.set_line(label=llab)
    128177                self._plotter.plot(x,y,m)
     
    134183        return
    135184
    136     def _plot_scans(self, scans, colmode):       
     185    def _plot_scans(self, scans, colmode):
     186        print "Can only plot one row per scan."
    137187        if colmode == 's':
    138188            return
     
    140190                 'i':'scan.setif(j)',
    141191                 'p':'scan.setpol(j)'}
     192        cdict2 = {'b':'self._cursor["b"]',
     193                  'i':'self._cursor["i"]',
     194                  'p':'self._cursor["p"]'}
     195       
    142196        n = len(scans)
     197        ncol = 1
    143198        if self._stacking is not None:
    144199            scan = scans[0]
     
    152207            else:
    153208                self._plotter.set_panels(rows=n,cols=0,nplots=n)
    154         i = 0
     209        self._plotter.palette(1)
    155210        for scan in scans:
    156211            if n > 1:
    157                 self._plotter.subplot(i)
     212                self._plotter.subplot(scans.index(scan))
    158213                self._plotter.palette(1)
    159             for j in range(ncol):
     214            colvals = eval(cdict2.get(colmode))
     215            rowsel = self._cursor["t"][0]
     216            for j in colvals:
     217                polmode = "raw"
     218                jj = colvals.index(j)
     219                savej = j
     220                for k in cdict.keys():
     221                    sel = eval(cdict2.get(k))                   
     222                    j = sel[0]
     223                    eval(cdict.get(k))
     224                    if k == "p":
     225                        which = self._cursor["p"].index(j)
     226                        polmode = self._polmode[which]
     227                        j = which
     228                j = savej
     229                if colmode == "p":
     230                    polmode = self._polmode[self._cursor["p"].index(j)]
     231                    j = jj
    160232                eval(cdict.get(colmode))
    161233                x = None
     
    164236                tlab = self._title
    165237                if not self._title:
    166                     tlab = scan._getsourcename()
    167                 x,xlab = scan.get_abcissa()
     238                    tlab = scan._getsourcename(rowsel)
     239                x,xlab = scan.get_abcissa(rowsel)
    168240                if self._abcissa: xlab = self._abcissa
    169                 y = scan._getspectrum()
     241                if polmode == "stokes":
     242                    y = scan._getstokesspectrum(rowsel)
     243                elif polmode == "stokes2":
     244                    y = scan._getstokesspectrum(rowsel,True)
     245                else:
     246                    y = scan._getspectrum(rowsel)
    170247                if self._ordinate:
    171248                    ylab = self._ordinate
    172249                else:
    173250                    ylab = 'Flux ('+scan.get_fluxunit()+')'
    174                 m = scan._getmask()
     251                m = scan._getmask(rowsel)
    175252                if self._lmap and len(self._lmap) > 0:
    176                     llab = self._lmap[j]
    177                 else:
    178                     llab = self._ldict.get(colmode)+' '+str(j)
     253                    llab = self._lmap[jj]
     254                else:
     255                    if colmode == 'p':
     256                        if polmode == "stokes":
     257                            llab = scan._getpolarizationlabel(0,1,0)
     258                        elif polmode == "stokes2":
     259                            llab = scan._getpolarizationlabel(0,1,1)
     260                        else:
     261                            llab = scan._getpolarizationlabel(1,0,0)
     262                    else:
     263                        llab = self._ldict.get(colmode)+' '+str(j)
    179264                self._plotter.set_line(label=llab)
    180265                self._plotter.plot(x,y,m)
     
    185270            self._plotter.set_axes('ylabel',ylab)
    186271            self._plotter.set_axes('title',tlab)
    187             i += 1
    188272        return
    189273   
     
    191275        if colmode == self._panels:
    192276            return
    193         cdict = {'b':'scan.setbeam(j)',
    194                  'i':'scan.setif(j)',
    195                  'p':'scan.setpol(j)',
    196                  's':'scans[j]'}
     277        cdict = {'b':'scan.setbeam(i)',
     278                 'i':'scan.setif(i)',
     279                 'p':'scan.setpol(i)'}
     280        cdict2 = {'b':'self._cursor["b"]',
     281                  'i':'self._cursor["i"]',
     282                  'p':'self._cursor["p"]',
     283                  's': 'scans',
     284                  't': 'self._cursor["t"]'}
    197285        scan = scans[0]
    198286        n = eval(self._cdict.get(self._panels))
     287        ncol=1
    199288        if self._stacking is not None:           
    200289            ncol = eval(self._cdict.get(colmode))
     
    206295                                         nplots=n)
    207296            else:
    208                 print n
    209297                self._plotter.set_panels(rows=n,cols=0,nplots=n)
    210         for i in range(n):
     298        self._plotter.palette(1)
     299        panels = self._cursor[self._panels]       
     300        for i in panels:
     301            polmode = "raw"
     302            ii = self._cursor[self._panels].index(i)
    211303            if n>1:
    212                 self._plotter.subplot(i)
    213                 self._plotter.palette(1)
    214             k=0
    215             j=i
    216             eval(cdict.get(self._panels))
    217             for j in range(ncol):
     304                self._plotter.subplot(ii)
     305            if self._panels == "p":
     306                polmode = self._polmode[ii]
     307                eval(cdict.get(self._panels))
     308            else:
     309                eval(cdict.get(self._panels))
     310            colvals = eval(cdict2.get(colmode))
     311            for j in colvals:
     312                rowsel = self._cursor["t"][0]
     313                jj = colvals.index(j)
     314                savei = i
     315                for k in cdict.keys():
     316                    if k != self._panels:
     317                        sel = eval(cdict2.get(k))
     318                        i = sel[0]
     319                        if k == "p":
     320                            which = self._cursor["p"].index(j)
     321                            polmode = self._polmode[which]
     322                            i = which                       
     323                        eval(cdict.get(k))
     324                i = savei
    218325                if colmode == 's':
    219                     scan = eval(cdict.get(colmode))
     326                    scan = j
    220327                elif colmode == 't':
    221                     k = j
    222                 else:
     328                    rowsel = j                   
     329                else:
     330                    savei = i
     331                    if colmode == 'p':
     332                        polmode = self._polmode[self._cursor["p"].index(j)]
     333                    i = j
    223334                    eval(cdict.get(colmode))
     335                    i = savei
    224336                x = None
    225337                y = None
    226338                m = None
    227                 x,xlab = scan.get_abcissa(k)
     339                x,xlab = scan.get_abcissa(rowsel)
    228340                if self._abcissa: xlab = self._abcissa
    229                 y = scan._getspectrum(k)
     341                if polmode == "stokes":
     342                    y = scan._getstokesspectrum(rowsel)
     343                elif polmode == "stokes2":
     344                    y = scan._getstokesspectrum(rowsel,True)
     345                else:
     346                    y = scan._getspectrum(rowsel)
     347
    230348                if self._ordinate:
    231349                    ylab = self._ordinate
    232350                else:
    233351                    ylab = 'Flux ('+scan.get_fluxunit()+')'
    234                 m = scan._getmask(k)
     352                m = scan._getmask(rowsel)
    235353                if colmode == 's' or colmode == 't':
    236                     if not self._title:
     354                    if self._title and len(self._title) > 0:
     355                        tlab = self._title[ii]
     356                    else:                       
    237357                        tlab = self._ldict.get(self._panels)+' '+str(i)
    238                     else:
    239                         if len(self.title) == n:
    240                             tlab = self._title[i]
    241                         else:
    242                             tlab = self._ldict.get(self._panels)+' '+str(i)
    243                     llab = scan._getsourcename(k)
     358                    llab = scan._getsourcename(rowsel)
    244359                else:
    245360                    if self._title and len(self._title) > 0:
    246                         tlab = self._title[i]
     361                        tlab = self._title[ii]
    247362                    else:
    248363                        tlab = self._ldict.get(self._panels)+' '+str(i)
    249364                    if self._lmap and len(self._lmap) > 0:
    250                         llab = self._lmap[j]
     365                        llab = self._lmap[jj]
    251366                    else:
    252                         llab = self._ldict.get(colmode)+' '+str(j)
     367                        if colmode == 'p':
     368                            if polmode == "stokes":
     369                                llab = scan._getpolarizationlabel(0,1,0)
     370                            elif polmode == "stokes2":
     371                                llab = scan._getpolarizationlabel(0,1,1)
     372                            else:
     373                                llab = scan._getpolarizationlabel(1,0,0)
     374                        else:
     375                            llab = self._ldict.get(colmode)+' '+str(j)
     376                if self._panels == 'p':
     377                    if polmode == "stokes":
     378                        tlab = scan._getpolarizationlabel(0,1,0)
     379                    elif polmode == "stokes2":
     380                        tlab = scan._getpolarizationlabel(0,1,1)
     381                    else:
     382                        tlab = scan._getpolarizationlabel(1,0,0)
    253383                self._plotter.set_line(label=llab)
    254384                self._plotter.plot(x,y,m)
     
    289419
    290420    def set_panels(self, what=None):
    291         """       
    292         """
    293421        if not what:
    294422             what = rcParams['plotter.panelling']
     
    326454        return False
    327455
    328     def set_range(self,start=None,end=None):
     456    def set_range(self,xstart=None,xend=None,ystart=None,yend=None):
    329457        """
    330458        Set the range of interest on the abcissa of the plot
    331459        Parameters:
    332             start,end:    The start an end point of the 'zoom' window
     460            [x,y]start,[x,y]end:  The start and end points of the 'zoom' window
    333461        Note:
    334462            These become non-sensical when the unit changes.
     
    336464
    337465        """
    338         if start is None and end is None:
    339             self._minmax = None
     466        if xstart is None and xend is None:
     467            self._minmaxx = None
    340468            if self._data: self.plot()
    341         else:
    342             self._minmax = [start,end]
     469            return
     470        if ystart is None and yend is None:
     471            self._minmaxy = None
    343472            if self._data: self.plot()
     473            return
     474        self._minmaxx = [xstart,xend]
     475        self._minmaxy = [ystart,yend]
     476        if self._data: self.plot()
    344477        return
    345478   
     
    391524        self._plotter.save(filename)
    392525        return
    393 
     526   
     527    def set_cursor(self, row=None,beam=None,IF=None,pol=None):
     528        """
     529        Specify a 'cursor' for plotting selected spectra. Time (rows),
     530        Beam, IF, Polarisation ranges can be specified.
     531        Parameters:
     532            Default for all paramaters is to select all available
     533            row:    selects the rows (time stamps) to be plotted, this has
     534                    to be a vector of row indices, e.g. row=[0,2,5] or row=[2]
     535            beam:   select a range of beams
     536            IF:     select a range of IFs
     537            pol:    select Polarisations for plotting these can be by index
     538                    (raw polarisations (default)) or by names any of:
     539                    ["I", "Q", "U", "V"] or
     540                    ["I", "Plinear", "Pangle", "V"] or
     541                    ["XX", "YY", "Real(XY)", "Imag(XY)"]
     542                    Circular polarisation are not handled yet.
     543        Example:
     544            plotter.set_mode('pol','time')
     545            plotter.plot(myscan) # plots all raw polarisations colour stacked
     546            plotter.set_cursor(pol=["I"]) # plot "I" only for all rows
     547            # plot "I" only for two time stamps row=0 and row=2
     548            plotter.set_cursor(row=[0,2],pol=["I"])
     549
     550        Note:
     551            Be careful to select only exisiting polarisations.           
     552        """
     553        if not self._data:
     554            print "Can only set cursor after a first call to plot()"
     555            return
     556       
     557        n = self._data[0].nrow()
     558        if row is None:
     559            self._cursor["t"] = range(n)
     560        else:
     561            for i in row:
     562                if 0 > i >= n:
     563                    print "Row index '%d' out of range" % i
     564                    return
     565            self._cursor["t"] = row
     566
     567        n = self._data[0].nbeam()
     568        if beam is None:
     569            self._cursor["b"] = range(n)
     570        else:
     571            for i in beam:
     572                if 0 > i >= n:
     573                    print "Beam index '%d' out of range" % i
     574                    return           
     575            self._cursor["b"] = beam
     576
     577        n = self._data[0].nif()
     578        if IF is None:
     579            self._cursor["i"] = range(n)
     580        else:
     581            for i in IF:
     582                if 0 > i >= n:
     583                    print "IF index '%d' out of range" %i
     584                    return           
     585            self._cursor["i"] = IF           
     586
     587        n = self._data[0].npol()
     588        dstokes = {"I":0,"Q":1,"U":2,"V":3}
     589        dstokes2 = {"I":0,"Plinear":1,"Pangle":2,"V":3}
     590        draw = {"XX":0, "YY":1,"Real(XY)":2, "Imag(XY)":3}
     591        dcirc = { "RR":0,"LL":1,"RL":2,"LR":3}
     592       
     593        if pol is None:
     594            self._cursor["p"] = range(n)
     595            self._polmode = ["raw" for i in range(n)]
     596        else:
     597            if isinstance(pol,str):
     598                pol = pol.split()
     599            polmode = []
     600            pols = []
     601            for i in pol:
     602                if isinstance(i,str):
     603                    if draw.has_key(i):
     604                        pols.append(draw.get(i))
     605                        polmode.append("raw")
     606                    elif dstokes.has_key(i):
     607                        pols.append(dstokes.get(i))
     608                        polmode.append("stokes")
     609                    elif dstokes2.has_key(i):
     610                        pols.append(dstokes2.get(i))
     611                        polmode.append("stokes2")
     612                    elif dcirc.has_key(i):
     613                        pols.append(dcirc.get(i))
     614                        polmode.append("cricular")
     615                    else:
     616                        "Pol type '%s' not valid" %i
     617                        return
     618                elif 0 > i >= n:
     619                    print "Pol index '%d' out of range" %i
     620                    return
     621                else:
     622                    pols.append(i)
     623                    polmode.append("raw")
     624            self._cursor["p"] = pols
     625            self._polmode = polmode
     626        if self._data: self.plot()
     627
     628           
    394629if __name__ == '__main__':
    395630    plotter = asapplotter()
Note: See TracChangeset for help on using the changeset viewer.