FITS files in Python

[Pyfits] is a really useful python module that allows you to read in and write out [FITS] files. Since most astronomical data comes in FITS file format its darn important that you can read them in. Here are some basic instructions on how todo this:
Firstly ensure you have the most useful pre-requisites ([Pyfits], [numpy ], [scipy]) and import them:
import pyfits, numpy, scipy
Now read in the file:
input_file = “A.fits”
hdulist = pyfits.open(input_file)
Get the data:
img_data = hdulist[0].data
Get the header:
img_header = hdulist[0].header
To read a specific item from the header:
h3 = float(hdulist[0].header[‘CDELT3’])
To update a specific header item:
hdulist[0].header.update(‘BUNIT’,’PI’)
To output the FITS file, overwritting if necessary and converting to 32bit floats:
pyfits.writeto(“output.fits”,float32(img_data),img_header,clobber=True)

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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

%d bloggers like this: