Skip to content

Instantly share code, notes, and snippets.



Created Nov 25, 2011
What would you like to do?
Python 2.7+ script for printing a FITS header. Accepts multiple files and, optionally, keywords. This is a wrapper around pyfits.getheader to make it accessible from the command line.
#!/usr/bin/env python
Print a FITS header or keyword. Type imhead -h for help.
Requires pyfits and Python 2.7+.
Print a header:
imhead jb1f98q1q_raw.fits
Print an extension header:
imhead -e 1 jb1f98q1q_raw.fits
Print only specified keywords:
imhead jb1f98q1q_raw.fits -k expstart -k expend
import argparse
import pyfits
def print_header(fits, ext=0, keys=None):
head = pyfits.getheader(fits, ext=ext).ascard
if not keys:
print head
for key in keys:
print head[key]
def parse_args():
parser = argparse.ArgumentParser(description=
'Print a FITS header or keyword.')
parser.add_argument('fits_files', nargs='+', type=str,
help='Name of fits files.')
parser.add_argument('-e', '--ext', type=int, default=0,
help='Extension number. Defaults to 0.')
parser.add_argument('-k', '--key', type=str, action='append',
help='Keyword to print. May occur multiple times.')
return parser.parse_args()
def main():
args = parse_args()
for fits_file in args.fits_files:
print_header(fits_file, args.ext, args.key)
if __name__ == '__main__':
raise SystemExit(main())

This comment has been minimized.

Copy link
Owner Author

@jiffyclub jiffyclub commented Nov 28, 2011

A Python 2.3+ version of this that uses optparse is also available:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.