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