Monday, March 8, 2010

Select fits files based on header keyword

The task of selecting a list of fits files based on some header keyword value is much easier to do with the IRAF task hselect. Although this example is very useful for understanding how to use pyfits and the sys module for passing parameters via command line (like bash our Perl's @ARGV).
#!/usr/bin/python

import sys
import pyfits
import re

for file in sys.argv[1:]: #exclude the first element of sys.argv
hdulist = pyfits.open(file) #open fits
key = hdulist[1].header['OBJECT'] #reads a keyword from the fits header
if (re.search('standard',key)): #search the keyword value for some string
print file

To execute just run on the command line:

# python script.py file1 file2 ...

or

# chmod +x script.py
# ./script.py file1 file2 ...

All bash's wildcards (*, ?, etc) are accepted.

No comments:

Post a Comment

 
Locations of visitors to this page