Module 5: Doing real research · Lesson 5.5 · about 25 min
FITS files and Python: astronomy data in your hands
Astronomy images and tables come as FITS files: a text header that describes the data, then the numbers. With Python's astropy and NumPy you can open one, read its header, slice the image and measure it in a few lines.
Module 5 · about 25 minutes · read first: 5.4 Spectroscopy: from raw frame to line, Sky watching 6.3 Calibration frames: darks, flats and bias · maths: arrays and indexing. You’ll be able to describe what is inside a FITS file, open one in Python, read its header and measure part of its image.
1. Why this matters
Nearly every professional observatory, space telescope and archive shares data as FITS (Flexible Image Transport System), a format created in 1981 and still readable today. Amateur cameras save FITS too. Being able to open and measure FITS files yourself is the step from looking at pretty pictures to doing science with the numbers.
2. Inside a FITS file
A FITS file is one or more HDUs (header and data units). Each has:
- a header: plain-text lines of
KEYWORD = value / comment, such asEXPTIME = 120.0 / exposure time in seconds,FILTER = 'V',DATE-OBS, and the world coordinate system (WCS) that ties pixels to RA and Dec; - data: an image (a 2D grid of numbers), a cube, or a table.
In Python:
from astropy.io import fits
import numpy as np
with fits.open("m42_v_120s.fits") as hdul:
hdr = hdul[0].header
data = hdul[0].data.astype(float)
print(hdr["EXPTIME"], hdr["FILTER"], data.shape) # e.g. 120.0 V (48, 64)
sky = np.median(data[0:8, 0:8])
Things to notice:
- The slice text always names rows first, then columns.
- Over empty sky the mean and median agree; over a star the mean jumps much more.
Open a FITS image the way a researcher does:
hdul = fits.open("m51.fits") lists the parts (HDUs). Most images are in the first one.hdr["EXPTIME"], hdr["FILTER"], hdr["NAXIS1"]: every fact about the observation as keyword = value.data = hdul[0].data gives a NumPy array. Rows are y, columns are x: data[y, x].cut = data[500:600, 800:900] takes a 100 × 100 patch around a star.np.median(sky) for the background and 1.4826 * MAD for robust noise that ignores stars and cosmic rays.Maths and principle
Mean and median of values: the mean is ; the median is the middle value when sorted.
Standard deviation: . Over blank sky it measures the noise per pixel.
Robust noise: astronomers often use , where MAD is the median of ; it ignores stars and cosmic rays.
Pixel to sky (simple tangent-plane WCS): offset in degrees .
3. What it means physically
Each number in the image is a count of electrons freed by photons in one pixel during the exposure (plus noise and offsets you calibrate away; Sky watching 6.3). The header is the lab notebook that travels with the data: without the exposure time, filter and date, the numbers can’t be turned into brightness or compared with anyone else’s.
- NAXIS1, NAXIS2
- Image width and height in pixels.
- BITPIX
- Bits per pixel; negative means floating point.
- σ ≈ 1.4826 × MAD
- Robust noise from the median absolute deviation.
- Physical meaning
- A FITS file is a labelled box. The label (header) says what is inside, where the telescope pointed, how long it exposed and through which filter; the box holds the raw numbers. Because the label is plain text, a file written in 1981 can still be read today.
“FITS images are just pictures like JPEGs.” A FITS image holds the actual measured numbers, often 16- or 32-bit, with no compression losses and a full header. A JPEG keeps only 8 bits of display brightness and throws the science away.
4. Worked example
An 8 × 8 box of blank sky in a 120 s exposure has median 200 counts and standard deviation 14 counts. Is that what photon noise alone predicts if the camera gain is 1 electron per count?
Pure photon noise: counts. The measured 14 matches, so the sky noise is almost all from photons, and read noise is small. Sky rate: electrons per second per pixel, which you would use in the SNR formula (Sky watching 6.2).
5. Practice
More practice (answers below):
- Write the NumPy slice for rows 100 to 149 and columns 200 to 299.
- A header says
EXPTIME = 30and a star gives 9,000 counts. What is its count rate? - Why is FITS still used after more than 40 years?
Answers
data[100:150, 200:300].- 300 counts per second.
- It is open, simple, self-describing (the header), and every tool can read it, so old data stay usable forever.
- FITS = header (keyword = value text) + data (arrays or tables).
- astropy.io.fits opens files; data comes as NumPy arrays indexed [y, x].
- Median and MAD give robust sky level and noise.
- The WCS keywords map pixels to sky coordinates.
Try it with real numbers: open any FITS file in your browser with the FITS viewer, and stack frames with the image stacker.
New words: FITS, HDU, header, keyword, WCS, NumPy array, slice, median absolute deviation.
Check yourself
Sources
- Wells, D. C., Greisen, E. W. and Harten, R. H. (1981), A&AS 44, 363 (the FITS standard); Astropy Collaboration (2022), ApJ 935, 167
Your ticks stay in this browser. Save them as a file on the course home.
Updated 2026-11-22. Numbers in the examples are made up to show the idea unless a source is given. Simulations run in your browser on made-up data; nothing you type is sent anywhere. Astronomy only: nothing here is astrology.