kshana 0.16.0

Open, reproducible PNT-resilience simulator with quantum-sensor performance models
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
// SPDX-License-Identifier: Apache-2.0
//! Ephemeris & ground-track scenario — the user-facing surface for satellite
//! VELOCITY, the full frame reductions, and the WGS-84 sub-satellite point.
//!
//! The propagators (SGP4 from a TLE, or the analytic Keplerian orbit) already
//! compute a velocity and the library already reduces TEME → GCRS (≈ J2000) and
//! TEME → ITRF/ECEF → geodetic; this pack runs a single satellite over a time
//! grid and emits, at every step:
//!
//! - the inertial **state** (position *and* velocity) in TEME and in GCRS,
//! - the Earth-fixed **ITRF/ECEF** position,
//! - the **sub-satellite point** (WGS-84 geodetic latitude / longitude / height) —
//!   the "where is the satellite right now" ground track, and
//! - for an optional ground **station**, the topocentric azimuth / elevation /
//!   range and the **range-rate** (the geometric Doppler the receiver sees).
//!
//! Time handling: the SGP4 epoch is the TLE's own epoch (UTC); the analytic orbit
//! takes an explicit `epoch`. UT1 drives Earth rotation (`dut1_s`, the UT1−UTC
//! offset; default 0 ⇒ the DUT1≈0 approximation — a few-arcsecond, i.e.
//! few-hundred-metre, longitude shift in the ground track), TT drives
//! precession/nutation, and `xp_arcsec`/`yp_arcsec` are the
//! IERS polar-motion pole coordinates (default 0). The frame chain is the one
//! validated to the millimetre against the published Vallado vectors
//! (`tests/frame_reference_vectors.rs`).

use crate::frames::{
    arcsec, ecef_to_geodetic, ecef_to_teme, geodetic_to_ecef, look_angles, teme_to_itrf, Geodetic,
};
use crate::orbit::{OrbitCfg, Propagator};
use crate::rinex::EpochUtc;
use crate::timescales::{julian_date, utc_to_tt, utc_to_ut1};
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};

type Vec3 = [f64; 3];

/// Speed of light (m/s), for the carrier Doppler shift.
const C_M_S: f64 = 299_792_458.0;
/// GPS L1 carrier (Hz) — the default the Doppler shift is reported at.
const GPS_L1_HZ: f64 = 1_575_420_000.0;
/// Earth rotation rate as the GMST advance rate (rad/s): one full TEME→ECEF
/// rotation `R3(GMST)` turns at this rate, so the inertial velocity of a fixed
/// ground station is `ω × r`. Matches the sidereal day, not the solar day.
const GMST_RATE_RAD_S: f64 = 7.292_115_855_3e-5;
/// JD of the SGP4 epoch origin (1950 Jan 0.0 UTC). `jd_utc = epoch_days_1950 + this`.
const JD_1950_ORIGIN: f64 = 2_433_281.5;

/// A geodetic ground station (degrees / metres).
#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
pub struct StationCfg {
    pub lat_deg: f64,
    pub lon_deg: f64,
    #[serde(default)]
    pub alt_m: f64,
}

impl StationCfg {
    fn geodetic(&self) -> Geodetic {
        Geodetic {
            lat_rad: self.lat_deg.to_radians(),
            lon_rad: self.lon_deg.to_radians(),
            alt_m: self.alt_m,
        }
    }
}

fn default_step_s() -> f64 {
    60.0
}
fn default_duration_s() -> f64 {
    5_400.0
}
fn default_carrier_hz() -> f64 {
    GPS_L1_HZ
}

