Python Basics for CASA

A few Python details every CASA user should know

Python Basics for CASA

 Within CASA, you use Python to interact with the system. This does not mean an extensive Python course is necessary - basic interaction with the system (assigning parameters, running tasks) is straightforward. At the same time, the full potential of Python is at the more experienced user’s disposal. Some further details about Python, IPython, and the interaction between Python and CASA can be found in Section "Python and CASA".

The following are some examples of helpful hints and tricks on making Python work for you in CASA.


Python variables are set using the <parameter> = <value> syntax. Python assigns the type dynamically as you set the value, and thus you can easily give it a nonsensical value, e.g.

vis = ''
vis = 1

The CASA parameter system will check types when you run a task or tool, or more helpfully when you set inputs using inp (see below). CASA will check and protect the assignments of the global parameters in its namespace.

Note that Python variable names are case-sensitive:

CASA <109>: Foo = 'bar'
CASA <110>: foo = 'Bar'
CASA <111>: foo
  Out[111]: 'Bar'
CASA <112>: Foo
  Out[112]: 'bar'

so be careful.

Also note that misspelling a variable assignment will not be noticed (as long as it is a valid Python variable name) by the interface. For example, if you wish to set correlation=’RR’ but instead type correllation=’RR’ you will find correlation unset and a new correllation variable set. Command completion (see "Getting Help in CASA") should help you avoid this. 

Lists and Ranges

Sometimes, you need to give a task a list of indices. If these are consecutive, you can use the Python range function to generate this list:

CASA <1>: iflist=range(4,8)
CASA <2>: print iflist
[4, 5, 6, 7]
CASA <3>: iflist=range(4)
CASA <4>: print iflist
[0, 1, 2, 3]

See "Python and CASA"  for more information.


 As in C, Python indices are 0-based. For example, the first element in a list antlist would be antlist[0]:

CASA <113>: antlist=range(5)
CASA <114>: antlist
  Out[114]: [0, 1, 2, 3, 4]
CASA <115>: antlist[0]
  Out[115]: 0
CASA <116>: antlist[4]
  Out[116]: 4

CASA also uses 0-based indexing internally for elements in the MeasurementSet (MS – the basic construct that contains visibility and/or single dish data). Thus, we will often talk about Field or Antenna “ID”s which will be indexed starting at 0. For example, the first field in an MS would have FIELD_ID==0 in the MSselect syntax, and can be indexed as field=’0’ in most tasks, as well as by name, e.g; field=’0137+331’. You will see these indices in the MS summary from the task listobs.


Python pays attention to the indentation of lines, as it uses indentation to determine the level of nesting in loops.

Alert: Be careful when cutting and pasting: if you get the wrong indentation, then unpredictable things can happen (usually it just gives an error).

See Section "Python and CASA" for more information.

System & shell access

If you want to access system commands from a script, use the os.system command ("Python and CASA").

In interactive mode, any input line beginning with a ! character is passed verbatim to the underlying operating system. Also, several common commands (ls, pwd, less) may be executed with or without the ’!’, although the cp command must use ’!’ and cd must be executed without the ’!’. For example:

CASA <5>: !rm -r

Note that if you want to access a Unix environment variable, you will need to prefix with a double $$ instead of a single $ — for example, to print the value of the $PAGER variable, you would use

  CASA <6>: !echo $$PAGER

See "Python and CASA" for more information.

Executing Python scripts

You can execute Python scripts (ASCII text files containing Python or CASA commands) using the execfile command. For example, to execute the script contained in the file (in the current directory), you would type

CASA <7>: execfile('')


CASA <8>: execfile ''

which will invoke the IPython auto-parenthesis feature.

NOTE: in some cases, you can use the IPython run command instead, e.g.

CASA <9>: run

In this case, you do not need the quotes around the filename. This is most useful for re-initializing the task parameters, e.g.

CASA <10>: run clean.last

(see "CASA Tasks").

See Section "Python and CASA" for more information.

A script can also be executed from the command line without starting CASA first. To do so, use the -c CASA startup option:

casa -c