casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
ngc5921_usecase.py
Go to the documentation of this file.
00001 ##########################################################################
00002 #                                                                        #
00003 # Use Case Script for NGC 5921                                           #
00004 #                                                                        #
00005 # Converted by STM 2007-05-26                                            #
00006 # Updated      STM 2007-06-15 (Alpha Patch 1)                            #
00007 # Updated      STM 2007-09-05 (Alpha Patch 2+)                           #
00008 # Updated      STM 2007-09-18 (Alpha Patch 2+)                           #
00009 # Updated      STM 2007-09-18 (Pre-Beta) add immoments                   #
00010 # Updated      STM 2007-10-04 (Beta) update                              #
00011 # Updated      STM 2007-10-10 (Beta) add export                          #
00012 # Updated      STM 2007-11-08 (Beta Patch 0.5) add RRusk stuff           #
00013 # Updated      STM 2008-03-25 (Beta Patch 1.0)                           #
00014 # Updated      STM 2008-05-23 (Beta Patch 2.0) new tasking/clean/cal     #
00015 # Updated      STM 2008-06-11 (Beta Patch 2.0)                           #
00016 #                                                                        #
00017 # Features Tested:                                                       #
00018 #    The script illustrates end-to-end processing with CASA              #
00019 #    as depicted in the following flow-chart.                            #
00020 #                                                                        #
00021 #    Filenames will have the <prefix> = 'ngc5921.usecase'                #
00022 #                                                                        #
00023 #    Input Data           Process          Output Data                   #
00024 #                                                                        #
00025 #   NGC5921.fits --> importuvfits  -->  <prefix>.ms   +                  #
00026 #   (1.4GHz,               |            <prefix>.ms.flagversions         #
00027 #    63 sp chan,           v                                             #
00028 #    D-array)           listobs    -->  casapy.log                       #
00029 #                          |                                             #
00030 #                          v                                             #
00031 #                     flagautocorr                                       #
00032 #                          |                                             #
00033 #                          v                                             #
00034 #                        setjy                                           #
00035 #                          |                                             #
00036 #                          v                                             #
00037 #                       bandpass   -->  <prefix>.bcal                    #
00038 #                          |                                             #
00039 #                          v                                             #
00040 #                       gaincal    -->  <prefix>.gcal                    #
00041 #                          |                                             #
00042 #                          v                                             #
00043 #                      fluxscale   -->  <prefix>.fluxscale               #
00044 #                          |                                             #
00045 #                          v                                             #
00046 #                      applycal    -->  <prefix>.ms                      #
00047 #                          |                                             #
00048 #                          v                                             #
00049 #                        split     -->  <prefix>.cal.split.ms            #
00050 #                          |                                             #
00051 #                          v                                             #
00052 #                        split     -->  <prefix>.src.split.ms            #
00053 #                          |                                             #
00054 #                          v                                             #
00055 #                    exportuvfits  -->  <prefix>.split.uvfits            #
00056 #                          |                                             #
00057 #                          v                                             #
00058 #                      uvcontsub   -->  <prefix>.ms.cont +               #
00059 #                          |            <prefix>.ms.contsub              #
00060 #                          v                                             #
00061 #                        clean     -->  <prefix>.clean.image +           #
00062 #                          |            <prefix>.clean.model +           #
00063 #                          |            <prefix>.clean.residual          #
00064 #                          v                                             #
00065 #                     exportfits   -->  <prefix>.clean.fits              #
00066 #                          |                                             #
00067 #                          v                                             #
00068 #                       imhead     -->  casapy.log                       #
00069 #                          |                                             #
00070 #                          v                                             #
00071 #                       imstat     -->  xstat (parameter)                #
00072 #                          |                                             #
00073 #                          v                                             #
00074 #                      immoments   -->  <prefix>.moments.integrated +    #
00075 #                          |            <prefix>.moments.weighted_coord  #
00076 #                          v                                             #
00077 ##########################################################################
00078 
00079 import time
00080 import os
00081 
00082 # 
00083 # Set up some useful variables
00084 #
00085 # Get to path to the CASA home and stip off the name
00086 pathname=os.environ.get('CASAPATH').split()[0]
00087 
00088 # This is where the NGC5921 UVFITS data will be
00089 fitsdata=pathname+'/data/demo/NGC5921.fits'
00090 #
00091 # Or use data in current directory
00092 #fitsdata='NGC5921.fits'
00093 
00094 # The prefix to use for all output files
00095 prefix='ngc5921.usecase'
00096 
00097 # Clean up old files
00098 os.system('rm -rf '+prefix+'*')
00099 
00100 #
00101 #=====================================================================
00102 #
00103 # Import the data from FITS to MS
00104 #
00105 print '--Import--'
00106 
00107 # Safest to start from task defaults
00108 default('importuvfits')
00109 
00110 # Set up the MS filename and save as new global variable
00111 msfile = prefix + '.ms'
00112 
00113 # Use task importuvfits
00114 fitsfile = fitsdata
00115 vis = msfile
00116 
00117 saveinputs('importuvfits',prefix+'.importuvfits.saved')
00118 
00119 importuvfits()
00120 
00121 #
00122 # Note that there will be a ngc5921.usecase.ms.flagversions
00123 # there containing the initial flags as backup for the main ms
00124 # flags.
00125 #
00126 #=====================================================================
00127 #
00128 # List a summary of the MS
00129 #
00130 print '--Listobs--'
00131 
00132 # Don't default this one and make use of the previous setting of
00133 # vis.  Remember, the variables are GLOBAL!
00134 
00135 # You may wish to see more detailed information, like the scans.
00136 # In this case use the verbose = True option
00137 verbose = True
00138 
00139 listobs()
00140 
00141 # You should get in your logger window and in the casapy.log file
00142 # something like:
00143 #
00144 # MeasurementSet Name:  /home/sandrock2/smyers/Testing2/Sep07/ngc5921.usecase.ms
00145 # MS Version 2
00146 # 
00147 # Observer: TEST     Project:   
00148 # Observation: VLA
00149 # 
00150 # Data records: 22653       Total integration time = 5280 seconds
00151 #    Observed from   09:19:00   to   10:47:00
00152 # 
00153 #    ObservationID = 0         ArrayID = 0
00154 #   Date        Timerange                Scan  FldId FieldName      SpwIds
00155 #   13-Apr-1995/09:19:00.0 - 09:24:30.0     1      0 1331+30500002_0  [0]
00156 #               09:27:30.0 - 09:29:30.0     2      1 1445+09900002_0  [0]
00157 #               09:33:00.0 - 09:48:00.0     3      2 N5921_2        [0]
00158 #               09:50:30.0 - 09:51:00.0     4      1 1445+09900002_0  [0]
00159 #               10:22:00.0 - 10:23:00.0     5      1 1445+09900002_0  [0]
00160 #               10:26:00.0 - 10:43:00.0     6      2 N5921_2        [0]
00161 #               10:45:30.0 - 10:47:00.0     7      1 1445+09900002_0  [0]
00162 # 
00163 # Fields: 3
00164 #   ID   Code Name          Right Ascension  Declination   Epoch   
00165 #   0    C    1331+30500002_013:31:08.29      +30.30.32.96  J2000   
00166 #   1    A    1445+09900002_014:45:16.47      +09.58.36.07  J2000   
00167 #   2         N5921_2       15:22:00.00      +05.04.00.00  J2000   
00168 # 
00169 # Spectral Windows:  (1 unique spectral windows and 1 unique polarization setups)
00170 #   SpwID  #Chans Frame Ch1(MHz)    Resoln(kHz) TotBW(kHz)  Ref(MHz)    Corrs   
00171 #   0          63 LSRK  1412.68608  24.4140625  1550.19688  1413.44902  RR  LL  
00172 # 
00173 # Feeds: 28: printing first row only
00174 #   Antenna   Spectral Window     # Receptors    Polarizations
00175 #   1         -1                  2              [         R, L]
00176 # 
00177 # Antennas: 27:
00178 #   ID   Name  Station   Diam.    Long.         Lat.         
00179 #   0    1     VLA:N7    25.0 m   -107.37.07.2  +33.54.12.9  
00180 #   1    2     VLA:W1    25.0 m   -107.37.05.9  +33.54.00.5  
00181 #   2    3     VLA:W2    25.0 m   -107.37.07.4  +33.54.00.9  
00182 #   3    4     VLA:E1    25.0 m   -107.37.05.7  +33.53.59.2  
00183 #   4    5     VLA:E3    25.0 m   -107.37.02.8  +33.54.00.5  
00184 #   5    6     VLA:E9    25.0 m   -107.36.45.1  +33.53.53.6  
00185 #   6    7     VLA:E6    25.0 m   -107.36.55.6  +33.53.57.7  
00186 #   7    8     VLA:W8    25.0 m   -107.37.21.6  +33.53.53.0  
00187 #   8    9     VLA:N5    25.0 m   -107.37.06.7  +33.54.08.0  
00188 #   9    10    VLA:W3    25.0 m   -107.37.08.9  +33.54.00.1  
00189 #   10   11    VLA:N4    25.0 m   -107.37.06.5  +33.54.06.1  
00190 #   11   12    VLA:W5    25.0 m   -107.37.13.0  +33.53.57.8  
00191 #   12   13    VLA:N3    25.0 m   -107.37.06.3  +33.54.04.8  
00192 #   13   14    VLA:N1    25.0 m   -107.37.06.0  +33.54.01.8  
00193 #   14   15    VLA:N2    25.0 m   -107.37.06.2  +33.54.03.5  
00194 #   15   16    VLA:E7    25.0 m   -107.36.52.4  +33.53.56.5  
00195 #   16   17    VLA:E8    25.0 m   -107.36.48.9  +33.53.55.1  
00196 #   17   18    VLA:W4    25.0 m   -107.37.10.8  +33.53.59.1  
00197 #   18   19    VLA:E5    25.0 m   -107.36.58.4  +33.53.58.8  
00198 #   19   20    VLA:W9    25.0 m   -107.37.25.1  +33.53.51.0  
00199 #   20   21    VLA:W6    25.0 m   -107.37.15.6  +33.53.56.4  
00200 #   21   22    VLA:E4    25.0 m   -107.37.00.8  +33.53.59.7  
00201 #   23   24    VLA:E2    25.0 m   -107.37.04.4  +33.54.01.1  
00202 #   24   25    VLA:N6    25.0 m   -107.37.06.9  +33.54.10.3  
00203 #   25   26    VLA:N9    25.0 m   -107.37.07.8  +33.54.19.0  
00204 #   26   27    VLA:N8    25.0 m   -107.37.07.5  +33.54.15.8  
00205 #   27   28    VLA:W7    25.0 m   -107.37.18.4  +33.53.54.8  
00206 # 
00207 # Tables:
00208 #    MAIN                   22653 rows     
00209 #    ANTENNA                   28 rows     
00210 #    DATA_DESCRIPTION           1 row      
00211 #    DOPPLER             <absent>  
00212 #    FEED                      28 rows     
00213 #    FIELD                      3 rows     
00214 #    FLAG_CMD             <empty>  
00215 #    FREQ_OFFSET         <absent>  
00216 #    HISTORY                  273 rows     
00217 #    OBSERVATION                1 row      
00218 #    POINTING                 168 rows     
00219 #    POLARIZATION               1 row      
00220 #    PROCESSOR            <empty>  
00221 #    SOURCE                     3 rows     
00222 #    SPECTRAL_WINDOW            1 row      
00223 #    STATE                <empty>  
00224 #    SYSCAL              <absent>  
00225 #    WEATHER             <absent>  
00226 # 
00227 #
00228 #=====================================================================
00229 #
00230 # Get rid of the autocorrelations from the MS
00231 #
00232 print '--Flag auto-correlations--'
00233 
00234 # Don't default this one either, there is only one parameter (vis)
00235 default('tflagdata')
00236 vis = msfile
00237 mode = 'manual'
00238 autocorr = True
00239 tflagdata()
00240 
00241 #
00242 #=====================================================================
00243 #
00244 # Set the fluxes of the primary calibrator(s)
00245 #
00246 print '--Setjy--'
00247 default('setjy')
00248 
00249 vis = msfile
00250 
00251 #
00252 # 1331+305 = 3C286 is our primary calibrator
00253 # Use the wildcard on the end of the source name
00254 # since the field names in the MS have inherited the
00255 # AIPS qualifiers
00256 field = '1331+305*'
00257 
00258 # This is 1.4GHz D-config and 1331+305 is sufficiently unresolved
00259 # that we dont need a model image.  For higher frequencies
00260 # (particularly in A and B config) you would want to use one.
00261 modimage = ''
00262 
00263 # Setjy knows about this source so we dont need anything more
00264 
00265 saveinputs('setjy',prefix+'.setjy.saved')
00266 
00267 setjy()
00268 
00269 #
00270 # You should see something like this in the logger and casapy.log file:
00271 #
00272 # 1331+30500002_0  spwid=  0  [I=14.76, Q=0, U=0, V=0] Jy, (Perley-Taylor 99)
00273 #
00274 # So its using 14.76Jy as the flux of 1331+305 in the single Spectral Window
00275 # in this MS.
00276 #
00277 #=====================================================================
00278 #
00279 # Bandpass calibration
00280 #
00281 print '--Bandpass--'
00282 default('bandpass')
00283 
00284 # We can first do the bandpass on the single 5min scan on 1331+305
00285 # At 1.4GHz phase stablility should be sufficient to do this without
00286 # a first (rough) gain calibration.  This will give us the relative
00287 # antenna gain as a function of frequency.
00288 
00289 vis = msfile
00290 
00291 # set the name for the output bandpass caltable
00292 btable = prefix + '.bcal'
00293 caltable = btable
00294 
00295 # No gain tables yet
00296 gaintable = ''
00297 gainfield = ''
00298 interp = ''
00299 
00300 # Use flux calibrator 1331+305 = 3C286 (FIELD_ID 0) as bandpass calibrator
00301 field = '0'
00302 # all channels
00303 spw = ''
00304 # No other selection
00305 selectdata = False
00306 
00307 # In this band we do not need a-priori corrections for
00308 # antenna gain-elevation curve or atmospheric opacity
00309 # (at 8GHz and above you would want these)
00310 gaincurve = False
00311 opacity = 0.0
00312 
00313 # Choose bandpass solution type
00314 # Pick standard time-binned B (rather than BPOLY)
00315 bandtype = 'B'
00316 
00317 # set solution interval arbitrarily long (get single bpass)
00318 solint = 86400.0
00319 
00320 # reference antenna Name 15 (15=VLA:N2) (Id 14)
00321 refant = '15'
00322 
00323 saveinputs('bandpass',prefix+'.bandpass.saved')
00324 
00325 bandpass()
00326 
00327 #
00328 #=====================================================================
00329 #
00330 # Use plotcal to examine the bandpass solutions
00331 #
00332 print '--Plotcal (bandpass)--'
00333 default('plotcal')
00334 
00335 caltable = btable
00336 field = '0'
00337 
00338 # No GUI for this script
00339 showgui = False
00340 # If you want to do this interactively and iterate over antenna, set
00341 #iteration = 'antenna'
00342 #showgui = True
00343 
00344 # Set up 2x1 panels - upper panel amp vs. channel
00345 subplot = 211
00346 yaxis = 'amp'
00347 # No output file yet (wait to plot next panel)
00348 
00349 saveinputs('plotcal',prefix+'.plotcal.b.amp.saved')
00350 
00351 plotcal()
00352 #
00353 # Set up 2x1 panels - lower panel phase vs. channel
00354 subplot = 212
00355 yaxis = 'phase'
00356 # Now send final plot to file in PNG format (via .png suffix)
00357 figfile = caltable + '.plotcal.png'
00358 
00359 saveinputs('plotcal',prefix+'.plotcal.b.phase.saved')
00360 
00361 plotcal()
00362 #
00363 # Note the rolloff in the start and end channels.  Looks like
00364 # channels 6-56 (out of 0-62) are the best
00365 
00366 #=====================================================================
00367 #
00368 # Gain calibration
00369 #
00370 print '--Gaincal--'
00371 default('gaincal')
00372 
00373 # Armed with the bandpass, we now solve for the
00374 # time-dependent antenna gains
00375 
00376 vis = msfile
00377 
00378 # set the name for the output gain caltable
00379 gtable = prefix + '.gcal'
00380 caltable = gtable
00381 
00382 # Use our previously determined bandpass
00383 # Note this will automatically be applied to all sources
00384 # not just the one used to determine the bandpass
00385 gaintable = btable
00386 gainfield = ''
00387 
00388 # Use nearest (there is only one bandpass entry)
00389 interp = 'nearest'
00390 
00391 # Gain calibrators are 1331+305 and 1445+099 (FIELD_ID 0 and 1)
00392 field = '0,1'
00393 
00394 # We have only a single spectral window (SPW 0)
00395 # Choose 51 channels 6-56 out of the 63
00396 # to avoid end effects.
00397 # Channel selection is done inside spw
00398 spw = '0:6~56'
00399 
00400 # No other selection
00401 selectdata = False
00402 
00403 # In this band we do not need a-priori corrections for
00404 # antenna gain-elevation curve or atmospheric opacity
00405 # (at 8GHz and above you would want these)
00406 gaincurve = False
00407 opacity = 0.0
00408 
00409 # scan-based G solutions for both amplitude and phase
00410 gaintype = 'G'
00411 solint = 'inf'
00412 combine = ''
00413 calmode = 'ap'
00414 
00415 # minimum SNR allowed
00416 minsnr = 1.0
00417 
00418 # reference antenna 15 (15=VLA:N2)
00419 refant = '15'
00420 
00421 saveinputs('gaincal',prefix+'.gaincal.saved')
00422 
00423 gaincal()
00424 
00425 #
00426 #=====================================================================
00427 #
00428 # Bootstrap flux scale
00429 #
00430 print '--Fluxscale--'
00431 default('fluxscale')
00432 
00433 vis = msfile
00434 
00435 # set the name for the output rescaled caltable
00436 ftable = prefix + '.fluxscale'
00437 fluxtable = ftable
00438 
00439 # point to our first gain cal table
00440 caltable = gtable
00441 
00442 # we will be using 1331+305 (the source we did setjy on) as
00443 # our flux standard reference - note its extended name as in
00444 # the FIELD table summary above (it has a VLA seq number appended)
00445 reference = '1331*'
00446 
00447 # we want to transfer the flux to our other gain cal source 1445+099
00448 transfer = '1445*'
00449 
00450 saveinputs('fluxscale',prefix+'.fluxscale.saved')
00451 
00452 fluxscale()
00453 
00454 # In the logger you should see something like:
00455 # Flux density for 1445+09900002_0 in SpW=0 is:
00456 #     2.48576 +/- 0.00123122 (SNR = 2018.94, nAnt= 27)
00457 
00458 # If you run plotcal() on the tablein = 'ngc5921.usecase.fluxscale'
00459 # you will see now it has brought the amplitudes in line between
00460 # the first scan on 1331+305 and the others on 1445+099
00461 
00462 #
00463 #=====================================================================
00464 #
00465 # Now use plotcal to examine the gain solutions
00466 #
00467 print '--Plotcal (fluxscaled gains)--'
00468 default('plotcal')
00469 
00470 caltable = ftable
00471 field = '0,1'
00472 
00473 # No GUI for this script
00474 showgui = False
00475 # If you want to do this interactively and iterate over antenna, set
00476 #iteration = 'antenna'
00477 #showgui = True
00478 
00479 # Set up 2x1 panels - upper panel amp vs. time
00480 subplot = 211
00481 yaxis = 'amp'
00482 # No output file yet (wait to plot next panel)
00483 
00484 saveinputs('plotcal',prefix+'.plotcal.gscaled.amp.saved')
00485 
00486 plotcal()
00487 #
00488 # Set up 2x1 panels - lower panel phase vs. time
00489 subplot = 212
00490 yaxis = 'phase'
00491 # Now send final plot to file in PNG format (via .png suffix)
00492 figfile = caltable + '.plotcal.png'
00493 
00494 saveinputs('plotcal',prefix+'.plotcal.gscaled.phase.saved')
00495 
00496 plotcal()
00497 #
00498 # The amp and phase coherence looks good
00499 
00500 #=====================================================================
00501 #
00502 # Apply our calibration solutions to the data
00503 # (This will put calibrated data into the CORRECTED_DATA column)
00504 #
00505 print '--ApplyCal--'
00506 default('applycal')
00507 
00508 vis = msfile
00509 
00510 # We want to correct the calibrators using themselves
00511 # and transfer from 1445+099 to itself and the target N5921
00512 
00513 # Start with the fluxscale/gain and bandpass tables
00514 gaintable = [ftable,btable]
00515 
00516 # pick the 1445+099 out of the gain table for transfer
00517 # use all of the bandpass table
00518 gainfield = ['1','*']
00519 
00520 # interpolation using linear for gain, nearest for bandpass
00521 interp = ['linear','nearest']
00522 
00523 # only one spw, do not need mapping
00524 spwmap = []
00525 
00526 # all channels
00527 spw = ''
00528 selectdata = False
00529 
00530 # as before
00531 gaincurve = False
00532 opacity = 0.0
00533 
00534 # select the fields for 1445+099 and N5921
00535 field = '1,2'
00536 
00537 applycal()
00538 
00539 # Now for completeness apply 1331+305 to itself
00540 
00541 field = '0'
00542 gainfield = ['0','*']
00543 
00544 # The CORRECTED_DATA column now contains the calibrated visibilities
00545 
00546 saveinputs('applycal',prefix+'.applycal.saved')
00547 
00548 applycal()
00549 
00550 #
00551 #=====================================================================
00552 #
00553 # Now use plotxy to plot the calibrated target data (before contsub)
00554 #
00555 print '--Plotxy (NGC5921)--'
00556 default('plotxy')
00557 
00558 vis = msfile
00559 
00560 field = '2'
00561 # Edge channels are bad
00562 spw = '0:4~59'
00563 
00564 # Time average across scans
00565 timebin = '86000.'
00566 crossscans = True
00567 
00568 # No GUI for this script
00569 interactive = False
00570 
00571 # Set up 2x1 panels - upper panel amp vs. channel
00572 subplot = 211
00573 xaxis = 'channel'
00574 yaxis = 'amp'
00575 datacolumn = 'corrected'
00576 # No output file yet (wait to plot next panel)
00577 
00578 saveinputs('plotxy',prefix+'.plotxy.final.amp.saved')
00579 
00580 plotxy()
00581 #
00582 # Set up 2x1 panels - lower panel phase vs. time
00583 subplot = 212
00584 yaxis = 'phase'
00585 datacolumn = 'corrected'
00586 # Now send final plot to file in PNG format (via .png suffix)
00587 figfile = vis + '.plotxy.png'
00588 
00589 saveinputs('plotxy',prefix+'.plotxy.final.phase.saved')
00590 
00591 plotxy()
00592 
00593 #=====================================================================
00594 #
00595 # Split the gain calibrater data, then the target
00596 #
00597 print '--Split 1445+099 Data--'
00598 default('split')
00599 
00600 vis = msfile
00601 
00602 # We first want to write out the corrected data for the calibrator
00603 
00604 # Make an output vis file
00605 calsplitms = prefix + '.cal.split.ms'
00606 outputvis = calsplitms
00607 
00608 # Select the 1445+099 field, all chans
00609 field = '1445*'
00610 spw = ''
00611 
00612 # pick off the CORRECTED_DATA column
00613 datacolumn = 'corrected'
00614 
00615 saveinputs('split',prefix+'.split.1445.saved')
00616 
00617 split()
00618 
00619 #
00620 # Now split NGC5921 data (before continuum subtraction)
00621 #
00622 print '--Split NGC5921 Data--'
00623 
00624 splitms = prefix + '.src.split.ms'
00625 outputvis = splitms
00626 
00627 # Pick off N5921 
00628 field = 'N5921*'
00629 
00630 saveinputs('split',prefix+'.split.n5921.saved')
00631 
00632 split()
00633 
00634 #=====================================================================
00635 #
00636 # Export the NGC5921 data as UVFITS
00637 # Start with the split file.
00638 #
00639 print '--Export UVFITS--'
00640 default('exportuvfits')
00641 
00642 srcuvfits = prefix + '.split.uvfits'
00643 
00644 vis = splitms
00645 fitsfile = srcuvfits
00646 
00647 # Since this is a split dataset, the calibrated data is
00648 # in the DATA column already.
00649 datacolumn = 'data'
00650 
00651 # Write as a multisource UVFITS (with SU table)
00652 # even though it will have only one field in it
00653 multisource = True
00654 
00655 # Run asynchronously so as not to interfere with other tasks
00656 # (BETA: also avoids crash on next importuvfits)
00657 async = True
00658 
00659 saveinputs('exportuvfits',prefix+'.exportuvfits.saved')
00660 
00661 myhandle = exportuvfits()
00662 
00663 print "The return value for this exportuvfits async task for tm is "+str(myhandle)
00664 
00665 #=====================================================================
00666 #
00667 # UV-plane continuum subtraction on the target
00668 # (this will update the CORRECTED_DATA column)
00669 #
00670 print '--UV Continuum Subtract--'
00671 default('uvcontsub')
00672 
00673 vis = msfile
00674 
00675 # Pick off N5921 
00676 field = 'N5921*'
00677 
00678 # Use channels 4-6 and 50-59 for continuum
00679 fitspw='0:4~6;50~59'
00680 
00681 # Output all of spw 0
00682 spw = '0'
00683 
00684 # Averaging time (none)
00685 solint = 0.0
00686 
00687 # Fit only a mean level
00688 fitorder = 0
00689 
00690 # Do the uv-plane subtraction
00691 fitmode = 'subtract'
00692 
00693 # Let it split out the data automatically for us
00694 splitdata = True
00695 
00696 saveinputs('uvcontsub',prefix+'.uvcontsub.saved')
00697 
00698 uvcontsub()
00699 
00700 # You will see it made two new MS:
00701 # ngc5921.usecase.ms.cont
00702 # ngc5921.usecase.ms.contsub
00703 
00704 srcsplitms = msfile + '.contsub'
00705 
00706 # Note that ngc5921.usecase.ms.contsub contains the uv-subtracted
00707 # visibilities (in its DATA column), and ngc5921.usecase.ms.cont
00708 # the pseudo-continuum visibilities (as fit).
00709 
00710 # The original ngc5921.usecase.ms now contains the uv-continuum
00711 # subtracted vis in its CORRECTED_DATA column and the continuum
00712 # in its MODEL_DATA column as per the fitmode='subtract'
00713 
00714 # Done with calibration
00715 #=====================================================================
00716 #
00717 # Now make a dirty image cube
00718 #
00719 print '--Clean (invert)--'
00720 default('clean')
00721 
00722 # Pick up our split source continuum-subtracted data
00723 vis = srcsplitms
00724 
00725 # Make an image root file name
00726 imname = prefix + '.dirty'
00727 imagename = imname
00728 
00729 # Set up the output image cube
00730 mode = 'channel'
00731 nchan = 46
00732 start = 5
00733 width = 1
00734 
00735 # This is a single-source MS with one spw
00736 field = '0'
00737 spw = ''
00738 
00739 # Set the output image size and cell size (arcsec)
00740 imsize = [256,256]
00741 
00742 # Pixel size 15 arcsec for this data (1/3 of 45" beam)
00743 # VLA D-config L-band
00744 cell = [15.,15.]
00745 
00746 # Fix maximum number of iterations
00747 niter = 0
00748 
00749 # Set up the weighting
00750 # Use Briggs weighting (a moderate value, on the uniform side)
00751 weighting = 'briggs'
00752 robust = 0.5
00753 
00754 saveinputs('clean',prefix+'.invert.saved')
00755 
00756 clean()
00757 
00758 # Should find stuff in the logger like:
00759 #
00760 # Fitted beam used in restoration: 51.5204 by 45.5982 (arcsec)
00761 #     at pa 14.6547 (deg) 
00762 #
00763 # It will have made the images:
00764 # -----------------------------
00765 # ngc5921.usecase.clean.image
00766 # ngc5921.usecase.clean.model
00767 # ngc5921.usecase.clean.residual
00768 # ngc5921.usecase.clean.boxclean.mask
00769 
00770 dirtyimage = imname+'.image'
00771  
00772 #=====================================================================
00773 #
00774 # Get the dirty image cube statistics
00775 #
00776 print '--Imstat (dirty cube)--'
00777 default('imstat')
00778 
00779 imagename = dirtyimage
00780 
00781 # Do whole image
00782 box = ''
00783 
00784 dirtystats = imstat()
00785 
00786 # Statistics will printed to the terminal, and the output 
00787 # parameter will contain a dictionary of the statistics
00788 
00789 #=====================================================================
00790 #
00791 # Now clean an image cube of N5921
00792 #
00793 print '--Clean (clean)--'
00794 default('clean')
00795 
00796 # Pick up our split source continuum-subtracted data
00797 vis = srcsplitms
00798 
00799 # Make an image root file name
00800 imname = prefix + '.clean'
00801 imagename = imname
00802 
00803 # Set up the output image cube
00804 mode = 'channel'
00805 nchan = 46
00806 start = 5
00807 width = 1
00808 
00809 # This is a single-source MS with one spw
00810 field = '0'
00811 spw = ''
00812 
00813 # Standard gain factor 0.1
00814 gain = 0.1
00815 
00816 # Set the output image size and cell size (arcsec)
00817 imsize = [256,256]
00818 
00819 # Do a simple Clark clean
00820 psfmode = 'clark'
00821 # No Cotton-Schwab iterations
00822 csclean = False
00823 
00824 # If desired, you can do a Cotton-Schwab clean
00825 # but will have only marginal improvement for this data
00826 #csclean = True
00827 # Twice as big for Cotton-Schwab (cleans inner quarter)
00828 #imsize = [512,512]
00829 
00830 # Pixel size 15 arcsec for this data (1/3 of 45" beam)
00831 # VLA D-config L-band
00832 cell = [15.,15.]
00833 
00834 # Fix maximum number of iterations
00835 niter = 6000
00836 
00837 # Also set flux residual threshold (in mJy)
00838 threshold=8.0
00839 
00840 # Set up the weighting
00841 # Use Briggs weighting (a moderate value, on the uniform side)
00842 weighting = 'briggs'
00843 robust = 0.5
00844 
00845 # Set a cleanbox +/-20 pixels around the center 128,128
00846 mask = [108,108,148,148]
00847 
00848 # But if you had a cleanbox saved in a file, e.g. "regionfile.txt"
00849 # you could use it:
00850 #mask='regionfile.txt'
00851 #
00852 # If you don't want any clean boxes or masks, then
00853 #mask = ''
00854 
00855 # If you want interactive clean set to True
00856 #interactive=True
00857 interactive=False
00858 
00859 saveinputs('clean',prefix+'.clean.saved')
00860 
00861 clean()
00862 
00863 # Should find stuff in the logger like:
00864 #
00865 # Fitted beam used in restoration: 51.5643 by 45.6021 (arcsec)
00866 #     at pa 14.5411 (deg)
00867 #
00868 # It will have made the images:
00869 # -----------------------------
00870 # ngc5921.usecase.clean.image
00871 # ngc5921.usecase.clean.model
00872 # ngc5921.usecase.clean.residual
00873 # ngc5921.usecase.clean.boxclean.mask
00874 
00875 clnimage = imname+'.image'
00876 
00877 #=====================================================================
00878 #
00879 # Done with imaging
00880 # Now view the image cube of N5921
00881 #
00882 #print '--View image--'
00883 #viewer(clnimage,'image')
00884 
00885 #=====================================================================
00886 #
00887 # Export the Final CLEAN Image as FITS
00888 #
00889 print '--Final Export CLEAN FITS--'
00890 default('exportfits')
00891 
00892 clnfits = prefix + '.clean.fits'
00893 
00894 imagename = clnimage
00895 fitsimage = clnfits
00896 
00897 # Run asynchronously so as not to interfere with other tasks
00898 # (BETA: also avoids crash on next importfits)
00899 async = True
00900 
00901 saveinputs('exportfits',prefix+'.exportfits.saved')
00902 
00903 myhandle2 = exportfits()
00904 
00905 print "The return value for this exportfits async task for tm is "+str(myhandle2)
00906 
00907 #=====================================================================
00908 #
00909 # Print the image header
00910 #
00911 print '--Imhead--'
00912 default('imhead')
00913 
00914 imagename = clnimage
00915 
00916 mode = 'summary'
00917 
00918 imhead()
00919 
00920 # A summary of the cube will be seen in the logger
00921 
00922 #=====================================================================
00923 #
00924 # Get the cube statistics
00925 #
00926 print '--Imstat (cube)--'
00927 default('imstat')
00928 
00929 imagename = clnimage
00930 
00931 # Do whole image
00932 box = ''
00933 # or you could stick to the cleanbox
00934 #box = '108,108,148,148'
00935 
00936 cubestats = imstat()
00937 
00938 # Statistics will printed to the terminal, and the output 
00939 # parameter will contain a dictionary of the statistics
00940 
00941 #=====================================================================
00942 #
00943 # Get some image moments
00944 #
00945 print '--ImMoments--'
00946 default('immoments')
00947 
00948 imagename = clnimage
00949 
00950 # Do first and second moments
00951 moments = [0,1]
00952 
00953 # Need to mask out noisy pixels, currently done
00954 # using hard global limits
00955 excludepix = [-100,0.009]
00956 
00957 # Include all planes
00958 planes = ''
00959 
00960 # Output root name
00961 momfile = prefix + '.moments'
00962 outfile = momfile
00963 
00964 saveinputs('immoments',prefix+'.immoments.saved')
00965 
00966 immoments()
00967 
00968 momzeroimage = momfile + '.integrated'
00969 momoneimage = momfile + '.weighted_coord'
00970 
00971 # It will have made the images:
00972 # --------------------------------------
00973 # ngc5921.usecase.moments.integrated
00974 # ngc5921.usecase.moments.weighted_coord
00975 
00976 #
00977 #=====================================================================
00978 #
00979 # Get some statistics of the moment images
00980 #
00981 print '--Imstat (moments)--'
00982 default('imstat')
00983 
00984 imagename = momzeroimage
00985 momzerostats = imstat()
00986 
00987 imagename = momoneimage
00988 momonestats = imstat()
00989 
00990 #=====================================================================
00991 #
00992 # Set up an output logfile
00993 import datetime
00994 datestring=datetime.datetime.isoformat(datetime.datetime.today())
00995 
00996 outfile = 'out.'+prefix+'.'+datestring+'.log'
00997 logfile=open(outfile,'w')
00998 print >>logfile,'Results for '+prefix+' :'
00999 print >>logfile,""
01000 
01001 #=====================================================================
01002 #
01003 # Can do some image statistics if you wish
01004 # Treat this like a regression script
01005 # WARNING: currently requires toolkit
01006 #
01007 print ' NGC5921 results '
01008 print ' =============== '
01009 
01010 print >>logfile,' NGC5921 results '
01011 print >>logfile,' =============== '
01012 
01013 #
01014 # Use the ms tool to get max of the MSs
01015 # Eventually should be available from a task
01016 #
01017 # Pull the max cal amp value out of the MS
01018 ms.open(calsplitms)
01019 thistest_cal = max(ms.range(["amplitude"]).get('amplitude'))
01020 ms.close()
01021 oldtest_cal = 34.0338668823
01022 diff_cal = abs((oldtest_cal-thistest_cal)/oldtest_cal)
01023 
01024 print ' Calibrator data ampl max = ',thistest_cal
01025 print '   Previous: cal data max = ',oldtest_cal
01026 print '   Difference (fractional) = ',diff_cal
01027 print ''
01028 
01029 print >>logfile,' Calibrator data ampl max = ',thistest_cal
01030 print >>logfile,'   Previous: cal data max = ',oldtest_cal
01031 print >>logfile,'   Difference (fractional) = ',diff_cal
01032 print >>logfile,''
01033 
01034 # Pull the max src amp value out of the MS
01035 ms.open(srcsplitms)
01036 thistest_src = max(ms.range(["amplitude"]).get('amplitude'))
01037 ms.close()
01038 oldtest_src =  46.2060050964 # now in all chans
01039 diff_src = abs((oldtest_src-thistest_src)/oldtest_src)
01040 
01041 print ' Target Src data ampl max = ',thistest_src
01042 print '   Previous: src data max = ',oldtest_src
01043 print '   Difference (fractional) = ',diff_src
01044 print ''
01045 
01046 print >>logfile,' Target Src data ampl max = ',thistest_src
01047 print >>logfile,'   Previous: src data max = ',oldtest_src
01048 print >>logfile,'   Difference (fractional) = ',diff_src
01049 print >>logfile,''
01050 
01051 #
01052 # Now use the stats produced by imstat above
01053 #
01054 # First the dirty image
01055 #
01056 # Pull the max from the cubestats dictionary
01057 # created above using imstat
01058 thistest_dirtymax=dirtystats['max'][0]
01059 oldtest_dirtymax = 0.0515365377069
01060 diff_dirtymax = abs((oldtest_dirtymax-thistest_dirtymax)/oldtest_dirtymax)
01061 
01062 print ' Dirty Image max = ',thistest_dirtymax
01063 print '   Previous: max = ',oldtest_dirtymax
01064 print '   Difference (fractional) = ',diff_dirtymax
01065 print ''
01066 
01067 print >>logfile,' Dirty Image max = ',thistest_dirtymax
01068 print >>logfile,'   Previous: max = ',oldtest_dirtymax
01069 print >>logfile,'   Difference (fractional) = ',diff_dirtymax
01070 print >>logfile,''
01071 
01072 # Pull the rms from the cubestats dictionary
01073 thistest_dirtyrms=dirtystats['rms'][0]
01074 oldtest_dirtyrms = 0.00243866862729
01075 diff_dirtyrms = abs((oldtest_dirtyrms-thistest_dirtyrms)/oldtest_dirtyrms)
01076 
01077 print ' Dirty Image rms = ',thistest_dirtyrms
01078 print '   Previous: rms = ',oldtest_dirtyrms
01079 print '   Difference (fractional) = ',diff_dirtyrms
01080 print ''
01081 
01082 print >>logfile,' Dirty Image rms = ',thistest_dirtyrms
01083 print >>logfile,'   Previous: rms = ',oldtest_dirtyrms
01084 print >>logfile,'   Difference (fractional) = ',diff_dirtyrms
01085 print >>logfile,''
01086 
01087 # Now the clean image
01088 #
01089 # Pull the max from the cubestats dictionary
01090 # created above using imstat
01091 thistest_immax=cubestats['max'][0]
01092 oldtest_immax = 0.052414759993553162
01093 diff_immax = abs((oldtest_immax-thistest_immax)/oldtest_immax)
01094 
01095 print ' Clean Image max = ',thistest_immax
01096 print '   Previous: max = ',oldtest_immax
01097 print '   Difference (fractional) = ',diff_immax
01098 print ''
01099 
01100 print >>logfile,' Clean Image max = ',thistest_immax
01101 print >>logfile,'   Previous: max = ',oldtest_immax
01102 print >>logfile,'   Difference (fractional) = ',diff_immax
01103 print >>logfile,''
01104 
01105 # Pull the rms from the cubestats dictionary
01106 thistest_imrms=cubestats['rms'][0]
01107 oldtest_imrms = 0.0020218724384903908
01108 diff_imrms = abs((oldtest_imrms-thistest_imrms)/oldtest_imrms)
01109 
01110 print ' Clean image rms = ',thistest_imrms
01111 print '   Previous: rms = ',oldtest_imrms
01112 print '   Difference (fractional) = ',diff_imrms
01113 print ''
01114 
01115 print >>logfile,' Clean image rms = ',thistest_imrms
01116 print >>logfile,'   Previous: rms = ',oldtest_imrms
01117 print >>logfile,'   Difference (fractional) = ',diff_imrms
01118 print >>logfile,''
01119 
01120 # Now the moment images
01121 #
01122 # Pull the max from the momzerostats dictionary
01123 thistest_momzeromax=momzerostats['max'][0]
01124 oldtest_momzeromax = 1.40223777294
01125 diff_momzeromax = abs((oldtest_momzeromax-thistest_momzeromax)/oldtest_momzeromax)
01126 
01127 print ' Moment 0 image max = ',thistest_momzeromax
01128 print '   Previous: m0 max = ',oldtest_momzeromax
01129 print '   Difference (fractional) = ',diff_momzeromax
01130 print ''
01131 
01132 print >>logfile,' Moment 0 image max = ',thistest_momzeromax
01133 print >>logfile,'   Previous: m0 max = ',oldtest_momzeromax
01134 print >>logfile,'   Difference (fractional) = ',diff_momzeromax
01135 print >>logfile,''
01136 
01137 # Pull the mean from the momonestats dictionary
01138 thistest_momoneavg=momonestats['mean'][0]
01139 oldtest_momoneavg = 1479.77119646
01140 diff_momoneavg = abs((oldtest_momoneavg-thistest_momoneavg)/oldtest_momoneavg)
01141 
01142 print ' Moment 1 image mean = ',thistest_momoneavg
01143 print '   Previous: m1 mean = ',oldtest_momoneavg
01144 print '   Difference (fractional) = ',diff_momoneavg
01145 print ''
01146 print '--- Done ---'
01147 
01148 print >>logfile,' Moment 1 image mean = ',thistest_momoneavg
01149 print >>logfile,'   Previous: m1 mean = ',oldtest_momoneavg
01150 print >>logfile,'   Difference (fractional) = ',diff_momoneavg
01151 print >>logfile,''
01152 print >>logfile,'--- Done ---'
01153 
01154 # Should see output like:
01155 #
01156 # Clean image max should be  0.0524147599936
01157 # Found : Image Max =  0.0523551553488
01158 # Difference (fractional) =  0.00113717290288
01159 #
01160 # Clean image rms should be  0.00202187243849
01161 # Found : Image rms =  0.00202226242982
01162 # Difference (fractional) =  0.00019288621809
01163 #
01164 # Moment 0 image max should be  1.40223777294
01165 # Found : Moment 0 Max =  1.40230333805
01166 # Difference (fractional) =  4.67574844349e-05
01167 #
01168 # Moment 1 image mean should be  1479.77119646
01169 # Found : Moment 1 Mean =  1479.66974528
01170 # Difference (fractional) =  6.85586935973e-05
01171 #
01172 #=====================================================================
01173 # Done
01174 #
01175 logfile.close()
01176 print "Results are in "+outfile