pleiades-data 0.5.3

Packaged offline ephemeris data (precomputed Sun and Moon positions, derived from JPL public-domain ephemerides) and its EphemerisBackend for the pleiades astrology workspace.
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
use super::*;

#[derive(Clone, Debug, PartialEq)]
pub struct PackagedArtifactTargetThresholdScopeSummary {
    /// Release-scoped body class that the fit envelope applies to.
    pub scope: &'static str,
    /// Bundled bodies that contribute to the scope envelope.
    pub bodies: Vec<CelestialBody>,
    /// Bundled bodies that contribute to the scope envelope.
    pub body_count: usize,
    /// Measured fit envelope for the scoped body set.
    pub fit_envelope: PackagedArtifactFitEnvelopeSummary,
}

impl PackagedArtifactTargetThresholdScopeSummary {
    /// Returns the scope-specific fit posture as a compact human-readable line.
    pub fn summary_line(&self) -> String {
        format!(
            "scope={}; bodies={}; {}",
            self.scope,
            format_scope_bodies(&self.bodies),
            self.fit_envelope.summary_line(),
        )
    }

    /// Returns the validated scope-specific fit posture as a compact human-readable line.
    pub fn validated_summary_line(
        &self,
    ) -> Result<String, PackagedArtifactFitEnvelopeSummaryValidationError> {
        self.validate()?;
        Ok(self.summary_line())
    }

    /// Returns `Ok(())` when the scope summary still matches the current packaged-artifact posture.
    pub fn validate(&self) -> Result<(), PackagedArtifactFitEnvelopeSummaryValidationError> {
        let expected =
            packaged_artifact_target_threshold_scope_envelope_summary_details(self.scope);
        if self != &expected {
            return Err(
                PackagedArtifactFitEnvelopeSummaryValidationError::FieldOutOfSync {
                    field: "scope fit envelope",
                },
            );
        }

        Ok(())
    }
}

impl fmt::Display for PackagedArtifactTargetThresholdScopeSummary {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.summary_line())
    }
}

#[derive(Clone, Debug, PartialEq)]
pub struct PackagedArtifactTargetThresholdScopeEnvelopesSummary {
    /// Scope-specific fit envelopes that make up the current packaged-artifact posture.
    pub scope_envelopes: Vec<PackagedArtifactTargetThresholdScopeSummary>,
}

/// Validation error for a packaged-artifact target-threshold scope envelopes summary that drifted from the current posture.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum PackagedArtifactTargetThresholdScopeEnvelopesSummaryValidationError {
    /// A summary field is out of sync with the current packaged-artifact posture.
    FieldOutOfSync { field: &'static str },
}

impl fmt::Display for PackagedArtifactTargetThresholdScopeEnvelopesSummaryValidationError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::FieldOutOfSync { field } => write!(
                f,
                "the packaged-artifact target-threshold scope envelopes summary field `{field}` is out of sync with the current posture"
            ),
        }
    }
}

impl std::error::Error for PackagedArtifactTargetThresholdScopeEnvelopesSummaryValidationError {}

impl PackagedArtifactTargetThresholdScopeEnvelopesSummary {
    /// Returns the scope-envelope posture as a compact human-readable line.
    pub fn summary_line(&self) -> String {
        format!("scope envelopes: {}", join_display(&self.scope_envelopes))
    }

    /// Returns `Ok(())` when the scope-envelope posture still matches the current packaged-artifact posture.
    pub fn validate(
        &self,
    ) -> Result<(), PackagedArtifactTargetThresholdScopeEnvelopesSummaryValidationError> {
        let expected = packaged_artifact_target_threshold_scope_envelopes_summary_details();
        if self != &expected {
            return Err(
                PackagedArtifactTargetThresholdScopeEnvelopesSummaryValidationError::FieldOutOfSync {
                    field: "scope_envelopes",
                },
            );
        }

        for scope_envelope in &self.scope_envelopes {
            scope_envelope.validate().map_err(|_| {
                PackagedArtifactTargetThresholdScopeEnvelopesSummaryValidationError::FieldOutOfSync {
                    field: "scope_envelopes",
                }
            })?;
        }

        Ok(())
    }

