casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
jupiter6cm_caldemo.py
Go to the documentation of this file.
00001 ######################################################################
00002 #                                                                    #
00003 # Use Case Demo Script for Jupiter 6cm VLA                           #
00004 # Trimmed down from Use Case jupiter6cm_usecase.py                   #
00005 #                                                                    #
00006 # Assumes you have already flagged using jupiter6cm_flagdemo.py      #
00007 # Will do calibration steps                                          #
00008 #                                                                    #
00009 # Updated STM 2008-05-26 (Beta Patch 2.0) new cal parameters         #
00010 # Updated STM 2008-06-12 (Beta Patch 2.0) for summer school demo     #
00011 #                                                                    #
00012 ######################################################################
00013 
00014 import time
00015 import os
00016 
00017 # 
00018 #=====================================================================
00019 #
00020 # This script has some interactive commands: scriptmode = True
00021 # if you are running it and want it to stop during interactive parts.
00022 
00023 scriptmode = True
00024 
00025 #=====================================================================
00026 #
00027 # Set up some useful variables - these will be set during the script
00028 # also, but if you want to restart the script in the middle here
00029 # they are in one place:
00030 
00031 prefix = 'jupiter6cm.demo'
00032 
00033 msfile = prefix + '.ms'
00034 
00035 #
00036 #=====================================================================
00037 #
00038 # Special prefix for this calibration demo output
00039 #
00040 calprefix = prefix + '.cal'
00041 
00042 # Clean up old files
00043 os.system('rm -rf '+calprefix+'*')
00044 
00045 #
00046 #=====================================================================
00047 # Calibration variables
00048 #
00049 # spectral windows to process
00050 usespw = ''
00051 usespwlist = ['0','1']
00052 
00053 # prior calibration to apply
00054 usegaincurve = True
00055 gainopacity = 0.0
00056 
00057 # reference antenna 11 (11=VLA:N1)
00058 calrefant = '11'
00059 
00060 gtable = calprefix + '.gcal'
00061 ftable = calprefix + '.fluxscale'
00062 atable = calprefix + '.accum'
00063 
00064 #
00065 #=====================================================================
00066 # Polarization calibration setup
00067 #
00068 dopolcal = True
00069 
00070 ptable = calprefix + '.pcal'
00071 xtable = calprefix + '.polx'
00072 
00073 # Pol leakage calibrator
00074 poldfield = '0137+331'
00075 
00076 # Pol angle calibrator
00077 polxfield = '1331+305'
00078 # At Cband the fractional polarization of this source is 0.112 and
00079 # the R-L PhaseDiff = 66deg (EVPA = 33deg)
00080 polxfpol = 0.112
00081 polxrlpd_deg = 66.0
00082 # Dictionary of IPOL in the spw
00083 polxipol = {'0' : 7.462,
00084             '1' : 7.510}
00085 
00086 # Make Stokes lists for setjy
00087 polxiquv = {}
00088 for spw in ['0','1']:
00089     ipol = polxipol[spw]
00090     fpol = polxfpol
00091     ppol = ipol*fpol
00092     rlpd = polxrlpd_deg*pi/180.0
00093     qpol = ppol*cos(rlpd)
00094     upol = ppol*sin(rlpd)
00095     polxiquv[spw] = [ipol,qpol,upol,0.0]
00096 
00097 #
00098 # Split output setup
00099 #
00100 srcname = 'JUPITER'
00101 srcsplitms = calprefix + '.' + srcname + '.split.ms'
00102 calname = '0137+331'
00103 calsplitms = calprefix + '.' + calname + '.split.ms'
00104 
00105 #
00106 #=====================================================================
00107 # Calibration Reset
00108 #=====================================================================
00109 #
00110 # Reset the CORRECTED_DATA column to data
00111 #
00112 print '--Clearcal--'
00113 default('clearcal')
00114 
00115 vis = msfile
00116 
00117 clearcal()
00118 
00119 print "Reset calibration for MS "+vis
00120 print ""
00121 #
00122 #=====================================================================
00123 # Calibration
00124 #=====================================================================
00125 #
00126 # Set the fluxes of the primary calibrator(s)
00127 #
00128 print '--Setjy--'
00129 default('setjy')
00130 
00131 print "Use setjy to set flux of 1331+305 (3C286)"
00132 
00133 vis = msfile
00134 
00135 #
00136 # 1331+305 = 3C286 is our primary calibrator
00137 field = '1331+305'     
00138 
00139 # Setjy knows about this source so we dont need anything more
00140 
00141 setjy()
00142 
00143 #
00144 # You should see something like this in the logger and casapy.log file:
00145 #
00146 # 1331+305  spwid=  0  [I=7.462, Q=0, U=0, V=0] Jy, (Perley-Taylor 99)
00147 # 1331+305  spwid=  1  [I=7.51, Q=0, U=0, V=0] Jy, (Perley-Taylor 99)
00148 # 
00149 
00150 print "Look in logger for the fluxes (should be 7.462 and 7.510 Jy)"
00151 
00152 #
00153 #=====================================================================
00154 #
00155 # Initial gain calibration
00156 #
00157 print '--Gaincal--'
00158 default('gaincal')
00159 
00160 print "Solve for antenna gains on 1331+305 and 0137+331"
00161 print "We have 2 single-channel continuum spw"
00162 print "Do not want bandpass calibration"
00163 
00164 vis = msfile
00165 
00166 # set the name for the output gain caltable
00167 caltable = gtable
00168 
00169 print "Output gain cal table will be "+gtable
00170 
00171 # Gain calibrators are 1331+305 and 0137+331 (FIELD_ID 7 and 0)
00172 # We have 2 IFs (SPW 0,1) with one channel each
00173 
00174 # selection is via the field and spw strings
00175 field = '1331+305,0137+331'
00176 spw = ''
00177 
00178 # a-priori calibration application
00179 gaincurve = usegaincurve
00180 opacity = gainopacity
00181 
00182 # scan-based G solutions for both amplitude and phase
00183 gaintype = 'G'
00184 calmode = 'ap'
00185 
00186 # one solution per scan
00187 solint = 'inf'
00188 combine = ''
00189 
00190 # do not apply parallactic angle correction (yet)
00191 parang = False
00192 
00193 # reference antenna
00194 refant = calrefant
00195 
00196 # minimum SNR 3
00197 minsnr = 3
00198 
00199 gaincal()
00200 
00201 #
00202 #=====================================================================
00203 #
00204 # Bootstrap flux scale
00205 #
00206 print '--Fluxscale--'
00207 default('fluxscale')
00208 
00209 print "Use fluxscale to rescale gain table to make new one"
00210 
00211 vis = msfile
00212 
00213 # set the name for the output rescaled caltable
00214 fluxtable = ftable
00215 
00216 print "Output scaled gain cal table is "+ftable
00217 
00218 # point to our first gain cal table
00219 caltable = gtable
00220 
00221 # we will be using 1331+305 (the source we did setjy on) as
00222 # our flux standard reference
00223 reference = '1331+305'
00224 
00225 # we want to transfer the flux to our other gain cal source 0137+331
00226 # to bring its gain amplitues in line with the absolute scale
00227 transfer = '0137+331'
00228 
00229 fluxscale()
00230 
00231 # You should see in the logger something like:
00232 #Flux density for 0137+331 in SpW=0 is:
00233 #   5.42575 +/- 0.00285011 (SNR = 1903.7, nAnt= 27)
00234 #Flux density for 0137+331 in SpW=1 is:
00235 #   5.46569 +/- 0.00301326 (SNR = 1813.88, nAnt= 27)
00236 
00237 #
00238 #---------------------------------------------------------------------
00239 # Plot calibration
00240 #
00241 print '--PlotCal--'
00242 default('plotcal')
00243 
00244 showgui = True
00245     
00246 caltable = ftable
00247 multiplot = True
00248 yaxis = 'amp'
00249 
00250 showgui = True
00251     
00252 plotcal()
00253 
00254 print ""
00255 print "-------------------------------------------------"
00256 print "Plotcal"
00257 print "Looking at amplitude in cal-table "+caltable
00258 
00259 # Pause script if you are running in scriptmode
00260 if scriptmode:
00261     user_check=raw_input('Return to continue script\n')
00262 
00263 #
00264 # Now go back and plot to file
00265 #
00266 showgui = False
00267 
00268 yaxis = 'amp'
00269 
00270 figfile = caltable + '.plotcal.amp.png'
00271 print "Plotting calibration to file "+figfile
00272 #saveinputs('plotcal',caltable.plotcal.amp.saved')
00273 plotcal()
00274 
00275 yaxis = 'phase'
00276 
00277 figfile = caltable + '.plotcal.phase.png'
00278 print "Plotting calibration to file "+figfile
00279 #saveinputs('plotcal',caltable.plotcal.phase.saved')
00280 plotcal()
00281 
00282 #
00283 #=====================================================================
00284 # Polarization Calibration
00285 #=====================================================================
00286 #
00287 if (dopolcal):
00288     print '--Polcal (D)--'
00289     default('polcal')
00290     
00291     print "Solve for polarization leakage on 0137+331"
00292     print "Pretend it has unknown polarization"
00293 
00294     vis = msfile
00295 
00296     # Start with the un-fluxscaled gain table
00297     gaintable = gtable
00298 
00299     # use settings from gaincal
00300     gaincurve = usegaincurve
00301     opacity = gainopacity
00302     
00303     # Output table
00304     caltable = ptable
00305 
00306     # Use a 3C48 tracked through a range of PA
00307     field = '0137+331'
00308     spw = ''
00309 
00310     # No need for further selection
00311     selectdata=False
00312 
00313     # Polcal mode (D+QU = unknown pol for D)
00314     poltype = 'D+QU'
00315 
00316     # One solution for entire dataset
00317     solint = 'inf'
00318     combine = 'scan'
00319 
00320     # reference antenna
00321     refant = calrefant
00322 
00323     # minimum SNR 3
00324     minsnr = 3
00325 
00326     #saveinputs('polcal',calprefix+'.polcal.saved')
00327     polcal()
00328     
00329     #=====================================================================
00330     #
00331     # List polcal solutions
00332     #
00333     print '--Listcal (PolD)--'
00334 
00335     listfile = caltable + '.list'
00336 
00337     print "Listing calibration to file "+listfile
00338 
00339     listcal()
00340     
00341     #=====================================================================
00342     #
00343     # Plot polcal solutions
00344     #
00345     print '--Plotcal (PolD)--'
00346     
00347     iteration = ''
00348     showgui = False
00349     
00350     xaxis = 'real'
00351     yaxis = 'imag'
00352     figfile = caltable + '.plotcal.reim.png'
00353     print "Plotting calibration to file "+figfile
00354     #saveinputs('plotcal',caltable+'.plotcal.reim.saved')
00355     plotcal()
00356 
00357     xaxis = 'antenna'
00358     yaxis = 'amp'
00359     figfile = caltable + '.plotcal.antamp.png'
00360     print "Plotting calibration to file "+figfile
00361     #saveinputs('plotcal',caltable+'.plotcal.antamp.saved')
00362     plotcal()
00363 
00364     xaxis = 'antenna'
00365     yaxis = 'phase'
00366     figfile = caltable + '.plotcal.antphase.png'
00367     print "Plotting calibration to file "+figfile
00368     #saveinputs('plotcal',caltable+'.plotcal.antphase.saved')
00369     plotcal()
00370 
00371     xaxis = 'antenna'
00372     yaxis = 'snr'
00373     figfile = caltable + '.plotcal.antsnr.png'
00374     print "Plotting calibration to file "+figfile
00375     #saveinputs('plotcal',caltable+'.plotcal.antsnr.saved')
00376     plotcal()
00377 
00378     #=====================================================================
00379     # Do Chi (X) pol angle calibration
00380     #=====================================================================
00381     # First set the model
00382     print '--Setjy--'
00383     default('setjy')
00384         
00385     vis = msfile
00386         
00387     print "Use setjy to set IQU fluxes of "+polxfield
00388     field = polxfield
00389     
00390     for spw in usespwlist:
00391         fluxdensity = polxiquv[spw]
00392         
00393         #saveinputs('setjy',calprefix+'.setjy.polspw.'+spw+'.saved')
00394         setjy()
00395     
00396     #
00397     # Polarization (X-term) calibration
00398     #
00399     print '--PolCal (X)--'
00400     default('polcal')
00401     
00402     print "Polarization R-L Phase Calibration (linear approx)"
00403     
00404     vis = msfile
00405     
00406     # Start with the G and D tables
00407     gaintable = [gtable,ptable]
00408     
00409     # use settings from gaincal
00410     gaincurve = usegaincurve
00411     opacity = gainopacity
00412     
00413     # Output table
00414     caltable = xtable
00415 
00416     # previously set with setjy
00417     field = polxfield
00418     spw = ''
00419     
00420     selectdata=False
00421     
00422     # Solve for Chi
00423     poltype = 'X'
00424     solint = 'inf'
00425     combine = 'scan'
00426     
00427     # reference antenna
00428     refant = calrefant
00429     
00430     # minimum SNR 3
00431     minsnr = 3
00432     
00433     #saveinputs('polcal',calprefix+'.polcal.X.saved')
00434     polcal()
00435     
00436 #=====================================================================
00437 # Apply the Calibration
00438 #=====================================================================
00439 #
00440 # Interpolate the gains onto Jupiter (and others)
00441 #
00442 # print '--Accum--'
00443 # default('accum')
00444 # 
00445 # print "This will interpolate the gains onto Jupiter"
00446 # 
00447 # vis = msfile
00448 # 
00449 # tablein = ''
00450 # incrtable = ftable
00451 # calfield = '1331+305, 0137+331'
00452 # 
00453 # # set the name for the output interpolated caltable
00454 # caltable = atable
00455 # 
00456 # print "Output cumulative gain table will be "+atable
00457 # 
00458 # # linear interpolation
00459 # interp = 'linear'
00460 # 
00461 # # make 10s entries
00462 # accumtime = 10.0
00463 # 
00464 # accum()
00465 #
00466 # NOTE: bypassing this during testing
00467 atable = ftable
00468 
00469 # #=====================================================================
00470 #
00471 # Correct the data
00472 # (This will put calibrated data into the CORRECTED_DATA column)
00473 #
00474 print '--ApplyCal--'
00475 default('applycal')
00476 
00477 print "This will apply the calibration to the DATA"
00478 print "Fills CORRECTED_DATA"
00479 
00480 vis = msfile
00481 
00482 # Start with the interpolated fluxscale/gain table
00483 gaintable = [atable,ptable,xtable]
00484 
00485 # use settings from gaincal
00486 gaincurve = usegaincurve
00487 opacity = gainopacity
00488 
00489 # select the fields
00490 field = '1331+305,0137+331,JUPITER'
00491 spw = ''
00492 selectdata = False
00493 
00494 # IMPORTANT set parang=True for polarization
00495 parang = True
00496 
00497 # do not need to select subset since we did accum
00498 # (note that correct only does 'nearest' interp)
00499 gainfield = ''
00500 
00501 applycal()
00502 
00503 #
00504 #=====================================================================
00505 #
00506 # Now split the Jupiter target data
00507 #
00508 print '--Split Jupiter--'
00509 default('split')
00510 
00511 vis = msfile
00512 
00513 # Now we write out the corrected data to a new MS
00514 
00515 # Select the Jupiter field
00516 field = srcname
00517 spw = ''
00518 
00519 # pick off the CORRECTED_DATA column
00520 datacolumn = 'corrected'
00521 
00522 # Make an output vis file
00523 outputvis = srcsplitms
00524 
00525 print "Split "+field+" data into new ms "+srcsplitms
00526 
00527 split()
00528 
00529 # Also split out 0137+331 as a check
00530 field = calname
00531 
00532 outputvis = calsplitms
00533 
00534 print "Split "+field+" data into new ms "+calsplitms
00535 
00536 split()
00537 
00538 #=====================================================================
00539 # Force scratch column creation so plotxy will work
00540 #
00541 vis = srcsplitms
00542 clearcal()
00543 
00544 vis = calsplitms
00545 clearcal()
00546 
00547 #=====================================================================
00548 # Use Plotxy to look at the split calibrated data
00549 #
00550 print '--Plotxy--'
00551 default('plotxy')
00552 
00553 vis = srcsplitms
00554 selectdata = True
00555 
00556 # Plot only the RR and LL for now
00557 correlation = 'RR LL'
00558 
00559 # Plot amplitude vs. uvdist
00560 xaxis = 'uvdist'
00561 datacolumn = 'data'
00562 multicolor = 'both'
00563 
00564 iteration = ''
00565 selectplot = True
00566 interactive = True
00567 
00568 field = 'JUPITER'
00569 yaxis = 'amp'
00570 # Use the field name as the title
00571 title = field+"  "
00572 
00573 plotxy()
00574 
00575 print ""
00576 print "-----------------------------------------------------"
00577 print "Plotting JUPITER corrected visibilities"
00578 print "Look for outliers"
00579 
00580 # Pause script if you are running in scriptmode
00581 if scriptmode:
00582     user_check=raw_input('Return to continue script\n')
00583 
00584 # Now go back and plot to files
00585 interactive = False
00586 
00587 #
00588 # First the target
00589 #
00590 vis = srcsplitms
00591 field = srcname
00592 yaxis = 'amp'
00593 # Use the field name as the title
00594 title = field+"  "
00595 figfile = vis + '.plotxy.amp.png'
00596 print "Plotting to file "+figfile
00597 #saveinputs('plotxy',vis+'.plotxy.amp.saved')
00598 
00599 plotxy()
00600 
00601 yaxis = 'phase'
00602 # Use the field name as the title
00603 figfile = vis + '.plotxy.phase.png'
00604 print "Plotting to file "+figfile
00605 #saveinputs('plotxy',vis+'.plotxy.phase.saved')
00606 
00607 plotxy()
00608 
00609 #
00610 # Now the calibrator
00611 #
00612 vis = calsplitms
00613 field = calname
00614 yaxis = 'amp'
00615 # Use the field name as the title
00616 title = field+"  "
00617 figfile = vis + '.plotxy.amp.png'
00618 print "Plotting to file "+figfile
00619 #saveinputs('plotxy',vis+'.plotxy.amp.saved')
00620 
00621 plotxy()
00622 
00623 yaxis = 'phase'
00624 # Use the field name as the title
00625 figfile = vis + '.plotxy.phase.png'
00626 print "Plotting to file "+figfile
00627 #saveinputs('plotxy',vis+'.plotxy.phase.saved')
00628 
00629 plotxy()
00630 
00631 #
00632 #=====================================================================
00633 # Done
00634 
00635 print 'Calibration completed for '+calprefix