casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
test_flagcmd.py
Go to the documentation of this file.
00001 import shutil
00002 import unittest
00003 import os
00004 import filecmp
00005 from tasks import *
00006 from taskinit import *
00007 from __main__ import default
00008 
00009 #
00010 # Test of flagcmd task. It uses flagdata to unflag and summary
00011 #
00012 
00013 def test_eq(result, total, flagged):
00014 
00015     print "%s of %s data was flagged, expected %s of %s" % \
00016     (result['flagged'], result['total'], flagged, total)
00017     assert result['total'] == total, \
00018                "%s data in total; %s expected" % (result['total'], total)
00019     assert result['flagged'] == flagged, \
00020            "%s flags set; %s expected" % (result['flagged'], flagged)
00021 
00022 def create_input(str_text,fname=''):
00023     '''Save the string in a text file'''
00024     
00025     if fname=='':
00026         inp = 'flagcmd.txt'
00027     else:
00028         inp=fname
00029 
00030     cmd = str_text
00031     
00032     # remove file first
00033     if os.path.exists(inp):
00034         os.system('rm -f '+ inp)
00035         
00036     # save it in a file    
00037     with open(inp, 'w') as f:
00038         f.write(cmd)
00039         
00040     f.close()
00041     
00042     # return the name of the file
00043     return inp
00044     
00045 # Path for data
00046 datapath = os.environ.get('CASAPATH').split()[0] + "/data/regression/unittest/flagdata/"
00047 
00048 # Base class which defines setUp functions
00049 # for importing different data sets
00050 class test_base(unittest.TestCase):
00051     
00052     def setUp_ngc5921(self):
00053         self.vis = "ngc5921.ms"
00054 
00055         if os.path.exists(self.vis):
00056             print "The MS is already around, just unflag"
00057         else:
00058             os.system('cp -r '+datapath + self.vis +' '+ self.vis)
00059 
00060         os.system('rm -rf ' + self.vis + '.flagversions')
00061         flagdata(vis=self.vis, mode='unflag', savepars=False)
00062         default(flagcmd)
00063 
00064     def setUp_multi(self):
00065         self.vis = "multiobs.ms"
00066 
00067         if os.path.exists(self.vis):
00068             print "The MS is already around, just unflag"
00069         else:
00070             print "Moving data..."
00071             os.system('cp -r '+datapath + self.vis +' '+ self.vis)
00072 
00073         os.system('rm -rf ' + self.vis + '.flagversions')
00074         flagdata(vis=self.vis, mode='unflag', savepars=False)
00075         default(flagcmd)
00076 
00077     def setUp_flagdatatest_alma(self):
00078         self.vis = "flagdatatest-alma.ms"
00079 
00080         if os.path.exists(self.vis):
00081             print "The MS is already around, just unflag"
00082         else:
00083             print "Moving data..."
00084             os.system('cp -r '+datapath + self.vis +' '+ self.vis)
00085 
00086         os.system('rm -rf ' + self.vis + '.flagversions')
00087         flagdata(vis=self.vis, mode='unflag', savepars=False)
00088         default(flagcmd)
00089 
00090         
00091     def setUp_evla(self):
00092         self.vis = "tosr0001_scan3_noonline.ms"
00093 
00094         if os.path.exists(self.vis):
00095             print "The MS is already around, just unflag"
00096         else:
00097             print "Moving data..."
00098             os.system('cp -r '+datapath + self.vis +' '+ self.vis)
00099 
00100         os.system('rm -rf ' + self.vis + '.flagversions')
00101         flagdata(vis=self.vis, mode='unflag', savepars=False)
00102         default(flagcmd)
00103         
00104     def setUp_shadowdata(self):
00105         self.vis = "shadowtest_part.ms"
00106 
00107         if os.path.exists(self.vis):
00108             print "The MS is already around, just unflag"
00109         else:
00110             print "Moving data..."
00111             os.system('cp -r '+datapath + self.vis +' '+ self.vis)
00112 
00113         os.system('rm -rf ' + self.vis + '.flagversions')
00114         flagdata(vis=self.vis, mode='unflag', savepars=False)
00115         default(flagcmd)
00116         
00117     def setUp_data4rflag(self):
00118         self.vis = "Four_ants_3C286.ms"
00119 
00120         if os.path.exists(self.vis):
00121             print "The MS is already around, just unflag"
00122         else:
00123             print "Moving data..."
00124             os.system('cp -r '+datapath + self.vis +' '+ self.vis)
00125 
00126         os.system('rm -rf ' + self.vis + '.flagversions')
00127         flagdata(vis=self.vis, mode='unflag', savepars=False)
00128         default(flagcmd)
00129 
00130     def setUp_bpass_case(self):
00131         self.vis = "cal.fewscans.bpass"
00132 
00133         if os.path.exists(self.vis):
00134             print "The CalTable is already around, just unflag"
00135         else:
00136             print "Moving data..."
00137             os.system('cp -r ' + \
00138                         os.environ.get('CASAPATH').split()[0] +
00139                         "/data/regression/unittest/flagdata/" + self.vis + ' ' + self.vis)
00140 
00141         os.system('rm -rf ' + self.vis + '.flagversions')
00142         flagdata(vis=self.vis, mode='unflag', savepars=False)
00143         default(flagcmd)
00144         
00145 class test_manual(test_base):
00146     '''Test manual selections'''
00147     
00148     def setUp(self):
00149         self.setUp_multi()
00150         
00151     def test_observation(self):
00152         input = "observation='1'"
00153         filename = create_input(input)
00154         
00155         flagcmd(vis=self.vis, inpmode='list', inpfile=filename, action='apply', savepars=False)
00156         test_eq(flagdata(vis=self.vis, mode='summary'), 2882778, 28500)
00157 
00158     def test_compatibility(self):
00159         input = "observation='1' mode='manualflag'"
00160         filename = create_input(input)
00161         
00162         flagcmd(vis=self.vis, inpmode='list', inpfile=filename, action='apply', savepars=False)
00163         test_eq(flagdata(vis=self.vis, mode='summary'), 2882778, 28500)
00164         
00165     def test_autocorr(self):
00166         '''flagcmd: autocorr=True'''
00167         self.setUp_ngc5921()
00168         flagcmd(vis=self.vis, inpmode='list', inpfile=['autocorr=True'], action='apply')
00169         res = flagdata(vis=self.vis, mode='summary')
00170         self.assertEqual(res['flagged'], 203994, 'Should flag only the auto-correlations')
00171         
00172 
00173 class test_alma(test_base):
00174     # Test various selections for alma data 
00175 
00176     def setUp(self):
00177         self.setUp_flagdatatest_alma()
00178     
00179     def test_intent(self):
00180         '''flagcmd: test scan intent selection'''
00181         
00182         input = "intent='CAL*POINT*'\n"\
00183                 "#scan=3,4"
00184         filename = create_input(input)
00185         
00186         # flag POINTING CALIBRATION scans and ignore comment line
00187         flagcmd(vis=self.vis, inpmode='list', inpfile=filename, action='apply', savepars=False)
00188         test_eq(flagdata(vis=self.vis,mode='summary', antenna='2'), 377280, 26200)
00189         res = flagdata(vis=self.vis,mode='summary')
00190         self.assertEqual(res['scan']['1']['flagged'], 80184, 'Only scan 1 should be flagged')
00191         self.assertEqual(res['scan']['4']['flagged'], 0, 'Scan 4 should not be flagged')
00192         
00193     def test_cmd(self):
00194         '''flagcmd: inpmode=list with empty parameter'''
00195         
00196         # Test the correct parsing with empty parameter such as antenna=''
00197         flagcmd(vis=self.vis, inpmode='list', 
00198                  inpfile=["intent='CAL*POINT*' field=''","scan='3,4' antenna=''","scan='5'"], 
00199                  action='apply', savepars=False)
00200         res = flagdata(vis=self.vis,mode='summary')
00201         self.assertEqual(res['scan']['1']['flagged'], 80184)
00202         self.assertEqual(res['scan']['4']['flagged'], 48132)
00203         self.assertEqual(res['flagged'], 160392)
00204         
00205              
00206     def test_extract(self):
00207         '''flagcmd: action = extract and apply clip on WVR'''
00208         # Remove any cmd from table
00209         flagcmd(vis=self.vis, action='clear', clearall=True)
00210         
00211         # Save cmd to FLAG_CMD
00212         cmd = "mode='clip' clipminmax=[0,50] correlation='ABS_WVR'"
00213         flagcmd(vis=self.vis, inpmode='list', inpfile=[cmd], action='list', savepars=True)
00214         
00215         # Extract it
00216         res = flagcmd(vis=self.vis, action='extract', useapplied=True)
00217         
00218         # Apply to clip only WVR
00219         flagcmd(vis=self.vis, inpmode='list', inpfile=[res[0]['command']], savepars=False, action='apply')
00220         ret = flagdata(vis=self.vis, mode='summary')
00221         self.assertEqual(ret['flagged'], 22752)
00222         self.assertEqual(ret['correlation']['I']['flagged'], 22752)
00223         self.assertEqual(ret['correlation']['XX']['flagged'], 0)
00224         self.assertEqual(ret['correlation']['YY']['flagged'], 0)
00225                 
00226                 
00227 class test_unapply(test_base):
00228     # Action unapply
00229     def setUp(self):
00230         self.setUp_ngc5921()
00231 
00232     def test_utfcrop(self):
00233         '''flagcmd: unapply tfcrop agent'''
00234         # Remove any cmd from table
00235         flagcmd(vis=self.vis, action='clear', clearall=True)
00236 
00237         # Flag using manual agent
00238         input = "scan=1"
00239         filename = create_input(input)
00240         flagcmd(vis=self.vis, inpmode='list', inpfile=filename, action='apply', savepars=True)
00241         
00242         # Flag using tfcrop agent from file
00243         # Note : For this test, scan=4 gives identical flags on 32/64 bit machines,
00244         #           and one flag difference on a Mac (32)
00245         #           Other scans give differences at the 0.005% level.
00246         input = "scan=4 mode=tfcrop correlation='ABS_RR'"
00247         filename = create_input(input)
00248         flagcmd(vis=self.vis, inpmode='list', inpfile=filename, action='apply', savepars=True)
00249         res = flagdata(vis=self.vis,mode='summary')
00250         self.assertEqual(res['scan']['1']['flagged'], 568134, 'Whole scan=1 should be flagged')
00251         #self.assertEqual(res['scan']['4']['flagged'], 1201, 'scan=4 should be partially flagged')
00252         self.assertTrue(res['scan']['4']['flagged']>= 1200 and res['scan']['4']['flagged']<= 1204, 'scan=4 should be partially flagged')
00253         
00254         # Unapply only the tfcrop line
00255         flagcmd(vis=self.vis, action='unapply', useapplied=True, tablerows=1, savepars=False)
00256         result = flagdata(vis=self.vis,mode='summary',scan='4')
00257         self.assertEqual(result['flagged'], 0, 'Expected 0 flags, found %s'%result['flagged'])
00258         self.assertEqual(result['total'], 95256,'Expected total 95256, found %s'%result['total'])
00259 
00260     def test_uquack(self):
00261         '''flagcmd: unapply quack agent'''
00262         # Remove any cmd from table
00263         flagcmd(vis=self.vis, action='clear', clearall=True)
00264 
00265         # Flag using manual agent
00266         input = "scan=1"
00267         filename = create_input(input)
00268         flagcmd(vis=self.vis, inpmode='list', inpfile=filename, action='apply', savepars=True)
00269 
00270         # Flag using the quack agent
00271         input = "scan=1~3 mode=quack quackinterval=1.0"
00272         filename = create_input(input)
00273         flagcmd(vis=self.vis, inpmode='list', inpfile=filename, action='apply', savepars=True)
00274         
00275         # Unapply only the quack line
00276         flagcmd(vis=self.vis, action='unapply', useapplied=True, tablerows=1, savepars=True)
00277         result = flagdata(vis=self.vis,mode='summary',scan='1')
00278         
00279         # Only the manual flags should be there
00280         self.assertEqual(result['flagged'], 568134, 'Expected 568134 flags, found %s'%result['flagged'])
00281         self.assertEqual(result['total'], 568134,'Expected total 568134, found %s'%result['total'])
00282         
00283     def test_umanualflag(self):
00284         '''flagcmd: unapply manual agent'''
00285         # Remove any cmd from table
00286         flagcmd(vis=self.vis, action='clear', clearall=True)
00287 
00288         # Flag using manual agent
00289         input = "scan=1"
00290         filename = create_input(input)
00291         flagcmd(vis=self.vis, inpmode='list', inpfile=filename, action='apply', savepars=True)
00292 
00293         # Flag using the quack agent
00294         input = "scan=1~3 mode=quack quackinterval=1.0"
00295         filename = create_input(input)
00296         flagcmd(vis=self.vis, inpmode='list', inpfile=filename, action='apply', savepars=True)
00297         
00298         # Unapply only the manual line
00299         flagcmd(vis=self.vis, action='unapply', useapplied=True, tablerows=0, savepars=False)
00300         result = flagdata(vis=self.vis,mode='summary',scan='1')
00301         
00302         # Only the quack flags should be left
00303         self.assertEqual(result['flagged'], 44226, 'Expected 44226 flags, found %s'%result['flagged'])
00304         self.assertEqual(result['total'], 568134,'Expected total 568134, found %s'%result['total'])
00305         
00306 
00307     def test_uscans(self):
00308         '''flagcmd: Unapply only APPLIED=True'''
00309         # Remove any cmd from table
00310         flagcmd(vis=self.vis, action='clear', clearall=True)
00311         
00312         # Flag several scans and save them to FLAG_CMD with APPLIED=True
00313         flagdata(vis=self.vis, scan='7', savepars=True)
00314         flagdata(vis=self.vis, scan='1', savepars=True)
00315         flagdata(vis=self.vis, scan='2', savepars=True)
00316         flagdata(vis=self.vis, scan='3', savepars=True)
00317         flagdata(vis=self.vis, scan='4', savepars=True)
00318         
00319         # There should be 5 cmds in FLAG_CMD. Unapply row=1 and set APPLIED to False
00320         flagcmd(vis=self.vis, action='unapply', tablerows=1, savepars=False)
00321         
00322         # Unapply scans 2 and 3 only. It should not re-apply scan=1 (row 1)
00323         flagcmd(vis=self.vis, action='unapply', tablerows=[2,3], savepars=False)
00324         
00325         # We should have left only scans 4 and 7 flagged.
00326         res = flagdata(vis=self.vis, mode='summary')
00327         self.assertEqual(res['scan']['1']['flagged'], 0, "It should not re-apply tablerows=1")
00328         self.assertEqual(res['scan']['4']['flagged'], 95256, "It should not unapply tablerows=4")
00329         self.assertEqual(res['scan']['7']['flagged'], 190512, "It should not unapply tablerows=7")
00330         self.assertEqual(res['flagged'], 285768)
00331 
00332 class test_savepars(test_base):
00333     # Action unapply
00334     def setUp(self):
00335         self.setUp_ngc5921()
00336 
00337     def test_list1(self):
00338         '''flagcmd: list and savepars=True/False'''
00339         # Remove any cmd from table
00340         flagcmd(vis=self.vis, action='clear', clearall=True)
00341         
00342         ########## TEST 1 
00343         # create text file called flagcmd.txt
00344         input = "scan=4 mode=clip correlation=ABS_RR clipminmax=[0,4]\n"
00345         filename = create_input(input)
00346         filename1 = 'filename1.txt'
00347         os.system('cp '+filename+' '+filename1)
00348 
00349         # save command to MS
00350         flagcmd(vis=self.vis, action='list', inpmode='list', inpfile=[input], savepars=True)
00351         
00352         # list/save to a file
00353         os.system('rm -rf myflags.txt')
00354         flagcmd(vis=self.vis, action='list', outfile='myflags.txt', savepars=True)
00355         
00356         # compare saved file with original input file
00357         self.assertTrue(filecmp.cmp(filename1, 'myflags.txt', 1), 'Files should be equal')
00358         
00359         ########## TEST 2 
00360         # create another input
00361         input = "scan=1~3 mode=manual\n"
00362         filename = create_input(input)
00363         
00364         # apply and don't save to MS
00365         flagcmd(vis=self.vis, inpmode='list', inpfile=filename, action='apply', savepars=False)
00366         
00367         # list and check that parameters were not saved to MS
00368         os.system('rm -rf myflags.txt')
00369         flagcmd(vis=self.vis, action='list', outfile='myflags.txt', savepars=True)
00370         self.assertFalse(filecmp.cmp(filename, 'myflags.txt', 1), 'Files should not be equal')
00371         
00372         ########### TEST 3 
00373         # apply cmd from TEST 1 and update APPLIED column
00374         flagcmd(vis=self.vis, action='apply', savepars=False)
00375         
00376         # scans=1~3 should be fully flagged
00377         res = flagdata(vis=self.vis, mode='summary')
00378         self.assertEqual(res['scan']['1']['flagged'], 568134)
00379         self.assertEqual(res['scan']['1']['total'], 568134)
00380         self.assertEqual(res['scan']['2']['flagged'], 238140)
00381         self.assertEqual(res['scan']['2']['total'], 238140)
00382         self.assertEqual(res['scan']['3']['flagged'], 762048)
00383         self.assertEqual(res['scan']['3']['total'], 762048)
00384         # scan 4 should be partially flagged by clip mode
00385         self.assertEqual(res['scan']['4']['flagged'], 3348, 'Only RR should be flagged')
00386         self.assertEqual(res['scan']['4']['total'], 95256)
00387         
00388         # Only cmd form TEST 1 should be in MS
00389         os.system('rm -rf myflags.txt')
00390         flagcmd(vis=self.vis, action='list', outfile='myflags.txt', useapplied=True, savepars=True)
00391         self.assertTrue(filecmp.cmp(filename1, 'myflags.txt', 1), 'Files should be equal')        
00392 
00393 
00394 class test_XML(test_base):
00395     
00396     def setUp(self):
00397         self.setUp_evla()
00398         
00399     def test_xml1(self):
00400         '''flagcmd: list xml file and save in outfile'''
00401         
00402         # The MS only contains clip and shadow commands
00403         # The XML contain the online flags
00404         flagcmd(vis=self.vis, action='list', inpmode='xml', savepars=True, outfile='origxml.txt')
00405         
00406         # Now save the online flags to the FLAG_CMD without applying
00407         flagcmd(vis=self.vis, action='list', inpmode='xml', savepars=True)
00408         
00409         # Now apply them by selecting the reasons and save in another file
00410         # 507 cmds crash on my computer.
00411         reasons = ['ANTENNA_NOT_ON_SOURCE','FOCUS_ERROR','SUBREFLECTOR_ERROR']
00412         flagcmd(vis=self.vis, action='apply', reason=reasons, savepars=True, outfile='myxml.txt')
00413                 
00414         # Compare with original XML
00415         self.assertTrue(filecmp.cmp('origxml.txt', 'myxml.txt',1), 'Files should be equal')
00416         
00417         # Check that APPLIED column has been updated to TRUE
00418         
00419         
00420     def test_xml2(self):
00421         '''flagcmd: list xml file and save in outfile'''
00422         
00423         # The MS only contains clip and shadow commands
00424         
00425         # Apply the shadow command
00426         flagcmd(vis=self.vis, action='apply', reason='SHADOW')
00427         res = flagdata(vis=self.vis, mode='summary')
00428         self.assertEqual(res['flagged'], 240640)
00429         
00430     def test_missing_corr(self):
00431         '''flagcmd: CAS-4234, non-existing correlation raise no error'''
00432         flagcmd(vis=self.vis, action='clear', clearall=True)
00433         
00434         flagcmd(vis=self.vis, inpmode='list', inpfile=["correlation='XX,RR,RL'"], action='list',
00435                 savepars=True)
00436         
00437         flagcmd(vis=self.vis, action='apply')
00438         
00439         res = flagdata(vis=self.vis, mode='summary')
00440         
00441         self.assertEqual(res['flagged'], 208000+208000, 'Should only flag RR and RL and not fail')
00442 
00443 class test_shadow(test_base):
00444     def setUp(self):
00445         self.setUp_shadowdata()
00446 
00447     def test_CAS2399(self):
00448         '''flagcmd: shadow by antennas not present in MS'''
00449         
00450         # Create antennafile in disk
00451         input = 'name=VLA01\n'+\
00452                 'diameter=25.0\n'+\
00453                 'position=[-1601144.96146691, -5041998.01971858, 3554864.76811967]\n'+\
00454                 'name=VLA02\n'+\
00455                 'diameter=25.0\n'+\
00456                 'position=[-1601105.7664601889, -5042022.3917835914, 3554847.245159178]\n'+\
00457                 'name=VLA09\n'+\
00458                 'diameter=25.0\n'+\
00459                 'position=[-1601197.2182404203, -5041974.3604805721, 3554875.1995636248]\n'+\
00460                 'name=VLA10\n'+\
00461                 'diameter=25.0\n'+\
00462                 'position=[-1601227.3367843349,-5041975.7011900628,3554859.1642644769]\n'            
00463 
00464 #        antfile = 'myants.txt'
00465 #        if os.path.exists(antfile):
00466 #            os.system('rm -rf myants.txt')
00467 
00468         filename = create_input(input)
00469 
00470         # Create command line
00471         input = ["mode='shadow' tolerance=10.0 addantenna='flagcmd.txt'"]
00472 #        filename = 'cmdfile.txt'
00473 #        if os.path.exists(filename):
00474 #            os.system('rm -rf cmdfile.txt')
00475         
00476 #        create_input(input, filename)
00477         
00478         # Flag
00479         flagcmd(vis=self.vis, action='clear', clearall=True)
00480 #        flagcmd(vis=self.vis, action='apply', inpmode='list', inpfile=filename)
00481         flagcmd(vis=self.vis, action='apply', inpmode='list', inpfile=input)
00482         
00483         # Check flags
00484         res = flagdata(vis=self.vis, mode='summary')
00485         self.assertEqual(res['antenna']['VLA18']['flagged'], 3364)
00486         self.assertEqual(res['antenna']['VLA19']['flagged'], 1124)
00487         self.assertEqual(res['antenna']['VLA20']['flagged'], 440)        
00488 
00489 
00490 # Test rflag inputs with filenames, as well as inline thresholds.
00491 # rflag does not generate output thresholds for action='apply'.
00492 class test_rflag(test_base):
00493 
00494     def setUp(self):
00495         self.setUp_data4rflag()
00496 
00497     def test_rflaginputs(self):
00498         """flagcmd:: Test of rflag threshold-inputs of both types (file and inline)
00499         """
00500 
00501         # (1) and (2) are the same as test_flagdata[test_rflag3].
00502         # -- (3),(4),(5) should produce the same answers as (1) and (2)
00503         #
00504         # (1) Test input/output files, through the task, mode='rflag'
00505         #flagdata(vis=self.vis, mode='rflag', spw='9,10', timedev='tdevfile.txt', \
00506         #              freqdev='fdevfile.txt', action='calculate');
00507         #flagdata(vis=self.vis, mode='rflag', spw='9,10', timedev='tdevfile.txt', \
00508         #              freqdev='fdevfile.txt', action='apply');
00509         #res1 = flagdata(vis=self.vis, mode='summary')
00510         #print "(1) Finished flagdata : test 1 : ", res1['flagged']
00511 
00512 
00513         # (2) Test rflag output written to cmd file via mode='rflag' and 'savepars' 
00514         #      and then read back in via list mode. 
00515         #      Also test the 'savepars' when timedev and freqdev are specified differently...
00516         #flagdata(vis=self.vis,mode='unflag');
00517         #flagdata(vis=self.vis, mode='rflag', spw='9,10', timedev='', \
00518         #              freqdev=[],action='calculate',savepars=True,outfile='outcmd.txt');
00519         #os.system('cat outcmd.txt')
00520         #flagdata(vis=self.vis, mode='list', inpfile='outcmd.txt');
00521         #res2 = flagdata(vis=self.vis, mode='summary')
00522         #print "(2) Finished flagdata : test 2 : ", res2['flagged']
00523 
00524 
00525         # (3) flagcmd : Send in the same text files produces/used in (1)
00526         input1 = "{'name':'Rflag','timedev':[[1.0,9.0,0.038859],[1.0,10.0,0.162833]]}\n"
00527         input2 = "{'name':'Rflag','freqdev':[[1.0,9.0,0.079151],[1.0,10.0,0.205693]]}\n"
00528         filename1 = create_input(input1,'tdevfile.txt')
00529         filename2 = create_input(input2,'fdevfile.txt')
00530 
00531         commlist=['mode=rflag spw=9,10 timedev='+filename1+' freqdev='+filename2]
00532 
00533         flagdata(vis=self.vis,mode='unflag');
00534         flagcmd(vis=self.vis, inpmode='list', inpfile=commlist, action='apply')
00535         res3 = flagdata(vis=self.vis, mode='summary')
00536         print "(3) Finished flagcmd test : using tdevfile, fdevfile in the cmd (test 1)) : ", res3['flagged']
00537 
00538 
00539         # (4) Give the values directly in the cmd input.....
00540         commlist=['mode=rflag spw=9,10 timedev=[[1.0,9.0,0.038859],[1.0,10.0,0.162833]] \
00541                           freqdev=[[1.0,9.0,0.079151],[1.0,10.0,0.205693]]']
00542 
00543         flagdata(vis=self.vis,mode='unflag');
00544         flagcmd(vis=self.vis, inpmode='list', inpfile=commlist, action='apply')
00545         res4 = flagdata(vis=self.vis, mode='summary')
00546 
00547         print "(4) Finished flagcmd test : using cmd arrays : ", res4['flagged']
00548 
00549 
00550         # (5) Use the outcmd.txt file generated by (2). 
00551         #       i.e. re-run the threshold-generation of (2) with savepars=True
00552         flagdata(vis=self.vis,mode='unflag');
00553         flagdata(vis=self.vis, mode='rflag', spw='9,10', timedev='', \
00554                       freqdev=[],action='calculate',savepars=True,outfile='outcmd.txt');
00555         flagcmd(vis=self.vis, inpmode='list', inpfile='outcmd.txt');
00556         res5 = flagdata(vis=self.vis, mode='summary')
00557         print "(5) Finished flagcmd test : using outcmd.txt from flagdata (test 2) : ", res5['flagged']
00558 
00559         self.assertEqual(res3['flagged'],res4['flagged']);
00560         self.assertEqual(res3['flagged'],res5['flagged']);
00561         self.assertEqual(res3['flagged'], 39504.0)
00562 
00563     def test_rflagauto(self):
00564         """flagcmd:: Test of rflag with defaults
00565         """
00566         # (6) flagcmd AUTO. Should give same answers as test_flagdata[test_rflag1]
00567         flagdata(vis=self.vis,mode='unflag');
00568         flagcmd(vis=self.vis, inpmode='list', inpfile=['mode=rflag spw=9,10'], action='apply')
00569         res6 = flagdata(vis=self.vis, mode='summary')
00570         print "(6) Finished flagcmd test : auto : ", res6['flagged']
00571 
00572         #(7) flagdata AUTO (same as test_flagdata[test_rflag1])
00573         #flagdata(vis=self.vis,mode='unflag');
00574         #flagdata(vis=self.vis, mode='rflag', spw='9,10');
00575         #res7 = flagdata(vis=self.vis, mode='summary')
00576         #print "\n---------------------- Finished flagdata test : auto : ", res7['flagged']
00577 
00578         self.assertEqual(res6['flagged'], 42728.0)
00579 
00580 
00581 class test_actions(test_base):
00582     
00583     def setUp(self):
00584         self.setUp_data4rflag()
00585         
00586     def tearDown(self):
00587         if os.path.exists('fourplot.png'):
00588             os.remove('fourplot.png')
00589         
00590     def test_plot(self):
00591         '''flagcmd: Test action=plot'''
00592         outplot = 'fourplot.png'
00593         flagcmd(vis=self.vis, inpmode='list', 
00594             inpfile=["intent='CAL*POINT*' field=''","scan='5'"], 
00595             action='list', savepars=True)
00596         
00597         flagcmd(vis=self.vis, inpmode='table', useapplied=True, action='plot',
00598                 plotfile=outplot)
00599         
00600         self.assertTrue(os.path.exists(outplot),'Plot file was not created')
00601 
00602         
00603     def test_list1(self):
00604          '''flagcmd: action=list with inpmode from a list'''
00605          flagcmd(vis=self.vis, action='clear', clearall=True)         
00606          cmd = ["spw='5~7'","spw='1'"]
00607          flagcmd(vis=self.vis, action='list', inpmode='list', inpfile=cmd, savepars=True)
00608          
00609          # Apply the flags
00610          flagcmd(vis=self.vis)
00611          
00612          res=flagdata(vis=self.vis, mode='summary')
00613          self.assertEqual(res['flagged'],1099776)  
00614         
00615     def test_list2(self):
00616          '''flagcmd: action=list with inpmode from a file'''
00617          flagcmd(vis=self.vis, action='clear', clearall=True)         
00618          cmd = "spw='5~7'\n"+\
00619                 "spw='1'"
00620          filename = create_input(cmd)         
00621          flagcmd(vis=self.vis, action='list', inpmode='list', inpfile=filename, savepars=True)
00622 
00623          # Apply the flags
00624          flagcmd(vis=self.vis)
00625          
00626          res=flagdata(vis=self.vis, mode='summary')
00627          self.assertEqual(res['flagged'],1099776)  
00628         
00629 class test_cmdbandpass(test_base):
00630     """Flagcmd:: Test flagging task with Bpass-based CalTable """
00631     
00632     def setUp(self):
00633         self.setUp_bpass_case()
00634 
00635     def test_unsupported_mode_in_list(self):
00636         '''Flagcmd: elevation and shadow are not supported in cal tables'''
00637         res = flagcmd(vis=self.vis, inpmode='list', inpfile=["mode='elevation'",
00638                                                              "spw='1'"])
00639         
00640         res = flagdata(vis=self.vis, mode='summary')
00641         self.assertEqual(res['spw']['1']['flagged'], 83200)
00642 
00643     def test_default_cparam(self):
00644         '''Flagcmd: flag CPARAM as the default column'''
00645         flagcmd(vis=self.vis, inpmode='list', inpfile=["mode='clip' clipzeros=True"])
00646         res = flagdata(vis=self.vis, mode='summary')
00647         self.assertEqual(res['flagged'], 9950, 'Should use CPARAM as the default column')
00648         
00649     def test_manual_field_selection_for_bpass(self):
00650         """Flagcmd:: Manually flag a bpass-based CalTable using field selection"""
00651         
00652         flagcmd(vis=self.vis, inpmode='list', inpfile=["field='3C286_A'"])
00653         summary=flagdata(vis=self.vis, mode='summary')
00654         
00655         self.assertEqual(summary['field']['3C286_A']['flagged'], 499200.0)
00656         self.assertEqual(summary['field']['3C286_B']['flagged'], 0)
00657         self.assertEqual(summary['field']['3C286_C']['flagged'], 0)
00658         self.assertEqual(summary['field']['3C286_D']['flagged'], 0)
00659 
00660     def test_list_field_Selection_for_bpass(self):
00661         """Flagcmd:: Manually flag a bpass-based CalTable using file in list mode """
00662         
00663         input = "field='3C286_A'"
00664         filename = create_input(input)
00665 
00666         flagcmd(vis=self.vis, inpmode='list', inpfile=filename)
00667         summary=flagdata(vis=self.vis, mode='summary')
00668         self.assertEqual(summary['field']['3C286_A']['flagged'], 499200.0)
00669         self.assertEqual(summary['field']['3C286_B']['flagged'], 0)
00670         self.assertEqual(summary['field']['3C286_C']['flagged'], 0)
00671         self.assertEqual(summary['field']['3C286_D']['flagged'], 0)
00672 
00673     def test_MS_flagcmds(self):
00674         """Flagcmd:: Save flags to MS and apply to cal table"""
00675         self.setUp_data4rflag()
00676         msfile = self.vis
00677         flagcmd(vis=msfile, action='clear', clearall=True)
00678         flagdata(vis=msfile, antenna='ea09', action='', savepars=True)
00679         
00680         self.setUp_bpass_case()
00681         flagcmd(vis=self.vis, inpfile=msfile, action='apply')
00682         summary=flagdata(vis=self.vis, mode='summary')
00683         self.assertEqual(summary['antenna']['ea09']['flagged'], 48000.0)
00684         self.assertEqual(summary['antenna']['ea10']['flagged'], 0.0)
00685         
00686     def test_clip_one_list(self):
00687         '''Flagcmd: Flag one solution using one command in a list'''
00688         flagcmd(vis=self.vis, inpmode='list', inpfile=["mode='clip' clipminmax=[0,3] correlation='REAL_Sol1'"])
00689         res = flagdata(vis=self.vis, mode='summary')
00690         self.assertEqual(res['flagged'], 309388)
00691         self.assertEqual(res['correlation']['Sol2']['flagged'], 0)
00692                 
00693     def test_flagbackup(self):
00694         '''Flagcmd: backup cal table flags'''
00695         # Create a local copy of the tool
00696         aflocal = casac.agentflagger()
00697         flagmanager(vis=self.vis, mode='list')
00698         aflocal.open(self.vis)
00699         self.assertEqual(len(aflocal.getflagversionlist()), 3)
00700         aflocal.done()
00701 
00702         flagcmd(vis=self.vis, inpmode='list', inpfile=["spw='3'"])
00703         flagmanager(vis=self.vis, mode='list')
00704         aflocal.open(self.vis)
00705         self.assertEqual(len(aflocal.getflagversionlist()), 4)
00706         aflocal.done()
00707 
00708         flagcmd(vis=self.vis, inpmode='list', inpfile=["spw='4'"], flagbackup=False)
00709         flagmanager(vis=self.vis, mode='list')
00710         aflocal.open(self.vis)
00711         self.assertEqual(len(aflocal.getflagversionlist()), 4)
00712         aflocal.done()
00713         
00714         newname = 'BackupBeforeSpwFlags'
00715 
00716         flagmanager(vis=self.vis, mode='rename', oldname='flagcmd_1', versionname=newname, 
00717                     comment='Backup of flags before applying flags on spw')
00718         flagmanager(vis=self.vis, mode='list')
00719         aflocal.open(self.vis)
00720         self.assertEqual(len(aflocal.getflagversionlist()), 4)
00721         aflocal.done()
00722         
00723         # Apply spw=5 flags
00724         flagcmd(vis=self.vis, inpmode='list', inpfile=["spw='5'"], flagbackup=True)
00725         res = flagdata(vis=self.vis, mode='summary')
00726         self.assertEqual(res['spw']['3']['flagged'], 83200)
00727         self.assertEqual(res['spw']['4']['flagged'], 83200)
00728         self.assertEqual(res['spw']['5']['flagged'], 83200)
00729         self.assertEqual(res['flagged'], 83200*3)
00730         
00731         # Restore backup
00732         flagmanager(vis=self.vis, mode='restore', versionname='BackupBeforeSpwFlags', merge='replace')
00733         res = flagdata(vis=self.vis, mode='summary')
00734         self.assertEqual(res['spw']['5']['flagged'], 0)
00735         self.assertEqual(res['flagged'], 0)
00736 
00737     def test_save_cal(self):
00738         '''Flagcmd: list flag cmds from MS and save to a file'''
00739         self.setUp_data4rflag()
00740         msfile = self.vis
00741         flagcmd(vis=msfile, action='clear', clearall=True)
00742         
00743         # Save cmds to FLAG_CMD table
00744         flagcmd(vis=msfile, inpmode='list', inpfile=["spw='1,3",
00745                                                      "mode='clip' clipminmax=[0,500] datacolumn='SNR'"], 
00746                 action='list', savepars=True)
00747         
00748         self.setUp_bpass_case()
00749         flagcmds = 'calflags.txt'
00750         if os.path.exists(flagcmds):
00751             os.system('rm -rf '+flagcmds)
00752             
00753         # Apply to cal table and save to an external file
00754         flagcmd(vis=self.vis, inpmode='table', inpfile=msfile, savepars=True, outfile=flagcmds)
00755         self.assertTrue(os.path.exists(flagcmds))
00756         res = flagdata(vis=self.vis, mode='summary')
00757         self.assertEqual(res['flagged'], 246315)
00758         
00759         # Apply from list in file and compare
00760         flagdata(vis=self.vis, mode='unflag')
00761         flagcmd(vis=self.vis, inpmode='list', inpfile=flagcmds, action='apply')
00762         res = flagdata(vis=self.vis, mode='summary')
00763         self.assertEqual(res['flagged'], 246315)
00764 
00765         
00766 # Dummy class which cleans up created files
00767 class cleanup(test_base):
00768     
00769     def tearDown(self):
00770         os.system('rm -rf multiobs.ms*')
00771         os.system('rm -rf flagdatatest-alma.ms*')
00772         os.system('rm -rf ngc5921.ms*')
00773         os.system('rm -rf Four_ants*.ms*')
00774         os.system('rm -rf shadowtest*.ms*')
00775         os.system('rm -rf tosr0001_scan3*.ms*')
00776         os.system('rm -rf cal.fewscans.bpass*')
00777 
00778 
00779     def test1(self):
00780         '''flagcmd: Cleanup'''
00781         pass
00782 
00783 
00784 def suite():
00785     return [test_manual,
00786             test_alma,
00787             test_unapply,
00788             test_savepars,
00789             test_XML,
00790             test_shadow,
00791             test_rflag,
00792             test_actions,
00793             test_cmdbandpass,
00794             cleanup]
00795         
00796         
00797         
00798         
00799