00001 import os
00002 import sys
00003 import shutil
00004 import string
00005 import listing as lt
00006 from __main__ import default
00007 from tasks import *
00008 from taskinit import *
00009 import unittest
00010
00011 '''
00012 Unit tests for task listobs. It tests the following parameters:
00013 vis: wrong and correct values
00014 selectdata: several data selection parameters
00015 verbose true or false
00016 listfile: save on a file
00017
00018 '''
00019
00020 datapath = os.environ.get('CASAPATH').split()[0] + '/data/regression/unittest/listobs/'
00021
00022
00023 testmms = False
00024 if os.environ.has_key('TEST_DATADIR'):
00025 DATADIR = str(os.environ.get('TEST_DATADIR'))+'/listobs/'
00026 if os.path.isdir(DATADIR):
00027 testmms = True
00028 datapath = DATADIR
00029 else:
00030 print 'WARN: directory '+DATADIR+' does not exist'
00031
00032 print 'listobs tests will use data from '+datapath
00033
00034
00035 reffile = datapath+'reflistobs'
00036
00037
00038 msfile1 = 'ngc5921_ut.ms'
00039 msfile2 = 'uid___X02_X3d737_X1_01_small.ms'
00040
00041 class listobs_test1(unittest.TestCase):
00042
00043 def setUp(self):
00044 self.res = None
00045 if (not os.path.lexists(msfile1)):
00046 shutil.copytree(datapath+msfile1, msfile1, symlinks=True)
00047
00048 if (not os.path.lexists(msfile2)):
00049 shutil.copytree(datapath+msfile2, msfile2, symlinks=True)
00050
00051 default(listobs)
00052
00053
00054 def test1(self):
00055 '''Listobs 1: Input MS'''
00056 self.res = listobs(vis=msfile1)
00057 self.assertEqual(self.res, None, "Return value should be None")
00058
00059 def test2(self):
00060 '''Listobs 2: CSV-591. Check if long field names are fully displayed'''
00061 ms.open(msfile1)
00062 res = ms.summary(True)
00063 ms.close()
00064 name = res['field_0']['name']
00065 self.assertFalse(name.__contains__('*'), "Field name contains a *")
00066 name = res['scan_7']['0']['FieldName']
00067 self.assertFalse(name.__contains__('*'), "Field name contains a *")
00068
00069 def test3(self):
00070 '''Listobs 3: CAS-2751. Check that ALMA MS displays one row per scan'''
00071 ms.open(msfile2)
00072 res = ms.summary(True)
00073 ms.close()
00074
00075 btime = res['scan_1']['0']['BeginTime']
00076 etime = res['scan_1']['0']['EndTime']
00077 self.assertNotEqual(btime, etime, "Begin and End times of scan=1 should not be equal")
00078
00079
00080 output = 'listobs4.txt'
00081 out = "newobs4.txt"
00082 reference = reffile+'4'
00083 diff = "difflistobs4"
00084
00085 listobs(vis=msfile2, verbose=True, listfile=output)
00086
00087 os.system("sed '1,3d' "+ output+ ' > '+ out)
00088 os.system("diff "+reference+" "+out+" > "+diff)
00089 self.assertTrue(lt.compare(out,reference),
00090 'New and reference files are different. %s != %s. '
00091 'See the diff file %s.'%(out,reference, diff))
00092
00093 def test4(self):
00094 '''Listobs 4: Save on a file, verbose=False'''
00095 output = 'listobs5.txt'
00096 out = "newobs5.txt"
00097 reference = reffile+'5'
00098 diff1 = "diff1listobs5"
00099 diff2 = "diff2listobs5"
00100
00101
00102 self.res = listobs(vis=msfile1, verbose = False, listfile=output)
00103
00104 os.system("sed '1,3d' "+ output+ ' > '+ out)
00105 os.system("diff "+reference+" "+out+" > "+diff1)
00106 self.assertTrue(lt.compare(out,reference),
00107 'New and reference files are different in first run. %s != %s. '
00108 'See the diff file %s.'%(out,reference, diff1))
00109
00110 os.system('rm -rf '+output+ " "+out)
00111 self.res = listobs(vis=msfile1, verbose = False, listfile=output)
00112
00113 os.system("sed '1,3d' "+ output+ ' > '+ out)
00114 os.system("diff "+reference+" "+out+" > "+diff2)
00115 self.assertTrue(lt.compare(out,reference),
00116 'New and reference files are different in second run. %s != %s. '
00117 'See the diff file %s.'%(out,reference,diff2))
00118
00119 def test5(self):
00120 '''Listobs 5: Save on a file, verbose=True'''
00121 output = 'listobs6.txt'
00122 out = "newobs6.txt"
00123 diff = "difflistobs6"
00124 reference = reffile+'6'
00125 self.res = listobs(vis=msfile1, listfile=output, verbose = True)
00126
00127 os.system("sed '1,3d' "+ output+ ' > '+ out)
00128 os.system("diff "+reference+" "+out+" > "+diff)
00129 self.assertTrue(lt.compare(out,reference),
00130 'New and reference files are different. %s != %s. '
00131 'See the diff file %s.'%(out,reference,diff))
00132
00133
00134 def test6(self):
00135 '''Listobs 6: test scan selection parameters'''
00136 output = "listobs7.txt"
00137 out = "newobs7.txt"
00138 diff = "difflistobs7"
00139 reference = reffile+'7'
00140 self.res = listobs(vis=msfile1, scan='2', listfile=output, verbose=True)
00141
00142 os.system("sed '1,3d' "+ output+ ' > '+ out)
00143 os.system("diff "+reference+" "+out+" > "+diff)
00144 self.assertTrue(lt.compare(out,reference),
00145 'New and reference files are different. %s != %s. '
00146 'See the diff file %s.'%(out,reference,diff))
00147
00148 def test7(self):
00149 '''Listobs 7: test antenna selection parameters'''
00150 output = "listobs8.txt"
00151 out = "newobs8.txt"
00152 diff = "difflistobs8"
00153 reference = reffile+'8'
00154 self.res = listobs(vis=msfile1, antenna='3&&4', listfile=output, verbose=True)
00155
00156 os.system("sed '1,3d' "+ output+ ' > '+ out)
00157 os.system("diff "+reference+" "+out+" > "+diff)
00158 self.assertTrue(lt.compare(out,reference),
00159 'New and reference files are different. %s != %s. '
00160 'See the diff file %s.'%(out,reference,diff))
00161
00162
00163 class listobs_cleanup(unittest.TestCase):
00164
00165 def tearDown(self):
00166
00167 shutil.rmtree(msfile1,ignore_errors=True)
00168 shutil.rmtree(msfile2,ignore_errors=True)
00169 os.system('rm -rf ' + 'listobs*.txt')
00170
00171 def test_run(self):
00172 '''Listobs: Cleanup'''
00173 pass
00174
00175 def suite():
00176 return [listobs_test1,listobs_cleanup]