This section provides a quick introduction of using astropy.io.fits. The
goal is to demonstrate the package’s basic features without getting into too
much detail. If you are a first time user or have never used Astropy or PyFITS,
this is where you should start. See also the FAQ for
answers to common questions/issues.

The astropy.io.fits.util.get_testdata_filepath() function,
used in the examples here, is for accessing data shipped with Astropy.
To work with your own data instead, please use astropy.io.fits.open(),
which takes either relative or absolute path.

Once the astropy.io.fits package is loaded using the standard convention
[1], we can open an existing FITS file:

The open() function has several optional arguments which will be
discussed in a later chapter. The default mode, as in the above example, is
“readonly”. The open function returns an object called an HDUList
which is a list-like collection of HDU objects. An HDU (Header Data Unit) is
the highest level component of the FITS file structure, consisting of a header
and (typically) a data array or table.

After the above open call, hdul[0] is the primary HDU, hdul[1] is
the first extension HDU, etc (if there are any extensions), and so on. It
should be noted that Astropy is using zero-based indexing when referring to
HDUs and header cards, though the FITS standard (which was designed with
FORTRAN in mind) uses one-based indexing.

After exiting the with scope the file will be closed automatically. That’s
(generally) the preferred way to open a file in Python, because it will close
the file even if an exception happens.

If the file is opened with lazy_load_hdus=False, all of the headers will
still be accessible after the HDUList is closed. The headers and data may or
may not be accessible depending on whether the data are touched and if they
are memory-mapped, see later chapters for detail.

The open() function supports a memmap=True argument that allows the
array data of each HDU to be accessed with mmap, rather than being read into
memory all at once. This is particularly useful for working with very large
arrays that cannot fit entirely into physical memory. Here memmap=True by default, and this value is obtained from the configuration item astropy.io.fits.Conf.use_memmap.

This has minimal impact on smaller files as well, though some operations, such
as reading the array data sequentially, may incur some additional overhead. On
32-bit systems arrays larger than 2-3 GB cannot be mmap’d (which is fine,
because by that point you’re likely to run out of physical memory anyways), but
64-bit systems are much less limited in this respect.

Warning

When opening a file with memmap=True, because of how mmap works this means that
when the HDU data is accessed (i.e. hdul[0].data) another handle to the FITS file
is opened by mmap. This means that even after calling hdul.close() the mmap still
holds an open handle to the data so that it can still be accessed by unwary programs
that were built with the assumption that the .data attribute has all the data in-memory.

In order to force the mmap to close either wait for the containing HDUList object to go
out of scope, or manually call delhdul[0].data (this works so long as there are no other
references held to the data array).

Due to the FITS format’s FORTRAN origins, FITS does not natively support
unsigned integer data in images or tables. However, there is a common
convention to store unsigned integers as signed integers, along with a
shift instruction (a BZERO keyword with value 2**(BITPIX-1)) to
shift up all signed integers to unsigned integers. For example, when writing
the value 0 as an unsigned 32-bit integer, it is stored in the FITS
file as -32768, along with the header keyword BZERO=32768.

Astropy recognizes and applies this convention by default, so that all data
that looks like it should be interpreted as unsigned integers is automatically
converted (this applies to both images and tables). In Astropy versions prior
to v1.1.0 this was not applied automatically, and it is necessary to pass the
argument uint=True to open(). In v1.1.0 or later this is the
default.

The open() function will seamlessly open FITS files that have been
compressed with gzip, bzip2 or pkzip. Note that in this context we’re talking
about a fits file that has been compressed with one of these utilities - e.g. a
.fits.gz file.

There are some limitations with working with compressed files. For example with Zip
files that contain multiple compressed files, only the first file will be accessible.
Also bzip does not support the append or update access modes.

When writing a file (e.g. with the writeto() function), compression will be
determined based on the filename extension given, or the compression used in a
pre-existing file that is being written to.

As mentioned earlier, each element of an HDUList is an HDU object with
.header and .data attributes, which can be used to access the header
and data portions of the HDU.

