[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 |
|
---|
| 15 | def defineImage( self, nx=-1, ny=-1, cellx='', celly='', center='' ):
|
---|
| 16 | self.gridder._defineimage( nx, ny, cellx, celly, center )
|
---|
| 17 |
|
---|
| 18 | def setOption( self, convType='box', convSupport=-1 ):
|
---|
| 19 | self.gridder._setoption( convType, convSupport )
|
---|
| 20 |
|
---|
| 21 | def grid( self ):
|
---|
| 22 | self.gridder._grid()
|
---|
| 23 |
|
---|
| 24 | def save( self, outfile='' ):
|
---|
| 25 | self.outfile = self.gridder._save( outfile )
|
---|
| 26 |
|
---|
[2358] | 27 | def plot( self, plotchan=-1 ):
|
---|
[2356] | 28 | plotter = _SDGridPlotter( self.infile, self.outfile )
|
---|
[2358] | 29 | plotter.plot( chan=plotchan )
|
---|
[2356] | 30 |
|
---|
| 31 | class _SDGridPlotter:
|
---|
| 32 | def __init__( self, infile, outfile=None ):
|
---|
| 33 | self.infile = infile
|
---|
| 34 | self.outfile = outfile
|
---|
| 35 | if self.outfile is None:
|
---|
| 36 | self.outfile = self.infile.rstrip('/')+'.grid'
|
---|
| 37 | self.grid = None
|
---|
| 38 | self.pointing = None
|
---|
| 39 | self.data = None
|
---|
| 40 | self.nx = -1
|
---|
| 41 | self.ny = -1
|
---|
| 42 | self.nchan = 0
|
---|
| 43 | self.cellx = 0.0
|
---|
| 44 | self.celly = 0.0
|
---|
| 45 | self.center = [0.0,0.0]
|
---|
| 46 | self.nonzero = [[0.0],[0.0]]
|
---|
| 47 | self.get()
|
---|
| 48 |
|
---|
| 49 | def get( self ):
|
---|
| 50 | s = scantable( self.infile, average=False )
|
---|
| 51 | self.pointing = numpy.array( s.get_directionval() ).transpose()
|
---|
| 52 | spectra = []
|
---|
| 53 | for i in xrange(s.nrow()):
|
---|
| 54 | spectra.append( s._getspectrum( i ) )
|
---|
| 55 | spectra = numpy.array( spectra ).transpose()
|
---|
| 56 | self.nchan = spectra.shape[0]
|
---|
| 57 | del s
|
---|
| 58 |
|
---|
| 59 | idx = spectra.nonzero()[1]
|
---|
[2358] | 60 | #self.nonzero = self.pointing.take( idx, axis=1 )
|
---|
[2356] | 61 |
|
---|
| 62 | s = scantable( self.outfile, average=False )
|
---|
| 63 | self.grid = numpy.array( s.get_directionval() ).transpose()
|
---|
| 64 | dirstring = s.get_direction()
|
---|
| 65 | nrow = s.nrow()
|
---|
| 66 | spectra = []
|
---|
| 67 | for i in xrange(nrow):
|
---|
| 68 | spectra.append( s._getspectrum( i ) )
|
---|
| 69 | spectra = numpy.array( spectra ).transpose()
|
---|
| 70 |
|
---|
| 71 | idx = 0
|
---|
| 72 | d0 = dirstring[0].split()[-1]
|
---|
| 73 | while ( dirstring[idx].split()[-1] == d0 ):
|
---|
| 74 | idx += 1
|
---|
| 75 |
|
---|
| 76 | self.ny = idx
|
---|
| 77 | self.nx = nrow / idx
|
---|
| 78 |
|
---|
| 79 | self.cellx = abs( self.grid[0][0] - self.grid[0][1] )
|
---|
| 80 | self.celly = abs( self.grid[1][0] - self.grid[1][self.ny] )
|
---|
| 81 |
|
---|
| 82 | self.data = spectra.reshape( (self.nchan,self.nx,self.ny) )
|
---|
| 83 |
|
---|
| 84 | def plot( self, chan=-1 ):
|
---|
| 85 | if chan < 0:
|
---|
| 86 | data = self.data.mean(axis=0)
|
---|
[2358] | 87 | title = 'Gridded Image (averaged over channel)'
|
---|
[2356] | 88 | else:
|
---|
| 89 | data = self.data[chan]
|
---|
[2358] | 90 | title = 'Gridded Image (channel %s)'%(chan)
|
---|
[2356] | 91 | pl.figure(10)
|
---|
| 92 | pl.clf()
|
---|
| 93 | pl.plot(self.grid[0],self.grid[1],'.',color='blue')
|
---|
| 94 | pl.plot(self.pointing[0],self.pointing[1],'.',color='red')
|
---|
[2358] | 95 | #pl.plot(self.nonzero[0],self.nonzero[1],'o',color='green')
|
---|
[2356] | 96 | extent=[self.grid[0].min()-0.5*self.cellx,
|
---|
| 97 | self.grid[0].max()+0.5*self.cellx,
|
---|
| 98 | self.grid[1].min()-0.5*self.celly,
|
---|
| 99 | self.grid[1].max()+0.5*self.celly]
|
---|
| 100 | pl.imshow(data,extent=extent,origin='lower',interpolation='nearest')
|
---|
| 101 | pl.colorbar()
|
---|
| 102 | pl.xlabel('R.A. [rad]')
|
---|
| 103 | pl.ylabel('Dec. [rad]')
|
---|
[2358] | 104 | pl.title( title )
|
---|