Writing FITS files¶
- fitsy.image(data, header=None, primary=True)¶
Build an image HDU from a numpy array.
- Parameters:
data (numpy.ndarray) – Image pixels (any supported FITS dtype:
u8,i16,i32,i64,f32,f64). The returned HDU’s NAXIS list is the reverse ofdata.shape(numpy is row-major while FITS is fastest-axis-first).header (dict, optional) – Extra header cards to merge in. Values may be scalars or
(value, comment)tuples.primary (bool, optional) – If True (default) and this is the first HDU written, mark it as the primary HDU. Subsequent calls become image extensions.
- Returns:
ImageBuilder – Pass to
write().
- fitsy.compressed_image(data, header=None, *, tile_shape=None, extname=None)¶
Tile-compress a numpy array into a BINTABLE HDU (ZIMAGE).
The result is identical in structure to a CFITSIO/fpack-produced compressed image extension: a BINTABLE with ZIMAGE = T whose rows hold one compressed tile each. Astropy and funpack will transparently decompress it;
fitsy.openwill too.- Parameters:
data (numpy.ndarray) – Image pixels (any supported FITS dtype).
header (dict, optional) – Extra cards merged into the synthesized image header before compression. Structural keywords (
BITPIX,NAXIS*, etc.) are ignored.tile_shape (sequence[int], optional) – Tile shape in FITS axis order (tile[0] = NAXIS1 direction). Length must equal data.ndim. Default is
(NAXIS1, 1, 1, ...)per Pence & Seaman Sec.3 – one row per tile, which is the convention fpack uses.extname (str, optional) – EXTNAME keyword on the resulting BINTABLE. Default
"COMPRESSED_IMAGE".
Notes
The current Rust core only emits
ZCMPTYPE = 'GZIP_1'compressed tiles. The output is fully standards compliant and readable by every FITS library; if you need RICE/HCOMPRESS use the Rust API directly or runfpack -ron the output.
- fitsy.bintable(columns, units=None, extname=None)¶
Build a
BINTABLEHDU from a column dictionary.- Parameters:
columns (dict[str, sequence]) –
One entry per column. All columns must share the same row count. Supported value kinds:
numpy
bool/u8/i16/i32/i64/f32/f64arrays (1-D, or 2-D for fixed-repeat columns)list[str]->nA(right-padded to the longest string)list[complex]->M(C128)list[list[float]]->1PDvariable-length column (heap-stored,f64element type)
units (dict[str, str], optional) – Per-column
TUNITnstrings. Keys must match column names incolumns; entries for unknown columns are ignored.extname (str, optional) – Sets the
EXTNAMEkeyword for this extension.
- Returns:
BinTableBuilder – Pass to
write().
- fitsy.ascii_table(columns, formats=None, tnulls=None, units=None, extname=None)¶
Build an ASCII
TABLEHDU from a column dictionary.- Parameters:
columns (dict[str, sequence]) –
One entry per column. Supported value kinds:
list[int]or numpy int array ->I{w}(useNonecells forTNULL; combine withtnulls={col: "-9999"})list[float]or numpy float array ->E{w}.{d}by defaultlist[str]->A{maxlen}
formats (dict[str, str], optional) – Per-column override for the auto-chosen
TFORM.tnulls (dict[str, str], optional) –
TNULLsentinel string for integer columns containingNone.units (dict[str, str], optional) – Per-column
TUNITstrings.extname (str, optional) – Sets the
EXTNAMEkeyword.
- Returns:
AsciiTableBuilder – Pass to
write().
- fitsy.write(path, hdus, overwrite=False, *, checksums=False)¶
Write a sequence of HDU builders to disk.
- Parameters:
path (str or pathlib.Path) – Destination path. Overwritten if it already exists.
hdus (list) – Builders returned by
image(),bintable(), orascii_table(). The first item must be an image (it becomes the primary HDU); for table-only files passfitsy.image(np.zeros((0,)))first.overwrite (bool, optional) – If False (the default), raise
FileExistsErrorrather than truncating an existing file atpath.checksums (bool, optional) – If True, compute and stamp
CHECKSUMandDATASUMcards on every emitted HDU (FITS Checksum Proposal). Defaults to False.
Examples
>>> import numpy as np, fitsy >>> fitsy.write("out.fits", [ ... fitsy.image(np.zeros((10, 10), dtype=np.float32)), ... ])
- fitsy.append(path, data, header=None)¶
Append one image HDU to an existing FITS file.
The HDU is streamed in place at the end of the file – existing HDUs are not read or rewritten. The Python data array is converted to a non-primary (XTENSION = ‘IMAGE ‘) HDU before being written. Matches astropy.io.fits.append semantics.
- fitsy.setval(path, key, value, *, ext=None, comment=None)¶
Set one header keyword in path (rewrites the file).
- fitsy.delval(path, key, *, ext=None)¶
Remove one header keyword from path (rewrites the file).
- class fitsy.ImageBuilder¶
Bases:
objectOpaque image HDU spec produced by
image().Pass to
write()as part of a list of builders.
- class fitsy.BinTableBuilder¶
Bases:
objectOpaque BINTABLE HDU spec produced by
bintable().
- class fitsy.AsciiTableBuilder¶
Bases:
objectOpaque ASCII TABLE HDU spec produced by
ascii_table().