casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
accum_regression.py
Go to the documentation of this file.
00001 #############################################################################
00002 ## $Id:$
00003 # Test Name:                                                                #
00004 #    Regression Test Script for accum ()                                    #
00005 #                                                                           #
00006 # Rationale for Inclusion:                                                  #
00007 #    It ensures that the task is working properly.                          #
00008 #                                                                           # 
00009 # Features tested:                                                          #
00010 #    1) Is the task working properly?                                       #
00011 #    2) Is the task producing the same results as the reference?            #
00012 #   The script will as well test the following features:                    #
00013 #                                                                           #
00014 #    Input Data           Process              Output Data                  #
00015 # ngc5921.fits    --->  importuvfits ----> ngc5921.ms                       #
00016 #                            |                                              #
00017 #                            v                                              #
00018 #                            |                                              #
00019 #                            v                                              #
00020 #                          setjy     ----> ngc1333.ms                       #
00021 #                            |                                              #
00022 #                            v                                              #
00023 #                         gaincal    ----> ngc1333.int.gcal                 #
00024 #  apply the gain solution using solution interval per integration          #
00025 #                            |                                              #
00026 #                            v                                              #
00027 #                         gaincal    ----> ngc1333.scan.gcal                #
00028 #  apply the gain solution using solution interval infinite (up to the      #
00029 #  boundaries                                                               #
00030 #                            |                                              #
00031 #                            v                                              #
00032 #                         gaincal    ----> ngc1333.rint.gcal                #
00033 #  apply the gain solution using the previous scan gain table, but using    #
00034 #  solution interval per integration                                        #
00035 #                            |                                              #
00036 #                            v                                              #
00037 #                        accum   ------>  ngc1333.acc1.gcal                 #
00038 #  accumulate the first time, using the scan table as the gain table        #
00039 #                            |                                              #
00040 #                            v                                              #
00041 #                        accum   ------>  ngc1333.acc2.gcal                 #
00042 #  accumulate the second time, using the relative int table as the gain     #
00043 #  table. This last table will be compared with the first gain table        #
00044 #                                                                           #
00045 #                                                                           #
00046 # Input data:                                                               #
00047 #    ngc5921.fits                                                           #
00048 #                                                                           #
00049 # Note: all input data have relative paths to the local directory           #
00050 #############################################################################
00051 
00052 
00053 import os
00054 import time
00055 import regression_utility as tstutl
00056 from __main__ import default
00057 from tasks import *
00058 from taskinit import *
00059 
00060 
00061 # Enable benchmarking?
00062 benchmarking = True
00063 usedasync = False
00064 
00065 #
00066 # Set up some useful variables
00067 #
00068 # This is where the NGC1333 UVFITS data will be
00069 fitsdata='ngc5921.fits'
00070 
00071 # The testdir where all output files will be kept
00072 testdir='accum_regression'
00073 
00074 # The prefix to use for output files.
00075 prefix=testdir+"/"+'ngc5921'
00076 
00077 # Make new test directory
00078 # (WARNING! Removes old test directory of the same name if one exists)
00079 tstutl.maketestdir(testdir)
00080 
00081 # Start benchmarking
00082 if benchmarking:
00083     startTime = time.time()
00084     startProc = time.clock()
00085 
00086 #
00087 #=====================================================================
00088 #
00089 # Import the data from FITS to MS
00090 #
00091 try:
00092     
00093     print '--Import--'
00094     
00095     # Safest to start from task defaults
00096     default('importuvfits')
00097     
00098     # Set up the MS filename and save as new global variable
00099     msfile = prefix + '.ms'
00100     
00101     # Use task importuvfits
00102     fitsfile = fitsdata
00103     vis = msfile
00104     antnamescheme="new"
00105     importuvfits()
00106     
00107     # Record import time
00108     if benchmarking:
00109         importtime = time.time()
00110     
00111     #
00112     #=====================================================================
00113     #
00114     # Set the fluxes of the primary calibrator(s)
00115     #
00116     print '--Setjy--'
00117     default('setjy')
00118     
00119     setjy(vis=msfile,field='0',scalebychan=False,standard='Perley-Taylor 99')
00120     
00121     # Record setjy completion time
00122     if benchmarking:
00123         setjytime = time.time()
00124     
00125     #
00126     #=====================================================================
00127     #
00128     # Gain calibration using integration time
00129     #
00130     print '--Gaincal using interval per integration--'
00131     default('gaincal')
00132     
00133     Ginttable = prefix + '.int.gcal'
00134     gaincal(vis=msfile,caltable=Ginttable,
00135             field='0', gaintype='G',solint='int',combine='',refant='VA02')
00136     
00137     
00138     # gaincal calibration completion time
00139     if benchmarking:
00140         gaintime1 = time.time()
00141     
00142     #
00143     #=====================================================================
00144     #
00145     # Gain calibration using scan
00146     #
00147     print '--Gaincal using interval infinite--'
00148     default('gaincal')
00149     
00150     Gscantable = prefix + '.scan.gcal'
00151     gaincal(vis=msfile,caltable=Gscantable,
00152             field='0', gaintype='G',solint='inf',combine='',refant='VA02')
00153     
00154     
00155     # gaincal calibration completion time
00156     if benchmarking:
00157         gaintime2 = time.time()
00158     
00159     #
00160     #=====================================================================
00161     #
00162     # Gain calibration using scan table to recalibrate
00163     #
00164     print '--Gaincal using previous solution--'
00165     default('gaincal')
00166     
00167     Grinttable = prefix + '.rint.gcal'
00168     gaincal(vis=msfile,caltable=Grinttable,field='0', gaintype='G',gaintable=Gscantable,
00169             solint='int',combine='',interp='nearest',refant='VA02')
00170     
00171     
00172     # gaincal calibration completion time
00173     if benchmarking:
00174         gaintime3 = time.time()
00175     
00176     #
00177     #=====================================================================
00178     #
00179     # Rum accum on tables
00180     #
00181     print '--Accum on initial data--'
00182     default('accum')
00183     
00184     Acc1table = prefix + '.acc1.gcal'
00185     accum(vis=msfile,tablein='',accumtime=1.0,caltable=Acc1table,incrtable=Gscantable,
00186           field='0', calfield='0',interp='nearest')
00187     
00188     
00189     # gaincal calibration completion time
00190     if benchmarking:
00191         accumtime1 = time.time()
00192     
00193     
00194     #
00195     #=====================================================================
00196     #
00197     # Rum accum again on tables
00198     #
00199     print '--Accum using previous solution--'
00200     default('accum')
00201     
00202     Acc2table = prefix + '.acc2.gcal'
00203     accum(vis=msfile,tablein=Acc1table,caltable=Acc2table,incrtable=Grinttable,
00204           field='0', calfield='0',interp='nearest')
00205     
00206     
00207     # gaincal calibration completion time
00208     if benchmarking:
00209         accumtime2 = time.time()
00210         
00211     endProc = time.clock()
00212     endTime = time.time()
00213 
00214     # Save plot with the two table
00215     print '--Saving Plots--'
00216     saveplot = prefix + '.pdf'
00217     default('plotcal')
00218     plotcal(caltable=Ginttable,plotsymbol='+',overplot=False,field='0',markersize=10.0,
00219             showgui=False,figfile=saveplot)
00220     plotcal(caltable=Acc2table,plotsymbol='.',overplot=True,field='0',markersize=8.0,
00221             showgui=False,figfile=saveplot)
00222 
00223         
00224     # Compare Acc2table with Ginttable
00225     EPS = 1e-5
00226     total = 0
00227     fail = 0
00228     
00229     tb.open(Ginttable)
00230 #    intcol = tb.getvarcol('GAIN')
00231     intcol = tb.getvarcol('CPARAM')
00232     iflag = tb.getvarcol('FLAG')
00233     tb.close()
00234     
00235     tb.open(Acc2table)
00236     afield = tb.query('FIELD_ID == 0')
00237 #    acccol = afield.getvarcol('GAIN')
00238     acccol = afield.getvarcol('CPARAM')
00239     aflag = afield.getvarcol('FLAG')
00240     afield.done()
00241     tb.close()
00242     
00243     n1 = len(intcol)
00244     n2 = len(acccol)
00245     if n1 != n2 :
00246         print >> sys.stderr, "The two tables have different lengths"
00247         
00248      # Loop over every row,pol and get the data
00249     for i in range(1,n1,1) :
00250       row = 'r%s'%i     
00251       # polarization is 0-1
00252       for pol in range(0,2) :     
00253         
00254         # do not take flagged values
00255         if (not (iflag[row][pol] and aflag[row][pol])) :
00256             total += 1
00257             intdata = intcol[row][pol]
00258             accdata = acccol[row][pol]
00259     #        print intdata,accdata
00260             if (abs(intdata - accdata) > EPS) :
00261                 fail += 1
00262                 print >>sys.stderr, row,pol,intdata,accdata
00263             
00264     if fail > 0 :
00265         perc = fail*100/total
00266         regstate = False
00267         print >> sys.stdout, ''
00268         print >> sys.stdout, 'Regression FAILED'
00269         print >> sys.stdout, ''
00270         print >> sys.stderr, "Regression test failed: %f %% of values are different "\
00271                         "by more than the allowed maximum %s" %(perc,EPS)
00272     else :
00273         regstate = True
00274         print >> sys.stdout, ''
00275         print >> sys.stdout, 'Regression PASSED'
00276         print >> sys.stdout, ''
00277         print >> sys.stdout, "Regression tests passed. %s rows were analysed" %total
00278 
00279     print >>sys.stdout,'********* Benchmarking *****************'
00280     print >>sys.stdout,'*                                      *'
00281     print >>sys.stdout,'Total wall clock time was: '+str(endTime - startTime)
00282     print >>sys.stdout,'Total CPU        time was: '+str(endProc - startProc)
00283     print >>sys.stdout,'* Breakdown:                           *'
00284     print >>sys.stdout,'*   import       time was: '+str(importtime-startTime)
00285     print >>sys.stdout,'*   setjy        time was: '+str(setjytime-importtime)
00286     print >>sys.stdout,'*   gaincal1     time was: '+str(gaintime1-setjytime)
00287     print >>sys.stdout,'*   gaincal2     time was: '+str(gaintime2-gaintime1)
00288     print >>sys.stdout,'*   gaincal3     time was: '+str(gaintime3-gaintime2)
00289     print >>sys.stdout,'*   accum1       time was: '+str(accumtime1-gaintime3)
00290     print >>sys.stdout,'*   accum2       time was: '+str(accumtime2-accumtime1)
00291     print >>sys.stdout,'*****************************************'
00292 
00293 except Exception, instance:
00294     print >> sys.stderr, "Regression test failed for accum instance = ", instance
00295