    /// Returns the validated scope-envelope posture as a compact human-readable line.
    pub fn validated_summary_line(
        &self,
    ) -> Result<String, PackagedArtifactTargetThresholdScopeEnvelopesSummaryValidationError> {
        self.validate()?;
        Ok(self.summary_line())
    }
}

impl fmt::Display for PackagedArtifactTargetThresholdScopeEnvelopesSummary {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.summary_line())
    }
}

impl PackagedArtifactFitEnvelopeSummary {
    /// Returns the packaged-artifact fit evidence as a compact human-readable line.
    pub fn summary_line(&self) -> String {
        format!(
            "fit envelope: {}/{} segment samples across {} bundled bodies; mean Δlon={:.12}°, mean Δlat={:.12}°, mean Δdist={:.12} AU; max Δlon={:.12}°, max Δlat={:.12}°, max Δdist={:.12} AU",
            self.sample_count,
            self.expected_sample_count,
            self.body_count,
            self.mean_longitude_delta_degrees,
            self.mean_latitude_delta_degrees,
            self.mean_distance_delta_au,
            self.max_longitude_delta_degrees,
            self.max_latitude_delta_degrees,
            self.max_distance_delta_au,
        )
    }

    /// Returns `Ok(())` when the fit envelope still matches the current packaged artifact.
    pub fn validate(&self) -> Result<(), PackagedArtifactFitEnvelopeSummaryValidationError> {
        let artifact = packaged_artifact();
        let expected = packaged_artifact_fit_envelope_summary_details();
        // NOTE: `expected_sample_count` is defined as the realized coverable count (planned
        // fractions for which both the fit-truth backend and the packaged backend yield an
        // ecliptic+distance state). The fit-truth backend (see `FitTruthBackend`) measures major
        // bodies against the dense de440 production reference corpus (full 1900–2100 window, ≥3
        // entries/body, brackets every sampled epoch) and asteroid/custom bodies against the
        // JplSnapshotBackend they were fit from. Both `expected_sample_count` and `sample_count`
        // are derived from the same realized sample set, so the two checks below are informational
        // consistency guards (they confirm the live summary was built from the same realized set)
        // rather than a strict planned-vs-realized invariant.
        // The meaningful drift gate is the value comparison via `self != &expected` below.
        let expected_sample_count = packaged_artifact_fit_expected_sample_count(artifact);
        let expected_body_count = artifact.bodies.len();

        if self.expected_sample_count != expected_sample_count {
            return Err(
                PackagedArtifactFitEnvelopeSummaryValidationError::FieldOutOfSync {
                    field: "expected_sample_count",
                },
            );
        }
        if self.sample_count != expected_sample_count {
            return Err(
                PackagedArtifactFitEnvelopeSummaryValidationError::FieldOutOfSync {
                    field: "sample_count",
                },
            );
        }
        if self.body_count != expected_body_count {
            return Err(
                PackagedArtifactFitEnvelopeSummaryValidationError::FieldOutOfSync {
                    field: "body_count",
                },
            );
        }

        if self != &expected {
            for (field, matches) in [
                (
                    "mean_longitude_delta_degrees",
                    self.mean_longitude_delta_degrees == expected.mean_longitude_delta_degrees,
                ),
                (
                    "mean_latitude_delta_degrees",
                    self.mean_latitude_delta_degrees == expected.mean_latitude_delta_degrees,
                ),
                (
                    "mean_distance_delta_au",
                    self.mean_distance_delta_au == expected.mean_distance_delta_au,
                ),
                (
                    "max_longitude_delta_degrees",
                    self.max_longitude_delta_degrees == expected.max_longitude_delta_degrees,
                ),
                (
                    "max_latitude_delta_degrees",
                    self.max_latitude_delta_degrees == expected.max_latitude_delta_degrees,
                ),
                (
                    "max_distance_delta_au",
                    self.max_distance_delta_au == expected.max_distance_delta_au,
                ),
            ] {
                if !matches {
                    return Err(
                        PackagedArtifactFitEnvelopeSummaryValidationError::FieldOutOfSync { field },
                    );
                }
            }
        }

        Ok(())
    }
}

impl fmt::Display for PackagedArtifactFitEnvelopeSummary {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.summary_line())
    }
}

