casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
test_sdgrid.py
Go to the documentation of this file.
00001 import os
00002 import sys
00003 import shutil
00004 from __main__ import default
00005 from tasks import *
00006 from taskinit import *
00007 import unittest
00008 import sha
00009 import time
00010 import numpy
00011 import re
00012 import string
00013 
00014 from sdgrid import sdgrid
00015 import asap as sd
00016 
00017 #
00018 # Unit test of sdgrid task.
00019 # 
00020 
00021 ###
00022 # Base class for sdimaging unit test
00023 ###
00024 class sdgrid_unittest_base(object):
00025     """
00026     """
00027     taskname='sdgrid'
00028     datapath=os.environ.get('CASAPATH').split()[0] + '/data/regression/unittest/sdgrid/'
00029     data=None
00030     tolerance=0.01
00031     outfile='sdgrid.asap.grid'
00032 
00033     def getdata(self):
00034         tb.open(self.outfile)
00035         self.data = tb.getcol('SPECTRA')
00036         tb.close()
00037 
00038     def check(self,ref,val):
00039         diff=abs((val-ref)/ref)
00040         #print 'diff=',diff
00041         self.assertTrue(diff < self.tolerance,
00042                         msg='grid result differ: ref %s, val %s'%(ref,val))
00043 
00044     def nonzero(self,ref,index):
00045         #refpix=ref[1]
00046         #resultpix=index[1]
00047         refpix = ref
00048         resultpix = index
00049         msglt = 'There are nonzero pixels that should be zero'
00050         msggt = 'There are zero pixels that should be nonzero'
00051         self.assertEqual(len(refpix),len(resultpix),
00052                          msg=(msglt if len(refpix) < len(resultpix) else msggt))
00053         for i in xrange(len(refpix)):
00054             self.assertEqual(refpix[i],resultpix[i],
00055                              msg='Index doesn\'t match: ref %s, result %s'%(refpix[i],resultpix[i]))
00056 
00057     def generateNonzeroPix(self,npol,npix,width):
00058         index=[]
00059         start=(npix-1)/2-(width-1)
00060         end=(npix-1)/2+(width-1)
00061         #print 'start=',start,',end=',end
00062         for i in xrange(start,end+1):
00063             tweak=npol if (width>=4 and (i==start or i==end)) else 0
00064             ifrom=npol*npix*i+npol*start+tweak
00065             ito=ifrom+npol*2*(width-1)-2*tweak
00066             index+=range(ifrom,ito+npol)
00067         #print 'index=',index
00068         #nonzeropix_ref=(numpy.zeros(len(index),int),numpy.array(index))
00069         nonzeropix_ref=numpy.array(index)
00070         return nonzeropix_ref
00071 
00072     def addrow(self,val):
00073         tb.open(self.datapath+'/'+self.rawfile)
00074         tb.copyrows(self.rawfile,0,-1,1)
00075         tb.close()
00076         tb.open(self.rawfile,nomodify=False)
00077         tb.putcell('SPECTRA',tb.nrows()-1,val)
00078         tb.flush()
00079         tb.close()
00080 
00081 ###
00082 # Test on bad parameter settings
00083 ###
00084 class sdgrid_failure_case(sdgrid_unittest_base,unittest.TestCase):
00085     """
00086     Test on bad parameter setting
00087     """
00088     # Input and output names
00089     prefix=sdgrid_unittest_base.taskname+'Test0'
00090     badid=99
00091     rawfile='testimage1chan.1point.asap'
00092 
00093     def setUp(self):
00094         if os.path.exists(self.rawfile):
00095             shutil.rmtree(self.rawfile)
00096         shutil.copytree(self.datapath+self.rawfile, self.rawfile)
00097 
00098         default(sdgrid)
00099 
00100     def tearDown(self):
00101         if (os.path.exists(self.rawfile)):
00102             shutil.rmtree(self.rawfile)
00103         if (os.path.exists(self.outfile)):
00104             shutil.rmtree(self.outfile)
00105 
00106     def test000(self):
00107         """Test 000: Default parameters"""
00108         # argument verification error
00109         res=sdgrid()
00110         self.assertFalse(res)
00111 
00112     def test001(self):
00113         """Test001: Invalid IFNO"""
00114         try:
00115             res=sdgrid(infiles=self.rawfile,ifno=self.badid,npix=16,cell='20arcsec',outfile=self.outfile)
00116             self.assertTrue(False,
00117                             msg='The task must throw exception')
00118         except Exception, e:
00119             pos=str(e).find('No corresponding rows for given selection: IFNO %s'%(self.badid))
00120             self.assertNotEqual(pos,-1,
00121                                 msg='Unexpected exception was thrown: %s'%(str(e)))
00122 
00123     def test002(self):
00124         """Test002: Invalid POLNO"""
00125         try:
00126             res=sdgrid(infiles=self.rawfile,pollist=self.badid,npix=16,cell='20arcsec',outfile=self.outfile)
00127             self.assertTrue(False,
00128                             msg='The task must throw exception')
00129         except Exception, e:
00130             pos=str(e).find('Empty pollist')
00131             self.assertNotEqual(pos,-1,
00132                                 msg='Unexpected exception was thrown: %s'%(str(e)))
00133 
00134     def test003(self):
00135         """Test003: Invalid gridfunction"""
00136         # argument verification error
00137         res=sdgrid(infiles=self.rawfile,gridfunction='NONE',npix=16,cell='20arcsec',outfile=self.outfile)
00138         self.assertFalse(res)
00139 
00140     def test004(self):
00141         """Test004: Invalid weight type"""
00142         # argument verification error
00143         res=sdgrid(infiles=self.rawfile,weight='NONE',npix=16,cell='20arcsec',outfile=self.outfile)
00144         self.assertFalse(res)
00145 
00146     def test005(self):
00147         """Test005: Check overwrite option"""
00148         shutil.copytree(self.rawfile,self.outfile)
00149         try:
00150             res=sdgrid(infiles=self.rawfile,npix=16,cell='20arcsec',outfile=self.outfile,overwrite=False)
00151             self.assertTrue(False,
00152                             msg='The task must throw exception')
00153         except Exception, e:
00154             pos=str(e).find('Output file \'%s\' exists.'%(self.outfile))
00155             self.assertNotEqual(pos,-1,
00156                                 msg='Unexpected exception was thrown: %s'%(str(e)))
00157 
00158 # Those two tests are meaningless
00159 #    def test006(self):
00160 #        """Test006: Invalid npix"""
00161 #        res=sdgrid(infiles=self.rawfile,npix=-99,cell='',outfile=self.outfile)
00162 #        self.assertFalse(res)
00163 #
00164 #    def test007(self):
00165 #        """Test007: Invalid unit for cell"""
00166 #        res=sdgrid(infiles=self.rawfile,npix=16,cell='20none',outfile=self.outfile)
00167 #        self.assertFalse(res)
00168 
00169     def test008(self):
00170         """Test008: Invalid format for center coordinate"""
00171         try:
00172             res=sdgrid(infiles=self.rawfile,npix=16,cell='20arcsec',outfile=self.outfile,center='Invalid format')
00173             self.assertTrue(False,
00174                             msg='The task must throw exception')
00175         except Exception, e:
00176             pos=str(e).find('Empty QuantumHolder argument for asQuantumDouble')
00177             self.assertNotEqual(pos,-1,
00178                                 msg='Unexpected exception was thrown: %s'%(str(e)))
00179 
00180         
00181 
00182 ###
00183 # Test simple gridding
00184 ###
00185 class sdgrid_single_integ(sdgrid_unittest_base,unittest.TestCase):
00186     """
00187     Test simple gridding using data containing only one integration.
00188     """
00189     # Input and output names
00190     rawfile='testimage1chan.1point.asap'
00191     #prefix=sdgrid_unittest_base.taskname+'Test1'
00192     #outfile=prefix+'.asap'
00193 
00194     def setUp(self):
00195         if os.path.exists(self.rawfile):
00196             shutil.rmtree(self.rawfile)
00197         shutil.copytree(self.datapath+self.rawfile, self.rawfile)
00198 
00199         default(sdgrid)
00200 
00201     def tearDown(self):
00202         if (os.path.exists(self.rawfile)):
00203             shutil.rmtree(self.rawfile)
00204         if (os.path.exists(self.outfile)):
00205             shutil.rmtree(self.outfile)
00206 
00207     def test100(self):
00208         """Test 100: Box kernel"""
00209         npix=17
00210         res=sdgrid(infiles=self.rawfile,gridfunction='BOX',npix=npix,cell='20arcsec',outfile=self.outfile,plot=False)
00211         self.assertEqual(res,None,
00212                          msg='Any error occurred during gridding')
00213         self.getdata()
00214 
00215         # center is only nonzero pixel
00216         npol=2
00217         width=1
00218         nonzeropix_ref=self.generateNonzeroPix(npol,npix,width)
00219         nonzeropix=self.data.nonzero()[1]
00220         self.nonzero(nonzeropix_ref,nonzeropix)
00221 
00222         # pol0 must be 10.0
00223         pol0=self.data[0,nonzeropix[0]]
00224         self.check(10.0,pol0)
00225 
00226         # pol1 must be 1.0
00227         pol1=self.data[0,nonzeropix[1]]
00228         self.check(1.0,pol1)
00229         
00230 
00231     def test101(self):
00232         """Test101: SF kernel"""
00233         npix=17
00234         res=sdgrid(infiles=self.rawfile,gridfunction='SF',npix=npix,cell='20arcsec',outfile=self.outfile,plot=False)
00235         self.assertEqual(res,None,
00236                          msg='Any error occurred during gridding')
00237         self.getdata()
00238 
00239         # default width for SF is 3
00240         width=3
00241         npol=2
00242         nonzeropix=self.data.nonzero()[1]
00243         nonzeropix_ref=self.generateNonzeroPix(npol,npix,width)
00244         self.nonzero(nonzeropix_ref,nonzeropix)
00245 
00246         # pol0 must be 10.0 while pol1 must be 1.0
00247         for i in xrange(0,len(nonzeropix),npol):
00248             pol0=self.data[0,nonzeropix[i]]
00249             self.check(10.0,pol0)
00250             pol1=self.data[0,nonzeropix[i+1]]
00251             self.check(1.0,pol1)
00252         
00253 
00254     def test102(self):
00255         """Test102: Gaussian kernel"""
00256         npix=17
00257         res=sdgrid(infiles=self.rawfile,gridfunction='GAUSS',npix=npix,cell='20arcsec',outfile=self.outfile,plot=False)
00258         self.assertEqual(res,None,
00259                          msg='Any error occurred during gridding')
00260         self.getdata()
00261 
00262         # default width for GAUSS is 4
00263         width=2
00264         npol=2
00265         nonzeropix=self.data.nonzero()[1]
00266         nonzeropix_ref=numpy.array([218, 219, 220, 221, 222, 223, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 284, 285, 286, 287, 288, 289, 290, 291, 292, 293, 318, 319, 320, 321, 322, 323, 324, 325, 326, 327, 354, 355, 356, 357, 358, 359])
00267         #nonzeropix_ref=self.generateNonzeroPix(npol,npix,width)
00268         self.nonzero(nonzeropix_ref,nonzeropix)
00269         
00270         # pol0 must be 10.0 while pol1 must be 1.0
00271         for i in xrange(0,len(nonzeropix),npol):
00272             pol0=self.data[0,nonzeropix[i]]
00273             self.check(10.0,pol0)
00274             pol1=self.data[0,nonzeropix[i+1]]
00275             self.check(1.0,pol1)
00276 
00277     def test103(self):
00278         """Test103: Gaussian*Jinc kernel"""
00279         npix=17
00280         res=sdgrid(infiles=self.rawfile,gridfunction='GJINC',npix=npix,cell='20arcsec',outfile=self.outfile,plot=False)
00281         self.assertEqual(res,None,
00282                          msg='Any error occurred during gridding')
00283         self.getdata()
00284 
00285         # default width for GAUSS is 4
00286         width=2
00287         npol=2
00288         nonzeropix=self.data.nonzero()[1]
00289         nonzeropix_ref=numpy.array([252, 253, 254, 255, 256, 257, 286, 287, 288, 289, 290, 291, 320, 321, 322, 323, 324, 325])
00290         #nonzeropix_ref=self.generateNonzeroPix(npol,npix,width)
00291         self.nonzero(nonzeropix_ref,nonzeropix)
00292         
00293         # pol0 must be 10.0 while pol1 must be 1.0
00294         for i in xrange(0,len(nonzeropix),npol):
00295             pol0=self.data[0,nonzeropix[i]]
00296             self.check(10.0,pol0)
00297             pol1=self.data[0,nonzeropix[i+1]]
00298             self.check(1.0,pol1)
00299         
00300 
00301 ###
00302 # Test clipminmax
00303 ###
00304 class sdgrid_clipping(sdgrid_unittest_base,unittest.TestCase):
00305     """
00306     Test clipminmax
00307     """
00308     rawfile='testimage1chan.1point.asap'
00309     
00310     def setUp(self):
00311         if os.path.exists(self.rawfile):
00312             shutil.rmtree(self.rawfile)
00313         shutil.copytree(self.datapath+self.rawfile, self.rawfile)
00314 
00315         default(sdgrid)
00316 
00317         # modification of input file along with this test
00318         #   - add row to enable clipping
00319         #   - all polno set to 0
00320         self.addrow([0.1])
00321         tb.open(self.rawfile,nomodify=False)
00322         polno=tb.getcol('POLNO')
00323         polno[:]=0
00324         tb.putcol('POLNO',polno)
00325         tb.close()
00326 
00327     def tearDown(self):
00328         if (os.path.exists(self.rawfile)):
00329             shutil.rmtree(self.rawfile)
00330         if (os.path.exists(self.outfile)):
00331             shutil.rmtree(self.outfile)
00332 
00333     def test200(self):
00334         """Test 200: test clipping"""
00335         npix=17
00336         res=sdgrid(infiles=self.rawfile,gridfunction='BOX',npix=npix,cell='20arcsec',outfile=self.outfile,plot=False,clipminmax=True)
00337         self.assertEqual(res,None,
00338                          msg='Any error occurred during gridding')
00339         self.getdata()
00340         
00341         # center is only nonzero pixel
00342         npol=1
00343         width=1
00344         nonzeropix_ref=self.generateNonzeroPix(npol,npix,width)
00345         nonzeropix=self.data.nonzero()[1]
00346         #print nonzeropix
00347         self.nonzero(nonzeropix_ref,nonzeropix)
00348 
00349         # pol0 must be 1.0
00350         pol0=self.data[0,nonzeropix[0]]
00351         self.check(1.0,pol0)
00352         
00353 
00354 ###
00355 # Test for flag
00356 ###
00357 class sdgrid_flagging(sdgrid_unittest_base,unittest.TestCase):
00358     """
00359     Test for flag
00360     """
00361     rawfile='testimage1chan.1point.asap'
00362     
00363     def setUp(self):
00364         if os.path.exists(self.rawfile):
00365             shutil.rmtree(self.rawfile)
00366         shutil.copytree(self.datapath+self.rawfile, self.rawfile)
00367 
00368         default(sdgrid)
00369 
00370     def tearDown(self):
00371         if (os.path.exists(self.rawfile)):
00372             shutil.rmtree(self.rawfile)
00373         if (os.path.exists(self.outfile)):
00374             shutil.rmtree(self.outfile)
00375 
00376     def test300(self):
00377         """Test 300: Test channel flagging"""
00378         # channel flag pol0 data
00379         tb.open(self.rawfile,nomodify=False)
00380         fl=tb.getcell('FLAGTRA',0)
00381         fl[:]=1
00382         tb.putcell('FLAGTRA',0,fl)
00383         tb.close()
00384 
00385         # exec task
00386         npix=17
00387         res=sdgrid(infiles=self.rawfile,gridfunction='BOX',npix=npix,cell='20arcsec',outfile=self.outfile,plot=False)
00388         self.assertEqual(res,None,
00389                          msg='Any error occurred during gridding')
00390 
00391         # test result
00392         self._testresult(npix)
00393 
00394     def test301(self):
00395         """Test 301: Test row flagging"""
00396         # row flag pol0 data
00397         tb.open(self.rawfile,nomodify=False)
00398         fl=tb.getcell('FLAGROW',0)
00399         fl=1
00400         tb.putcell('FLAGROW',0,fl)
00401         tb.close()
00402 
00403         # exec task
00404         npix=17
00405         res=sdgrid(infiles=self.rawfile,gridfunction='BOX',npix=npix,cell='20arcsec',outfile=self.outfile,plot=False)
00406         self.assertEqual(res,None,
00407                          msg='Any error occurred during gridding')
00408 
00409         # test result
00410         self._testresult(npix)
00411 
00412     def _testresult(self,npix):
00413         self.getdata()
00414         
00415         # center is only nonzero pixel
00416         npol=2
00417         width=1
00418         nonzeropix_ref=self.generateNonzeroPix(npol,npix,width)
00419         # pol0 is flagged so that data must be zero
00420         #nonzeropix_ref2=(numpy.array([nonzeropix_ref[0][1]]),
00421         #                 numpy.array([nonzeropix_ref[1][1]]))
00422         nonzeropix_ref2=numpy.array([nonzeropix_ref[1]])
00423         nonzeropix=self.data.nonzero()[1]
00424         #print nonzeropix
00425         self.nonzero(nonzeropix_ref2,nonzeropix)
00426 
00427         # pol1 must be 1.0
00428         pol1=self.data[0,nonzeropix[0]]
00429         self.check(1.0,pol1)
00430         
00431 
00432 ###
00433 # Test various weighting
00434 ###
00435 class sdgrid_weighting(sdgrid_unittest_base,unittest.TestCase):
00436     """
00437     Test various weighting: UNIFORM, TSYS, TINTSYS
00438     """
00439     rawfile='testimage1chan.1point.asap'
00440     
00441     def setUp(self):
00442         if os.path.exists(self.rawfile):
00443             shutil.rmtree(self.rawfile)
00444         shutil.copytree(self.datapath+self.rawfile, self.rawfile)
00445 
00446         default(sdgrid)
00447 
00448         # modification of input file along with this test
00449         #   - all polno set to 0
00450         tb.open(self.rawfile,nomodify=False)
00451         polno=tb.getcol('POLNO')
00452         polno[:]=0
00453         tb.putcol('POLNO',polno)
00454         tb.close()
00455 
00456     def tearDown(self):
00457         if (os.path.exists(self.rawfile)):
00458             shutil.rmtree(self.rawfile)
00459         if (os.path.exists(self.outfile)):
00460             shutil.rmtree(self.outfile)
00461 
00462     def test400(self):
00463         """Test 400: test UNIFORM weighting"""
00464         npix=17
00465         res=sdgrid(infiles=self.rawfile,gridfunction='BOX',npix=npix,cell='20arcsec',outfile=self.outfile,plot=False,weight='UNIFORM')
00466         self.assertEqual(res,None,
00467                          msg='Any error occurred during gridding')
00468         self.getdata()
00469 
00470         # center is only nonzero pixel
00471         npol=1
00472         width=1
00473         nonzeropix_ref=self.generateNonzeroPix(npol,npix,width)
00474         nonzeropix=self.data.nonzero()[1]
00475         #print nonzeropix
00476         self.nonzero(nonzeropix_ref,nonzeropix)
00477 
00478         # pol0 must be 5.5 (={10.0+1.0}/{1.0+1.0}
00479         pol0=self.data[0,nonzeropix[0]]
00480         self.check(5.5,pol0)
00481         
00482     def test401(self):
00483         """Test 401: test TINT weighting"""
00484         # modify INTERVAL
00485         tb.open(self.rawfile,nomodify=False)
00486         integ=tb.getcol('INTERVAL')
00487         integ[0]=0.5
00488         integ[1]=1.0
00489         tb.putcol('INTERVAL',integ)
00490         tb.close()
00491 
00492         # exec task
00493         npix=17
00494         res=sdgrid(infiles=self.rawfile,gridfunction='BOX',npix=npix,cell='20arcsec',outfile=self.outfile,plot=False,weight='TINT')
00495         self.assertEqual(res,None,
00496                          msg='Any error occurred during gridding')
00497         self.getdata()
00498 
00499         # center is only nonzero pixel
00500         npol=1
00501         width=1
00502         nonzeropix_ref=self.generateNonzeroPix(npol,npix,width)
00503         nonzeropix=self.data.nonzero()[1]
00504         #print nonzeropix
00505         self.nonzero(nonzeropix_ref,nonzeropix)
00506 
00507         # pol0 must be 4.0 (={10.0*0.5+1.0*1.0}/{0.5+1.0})
00508         pol0=self.data[0,nonzeropix[0]]
00509         self.check(4.0,pol0)
00510         
00511 
00512     def test402(self):
00513         """Test402: test TSYS weighting"""
00514         # modify TSYS
00515         tb.open(self.rawfile,nomodify=False)
00516         tsys=tb.getcol('TSYS')
00517         tsys[:,0]=numpy.sqrt(2.0)
00518         tsys[:,1]=1.0
00519         tb.putcol('TSYS',tsys)
00520         tb.close()
00521 
00522         # exec task
00523         npix=17
00524         res=sdgrid(infiles=self.rawfile,gridfunction='BOX',npix=npix,cell='20arcsec',outfile=self.outfile,plot=False,weight='TSYS')
00525         self.assertEqual(res,None,
00526                          msg='Any error occurred during gridding')
00527         self.getdata()
00528 
00529         # center is only nonzero pixel
00530         npol=1
00531         width=1
00532         nonzeropix_ref=self.generateNonzeroPix(npol,npix,width)
00533         nonzeropix=self.data.nonzero()[1]
00534         #print nonzeropix
00535         self.nonzero(nonzeropix_ref,nonzeropix)
00536 
00537         # pol0 must be 4.0 (={10.0*0.5+1.0*1.0}/{0.5+1.0})
00538         pol0=self.data[0,nonzeropix[0]]
00539         self.check(4.0,pol0)
00540         
00541 
00542     def test403(self):
00543         """Test403: test TINTSYS weighting"""
00544         # modify TSYS and INTERVAL
00545         tb.open(self.rawfile,nomodify=False)
00546         tsys=tb.getcol('TSYS')
00547         tsys[:,0]=numpy.sqrt(2.0)
00548         tsys[:,1]=1.0
00549         tb.putcol('TSYS',tsys)
00550         integ=tb.getcol('INTERVAL')
00551         integ[0]=0.5
00552         integ[1]=1.0
00553         tb.putcol('INTERVAL',integ)
00554         tb.close()
00555 
00556         # exec task
00557         npix=17
00558         res=sdgrid(infiles=self.rawfile,gridfunction='BOX',npix=npix,cell='20arcsec',outfile=self.outfile,plot=False,weight='TINTSYS')
00559         self.assertEqual(res,None,
00560                          msg='Any error occurred during gridding')
00561         self.getdata()
00562 
00563         # center is only nonzero pixel
00564         npol=1
00565         width=1
00566         nonzeropix_ref=self.generateNonzeroPix(npol,npix,width)
00567         nonzeropix=self.data.nonzero()[1]
00568         #print nonzeropix
00569         self.nonzero(nonzeropix_ref,nonzeropix)
00570 
00571         # pol0 must be 2.8 (={10.0*0.5*0.5+1.0*1.0*1.0}/{0.5*0.5+1.0*1.0})
00572         pol0=self.data[0,nonzeropix[0]]
00573         self.check(2.8,pol0)
00574         
00575 ###
00576 # Test grid map data
00577 ###
00578 class sdgrid_map(sdgrid_unittest_base,unittest.TestCase):
00579     """
00580     Test grid map data
00581     """
00582     rawfile='testimage1chan.map.asap'
00583 
00584     def setUp(self):
00585         if os.path.exists(self.rawfile):
00586             shutil.rmtree(self.rawfile)
00587         shutil.copytree(self.datapath+self.rawfile, self.rawfile)
00588 
00589         default(sdgrid)
00590 
00591     def tearDown(self):
00592         if (os.path.exists(self.rawfile)):
00593             shutil.rmtree(self.rawfile)
00594         if (os.path.exists(self.outfile)):
00595             shutil.rmtree(self.outfile)
00596 
00597     def test500(self):
00598         """Test BOX gridding for map data"""
00599         npix=17
00600         res=sdgrid(infiles=self.rawfile,gridfunction='BOX',npix=npix,cell='20arcsec',outfile=self.outfile,plot=False)
00601         self.assertEqual(res,None,
00602                          msg='Any error occurred during gridding')
00603         self.getdata()
00604 
00605         # center is only nonzero pixel
00606         npol=2
00607         width=1
00608         nonzeropix_ref=self.generateNonzeroPix(npol,npix,width)
00609         nonzeropix=self.data.nonzero()[1]
00610         self.nonzero(nonzeropix_ref,nonzeropix)
00611 
00612         pol0=self.data[0,nonzeropix[0]]
00613         #self.check(0.625,pol0)
00614         #self.check(0.5,pol0)
00615         self.check(0.6666666667,pol0)
00616         
00617         pol1=self.data[0,nonzeropix[1]]
00618         #self.check(0.0625,pol1)
00619         #self.check(0.05,pol1)
00620         self.check(0.06666666667,pol1)
00621 
00622     def test501(self):
00623         """Test SF gridding for map data"""
00624         npix=17
00625         res=sdgrid(infiles=self.rawfile,gridfunction='SF',npix=npix,cell='20arcsec',outfile=self.outfile,plot=False)
00626         self.assertEqual(res,None,
00627                          msg='Any error occurred during gridding')
00628         self.getdata()
00629 
00630         # default width for SF is 3
00631         width=3
00632         npol=2
00633         nonzeropix=self.data.nonzero()[1]
00634         nonzeropix_ref=self.generateNonzeroPix(npol,npix,width)
00635         self.nonzero(nonzeropix_ref,nonzeropix)
00636 
00637         # check nonzero values
00638         refdata=[  1.54954410e-04,   1.54954414e-05,   4.63147834e-03,
00639                    4.63147851e-04,   9.89488605e-03,   9.89488559e-04,
00640                    4.63147834e-03,   4.63147851e-04,   1.54954410e-04,
00641                    1.54954414e-05,   4.63147834e-03,   4.63147851e-04,
00642                    3.81659232e-02,   3.81659227e-03,   6.86512142e-02,
00643                    6.86512096e-03,   3.81659232e-02,   3.81659227e-03,
00644                    4.63147834e-03,   4.63147851e-04,   9.89488605e-03,
00645                    9.89488559e-04,   6.86512142e-02,   6.86512096e-03,
00646                    1.19758800e-01,   1.19758807e-02,   6.86512142e-02,
00647                    6.86512096e-03,   9.89488605e-03,   9.89488559e-04,
00648                    4.63147834e-03,   4.63147851e-04,   3.81659232e-02,
00649                    3.81659227e-03,   6.86512142e-02,   6.86512096e-03,
00650                    3.81659232e-02,   3.81659227e-03,   4.63147834e-03,
00651                    4.63147851e-04,   1.54954410e-04,   1.54954414e-05,
00652                    4.63147834e-03,   4.63147851e-04,   9.89488605e-03,
00653                    9.89488559e-04,   4.63147834e-03,   4.63147851e-04,
00654                    1.54954410e-04,   1.54954414e-05]
00655         nonzerodata=numpy.take(self.data,nonzeropix,axis=1).squeeze()
00656         for i in xrange(len(nonzerodata)):
00657             self.check(refdata[i],nonzerodata[i])
00658 
00659     def test502(self):
00660         """Test GAUSS gridding for map data"""
00661         npix=17
00662         res=sdgrid(infiles=self.rawfile,gridfunction='GAUSS',npix=npix,cell='20arcsec',outfile=self.outfile,plot=False)
00663         self.assertEqual(res,None,
00664                          msg='Any error occurred during gridding')
00665         self.getdata()
00666         
00667         # default width for GAUSS is 4
00668         width=3
00669         npol=2
00670         nonzeropix=self.data.nonzero()[1]
00671         nonzeropix_ref = numpy.array([218, 219, 220, 221, 222, 223, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 284, 285, 286, 287, 288, 289, 290, 291, 292, 293, 318, 319, 320, 321, 322, 323, 324, 325, 326, 327, 354, 355, 356, 357, 358, 359])
00672         #nonzeropix_ref=self.generateNonzeroPix(npol,npix,width)
00673         self.nonzero(nonzeropix_ref,nonzeropix)
00674 
00675         refdata = [1.37290766e-03,   1.37290757e-04,   3.63217224e-03,
00676          3.63217230e-04,   1.37290766e-03,   1.37290757e-04,
00677          1.37290766e-03,   1.37290757e-04,   2.71596070e-02,
00678          2.71596084e-03,   7.29541257e-02,   7.29541294e-03,
00679          2.71596070e-02,   2.71596084e-03,   1.37290766e-03,
00680          1.37290757e-04,   3.63217224e-03,   3.63217230e-04,
00681          7.29541257e-02,   7.29541294e-03,   1.98309869e-01,
00682          1.98309869e-02,   7.29541257e-02,   7.29541294e-03,
00683          3.63217224e-03,   3.63217230e-04,   1.37290766e-03,
00684          1.37290757e-04,   2.71596070e-02,   2.71596084e-03,
00685          7.29541257e-02,   7.29541294e-03,   2.71596070e-02,
00686          2.71596084e-03,   1.37290766e-03,   1.37290757e-04,
00687          1.37290766e-03,   1.37290757e-04,   3.63217224e-03,
00688          3.63217230e-04,   1.37290766e-03,   1.37290757e-04]
00689         nonzerodata=numpy.take(self.data,nonzeropix,axis=1).squeeze()
00690         for i in xrange(len(nonzerodata)):
00691             self.check(refdata[i],nonzerodata[i])
00692 
00693     def test503(self):
00694         """Test GJINC gridding for map data"""
00695         npix=17
00696         res=sdgrid(infiles=self.rawfile,gridfunction='GJINC',npix=npix,cell='20arcsec',outfile=self.outfile,plot=False)
00697         self.assertEqual(res,None,
00698                          msg='Any error occurred during gridding')
00699         self.getdata()
00700         
00701         # default width for GAUSS is 4
00702         width=3
00703         npol=2
00704         nonzeropix=self.data.nonzero()[1]
00705         nonzeropix_ref = numpy.array([252, 253, 254, 255, 256, 257, 286, 287, 288, 289, 290, 291, 320, 321, 322, 323, 324, 325])
00706         #nonzeropix_ref=self.generateNonzeroPix(npol,npix,width)
00707         self.nonzero(nonzeropix_ref,nonzeropix)
00708 
00709         refdata = [0.0337296, 0.00337296, 0.0818698, 0.00818698, 0.0337296,
00710                    0.00337296, 0.0818698, 0.00818698, 0.16894495, 0.0168945,
00711                    0.0818698, 0.00818698, 0.0337296, 0.00337296, 0.0818698,
00712                    0.00818698, 0.0337296, 0.00337296]
00713         nonzerodata=numpy.take(self.data,nonzeropix,axis=1).squeeze()
00714         for i in xrange(len(nonzerodata)):
00715             self.check(refdata[i],nonzerodata[i])
00716 
00717 ###
00718 # Test DEC correction
00719 ###
00720 class sdgrid_dec_correction(sdgrid_unittest_base,unittest.TestCase):
00721     """
00722     Test DEC correction factor for horizontal (R.A.) auto grid setting.
00723     """
00724     rawfile='testimage1chan.1point.asap'
00725     
00726     def setUp(self):
00727         if os.path.exists(self.rawfile):
00728             shutil.rmtree(self.rawfile)
00729         shutil.copytree(self.datapath+self.rawfile, self.rawfile)
00730 
00731         default(sdgrid)
00732 
00733         # modification of input file along with this test
00734         #   - declination set to 60deg
00735         tb.open(self.rawfile,nomodify=False)
00736         dir=tb.getcol('DIRECTION')
00737         dir[1,:]=60.0*numpy.pi/180.0
00738         tb.putcol('DIRECTION',dir)
00739         tb.close()
00740 
00741     def tearDown(self):
00742         if (os.path.exists(self.rawfile)):
00743             shutil.rmtree(self.rawfile)
00744         if (os.path.exists(self.outfile)):
00745             shutil.rmtree(self.outfile)
00746 
00747     def test600(self):
00748         """Test 600: Test DEC correction factor"""
00749         npix=17
00750         res=sdgrid(infiles=self.rawfile,gridfunction='BOX',npix=npix,cell='20arcsec',outfile=self.outfile,plot=False)
00751         self.assertEqual(res,None,
00752                          msg='Any error occurred during gridding')
00753 
00754         # check horizontal and vertical grid
00755         tb.open(self.outfile)
00756         dir0=tb.getcell('DIRECTION',0)
00757         dir1=tb.getcell('DIRECTION',2)
00758         dir2=tb.getcell('DIRECTION',npix*2)
00759         dx=dir1[0]-dir0[0]
00760         dy=dir2[1]-dir0[1]
00761         #print 'dx=',dx,',dy=',dy
00762         diff=abs((0.5*dx-dy)/dy)
00763         self.assertTrue(diff<0.01,
00764                         msg='DEC correction is not correct.')
00765 
00766 
00767 ###
00768 # Test to change center for gridding
00769 ###
00770 class sdgrid_grid_center(sdgrid_unittest_base,unittest.TestCase):
00771     """
00772     Test to change center for gridding
00773     """
00774     rawfile='testimage1chan.1point.asap'
00775     
00776     def setUp(self):
00777         if os.path.exists(self.rawfile):
00778             shutil.rmtree(self.rawfile)
00779         shutil.copytree(self.datapath+self.rawfile, self.rawfile)
00780 
00781         default(sdgrid)
00782 
00783     def tearDown(self):
00784         if (os.path.exists(self.rawfile)):
00785             shutil.rmtree(self.rawfile)
00786         if (os.path.exists(self.outfile)):
00787             shutil.rmtree(self.outfile)
00788 
00789     def test700(self):
00790         """Test 700: Test to change center for gridding"""
00791         tb.open(self.rawfile)
00792         dir=tb.getcell('DIRECTION',0)
00793         tb.close()
00794 
00795         #shift center 3 pixels upward
00796         nshift=3
00797         pix=20.0
00798         dir[1]+=nshift*pix/3600.0*numpy.pi/180.0
00799 
00800         npix=17
00801         res=sdgrid(infiles=self.rawfile,gridfunction='BOX',npix=npix,cell='%sarcsec'%(pix),outfile=self.outfile,plot=False,center=dir)
00802         self.assertEqual(res,None,
00803                          msg='Any error occurred during gridding')
00804 
00805         self.getdata()
00806         
00807         # center is only nonzero pixel
00808         npol=2
00809         width=1
00810         nonzeropix_ref=self.generateNonzeroPix(npol,npix,width)
00811         #print nonzeropix_ref
00812         # shift 3 pixels downward
00813         nonzeropix_ref[0]-=nshift*npol*npix
00814         nonzeropix_ref[1]-=nshift*npol*npix
00815         nonzeropix=self.data.nonzero()[1]
00816         #print nonzeropix_ref
00817         #print nonzeropix
00818         self.nonzero(nonzeropix_ref,nonzeropix)
00819 
00820 
00821 def suite():
00822     return [sdgrid_failure_case, sdgrid_single_integ,
00823             sdgrid_clipping, sdgrid_flagging,
00824             sdgrid_weighting, sdgrid_map,
00825             sdgrid_dec_correction, sdgrid_grid_center]