source: trunk/python/asapgrid.py @ 2390

Last change on this file since 2390 was 2390, checked in by Takeshi Nakazato, 12 years ago

New Development: No

JIRA Issue: Yes CAS-2816

Ready for Test: Yes

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...

More than one input data are supported.


File size: 7.6 KB
Line 
1import numpy
2from asap import rcParams
3from asap.scantable import scantable
4from asap.selector import selector
5from asap._asap import stgrid
6import pylab as pl
7from logging import asaplog
8
9class asapgrid:
10    def __init__( self, infile ):
11        self.outfile = None
12        self.ifno = None
13        self.gridder = stgrid()
14        self.setData( infile )
15
16    def setData( self, infile ):
17        if isinstance( infile, str ):
18            self.gridder._setin( infile )
19        else:
20            self.gridder._setfiles( infile )
21        self.infile = infile
22
23    def setIF( self, ifno ):
24        self.ifno = ifno
25        self.gridder._setif( self.ifno )
26
27    def setPolList( self, pollist ):
28        self.gridder._setpollist( pollist )
29
30    def setScanList( self, scanlist ):
31        self.gridder._setscanlist( scanlist )
32
33    def defineImage( self, nx=-1, ny=-1, cellx='', celly='', center='' ):
34        self.gridder._defineimage( nx, ny, cellx, celly, center )
35
36    def setFunc( self, func='box', width=-1 ):
37        self.gridder._setfunc( func, width )
38
39    def setWeight( self, weightType='uniform' ):
40        self.gridder._setweight( weightType )
41
42    def grid( self ):
43        self.gridder._grid()
44
45    def save( self, outfile='' ):
46        self.outfile = self.gridder._save( outfile )
47
48    def plot( self, plotchan=-1, plotpol=-1, plotobs=False, plotgrid=False ):
49        import time
50        t0=time.time()
51        # to load scantable on disk
52        storg = rcParams['scantable.storage']
53        rcParams['scantable.storage'] = 'disk'
54        plotter = _SDGridPlotter( self.infile, self.outfile, self.ifno )
55        plotter.plot( chan=plotchan, pol=plotpol, plotobs=plotobs, plotgrid=plotgrid )
56        # back to original setup
57        rcParams['scantable.storage'] = storg
58        t1=time.time()
59        asaplog.push('plot: elapsed time %s sec'%(t1-t0))
60        asaplog.post('DEBUG','asapgrid.plot')
61       
62class _SDGridPlotter:
63    def __init__( self, infile, outfile=None, ifno=-1 ):
64        if isinstance( infile, str ):
65            self.infile = [infile]
66        else:
67            self.infile = infile
68        self.outfile = outfile
69        if self.outfile is None:
70            self.outfile = self.infile[0].rstrip('/')+'.grid'
71        self.nx = -1
72        self.ny = -1
73        self.nchan = 0
74        self.npol = 0
75        self.pollist = []
76        self.cellx = 0.0
77        self.celly = 0.0
78        self.center = [0.0,0.0]
79        self.nonzero = [[0.0],[0.0]]
80        self.ifno = ifno
81        self.tablein = None
82        self.nrow = 0
83        self.blc = None
84        self.trc = None
85        self.get()
86
87    def get( self ):
88        s = scantable( self.outfile, average=False )
89        self.nchan = len(s._getspectrum(0))
90        nrow = s.nrow()
91        pols = numpy.ones( nrow, dtype=int )
92        for i in xrange(nrow):
93            pols[i] = s.getpol(i)
94        self.pollist, indices = numpy.unique( pols, return_inverse=True )
95        self.npol = len(self.pollist)
96        self.pollist = self.pollist[indices[:self.npol]]
97        #print 'pollist=',self.pollist
98        #print 'npol=',self.npol
99        #print 'nrow=',nrow
100
101        idx = 0
102        d0 = s.get_direction( 0 ).split()[-1]
103        while ( s.get_direction(self.npol*idx).split()[-1] == d0 ): 
104            idx += 1
105       
106        self.nx = idx
107        self.ny = nrow / (self.npol * idx )
108        #print 'nx,ny=',self.nx,self.ny
109
110        self.blc = s.get_directionval( 0 )
111        self.trc = s.get_directionval( nrow-self.npol )
112        #print self.blc
113        #print self.trc
114        incrx = s.get_directionval( self.npol )
115        incry = s.get_directionval( self.nx*self.npol )
116        self.cellx = abs( self.blc[0] - incrx[0] )
117        self.celly = abs( self.blc[1] - incry[1] )
118        #print 'cellx,celly=',self.cellx,self.celly
119
120    def plot( self, chan=-1, pol=-1, plotobs=False, plotgrid=False ):
121        if pol < 0:
122            opt = 'averaged over pol'
123        else:
124            opt = 'pol %s'%(pol)
125        if chan < 0:
126            opt += ', averaged over channel'
127        else:
128            opt += ', channel %s'%(chan)
129        data = self.getData( chan, pol )
130        title = 'Gridded Image (%s)'%(opt)
131        pl.figure(10)
132        pl.clf()
133        # plot grid position
134        if plotgrid:
135            x = numpy.arange(self.blc[0],self.trc[0]+0.5*self.cellx,self.cellx,dtype=float)
136            #print 'len(x)=',len(x)
137            #print 'x=',x
138            ybase = numpy.ones(self.nx,dtype=float)*self.blc[1]
139            #print 'len(ybase)=',len(ybase)
140            incr = self.celly
141            for iy in xrange(self.ny):
142                y = ybase + iy * incr
143                #print y
144                pl.plot(x,y,',',color='blue')
145        # plot observed position
146        if plotobs:
147            for i in xrange(len(self.infile)):
148                self.createTableIn( self.infile[i] )
149                irow = 0
150                while ( irow < self.nrow ):
151                    chunk = self.getPointingChunk( irow )
152                    #print chunk
153                    pl.plot(chunk[0],chunk[1],',',color='green')
154                    irow += chunk.shape[1]
155                    #print irow
156        # show image
157        extent=[self.blc[0]-0.5*self.cellx,
158                self.trc[0]+0.5*self.cellx,
159                self.blc[1]-0.5*self.celly,
160                self.trc[1]+0.5*self.celly]
161        pl.imshow(data,extent=extent,origin='lower',interpolation='nearest')
162        pl.colorbar()
163        pl.xlabel('R.A. [rad]')
164        pl.ylabel('Dec. [rad]')
165        pl.title( title )
166
167    def createTableIn( self, tab ):
168        del self.tablein
169        self.tablein = scantable( tab, average=False )
170        if self.ifno < 0:
171            ifno = self.tablein.getif(0)
172            print 'ifno=',ifno
173        else:
174            ifno = self.ifno
175        sel = selector()
176        sel.set_ifs( ifno )
177        self.tablein.set_selection( sel )
178        self.nchan = len(self.tablein._getspectrum(0))
179        self.nrow = self.tablein.nrow()
180        del sel
181       
182
183    def getPointingChunk( self, irow ):
184        numchunk = 1000
185        nrow = min( self.nrow-irow, numchunk )
186        #print 'nrow=',nrow
187        v = numpy.zeros( (2,nrow), dtype=float )
188        idx = 0
189        for i in xrange(irow,irow+nrow):
190            d = self.tablein.get_directionval( i )
191            v[0,idx] = d[0]
192            v[1,idx] = d[1]
193            idx += 1
194        return v
195
196    def getData( self, chan=-1, pol=-1 ):
197        if chan == -1:
198            spectra = self.__chanAverage()
199        else:
200            spectra = self.__chanIndex( chan )
201        data = spectra.reshape( (self.npol,self.ny,self.nx) )
202        if pol == -1:
203            retval = data.mean(axis=0)
204        else:
205            retval = data[pol]
206        return retval
207
208    def __chanAverage( self ):
209        s = scantable( self.outfile, average=False )
210        nrow = s.nrow()
211        spectra = numpy.zeros( (self.npol,nrow/self.npol), dtype=float )
212        irow = 0
213        sp = [0 for i in xrange(self.nchan)]
214        for i in xrange(nrow/self.npol):
215            for ip in xrange(self.npol):
216                sp = s._getspectrum( irow )
217                spectra[ip,i] = numpy.mean( sp )
218                irow += 1
219        return spectra
220
221    def __chanIndex( self, idx ):
222        s = scantable( self.outfile, average=False )
223        nrow = s.nrow()
224        spectra = numpy.zeros( (self.npol,nrow/self.npol), dtype=float )
225        irow = 0
226        sp = [0 for i in xrange(self.nchan)]
227        for i in xrange(nrow/self.npol):
228            for ip in xrange(self.npol):
229                sp = s._getspectrum( irow )
230                spectra[ip,i] = sp[idx]
231                irow += 1
232        return spectra
233       
234           
Note: See TracBrowser for help on using the repository browser.