A powerful, dataset-agnostic Python library for astronomical data acquisition with built-in caching, rate limiting, async support, and progress tracking
Project description
๐ NexusCosmos
A powerful, dataset-agnostic Python library for astronomical data acquisition.
NexusCosmos provides a unified interface to fetch astronomical data from any API source โ whether it's NASA JPL Horizons, Minor Planet Center, ESA GAIA, or your own custom endpoints. Built with modern Python best practices, it handles all the complex infrastructure so you can focus on the science.
โจ Key Features
| Feature | Description |
|---|---|
| ๐ Any Data Source | Works with any REST API - just provide the URL |
| โก Async Support | Fetch 100+ objects in parallel (3-10x faster) |
| ๐ Progress Tracking | Visual progress bars for batch operations |
| ๐พ Smart Caching | File-based TTL cache reduces redundant API calls |
| ๐ฆ Rate Limiting | Built-in rate limiter respects API limits |
| ๐ Auto Retry | Exponential backoff handles transient failures |
| ๐ก๏ธ Error Recovery | Falls back to cached data when APIs are unavailable |
| ๐ Request Inspection | Debug exactly what's being sent/received |
| ๐ค Data Export | Export to JSON, CSV, pandas DataFrame, Astropy Table |
๐ฆ Installation
# Basic installation
pip install nexuscosmos
# With optional dependencies
pip install nexuscosmos[async] # For async support (aiohttp)
pip install nexuscosmos[progress] # For progress bars (tqdm)
pip install nexuscosmos[pandas] # For DataFrame export
pip install nexuscosmos[astropy] # For Astropy Table export
pip install nexuscosmos[all] # Everything
๐ Quick Start
Basic Usage (Any API)
from nexuscosmos import GenericAcquisitionClient, QueryConfig
# Connect to ANY astronomical API
client = GenericAcquisitionClient(
base_url="https://api.example.com/data",
show_progress=True, # Show progress bars
debug=True # Enable debug logging
)
# Configure your query
config = QueryConfig(
object_id="asteroid_123",
start_time="2025-01-01",
stop_time="2025-01-31",
step_size="1d"
)
# Fetch data
result = client.fetch_data(config)
if result['success']:
print(f"Data: {result['data']}")
Batch Operations (Multiple Objects)
# Fetch data for multiple objects at once
configs = [
QueryConfig(object_id=f"object_{i}", start_time="2025-01-01", stop_time="2025-01-31")
for i in range(50)
]
# Sequential with progress bar
results = client.fetch_batch(configs, show_progress=True)
# Output: Fetching data: 100%|โโโโโโโโโโโโ| 50/50 [00:30<00:00, 1.67obj/s]
Async Operations (3-10x Faster)
import asyncio
from nexuscosmos import AsyncGenericAcquisitionClient, QueryConfig
async def fetch_many():
client = AsyncGenericAcquisitionClient(
base_url="https://api.example.com/data",
max_concurrent=10, # 10 parallel requests
show_progress=True
)
configs = [
QueryConfig(object_id=f"object_{i}", start_time="2025-01-01", stop_time="2025-01-31")
for i in range(100)
]
# Fetch all in parallel - MUCH faster!
results = await client.fetch_data_async_batch(configs)
return results
results = asyncio.run(fetch_many())
print(f"Fetched {len(results)} objects")
๐ญ Using with JPL Horizons
NexusCosmos includes a ready-to-use client for NASA JPL Horizons:
from nexuscosmos import HorizonsClient, QueryConfig
# Pre-configured for JPL Horizons API
client = HorizonsClient()
# Quick presets for common queries
config = QueryConfig.quick_ephemeris(object_id="1I") # 1I/'Oumuamua
result = client.fetch_data(config)
# Supports interstellar objects with friendly names
# "1I", "Oumuamua", "2I/Borisov", "3I/ATLAS" all work!
๐พ Data Export
from nexuscosmos import DataExporter
# After fetching data...
result = client.fetch_data(config)
exporter = DataExporter(result['data'])
# Export to various formats
json_str = exporter.to_json(indent=2)
csv_str = exporter.to_csv()
df = exporter.to_dataframe() # Requires pandas
table = exporter.to_astropy_table() # Requires astropy
# Save directly to files
exporter.save_json("output.json")
exporter.save_csv("output.csv")
๐ง Advanced Configuration
Client Options
client = GenericAcquisitionClient(
base_url="https://api.example.com",
# Rate Limiting
enforce_rate_limit=True,
max_requests_per_minute=60,
# Caching
cache=FileCache(cache_dir="./cache", ttl_seconds=86400), # 24hr cache
# Resilience
fallback_to_cache=True, # Use stale cache if API fails
timeout_seconds=30,
# Debugging
debug=True,
show_progress=True
)
Request Inspection
# After a request, inspect what happened
result = client.fetch_data(config)
print(f"URL called: {client.last_request_url}")
print(f"Parameters: {client.last_request_params}")
print(f"Status code: {client.last_response_status}")
print(f"Response time: {client.last_response_time:.2f}s")
print(f"Error (if any): {client.last_error}")
Custom Rate Limiter & Backoff
from nexuscosmos import RateLimiter, ExponentialBackoff, FileCache
client = GenericAcquisitionClient(
base_url="https://api.example.com",
rate_limiter=RateLimiter(max_attempts=100, window_minutes=1),
backoff=ExponentialBackoff(base_delay=2.0, max_delay=120.0, multiplier=3.0),
cache=FileCache(cache_dir="./my_cache", ttl_seconds=3600)
)
๐๏ธ Extending for Custom APIs
Create your own client by extending the base class:
from nexuscosmos import BaseAcquisitionClient, BaseTextParser
class MySpaceAgencyClient(BaseAcquisitionClient):
"""Client for My Space Agency's API."""
BASE_URL = "https://api.myspaceagency.org/v2"
def __init__(self, api_key: str, **kwargs):
super().__init__(**kwargs)
self.api_key = api_key
self.base_url = self.BASE_URL
def fetch_data(self, query_config):
# Your custom implementation
params = {
"target": query_config.object_id,
"start": query_config.start_time,
"end": query_config.stop_time,
"api_key": self.api_key
}
# ... fetch and parse
๐ Data Models
EphemerisData
Flexible container for astronomical observations:
from nexuscosmos import EphemerisData
ephem = EphemerisData(
datetime_str="2025-01-15 12:00:00",
RA=45.5, # Right Ascension (degrees)
DEC=30.2, # Declination (degrees)
delta=1.5, # Distance from observer (AU)
r=1.2, # Heliocentric distance (AU)
V_mag=15.5, # Visual magnitude
# ... many more optional fields
)
OrbitalElements
Container for orbital parameters:
from nexuscosmos import OrbitalElements
orbit = OrbitalElements(
epoch="2025-01-15",
a=2.5, # Semi-major axis (AU)
e=0.95, # Eccentricity
i=135.0, # Inclination (degrees)
# ...
)
# Automatic orbit type detection
orbit.is_elliptical() # True if e < 1
orbit.is_hyperbolic() # True if e > 1
orbit.is_parabolic() # True if e โ 1
๐งช Testing
# Run tests
pytest tests/ -v
# With coverage
pytest tests/ --cov=nexuscosmos --cov-report=html
๐ Project Structure
nexuscosmos/
โโโ __init__.py # Package exports
โโโ acquisition_base.py # Generic & Async clients
โโโ config.py # QueryConfig, ClientConfig
โโโ models.py # EphemerisData, OrbitalElements
โโโ cache.py # FileCache with TTL
โโโ utils.py # RateLimiter, ExponentialBackoff
โโโ export.py # DataExporter (JSON, CSV, DataFrame)
โโโ parsers_base.py # Base parser classes
โโโ datasets/
โโโ horizons/ # JPL Horizons implementation
โโโ client.py # HorizonsClient
โโโ parser.py # HorizonsParser
๐ค Contributing
Contributions are welcome! Areas we'd love help with:
- New Dataset Clients: Minor Planet Center, ESA GAIA, etc.
- Documentation: Tutorials, examples, API docs
- Testing: More test coverage
- Features: See Issues
๐ License
MIT License - see LICENSE for details.
๐ Acknowledgments
- NASA JPL Horizons for their excellent ephemeris API
- The Python astronomy community
- All contributors and users
Made with โค๏ธ for the astronomy community
"The cosmos is within us. We are made of star-stuff." โ Carl Sagan
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file nexuscosmos-0.2.0.tar.gz.
File metadata
- Download URL: nexuscosmos-0.2.0.tar.gz
- Upload date:
- Size: 35.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5e3055ddb07975f011e3668e26904cdc1a3d2b1a8925f0e673c23a9083e7883a
|
|
| MD5 |
48e41dcafa030b1616f06f785815694a
|
|
| BLAKE2b-256 |
15e338bb8ec434fd37aa657743b68027ba79a6ad77f3b3f55c1bed972f818314
|
File details
Details for the file nexuscosmos-0.2.0-py3-none-any.whl.
File metadata
- Download URL: nexuscosmos-0.2.0-py3-none-any.whl
- Upload date:
- Size: 35.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0fe7b8d6531f290e1d385c5c3d003b31abed5cea5099a03dc0df65d7a72efdf4
|
|
| MD5 |
ecc9c04d876a043c9fa7f6f3c93ca715
|
|
| BLAKE2b-256 |
db236c82fe76bffa564bc06139ac94648286a07ad9be5dbbe862ea214b449f5e
|