In development

Email me at release

Anatomy of a FITS file: HDUs, headers, BITPIX and BZERO

A byte-by-byte walk through a real TESS light curve: 2880-byte blocks, 80-character cards, the data-size formula, byte order and BZERO.

Every FITS file is built from the same few rules. A reader that gets one of them wrong can return plausible-looking wrong numbers instead of an error. This post walks the bytes of a real public file, a TESS light curve, using only the rules in the FITS Standard 4.0. It checks every offset against astropy and ends with the mistakes that produce silent errors.

The answer in two sentences

A FITS file is a sequence of 2880-byte blocks holding one or more header-and-data units (HDUs). Each header is plain ASCII text that says exactly how many bits of data follow and how to read them. Binary data are big-endian and padded with zeros to the next block boundary.

The rules, from the standard

All section and equation numbers below refer to FITS 4.0.

Blocks and HDUs (§3.1, §3.2). A file is a whole number of 2880-byte blocks. The primary HDU starts at byte 0, and each later HDU starts on the block right after the previous one ends. Every HDU is one or more header blocks followed by zero or more data blocks.

Headers (§3.2, §3.3.1, §4.4.1). A header is a series of 80-character keyword records (“cards”), 36 to a block. Header blocks may contain only ASCII characters 32 to 126, so a tab, a null byte or a line break anywhere in a header makes the file non-conforming. The END card closes the header, and everything after it, up to the end of that block, is ASCII spaces.

How much data follows (Eq. 1 and Eq. 2). The header states the data size. For an extension, the number of bits is

Nbits = |BITPIX| × GCOUNT × (PCOUNT + NAXIS1 × NAXIS2 × … × NAXISm)

The primary HDU uses the same formula with GCOUNT = 1 and PCOUNT = 0 (Eq. 1). If NAXIS is 0 there is no data at all. The data then fill whole blocks, and the unused tail of the last block is set to zero (§3.3.2 for images, §7.3.3 for binary tables). ASCII tables, a separate extension type, pad with spaces instead (§7.2.3).

What the bits mean (§3.3.2, Table 8). BITPIX gives the type: 8, 16, 32 and 64 are integers, and −32 and −64 are IEEE single- and double-precision floats. Values are stored big-endian, most significant byte first. In an array, axis 1 varies fastest, and indices count from 1.

Scaling (Eq. 3, Table 11). The value in the file is not always the value it represents:

physical value = BZERO + BSCALE × array value

The standard uses this to store unsigned integers in signed types. Unsigned 16-bit data, for example, are written as BITPIX = 16 with BZERO = 32768 and BSCALE = 1.

Worked example: one TESS light curve, byte by byte

The file is the 2-minute light curve of TIC 52368076 from TESS sector 1. This is TOI-125, the star in Naponiello et al. (2026), an arXiv preprint accepted by A&A. The file was written by the SPOC pipeline (data release 42), and it is 2,039,040 bytes long, exactly 708 blocks. Our script, analysis.py, reads it as raw bytes and applies only the rules above.

HDU Type Cards (with END) Header blocks Data bytes (Eq. 1 or 2) Data blocks Zero fill
0 primary, NAXIS = 0 45 2 0 0 0
1 BINTABLE “LIGHTCURVE”, 100 × 20,076 168 5 2,007,600 698 2,640
2 IMAGE “APERTURE”, 11 × 11, BITPIX = 32 50 2 484 1 2,396

Three things to notice:

  • None of these headers fits in one block. 45 cards need 2 blocks, and 168 cards need 5, because each block holds 36.
  • The table size follows from Eq. 2. A binary table has BITPIX = 8, NAXIS1 = bytes per row (100) and NAXIS2 = rows (20,076). That gives 8 × 1 × (0 + 100 × 20,076) = 16,060,800 bits, or 2,007,600 bytes. That fills 697 blocks and part of a 698th, so 2,640 zero bytes follow before the next HDU can start.
  • The pieces add up. 2 + 5 + 698 + 2 + 1 = 708 blocks, the whole file, with nothing left over.
Byte map of a 2,039,040-byte TESS light-curve file in three strips. The whole file is almost entirely table data. The first nine 2880-byte records hold a 2-record primary header, a 5-record table header and the start of the table rows. The last four records hold the end of the table, 2,640 zero bytes of fill, a 2-record image header and an 11 by 11 image followed by zero fill.
Figure 1. Layout of tess2018206045859-s0001-0000000052368076-0120-s_lc.fits (TESS sector 1, TIC 52368076, observed 2018-07-25 to 2018-08-22 TDB; SPOC data release 42). Top: the whole file. Middle: the first nine 2880-byte records. Bottom: the last four. Offsets computed by our script from the FITS 4.0 rules and checked against astropy 8.0.3. Values in figures/fig-1-byte-map.csv.Credit: Astrametry; data NASA TESS (MAST)

