SmartHDUList¶
-
class
gammapy.utils.fits.SmartHDUList(hdu_list)[source]¶ Bases:
objectA FITS HDU list wrapper with some sugar.
This is a thin wrapper around
HDUList, with some conveniences built in.Parameters: hdu_list :
HDUListHDU list (stored in
hdu_listattribute)Examples
Opening a SmartHDUList calls
astropy.io.fits.opento get aHDUListobject, and then stores it away in thehdu_listattribute:>>> from gammapy.utils.fits import SmartHDUList >>> hdus = SmartHDUList.open('$GAMMAPY_EXTRA/datasets/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
TODO: add more conveniences, e.g. to create HDU lists from lists of Gammapy objects that can be serialised to FITS (e.g. SkyImage, SkyCube, EventList, …)
Attributes Summary
namesList 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
hduis given, tries to find an HDU of that given name or number. Otherwise, ifhdu_typeis given, looks for the first suitable HDU.Raises
KeyErrorif 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
filenameis passed throughmake_path, which accepts strings or Path objects and does environment variable expansion.Parameters: filename :
strFilename
-
write(filename, **kwargs)[source]¶ Write HDU list to FITS file.
This calls
astropy.io.fits.HDUList.writeto, passing**kwargs.The
filenameis passed throughmake_path, which accepts strings or Path objects and does environment variable expansion.Parameters: filename :
strFilename
-