#[cfg(test)]
pub(crate) fn packaged_artifact_fit_sample_fractions(segment: &Segment) -> &'static [f64] {
    if segment.start.julian_day.days() == segment.end.julian_day.days() {
        &[0.0]
    } else {
        &[0.25, 0.5, 0.75]
    }
}

pub(crate) fn packaged_artifact_fit_sample_fractions_for_body(
    body: &CelestialBody,
    segment: &Segment,
) -> &'static [f64] {
    if segment.start.julian_day.days() == segment.end.julian_day.days() {
        &[0.0]
    } else {
        match packaged_artifact_body_cadence(body) {
            PackagedArtifactBodyCadence::Luminaries
            | PackagedArtifactBodyCadence::LunarPoints
            | PackagedArtifactBodyCadence::SelectedAsteroids
            | PackagedArtifactBodyCadence::Pluto
            | PackagedArtifactBodyCadence::CustomBodies => {
                packaged_artifact_segment_validation_fractions_for_body(body)
            }
            PackagedArtifactBodyCadence::InnerPlanets
            | PackagedArtifactBodyCadence::OuterPlanets => {
                PACKAGED_ARTIFACT_MEDIUM_VALIDATION_SAMPLE_FRACTIONS
            }
        }
    }
}

pub(crate) fn distance_channel_from_samples(
    start: f64,
    midpoint: Option<f64>,
    end: f64,
) -> PolynomialChannel {
    midpoint
        .map(|midpoint| {
            PolynomialChannel::quadratic(ChannelKind::DistanceAu, 10, start, midpoint, end, 0.5)
        })
        .unwrap_or_else(|| PolynomialChannel::linear(ChannelKind::DistanceAu, 10, start, end))
}

pub(crate) fn distance_channel_from_four_point_control_points(
    start: f64,
    first_third: f64,
    second_third: f64,
    end: f64,
) -> Option<PolynomialChannel> {
    polynomial_channel_from_samples(
        ChannelKind::DistanceAu,
        10,
        &[
            (0.0, start),
            (1.0 / 3.0, first_third),
            (2.0 / 3.0, second_third),
            (1.0, end),
        ],
    )
}

fn channel_from_fit_control_points(
    kind: ChannelKind,
    scale_exponent: u8,
    samples: &[(f64, f64)],
) -> Option<PolynomialChannel> {
    const TARGET_FRACTIONS: [f64; 4] = [0.0, 1.0 / 3.0, 2.0 / 3.0, 1.0];

    if samples.len() < TARGET_FRACTIONS.len() {
        return None;
    }

    let mut selected_samples = Vec::with_capacity(TARGET_FRACTIONS.len());
    let mut used_indices = vec![false; samples.len()];

    for target_fraction in TARGET_FRACTIONS {
        let mut best_index = None;
        let mut best_distance = f64::INFINITY;

        for (index, (fraction, _)) in samples.iter().enumerate() {
            if used_indices[index] {
                continue;
            }

            let distance = (*fraction - target_fraction).abs();
            if distance < best_distance {
                best_distance = distance;
                best_index = Some(index);
            }
        }

        let index = best_index?;

        used_indices[index] = true;
        selected_samples.push(samples[index]);
    }

    polynomial_channel_from_samples(kind, scale_exponent, &selected_samples)
}

pub(crate) fn channel_from_fit_samples_with_control_points(
    kind: ChannelKind,
    scale_exponent: u8,
    samples: &[(f64, f64)],
) -> Option<PolynomialChannel> {
    polynomial_channel_from_samples(kind, scale_exponent, samples)
        .or_else(|| channel_from_fit_control_points(kind, scale_exponent, samples))
}

pub(crate) fn channel_from_dense_fit_samples_with_control_points(
    kind: ChannelKind,
    scale_exponent: u8,
    samples: &[(f64, f64)],
) -> Option<PolynomialChannel> {
    channel_from_fit_control_points(kind, scale_exponent, samples)
        .or_else(|| polynomial_channel_from_samples(kind, scale_exponent, samples))
}

