astroceleste-engine 0.0.6

Astrological chart calculation engine on JPL ephemerides
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
//! A complete chart: positions, houses, aspects, fixed stars, lots, temperament and
//! lunar status, in the JSON shape of the reference's `calculate_chart_data`.

use serde::{Serialize, Serializer};
use serde_json::Value;

use crate::aspects::{natal_aspects, Aspect, OrbSettings, Point};
use crate::dignities::{
    antiscia, condition, is_diurnal, receptions, AntisciaContact, Condition, DignityScheme,
    Reception,
};
use crate::ephemeris::KernelSet;
use crate::error::EngineError;
use crate::fixed_stars::{fixed_stars, FixedStarPosition};
use crate::horary::{planetary_hours, PlanetaryHours};
use crate::houses::{calculate_houses, HouseSystem, Houses};
use crate::instant::UtcInstant;
use crate::lots::{arabic_parts, Lot};
use crate::lunar::{lunar_status, LunarStatus};
use crate::planets::calculate_planets;
use crate::symbolic::symbolic_degree_number;
use crate::temperament::{temperament, Temperament};
use crate::zodiac::{
    ayanamsa, ayanamsa_info, determine_house, longitude_to_zodiac, AyanamsaInfo, DEFAULT_AYANAMSA,
};

/// Every body a complete chart carries; missing ones are reported in
/// `unavailable_bodies` rather than silently dropped.
const EXPECTED_BODIES: [&str; 16] = [
    "Sun",
    "Moon",
    "Mercury",
    "Venus",
    "Mars",
    "Jupiter",
    "Saturn",
    "Uranus",
    "Neptune",
    "Pluto",
    "North Node",
    "South Node",
    "Chiron",
    "Lilith",
    "Ascendant",
    "Midheaven",
];

/// What to compute.
#[derive(Debug, Clone)]
pub struct ChartRequest<'a> {
    /// Moment of the chart.
    pub instant: UtcInstant,
    /// Geographic latitude in degrees, north positive.
    pub latitude: f64,
    /// Geographic longitude in degrees, east positive.
    pub longitude: f64,
    /// House system code, echoed as given; its first letter selects the system.
    pub house_system: &'a str,
    /// "tropical" or "sidereal" (anything else is tropical).
    pub zodiac_type: &'a str,
    /// Ayanamsa code for sidereal charts (unknown codes use the default).
    pub ayanamsa: &'a str,
    /// Caller orb settings, merged over the defaults.
    pub orb_settings: Option<&'a Value>,
    /// Doctrine of triplicities and bounds: "lilly" (default) or "dorothean".
    pub dignity_scheme: &'a str,
}

impl<'a> ChartRequest<'a> {
    /// A tropical Placidus request with default orbs.
    pub fn new(instant: UtcInstant, latitude: f64, longitude: f64) -> Self {
        ChartRequest {
            instant,
            latitude,
            longitude,
            house_system: "P",
            zodiac_type: "tropical",
            ayanamsa: DEFAULT_AYANAMSA,
            orb_settings: None,
            dignity_scheme: "lilly",
        }
    }
}

/// A planet, lunar point or angle placed in the chart.
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct Placement {
    /// Body or angle name, e.g. "Sun", "North Node", "Ascendant".
    pub name: &'static str,
    /// Glyph, e.g. "☉" ("ASC" and "MC" for the angles).
    pub symbol: &'static str,
    /// Zodiac sign name, e.g. "Taurus".
    pub sign: &'static str,
    /// Zodiac sign glyph, e.g. "♉".
    pub sign_symbol: &'static str,
    /// Whole degrees within the sign (0-29).
    pub degree: i64,
    /// Arc minutes past `degree` (0-59).
    pub minute: i64,
    /// Ecliptic longitude in degrees [0, 360), tropical or sidereal as requested.
    pub ecliptic_longitude: f64,
    /// House (1-12) the point falls in.
    pub house: u8,
    /// Apparent speed in ecliptic longitude, degrees per day.
    pub speed: f64,
    /// Whether the apparent motion is retrograde.
    pub is_retrograde: bool,
    /// Symbolic degree (1-30) within the sign, as used by degree symbolism.
    pub symbolic_degree: i64,
    /// Essential dignity, sect, phase to the Sun and speed: the seven planets only.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub condition: Option<Condition>,
}

