Home / Learn / Science / Astronomy / Lesson 5.5
All lessons

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.

Key points, meanings and formulas on one page: choose "Save as PDF" in the print box.

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.

Astronomy images and tables come as FITS files, a plain-text header that describes the data followed by the numbers, and with Python you can open one, read it and measure it in a few lines.

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 as EXPTIME = 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])
Try it: slice an image like NumPy
 
A made-up 64 × 48 image with a sky level of about 200 counts and a few stars. In NumPy, rows come first: data[y0:y1, x0:x1], and row 0 is at the top when you print the array but at the bottom when astronomy software draws it (as here). The median ignores stars better than the mean, which is why it is used for the sky.

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:

Open. hdul = fits.open("m51.fits") lists the parts (HDUs). Most images are in the first one.
Read the header. hdr["EXPTIME"], hdr["FILTER"], hdr["NAXIS1"]: every fact about the observation as keyword = value.
Get the numbers. data = hdul[0].data gives a NumPy array. Rows are y, columns are x: data[y, x].
Slice. cut = data[500:600, 800:900] takes a 100 × 100 patch around a star.
Measure. 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 n values: the mean is x‾=1n∑xi; the median is the middle value when sorted.

Standard deviation: σ=1n∑(xi−x‾)2. Over blank sky it measures the noise per pixel.

Robust noise: astronomers often use σ≈1.4826×MAD, where MAD is the median of |xi−median|; it ignores stars and cosmic rays.

Pixel to sky (simple tangent-plane WCS): offset in degrees ≈(x−xref)×CDELT1.

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: 200=14.1 counts. The measured 14 matches, so the sky noise is almost all from photons, and read noise is small. Sky rate: 200/120=1.7 electrons per second per pixel, which you would use in the SNR formula (Sky watching 6.2).

5. Practice

More practice (answers below):

  1. Write the NumPy slice for rows 100 to 149 and columns 200 to 299.
  2. A header says EXPTIME = 30 and a star gives 9,000 counts. What is its count rate?
  3. Why is FITS still used after more than 40 years?
Answers
  1. data[100:150, 200:300].
  2. 300 counts per second.
  3. 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

1A FITS header holds:
2In NumPy, data[10:20, 30:40] selects:
3Why use the median rather than the mean for the sky level?

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.