Interactive mask editing - eakadams/ihow-hi GitHub Wiki
This page provides information on how to interactively edit a mask to remove noise peaks that enter with threshold clips. CASA provides some functionality for this, but this varies from version to version. In addition, there are now many tools that can help build masks, replacing the manual procedure with algorithms, but the manual procedure is still helpful to understand what the tools should be doing and why.
Interactive clean
This worked for the instructor with CASA 4.6.1, and is how the final clean mask for the tutorial was prepared. Note this calls im.drawmask() under the hood (see below) which is depreciated, so this is a solution that may not always work.
This picks up from the end of the "Final mask" section in the Masking and cleaning tutorial.
Now open up a tool to interactively select regions of real emission. We will do this by specifying our clipped mask to tclean but specifying interactive=True so that we can edit the mask (erasing noise components) before running the clean.
tclean(vis='sexb.ms.contsub',imsize=300,cell='10arcsec',specmode='cube',restfreq='1420.405752MHz',imagename='clean',
weighting='briggs',robust=0.2,niter=100000,outframe='bary',spw='0:80~155',threshold='3.0mJy',mask='tmp_mask4.im',interactive=True)
This launches a viewer where we can interact with the mask because we specified interactive=True:

You will want to select regions using the right mouse button to erase from the clean mask. For the noise channels, you can draw a box that is the whole region. These boxes are initially green and then activated by double-clicking inside. Make sure to select Erase as the option for the action. We have to page through channel by channel and do something for each channel. This is tedious and one motivation for having imaged just a subset of channels, with only a few for noise around source. You can change the display parameters to make things easier (e.g., change contour colors or image display). Note that the displayed image is the dirty image, with no cleaning.
An example of how you will select emission to erase:

Once you hit channels with emission (starting in 7) be careful as you now want to erase the noise peaks but not the main mask that contains the source. You can page back and forth in channels to check for spectrally contiguous emission before deciding to remove something.
An example of a channel where only some mask areas should be erased:

The last channel with emission is 147; then evertyhing needs to be erased after that.
Once you have paged through all the channels, play through a couple of times to make sure you have erased all parts of the mask that you should and that only real emission parts are left (but note that an uncleaned full-res image is dispalyed, so the mask and image don't always match up)
Once you're happy, press the blue arrow to continue the cleaning uninterrupted, using the mask you have created/edited.
You may also note that as you work through the cube that there are many channels which have masks with significant sidelobes include as they were not cleaned in the earlier iterations. The iterations could have of course continued before doing the clip, and you may prefer to do that in some cases. In this case, the iterations were kept minimal in order to move the tutorial along.
im.drawmask
This is the approach the instructor used in the past, and it worked in older versions of CASA. This tool is no longer supported, so will not work in the future.
These are notes from previous reductions where I have used this approach:
im.open()
#Ignore the error message - doesn't matter.
im.drawmask(image='tmp_mask5.im',mask='selected_mask5.im')
#select emission and then exit by clicking blue arrow
#Do this after done
im.done()
Combine the two masks
immath(imagename=['tmp_mask5.im','selected_mask5.im'],mode='evalexpr',expr='iif(IM0>0.5 && IM1>0.5,1,0)',outfile='final_clean_mask.im')
Note a possible advantage is that in this approach you select emission to keep in your mask (highlight the clipped regions you want to keep). So that can be less selection.
deconvolve task
The deconvolve was added in CASA 6.5.2, so you should use CASA 6.5.2 or higher (current version is CASA 6.5.6).
deconvolve(imagename='qqq',niter=0,interactive=True) Here, imagename is the same prefix that you would use in tclean. The images qqq.residual and qqq.psf are expected to be on disk already.
This opens the interactive viewer, and you can draw the mask. If you don't want any iterations, niter=0 will let you just exist after drawing the mask, and then qqq.mask exists on disk.
Automasking
CASA also now offers algorithms to help build a mask. You can find more information about masking in CASA in the docs.