casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
alma_asdm+reimported-asdm_sf.py
Go to the documentation of this file.
00001 #############################################################################
00002 # $Id:$
00003 # Test Name:                                                                #
00004 #    Regression Test Script for ASDM version 1 import to MS                 #
00005 #    and the "inverse filler" task exportasdm, and subsequent               #
00006 #    data analysis.                                                         #
00007 #                                                                           # 
00008 # Rationale for Inclusion:                                                  #
00009 #    The conversion of ASDM to MS and back needs to be verified.            #
00010 #                                                                           # 
00011 # Features tested:                                                          #
00012 #    1) Is the import performed without raising exceptions?                 #
00013 #    2) Do all expected tables exist?                                       #
00014 #    3) Can the MS be opened?                                               #
00015 #    4) Do the tables contain expected values?                              #
00016 #    5) Is exportasdm performed without raising exceptions?                 #
00017 #    6) Is the created ASDM well-formed (XML) and complete?                 #
00018 #    7) Can the resulting ASDM be reimported without raising exceptions?    #
00019 #    8) Does it have the same number of integrations as the original?       #
00020 #    9) Does an imported ASDM pass a serious analysis?                      #
00021 #   10) After exporting the MS generated in (9) to an ASDM, does the        #
00022 #       re-imported MS pass the same analysis with the same results?        #
00023 #                                                                           #
00024 # Input data:                                                               #
00025 #     two datasets for the filler of ASDM 1                                 #
00026 #     one WVR correction cal table                                          #
00027 #     one simulated MS dataset                                              #
00028 #                                                                           #
00029 #############################################################################
00030 
00031 myname = 'alma_asdm+reimported-asdm_sf'
00032 
00033 # default ASDM dataset name
00034 myasdm_dataset_name = 'uid___X5f_X18951_X1'
00035 myms_dataset_name = 'M51.ms'
00036 myasdm_dataset2_name = 'uid___X02_X56142_X1'
00037 mywvr_correction_file = 'N3256_B6_0.WVR'
00038 
00039 # get the dataset name from the wrapper if possible
00040 mydict = locals()
00041 if mydict.has_key("asdm_dataset_name"):
00042     myasdm_dataset_name = mydict["asdm_dataset_name"]
00043 if mydict.has_key("ms_dataset_name"):
00044     myms_dataset_name = mydict["ms_dataset_name"]
00045 if mydict.has_key("asdm_dataset2_name"):
00046     myasdm_dataset_name = mydict["asdm_dataset2_name"]
00047 if mydict.has_key("wvr_dataset_name"):
00048     mywvr_correction_file = mydict["wvr_correction_file"]
00049 
00050 # name of the resulting MS
00051 msname = myasdm_dataset_name+'.ms'
00052 
00053 # name of the exported ASDM
00054 asdmname = myms_dataset_name+'.asdm'
00055 
00056 # name of the reimported MS
00057 reimp_msname = 'reimported-'+myms_dataset_name
00058 
00059 def checktable(thename, theexpectation):
00060     global msname, myname
00061     tb.open(msname+"/"+thename)
00062     if thename == "":
00063         thename = "MAIN"
00064     for mycell in theexpectation:
00065         print myname, ": comparing ", mycell
00066         value = tb.getcell(mycell[0], mycell[1])
00067         # see if value is array
00068         try:
00069             isarray = value.__len__
00070         except:
00071             # it's not an array
00072             # zero tolerance?
00073             if mycell[3] == 0:
00074                 in_agreement = (value == mycell[2])
00075             else:
00076                 in_agreement = ( abs(value - mycell[2]) < mycell[3]) 
00077         else:
00078             # it's an array
00079             # zero tolerance?
00080             if mycell[3] == 0:
00081                 in_agreement =  (value == mycell[2]).all() 
00082             else:
00083                 in_agreement = (abs(value - mycell[2]) < mycell[3]).all() 
00084         if not in_agreement:
00085             print myname, ":  Error in MS subtable", thename, ":"
00086             print "     column ", mycell[0], " row ", mycell[1], " contains ", value
00087             print "     expected value is ", mycell[2]
00088             tb.close()
00089             raise
00090     tb.close()
00091     print myname, ": table ", thename, " as expected."
00092     return
00093 
00094 #########################
00095 
00096 def verify_asdm(asdmname, withPointing):
00097     print "Verifying asdm ", asdmname
00098     if(not os.path.exists(asdmname)):
00099         print "asdm ", asdmname, " doesn't exist."
00100         raise Exception
00101     # test for the existence of all obligatory tables
00102     allTables = [ "Antenna.xml",
00103                   "ASDM.xml",
00104                  # "CalData.xml",
00105                  # "CalDelay.xml",
00106                  # "CalReduction.xml",
00107                   "ConfigDescription.xml",
00108                   "CorrelatorMode.xml",
00109                   "DataDescription.xml",
00110                   "ExecBlock.xml",
00111                   "Feed.xml",
00112                   "Field.xml",
00113                  #"FocusModel.xml",
00114                  #"Focus.xml",
00115                   "Main.xml",
00116                   "PointingModel.xml",
00117                   "Polarization.xml",
00118                   "Processor.xml",
00119                   "Receiver.xml",
00120                   "SBSummary.xml",
00121                   "Scan.xml",
00122                   "Source.xml",
00123                   "SpectralWindow.xml",
00124                   "State.xml",
00125                   "Station.xml",
00126                   "Subscan.xml",
00127                   "SwitchCycle.xml"
00128                   ]
00129     isOK = True
00130     # test if xmllint is available and can be used in the following
00131     xmllint_ok = (os.system('xmllint --version') == 0)
00132     for fileName in allTables:
00133         filePath = asdmname+'/'+fileName
00134         if(not os.path.exists(filePath)):
00135             print "ASDM table file ", filePath, " doesn't exist."
00136             isOK = False
00137         elif(xmllint_ok):
00138             # test if well formed
00139             rval = os.system('xmllint --noout '+filePath)
00140             if(rval !=0):
00141                 print "Table ", filePath, " is not a well formed XML document."
00142                 isOK = False
00143     if(isOK and not xmllint_ok):
00144         print "Note: Test of XML well-formedness not possible since xmllint not available."
00145     else:
00146         print "Note: xml validation not possible since ASDM DTDs (schemas) not yet online."
00147         
00148     if(not os.path.exists(asdmname+"/ASDMBinary")):
00149         print "ASDM binary directory "+asdmname+"/ASDMBinary doesn't exist."
00150         isOK = False
00151 
00152     if(withPointing and not os.path.exists(asdmname+"/Pointing.bin")):
00153         print "ASDM binary file "+asdmname+"/Pointing.bin doesn't exist."
00154         isOK = False
00155 
00156     if (not isOK):
00157         raise Exception
00158 
00159 
00160 def analyseASDM(basename, caltablename0, genwvr=True):
00161     # Reduction of NGC3256 Band 6
00162     # M. Zwaan, May 2010
00163     # D. Petry, May 2010
00164     
00165     # We ignore flux calibration for now.
00166     # The script does bandpass, gain calibration (gaincal), WVR correction and a delay corrections.
00167     # Calibration tables are applied with applycal and images of the calibrator and the
00168     # galaxy are made.
00169 
00170     isOK = True
00171     
00172     msname=basename+'.ms'
00173     contimage=basename+'_cont'
00174     lineimage=basename+'_line'
00175     bname=basename+'_B'
00176     caltablename=bname+'_cal'
00177     msn=bname+'.ms'
00178 
00179     calfield="J1037*" # which field is the phase calibrator
00180     sciencefield="ngc3256" # which field is the target
00181 
00182     avspw=["0:10~118","1:10~118"] # which channels to average for the gain calibration
00183     delay=[0,-8] # there is an 8ns delay problem in the second BB
00184 
00185         
00186     # Find the asdm
00187     asdm=basename
00188     os.system('rm -rf '+bname+'_* '+msn)
00189     print ">> Importing the asdm: ", asdm, " as measurement set: ",msn 
00190     importasdm(
00191         asdm=asdm,
00192         vis=msn,
00193         corr_mode="all",
00194         srt="all",
00195         time_sampling="all",ocorr_mode="ca",compression=False,asis="",
00196         wvr_corrected_data="no",
00197         verbose=True,
00198         showversion=False,
00199         useversion='v3'
00200         )
00201 
00202     if(casadef.casa_version>='3.4.0' and genwvr): # generate the WVR correction table
00203         print ">> Generating the WVR caltable ", caltablename0
00204         os.system('rm -rf '+caltablename0)
00205         wvrgcal(vis=msn, caltable=caltablename0, segsource=False, toffset=-2, reversespw='0~7')
00206     else:
00207         print ">> Reusing existing WVR caltable ", caltablename0
00208     
00209     # Delay correction
00210     
00211     # Could have done this for both spw simultanesouly
00212     print "\n>> 8ns delay corrections: Calculate K tables"
00213     for i in range(2):
00214         os.system('rm -rf '+caltablename+"_spw"+str(i)+'.K')
00215         gencal(
00216             vis=msn,
00217             caltable=caltablename+"_spw"+str(i)+".K",
00218             caltype="sbd",
00219             spw=str(i),
00220             antenna="0",
00221             pol="",
00222             parameter=delay[i]
00223             )
00224 
00225     # Bandpass and Gain calibration
00226     
00227     # Bandpass calibration with 'bandpass' 
00228         
00229     print ">> Find B solutions"
00230     for i in range(2):
00231         print ">> SPW: ",i
00232         os.system('rm -rf '+caltablename+'_spw'+str(i)+'.B')
00233         
00234         # Use the bright phase calibrator for the bandpass
00235         bandpass(
00236             vis=msn,
00237             spw=str(i),
00238             field=calfield,
00239             caltable=caltablename+"_spw"+str(i)+".B",
00240             bandtype="B",
00241             solint="inf",
00242             combine="scan",
00243             minblperant=2,
00244             solnorm=False,
00245             minsnr=-2, # i.e. use all solutions, new parameter in 3.0.2
00246             gaintable=caltablename+"_spw"+str(i)+".K",
00247             spwmap=[i]
00248             )
00249 
00250     print ">> Plot the bandpass solutions"
00251     pl.clf()
00252     for i in range(2):
00253         plotcal(
00254             caltable=caltablename+"_spw"+str(i)+".B",xaxis="chan",yaxis="phase",
00255             spw="",subplot=221+i,overplot=False,plotsymbol='.',
00256             timerange="", showgui=False
00257             )
00258         tget(plotcal)
00259         subplot=223+i
00260         yaxis="amp"
00261         plotrange=[0,0,0,0.02]
00262         figfile=caltablename+"_bandpass.png"
00263         plotcal()
00264         
00265     # For GSPLINE solutions, have to do it for different spws separately
00266     
00267     print ">> Find G solutions"
00268     for i in range(2):
00269         print ">> SPW: ",i
00270         os.system('rm -rf '+caltablename+'_spw'+str(i)+'.G')
00271         gaincal(
00272             vis=msn,
00273             caltable=caltablename+"_spw"+str(i)+".G",
00274             field=calfield,
00275             spw=avspw[i],
00276             selectdata=True,
00277             solint="60s",
00278             gaintable=[caltablename+'_spw'+str(i)+'.K',caltablename+'_spw'+str(i)+'.B'],
00279             spwmap=[[i],[i]],
00280             combine="",refant="0",minblperant=2,minsnr=-1,solnorm=False,
00281             gaintype="G",calmode="ap",
00282             )
00283 
00284     print ">> Find G solutions, using WVR corrections"
00285     for i in range(2):
00286         print ">> SPW: ",i
00287         os.system('rm -rf '+caltablename+'_spw'+str(i)+'.G_WVR')
00288         wvrspw = 0
00289         if(casadef.casa_version>='3.4.0'):
00290             wvrspw = i
00291         gaincal(
00292             vis=msn,
00293             caltable=caltablename+"_spw"+str(i)+".G_WVR",
00294             field=calfield,
00295             spw=avspw[i],
00296             selectdata=True,
00297             solint="60s",
00298             # Pre-apply the WVR correction, the bandpass and the delay correction
00299             gaintable=[caltablename0,caltablename+'_spw'+str(i)+'.K',caltablename+'_spw'+str(i)+'.B'],
00300             spwmap=[[wvrspw],[i],[i]],
00301             combine="",refant="0",minblperant=2,minsnr=-1,solnorm=False,
00302             gaintype="G",calmode="ap",
00303             )   
00304 
00305 
00306     print ">> Find GSPLINE solutions, using WVR corrections"
00307     for i in range(2):
00308         print ">> SPW: ",i
00309         os.system('rm -rf '+caltablename+'.GSPLINE_WVR_'+str(i))
00310         wvrspw = 0
00311         if(casadef.casa_version>='3.4.0'):
00312             wvrspw = i
00313         gaincal(
00314             vis=msn,
00315             caltable=caltablename+".GSPLINE_WVR_"+str(i),
00316             field=calfield,
00317             spw=avspw[i],
00318             selectdata=True,
00319             solint="300s",
00320             splinetime=10000,
00321             # Pre-apply the WVR correction, the bandpass and the delay correction
00322             gaintable=[caltablename0,caltablename+'_spw'+str(i)+'.K',caltablename+'_spw'+str(i)+'.B'],
00323             spwmap=[[wvrspw],[i],[i]],
00324             combine="",refant="0",minblperant=2,minsnr=-1,solnorm=False,
00325             gaintype="GSPLINE",
00326             npointaver=2,
00327             preavg=300,
00328             calmode="ap",
00329             )   
00330 
00331 
00332     print "\n>> Plot the solutions: small points: uncorrected"
00333     print ">>                     large points: WVR corrected"
00334     print ">>                     lines: spline fits, WVR corrected"
00335     pl.clf()
00336     for i in range(2):
00337         plotcal(
00338             caltable=caltablename+"_spw"+str(i)+".G_WVR",xaxis="time",yaxis="phase",
00339             spw="",subplot=211+i,overplot=False,plotsymbol='o',
00340             timerange="", showgui=False
00341             )
00342         tget(plotcal)
00343         overplot=T
00344         plotsymbol='.'; caltable=caltablename+"_spw"+str(i)+".G"; plotcal()
00345         plotsymbol=':'; caltable=caltablename+".GSPLINE_WVR_"+str(i);
00346         figfile=caltablename+"_gaincals.png"
00347         plotcal()
00348 
00349 
00350     # Apply the gain calibrations
00351     
00352     # Have to apply the G_WVR table and the WVR table simultaneously
00353     # Or just the G table
00354     # (Do not apply just the G_WVR table or G and WVR tables)
00355     
00356     # Use spwmap to apply to all spws
00357     # Use spwmap=[[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0]]
00358     # for the unsplit ms.
00359     # There are eight numbers because wvr has four spws, although only one is shown in listobs
00360     
00361     print ">> Apply the G and WVR solutions to",msn
00362     for i in range (2):
00363         print ">> SPW: ",i
00364         wvrspw = 0
00365         if(casadef.casa_version>='3.4.0'):
00366             wvrspw = i
00367         applycal(
00368             vis=msn,
00369             field="",
00370             spw=avspw[i],
00371             selectdata=False,timerange="",uvrange="",
00372             antenna="",scan="",msselect="",
00373             gaincurve=False,opacity=0.0,parang=False,calwt=True,
00374             # We use G after all, not Gspline
00375             gaintable=[caltablename+"_spw"+str(i)+".G_WVR",caltablename0,caltablename+"_spw"+str(i)+".K",caltablename+"_spw"+str(i)+".B"],
00376             gainfield=['','','',''],
00377             interp=['linear','','',''],
00378             spwmap=[[i],[wvrspw],[i],[i]]
00379             )
00380 
00381     # Clean the calibrator
00382 
00383     rms = [0.,0.]
00384     peak = [0.,0.]
00385     
00386     for i in range (2):
00387         
00388         # tb.open(msn0+'/FIELD')
00389         # coords= tb.getcol('PHASE_DIR')
00390         # calCoords=coords[:,:,int(calfield)]
00391         # calRa=qa.formxxx(str(float(calCoords[0]))+'rad','hms')
00392         # calDec=qa.formxxx(str(float(calCoords[1]))+'rad','dms')
00393         
00394         # print ">> The coordinates of the phase calibrator are:", calRa,",",calDec
00395         
00396         calimage=caltablename+"cal_spw"+str(i)
00397         
00398         os.system('rm -rf '+calimage+'.*')
00399         
00400         print "\n>> Clean the phase calibrator, spw",str(i)
00401         print ">> The imagename is",calimage
00402         
00403         default(clean)
00404         clean(
00405             vis=msn,
00406             imagename=calimage,
00407             field=calfield,
00408             spw=avspw[i],
00409             niter=500,
00410             gain=0.15,
00411             threshold="0.0mJy",
00412             psfmode="hogbom",
00413             imagermode='',
00414             interactive=False,
00415             mask=[298,298,302,302],
00416             nchan=-1, start=0, width=1,
00417             imsize=[600, 600],
00418             cell=['0.2arcsec','0.2arcsec']
00419             )
00420         
00421         calstat=imstat(imagename=calimage+".image",region="",box="100,100,240,500")
00422         rms[i]=(calstat['rms'][0])
00423         print ">> rms in calibrator image: "+str(rms[i])
00424         calstat=imstat(imagename=calimage+".image",region="")
00425         peak[i]=(calstat['max'][0])
00426         print ">> Peak in calibrator image: "+str(peak[i])
00427         print ">> Dynamic range in calibrator image: "+str(peak[i]/rms[i])
00428 
00429     reference_rms = [0.0016516, 0.0009388]
00430     reference_peak = [1.00014091, 1.00002384]
00431 
00432     for i in range(2):
00433         print ">> image rms ", i, " is ", rms[i], " expected value is ", reference_rms[i] 
00434         if(abs(rms[i] - reference_rms[i])/reference_rms[i] > 0.01):
00435             print ">> ERROR." 
00436             isOK = False
00437         print ">> image peak ", i, " is ", peak[i], " expected value is ", reference_peak[i] 
00438         if(abs(peak[i] - reference_peak[i])/reference_peak[i] > 0.01):
00439             print ">> ERROR." 
00440             isOK = False
00441             
00442     if (isOK):
00443         print ''
00444         print 'Regression PASSED'
00445         print ''
00446     else:
00447         print ''
00448         print 'Regression FAILED'
00449         print ''
00450 
00451     return isOK
00452 
00453 
00454 ###########################
00455 # beginning of actual test 
00456 
00457 part1 = True
00458 
00459 # part 1
00460 try:
00461     importasdm(myasdm_dataset_name, useversion='v3')
00462 except:
00463     print myname, ": Error ", sys.exc_info()[0]
00464     part1 = False
00465 else:
00466     print myname, ": Success! Now checking output ..."
00467     # when SubMS::setupMS() is used, the main table
00468     # somehow starts from table.f1 instead of table.f0
00469     mscomponents = set(["table.dat",
00470                         #"table.f0",
00471                         "table.f1",
00472                         "table.f2",
00473                         "table.f3",
00474                         "table.f4",
00475                         "table.f5",
00476                         "table.f6",
00477                         "table.f7",
00478                         "table.f8",
00479                         "ANTENNA/table.dat",
00480                         "DATA_DESCRIPTION/table.dat",
00481                         "FEED/table.dat",
00482                         "FIELD/table.dat",
00483                         "FLAG_CMD/table.dat",
00484                         "HISTORY/table.dat",
00485                         "OBSERVATION/table.dat",
00486                         "POINTING/table.dat",
00487                         "POLARIZATION/table.dat",
00488                         "PROCESSOR/table.dat",
00489                         "SOURCE/table.dat",
00490                         "SPECTRAL_WINDOW/table.dat",
00491                         "STATE/table.dat",
00492                         "SYSCAL/table.dat",
00493                         "ANTENNA/table.f0",
00494                         "DATA_DESCRIPTION/table.f0",
00495                         "FEED/table.f0",
00496                         "FIELD/table.f0",
00497                         "FLAG_CMD/table.f0",
00498                         "HISTORY/table.f0",
00499                         "OBSERVATION/table.f0",
00500                         "POINTING/table.f0",
00501                         "POLARIZATION/table.f0",
00502                         "PROCESSOR/table.f0",
00503                         "SOURCE/table.f0",
00504                         "SPECTRAL_WINDOW/table.f0",
00505                         "STATE/table.f0",
00506                         "SYSCAL/table.f0"
00507                         ])
00508     for name in mscomponents:
00509         if not os.access(msname+"/"+name, os.F_OK):
00510             print myname, ": Error  ", msname+"/"+name, "doesn't exist ..."
00511             part1 = False
00512         else:
00513             print myname, ": ", name, "present."
00514     print myname, ": MS exists. All tables present. Try opening as MS ..."
00515     try:
00516         ms.open(msname)
00517     except:
00518         print myname, ": Error  Cannot open MS table", tablename
00519         part1 = False
00520     else:
00521         ms.close()
00522         print myname, ": OK. Checking tables in detail ..."
00523 
00524         # check main table first
00525         name = ""
00526         #             col name, row number, expected value, tolerance
00527         expected = [
00528                      ['UVW',       42, [ 0., 0., 0. ], 1E-7],
00529                      ['EXPOSURE',  42, 2.016, 0],
00530                      ['DATA',      42, [ [ 0.11164435+0.j],[ 1.01130176+0.j] ], 1E-7]
00531                      ]
00532         checktable(name, expected)
00533 
00534         expected = [
00535                      ['UVW',       557, [-172.89958856,  -80.72977367,   63.39288203], 1E-6], # based on geodetic data r3846 
00536                      ['EXPOSURE',  557, 6.048, 0],
00537                      ['DATA',      557,
00538                       [[ -4.17647697e-03 +3.08606686e-05j,  -1.18642126e-03 +7.54371868e-05j,
00539                          9.60109683e-05 -1.37158531e-05j,  -5.48634125e-05 -7.20082244e-05j,
00540                          7.54371868e-05 -1.37158531e-05j,   5.48634125e-05 +2.74317063e-05j,
00541                          2.74317063e-05 +3.77185934e-05j,  -1.02868898e-05 +3.08606686e-05j,
00542                          1.37158531e-05 -2.40027421e-05j,   5.82923749e-05 -6.17213373e-05j,
00543                          9.25820059e-05 -5.82923749e-05j,   8.22951188e-05 -3.42896328e-06j,
00544                          8.57240811e-05 +3.42896310e-05j,   1.13155787e-04 -3.42896328e-06j,
00545                          9.94399306e-05 -6.85792656e-06j,   6.17213373e-05 +4.45765218e-05j,
00546                          2.40027421e-05 +2.40027421e-05j,  -2.74317063e-05 -5.82923749e-05j,
00547                          -3.08606686e-05 -7.20082244e-05j,   3.42896328e-06 -1.71448155e-05j,
00548                          -2.40027421e-05 -6.85792656e-06j,  -7.54371868e-05 -2.40027421e-05j,
00549                          -4.80054841e-05 -1.02868898e-05j,  -1.37158531e-05 +3.42896310e-05j,
00550                          0.00000000e+00 +3.77185934e-05j,   6.85792656e-06 +2.74317063e-05j,
00551                          -1.37158531e-05 +7.54371868e-05j,  -1.02868898e-05 +8.22951188e-05j,
00552                          5.82923749e-05 +1.37158531e-05j,   5.48634125e-05 +3.42896328e-06j,
00553                          -6.85792656e-06 +2.40027421e-05j,  -3.77185934e-05 +3.42896310e-05j,
00554                          -4.11475594e-05 +1.37158531e-05j,  -3.42896328e-06 +2.05737797e-05j,
00555                          1.71448155e-05 +5.48634125e-05j,   1.37158531e-05 +2.05737797e-05j,
00556                          2.40027421e-05 -4.45765218e-05j,   7.88661564e-05 -5.48634125e-05j,
00557                          8.91530435e-05 -3.77185934e-05j,   2.05737797e-05 -3.77185934e-05j,
00558                          1.02868898e-05 +3.42896328e-06j,   1.02868898e-05 +5.48634125e-05j,
00559                          2.05737797e-05 +7.54371868e-05j,   7.20082244e-05 +5.82923749e-05j,
00560                          4.11475594e-05 +6.51502996e-05j,  -5.48634125e-05 +1.16584750e-04j,
00561                          -9.25820059e-05 +8.57240811e-05j,  -7.54371868e-05 +3.42896310e-05j,
00562                          -4.80054841e-05 +2.74317063e-05j,  -1.02868898e-05 +0.00000000e+00j,
00563                          5.48634125e-05 +0.00000000e+00j,   1.13155787e-04 +1.71448155e-05j,
00564                          1.09726825e-04 +2.05737797e-05j,   8.57240811e-05 +3.77185934e-05j,
00565                          3.08606686e-05 +4.45765218e-05j,  -4.45765218e-05 +4.45765218e-05j,
00566                          -2.05737797e-05 +7.88661564e-05j,  -6.85792656e-06 +8.91530435e-05j,
00567                          -4.80054841e-05 +6.85792620e-05j,  -3.42896328e-06 +5.14344465e-05j,
00568                          4.45765218e-05 +1.02868898e-05j,   5.14344465e-05 -5.14344465e-05j,
00569                          6.17213373e-05 +1.37158531e-05j,   5.82923749e-05 +1.44016449e-04j,
00570                          4.11475594e-05 +7.88661564e-05j,   3.42896310e-05 -1.02868898e-05j,
00571                          2.74317063e-05 +1.37158531e-05j,   2.05737797e-05 +6.85792656e-06j,
00572                          1.37158531e-05 -3.42896328e-06j,   2.05737797e-05 +0.00000000e+00j,
00573                          4.45765218e-05 +0.00000000e+00j,   9.60109683e-05 -3.42896328e-06j,
00574                          8.57240811e-05 +6.85792656e-06j,  -3.42896310e-05 +2.40027421e-05j,
00575                          -6.85792620e-05 -2.05737797e-05j,  -1.02868898e-05 -7.20082244e-05j,
00576                          0.00000000e+00 -8.22951188e-05j,   1.02868898e-05 -2.40027421e-05j,
00577                          3.77185934e-05 +6.85792656e-06j,   1.71448155e-05 -2.40027421e-05j,
00578                          -6.17213373e-05 +2.05737797e-05j,  -7.20082244e-05 +2.05737797e-05j,
00579                          1.37158531e-05 -2.05737797e-05j,   3.77185934e-05 -2.40027421e-05j,
00580                          4.11475594e-05 -3.08606686e-05j,   9.60109683e-05 -5.14344465e-05j,
00581                          1.09726825e-04 -7.88661564e-05j,   3.08606686e-05 -7.88661564e-05j,
00582                          -4.11475594e-05 -2.40027421e-05j,  -2.05737797e-05 +4.45765218e-05j,
00583                          1.37158531e-05 +2.74317063e-05j,  -2.74317063e-05 -1.71448155e-05j,
00584                          -5.48634125e-05 -1.02868898e-05j,  -2.74317063e-05 +4.80054841e-05j,
00585                          0.00000000e+00 +1.16584750e-04j,  -6.85792656e-06 +9.94399306e-05j,
00586                          -4.45765218e-05 +1.71448155e-05j,  -4.11475594e-05 +3.42896328e-06j,
00587                          0.00000000e+00 +3.77185934e-05j,   3.08606686e-05 +2.40027421e-05j,
00588                          3.77185934e-05 +1.37158531e-05j,   5.14344465e-05 +7.88661564e-05j,
00589                          6.85792620e-05 +1.26871644e-04j,   6.51502996e-05 +8.91530435e-05j,
00590                          6.51502996e-05 +4.45765218e-05j,   3.77185934e-05 +5.48634125e-05j,
00591                          1.71448155e-05 +7.20082244e-05j,   1.02868898e-05 +3.77185934e-05j,
00592                          2.40027421e-05 +2.05737797e-05j,   5.82923749e-05 +1.71448155e-05j,
00593                          3.42896328e-06 +3.42896328e-06j,  -6.17213373e-05 -1.02868898e-05j,
00594                          -4.80054841e-05 -3.42896310e-05j,  -6.17213373e-05 -4.80054841e-05j,
00595                          -7.54371868e-05 -4.45765218e-05j,   0.00000000e+00 -2.40027421e-05j,
00596                          2.05737797e-05 -2.40027421e-05j,  -8.22951188e-05 -3.42896328e-06j,
00597                          -8.22951188e-05 +2.05737797e-05j,   3.42896328e-06 +2.40027421e-05j,
00598                          -2.40027421e-05 +7.54371868e-05j,  -5.48634125e-05 +9.60109683e-05j,
00599                          2.40027421e-05 +2.05737797e-05j,   7.88661564e-05 -3.42896328e-06j,
00600                          4.45765218e-05 +7.54371868e-05j,  -3.08606686e-05 +8.22951188e-05j,
00601                          -5.48634125e-05 +1.02868898e-05j,   1.02868898e-05 -1.02868898e-05j
00602                          ],
00603                        [ -2.24771962e-01 +1.02868898e-05j,  -6.19887970e-02 +6.17213373e-05j,
00604                          8.64784513e-03 +8.22951188e-05j,  -2.27340264e-03 +0.00000000e+00j,
00605                          9.36106953e-04 -4.45765218e-05j,  -5.21202397e-04 -1.71448155e-05j,
00606                          2.81174987e-04 -3.77185934e-05j,  -2.16024680e-04 -4.80054841e-05j,
00607                          3.08606686e-05 -1.37158531e-05j,  -2.09166756e-04 +0.00000000e+00j,
00608                          2.05737797e-05 -3.42896328e-06j,  -4.80054841e-05 -6.17213373e-05j,
00609                          5.14344465e-05 -1.02868893e-04j,  -7.20082244e-05 -7.20082244e-05j,
00610                          -2.05737797e-05 -8.57240811e-05j,  -1.02868898e-05 -1.16584750e-04j,
00611                          5.48634125e-05 -9.60109683e-05j,   1.02868898e-05 -8.57240811e-05j,
00612                          6.85792620e-05 -2.05737797e-05j,  -1.02868898e-05 +1.06297855e-04j,
00613                          -6.85792656e-06 +7.20082244e-05j,  -3.42896328e-06 -2.05737797e-05j,
00614                          -3.77185934e-05 -2.05737797e-05j,  -6.51502996e-05 +4.11475594e-05j,
00615                          -6.51502996e-05 +1.09726825e-04j,  -1.50874374e-04 +8.91530435e-05j,
00616                          -8.91530435e-05 +2.74317063e-05j,   3.42896328e-06 +3.42896310e-05j,
00617                          3.42896328e-06 +3.77185934e-05j,  -5.48634125e-05 +2.40027421e-05j,
00618                          -6.17213373e-05 +5.82923749e-05j,  -6.17213373e-05 +7.88661564e-05j,
00619                          -1.71448155e-05 +2.05737797e-05j,  -2.40027421e-05 -5.82923749e-05j,
00620                          -4.80054841e-05 -7.88661564e-05j,  -1.02868898e-05 -1.37158531e-05j,
00621                          -2.74317063e-05 -2.74317063e-05j,  -9.94399306e-05 -1.23442675e-04j,
00622                          -7.20082244e-05 -9.25820059e-05j,  -4.80054841e-05 -3.42896328e-06j,
00623                          -2.74317063e-05 -3.77185934e-05j,  -5.82923749e-05 -6.85792620e-05j,
00624                          -1.20013712e-04 -4.80054841e-05j,  -9.94399306e-05 -6.17213373e-05j,
00625                          -4.45765218e-05 -5.14344465e-05j,  -8.22951188e-05 -1.71448155e-05j,
00626                          -7.20082244e-05 -4.45765218e-05j,   1.02868898e-05 -6.17213373e-05j,
00627                          3.42896310e-05 +1.71448155e-05j,   1.02868898e-05 +7.54371868e-05j,
00628                          0.00000000e+00 +1.71448155e-05j,  -1.02868898e-05 -4.80054841e-05j,
00629                          3.08606686e-05 -4.80054841e-05j,   4.11475594e-05 -1.02868898e-05j,
00630                          4.45765218e-05 +2.05737797e-05j,   3.08606686e-05 +6.17213373e-05j,
00631                          -4.45765218e-05 +8.91530435e-05j,  -7.54371868e-05 +5.82923749e-05j,
00632                          -5.48634125e-05 -4.80054841e-05j,  -2.05737797e-05 -1.30300599e-04j,
00633                          4.45765218e-05 -9.25820059e-05j,   0.00000000e+00 -2.40027421e-05j,
00634                          -7.54371868e-05 +1.02868898e-05j,  -7.88661564e-05 +3.42896310e-05j,
00635                          -2.05737797e-05 +2.05737797e-05j,   1.37158531e-05 -5.48634125e-05j,
00636                          1.37158531e-05 -8.22951188e-05j,   2.74317063e-05 -4.80054841e-05j,
00637                          1.71448155e-05 -5.82923749e-05j,  -1.71448155e-05 -6.17213373e-05j,
00638                          2.74317063e-05 -5.48634125e-05j,   6.17213373e-05 -9.60109683e-05j,
00639                          -1.02868898e-05 -1.06297855e-04j,  -7.20082244e-05 -8.57240811e-05j,
00640                          1.02868898e-05 -8.22951188e-05j,   7.88661564e-05 -5.48634125e-05j,
00641                          1.02868893e-04 -2.74317063e-05j,   1.23442675e-04 -3.42896328e-06j,
00642                          9.94399306e-05 +3.77185934e-05j,   3.42896310e-05 +5.14344465e-05j,
00643                          2.40027421e-05 +4.11475594e-05j,   3.77185934e-05 +0.00000000e+00j,
00644                          -3.42896328e-06 -5.48634125e-05j,  -1.37158531e-05 -8.91530435e-05j,
00645                          3.77185934e-05 -8.57240811e-05j,   2.40027421e-05 -5.82923749e-05j,
00646                          -1.02868898e-05 -6.17213373e-05j,   3.77185934e-05 -3.77185934e-05j,
00647                          4.11475594e-05 +3.42896310e-05j,  -2.40027421e-05 -2.05737797e-05j,
00648                          -2.74317063e-05 -1.09726825e-04j,  -2.40027421e-05 -8.57240811e-05j,
00649                          3.42896310e-05 -2.74317063e-05j,   1.13155787e-04 -1.37158531e-05j,
00650                          1.30300599e-04 -3.77185934e-05j,   1.13155787e-04 -6.17213373e-05j,
00651                          8.57240811e-05 -3.42896310e-05j,   6.51502996e-05 +2.05737797e-05j,
00652                          4.45765218e-05 +6.85792656e-06j,  -1.37158531e-05 -8.57240811e-05j,
00653                          -7.54371868e-05 -8.91530435e-05j,  -3.42896310e-05 -3.08606686e-05j,
00654                          3.42896328e-06 -5.48634125e-05j,  -3.77185934e-05 -1.16584750e-04j,
00655                          -2.40027421e-05 -9.60109683e-05j,  -2.40027421e-05 -3.08606686e-05j,
00656                          -4.45765218e-05 +6.51502996e-05j,   2.40027421e-05 +1.06297855e-04j,
00657                          7.20082244e-05 +2.05737797e-05j,  -1.71448155e-05 -6.51502996e-05j,
00658                          -1.02868893e-04 -5.82923749e-05j,  -1.16584750e-04 -4.11475594e-05j,
00659                          -4.45765218e-05 -1.71448155e-05j,   4.45765218e-05 +3.42896328e-06j,
00660                          1.71448155e-05 +1.37158531e-05j,  -2.74317063e-05 +3.77185934e-05j,
00661                          -1.02868898e-05 +4.45765218e-05j,   3.42896328e-06 +1.71448155e-05j,
00662                          3.42896328e-06 +2.74317063e-05j,  -6.17213373e-05 +5.48634125e-05j,
00663                          -9.60109683e-05 +3.42896328e-06j,  -3.42896310e-05 -2.40027421e-05j,
00664                          -3.42896310e-05 +1.71448155e-05j,  -8.91530435e-05 -2.40027421e-05j,
00665                          0.00000000e+00 -5.82923749e-05j,   1.57732313e-04 -3.42896328e-06j,
00666                          1.47445418e-04 +1.71448155e-05j,   1.02868893e-04 -1.02868898e-05j
00667                          ]
00668                        ],
00669                       1E-7
00670                       ]
00671                      ]
00672         checktable(name, expected)
00673         
00674         name = "ANTENNA"
00675         expected = [ ['OFFSET',       1, [ 0.,  0.,  0.], 0],
00676                      #['POSITION',     1, [2224600.1130,  -5440364.6494, -2481417.4709], 0.001],
00677                      ['POSITION',     1,  [2224602.5538,  -5440370.6185, -2481420.1935], 0.001],
00678                      ['DISH_DIAMETER',1, 12.0, 0]
00679                      ]
00680         checktable(name, expected)
00681         
00682         name = "POINTING"
00683         expected = [ ['DIRECTION',       10, [[-0.97039579],[ 0.88554736]], 1E-7],
00684                      ['INTERVAL',        10, 0.048, 0],
00685                      ['TARGET',          10, [[-0.97039579],[ 0.88554736]], 1E-7],
00686                      ['TIME',            10, 4775780726.88, 0.01],
00687                      ['TIME_ORIGIN',     10, 0., 0],
00688                      ['POINTING_OFFSET', 10, [[ 0.],[ 0.]], 0],
00689                      ['ENCODER',         10, [-0.96686338, 0.88392100], 1E-7 ]
00690                      ]
00691         checktable(name, expected)
00692 
00693 if (not part1):
00694     print "Part 1 failed."
00695 
00696 part2 = True
00697         
00698 myvis = myms_dataset_name
00699 os.system('rm -rf exportasdm-output.asdm myinput.ms')
00700 os.system('cp -R ' + myvis + ' myinput.ms')
00701 default('exportasdm')
00702 try:
00703     print "\n>>>> Test of exportasdm: input MS  is ", myvis
00704     print "(a simulated input MS with pointing table)"
00705     rval = exportasdm(
00706         vis = 'myinput.ms',
00707         asdm = 'exportasdm-output.asdm',
00708         archiveid="S002",
00709         apcorrected=False,
00710         useversion='v3'
00711         )
00712     print "rval is ", rval
00713     if not rval:
00714         raise Exception
00715     os.system('rm -rf '+asdmname+'; mv exportasdm-output.asdm '+asdmname)
00716     verify_asdm(asdmname, True)
00717 except:
00718     print myname, ': *** Unexpected error exporting MS to ASDM, regression failed ***'   
00719     raise
00720     
00721 try:
00722     print "Reimporting the created ASDM ...."
00723     importasdm(asdm=asdmname, vis=reimp_msname, wvr_corrected_data='no', useversion='v3')
00724     print "Testing existence of reimported MS ...."
00725     if(not os.path.exists(reimp_msname)):
00726         print "MS ", reimp_msname, " doesn't exist."
00727         raise Exception
00728     print "Testing equivalence of the original and the reimported MS."
00729     tb.open(myms_dataset_name)
00730     nrowsorig = tb.nrows()
00731     print "Original MS contains ", nrowsorig, "integrations."
00732     tb.close()
00733     tb.open(reimp_msname)
00734     nrowsreimp = tb.nrows()
00735     print "Reimported MS contains ", nrowsreimp, "integrations."
00736     if(not nrowsreimp==nrowsorig):
00737         print "Numbers of integrations disagree."
00738         part2 = False
00739 except:
00740     print myname, ': *** Unexpected error reimporting the exported ASDM, regression failed ***'   
00741     part2 = False
00742     
00743 #############
00744 # Now import an ASDM and do a serious analysis
00745 
00746 print
00747 print '==================================================================='
00748 print "Serious analysis of an ASDM ..."
00749 
00750 rval = True
00751 part3 = True
00752 
00753 try:
00754     rval = analyseASDM(myasdm_dataset2_name, mywvr_correction_file)
00755 except:
00756     print myname, ': *** Unexpected error analysing ASDM, regression failed ***'   
00757     part3 = False
00758 
00759 if(not rval):
00760     print myname, ': *** Unexpected error analysing ASDM, regression failed ***'   
00761     part3 = False
00762 
00763 #############
00764 #
00765 
00766 dopart4 = True
00767 
00768 part4 = True
00769 
00770 if dopart4:
00771 
00772     print
00773     print '==================================================================='
00774     print "Export the previously imported ASDM ..."
00775     default('exportasdm')
00776     try:
00777         # os.system('rm -rf '+myasdm_dataset2_name+'-re-exported*')
00778         os.system('rm -rf '+myasdm_dataset2_name+'-re-exported* '+myasdm_dataset2_name+'-split*')
00779         
00780         # use only the actual visibility data, not the WVR data
00781         split(vis=myasdm_dataset2_name+'.ms',
00782               outputvis=myasdm_dataset2_name+'-split.ms',
00783               datacolumn='data',
00784               spw='0~3'
00785               )
00786         
00787         rval = exportasdm(
00788 #            vis = myasdm_dataset2_name+'.ms',
00789             vis = myasdm_dataset2_name+'-split.ms',
00790             asdm = myasdm_dataset2_name+'-re-exported',
00791             archiveid="X001",
00792             apcorrected=False,
00793             datacolumn='DATA', # the default
00794             useversion='v3'
00795             )
00796         print "rval is ", rval
00797         if not rval:
00798             raise Exception
00799         verify_asdm(myasdm_dataset2_name+'-re-exported', True)
00800     except:
00801         print myname, ': *** Unexpected error re-exporting MS to ASDM, regression failed ***'   
00802         raise
00803 
00804 #############
00805     # repeat analysis on re-exported ASDM
00806 
00807     print
00808     print '==================================================================='
00809     print "Serious analysis of an exported and re-imported ASDM ..."
00810     
00811     rval = True
00812     try:
00813         rval = analyseASDM(myasdm_dataset2_name+'-re-exported', mywvr_correction_file,
00814                            False # do not regenerate the WVR table
00815                            )
00816     except:
00817         print myname, ': *** Unexpected error analysing re-exported ASDM, regression failed ***'   
00818         part4 = False
00819         
00820     if(not rval):
00821         print myname, ': *** Unexpected error analysing re-exported ASDM, regression failed ***'   
00822         part4 = False
00823 
00824 # end if dopart4
00825 
00826 print
00827 print '==================================================================='
00828 print "Summary"
00829 
00830 if(not part1):
00831     print "Part 1: ASDM import failed."
00832 else:
00833     print "Part 1: ASDM import passed."
00834 if(not part2):
00835     print "Part 2: ASDM export failed."
00836 else:
00837     print "Part 2: ASDM export passed."
00838 if(not part3):
00839     print "Part 3: serious analysis of an ASDM failed."
00840 else:
00841     print "Part 3: serious analysis of an ASDM passed."
00842 if dopart4:
00843     if not part4:
00844         print "Part 4: serious analysis of an exported and re-imported ASDM failed."
00845     else:
00846         print "Part 4: serious analysis of an exported and re-imported ASDM passed."
00847 else:
00848         print "Part 4: serious analysis of an exported and re-imported ASDM not executed."    
00849 
00850 if(not (part1 and part2 and part3 and part4)):
00851     print "Regression failed."
00852     raise
00853 else:
00854     print "Regression passed."