Foundations to Frontier
Tests
C. Programming, Tools and HPC › C7 Scientific Data Formats

FITS: The Astronomy Data Format

FITS (Flexible Image Transport System), standardised in 1981 and endorsed by the IAU, is still how telescopes, space observatories and archives exchange images, spectra, data cubes and tables. A FITS file is a sequence of HDUs, each an ASCII header of 80-character keyword cards followed by big-endian binary data in 2880-byte blocks. This topic covers the header and data layout, BITPIX/BSCALE/BZERO, extensions (IMAGE, BINTABLE), the World Coordinate System that maps pixels to sky coordinates, tiled image compression (Rice, fpack) and the tools (Astropy, CFITSIO, DS9). In the exercises you write and read FITS files byte by byte, convert pixels to RA/Dec through the TAN projection and implement Rice coding; then you measure how far a calibrated sky image can be compressed by quantization before noise or photometry suffer, with and without subtractive dithering.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

A file format older than most telescopes in orbit

A FITS file starts with plain text you can read in any editor: lines like NAXIS1 = 4096, EXPTIME = 300.0, OBJECT = 'M31', each exactly 80 characters, then the picture’s numbers in binary. The text header tells any program, on any computer, how to read the numbers and where on the sky each pixel points. Because the format is never allowed to break old files, a FITS image from 1985 still opens today.

Quantizing a float image for compression
≈ 2.57 bits/pixel (vs 32 for float32) · added noise 0.287σ (theory 0.289σ) · background median shift -0.289σ
Grey line: original pixels along a cut through a star; orange dots: values after quantizing and restoring. Coarser steps cost fewer bits (about one bit per halving of Q) but add noise q/√12. Without dithering, restored values sit on a grid, so the background median jumps to the nearest step: move the sky offset with Q ≤ 1 to see a bias that would shift every star's photometry. Dithering with a reproducible random offset per pixel removes it at no extra cost.
Why engineers care

Hubble, JWST, Chandra, Gaia, ground observatories and many solar and planetary instruments deliver FITS; mission pipelines, archives (MAST, ESA, IRSA) and spacecraft calibration tools read and write it. Compression choices made at the archive decide how many petabytes a survey occupies.

📝 Quick check (Level 0)
0/3 answered
  1. 1. A FITS header is…
  2. 2. In the demo, coarser quantization…
  3. 3. Why does FITS never change incompatibly?

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

Structure

A file is a list of HDUs (header + data units). The primary HDU holds an image (or nothing); extensions follow: IMAGE, BINTABLE (typed columns, e.g. a source catalogue or spectrum), TABLE (ASCII). Each header is 80-byte cards ending with END, padded to 2880-byte blocks; data are big-endian, also padded.

Keyword Meaning
BITPIX 8, 16, 32, 64 (integers) or −32, −64 (IEEE floats)
NAXIS, NAXIS1, NAXIS2 dimensions; NAXIS1 varies fastest (columns)
BSCALE, BZERO physical = BZERO + BSCALE × stored
EXPTIME, DATE-OBS, TELESCOP, INSTRUME, FILTER observation metadata
CTYPEi, CRPIXi, CRVALi, CDELTi or CDi_j World Coordinate System
from astropy.io import fits
from astropy.wcs import WCS
with fits.open("image.fits") as hdul:
    hdul.info()
    data, hdr = hdul[0].data, hdul[0].header        # data shape (NAXIS2, NAXIS1)
    w = WCS(hdr)
    sky = w.pixel_to_world(100, 200)                 # 0-based pixel → SkyCoord
📝 Quick check (Level 1)
0/3 answered
  1. 1. BITPIX = −32 means…
  2. 2. A 4096 × 2048 (rows × cols) array has NAXIS1 =…
  3. 3. A catalogue of sources is best stored as…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 35 minUndergraduate students and practising engineers.

Unsigned integers and scaling

FITS has no unsigned 16-bit type, so CCD images in 0–65 535 are stored as int16 with BZERO = 32768. Floats can also be stored as scaled integers (BSCALE ≠ 1), trading precision for size.

World Coordinate System

Pixel (p₁, p₂), counted from 1, maps to intermediate coordinates x = CDELT1·(p₁ − CRPIX1), y = CDELT2·(p₂ − CRPIX2) (or through a CD matrix for rotation and skew), then through the projection named in CTYPE (RA---TAN is the gnomonic projection) to (RA, Dec) around the reference point (CRVAL1, CRVAL2). CDELT1 is usually negative because RA increases to the east, which is to the left on the sky. Distortion terms (SIP, TPV) correct camera optics.

Tiled compression

The image is split into tiles (rows by default) stored as a compressed BINTABLE. Integer images use Rice coding: pixel differences, zig-zag mapped to non-negative integers, then for each block of 32 values the k that minimises unary(z ≫ k) + k bits. Float images are first quantized to integers with step q = σ/Q, where σ is the background noise; subtractive dithering adds a reproducible random offset before rounding and removes it after, so restored values are not stuck on a grid.

🐍 Exercise 1: FITS byte by byte, WCS and Rice coding
Format header cards; build and parse headers padded to 2880 bytes; write and read a primary HDU (big-endian data, NAXIS order, uint16 through BZERO); convert pixels to RA/Dec and back with the TAN projection; implement Rice coding of integer sequences with per-block k and decode it back.
📝 Quick check (Level 2)
0/3 answered
  1. 1. Stored int16 −32768 with BZERO 32768 is physically…
  2. 2. The pixel at CRPIX maps to…
  3. 3. Zig-zag maps −1 to…

