or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

file-operations.mdheader-operations.mdimage-operations.mdindex.mdtable-operations.md
tile.json

tessl/pypi-fitsio

A full featured python library to read from and write to FITS files

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
pypipkg:pypi/fitsio@1.2.x

To install, run

npx @tessl/cli install tessl/pypi-fitsio@1.2.0

index.mddocs/

fitsio

A 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.

Package Information

  • Package Name: fitsio
  • Language: Python
  • Installation: pip install fitsio

Core Imports

import fitsio

For convenience functions:

from fitsio import FITS, read, read_header, write

For header operations:

from fitsio import FITSHDR, FITSRecord

Basic Usage

import 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')

Architecture

fitsio uses a hierarchical structure that mirrors FITS file organization:

  • FITS class: Top-level file interface managing HDU access and file operations
  • HDU classes: Extension-specific handlers (ImageHDU, TableHDU, AsciiTableHDU) providing specialized read/write operations
  • FITSHDR class: Header container managing keywords, comments, and metadata
  • Convenience functions: High-level read/write functions for common operations

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.

Capabilities

File Operations

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): ...

File Operations

Header Operations

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: ...

Header Operations

Image Operations

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): ...

Image Operations

Table Operations

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): ...

Table Operations

Constants

File Access Modes

READONLY = 0
READWRITE = 1

Compression Types

NOCOMPRESS = 0
RICE_1 = 11
GZIP_1 = 21
GZIP_2 = 22
PLIO_1 = 31
HCOMPRESS_1 = 41

HDU Types

ANY_HDU = -1
IMAGE_HDU = 0
ASCII_TBL = 1
BINARY_TBL = 2

Dithering Methods

NO_DITHER = -1
SUBTRACTIVE_DITHER_1 = 1
SUBTRACTIVE_DITHER_2 = 2

Utility Functions

def cfitsio_version(asfloat=False):
    """Get version of bundled cfitsio library."""

class FITSRuntimeWarning: ...
class FITSFormatError(Exception): ...