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