astroceleste-engine 0.0.4

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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
//! Aspects, orbs and cross-aspects (`charts/calc/aspects.py`).

use serde::Serialize;
use serde_json::{json, Map, Value};

use crate::error::EngineError;
use crate::pyfloat;

#[derive(Debug, Clone, Copy, PartialEq)]
pub struct AspectRule {
    pub name: &'static str,
    pub angle: f64,
    pub symbol: &'static str,
    pub orb: f64,
    pub is_major: bool,
}

pub const ASPECT_RULES: [AspectRule; 7] = [
    AspectRule {
        name: "Conjunction",
        angle: 0.0,
        symbol: "☌",
        orb: 8.0,
        is_major: true,
    },
    AspectRule {
        name: "Sextile",
        angle: 60.0,
        symbol: "⚹",
        orb: 6.0,
        is_major: true,
    },
    AspectRule {
        name: "Square",
        angle: 90.0,
        symbol: "□",
        orb: 7.0,
        is_major: true,
    },
    AspectRule {
        name: "Trine",
        angle: 120.0,
        symbol: "△",
        orb: 8.0,
        is_major: true,
    },
    AspectRule {
        name: "Opposition",
        angle: 180.0,
        symbol: "☍",
        orb: 8.0,
        is_major: true,
    },
    AspectRule {
        name: "Semi-Sextile",
        angle: 30.0,
        symbol: "⚺",
        orb: 2.5,
        is_major: false,
    },
    AspectRule {
        name: "Quincunx",
        angle: 150.0,
        symbol: "⚻",
        orb: 3.0,
        is_major: false,
    },
];

const ASPECT_WEIGHT_FACTORS: [(&str, f64); 7] = [
    ("Conjunction", 1.0),
    ("Opposition", 1.0),
    ("Trine", 1.0),
    ("Square", 0.875),
    ("Sextile", 0.75),
    ("Semi-Sextile", 0.35),
    ("Quincunx", 0.4),
];

/// `DEFAULT_ORB_SETTINGS`, in the reference's key order.
pub fn default_orb_settings() -> Map<String, Value> {
    let value = json!({
        "method": "moiety_hybrid",
        "include_minor_aspects": false,
        "include_chiron": false,
        "include_lilith": false,
        "aspect_orbs": {
            "Conjunction": 8.0,
            "Sextile": 6.0,
            "Square": 7.0,
            "Trine": 8.0,
            "Opposition": 8.0,
            "Semi-Sextile": 2.5,
            "Quincunx": 3.0,
        },
        "planet_orbs": {
            "Sun": 15.0,
            "Moon": 12.0,
            "Mercury": 7.0,
            "Venus": 7.0,
            "Mars": 8.0,
            "Jupiter": 9.0,
            "Saturn": 9.0,
            "Uranus": 5.0,
            "Neptune": 5.0,
            "Pluto": 5.0,
            "North Node": 5.0,
            "South Node": 5.0,
            "Chiron": 5.0,
            "Lilith": 5.0,
            "Ascendant": 5.0,
            "Midheaven": 5.0,
        },
        "fixed_star_orb": 1.5,
    });
    match value {
        Value::Object(map) => map,
        _ => unreachable!(),
    }
}

/// Python `float(value)` for JSON values: numbers, numeric strings and booleans.
pub fn py_float(value: &Value) -> Result<f64, EngineError> {
    match value {
        Value::Number(n) => n
            .as_f64()
            .ok_or_else(|| EngineError::InvalidInput(format!("not a number: {n}"))),
        Value::Bool(b) => Ok(f64::from(u8::from(*b))),
        Value::String(s) => {
            let t = s.trim().to_ascii_lowercase().replace('_', "");
            match t.as_str() {
                "inf" | "+inf" | "infinity" | "+infinity" => Ok(f64::INFINITY),
                "-inf" | "-infinity" => Ok(f64::NEG_INFINITY),
                "nan" | "+nan" | "-nan" => Ok(f64::NAN),
                _ => t
                    .parse()
                    .map_err(|_| EngineError::InvalidInput(format!("not a number: {s:?}"))),
            }
        }
        other => Err(EngineError::InvalidInput(format!("not a number: {other}"))),
    }
}

