CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-astropy

Astronomy and astrophysics core library providing comprehensive tools for astronomical computations and data handling

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

Overview
Eval results
Files

fits-io.mddocs/

FITS File I/O

Complete FITS (Flexible Image Transport System) file support for reading, writing, and manipulating astronomical data files with headers, image data, and binary tables.

Capabilities

Basic FITS Operations

High-level functions for common FITS file operations including reading data and headers, writing files, and updating existing files.

def open(name, mode='readonly', memmap=None, save_backup=False, cache=True, lazy_load_hdus=True, **kwargs):
    """
    Open a FITS file and return an HDUList object.
    
    Parameters:
    - name: file name or file-like object
    - mode: file access mode ('readonly', 'update', 'append', 'denywrite', 'ostream')
    - memmap: use memory mapping for large files
    - save_backup: save backup when updating files
    - cache: cache file in memory
    - lazy_load_hdus: load HDUs only when accessed
    
    Returns:
    HDUList: list of Header Data Units
    """

def getdata(filename, *args, header=False, lower=None, upper=None, view=None, **kwargs):
    """
    Get data from FITS file.
    
    Parameters:
    - filename: FITS file name
    - *args: HDU specification (ext, extname, extver)
    - header: also return header
    - lower, upper: data range bounds
    - view: numpy slice for data subset
    
    Returns:
    ndarray or tuple: data array, optionally with header
    """

def getheader(filename, *args, **kwargs):
    """
    Get header from FITS file.
    
    Parameters:
    - filename: FITS file name  
    - *args: HDU specification
    
    Returns:
    Header: FITS header object
    """

def writeto(filename, data, header=None, output_verify='exception', overwrite=False, checksum=False, **kwargs):
    """
    Write data to FITS file.
    
    Parameters:
    - filename: output file name
    - data: data array to write
    - header: FITS header
    - output_verify: verification level
    - overwrite: overwrite existing file
    - checksum: compute checksums
    """

def append(filename, data, header=None, checksum=False, verify=True, **kwargs):
    """Append HDU to existing FITS file."""

def update(filename, data, *args, header=None, verify=True, **kwargs):
    """Update HDU in existing FITS file."""

def info(filename, output=None, **kwargs):
    """
    Print summary information about FITS file.
    
    Parameters:
    - filename: FITS file name
    - output: output stream (default: stdout)
    """

HDU Classes and Structure

HDU (Header Data Unit) classes representing different types of FITS extensions with headers and data.

class HDUList:
    """
    List-like container for FITS Header Data Units.
    
    Parameters:
    - hdus: list of HDU objects
    - file: associated file object
    """
    def __init__(self, hdus=[], file=None): ...
    
    def writeto(self, fileobj, output_verify='exception', overwrite=False, checksum=False):
        """Write HDUList to file."""
    
    def append(self, hdu):
        """Append HDU to list."""
    
    def insert(self, index, hdu):
        """Insert HDU at specified index."""
    
    def info(self, output=None):
        """Print information about HDUs."""
    
    def verify(self, option='warn'):
        """Verify FITS standard compliance."""
    
    def close(self):
        """Close associated file."""
    
    def __getitem__(self, key):
        """Get HDU by index, name, or (name, version)."""

class PrimaryHDU:
    """
    Primary Header Data Unit (first HDU in FITS file).
    
    Parameters:
    - data: data array
    - header: FITS header
    - do_not_scale_image_data: disable automatic scaling
    - uint: treat signed integers as unsigned
    - scale_back: scale data when writing
    """
    def __init__(self, data=None, header=None, do_not_scale_image_data=False, uint=False, scale_back=None): ...
    
    @property
    def data(self):
        """HDU data array."""
    
    @property
    def header(self):
        """HDU header."""
    
    def writeto(self, name, output_verify='exception', overwrite=False, checksum=False): ...

class ImageHDU(PrimaryHDU):
    """Image extension HDU."""
    def __init__(self, data=None, header=None, name=None, **kwargs): ...

