Python imhead and hedit

Working at STSCI I frequently deal with FITS files and while working on the new CALACS pipeline I often needed to see and change header values to run CALACS under different regimes. This can be done with IRAF tasks imhead and hedit but I try to use IRAF as little as possible.

PyFITS has convenience functions for working with FITS headers but as of version 3.0.7 it doesn’t come with any scripts that make them accessible from the command line. I expect that soon PyFITS and/or AstroPy will have these scripts, but in the meantime here are the ones I use.


This wraps pyfits.getheader to print an image header to the terminal. There are options to specify an extension header or only certain keys to print. See the code here:


Print an entire header:

> imhead jb1f98q1q_raw.fits

Print an extension header:

> imhead -e 1 jb1f98q1q_raw.fits

Print only certain keywords (case insensitive):

> imhead jb1f98q1q_raw.fits -k expstart -k expend

My most common usage is to print the entire header but pipe the output to grep so I don’t have to remember the exact keyword names:

> imhead jb1f98q1q_flt.fits | grep OBS
DATE-OBS= '2009-12-03' / UT date of start of observation (yyyy-mm-dd) 
TIME-OBS= '02:25:29' / UT time of start of observation (hh:mm:ss) 
OBSTYPE = 'IMAGING ' / observation type - imaging or spectroscopic 
OBSMODE = 'ACCUM ' / operating mode


This wraps pyfits.setval to enable modification of headers from the command line. It supports modification of multiple files with one call, changing extension headers, and specifying the keyword type. See the code here:


Modify a single file:

> hedit jb1f98q1q_raw.fits EXPSTART 58000 -i
jb1f98q1q_raw.fits[0]: EXPSTART -> 58000

Modify multiple files:

> hedit *.fits FLATCORR PERFORM

Modify an extension header:

> hedit jb1f98q1q_raw.fits SOMEKEY SOMEVALUE --ext 1


To use these yourself copy the code to a local file wherever you keep scripts and set them to executable. The files I linked above require Python 2.7 because they use the argparse module, but if you are using an older version of Python I’ve also written versions of these using optparse. Those versions are at (imhead) and (hedit). Enjoy!

2 thoughts on “Python imhead and hedit

  1. I’m having trouble installing pyfits on my MacBook Air. I have installed the anaconda python distribution as you advise, but for some reason it does not include pyfits. I have used both easy_install and pip to try to install pyfits, but it keeps returning with an error, saying that it cannot locate a working compiler.

    I have installed Xcode.

    Thanks for you advise,

    • There is probably one thing you’re missing and that’s the Xcode command line tools. You can have Xcode install those by going into Preferences -> Downloads.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s