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