casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
jupiter6cm_poldemo.py
Go to the documentation of this file.
00001 ######################################################################
00002 #                                                                    #
00003 # jupiter6cm_poldemo.py                                              #
00004 #                                                                    #
00005 # Use Case Demo Script for Jupiter 6cm VLA                           #
00006 # Trimmed down from Use Case jupiter6cm_usecase.py                   #
00007 #                                                                    #
00008 # Assumes:                                                           #
00009 #    you have already flagged using jupiter6cm_flagdemo.py           #
00010 #    calibrated using jupiter6cm_caldemo.py                          #
00011 #    imaged/self-calibratied jupiter6cm_imagingdemo.py               #
00012 # Will do polarization imaging                                       #
00013 #                                                                    #
00014 # Updated STM 2008-05-26 (Beta Patch 2.0) new clean task             #
00015 # Updated STM 2008-06-05 (Beta Patch 2.0) complex pol example        #
00016 # Updated STM 2008-06-12 (Beta Patch 2.0) for summer school demo     #
00017 #                                                                    #
00018 ######################################################################
00019 
00020 import time
00021 import os
00022 
00023 # 
00024 #=====================================================================
00025 #
00026 # This script has some interactive commands: scriptmode = True
00027 # if you are running it and want it to stop during interactive parts.
00028 
00029 scriptmode = True
00030 
00031 #=====================================================================
00032 #
00033 # Set up some useful variables - these will be set during the script
00034 # also, but if you want to restart the script in the middle here
00035 # they are in one place:
00036 
00037 prefix='jupiter6cm.demo'
00038 
00039 msfile = prefix + '.ms'
00040 
00041 #
00042 # Special prefix for calibration demo output
00043 #
00044 calprefix = prefix + '.cal'
00045 
00046 srcname = 'JUPITER'
00047 srcsplitms = calprefix + '.' + srcname + '.split.ms'
00048 
00049 #
00050 #=====================================================================
00051 #
00052 # Special prefix for this polarization imaging demo output
00053 #
00054 polprefix = prefix + '.polimg'
00055 
00056 # Clean up old files
00057 os.system('rm -rf '+polprefix+'*')
00058 
00059 #
00060 #=====================================================================
00061 #
00062 # Imaging parameters
00063 #
00064 # This is D-config VLA 6cm (4.85GHz) obs
00065 # Check the observational status summary
00066 # Primary beam FWHM = 45'/f_GHz = 557"
00067 # Synthesized beam FWHM = 14"
00068 # RMS in 10min (600s) = 0.06 mJy (thats now, but close enough)
00069 
00070 # Set the output image size and cell size (arcsec)
00071 # 4" will give 3.5x oversampling
00072 clncell = [4.,4.]
00073 
00074 # 280 pix will cover to 2xPrimaryBeam
00075 # clean will say to use 288 (a composite integer) for efficiency
00076 #clnalg = 'clark'
00077 clnalg = 'hogbom'
00078 clnmode = 'csclean'
00079 clnimsize = [288,288]
00080 
00081 # iterations
00082 clniter = 10000
00083 
00084 # Also set flux residual threshold (0.04 mJy)
00085 # From our listobs:
00086 # Total integration time = 85133.2 seconds
00087 # With rms of 0.06 mJy in 600s ==> rms = 0.005 mJy
00088 # Set to 10x thermal rms
00089 clnthreshold=0.05
00090 
00091 polimname = polprefix + '.clean'
00092 polimage  = polimname+'.image'
00093 polmodel  = polimname+'.model'
00094 polresid  = polimname+'.residual'
00095 polmask   = polimname+'.clean_interactive.mask'
00096 
00097 #
00098 # Other files
00099 #
00100 ipolimage = polimage+'.I'
00101 qpolimage = polimage+'.Q'
00102 upolimage = polimage+'.U'
00103 
00104 poliimage = polimage+'.poli'
00105 polaimage = polimage+'.pola'
00106 
00107 #
00108 #=====================================================================
00109 # Polarization Imaging
00110 #=====================================================================
00111 #
00112 print '--Clean (Polarization)--'
00113 default('clean')
00114 
00115 print "Now clean polarized data"
00116 
00117 vis = srcsplitms
00118 
00119 imagename = polimname
00120 
00121 field = '*'
00122 spw = ''
00123 mode = 'mfs'
00124 gain = 0.1
00125 
00126 # Polarization
00127 stokes = 'IQUV'
00128 
00129 psfmode = clnalg
00130 imagermode = clnmode
00131 
00132 imsize = clnimsize
00133 cell = clncell
00134 niter = clniter
00135 threshold = clnthreshold
00136 
00137 weighting = 'briggs'
00138 robust = 0.5
00139 
00140 interactive = True
00141 npercycle = 100
00142 
00143 saveinputs('clean',imagename+'.clean.saved')
00144 clean()
00145 
00146 print ""
00147 print "----------------------------------------------------"
00148 print "Clean"
00149 print "Final restored clean image is "+polimage
00150 print "Final clean model is "+polmodel
00151 print "The clean residual image is "+polresid
00152 print "Your final clean mask is "+polmask
00153 
00154 #
00155 #=====================================================================
00156 # Image Analysis
00157 #=====================================================================
00158 #
00159 # Polarization statistics
00160 print '--Final Pol Imstat--'
00161 default('imstat')
00162 
00163 imagename = polimage
00164 
00165 on_statistics = {}
00166 off_statistics = {}
00167 
00168 # lower right corner of the image (clnimsize = [288,288])
00169 onbox = ''
00170 # lower right corner of the image (clnimsize = [288,288])
00171 offbox = '216,1,287,72'
00172 
00173 for stokes in ['I','Q','U','V']:
00174     box = onbox
00175     on_statistics[stokes] = imstat()
00176     box = offbox
00177     off_statistics[stokes] = imstat()
00178 
00179 #
00180 # Peel off some Q and U planes
00181 #
00182 print '--Immath--'
00183 default('immath')
00184 
00185 mode = 'evalexpr'
00186 
00187 stokes = 'I'
00188 outfile = ipolimage
00189 expr = '\"'+polimage+'\"'
00190 
00191 immath()
00192 print "Created I image "+outfile
00193 
00194 stokes = 'Q'
00195 outfile = qpolimage
00196 expr = '\"'+polimage+'\"'
00197 
00198 immath()
00199 print "Created Q image "+outfile
00200 
00201 stokes = 'U'
00202 outfile = upolimage
00203 expr = '\"'+polimage+'\"'
00204 
00205 immath()
00206 print "Created U image "+outfile
00207 
00208 #
00209 #---------------------------------------------------------------------
00210 # Now make POLI and POLA images
00211 #
00212 stokes = ''
00213 outfile = poliimage
00214 mode = 'poli'
00215 imagename = [qpolimage,upolimage]
00216 # Use our rms above for debiasing
00217 mysigma = 0.5*( off_statistics['Q']['rms'][0] + off_statistics['U']['rms'][0] )
00218 #sigma = str(mysigma)+'Jy/beam'
00219 # This does not work well yet
00220 sigma = '0.0Jy/beam'
00221 
00222 immath()
00223 print "Created POLI image "+outfile
00224 
00225 outfile = polaimage
00226 mode = 'pola'
00227 
00228 immath()
00229 print "Created POLA image "+outfile
00230 
00231 #
00232 #---------------------------------------------------------------------
00233 # Save statistics of these images
00234 default('imstat')
00235 
00236 imagename = poliimage
00237 stokes = ''
00238 box = onbox
00239 on_statistics['POLI'] = imstat()
00240 box = offbox
00241 off_statistics['POLI'] = imstat()
00242 
00243 #
00244 #
00245 #---------------------------------------------------------------------
00246 # Display clean I image in viewer but with polarization vectors
00247 #
00248 # If you did not do interactive clean, bring up viewer manually
00249 viewer(polimage,'image')
00250 
00251 print "Displaying pol I now.  You should overlay pola vectors"
00252 print "Bring up the Load Data panel:"
00253 print ""
00254 print "Use LEL for POLA VECTOR with cut above 6*mysigma in POLI = "+str(6*mysigma)
00255 print "For example:"
00256 print "\'"+polaimage+"\'[\'"+poliimage+"\'>0.0048]"
00257 print ""
00258 print "In the Data Display Options for the vector plot:"
00259 print "  Set the x,y increments to 2 (default is 3)"
00260 print "  Use an extra rotation this 90deg to get B field"
00261 print "Note the lengths are all equal. You can fiddle these."
00262 print ""
00263 print "You can also load the poli image as contours"
00264 
00265 # Pause script if you are running in scriptmode
00266 if scriptmode:
00267     user_check=raw_input('Return to continue script\n')
00268 
00269 # NOTE: the LEL will be something like
00270 # 'jupiter6cm.usecase.polimg.clean.image.pola'['jupiter6cm.usecase.polimg.clean.image.poli'>0.005]
00271 
00272 #
00273 # NOTE: The viewer can take complex images to make Vector plots, although
00274 # the image analysis tasks (and ia tool) cannot yet handle these.  But we
00275 # can use the imagepol tool (which is not imported by default) to make
00276 # a complex image of the linear polarized intensity for display.
00277 # See CASA User Reference Manual:
00278 # http://casa.nrao.edu/docs/casaref/imagepol-Tool.html
00279 #
00280 # Make an imagepol tool and open the clean image 
00281 po = casac.imagepol()
00282 po.open(polimage)
00283 # Use complexlinpol to make a Q+iU image
00284 complexlinpolimage = polimname + '.cmplxlinpol'
00285 po.complexlinpol(complexlinpolimage)
00286 po.close()
00287 
00288 # You can now display this in the viewer, in particular overlay this
00289 # over the intensity raster with the poli contours.  The vector lengths
00290 # will be proportional to the polarized intensity.  You can play with
00291 # the Data Display Options panel for vector spacing and length.
00292 # You will want to have this masked, like the pola image above, on
00293 # the polarized intensity.  When you load the image, use the LEL:
00294 # 'jupiter6cm.usecase.polimg.clean.cmplxlinpol'['jupiter6cm.usecase.polimg.clean.image.poli'>0.005]
00295 
00296 #=====================================================================
00297 #
00298 # Print results
00299 #
00300 print ""
00301 print ' Jupiter polarization results '
00302 print ' ============================ '
00303 print ''
00304 for stokes in ['I','Q','U','V','POLI']:
00305     print ''
00306     print ' =============== '
00307     print ''
00308     print ' Polarization (Stokes '+stokes+'):'
00309     mymax = on_statistics[stokes]['max'][0]
00310     mymin = on_statistics[stokes]['min'][0]
00311     myrms = off_statistics[stokes]['rms'][0]
00312     absmax = max(mymax,mymin)
00313     mydra = absmax/myrms
00314     print '   Clean image  ON-SRC max = ',mymax
00315     print '   Clean image  ON-SRC min = ',mymin
00316     print '   Clean image OFF-SRC rms = ',myrms
00317     print '   Clean image dynamic rng = ',mydra
00318 
00319 
00320 print '--- Done ---'
00321 
00322 #
00323 #=====================================================================