Skip to main content

A library to embed a SQLite database within a FITS file.

Project description

fits-sqlite

A Python library for embedding a portable SQLite database inside a FITS (Flexible Image Transport System) file.

Core Concept

fits-sqlite provides a mechanism to create "Self-Contained Experiment Files" where raw scientific data (the FITS image) and its analysis history (a relational SQLite DB) travel together. This is ideal for sharing data and analysis results without requiring a connection to a central database server.

The library does not store redundant pixel data. Instead, the embedded database stores metadata, coordinates, and analysis results, treating the FITS file as the single source of truth for image data.

Installation

From Source (Current)

To install the library directly from the repository:

git clone https://github.com/coredumped/fits_sqlite.git
cd fits_sqlite
pip install .

From PyPI

(Coming soon)

pip install fits-sqlite

Usage

The library provides a high-level Python context manager that mimics the standard sqlite3 interface, abstracting away the underlying FITS file structure. It uses a temporary directory to safely manage the database and any associated sidecar files (like WAL or journals), ensuring clean cleanup after use.

Creating a new FITS file with an embedded database

import fits_sqlite
import os

FITS_FILE = 'exposure_01.fits'

# Define a schema for your analysis data
TABLE_SCHEMA = """
CREATE TABLE clusters (
    cluster_id INT PRIMARY KEY,
    x INT,
    y INT,
    width INT,
    height INT,
    energy REAL,
    class_score REAL
);
"""

# Sample data
TEST_DATA = [
    (1, 100, 150, 20, 20, 512.5, 0.95),
    (2, 300, 400, 25, 25, 1024.0, 0.88),
]

# The context manager handles creating the file if it doesn't exist
with fits_sqlite.connect(FITS_FILE) as conn:
    cursor = conn.cursor()
    print("Creating table 'clusters'...")
    cursor.execute(TABLE_SCHEMA)
    
    print(f"Inserting {len(TEST_DATA)} rows...")
    cursor.executemany("INSERT INTO clusters VALUES (?, ?, ?, ?, ?, ?, ?);", TEST_DATA)

print(f"Database created and embedded in {FITS_FILE}")

Reading from an existing FITS file

import fits_sqlite

FITS_FILE = 'exposure_01.fits'

with fits_sqlite.connect(FITS_FILE) as conn:
    cursor = conn.cursor()
    
    print("Querying for clusters with energy > 900...")
    cursor.execute("SELECT * FROM clusters WHERE energy > 900 ORDER BY cluster_id")
    results = cursor.fetchall()
    
    print(f"Found {len(results)} matching rows:")
    for row in results:
        print(row)

Using a Custom HDU Name

By default, the database is stored in a binary table HDU named EMBEDDED_DB. You can specify a custom HDU name if needed:

with fits_sqlite.connect('my_data.fits', hdu_name='MY_CUSTOM_DB') as conn:
    # The database will be stored in/read from the 'MY_CUSTOM_DB' HDU
    pass

Examples

The examples/ directory contains a complete sample script basic_example.py that demonstrates a real-world workflow:

  1. Downloads a sample astronomical image (HorseHead.fits).
  2. Calculates image statistics (mean, stddev, max) using Astropy.
  3. Creates an embedded SQLite table to store this analysis history.
  4. Saves the stats and queries them back.

To run the example:

python3 examples/basic_example.py

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fits_sqlite-0.1.0.tar.gz (7.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fits_sqlite-0.1.0-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

Details for the file fits_sqlite-0.1.0.tar.gz.

File metadata

  • Download URL: fits_sqlite-0.1.0.tar.gz
  • Upload date:
  • Size: 7.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for fits_sqlite-0.1.0.tar.gz
Algorithm Hash digest
SHA256 c8e9ceaf9561e6f05dbeeebdb3c3cde990205e75ee0522b2b89752596aac39bd
MD5 85829f3b7cd28bf5c44d8babda87d2e8
BLAKE2b-256 b1e37b132ad659ae2d2b3ebd33551ffaff81dc0da336aafab4408f70152be7b0

See more details on using hashes here.

Provenance

The following attestation bundles were made for fits_sqlite-0.1.0.tar.gz:

Publisher: publish.yml on coredumped/fits_sqlite

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fits_sqlite-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: fits_sqlite-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for fits_sqlite-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 63349e4a04f2da9c481a3d4d3348cd5c8171578dde295287802d38e0c52c32c7
MD5 2aa17e08812dfa90c5f52f1a3ecb7984
BLAKE2b-256 f91ff105c7f9c7e7a67bdfeabc0816636e9b6b61bc53d86e8c62bc9d80c28fe1

See more details on using hashes here.

Provenance

The following attestation bundles were made for fits_sqlite-0.1.0-py3-none-any.whl:

Publisher: publish.yml on coredumped/fits_sqlite

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page