""" 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, 17 May 2017, and 19 May 2017 (3 execution blocks) LB1: 2016.1.00484.L Observed 29 September 2017 (1 execution block) reducer: N. Kurtovic """ """ Starting matter """ import os execfile('reduction_utils.py') skip_plots = True # if True, can run script non-interactively """ Input for loading data """ prefix = 'AS205' SB1_path = '/full_path/to_calibrated/msfile.ms' SB2_path = '/full_path/to_calibrated/msfile.ms' SB3_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' : 'V866 Sco', 'line_spws': np.array([0,2,3]), # CO SPWs 'line_freqs': np.array([2.30538e11, 2.2039868420e11, 2.1956035410e11]), }, 'SB2': {'vis' : SB2_path, 'name' : 'SB2', 'field' : 'V866 Sco', 'line_spws': np.array([0,2,3]), # CO SPWs 'line_freqs': np.array([2.30538e11, 2.2039868420e11, 2.1956035410e11]), }, 'SB3': {'vis' : SB3_path, 'name' : 'SB3', 'field': 'AS_205', 'line_spws': np.array([0,4,8]), # CO SPWs 'line_freqs': np.array([2.30538e11, 2.30538e11, 2.30538e11]), }, 'LB1': {'vis' : LB1_path, 'name' : 'LB1', 'field' : 'AS_205', 'line_spws': np.array([3]), # CO SPWs 'line_freqs': np.array([2.30538e11]), } } """ Since SB1 and SB2 are from Cycle 0, need to re-calculate their visibility weights properly before continuum averaging. """ os.system('rm -rf '+prefix+'_SB1_in.ms*') split(vis=data_params['SB1']['vis'], field=data_params['SB1']['field'], spw='0~3', outputvis=prefix+'_SB1_in.ms', datacolumn='data') cchans = '0:0~2200;3500~3839, 1:0~3839, 2:0~650;1860~3839, 3:0~140;1350~3839' fchans = '2:651~1855, 3:148~1348' statwt(vis=prefix+'_SB1_in.ms', fitspw=cchans, datacolumn='data') flagmanager(vis=prefix+'_SB1_in.ms', mode='save', versionname='before_cont_flags', comment='Flag states before spectral lines are flagged') flagdata(vis=prefix+'_SB1_in.ms', mode='manual', spw=fchans, flagbackup=False, field=data_params['SB1']['field']) os.system('rm -rf '+prefix+'_SB1_lines.ms*') split(vis=prefix+'_SB1_in.ms', field=data_params['SB1']['field'], spw='0~3', outputvis=prefix+'_SB1_lines.ms', width=[1,3840,3840,3840], timebin='0s', datacolumn='data', intent='OBSERVE_TARGET#ON_SOURCE', keepflags=False) flagmanager(vis=prefix+'_SB1_in.ms', mode='restore', versionname='before_cont_flags') os.system('rm -rf '+prefix+'_SB2_in.ms*') split(vis=data_params['SB2']['vis'], field=data_params['SB2']['field'], spw='0~3', outputvis=prefix+'_SB2_in.ms', datacolumn='data') cchans = '0:0~1860;3130~3839, 1:0~3839, 2:0~990;2210~3839, 3:0~490;1700~3839' fchans = '0:1868~3128, 2:999~2204, 3:495~1695' statwt(vis=prefix+'_SB2_in.ms', fitspw=cchans, datacolumn='data') flagmanager(vis=prefix+'_SB2_in.ms', mode='save', versionname='before_cont_flags', comment='Flag states before spectral lines are flagged') flagdata(vis=prefix+'_SB2_in.ms', mode='manual', spw=fchans, flagbackup=False, field=data_params['SB2']['field']) os.system('rm -rf '+prefix+'_SB2_lines.ms*') split(vis=prefix+'_SB2_in.ms', field=data_params['SB2']['field'], spw='0~3', outputvis=prefix+'_SB2_lines.ms', width=[1,3840,3840,3840], timebin='0s', datacolumn='data', intent='OBSERVE_TARGET#ON_SOURCE', keepflags=False) flagmanager(vis=prefix+'_SB2_in.ms', mode='restore', versionname='before_cont_flags') """ Split out the SB3 data, with the continuum SPWs averaged down """ os.system('rm -rf %s*' % prefix+'_SB3_lines') split(vis=data_params['SB3']['vis'], field=data_params['SB3']['field'], spw='0~11', outputvis=prefix+'_SB3_lines.ms', width=[1,128,128,128,1,128,128,128,1,128,128,128], datacolumn='data', intent='OBSERVE_TARGET#ON_SOURCE', keepflags=False) """ Split out the LB data, with the continuum SPWs averaged down """ os.system('rm -rf '+prefix+'_LB1_lines*') split(vis=data_params['LB1']['vis'], field=data_params['LB1']['field'], spw='0~3', outputvis=prefix+'_LB1_lines.ms', width=[128,128,128,1], timebin='6s', datacolumn='data', intent='OBSERVE_TARGET#ON_SOURCE', keepflags=False) """ Apply same shifts and re-scalings as for continuum """ common_dir = 'J2000 16h11m31.35202s -18d38m26.232956s' SB_shift = prefix+'_SB1_lines_shift' os.system('rm -rf '+SB_shift+'.ms*') fixvis(vis=prefix+'_SB1_lines.ms', outputvis=SB_shift+'.ms', field=data_params['SB1']['field'], phasecenter='J2000 16h11m31.35473s -18d38m26.09852s') fixplanets(vis=SB_shift+'.ms', field=data_params['SB1']['field'], direction=common_dir) SB_shift = prefix+'_SB2_lines_shift' os.system('rm -rf '+SB_shift+'.ms*') fixvis(vis=prefix+'_SB2_lines.ms', outputvis=SB_shift+'.ms', field=data_params['SB2']['field'], phasecenter='J2000 16h11m31.35629s -18d38m26.13575s') fixplanets(vis=SB_shift+'.ms', field=data_params['SB2']['field'], direction=common_dir) split_all_obs(prefix+'_SB3_lines.ms', prefix+'_SB3_lines_exec') SB_shift = prefix+'_SB3_lines_exec0_shift' os.system('rm -rf '+SB_shift+'.ms*') fixvis(vis=prefix+'_SB3_lines_exec0.ms', outputvis=SB_shift+'.ms', field=data_params['SB3']['field'], phasecenter='J2000 16h11m31.35262s -18d38m26.252276s') fixplanets(vis=SB_shift+'.ms', field=data_params['SB3']['field'], direction=common_dir) SB_shift = prefix+'_SB3_lines_exec1_shift' os.system('rm -rf '+SB_shift+'.ms*') fixvis(vis=prefix+'_SB3_lines_exec1.ms', outputvis=SB_shift+'.ms', field=data_params['SB3']['field'], phasecenter='J2000 16h11m31.35272s -18d38m26.215186s') fixplanets(vis=SB_shift+'.ms', field=data_params['SB3']['field'], direction=common_dir) SB_shift = prefix+'_SB3_lines_exec2_shift' os.system('rm -rf '+SB_shift+'.ms*') fixvis(vis=prefix+'_SB3_lines_exec2.ms', outputvis=SB_shift+'.ms', field=data_params['SB3']['field'], phasecenter='J2000 16h11m31.35055s -18d38m26.243996s') fixplanets(vis=SB_shift+'.ms', field=data_params['SB3']['field'], direction=common_dir) LB_shift = prefix+'_LB1_lines_shift' os.system('rm -rf '+LB_shift+'.ms*') fixvis(vis=prefix+'_LB1_lines.ms', outputvis=LB_shift+'.ms', \ field=data_params['LB1']['field'], phasecenter=common_dir) rescale_flux(prefix+'_SB1_lines_shift.ms', [1.080]) rescale_flux(prefix+'_SB2_lines_shift.ms', [1.033]) rescale_flux(prefix+'_SB3_lines_exec1_shift.ms', [1.044]) rescale_flux(prefix+'_SB3_lines_exec2_shift.ms', [1.040]) rescale_flux(prefix+'_LB1_lines_shift.ms', [1.009]) """ Apply the self-calibration solutions to the SB data """ SB_CO = prefix+'_SB_CO' os.system('rm -rf %s*' % SB_CO) concat(vis=[prefix+'_SB1_lines_shift_rescaled.ms', prefix+'_SB2_lines_shift_rescaled.ms', prefix+'_SB3_lines_exec0_shift.ms', prefix+'_SB3_lines_exec1_shift_rescaled.ms', prefix+'_SB3_lines_exec2_shift_rescaled.ms'], concatvis=SB_CO+'.ms', dirtol='0.1arcsec', copypointing=False) applycal(vis=SB_CO+'.ms', spw='0~15', gaintable=[prefix+'_SB.p1', prefix+'_SB.p2', prefix+'_SB.ap'], interp='linearPD', calwt=True, flagbackup=False) os.system('rm -rf '+SB_CO+'_selfcal.ms*') split(vis=SB_CO+'.ms', outputvis=SB_CO+'_selfcal.ms', datacolumn='corrected', keepflags=False) # save some space by deleting intermediate files os.system('rm -rf '+SB_CO+'.ms*') os.system('rm -rf '+prefix+'_SB1_lines*ms*') os.system('rm -rf '+prefix+'_SB2_lines*ms*') os.system('rm -rf '+prefix+'_SB3_lines_exec*ms*') """ Now apply the self-calibration solutions to the combined dataset """ combined_CO = prefix+'_combined_CO' os.system('rm -rf '+combined_CO+'.ms*') concat(vis=[SB_CO+'_selfcal.ms', prefix+'_LB1_lines_shift_rescaled.ms'], concatvis=combined_CO+'.ms', dirtol='0.1arcsec', copypointing=False) applycal(vis=combined_CO+'.ms', spw='0~19', spwmap=[[0,0,0,0,4,4,4,4,8,8,8,8,12,12,12,12,16,16,16,16]]*6, gaintable=[prefix+'_combined.p1', prefix+'_combined.p2', prefix+'_combined.p3', prefix+'_combined.p4', prefix+'_combined.ap', prefix+'_combined.ap1'], interp='linearPD', calwt=True, applymode='calonly') os.system('rm -rf '+combined_CO+'_selfcal.ms*') split(vis=combined_CO+'.ms', outputvis=combined_CO+'_selfcal.ms', field='', spw='0,4,8,12,19', datacolumn='corrected', keepflags=False) # save some space by deleting intermediate files os.system('rm -rf '+combined_CO+'.ms*') os.system('rm -rf '+prefix+'_LB1_lines_exec*ms*') """ Continuum subtraction """ fitspw = '0:0~1868;3492~3839,1:0~1841;1998~3839,2:0~1841;1998~3839,3:0~1841;1998~3839,4:0~1841;1998~3839' os.system('rm -rf '+combined_CO+'_selfcal.ms.contsub*') uvcontsub(vis=combined_CO+'_selfcal.ms', spw='0~4', fitspw=fitspw, excludechans=False, solint='int', fitorder=1, want_cont=False) """ Define the channels of interest """ chanstart = '-9.0km/s' chanwidth = '0.35km/s' nchan = 60 """ Split and regrid into the channels of interest """ # Continuum-subtracted SB_only = prefix+'_CO_SBonly.ms.contsub' os.system('rm -rf '+SB_only+'*') split(vis=combined_CO+'_selfcal.ms.contsub', outputvis=SB_only, spw='0', datacolumn='data') SB_cvel = SB_only+'.cvel' os.system('rm -rf '+SB_cvel+'*') mstransform(vis=SB_only, outputvis=SB_cvel, keepflags=False, datacolumn='data', regridms=True, mode='velocity', start=chanstart, width=chanwidth, nchan=nchan, outframe='LSRK', veltype='radio', restfreq='230.538GHz') LB_only = prefix+'_CO_LBonly.ms.contsub' os.system('rm -rf '+LB_only+'*') split(vis=combined_CO+'_selfcal.ms.contsub', outputvis=LB_only, spw='1,2,3,4', datacolumn='data') LB_cvel = LB_only+'.cvel' os.system('rm -rf '+LB_cvel+'*') mstransform(vis=LB_only, outputvis=LB_cvel, keepflags=False, datacolumn='data', regridms=True, mode='velocity', start=chanstart, width=chanwidth, nchan=nchan, outframe='LSRK', veltype='radio', restfreq='230.538GHz') contsub_concat = prefix+'_COcube.ms.contsub' os.system('rm -rf '+contsub_concat) concat(vis=[SB_cvel, LB_cvel], concatvis=contsub_concat, dirtol='0.1arcsec', copypointing=False) # With continuum cSB_only = prefix+'_CO_SBonly.ms' os.system('rm -rf '+cSB_only) split(vis=combined_CO+'_selfcal.ms', outputvis=cSB_only, spw='0', datacolumn='data') cSB_cvel = cSB_only+'.cvel' os.system('rm -rf '+cSB_cvel) mstransform(vis=cSB_only, outputvis=cSB_cvel, keepflags=False, datacolumn='data', regridms=True, mode='velocity', start=chanstart, width=chanwidth, nchan=nchan, outframe='LSRK', veltype='radio', restfreq='230.538GHz') cLB_only = prefix+'_CO_LBonly.ms' os.system('rm -rf '+cLB_only) split(vis=combined_CO+'_selfcal.ms', outputvis=cLB_only, spw='1,2,3,4', datacolumn='data') cLB_cvel = cLB_only+'.cvel' os.system('rm -rf '+cLB_cvel) mstransform(vis=cLB_only, outputvis=cLB_cvel, keepflags=False, datacolumn='data', regridms=True, mode='velocity', start=chanstart, width=chanwidth, nchan=nchan, outframe='LSRK', veltype='radio', restfreq='230.538GHz') cont_concat = prefix+'_COcube.ms' os.system('rm -rf '+cont_concat) concat(vis=[cSB_cvel, cLB_cvel], concatvis=cont_concat, dirtol='0.1arcsec', copypointing=False) """ Imaging (continuum-subtracted only) """ imagename = prefix+'_CO' for ext in ['.image','.mask','.model','.pb','.psf','.residual','.sumwt']: os.system('rm -rf '+ imagename + ext) tclean(vis=prefix+'_COcube.ms.contsub', imagename=imagename, specmode='cube', imsize=1500, deconvolver='multiscale', start=chanstart, width=chanwidth, nchan=nchan, outframe='LSRK', veltype='radio', restfreq='230.538GHz', cell='0.01arcsec', scales = [0,10,30,100,250], gain=0.3, niter=50000, weighting='briggs', robust=1.0, threshold='3mJy', uvtaper='0.04arcsec', interactive=True, nterms=1, restoringbeam='common') # Note that masking is done interactively """ Final outputs """ """ Save the final MS files """ os.system('tar cvzf '+prefix+'_CO.ms.tgz '+prefix+'_COcube.ms.contsub') os.system('tar cvzf '+prefix+'_COcont.ms.tgz '+prefix+'_COcube.ms') """ Save the imaged datacube """ exportfits(imagename+'.image', imagename+'.fits')