Image Continuum CASA 6.1.1

From CASA Guides
Jump to navigationJump to search

This guide should be used after completing Imaging_Prep. You should have created calibrated_final.ms prior to proceeding.

Check CASA version

It is highly recommended that the same CASA version used for calibration is used for imaging. This template is not recommended to be used in a CASA version <4.4. If CASA closes, change the version of CASA you’re using by either downloading the proper version from Obtaining CASA or initializing the suggested version.

# in CASA
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")

Image Parameters

Before imaging, you should set a few key parameters that you will use throughout the rest of the script.

Set the field id for the science target you are interested in imaging. Check the ALMA Observing Tool (OT) as well as the listobs file generated to find this information. If you are imaging a mosaic then you will have to select all the fields as field = ‘3~25’. You can also set the field variable to the name of the source, as long as it matches the name in the listobs file. Do not leave the field parameter blank.

# in CASA
# update for your ms
field='0'

Uncomment the imager mode that is relevant to your dataset. Set the phase center by field id or coordinates if you are imaging a mosaic. Check the spatial setup in the weblog (for pipeline calibrations) or qa2 report (for manual calibrations) to find the phasecenter. You should choose the central field for the phase center in order to get the best results.

If you are unsure which field to use for the phase center after looking at the weblog then you may use the following Analysis Utilities command:

