SmartHDUList

class gammapy.utils.fits.SmartHDUList(hdu_list)[source]

Bases: object

A FITS HDU list wrapper with some sugar.

This is a thin wrapper around HDUList, with some conveniences built in.

Parameters:
hdu_list : HDUList

HDU list (stored in hdu_list attribute)

Examples

Opening a SmartHDUList calls astropy.io.fits.open to get a HDUList object, and then stores it away in the hdu_list attribute:

>>> from gammapy.utils.fits import SmartHDUList
>>> hdus = SmartHDUList.open('$GAMMAPY_DATA/catalogs/fermi/gll_psch_v08.fit.gz')
>>> type(hdus.hdu_list)
astropy.io.fits.hdu.hdulist.HDUList

So of course, you can do the usual things via hdus.hdu_list:

>>> hdus.hdu_list.filename()
>>> hdus.hdu_list.info()
>>> [hdu.name for hdu in hdus.hdu_list]

In addition, for a SmartHDUList, it’s easier to get the HDUs you want:

>>> hdus.get_hdu('Extended Sources')  # by name
>>> hdus.get_hdu(2)  # by index
>>> hdus.get_hdu(hdu_type='image')  # first image (skip primary if empty)
>>> hdus.get_hdu(hdu_type='table')  # first table

Attributes Summary

names List of HDU names (stripped, upper-case).

Methods Summary

get_hdu([hdu, hdu_type]) Get HDU with given name, number or type.
get_hdu_index([hdu, hdu_type]) Get index of HDU with given name, number or type.
open(filename, **kwargs) Create from FITS file (SmartHDUList).
write(filename, **kwargs) Write HDU list to FITS file.

Attributes Documentation

names

List of HDU names (stripped, upper-case).

Methods Documentation

get_hdu(hdu=None, hdu_type=None)[source]

Get HDU with given name, number or type.

This method simply calls get_hdu_index(hdu, hdu_type), and if successful, returns the HDU for that given index.

get_hdu_index(hdu=None, hdu_type=None)[source]

Get index of HDU with given name, number or type.

If hdu is given, tries to find an HDU of that given name or number. Otherwise, if hdu_type is given, looks for the first suitable HDU.

Raises KeyError if no suitable HDU is found.

Parameters:
hdu : int or str

HDU number or name, passed to astropy.io.fits.HDUList.index_of.

hdu_type : {‘primary’, ‘image’ , ‘table’}

Type of HDU to load

Returns:
idx : int

HDU index

classmethod open(filename, **kwargs)[source]

Create from FITS file (SmartHDUList).

This calls astropy.io.fits.open, passing **kwargs. It reads the FITS headers, but not the data.

The filename is passed through make_path, which accepts strings or Path objects and does environment variable expansion.

Parameters:
filename : str

Filename

write(filename, **kwargs)[source]

Write HDU list to FITS file.

This calls astropy.io.fits.HDUList.writeto, passing **kwargs.

The filename is passed through make_path, which accepts strings or Path objects and does environment variable expansion.

Parameters:
filename : str

Filename