Creating lists of files for task input: using glob

From CASA Guides

Hints, Tips, and Tricks

CARMA Hints, Tips, & Tricks

If you need to import a number of files, you can use a python module called 'glob' to search for and assemble a list for you. The following lines of code will produce an array of filenames that you can give CASA as an input for the "importuvfits" command:

# in CASA
# this next line imports the module glob, but names it 'filesearch' to help remind you what it does
from glob import glob as filesearch
import os


inlist = sorted(filesearch(fitsdir+'*.fits'))

In the same python script, you can now use the following "for" loop to read each of the individual FITS files into an MS of the same name (e.g., for a FITS file named "c0xxx.4.fits", produce an MS called "" .) The 'infile' definition strips off the directory path,leaving only the FITS filename. The 'outfile' definition strips off '.fits' from each of the FITS files, and replaces it with '.ms' to create a list of output MS filenames.

Finally, the call to importuvfits used this pair of filenames to read the data from each FITS file into the corresponding MS:

# in CASA
for item in inlist:
    importuvfits(fitsfile=item, vis=msdir+outfile)