casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
test_sdcal.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 # to rethrow exception 
00015 import inspect
00016 g = sys._getframe(len(inspect.stack())-1).f_globals
00017 g['__rethrow_casa_exceptions'] = True
00018 from sdcal_cli import sdcal_cli as sdcal
00019 #from sdcal import sdcal
00020 import asap as sd
00021 
00022 #
00023 # Unit test of sdcal task.
00024 # 
00025 
00026 ###
00027 # Base class for sdcal unit test
00028 ###
00029 class sdcal_unittest_base:
00030     """
00031     Base class for sdcal unit test
00032     """
00033     taskname='sdcal'
00034     datapath=os.environ.get('CASAPATH').split()[0] + '/data/regression/unittest/sdcal/'
00035     tolerance=1.0e-15
00036 
00037     def _checkfile( self, name ):
00038         isthere=os.path.exists(name)
00039         self.assertEqual(isthere,True,
00040                          msg='output file %s was not created because of the task failure'%(name))
00041         
00042 
00043     def _getspectra( self, name ):
00044         isthere=os.path.exists(name)
00045         self.assertEqual(isthere,True,
00046                          msg='file %s does not exist'%(name))        
00047         tb.open(name)
00048         sp=tb.getcol('SPECTRA').transpose()
00049         tb.close()
00050         return sp
00051 
00052     def _checkshape( self, sp, ref ):
00053         # check array dimension 
00054         self.assertEqual( sp.ndim, ref.ndim,
00055                           msg='array dimension differ' )
00056         # check number of spectra
00057         self.assertEqual( sp.shape[0], ref.shape[0],
00058                           msg='number of spectra differ' )
00059         # check number of channel
00060         self.assertEqual( sp.shape[1], ref.shape[1],
00061                           msg='number of channel differ' )
00062 
00063     def _diff(self, sp, ref):
00064         diff=abs((sp-ref)/ref)
00065         idx=numpy.argwhere(numpy.isnan(diff))
00066         #print idx
00067         if len(idx) > 0:
00068             diff[idx]=sp[idx]
00069         return diff
00070         
00071 
00072 ###
00073 # Base class for calibration test
00074 ###
00075 class sdcal_caltest_base(sdcal_unittest_base):
00076     """
00077     Base class for calibration test
00078     """
00079     reffile=''
00080     postfix='.cal.asap'
00081 
00082     def _comparecal( self, name ):
00083         self._checkfile(name)
00084         sp=self._getspectra(name)
00085         spref=self._getspectra(self.reffile)
00086 
00087         self._checkshape( sp, spref )
00088         
00089         for irow in xrange(sp.shape[0]):
00090             diff=self._diff(sp[irow],spref[irow])
00091             retval=numpy.all(diff<0.01)
00092             maxdiff=diff.max()
00093             self.assertEqual( retval, True,
00094                              msg='calibrated result is wrong (irow=%s): maxdiff=%s'%(irow,diff.max()) )
00095         del sp, spref
00096 
00097 ###
00098 # Base class for averaging test
00099 ###
00100 class sdcal_avetest_base(sdcal_unittest_base):
00101     """
00102     Base class for averaging test
00103     """
00104     postfix='ave.asap'
00105 
00106     def _split( self, s ):
00107         pos0=s.find('[')+1
00108         pos1=s.find(']')
00109         substr=s[pos0:pos1]
00110         return substr.split(',')
00111 
00112     def _getrefdata( self, name ):
00113         isthere=os.path.exists(name)
00114         self.assertEqual(isthere,True,
00115                          msg='file %s does not exist'%(name))
00116         f=open(name)
00117         lines=f.readlines()
00118         f.close
00119         # scanno
00120         scans=[]
00121         if re.match('^SCANNO:',lines[0]) is not None:
00122             ss=self._split(lines[0])
00123             for s in ss:
00124                 scans.append(string.atoi(s))
00125 
00126         # polno
00127         pols=[]
00128         if re.match('^POLNO:',lines[1]) is not None:
00129             ss=self._split(lines[1])
00130             for s in ss:
00131                 pols.append(string.atoi(s))
00132 
00133         # spectral data
00134         sp=[]
00135         for line in lines[2:]:
00136             tmp=[]
00137             ss=self._split(line)
00138             for s in ss:
00139                 tmp.append(string.atof(s))
00140             sp.append(tmp)
00141 
00142         # python list -> numpy array
00143         scans=numpy.unique(scans)
00144         pols=numpy.unique(pols)
00145         sp=numpy.array(sp)
00146 
00147         return (scans, pols, sp)
00148     
00149 
00150     def _compare( self, name1, name2 ):
00151         self._checkfile(name1)
00152         sp=self._getspectra( name1 )
00153         (scan0,pol0,sp0)=self._getrefdata( name2 )
00154 
00155         self._checkshape( sp, sp0 )
00156 
00157         s=sd.scantable(name1,False)
00158         scan=numpy.array(s.getscannos())
00159         pol=numpy.array(s.getpolnos())
00160         #print scan0, scan
00161         #print pol0, pol
00162         retval=len(pol)==len(pol0) and all(pol==pol0)
00163         self.assertEqual( retval, True,
00164                           msg='POLNO is wrong' )
00165         retval=len(scan)==len(scan0) and all(scan==scan0)
00166         self.assertEqual( retval, True,
00167                           msg='SCANNO is wrong' )
00168         del s
00169         
00170         for irow in xrange(sp.shape[0]):
00171             diff=self._diff(sp[irow],sp0[irow])
00172             retval=numpy.all(diff<0.01)
00173             self.assertEqual( retval, True,
00174                               msg='averaged result is wrong (irow=%s): maxdiff=%s'%(irow,diff.max()) )
00175         del sp, sp0
00176 
00177 ###
00178 # Base class for edgemarker testing
00179 ###
00180 class sdcal_edgemarker_base(sdcal_unittest_base):
00181     """
00182     Base class for edgemarker testing
00183     """
00184     def _readref( self, name ):
00185         f = open( name, 'r' )
00186         lines = f.readlines()
00187         f.close()
00188 
00189         ret = []
00190         nline = len(lines)
00191         for line in lines:
00192             s = line.split()
00193             ret.append( [float(s[0]),float(s[1])] )
00194         return numpy.array(ret).transpose()
00195     
00196     def _checkmarker( self, name, refdata ):
00197         # refdata shape is (2,noff)
00198         noff = refdata.shape[1]
00199         tb.open(name)
00200         tsel = tb.query('SRCTYPE==1')
00201         nrow = tsel.nrows()
00202         self.assertTrue( nrow > 0,
00203                          msg='data doesn\'t have OFF spectra' )
00204         dir = tsel.getcol('DIRECTION')
00205         tsel.close()
00206         tb.close()
00207         
00208         self.assertEqual( 2*noff, dir.shape[1], # refdata store only POLNO==0
00209                           msg='number of OFF differ: %s (should be %s)'%(dir.shape[1],2*noff) )
00210 
00211         for irow in xrange(2*noff):
00212             idx = int(irow) / 2
00213             diff = self._diff( dir[:,irow], refdata[:,idx] )
00214             self.assertEqual( numpy.all(diff<0.01), True,
00215                               msg='direction for OFF differ (irow=%s): [%s,%s] (should be [%s,%s])'%(irow,dir[0,irow],dir[1,irow],refdata[0,idx],refdata[1,idx]) )
00216 
00217 ###
00218 # Test on bad parameter settings
00219 ###
00220 class sdcal_test0(sdcal_unittest_base,unittest.TestCase):
00221     """
00222     Test on bad parameter setting
00223     """
00224     # Input and output names
00225     rawfile='calpsGBT.asap'
00226     prefix=sdcal_unittest_base.taskname+'Test0'
00227     outfile=prefix+'.asap'
00228 
00229     def setUp(self):
00230         self.res=None
00231         if (not os.path.exists(self.rawfile)):
00232             shutil.copytree(self.datapath+self.rawfile, self.rawfile)
00233 
00234         default(sdcal)
00235 
00236     def tearDown(self):
00237         if (os.path.exists(self.rawfile)):
00238             shutil.rmtree(self.rawfile)
00239         os.system( 'rm -rf '+self.prefix+'*' )
00240 
00241     def test000(self):
00242         """Test 000: Default parameters"""
00243         # argument verification error
00244         try:
00245             self.res=sdcal()
00246             #self.assertFalse(self.res)
00247             self.fail("The task must throw exception.")
00248         except Exception, e:
00249             pos=str(e).find("Parameter verification failed")
00250             self.assertNotEqual(pos,-1,
00251                                 msg='Unexpected exception was thrown: %s'%(str(e)))
00252         
00253     def test001(self):
00254         """Test 001: Time averaging without weight"""
00255         try:
00256             self.res=sdcal(infile=self.rawfile,timeaverage=True,outfile=self.outfile)
00257             self.assertTrue(False,
00258                             msg='The task must throw exception')
00259         except Exception, e:
00260             pos=str(e).find('Please specify weight type of time averaging')
00261             self.assertNotEqual(pos,-1,
00262                                 msg='Unexpected exception was thrown: %s'%(str(e)))
00263 
00264     def test002(self):
00265         """Test 002: Polarization averaging without weight"""
00266         try:
00267             self.res=sdcal(infile=self.rawfile,polaverage=True,outfile=self.outfile)
00268             self.assertTrue(False,
00269                             msg='The task must throw exception')
00270         except Exception, e:
00271             pos=str(e).find('Please specify weight type of polarization averaging')
00272             self.assertNotEqual(pos,-1,
00273                                 msg='Unexpected exception was thrown: %s'%(str(e)))
00274 
00275     def test003(self):
00276         """Test 003: Invalid calibration mode"""
00277         # argument verification error
00278         try:
00279             self.res=sdcal(infile=self.rawfile,calmode='invalid',outfile=self.outfile)
00280             #self.assertFalse(self.res)
00281             self.fail("The task must throw exception.")
00282         except Exception, e:
00283             pos=str(e).find("Parameter verification failed")
00284             self.assertNotEqual(pos,-1,
00285                                 msg='Unexpected exception was thrown: %s'%(str(e)))
00286 
00287     def test004(self):
00288         """Test 004: Specify existing output file name with overwrite=False"""
00289         outfile='calpsGBT.cal.asap'
00290         if (not os.path.exists(outfile)):
00291             shutil.copytree(self.datapath+outfile, outfile)
00292         try:
00293             self.res=sdcal(infile=self.rawfile,outfile=outfile,overwrite=False)
00294             self.assertTrue(False,
00295                             msg='The task must throw exception')
00296         except Exception, e:
00297             pos=str(e).find('Output file \'%s\' exists.'%(outfile))
00298             self.assertNotEqual(pos,-1,
00299                                 msg='Unexpected exception was thrown: %s'%(str(e)))
00300         finally:
00301             os.system( 'rm -rf %s'%outfile )        
00302 
00303 
00304 
00305 ###
00306 # Test GBT position switch calibration 
00307 ###
00308 class sdcal_test1(sdcal_caltest_base,unittest.TestCase):
00309     """
00310     Test GBT position switch calibration 
00311     
00312     Data is taken from OrionS_rawACSmod and created by the following
00313     script:
00314 
00315     asap_init()
00316     s=sd.scantable('OrionS_rawACSmod',average=False)
00317     sel=sd.selector()
00318     sel.set_ifs([0])
00319     sel.set_scans([20,21,22,23])
00320     s.set_selection(sel)
00321     s.save('calpsGBT.asap','ASAP')
00322 
00323     In addition, unnecessary TCAL rows were removed.
00324     """
00325     # Input and output names
00326     rawfile='calpsGBT.asap'
00327     reffile='calpsGBT.cal.asap'
00328     prefix=sdcal_unittest_base.taskname+'Test1'
00329     calmode='ps'
00330 
00331     def setUp(self):
00332         self.res=None
00333         if (not os.path.exists(self.rawfile)):
00334             shutil.copytree(self.datapath+self.rawfile, self.rawfile)
00335         if (not os.path.exists(self.reffile)):
00336             shutil.copytree(self.datapath+self.reffile, self.reffile)
00337 
00338         default(sdcal)
00339 
00340     def tearDown(self):
00341         if (os.path.exists(self.rawfile)):
00342             shutil.rmtree(self.rawfile)
00343         if (os.path.exists(self.reffile)):
00344             shutil.rmtree(self.reffile)
00345         os.system( 'rm -rf '+self.prefix+'*' )
00346 
00347     def test100(self):
00348         """Test 100: test to calibrate data (GBT position switch)"""
00349         outname=self.prefix+self.postfix
00350         self.res=sdcal(infile=self.rawfile,calmode=self.calmode,tau=0.09,outfile=outname,outform='ASAP')
00351         self.assertEqual(self.res,None,
00352                          msg='Any error occurred during calibration')
00353         self._comparecal(outname)
00354 
00355 ###
00356 # Test GBT nodding calibration 
00357 ###
00358 class sdcal_test2(sdcal_caltest_base,unittest.TestCase):
00359     """
00360     Test GBT nodding calibration 
00361     
00362     Data is taken from IRC+10216_rawACSmod and created by the following
00363     script:
00364 
00365     asap_init()
00366     s=sd.scantable('IRC+10216_rawACSmod',average=False)
00367     sel=sd.selector()
00368     sel.set_scans([229,230])
00369     sel.set_ifs([3])
00370     sel.set_cycles([0,1])
00371     s.set_selection(sel)
00372     s.save('calnodGBT.asap','ASAP')
00373 
00374     In addition, unnecessary TCAL rows were removed.
00375     """
00376     # Input and output names
00377     rawfile='calnodGBT.asap'
00378     reffile='calnodGBT.cal.asap'
00379     prefix=sdcal_unittest_base.taskname+'Test2'
00380     calmode='nod'
00381 
00382     def setUp(self):
00383         self.res=None
00384         if (not os.path.exists(self.rawfile)):
00385             shutil.copytree(self.datapath+self.rawfile, self.rawfile)
00386         if (not os.path.exists(self.reffile)):
00387             shutil.copytree(self.datapath+self.reffile, self.reffile)
00388 
00389         default(sdcal)
00390 
00391     def tearDown(self):
00392         if (os.path.exists(self.rawfile)):
00393             shutil.rmtree(self.rawfile)
00394         if (os.path.exists(self.reffile)):
00395             shutil.rmtree(self.reffile)
00396         os.system( 'rm -rf '+self.prefix+'*' )
00397 
00398     def test200(self):
00399         """Test 200: test to calibrate data (GBT nod)"""
00400         outname=self.prefix+self.postfix
00401         self.res=sdcal(infile=self.rawfile,calmode=self.calmode,tau=0.09,outfile=outname,outform='ASAP')
00402         self.assertEqual(self.res,None,
00403                          msg='Any error occurred during calibration')
00404         self._comparecal(outname)
00405 
00406 ###
00407 # Test GBT frequency switch calibration 
00408 ###
00409 class sdcal_test3(sdcal_caltest_base,unittest.TestCase):
00410     """
00411     Test GBT frequency switch calibration 
00412     
00413     Data is taken from FLS3_all_newcal_SP and created by the following
00414     script:
00415 
00416     asap_init()
00417     sd.rc('scantable',storage='disk')
00418     s=sd.scantable('FLS3_all_newcal_SP',average=False)
00419     sel=sd.selector()
00420     sel.set_scans([14914])
00421     sel.set_cycles([0,1])
00422     s.set_selection(sel)
00423     s.save('calfsGBT.asap','ASAP')
00424 
00425     In addition, unnecessary TCAL rows were removed.
00426     """
00427     # Input and output names
00428     rawfile='calfsGBT.asap'
00429     reffile='calfsGBT.cal.asap'
00430     prefix=sdcal_unittest_base.taskname+'Test3'
00431     calmode='fs'
00432 
00433     def setUp(self):
00434         self.res=None
00435         if (not os.path.exists(self.rawfile)):
00436             shutil.copytree(self.datapath+self.rawfile, self.rawfile)
00437         if (not os.path.exists(self.reffile)):
00438             shutil.copytree(self.datapath+self.reffile, self.reffile)
00439 
00440         default(sdcal)
00441 
00442     def tearDown(self):
00443         if (os.path.exists(self.rawfile)):
00444             shutil.rmtree(self.rawfile)
00445         if (os.path.exists(self.reffile)):
00446             shutil.rmtree(self.reffile)
00447         os.system( 'rm -rf '+self.prefix+'*' )
00448 
00449     def test300(self):
00450         """Test 300: test to calibrate data (GBT frequency switch)"""
00451         outname=self.prefix+self.postfix
00452         self.res=sdcal(infile=self.rawfile,calmode=self.calmode,outfile=outname,outform='ASAP')
00453         self.assertEqual(self.res,None,
00454                          msg='Any error occurred during calibration')
00455         self._comparecal(outname)
00456         
00457 ###
00458 # Test quotient
00459 ###
00460 class sdcal_test4(sdcal_caltest_base,unittest.TestCase):
00461     """
00462     Test quotient.
00463     
00464     Data is taken from MOPS.rpf, which is included in ASAP package
00465     for testing, and created by the following script:
00466 
00467     asap_init()
00468     s=sd.scantable('MOPS.rpf',average=False)
00469     sel=sd.selector()
00470     sel.set_cycles([0,1])
00471     s.set_selection(sel)
00472     s.save('quotient.asap','ASAP')
00473 
00474     """
00475     # Input and output names
00476     rawfile='quotient.asap'
00477     reffile='quotient.cal.asap'
00478     prefix=sdcal_unittest_base.taskname+'Test4'
00479     calmode='quotient'
00480 
00481     def setUp(self):
00482         self.res=None
00483         if (not os.path.exists(self.rawfile)):
00484             shutil.copytree(self.datapath+self.rawfile, self.rawfile)
00485         if (not os.path.exists(self.reffile)):
00486             shutil.copytree(self.datapath+self.reffile, self.reffile)
00487 
00488         default(sdcal)
00489 
00490     def tearDown(self):
00491         if (os.path.exists(self.rawfile)):
00492             shutil.rmtree(self.rawfile)
00493         if (os.path.exists(self.reffile)):
00494             shutil.rmtree(self.reffile)
00495         os.system( 'rm -rf '+self.prefix+'*' )
00496 
00497     def test400(self):
00498         """Test 400: test to calibrate data (quotient)"""
00499         outname=self.prefix+self.postfix
00500         self.res=sdcal(infile=self.rawfile,calmode=self.calmode,outfile=outname,outform='ASAP')
00501         self.assertEqual(self.res,None,
00502                          msg='Any error occurred during calibration')
00503         self._comparecal(outname)
00504 
00505 ###
00506 # Test ALMA position switch calibration
00507 ###
00508 class sdcal_test5(sdcal_caltest_base,unittest.TestCase):
00509     """
00510     Test ALMA position switch calibration (OTF raster with OFF scan)
00511     
00512     Data is taken from uid___A002_X8ae1b_X1 (DV01) and created by 
00513     the following script:
00514 
00515     asap_init()
00516     sd.splitant('uid___A002_X8ae1b_X1') # to split data by antenna
00517     s=sd.scantable('uid___A002_X8ae1b.DV01.asap',average=False)
00518     sel=sd.selector()
00519     sel.set_ifs([2])
00520     sel.set_cycles([20,118,205])
00521     s.set_selection(sel)
00522     s.save('calpsALMA.asap','ASAP')
00523 
00524     """
00525     # Input and output names
00526     rawfile='calpsALMA.asap'
00527     reffile='calpsALMA.cal.asap'
00528     prefix=sdcal_unittest_base.taskname+'Test5'
00529     calmode='ps'
00530 
00531     def setUp(self):
00532         self.res=None
00533         if (not os.path.exists(self.rawfile)):
00534             shutil.copytree(self.datapath+self.rawfile, self.rawfile)
00535         if (not os.path.exists(self.reffile)):
00536             shutil.copytree(self.datapath+self.reffile, self.reffile)
00537 
00538         default(sdcal)
00539 
00540     def tearDown(self):
00541         if (os.path.exists(self.rawfile)):
00542             shutil.rmtree(self.rawfile)
00543         if (os.path.exists(self.reffile)):
00544             shutil.rmtree(self.reffile)
00545         os.system( 'rm -rf '+self.prefix+'*' )
00546 
00547     def test500(self):
00548         """Test 500: test to calibrate data (ALMA position switch)"""
00549         outname=self.prefix+self.postfix
00550         self.res=sdcal(infile=self.rawfile,calmode=self.calmode,outfile=outname,outform='ASAP')
00551         self.assertEqual(self.res,None,
00552                          msg='Any error occurred during calibration')
00553         self._comparecal(outname)
00554 
00555 
00556 ###
00557 # Test polarization averaging with/without scan average
00558 ###
00559 class sdcal_test6(sdcal_avetest_base,unittest.TestCase):
00560     """
00561     Test polarization averaging with/without scan average
00562 
00563     Data is calpsGBT.cal.asap.
00564 
00565     ###NOTE###
00566     Polarization average averages data in both time and polarization.
00567     """
00568     # Input and output names
00569     rawfile='calpsGBT.cal.asap'
00570     prefix=sdcal_unittest_base.taskname+'Test6'
00571     reffiles=['polaverage.ref0',
00572               'polaverage.ref1',
00573               'polaverage.ref2',
00574               'polaverage.ref3']
00575     
00576     def setUp(self):
00577         self.res=None
00578         if (not os.path.exists(self.rawfile)):
00579             shutil.copytree(self.datapath+self.rawfile, self.rawfile)
00580         for reffile in self.reffiles:
00581             if (not os.path.exists(reffile)):
00582                 shutil.copyfile(self.datapath+reffile, reffile)
00583 
00584         default(sdcal)
00585 
00586     def tearDown(self):
00587         if (os.path.exists(self.rawfile)):
00588             shutil.rmtree(self.rawfile)
00589         for reffile in self.reffiles:
00590             if (os.path.exists(reffile)):
00591                 os.remove(reffile)
00592         os.system( 'rm -rf '+self.prefix+'*' )
00593 
00594     def test600(self):
00595         """Test 600: test polarization average with pweight='var' (scan average)"""
00596         outname=self.prefix+self.postfix
00597         self.res=sdcal(infile=self.rawfile,scanaverage=True,polaverage=True,pweight='var',outfile=outname,outform='ASAP')
00598         self.assertEqual(self.res,None,
00599                          msg='Any error occurred during polarization averaging')
00600         self._compare(outname,self.reffiles[0])
00601         
00602     def test601(self):
00603         """Test 601: test polarization average with pweight='var' (no scan average)"""
00604         outname=self.prefix+self.postfix
00605         self.res=sdcal(infile=self.rawfile,scanaverage=False,polaverage=True,pweight='var',outfile=outname,outform='ASAP')
00606         self.assertEqual(self.res,None,
00607                          msg='Any error occurred during polarization averaging')
00608         self._compare(outname,self.reffiles[1])
00609 
00610     def test602(self):
00611         """Test 602: test polarization average with pweight='tsys' (scan average)"""
00612         outname=self.prefix+self.postfix
00613         self.res=sdcal(infile=self.rawfile,scanaverage=True,polaverage=True,pweight='tsys',outfile=outname,outform='ASAP')
00614         self.assertEqual(self.res,None,
00615                          msg='Any error occurred during polarization averaging')
00616         self._compare(outname,self.reffiles[2])
00617         
00618     def test603(self):
00619         """Test 603: test polarization average with pweight='tsys' (no scan average)"""
00620         outname=self.prefix+self.postfix
00621         self.res=sdcal(infile=self.rawfile,scanaverage=False,polaverage=True,pweight='tsys',outfile=outname,outform='ASAP')
00622         self.assertEqual(self.res,None,
00623                          msg='Any error occurred during polarization averaging')
00624         self._compare(outname,self.reffiles[3])
00625 
00626 
00627 ###
00628 # Test time averaging with/without scan average
00629 ###
00630 class sdcal_test7(sdcal_avetest_base,unittest.TestCase):
00631     """
00632     Test time averaging with/without scan average
00633 
00634     Data is calpsGBT.cal.asap.
00635     """
00636     # Input and output names
00637     rawfile='calpsGBT.cal.asap'
00638     prefix=sdcal_unittest_base.taskname+'Test7'
00639     reffiles=['timeaverage.ref0',
00640               'timeaverage.ref1',
00641               'timeaverage.ref2',
00642               'timeaverage.ref3',
00643               'timeaverage.ref4',
00644               'timeaverage.ref5',
00645               'timeaverage.ref6',
00646               'timeaverage.ref7',
00647               'timeaverage.ref8',
00648               'timeaverage.ref9']
00649 
00650     def setUp(self):
00651         self.res=None
00652         if (not os.path.exists(self.rawfile)):
00653             shutil.copytree(self.datapath+self.rawfile, self.rawfile)
00654         for reffile in self.reffiles:
00655             if (not os.path.exists(reffile)):
00656                 shutil.copyfile(self.datapath+reffile, reffile)
00657 
00658         default(sdcal)
00659 
00660     def tearDown(self):
00661         if (os.path.exists(self.rawfile)):
00662             shutil.rmtree(self.rawfile)
00663         for reffile in self.reffiles:
00664             if (os.path.exists(reffile)):
00665                 os.remove(reffile)
00666         os.system( 'rm -rf '+self.prefix+'*' )
00667 
00668     def test700(self):
00669         """Test 700: test time average with tweight='var' (scan average)"""
00670         outname=self.prefix+self.postfix
00671         self.res=sdcal(infile=self.rawfile,scanaverage=True,timeaverage=True,tweight='var',outfile=outname,outform='ASAP')
00672         self.assertEqual(self.res,None,
00673                          msg='Any error occurred during time averaging')
00674         self._compare(outname,self.reffiles[0])
00675         
00676     def test701(self):
00677         """Test 701: test time average with tweight='var' (no scan average)"""
00678         outname=self.prefix+self.postfix
00679         self.res=sdcal(infile=self.rawfile,scanaverage=False,timeaverage=True,tweight='var',outfile=outname,outform='ASAP')
00680         self.assertEqual(self.res,None,
00681                          msg='Any error occurred during time averaging')
00682         self._compare(outname,self.reffiles[1])
00683         
00684     def test702(self):
00685         """Test 702: test time average with tweight='tsys' (scan average)"""
00686         outname=self.prefix+self.postfix
00687         self.res=sdcal(infile=self.rawfile,scanaverage=True,timeaverage=True,tweight='tsys',outfile=outname,outform='ASAP')
00688         self.assertEqual(self.res,None,
00689                          msg='Any error occurred during time averaging')
00690         self._compare(outname,self.reffiles[2])
00691         
00692     def test703(self):
00693         """Test 703: test time average with tweight='tsys' (no scan average)"""
00694         outname=self.prefix+self.postfix
00695         self.res=sdcal(infile=self.rawfile,scanaverage=False,timeaverage=True,tweight='tsys',outfile=outname,outform='ASAP')
00696         self.assertEqual(self.res,None,
00697                          msg='Any error occurred during time averaging')
00698         self._compare(outname,self.reffiles[3])
00699         
00700     def test704(self):
00701         """Test 704: test time average with tweight='tint' (scan average)"""
00702         outname=self.prefix+self.postfix
00703         self.res=sdcal(infile=self.rawfile,scanaverage=True,timeaverage=True,tweight='tint',outfile=outname,outform='ASAP')
00704         self.assertEqual(self.res,None,
00705                          msg='Any error occurred during time averaging')
00706         self._compare(outname,self.reffiles[4])
00707         
00708     def test705(self):
00709         """Test 705: test time average with tweight='tint' (no scan average)"""
00710         outname=self.prefix+self.postfix
00711         self.res=sdcal(infile=self.rawfile,scanaverage=False,timeaverage=True,tweight='tint',outfile=outname,outform='ASAP')
00712         self.assertEqual(self.res,None,
00713                          msg='Any error occurred during time averaging')
00714         self._compare(outname,self.reffiles[5])
00715         
00716     def test706(self):
00717         """Test 706: test time average with tweight='tintsys' (scan average)"""
00718         outname=self.prefix+self.postfix
00719         self.res=sdcal(infile=self.rawfile,scanaverage=True,timeaverage=True,tweight='tintsys',outfile=outname,outform='ASAP')
00720         self.assertEqual(self.res,None,
00721                          msg='Any error occurred during time averaging')
00722         self._compare(outname,self.reffiles[6])
00723         
00724     def test707(self):
00725         """Test 707: test time average with tweight='tintsys' (no scan average)"""
00726         outname=self.prefix+self.postfix
00727         self.res=sdcal(infile=self.rawfile,scanaverage=False,timeaverage=True,tweight='tintsys',outfile=outname,outform='ASAP')
00728         self.assertEqual(self.res,None,
00729                          msg='Any error occurred during time averaging')
00730         self._compare(outname,self.reffiles[7])
00731         
00732     def test708(self):
00733         """Test 708: test time average with tweight='median' (scan average)"""
00734         outname=self.prefix+self.postfix
00735         self.res=sdcal(infile=self.rawfile,scanaverage=True,timeaverage=True,tweight='median',outfile=outname,outform='ASAP')
00736         self.assertEqual(self.res,None,
00737                          msg='Any error occurred during time averaging')
00738         self._compare(outname,self.reffiles[8])
00739         
00740     def test709(self):
00741         """Test 709: test time average with tweight='median' (no scan average)"""
00742         outname=self.prefix+self.postfix
00743         self.res=sdcal(infile=self.rawfile,scanaverage=False,timeaverage=True,tweight='median',outfile=outname,outform='ASAP')
00744         self.assertEqual(self.res,None,
00745                          msg='Any error occurred during time averaging')
00746         self._compare(outname,self.reffiles[9])
00747         
00748 
00749 
00750 ###
00751 # Test mixed operation (cal+average,time+pol average,...)
00752 ###
00753 class sdcal_test8(sdcal_avetest_base,unittest.TestCase):
00754     """
00755     Test other things including mixed opearation
00756 
00757        - calibration + time average
00758        - calibration + polarization average
00759        - time average + polarzation average
00760        - channelrange parameter
00761        
00762     """
00763     # Input and output names
00764     rawfile='calpsGBT.asap'
00765     calfile='calpsGBT.cal.asap'
00766     prefix=sdcal_unittest_base.taskname+'Test8'
00767     reffiles=['polaverage.ref2',
00768               'timeaverage.ref6',
00769               'timepolaverage.ref']
00770 
00771     def setUp(self):
00772         self.res=None
00773         if (not os.path.exists(self.rawfile)):
00774             shutil.copytree(self.datapath+self.rawfile, self.rawfile)
00775         if (not os.path.exists(self.calfile)):
00776             shutil.copytree(self.datapath+self.calfile, self.calfile)
00777         for reffile in self.reffiles:
00778             if (not os.path.exists(reffile)):
00779                 shutil.copyfile(self.datapath+reffile, reffile)
00780 
00781         default(sdcal)
00782 
00783     def tearDown(self):
00784         if (os.path.exists(self.rawfile)):
00785             shutil.rmtree(self.rawfile)
00786         if (os.path.exists(self.calfile)):
00787             shutil.rmtree(self.calfile)
00788         for reffile in self.reffiles:
00789             if (os.path.exists(reffile)):
00790                 os.remove(reffile)
00791         os.system( 'rm -rf '+self.prefix+'*' )
00792 
00793     def test800(self):
00794         """Test 800: test calibration + polarization average with pweight='tsys' (scan average)"""
00795         outname=self.prefix+self.postfix
00796         self.res=sdcal(infile=self.rawfile,calmode='ps',scanaverage=True,polaverage=True,pweight='tsys',tau=0.09,outfile=outname,outform='ASAP')
00797         self.assertEqual(self.res,None,
00798                          msg='Any error occurred during calibration and averaging')
00799         self._compare(outname,self.reffiles[0])
00800         
00801     def test801(self):
00802         """Test 801: test calibration + time average with tweight='tintsys' (scan average)"""
00803         outname=self.prefix+self.postfix
00804         self.res=sdcal(infile=self.rawfile,calmode='ps',scanaverage=True,timeaverage=True,tweight='tintsys',tau=0.09,outfile=outname,outform='ASAP')
00805         self.assertEqual(self.res,None,
00806                          msg='Any error occurred during calibration and averaging')
00807         self._compare(outname,self.reffiles[1])
00808         
00809     def test802(self):
00810         """Test 802: test polarization average with pweight='tsys' + time average with tweight='tintsys' (no scan average)"""
00811         outname=self.prefix+self.postfix
00812         self.res=sdcal(infile=self.calfile,scanaverage=False,timeaverage=True,tweight='tintsys',polaverage=True,pweight='tsys',outfile=outname,outform='ASAP')
00813         self.assertEqual(self.res,None,
00814                          msg='Any error occurred during averaging')
00815         self._compare(outname,self.reffiles[2])
00816         
00817     def test803(self):
00818         """Test 803: test channelrange selection"""
00819         chrange=[1000,8000]
00820         outname=self.prefix+self.postfix
00821         self.res=sdcal(infile=self.calfile,channelrange=chrange,outfile=outname,outform='ASAP')
00822         self.assertEqual(self.res,None,
00823                          msg='Any error occurred during channel range selection')
00824 
00825         s0=sd.scantable(self.calfile,False)
00826         s1=sd.scantable(outname,False)
00827 
00828         # check spectral value
00829         sp0=numpy.array(s0._getspectrum(0))
00830         sp1=numpy.array(s1._getspectrum(0))
00831         self.res=all(sp0[chrange[0]:chrange[1]+1]==sp1)
00832         self.assertEqual(self.res,True,
00833                          msg='spectral data do not match')
00834 
00835         # check frequency
00836         s0.set_unit('GHz')
00837         s1.set_unit('GHz')
00838         abc0=numpy.array(s0._getabcissa(0))
00839         abc1=numpy.array(s1._getabcissa(0))
00840         self.res=numpy.allclose(abc0[chrange[0]:chrange[1]+1],abc1)
00841         self.assertEqual(self.res,True,
00842                          msg='frequencies do not match')
00843 
00844         del s0,s1
00845 
00846 ###
00847 # Test averageall parameter
00848 ###
00849 class sdcal_test9(sdcal_avetest_base,unittest.TestCase):
00850     """
00851     Test averageall parameter that forces to average spectra with
00852     different spectral resolution.
00853 
00854     Here, test to average the following two spectra that have same
00855     frequency in band center.
00856 
00857        - nchan = 8192, resolution = 6104.23 Hz
00858        - nchan = 8192, resolution = 12208.5 Hz
00859        
00860     """
00861     # Input and output names
00862     rawfile='averageall.asap'
00863     prefix=sdcal_unittest_base.taskname+'Test9'
00864     reffiles=['averageall.ref']
00865 
00866     def setUp(self):
00867         self.res=None
00868         if (not os.path.exists(self.rawfile)):
00869             shutil.copytree(self.datapath+self.rawfile, self.rawfile)
00870         for reffile in self.reffiles:
00871             if (not os.path.exists(reffile)):
00872                 shutil.copyfile(self.datapath+reffile, reffile)
00873 
00874         default(sdcal)
00875 
00876     def tearDown(self):
00877         if (os.path.exists(self.rawfile)):
00878             shutil.rmtree(self.rawfile)
00879         for reffile in self.reffiles:
00880             if (os.path.exists(reffile)):
00881                 os.remove(reffile)
00882         os.system( 'rm -rf '+self.prefix+'*' )
00883 
00884     def test900(self):
00885         """Test 900: test averageall parameter"""
00886         outname=self.prefix+self.postfix
00887         self.res=sdcal(infile=self.rawfile,scanaverage=False,timeaverage=True,tweight='tintsys',averageall=True,polaverage=False,outfile=outname,outform='ASAP')
00888         self.assertEqual(self.res,None,
00889                          msg='Any error occurred during averaging')
00890         self._compare(outname,self.reffiles[0])
00891         self._checkFrequencies(outname)
00892 
00893     def _checkFrequencies(self,outfile):
00894         tb.open(self.rawfile.rstrip('/')+'/FREQUENCIES')
00895         rpin=tb.getcol('REFPIX')
00896         rvin=tb.getcol('REFVAL')
00897         icin=tb.getcol('INCREMENT')
00898         tb.close
00899         tb.open(outfile.rstrip('/')+'/FREQUENCIES')
00900         rpout=tb.getcol('REFPIX')
00901         rvout=tb.getcol('REFVAL')
00902         icout=tb.getcol('INCREMENT')
00903         tb.close
00904         ic=[icin[2:4].max(),icin[0:2].max()]
00905         ledge=[rvin[2]-ic[0]*(rpin[2]+0.5),
00906                rvin[0]-ic[1]*(rpin[0]+0.5)]
00907         redge=[rvin[2]+ic[0]*(8191-rpin[2]+0.5),
00908                rvin[0]+ic[1]*(8191-rpin[0]+0.5)]
00909         eps = 1.0e-15
00910         for i in xrange(2):
00911             self.assertEqual(ic[i],icout[i],
00912                              msg='INCREMENT for FREQ_ID=%s differ'%(i))
00913             fmin=rvout[i]-icout[i]*(rpout[i]+0.5)
00914             self.assertTrue(abs((fmin-ledge[i])/ledge[i]) < eps,
00915                              msg='Left frequency edge for FREQ_ID=%s does not match'%(i))
00916             fmax=rvout[i]+icout[i]*(8191-rpout[i]+0.5)
00917             self.assertTrue(abs((fmax-redge[i])/redge[i]) < eps,
00918                              msg='Right frequency edge for FREQ_ID=%s does not match'%(i))
00919 
00920 ###
00921 # Test edgemarker
00922 ###
00923 class sdcal_test_edgemarker_generic(sdcal_edgemarker_base,unittest.TestCase):
00924     """
00925     Test edgemarker function that is available for calmode='otf'. 
00926 
00927     Here, data will not be calibrated and only edge marking process
00928     will be executed. 
00929     """
00930     # Input and output names
00931     rawfile='lissajous.asap'
00932     prefix=sdcal_unittest_base.taskname+'TestEdgeMarkerGeneric'
00933     reffiles = [ 'marker.otf.default.ref',
00934                  'marker.otf.custom.ref' ]
00935 
00936     def setUp(self):
00937         self.res=None
00938         if (not os.path.exists(self.rawfile)):
00939             shutil.copytree(self.datapath+self.rawfile, self.rawfile)
00940         for reffile in self.reffiles:
00941             if (not os.path.exists(reffile)):
00942                 shutil.copyfile(self.datapath+reffile, reffile)
00943         default(sdcal)
00944 
00945     def tearDown(self):
00946         if (os.path.exists(self.rawfile)):
00947             shutil.rmtree(self.rawfile)
00948         for reffile in self.reffiles:
00949             if (os.path.exists(reffile)):
00950                 os.remove(reffile)
00951         os.system( 'rm -rf '+self.prefix+'*' )
00952 
00953     def testEdgeMarkerGeneric0(self):
00954         """
00955         Test default setting for edgemarker
00956         """
00957         outname = self.prefix+'.asap'
00958         self.res = sdcal(infile=self.rawfile,calmode='otf',markonly=True,scanaverage=False,timeaverage=False,polaverage=False,outfile=outname,outform='ASAP')
00959         refdir = self._readref( self.reffiles[0] )
00960         self._checkfile( outname ) 
00961         self._checkmarker( outname, refdir )
00962 
00963     def testEdgeMarkerGeneric1(self):
00964         """
00965         Test customized edge marking
00966         """
00967         outname = self.prefix+'.asap'
00968         self.res = sdcal(infile=self.rawfile,calmode='otf',fraction='3%',markonly=True,scanaverage=False,timeaverage=False,polaverage=False,outfile=outname,outform='ASAP')
00969         refdir = self._readref( self.reffiles[1] )
00970         self._checkfile( outname ) 
00971         self._checkmarker( outname, refdir )
00972 
00973 
00974 class sdcal_test_edgemarker_raster(sdcal_edgemarker_base,unittest.TestCase):
00975     """
00976     Test edgemarker function that is available for calmode='otfraster'. 
00977 
00978     Here, data will not be calibrated and only edge marking process
00979     will be executed. 
00980     """
00981     # Input and output names
00982     rawfile='raster.asap'
00983     prefix=sdcal_unittest_base.taskname+'TestEdgeMarkerRaster'
00984     reffiles=[ 'marker.raster.default.ref',
00985                'marker.raster.custom.ref' ]
00986 
00987     def setUp(self):
00988         self.res=None
00989         if (not os.path.exists(self.rawfile)):
00990             shutil.copytree(self.datapath+self.rawfile, self.rawfile)
00991         for reffile in self.reffiles:
00992             if (not os.path.exists(reffile)):
00993                 shutil.copyfile(self.datapath+reffile, reffile)
00994         default(sdcal)
00995 
00996     def tearDown(self):
00997         if (os.path.exists(self.rawfile)):
00998             shutil.rmtree(self.rawfile)
00999         for reffile in self.reffiles:
01000             if (os.path.exists(reffile)):
01001                 os.remove(reffile)
01002         os.system( 'rm -rf '+self.prefix+'*' )
01003 
01004     def testEdgeMarkerRaster0(self):
01005         """
01006         Test default setting for edgemarker
01007         """
01008         outname = self.prefix+'.asap'
01009         self.res = sdcal(infile=self.rawfile,calmode='otfraster',markonly=True,scanaverage=False,timeaverage=False,polaverage=False,outfile=outname,outform='ASAP')
01010         refdir = self._readref( self.reffiles[0] )
01011         self._checkfile( outname ) 
01012         self._checkmarker( outname, refdir )
01013 
01014     def testEdgeMarkerRaster1(self):
01015         """
01016         Test default setting for edgemarker
01017         """
01018         outname = self.prefix+'.asap'
01019         self.res = sdcal(infile=self.rawfile,calmode='otfraster',noff=1,markonly=True,scanaverage=False,timeaverage=False,polaverage=False,outfile=outname,outform='ASAP')
01020         refdir = self._readref( self.reffiles[1] )
01021         self._checkfile( outname ) 
01022         self._checkmarker( outname, refdir )
01023 
01024 def suite():
01025     return [sdcal_test0, sdcal_test1,
01026             sdcal_test2, sdcal_test3,
01027             sdcal_test4, sdcal_test5,
01028             sdcal_test6, sdcal_test7,
01029             sdcal_test8, sdcal_test9,
01030             sdcal_test_edgemarker_generic,
01031             sdcal_test_edgemarker_raster]