A full featured python library to read from and write to FITS files
npx @tessl/cli install tessl/pypi-fitsio@1.2.0A comprehensive Python library for reading from and writing to FITS (Flexible Image Transport System) files using the cfitsio library. Built as a C extension with Python bindings, fitsio provides efficient access to FITS image, binary table, and ASCII table extensions while supporting advanced features like tile compression, variable-length table columns, and direct handling of compressed files.
pip install fitsioimport fitsioFor convenience functions:
from fitsio import FITS, read, read_header, writeFor header operations:
from fitsio import FITSHDR, FITSRecordimport fitsio
import numpy as np
# Read data from a FITS file
data = fitsio.read('data.fits')
# Read a specific extension and columns
table_data = fitsio.read('data.fits', ext=1, columns=['x', 'y', 'flux'])
# Read header information
header = fitsio.read_header('data.fits')
# Write data to a new FITS file
image = np.random.random((100, 100))
fitsio.write('output.fits', image)
# Write a structured array as a table
records = np.zeros(10, dtype=[('id', 'i4'), ('x', 'f8'), ('y', 'f8')])
records['id'] = np.arange(10)
records['x'] = np.random.random(10)
records['y'] = np.random.random(10)
fitsio.write('table.fits', records)
# Using the FITS class for more control
with fitsio.FITS('data.fits', 'rw') as fits:
# Read image data with slicing
image_subset = fits[0][10:50, 20:80]
# Read table rows and columns
table_subset = fits[1]['x', 'y'][100:200]
# Write new data
fits.write(new_data, compress='rice')fitsio uses a hierarchical structure that mirrors FITS file organization:
The library provides both high-level convenience functions for simple operations and low-level control through the FITS class for complex workflows. All operations leverage the bundled cfitsio library for performance and compatibility.
Core FITS file handling including opening, closing, reading, and writing FITS files. Provides both convenience functions for simple operations and the FITS class for full control over file operations.
class FITS:
def __init__(self, filename, mode='r', **kwargs): ...
def close(self): ...
def write(self, data, **kwargs): ...
def read(filename, ext=None, **kwargs): ...
def write(filename, data, **kwargs): ...
def read_header(filename, ext=0, **kwargs): ...
def read_scamp_head(filename, header=None): ...FITS header manipulation including reading, writing, and modifying header keywords, comments, and metadata. Supports complete header management with proper FITS formatting and validation.
class FITSHDR:
def __init__(self, record_list=None): ...
def add_record(self, record): ...
def __setitem__(self, key, value): ...
def __getitem__(self, key): ...
class FITSRecord: ...
class FITSCard: ...Reading and writing FITS image data with support for subsets, compression, reshaping, and various data types. Includes numpy-style slicing for efficient access to image subregions without loading entire images into memory.
class ImageHDU:
def read(self, **kwargs): ...
def write(self, img, start=0): ...
def get_dims(self): ...
def reshape(self, dims): ...
def is_compressed(self): ...
def __getitem__(self, slice): ...Reading and writing FITS table data (binary and ASCII tables) with support for column operations, row filtering, variable-length columns, and table modifications. Includes advanced features like WHERE clause filtering and efficient column-wise access.
class TableHDU:
def read(self, **kwargs): ...
def read_column(self, col, **kwargs): ...
def write(self, data, **kwargs): ...
def append(self, data, **kwargs): ...
def where(self, expression, **kwargs): ...
def insert_column(self, name, data, **kwargs): ...
def __getitem__(self, key): ...READONLY = 0
READWRITE = 1NOCOMPRESS = 0
RICE_1 = 11
GZIP_1 = 21
GZIP_2 = 22
PLIO_1 = 31
HCOMPRESS_1 = 41ANY_HDU = -1
IMAGE_HDU = 0
ASCII_TBL = 1
BINARY_TBL = 2NO_DITHER = -1
SUBTRACTIVE_DITHER_1 = 1
SUBTRACTIVE_DITHER_2 = 2def cfitsio_version(asfloat=False):
"""Get version of bundled cfitsio library."""
class FITSRuntimeWarning: ...
class FITSFormatError(Exception): ...