The row is the sum of its columns

The table has 20 columns. Each TFORMn keyword gives a column’s type, and Table 18 of the standard gives its size:

  • 5 columns are D, 8-byte floats: TIME and four centroid columns
  • 2 are J, 4-byte integers: CADENCENO and QUALITY
  • 13 are E, 4-byte floats: the fluxes, errors and other values

That is 40 + 8 + 52 = 100 bytes, exactly NAXIS1. If your column sizes do not add up to NAXIS1, you have misread a TFORM.

Reading the first value by hand

The table data start at byte 20,160 (7 header blocks × 2880). TIME is the first column, so the first 8 bytes of the table are the first time stamp: 40 94 b5 2f ac af b2 c1.

  • Read big-endian, as the standard requires, they are 1325.2965571835377.
  • Read little-endian, the same bytes are −313,502,767.7.

Astropy returns 1325.2965571835377. The header says what the number means: TUNIT1 = 'BJD - 2457000, days', with TIMESYS = 'TDB' and BJDREFI = 2457000. So the first cadence is at BJDTDB 2458325.2966. The CADENCENO integer 12 bytes into the row reads 70,444 both by hand and in astropy.

The TESS header gives the time reference in its own keywords, BJDREFI and TIMEREF = 'SOLARSYSTEM'. The standard’s time chapter names the equivalents JDREFI/JDREFF (§9.2.2) and TREFPOS (§9.2.3). A general-purpose reader that looks only for the standard’s names will miss the TESS reference, and §9.2.2 then tells it to assume a reference of MJD 0. Check which keywords your mission actually writes.

What astropy says

For all three HDUs, astropy 8.0.3’s fileinfo() reports the same header offsets (0; 5,760; 2,030,400), the same data offsets (5,760; 20,160; 2,036,160) and the same data spans as our byte walk. The walk also confirms that every header pad is ASCII spaces and every data pad is zero bytes.

BZERO: the unsigned-integer trick

The TESS file does not use BZERO, so the script makes a small file to show it. It writes three unsigned 16-bit pixels, 0, 1000 and 65535, with astropy. Astropy writes BITPIX = 16, BZERO = 32768 and BSCALE = 1, as Table 11 of the standard prescribes.

The integers actually stored in the file are −32768, −31768 and 32767. Eq. 3 turns them back into 0, 1000 and 65535. A reader that skips Eq. 3 would report a sky level of 1000 ADU as −31,768.

The same trick covers signed bytes (BZERO = −128) and unsigned 32- and 64-bit integers (BZERO = 231 and 263).

Pitfalls that give wrong numbers without an error

  1. Assuming a one-block header. Scan for the END card, then skip to the next block boundary. Here the table header is 5 blocks.
  2. Forgetting the zero fill. The next HDU starts at the next block boundary, not right after the last data byte. Here that is 2,640 bytes later.
  3. Reading in native byte order. On a little-endian machine the first time stamp becomes −313,502,767.7. Astropy handles this for you: its arrays carry a big-endian data type, which NumPy reads correctly. A hand-written reader or a raw memory map has to do it itself.
  4. Ignoring BZERO and BSCALE. This is one way 16-bit camera data come out negative. Apply Eq. 3 to every value, and compare BLANK with the stored value, not the physical one (§4.4.2.5).
  5. Swapping the axes. NAXIS1 is the fastest-varying axis, the image width. NumPy reverses the order: our script writes a NumPy array of shape (2, 3), and astropy records NAXIS1 = 3, NAXIS2 = 2. FITS pixel (1, 1) is NumPy element [0, 0].
  6. Writing non-ASCII headers. A tab or a UTF-8 degree sign in a comment makes the file non-conforming (§3.2).

How Astrametry handles this

Astrametry’s FITS reader is in development. Its method dossier sets out:

  • the rules above
  • the integer path for BZERO
  • the tile-compression cases
  • the validation tests

Free browser versions of a FITS viewer and a header viewer are planned.

Read the methods

Data and code

Data
Versions

python 3.13.14 · astropy 8.0.3 · numpy 2.5.5 · matplotlib 3.11.2

Rerun
uv run --no-project --python 3.13 --with astropy==8.0.3 --with numpy==2.5.5 --with matplotlib==3.11.2 python -I analysis.py <path to the light-curve file>

Written with AI from the sources cited below and approved by the founder.

Sources

Astrametry is in development.

It will calibrate, plate-solve and measure your FITS images on your own computer. We will send one email when 1.0 is ready, and nothing else.