FITS is most popular image file format for astronomical images. In Python there is a library named PyFITS that handles this format.
FITS means Flexible Image Transport System. We can install PyFITS with the next command:
sudo pip install pyfits or download the source code and execute this command:
sudo setup.py install. Before studying PyFITS we’ll take a look to FITS.
First and foremost, we need to say: FITS isn’t only an image file format, but is the standard archival data format for astronomical data sets.
A FITS file is composed by the following FITS structures:
- Primary Header Data and Unit. HDU is a data structure consisting of a header and the data the header describes.
- Conforming extensions (optional)
- Other special records(optional and restricted)
Primary header: The first component of a FITS file shall be the primary HDU which always contains the primary header and maybe be followed by the primary data array.
The header of a primary HDU consists of one or more header blocks, each containing a series of 80-character keyword records.
Each 80-character header keyword record consists of:
- a keyword name
- a value indicator (only required if a value is present)
- an optional value and
- an optional comment
The last header block must contain the
END keyword, which marks the logical end of the header.
Primary Data Array: It consist of a single data array with from 1 to 999 dimensions.
Extensions: New extension types should be created only when the organization of the information is such that it cannot be handled by one of the existing extension types. A FITS file that contains extensions is commonly referred to as a multi-extension FITS (MEF) file.
Written above is more or less the definition and structure of a FITS file. Now let’s see anything about PyFITS
import pyfits as pf, sys hdulist = pf.open(sys.argv) print(hdulist) print(hdulist.info()) for hdu in hdulist: print(hdu)
We open an existing FITS file
(2) and save it as an object in
hdulist variable. As we read earlier, we can confirm that there is a primary HDU, represented by
hdulist and since
hdulist we find the extensions HDU.
We can also confirm that each HDU has a header and data attribute:
- Definition of the Flexible Image Transport System (FITS). FITS Working Group
- The PyFITS Handbook. Space Telescope Science Institute