/// An ephemeris / ground-track scenario: one satellite (a TLE → SGP4, or an
/// analytic `orbit`), a time grid, and an optional ground station.
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct EphemerisScenario {
    /// The scenario kind tag (`ephemeris`); ignored by the runner.
    #[serde(default)]
    pub kind: Option<String>,
    /// A two-line element set (`"line1\nline2"`, an optional name line is allowed).
    #[serde(default)]
    pub tle: Option<String>,
    /// An analytic Keplerian orbit, the alternative to `tle`. Requires `epoch`.
    #[serde(default)]
    pub orbit: Option<OrbitCfg>,
    /// The UTC instant labelling `t = 0`. Required for an analytic `orbit`; for a
    /// TLE it defaults to (and normally should be left as) the TLE's own epoch.
    #[serde(default)]
    pub epoch: Option<EpochUtc>,
    #[serde(default = "default_step_s")]
    pub step_s: f64,
    #[serde(default = "default_duration_s")]
    pub duration_s: f64,
    #[serde(default)]
    pub station: Option<StationCfg>,
    /// UT1−UTC (s); 0 ⇒ the DUT1≈0 approximation (≤ ~13″, i.e. a few-hundred-metre,
    /// longitude shift in the ground track).
    #[serde(default)]
    pub dut1_s: f64,
    /// IERS polar-motion pole coordinates (arcsec); default 0.
    #[serde(default)]
    pub xp_arcsec: f64,
    #[serde(default)]
    pub yp_arcsec: f64,
    /// Carrier frequency (Hz) the Doppler shift is reported at; default GPS L1.
    #[serde(default = "default_carrier_hz")]
    pub carrier_hz: f64,
}

/// One station-relative view: look angles and the geometric range-rate / Doppler.
#[derive(Clone, Copy, Debug, Serialize)]
pub struct StationView {
    pub az_deg: f64,
    pub el_deg: f64,
    pub range_km: f64,
    /// Range-rate (m/s); positive = receding (range increasing).
    pub range_rate_m_s: f64,
    /// Carrier Doppler shift (Hz) = −range_rate / λ; positive as the satellite closes.
    pub doppler_hz: f64,
    /// True when the satellite is at or above the station's local horizon.
    pub visible: bool,
}

/// One time sample of the ephemeris.
#[derive(Clone, Debug, Serialize)]
pub struct EphemSample {
    pub t_s: f64,
    pub jd_utc: f64,
    pub teme_r_m: Vec3,
    pub teme_v_m_s: Vec3,
    pub gcrs_r_m: Vec3,
    pub gcrs_v_m_s: Vec3,
    pub ecef_r_m: Vec3,
    pub lat_deg: f64,
    pub lon_deg: f64,
    pub alt_km: f64,
    /// Inertial speed (m/s).
    pub speed_m_s: f64,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub station_view: Option<StationView>,
}

/// The result of an ephemeris run: summary extrema plus every sample.
#[derive(Clone, Debug, Serialize)]
pub struct EphemerisResult {
    pub scenario_hash: String,
    pub source: String,
    pub jd_utc0: f64,
    pub n_samples: usize,
    pub lat_min_deg: f64,
    pub lat_max_deg: f64,
    pub alt_min_km: f64,
    pub alt_max_km: f64,
    pub speed_min_m_s: f64,
    pub speed_max_m_s: f64,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_elevation_deg: Option<f64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub peak_doppler_hz: Option<f64>,
    pub samples: Vec<EphemSample>,
}

fn norm(v: Vec3) -> f64 {
    (v[0] * v[0] + v[1] * v[1] + v[2] * v[2]).sqrt()
}

/// Build the propagator and the absolute UTC Julian date at `t = 0`.
fn build(scn: &EphemerisScenario) -> Result<(Propagator, f64, String), String> {
    if let Some(tle) = &scn.tle {
        let lines: Vec<&str> = tle
            .lines()
            .map(str::trim)
            .filter(|l| !l.is_empty())
            .collect();
        let l1 = lines
            .iter()
            .find(|l| l.starts_with("1 "))
            .ok_or("TLE is missing line 1 (a line starting `1 `)")?;
        let l2 = lines
            .iter()
            .find(|l| l.starts_with("2 "))
            .ok_or("TLE is missing line 2 (a line starting `2 `)")?;
        let t = crate::tle::parse_tle(l1, l2)?;
        let jd0 = match &scn.epoch {
            Some(e) => julian_date(e.year, e.month, e.day, e.hour, e.minute, e.second),
            None => t.epoch_days_1950 + JD_1950_ORIGIN,
        };
        let prop = Propagator::Sgp4(Box::new(t.to_sgp4(crate::sgp4::wgs72(), false)));
        Ok((prop, jd0, "sgp4 (TLE)".to_string()))
    } else if let Some(o) = &scn.orbit {
        let e = scn
            .epoch
            .as_ref()
            .ok_or("an analytic `orbit` ephemeris requires an `epoch`")?;
        let jd0 = julian_date(e.year, e.month, e.day, e.hour, e.minute, e.second);
        Ok((Propagator::Kepler(o.to_orbit()), jd0, "kepler".to_string()))
    } else {
        Err("ephemeris scenario needs either a `tle` or an `orbit`".to_string())
    }
}

