Fork me on GitHub

fitsutils by nbarbey

A set of small command-line tools for FITS files

fitstables

Why fitstables

fitstables aims at rewriting the IDL mrdfits and mwrfits functions. It can read FITS files written by the IDL mwrfits function as well as writing such files. However, for now, only the most basic usages is implemented.

A simple exemple

Get a simple fits file on the internet containing a binary table.
wget http://fits.gsfc.nasa.gov/samples/IUElwp25637mxlo.fits
Now, you can launch your favorite python command line interface (IPython for instance). Inside, python type:
from fitsutils import *
d = mrdfits("IUElwp25637mxlo.fits")
print d.keys()
You should get something like this :
['DELTAW', 'QUALITY', 'FLUX', 'NPOINTS', 'BACKGROUND', 'APERTURE', 'WAVELENGTH', 'NET', 'SIGMA']

This is the list of variables contained in the binary table. Each variable is a Numpy ndarray. So you can access your data as usual in Numpy:
# get the shape of the FLUX array:
print d['FLUX'].shape
# get the first 10 FLUX values:
print d['FLUX'][:10]
# etc ...

Important note

Note that dimension orders are different in IDL and Python. Indeed, IDL has column-major ordering (Fortran ordering) while Python has row-major ordering (C ordering) of dimensions.

For instance, writing a FITS in IDL and reading it in Python.

In IDL, type:
IDL> data={data:indgen(4,5)}
IDL> print, size(data.data, /dim)
;           4           5
IDL> print, data.data[3, 2]
;11
IDL> print, data.data[2, 3]
;14

IDL> print, data.data
;       0       1       2       3
;       4       5       6       7
;       8       9      10      11
;      12      13      14      15
;      16      17      18      19

IDL> mwrfits, data, 'stamp.fits'
Now, in Python:
In [1]: from fitsutils.fitstables import *

In [2]: data = mrdfits('stamp.fits')

In [3]: data
Out[3]: 
{'DATA': array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [12, 13, 14, 15],
       [16, 17, 18, 19]], dtype=int16)}

In [4]: data['DATA'][2, 3]
Out[4]: 11

Partial installation

If you do not want to install fitsutils and still want to use fitstables, you can simply put the fitstables file in your path. Do not forget to make it executable.
wget https://github.com/nbarbey/fitsutils/raw/master/fitsutils/fitstables.py
chmod +x fitstables.py