This tutorial introduces the FITS Input/Output (I/O) routines available for IDL. Virtually all the FITS I/O routines are developed and maintained at the Goddard Library. Most astronomers will only ever use a few fundamental, advanced FITS I/O routines that will be discussed here. You should be aware, however, that there are many more lower level FITS routines in IDL in case you need that level of control over reading and writing FITS files. We will also discuss in this tutorial how to read and write binary FITS tables which are invaluable for data analysis. Finally, all the IDL routines can read compressed (gzipped) FITS files as easily as un-compressed files, which is nice because it saves disk space.
Download (SHIFT+click) the following FITS image of NGC 1068 to your local directory and then start an IDL session.
To read the FITS file into a variable type
image = readfits('ngc1068.fits')
help, image
You almost always also want the FITS header, which is output from readfits():
image = readfits('ngc1068.fits',head)
print, head
Alternatively, sometimes you only want to read the header without bothering with the image. In this case you can type
head = headfits('ngc1068.fits')
Another much more sophistated FITS reader is mrdfits(), which is designed to read any kind of FITS file (take a look at the documentation). We will use this routine to read binary FITS files, below. To read the zeroth extension of your FITS file type
image = mrdfits('ngc1068.fits',0,head)
We will describe FITS file extensions a little more below. Sometimes you might be dealing with a very long header. To make it more readable the way you might use the UNIX command more type
hprint, head
To extract keywords from a FITS header use the sxpar() function:
naxis1 = sxpar(head,'NAXIS1') naxis2 = sxpar(head,'NAXIS2') print, naxis1, naxis2
Now let's generate an image and a FITS header and write them out to disk. Use this fancy bit of code to make a two-dimensional Gaussian function:
dim = 100 sigma = 10 x = dist(dim) top = -x^2/(2*(sigma)^2) im = shift(exp(top),dim/2,dim/2) im = im / total(im) surface, im
Next we need a generic FITS header, to which we will add our own keywords and history notes. We will need the routines mkhdr, sxaddpar, and sxaddhist.
mkhdr, h, im print, h
To add some keywords to the header use sxaddpar and to delete keywords we don't want use sxdelpar :
sxdelpar, h, 'COMMENT' sxaddpar, h, 'SIGMA', 10, ' Gaussian Sigma' print, h
To add a history note (always good) type
sxaddhist, 'This is my new header - woo hoo.', h print, h
Finally, to write out our FITS file type
writefits, 'myfits.fits', im, h
Note that if you didn't give writefits a header it would have generated a generic one using mkhdr. For example,
writefits, 'dum.fits', im
print, headfits('dum.fits')
Finally let's look at how we can read and write structures and arrays of structures as binary FITS tables. If you haven't learned about structures I recommend this tutorial. The methods I describe here are extremely powerful and easy to use in IDL. Once you have mastered binary FITS tables and data structures you will never need to use ASCII text files again, guaranteed, except maybe to satisfy non-IDL collaborators. Generate three images with different noise properties and store statistics on each image in an array of data structures.
; initialize the output data structures
nimage = 3L ; number of images
imstats = {$
image: 0L, $
mean: 0.0, $
median: 0.0, $
sigma: 0.0}
imstats = replicate(imstats,nimage) ; copy the blank structure NIMAGE times
imstats.image = lindgen(nimage)+1 ; fill the IMAGE field
; generate each noisy image, calculate the statistics of the
; image, and then fill the data structure
im1 = randomu(seed,100,100)*10-4.5
im2 = randomu(seed,100,100)*1.0+145.0
im3 = randomu(seed,100,100)*45+23.0
imstats.mean = [mean(im1),mean(im2),mean(im3)]
imstats.median = [median(im1),median(im2),median(im3)]
imstats.sigma = [stddev(im1),stddev(im2),stddev(im3)]
help, imstats, /str
struct_print, imstats
The power of using arrays of structures is that all your information can be stored in a single IDL variable, in this case imstats. Now let's write this data structure out as a binary FITS table. Type
mwrfits, imstats, 'imstats.fits', /create
The create keyword overwrites any existing file called imstats.fits. Note that we didn't need to create a FITS header for this data structure, but you are certainly free to. Binary FITS tables are written to the first (rather than the zeroth) extension of a FITS file. A FITS file can have an arbitrary number of extensions. To read the binary FITS table back into your IDL session in a different variable type
s = mrdfits('imstats.fits',1)
help, s, /str
Last update 2003 November. Email J. Moustakas with questions or comments.