/// Run the ephemeris / ground-track scenario.
pub fn run_ephemeris(scn: &EphemerisScenario) -> Result<EphemerisResult, String> {
    if !scn.step_s.is_finite() || scn.step_s <= 0.0 {
        return Err("step_s must be positive".to_string());
    }
    if scn.duration_s < 0.0 {
        return Err("duration_s must be non-negative".to_string());
    }
    let (prop, jd_utc0, source) = build(scn)?;
    let station = scn.station;
    let lambda_m = C_M_S / scn.carrier_hz;
    let (xp, yp) = (arcsec(scn.xp_arcsec), arcsec(scn.yp_arcsec));

    let n = (scn.duration_s / scn.step_s).round() as usize;
    let mut samples = Vec::with_capacity(n + 1);
    let (mut lat_min, mut lat_max) = (f64::INFINITY, f64::NEG_INFINITY);
    let (mut alt_min, mut alt_max) = (f64::INFINITY, f64::NEG_INFINITY);
    let (mut spd_min, mut spd_max) = (f64::INFINITY, f64::NEG_INFINITY);
    let mut max_el: Option<f64> = None;
    let mut peak_dopp: Option<f64> = None;

    for i in 0..=n {
        let t = i as f64 * scn.step_s;
        let jd_utc = jd_utc0 + t / 86_400.0;
        let jd_ut1 = utc_to_ut1(jd_utc, scn.dut1_s);
        let jd_tt = utc_to_tt(jd_utc);

        let st = prop.state_eci(t); // TEME (m, m/s)
        let gcrs = prop.state_gcrs(t, jd_tt); // GCRS (≈ J2000)
        let ecef = teme_to_itrf(st.r_m, jd_ut1, xp, yp, jd_tt);
        let geod = ecef_to_geodetic(ecef);
        let (lat_deg, lon_deg, alt_km) = (
            geod.lat_rad.to_degrees(),
            geod.lon_rad.to_degrees(),
            geod.alt_m / 1000.0,
        );
        let speed = norm(st.v_m_s);

        let station_view = station.map(|s| {
            let st_geod = s.geodetic();
            let look = look_angles(st_geod, ecef);
            // Range-rate in the inertial (TEME) frame: the station rotates with the
            // Earth (v = ω × r about the TEME z-axis), the satellite carries its own
            // inertial velocity, and range-rate is the relative velocity projected
            // onto the line of sight. Frame-invariant — equals d(range)/dt exactly.
            let s_teme = ecef_to_teme(geodetic_to_ecef(st_geod), jd_ut1);
            let v_station = [
                -GMST_RATE_RAD_S * s_teme[1],
                GMST_RATE_RAD_S * s_teme[0],
                0.0,
            ];
            let d = [
                st.r_m[0] - s_teme[0],
                st.r_m[1] - s_teme[1],
                st.r_m[2] - s_teme[2],
            ];
            let rng = norm(d);
            let v_rel = [
                st.v_m_s[0] - v_station[0],
                st.v_m_s[1] - v_station[1],
                st.v_m_s[2] - v_station[2],
            ];
            let range_rate = if rng > 0.0 {
                (v_rel[0] * d[0] + v_rel[1] * d[1] + v_rel[2] * d[2]) / rng
            } else {
                0.0
            };
            let doppler = -range_rate / lambda_m;
            let el_deg = look.el_rad.to_degrees();
            if el_deg >= 0.0 {
                max_el = Some(max_el.map_or(el_deg, |m: f64| m.max(el_deg)));
                let ad = doppler.abs();
                peak_dopp = Some(peak_dopp.map_or(ad, |m: f64| m.max(ad)));
            }
            StationView {
                az_deg: look.az_rad.to_degrees(),
                el_deg,
                range_km: look.range_m / 1000.0,
                range_rate_m_s: range_rate,
                doppler_hz: doppler,
                visible: el_deg >= 0.0,
            }
        });

        lat_min = lat_min.min(lat_deg);
        lat_max = lat_max.max(lat_deg);
        alt_min = alt_min.min(alt_km);
        alt_max = alt_max.max(alt_km);
        spd_min = spd_min.min(speed);
        spd_max = spd_max.max(speed);

        samples.push(EphemSample {
            t_s: t,
            jd_utc,
            teme_r_m: st.r_m,
            teme_v_m_s: st.v_m_s,
            gcrs_r_m: gcrs.r_m,
            gcrs_v_m_s: gcrs.v_m_s,
            ecef_r_m: ecef,
            lat_deg,
            lon_deg,
            alt_km,
            speed_m_s: speed,
            station_view,
        });
    }

    let mut hasher = Sha256::new();
    hasher.update(serde_json::to_string(scn).unwrap_or_default().as_bytes());
    let scenario_hash = format!("{:x}", hasher.finalize());

    Ok(EphemerisResult {
        scenario_hash,
        source,
        jd_utc0,
        n_samples: samples.len(),
        lat_min_deg: lat_min,
        lat_max_deg: lat_max,
        alt_min_km: alt_min,
        alt_max_km: alt_max,
        speed_min_m_s: spd_min,
        speed_max_m_s: spd_max,
        max_elevation_deg: max_el,
        peak_doppler_hz: peak_dopp,
        samples,
    })
}

