Simple AstroPy Python FITS image stack examples

Here we assume an image stack in file myimg.fits. FITS do not memory map except in special cases, and usually they are under 2 GB so you can work with the whole image stack on a modern PC. That is, you normally can’t just load a single image from an image stack, you load the whole image stack and then index the 3-D array in RAM.

from import fits

fn = 'myimg.fits'

with, mode='readonly') as h:
    img = h[0].data
    lat = h[0].header['GLAT']
    lon = h[0].header['GLON']

The header contained location metadata that we assigned to lat and lon.

Avoid using FITS

FITS is an old workhorse format, but generally one should avoid using FITS unless colleagues/boss demand FITS. Newer image formats are much more efficient and robust such as NetCDF4 and HDF5. HDF5 and NetCDF4 can have enormous, effectively unlimited file sizes, and easily store arbitrary organizations of variables, data, and metadata.