casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
test_agentflagger.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 class test_base(unittest.TestCase):
00011 
00012     def setUp_bpass_case(self):
00013         self.vis = "cal.fewscans.bpass"
00014 
00015         if os.path.exists(self.vis):
00016             print "The CalTable is already around, just unflag"
00017         else:
00018             print "Moving data..."
00019             os.system('cp -r ' + \
00020                         os.environ.get('CASAPATH').split()[0] +
00021                         "/data/regression/unittest/flagdata/" + self.vis + ' ' + self.vis)
00022 
00023         os.system('rm -rf ' + self.vis + '.flagversions')
00024         
00025         self.unflag_table()
00026 
00027 
00028     def setUp_tsys_case(self):
00029         self.vis = "X7ef.tsys"
00030          
00031         if os.path.exists(self.vis):
00032             print "The CalTable is already around, just unflag"
00033             
00034         else:
00035             print "Moving data..."
00036             os.system('cp -r ' + \
00037                         os.environ.get('CASAPATH').split()[0] +
00038                         "/data/regression/unittest/flagdata/" + self.vis + ' ' + self.vis)
00039 
00040         os.system('rm -rf ' + self.vis + '.flagversions')
00041 
00042         self.unflag_table()
00043 
00044 
00045     def setUp_4Ants(self):
00046         self.vis = "Four_ants_3C286.ms"
00047 
00048         if os.path.exists(self.vis):
00049             print "The MS is already around, just unflag"
00050         else:
00051             print "Moving data..."
00052             os.system('cp -r ' + \
00053                          os.environ.get('CASAPATH').split()[0] +
00054                         "/data/regression/unittest/flagdata/" + self.vis + ' ' + self.vis)
00055 
00056         os.system('rm -rf ' + self.vis + '.flagversions')
00057 
00058         self.unflag_table()
00059 
00060 
00061     def setUp_CAS_4052(self):
00062         self.vis = "TwoSpw.ms"
00063 
00064         if os.path.exists(self.vis):
00065             print "The MS is already around, just unflag"
00066         else:
00067             print "Moving data..."
00068             os.system('cp -r ' + \
00069                         os.environ.get('CASAPATH').split()[0] +
00070                         "/data/regression/unittest/flagdata/" + self.vis + ' ' + self.vis)
00071 
00072         os.system('rm -rf ' + self.vis + '.flagversions')
00073 
00074         self.unflag_table()
00075 
00076 
00077     def unflag_table(self):
00078 
00079         aflocal = casac.agentflagger()
00080         aflocal.open(self.vis)
00081         aflocal.selectdata()
00082         agentUnflag={'apply':True,'mode':'unflag'}
00083         aflocal.parseagentparameters(agentUnflag)
00084         aflocal.init()
00085         aflocal.run(writeflags=True)
00086         aflocal.done()
00087 
00088 
00089 class test_tsys(test_base):
00090     """AgentFlagger:: Test flagging tool with Tsys-based CalTable """
00091     
00092     def setUp(self):
00093          self.setUp_tsys_case()
00094 
00095     def test_unsupported_elevation_tsys(self):
00096         '''AgentFlagger: Unsupported elevation mode'''
00097         aflocal = casac.agentflagger()
00098         aflocal.open(self.vis)
00099         aflocal.selectdata()
00100         aflocal.parseelevationparameters()
00101         aflocal.init()
00102         res = aflocal.run(writeflags=True)
00103         aflocal.done() 
00104         self.assertEqual(res, {})
00105 
00106     def test_mixed_agents_tsys(self):
00107         '''AgentFlagger: supported and unsupported agents in a list'''
00108         aflocal = casac.agentflagger()
00109         aflocal.open(self.vis)
00110         aflocal.selectdata()
00111         aflocal.parsemanualparameters(spw='1')
00112         aflocal.parseagentparameters({'mode':'shadow','spw':'3'}) #unsupported mode
00113         aflocal.parseelevationparameters()     # unsupported mode
00114         aflocal.parsemanualparameters(spw='5')
00115         aflocal.parsesummaryparameters(spw='1,3,5')
00116         aflocal.init()
00117         res = aflocal.run(writeflags=True)
00118         aflocal.done() 
00119         self.assertEqual(res['report0']['spw']['1']['flagged'], 32256)
00120         self.assertEqual(res['report0']['spw']['3']['flagged'], 0)
00121         self.assertEqual(res['report0']['spw']['5']['flagged'], 32256)
00122         self.assertEqual(res['report0']['flagged'], 32256*2)
00123         
00124     def test_manual_field_selection_agent_layer_for_tsys_CalTable(self):
00125         """AgentFlagger:: Manually flag a Tsys-based CalTable using flag agent selection engine for field """
00126         aflocal = casac.agentflagger()
00127         aflocal.open(self.vis)
00128         aflocal.selectdata()
00129         agentUnflag={'apply':True,'mode':'unflag'}
00130         agentManual={'apply':True,'mode':'manual','field':'0'}
00131         agentSummary={'apply':True,'mode':'summary'}
00132         aflocal.parseagentparameters(agentUnflag)
00133         aflocal.parseagentparameters(agentManual)
00134         aflocal.parseagentparameters(agentSummary)
00135         aflocal.init()
00136         summary = aflocal.run(writeflags=True)
00137         aflocal.done() 
00138 
00139         self.assertEqual(summary['report0']['field']['3c279']['flagged'], 9216)
00140         self.assertEqual(summary['report0']['field']['Titan']['flagged'], 0)
00141         self.assertEqual(summary['report0']['field']['TW Hya']['flagged'], 0)
00142         self.assertEqual(summary['report0']['field']['J1037-295=QSO']['flagged'], 0)
00143 
00144     def test_manual_antenna_selection_agent_layer_for_tsys_CalTable(self):
00145         """AgentFlagger:: Manually flag a Tsys-based CalTable using flag agent selection engine for antenna"""
00146         aflocal = casac.agentflagger()
00147         aflocal.open(self.vis)
00148         aflocal.selectdata()
00149         agentUnflag={'apply':True,'mode':'unflag'}
00150         agentManual={'apply':True,'mode':'manual','antenna':'DV09'}
00151         agentSummary={'apply':True,'mode':'summary'}
00152         aflocal.parseagentparameters(agentUnflag)
00153         aflocal.parseagentparameters(agentManual)
00154         aflocal.parseagentparameters(agentSummary)
00155         aflocal.init()
00156         summary = aflocal.run(writeflags=True)
00157         aflocal.done() 
00158 
00159         self.assertEqual(summary['report0']['antenna']['DV09']['flagged'], 14336)
00160         self.assertEqual(summary['report0']['antenna']['DV10']['flagged'], 0)
00161 
00162     def test_manual_antenna_selection_agent_layer_for_tsys_CalTable2(self):
00163         """AgentFlagger:: Manually flag a Tsys-based CalTable using flag agent selection engine for antenna"""
00164 
00165         # Run the previous test but using the specific parsing functions
00166         aflocal = casac.agentflagger()
00167         aflocal.open(self.vis)
00168         aflocal.selectdata()
00169         aflocal.parsemanualparameters(apply=False)
00170         aflocal.parsemanualparameters(antenna='DV09')
00171         aflocal.parsesummaryparameters()
00172         aflocal.init()
00173         summary = aflocal.run(writeflags=True)
00174         aflocal.done() 
00175 
00176         self.assertEqual(summary['report0']['antenna']['DV09']['flagged'], 14336)
00177         self.assertEqual(summary['report0']['antenna']['DV10']['flagged'], 0)
00178 
00179     def test_manual_field_msSelection_layer_for_tsys_CalTable(self):
00180         """AgentFlagger:: Manually flag a Tsys-based CalTable using MSSelection for field """
00181         aflocal = casac.agentflagger()
00182 
00183         aflocal.open(self.vis)
00184         aflocal.selectdata(field='0')
00185         agentUnflag={'apply':True,'mode':'unflag'}
00186         agentManual={'apply':True,'mode':'manual'}
00187         aflocal.parseagentparameters(agentUnflag)
00188         aflocal.parseagentparameters(agentManual)
00189         aflocal.init()
00190         summary = aflocal.run(writeflags=True)
00191         aflocal.done() 
00192 
00193         aflocal.open(self.vis)
00194         aflocal.selectdata()
00195         agentSummary={'apply':True,'mode':'summary'}
00196         aflocal.parseagentparameters(agentSummary)
00197         aflocal.init()
00198         summary = aflocal.run(writeflags=True)
00199         aflocal.done() 
00200 
00201         self.assertEqual(summary['report0']['field']['3c279']['flagged'], 9216.0)
00202         self.assertEqual(summary['report0']['field']['Titan']['flagged'], 0)
00203         self.assertEqual(summary['report0']['field']['TW Hya']['flagged'], 0)
00204         self.assertEqual(summary['report0']['field']['J1037-295=QSO']['flagged'], 0)        
00205 
00206     def test_manual_antenna_msSelection_layer_for_tsys_CalTable(self):
00207         """AgentFlagger:: Manually flag a Tsys-based CalTable using MSSelection for antenna"""
00208         aflocal = casac.agentflagger()
00209 
00210         aflocal.open(self.vis)
00211         aflocal.selectdata(antenna='DV09')
00212         agentUnflag={'apply':True,'mode':'unflag'}
00213         agentManual={'apply':True,'mode':'manual'}
00214         aflocal.parseagentparameters(agentUnflag)
00215         aflocal.parseagentparameters(agentManual)
00216         aflocal.init()
00217         summary = aflocal.run(writeflags=True)
00218         aflocal.done() 
00219 
00220         aflocal.open(self.vis)
00221         aflocal.selectdata()
00222         agentSummary={'apply':True,'mode':'summary'}
00223         aflocal.parseagentparameters(agentSummary)
00224         aflocal.init()
00225         summary = aflocal.run(writeflags=True)
00226         aflocal.done() 
00227 
00228         self.assertEqual(summary['report0']['antenna']['DV09']['flagged'], 14336)
00229         self.assertEqual(summary['report0']['antenna']['DV10']['flagged'], 0)
00230         
00231     def test_clip_minmax_fparm_sol1(self):
00232         """AgentFlagger:: Test cliping first calibration solution product of FPARAM column using a minmax range """
00233         aflocal = casac.agentflagger()
00234         datacolumn = 'FPARAM'
00235         correlation = 'Sol1'
00236         aflocal.open(self.vis)
00237         aflocal.selectdata()
00238         agentUnflag={'apply':True,'mode':'unflag'}
00239         agentClip={'apply':True,'mode':'clip','clipzeros':True,'clipminmax':[0.,600.],'datacolumn':datacolumn,'correlation':correlation}
00240         agentSummary={'apply':True,'mode':'summary'}
00241         aflocal.parseagentparameters(agentUnflag)
00242         aflocal.parseagentparameters(agentClip)
00243         aflocal.parseagentparameters(agentSummary)
00244         aflocal.init()
00245         summary = aflocal.run(writeflags=True)
00246         aflocal.done() 
00247 
00248         self.assertEqual(summary['report0']['total'], 129024.0)
00249         self.assertEqual(summary['report0']['flagged'], 750.0)
00250         self.assertEqual(summary['report0']['correlation']['Sol1']['flagged'], 750.0)
00251         self.assertEqual(summary['report0']['correlation']['Sol1']['total'], 64512.0)
00252         self.assertEqual(summary['report0']['correlation']['Sol2']['flagged'], 0.0)
00253         self.assertEqual(summary['report0']['correlation']['Sol2']['total'], 64512.0)
00254 
00255     def test_clip_minmax_fparm_sol1_2(self):
00256         """AgentFlagger:: Test cliping first calibration solution product of FPARAM column using a minmax range """
00257 
00258         # Run the previous test but using the specific parsing functions
00259         aflocal = casac.agentflagger()
00260         datacolumn = 'FPARAM'
00261         correlation = 'Sol1'
00262         aflocal.open(self.vis)
00263         aflocal.selectdata()
00264         aflocal.parsemanualparameters(apply=False)
00265         aflocal.parseclipparameters(clipzeros=True,clipminmax=[0.,600.],datacolumn=datacolumn,
00266                                      correlation=correlation)
00267         aflocal.parsesummaryparameters()
00268         aflocal.init()
00269         summary = aflocal.run(writeflags=True)
00270         aflocal.done() 
00271         self.assertEqual(summary['report0']['total'], 129024)
00272         self.assertEqual(summary['report0']['flagged'], 750)
00273         self.assertEqual(summary['report0']['correlation']['Sol1']['flagged'], 750)
00274         self.assertEqual(summary['report0']['correlation']['Sol1']['total'], 64512)
00275         self.assertEqual(summary['report0']['correlation']['Sol2']['flagged'], 0)
00276         self.assertEqual(summary['report0']['correlation']['Sol2']['total'], 64512)
00277 
00278     def test_clip_minmax_fparm_sol1_extension(self):
00279         """AgentFlagger:: Test cliping first calibration solution product of FPARAM column using a minmax range, and then extend to the other solution """
00280         aflocal = casac.agentflagger()
00281         datacolumn = 'FPARAM'
00282         aflocal.open(self.vis)
00283         aflocal.selectdata()
00284         agentUnflag={'apply':True,'mode':'unflag'}
00285         agentClip={'apply':True,'mode':'clip','clipzeros':True,'clipminmax':[0.,600.],'datacolumn':datacolumn,'correlation':'Sol1'}
00286         agentExtension={'apply':True,'mode':'extend','extendpols':True,'growfreq':0.0, 'growtime':0.0}
00287         agentSummary={'apply':True,'mode':'summary','correlation':'""'}
00288         aflocal.parseagentparameters(agentUnflag)
00289         aflocal.parseagentparameters(agentClip)
00290         aflocal.parseagentparameters(agentExtension)
00291         aflocal.parseagentparameters(agentSummary)
00292         aflocal.init()
00293         summary = aflocal.run(writeflags=True)
00294         aflocal.done() 
00295 
00296         self.assertEqual(summary['report0']['total'], 129024)
00297         self.assertEqual(summary['report0']['flagged'], 1500)
00298         self.assertEqual(summary['report0']['correlation']['Sol1']['flagged'], 750)
00299         self.assertEqual(summary['report0']['correlation']['Sol2']['flagged'], 750)
00300 
00301     def test_clip_minmax_fparm_sol2(self):
00302         """AgentFlagger:: Test cliping second calibration solution product of FPARAM column using a minmax range """
00303         aflocal = casac.agentflagger()
00304         datacolumn = 'FPARAM'
00305         correlation = 'Sol2'
00306         aflocal.open(self.vis)
00307         aflocal.selectdata()
00308         agentUnflag={'apply':True,'mode':'unflag'}
00309         agentClip={'apply':True,'mode':'clip','clipzeros':True,'clipminmax':[0.,600.],'datacolumn':datacolumn,'correlation':correlation}
00310         agentSummary={'apply':True,'mode':'summary'}
00311         aflocal.parseagentparameters(agentUnflag)
00312         aflocal.parseagentparameters(agentClip)
00313         aflocal.parseagentparameters(agentSummary)
00314         aflocal.init()
00315         summary = aflocal.run(writeflags=True)
00316         aflocal.done() 
00317 
00318         self.assertEqual(summary['report0']['total'], 129024.0)
00319         self.assertEqual(summary['report0']['flagged'], 442.0)
00320         self.assertEqual(summary['report0']['correlation']['Sol1']['flagged'], 0.0)
00321         self.assertEqual(summary['report0']['correlation']['Sol1']['total'], 64512.0)
00322         self.assertEqual(summary['report0']['correlation']['Sol2']['flagged'], 442.0)
00323         self.assertEqual(summary['report0']['correlation']['Sol2']['total'], 64512.0)
00324 
00325     def test_clip_minmax_fparm_sol1sol2(self):
00326         """AgentFlagger:: Test cliping first and second calibration solution products of FPARAM column using a minmax range """
00327         aflocal = casac.agentflagger()
00328         datacolumn = 'FPARAM'
00329         correlation = 'Sol1,Sol2'
00330         aflocal.open(self.vis)
00331         aflocal.selectdata()
00332         agentUnflag={'apply':True,'mode':'unflag'}
00333         agentClip={'apply':True,'mode':'clip','clipzeros':True,'clipminmax':[0.,600.],'datacolumn':datacolumn,'correlation':correlation}
00334         agentSummary={'apply':True,'mode':'summary'}
00335         aflocal.parseagentparameters(agentUnflag)
00336         aflocal.parseagentparameters(agentClip)
00337         aflocal.parseagentparameters(agentSummary)
00338         aflocal.init()
00339         summary = aflocal.run(writeflags=True)
00340         aflocal.done() 
00341 
00342         self.assertEqual(summary['report0']['total'], 129024.0)
00343         self.assertEqual(summary['report0']['flagged'], 1192.0)
00344         self.assertEqual(summary['report0']['correlation']['Sol1']['flagged'], 750.0)
00345         self.assertEqual(summary['report0']['correlation']['Sol1']['total'], 64512.0)
00346         self.assertEqual(summary['report0']['correlation']['Sol2']['flagged'], 442.0)
00347         self.assertEqual(summary['report0']['correlation']['Sol2']['total'], 64512.0)
00348 
00349     def test_clip_minmax_fparm_all(self):
00350         """AgentFlagger:: Test cliping all calibration solution products of FPARAM column using a minmax range """
00351         aflocal = casac.agentflagger()
00352         datacolumn = 'FPARAM'
00353         correlation = ''
00354         aflocal.open(self.vis)
00355         aflocal.selectdata()
00356         agentUnflag={'apply':True,'mode':'unflag'}
00357         agentClip={'apply':True,'mode':'clip','clipzeros':True,'clipminmax':[0.,600.],'datacolumn':datacolumn,'correlation':correlation}
00358         agentSummary={'apply':True,'mode':'summary'}
00359         aflocal.parseagentparameters(agentUnflag)
00360         aflocal.parseagentparameters(agentClip)
00361         aflocal.parseagentparameters(agentSummary)
00362         aflocal.init()
00363         summary = aflocal.run(writeflags=True)
00364         aflocal.done() 
00365 
00366         self.assertEqual(summary['report0']['total'], 129024.0)
00367         self.assertEqual(summary['report0']['flagged'], 1192.0)
00368         self.assertEqual(summary['report0']['correlation']['Sol1']['flagged'], 750.0)
00369         self.assertEqual(summary['report0']['correlation']['Sol1']['total'], 64512.0)
00370         self.assertEqual(summary['report0']['correlation']['Sol2']['flagged'], 442.0)
00371         self.assertEqual(summary['report0']['correlation']['Sol2']['total'], 64512.0)
00372 
00373     def test_clip_zeros_fparm_all(self):
00374         """AgentFlagger:: Test cliping only zeros in all calibration solution products of FPARAM column"""
00375         aflocal = casac.agentflagger()
00376         datacolumn = 'FPARAM'
00377         correlation = ''
00378         aflocal.open(self.vis)
00379         aflocal.selectdata()
00380         agentUnflag={'apply':True,'mode':'unflag'}
00381         agentClip={'apply':True,'mode':'clip','clipzeros':True,'datacolumn':datacolumn,'correlation':correlation}
00382         agentSummary={'apply':True,'mode':'summary'}
00383         aflocal.parseagentparameters(agentUnflag)
00384         aflocal.parseagentparameters(agentClip)
00385         aflocal.parseagentparameters(agentSummary)
00386         aflocal.init()
00387         summary = aflocal.run(writeflags=True)
00388         aflocal.done() 
00389 
00390         self.assertEqual(summary['report0']['total'], 129024.0)
00391         self.assertEqual(summary['report0']['flagged'], 0.0)
00392         self.assertEqual(summary['report0']['correlation']['Sol1']['flagged'], 0.0)
00393         self.assertEqual(summary['report0']['correlation']['Sol1']['total'], 64512.0)
00394         self.assertEqual(summary['report0']['correlation']['Sol2']['flagged'], 0.0)
00395         self.assertEqual(summary['report0']['correlation']['Sol2']['total'], 64512.0)
00396 
00397     def test_clip_nan_and_inf_fparm_all(self):
00398         """AgentFlagger:: Test cliping only NaNs/Infs in all calibration solution products of FPARAM column"""
00399         aflocal = casac.agentflagger()
00400         datacolumn = 'FPARAM'
00401         correlation = ''
00402         aflocal.open(self.vis)
00403         aflocal.selectdata()
00404         agentUnflag={'apply':True,'mode':'unflag'}
00405         agentClip={'apply':True,'mode':'clip','datacolumn':datacolumn,'correlation':correlation}
00406         agentSummary={'apply':True,'mode':'summary'}
00407         aflocal.parseagentparameters(agentUnflag)
00408         aflocal.parseagentparameters(agentClip)
00409         aflocal.parseagentparameters(agentSummary)
00410         aflocal.init()
00411         summary = aflocal.run(writeflags=True)
00412         aflocal.done() 
00413 
00414         self.assertEqual(summary['report0']['total'], 129024.0)
00415         self.assertEqual(summary['report0']['flagged'], 0.0)
00416         self.assertEqual(summary['report0']['correlation']['Sol1']['flagged'], 0.0)
00417         self.assertEqual(summary['report0']['correlation']['Sol1']['total'], 64512.0)
00418         self.assertEqual(summary['report0']['correlation']['Sol2']['flagged'], 0.0)
00419         self.assertEqual(summary['report0']['correlation']['Sol2']['total'], 64512.0)
00420 
00421     def test_clip_minmax_fparm_error_case_absall(self):
00422         """AgentFlagger:: Error case test when a complex operator is used with CalTables """
00423         aflocal = casac.agentflagger()
00424         datacolumn = 'FPARAM'
00425         correlation = 'ABS_ALL'
00426         aflocal.open(self.vis)
00427         aflocal.selectdata()
00428         agentUnflag={'apply':True,'mode':'unflag'}
00429         agentClip={'apply':True,'mode':'clip','clipzeros':True,'clipminmax':[0.,600.],'datacolumn':datacolumn,'correlation':correlation}
00430         agentSummary={'apply':True,'mode':'summary'}
00431         aflocal.parseagentparameters(agentUnflag)
00432         aflocal.parseagentparameters(agentClip)
00433         aflocal.parseagentparameters(agentSummary)
00434         aflocal.init()
00435         summary = aflocal.run(writeflags=True)
00436         aflocal.done() 
00437 
00438         self.assertEqual(summary['report0']['total'], 129024.0)
00439         self.assertEqual(summary['report0']['flagged'], 1192.0)
00440         self.assertEqual(summary['report0']['correlation']['Sol1']['flagged'], 750.0)
00441         self.assertEqual(summary['report0']['correlation']['Sol1']['total'], 64512.0)
00442         self.assertEqual(summary['report0']['correlation']['Sol2']['flagged'], 442.0)
00443         self.assertEqual(summary['report0']['correlation']['Sol2']['total'], 64512.0)
00444 
00445  
00446     def test_clip_minmax_snr_all_for_tsys_CalTable(self):
00447         """AgentFlagger:: Test cliping all calibration solution products of SNR column using a minmax range for Tsys CalTable"""
00448         aflocal = casac.agentflagger()
00449         datacolumn = 'SNR'
00450         correlation = ''
00451         aflocal.open(self.vis)
00452         aflocal.selectdata()
00453         agentUnflag={'apply':True,'mode':'unflag'}
00454         agentClip={'apply':True,'mode':'clip','clipzeros':True,'clipminmax':[0.,2.],'datacolumn':datacolumn,'correlation':correlation}
00455         agentSummary={'apply':True,'mode':'summary'}
00456         aflocal.parseagentparameters(agentUnflag)
00457         aflocal.parseagentparameters(agentClip)
00458         aflocal.parseagentparameters(agentSummary)
00459         aflocal.init()
00460         summary = aflocal.run(writeflags=True)
00461         aflocal.done() 
00462 
00463         self.assertEqual(summary['report0']['total'], 129024.0)
00464         self.assertEqual(summary['report0']['flagged'], 0.0)
00465         self.assertEqual(summary['report0']['correlation']['Sol1']['flagged'], 0.0)
00466         self.assertEqual(summary['report0']['correlation']['Sol1']['total'], 64512.0)
00467         self.assertEqual(summary['report0']['correlation']['Sol2']['flagged'], 0.0)
00468         self.assertEqual(summary['report0']['correlation']['Sol2']['total'], 64512.0)
00469 
00470 
00471 class test_bpass(test_base):
00472     """AgentFlagger:: Test flagging tool with Bpass-based CalTable """
00473     
00474     def setUp(self):
00475         self.setUp_bpass_case()
00476 
00477     def test_default_cparam_bpass(self):
00478         '''Flagdata: flag CPARAM as the default column'''
00479         aflocal = casac.agentflagger()
00480         aflocal.open(self.vis)
00481         aflocal.selectdata()
00482         aflocal.parseclipparameters(clipzeros=True)
00483         aflocal.parsesummaryparameters()
00484         aflocal.init()
00485         summary = aflocal.run(writeflags=True)
00486         aflocal.done() 
00487         self.assertEqual(summary['report0']['flagged'], 9950, 'Should use CPARAM as the default column')
00488 
00489     def test_manual_field_selection_agent_layer_for_bpass_CalTable(self):
00490         """AgentFlagger:: Manually flag a bpass-based CalTable using flag agent selection engine for field """
00491         aflocal = casac.agentflagger()
00492         aflocal.open(self.vis)
00493         aflocal.selectdata()
00494         agentUnflag={'apply':True,'mode':'unflag'}
00495         agentManual={'apply':True,'mode':'manual','field':'3C286_A'}
00496         agentSummary={'apply':True,'mode':'summary'}
00497         aflocal.parseagentparameters(agentUnflag)
00498         aflocal.parseagentparameters(agentManual)
00499         aflocal.parseagentparameters(agentSummary)
00500         aflocal.init()
00501         summary = aflocal.run(writeflags=True)
00502         aflocal.done() 
00503 
00504         self.assertEqual(summary['report0']['field']['3C286_A']['flagged'], 499200.0)
00505         self.assertEqual(summary['report0']['field']['3C286_B']['flagged'], 0)
00506         self.assertEqual(summary['report0']['field']['3C286_C']['flagged'], 0)
00507         self.assertEqual(summary['report0']['field']['3C286_D']['flagged'], 0)
00508 
00509     def test_manual_antenna_selection_agent_layer_for_bpass_CalTable(self):
00510         """AgentFlagger:: Manually flag a bpass-based CalTable using flag agent selection engine for antenna"""
00511         aflocal = casac.agentflagger()
00512         aflocal.open(self.vis)
00513         aflocal.selectdata()
00514         agentUnflag={'apply':True,'mode':'unflag'}
00515         agentManual={'apply':True,'mode':'manual','antenna':'ea09'}
00516         agentSummary={'apply':True,'mode':'summary'}
00517         aflocal.parseagentparameters(agentUnflag)
00518         aflocal.parseagentparameters(agentManual)
00519         aflocal.parseagentparameters(agentSummary)
00520         aflocal.init()
00521         summary = aflocal.run(writeflags=True)
00522         aflocal.done() 
00523 
00524         self.assertEqual(summary['report0']['antenna']['ea09']['flagged'], 48000.0)
00525         self.assertEqual(summary['report0']['antenna']['ea10']['flagged'], 0.0)
00526 
00527     def test_manual_field_msSelection_layer_for_bpass_CalTable(self):
00528         """AgentFlagger:: Manually flag a bpass-based CalTable using MSSelection for field """
00529         aflocal = casac.agentflagger()
00530 
00531         aflocal.open(self.vis)
00532         aflocal.selectdata({'field':'3C286_A'})
00533         agentUnflag={'apply':True,'mode':'unflag'}
00534         agentManual={'apply':True,'mode':'manual'}
00535         aflocal.parseagentparameters(agentUnflag)
00536         aflocal.parseagentparameters(agentManual)
00537         aflocal.init()
00538         summary = aflocal.run(writeflags=True)
00539         aflocal.done() 
00540 
00541         aflocal.open(self.vis)
00542         aflocal.selectdata()
00543         agentSummary={'apply':True,'mode':'summary'}
00544         aflocal.parseagentparameters(agentSummary)
00545         aflocal.init()
00546         summary = aflocal.run(writeflags=True)
00547         aflocal.done() 
00548 
00549         self.assertEqual(summary['report0']['field']['3C286_A']['flagged'], 499200.0)
00550         self.assertEqual(summary['report0']['field']['3C286_B']['flagged'], 0)
00551         self.assertEqual(summary['report0']['field']['3C286_C']['flagged'], 0)
00552         self.assertEqual(summary['report0']['field']['3C286_D']['flagged'], 0)
00553 
00554     def test_manual_antenna_msSelection_layer_for_bpass_CalTable(self):
00555         """AgentFlagger:: Manually flag a bpass-based CalTable using MSSelection for antenna"""
00556         aflocal = casac.agentflagger()
00557 
00558         aflocal.open(self.vis)
00559         aflocal.selectdata({'antenna':'ea09'})
00560         agentUnflag={'apply':True,'mode':'unflag'}
00561         agentManual={'apply':True,'mode':'manual'}
00562         aflocal.parseagentparameters(agentUnflag)
00563         aflocal.parseagentparameters(agentManual)
00564         aflocal.init()
00565         summary = aflocal.run(writeflags=True)
00566         aflocal.done() 
00567 
00568         aflocal.open(self.vis)
00569         aflocal.selectdata()
00570         agentSummary={'apply':True,'mode':'summary'}
00571         aflocal.parseagentparameters(agentSummary)
00572         aflocal.init()
00573         summary = aflocal.run(writeflags=True)
00574         aflocal.done() 
00575 
00576         self.assertEqual(summary['report0']['antenna']['ea09']['flagged'], 48000.0)
00577         self.assertEqual(summary['report0']['antenna']['ea10']['flagged'], 0.0)
00578 
00579     def test_clip_minmax_cparam_for_bpass(self):
00580         """AgentFlagger:: Clip all calibration solutions of CPARAM column using a minmax range"""
00581         aflocal = casac.agentflagger()
00582         datacolumn = 'CPARAM'
00583         aflocal.open(self.vis)
00584         aflocal.selectdata()
00585         aflocal.parseclipparameters(clipminmax=[0.,0.3],datacolumn=datacolumn,clipzeros=True)
00586         aflocal.parsesummaryparameters()
00587         aflocal.init()
00588         summary = aflocal.run(writeflags=True)
00589         aflocal.done() 
00590         self.assertEqual(summary['report0']['flagged'], 10047)
00591         self.assertEqual(summary['report0']['total'], 1248000)
00592         self.assertEqual(summary['report0']['correlation']['Sol1']['flagged'], 10008)
00593         self.assertEqual(summary['report0']['correlation']['Sol2']['flagged'], 39)
00594         
00595     def test_clip_minmax_snr_all_for_bpass_CalTable(self):
00596         """AgentFlagger:: Test cliping all calibration solution products of SNR column using a minmax range for bpass CalTable"""
00597         aflocal = casac.agentflagger()
00598         datacolumn = 'SNR'
00599         correlation = ''
00600         aflocal.open(self.vis)
00601         aflocal.selectdata()
00602         agentUnflag={'apply':True,'mode':'unflag'}
00603         agentClip={'apply':True,'mode':'clip','clipzeros':True,'clipminmax':[0.,550.],'datacolumn':datacolumn,'correlation':correlation}
00604         agentSummary={'apply':True,'mode':'summary'}
00605         aflocal.parseagentparameters(agentUnflag)
00606         aflocal.parseagentparameters(agentClip)
00607         aflocal.parseagentparameters(agentSummary)
00608         aflocal.init()
00609         summary = aflocal.run(writeflags=True)
00610         aflocal.done() 
00611 
00612         self.assertEqual(summary['report0']['total'], 1248000.0)
00613         self.assertEqual(summary['report0']['flagged'], 73174.0)
00614         self.assertEqual(summary['report0']['correlation']['Sol1']['flagged'], 35130.0)
00615         self.assertEqual(summary['report0']['correlation']['Sol1']['total'], 624000.0)
00616         self.assertEqual(summary['report0']['correlation']['Sol2']['flagged'], 38044.0)
00617         self.assertEqual(summary['report0']['correlation']['Sol2']['total'], 624000.0)
00618 
00619     def test_rflag_cparam_sol2(self):
00620         """AgentFlagger:: Test rflag solution 2 of CPARAM column for bpass"""
00621         aflocal = casac.agentflagger()
00622         correlation = 'Sol2'
00623         aflocal.open(self.vis)
00624         aflocal.selectdata()
00625         agentRflag={'apply':True,'mode':'rflag','correlation':correlation}
00626         agentSummary={'apply':True,'mode':'summary'}
00627         aflocal.parseagentparameters(agentRflag)
00628         aflocal.parseagentparameters(agentSummary)
00629         aflocal.init()
00630         summary = aflocal.run(writeflags=True)
00631         aflocal.done() 
00632         self.assertEqual(summary['report0']['flagged'], 13197)
00633         self.assertEqual(summary['report0']['correlation']['Sol1']['flagged'], 0)
00634         self.assertEqual(summary['report0']['correlation']['Sol2']['flagged'], 13197)
00635 
00636     def test_tfcrop_cparam_sol1_extension(self):
00637         """AgentFlagger:: Test tfcrop first calibration solution product of CPARAM column, 
00638         and then extend to the other solution for bpass CalTable"""
00639         aflocal = casac.agentflagger()
00640         datacolumn = "CPARAM"
00641         correlation = 'Sol1'
00642         aflocal.open(self.vis)
00643         aflocal.selectdata()
00644         
00645         # Pre-clip data to avoid problems with near-zero values
00646         agentClip={'apply':True,'mode':'clip','clipzeros':True,'datacolumn':datacolumn,'correlation':correlation}
00647         aflocal.parseagentparameters(agentClip)
00648         
00649         aflocal.parsetfcropparameters(datacolumn=datacolumn, correlation=correlation)
00650         aflocal.parsesummaryparameters()
00651         # Extend to the other solution
00652         aflocal.parseextendparameters(extendpols=True,growfreq=0.0,growtime=0.0)
00653         aflocal.parsesummaryparameters()
00654         aflocal.init()
00655         summary = aflocal.run(writeflags=True)
00656         aflocal.done()
00657         
00658         # flags from first summary, only tfcrop       
00659         assert abs(summary['report0']['flagged'] - 29299) <= 5        
00660         assert abs(summary['report0']['correlation']['Sol1']['flagged'] - 29299) <= 5
00661         
00662         # flags from second summary, tfcrop+extend
00663         assert abs(summary['report2']['flagged'] - 2*29299) <= 10
00664         assert abs(summary['report2']['correlation']['Sol2']['flagged'] - 29299) <= 5        
00665 
00666 #class test_MS(test_base):
00667 #
00668 #    def setUP(self):
00669 #        self.setUp_4Ants()
00670 #   
00671 #    def test_catchNullSel(self):
00672 #        '''AgentFlagger: Catch null selections'''
00673 #        aflocal = casac.agentflagger()
00674 #        aflocal.open(self.vis)
00675 #        aflocal.selectdata(scan='1', catchnullsel=True)
00676 #        self.assertFalse(aflocal.parsemanualparameters())
00677 #        self.assertFalse(aflocal.init())
00678 #        res = aflocal.run()
00679 #        aflocal.done()
00680 #        self.assertTrue(res == {})
00681 #        
00682 #    def test_donotcatchNullSel(self):
00683 #        '''AgentFlagger: do not catch null selections'''
00684 #        
00685     
00686 class test_display(test_base):
00687     """AgentFlagger:: Automatic test to check basic behaviour of display GUI using pause=False option """
00688 
00689     def test_display_data_single_channel_selection(self):
00690         """AgentFlagger:: Check nominal behaviour for single spw:chan selection """
00691         self.setUp_4Ants()
00692         aflocal = casac.agentflagger()
00693         aflocal.open(self.vis)
00694         aflocal.selectdata()
00695         agentUnflag={'apply':True,'mode':'unflag'}
00696         agentManual={'apply':True,'mode':'manual','spw':'*:20~40'}
00697         agentSummary={'apply':True,'mode':'summary'}
00698         agentDisplay={'mode':'display','datadisplay':True,'pause':False}
00699         aflocal.parseagentparameters(agentManual)
00700         aflocal.parseagentparameters(agentSummary)
00701         aflocal.parseagentparameters(agentDisplay)
00702         aflocal.init()
00703         summary = aflocal.run(writeflags=True)
00704         aflocal.done()
00705 
00706     def test_display_data_multiple_channel_selection(self):
00707         """AgentFlagger:: Check behaviour for multiple spw:chan selection """
00708         self.setUp_4Ants()
00709         aflocal = casac.agentflagger()
00710         aflocal.open(self.vis)
00711         aflocal.selectdata()
00712         agentUnflag={'apply':True,'mode':'unflag'}
00713         agentManual={'apply':True,'mode':'manual','spw':'*:10~20;30~40'}
00714         agentSummary={'apply':True,'mode':'summary'}
00715         agentDisplay={'mode':'display','datadisplay':True,'pause':False}
00716         aflocal.parseagentparameters(agentUnflag)
00717         aflocal.parseagentparameters(agentManual)
00718         aflocal.parseagentparameters(agentSummary)
00719         aflocal.parseagentparameters(agentDisplay)
00720         aflocal.init()
00721         summary = aflocal.run(writeflags=True)
00722         aflocal.done()
00723 
00724     def test_display_data_different_corrs_per_spw(self):
00725         """AgentFlagger:: Check behaviour when the number of correlation products changes between SPWs """
00726         self.setUp_CAS_4052()
00727         aflocal = casac.agentflagger()
00728         aflocal.open(self.vis)
00729         aflocal.selectdata()
00730         agentUnflag={'apply':True,'mode':'unflag'}
00731         agentManual={'apply':True,'mode':'manual','spw':'*:100~200;300~400'}
00732         agentSummary={'apply':True,'mode':'summary'}
00733         agentDisplay={'mode':'display','datadisplay':True,'pause':False}
00734         aflocal.parseagentparameters(agentUnflag)
00735         aflocal.parseagentparameters(agentManual)
00736         aflocal.parseagentparameters(agentSummary)
00737         aflocal.parseagentparameters(agentDisplay)
00738         aflocal.init()
00739         summary = aflocal.run(writeflags=True)
00740         aflocal.done()
00741 
00742     def test_display_cal_tables(self):
00743         '''AgentFlagger: Select spws, display and flag cal tables'''
00744         self.setUp_bpass_case()
00745         aflocal = casac.agentflagger()
00746         aflocal.open(self.vis)
00747         aflocal.selectdata(spw='1')
00748         aflocal.parsemanualparameters(spw='1:0~10;60~63')
00749         aflocal.parsesummaryparameters(spwchan=True)
00750 #        agentManual={'apply':True,'mode':'manual'}
00751 #        agentSummary={'apply':True,'mode':'summary','spwchan':True}
00752         agentDisplay={'mode':'display','datadisplay':True,'pause':False}
00753 #        aflocal.parseagentparameters(agentManual)
00754 #        aflocal.parseagentparameters(agentSummary)
00755         aflocal.parseagentparameters(agentDisplay)
00756         aflocal.init()
00757         summary = aflocal.run(writeflags=True)
00758         aflocal.done()
00759         
00760         self.assertEqual(summary['report0']['spw:channel']['1:0']['flagged'], 1300)
00761         self.assertEqual(summary['report0']['spw:channel']['1:5']['flagged'], 1300)
00762         self.assertEqual(summary['report0']['spw:channel']['1:10']['flagged'], 1300)
00763         self.assertEqual(summary['report0']['spw:channel']['1:11']['flagged'], 0)
00764         self.assertEqual(summary['report0']['spw:channel']['1:60']['flagged'], 1300)
00765         self.assertEqual(summary['report0']['spw:channel']['1:59']['flagged'], 0)
00766         self.assertEqual(summary['report0']['flagged'], 15*1300)
00767 
00768 
00769 # Dummy class which cleans up created files
00770 class cleanup(test_base):
00771             
00772     def tearDown(self):
00773         os.system('rm -rf cal.fewscans.bpass*')
00774         os.system('rm -rf X7ef.tsys*')
00775         os.system('rm -rf Four_ants_3C286.ms*')
00776         os.system('rm -rf TwoSpw.ms*')
00777 
00778     def test1(self):
00779         '''AgentFlagger: Cleanup'''
00780         pass
00781 
00782 
00783 def suite():
00784     return [test_tsys,
00785             test_bpass,
00786             test_display,
00787             cleanup]