fn esc(s: &str) -> String {
    s.replace('&', "&amp;")
        .replace('<', "&lt;")
        .replace('>', "&gt;")
}

/// A self-contained ground-track SVG: an equirectangular world map (lon −180..180,
/// lat −90..90) with the actual continents (Natural Earth 1:110m land) drawn behind
/// a 30° graticule and the equator, and the sub-satellite track as a polyline broken
/// at the antimeridian. The station, if any, is a small marker. No provenance footer
/// — the central stamp in `api.rs` adds it.
pub fn to_svg(r: &EphemerisResult) -> String {
    let (w, h) = (720.0, 360.0);
    let proj = |lon: f64, lat: f64| -> (f64, f64) {
        ((lon + 180.0) / 360.0 * w, (90.0 - lat) / 180.0 * h)
    };
    let mut svg = format!(
        "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"{w}\" height=\"{h}\" viewBox=\"0 0 {w} {h}\">\
         <rect width=\"{w}\" height=\"{h}\" fill=\"#0c0b08\"/>"
    );
    // The actual landmasses (Natural Earth 1:110m, simplified) as the map base, each
    // ring filled and outlined in the same equirectangular projection as the track. A
    // jump over the antimeridian starts a fresh sub-path so no land smears across.
    for ring in crate::worldmap::LAND {
        let mut d = String::new();
        let mut prev_lon = f64::NAN;
        for &(rlon, rlat) in ring.iter() {
            let (rlon, rlat) = (rlon as f64, rlat as f64);
            let (x, y) = proj(rlon, rlat);
            let cmd = if !prev_lon.is_finite() || (rlon - prev_lon).abs() > 180.0 {
                'M'
            } else {
                'L'
            };
            d.push_str(&format!("{cmd}{x:.1} {y:.1}"));
            prev_lon = rlon;
        }
        d.push('Z');
        svg.push_str(&format!(
            "<path d=\"{d}\" fill=\"#201a11\" stroke=\"#39301f\" stroke-width=\"0.5\"/>"
        ));
    }
    // Graticule every 30°.
    let mut lon = -180.0;
    while lon <= 180.0 {
        let (x, _) = proj(lon, 0.0);
        svg.push_str(&format!(
            "<line x1=\"{x:.1}\" y1=\"0\" x2=\"{x:.1}\" y2=\"{h}\" stroke=\"#262019\" stroke-width=\"1\"/>"
        ));
        lon += 30.0;
    }
    let mut lat = -90.0;
    while lat <= 90.0 {
        let (_, y) = proj(0.0, lat);
        let col = if lat == 0.0 { "#342c21" } else { "#262019" };
        svg.push_str(&format!(
            "<line x1=\"0\" y1=\"{y:.1}\" x2=\"{w}\" y2=\"{y:.1}\" stroke=\"{col}\" stroke-width=\"1\"/>"
        ));
        lat += 30.0;
    }
    // The ground track, split where the longitude wraps the antimeridian.
    let mut seg = String::new();
    let mut prev_lon = f64::NAN;
    let flush = |svg: &mut String, seg: &mut String| {
        if seg.split(' ').filter(|s| !s.is_empty()).count() >= 2 {
            svg.push_str(&format!(
                "<polyline points=\"{}\" fill=\"none\" stroke=\"#e0bd84\" stroke-width=\"1.8\"/>",
                seg.trim()
            ));
        }
        seg.clear();
    };
    for s in &r.samples {
        if prev_lon.is_finite() && (s.lon_deg - prev_lon).abs() > 180.0 {
            flush(&mut svg, &mut seg);
        }
        let (x, y) = proj(s.lon_deg, s.lat_deg);
        seg.push_str(&format!("{x:.1},{y:.1} "));
        prev_lon = s.lon_deg;
    }
    flush(&mut svg, &mut seg);
    // Start marker.
    if let Some(s0) = r.samples.first() {
        let (x, y) = proj(s0.lon_deg, s0.lat_deg);
        svg.push_str(&format!(
            "<circle cx=\"{x:.1}\" cy=\"{y:.1}\" r=\"3.5\" fill=\"#f1ece2\" stroke=\"#0c0b08\" stroke-width=\"0.8\"/>"
        ));
    }
    let title = format!(
        "{} ground track · {} samples · alt {:.0}{:.0} km · |lat| ≤ {:.1}°",
        r.source,
        r.n_samples,
        r.alt_min_km,
        r.alt_max_km,
        r.lat_max_deg.abs().max(r.lat_min_deg.abs()),
    );
    svg.push_str(&format!(
        "<text x=\"10\" y=\"20\" fill=\"#e6edf3\" font-family=\"sans-serif\" font-size=\"13\">{}</text>",
        esc(&title)
    ));
    svg.push_str("</svg>");
    svg
}

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

    // A real published ISS (ZARYA) two-line element set. Inclination 51.64° is the
    // recognisable ISS value the ground-track latitude band must respect.
    const ISS_TLE: &str = "\
1 25544U 98067A   20045.18587073  .00000950  00000-0  25302-4 0  9990
2 25544  51.6443 242.0161 0004885 264.6060 207.3845 15.49165514212791";

    fn iss_scenario() -> EphemerisScenario {
        EphemerisScenario {
            kind: Some("ephemeris".into()),
            tle: Some(ISS_TLE.into()),
            orbit: None,
            epoch: None,
            step_s: 30.0,
            duration_s: 5_580.0, // ~one ISS revolution
            station: None,
            dut1_s: 0.0,
            xp_arcsec: 0.0,
            yp_arcsec: 0.0,
            carrier_hz: GPS_L1_HZ,
        }
    }

    #[test]
    fn ground_track_latitude_stays_within_inclination_band() {
        // A satellite can never reach a sub-satellite latitude above its orbital
        // inclination (plus a fraction of a degree of geodetic-vs-geocentric slack).
        let r = run_ephemeris(&iss_scenario()).unwrap();
        let max_abs_lat = r.lat_max_deg.abs().max(r.lat_min_deg.abs());
        assert!(
            max_abs_lat <= 52.0,
            "max |lat| {max_abs_lat:.2}° exceeds the 51.64° ISS inclination band"
        );
        // ...and it actually reaches the high latitudes (not a degenerate equatorial run).
        assert!(
            max_abs_lat > 50.0,
            "ground track only reached |lat| {max_abs_lat:.2}°"
        );
    }

    #[test]
    fn altitude_and_speed_are_in_the_iss_leo_regime() {
        let r = run_ephemeris(&iss_scenario()).unwrap();
        // ISS altitude ~400–420 km; allow a generous LEO band.
        assert!(
            r.alt_min_km > 350.0 && r.alt_max_km < 470.0,
            "alt {:.0}–{:.0} km",
            r.alt_min_km,
            r.alt_max_km
        );
        // ISS orbital speed ~7.66 km/s.
        assert!(
            r.speed_min_m_s > 7_400.0 && r.speed_max_m_s < 7_900.0,
            "speed {:.0}–{:.0} m/s",
            r.speed_min_m_s,
            r.speed_max_m_s
        );
    }

    #[test]
    fn frames_preserve_magnitude_and_velocity_is_nonzero() {
        // GCRS and TEME are rotations of one another: |r| and |v| are invariant. The
        // ECEF position is also a rotation of TEME (Earth-fixed), so |r| matches too.
        let r = run_ephemeris(&iss_scenario()).unwrap();
        for s in &r.samples {
            let rt = norm(s.teme_r_m);
            assert!((norm(s.gcrs_r_m) - rt).abs() < 1.0, "|r| GCRS vs TEME");
            assert!((norm(s.ecef_r_m) - rt).abs() < 1.0, "|r| ECEF vs TEME");
            assert!(
                (norm(s.gcrs_v_m_s) - norm(s.teme_v_m_s)).abs() < 1e-3,
                "|v| GCRS vs TEME"
            );
            assert!(
                norm(s.teme_v_m_s) > 1_000.0,
                "velocity must be exposed and non-zero"
            );
        }
    }

    #[test]
    fn ground_track_longitude_regresses_westward_over_a_revolution() {
        // For a prograde LEO the track shifts west ~22.5° each revolution as the
        // Earth turns underneath: the longitudes must actually move (not be frozen).
        let r = run_ephemeris(&iss_scenario()).unwrap();
        let spread = r
            .samples
            .windows(2)
            .map(|w| (w[1].lon_deg - w[0].lon_deg).abs())
            .filter(|d| *d < 180.0) // ignore antimeridian wraps
            .fold(0.0_f64, f64::max);
        assert!(spread > 0.1, "ground track longitude is not advancing");
    }

    /// The worst |reported range-rate − Richardson-FD(range)| (m/s) over the interior
    /// of a run, with the station placed off-track for smooth geometry. The Richardson
    /// (O(h⁴)) finite difference of the geometric range is the exact d(range)/dt, so
    /// this isolates the implementation of `range_rate = (v_sat − v_station)·û`.
    fn worst_range_rate_vs_fd(scn: &EphemerisScenario) -> (f64, usize) {
        let r = run_ephemeris(scn).unwrap();
        let st = scn.station.unwrap().geodetic();
        let (prop, _, _) = build(scn).unwrap();
        let range_at = |t: f64| -> f64 {
            let jd_utc = r.jd_utc0 + t / 86_400.0;
            let jd_ut1 = utc_to_ut1(jd_utc, scn.dut1_s);
            let jd_tt = utc_to_tt(jd_utc);
            let ecef = teme_to_itrf(prop.state_eci(t).r_m, jd_ut1, 0.0, 0.0, jd_tt);
            look_angles(st, ecef).range_m
        };
        let h = 0.25_f64;
        let deriv = |t: f64| -> f64 {
            let d1 = (range_at(t + h) - range_at(t - h)) / (2.0 * h);
            let d2 = (range_at(t + 2.0 * h) - range_at(t - 2.0 * h)) / (4.0 * h);
            (4.0 * d1 - d2) / 3.0
        };
        let (mut worst, mut checked) = (0.0_f64, 0usize);
        for s in &r.samples {
            if s.t_s < 2.0 * h || s.t_s > scn.duration_s - 2.0 * h {
                continue;
            }
            worst = worst.max((deriv(s.t_s) - s.station_view.unwrap().range_rate_m_s).abs());
            checked += 1;
        }
        (worst, checked)
    }

    #[test]
    fn exposed_velocity_is_the_true_position_derivative_kepler() {
        // TIGHT, Julian-Date-noise-free battle test of the exposed velocity. The
        // radial range-rate to the geocentre is d|r|/dt = v·r̂ — it has NO sidereal-
        // time term, so unlike a station range-rate it is not limited by the single-
        // f64 JD resolution (~47 µs near J2000+). Computed from the exposed (r, v) it
        // must equal the Richardson FD of |r(t)| to << 1 mm/s, proving the velocity is
        // exposed as the genuine derivative of the position and the projection is
        // correct. (The Keplerian velocity is a ½-s central difference of position,
        // whose ~0.4 mm/s truncation is the only residual — hence the 2 mm/s bound.)
        let scn = EphemerisScenario {
            kind: Some("ephemeris".into()),
            tle: None,
            orbit: Some(OrbitCfg {
                altitude_km: 550.0,
                inclination_deg: 53.0,
                raan_deg: 0.0,
                u0_deg: 0.0,
                eccentricity: 0.0,
                argp_deg: 0.0,
                j2: false,
            }),
            epoch: Some(EpochUtc {
                year: 2024,
                month: 1,
                day: 1,
                hour: 0,
                minute: 0,
                second: 0.0,
            }),
            step_s: 30.0,
            duration_s: 3_000.0,
            station: None,
            dut1_s: 0.0,
            xp_arcsec: 0.0,
            yp_arcsec: 0.0,
            carrier_hz: GPS_L1_HZ,
        };
        let r = run_ephemeris(&scn).unwrap();
        let (prop, _, _) = build(&scn).unwrap();
        let rad_at = |t: f64| norm(prop.state_eci(t).r_m);
        let h = 0.25_f64;
        let deriv = |t: f64| -> f64 {
            let d1 = (rad_at(t + h) - rad_at(t - h)) / (2.0 * h);
            let d2 = (rad_at(t + 2.0 * h) - rad_at(t - 2.0 * h)) / (4.0 * h);
            (4.0 * d1 - d2) / 3.0
        };
        let (mut worst, mut checked) = (0.0_f64, 0usize);
        for s in &r.samples {
            if s.t_s < 2.0 * h || s.t_s > scn.duration_s - 2.0 * h {
                continue;
            }
            let rmag = norm(s.teme_r_m);
            let pred = (s.teme_v_m_s[0] * s.teme_r_m[0]
                + s.teme_v_m_s[1] * s.teme_r_m[1]
                + s.teme_v_m_s[2] * s.teme_r_m[2])
                / rmag; // v·r̂
            worst = worst.max((pred - deriv(s.t_s)).abs());
            checked += 1;
        }
        assert!(checked > 50, "not enough interior samples ({checked})");
        eprintln!("kepler worst |v·r̂ − d|r|/dt| = {worst:.2e} m/s over {checked} samples");
        assert!(
            worst < 2e-3,
            "exposed velocity ≠ position derivative: {worst:.3e} m/s (> 2 mm/s)"
        );
    }

    #[test]
    fn station_range_rate_matches_range_derivative_within_jd_resolution() {
        // The full Earth-rotating-station range-rate (hence Doppler) matches a
        // Richardson FD of the geometric range to ~0.03 m/s for both the analytic
        // orbit and SGP4. The residual is dominated by the single-f64 Julian-Date
        // resolution (~47 µs) in the FD reference's sidereal-time term — a property of
        // the finite-difference REFERENCE, NOT of the production range-rate, which is
        // fully analytic (the station inertial velocity is ω × r, no JD differencing).
        // The exposed velocity itself is validated JD-free to < 1 mm/s above (Kepler)
        // and to 1 mm/s against the official tcppver vectors (SGP4).
        let kepler = EphemerisScenario {
            kind: Some("ephemeris".into()),
            tle: None,
            orbit: Some(OrbitCfg {
                altitude_km: 550.0,
                inclination_deg: 53.0,
                raan_deg: 0.0,
                u0_deg: 0.0,
                eccentricity: 0.0,
                argp_deg: 0.0,
                j2: false,
            }),
            epoch: Some(EpochUtc {
                year: 2024,
                month: 1,
                day: 1,
                hour: 0,
                minute: 0,
                second: 0.0,
            }),
            step_s: 30.0,
            duration_s: 3_000.0,
            station: Some(StationCfg {
                lat_deg: 20.0,
                lon_deg: 10.0,
                alt_m: 100.0,
            }),
            dut1_s: 0.0,
            xp_arcsec: 0.0,
            yp_arcsec: 0.0,
            carrier_hz: GPS_L1_HZ,
        };
        let (wk, ck) = worst_range_rate_vs_fd(&kepler);
        eprintln!("kepler station worst |range_rate − FD| = {wk:.2e} m/s over {ck} samples");
        assert!(
            ck > 50 && wk < 0.06,
            "kepler station range-rate vs FD: {wk:.3e} m/s"
        );

        let mut sgp4 = iss_scenario();
        let probe = run_ephemeris(&sgp4).unwrap();
        let mid = &probe.samples[probe.samples.len() / 2];
        let st_lat = (mid.lat_deg + 25.0).clamp(-80.0, 80.0);
        sgp4.station = Some(StationCfg {
            lat_deg: st_lat,
            lon_deg: mid.lon_deg,
            alt_m: 100.0,
        });
        sgp4.step_s = 30.0;
        sgp4.duration_s = 3_000.0;
        let (ws, cs) = worst_range_rate_vs_fd(&sgp4);
        eprintln!("sgp4 station worst |range_rate − FD| = {ws:.2e} m/s over {cs} samples");
        assert!(
            cs > 50 && ws < 0.06,
            "sgp4 station range-rate vs FD: {ws:.3e} m/s"
        );
    }

    #[test]
    fn doppler_sign_and_magnitude_are_physical() {
        // L1 Doppler for a LEO pass is a few tens of kHz peak; positive (closing)
        // before TCA and negative (receding) after. Place the station at a computed
        // sub-satellite point so a near-overhead pass is guaranteed, then check the
        // peak magnitude band and the sign change across closest approach.
        let mut scn = iss_scenario();
        let probe = run_ephemeris(&scn).unwrap();
        let mid = &probe.samples[probe.samples.len() / 2];
        scn.station = Some(StationCfg {
            lat_deg: mid.lat_deg,
            lon_deg: mid.lon_deg,
            alt_m: 0.0,
        });
        scn.step_s = 5.0;
        let r = run_ephemeris(&scn).unwrap();
        let dopps: Vec<f64> = r
            .samples
            .iter()
            .filter_map(|s| s.station_view)
            .filter(|v| v.visible)
            .map(|v| v.doppler_hz)
            .collect();
        assert!(!dopps.is_empty(), "no visible pass found");
        let peak = dopps.iter().fold(0.0_f64, |m, d| m.max(d.abs()));
        assert!(
            (8_000.0..70_000.0).contains(&peak),
            "peak Doppler {peak:.0} Hz out of LEO L1 band"
        );
        assert!(
            dopps.iter().any(|d| *d > 0.0) && dopps.iter().any(|d| *d < 0.0),
            "Doppler should change sign across TCA"
        );
    }

    #[test]
    fn analytic_orbit_path_runs_with_an_explicit_epoch() {
        let scn = EphemerisScenario {
            kind: Some("ephemeris".into()),
            tle: None,
            orbit: Some(OrbitCfg {
                altitude_km: 550.0,
                inclination_deg: 53.0,
                raan_deg: 0.0,
                u0_deg: 0.0,
                eccentricity: 0.0,
                argp_deg: 0.0,
                j2: true,
            }),
            epoch: Some(EpochUtc {
                year: 2024,
                month: 1,
                day: 1,
                hour: 0,
                minute: 0,
                second: 0.0,
            }),
            step_s: 60.0,
            duration_s: 5_700.0,
            station: None,
            dut1_s: 0.0,
            xp_arcsec: 0.0,
            yp_arcsec: 0.0,
            carrier_hz: GPS_L1_HZ,
        };
        let r = run_ephemeris(&scn).unwrap();
        assert_eq!(r.source, "kepler");
        // A 53° inclination orbit reaches ~53° latitude.
        assert!(r.lat_max_deg.abs().max(r.lat_min_deg.abs()) > 50.0);
        // Velocity is exposed for the analytic orbit too.
        assert!(r.speed_min_m_s > 7_000.0);
    }

    #[test]
    fn analytic_orbit_without_epoch_is_an_error() {
        let mut scn = iss_scenario();
        scn.tle = None;
        scn.orbit = Some(OrbitCfg {
            altitude_km: 500.0,
            inclination_deg: 45.0,
            raan_deg: 0.0,
            u0_deg: 0.0,
            eccentricity: 0.0,
            argp_deg: 0.0,
            j2: false,
        });
        scn.epoch = None;
        assert!(run_ephemeris(&scn).is_err());
    }

    #[test]
    fn svg_renders_and_hash_is_stable() {
        let r = run_ephemeris(&iss_scenario()).unwrap();
        let svg = to_svg(&r);
        assert!(svg.starts_with("<svg") && svg.trim_end().ends_with("</svg>"));
        assert!(svg.contains("polyline"), "ground track polyline present");
        // The actual world map is drawn behind the track: one filled <path> per
        // landmass ring, so the track reads over real continents — not a bare grid.
        let land_paths = svg.matches("<path").count();
        assert!(
            land_paths >= 50,
            "expected the world landmasses to be drawn, found only {land_paths} <path> elements"
        );
        assert_eq!(
            land_paths,
            crate::worldmap::LAND.len(),
            "one path per land ring"
        );
        // No own provenance footer — the central stamp owns that.
        assert!(!svg.contains("kshana.dev"));
        assert_eq!(
            r.scenario_hash,
            run_ephemeris(&iss_scenario()).unwrap().scenario_hash
        );
    }
}