For those unfamiliar with FITS headers, they consist of a list of 80 byte
“cards”, where a card contains a keyword, a value, and a comment. The keyword
and comment must both be strings, whereas the value can be a string or an
integer, floating point number, complex number, or True/False. Keywords
are usually unique within a header, except in a few special cases.

The header attribute is a Header instance, another Astropy object. To get the
value associated with a header keyword, simply do (a la Python dicts):

Although keyword names are always in upper case inside the FITS file,
specifying a keyword name with Astropy is case-insensitive, for the user’s
convenience. If the specified keyword name does not exist, it will raise a
KeyError exception.

Similarly, it is easy to update a keyword’s value in Astropy, either through
keyword name or index:

>>> hdr=hdul[0].header>>> hdr['targname']='NGC121-a'>>> hdr[27]=99

Please note however that almost all application code should update header
values via their keyword name and not via their positional index. This is
because most FITS keywords may appear at any position in the header.

It is also possible to update both the value and comment associated with a
keyword by assigning them as a tuple:

Like a dict, one may also use the above syntax to add a new keyword/value pair
(and optionally a comment as well). In this case the new card is appended to
the end of the header (unless it’s a commentary keyword such as COMMENT or
HISTORY, in which case it is appended after the last card with that keyword).

Another way to either update an existing card or append a new one is to use the
Header.set() method:

>>> hdr.set('observer','Edwin Hubble')

Comment or history records are added like normal cards, though in their case a
new card is always created, rather than updating an existing HISTORY or COMMENT
card:

Note: Be careful not to confuse COMMENT cards with the comment value for normal
cards.

To update existing COMMENT or HISTORY cards, reference them by index:

>>> hdr['history'][0]='I updated this file on 2/27/09'>>> hdr['history']I updated this file on 2/27/09>>> hdr['comment'][1]='I like using JWST observations'>>> hdr['comment']Edwin Hubble really knew his stuffI like using JWST observations

To see the entire header as it appears in the FITS file (with the END card and
padding stripped), simply enter the header object by itself, or
print(repr(hdr)):

Entering simply print(hdr) will also work, but may not be very legible
on most displays, as this displays the header as it is written in the FITS file
itself, which means there are no linebreaks between cards. This is a common
source of confusion for new users.

If an HDU’s data is an image, the data attribute of the HDU object will return
a numpy ndarray object. Refer to the numpy documentation for details
on manipulating these numerical arrays:

>>> data=hdul[1].data

Here, data points to the data object in the second HDU (the first HDU,
hdul[0], being the primary HDU) which corresponds to the ‘SCI’
extension. Alternatively, you can access the extension by its extension name
(specified in the EXTNAME keyword):

>>> data=hdul['SCI'].data

If there is more than one extension with the same EXTNAME, the EXTVER value
needs to be specified along with the EXTNAME as a tuple; e.g.:

>>> data=hdul['sci',2].data

Note that the EXTNAME is also case-insensitive.

The returned numpy object has many attributes and methods for a user to get
information about the array, e.g.:

>>> data.shape(40, 40)>>> data.dtype.name'int16'

Since image data is a numpy object, we can slice it, view it, and perform
mathematical operations on it. To see the pixel value at x=5, y=2:

>>> print(data[1,4])348

Note that, like C (and unlike FORTRAN), Python is 0-indexed and the indices
have the slowest axis first and fastest changing axis last; i.e. for a 2-D
image, the fast axis (X-axis) which corresponds to the FITS NAXIS1 keyword, is
the second index. Similarly, the 1-indexed sub-section of x=11 to 20
(inclusive) and y=31 to 40 (inclusive) would be given in Python as:

This example changes the values of both the pixel [1, 4] and the sub-section
[30:40, 10:20] to the new value of 999. See the Numpy documentation for
more details on Python-style array indexing and slicing.

The next example of array manipulation is to convert the image data from counts
to flux:

Note that performing an operation like this on an entire image requires holding
the entire image in memory. This example performs the multiplication in-place
so that no copies are made, but the original image must first be able to fit in
main memory. For most observations this should not be an issue on modern
personal computers.