pub(crate) fn distance_channel_from_dense_fit_samples(
    samples: &[(f64, f64)],
    start: f64,
    midpoint: Option<f64>,
    end: f64,
) -> PolynomialChannel {
    channel_from_fit_control_points(ChannelKind::DistanceAu, 10, samples)
        .or_else(|| {
            channel_from_fit_samples_with_control_points(ChannelKind::DistanceAu, 10, samples)
        })
        .unwrap_or_else(|| distance_channel_from_samples(start, midpoint, end))
}

pub(crate) fn distance_channel_from_fit_samples(
    samples: &[(f64, f64)],
    start: f64,
    midpoint: Option<f64>,
    end: f64,
) -> PolynomialChannel {
    channel_from_fit_samples_with_control_points(ChannelKind::DistanceAu, 10, samples)
        .unwrap_or_else(|| distance_channel_from_samples(start, midpoint, end))
}

/// Builds the ecliptic request for a (body, segment, fraction) fit sample.
fn packaged_artifact_fit_sample_request(
    body: &CelestialBody,
    segment: &Segment,
    fraction: f64,
) -> (Instant, EphemerisRequest) {
    let start = segment.start.julian_day.days();
    let span = segment.end.julian_day.days() - start;
    let instant = Instant::new(
        JulianDay::from_days(start + span * fraction),
        segment.start.scale,
    );
    let request = EphemerisRequest {
        body: body.clone(),
        instant,
        observer: None,
        frame: CoordinateFrame::Ecliptic,
        zodiac_mode: ZodiacMode::Tropical,
        apparent: Apparentness::Mean,
    };
    (instant, request)
}

pub(crate) fn packaged_artifact_fit_expected_sample_count_with_filter<F>(
    _artifact: &CompressedArtifact,
    mut include_body: F,
) -> usize
where
    F: FnMut(&CelestialBody) -> bool,
{
    // The envelope compares the de440-fit artifact against the kernel-free
    // `FitTruthBackend`: major bodies against the dense de440 production reference
    // corpus (interior ∪ boundary ∪ fast_clusters, full 1900–2100 window, ≥3
    // entries/body so it brackets every sampled epoch and never extrapolates), and
    // asteroid/custom bodies against the `JplSnapshotBackend` they were fit from.
    //
    // The expected count is DEFINED as the number of GENUINELY COVERABLE planned
    // samples for the artifact's window — i.e. exactly the realized fit samples
    // (each passed the reference + packaged ecliptic+distance gate in
    // `packaged_artifact_fit_samples_with_filter`). So `expected == actual` holds
    // by construction for a legitimate reason (it counts what is genuinely
    // coverable), not by loosening an equality. Counting the cached realized
    // samples also avoids a second backend pass over the dense artifact.
    packaged_artifact_fit_samples_for_current_artifact()
        .iter()
        .filter(|sample| include_body(&sample.body))
        .count()
}

fn packaged_artifact_fit_expected_sample_count(artifact: &CompressedArtifact) -> usize {
    packaged_artifact_fit_expected_sample_count_with_filter(artifact, |_| true)
}

/// Fit-envelope truth backend that measures each bundled body against the SAME
/// source the generator fit that body against, so the envelope deltas are a true
/// generator-vs-source residual rather than a generator-vs-mismatched-source
/// artifact.
///
/// The artifact generator (`regenerate.rs`) fits the ten major bodies from the
/// de440 kernel and fits the selected-asteroid / custom bodies from the narrow
/// `JplSnapshotBackend` reference snapshot (`regenerate.rs:162–182`,
/// "major bodies are fit from the kernel, never from the snapshot"). This truth
/// backend mirrors that split:
/// - major bodies → the dense de440-derived production reference corpus
///   (interior ∪ boundary ∪ fast_clusters), the kernel-free analogue of the de440
///   kernel they were fit from. It spans the full 1900–2100 window with ≥3
///   entries/body so Lagrange interpolation never extrapolates.
/// - selected-asteroid / custom bodies → `JplSnapshotBackend`, the exact source
///   they were fit against. Measuring asteroids against the corpus instead would
///   compare them to a body they were never fit from, and the constrained asteroid
///   corpus is too coarse for fast movers (Eros at ~180-day spacing) so cubic
///   interpolation overshoots into non-physical multi-million-AU deltas — an
///   interpolation artifact, not a real residual.
///
/// Kernel-free: the corpus and the snapshot both read committed CSVs via
/// `include_str!`. This backend only *measures* the committed artifact and is
/// never a generation input, so byte-identity is preserved.
///
/// Corpus de-duplication: the three major-body slices overlap at their shared
/// anchor epochs (the boundary slice repeats interior/fast-cluster anchor rows),
/// so the merged corpus holds a handful of EXACT-DUPLICATE `(body, epoch)` rows
/// with identical coordinates. `SnapshotCorpusBackend`'s Lagrange interpolation
/// ranks nearest nodes by `|epoch − target|` without de-duplicating, so a
/// duplicated node yields two selected samples at the same epoch and a zero
/// `(xi − xj)` denominator → `inf`/`NaN` deltas. We de-duplicate identical
/// `(body, epoch)` rows here before building the corpus backend. This is lossless
/// (the dropped rows are byte-identical to the kept ones — verified) and never
/// touches the committed CSVs.
struct FitTruthBackend {
    corpus: SnapshotCorpusBackend,
    snapshot: JplSnapshotBackend,
}