/// Orb settings merged over the defaults, as the orchestrator and cross-aspects do:
/// only `method`, `fixed_star_orb`, `aspect_orbs` and `planet_orbs` are taken from the
/// caller, the orb tables key by key. Values are kept as given (they are echoed back in
/// the chart) and read with Python `float()` semantics when used.
#[derive(Debug, Clone, PartialEq)]
pub struct OrbSettings(Map<String, Value>);

impl Default for OrbSettings {
    fn default() -> Self {
        OrbSettings(default_orb_settings())
    }
}

impl OrbSettings {
    pub fn merge(custom: Option<&Value>) -> Result<Self, EngineError> {
        let mut merged = default_orb_settings();
        // `if orb_settings and isinstance(orb_settings, dict)`: an empty dict keeps defaults.
        let Some(Value::Object(custom)) = custom else {
            return Ok(OrbSettings(merged));
        };
        if custom.is_empty() {
            return Ok(OrbSettings(merged));
        }
        if let Some(method) = custom.get("method") {
            merged.insert("method".into(), method.clone());
        }
        let star_orb = match custom.get("fixed_star_orb") {
            Some(v) => py_float(v)?,
            None => 1.5,
        };
        merged.insert("fixed_star_orb".into(), json!(star_orb));
        for key in ["aspect_orbs", "planet_orbs"] {
            let table = merged.get_mut(key).and_then(Value::as_object_mut).unwrap();
            match custom.get(key) {
                None => {}
                Some(Value::Object(given)) => {
                    for (k, v) in given {
                        table.insert(k.clone(), v.clone());
                    }
                }
                Some(other) => {
                    return Err(EngineError::InvalidInput(format!(
                        "{key} must be an object, got {other}"
                    )))
                }
            }
        }
        Ok(OrbSettings(merged))
    }

    #[cfg(test)]
    pub fn as_map(&self) -> &Map<String, Value> {
        &self.0
    }

    pub fn to_value(&self) -> Value {
        Value::Object(self.0.clone())
    }

    fn table(&self, key: &str) -> Option<&Map<String, Value>> {
        self.0.get(key).and_then(Value::as_object)
    }

    fn lookup(&self, table: &str, name: &str, default: f64) -> Result<f64, EngineError> {
        match self.table(table).and_then(|t| t.get(name)) {
            Some(v) => py_float(v),
            None => Ok(default),
        }
    }

    pub fn fixed_star_orb(&self) -> Result<f64, EngineError> {
        match self.0.get("fixed_star_orb") {
            Some(v) => py_float(v),
            None => Ok(1.5),
        }
    }

    /// `get_effective_max_orb` for two bodies and an aspect.
    pub fn max_orb(&self, body1: &str, body2: &str, aspect: &str) -> Result<f64, EngineError> {
        let method = self.0.get("method").and_then(Value::as_str).unwrap_or("");
        let default_aspect_orb = self.lookup("aspect_orbs", aspect, 8.0)?;
        match method {
            "moiety_traditional" | "moiety_aspect_weighted" | "moiety_hybrid" => {
                let moiety = (self.lookup("planet_orbs", body1, 5.0)?
                    + self.lookup("planet_orbs", body2, 5.0)?)
                    / 2.0;
                Ok(match method {
                    "moiety_aspect_weighted" => {
                        let conj = self.lookup("aspect_orbs", "Conjunction", 8.0)?;
                        let has_aspect = self
                            .table("aspect_orbs")
                            .is_some_and(|t| t.contains_key(aspect));
                        let weight = if conj > 0.0 && has_aspect {
                            self.lookup("aspect_orbs", aspect, 8.0)? / conj
                        } else {
                            ASPECT_WEIGHT_FACTORS
                                .iter()
                                .find(|(name, _)| *name == aspect)
                                .map_or(1.0, |(_, w)| *w)
                        };
                        pyfloat::round(moiety * weight, 2)
                    }
                    "moiety_hybrid" => {
                        let capped = if default_aspect_orb < moiety {
                            default_aspect_orb
                        } else {
                            moiety
                        };
                        pyfloat::round(capped, 2)
                    }
                    _ => pyfloat::round(moiety, 2),
                })
            }
            _ => Ok(default_aspect_orb),
        }
    }
}

