NRAO Home > CASA > CASA Cookbook and User Reference Manual

B.6.2 Loops

The for loop

   for iter in seq:  
      <statements>

iterates over elements of a sequence seq, assigning each in turn to iter. The sequence is usually a list of values.

For example,

splitms = ’polcal_20080224.cband.all.split.ms’  
srclist = [’0137+331’,’2136+006’,’2202+422’,’2253+161’,’0319+415’,’0359+509’]  
spwlist = [’0’,’1’]  
 
for src in srclist:  
 
    for spwid in spwlist:  
 
        imname = splitms + ’.’ + src + ’.’ + spwid + ’.clean’  
        clean(vis=splitms,field=src,spw=spwid,imagename=imname,  
            stokes=’IQUV’,psfmode=’hogbom’,imagermode=’csclean’,  
            imsize=[288,288],cell=[0.4,0.4],niter=1000,  
            threshold=1.3,mask=[134,134,154,154])  
 
    # Done with  spw  
 
# Done with sources

As usual, blocks are closed by blank lines of the previous indentation level.

You can use the range (§ B.4) Python function to generate a numerical loop:

vis = ’polcal_20080224.cband.all.ms’  
for i in range(0,6):  
   fld = str(i)  
   plotxy(vis,field=fld,xaxis=’uvdist’,yaxis=’amp’)  
 
# Done with fields [0, 1, 2, 3, 4, 5]

There is also a while loop construct

   while <expression>:  
      <statements>

which executes the statement block while the <expression> is True. The while loop can also take an else block.

For example,

# Do an explicit set of clean iterations down to a limit  
prevrms = 1.e10  
while rms > 0.001 :  
    clean(vis=splitms,field=src,spw=spwid,imagename=imname,  
          stokes=’IQUV’,psfmode=’hogbom’,imagermode=’csclean’,  
          imsize=[288,288],cell=[0.4,0.4],niter=200,  
          threshold=1.3,mask=[134,134,154,154])  
 
    offstat=imstat(imname+’.residual’,box=’224,224,284,284’)  
    rms=offstat[’sigma’][0]  
    if rms > prevrms:  
         break                # the rms has increased, stop  
 
    prevrms = rms  
 
# Clean until the off-source rms residual, reaches 0.001 Jy

Note that you can exit a loop using the break statement, as we have here when the rms increases.


More information about CASA may be found at the CASA web page

Copyright © 2010 Associated Universities Inc., Washington, D.C.

This code is available under the terms of the GNU General Public Lincense


Home | Contact Us | Directories | Site Map | Help | Privacy Policy | Search