impl FitTruthBackend {
    fn fits_from_snapshot(body: &CelestialBody) -> bool {
        use crate::coverage::PackagedArtifactBodyCadence;
        matches!(
            crate::coverage::packaged_artifact_body_cadence(body),
            PackagedArtifactBodyCadence::SelectedAsteroids
                | PackagedArtifactBodyCadence::CustomBodies
        )
    }
}

impl EphemerisBackend for FitTruthBackend {
    fn metadata(&self) -> pleiades_backend::BackendMetadata {
        self.corpus.metadata()
    }

    fn supports_body(&self, body: CelestialBody) -> bool {
        if Self::fits_from_snapshot(&body) {
            self.snapshot.supports_body(body)
        } else {
            self.corpus.supports_body(body)
        }
    }

    fn position(&self, req: &EphemerisRequest) -> Result<EphemerisResult, EphemerisError> {
        if Self::fits_from_snapshot(&req.body) {
            self.snapshot.position(req)
        } else {
            self.corpus.position(req)
        }
    }
}

/// Returns the once-cached fit-truth backend (see [`FitTruthBackend`]).
fn fit_truth_backend() -> &'static FitTruthBackend {
    static BACKEND: OnceLock<FitTruthBackend> = OnceLock::new();
    BACKEND.get_or_init(|| {
        let mut seen = std::collections::HashSet::new();
        let entries = production_reference_corpus()
            .iter()
            .filter(|entry| {
                seen.insert((entry.body.clone(), entry.epoch.julian_day.days().to_bits()))
            })
            .cloned()
            .collect::<Vec<_>>();
        FitTruthBackend {
            corpus: SnapshotCorpusBackend::from_entries(entries),
            snapshot: JplSnapshotBackend,
        }
    })
}

fn packaged_artifact_fit_samples_with_filter<F>(
    artifact: &CompressedArtifact,
    mut include_body: F,
) -> Vec<PackagedArtifactFitSample>
where
    F: FnMut(&CelestialBody) -> bool,
{
    let reference_backend = fit_truth_backend();
    let packaged_backend = packaged_backend();
    let mut samples = Vec::new();

    for body_artifact in &artifact.bodies {
        if !include_body(&body_artifact.body) {
            continue;
        }

        for segment in &body_artifact.segments {
            for fraction in
                packaged_artifact_fit_sample_fractions_for_body(&body_artifact.body, segment)
            {
                let (instant, request) =
                    packaged_artifact_fit_sample_request(&body_artifact.body, segment, *fraction);
                let expected = match reference_backend.position(&request) {
                    Ok(result) => result,
                    Err(_) => continue,
                };
                let actual = match packaged_backend.position(&request) {
                    Ok(result) => result,
                    Err(_) => continue,
                };

                let (Some(expected_ecliptic), Some(actual_ecliptic)) =
                    (expected.ecliptic, actual.ecliptic)
                else {
                    continue;
                };
                let (Some(expected_distance), Some(actual_distance)) =
                    (expected_ecliptic.distance_au, actual_ecliptic.distance_au)
                else {
                    continue;
                };

                samples.push(PackagedArtifactFitSample {
                    body: body_artifact.body.clone(),
                    segment_start: segment.start,
                    segment_end: segment.end,
                    sample_instant: instant,
                    sample_fraction: *fraction,
                    longitude_delta_degrees: Angle::from_degrees(
                        actual_ecliptic.longitude.degrees() - expected_ecliptic.longitude.degrees(),
                    )
                    .normalized_signed()
                    .degrees()
                    .abs(),
                    latitude_delta_degrees: (actual_ecliptic.latitude.degrees()
                        - expected_ecliptic.latitude.degrees())
                    .abs(),
                    distance_delta_au: (actual_distance - expected_distance).abs(),
                });
            }
        }
    }

    samples
}

