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//! | [`calculate_election_chart`] | the chart plus [`ElectionData`]: an electional score with the rules that apply |
38//! | [`search_elections`] | the best [`ElectionWindow`]s over a span of time at a place |
39//!
40//! Every result implements [`serde::Serialize`] and serializes to the JSON of the
41//! Astroceleste API, with the same key order and the same integer vs float types. The
42//! Python and WebAssembly bindings return exactly that JSON.
43//!
44//! # Loading kernels
45//!
46//! Positions come from NASA JPL SPK kernels (`de440s.bsp` covers 1849–2150; DE441 covers
47//! 13200 BC–17191). A [`KernelSet`](ephemeris::KernelSet) holds them in preference order,
48//! and each date is computed with the first kernel that covers it. [`Spk::open`] reads a
49//! file. Where there is no file system (WebAssembly) or the kernel is bundled with an app,
50//! load the bytes and use [`Spk::from_bytes`]. [`Spk::excerpt`] cuts a smaller kernel for a
51//! date range, with positions unchanged inside it (1950–2050 of DE440s is about 11 MB).
52//!
53//! ```no_run
54//! use astroceleste_engine::ephemeris::{Kernel, KernelSet, Spk};
55//!
56//! # fn download(_: &str) -> Vec<u8> { Vec::new() }
57//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
58//! let bytes: Vec<u8> = download("https://example.com/de440s-1950-2050.bsp");
59//! let mut kernels = KernelSet::new();
60//! kernels.push(Kernel::new("de440s-1950-2050.bsp", Spk::from_bytes(bytes)?)?);
61//! assert!(kernels.coverage().is_some());
62//! # Ok(())
63//! # }
64//! ```
65//!
66//! # Errors
67//!
68//! Calculations return [`EngineError`], whose [`code`](EngineError::code) is the stable
69//! error code of the Astroceleste API. A date that no loaded kernel covers is
70//! [`EngineError::OutOfRange`]: the engine never extrapolates or approximates.
71//!
72//! ```no_run
73//! # use astroceleste_engine::ephemeris::KernelSet;
74//! # use astroceleste_engine::{calculate_chart, ChartRequest, EngineError, UtcInstant};
75//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
76//! # let kernels = KernelSet::new();
77//! let request = ChartRequest::new(UtcInstant::parse("1700-01-01T00:00:00Z")?, 41.9, 12.5);
78//! match calculate_chart(&kernels, &request) {
79//! Ok(chart) => println!("{} planets", chart.planets.len()),
80//! Err(EngineError::OutOfRange { jd, coverage }) => {
81//! eprintln!("JD {jd} is outside the loaded kernels ({coverage:?})")
82//! }
83//! Err(err) => eprintln!("{}: {err}", err.code()),
84//! }
85//! # Ok(())
86//! # }
87//! ```
88//!
89//! # Platforms
90//!
91//! The crate is pure Rust with no C code, and depends only on `serde` and `serde_json`.
92//! It builds for servers and desktops, `wasm32-unknown-unknown`, Android and iOS. Python
93//! (`pip install astroceleste-engine`) and JavaScript (`npm install astroceleste-engine`)
94//! bindings are published from the same repository.
95//!
96//! # Further reading
97//!
98//! - [API guide](https://github.com/ffalcinelli/astroceleste-engine/blob/main/docs/api.md):
99//! request options (house systems, ayanamsas, orb settings) and the chart JSON
100//! - [Ephemerides](https://github.com/ffalcinelli/astroceleste-engine/blob/main/docs/ephemerides.md):
101//! kernels, coverage and excerpts
102//! - [Accuracy](https://github.com/ffalcinelli/astroceleste-engine/blob/main/docs/accuracy.md):
103//! how results are verified against the reference implementation
104//! - [Live demo](https://ffalcinelli.github.io/astroceleste-engine/): this crate compiled
105//! to WebAssembly, computing charts in your browser
106//!
107//! [`Spk::open`]: ephemeris::Spk::open
108//! [`Spk::from_bytes`]: ephemeris::Spk::from_bytes
109//! [`Spk::excerpt`]: ephemeris::Spk::excerpt
110
111mod almanac;
112mod aspects;
113mod catalog;
114mod chart;
115mod chiron;
116mod constants;
117mod derived;
118mod election;
119pub mod ephemeris;
120mod error;
121mod fixed_stars;
122#[doc(hidden)] // exposed for the reduction tests; not part of the API
123pub mod frames;
124mod horary;
125mod houses;
126mod instant;
127mod lots;
128mod lunar;
129mod planets;
130mod pyfloat;
131mod symbolic;
132mod temperament;
133#[doc(hidden)] // exposed for the reduction tests; not part of the API
134pub mod time;
135mod zodiac;
136
137pub use aspects::{Aspect, CrossAspect};
138pub use catalog::LunarMansion;
139pub use chart::{calculate_chart, Chart, ChartRequest, HouseCusp, Placement};
140pub use derived::{
141 calculate_derived_chart, calculate_synastry, calculate_transit_chart, Synastry, TransitChart,
142};
143pub use election::{
144 calculate_election_chart, search_elections, CriteriaSummary, ElectionChart, ElectionCriteria,
145 ElectionData, ElectionFactor, ElectionSearch, ElectionWindow, Exclusions, HourRange,
146 NatalPoint, Purpose, UtcOffset, MAX_SEARCH_DAYS,
147};
148pub use error::EngineError;
149pub use fixed_stars::FixedStarPosition;
150pub use horary::{
151 calculate_horary_chart, ApplyingAspect, HoraryChart, HoraryData, MoonStatus, PlanetaryHours,
152 SeparatingAspect, Stricture,
153};
154pub use instant::{ParseError, UtcInstant};
155pub use lots::Lot;
156pub use lunar::LunarStatus;
157pub use temperament::{Factor, Qualities, Scores, Temperament};