fits-header
A pure-Rust library for reading and writing the header of a FITS file.
- Header-scoped: this crate never owns, inspects, or fabricates pixel data. Editing a
file on disk means
Header::update_file, which preserves the existing data unit byte-for-byte; creating a new file meansHeader::write_to_file, which writes your own pixel bytes right after the header and errors instead of overwriting an existing path. - Pure Rust, no C or system libraries. Builds with the MSVC toolchain.
- Every card is retained on parse; untouched cards (including long-string runs) re-serialize byte-for-byte. Only created or edited cards are re-rendered.
- Keyword access is strict: a bare name addresses the sole occurrence of a keyword and
errors when it is duplicated;
("NAME", n)selects the n-th occurrence — seeKey. - Create, read, update, and delete single or multiple keywords; batch mutations
(
set_many,remove_many) are atomic (all or nothing). - Typed reads and writes:
get::<T>covers strings, numbers, booleans, and date/times;Literal/Fixed/Sciwrappers control number formatting on write. - Long strings use the
CONTINUEconvention on read and write (withLONGSTRN). - The API is an ordered
HeaderofRecords — value cards, repeatable commentary cards, and opaque pass-through cards; it contains no application types. HIERARCHand other non-standard or malformed cards parse asRecordKind::Opaquerecords: preserved byte-for-byte on re-serialization, but not addressable by keyword (get/set/removenever see them).
Install
# opt into serde derives on the public types:
Usage
A Header is an in-memory value; set/remove/append mutate it only — nothing is
written to disk until you persist it.
use ;
// Editing a file on disk: the data unit (and any later HDUs) survives untouched. This
// is the common path — most callers only ever edit an existing file.
// Creating a NEW file: build a header, then hand it your own pixel bytes. Errors if
// `path` already exists — this crate never fabricates pixel data or clobbers a file.
See the guide for a
longer, task-oriented walkthrough backed by
examples/quickstart.rs.
Repeated keywords (HISTORY / COMMENT)
Commentary keywords like HISTORY and COMMENT repeat.
append
adds an occurrence,
get_all
reads every one in order, and an ("HISTORY", n) key addresses a single
occurrence to update it in place or remove it.
use Header;
let mut header = new;
header.append?;
header.append?;
assert_eq!;
// Read the processing log in order.
assert_eq!;
// Update one occurrence in place, then drop another. Commentary cards carry no
// value, so read them through `get`/`get_all`, not `get_str`.
header.set?;
header.remove?;
assert_eq!;
# Ok::
Reading and writing real files
Header::read_from_file(path)— read a header from disk. Parsing stops atEND, so the data unit is read but never interpreted.Header::update_file(path, edit)— the common path: edit an existing file in place. Reads the file, locates the header by scanning forEND, hands you the parsed header to mutate, then writes the new header back followed by everything that came after the original one (data unit, later HDUs) untouched. The write is atomic (temp file + rename). Errors withFitsError::MissingEndif the file has noENDcard.Header::write_to_file(path, data)— the rarer path: create a new file from a header and pixel bytes you already have. Errors instead of overwritingpathif it already exists; pass&[]for a header-only file.Header::to_header_bytes()— the lower-level building block behind both: the header block only (cards +END, padded to a 2880-byte multiple), for callers who assemble the file bytes themselves.
Documentation
- guide — task-oriented quickstart.
- docs.rs/fits-header — full API reference, generated
from the crate's rustdoc. Every public item is documented; the examples are compiled
and run as part of the test suite. Build it locally with
cargo doc --no-deps --all-features --open.
Features
Development
License
Licensed under the Apache License, Version 2.0.