Simple AstroPy Python FITS image stack examples

Assume an image stack in file myimg.fits. FITS files do not memory map except in special cases. Usually FITS files are under 2 GB, making it feasible to work with the whole image stack on a modern PC. That is, load the whole image stack and then index the 3-D array in RAM.

from astropy.io import fits

fn = 'myimg.fits'

with fits.open(fn, 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.

Newer image formats HDF5 and NetCDF4 can have effectively unlimited file sizes, and easily store arbitrary organizations of variables, data, and metadata.


Related: read FITS image stack in Matlab