Expand description
A blazing-fast reader and writer for FITS (Flexible Image Transport System) files — the standard data format of astronomy.
§Layering
The format’s structure maps onto a stack of layers, so the hot decode path
stays lean and the semantic layers compute only on demand. WCS (§8) and time
(§9) are dependency-free pure math, always compiled and surfaced directly as
Header getters (Header::wcs, Header::time); tiled compression carries
a dependency and stays behind the compression feature.
bytes ─► block layer ─► HDU layer ─► header model ─► typed data
(2880 grid, (boundary (ordered (images,
padding, scan, lazy records + tables,
I/O quantum) seeking) keyword index) heap, VLAs)BLOCK_SIZE— the 2880-byte block grid, padding rules, and rounding math.Bitpix— the array element type selector (BITPIX).Header,Value— an ordered header model (an internalCardlist) that round-trips byte-for-byte, with a side index for O(1) keyword lookup; it also parses the WCS and time layers on request (Header::wcs/Header::time).HduKind— HDU classification and the data-unit sizing formula that makes boundaries computable from headers alone (no data read required).FitsReader— lazy, seeking access to the HDU sequence of a file.
§Status
The structural spine (blocks, headers, HDU boundaries, lazy reading) plus
typed image decode/encode (Image), the multi-HDU writer (FitsWriter),
ASCII/binary tables, WCS, time coordinates, and tiled image+table compression
are implemented and tested — see each module’s docs for its design.
Re-exports§
pub use bitvec;pub use num_complex;
Structs§
- Ascii
Column - One ASCII-table column.
- Ascii
Column Reader - A handle to one column of an
AsciiTable, fromAsciiTable::column_by_idxorAsciiTable::column_by_name. Decode through it without re-passing the descriptor:rawfor the typed values,physicalfor the scaled plane. Borrows the table, so it cannot outlive it. - Ascii
Table - A parsed ASCII table plus its row bytes.
- Ascii
Write Column - One column to write into an ASCII table: data (
Text/I64/F64only), the fixed field width in characters, and the decimal count for floats. - BinTable
- A binary table’s structure plus its data unit.
- BitColumn
- A binary table’s
X(bit-array) column as a borrowed, 2-D bit view — fromColumnReader::bits(rectangular,nrows × repeat) orColumnReader::vla_bits(jaggedPX/QX). Bits are MSB-first (§7.3.2) and viewed in place over the data unit (zero-copy), so this borrows the table and can’t outlive it. - Checksum
Report - Result of
FitsReader::verify_checksum. A field isNonewhen its keyword is absent. - Column
- One column of a binary table: its
TFORMnformat, optional name/unit, theTSCALn/TZEROn/TNULLnmetadata, and its byte offset within a row. - Column
Reader - A handle to one column of a
BinTable, fromBinTable::column_by_idxorBinTable::column_by_name. Decode through it without re-passing the column descriptor:rawfor the typed values,physicalfor the scaledf64plane,unsigned/complex/bitsfor the special kinds, andvla(+vla_physical/vla_bits) for variable-lengthP/Qcolumns. Borrows the table, so it cannot outlive it. - Complex
- A complex number in Cartesian form.
- Compress
Options compression - Write-time tuning for
crate::FitsWriter::write_compressed_image. Each field applies only to the codecs that use it; the rest ignore it. Every field defaults to conventional behavior, soCompressOptions::default()(row tiling) orCompressOptions::tiled(shape)is the common case. - Data
Unit - A data unit read from the source: the full block-padded bytes plus the range
within them holding the actual data. The bytes after
data_rangeare FITS block fill, not part of the data array. - Datetime
- A calendar datetime (proleptic Gregorian, UTC-agnostic).
secondmay reach 60.x to represent a leap second; the JD arithmetic rolls it over naturally. - Epoch
Time - A time from a
JEPOCH/BEPOCHkeyword: its MJD and the scale the keyword implies (TDB forJEPOCH, ET ≈ TT forBEPOCH). - Fits
Reader - A FITS file opened over a seekable byte source. Opening scans HDU boundaries from headers alone (no data is read); data units are fetched on demand.
- Fits
Time - A header’s time coordinate frame (§9): the reference epoch, scale, unit, and the resolved global time keywords.
- Fits
Writer - Writes FITS HDUs to a byte sink. The first HDU written becomes the primary array; subsequent images/tables are written as extensions.
- GtiInterval
- A Good Time Interval as absolute MJD (§9.7).
- Hdu
- One Header/Data Unit located by the reader.
- Header
- A parsed header unit: an ordered list of content cards plus a side index for O(1) keyword lookup.
- Header
Entry - A read-only view of one stored header record, yielded by
Header::iter. - Image
- An N-dimensional image: a flat, Fortran-ordered buffer (axis 0 varies
fastest), the axis lengths from
NAXISn, and the scaling map that turns its stored (raw) samples into physical values. - Mmap
Source mmap - A memory-mapped file source: the kernel pages the file in on access, and data
units decode straight from the mapping (no staging copy, no read syscalls). The
owned
memmap2::Mmapkeeps the mapping alive for the reader’s lifetime. - Phase
Axis - The §9.6
'PHASE'axis folding parameters: the zero-phase reference timeCZPHSiaand the periodCPERIia, inTIMEUNITrelative to the time reference (theTSTARTconvention). - Random
Groups - A decoded random-groups primary array.
- RawImage
- An image read from an HDU, in whichever form the reader could give cheaply —
returned by
crate::FitsReader::read_imagefor both plain and tiled- compressed images, so callers needn’t know which they have. Carries the shape,BITPIX, andScaling; the pixels are exposed lazily throughdecode,u8,physical, andunsigned. - Scaling
- The linear
BSCALE/BZEROmap from a stored value to its physical value, plus the integerBLANKsentinel marking undefined pixels. - Slice
Source - An in-memory byte source: the whole file already resident as a slice (e.g. an mmap, or bytes read up front). Fetches return a zero-copy borrow, so a decode reads straight from these bytes with no staging copy.
- Stream
Source - A streaming
Read + Seeksource. Each fetch seeks and copies the range out — there is no resident image to borrow, so reads cost one extra memory pass. - TDisp
- A parsed
TDISPndisplay format: the format letter, field width, optional decimal places (.d/.m), and optional exponent width (a trailingEe). - Tform
- A parsed
TFORMnvalue: a repeat count, an element kind, and (for theP/Qvariable-length-array descriptors) the kind of the array elements in the heap. TherTaform’s trailing(emax)size hint is not retained. - Time
Bounds - The global bound / duration / error time keywords (§9.4, §9.5, §9.7), as read
by
Header::time_bounds. Start/end are absolute MJD; the rest are inTIMEUNIT. - Wcs
- A parsed world coordinate system for one (optionally alternate) axis set.
- Write
Column - One column to write into a binary table: its name, optional unit, data, and
the number of elements per row (
repeat). ForColumnData::Text,repeatis the fixed character width of the field.
Enums§
- Ascii
Kind - The value type of an ASCII-table column.
- Bitpix
- The physical element type of an array, selected by the
BITPIXkeyword. - Column
Data - A decoded column, flattened across all rows in row order. For array columns
(
repeat > 1) each row contributesrepeatconsecutive elements; forA, each row contributes oneString. Values are raw (big-endian decoded but notTSCALn/TZEROn-scaled). - Dither
Method compression - Float-quantization dithering for
crate::FitsWriter::write_compressed_image(ZQUANTIZ, §10.2). Applies only when compressing a float image; the integer codecs ignore it. - Epoch
- A reference epoch: Julian (
J2000.0) or Besselian (B1950.0). - Fits
Error - HduKind
- The structural kind of an HDU, inferred from its mandatory keywords.
- Image
Array ndarray - An image as an N-dimensional
ndarrayarray, tagged by element type — the n-D analog ofImageData, fromImage::into_ndarray/RawImage::to_ndarray. Requires thendarrayfeature. - Image
Data - An owned, host-endian sample buffer, tagged by its
BITPIXelement type. - Image
View - A borrowed, host-endian view of an image’s samples, tagged by
BITPIX— the zero-/low-copy counterpart to the ownedImageData, returned bycrate::FitsReader::read_image_view. Match it exactly likeImageData, but the slices borrow the reader’s reused decode scratch (or, forBITPIX = 8, the source bytes directly), so a view is valid only until the next read — ideal for a hot loop that processes each image and moves on, since reusing one scratch across reads pays the output allocation (and its page faults) once and even reuses it across differentBITPIX. For samples you need to keep, use the ownedRawImage::decode. - Projection
- A celestial projection algorithm — the 3-letter
CTYPEcode. - Sample
Type - The effective element type of an image’s physical samples — the analogue of
cfitsio’s image “equivalent type”.
BITPIXrecords only the stored width and signedness; the FITS unsigned and signed-byte conventions then layer aBZEROoffset on top (BSCALE == 1withBZERO = 2^(n-1), orBZERO = -128for signed bytes), so the values actually mean an unsigned (or signed-byte) integer. This enum is whatRawImage::physical/RawImage::unsignedyield, resolved up front fromBITPIX+Scalingwithout touching the pixels — so a caller can pick a code path (e.g. a per-type normalization range) without re-deriving theBZEROconvention itself. - TDisp
Kind - The format letter of a
TDISPndisplay format (§7.3.4, Table 20). - Tform
Kind - The element type of a binary-table column, from the letter of its
TFORMncode (Table 18). - Time
Axis Kind - A time-related WCS axis type (§9.6).
- Time
Scale - A FITS time scale (
TIMESYS/CTYPEi). - Unsigned
View - A typed integer realization of the FITS unsigned (and signed-byte) storage
conventions —
BSCALE == 1withBZEROthe sign-bit offset. Values are exact (nof64rounding), recovered by flipping the stored sign bit. - Value
- The typed value of a valued keyword record (§4.2).
Constants§
- BLOCK_
SIZE - The fundamental layout unit: 2880 bytes = 36 × 80-byte cards.
- CARD_
SIZE - A keyword record (card) is 80 bytes of restricted ASCII.
Traits§
- Source
- A seekable byte source the reader fetches HDU header and data units from. Sealed — implemented only by this crate’s source types, never externally.
Type Aliases§
- Mmap
Reader mmap - A
FitsReaderover a memory-mapped file — the typeFitsReader::open_mmapreturns. Requires themmapfeature. - Result
- Slice
Reader - A
FitsReaderover an in-memory byte slice — the typeFitsReader::from_bytesreturns. The lifetime is that of the borrowed bytes. - Stream
Reader - A
FitsReaderover a seeking byte source (Read + Seek, e.g. aFile) — the typeFitsReader::openreturns. A friendlier name forFitsReader<StreamSource<R>>.