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