HDU (base class)

Every HDU type subclasses rustfits.HDU and inherits its shared accessors (header, index, extname, extver, has_data). Image and table subclasses add their own data-access surface.

class rustfits.HDU

Bases: object

Base class for every FITS Header-Data Unit (HDU).

All HDU types — ImageHDU, TableHDU, CompressedImageHDU, CompressedTableHDU, AsciiTableHDU — inherit from this class and share the header access, identity, and “do you have data?” surface defined here.

HDU instances are produced by indexing a FITS object (hdu = fits[1]) or iterating it (for hdu in fits: ...). They cannot be constructed directly from Python; the file object owns the on-disk layout.

The shared inherited surface is small on purpose — most of the useful methods live on the subclasses, where the data layout is known.

extname

EXTNAME header value, or None when the keyword is absent.

EXTNAME is the user-visible name of the HDU (e.g. 'SCI', 'CATALOG'). Combined with extver it’s the standard way to identify HDUs without relying on position-by-index.

extver

EXTVER header value, defaulting to 1 when absent.

Per the FITS standard, multiple HDUs may share an EXTNAME and are distinguished by EXTVER. Returns 1 rather than None for the absent case so callers can compare/select without handling Optional[int].

has_data

True iff this HDU has a non-empty data section.

Works uniformly across image and table HDUs: the test is NAXIS > 0 AND every NAXISn > 0. For images that means “at least one pixel”; for tables it means “at least one row of at least one column”.

Useful for picking the first HDU worth reading in a file (primary HDUs are often empty stubs):

hdu = next(h for h in fits if h.has_data)
arr = hdu.read()

Edge case: a VLA table with NAXIS2=0 but PCOUNT>0 (heap-only) returns False — no main rows means there’s nothing to interpret the heap through, which is the right answer for the “is this HDU worth reading?” question.

header

The HDU’s FITSHeader.

Returns a live view of this HDU’s header cards. Mutations via the header object (__setitem__, __delitem__, update, add_comment, add_history, add_blank) write through to disk immediately, following the disk-write-before-commit ordering documented on FITSHeader.

Reads are cheap; mutations may grow the reserved header blocks in place if the new card list exceeds the current allotment.

index

The HDU’s 0-based position in its file.

Stable for the lifetime of the FITS object — even when an earlier HDU grows and shifts this HDU’s bytes forward, the index is unchanged because the HDU is still at the same position in the file’s HDU list.