pub(crate) fn packaged_artifact_fit_samples_for_current_artifact(
) -> &'static [PackagedArtifactFitSample] {
    static SAMPLES: OnceLock<Vec<PackagedArtifactFitSample>> = OnceLock::new();
    SAMPLES
        .get_or_init(|| {
            let artifact = packaged_artifact();
            packaged_artifact_fit_samples_with_filter(artifact, |_| true)
        })
        .as_slice()
}

pub(crate) fn packaged_artifact_fit_outlier_sample_fractions(
    body: &CelestialBody,
    segment: &Segment,
) -> &'static [f64] {
    if segment.start.julian_day.days() == segment.end.julian_day.days() {
        &[0.0]
    } else {
        packaged_artifact_segment_validation_fractions_for_body(body)
    }
}

fn packaged_artifact_fit_outlier_samples_with_filter<F>(
    artifact: &CompressedArtifact,
    mut include_body: F,
) -> Vec<PackagedArtifactFitSample>
where
    F: FnMut(&CelestialBody) -> bool,
{
    let reference_backend = fit_truth_backend();
    let packaged_backend = packaged_backend();
    let mut samples = Vec::new();

    for body_artifact in &artifact.bodies {
        if !include_body(&body_artifact.body) {
            continue;
        }

        for segment in &body_artifact.segments {
            for fraction in
                packaged_artifact_fit_outlier_sample_fractions(&body_artifact.body, segment)
            {
                let (instant, request) =
                    packaged_artifact_fit_sample_request(&body_artifact.body, segment, *fraction);
                let expected = match reference_backend.position(&request) {
                    Ok(result) => result,
                    Err(_) => continue,
                };
                let actual = match packaged_backend.position(&request) {
                    Ok(result) => result,
                    Err(_) => continue,
                };

                let (Some(expected_ecliptic), Some(actual_ecliptic)) =
                    (expected.ecliptic, actual.ecliptic)
                else {
                    continue;
                };
                let (Some(expected_distance), Some(actual_distance)) =
                    (expected_ecliptic.distance_au, actual_ecliptic.distance_au)
                else {
                    continue;
                };

                samples.push(PackagedArtifactFitSample {
                    body: body_artifact.body.clone(),
                    segment_start: segment.start,
                    segment_end: segment.end,
                    sample_instant: instant,
                    sample_fraction: *fraction,
                    longitude_delta_degrees: Angle::from_degrees(
                        actual_ecliptic.longitude.degrees() - expected_ecliptic.longitude.degrees(),
                    )
                    .normalized_signed()
                    .degrees()
                    .abs(),
                    latitude_delta_degrees: (actual_ecliptic.latitude.degrees()
                        - expected_ecliptic.latitude.degrees())
                    .abs(),
                    distance_delta_au: (actual_distance - expected_distance).abs(),
                });
            }
        }
    }

    samples
}

pub(crate) fn packaged_artifact_fit_outlier_samples_for_current_artifact(
) -> &'static [PackagedArtifactFitSample] {
    static SAMPLES: OnceLock<Vec<PackagedArtifactFitSample>> = OnceLock::new();
    SAMPLES
        .get_or_init(|| {
            let artifact = packaged_artifact();
            packaged_artifact_fit_outlier_samples_with_filter(artifact, |_| true)
        })
        .as_slice()
}