If at this point you want to preserve all the changes you made and write it to
a new file, you can use the HDUList.writeto() method (see below).

This section describes reading and writing table data in the FITS format using
the fits package directly. For simple cases, however, the
high-level Unified file read/write interface will often suffice and is somewhat easier to use.
See the Unified I/O FITS section for details.

Like images, the data portion of a FITS table extension is in the .data
attribute:

>>> fits_table_filename=fits.util.get_testdata_filepath('tb.fits')>>> hdul=fits.open(fits_table_filename)>>> data=hdul[1].data# assuming the first extension is a table

If you are familiar with numpy recarray (record array) objects, you
will find the table data is basically a record array with some extra
properties. But familiarity with record arrays is not a prerequisite for this
guide.

To see the first row of the table:

>>> print(data[0])(1, 'abc', 3.7000000715255736, False)

Each row in the table is a FITS_record object which looks like a
(Python) tuple containing elements of heterogeneous data types. In this
example: an integer, a string, a floating point number, and a Boolean value. So
the table data are just an array of such records. More commonly, a user is
likely to access the data in a column-wise way. This is accomplished by using
the field() method. To get the first column (or “field” in
Numpy parlance–it is used here interchangeably with “column”) of the table,
use:

>>> data.field(0)array([1, 2]...)

A numpy object with the data type of the specified field is returned.

Like header keywords, a column can be referred either by index, as above, or by
name:

>>> data.field('c1')array([1, 2]...)

When accessing a column by name, dict-like access is also possible (and even
preferable):

>>> data['c1']array([1, 2]...)

In most cases it is preferable to access columns by their name, as the column
name is entirely independent of its physical order in the table. As with
header keywords, column names are case-insensitive.

But how do we know what columns we have in a table? First, let’s introduce
another attribute of the table HDU: the columns
attribute:

>>> cols=hdul[1].columns

This attribute is a ColDefs (column definitions) object. If we use the
ColDefs.info() method from the interactive prompt:

it will show the attributes of all columns in the table, such as their names,
formats, bscales, bzeros, etc. A similar output that will display the column
names and their formats can be printed from within a script with:

As mentioned earlier, after a user opened a file, made a few changes to either
header or data, the user can use HDUList.writeto() to save the changes.
This takes the version of headers and data in memory and writes them to a new
FITS file on disk. Subsequent operations can be performed to the data in memory
and written out to yet another different file, all without recopying the
original data to (more) memory:

hdul.writeto('newtable.fits')

will write the current content of hdulist to a new disk file newfile.fits.
If a file was opened with the update mode, the HDUList.flush() method can
also be used to write all the changes made since open(), back to the
original file. The close() method will do the same for a FITS
file opened with update mode:

withfits.open('original.fits',mode='update')ashdul:# Change something in hdul.hdul.flush()# changes are written back to original.fits# closing the file will also flush any changes and prevent further writing

So far we have demonstrated how to read and update an existing FITS file. But
how about creating a new FITS file from scratch? Such tasks are very easy in
Astropy for an image HDU. We’ll first demonstrate how to create a FITS file
consisting only the primary HDU with image data.

First, we create a numpy object for the data part:

>>> importnumpyasnp>>> n=np.arange(100.0)# a simple sequence of floats from 0.0 to 99.9

To create a table HDU is a little more involved than image HDU, because a
table’s structure needs more information. First of all, tables can only be an
extension HDU, not a primary. There are two kinds of FITS table extensions:
ASCII and binary. We’ll use binary table examples here.

To create a table from scratch, we need to define columns first, by
constructing the Column objects and their data. Suppose we have two
columns, the first containing strings, and the second containing floating point
numbers:

This shortcut will automatically create a minimal primary HDU with no data and
prepend it to the table HDU to create a valid FITS file. If you require
additional data or header keywords in the primary HDU you may still create a
PrimaryHDU object and build up the FITS file manually using an
HDUList.

