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
00009 '''
00010 Unit tests for task smoothcal. It tests the following parameters:
00011 vis: wrong and correct values
00012 tablein: wrong and correct values
00013 caltable: existence of output
00014 field: wrong field value; non-default value
00015 smmothtype: unsupported value; non-default value
00016 smoothtime: unsupported value; non-default values
00017
00018 Other tests: check the values of column smoothed GAIN against reference.
00019 '''
00020 class smoothcal_test(unittest.TestCase):
00021
00022
00023 msfile = 'ngc1333_ut.ms'
00024 gcal = 'ngc1333_ut_nct.gcal'
00025 ref = 'ngc1333_ut_nct.ref'
00026 res = None
00027 out = 'smoothcal_test'
00028
00029 def setUp(self):
00030 self.res = None
00031 default(smoothcal)
00032 datapath = os.environ.get('CASAPATH').split()[0] + '/data/regression/unittest/smoothcal/'
00033 shutil.copytree(datapath+self.msfile, self.msfile)
00034 shutil.copytree(datapath+self.gcal, self.gcal)
00035 shutil.copytree(datapath+self.ref, self.ref)
00036
00037 def tearDown(self):
00038 if (os.path.exists(self.msfile)):
00039 os.system('rm -rf ' + self.msfile)
00040 if (os.path.exists(self.gcal)):
00041 os.system('rm -rf ' + self.gcal)
00042 if (os.path.exists(self.ref)):
00043 os.system('rm -rf ' + self.ref)
00044 if (os.path.exists(self.out)):
00045 os.system('rm -rf ' + self.out)
00046
00047 def getvarcol(self,table,colname):
00048 '''Return the requested column'''
00049 tb.open(table)
00050 col = tb.getvarcol(colname)
00051 tb.close()
00052 return col
00053
00054 def test0(self):
00055 '''Test 0: Default values'''
00056 self.res = smoothcal()
00057 self.assertFalse(self.res)
00058
00059 def test1(self):
00060 """Test 1: Wrong input MS should return False"""
00061 msfile = 'badmsfile'
00062 self.res = smoothcal(vis=msfile,tablein=self.gcal,caltable=self.out)
00063 self.assertFalse(self.res)
00064 self.assertFalse(os.path.exists(self.out))
00065
00066 def test2(self):
00067 """Test 2: Wrong input gcal should return False"""
00068 gcal = 'badgcal'
00069 self.res = smoothcal(vis=self.msfile,tablein=gcal,caltable=self.out)
00070 self.assertFalse(self.res)
00071 self.assertFalse(os.path.exists(self.out))
00072
00073 def test3(self):
00074 """Test 3: Good input should return None"""
00075 self.res = smoothcal(vis=self.msfile,tablein=self.gcal,caltable=self.out)
00076 self.assertEqual(self.res,None)
00077 self.assertTrue(os.path.exists(self.out))
00078
00079 def test4(self):
00080 """Test 4: Unsupported smoothtype"""
00081 self.res = smoothcal(vis=self.msfile,tablein=self.gcal,caltable=self.out,smoothtype='average')
00082 self.assertFalse(self.res)
00083 self.assertFalse(os.path.exists(self.out))
00084
00085 def test5(self):
00086 '''Test 5: Non-default smoothtype'''
00087 self.res = smoothcal(vis=self.msfile,tablein=self.gcal,caltable=self.out,smoothtype='mean')
00088 self.assertEqual(self.res,None)
00089 self.assertTrue(os.path.exists(self.out))
00090
00091 def test6(self):
00092 '''Test 6: Unsupported smoothtime'''
00093 self.res = smoothcal(vis=self.msfile,tablein=self.gcal,caltable=self.out,smoothtime=-1)
00094 self.assertFalse(os.path.exists(self.out))
00095
00096 def test7(self):
00097 '''Test 7: Non-default smoothtype and smoothtime'''
00098 self.res = smoothcal(vis=self.msfile,tablein=self.gcal,caltable=self.out,smoothtype='mean',
00099 smoothtime=7200)
00100 self.assertEqual(self.res,None)
00101 self.assertTrue(os.path.exists(self.out))
00102
00103 def test8(self):
00104 '''Unsupported field values'''
00105 self.res = smoothcal(vis=self.msfile,tablein=self.gcal,caltable=self.out,field='23~30')
00106 self.assertFalse(os.path.exists(self.out))
00107
00108 def test9(self):
00109 '''Test 9: Non-default field selection'''
00110 self.res = smoothcal(vis=self.msfile,tablein=self.gcal,caltable=self.out,smoothtype='median',
00111 smoothtime=5000.5,field='2')
00112
00113 self.assertTrue(os.path.exists(self.out))
00114
00115 def test10(self):
00116 '''Test10: Compare smoothed values with reference'''
00117 self.res=smoothcal(vis=self.msfile,tablein=self.gcal,caltable=self.out,smoothtype='mean',
00118 smoothtime=7200.)
00119 self.assertEqual(self.res,None)
00120 refcol = self.getvarcol(self.ref, 'CPARAM')
00121 smcol = self.getvarcol(self.out, 'CPARAM')
00122 nrows = len(refcol)
00123 EPS = 1e-5;
00124
00125 for i in range(1,nrows,1) :
00126 row = 'r%s'%i
00127
00128 for pol in range(0,2) :
00129 refdata = refcol[row][pol]
00130 smdata = smcol[row][pol]
00131 self.assertTrue(abs(refdata - smdata) < EPS)
00132
00133
00134
00135 def suite():
00136 return [smoothcal_test]
00137
00138
00139
00140
00141