pub(crate) fn packaged_artifact_fit_envelope_summary_from_samples(
    samples: &[PackagedArtifactFitSample],
    expected_sample_count: usize,
) -> PackagedArtifactFitEnvelopeSummary {
    let sample_count = samples.len();
    let mut observed_bodies = Vec::new();
    let mut mean_longitude_delta_degrees: f64 = 0.0;
    let mut mean_latitude_delta_degrees: f64 = 0.0;
    let mut mean_distance_delta_au: f64 = 0.0;
    let mut max_longitude_delta_degrees: f64 = 0.0;
    let mut max_latitude_delta_degrees: f64 = 0.0;
    let mut max_distance_delta_au: f64 = 0.0;

    for sample in samples {
        if !observed_bodies.contains(&sample.body) {
            observed_bodies.push(sample.body.clone());
        }
        mean_longitude_delta_degrees += sample.longitude_delta_degrees;
        mean_latitude_delta_degrees += sample.latitude_delta_degrees;
        mean_distance_delta_au += sample.distance_delta_au;
        max_longitude_delta_degrees =
            max_longitude_delta_degrees.max(sample.longitude_delta_degrees);
        max_latitude_delta_degrees = max_latitude_delta_degrees.max(sample.latitude_delta_degrees);
        max_distance_delta_au = max_distance_delta_au.max(sample.distance_delta_au);
    }

    if sample_count > 0 {
        let sample_count = sample_count as f64;
        mean_longitude_delta_degrees /= sample_count;
        mean_latitude_delta_degrees /= sample_count;
        mean_distance_delta_au /= sample_count;
    }

    PackagedArtifactFitEnvelopeSummary {
        sample_count,
        expected_sample_count,
        body_count: observed_bodies.len(),
        mean_longitude_delta_degrees,
        mean_latitude_delta_degrees,
        mean_distance_delta_au,
        max_longitude_delta_degrees,
        max_latitude_delta_degrees,
        max_distance_delta_au,
    }
}

/// Returns the current packaged-artifact fit envelope summary record.
pub fn packaged_artifact_fit_envelope_summary_details() -> PackagedArtifactFitEnvelopeSummary {
    static SUMMARY: OnceLock<PackagedArtifactFitEnvelopeSummary> = OnceLock::new();
    SUMMARY
        .get_or_init(|| {
            let artifact = packaged_artifact();
            let samples = packaged_artifact_fit_samples_for_current_artifact();
            packaged_artifact_fit_envelope_summary_from_samples(
                samples,
                packaged_artifact_fit_expected_sample_count(artifact),
            )
        })
        .clone()
}

fn packaged_artifact_fit_channel_rank(channel: ChannelKind) -> usize {
    match channel {
        ChannelKind::DistanceAu => 0,
        ChannelKind::Longitude => 1,
        ChannelKind::Latitude => 2,
        _ => unreachable!("unsupported packaged-artifact channel kind"),
    }
}

pub(crate) fn packaged_artifact_fit_channel_delta(
    sample: &PackagedArtifactFitSample,
    channel: ChannelKind,
) -> f64 {
    match channel {
        ChannelKind::Longitude => sample.longitude_delta_degrees,
        ChannelKind::Latitude => sample.latitude_delta_degrees,
        ChannelKind::DistanceAu => sample.distance_delta_au,
        _ => unreachable!("unsupported packaged-artifact channel kind"),
    }
}