Level 3: Advanced

★★★★☆ Advanced⏱ 30 minGraduate level, with full derivations and proofs.

Working with FITS in practice

  • Use Astropy (astropy.io.fits, astropy.wcs, astropy.table) in Python and CFITSIO in C; fitsio is a fast alternative; DS9 and Aladin for viewing.
  • Memory-map large images (fits.open(..., memmap=True)) and read sections; avoid modifying headers of open archive files in place.
  • Keep metadata in standard keywords; long strings use the CONTINUE convention, and long or custom keywords the HIERARCH convention.
  • Archives store fpack-compressed files (.fits.fz); lossless for integer raw frames, quantized (Q around 4–16, dithered) for calibrated float frames.
  • For very large tables, archives also publish Parquet or HATS copies (C7.1); FITS remains the interchange and archival format.
📝 Quick check (Level 3)
0/3 answered
  1. 1. A .fits.fz file is…
  2. 2. Typical fpack quantization for calibrated floats?
  3. 3. Library to convert pixels to sky coordinates in Python?

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

Quantization and compression, measured

Exercise 1. A 100 × 70 uint16 image writes as one header block plus 5 data blocks (17 280 bytes), stored as big-endian int16 with BZERO = 32768, and reads back bit for bit. With CDELT1 = −1″, moving one pixel to lower x changes RA·cos(Dec) by +1″; pixel → sky → pixel round-trips to 10⁻⁶ pixel. Rice coding of 108 noisy integers uses under a third of their 32-bit size and decodes exactly.

Exercise 2: compressing a calibrated sky image (512 × 512 float32 in electrons: sky 1000 e⁻, 60 stars of 10³–3 × 10⁵ e⁻, Poisson and 5 e⁻ read noise, 2% flat field; background σ = 32.6 e⁻ estimated from pixel differences; photometry in 3σ_PSF apertures with an annulus median background, errors in units of each star’s photometric uncertainty).

Q (q = σ/Q) Rice bits/pixel zlib bits/pixel Added noise Noise increase Photometry bias, no dither With dither
0.5 2.10 2.57 0.58σ 15.5% +5.40σ −0.26σ
1 2.95 4.02 0.29σ 4.1% −2.46σ +0.11σ
2 3.88 5.30 0.14σ 1.0% +0.48σ +0.004σ
4 4.86 6.57 0.072σ 0.26% +0.086σ +0.004σ
16 6.85 9.35 0.018σ 0.02% −0.004σ −0.007σ
64 8.85 12.5 0.0045σ ≈ 0 0.000σ +0.002σ

Lossless zlib on the float32 image needs 26.1 bits/pixel: noise bits do not compress. Quantizing at Q = 4 with dithering stores the same image in 4.87 bits/pixel (6.6× smaller than float32, 5.4× smaller than lossless) while raising the noise by 0.26% and leaving photometry unbiased. Each halving of q costs about one bit per pixel, as entropy predicts, and the added noise matches q/√12 exactly. The surprise is the bias without dithering: at Q ≤ 1 the restored background sits on a grid, so the annulus median jumps by up to q/2 per pixel and shifts every star’s flux by several σ; dithering removes it for 0–0.14 extra bits. Rice coding of differences beats zlib on the same integers by 1–4 bits/pixel.

🐍 Exercise 2 (research): how far can a sky image be compressed?
Simulate a calibrated CCD image; estimate the background noise robustly; quantize with step σ/Q for several Q with and without subtractive dithering; measure Rice and zlib bits per pixel, added noise and the bias and scatter of aperture photometry against the unquantized image.

FITS in AI, robotics and aerospace

  • Space telescopes: HST, JWST, Chandra, XMM-Newton, Euclid and Roman products are FITS; JWST also uses ASDF alongside.
  • Survey archives (SDSS, Pan-STARRS, Rubin) distribute compressed FITS images and FITS or Parquet catalogues.
  • ML for astronomy: galaxy morphology, transient classification and photometric-redshift models read FITS cutouts; dataset builders convert to HDF5 or Zarr for training.
  • Solar and planetary missions: SDO, Solar Orbiter and many instruments use FITS; planetary data use the related PDS4 standard.

References

  1. W. D. Pence et al. (2010). “Definition of the Flexible Image Transport System (FITS), version 3.0.” Astronomy & Astrophysics 524, A42.
  2. E. W. Greisen, M. R. Calabretta (2002). “Representations of world coordinates in FITS.” A&A 395, 1061; M. R. Calabretta, E. W. Greisen (2002). “Representations of celestial coordinates in FITS.” A&A 395, 1077.
  3. W. D. Pence, R. Seaman, R. L. White (2009). “Lossless astronomical image compression and the effects of noise.” PASP 121, 414.
  4. W. D. Pence, R. L. White, R. Seaman (2010). “Optimal compression of floating-point astronomical images without significant loss of information.” PASP 122, 1065.
  5. R. F. Rice, J. R. Plaunt (1971). “Adaptive variable-length coding for efficient compression of spacecraft television data.” IEEE Trans. Communication Technology 19(6).
  6. Astropy Collaboration (2022). “The Astropy Project: sustaining and growing a community-oriented open-source project.” ApJ 935, 167.