astroceleste_engine/lib.rs
1//! astroceleste-engine: astrological chart calculation on JPL ephemerides.
2//!
3//! One implementation shared by the Astroceleste server (Python bindings), desktop and
4//! mobile apps (native) and the web app (WASM), so every platform computes identical charts.
5//!
6//! **Experimental (0.0.x):** the API may change in any release.
7//!
8//! ```no_run
9//! use astroceleste_engine::ephemeris::{Kernel, KernelSet, Spk};
10//! use astroceleste_engine::{calculate_chart, ChartRequest, UtcInstant};
11//!
12//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
13//! // Kernels in preference order: the first one covering a date is used.
14//! let mut kernels = KernelSet::new();
15//! kernels.push(Kernel::new("de440s.bsp", Spk::open("kernels/de440s.bsp")?)?);
16//!
17//! let mut request = ChartRequest::new(UtcInstant::parse("1987-05-17T14:30:00Z")?, 41.9, 12.5);
18//! request.house_system = "P";
19//! request.zodiac_type = "sidereal";
20//! request.ayanamsa = "lahiri";
21//!
22//! let chart = calculate_chart(&kernels, &request)?;
23//! println!("{}", serde_json::to_string_pretty(&chart)?);
24//! # Ok(())
25//! # }
26//! ```
27//!
28//! # Entry points
29//!
30//! | Function | Result |
31//! |---|---|
32//! | [`calculate_chart`] | a natal or event [`Chart`]: planets, houses, aspects, fixed stars, lots, temperament, lunar status |
33//! | [`calculate_horary_chart`] | the chart plus [`HoraryData`]: planetary hours, significators, the Moon's aspects, strictures |
34//! | [`calculate_transit_chart`] | the sky at a moment and place, with its [`CrossAspect`]s to natal planets |
35//! | [`calculate_synastry`] | cross-aspects between two charts' planets (no kernel needed) |
36//! | [`calculate_derived_chart`] | a stored chart turned to a new first house (no kernel needed) |
37//!
38//! Every result implements [`serde::Serialize`] and serializes to the JSON of the
39//! Astroceleste API, with the same key order and the same integer vs float types. The
40//! Python and WebAssembly bindings return exactly that JSON.
41//!
42//! # Loading kernels
43//!
44//! Positions come from NASA JPL SPK kernels (`de440s.bsp` covers 1849–2150; DE441 covers
45//! 13200 BC–17191). A [`KernelSet`](ephemeris::KernelSet) holds them in preference order,
46//! and each date is computed with the first kernel that covers it. [`Spk::open`] reads a
47//! file. Where there is no file system (WebAssembly) or the kernel is bundled with an app,
48//! load the bytes and use [`Spk::from_bytes`]. [`Spk::excerpt`] cuts a smaller kernel for a
49//! date range, with positions unchanged inside it (1950–2050 of DE440s is about 11 MB).
50//!
51//! ```no_run
52//! use astroceleste_engine::ephemeris::{Kernel, KernelSet, Spk};
53//!
54//! # fn download(_: &str) -> Vec<u8> { Vec::new() }
55//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
56//! let bytes: Vec<u8> = download("https://example.com/de440s-1950-2050.bsp");
57//! let mut kernels = KernelSet::new();
58//! kernels.push(Kernel::new("de440s-1950-2050.bsp", Spk::from_bytes(bytes)?)?);
59//! assert!(kernels.coverage().is_some());
60//! # Ok(())
61//! # }
62//! ```
63//!
64//! # Errors
65//!
66//! Calculations return [`EngineError`], whose [`code`](EngineError::code) is the stable
67//! error code of the Astroceleste API. A date that no loaded kernel covers is
68//! [`EngineError::OutOfRange`]: the engine never extrapolates or approximates.
69//!
70//! ```no_run
71//! # use astroceleste_engine::ephemeris::KernelSet;
72//! # use astroceleste_engine::{calculate_chart, ChartRequest, EngineError, UtcInstant};
73//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
74//! # let kernels = KernelSet::new();
75//! let request = ChartRequest::new(UtcInstant::parse("1700-01-01T00:00:00Z")?, 41.9, 12.5);
76//! match calculate_chart(&kernels, &request) {
77//! Ok(chart) => println!("{} planets", chart.planets.len()),
78//! Err(EngineError::OutOfRange { jd, coverage }) => {
79//! eprintln!("JD {jd} is outside the loaded kernels ({coverage:?})")
80//! }
81//! Err(err) => eprintln!("{}: {err}", err.code()),
82//! }
83//! # Ok(())
84//! # }
85//! ```
86//!
87//! # Platforms
88//!
89//! The crate is pure Rust with no C code, and depends only on `serde` and `serde_json`.
90//! It builds for servers and desktops, `wasm32-unknown-unknown`, Android and iOS. Python
91//! (`pip install astroceleste-engine`) and JavaScript (`npm install astroceleste-engine`)
92//! bindings are published from the same repository.
93//!
94//! # Further reading
95//!
96//! - [API guide](https://github.com/ffalcinelli/astroceleste-engine/blob/main/docs/api.md):
97//! request options (house systems, ayanamsas, orb settings) and the chart JSON
98//! - [Ephemerides](https://github.com/ffalcinelli/astroceleste-engine/blob/main/docs/ephemerides.md):
99//! kernels, coverage and excerpts
100//! - [Accuracy](https://github.com/ffalcinelli/astroceleste-engine/blob/main/docs/accuracy.md):
101//! how results are verified against the reference implementation
102//! - [Live demo](https://ffalcinelli.github.io/astroceleste-engine/): this crate compiled
103//! to WebAssembly, computing charts in your browser
104//!
105//! [`Spk::open`]: ephemeris::Spk::open
106//! [`Spk::from_bytes`]: ephemeris::Spk::from_bytes
107//! [`Spk::excerpt`]: ephemeris::Spk::excerpt
108
109mod almanac;
110mod aspects;
111mod catalog;
112mod chart;
113mod chiron;
114mod constants;
115mod derived;
116pub mod ephemeris;
117mod error;
118mod fixed_stars;
119#[doc(hidden)] // exposed for the reduction tests; not part of the API
120pub mod frames;
121mod horary;
122mod houses;
123mod instant;
124mod lots;
125mod lunar;
126mod planets;
127mod pyfloat;
128mod symbolic;
129mod temperament;
130#[doc(hidden)] // exposed for the reduction tests; not part of the API
131pub mod time;
132mod zodiac;
133
134pub use aspects::{Aspect, CrossAspect};
135pub use catalog::LunarMansion;
136pub use chart::{calculate_chart, Chart, ChartRequest, HouseCusp, Placement};
137pub use derived::{
138 calculate_derived_chart, calculate_synastry, calculate_transit_chart, Synastry, TransitChart,
139};
140pub use error::EngineError;
141pub use fixed_stars::FixedStarPosition;
142pub use horary::{
143 calculate_horary_chart, ApplyingAspect, HoraryChart, HoraryData, MoonStatus, PlanetaryHours,
144 SeparatingAspect, Stricture,
145};
146pub use instant::{ParseError, UtcInstant};
147pub use lots::Lot;
148pub use lunar::LunarStatus;
149pub use temperament::{Factor, Qualities, Scores, Temperament};