fn packaged_artifact_fit_outlier_summary_from_samples(
    samples: &[PackagedArtifactFitSample],
) -> PackagedArtifactFitOutlierSummary {
    let mut families: HashMap<
        (
            CelestialBody,
            ChannelKind,
            PackagedArtifactFitSegmentFamilyKey,
        ),
        PackagedArtifactFitChannelFamilyAccumulator,
    > = HashMap::new();

    for sample in samples {
        let family_key = PackagedArtifactFitSegmentFamilyKey::from_sample(sample);

        for channel in [
            ChannelKind::DistanceAu,
            ChannelKind::Longitude,
            ChannelKind::Latitude,
        ] {
            let entry = families
                .entry((sample.body.clone(), channel, family_key))
                .or_insert_with(PackagedArtifactFitChannelFamilyAccumulator::new);
            entry.push(sample, channel);
        }
    }

    let mut body_channel_outliers: HashMap<
        CelestialBody,
        [Option<PackagedArtifactFitChannelOutlier>; 3],
    > = HashMap::new();

    for ((body, channel, _family_key), family) in families {
        let Some(outlier) = family.finish(channel) else {
            continue;
        };
        let entry = body_channel_outliers
            .entry(body)
            .or_insert_with(|| [None, None, None]);
        let channel_index = packaged_artifact_fit_channel_rank(channel);
        let should_replace = entry[channel_index]
            .as_ref()
            .map(|existing| {
                outlier.delta > existing.delta
                    || (outlier.delta == existing.delta
                        && outlier.segment_span_days < existing.segment_span_days)
            })
            .unwrap_or(true);

        if should_replace {
            entry[channel_index] = Some(outlier);
        }
    }

    let mut body_summaries = body_channel_outliers
        .into_iter()
        .map(|(body, outliers)| {
            let mut channel_outliers = Vec::new();
            for channel in [
                ChannelKind::DistanceAu,
                ChannelKind::Longitude,
                ChannelKind::Latitude,
            ] {
                if let Some(outlier) = outliers[packaged_artifact_fit_channel_rank(channel)].clone()
                {
                    channel_outliers.push(outlier);
                }
            }
            PackagedArtifactFitBodyOutlierSummary {
                body,
                channel_outliers,
            }
        })
        .collect::<Vec<_>>();

    body_summaries.sort_by_key(|summary| summary.body.to_string());

    PackagedArtifactFitOutlierSummary {
        body_count: body_summaries.len(),
        body_summaries,
    }
}

/// Returns the current packaged-artifact body/channel fit outlier summary record.
pub fn packaged_artifact_fit_outlier_summary_details() -> PackagedArtifactFitOutlierSummary {
    static SUMMARY: OnceLock<PackagedArtifactFitOutlierSummary> = OnceLock::new();
    SUMMARY
        .get_or_init(|| {
            let samples = packaged_artifact_fit_outlier_samples_for_current_artifact();
            packaged_artifact_fit_outlier_summary_from_samples(samples)
        })
        .clone()
}

/// Returns the calibrated packaged-artifact fit threshold summary record.
pub fn packaged_artifact_fit_threshold_summary_details() -> PackagedArtifactFitThresholdSummary {
    PACKAGED_ARTIFACT_FIT_THRESHOLD_SUMMARY
}

/// Returns the current packaged-artifact fit margins relative to the calibrated thresholds.
pub fn packaged_artifact_fit_margin_summary_details() -> PackagedArtifactFitMarginSummary {
    let summary = PackagedArtifactFitMarginSummary {
        envelope: packaged_artifact_fit_envelope_summary_details(),
        thresholds: packaged_artifact_fit_threshold_summary_details(),
    };
    debug_assert!(summary.validate().is_ok());
    summary
}

/// Returns the current packaged-artifact fit threshold violations summary record.
pub fn packaged_artifact_fit_threshold_violation_summary_details(
) -> PackagedArtifactFitThresholdViolationsSummary {
    let envelope = packaged_artifact_fit_envelope_summary_details();
    let thresholds = packaged_artifact_fit_threshold_summary_details();
    let violations = packaged_artifact_fit_threshold_violations_from_envelope_and_thresholds(
        &envelope,
        &thresholds,
    );

    PackagedArtifactFitThresholdViolationsSummary { violations }
}

pub(crate) fn packaged_artifact_body_scope(body: &CelestialBody) -> &'static str {
    match body {
        CelestialBody::Sun | CelestialBody::Moon => "luminaries",
        CelestialBody::Mercury
        | CelestialBody::Venus
        | CelestialBody::Mars
        | CelestialBody::Jupiter
        | CelestialBody::Saturn
        | CelestialBody::Uranus
        | CelestialBody::Neptune => "major planets",
        CelestialBody::Pluto => "pluto",
        CelestialBody::MeanNode
        | CelestialBody::TrueNode
        | CelestialBody::MeanApogee
        | CelestialBody::TrueApogee
        | CelestialBody::MeanPerigee
        | CelestialBody::TruePerigee => "lunar points",
        CelestialBody::Ceres
        | CelestialBody::Pallas
        | CelestialBody::Juno
        | CelestialBody::Vesta => "selected asteroids",
        CelestialBody::Custom(custom) if custom.catalog.eq_ignore_ascii_case("asteroid") => {
            "selected asteroids"
        }
        CelestialBody::Custom(_) => "custom bodies",
        _ => "custom bodies",
    }
}