CASA Region Format Examples

From CASA Guides
Jump to: navigation, search

Regions are used in CASA to identify particular positions or areas in images, and particular volumes in image cubes. Common uses of regions include defining clean boxes, performing image analysis, and cutting out subimages. Additionally, the region format includes annotations, allowing line art and text to be drawn on images.

In CASA, regions are simple ASCII files that use a flexible combination of shapes and coordinate systems to define the desired positions, areas, volumes, etc. A CASA Region Text File (CRTF) can either be written with a text editor, or created interactively with the CASA viewer.

The CRTF can be given directly to CASA tasks, such as clean, using the mask parameter:

inp clean
mask = 'myCRTF.txt'

Note that with clean, <imagename>.mask will also be used as a mask if it exists, in addition to anything input with the mask parameter. Clean can also accept multiple regions, and will take the union of these.

inp clean
mask = ['myCRTF_1.txt', 'myCRTF_2.txt', 'myCRTF_3.txt']

Clean is the only task or tool that accepts a CRTF as the mask parameter. Clean is also the only task or tool that can accept a list of masks -- clean even accepts a mix of different formats in the same list. This makes the clean task extraordinarily flexible in regard to regions, but also very unique; all other tasks and tools will only accept a single CRTF through their region parameter.

Fortunately, multiple CRTFs can be combined into a single CRTF by simply concatenating the text files, so this is not an issue with this region format. Here is one way to concatenate text files in a CASA script. Text files 1 and 2 will be combined and a new file, 3 will be written:

os.system( 'cat ' + 'myCRTF_1.txt' + ' ' + 'myCRTF_2.txt' + ' > ' + 'myCRTF_3.txt' )

Now for some examples of the CRTF used with other tasks (remember to use the region parameter):

We can use immath to cut out and save a subimage defined by a region:

imagename = ['myImage.image']
mode = 'evalexpr'
expr = 'IM0'
region = 'myCRTF.txt'

We can fit a 2-dimensional Gaussian to the pixels within a region, using the task imfit:

imagename = 'myImage.image'
region = 'myCRTF.txt'

To calculate image statistics using the pixel values within your region, we can use the imstat task. If we call it like a function, it will return a python dictionary containing various calculated values. Each value is indexed by a keyword. We list the available keywords and then print the one:

mystats = imstat( imagename = 'myImage.image', region = 'myCRTF.txt')
print mystats.keys()
print mystats['rms']

To extract pixel values within a region, we can use imval. It will return a dictionary whose keyword 'data' will contain a numpy array with the pixel values.

myVal = imval(imagename='myImage.image', region='myCRTF.txt')
myData = myVal['data']

Other tasks that may accept a CRTF include:

  • imcontsub
  • widefield
  • mosaic
  • imcollapse
  • immoments
  • imsmooth
  • specfit

A CRTF can also be given to methods in the CASA image analysis (ia) toolkit, but not directly. This is because the lattice region format accepted by tools needs to be a Regionmanager Object Python Dictionary (ROPD).

The ROPD format combines the information contained in the CRTF with information about the coordinate system of a particular image so that they can be properly used together. While tasks in fact do this behind the scenes, you will need to create the ROPD yourself if you want to pass a region to a toolkit method.

Fortunately, it is easy to create the ROPD from the CRTF, plus the image or image cube of interest. First, one needs to open the image with the image analysis toolkit:

# In CASA 'myImage.image' )

Now, make a ROPD from your CRTF using a method from the regions toolkit:

myRGN = rg.fromtextfile(filename='myCRTF.txt', shape=ia.shape(), csys=ia.coordsys().torecord())

You can now pass this ROPD to different image analysis tools. For example, to calculate various image statistics using only the pixels within your region:

mystats = ia.statistics(region = myRGN)

You can retrieve the image pixel values within your region as a numpy array:

myArray = ia.getregion(region = myRGN)

Or, to cut out and save a subimage containing only the pixels within your region:

ia.subimage(region = myRGN, outfile='subimage.image')

Be sure to close the image when you are done:


Also, here is one method, using the python pickle module, to save a (binary) hardcopy of your ROPD to reuse in another CASA session:

import pickle
out1 ='myRGN.pkl', 'wb')
pickle.dump(myRGN, out1)

Which can be loaded back into CASA with:

import pickle
in1 ='myRGN.pkl', 'rb')
myRGN = pickle.load(in1)