[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 | |
---|
[2360] | 44 | def plot( self, plotchan=-1, plotpol=-1 ): |
---|
[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 ) |
---|
[2360] | 51 | plotter.plot( chan=plotchan, pol=plotpol ) |
---|
[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: |
---|
[2367] | 59 | def __init__( self, infile, outfile=None, ifno=0 ): |
---|
[2356] | 60 | self.infile = infile |
---|
| 61 | self.outfile = outfile |
---|
| 62 | if self.outfile is None: |
---|
| 63 | self.outfile = self.infile.rstrip('/')+'.grid' |
---|
| 64 | self.grid = None |
---|
| 65 | self.pointing = None |
---|
| 66 | self.nx = -1 |
---|
| 67 | self.ny = -1 |
---|
| 68 | self.nchan = 0 |
---|
[2360] | 69 | self.npol = 0 |
---|
| 70 | self.pollist = [] |
---|
[2356] | 71 | self.cellx = 0.0 |
---|
| 72 | self.celly = 0.0 |
---|
| 73 | self.center = [0.0,0.0] |
---|
| 74 | self.nonzero = [[0.0],[0.0]] |
---|
[2367] | 75 | self.ifno = ifno |
---|
[2356] | 76 | self.get() |
---|
| 77 | |
---|
| 78 | def get( self ): |
---|
| 79 | s = scantable( self.infile, average=False ) |
---|
[2367] | 80 | sel = selector() |
---|
| 81 | sel.set_ifs( self.ifno ) |
---|
| 82 | s.set_selection( sel ) |
---|
[2356] | 83 | self.pointing = numpy.array( s.get_directionval() ).transpose() |
---|
[2367] | 84 | self.nchan = len(s._getspectrum(0)) |
---|
| 85 | s.set_selection() |
---|
[2356] | 86 | del s |
---|
[2367] | 87 | del sel |
---|
[2356] | 88 | |
---|
| 89 | s = scantable( self.outfile, average=False ) |
---|
| 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 |
---|
| 100 | dirstring = numpy.array(s.get_direction()).take(range(0,nrow,self.npol)) |
---|
| 101 | self.grid = numpy.array( s.get_directionval() ).take(range(0,nrow,self.npol),axis=0).transpose() |
---|
[2356] | 102 | |
---|
| 103 | idx = 0 |
---|
| 104 | d0 = dirstring[0].split()[-1] |
---|
| 105 | while ( dirstring[idx].split()[-1] == d0 ): |
---|
| 106 | idx += 1 |
---|
[2367] | 107 | |
---|
[2356] | 108 | self.ny = idx |
---|
[2360] | 109 | self.nx = nrow / (self.npol * idx ) |
---|
| 110 | #print 'nx,ny=',self.nx,self.ny |
---|
[2356] | 111 | |
---|
| 112 | self.cellx = abs( self.grid[0][0] - self.grid[0][1] ) |
---|
| 113 | self.celly = abs( self.grid[1][0] - self.grid[1][self.ny] ) |
---|
[2360] | 114 | #print 'cellx,celly=',self.cellx,self.celly |
---|
[2356] | 115 | |
---|
[2360] | 116 | def plot( self, chan=-1, pol=-1 ): |
---|
| 117 | if pol < 0: |
---|
| 118 | opt = 'averaged over pol' |
---|
[2356] | 119 | else: |
---|
[2360] | 120 | opt = 'pol %s'%(pol) |
---|
| 121 | if chan < 0: |
---|
| 122 | opt += ', averaged over channel' |
---|
| 123 | else: |
---|
| 124 | opt += ', channel %s'%(chan) |
---|
[2367] | 125 | data = self.getData( chan, pol ) |
---|
[2360] | 126 | title = 'Gridded Image (%s)'%(opt) |
---|
[2356] | 127 | pl.figure(10) |
---|
| 128 | pl.clf() |
---|
[2365] | 129 | pl.plot(self.grid[0],self.grid[1],',',color='blue') |
---|
| 130 | pl.plot(self.pointing[0],self.pointing[1],',',color='green') |
---|
[2356] | 131 | extent=[self.grid[0].min()-0.5*self.cellx, |
---|
| 132 | self.grid[0].max()+0.5*self.cellx, |
---|
| 133 | self.grid[1].min()-0.5*self.celly, |
---|
| 134 | self.grid[1].max()+0.5*self.celly] |
---|
| 135 | pl.imshow(data,extent=extent,origin='lower',interpolation='nearest') |
---|
| 136 | pl.colorbar() |
---|
| 137 | pl.xlabel('R.A. [rad]') |
---|
| 138 | pl.ylabel('Dec. [rad]') |
---|
[2358] | 139 | pl.title( title ) |
---|
[2367] | 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 | |
---|