casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
ngc4826_tutorial.py
Go to the documentation of this file.
00001 
00002 ##########################################################################
00003 #                                                                        #
00004 # Demo Script for NGC 4826 (BIMA line data)                              #
00005 #                                                                        #
00006 # Converted by  STM 2008-05-27 (Beta Patch 2.0) new tasking/clean/cal    #
00007 # Updated by     CB 2008-05-30                  start from raw data      #
00008 # Updated by    STM 2008-06-01                  scriptmode, plotting     #
00009 # Updated by CB,STM 2008-06-02                  improvements             #
00010 # Updated by CB,STM 2008-06-03                  bigger cube, pbcor       #
00011 # Updated by CB,STM 2008-06-04                  pbcor stuff              #
00012 # Updated by CB,STM 2008-06-04                  tutorial script          #
00013 # Updated by CB     2008-06-05                  small tweaks             #
00014 # Updated by CB,STM 2008-06-12                  final revisions (DS)     #
00015 # Updated by STM    2008-06-14                  post-school fix          #
00016 #                                                                        #
00017 # N4826 - BIMA SONG Data                                                 #
00018 #                                                                        #
00019 # This data is from the BIMA Survey of Nearby Galaxies (BIMA SONG)       #
00020 # Helfer, Thornley, Regan, et al., 2003, ApJS, 145, 259                  #
00021 # Many thanks to Michele Thornley for providing the data and description #
00022 #                                                                        #
00023 # First day of observations only                                         #
00024 # Trial for summer school                                                #
00025 #                                                                        #
00026 # Script Notes:                                                          #
00027 #    o The "default" commands are not necessary, but are included        #
00028 #      in case we want to change from function calls to globals          #
00029 #    o This script has some interactive commands, such as with plotxy    #
00030 #      and the viewer.  This script will stop and require a              #
00031 #      carriage-return to continue at these points.                      #
00032 #    o Sometimes cut-and-paste of a series of lines from this script     #
00033 #      into the casapy terminal will get garbled (usually a single       #
00034 #      dropped character). In this case, try fewer lines, like groups    #
00035 #      of 4-6.                                                           #
00036 #                                                                        #
00037 ##########################################################################
00038 import os
00039 
00040 # Some diagnostic stuff
00041 pl.ion()
00042 pl.clf()
00043 
00044 #
00045 ##########################################################################
00046 #                                                                        
00047 # Clear out previous run results
00048 os.system('rm -rf ngc4826.tutorial.*')
00049 
00050 # Sets a shorthand for the ms, not necessary
00051 prefix='ngc4826.tutorial'
00052 msfile = prefix + '.16apr98.ms'
00053 
00054 print 'Tutorial Script for BIMASONG NGC4826 Mosaic'
00055 print 'Will do: import, flagging, calibration, imaging'
00056 print ''
00057 #
00058 ##########################################################################
00059 #
00060 # 
00061 ##########################################################################
00062 #
00063 # N4826 - BIMA SONG Data
00064 # 16apr98
00065 #       source=ngc4826
00066 #       phasecal=1310+323
00067 #       fluxcal=3c273, Flux = 23 Jy on 16apr98
00068 #       passcal= none - data were observed with online bandpass correction.
00069 #
00070 # NOTE: This data has been filled into MIRIAD, line-length correction 
00071 #       done, and then exported as separate files for each source.
00072 #       3c273 was not line length corrected since it was observed
00073 #       for such a short amount of time that it did not need it.  
00074 #
00075 # From miriad: source Vlsr = 408; delta V is 20 km/s 
00076 #
00077 ##########################################################################
00078 # Import and concatenate sources
00079 ##########################################################################
00080 #
00081 # USB spectral windows written separately by miriad for 16apr98
00082 # Assumes these are in sub-directory called "fitsfiles" of working directory
00083 print '--Importuvfits (16apr98)--'
00084 default('importuvfits')
00085 
00086 print "Starting from the uvfits files exported by miriad"
00087 print "The USB spectral windows were written separately by miriad for 16apr98"
00088 
00089 importuvfits(fitsfile='fitsfiles/3c273.fits5', vis='ngc4826.tutorial.3c273.5.ms')
00090 
00091 importuvfits(fitsfile='fitsfiles/3c273.fits6', vis='ngc4826.tutorial.3c273.6.ms')
00092 
00093 importuvfits(fitsfile='fitsfiles/3c273.fits7', vis='ngc4826.tutorial.3c273.7.ms')
00094 
00095 importuvfits(fitsfile='fitsfiles/3c273.fits8', vis='ngc4826.tutorial.3c273.8.ms')
00096 
00097 importuvfits(fitsfile='fitsfiles/1310+323.ll.fits9', vis='ngc4826.tutorial.1310+323.ll.9.ms')
00098 
00099 importuvfits(fitsfile='fitsfiles/1310+323.ll.fits10', vis='ngc4826.tutorial.1310+323.ll.10.ms')
00100 
00101 importuvfits(fitsfile='fitsfiles/1310+323.ll.fits11', vis='ngc4826.tutorial.1310+323.ll.11.ms')
00102 
00103 importuvfits(fitsfile='fitsfiles/1310+323.ll.fits12', vis='ngc4826.tutorial.1310+323.ll.12.ms')
00104 
00105 importuvfits(fitsfile='fitsfiles/1310+323.ll.fits13', vis='ngc4826.tutorial.1310+323.ll.13.ms')
00106 
00107 importuvfits(fitsfile='fitsfiles/1310+323.ll.fits14', vis='ngc4826.tutorial.1310+323.ll.14.ms')
00108 
00109 importuvfits(fitsfile='fitsfiles/1310+323.ll.fits15', vis='ngc4826.tutorial.1310+323.ll.15.ms')
00110 
00111 importuvfits(fitsfile='fitsfiles/1310+323.ll.fits16', vis='ngc4826.tutorial.1310+323.ll.16.ms')
00112 
00113 importuvfits(fitsfile='fitsfiles/ngc4826.ll.fits5', vis='ngc4826.tutorial.ngc4826.ll.5.ms')
00114 
00115 importuvfits(fitsfile='fitsfiles/ngc4826.ll.fits6', vis='ngc4826.tutorial.ngc4826.ll.6.ms')
00116 
00117 importuvfits(fitsfile='fitsfiles/ngc4826.ll.fits7', vis='ngc4826.tutorial.ngc4826.ll.7.ms')
00118 
00119 importuvfits(fitsfile='fitsfiles/ngc4826.ll.fits8', vis='ngc4826.tutorial.ngc4826.ll.8.ms')
00120 
00121 #
00122 ##########################################################################
00123 #
00124 print '--Concat--'
00125 default('concat')
00126 
00127 concat(vis=['ngc4826.tutorial.3c273.5.ms',
00128             'ngc4826.tutorial.3c273.6.ms',
00129             'ngc4826.tutorial.3c273.7.ms',
00130             'ngc4826.tutorial.3c273.8.ms',
00131             'ngc4826.tutorial.1310+323.ll.9.ms',
00132             'ngc4826.tutorial.1310+323.ll.10.ms',
00133             'ngc4826.tutorial.1310+323.ll.11.ms',
00134             'ngc4826.tutorial.1310+323.ll.12.ms',
00135             'ngc4826.tutorial.1310+323.ll.13.ms',
00136             'ngc4826.tutorial.1310+323.ll.14.ms',
00137             'ngc4826.tutorial.1310+323.ll.15.ms',
00138             'ngc4826.tutorial.1310+323.ll.16.ms',
00139             'ngc4826.tutorial.ngc4826.ll.5.ms',
00140             'ngc4826.tutorial.ngc4826.ll.6.ms',
00141             'ngc4826.tutorial.ngc4826.ll.7.ms',
00142             'ngc4826.tutorial.ngc4826.ll.8.ms'],
00143        concatvis='ngc4826.tutorial.ms',
00144        freqtol="",dirtol="1arcsec",async=False)
00145 
00146 #
00147 ##########################################################################
00148 #
00149 # TUTORIAL NOTES:
00150 #
00151 # You can invoke tasks in two ways:
00152 #
00153 # (1) As function calls with arguments as shown above for concat and used
00154 #     extensively in this script, e.g.
00155 #
00156 #        task( par1=val1, par2=val2, ... )
00157 #
00158 #     with parameters set as arguments in the call.  Note that in this
00159 #     case, the global parameter values are NOT used or changed, and any
00160 #     task parameters that are not specified as arguments to the call
00161 #     will be defaulted to the task-specific default values (see the
00162 #     "help task" description).
00163 #
00164 # (2) By setting the values of the global parameters and then using the
00165 #     "go" command (if taskname is set) or calling the task with no
00166 #     arguments.  For example:
00167 #
00168 #        default task
00169 #        par1 = val1
00170 #        par2 = val2
00171 #        ...
00172 #        inp
00173 #        task()
00174 #
00175 #     In this case, the "default" command sets the parmeters to their
00176 #     task defaults, and sets the "taskname" paramter to the task to be
00177 #     run.  The "inp" command displays the current values for the task
00178 #     parameters.  Then the call with no arguments runs with the globals.
00179 #
00180 #     Warning: "go" does not work inside scripts. See Cookbook.
00181 #
00182 # Using the concat call above as an example, we would do:
00183 #
00184 #default('concat')
00185 #
00186 #vis = ['ngc4826.tutorial.3c273.5.ms',
00187 #       'ngc4826.tutorial.3c273.6.ms',
00188 #       'ngc4826.tutorial.3c273.7.ms',
00189 #       'ngc4826.tutorial.3c273.8.ms',
00190 #       'ngc4826.tutorial.1310+323.ll.9.ms',
00191 #       'ngc4826.tutorial.1310+323.ll.10.ms',
00192 #       'ngc4826.tutorial.1310+323.ll.11.ms',
00193 #       'ngc4826.tutorial.1310+323.ll.12.ms',
00194 #       'ngc4826.tutorial.1310+323.ll.13.ms',
00195 #       'ngc4826.tutorial.1310+323.ll.14.ms',
00196 #       'ngc4826.tutorial.1310+323.ll.15.ms',
00197 #       'ngc4826.tutorial.1310+323.ll.16.ms',       
00198 #       'ngc4826.tutorial.ngc4826.ll.5.ms',
00199 #       'ngc4826.tutorial.ngc4826.ll.6.ms',
00200 #       'ngc4826.tutorial.ngc4826.ll.7.ms',
00201 #       'ngc4826.tutorial.ngc4826.ll.8.ms']
00202 #
00203 #concatvis='ngc4826.tutorial.ms'
00204 #freqtol = ""
00205 #dirtol = "1arcsec"
00206 #async=False
00207 #
00208 #concat()
00209 
00210 #
00211 ##########################################################################
00212 #
00213 # Fix up the MS (temporary, changes to importfits underway)
00214 #
00215 print '--Fixing up spw rest frequencies in MS--'
00216 vis='ngc4826.tutorial.ms'
00217 tb.open(vis+'/SOURCE',nomodify=false)
00218 spwid=tb.getcol('SPECTRAL_WINDOW_ID')
00219 spwid.setfield(-1,int)
00220 tb.putcol('SPECTRAL_WINDOW_ID',spwid)
00221 tb.close()
00222 
00223 # This ensures that the rest freq will be found for all spws. 
00224 
00225 #
00226 ##########################################################################
00227 # 16 APR Calibration
00228 ##########################################################################
00229 print '--Clearcal--'
00230 print 'Create scratch columns and initialize in '+'ngc4826.tutorial.ms'
00231 
00232 # Force create/initialize of scratch columns
00233 # NOTE: plotxy will not run properly without this step.
00234 #
00235 clearcal(vis='ngc4826.tutorial.ms')
00236 
00237 #
00238 ##########################################################################
00239 #
00240 # List contents of MS
00241 #
00242 print '--Listobs--'
00243 listobs(vis='ngc4826.tutorial.ms')
00244 
00245 # Should see the listing included at the end of this script
00246 #
00247 
00248 print "There are 3 fields observed in a total of 16 spectral windows"
00249 print "   field=0    3c273    spwids 0,1,2,3               64 chans "
00250 print "   field=1    1310+323 spwids 4,5,6,7,8,9,10,11     32 chans "
00251 print "   field=2~8  NGC4826  spwids 12,13,14,15           64 chans "
00252 print ""
00253 print "See listobs summary in logger"
00254 #
00255 ##########################################################################
00256 # Plotting and Flagging
00257 ##########################################################################
00258 #
00259 # The plotxy task is the interactive x-y display and flagging GUI
00260 #
00261 print '--Plotxy--'
00262 default(plotxy)
00263 
00264 # Here we will suggest things to plot, and actually only do a few
00265 # (near the end of this task block).  If you like you can
00266 # uncomment these when you run this script
00267 #
00268 # First look at amplitude as a funciton of uv-distance using an
00269 # average over all channels for each source.
00270 #
00271 # Interactive plotxy
00272 #plotxy(vis='ngc4826.tutorial.ms',xaxis='uvdist',yaxis='amp',field='0',spw='0~3',
00273 #       averagemode='vector',width='1000',
00274 #       selectplot=True,title='Field 0 SPW 0~3')
00275 #       
00276 # Pause script if you are running in scriptmode
00277 #user_check=raw_input('Return to continue script\n')
00278 
00279 # NOTE: width here needs to be larger than combination of all channels
00280 # selected with spw and/or field. Since field and spw are unique in this
00281 # case, both don't need to be specified, however plotting is much faster
00282 # if you "help it" by selecting both.
00283 #
00284 # Now average over all times across scans but not over channel and
00285 # plot versus channel. There are four 64-channel spws set end-to-end
00286 # by plotxy You should see bad edge channels in each spw segment We
00287 # will flag these (non-interactively) later
00288 #
00289 # Interactive plotxy
00290 #plotxy(vis='ngc4826.tutorial.ms',xaxis='channel',yaxis='amp',field='0',spw='0~3',
00291 #       averagemode='vector',timebin='1e7',crossscans=True,
00292 #       selectplot=True,newplot=False,title='Field 0 SPW 0~3')
00293 #
00294 # Pause script if you are running in scriptmode
00295 #user_check=raw_input('Return to continue script\n')
00296 
00297 # You can also plot versus velocity by setting xaxis='velocity'
00298 #
00299 # You might do this for all the other spw/field combos
00300 #
00301 
00302 # You can also non-interactively plotxy to a file
00303 # (note this works only if iteration is not set)
00304 # Also, see how we use Python variables to make this easier
00305 #
00306 #field = '0'
00307 #spw = '0~3'
00308 #figfile = 'ngc4826.tutorial.ms' + '.plotxy.'+field+'.spectrum.raw.png'
00309 #plotxy(vis='ngc4826.tutorial.ms',xaxis='velocity',yaxis='amp',field=field,spw=spw,
00310 #       averagemode='vector',timebin='1e7',crossscans=True,
00311 #       selectplot=True,newplot=False,title='Field '+field+' SPW '+spw,
00312 #       interactive=False,figfile=figfile)
00313 
00314 # Now lets look at the target source, the first of the NGC4826 mosaic fields
00315 # which are 2~8 in this MS.
00316 #
00317 # Since we are plotting versus velocity we can clearly see the bad edge
00318 # channels and the overlap between spw
00319 #
00320 # There is nothing terribly wrong with this data and again we will flag the
00321 # edge channels non-interactively later for consistency.
00322 #
00323 # Normally, if there were obviously bad data, you would flag it here
00324 # before calibration.  To do this, hit the Mark Region button, then draw a box around
00325 # some of the moderately high outliers, and then Flag.
00326 #
00327 # But this data is relatively clean, and flagging will not improve results.
00328 #
00329 # Interactive plotxy
00330 plotxy(vis='ngc4826.tutorial.ms',xaxis='velocity',yaxis='amp',field='2',spw='12~15',
00331        averagemode='vector',timebin='1e7',crossscans=True,
00332        selectplot=True,newplot=False,title='Field 2 SPW 12~15')
00333 
00334 print "You could Mark Region around outliers and Flag"
00335 # Pause script if you are running in scriptmode
00336 user_check=raw_input('Return to continue script\n')
00337 
00338 #
00339 # You could set up a Python loop to do all the N4826 fields, like this:
00340 #
00341 #for fld in range(2,9):
00342 #       field = str(fld)
00343 #       plotxy(vis='ngc4826.tutorial.ms',xaxis='velocity',yaxis='amp',field=field,spw=spw,
00344 #              averagemode='vector',timebin='1e7',crossscans=True,
00345 #              selectplot=True,newplot=False,title='Field '+field+' SPW '+spw)
00346 #
00347 #       print "Nominally, Mark Region around outliers and Flag"
00348 #       # Pause script if you are running in scriptmode
00349 #       user_check=raw_input('Return to continue script\n')
00350 
00351 # Back to first field.
00352 # You can also have it iterate over baselines, using Next to advance
00353 # Ignore baselines 1:1, 2:2 etc. as they would correspond to autocorrelations
00354 # if they were present (they are not in this dataset)
00355 #
00356 # Interactive plotxy
00357 #plotxy(vis='ngc4826.tutorial.ms',xaxis='channel',yaxis='amp',field='0',spw='0~3',
00358 #       averagemode='vector',timebin='1e7',crossscans=True,
00359 #       iteration='baseline',
00360 #       selectplot=True,newplot=False,title='Field 0 SPW 0~3')
00361 #       
00362 # Pause script if you are running in scriptmode
00363 #user_check=raw_input('Return to continue script\n')
00364 
00365 #
00366 # Finally, look for bad data. Here we look at field 8 w/o averaging
00367 plotxy(vis='ngc4826.tutorial.ms',xaxis='time',yaxis='amp',field='8',spw='12~15',
00368        selectplot=True,newplot=False,title='Field 8 SPW 12~15')
00369 
00370 print "You can see some bad data here"
00371 print "Mark Region and Locate, look in logger"
00372 print "This is a correlator glitch in baseline 3-9 at 06:19:30"
00373 print "PLEASE DON\'T FLAG ANYTHING HERE. THE SCRIPT WILL DO IT!"
00374 print "In a normal session you could Mark Region and Flag."
00375 print "Here we will use flagdata instead."
00376 # Pause script if you are running in scriptmode
00377 user_check=raw_input('Return to continue script\n')
00378 
00379 # If you change xaxis='channel' you see its all channels
00380 #
00381 ##########################################################################
00382 #
00383 # Flag end channels
00384 #
00385 print '--Flagdata--'
00386 default('flagdata')
00387 
00388 print ""
00389 print "Flagging edge channels in all spw"
00390 print "  0~3:0~1;62~63 , 4~11:0~1;30~31, 12~15:0~1;62~63 "
00391 print ""
00392 
00393 flagdata(vis='ngc4826.tutorial.ms', mode='manualflag',
00394          spw='0~3:0;1;62;63,4~11:0;1;30;31,12~15:0;1;62;63')
00395 
00396 #
00397 # Flag correlator glitch
00398 #
00399 print ""
00400 print "Flagging bad correlator field 8 antenna 3&9 spw 15 all channels"
00401 print "  timerange 1998/04/16/06:19:00.0~1998/04/16/06:20:00.0"
00402 print ""
00403 
00404 flagdata(vis='ngc4826.tutorial.ms', mode='manualflag', field='8', spw='15', antenna='3&9', 
00405          timerange='1998/04/16/06:19:00.0~1998/04/16/06:20:00.0')
00406 
00407 #
00408 # Flag non-fringing antenna 6
00409 #
00410 # NOTE: this is already flagged in the data so do nothing more here
00411 #flagdata(vis='ngc4826.tutorial.ms', mode='manualflag', antenna='6',
00412 #        timerange='1998/04/16/09:42:39.0~1998/04/16/10:24:46.0')
00413 
00414 #
00415 #
00416 ##########################################################################
00417 #
00418 # Some example clean-up editing
00419 # Slightly high almost-edge channel in field='1', spw='4' (channel 2)
00420 # can be flagged interactively with plotxy.
00421 #
00422 #plotxy(vis='ngc4826.tutorial.ms',
00423 #       xaxis='channel',yaxis='amp',field='1',spw='4',
00424 #       averagemode='vector',timebin='1e7',crossscans=True,
00425 #       selectplot=True,newplot=False,title='Field 1 SPW 4')
00426 
00427 print "Completed pre-calibration flagging"
00428 
00429 #
00430 ##########################################################################
00431 #
00432 # Use Flagmanager to save a copy of the flags so far
00433 #
00434 print '--Flagmanager--'
00435 default('flagmanager')
00436 
00437 print "Now will use flagmanager to save a copy of the flags we just made"
00438 print "These are named myflags"
00439 
00440 flagmanager(vis='ngc4826.tutorial.ms',mode='save',versionname='myflags',
00441             comment='My flags',merge='replace')
00442 
00443 # Can also use Flagmanager to list all saved versions
00444 #
00445 #flagmanager(vis='ngc4826.tutorial.ms',mode='list')
00446 
00447 #
00448 ##########################################################################
00449 #
00450 # CALIBRATION
00451 #
00452 ##########################################################################
00453 #
00454 # Bandpasses are very flat because of observing mode used (online bandpass
00455 # correction) so bandpass calibration is unecessary for these data.
00456 #
00457 ##########################################################################
00458 #
00459 # Derive gain calibration solutions.
00460 # We will use VLA-like G (per-scan) calibration:
00461 #
00462 ##########################################################################
00463 #
00464 # Set the flux density of 3C273 to 23 Jy
00465 #
00466 print '--Setjy (3C273)--'
00467 default('setjy')
00468 
00469 setjy(vis='ngc4826.tutorial.ms',field='0',fluxdensity=[23.0,0.,0.,0.],spw='0~3')
00470 #
00471 # Not really necessary to set spw but you get lots of warning messages if
00472 # you don't
00473 #
00474 ##########################################################################
00475 #
00476 # Gain calibration
00477 #
00478 print '--Gaincal--'
00479 default('gaincal')
00480 
00481 # This should be combining all spw for the two calibrators for single
00482 # scan-based solutions
00483 
00484 print 'Gain calibration for fields 0,1 and spw 0~11'
00485 print 'Using solint=inf combining over spw'
00486 print 'Output table ngc4826.tutorial.16apr98.gcal'
00487 
00488 gaincal(vis='ngc4826.tutorial.ms', caltable='ngc4826.tutorial.16apr98.gcal',
00489         field='0,1', spw='0~11', gaintype='G', minsnr=2.0,
00490         refant='ANT5', gaincurve=False, opacity=0.0,
00491         solint='inf', combine='spw')
00492 
00493 #
00494 ##########################################################################
00495 #
00496 # Transfer the flux density scale:
00497 #
00498 print '--Fluxscale--'
00499 default('fluxscale')
00500 
00501 print ''
00502 print 'Transferring flux of 3C273 to sources: 1310+323'
00503 print 'Output table ngc4826.tutorial.16apr98.fcal'
00504 
00505 fluxscale(vis='ngc4826.tutorial.ms', caltable='ngc4826.tutorial.16apr98.gcal',
00506           fluxtable='ngc4826.tutorial.16apr98.fcal',
00507           reference='3C273', transfer=['1310+323'])
00508 
00509 # Flux density for 1310+323 is: 1.48 +/- 0.016 (SNR = 90.6, nAnt= 8)
00510 #
00511 ##########################################################################
00512 #
00513 # Plot calibration
00514 print '--Plotcal (fluxscale)--'
00515 default(plotcal)
00516 
00517 # Interactive plotcal
00518 plotcal(caltable='ngc4826.tutorial.16apr98.fcal', yaxis='amp', field='')
00519 print ''
00520 print 'Plotting final scaled gain calibration table'
00521 print 'First amp vs. time for all fields '
00522         
00523 # Pause script if you are running in scriptmode
00524 user_check=raw_input('Return to continue script\n')
00525 
00526 plotcal(caltable='ngc4826.tutorial.16apr98.fcal', yaxis='phase', field='')
00527 print ''
00528 print 'and phase vs. time '
00529 
00530 # Pause script if you are running in scriptmode
00531 user_check=raw_input('Return to continue script\n')
00532 
00533 # And you can plot the SNR of the solution
00534 #plotcal(caltable='ngc4826.tutorial.16apr98.fcal', yaxis='snr', field='')
00535 
00536 # You can also plotcal to file
00537 #figfile = 'ngc4826.tutorial.16apr98.fcal.plotcal.amp.png'
00538 #plotcal(caltable='ngc4826.tutorial.16apr98.fcal',yaxis='amp',field='',showgui=False,figfile=figfile)
00539 #figfile = 'ngc4826.tutorial.16apr98.fcal.plotcal.phase.png'
00540 #plotcal(caltable='ngc4826.tutorial.16apr98.fcal',yaxis='phase',field='',showgui=False,figfile=figfile)
00541 
00542 #
00543 ##########################################################################
00544 #
00545 # Correct the calibrater/target source data:
00546 # Use new parm spwmap to apply gain solutions derived from spwid1
00547 # to all other spwids... 
00548 print '--Applycal--'
00549 default('applycal')
00550 
00551 print 'Applying calibration table ngc4826.tutorial.16apr98.fcal to data'
00552 
00553 applycal(vis='ngc4826.tutorial.ms',
00554          field='', spw='',
00555          gaincurve=False, opacity=0.0, 
00556          gaintable='ngc4826.tutorial.16apr98.fcal',
00557          spwmap=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0])
00558 
00559 #
00560 ##########################################################################
00561 #
00562 # Check calibrated data
00563 print '--Plotxy--'
00564 default(plotxy)
00565 
00566 #
00567 # Here we plot the first of the NGC4826 fields unaveraged versus velocity
00568 # Notice how the spw fit together
00569 
00570 # Interactive plotxy
00571 #print "Will plot all the NGC4826 calibrated data unaveraged - this will take a while"
00572 #plotxy(vis='ngc4826.tutorial.ms',xaxis='velocity',yaxis='amp',field='2~8',spw='12~15',
00573 #       averagemode='vector',datacolumn='corrected',
00574 #       selectplot=True,newplot=False,title='Field 2~8 SPW 12~15')
00575 
00576 #print ""
00577 #print "Look for outliers, flag them if there are any bad ones"
00578 #print ""
00579         
00580 # Pause script if you are running in scriptmode
00581 #user_check=raw_input('Return to continue script\n')
00582 
00583 # You can also plot all the N4826 fields 2 through 8, for example using a loop:
00584 
00585 #for fld in range(2,9):
00586 #       field = str(fld)
00587 #       plotxy(vis='ngc4826.tutorial.ms',xaxis='velocity',yaxis='amp',
00588 #              field=field,spw='11~15',
00589 #              averagemode='vector',datacolumn='corrected',
00590 #              selectplot=True,newplot=False,title='Field '+field+' SPW 11~15')
00591 #       
00592 #       user_check=raw_input('Return to continue script\n')
00593 
00594 # Now here we time-average the data, plotting versus velocity
00595 
00596 #plotxy(vis='ngc4826.tutorial.ms',xaxis='velocity',yaxis='amp',field=field,spw=spw,
00597 #       averagemode='vector',datacolumn='corrected',
00598 #       timebin='1e7',crossscans=True,plotcolor='blue',
00599 #       selectplot=True,newplot=False,title='Field '+field+' SPW '+spw)
00600 #print ""
00601 #print 'Final Spectrum field '+field+' spw '+spw+' TimeAverage Corrected Data'
00602         
00603 # Pause script if you are running in scriptmode
00604 #user_check=raw_input('Return to continue script\n')
00605 
00606 # Here we overplot 3C273 the Time+Chan averaged calibrated and uncalibrated data
00607 
00608 #
00609 # First the corrected column in blue
00610 #field = '0'
00611 #spw = '0~3'
00612 #plotxy(vis='ngc4826.tutorial.ms',xaxis='uvdist',yaxis='amp',field=field,spw=spw,
00613 #       averagemode='vector',width='1000',datacolumn='corrected',
00614 #       timebin='1e7',crossscans=True,plotcolor='blue',
00615 #       selectplot=True,newplot=False,title='Field '+field+' SPW '+spw)
00616 #print ""
00617 #print 'Plotting field '+field+' spw '+spw+' TimeChanAverage Corrected Data in blue'
00618 #
00619 # Now the original data column in red
00620 #plotxy(vis='ngc4826.tutorial.ms',xaxis='uvdist',yaxis='amp',field=field,spw=spw,
00621 #       averagemode='vector',width='1000',datacolumn='data',
00622 #       timebin='1e7',crossscans=True,plotcolor='red',overplot=True,
00623 #       selectplot=True,newplot=False,title='Field '+field+' SPW '+spw)
00624 #
00625 #print 'OverPlotting field '+field+' spw '+spw+' TimeChanAverage Original Data in red'
00626         
00627 ## Pause script if you are running in scriptmode
00628 #user_check=raw_input('Return to continue script\n')
00629 
00630 # Can repeat for
00631 #field = '1'
00632 #spw = '4~11'
00633 
00634 print "Done calibration and plotting"
00635 #
00636 ##########################################################################
00637 #
00638 # SPLIT THE DATA INTO SINGLE-SOURCE MS
00639 # AND THEN IMAGE THE CALIBRATOR
00640 #
00641 ##########################################################################
00642 #
00643 #
00644 # Split out calibrated target source and calibrater data:
00645 #
00646 print '--Split--'
00647 default('split')
00648 
00649 print 'Splitting 3C273 data to ngc4826.tutorial.16apr98.3C273.split.ms'
00650 
00651 split(vis='ngc4826.tutorial.ms',
00652       outputvis='ngc4826.tutorial.16apr98.3C273.split.ms',
00653       field='0',spw='0~3:0~63', datacolumn='corrected')
00654 
00655 print 'Splitting 1310+323 data to ngc4826.tutorial.16apr98.1310+323.split.ms'
00656 
00657 split(vis='ngc4826.tutorial.ms',
00658       outputvis='ngc4826.tutorial.16apr98.1310+323.split.ms',
00659       field='1', spw='4~11:0~31', datacolumn='corrected')
00660 
00661 print 'Splitting NGC4826 data to ngc4826.tutorial.16apr98.src.split.ms'
00662 
00663 split(vis='ngc4826.tutorial.ms',
00664       outputvis='ngc4826.tutorial.16apr98.src.split.ms',
00665       field='2~8', spw='12~15:0~63',
00666       datacolumn='corrected')
00667 
00668 #
00669 ##########################################################################
00670 #
00671 #print '--Clearcal (split data)--'
00672 
00673 # If you want to use plotxy before cleaning to look at the split ms
00674 # then you will need to force the creation of the scratch columns
00675 # (clean will also do this)
00676 #clearcal('ngc4826.tutorial.16apr98.1310+323.split.ms')
00677 
00678 #clearcal('ngc4826.tutorial.16apr98.src.split.ms')
00679 
00680 #Then you might look at the data
00681 #clearplot()
00682 #plotxy(vis='ngc4826.tutorial.16apr98.src.split.ms',xaxis='time',yaxis='amp')
00683 
00684 #
00685 ##########################################################################
00686 #
00687 # You might image the calibrater data:
00688 #
00689 #print '--Clean (1310+323)--'
00690 #default('clean')
00691 #
00692 #
00693 #clean(vis='ngc4826.tutorial.16apr98.1310+323.split.ms', 
00694 #      imagename='ngc4826.tutorial.16apr98.cal.clean',
00695 #      cell=[1.,1.],imsize=[256,256],
00696 #      field='0',spw='0~7',threshold=10.,
00697 #      mode='mfs',psfmode='clark',niter=100,stokes='I')
00698 
00699 # You can look at this in the viewer
00700 #viewer('ngc4826.tutorial.16apr98.cal.clean.image')
00701 
00702 #
00703 #
00704 ##########################################################################
00705 #
00706 # IMAGING OF NGC4826 MOSAIC
00707 #
00708 ##########################################################################
00709 #
00710 #          Mosaic field spacing looks like:
00711 #
00712 #          F3 (field 3)         F2 (field 2)
00713 #
00714 #   F4 (field 4)      F0 (field 0)        F1 (field 1)
00715 #
00716 #          F5 (field 5)         F6 (field 6)
00717 #
00718 # 4x64 channels = 256 channels 
00719 #
00720 # Primary Beam should be about 1.6' FWHM (7m dishes, 2.7mm wavelength)
00721 # Resolution should be about 5-8"
00722 ##########################################################################
00723 #
00724 # Image the target source mosaic:
00725 #
00726 print '--Clean (NGC4826)--'
00727 default('clean')
00728 
00729 clean(vis='ngc4826.tutorial.16apr98.src.split.ms',
00730       imagename='ngc4826.tutorial.16apr98.src.clean',
00731       field='0~6',spw='0~3',
00732       cell=[1.,1.],imsize=[256,256],stokes='I',
00733       mode='channel',nchan=36,start=35,width=4,
00734       psfmode='clark',imagermode='mosaic',scaletype='SAULT',
00735       cyclefactor=1.5,niter=10000,threshold='45mJy',
00736       minpb=0.3,pbcor=False)
00737 
00738 ### NOTE: mosaic data ...Sault weighting implies a noise unform image
00739 
00740 ### NOTE: that niter is set to large number so that stopping point is
00741 ### controlled by threshold.
00742 
00743 ### NOTE: with pbcor=False, the final image is not "flux correct",
00744 ### instead the image has constant noise despite roll off in power as
00745 ### you move out from the phase center(s). Though this format makes it
00746 ### "look nicest", for all flux density measurements, and to get an
00747 ### accurate integrated intensity image, one needs to divide the
00748 ### srcimage.image/srcimage.flux in order to correct for the mosaic
00749 ### response pattern. One could also achieve this by setting pbcor=True
00750 ### in clean.
00751 
00752 # Try running clean adding the parameter interactive=True.
00753 # This parameter will periodically bring up the viewer to allow
00754 # interactive clean boxing. For poor uv-coverage, deep negative bowls
00755 # from missing short spacings, this can be very important to get correct
00756 # integrated flux densities.
00757 
00758 #
00759 ##########################################################################
00760 #
00761 # Do interactive viewing of clean image
00762 print '--Viewer--'
00763 viewer('ngc4826.tutorial.16apr98.src.clean.image')
00764 
00765 print ""
00766 print "This is the non-pbcorrected cube of NGC4826"
00767 print "Use tape deck to move through channels"
00768 print "Close the viewer when done"
00769 print ""
00770 #
00771 # Pause script if you are running in scriptmode
00772 user_check=raw_input('Return to continue script\n')
00773 
00774 #
00775 ##########################################################################
00776 #
00777 # Statistics on clean image cube
00778 #
00779 print '--ImStat (Clean cube)--'
00780 
00781 srcstat = imstat('ngc4826.tutorial.16apr98.src.clean.image')
00782 
00783 print "Found image max = "+str(srcstat['max'][0])
00784 
00785 # offbox = '106,161,153,200'
00786 
00787 offstat = imstat('ngc4826.tutorial.16apr98.src.clean.image',
00788                  box='106,161,153,200')
00789 
00790 print "Found off-source image rms = "+str(offstat['sigma'][0])
00791 
00792 # cenbox = '108,108,148,148'
00793 # offlinechan = '0,1,2,3,4,5,30,31,32,33,34,35'
00794 
00795 offlinestat = imstat('ngc4826.tutorial.16apr98.src.clean.image',
00796                      box='108,108,148,148',
00797                      chans='0,1,2,3,4,5,30,31,32,33,34,35')
00798 
00799 print "Found off-line image rms = "+str(offlinestat['sigma'][0])
00800 
00801 #
00802 ##########################################################################
00803 #
00804 # Manually correct for mosaic response pattern using .image/.flux images
00805 #
00806 print '--ImMath (PBcor)--'
00807 
00808 immath(outfile='ngc4826.tutorial.16apr98.src.clean.pbcor',
00809        mode='evalexpr',
00810        expr="'ngc4826.tutorial.16apr98.src.clean.image'/'ngc4826.tutorial.16apr98.src.clean.flux'")
00811 
00812 #
00813 ##########################################################################
00814 #
00815 # Statistics on PBcor image cube
00816 #
00817 print '--ImStat (PBcor cube)--'
00818 
00819 pbcorstat = imstat('ngc4826.tutorial.16apr98.src.clean.pbcor')
00820 
00821 print "Found image max = "+str(pbcorstat['max'][0])
00822 
00823 pbcoroffstat = imstat('ngc4826.tutorial.16apr98.src.clean.pbcor',
00824                       box='106,161,153,200')
00825 
00826 print "Found off-source image rms = "+str(pbcoroffstat['sigma'][0])
00827 
00828 pbcorofflinestat = imstat('ngc4826.tutorial.16apr98.src.clean.pbcor',
00829                           box='108,108,148,148',
00830                           chans='0,1,2,3,4,5,30,31,32,33,34,35')
00831 
00832 print "Found off-line image rms = "+str(pbcorofflinestat['sigma'][0])
00833 
00834 #
00835 ##########################################################################
00836 #
00837 # Do zeroth and first moments
00838 #
00839 # NGC4826 LSR velocity is 408 km/s; delta is 20 km/s
00840 #
00841 print '--ImMoments--'
00842 default('immoments')
00843 
00844 momfile = 'ngc4826.tutorial.16apr98.moments'
00845 momzeroimage = 'ngc4826.tutorial.16apr98.moments.integrated'
00846 momoneimage = 'ngc4826.tutorial.16apr98.moments.mom1'
00847 
00848 print "Calculating Moments 0,1 for PBcor image"
00849 
00850 immoments(imagename='ngc4826.tutorial.16apr98.src.clean.pbcor',
00851           moments=0,axis=3,
00852           chans='7~28',
00853           outfile='ngc4826.tutorial.16apr98.moments.integrated') 
00854 
00855 # TUTORIAL NOTES: For moment 0 we use the image corrected for the
00856 # mosaic response to get correct integrated flux densities. However,
00857 # in *real signal* regions, the value of moment 1 is not dependent on
00858 # the flux being correct so the non-pb corrected SAULT image can be
00859 # used, this avoids having lots of junk show up at the edges of your
00860 # moment 1 image due to the primary beam correction. Try it both ways
00861 # and see for yourself.
00862 
00863 # TUTORIAL NOTES:
00864 #
00865 # Moments greater than zero need to have a conservative lower
00866 # flux cutoff to produce sensible results.
00867 
00868 immoments(imagename='ngc4826.tutorial.16apr98.src.clean.image',
00869           moments=1,axis=3,includepix=[0.2,1000.0],
00870           chans='7~28',
00871           outfile='ngc4826.tutorial.16apr98.moments.mom1') 
00872 
00873 # Now view the resulting images
00874 viewer('ngc4826.tutorial.16apr98.moments.integrated')
00875 #
00876 print "Now viewing Moment-0 ngc4826.tutorial.16apr98.moments.integrated"
00877 print "Note PBCOR effects at field edge"
00878 print "Change the colorscale to get better image"
00879 print "You can also Open and overlay Contours of Moment-1 ngc4826.tutorial.16apr98.moments.mom1"
00880 print "Close the viewer when done"
00881 #
00882 # Pause script if you are running in scriptmode
00883 user_check=raw_input('Return to continue script\n')
00884 
00885 #
00886 ##########################################################################
00887 #
00888 # Statistics on moment images
00889 #
00890 print '--ImStat (Moment images)--'
00891 
00892 momzerostat=imstat('ngc4826.tutorial.16apr98.moments.integrated')
00893 
00894 print "Found moment 0 max = "+str(momzerostat['max'][0])
00895 
00896 print "Found moment 0 rms = "+str(momzerostat['rms'][0])
00897 
00898 momonestat=imstat('ngc4826.tutorial.16apr98.moments.mom1')
00899 
00900 print "Found moment 1 median = "+str(momonestat['median'][0])
00901 
00902 #
00903 ##########################################################################
00904 #
00905 # An alternative is to mask the pbcor image before calculating
00906 # moments.  The following block shows how to do this.
00907 
00908 # To do this, open the clean pbcor file in the viewer and use the
00909 # Region Manager to create a region file
00910 
00911 #print '--Viewer--'
00912 #viewer(ngc4826.tutorial.16apr98.src.clean.pbcor)
00913 
00914 # TUTORIAL NOTES: After loading ngc4826.tutorial.16apr98.src.clean.pbcor
00915 # in the viewer as a raster, click on the file icon in top left
00916 # corner, and select the momzero image but open as a Contour map
00917 # instead of Raster. Then decrease "contour scale factor" in "Data
00918 # Display Options" gui to something like 10. select "region manager
00919 # tool" from "tool" drop down menu In region manager tool select "all
00920 # axes". Then assign the sqiggly Polygon button to a mouse button by
00921 # clicking on it with a mouse button. Then draw a polygon region
00922 # around galaxy emission, avoiding edge regions. Then in "region
00923 # manager tool" select "save last region".
00924 
00925 # Pause script if you are running in scriptmode
00926 #user_check=raw_input('Return to continue script\n')
00927 
00928 # You should have created region file ngc4826.tutorial.16apr98.src.clean.pbcor.rgn
00929 
00930 # Now use immath to use the region file to mask the cube
00931 #
00932 #print '--ImMath (masking)--'
00933 #immath(outfile='ngc4826.tutorial.16apr98.src.clean.pbcor.masked',
00934 #       mode='evalexpr',
00935 #       expr="'ngc4826.tutorial.16apr98.src.clean.pbcor'",
00936 #       region='ngc4826.tutorial.16apr98.src.clean.pbcor.rgn')
00937 
00938 #
00939 # And then make masked moment images
00940 
00941 #print '--ImMoments (masked)--'
00942 #print 'Creating masked moment 0 image ngc4826.tutorial.16apr98.moments.integratedmasked'
00943 #       
00944 #immoments(imagename='ngc4826.tutorial.16apr98.src.clean.pbcor.masked',
00945 #          moments=0,axis=3,
00946 #          chans='7~28',
00947 #          outfile='ngc4826.tutorial.16apr98.moments.integratedmasked') 
00948 #
00949 #print 'Creating masked moment 1 image ngc4826.tutorial.16apr98.moments.mom1masked'
00950 #
00951 #immoments(imagename='ngc4826.tutorial.16apr98.src.clean.pbcor.masked',
00952 #          moments=1,axis=3,
00953 #          includepix=[0.2,1000.0],
00954 #          chans='7~28',
00955 #          outfile='ngc4826.tutorial.16apr98.moments.mom1masked') 
00956 
00957 # Now view the resulting images
00958 #viewer('ngc4826.tutorial.16apr98.moments.integratedmasked')
00959 #
00960 #print "Now viewing masked Moment-0 ngc4826.tutorial.16apr98.moments.integratedmasked"
00961 #print "You can Open and overlay Contours of Moment-1 ngc4826.tutorial.16apr98.moments.mom1masked"
00962 #
00963 # Pause script if you are running in scriptmode
00964 #user_check=raw_input('Return to continue script\n')
00965 
00966 # Finally, can compute and print statistics
00967 #print '--ImStat (masked moments)--'
00968 #
00969 #maskedmomzerostat = imstat('ngc4826.tutorial.16apr98.moments.integratedmasked')
00970 #print "Found masked moment 0 max = "+str(maskedmomzerostat['max'][0])
00971 #print "Found masked moment 0 rms = "+str(maskedmomzerostat['rms'][0])
00972 #
00973 #maskedmomonestat=imstat('ngc4826.tutorial.16apr98.moments.mom1masked')
00974 #print "Found masked moment 1 median = "+str(maskedmomonestat['median'][0])
00975 
00976 #
00977 ##########################################################################
00978 #
00979 # Now show how to print out results
00980 #
00981 print '--Results (16apr98)--'
00982 print ''
00983 #
00984 # Currently using non-PBcor values
00985 #
00986 im_srcmax16 = srcstat['max'][0]
00987 im_offrms16 = offstat['sigma'][0]
00988 im_offlinerms16 = offlinestat['sigma'][0]
00989 thistest_immax = momzerostat['max'][0]
00990 thistest_imrms = momzerostat['rms'][0]
00991 
00992 #
00993 # Report a few key stats
00994 #
00995 print '  NGC4826 Image Cube Max = '+str(im_srcmax16)
00996 print "          At ("+str(srcstat['maxpos'][0])+","+str(srcstat['maxpos'][1])+") Channel "+str(srcstat['maxpos'][3])
00997 print '          '+srcstat['maxposf']
00998 print ''
00999 print '          Off-Source Rms = '+str(im_offrms16)
01000 print '          Signal-to-Noise ratio = '+str(im_srcmax16/im_offrms16)
01001 print ''
01002 print '          Off-Line   Rms = '+str(im_offlinerms16)
01003 print '          Signal-to-Noise ratio = '+str(im_srcmax16/im_offlinerms16)
01004 
01005 # Note previous regression values (using this script STM 2008-06-04) were:
01006 #srcmax16=1.45868253708
01007 #immax=169.420959473
01008 #imrms=14.3375244141
01009 #offrms16=0.0438643493782
01010 #offlinerms16=0.0544108718199
01011 
01012 # Could print out comparison:
01013 #print ''
01014 #print '--Src image max (16apr98): '+str(im_srcmax16)+' was '+str(srcmax16)
01015 #print '--Off-src   rms (16apr98): '+str(im_offrms16)+' was '+str(offrms16)
01016 #print '--Off-line  rms (16apr98): '+str(im_offlinerms16)+' was '+str(offlinerms16)
01017 #print '--Moment  0 max (16apr98): '+str(thistest_immax)+' was '+str(immax)
01018 #print '--Moment  0 rms (16apr98): '+str(thistest_imrms)+' was '+str(imrms)
01019 #print ''
01020 
01021 print "Done with NGC4826 Tutorial"
01022 #
01023 ##########################################################################