Files
sorsfonalak/crates/astro-engine/src/engine.rs
T

133 lines
4.1 KiB
Rust
Raw Normal View History

//! The replaceable astronomical backend.
//!
//! Everything the chart logic needs from an ephemeris is behind the
//! [`Ephemeris`] trait. A new engine (e.g. one based on JPL DE440 in pure Rust)
//! only has to implement this trait.
use serde::Serialize;
use crate::types::{Ayanamsa, Body, HouseSystem};
#[derive(Debug, thiserror::Error)]
pub enum EngineError {
#[error("{0}")]
Calculation(String),
#[error("ephemeris engine is not available: {0}")]
Unavailable(String),
}
pub type Result<T> = std::result::Result<T, EngineError>;
/// Geographic observer position.
#[derive(Debug, Clone, Copy, PartialEq, Serialize)]
pub struct GeoPosition {
/// Degrees, north positive.
pub latitude: f64,
/// Degrees, east positive.
pub longitude: f64,
/// Meters above sea level.
pub elevation: f64,
}
/// Reference frame of ecliptic positions.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Frame {
/// `None` = tropical zodiac, `Some` = sidereal zodiac with this ayanamsa.
pub sidereal: Option<Ayanamsa>,
/// `Some` = topocentric positions for this observer, `None` = geocentric.
pub topocentric: Option<GeoPosition>,
}
impl Frame {
pub const TROPICAL_GEOCENTRIC: Frame = Frame { sidereal: None, topocentric: None };
}
/// Position and motion of a body.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct BodyState {
/// Ecliptic longitude, degrees `[0, 360)`.
pub longitude: f64,
/// Ecliptic latitude, degrees.
pub latitude: f64,
/// Distance in AU.
pub distance: f64,
/// Daily motion in longitude, degrees/day (negative = retrograde).
pub speed_longitude: f64,
pub speed_latitude: f64,
pub speed_distance: f64,
/// Right ascension (equator of date), degrees.
pub right_ascension: f64,
/// Declination (equator of date), degrees.
pub declination: f64,
/// The engine had to fall back to a less precise method (e.g. a data file
/// does not cover the date).
pub degraded_precision: bool,
}
/// House cusps and angles.
#[derive(Debug, Clone, PartialEq)]
pub struct HouseResult {
pub requested: HouseSystem,
/// System actually used (differs from `requested` when the requested one is
/// undefined at the given latitude and the engine fell back).
pub used: HouseSystem,
pub fallback_reason: Option<String>,
/// Cusps of houses 1..=12, degrees.
pub cusps: [f64; 12],
pub ascendant: f64,
pub midheaven: f64,
/// Right ascension of the MC (sidereal time in degrees).
pub armc: f64,
pub vertex: f64,
pub equatorial_ascendant: f64,
pub co_ascendant_koch: f64,
pub co_ascendant_munkasey: f64,
pub polar_ascendant: f64,
/// Daily motion of the ascendant / MC / vertex, degrees/day.
pub ascendant_speed: f64,
pub midheaven_speed: f64,
pub vertex_speed: f64,
}
#[derive(Debug, Clone, Copy, PartialEq, Serialize)]
pub struct Obliquity {
/// True obliquity of the ecliptic (with nutation), degrees.
pub true_obliquity: f64,
pub mean_obliquity: f64,
/// Nutation in longitude, degrees.
pub nutation_longitude: f64,
/// Nutation in obliquity, degrees.
pub nutation_obliquity: f64,
}
#[derive(Debug, Clone, Serialize)]
pub struct EphemerisInfo {
pub name: String,
pub version: String,
/// Years covered with full precision (data files), if limited.
pub precise_years: Option<(i32, i32)>,
pub license: String,
}
pub trait Ephemeris: Send + Sync {
fn info(&self) -> EphemerisInfo;
/// Delta T (TT - UT) in days.
fn delta_t(&self, jd_ut: f64) -> f64;
/// Greenwich apparent sidereal time, hours.
fn sidereal_time(&self, jd_ut: f64) -> f64;
fn obliquity(&self, jd_ut: f64) -> Result<Obliquity>;
fn position(&self, body: Body, jd_ut: f64, frame: &Frame) -> Result<BodyState>;
fn houses(&self, jd_ut: f64, observer: &GeoPosition, system: HouseSystem, frame: &Frame) -> Result<HouseResult>;
/// Illuminated fraction of the body's disc (0..1).
fn illumination(&self, body: Body, jd_ut: f64) -> Result<f64>;
/// Ayanamsa value in degrees.
fn ayanamsa(&self, jd_ut: f64, ayanamsa: Ayanamsa) -> Result<f64>;
}