Created
November 25, 2011 21:54
-
-
Save jiffyclub/1394499 to your computer and use it in GitHub Desktop.
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
Print a FITS header or keyword. Type imhead -h for help. | |
Requires pyfits and Python 2.7+. | |
Author | |
------ | |
@jiffyclub | |
git.io/jiffyclub | |
Examples | |
-------- | |
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 | |
else: | |
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()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A Python 2.3+ version of this that uses
optparse
is also available: https://gist.github.com/1401692.