/// The cusp of one house.
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct HouseCusp {
    /// House number (1-12).
    pub house_number: u8,
    /// Zodiac sign name, e.g. "Taurus".
    pub sign: &'static str,
    /// Zodiac sign glyph, e.g. "♉".
    pub sign_symbol: &'static str,
    /// Whole degrees within the sign (0-29).
    pub degree: i64,
    /// Arc minutes past `degree` (0-59).
    pub minute: i64,
    /// Ecliptic longitude in degrees [0, 360), tropical or sidereal as requested.
    pub ecliptic_longitude: f64,
    /// Symbolic degree (1-30) within the sign, as used by degree symbolism.
    pub symbolic_degree: i64,
}

/// A complete chart (`calculate_chart_data`); serializes to the API's JSON.
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct Chart {
    /// House system code, echoed from the request.
    pub house_system: String,
    /// "tropical" or "sidereal".
    pub zodiac_type: &'static str,
    /// Ayanamsa code; `None` for tropical charts.
    pub ayanamsa: Option<&'static str>,
    /// Human-readable ayanamsa name; `None` for tropical charts.
    pub ayanamsa_name: Option<&'static str>,
    /// Ayanamsa at the chart instant, in degrees; `None` for tropical charts.
    pub ayanamsa_value: Option<f64>,
    /// `ayanamsa_value` as degrees, minutes and seconds; `None` for tropical charts.
    pub ayanamsa_formatted: Option<String>,
    /// Mean precession rate used for the fixed stars, arcseconds per year.
    pub precession_rate_arcsec_yr: f64,
    /// Orb settings in effect: the defaults merged with the request's.
    pub orb_settings: Value,
    /// Planets, lunar points, Chiron, Lilith and the angles.
    pub planets: Vec<Placement>,
    /// Expected bodies that could not be computed (e.g. Chiron outside its table).
    pub unavailable_bodies: Vec<&'static str>,
    /// The twelve house cusps.
    pub houses: Vec<HouseCusp>,
    /// Aspects between chart points, then fixed-star conjunctions.
    pub aspects: Vec<Aspect>,
    /// Fixed stars conjunct a chart point.
    pub fixed_stars: Vec<FixedStarPosition>,
    /// The Arabic parts (lots).
    pub arabic_parts: Vec<Lot>,
    /// Temperament assessment.
    pub temperament: Temperament,
    /// Lunar phase and status; `None` (serialized as `{}`) without a Moon.
    #[serde(serialize_with = "empty_object_if_none")]
    pub lunar_status: Option<LunarStatus>,
    /// "diurnal" (the Sun above the horizon) or "nocturnal"; `None` without Sun and Ascendant.
    pub sect: Option<&'static str>,
    /// Doctrine of triplicities and bounds used for `condition` and `receptions`.
    pub dignity_scheme: &'static str,
    /// Receptions among the seven planets.
    pub receptions: Vec<Reception>,
    /// Antiscia and contra-antiscia among the seven planets and the angles.
    pub antiscia: Vec<AntisciaContact>,
    /// Planetary day and hour; absent from transit skies.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub planetary_hours: Option<PlanetaryHours>,
}

fn empty_object_if_none<S: Serializer>(v: &Option<LunarStatus>, s: S) -> Result<S::Ok, S::Error> {
    match v {
        Some(status) => status.serialize(s),
        None => serde_json::Map::new().serialize(s),
    }
}

