""" This script was written for CASA 5.1.1 Datasets calibrated (in order of date observed): SB1: 2016.1.00484.L Observed 14 May 2017 and 17 May 2017 (2 execution blocks) LB1: 2016.1.00484.L Observed 26 September 2017 (1 execution block) reducer: J. Huang """ """ Starting matter """ import os execfile('reduction_utils.py') skip_plots = True # if True, can run script non-interactively """ Input for loading data """ prefix = 'Sz114' SB1_path = '/full_path/to_calibrated/msfile.ms' LB1_path = '/full_path/to_calibrated/msfile.ms' # Note that if you are downloading data from the archive, your SPW numbering # may differ from this script, depending on how you split your data out! data_params = {'SB1': {'vis' : SB1_path, 'name' : 'SB1', 'field': 'Sz_114', 'line_spws': np.array([0, 4]), # CO SPWs 'line_freqs': np.array([2.30538e11, 2.30538e11]), }, 'LB1': {'vis' : LB1_path, 'name' : 'LB1', 'field' : 'Sz_114', 'line_spws': np.array([3]), # CO SPWs 'line_freqs': np.array([2.30538e11]), } } """ Check data (various options here; an example) """ if not skip_plots: for i in data_params.keys(): plotms(vis=data_params[i]['vis'], xaxis='channel', yaxis='amplitude', field=data_params[i]['field'], ydatacolumn='data', avgtime='1e8', avgscan=True, avgbaseline=True, iteraxis='spw') """ Identify 50 km/s-wide region containing CO emission; then flag that and do a spectral average to a pseudo-continuum MS """ for i in data_params.keys(): flagchannels_string = get_flagchannels(data_params[i], prefix, velocity_range=np.array([-15, 25])) avg_cont(data_params[i], prefix, flagchannels=flagchannels_string) """ Define simple masks and clean scales for imaging """ mask_rad = 0.8 # radius of mask in arcsec SB1_mask = 'circle[[%s, %s], %.1farcsec]' % \ ('16h09m01.834s', '-39.05.12.84', mask_rad) LB1_mask = 'circle[[%s, %s], %.1farcsec]' % \ ('16h09m01.834s', '-39.05.12.84', mask_rad) SB_scales = [0, 5, 10, 15] LB_scales = [0, 50, 100, 150] if not skip_plots: """ Image each dataset individually """ # images are saved in the format prefix+'_name_initcont_exec#.ms' image_each_obs(data_params['SB1'], prefix, mask=SB1_mask, scales=SB_scales, threshold='0.2mJy', interactive=False) image_each_obs(data_params['LB1'], prefix, mask=LB1_mask, scales=LB_scales, threshold='0.075mJy', interactive=False) """ Fit Gaussians to roughly estimate centers, inclinations, PAs """ fit_gaussian(prefix+'_SB1_initcont_exec0.image', region=SB1_mask) #Peak : ICRS 16h09m01.834777s -39d05m12.82564s fit_gaussian(prefix+'_SB1_initcont_exec1.image', region=SB1_mask) #Peak : ICRS 16h09m01.833125s -39d05m12.86043s fit_gaussian(prefix+'_LB1_initcont_exec0.image', region=LB1_mask) #Peak : ICRS 16h09m01.833722s -39d05m12.83809s #PA of Gaussian component: 179.30 deg #Inclination of Gaussian component: 4.53 deg """ The emission centers are slightly misaligned. So we split out the individual executions, shift the peaks to the phase center, and reassign the phase centers to a common direction. (Not in script: check that these shifts do what you think they should by re-imaging! They do for us.) """ """ Split out individual MSs for each execution """ split_all_obs(prefix+'_SB1_initcont.ms', prefix+'_SB1_initcont_exec') split_all_obs(prefix+'_LB1_initcont.ms', prefix+'_LB1_initcont_exec') """ Define a common direction (peak of LB) """ common_dir = 'J2000 16h09m01.83407 -039.05.12.824319' """ Shift each MS so emission center is at same phase center """ SB0_shift = prefix+'_SB1_initcont_exec0_shift.ms' os.system('rm -rf '+SB0_shift+'*') fixvis(vis=prefix+'_SB1_initcont_exec0.ms', outputvis=SB0_shift, field=data_params['SB1']['field'], phasecenter='ICRS 16h09m01.834777s -39d05m12.82564s') fixplanets(vis=SB0_shift, field=data_params['SB1']['field'], direction=common_dir) SB1_shift = prefix+'_SB1_initcont_exec1_shift.ms' os.system('rm -rf '+SB1_shift+'*') fixvis(vis=prefix+'_SB1_initcont_exec1.ms', outputvis=SB1_shift, field=data_params['SB1']['field'], phasecenter='ICRS 16h09m01.833125s -39d05m12.86043s') fixplanets(vis=SB1_shift, field=data_params['SB1']['field'], direction=common_dir) LB0_shift = prefix+'_LB1_initcont_exec0_shift.ms' os.system('rm -rf '+LB0_shift+'*') fixvis(vis=prefix+'_LB1_initcont_exec0.ms', outputvis=LB0_shift, field=data_params['LB1']['field'], phasecenter='ICRS 16h09m01.833722s -39d05m12.83809s') fixplanets(vis=LB0_shift, field=data_params['LB1']['field'], direction=common_dir) """ Now that everything is aligned, we inspect the flux calibration. """ if not skip_plots: """ Assign rough emission geometry parameters. """ PA, incl = 180, 4 """ Export MS contents into Numpy save files """ for msfile in [prefix+'_SB1_initcont_exec0_shift.ms', prefix+'_SB1_initcont_exec1_shift.ms', prefix+'_LB1_initcont_exec0_shift.ms']: export_MS(msfile) """ Plot deprojected visibility profiles for all data together """ plot_deprojected([prefix+'_SB1_initcont_exec0_shift.vis.npz', prefix+'_SB1_initcont_exec1_shift.vis.npz', prefix+'_LB1_initcont_exec0_shift.vis.npz'], fluxscale=[1.0, 1.0, 1.0], PA=PA, incl=incl, show_err=False) # LB1 seems slightly high; check how much """ Now inspect offsets by comparing against a reference """ estimate_flux_scale(reference=prefix+'_SB1_initcont_exec0_shift.vis.npz', comparison=prefix+'_LB1_initcont_exec0_shift.vis.npz', incl=incl, PA=PA) #The ratio of comparison : reference is 1.08375 #The scaling factor for gencal is 1.041 for your comparison measurement """ Correct the flux scales where appropriate. """ rescale_flux(prefix+'_LB1_initcont_exec0_shift.ms', [1.041]) """ SELF-CAL for short-baseline data """ """ Merge the SB executions back into a single MS """ SB_cont_p0 = prefix+'_SB_contp0' os.system('rm -rf %s*' % SB_cont_p0) concat(vis=[prefix+'_SB1_initcont_exec0_shift.ms', prefix+'_SB1_initcont_exec1_shift.ms'], concatvis=SB_cont_p0+'.ms', dirtol='0.1arcsec', copypointing=False) """ Set up a clean mask """ mask_ra = '16h09m01.834s' mask_dec = '-39.05.12.824' common_mask = 'circle[[%s,%s],%.1f arcsec]' % (mask_ra, mask_dec, mask_rad) """ Initial clean """ tclean_wrapper(vis=SB_cont_p0+'.ms', imagename=SB_cont_p0, mask=common_mask, scales=SB_scales, threshold='0.15mJy', savemodel='modelcolumn') """ Define a noise annulus, measure the peak SNR in map """ noise_annulus = "annulus[[%s, %s],['%.2farcsec', '4.25arcsec']]" % \ (mask_ra, mask_dec, 1.1*mask_rad) estimate_SNR(SB_cont_p0+'.image', disk_mask=common_mask, noise_mask=noise_annulus) #Sz114_SB_contp0.image #Beam 0.281 arcsec x 0.231 arcsec (-81.75 deg) #Flux inside disk mask: 48.72 mJy #Peak intensity of source: 19.07 mJy/beam #rms: 4.88e-02 mJy/beam #Peak SNR: 390.71 """ Self-calibration parameters """ SB_contspws = '0~7' SB_refant = 'DV18@A009' SB1_obs0_timerange = '2017/05/13/00~2017/05/15/00' SB1_obs1_timerange = '2017/05/16/00~2017/05/18/00' """ First round of phase-only self-cal (short baselines only) """ SB_p1 = prefix+'_SB.p1' os.system('rm -rf '+SB_p1) gaincal(vis=SB_cont_p0+'.ms', caltable=SB_p1, gaintype='T', spw=SB_contspws, refant=SB_refant, calmode='p', solint='30s', minsnr=1.5, minblperant=4) if not skip_plots: """ Inspect gain tables """ plotcal(caltable=SB_p1, xaxis='time', yaxis='phase',subplot=441, iteration='antenna', timerange=SB1_obs0_timerange, plotrange=[0,0,-180,180]) plotcal(caltable=SB_p1, xaxis='time', yaxis='phase',subplot=441, iteration='antenna', timerange=SB1_obs1_timerange, plotrange=[0,0,-180,180]) """ Apply the solutions """ applycal(vis=SB_cont_p0+'.ms', spw=SB_contspws, gaintable=[SB_p1], interp='linearPD', calwt=True) """ Split off a corrected MS """ SB_cont_p1 = prefix+'_SB_contp1' os.system('rm -rf %s*' % SB_cont_p1) split(vis=SB_cont_p0+'.ms', outputvis=SB_cont_p1+'.ms', datacolumn='corrected') """ Image the results; check the resulting map """ tclean_wrapper(vis=SB_cont_p1+'.ms', imagename=SB_cont_p1, mask=common_mask, scales=SB_scales, threshold='0.09mJy', savemodel='modelcolumn') estimate_SNR(SB_cont_p1+'.image', disk_mask=common_mask, noise_mask=noise_annulus) #Sz114_SB_contp1.image #Beam 0.281 arcsec x 0.231 arcsec (-81.75 deg) #Flux inside disk mask: 49.96 mJy #Peak intensity of source: 20.76 mJy/beam #rms: 3.22e-02 mJy/beam #Peak SNR: 645.11 """ Second round of phase-only self-cal (short baselines only) """ SB_p2 = prefix+'_SB.p2' os.system('rm -rf '+SB_p2) gaincal(vis=SB_cont_p1+'.ms', caltable=SB_p2, gaintype='T', spw=SB_contspws, refant=SB_refant, calmode='p', solint='18s', minsnr=1.5, minblperant=4) if not skip_plots: """ Inspect gain tables """ plotcal(caltable=SB_p2, xaxis='time', yaxis='phase', subplot=441, iteration='antenna', timerange=SB1_obs0_timerange, plotrange=[0,0,-180,180]) plotcal(caltable=SB_p2, xaxis='time', yaxis='phase', subplot=441, iteration='antenna', timerange=SB1_obs1_timerange, plotrange=[0,0,-180,180]) """ Apply the solutions """ applycal(vis=SB_cont_p1+'.ms', spw=SB_contspws, gaintable=[SB_p2], interp='linearPD', calwt=True) """ Split off a corrected MS """ SB_cont_p2 = prefix+'_SB_contp2' os.system('rm -rf %s*' % SB_cont_p2) split(vis=SB_cont_p1+'.ms', outputvis=SB_cont_p2+'.ms', datacolumn='corrected') """ Image the results; check the resulting map """ tclean_wrapper(vis=SB_cont_p2+'.ms', imagename=SB_cont_p2, mask=common_mask, scales=SB_scales, threshold='0.09mJy', savemodel='modelcolumn') estimate_SNR(SB_cont_p2+'.image', disk_mask=common_mask, noise_mask=noise_annulus) #Sz114_SB_contp2.image #Beam 0.281 arcsec x 0.231 arcsec (-81.69 deg) #Flux inside disk mask: 50.01 mJy #Peak intensity of source: 20.95 mJy/beam #rms: 3.23e-02 mJy/beam #Peak SNR: 649.14 """ Amplitude self-cal (short baselines only) """ SB_ap = prefix+'_SB.ap' os.system('rm -rf '+SB_ap) gaincal(vis=SB_cont_p2+'.ms', caltable=SB_ap, gaintype='T', spw=SB_contspws, refant=SB_refant, calmode='ap', solint='inf', minsnr=3.0, minblperant=4, solnorm=False) if not skip_plots: """ Inspect gain tables """ plotcal(caltable=SB_ap, xaxis='time', yaxis='amp', subplot=441, iteration='antenna', timerange=SB1_obs0_timerange, plotrange=[0,0,0,2]) plotcal(caltable=SB_ap, xaxis='time', yaxis='amp', subplot=441, iteration='antenna', timerange=SB1_obs1_timerange, plotrange=[0,0,0,2]) """ Apply the solutions """ applycal(vis=SB_cont_p2+'.ms', spw=SB_contspws, gaintable=[SB_ap], interp='linearPD', calwt=True) """ Split off a corrected MS """ SB_cont_ap = prefix+'_SB_contap' os.system('rm -rf %s*' % SB_cont_ap) split(vis=SB_cont_p2+'.ms', outputvis=SB_cont_ap+'.ms', datacolumn='corrected') """ Image the results; check the resulting map """ tclean_wrapper(vis=SB_cont_ap+'.ms', imagename=SB_cont_ap, mask=common_mask, scales=SB_scales, threshold='0.09mJy', savemodel='modelcolumn') estimate_SNR(SB_cont_ap+'.image', disk_mask=common_mask, noise_mask=noise_annulus) #Sz114_SB_contap.image #Beam 0.283 arcsec x 0.232 arcsec (-81.55 deg) #Flux inside disk mask: 49.26 mJy #Peak intensity of source: 20.95 mJy/beam #rms: 3.02e-02 mJy/beam #Peak SNR: 693.05 """ SELF-CAL for the combined (short-baseline + long-baseline) data """ """ Merge the SB+LB executions into a single MS """ combined_cont_p0 = prefix+'_combined_contp0' os.system('rm -rf %s*' % combined_cont_p0) concat(vis=[SB_cont_ap+'.ms', prefix+'_LB1_initcont_exec0_shift_rescaled.ms'], concatvis=combined_cont_p0+'.ms', dirtol='0.1arcsec', copypointing=False) """ Initial clean """ tclean_wrapper(vis=combined_cont_p0+'.ms', imagename=combined_cont_p0, mask=common_mask, scales=LB_scales, threshold='0.05mJy', savemodel='modelcolumn') estimate_SNR(combined_cont_p0+'.image', disk_mask=common_mask, noise_mask=noise_annulus) #Sz114_combined_contp0.image #Beam 0.067 arcsec x 0.028 arcsec (-88.04 deg) #Flux inside disk mask: 49.64 mJy #Peak intensity of source: 3.31 mJy/beam #rms: 1.90e-02 mJy/beam #Peak SNR: 173.76 """ Self-calibration parameters """ combined_contspws = '0~11' combined_refant = 'DV24@A090,DV18@A009' combined_spwmap = [0,0,0,0,4,4,4,4,8,8,8,8] LB1_obs0_timerange = '2017/09/25/00~2017/09/27/00' """ First round of phase-only self-cal (all data) """ combined_p1 = prefix+'_combined.p1' os.system('rm -rf '+combined_p1) gaincal(vis=combined_cont_p0+'.ms', caltable=combined_p1, gaintype='T', combine='spw,scan', spw=combined_contspws, refant=combined_refant, calmode='p', solint='360s', minsnr=1.5, minblperant=4) if not skip_plots: """ Inspect gain tables """ plotcal(caltable=combined_p1, xaxis='time', yaxis='phase', subplot=441, iteration='antenna', timerange=LB1_obs0_timerange, plotrange=[0,0,-180,180]) """ Apply the solutions """ applycal(vis=combined_cont_p0+'.ms', spw=combined_contspws, spwmap=combined_spwmap, gaintable=[combined_p1], interp='linearPD', calwt=True, applymode='calonly') """ Split off a corrected MS """ combined_cont_p1 = prefix+'_combined_contp1' os.system('rm -rf %s*' % combined_cont_p1) split(vis=combined_cont_p0+'.ms', outputvis=combined_cont_p1+'.ms', datacolumn='corrected') """ Image the results; check the resulting map """ tclean_wrapper(vis=combined_cont_p1+'.ms', imagename=combined_cont_p1, mask=common_mask, scales=LB_scales, threshold='0.05mJy', savemodel='modelcolumn') estimate_SNR(combined_cont_p1+'.image', disk_mask=common_mask, noise_mask=noise_annulus) #Sz114_combined_contp1.image #Beam 0.067 arcsec x 0.028 arcsec (-88.04 deg) #Flux inside disk mask: 49.24 mJy #Peak intensity of source: 3.33 mJy/beam #rms: 1.89e-02 mJy/beam #Peak SNR: 175.54 """ Second round of phase-only self-cal (all data) """ combined_p2 = prefix+'_combined.p2' os.system('rm -rf '+combined_p2) gaincal(vis=combined_cont_p1+'.ms', caltable=combined_p2, gaintype='T', combine='spw,scan', spw=combined_contspws, refant=combined_refant, calmode='p', solint='180s', minsnr=1.5, minblperant=4) if not skip_plots: """ Inspect gain tables """ plotcal(caltable=combined_p2, xaxis='time', yaxis='phase', subplot=441, iteration='antenna', timerange=LB1_obs0_timerange, plotrange=[0,0,-180,180]) """ Apply the solutions """ applycal(vis=combined_cont_p1+'.ms', spw=combined_contspws, spwmap=combined_spwmap, gaintable=[combined_p2], interp='linearPD', calwt=True, applymode='calonly') """ Split off a corrected MS """ combined_cont_p2 = prefix+'_combined_contp2' os.system('rm -rf %s*' % combined_cont_p2) split(vis=combined_cont_p1+'.ms', outputvis=combined_cont_p2+'.ms', datacolumn='corrected') """ Image the results; check the resulting map """ tclean_wrapper(vis=combined_cont_p2+'.ms', imagename=combined_cont_p2, mask=common_mask, scales=LB_scales, threshold='0.05mJy', savemodel='modelcolumn') estimate_SNR(combined_cont_p2+'.image', disk_mask=common_mask, noise_mask=noise_annulus) #Sz114_combined_contp2.image #Beam 0.067 arcsec x 0.028 arcsec (-88.04 deg) #Flux inside disk mask: 49.27 mJy #Peak intensity of source: 3.36 mJy/beam #rms: 1.89e-02 mJy/beam #Peak SNR: 178.15 # Additional phase-only cal on shorter intervals or amp self-cal attempts are # not helpful. """ Final outputs """ """ Save the final MS """ os.system('cp -r '+combined_cont_p2+'.ms '+prefix+'_continuum.ms') os.system('tar cvzf '+prefix+'_continuum.ms.tgz '+prefix+'_continuum.ms') """ Make a fiducial continuum image (based on experimentation) """ tclean_wrapper(vis=combined_cont_p2+'.ms', imagename=prefix+'_continuum', mask=common_mask, scales=LB_scales, threshold='0.05mJy') exportfits(prefix+'_continuum.image', prefix+'_continuum.fits')