For example, first create a new Header object to encapsulate any
keywords you want to include in the primary HDU, then as before create a
PrimaryHDU:

When we create a new primary HDU with a custom header as in the above example,
this will automatically include any additional header keywords that are
required by the FITS format (keywords such as SIMPLE and NAXIS for
example). In general, users should not have to manually manage such keywords,
and should only create and modify observation-specific informational keywords.

We then create a HDUList containing both the primary HDU and the newly created
table extension, and write to a new file:

Alternatively, we can append the table to the HDU list we already created in
the image file section:

>>> hdul.append(hdu)>>> hdul.writeto('image_and_table.fits')

The data structure used to represent FITS tables is called a FITS_rec
and is derived from the numpy.recarray interface. When creating
a new table HDU the individual column arrays will be assembled into a single
FITS_rec array.

So far, we have covered the most basic features of astropy.io.fits. In the
following chapters we’ll show more advanced examples and explain options in
each class and method.

astropy.io.fits also provides several high level (“convenience”) functions.
Such a convenience function is a “canned” operation to achieve one simple task.
By using these “convenience” functions, a user does not have to worry about
opening or closing a file, all the housekeeping is done implicitly.

Warning

These functions are useful for interactive Python sessions and simple
analysis scripts, but should not be used for application code, as they
are highly inefficient. For example, each call to getval()
requires re-parsing the entire FITS file. Code that makes repeated use
of these functions should instead open the file with open()
and access the data structures directly.

The first of these functions is getheader(), to get the header of an HDU.
Here are several examples of getting the header. Only the file name is required
for this function. The rest of the arguments are optional and flexible to
specify which HDU the user wants to access:

>>> fromastropy.io.fitsimportgetheader>>> hdr=getheader(fits_image_filename)# get default HDU (=0), i.e. primary HDU's header>>> hdr=getheader(fits_image_filename,0)# get primary HDU's header>>> hdr=getheader(fits_image_filename,2)# the second extension>>> hdr=getheader(fits_image_filename,'sci')# the first HDU with EXTNAME='SCI'>>> hdr=getheader(fits_image_filename,'sci',2)# HDU with EXTNAME='SCI' and EXTVER=2>>> hdr=getheader(fits_image_filename,('sci',2))# use a tuple to do the same>>> hdr=getheader(fits_image_filename,ext=2)# the second extension>>> hdr=getheader(fits_image_filename,extname='sci')# first HDU with EXTNAME='SCI'>>> hdr=getheader(fits_image_filename,extname='sci',extver=2)

The function getdata() gets the data of an HDU. Similar to
getheader(), it only requires the input FITS file name while the
extension is specified through the optional arguments. It does have one extra
optional argument header. If header is set to True, this function will return
both data and header, otherwise only data is returned:

The update() function will update the specified extension with the input
data/header. The 3rd argument can be the header associated with the data. If
the 3rd argument is not a header, it (and other positional arguments) are
assumed to be the extension specification(s). Header and extension specs can
also be keyword arguments.

The printdiff() function will print a difference report of two FITS files,
including headers and data. The first two arguments must be two FITS
filenames or FITS file objects with matching data types (i.e., if using strings
to specify filenames, both inputs must be strings). The third
argument is an optional extension specification, with the same call format
of getheader() and getdata(). In addition you can add any keywords
accepted by the FITSDiff class

fromastropy.io.fitsimportprintdiff# get a difference report of ext 2 of inA and inBprintdiff('inA.fits','inB.fits',ext=2)# ignore HISTORY and COMMMENT keywordsprintdiff('inA.fits','inB.fits',ignore_keywords=('HISTORY','COMMENT')

Finally, the info() function will print out information of the specified
FITS file:

A package for reading and writing FITS files and manipulating their
contents.

A module for reading and writing Flexible Image Transport System
(FITS) files. This file format was endorsed by the International
Astronomical Union in 1999 and mandated by NASA as the standard format
for storing high energy astrophysics data. For details of the FITS
standard, see the NASA/Science Office of Standards and Technology
publication, NOST 100-2.0.