[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.outfile = None |
---|
[2367] | 12 | self.ifno = None |
---|
[2390] | 13 | self.gridder = stgrid() |
---|
| 14 | self.setData( infile ) |
---|
[2356] | 15 | |
---|
| 16 | def setData( self, infile ): |
---|
[2390] | 17 | if isinstance( infile, str ): |
---|
| 18 | self.gridder._setin( infile ) |
---|
| 19 | else: |
---|
| 20 | self.gridder._setfiles( infile ) |
---|
| 21 | self.infile = infile |
---|
[2356] | 22 | |
---|
[2362] | 23 | def setIF( self, ifno ): |
---|
[2367] | 24 | self.ifno = ifno |
---|
| 25 | self.gridder._setif( self.ifno ) |
---|
[2362] | 26 | |
---|
[2360] | 27 | def setPolList( self, pollist ): |
---|
| 28 | self.gridder._setpollist( pollist ) |
---|
| 29 | |
---|
[2364] | 30 | def setScanList( self, scanlist ): |
---|
| 31 | self.gridder._setscanlist( scanlist ) |
---|
| 32 | |
---|
[2356] | 33 | def defineImage( self, nx=-1, ny=-1, cellx='', celly='', center='' ): |
---|
| 34 | self.gridder._defineimage( nx, ny, cellx, celly, center ) |
---|
| 35 | |
---|
[2364] | 36 | def setFunc( self, func='box', width=-1 ): |
---|
| 37 | self.gridder._setfunc( func, width ) |
---|
[2356] | 38 | |
---|
[2361] | 39 | def setWeight( self, weightType='uniform' ): |
---|
| 40 | self.gridder._setweight( weightType ) |
---|
| 41 | |
---|
[2356] | 42 | def grid( self ): |
---|
| 43 | self.gridder._grid() |
---|
| 44 | |
---|
| 45 | def save( self, outfile='' ): |
---|
| 46 | self.outfile = self.gridder._save( outfile ) |
---|
| 47 | |
---|
[2375] | 48 | def plot( self, plotchan=-1, plotpol=-1, plotobs=False, plotgrid=False ): |
---|
[2367] | 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 ) |
---|
[2375] | 55 | plotter.plot( chan=plotchan, pol=plotpol, plotobs=plotobs, plotgrid=plotgrid ) |
---|
[2367] | 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') |
---|
[2356] | 61 | |
---|
| 62 | class _SDGridPlotter: |
---|
[2373] | 63 | def __init__( self, infile, outfile=None, ifno=-1 ): |
---|
[2390] | 64 | if isinstance( infile, str ): |
---|
| 65 | self.infile = [infile] |
---|
| 66 | else: |
---|
| 67 | self.infile = infile |
---|
[2356] | 68 | self.outfile = outfile |
---|
| 69 | if self.outfile is None: |
---|
[2390] | 70 | self.outfile = self.infile[0].rstrip('/')+'.grid' |
---|
[2356] | 71 | self.nx = -1 |
---|
| 72 | self.ny = -1 |
---|
| 73 | self.nchan = 0 |
---|
[2360] | 74 | self.npol = 0 |
---|
| 75 | self.pollist = [] |
---|
[2356] | 76 | self.cellx = 0.0 |
---|
| 77 | self.celly = 0.0 |
---|
| 78 | self.center = [0.0,0.0] |
---|
| 79 | self.nonzero = [[0.0],[0.0]] |
---|
[2367] | 80 | self.ifno = ifno |
---|
[2372] | 81 | self.tablein = None |
---|
| 82 | self.nrow = 0 |
---|
| 83 | self.blc = None |
---|
| 84 | self.trc = None |
---|
[2356] | 85 | self.get() |
---|
| 86 | |
---|
| 87 | def get( self ): |
---|
| 88 | s = scantable( self.outfile, average=False ) |
---|
[2387] | 89 | self.nchan = len(s._getspectrum(0)) |
---|
[2356] | 90 | nrow = s.nrow() |
---|
[2360] | 91 | pols = numpy.ones( nrow, dtype=int ) |
---|
[2356] | 92 | for i in xrange(nrow): |
---|
[2360] | 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 |
---|
[2356] | 100 | |
---|
| 101 | idx = 0 |
---|
[2372] | 102 | d0 = s.get_direction( 0 ).split()[-1] |
---|
| 103 | while ( s.get_direction(self.npol*idx).split()[-1] == d0 ): |
---|
[2356] | 104 | idx += 1 |
---|
[2367] | 105 | |
---|
[2372] | 106 | self.nx = idx |
---|
| 107 | self.ny = nrow / (self.npol * idx ) |
---|
[2360] | 108 | #print 'nx,ny=',self.nx,self.ny |
---|
[2372] | 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] ) |
---|
[2360] | 118 | #print 'cellx,celly=',self.cellx,self.celly |
---|
[2356] | 119 | |
---|
[2375] | 120 | def plot( self, chan=-1, pol=-1, plotobs=False, plotgrid=False ): |
---|
[2360] | 121 | if pol < 0: |
---|
| 122 | opt = 'averaged over pol' |
---|
[2356] | 123 | else: |
---|
[2360] | 124 | opt = 'pol %s'%(pol) |
---|
| 125 | if chan < 0: |
---|
| 126 | opt += ', averaged over channel' |
---|
| 127 | else: |
---|
| 128 | opt += ', channel %s'%(chan) |
---|
[2367] | 129 | data = self.getData( chan, pol ) |
---|
[2360] | 130 | title = 'Gridded Image (%s)'%(opt) |
---|
[2356] | 131 | pl.figure(10) |
---|
| 132 | pl.clf() |
---|
[2372] | 133 | # plot grid position |
---|
[2375] | 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') |
---|
[2372] | 145 | # plot observed position |
---|
[2375] | 146 | if plotobs: |
---|
[2390] | 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 |
---|
[2372] | 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] |
---|
[2356] | 161 | pl.imshow(data,extent=extent,origin='lower',interpolation='nearest') |
---|
| 162 | pl.colorbar() |
---|
| 163 | pl.xlabel('R.A. [rad]') |
---|
| 164 | pl.ylabel('Dec. [rad]') |
---|
[2358] | 165 | pl.title( title ) |
---|
[2367] | 166 | |
---|
[2390] | 167 | def createTableIn( self, tab ): |
---|
| 168 | del self.tablein |
---|
| 169 | self.tablein = scantable( tab, average=False ) |
---|
[2387] | 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 ) |
---|
[2390] | 177 | self.tablein.set_selection( sel ) |
---|
[2387] | 178 | self.nchan = len(self.tablein._getspectrum(0)) |
---|
[2390] | 179 | self.nrow = self.tablein.nrow() |
---|
[2387] | 180 | del sel |
---|
| 181 | |
---|
| 182 | |
---|
[2372] | 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 | |
---|
[2367] | 196 | def getData( self, chan=-1, pol=-1 ): |
---|
| 197 | if chan == -1: |
---|
| 198 | spectra = self.__chanAverage() |
---|
| 199 | else: |
---|
| 200 | spectra = self.__chanIndex( chan ) |
---|
[2372] | 201 | data = spectra.reshape( (self.npol,self.ny,self.nx) ) |
---|
[2367] | 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 ) |
---|
[2372] | 210 | nrow = s.nrow() |
---|
[2367] | 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 ) |
---|
[2372] | 223 | nrow = s.nrow() |
---|
[2367] | 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 | |
---|