/// An aspect between two chart points. Fixed-star conjunctions carry no `is_major`.
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct Aspect {
    /// First point.
    pub body1: String,
    /// Second point.
    pub body2: String,
    /// Aspect name, e.g. "Trine".
    pub aspect_type: &'static str,
    /// Aspect glyph, e.g. "△".
    pub symbol: &'static str,
    /// Exact angle of the aspect, in degrees.
    pub angle: f64,
    /// Distance from exactness, in degrees (rounded to 0.01).
    pub orb: f64,
    /// Largest orb allowed for this pair and aspect, in degrees.
    pub max_orb: f64,
    /// Whether it is a major (Ptolemaic) aspect.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub is_major: Option<bool>,
    /// Always `true`, as in the reference implementation.
    pub is_applying: bool,
}

/// A cross-aspect between an overlay body (transit, partner) and a base body (natal).
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct CrossAspect {
    /// Overlay point (transit, partner).
    pub body1: String,
    /// Always "overlay".
    pub body1_source: &'static str,
    /// Base point (natal).
    pub body2: String,
    /// Always "base".
    pub body2_source: &'static str,
    /// Aspect name, e.g. "Trine".
    pub aspect_type: &'static str,
    /// Aspect glyph, e.g. "△".
    pub symbol: &'static str,
    /// Exact angle of the aspect, in degrees.
    pub angle: f64,
    /// Distance from exactness, in degrees (rounded to 0.01).
    pub orb: f64,
    /// Largest orb allowed for this pair and aspect, in degrees.
    pub max_orb: f64,
    /// Whether it is a major (Ptolemaic) aspect.
    pub is_major: bool,
    /// Always `true`, as in the reference implementation.
    pub is_applying: bool,
}

/// A named point on the ecliptic.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Point<'a> {
    pub name: &'a str,
    pub longitude: f64,
}

fn separation(a: f64, b: f64) -> f64 {
    let diff = pyfloat::rem((a - b).abs(), 360.0);
    if diff > 180.0 {
        360.0 - diff
    } else {
        diff
    }
}

fn is_node_pair(a: &str, b: &str) -> bool {
    (a == "North Node" && b == "South Node") || (a == "South Node" && b == "North Node")
}

/// The first aspect rule within orb for a separation, with its orb and max orb.
fn find_aspect(
    orbs: &OrbSettings,
    a: &str,
    b: &str,
    diff: f64,
) -> Result<Option<(&'static AspectRule, f64, f64)>, EngineError> {
    for rule in &ASPECT_RULES {
        let max_orb = orbs.max_orb(a, b, rule.name)?;
        let orb = (diff - rule.angle).abs();
        if orb <= max_orb {
            return Ok(Some((rule, orb, max_orb)));
        }
    }
    Ok(None)
}

/// Aspects between every pair of chart points, skipping the nodal axis.
pub fn natal_aspects(points: &[Point], orbs: &OrbSettings) -> Result<Vec<Aspect>, EngineError> {
    let mut out = Vec::new();
    for (i, p1) in points.iter().enumerate() {
        for p2 in &points[i + 1..] {
            if is_node_pair(p1.name, p2.name) {
                continue;
            }
            let diff = separation(p1.longitude, p2.longitude);
            if let Some((rule, orb, max_orb)) = find_aspect(orbs, p1.name, p2.name, diff)? {
                out.push(Aspect {
                    body1: p1.name.to_string(),
                    body2: p2.name.to_string(),
                    aspect_type: rule.name,
                    symbol: rule.symbol,
                    angle: rule.angle,
                    orb: pyfloat::round(orb, 2),
                    max_orb: pyfloat::round(max_orb, 2),
                    is_major: Some(rule.is_major),
                    is_applying: true,
                });
            }
        }
    }
    Ok(out)
}

