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