impl Chart {
    /// The chart's planets and angles as named ecliptic points.
    pub fn points(&self) -> Vec<Point<'_>> {
        self.planets
            .iter()
            .map(|p| Point {
                name: p.name,
                longitude: p.ecliptic_longitude,
            })
            .collect()
    }
}

fn symbol_of(name: &str) -> &'static str {
    match name {
        "Sun" => "☉",
        "Moon" => "☽",
        "Mercury" => "☿",
        "Venus" => "♀",
        "Mars" => "♂",
        "Jupiter" => "♃",
        "Saturn" => "♄",
        "Uranus" => "â™…",
        "Neptune" => "♆",
        "Pluto" => "♇",
        "North Node" => "☊",
        "South Node" => "☋",
        "Chiron" => "âš·",
        "Lilith" => "⚸",
        "Ascendant" => "ASC",
        "Midheaven" => "MC",
        _ => "",
    }
}

/// The positions part of a chart: placements and house cusps, without aspects, fixed
/// stars, lots or temperament. Electional searches evaluate thousands of these.
pub(crate) struct Sky {
    pub zodiac_type: &'static str,
    pub is_sidereal: bool,
    pub ayanamsa_code: &'static str,
    pub info: AyanamsaInfo,
    pub jd: f64,
    /// Cusps 1-12, degrees.
    pub cusps: Vec<f64>,
    pub house_cusps: Vec<HouseCusp>,
    pub planets: Vec<Placement>,
}