/// `calculate_cross_aspects`: every overlay body against every base body.
pub fn cross_aspects(
    base: &[Point],
    overlay: &[Point],
    custom_orbs: Option<&Value>,
) -> Result<Vec<CrossAspect>, EngineError> {
    let orbs = OrbSettings::merge(custom_orbs)?;
    let mut out = Vec::new();
    for o in overlay {
        for b in base {
            if is_node_pair(o.name, b.name) {
                continue;
            }
            let diff = separation(o.longitude, b.longitude);
            if let Some((rule, orb, max_orb)) = find_aspect(&orbs, o.name, b.name, diff)? {
                out.push(CrossAspect {
                    body1: o.name.to_string(),
                    body1_source: "overlay",
                    body2: b.name.to_string(),
                    body2_source: "base",
                    aspect_type: rule.name,
                    symbol: rule.symbol,
                    angle: rule.angle,
                    orb: pyfloat::round(orb, 2),
                    max_orb: pyfloat::round(max_orb, 2),
                    is_major: rule.is_major,
                    is_applying: true,
                });
            }
        }
    }
    Ok(out)
}

/// Chart points (`name`, `ecliptic_longitude`) from a stored planets list.
pub fn points_from_json(planets: &Value) -> Result<Vec<Point<'_>>, EngineError> {
    let list = planets
        .as_array()
        .ok_or_else(|| EngineError::InvalidInput("planets must be a list".into()))?;
    list.iter()
        .map(|p| {
            let name = p
                .get("name")
                .and_then(Value::as_str)
                .ok_or_else(|| EngineError::InvalidInput("every planet needs a name".into()))?;
            let longitude = p.get("ecliptic_longitude").map(py_float).ok_or_else(|| {
                EngineError::InvalidInput(format!("{name} has no ecliptic_longitude"))
            })??;
            Ok(Point { name, longitude })
        })
        .collect()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn hybrid_orb_is_capped_by_aspect() {
        let orbs = OrbSettings::default();
        // Sun 15 + Moon 12 → moiety 13.5, capped by Conjunction 8.
        assert_eq!(orbs.max_orb("Sun", "Moon", "Conjunction").unwrap(), 8.0);
        // Uranus 5 + Neptune 5 → 5, under Conjunction's 8.
        assert_eq!(
            orbs.max_orb("Uranus", "Neptune", "Conjunction").unwrap(),
            5.0
        );
        assert_eq!(orbs.max_orb("Sun", "Moon", "Semi-Sextile").unwrap(), 2.5);
    }

    #[test]
    fn custom_orbs_merge_key_by_key() {
        let custom = json!({"method": "fixed_aspect", "aspect_orbs": {"Trine": "6"}});
        let orbs = OrbSettings::merge(Some(&custom)).unwrap();
        assert_eq!(orbs.max_orb("Sun", "Moon", "Trine").unwrap(), 6.0);
        assert_eq!(orbs.max_orb("Sun", "Moon", "Square").unwrap(), 7.0);
        assert_eq!(orbs.as_map()["aspect_orbs"]["Trine"], json!("6"));
        let weighted = json!({"method": "moiety_aspect_weighted"});
        let orbs = OrbSettings::merge(Some(&weighted)).unwrap();
        // moiety 13.5 × (6 / 8)
        assert_eq!(orbs.max_orb("Sun", "Moon", "Sextile").unwrap(), 10.12); // 10.125, ties to even
    }
}