[2356] | 1 | import numpy
|
---|
| 2 | from asap.scantable import scantable
|
---|
| 3 | from asap._asap import stgrid
|
---|
| 4 | import pylab as pl
|
---|
| 5 |
|
---|
| 6 | class asapgrid:
|
---|
| 7 | def __init__( self, infile ):
|
---|
| 8 | self.infile = infile
|
---|
| 9 | self.outfile = None
|
---|
| 10 | self.gridder = stgrid( self.infile )
|
---|
| 11 |
|
---|
| 12 | def setData( self, infile ):
|
---|
| 13 | self.gridder._setin( infile )
|
---|
| 14 |
|
---|
[2362] | 15 | def setIF( self, ifno ):
|
---|
| 16 | self.gridder._setif( ifno )
|
---|
| 17 |
|
---|
[2360] | 18 | def setPolList( self, pollist ):
|
---|
| 19 | self.gridder._setpollist( pollist )
|
---|
| 20 |
|
---|
[2356] | 21 | def defineImage( self, nx=-1, ny=-1, cellx='', celly='', center='' ):
|
---|
| 22 | self.gridder._defineimage( nx, ny, cellx, celly, center )
|
---|
| 23 |
|
---|
[2363] | 24 | def setOption( self, convType='box', width=-1 ):
|
---|
| 25 | self.gridder._setoption( convType, width )
|
---|
[2356] | 26 |
|
---|
[2361] | 27 | def setWeight( self, weightType='uniform' ):
|
---|
| 28 | self.gridder._setweight( weightType )
|
---|
| 29 |
|
---|
[2356] | 30 | def grid( self ):
|
---|
| 31 | self.gridder._grid()
|
---|
| 32 |
|
---|
| 33 | def save( self, outfile='' ):
|
---|
| 34 | self.outfile = self.gridder._save( outfile )
|
---|
| 35 |
|
---|
[2360] | 36 | def plot( self, plotchan=-1, plotpol=-1 ):
|
---|
[2356] | 37 | plotter = _SDGridPlotter( self.infile, self.outfile )
|
---|
[2360] | 38 | plotter.plot( chan=plotchan, pol=plotpol )
|
---|
[2356] | 39 |
|
---|
| 40 | class _SDGridPlotter:
|
---|
| 41 | def __init__( self, infile, outfile=None ):
|
---|
| 42 | self.infile = infile
|
---|
| 43 | self.outfile = outfile
|
---|
| 44 | if self.outfile is None:
|
---|
| 45 | self.outfile = self.infile.rstrip('/')+'.grid'
|
---|
| 46 | self.grid = None
|
---|
| 47 | self.pointing = None
|
---|
| 48 | self.data = None
|
---|
| 49 | self.nx = -1
|
---|
| 50 | self.ny = -1
|
---|
| 51 | self.nchan = 0
|
---|
[2360] | 52 | self.npol = 0
|
---|
| 53 | self.pollist = []
|
---|
[2356] | 54 | self.cellx = 0.0
|
---|
| 55 | self.celly = 0.0
|
---|
| 56 | self.center = [0.0,0.0]
|
---|
| 57 | self.nonzero = [[0.0],[0.0]]
|
---|
| 58 | self.get()
|
---|
| 59 |
|
---|
| 60 | def get( self ):
|
---|
| 61 | s = scantable( self.infile, average=False )
|
---|
| 62 | self.pointing = numpy.array( s.get_directionval() ).transpose()
|
---|
| 63 | spectra = []
|
---|
| 64 | for i in xrange(s.nrow()):
|
---|
| 65 | spectra.append( s._getspectrum( i ) )
|
---|
| 66 | spectra = numpy.array( spectra ).transpose()
|
---|
| 67 | self.nchan = spectra.shape[0]
|
---|
| 68 | del s
|
---|
| 69 |
|
---|
| 70 | s = scantable( self.outfile, average=False )
|
---|
| 71 | nrow = s.nrow()
|
---|
[2360] | 72 | pols = numpy.ones( nrow, dtype=int )
|
---|
[2356] | 73 | for i in xrange(nrow):
|
---|
[2360] | 74 | pols[i] = s.getpol(i)
|
---|
| 75 | self.pollist, indices = numpy.unique( pols, return_inverse=True )
|
---|
| 76 | self.npol = len(self.pollist)
|
---|
| 77 | self.pollist = self.pollist[indices[:self.npol]]
|
---|
| 78 | #print 'pollist=',self.pollist
|
---|
| 79 | #print 'npol=',self.npol
|
---|
| 80 | #print 'nrow=',nrow
|
---|
| 81 | dirstring = numpy.array(s.get_direction()).take(range(0,nrow,self.npol))
|
---|
| 82 | self.grid = numpy.array( s.get_directionval() ).take(range(0,nrow,self.npol),axis=0).transpose()
|
---|
| 83 | spectra = numpy.zeros( (self.npol,self.nchan,nrow/self.npol), dtype=float )
|
---|
| 84 | irow = 0
|
---|
| 85 | for i in xrange(nrow/self.npol):
|
---|
| 86 | for ip in xrange(self.npol):
|
---|
| 87 | spectra[ip,:,i] = s._getspectrum( irow )
|
---|
| 88 | irow += 1
|
---|
[2356] | 89 |
|
---|
| 90 | idx = 0
|
---|
| 91 | d0 = dirstring[0].split()[-1]
|
---|
| 92 | while ( dirstring[idx].split()[-1] == d0 ):
|
---|
| 93 | idx += 1
|
---|
| 94 | self.ny = idx
|
---|
[2360] | 95 | self.nx = nrow / (self.npol * idx )
|
---|
| 96 | #print 'nx,ny=',self.nx,self.ny
|
---|
[2356] | 97 |
|
---|
| 98 | self.cellx = abs( self.grid[0][0] - self.grid[0][1] )
|
---|
| 99 | self.celly = abs( self.grid[1][0] - self.grid[1][self.ny] )
|
---|
[2360] | 100 | #print 'cellx,celly=',self.cellx,self.celly
|
---|
[2356] | 101 |
|
---|
[2360] | 102 | self.data = spectra.reshape( (self.npol,self.nchan,self.nx,self.ny) )
|
---|
[2356] | 103 |
|
---|
[2360] | 104 | def plot( self, chan=-1, pol=-1 ):
|
---|
| 105 | if pol < 0:
|
---|
[2356] | 106 | data = self.data.mean(axis=0)
|
---|
[2360] | 107 | opt = 'averaged over pol'
|
---|
[2356] | 108 | else:
|
---|
[2360] | 109 | idx = self.pollist.tolist().index( pol )
|
---|
| 110 | #print 'idx=',idx
|
---|
| 111 | data = self.data[idx]
|
---|
| 112 | opt = 'pol %s'%(pol)
|
---|
| 113 | if chan < 0:
|
---|
| 114 | data = data.mean(axis=0)
|
---|
| 115 | opt += ', averaged over channel'
|
---|
| 116 | else:
|
---|
| 117 | data = data[chan]
|
---|
| 118 | opt += ', channel %s'%(chan)
|
---|
| 119 | title = 'Gridded Image (%s)'%(opt)
|
---|
[2356] | 120 | pl.figure(10)
|
---|
| 121 | pl.clf()
|
---|
| 122 | pl.plot(self.grid[0],self.grid[1],'.',color='blue')
|
---|
| 123 | pl.plot(self.pointing[0],self.pointing[1],'.',color='red')
|
---|
| 124 | extent=[self.grid[0].min()-0.5*self.cellx,
|
---|
| 125 | self.grid[0].max()+0.5*self.cellx,
|
---|
| 126 | self.grid[1].min()-0.5*self.celly,
|
---|
| 127 | self.grid[1].max()+0.5*self.celly]
|
---|
| 128 | pl.imshow(data,extent=extent,origin='lower',interpolation='nearest')
|
---|
| 129 | pl.colorbar()
|
---|
| 130 | pl.xlabel('R.A. [rad]')
|
---|
| 131 | pl.ylabel('Dec. [rad]')
|
---|
[2358] | 132 | pl.title( title )
|
---|