#!/usr/bin/env python # =====================================================================================# # TEMPLATE IMAGING SCRIPT # # =====================================================================================# # # Helpful tip: Use the commands %cpaste or %paste to copy and paste # indented sections of code into the casa command line. # The commands below serve as a guide to best practices for imaging # ALMA data. It does not replace careful thought on your part while # imaging the data. You can remove or modify sections as necessary # depending on your particular imaging case (e.g., no # self-calibration, continuum only.) Refer to the imaging casaguides # for more information. ######################################## # Check CASA version import re if casadef.casa_version < '4.4.0' : sys.exit("Please use CASA version greater than or equal to 4.4.0 with this script") ################################################## # Create an Averaged Continuum MS finalvis='calibrated_final.ms' # This is your output ms from the data # preparation script. # Use plotms to identify line and continuum spectral windows. plotms(vis=finalvis, xaxis='channel', yaxis='amplitude', ydatacolumn='data', avgtime='1e8', avgscan=True, avgchannel='1', iteraxis='spw' ) # Set spws to be used to form continuum contspws = '0,1,2,3' # If you have complex line emission and no dedicated continuum # windows, you will need to flag the line channels prior to averaging. flagmanager(vis=finalvis,mode='save', versionname='before_cont_flags') initweights(vis=finalvis,wtmode='weight',dowtsp=True) # Flag the "line channels" flagchannels='2:1201~2199,3:1201~2199' # In this example , spws 2&3 have a line between channels 1201 and 2199 and spectral windows 0 and 1 are line-free. flagdata(vis=finalvis,mode='manual', spw=flagchannels,flagbackup=False) # check that flags are as expected, NOTE must check reload on plotms # gui if its still open. plotms(vis=finalvis,yaxis='amp',xaxis='channel', avgchannel='1',avgtime='1e8',avgscan=True,iteraxis='spw') # Average the channels within spws contvis='calibrated_final_cont.ms' rmtables(contvis) os.system('rm -rf ' + contvis + '.flagversions') # Note that to mitigate bandwidth smearing, please keep the width # of averaged channels less than 125MHz in Band 3, 4, and 6, and 250MHz # in Band 7 for both TDM and FDM modes. For example, for a 2GHz TDM window # with 15.625 MHz channels, this means that the maximum width parameter # should be 8 channels for Bands 3, 4, and 6 and 16 channels for Band 7. # This is especially important for any long baseline data. These limits # have been designed to have minimize the reduction of the peak flux to # 95%. See the "for continuum" header for more information on the imaging # wiki for more infomration. split2(vis=finalvis, spw=contspws, outputvis=contvis, width=[128,128,3840,3840], # number of channels to average together. The final channel width should be less than 125MHz in Bands 3, 4, and 6 and 250MHz in Band 7. datacolumn='data') # Check the weights. You will need to change antenna and field to # appropriate values plotms(vis=contvis, yaxis='wtsp',xaxis='freq',spw='',antenna='DA42',field='0') # If you flagged any line channels, restore the previous flags flagmanager(vis=finalvis,mode='restore', versionname='before_cont_flags') # Inspect continuum for any problems plotms(vis=contvis,xaxis='uvdist',yaxis='amp',coloraxis='spw') # ############################################# # Image Parameters # source parameters # ------------------ field='0' # science field(s). For a mosaic, select all mosaic fields. DO NOT LEAVE BLANK ('') OR YOU WILL TRIGGER A BUG IN CLEAN THAT WILL PUT THE WRONG COORDINATE SYSTEM ON YOUR FINAL IMAGE. # imagermode='csclean' # uncomment if single field # imagermode='mosaic' # uncomment if mosaic or if combining one 7m and one 12m pointing. # phasecenter=3 # uncomment and set to field number for phase # center. Note lack of ''. Use the weblog to # determine which pointing to use. Remember that the # field ids for each pointing will be re-numbered # after your initial split. You can also specify the # phase center using coordinates, e.g., # phasecenter='J2000 19h30m00 -40d00m00' # image parameters. # ---------------- cell='1arcsec' # cell size for imaging. imsize = [128,128] # size of image in pixels. # velocity parameters # ------------------- outframe='bary' # velocity reference frame. See science goals. veltype='radio' # velocity type. # imaging control # ---------------- # The cleaning below is done interactively, so niter and threshold can # be controlled within clean. weighting = 'briggs' robust=0.5 niter=1000 threshold = '0.0mJy' ############################################# # Imaging the Continuuum # Set the ms and continuum image name. contvis = 'calibrated_final_cont.ms' contimagename = 'calibrated_final_cont' # If necessary, run the following commands to get rid of older clean # data. #clearcal(vis=contvis) #delmod(vis=contvis) for ext in ['.flux','.image','.mask','.model','.pbcor','.psf','.residual','.flux.pbcoverage','.pb','.wtsum']: rmtables(contimagename+ext) clean(vis=contvis, imagename=contimagename, field=field, # phasecenter=phasecenter, # uncomment if mosaic. mode='mfs', psfmode='clark', imsize = imsize, cell= cell, weighting = weighting, robust = robust, niter = niter, threshold = threshold, interactive = True, imagermode = imagermode) # If you'd like to redo your clean, but don't want to make a new mask # use the following commands to save your original mask. This is an optional step. #contmaskname = 'cont.mask' ##rmtables(contmaskname) # if you want to delete the old mask #os.system('cp -ir ' + contimagename + '.mask ' + contmaskname) ############################################## # Self-calibration on the continuum [OPTIONAL] contvis = 'calibrated_final_cont.ms' contimagename = 'calibrated_final_cont' refant = 'DV09' # reference antenna. spwmap = [0,0,0] # mapping self-calibration solutions to individual spectral windows. Generally an array of n zeroes, where n is the number of spectral windows in the data sets. # save initial flags in case you don't like the final # self-calibration. The task applycal will flag data that doesn't have # solutions. flagmanager(vis=contvis,mode='save',versionname='before_selfcal',merge='replace') # Get rid of any models that might be hanging around in the image header delmod(vis=contvis,field=field,otf=True) # If you are re-doing your self-cal, uncomment the next line to reset # your corrected data column back to its original state. #clearcal(vis=contvis) # shallow clean on the continuum for ext in ['.flux','.image','.mask','.model','.pbcor','.psf','.residual','.flux.pbcoverage','.pb','.wtsum']: rmtables(contimagename + '_p0'+ ext) clean(vis=contvis, imagename=contimagename + '_p0', field=field, # phasecenter=phasecenter, # uncomment if mosaic. mode='mfs', psfmode='clark', imsize = imsize, cell= cell, weighting = weighting, robust=robust, niter=niter, threshold=threshold, interactive=True, usescratch=True, # needed for 4.3 and 4.4 (and maybe 4.5) imagermode=imagermode) # per scan solution rmtables('pcal1') gaincal(vis=contvis, caltable='pcal1', field=field, gaintype='T', refant=refant, calmode='p', combine='spw', solint='inf', minsnr=3.0, minblperant=6) # Check the solution plotcal(caltable='pcal1', xaxis='time', yaxis='phase', timerange='', iteration='antenna', subplot=421, plotrange=[0,0,-180,180]) # apply the calibration to the data for next round of imaging applycal(vis=contvis, field=field, spwmap=spwmap, gaintable=['pcal1'], gainfield='', calwt=False, flagbackup=False, interp='linearperobs') # clean deeper for ext in ['.flux','.image','.mask','.model','.pbcor','.psf','.residual','.flux.pbcoverage','.pb','.wtsum']: rmtables(contimagename + '_p1'+ ext) clean(vis=contvis, field=field, # phasecenter=phasecenter, # uncomment if mosaic. imagename=contimagename + '_p1', mode='mfs', psfmode='clark', imsize = imsize, cell= cell, weighting = weighting, robust=robust, niter=niter, threshold=threshold, interactive=True, usescratch=True, # needed for 4.3 and 4.4 (and maybe 4.5) imagermode=imagermode) # Note number of iterations performed. # shorter solution rmtables('pcal2') gaincal(vis=contvis, field=field, caltable='pcal2', gaintype='T', refant=refant, calmode='p', combine='spw', solint='30.25s', # solint=30.25s gets you five 12m integrations, while solint=50.5s gets you five 7m integration minsnr=3.0, minblperant=6) # Check the solution plotcal(caltable='pcal2', xaxis='time', yaxis='phase', timerange='', iteration='antenna', subplot=421, plotrange=[0,0,-180,180]) # apply the calibration to the data for next round of imaging applycal(vis=contvis, spwmap=spwmap, field=field, gaintable=['pcal2'], gainfield='', calwt=False, flagbackup=False, interp='linearperobs') # clean deeper for ext in ['.flux','.image','.mask','.model','.pbcor','.psf','.residual','.flux.pbcoverage','.pb','.wtsum']: rmtables(contimagename + '_p2'+ ext) clean(vis=contvis, imagename=contimagename + '_p2', field=field, # phasecenter=phasecenter, # uncomment if mosaic. mode='mfs', psfmode='clark', imsize = imsize, cell= cell, weighting = weighting, robust=robust, niter=niter, threshold=threshold, interactive=True, usescratch=True, # needed for 4.3 and 4.4 (and maybe 4.5) imagermode=imagermode) # shorter solution rmtables('pcal3') gaincal(vis=contvis, field=field, caltable='pcal3', gaintype='T', refant=refant, calmode='p', combine='spw', solint='int', minsnr=3.0, minblperant=6) # Check the solution plotcal(caltable='pcal3', xaxis='time', yaxis='phase', timerange='', iteration='antenna', subplot=421, plotrange=[0,0,-180,180]) # apply the calibration to the data for next round of imaging applycal(vis=contvis, spwmap=spwmap, field=field, gaintable=['pcal3'], gainfield='', calwt=False, flagbackup=False, interp='linearperobs') # do the amplitude self-calibration. for ext in ['.flux','.image','.mask','.model','.pbcor','.psf','.residual','.flux.pbcoverage','.pb','.wtsum']: rmtables(contimagename + '_p3'+ ext) clean(vis=contvis, imagename=contimagename + '_p3', field=field, # phasecenter=phasecenter, # uncomment if mosaic. mode='mfs', psfmode='clark', imsize = imsize, cell= cell, weighting = weighting, robust=robust, niter=niter, threshold=threshold, interactive=True, usescratch=True, # needed for 4.3 and 4.4 (and maybe 4.5) imagermode=imagermode) rmtables('apcal') gaincal(vis=contvis, field=field, caltable='apcal', gaintype='T', refant=refant, calmode='ap', combine='spw', solint='inf', minsnr=3.0, minblperant=6, # uvrange='>50m', # may need to use to exclude extended emission gaintable='pcal3', spwmap=spwmap, solnorm=True) plotcal(caltable='apcal', xaxis='time', yaxis='amp', timerange='', iteration='antenna', subplot=421, plotrange=[0,0,0.2,1.8]) applycal(vis=contvis, spwmap=[spwmap,spwmap], # select which spws to apply the solutions for each table field=field, gaintable=['pcal3','apcal'], gainfield='', calwt=False, flagbackup=False, interp=['linearperobs','linearperobs']) # Make amplitude and phase self-calibrated image. for ext in ['.flux','.image','.mask','.model','.pbcor','.psf','.residual','.flux.pbcoverage','.pb','.wtsum']: rmtables(contimagename + '_ap'+ ext) clean(vis=contvis, imagename=contimagename + '_ap', field=field, # phasecenter=phasecenter, # uncomment if mosaic. mode='mfs', psfmode='clark', imsize = imsize, cell= cell, weighting = weighting, robust=robust, niter=niter, threshold=threshold, interactive=True, usescratch=True, # needed for 4.3 and 4.4 (and maybe 4.5) imagermode=imagermode) # Save results of self-cal in a new ms split(vis=contvis, outputvis=contvis+'.selfcal', datacolumn='corrected') # reset the corrected data column in the ms to the original calibration. # This can also be used to return your ms to it's original # pre-self-cal state if you are unhappy with your self-calibration. #clearcal(vis=contvis) ######################################## # Continuum Subtraction for Line Imaging fitspw = '0,1,2:0~1200;1500~3839,3:0~1200;1500~3839' # *line-free* channels for fitting continuum linespw = '2,3' # line spectral windows. You can subtract the continuum from multiple spectral line windows at once. finalvis='calibrated_final.ms' uvcontsub(vis=finalvis, spw=linespw, # spw to do continuum subtraction on fitspw=fitspw, # regions without lines. excludechans=False, # fit the regions in fitspw combine='spw', solint='int', fitorder=1, want_cont=False) # This value should not be changed. # NOTE: Imaging the continuum produced by uvcontsub with # want_cont=True will lead to extremely poor continuum images because # of bandwidth smearing effects. For imaging the continuum, you should # always create a line-free continuum data set using the process # outlined above. ######################################################### # Apply continuum self-calibration to line data [OPTIONAL] # uncomment one of the following # linevis = finalvis+'.contsub' # if continuum subtracted # linevis = finalvis # if not continuum subtracted # save original flags in case you don't like the self-cal flagmanager(vis=linevis,mode='save',versionname='before_selfcal',merge='replace') spwmap_line = [0] # Mapping self-calibration solution to the individual line spectral windows. applycal(vis=linevis, spwmap=[spwmap_line, spwmap_line], # entering the appropriate spwmap_line value for each spw in the input dataset field=field, gaintable=['pcal3','apcal'], gainfield='', calwt=False, flagbackup=False, interp=['linearperobs','linearperobs']) # Save results of self-cal in a new ms and reset the image name. split(vis=linevis, outputvis=linevis+'.selfcal', datacolumn='corrected') # reset the corrected data column in the ms to the original calibration # This can also be used to return your ms to it's original # pre-self-cal state if you are unhappy with your self-calibration. #clearcal(linevis) linevis=linevis+'.selfcal' ############################################## # Image line emission [REPEAT AS NECESSARY] finalvis = 'calibrated_final.ms' # linevis = finalvis # uncomment if you neither continuum subtracted nor self-calibrated your data. # linevis = finalvis + '.contsub' # uncomment if continuum subtracted # linevis = finalvis + '.contsub.selfcal' # uncommment if both continuum subtracted and self-calibrated # linevis = finalvis + '.selfcal' # uncomment if just self-calibrated (no continuum subtraction) sourcename ='n253' # name of source linename = 'CO10' # name of transition (see science goals in OT for name) lineimagename = sourcename+'_'+linename # name of line image restfreq='115.27120GHz' # Typically the rest frequency of the line of # interest. If the source has a significant # redshift (z>0.2), use the observed sky # frequency (nu_rest/(1+z)) instead of the # rest frequency of the # line. # spw='1' # uncomment and replace with appropriate spw if necessary. start='-100km/s' # start velocity. See science goals for appropriate value. width='2km/s' # velocity width. See science goals. nchan = 100 # number of channels. See science goals for appropriate value. # If necessary, run the following commands to get rid of older clean # data. #clearcal(vis=linevis) #delmod(vis=linevis) for ext in ['.flux','.image','.mask','.model','.pbcor','.psf','.residual','.flux.pbcoverage','.pb','.wtsum']: rmtables(lineimagename + ext) clean(vis=linevis, imagename=lineimagename, field=field, # spw=spw, # phasecenter=phasecenter, # uncomment if mosaic. mode='velocity', start=start, width=width, nchan=nchan, outframe=outframe, veltype=veltype, restfreq=restfreq, niter=niter, threshold=threshold, interactive=True, cell=cell, imsize=imsize, weighting=weighting, robust=robust, imagermode=imagermode) # If you'd like to redo your clean, but don't want to make a new mask # use the following commands to save your original mask. This is an # optional step. # linemaskname = 'line.mask' ## rmtables(linemaskname) # uncomment if you want to overwrite the mask. # os.system('cp -ir ' + lineimagename + '.mask ' + linemaskname) ############################################## # Apply a primary beam correction import glob myimages = glob.glob("*.image") rmtables('*.pbcor') for image in myimages: pbimage = image.rsplit('.',1)[0]+'.flux' outfile = image.rsplit('.',1)[0]+'.pbcor' impbcor(imagename=image, pbimage=pbimage, outfile = outfile) ############################################## # Export the images import glob myimages = glob.glob("*.pbcor") for image in myimages: exportfits(imagename=image, fitsimage=image+'.fits',overwrite=True) myimages = glob.glob("*.flux") for image in myimages: exportfits(imagename=image, fitsimage=image+'.fits',overwrite=True) ############################################## # Create Diagnostic PNGs os.system("rm -rf *.png") mycontimages = glob.glob("calibrated*.image") for cimage in mycontimages: max=imstat(cimage)['max'][0] min=-0.1*max outimage = cimage+'.png' os.system('rm -rf '+outimage) imview(raster={'file':cimage,'range':[min,max]},out=outimage) # this will have to be run for each sourcename sourcename='' # insert source here, if it isn't already set mylineimages = glob.glob(sourcename+"*.image") for limage in mylineimages: rms=imstat(limage,chans='1')['rms'][0] mom8=limage+'.mom8' os.system("rm -rf "+mom8) immoments(limage,moments=[8],outfile=mom8) max=imstat(mom8)['max'][0] min=-0.1*max os.system("rm "+mom8+".png") imview(raster={'file':mom8,'range':[min,max]},out=mom8+'.png') ############################################## # Analysis # For examples of how to get started analyzing your data, see # https://casaguides.nrao.edu/index.php/TWHydraBand7_Imaging_4.3 #