class BinTableHDU:
    """
    Binary table HDU.
    
    Parameters:
    - data: table data (structured array, Table, etc.)
    - header: FITS header
    - name: extension name
    - uint: handle unsigned integers
    - character_as_bytes: handle character columns
    """
    def __init__(self, data=None, header=None, name=None, uint=False, character_as_bytes=False): ...
    
    @classmethod
    def from_columns(cls, columns, header=None, nrows=None, fill=False, **kwargs):
        """Create from Column objects."""

class TableHDU:
    """ASCII table HDU."""
    def __init__(self, data=None, header=None, name=None): ...

class CompImageHDU:
    """Compressed image HDU."""
    def __init__(self, data=None, header=None, name=None, compression_type='RICE_1', **kwargs): ...

Header Manipulation

FITS header handling with keyword management, comments, and history records.

class Header:
    """
    FITS header with ordered keyword-value pairs.
    
    Parameters:
    - cards: list of Card objects or keyword-value pairs
    - txtfile: read from text file
    - copy: copy input data
    """
    def __init__(self, cards=[], txtfile=None, copy=True): ...
    
    def __getitem__(self, key):
        """Get header value by keyword."""
    
    def __setitem__(self, key, value):
        """Set header keyword value."""
    
    def __delitem__(self, key):
        """Delete header keyword."""
    
    def __contains__(self, key):
        """Check if keyword exists."""
    
    def append(self, card=None, useblanks=True, bottom=False, end=False):
        """Append card to header."""
    
    def insert(self, key, card, useblanks=True, after=False):
        """Insert card before/after specified keyword."""
    
    def remove(self, keyword, ignore_missing=False, remove_all=False):
        """Remove keyword from header."""
    
    def rename_keyword(self, oldkeyword, newkeyword, force=False):
        """Rename header keyword."""
    
    def set(self, keyword, value=None, comment=None, before=None, after=None, savecomment=False):
        """Set keyword value with optional positioning."""
    
    def update(self, *args, **kwargs):
        """Update header with new keywords."""
    
    def add_history(self, value, before=None, after=None):
        """Add HISTORY record."""
    
    def add_comment(self, value, before=None, after=None):
        """Add COMMENT record."""
    
    def add_blank(self, value='', before=None, after=None):
        """Add blank card."""
    
    def copy(self, strip=False):
        """Copy header."""

class Card:
    """
    Individual FITS header card (keyword-value-comment).
    
    Parameters:
    - keyword: FITS keyword (8 characters max)
    - value: keyword value
    - comment: keyword comment
    - image: create from raw 80-character string
    """
    def __init__(self, keyword=None, value=None, comment=None, image=None): ...
    
    @property
    def keyword(self):
        """FITS keyword."""
    
    @property
    def value(self):
        """Keyword value."""
    
    @property
    def comment(self):
        """Keyword comment."""
    
    @property
    def image(self):
        """80-character card image."""

Usage Examples

Basic FITS File Operations

from astropy.io import fits
import numpy as np

# Read FITS file
with fits.open('image.fits') as hdul:
    fits.info('image.fits')  # Print file info
    header = hdul[0].header  # Primary header
    data = hdul[0].data      # Primary data
    
    # Access extension by name
    if 'EVENTS' in hdul:
        events = hdul['EVENTS'].data

# Quick data access
data = fits.getdata('image.fits')
header = fits.getheader('image.fits')
data, header = fits.getdata('image.fits', header=True)

Creating and Writing FITS Files

# Create simple image
data = np.random.random((100, 100))

# Write with basic header
fits.writeto('output.fits', data, overwrite=True)

# Create custom header
hdr = fits.Header()
hdr['OBJECT'] = 'M31'
hdr['EXPTIME'] = 600.0
hdr['FILTER'] = 'V'
hdr.add_comment('This is a test image')

fits.writeto('output_with_header.fits', data, header=hdr, overwrite=True)

Install with Tessl CLI

npx tessl i tessl/pypi-astropy

docs

configuration.md

constants.md

convolution.md

coordinates.md

cosmology.md

fits-io.md

index.md

modeling.md

nddata.md

samp.md

statistics.md

tables.md

time.md

timeseries.md

uncertainty.md

units-quantities.md

utils.md

visualization.md

wcs.md

tile.json