source: trunk/python/asapgrid.py@ 2372

Last change on this file since 2372 was 2372, checked in by Takeshi Nakazato, 13 years ago

New Development: No

JIRA Issue: Yes CAS-2816

Ready for Test: No

Interface Changes: No

What Interface Changed: Please list interface changes

Test Programs: List test programs

Put in Release Notes: Yes/No

Module(s): Module Names change impacts.

Description: Describe your changes here...

Speed up plot routine.


File size: 6.8 KB
Line 
1import numpy
2from asap import rcParams
3from asap.scantable import scantable
4from asap.selector import selector
5from asap._asap import stgrid
6import pylab as pl
7from logging import asaplog
8
9class asapgrid:
10 def __init__( self, infile ):
11 self.infile = infile
12 self.outfile = None
13 self.gridder = stgrid( self.infile )
14 self.ifno = None
15
16 def setData( self, infile ):
17 self.gridder._setin( infile )
18
19 def setIF( self, ifno ):
20 self.ifno = ifno
21 self.gridder._setif( self.ifno )
22
23 def setPolList( self, pollist ):
24 self.gridder._setpollist( pollist )
25
26 def setScanList( self, scanlist ):
27 self.gridder._setscanlist( scanlist )
28
29 def defineImage( self, nx=-1, ny=-1, cellx='', celly='', center='' ):
30 self.gridder._defineimage( nx, ny, cellx, celly, center )
31
32 def setFunc( self, func='box', width=-1 ):
33 self.gridder._setfunc( func, width )
34
35 def setWeight( self, weightType='uniform' ):
36 self.gridder._setweight( weightType )
37
38 def grid( self ):
39 self.gridder._grid()
40
41 def save( self, outfile='' ):
42 self.outfile = self.gridder._save( outfile )
43
44 def plot( self, plotchan=-1, plotpol=-1 ):
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 )
51 plotter.plot( chan=plotchan, pol=plotpol )
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')
57
58class _SDGridPlotter:
59 def __init__( self, infile, outfile=None, ifno=0 ):
60 self.infile = infile
61 self.outfile = outfile
62 if self.outfile is None:
63 self.outfile = self.infile.rstrip('/')+'.grid'
64 self.nx = -1
65 self.ny = -1
66 self.nchan = 0
67 self.npol = 0
68 self.pollist = []
69 self.cellx = 0.0
70 self.celly = 0.0
71 self.center = [0.0,0.0]
72 self.nonzero = [[0.0],[0.0]]
73 self.ifno = ifno
74 self.tablein = None
75 self.nrow = 0
76 self.blc = None
77 self.trc = None
78 self.get()
79
80 def get( self ):
81 self.tablein = scantable( self.infile, average=False )
82 sel = selector()
83 sel.set_ifs( self.ifno )
84 self.tablein.set_selection( sel )
85 self.nchan = len(self.tablein._getspectrum(0))
86 self.nrow = self.tablein.nrow()
87 del sel
88
89 s = scantable( self.outfile, average=False )
90 nrow = s.nrow()
91 pols = numpy.ones( nrow, dtype=int )
92 for i in xrange(nrow):
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
101 idx = 0
102 d0 = s.get_direction( 0 ).split()[-1]
103 while ( s.get_direction(self.npol*idx).split()[-1] == d0 ):
104 idx += 1
105
106 self.nx = idx
107 self.ny = nrow / (self.npol * idx )
108 #print 'nx,ny=',self.nx,self.ny
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] )
118 #print 'cellx,celly=',self.cellx,self.celly
119
120 def plot( self, chan=-1, pol=-1 ):
121 if pol < 0:
122 opt = 'averaged over pol'
123 else:
124 opt = 'pol %s'%(pol)
125 if chan < 0:
126 opt += ', averaged over channel'
127 else:
128 opt += ', channel %s'%(chan)
129 data = self.getData( chan, pol )
130 title = 'Gridded Image (%s)'%(opt)
131 pl.figure(10)
132 pl.clf()
133 # plot grid position
134 x = numpy.arange(self.blc[0],self.trc[0]+0.5*self.cellx,self.cellx,dtype=float)
135 #print 'len(x)=',len(x)
136 #print 'x=',x
137 ybase = numpy.ones(self.ny,dtype=float)*self.blc[1]
138 #print 'len(ybase)=',len(ybase)
139 incr = self.celly
140 for iy in xrange(self.ny):
141 y = ybase + iy * incr
142 #print y
143 pl.plot(x,y,',',color='blue')
144 # plot observed position
145 irow = 0
146 while ( irow < self.nrow ):
147 chunk = self.getPointingChunk( irow )
148 #print chunk
149 pl.plot(chunk[0],chunk[1],',',color='green')
150 irow += chunk.shape[1]
151 #print irow
152 # show image
153 extent=[self.blc[0]-0.5*self.cellx,
154 self.trc[0]+0.5*self.cellx,
155 self.blc[1]-0.5*self.celly,
156 self.trc[1]+0.5*self.celly]
157 pl.imshow(data,extent=extent,origin='lower',interpolation='nearest')
158 pl.colorbar()
159 pl.xlabel('R.A. [rad]')
160 pl.ylabel('Dec. [rad]')
161 pl.title( title )
162
163 def getPointingChunk( self, irow ):
164 numchunk = 1000
165 nrow = min( self.nrow-irow, numchunk )
166 #print 'nrow=',nrow
167 v = numpy.zeros( (2,nrow), dtype=float )
168 idx = 0
169 for i in xrange(irow,irow+nrow):
170 d = self.tablein.get_directionval( i )
171 v[0,idx] = d[0]
172 v[1,idx] = d[1]
173 idx += 1
174 return v
175
176 def getData( self, chan=-1, pol=-1 ):
177 if chan == -1:
178 spectra = self.__chanAverage()
179 else:
180 spectra = self.__chanIndex( chan )
181 data = spectra.reshape( (self.npol,self.ny,self.nx) )
182 if pol == -1:
183 retval = data.mean(axis=0)
184 else:
185 retval = data[pol]
186 #retval[0][self.nx-1] = -1.0
187 return retval
188
189 def __chanAverage( self ):
190 s = scantable( self.outfile, average=False )
191 nrow = s.nrow()
192 spectra = numpy.zeros( (self.npol,nrow/self.npol), dtype=float )
193 irow = 0
194 sp = [0 for i in xrange(self.nchan)]
195 for i in xrange(nrow/self.npol):
196 for ip in xrange(self.npol):
197 sp = s._getspectrum( irow )
198 spectra[ip,i] = numpy.mean( sp )
199 irow += 1
200 return spectra
201
202 def __chanIndex( self, idx ):
203 s = scantable( self.outfile, average=False )
204 nrow = s.nrow()
205 spectra = numpy.zeros( (self.npol,nrow/self.npol), dtype=float )
206 irow = 0
207 sp = [0 for i in xrange(self.nchan)]
208 for i in xrange(nrow/self.npol):
209 for ip in xrange(self.npol):
210 sp = s._getspectrum( irow )
211 spectra[ip,i] = sp[idx]
212 irow += 1
213 return spectra
214
215
Note: See TracBrowser for help on using the repository browser.