au.pickCellSize(calibrated_final.ms',imsize=True).

This will return a four element array with that contains the calculated cell size, the X axis number of pixels, the Y axis number of pixels, and the field number that is most centered in the mosaic. You may use this as the phase center field id in your script.

Another method of finding the central field number is using the task au.plotmosaic(‘calibrated_final.ms’) within CASA. This method will produce a plot of all fields with their corresponding field numbers plotted on the sky. You can also set the phasecenter with the coordinates at the center of this plot.

# in CASA
# 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'

Next you will determine the cell size. Find the approximate beam size of your observation using the following equation

206265.0/(longest baseline in wavelengths) ~ Beam Size(arcseconds) <figure id="Calibrated_final_AmpVsUVWave.png">

Amplitude versus UV wave for the calibrated_final.ms showing the maximum uv wave point for determining cell size.

</figure>

To determine the longest baseline in wavelength use plotms with xaxis=’uvwave’ and yaxis=’amp’ on your ms file. Figure 1 shows an example of this plot. It is recommended you fit 5 to 8 cells per beam when imaging. Therefore you can find the cell size by dividing the Beam Size by at least 5, however it is always better to put more pixels across your beam than too few.

Beam Size/5 ~ Cell Size(arc seconds)

The next step is to determine the image size in pixels. There are two methods of doing this depending on if you are imaging one field or a mosaic.

If this is a single field the image size can usually be approximated to be the same size as the primary beam of the telescope. The ALMA 12m primary beam in arcsec scales as 6300 / nu[GHz] and the ALMA 7m primary beam in arcsec scales as 10608 / nu[GHz], where nu[GHz] is the sky frequency. However, if there is significant point source and/or extended emission beyond the edges of your initial images, you should increase the imsize to incorporate more emission.

For mosaics, you can get the imsize from the spatial tab of the OT. The parameters "p length" and "q length" specify the dimensions of the mosaic. If you're imaging a mosaic, pad the imsize substantially to avoid artifacts. To know if you have padded enough you should, in your images, be able to see the edges of the outside fields being cut off.

Note that the imsize parameter is in PIXELS, not arcsec, so you will need to divide the image size in arcsec by the pixel size to determine a value for imsize.

If you would like to check any of these calculations you may use the following command, au.pickCellSize('calibrated_final.ms', imsize=True), in CASA. This will return a four element array of the calculated cell size, the x axis imsize, the y axis imsize, and the central field id number.

# in CASA
cell='1arcsec' # cell size for imaging.
imsize = [128,128] # size of image in pixels.

When imaging you will need to set two specific velocity parameters called outframe and veltype. Outframe is the coordinate system used for the observation and can be found in the OT under field setup. A list of acceptable outframes that can be used in CASA can be found at https://help.almascience.org/index.php?/Knowledgebase/Article/View/86/0/what-are-the-frequency-reference-frames-in-casa. Note: heliocentric(hel) is deprecated in CASA. Use barycentric(bary) in this case.

You will also have to set the veltype for the clean command. This variable has only two options available, radio and optical. It is standard to leave this set to ‘radio’ in all projects regardless of the velocity frame used in the project.

# in CASA
outframe='bary' # velocity reference frame. See science goals.
veltype='radio' # velocity type.

The last four parameters that must be set are associated with how clean will weight and clean the data. Weighting determines how much influence data points in the UV plane have on the final image. There are several weighting schemes that can be used such as briggs, natural and uniform weighting.

Natural: Natural weighting uses the same weights as that of the data weights when collected. This weighting scheme is the default in clean and gives low rms noise to the image but a poor angular resolution.

Uniform: Uniform weighting will give all points in the UV plane the same influence regardless of their weight. Compared to Natural weighting, it gives more influence to lower weighted cells . Because of this, sidelobes in the image will be reduced but the rms noise will increase. This type of weighting is best used for point source objects due to the low resolution but at the cost of high rms noise in the image.

Briggs and the Robust Parameter: This is the most common weighting used and gives a balance between the low rms of natural weighting while also giving the excellent angular resolution of uniform weighting. Briggs weighting will vary the weighting given to UV cells with the robust parameter. Setting this to 2 gives Natural weighting, -2 Uniform weighting, and a number in between a variance of the two. Brigg's Thesis

Currently, robust is set to 0.5 which gives the best rms with the smallest angular resolution to the image. You may find, after imaging, that you have to decrease the rms noise or angular resolution based on the desired performance requested in the proposal. Playing with the robust parameter can affect your final rms noise in the image and also the angular resolution.

The niter and threshold parameters affect when clean will stop the cleaning process. Niter is the number of major clean cycles allowed and once this number has reached zero interactive cleaning will finish. Once a certain flux level, set by threshold, has been reached, clean will stop. This is by default set at 0.0mJy but may be increased to prevent cleaning too deeply into the thermal noise.

There are several methods to determine what to set threshold to if you desire to change it. One method is to make a clean mask by setting niter equal to zero then inspect the image produced using viewer. Select a box around an area of no emission to determine the rms noise of the image. This should be roughly what threshold should be set at. Follow the instructions located in the “Image the Spectral Line Data” section of EVLA Spectral Line Imaging Analysis IRC+10216. If you plan on cleaning non-interactively then you must set threshold to a value other than 0.

# in CASA
weighting = 'briggs'
robust=0.5
niter=1000
threshold = '0.0mJy'

For more information on the various options available in clean, refer to David Wilner’s Imaging and Deconvolution presentation from the 2016 Synthesis Imaging Workshop. Use this table to find specific slides for different parameters you will set below.

Imaging Parameters
Pixel and Image Size Slides 40-41
Weighting Slides 42-48, 60-61
Deconvolution Algorithms Slides 50-53, 62

Create an Averaged Continuum MS

Create a separate, spectrally averaged continuum measurement set in order to increase the efficiency of clean/tclean. The process below identifies line free channels and creates this measurement set. The first step is to identify spectral lines in all spectral windows(spw) you will use to make the continuum image. Include all spectral windows with greater than 250 MHz bandwidth in order to maximize the continuum bandwidth. If you find spectral line emission you will need to flag these channels from the spw before imaging.

There are currently two methods to determine which channels have spectral line emission. The first is to simply use CASA to plot Channel vs Amp of each spw and note the channels with emission. You may wish to change the averaging and uvrange to identify extended emission:

# in CASA
plotms(vis=finalvis, xaxis='channel', yaxis='amplitude',
   	ydatacolumn='data',
   	avgtime='1e8', avgscan=True, avgchannel='1',
   	iteraxis='spw' )

The second, and more accurate, method is to use clean to make a quick dirty image cube of channels with niter set to zero and mode=’channel’. Inspecting this channel cube for line emission gives a better defined channel width to flag. The command below gives the basic command to create dirty image cubes. This should be repeated for each spw and science field.

#In CASA 


testimagename=testImage
field=[0] #list all target fields
spw=[0,1,2,3] #list all target spw’s

for i in field:
      for j in spw:  
            clean(vis=finalvis,
            	  imagename=testimagename+Field_+str(i)+_spw_+str(j), 
            	  field=str(i),
      	          spw=str(j),
	          #      phasecenter=phasecenter, # uncomment if mosaic.      
      	          mode='channel',
      	          outframe=outframe, 
      	          niter=0,
      	          interactive=True,
      	          cell=cell,
      	          imsize=imsize, 
      	          weighting=weighting, 
      	          robust=robust,
      	          imagermode=imagermode)

Once you have determined which spws have line emission and which ones you would like to use to form the continuum from your measurements, set the continuum spws to use:

# in CASA
# Set spws to be used to form continuum
contspws = '0,1,2,3'

The first step to flagging the spectral line channels in your data is to use the flagmanager() task to save the state of the data before any flagging is applied. You will need to revert back to the non spectral line flagged dataset when line imaging is done later on. In addition, if you accidently flag any data you can easily correct this using the *.before_cont_flags file.

# in CASA
flagmanager(vis=finalvis,mode='save',
versionname='before_cont_flags')

Initialize the weights in the ms using intiweights().

# in CASA
initweights(vis=finalvis,wtmode='weight',dowtsp=True)

The exact spectral window and channel ranges to flag in the flagchannels variable needs to be specified. For example, if spws 2&3 have a line between channels 1201 and 2199 and spectral windows 0 and 1 are line-free the command would be:

# in CASA
flagchannels='2:1201~2199,3:1201~2199' # modify the channel range for your dataset

Next, use the task flagdata() to apply these flags. After this is done it is good to check that the flags were applied correctly by using plotms to inspect the flagged ms.

# in CASA
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')

Before imaging the continuum there are several things that can be done to speed up cleaning of the final continuum image. One of these is to spectrally average several channels together within a single continuum window thus reducing the total number of channels while maintaining the same bandwidth.

Refer to [need link to this derivation How to Calculate Bandwidth Smearing in Radio Synthesis Measurements] to minimize bandwidth smearing effects.

125MHz/ChanWid(MHz) = number of channels to average for bands 3,4,5,6 250MHz/ChanWid(MHz) = number of channels to average for band 7

For example, if you have 128 channels, a Channel width of 15.625 MHz in band 6 the way to find the number of channels to average together is

125 MHz / 15.625 MHz = 8 channels

It is always a good check to make sure the number of channels you are averaging together is an integer multiple of the total number of channels original. This will tell you how many channels will remain in the ms. Using the example above averaging every 8 channels of 128 channels leaves 16 final channels each with a channel width of 125MHz.

Split2() averages the number of channels you specify within the width command and will save a new ms file called calibrated_final_cont.ms.

# in CASA
contvis='calibrated_final_cont.ms'
rmtables(contvis)
os.system('rm -rf ' + contvis + '.flagversions')
split2(vis=finalvis,
 spw=contspws,
 outputvis=contvis,
 width=[8,8,8,8], # 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')

Now you should check the weights of the new continuum measurement set. The ratio of the weights value between Time Domain Mode (TDM) and Frequency Domain Mode (FDM) windows should be approximately equal to the ratio of the channel weights.

# in CASA
# update the antenna and field parameters for your dataset
plotms(vis=contvis, yaxis='wtsp',xaxis='freq',spw='',antenna='DA42',field='0')

Use flagmanager() to restore the ms file to its original unflagged state.

# in CASA
# If you flagged any line channels, restore the previous flags
flagmanager(vis=finalvis,mode='restore',
        	versionname='before_cont_flags')

It is a good practice to inspect the continuum after this in order to confirm it is alright for future cleaning.

# in CASA
plotms(vis=contvis,xaxis='uvdist',yaxis='amp',coloraxis='spw')

Imaging the Continuum

Now that you have set all of the imaging parameters you will need in clean you can proceed to imaging the continuum. The First Look at Imaging CASAGuide gives an introduction to cleaning and imaging.

# in CASA
contvis = 'calibrated_final_cont.ms'         
contimagename = 'calibrated_final_cont'

If you have already run clean on the ms and would like to start over, be sure to use the following commands before cleaning the continuum.

# in CASA
# Clearcal clears the calibrated column of the ms.
# clearcal(contvis)
# Delmod removes any model column put in by clean.
# delmod(contvis)

clean creates a variety of files after it completes. If these files exist in the directory, clean will begin working on the files that already exist. The following command will delete all of these files. You may also change the cont_image_name parameter if you would like to keep the files you created previously.

# in CASA
for ext in ['.flux','.image','.mask','.model','.pbcor','.psf','.residual','.flux.pbcoverage','.pb','.wtsum']:
	rmtables(contimagename+ext)

If you are imaging a mosaic, the phasecenter parameter should be set. Refer to the imaging parameters section of this guide to see how to find this for your project.

Type “help clean()” in CASA if you would like to explore the possible parameters of clean. Mode=’mfs’ sets the spectral gridding type to multi-frequency synthesis and creates a continuum image. Leave the psfmode to use during minor cycles at the default ‘clark’ .

Clean interactively as the threshold is set at 0 mJy. The mask parameter may also be added if you have an existing file.

# in CASA
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)

The image to the left shows the clean GUI that will appear. If no source is apparent, no cleaning should be done. Press the red “X” to complete the task. If a source is apparent, create a mask around it. Once a mask is created, the green arrow will be illuminated and you can begin the first round of cleaning.

The logger window gives you vital information on the progression of the clean. Once the cycle is complete, the residuals will appear in the GUI. You should continue to iterate until the region inside the mask matches the noise outside the mask. You may need multiple cycles depending on the complexity of the source.

The red “X” is the only way clean can be safely exited. Otherwise, your measurement set will likely be corrupted.