Changeset 2367


Ignore:
Timestamp:
12/14/11 13:24:30 (13 years ago)
Author:
Takeshi Nakazato
Message:

New Development: No

JIRA Issue: Yes CAS-2816

Ready for Test: No

Interface Changes: No

What Interface Changed: Please list interface changes

Test Programs: List test programs

Put in Release Notes: Yes/No

Module(s): Module Names change impacts.

Description: Describe your changes here...

Speed up and save memory usage in plot routine.


File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/python/asapgrid.py

    r2365 r2367  
    11import numpy
     2from asap import rcParams
    23from asap.scantable import scantable
     4from asap.selector import selector
    35from asap._asap import stgrid
    46import pylab as pl
     7from logging import asaplog
    58
    69class asapgrid:
     
    912        self.outfile = None
    1013        self.gridder = stgrid( self.infile )
     14        self.ifno = None
    1115
    1216    def setData( self, infile ):
     
    1418
    1519    def setIF( self, ifno ):
    16         self.gridder._setif( ifno )
     20        self.ifno = ifno
     21        self.gridder._setif( self.ifno )
    1722
    1823    def setPolList( self, pollist ):
     
    3843
    3944    def plot( self, plotchan=-1, plotpol=-1 ):
    40         plotter = _SDGridPlotter( self.infile, self.outfile )
     45        import time
     46        t0=time.time()
     47        # to load scantable on disk
     48        storg = rcParams['scantable.storage']
     49        rcParams['scantable.storage'] = 'disk'
     50        plotter = _SDGridPlotter( self.infile, self.outfile, self.ifno )
    4151        plotter.plot( chan=plotchan, pol=plotpol )
     52        # back to original setup
     53        rcParams['scantable.storage'] = storg
     54        t1=time.time()
     55        asaplog.push('plot: elapsed time %s sec'%(t1-t0))
     56        asaplog.post('DEBUG','asapgrid.plot')
    4257       
    4358class _SDGridPlotter:
    44     def __init__( self, infile, outfile=None ):
     59    def __init__( self, infile, outfile=None, ifno=0 ):
    4560        self.infile = infile
    4661        self.outfile = outfile
     
    4964        self.grid = None
    5065        self.pointing = None
    51         self.data = None
    5266        self.nx = -1
    5367        self.ny = -1
     
    5973        self.center = [0.0,0.0]
    6074        self.nonzero = [[0.0],[0.0]]
     75        self.ifno = ifno
    6176        self.get()
    6277
    6378    def get( self ):
    6479        s = scantable( self.infile, average=False )
     80        sel = selector()
     81        sel.set_ifs( self.ifno )
     82        s.set_selection( sel )
    6583        self.pointing = numpy.array( s.get_directionval() ).transpose()
    66         spectra = []
    67         for i in xrange(s.nrow()):
    68             spectra.append( s._getspectrum( i ) )
    69         spectra = numpy.array( spectra ).transpose()
    70         self.nchan = spectra.shape[0]
     84        self.nchan = len(s._getspectrum(0))
     85        s.set_selection()
    7186        del s
     87        del sel
    7288
    7389        s = scantable( self.outfile, average=False )
     
    84100        dirstring = numpy.array(s.get_direction()).take(range(0,nrow,self.npol))
    85101        self.grid = numpy.array( s.get_directionval() ).take(range(0,nrow,self.npol),axis=0).transpose()
    86         spectra = numpy.zeros( (self.npol,self.nchan,nrow/self.npol), dtype=float )
    87         irow = 0
    88         for i in xrange(nrow/self.npol):
    89             for ip in xrange(self.npol):
    90                 spectra[ip,:,i] = s._getspectrum( irow )
    91                 irow += 1
    92102
    93103        idx = 0
     
    95105        while ( dirstring[idx].split()[-1] == d0 ): 
    96106            idx += 1
     107       
    97108        self.ny = idx
    98109        self.nx = nrow / (self.npol * idx )
     
    103114        #print 'cellx,celly=',self.cellx,self.celly
    104115
    105         self.data = spectra.reshape( (self.npol,self.nchan,self.nx,self.ny) )
    106 
    107116    def plot( self, chan=-1, pol=-1 ):
    108117        if pol < 0:
    109             data = self.data.mean(axis=0)
    110118            opt = 'averaged over pol'
    111119        else:
    112             idx = self.pollist.tolist().index( pol )
    113             #print 'idx=',idx
    114             data = self.data[idx]
    115120            opt = 'pol %s'%(pol)
    116121        if chan < 0:
    117             data = data.mean(axis=0)
    118122            opt += ', averaged over channel'
    119123        else:
    120             data = data[chan]
    121124            opt += ', channel %s'%(chan)
     125        data = self.getData( chan, pol )
    122126        title = 'Gridded Image (%s)'%(opt)
    123127        pl.figure(10)
     
    134138        pl.ylabel('Dec. [rad]')
    135139        pl.title( title )
     140
     141    def getData( self, chan=-1, pol=-1 ):
     142        if chan == -1:
     143            spectra = self.__chanAverage()
     144        else:
     145            spectra = self.__chanIndex( chan )
     146        data = spectra.reshape( (self.npol,self.nx,self.ny) )
     147        if pol == -1:
     148            retval = data.mean(axis=0)
     149        else:
     150            retval = data[pol]
     151        return retval
     152
     153    def __chanAverage( self ):
     154        s = scantable( self.outfile, average=False )
     155        nrow = self.nx * self.ny
     156        spectra = numpy.zeros( (self.npol,nrow/self.npol), dtype=float )
     157        irow = 0
     158        sp = [0 for i in xrange(self.nchan)]
     159        for i in xrange(nrow/self.npol):
     160            for ip in xrange(self.npol):
     161                sp = s._getspectrum( irow )
     162                spectra[ip,i] = numpy.mean( sp )
     163                irow += 1
     164        return spectra
     165
     166    def __chanIndex( self, idx ):
     167        s = scantable( self.outfile, average=False )
     168        nrow = self.nx * self.ny
     169        spectra = numpy.zeros( (self.npol,nrow/self.npol), dtype=float )
     170        irow = 0
     171        sp = [0 for i in xrange(self.nchan)]
     172        for i in xrange(nrow/self.npol):
     173            for ip in xrange(self.npol):
     174                sp = s._getspectrum( irow )
     175                spectra[ip,i] = sp[idx]
     176                irow += 1
     177        return spectra
     178       
     179           
Note: See TracChangeset for help on using the changeset viewer.