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) andNAXIS2= 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.
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:TIMEand four centroid columns - 2 are
J, 4-byte integers:CADENCENOandQUALITY - 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
- Assuming a one-block header. Scan for the
ENDcard, then skip to the next block boundary. Here the table header is 5 blocks. - 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.
- 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.
- Ignoring
BZEROandBSCALE. This is one way 16-bit camera data come out negative. Apply Eq. 3 to every value, and compareBLANKwith the stored value, not the physical one (§4.4.2.5). - Swapping the axes.
NAXIS1is the fastest-varying axis, the image width. NumPy reverses the order: our script writes a NumPy array of shape (2, 3), and astropy recordsNAXIS1 = 3,NAXIS2 = 2. FITS pixel (1, 1) is NumPy element[0, 0]. - 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.
Data and code
- Data
- tess2018206045859-s0001-0000000052368076-0120-s_lc.fitsSHA-256 0ce129b74e6e87cf5029dc83f0062466200d6a7cce6dc0d5f39588102e9b57c4
- Code
- 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
- IAU FITS Working Group (2018). Definition of the Flexible Image Transport System (FITS), the FITS Standard, Version 4.0, language-edited version of 13 August 2018. Standard document, not a journal article.
- NASA TESS mission, SPOC pipeline. 2-minute light curve of TIC 52368076, sector 1, data release 42, from MAST (public AWS copy linked in this post’s provenance). Mission data product.
- Naponiello, L. et al. (2026). Transit timing variations and a fourth sub-Neptune in the near-resonant system TOI-125. Accepted by A&A; arXiv version, CC BY 4.0. Used here only to identify the star.
- Astropy Collaboration et al. (2022). The Astropy Project: Sustaining and Growing a Community-oriented Open-source Project and the Latest Major Release (v5.0) of the Core Package. ApJ 935, 167. CC BY 4.0.