casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
test_concat.py
Go to the documentation of this file.
00001 #############################################################################
00002 # $Id:$
00003 # Test Name:                                                                #
00004 #    Unit Test Script for the concat task
00005 #    
00006 #                                                                           #
00007 #############################################################################
00008 import os
00009 import sys
00010 import shutil
00011 import glob
00012 from __main__ import default
00013 from tasks import *
00014 from taskinit import *
00015 import unittest
00016 
00017 myname = 'test_concat'
00018 
00019 # name of the resulting MS
00020 msname = 'concatenated.ms'
00021 
00022 testmms=False
00023 
00024 def checktable(thename, theexpectation, multims=False):
00025     global msname, myname
00026     if multims:        
00027         tb.open(msname+"/SUBMSS/"+thename)
00028     else:
00029         tb.open(msname+"/"+thename)
00030     if thename == "":
00031         thename = "MAIN"
00032     for mycell in theexpectation:
00033         print myname, ": comparing ", mycell
00034         value = tb.getcell(mycell[0], mycell[1])
00035         # see if value is array
00036         try:
00037             isarray = value.__len__
00038         except:
00039             # it's not an array
00040             # zero tolerance?
00041             if mycell[3] == 0:
00042                 in_agreement = (value == mycell[2])
00043             else:
00044                 in_agreement = ( abs(value - mycell[2]) < mycell[3]) 
00045         else:
00046             # it's an array
00047             # zero tolerance?
00048             if mycell[3] == 0:
00049                 in_agreement =  (value == mycell[2]).all() 
00050             else:
00051                 try:
00052                     in_agreement = (abs(value - mycell[2]) < mycell[3]).all()
00053                 except:
00054                     in_agreement = False
00055         if not in_agreement:
00056             print myname, ":  Error in MS subtable", thename, ":"
00057             print "     column ", mycell[0], " row ", mycell[1], " contains ", value
00058             print "     expected value is ", mycell[2]
00059             tb.close()
00060             return False
00061     tb.close()
00062     print myname, ": table ", thename, " as expected."
00063     return True
00064 
00065 
00066 ###########################
00067 # beginning of actual test 
00068 
00069 class test_concat(unittest.TestCase):
00070     
00071     def setUp(self):
00072         global testmms
00073         res = None
00074 
00075         datapath=os.environ.get('CASAPATH').split()[0]+'/data/regression/unittest/concat/input/'
00076         datapathmms = ''
00077         # Pick up alternative data directory to run tests on MMSs
00078         testmms = False
00079         if os.environ.has_key('TEST_DATADIR'):   
00080             testmms = True
00081             DATADIR = str(os.environ.get('TEST_DATADIR'))
00082             if os.path.isdir(DATADIR):
00083                 datapathmms = DATADIR+'/concat/input/'
00084 
00085         
00086         cpath = os.path.abspath(os.curdir)
00087         filespresent = sorted(glob.glob("*.ms"))
00088         if(datapathmms!=''): 
00089             print "\nTesting on MMSs ...\n"
00090             nonmmsinput = ['A2256LC2_4.5s-1.ms', 'part1.ms', 'part2-mod2.ms', 'shortpart1.ms', 'sim7.ms']
00091             os.chdir(datapathmms)
00092             for mymsname in sorted(glob.glob("*.ms")):
00093                 if not ((mymsname in filespresent) or (mymsname in nonmmsinput)):
00094                     print "Copying MMS", mymsname
00095                     shutil.copytree(mymsname, cpath+'/'+mymsname)
00096             os.chdir(datapath)
00097             for mymsname in nonmmsinput:
00098                 if not mymsname in filespresent:
00099                     print "Copying non-MMS ", mymsname
00100                     shutil.copytree(mymsname, cpath+'/'+mymsname)
00101         else:
00102             os.chdir(datapath)
00103             for mymsname in sorted(glob.glob("*.ms")):
00104                 if not mymsname in filespresent:
00105                     print "Copying ", mymsname
00106                     shutil.copytree(mymsname, cpath+'/'+mymsname)
00107                     
00108         os.chdir(cpath)
00109 
00110         default(concat)
00111         
00112     def tearDown(self):
00113         shutil.rmtree(msname,ignore_errors=True)
00114 
00115     def test1(self):
00116         '''Concat 1: 4 parts, same sources but different spws'''
00117         retValue = {'success': True, 'msgs': "", 'error_msgs': '' }    
00118         
00119         self.res = concat(vis=['part1.ms','part2.ms','part3.ms','part4.ms'],concatvis=msname)
00120         self.assertEqual(self.res,True)
00121 
00122         print myname, ": Now checking output ..."
00123         mscomponents = set(["table.dat",
00124                             "table.f0",
00125                             "table.f1",
00126                             "table.f2",
00127                             "table.f3",
00128                             "table.f4",
00129                             "table.f5",
00130                             "table.f6",
00131                             "table.f7",
00132                             "table.f8",
00133                             "ANTENNA/table.dat",
00134                             "DATA_DESCRIPTION/table.dat",
00135                             "FEED/table.dat",
00136                             "FIELD/table.dat",
00137                             "FLAG_CMD/table.dat",
00138                             "HISTORY/table.dat",
00139                             "OBSERVATION/table.dat",
00140                             "POINTING/table.dat",
00141                             "POLARIZATION/table.dat",
00142                             "PROCESSOR/table.dat",
00143                             "SOURCE/table.dat",
00144                             "SPECTRAL_WINDOW/table.dat",
00145                             "STATE/table.dat",
00146                             "ANTENNA/table.f0",
00147                             "DATA_DESCRIPTION/table.f0",
00148                             "FEED/table.f0",
00149                             "FIELD/table.f0",
00150                             "FLAG_CMD/table.f0",
00151                             "HISTORY/table.f0",
00152                             "OBSERVATION/table.f0",
00153                             "POINTING/table.f0",
00154                             "POLARIZATION/table.f0",
00155                             "PROCESSOR/table.f0",
00156                             "SOURCE/table.f0",
00157                             "SPECTRAL_WINDOW/table.f0",
00158                             "STATE/table.f0"
00159                             ])
00160         for name in mscomponents:
00161             if not os.access(msname+"/"+name, os.F_OK):
00162                 print myname, ": Error  ", msname+"/"+name, "doesn't exist ..."
00163                 retValue['success']=False
00164                 retValue['error_msgs']=retValue['error_msgs']+msname+'/'+name+' does not exist'
00165             else:
00166                 print myname, ": ", name, "present."
00167         print myname, ": MS exists. All tables present. Try opening as MS ..."
00168         try:
00169             ms.open(msname)
00170         except:
00171             print myname, ": Error  Cannot open MS table", tablename
00172             retValue['success']=False
00173             retValue['error_msgs']=retValue['error_msgs']+'Cannot open MS table '+tablename
00174         else:
00175             ms.close()
00176             if 'test1.ms' in glob.glob("*.ms"):
00177                 shutil.rmtree('test1.ms',ignore_errors=True)
00178             shutil.copytree(msname,'test1.ms')
00179             print myname, ": OK. Checking tables in detail ..."
00180             retValue['success']=True
00181 
00182             # check source table
00183             name = "SOURCE"
00184             #             col name, row number, expected value, tolerance
00185             expected = [
00186                 ['SOURCE_ID',           55, 13, 0],
00187                 ['SPECTRAL_WINDOW_ID',  55, 3, 0]
00188                 ]
00189             results = checktable(name, expected)
00190             if not results:
00191                 retValue['success']=False
00192                 retValue['error_msgs']=retValue['error_msgs']+'Check of table '+name+' failed'
00193             # check spw table
00194             name = "SPECTRAL_WINDOW"
00195             #             col name, row number, expected value, tolerance
00196             expected = [
00197                 ['NUM_CHAN',           3, 128, 0]
00198                 ]
00199             results = checktable(name, expected)
00200             if not results:
00201                 retValue['success']=False
00202                 retValue['error_msgs']=retValue['error_msgs']+'Check of table '+name+' failed'
00203 
00204         self.assertTrue(retValue['success'])
00205 
00206 
00207     def test2(self):
00208         '''Concat 2: 3 parts, different sources, different spws,  copypointing=False, visweightscale=[3.,2.,1.]'''
00209         retValue = {'success': True, 'msgs': "", 'error_msgs': '' }
00210         self.res = concat(vis=['part1.ms','part2-mod.ms','part3.ms'],concatvis=msname, copypointing=False, visweightscale=[3.,2.,1.])
00211         self.assertEqual(self.res,True)
00212         
00213         print myname, ": Now checking output ..."
00214         mscomponents = set(["table.dat",
00215                             "table.f0",
00216                             "table.f1",
00217                             "table.f2",
00218                             "table.f3",
00219                             "table.f4",
00220                             "table.f5",
00221                             "table.f6",
00222                             "table.f7",
00223                             "table.f8",
00224                             "ANTENNA/table.dat",
00225                             "DATA_DESCRIPTION/table.dat",
00226                             "FEED/table.dat",
00227                             "FIELD/table.dat",
00228                             "FLAG_CMD/table.dat",
00229                             "HISTORY/table.dat",
00230                             "OBSERVATION/table.dat",
00231                             "POINTING/table.dat",
00232                             "POLARIZATION/table.dat",
00233                             "PROCESSOR/table.dat",
00234                             "SOURCE/table.dat",
00235                             "SPECTRAL_WINDOW/table.dat",
00236                             "STATE/table.dat",
00237                             "ANTENNA/table.f0",
00238                             "DATA_DESCRIPTION/table.f0",
00239                             "FEED/table.f0",
00240                             "FIELD/table.f0",
00241                             "FLAG_CMD/table.f0",
00242                             "HISTORY/table.f0",
00243                             "OBSERVATION/table.f0",
00244                             "POINTING/table.f0",
00245                             "POLARIZATION/table.f0",
00246                             "PROCESSOR/table.f0",
00247                             "SOURCE/table.f0",
00248                             "SPECTRAL_WINDOW/table.f0",
00249                             "STATE/table.f0"
00250                             ])
00251         for name in mscomponents:
00252             if not os.access(msname+"/"+name, os.F_OK):
00253                 print myname, ": Error  ", msname+"/"+name, "doesn't exist ..."
00254                 retValue['success']=False
00255                 retValue['error_msgs']=retValue['error_msgs']+msname+'/'+name+' does not exist'
00256             else:
00257                 print myname, ": ", name, "present."
00258         print myname, ": MS exists. All tables present. Try opening as MS ..."
00259         try:
00260             ms.open(msname)
00261         except:
00262             print myname, ": Error  Cannot open MS table", tablename
00263             retValue['success']=False
00264             retValue['error_msgs']=retValue['error_msgs']+'Cannot open MS table '+tablename
00265         else:
00266             ms.close()
00267             if 'test2.ms' in glob.glob("*.ms"):
00268                 shutil.rmtree('test2.ms',ignore_errors=True)
00269             shutil.copytree(msname,'test2.ms')
00270             print myname, ": OK. Checking tables in detail ..."
00271             retValue['success']=True
00272 
00273             # check source table
00274             name = "SOURCE"
00275             #             col name, row number, expected value, tolerance
00276             expected = [
00277                 ['SOURCE_ID',           41, 13, 0],
00278                 ['SPECTRAL_WINDOW_ID',  41, 2, 0]
00279                 ]
00280             results = checktable(name, expected)
00281             if not results:
00282                 retValue['success']=False
00283                 retValue['error_msgs']=retValue['error_msgs']+'Check of table '+name+' failed'
00284             # check spw table
00285             name = "SPECTRAL_WINDOW"
00286             #             col name, row number, expected value, tolerance
00287             expected = [
00288                 ['NUM_CHAN',           2, 128, 0]
00289                 ]
00290             results = checktable(name, expected)
00291             if not results:
00292                 retValue['success']=False
00293                 retValue['error_msgs']=retValue['error_msgs']+'Check of table '+name+' failed'
00294 
00295             # collecting parameters for subsequent test of MAIN table
00296             msnrows = []
00297             oldweightbeg = []
00298             oldweightend = []
00299             ii = 0
00300             for myms in ['part1.ms','part2-mod.ms','part3.ms']:
00301                 tb.open(myms)
00302                 msnrows.append(tb.nrows())
00303                 oldweightbeg.append(tb.getcell('WEIGHT',0))
00304                 oldweightend.append(tb.getcell('WEIGHT',tb.nrows()-1))
00305                 tb.close()
00306 
00307 
00308             name = "" # i.e. Main
00309             #             col name, row number, expected value, tolerance
00310             expected = [
00311                     ['WEIGHT', 0, 3.*oldweightbeg[0], 1E-6], # scaling uses float precision
00312                     ['WEIGHT', msnrows[0]-1, 3.*oldweightend[0], 1E-6],
00313                     ['WEIGHT', msnrows[0], 2.*oldweightbeg[1], 1E-6],
00314                     ['WEIGHT', msnrows[0]+msnrows[1]-1, 2.*oldweightend[1], 1E-6],
00315                     ['WEIGHT', msnrows[0]+msnrows[1], oldweightbeg[2], 1E-6],
00316                     ['WEIGHT', msnrows[0]+msnrows[1]+msnrows[2]-1, oldweightend[2], 1E-6]
00317                 ]
00318 
00319             results = checktable(name, expected)
00320             if not results:
00321                 retValue['success']=False
00322                 retValue['error_msgs']=retValue['error_msgs']+'Check of table '+name+' failed'
00323 
00324         self.assertTrue(retValue['success'])
00325 
00326 
00327     def test3(self):
00328         '''Concat 3: 3 parts, different sources, same spws'''
00329         retValue = {'success': True, 'msgs': "", 'error_msgs': '' }    
00330         self.res = concat(vis=['part1.ms','part2-mod2.ms','part3.ms'],concatvis=msname)
00331         self.assertEqual(self.res,True)
00332 
00333         print myname, ": Now checking output ..."
00334         mscomponents = set(["table.dat",
00335                             "table.f0",
00336                             "table.f1",
00337                             "table.f2",
00338                             "table.f3",
00339                             "table.f4",
00340                             "table.f5",
00341                             "table.f6",
00342                             "table.f7",
00343                             "table.f8",
00344                             "ANTENNA/table.dat",
00345                             "DATA_DESCRIPTION/table.dat",
00346                             "FEED/table.dat",
00347                             "FIELD/table.dat",
00348                             "FLAG_CMD/table.dat",
00349                             "HISTORY/table.dat",
00350                             "OBSERVATION/table.dat",
00351                             "POINTING/table.dat",
00352                             "POLARIZATION/table.dat",
00353                             "PROCESSOR/table.dat",
00354                             "SOURCE/table.dat",
00355                             "SPECTRAL_WINDOW/table.dat",
00356                             "STATE/table.dat",
00357                             "ANTENNA/table.f0",
00358                             "DATA_DESCRIPTION/table.f0",
00359                             "FEED/table.f0",
00360                             "FIELD/table.f0",
00361                             "FLAG_CMD/table.f0",
00362                             "HISTORY/table.f0",
00363                             "OBSERVATION/table.f0",
00364                             "POINTING/table.f0",
00365                             "POLARIZATION/table.f0",
00366                             "PROCESSOR/table.f0",
00367                             "SOURCE/table.f0",
00368                             "SPECTRAL_WINDOW/table.f0",
00369                             "STATE/table.f0"
00370                             ])
00371         for name in mscomponents:
00372             if not os.access(msname+"/"+name, os.F_OK):
00373                 print myname, ": Error  ", msname+"/"+name, "doesn't exist ..."
00374                 retValue['success']=False
00375                 retValue['error_msgs']=retValue['error_msgs']+msname+'/'+name+' does not exist'
00376             else:
00377                 print myname, ": ", name, "present."
00378         print myname, ": MS exists. All tables present. Try opening as MS ..."
00379         try:
00380             ms.open(msname)
00381         except:
00382             print myname, ": Error  Cannot open MS table", tablename
00383             retValue['success']=False
00384             retValue['error_msgs']=retValue['error_msgs']+'Cannot open MS table '+tablename
00385         else:
00386             ms.close()
00387             if 'test3.ms' in glob.glob("*.ms"):
00388                 shutil.rmtree('test3.ms',ignore_errors=True)
00389             shutil.copytree(msname,'test3.ms')
00390             print myname, ": OK. Checking tables in detail ..."
00391             retValue['success']=True
00392 
00393             # check source table
00394             name = "SOURCE"
00395             #             col name, row number, expected value, tolerance
00396             expected = [
00397                 ['SOURCE_ID',           28, 13, 0],
00398                 ['SPECTRAL_WINDOW_ID',  28, 1, 0]
00399                 ]
00400             results = checktable(name, expected)
00401             if not results:
00402                 retValue['success']=False
00403                 retValue['error_msgs']=retValue['error_msgs']+'Check of table '+name+' failed'
00404             # check spw table
00405             name = "SPECTRAL_WINDOW"
00406             #             col name, row number, expected value, tolerance
00407             expected = [
00408                 ['NUM_CHAN',           1, 128, 0]
00409                 ]
00410             results = checktable(name, expected)
00411             if not results:
00412                 retValue['success']=False
00413                 retValue['error_msgs']=retValue['error_msgs']+'Check of table '+name+' failed'
00414 
00415         self.assertTrue(retValue['success'])
00416 
00417 
00418     def test4(self):
00419         '''Concat 4: five MSs with identical sources but different time/intervals on them (CSV-268)'''
00420         retValue = {'success': True, 'msgs': "", 'error_msgs': '' }
00421         
00422         self.res = concat(vis = ['shortpart1.ms', 'shortpart2.ms', 'shortpart3.ms', 'shortpart4.ms', 'shortpart5.ms'],
00423                           concatvis = msname)
00424         self.assertEqual(self.res,True)
00425 
00426         print myname, ": Now checking output ..."
00427         mscomponents = set(["table.dat",
00428                             "table.f0",
00429                             "table.f1",
00430                             "table.f2",
00431                             "table.f3",
00432                             "table.f4",
00433                             "table.f5",
00434                             "table.f6",
00435                             "table.f7",
00436                             "table.f8",
00437                             "ANTENNA/table.dat",
00438                             "DATA_DESCRIPTION/table.dat",
00439                             "FEED/table.dat",
00440                             "FIELD/table.dat",
00441                             "FLAG_CMD/table.dat",
00442                             "HISTORY/table.dat",
00443                             "OBSERVATION/table.dat",
00444                             "POINTING/table.dat",
00445                             "POLARIZATION/table.dat",
00446                             "PROCESSOR/table.dat",
00447                             "SOURCE/table.dat",
00448                             "SPECTRAL_WINDOW/table.dat",
00449                             "STATE/table.dat",
00450                             "ANTENNA/table.f0",
00451                             "DATA_DESCRIPTION/table.f0",
00452                             "FEED/table.f0",
00453                             "FIELD/table.f0",
00454                             "FLAG_CMD/table.f0",
00455                             "HISTORY/table.f0",
00456                             "OBSERVATION/table.f0",
00457                             "POINTING/table.f0",
00458                             "POLARIZATION/table.f0",
00459                             "PROCESSOR/table.f0",
00460                             "SOURCE/table.f0",
00461                             "SPECTRAL_WINDOW/table.f0",
00462                             "STATE/table.f0"
00463                             ])
00464         for name in mscomponents:
00465             if not os.access(msname+"/"+name, os.F_OK):
00466                 print myname, ": Error  ", msname+"/"+name, "doesn't exist ..."
00467                 retValue['success']=False
00468                 retValue['error_msgs']=retValue['error_msgs']+msname+'/'+name+' does not exist'
00469             else:
00470                 print myname, ": ", name, "present."
00471         print myname, ": MS exists. All tables present. Try opening as MS ..."
00472         try:
00473             ms.open(msname)
00474         except:
00475             print myname, ": Error  Cannot open MS table", tablename
00476             retValue['success']=False
00477             retValue['error_msgs']=retValue['error_msgs']+'Cannot open MS table '+tablename
00478         else:
00479             ms.close()
00480             if 'test4.ms' in glob.glob("*.ms"):
00481                 shutil.rmtree('test4.ms',ignore_errors=True)
00482             shutil.copytree(msname,'test4.ms')
00483             print myname, ": OK. Checking tables in detail ..."
00484             retValue['success']=True
00485         
00486     
00487             # check source table
00488             name = "SOURCE"
00489             #             col name, row number, expected value, tolerance
00490             expected = [
00491                 ['SOURCE_ID',           0, 0, 0],
00492                 ['SPECTRAL_WINDOW_ID',  0, 0, 0]
00493                 ]
00494             results = checktable(name, expected)
00495             if not results:
00496                 retValue['success']=False
00497                 retValue['error_msgs']=retValue['error_msgs']+'Check of table '+name+' failed'
00498             expected = [
00499                 ['SOURCE_ID',           7, 0, 0],
00500                 ['SPECTRAL_WINDOW_ID',  7, 7, 0]
00501                 ]
00502             results = checktable(name, expected)
00503             if not results:
00504                 retValue['success']=False
00505                 retValue['error_msgs']=retValue['error_msgs']+'Check of table '+name+' failed'
00506             expected = [
00507                 ['SOURCE_ID',           8, 1, 0],
00508                 ['SPECTRAL_WINDOW_ID',  8, 0, 0]
00509                 ]
00510             results = checktable(name, expected)
00511             if not results:
00512                 retValue['success']=False
00513                 retValue['error_msgs']=retValue['error_msgs']+'Check of table '+name+' failed'
00514             expected = [
00515                 ['SOURCE_ID',           15, 1, 0],
00516                 ['SPECTRAL_WINDOW_ID',  15, 7, 0]
00517                 ]
00518             results = checktable(name, expected)
00519             if not results:
00520                 retValue['success']=False
00521                 retValue['error_msgs']=retValue['error_msgs']+'Check of table '+name+' failed'
00522             expected = [
00523                 ['SOURCE_ID',           16, 0, 100000],
00524                 ['SPECTRAL_WINDOW_ID',  16, 0, 100000]
00525                 ]
00526             print "The following should fail: SOURCE row 16 should not exist"
00527             try:
00528                 results = checktable(name, expected)
00529             except:
00530                 print "Expected error."
00531                 results = False
00532             if results: 
00533                 retValue['success']=False
00534                 retValue['error_msgs']='SOURCE row 16 should not existCheck of table '+name+' failed'
00535             # check spw table
00536             name = "SPECTRAL_WINDOW"
00537             #             col name, row number, expected value, tolerance
00538             expected = [
00539                 ['NUM_CHAN',           8, 4, 0]
00540                 ]
00541             results = checktable(name, expected)
00542             if not results:
00543                 retValue['success']=False
00544                 retValue['error_msgs']=retValue['error_msgs']+'Check of table '+name+' failed'
00545     
00546                 
00547         self.assertTrue(retValue['success'])
00548         
00549     def test5(self):
00550         '''Concat 5: two MSs with different state table (CAS-2601)'''
00551         retValue = {'success': True, 'msgs': "", 'error_msgs': '' }
00552         
00553         self.res = concat(vis = ['A2256LC2_4.5s-1.ms','A2256LC2_4.5s-2.ms'],
00554                           concatvis = msname)
00555         self.assertEqual(self.res,True)
00556 
00557         print myname, ": Now checking output ..."
00558         mscomponents = set(["table.dat",
00559                             "table.f0",
00560                             "table.f1",
00561                             "table.f2",
00562                             "table.f3",
00563                             "table.f4",
00564                             "table.f5",
00565                             "table.f6",
00566                             "table.f7",
00567                             "table.f8",
00568                             "ANTENNA/table.dat",
00569                             "DATA_DESCRIPTION/table.dat",
00570                             "FEED/table.dat",
00571                             "FIELD/table.dat",
00572                             "FLAG_CMD/table.dat",
00573                             "HISTORY/table.dat",
00574                             "OBSERVATION/table.dat",
00575                             "POINTING/table.dat",
00576                             "POLARIZATION/table.dat",
00577                             "PROCESSOR/table.dat",
00578                             "SOURCE/table.dat",
00579                             "SPECTRAL_WINDOW/table.dat",
00580                             "STATE/table.dat",
00581                             "ANTENNA/table.f0",
00582                             "DATA_DESCRIPTION/table.f0",
00583                             "FEED/table.f0",
00584                             "FIELD/table.f0",
00585                             "FLAG_CMD/table.f0",
00586                             "HISTORY/table.f0",
00587                             "OBSERVATION/table.f0",
00588                             "POINTING/table.f0",
00589                             "POLARIZATION/table.f0",
00590                             "PROCESSOR/table.f0",
00591                             "SOURCE/table.f0",
00592                             "SPECTRAL_WINDOW/table.f0",
00593                             "STATE/table.f0"
00594                             ])
00595         for name in mscomponents:
00596             if not os.access(msname+"/"+name, os.F_OK):
00597                 print myname, ": Error  ", msname+"/"+name, "doesn't exist ..."
00598                 retValue['success']=False
00599                 retValue['error_msgs']=retValue['error_msgs']+msname+'/'+name+' does not exist'
00600             else:
00601                 print myname, ": ", name, "present."
00602         print myname, ": MS exists. All tables present. Try opening as MS ..."
00603         try:
00604             ms.open(msname)
00605         except:
00606             print myname, ": Error  Cannot open MS table", tablename
00607             retValue['success']=False
00608             retValue['error_msgs']=retValue['error_msgs']+'Cannot open MS table '+tablename
00609         else:
00610             ms.close()
00611             if 'test5.ms' in glob.glob("*.ms"):
00612                 shutil.rmtree('test5.ms',ignore_errors=True)
00613             shutil.copytree(msname,'test5.ms')
00614             print myname, ": OK. Checking tables in detail ..."
00615             retValue['success']=True        
00616     
00617             # check state table
00618             name = "STATE"
00619             #             col name, row number, expected value, tolerance
00620             expected = [
00621                 ['CAL',  0, 0, 0],
00622                 ['SIG',  0, 1, 0],
00623                 ['SUB_SCAN',  2, 1, 0]
00624                 ]
00625             results = checktable(name, expected)
00626             if not results:
00627                 retValue['success']=False
00628                 retValue['error_msgs']=retValue['error_msgs']+'Check of table '+name+' failed'
00629                 retValue['error_msgs']=retValue['error_msgs']+'Check of table '+name+' failed'
00630                 
00631         self.assertTrue(retValue['success'])
00632 
00633     def test6(self):
00634         '''Concat 6: two MSs with different state table and feed table'''
00635         retValue = {'success': True, 'msgs': "", 'error_msgs': '' }
00636         
00637         self.res = concat(vis = ['A2256LC2_4.5s-1.ms','A2256LC2_4.5s-2b.ms'],
00638                           concatvis = msname)
00639         self.assertEqual(self.res,True)
00640 
00641         print myname, ": Now checking output ..."
00642         mscomponents = set(["table.dat",
00643                             "table.f0",
00644                             "table.f1",
00645                             "table.f2",
00646                             "table.f3",
00647                             "table.f4",
00648                             "table.f5",
00649                             "table.f6",
00650                             "table.f7",
00651                             "table.f8",
00652                             "ANTENNA/table.dat",
00653                             "DATA_DESCRIPTION/table.dat",
00654                             "FEED/table.dat",
00655                             "FIELD/table.dat",
00656                             "FLAG_CMD/table.dat",
00657                             "HISTORY/table.dat",
00658                             "OBSERVATION/table.dat",
00659                             "POINTING/table.dat",
00660                             "POLARIZATION/table.dat",
00661                             "PROCESSOR/table.dat",
00662                             "SOURCE/table.dat",
00663                             "SPECTRAL_WINDOW/table.dat",
00664                             "STATE/table.dat",
00665                             "ANTENNA/table.f0",
00666                             "DATA_DESCRIPTION/table.f0",
00667                             "FEED/table.f0",
00668                             "FIELD/table.f0",
00669                             "FLAG_CMD/table.f0",
00670                             "HISTORY/table.f0",
00671                             "OBSERVATION/table.f0",
00672                             "POINTING/table.f0",
00673                             "POLARIZATION/table.f0",
00674                             "PROCESSOR/table.f0",
00675                             "SOURCE/table.f0",
00676                             "SPECTRAL_WINDOW/table.f0",
00677                             "STATE/table.f0"
00678                             ])
00679         for name in mscomponents:
00680             if not os.access(msname+"/"+name, os.F_OK):
00681                 print myname, ": Error  ", msname+"/"+name, "doesn't exist ..."
00682                 retValue['success']=False
00683                 retValue['error_msgs']=retValue['error_msgs']+msname+'/'+name+' does not exist'
00684             else:
00685                 print myname, ": ", name, "present."
00686         print myname, ": MS exists. All tables present. Try opening as MS ..."
00687         try:
00688             ms.open(msname)
00689         except:
00690             print myname, ": Error  Cannot open MS table", tablename
00691             retValue['success']=False
00692             retValue['error_msgs']=retValue['error_msgs']+'Cannot open MS table '+tablename
00693         else:
00694             ms.close()
00695             if 'test6.ms' in glob.glob("*.ms"):
00696                 shutil.rmtree('test6.ms',ignore_errors=True)
00697             shutil.copytree(msname,'test6.ms')
00698             print myname, ": OK. Checking tables in detail ..."
00699             retValue['success']=True        
00700     
00701             # check FEED table
00702             name = "FEED"
00703             #             col name, row number, expected value, tolerance
00704             expected = [
00705                 ['SPECTRAL_WINDOW_ID',  53, 1, 0],
00706                 ['SPECTRAL_WINDOW_ID',  54, 2, 0],
00707                 ['SPECTRAL_WINDOW_ID',  107, 3, 0],
00708                 ['RECEPTOR_ANGLE',  54, [-1,0], 0]
00709                 ]
00710             results = checktable(name, expected)
00711             if not results:
00712                 retValue['success']=False
00713                 retValue['error_msgs']=retValue['error_msgs']+'Check of table '+name+' failed'
00714                 retValue['error_msgs']=retValue['error_msgs']+'Check of table '+name+' failed'
00715                 
00716         self.assertTrue(retValue['success'])
00717 
00718     def test7(self):
00719         '''Concat 7: two MSs with different antenna table such that baseline label reversal becomes necessary'''
00720         retValue = {'success': True, 'msgs': "", 'error_msgs': '' }
00721         
00722         self.res = concat(vis = ['sim7.ms','sim8.ms'],
00723                           concatvis = msname)
00724         self.assertEqual(self.res,True)
00725 
00726         print myname, ": Now checking output ..."
00727         mscomponents = set(["table.dat",
00728                             "table.f0",
00729                             "table.f1",
00730                             "table.f2",
00731                             "table.f3",
00732                             "table.f4",
00733                             "table.f5",
00734                             "table.f6",
00735                             "table.f7",
00736                             "table.f8",
00737                             "ANTENNA/table.dat",
00738                             "DATA_DESCRIPTION/table.dat",
00739                             "FEED/table.dat",
00740                             "FIELD/table.dat",
00741                             "FLAG_CMD/table.dat",
00742                             "HISTORY/table.dat",
00743                             "OBSERVATION/table.dat",
00744                             "POINTING/table.dat",
00745                             "POLARIZATION/table.dat",
00746                             "PROCESSOR/table.dat",
00747                             "SOURCE/table.dat",
00748                             "SPECTRAL_WINDOW/table.dat",
00749                             "STATE/table.dat",
00750                             "ANTENNA/table.f0",
00751                             "DATA_DESCRIPTION/table.f0",
00752                             "FEED/table.f0",
00753                             "FIELD/table.f0",
00754                             "FLAG_CMD/table.f0",
00755                             "HISTORY/table.f0",
00756                             "OBSERVATION/table.f0",
00757                             "POINTING/table.f0",
00758                             "POLARIZATION/table.f0",
00759                             "PROCESSOR/table.f0",
00760                             "SOURCE/table.f0",
00761                             "SPECTRAL_WINDOW/table.f0",
00762                             "STATE/table.f0"
00763                             ])
00764         for name in mscomponents:
00765             if not os.access(msname+"/"+name, os.F_OK):
00766                 print myname, ": Error  ", msname+"/"+name, "doesn't exist ..."
00767                 retValue['success']=False
00768                 retValue['error_msgs']=retValue['error_msgs']+msname+'/'+name+' does not exist'
00769             else:
00770                 print myname, ": ", name, "present."
00771         print myname, ": MS exists. All tables present. Try opening as MS ..."
00772         try:
00773             ms.open(msname)
00774         except:
00775             print myname, ": Error  Cannot open MS table", tablename
00776             retValue['success']=False
00777             retValue['error_msgs']=retValue['error_msgs']+'Cannot open MS table '+tablename
00778         else:
00779             ms.close()
00780             if 'test7.ms' in glob.glob("*.ms"):
00781                 shutil.rmtree('test7.ms',ignore_errors=True)
00782             shutil.copytree(msname,'test7.ms')
00783             print myname, ": OK. Checking tables in detail ..."
00784             retValue['success']=True        
00785     
00786             # check Main table
00787             tb.open('test7.ms')
00788             ant1 = tb.getcol('ANTENNA1')
00789             ant2 = tb.getcol('ANTENNA2')
00790             tb.close()
00791             result = True
00792             print myname, ": OK. Checking baseline labels ..."
00793             for i in xrange(0,len(ant1)):
00794                 if(ant1[i]>ant2[i]):
00795                     print "Found incorrectly ordered baseline label in row ", i, ": ", ant1, " ", ant2
00796                     result = False
00797                     break
00798 
00799             if not result:
00800                 retValue['success']=False
00801                 retValue['error_msgs']=retValue['error_msgs']+'Check of table '+name+' failed'
00802                 retValue['error_msgs']=retValue['error_msgs']+'Check of table '+name+' failed'
00803                 
00804         self.assertTrue(retValue['success'])
00805 
00806     def test8(self):
00807         '''Concat 8: two MSs with different antenna tables, copypointing = False'''
00808         retValue = {'success': True, 'msgs': "", 'error_msgs': '' }
00809         
00810         self.res = concat(vis = ['sim7.ms','sim8.ms'],
00811                           concatvis = msname, copypointing=False)
00812         self.assertEqual(self.res,True)
00813 
00814         print myname, ": Now checking output ..."
00815         mscomponents = set(["table.dat",
00816                             "table.f0",
00817                             "table.f1",
00818                             "table.f2",
00819                             "table.f3",
00820                             "table.f4",
00821                             "table.f5",
00822                             "table.f6",
00823                             "table.f7",
00824                             "table.f8",
00825                             "ANTENNA/table.dat",
00826                             "DATA_DESCRIPTION/table.dat",
00827                             "FEED/table.dat",
00828                             "FIELD/table.dat",
00829                             "FLAG_CMD/table.dat",
00830                             "HISTORY/table.dat",
00831                             "OBSERVATION/table.dat",
00832                             "POINTING/table.dat",
00833                             "POLARIZATION/table.dat",
00834                             "PROCESSOR/table.dat",
00835                             "SOURCE/table.dat",
00836                             "SPECTRAL_WINDOW/table.dat",
00837                             "STATE/table.dat",
00838                             "ANTENNA/table.f0",
00839                             "DATA_DESCRIPTION/table.f0",
00840                             "FEED/table.f0",
00841                             "FIELD/table.f0",
00842                             "FLAG_CMD/table.f0",
00843                             "HISTORY/table.f0",
00844                             "OBSERVATION/table.f0",
00845                             "POINTING/table.f0",
00846                             "POLARIZATION/table.f0",
00847                             "PROCESSOR/table.f0",
00848                             "SOURCE/table.f0",
00849                             "SPECTRAL_WINDOW/table.f0",
00850                             "STATE/table.f0"
00851                             ])
00852         for name in mscomponents:
00853             if not os.access(msname+"/"+name, os.F_OK):
00854                 print myname, ": Error  ", msname+"/"+name, "doesn't exist ..."
00855                 retValue['success']=False
00856                 retValue['error_msgs']=retValue['error_msgs']+msname+'/'+name+' does not exist'
00857             else:
00858                 print myname, ": ", name, "present."
00859         print myname, ": MS exists. All tables present. Try opening as MS ..."
00860         try:
00861             ms.open(msname)
00862         except:
00863             print myname, ": Error  Cannot open MS table", tablename
00864             retValue['success']=False
00865             retValue['error_msgs']=retValue['error_msgs']+'Cannot open MS table '+tablename
00866         else:
00867             ms.close()
00868             if 'test8.ms' in glob.glob("*.ms"):
00869                 shutil.rmtree('test8.ms',ignore_errors=True)
00870             shutil.copytree(msname,'test8.ms')
00871             print myname, ": OK. Checking tables in detail ..."
00872             retValue['success']=True        
00873     
00874             # check Main table
00875             tb.open('test8.ms')
00876             ant1 = tb.getcol('ANTENNA1')
00877             ant2 = tb.getcol('ANTENNA2')
00878             tb.close()
00879             result = True
00880             print myname, ": OK. Checking baseline labels ..."
00881             for i in xrange(0,len(ant1)):
00882                 if(ant1[i]>ant2[i]):
00883                     print "Found incorrectly ordered baseline label in row ", i, ": ", ant1, " ", ant2
00884                     result = False
00885                     break
00886 
00887             if not result:
00888                 retValue['success']=False
00889                 retValue['error_msgs']=retValue['error_msgs']+'Check of table '+name+' failed'
00890                 
00891         self.assertTrue(retValue['success'])
00892 
00893 
00894 
00895     def test9(self):
00896         '''Concat 9: 3 parts, different sources, same spws, different scratch columns: no, yes, no'''
00897         global testmms
00898         retValue = {'success': True, 'msgs': "", 'error_msgs': '' }
00899 
00900         shutil.rmtree('part2-mod2-wscratch.ms',ignore_errors=True)
00901         shutil.copytree('part2-mod2.ms', 'part2-mod2-wscratch.ms')
00902         print 'creating scratch columns in part2-mod2-wscratch.ms'
00903         cb.open('part2-mod2-wscratch.ms') # calibrator-open creates scratch columns
00904         cb.close()
00905         
00906         if testmms:
00907             print "Expecting an Error ... "
00908 
00909         self.res = concat(vis=['part1.ms','part2-mod2-wscratch.ms','part3.ms'],concatvis=msname)
00910 
00911         if not testmms:
00912 
00913             self.assertEqual(self.res,True)
00914 
00915             print myname, ": Now checking output ..."
00916             mscomponents = set(["table.dat",
00917                                 "table.f0",
00918                                 "table.f1",
00919                                 "table.f2",
00920                                 "table.f3",
00921                                 "table.f4",
00922                                 "table.f5",
00923                                 "table.f6",
00924                                 "table.f7",
00925                                 "table.f8",
00926                                 "ANTENNA/table.dat",
00927                                 "DATA_DESCRIPTION/table.dat",
00928                                 "FEED/table.dat",
00929                                 "FIELD/table.dat",
00930                                 "FLAG_CMD/table.dat",
00931                                 "HISTORY/table.dat",
00932                                 "OBSERVATION/table.dat",
00933                                 "POINTING/table.dat",
00934                                 "POLARIZATION/table.dat",
00935                                 "PROCESSOR/table.dat",
00936                                 "SOURCE/table.dat",
00937                                 "SPECTRAL_WINDOW/table.dat",
00938                                 "STATE/table.dat",
00939                                 "ANTENNA/table.f0",
00940                                 "DATA_DESCRIPTION/table.f0",
00941                                 "FEED/table.f0",
00942                                 "FIELD/table.f0",
00943                                 "FLAG_CMD/table.f0",
00944                                 "HISTORY/table.f0",
00945                                 "OBSERVATION/table.f0",
00946                                 "POINTING/table.f0",
00947                                 "POLARIZATION/table.f0",
00948                                 "PROCESSOR/table.f0",
00949                                 "SOURCE/table.f0",
00950                                 "SPECTRAL_WINDOW/table.f0",
00951                                 "STATE/table.f0"
00952                                 ])
00953             for name in mscomponents:
00954                 if not os.access(msname+"/"+name, os.F_OK):
00955                     print myname, ": Error  ", msname+"/"+name, "doesn't exist ..."
00956                     retValue['success']=False
00957                     retValue['error_msgs']=retValue['error_msgs']+msname+'/'+name+' does not exist'
00958                 else:
00959                     print myname, ": ", name, "present."
00960             print myname, ": MS exists. All tables present. Try opening as MS ..."
00961             try:
00962                 ms.open(msname)
00963             except:
00964                 print myname, ": Error  Cannot open MS table", tablename
00965                 retValue['success']=False
00966                 retValue['error_msgs']=retValue['error_msgs']+'Cannot open MS table '+tablename
00967             else:
00968                 ms.close()
00969                 if 'test9.ms' in glob.glob("*.ms"):
00970                     shutil.rmtree('test9.ms',ignore_errors=True)
00971                 shutil.copytree(msname,'test9.ms')
00972                 print myname, ": OK. Checking tables in detail ..."
00973                 retValue['success']=True
00974 
00975                 # check source table
00976                 name = "SOURCE"
00977                 #             col name, row number, expected value, tolerance
00978                 expected = [
00979                     ['SOURCE_ID',           28, 13, 0],
00980                     ['SPECTRAL_WINDOW_ID',  28, 1, 0]
00981                     ]
00982                 results = checktable(name, expected)
00983                 if not results:
00984                     retValue['success']=False
00985                     retValue['error_msgs']=retValue['error_msgs']+'Check of table '+name+' failed'
00986                 # check spw table
00987                 name = "SPECTRAL_WINDOW"
00988                 #             col name, row number, expected value, tolerance
00989                 expected = [
00990                     ['NUM_CHAN',           1, 128, 0]
00991                     ]
00992                 results = checktable(name, expected)
00993                 if not results:
00994                     retValue['success']=False
00995                     retValue['error_msgs']=retValue['error_msgs']+'Check of table '+name+' failed'
00996 
00997             self.assertTrue(retValue['success'])
00998         # endif not testmms
00999         
01000     def test10(self):
01001         '''Concat 10: 3 parts, different sources, same spws, different scratch columns: yes, no, no'''
01002         global testmms
01003         retValue = {'success': True, 'msgs': "", 'error_msgs': '' }
01004         if testmms:
01005             print "Skipping this test as concat cannot create scratch columns in MMSs."
01006         else:
01007             shutil.rmtree('part1-wscratch.ms',ignore_errors=True)
01008             shutil.copytree('part1.ms', 'part1-wscratch.ms')
01009             print 'creating scratch columns in part1-wscratch.ms'
01010             cb.open('part1-wscratch.ms') # calibrator-open creates scratch columns
01011             cb.close()
01012 
01013             self.res = concat(vis=['part1-wscratch.ms','part2-mod2.ms','part3.ms'],concatvis=msname)
01014             self.assertEqual(self.res,True)
01015 
01016             print myname, ": Now checking output ..."
01017             mscomponents = set(["table.dat",
01018                                 "table.f0",
01019                                 "table.f1",
01020                                 "table.f2",
01021                                 "table.f3",
01022                                 "table.f4",
01023                                 "table.f5",
01024                                 "table.f6",
01025                                 "table.f7",
01026                                 "table.f8",
01027                                 "ANTENNA/table.dat",
01028                                 "DATA_DESCRIPTION/table.dat",
01029                                 "FEED/table.dat",
01030                                 "FIELD/table.dat",
01031                                 "FLAG_CMD/table.dat",
01032                                 "HISTORY/table.dat",
01033                                 "OBSERVATION/table.dat",
01034                                 "POINTING/table.dat",
01035                                 "POLARIZATION/table.dat",
01036                                 "PROCESSOR/table.dat",
01037                                 "SOURCE/table.dat",
01038                                 "SPECTRAL_WINDOW/table.dat",
01039                                 "STATE/table.dat",
01040                                 "ANTENNA/table.f0",
01041                                 "DATA_DESCRIPTION/table.f0",
01042                                 "FEED/table.f0",
01043                                 "FIELD/table.f0",
01044                                 "FLAG_CMD/table.f0",
01045                                 "HISTORY/table.f0",
01046                                 "OBSERVATION/table.f0",
01047                                 "POINTING/table.f0",
01048                                 "POLARIZATION/table.f0",
01049                                 "PROCESSOR/table.f0",
01050                                 "SOURCE/table.f0",
01051                                 "SPECTRAL_WINDOW/table.f0",
01052                                 "STATE/table.f0"
01053                                 ])
01054             for name in mscomponents:
01055                 if not os.access(msname+"/"+name, os.F_OK):
01056                     print myname, ": Error  ", msname+"/"+name, "doesn't exist ..."
01057                     retValue['success']=False
01058                     retValue['error_msgs']=retValue['error_msgs']+msname+'/'+name+' does not exist'
01059                 else:
01060                     print myname, ": ", name, "present."
01061             print myname, ": MS exists. All tables present. Try opening as MS ..."
01062             try:
01063                 ms.open(msname)
01064             except:
01065                 print myname, ": Error  Cannot open MS table", tablename
01066                 retValue['success']=False
01067                 retValue['error_msgs']=retValue['error_msgs']+'Cannot open MS table '+tablename
01068             else:
01069                 ms.close()
01070                 if 'test10.ms' in glob.glob("*.ms"):
01071                     shutil.rmtree('test10.ms',ignore_errors=True)
01072                 shutil.copytree(msname,'test10.ms')
01073                 print myname, ": OK. Checking tables in detail ..."
01074                 retValue['success']=True
01075 
01076                 # check source table
01077                 name = "SOURCE"
01078                 #             col name, row number, expected value, tolerance
01079                 expected = [
01080                     ['SOURCE_ID',           28, 13, 0],
01081                     ['SPECTRAL_WINDOW_ID',  28, 1, 0]
01082                     ]
01083                 results = checktable(name, expected)
01084                 if not results:
01085                     retValue['success']=False
01086                     retValue['error_msgs']=retValue['error_msgs']+'Check of table '+name+' failed'
01087                 # check spw table
01088                 name = "SPECTRAL_WINDOW"
01089                 #             col name, row number, expected value, tolerance
01090                 expected = [
01091                     ['NUM_CHAN',           1, 128, 0]
01092                     ]
01093                 results = checktable(name, expected)
01094                 if not results:
01095                     retValue['success']=False
01096                     retValue['error_msgs']=retValue['error_msgs']+'Check of table '+name+' failed'
01097 
01098             self.assertTrue(retValue['success'])
01099         # endif
01100 
01101 
01102 class concat_cleanup(unittest.TestCase):           
01103     def setUp(self):
01104         pass
01105     
01106     def tearDown(self):
01107         os.system('rm -rf *.ms')
01108 
01109     def testrun(self):
01110         '''Concat: Cleanup'''
01111         pass
01112     
01113 def suite():
01114     return [test_concat,concat_cleanup]        
01115