/// Placements and houses of a request (the first half of [`calculate_chart`]).
pub(crate) fn sky(kernels: &KernelSet, req: &ChartRequest) -> Result<Sky, EngineError> {
    let zodiac_type = match req.zodiac_type.trim().to_lowercase().as_str() {
        "sidereal" => "sidereal",
        _ => "tropical",
    };
    let is_sidereal = zodiac_type == "sidereal";
    let ayanamsa_code = ayanamsa(req.ayanamsa).code;

    let jd = req.instant.julian_day();
    let info = ayanamsa_info(jd, ayanamsa_code);
    let shift = if is_sidereal { info.value } else { 0.0 };

    let engine = calculate_planets(kernels, jd, shift)?;
    let houses = calculate_houses(
        jd,
        req.latitude,
        req.longitude,
        HouseSystem::from_code(req.house_system),
        shift,
    );
    let cusps = houses.cusps.to_vec();

    let house_cusps: Vec<HouseCusp> = houses
        .cusps
        .iter()
        .enumerate()
        .map(|(i, &lon)| {
            let z = longitude_to_zodiac(lon);
            HouseCusp {
                house_number: i as u8 + 1,
                sign: z.sign.name,
                sign_symbol: z.sign.symbol,
                degree: z.degree,
                minute: z.minute,
                ecliptic_longitude: lon,
                symbolic_degree: symbolic_degree_number(z.degree, z.minute),
            }
        })
        .collect();

    let raw: Vec<(&'static str, f64, f64, bool)> = engine
        .bodies
        .iter()
        .map(|b| (b.name, b.longitude, b.speed, b.is_retrograde))
        .collect();
    let planets = placements(raw, &houses);

    Ok(Sky {
        zodiac_type,
        is_sidereal,
        ayanamsa_code,
        info,
        jd,
        cusps,
        house_cusps,
        planets,
    })
}

/// Placements of bodies given as (name, longitude, speed, retrograde), followed by the
/// Ascendant and the Midheaven, with their houses.
pub(crate) fn placements(
    mut raw: Vec<(&'static str, f64, f64, bool)>,
    houses: &Houses,
) -> Vec<Placement> {
    raw.push(("Ascendant", houses.ascendant, 0.0, false));
    raw.push(("Midheaven", houses.midheaven, 0.0, false));
    raw.iter()
        .map(|&(name, lon, speed, is_retrograde)| {
            let z = longitude_to_zodiac(lon);
            Placement {
                name,
                symbol: symbol_of(name),
                sign: z.sign.name,
                sign_symbol: z.sign.symbol,
                degree: z.degree,
                minute: z.minute,
                ecliptic_longitude: lon,
                house: determine_house(lon, &houses.cusps),
                speed,
                is_retrograde,
                symbolic_degree: symbolic_degree_number(z.degree, z.minute),
                condition: None,
            }
        })
        .collect()
}

/// Set the condition of the seven planets, which needs the Sun and the Ascendant.
fn add_conditions(planets: &mut [Placement], scheme: DignityScheme) -> Option<&'static str> {
    let at = |name: &str| {
        planets
            .iter()
            .find(|p| p.name == name)
            .map(|p| p.ecliptic_longitude)
    };
    let (sun, asc) = (at("Sun")?, at("Ascendant")?);
    for planet in planets.iter_mut() {
        planet.condition = condition(planet, sun, asc, scheme);
    }
    Some(if is_diurnal(sun, asc) {
        "diurnal"
    } else {
        "nocturnal"
    })
}

/// Compute a chart (`calculate_chart_data`), with its planetary day and hour.
pub fn calculate_chart(kernels: &KernelSet, req: &ChartRequest) -> Result<Chart, EngineError> {
    let mut chart = chart_without_hours(kernels, req)?;
    chart.planetary_hours = Some(planetary_hours(
        kernels,
        req.instant,
        req.latitude,
        req.longitude,
    ));
    Ok(chart)
}

/// A chart without its planetary hours, which cost a sunrise search: for transit skies,
/// and for the callers that compute the hours themselves.
pub(crate) fn chart_without_hours(
    kernels: &KernelSet,
    req: &ChartRequest,
) -> Result<Chart, EngineError> {
    let orbs = OrbSettings::merge(req.orb_settings)?;
    let scheme = DignityScheme::from_code(req.dignity_scheme);
    let Sky {
        zodiac_type,
        is_sidereal,
        ayanamsa_code,
        info,
        jd,
        cusps,
        house_cusps,
        mut planets,
    } = sky(kernels, req)?;
    let sect = add_conditions(&mut planets, scheme);
    let points: Vec<Point> = planets
        .iter()
        .map(|p| Point {
            name: p.name,
            longitude: p.ecliptic_longitude,
        })
        .collect();

    let mut aspects = natal_aspects(&points, &orbs)?;
    let (stars, star_aspects) = fixed_stars(
        jd,
        &cusps,
        &points,
        orbs.fixed_star_orb()?,
        is_sidereal.then_some(info.value),
    );
    aspects.extend(star_aspects);

    let lots = arabic_parts(&points, &cusps);
    let temperament = temperament(&planets, &aspects);
    let lunar = lunar_status(&planets);
    let chart_receptions = receptions(&planets, &aspects, scheme);
    let chart_antiscia = antiscia(&planets);

    let mut unavailable: Vec<&'static str> = EXPECTED_BODIES
        .iter()
        .copied()
        .filter(|name| !planets.iter().any(|p| p.name == *name))
        .collect();
    unavailable.sort_unstable();

    Ok(Chart {
        house_system: req.house_system.to_string(),
        zodiac_type,
        ayanamsa: is_sidereal.then_some(ayanamsa_code),
        ayanamsa_name: is_sidereal.then_some(info.name),
        ayanamsa_value: is_sidereal.then_some(info.value),
        ayanamsa_formatted: is_sidereal.then(|| info.formatted.clone()),
        precession_rate_arcsec_yr: info.precession_rate_arcsec_yr,
        orb_settings: orbs.to_value(),
        planets,
        unavailable_bodies: unavailable,
        houses: house_cusps,
        aspects,
        fixed_stars: stars,
        arabic_parts: lots,
        temperament,
        lunar_status: lunar,
        sect,
        dignity_scheme: scheme.code(),
        receptions: chart_receptions,
        antiscia: chart_antiscia,
        planetary_hours: None,
    })
}