sidereon-core 0.11.1

The complete Sidereon engine: numerical astrodynamics propagation core plus the GNSS domain layer (SP3, broadcast ephemeris, multi-GNSS positioning, RTK/PPP, ionosphere/troposphere, DOP) behind a default-on gnss feature
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
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
//! Coordinate transformation pipeline.
//!
//! TEME -> GCRS replicates Skyfield's exact computation path including AU/day
//! unit scaling for bit-exact (0 ULP) parity.
//!
//! Also provides GCRS -> ITRS, ITRS -> geodetic (WGS84), and topocentric
//! (az/el/range) transformations.
//!
//! The pure compute functions live here in the core crate; the Rustler
//! decode/encode shims that used to wrap them stay in `orbis_nif` as glue, so
//! no domain formula lives in the NIF layer. The numerics, summation order,
//! transcendental sequence, and the single sanctioned `mul_add` site
//! (`mat3_vec3_mul_fma`) are preserved exactly so the existing Skyfield 0-ULP
//! parity holds.

use crate::astro::frames::nutation::{
    build_skyfield_nutation_matrix_unchecked,
    skyfield_equation_of_the_equinoxes_complimentary_terms_unchecked,
    skyfield_iau2000a_radians_unchecked, skyfield_mean_obliquity_radians_unchecked,
};
use crate::astro::frames::precession::{
    build_icrs_to_j2000, compute_skyfield_precession_matrix_unchecked,
};
use crate::astro::math::mat3::{inline_mxmxm, inline_rxr, inline_tr, Mat3};
use crate::astro::time::{civil, scales::TimeScales};
use crate::astro::{
    constants::astro::AU_KM,
    constants::earth::{WGS84_A_KM, WGS84_E2, WGS84_F},
    constants::geometry::AZIMUTH_ZENITH_EPS,
    constants::models::proj::{
        HALF_PI as PROJ_HALF_PI, RAD_TO_DEG as PROJ_RAD_TO_DEG, WGS84_A_M as PROJ_WGS84_A_M,
        WGS84_B_M as PROJ_WGS84_B_M, WGS84_E2S as PROJ_WGS84_E2S, WGS84_ES as PROJ_WGS84_ES,
    },
    constants::time::{DAYS_PER_JULIAN_CENTURY, J2000_JD, SECONDS_PER_DAY},
};

const TAU: f64 = std::f64::consts::TAU;
const ARCSECONDS_TO_RADIANS: f64 = 4.848_136_811_095_36e-6;

/// Error returned when public frame-transform inputs are outside the valid domain.
#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
pub enum FrameTransformError {
    /// A transform input was non-finite or otherwise invalid.
    #[error("invalid frame transform {field}: {reason}")]
    InvalidInput {
        field: &'static str,
        reason: &'static str,
    },
}

fn invalid_input(field: &'static str, reason: &'static str) -> FrameTransformError {
    FrameTransformError::InvalidInput { field, reason }
}

fn validate_finite(field: &'static str, value: f64) -> Result<(), FrameTransformError> {
    if value.is_finite() {
        Ok(())
    } else {
        Err(invalid_input(field, "must be finite"))
    }
}

fn validate_vec3(field: &'static str, values: &[f64; 3]) -> Result<(), FrameTransformError> {
    for value in values {
        if !value.is_finite() {
            return Err(invalid_input(field, "components must be finite"));
        }
    }
    Ok(())
}

fn validate_tuple3(field: &'static str, values: Vec3) -> Result<Vec3, FrameTransformError> {
    if values.0.is_finite() && values.1.is_finite() && values.2.is_finite() {
        Ok(values)
    } else {
        Err(invalid_input(field, "components must be finite"))
    }
}

fn validate_array3(field: &'static str, values: [f64; 3]) -> Result<[f64; 3], FrameTransformError> {
    validate_vec3(field, &values)?;
    Ok(values)
}

fn validate_mat3(field: &'static str, values: Mat3) -> Result<Mat3, FrameTransformError> {
    for row in &values {
        validate_vec3(field, row)?;
    }
    Ok(values)
}

fn validate_time_scales(ts: &TimeScales) -> Result<(), FrameTransformError> {
    validate_finite("jd_whole", ts.jd_whole)?;
    validate_finite("ut1_fraction", ts.ut1_fraction)?;
    validate_finite("tt_fraction", ts.tt_fraction)?;
    validate_finite("tdb_fraction", ts.tdb_fraction)?;
    validate_finite("jd_ut1", ts.jd_ut1)?;
    validate_finite("jd_tt", ts.jd_tt)?;
    validate_finite("jd_tdb", ts.jd_tdb)
}

fn validate_polar_motion(pole: PolarMotion) -> Result<(), FrameTransformError> {
    validate_finite("xp_rad", pole.xp_rad)?;
    validate_finite("yp_rad", pole.yp_rad)
}

fn validate_geodetic_degrees_km(
    latitude_deg: f64,
    longitude_deg: f64,
    altitude_km: f64,
) -> Result<(), FrameTransformError> {
    validate_finite("latitude_deg", latitude_deg)?;
    if !(-90.0..=90.0).contains(&latitude_deg) {
        return Err(invalid_input("latitude_deg", "must be in [-90, 90]"));
    }
    validate_finite("longitude_deg", longitude_deg)?;
    if !(-180.0..=180.0).contains(&longitude_deg) {
        return Err(invalid_input("longitude_deg", "must be in [-180, 180]"));
    }
    validate_finite("altitude_km", altitude_km)
}

/// A bare Cartesian triple (km or km/s depending on context).
///
/// This is the internal compute-layer return shape. Typed input structs
/// ([`TemeStateKm`], [`GeodeticStationKm`]) bundle the public entry points'
/// arguments, but the numerics below operate on raw triples to preserve the
/// original operation order exactly.
pub type Vec3 = (f64, f64, f64);

/// TEME-frame position and velocity (km, km/s): the input to
/// [`teme_to_gcrs_compute`].
pub struct TemeStateKm {
    pub position_km: [f64; 3],
    pub velocity_km_s: [f64; 3],
}

/// Geodetic ground-station position (WGS84) for topocentric look angles.
pub struct GeodeticStationKm {
    pub latitude_deg: f64,
    pub longitude_deg: f64,
    pub altitude_km: f64,
}

/// Polar-motion coordinates of the Celestial Intermediate Pole.
///
/// `xp_rad` and `yp_rad` are radians. The embedded EOP table currently carries
/// UT1-UTC only, so the historical transforms use [`PolarMotion::ZERO`] by
/// default. Precision callers with pole coordinates should use the explicit
/// `*_with_polar_motion` entry points below.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct PolarMotion {
    pub xp_rad: f64,
    pub yp_rad: f64,
}

impl PolarMotion {
    /// No polar-motion rotation; preserves the historical transform exactly.
    pub const ZERO: Self = Self {
        xp_rad: 0.0,
        yp_rad: 0.0,
    };

    /// Construct polar-motion coordinates from radians.
    pub fn from_radians(xp_rad: f64, yp_rad: f64) -> Result<Self, FrameTransformError> {
        validate_finite("xp_rad", xp_rad)?;
        validate_finite("yp_rad", yp_rad)?;
        Ok(Self { xp_rad, yp_rad })
    }

    /// Construct polar-motion coordinates from arcseconds.
    pub fn from_arcseconds(xp_arcsec: f64, yp_arcsec: f64) -> Result<Self, FrameTransformError> {
        validate_finite("xp_arcsec", xp_arcsec)?;
        validate_finite("yp_arcsec", yp_arcsec)?;
        Self::from_radians(
            xp_arcsec * ARCSECONDS_TO_RADIANS,
            yp_arcsec * ARCSECONDS_TO_RADIANS,
        )
    }

    fn is_zero(self) -> bool {
        self.xp_rad == 0.0 && self.yp_rad == 0.0
    }
}

impl Default for PolarMotion {
    fn default() -> Self {
        Self::ZERO
    }
}

/// Final matrix-vector multiply using explicit FMA.
/// This matches numpy's vectorized behavior and is the ONLY place
/// where f64::mul_add() should be used.
fn mat3_vec3_mul_fma(r: &Mat3, p: &[f64; 3]) -> [f64; 3] {
    let mut result = [0.0_f64; 3];
    for i in 0..3 {
        let sum = r[i][0] * p[0];
        let sum = f64::mul_add(r[i][1], p[1], sum);
        let sum = f64::mul_add(r[i][2], p[2], sum);
        result[i] = sum;
    }
    result
}

fn build_rot_z(angle: f64) -> Mat3 {
    let c = angle.cos();
    let s = angle.sin();
    [[c, -s, 0.0], [s, c, 0.0], [0.0, 0.0, 1.0]]
}

/// IERS polar-motion matrix, omitting the tiny TIO locator term `s'`.
///
/// The matrix maps TIRS pseudo-Earth-fixed coordinates to ITRS:
/// `W = R_y(xp) * R_x(yp)`, whose small-angle form is
/// `[[1, 0, xp], [0, 1, -yp], [-xp, yp, 1]]`.
pub fn polar_motion_matrix(pole: PolarMotion) -> Result<Mat3, FrameTransformError> {
    validate_polar_motion(pole)?;
    validate_mat3("polar_motion_matrix", polar_motion_matrix_unchecked(pole))
}

fn polar_motion_matrix_unchecked(pole: PolarMotion) -> Mat3 {
    if pole.is_zero() {
        return [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]];
    }

    let cx = pole.xp_rad.cos();
    let sx = pole.xp_rad.sin();
    let cy = pole.yp_rad.cos();
    let sy = pole.yp_rad.sin();

    [
        [cx, sx * sy, sx * cy],
        [0.0, cy, -sy],
        [-sx, cx * sy, cx * cy],
    ]
}

fn apply_polar_motion_to_itrs_matrix(mat: Mat3, pole: PolarMotion) -> Mat3 {
    if pole.is_zero() {
        mat
    } else {
        inline_rxr(&polar_motion_matrix_unchecked(pole), &mat)
    }
}

fn earth_rotation_angle(jd_whole: f64, ut1_fraction: f64) -> f64 {
    let days_since_j2000 = jd_whole - J2000_JD + ut1_fraction;
    // Force separate rounded operations to match Skyfield/Python's path.
    let spins_since_j2000: f64 = {
        let v = 0.00273781191135448 * days_since_j2000;
        // Use black_box-like pattern to prevent optimization
        let v_stored: f64 = v;
        v_stored
    };
    let th = 0.7790572732640 + spins_since_j2000;
    let mut result = (th % 1.0 + jd_whole % 1.0 + ut1_fraction) % 1.0;
    if result < 0.0 {
        result += 1.0;
    }
    result
}

fn compute_theta_gmst1982(jd_whole: f64, ut1_fraction: f64) -> f64 {
    let t = (jd_whole - J2000_JD + ut1_fraction) / DAYS_PER_JULIAN_CENTURY;
    let g = 67310.54841 + (8640184.812866 + (0.093104 + (-6.2e-6) * t) * t) * t;
    let mut theta = ((jd_whole % 1.0) + ut1_fraction + (g / SECONDS_PER_DAY) % 1.0) % 1.0 * TAU;
    if theta < 0.0 {
        theta += TAU;
    }
    theta
}

fn sidereal_time_hours(jd_whole: f64, ut1_fraction: f64, tdb_fraction: f64) -> f64 {
    let theta = earth_rotation_angle(jd_whole, ut1_fraction);
    let t = (jd_whole - J2000_JD + tdb_fraction) / DAYS_PER_JULIAN_CENTURY;
    let st = 0.014506
        + ((((-0.0000000368 * t - 0.000029956) * t - 0.00000044) * t + 1.3915817) * t
            + 4612.156534)
            * t;
    let mut result = (st / 54000.0 + theta * 24.0) % 24.0;
    if result < 0.0 {
        result += 24.0;
    }
    result
}

fn gast_radians(ts: &TimeScales, dpsi: f64) -> f64 {
    let gmst_hours = sidereal_time_hours(ts.jd_whole, ts.ut1_fraction, ts.tdb_fraction);
    let mean_ob = skyfield_mean_obliquity_radians_unchecked(ts.jd_tdb);
    let c_terms = skyfield_equation_of_the_equinoxes_complimentary_terms_unchecked(ts.jd_tt);
    let eq_eq = dpsi * mean_ob.cos() + c_terms;
    let mut gast_hours = (gmst_hours + eq_eq / TAU * 24.0) % 24.0;
    if gast_hours < 0.0 {
        gast_hours += 24.0;
    }
    gast_hours / 24.0 * TAU
}

/// Greenwich Mean Sidereal Time for an instant, radians in `[0, 2pi)`.
///
/// The IAU-1982 GMST used internally by the frame pipeline, surfaced as a public
/// entry point. This is a thin wrapper over the existing private sidereal-time
/// computation: it adds no new numerics, so the value is bit-identical to the
/// quantity the transforms consume.
pub fn greenwich_mean_sidereal_time_radians(ts: &TimeScales) -> Result<f64, FrameTransformError> {
    validate_time_scales(ts)?;
    let radians = greenwich_mean_sidereal_time_radians_unchecked(ts);
    validate_finite("gmst_radians", radians)?;
    Ok(radians)
}

fn greenwich_mean_sidereal_time_radians_unchecked(ts: &TimeScales) -> f64 {
    let hours = sidereal_time_hours(ts.jd_whole, ts.ut1_fraction, ts.tdb_fraction);
    hours / 24.0 * TAU
}

/// IAU-1982 GMST in radians from continuous seconds past J2000.
///
/// The input epoch is treated as UT1 for this thin drag helper. It reuses the
/// same `compute_theta_gmst1982` polynomial as
/// [`greenwich_mean_sidereal_time_radians`] and adds no new sidereal-time math.
pub fn greenwich_mean_sidereal_time_radians_from_j2000_seconds(
    sec: f64,
) -> Result<f64, FrameTransformError> {
    validate_finite("sec", sec)?;
    let (jd_whole, ut1_fraction) = civil::split_julian_date_add_seconds(J2000_JD, 0.0, sec);
    let mut radians = compute_theta_gmst1982(jd_whole, ut1_fraction) % TAU;
    if radians < 0.0 {
        radians += TAU;
    }
    validate_finite("gmst_radians", radians)?;
    Ok(radians)
}

/// Greenwich Apparent Sidereal Time for an instant, radians in `[0, 2pi)`.
///
/// GMST plus the equation of the equinoxes (nutation in longitude projected on
/// the true equator, with the IAU 2000 complementary terms). A thin wrapper over
/// the existing private GAST computation; bit-identical to the value the
/// GCRS<->ITRS transforms apply.
pub fn greenwich_apparent_sidereal_time_radians(
    ts: &TimeScales,
) -> Result<f64, FrameTransformError> {
    validate_time_scales(ts)?;
    let radians = greenwich_apparent_sidereal_time_radians_unchecked(ts);
    validate_finite("gast_radians", radians)?;
    Ok(radians)
}

fn greenwich_apparent_sidereal_time_radians_unchecked(ts: &TimeScales) -> f64 {
    let (dpsi, _deps) = skyfield_iau2000a_radians_unchecked(ts.jd_tt);
    gast_radians(ts, dpsi)
}

/// Build the TEME->GCRS rotation matrix T from time scales.
fn build_teme_to_gcrs_matrix(ts: &TimeScales, skyfield_compat: bool) -> Mat3 {
    let (dpsi, deps) = skyfield_iau2000a_radians_unchecked(ts.jd_tt);
    let mean_ob = skyfield_mean_obliquity_radians_unchecked(ts.jd_tdb);
    let true_ob = mean_ob + deps;

    let n = build_skyfield_nutation_matrix_unchecked(mean_ob, true_ob, dpsi);
    let p = compute_skyfield_precession_matrix_unchecked(ts.jd_tdb);
    let b = build_icrs_to_j2000();

    // Skyfield uses Kahan-compensated triple product (matching numpy einsum).
    // Direct mode uses standard sequential multiply (more precise).
    let m = if skyfield_compat {
        inline_mxmxm(&n, &p, &b)
    } else {
        let np = inline_rxr(&n, &p);
        inline_rxr(&np, &b)
    };

    let gast = gast_radians(ts, dpsi);
    let theta = compute_theta_gmst1982(ts.jd_whole, ts.ut1_fraction);
    let angle = theta - gast;

    let r = build_rot_z(angle);
    let g = inline_rxr(&r, &m);
    inline_tr(&g)
}

/// Build the TEME->GCRS rotation matrix T from time scales.
pub(crate) fn teme_to_gcrs_matrix(ts: &TimeScales, skyfield_compat: bool) -> Mat3 {
    build_teme_to_gcrs_matrix(ts, skyfield_compat)
}

/// Standard (non-FMA) matrix-vector multiply.
pub fn mat3_vec3_mul(r: &Mat3, p: &[f64; 3]) -> Result<[f64; 3], FrameTransformError> {
    validate_mat3("matrix", *r)?;
    validate_vec3("vector", p)?;
    validate_array3("matrix_vector_product", mat3_vec3_mul_unchecked(r, p))
}

pub(crate) fn mat3_vec3_mul_unchecked(r: &Mat3, p: &[f64; 3]) -> [f64; 3] {
    let mut result = [0.0_f64; 3];
    for i in 0..3 {
        let mut sum = 0.0;
        for j in 0..3 {
            sum += r[i][j] * p[j];
        }
        result[i] = sum;
    }
    result
}

/// Core TEME->GCRS transform. Returns ((px,py,pz), (vx,vy,vz)).
pub fn teme_to_gcrs_compute(
    state: &TemeStateKm,
    ts: &TimeScales,
    skyfield_compat: bool,
) -> Result<(Vec3, Vec3), FrameTransformError> {
    validate_time_scales(ts)?;
    validate_vec3("position_km", &state.position_km)?;
    validate_vec3("velocity_km_s", &state.velocity_km_s)?;
    let (position, velocity) = teme_to_gcrs_compute_unchecked(state, ts, skyfield_compat);
    Ok((
        validate_tuple3("gcrs_position_km", position)?,
        validate_tuple3("gcrs_velocity_km_s", velocity)?,
    ))
}

fn teme_to_gcrs_compute_unchecked(
    state: &TemeStateKm,
    ts: &TimeScales,
    skyfield_compat: bool,
) -> (Vec3, Vec3) {
    let [x, y, z] = state.position_km;
    let [vx, vy, vz] = state.velocity_km_s;
    let t = build_teme_to_gcrs_matrix(ts, skyfield_compat);

    if skyfield_compat {
        // AU/day scaling + FMA multiply matching Skyfield's _at() path.
        let r_au = [x / AU_KM, y / AU_KM, z / AU_KM];
        let r_gcrs_au = mat3_vec3_mul_fma(&t, &r_au);
        let r_gcrs = (
            r_gcrs_au[0] * AU_KM,
            r_gcrs_au[1] * AU_KM,
            r_gcrs_au[2] * AU_KM,
        );

        let v_au_d = [
            vx / AU_KM * SECONDS_PER_DAY,
            vy / AU_KM * SECONDS_PER_DAY,
            vz / AU_KM * SECONDS_PER_DAY,
        ];
        let v_gcrs_au_d = mat3_vec3_mul_fma(&t, &v_au_d);
        let v_gcrs = (
            v_gcrs_au_d[0] * AU_KM / SECONDS_PER_DAY,
            v_gcrs_au_d[1] * AU_KM / SECONDS_PER_DAY,
            v_gcrs_au_d[2] * AU_KM / SECONDS_PER_DAY,
        );
        (r_gcrs, v_gcrs)
    } else {
        // Direct km/s multiply -- no AU round-trip, no FMA.
        let r_teme = [x, y, z];
        let r_g = mat3_vec3_mul_unchecked(&t, &r_teme);
        let v_teme = [vx, vy, vz];
        let v_g = mat3_vec3_mul_unchecked(&t, &v_teme);
        ((r_g[0], r_g[1], r_g[2]), (v_g[0], v_g[1], v_g[2]))
    }
}

/// Core GCRS->TEME transform. Returns ((px,py,pz), (vx,vy,vz)).
///
/// This is the rotational inverse of [`teme_to_gcrs_compute`]: it uses the
/// transpose of the same orthogonal matrix and adds no new precession or
/// nutation math.
pub fn gcrs_to_teme_compute(
    state: &TemeStateKm,
    ts: &TimeScales,
    skyfield_compat: bool,
) -> Result<(Vec3, Vec3), FrameTransformError> {
    validate_time_scales(ts)?;
    validate_vec3("position_km", &state.position_km)?;
    validate_vec3("velocity_km_s", &state.velocity_km_s)?;
    let (position, velocity) = gcrs_to_teme_compute_unchecked(state, ts, skyfield_compat);
    Ok((
        validate_tuple3("teme_position_km", position)?,
        validate_tuple3("teme_velocity_km_s", velocity)?,
    ))
}

fn gcrs_to_teme_compute_unchecked(
    state: &TemeStateKm,
    ts: &TimeScales,
    skyfield_compat: bool,
) -> (Vec3, Vec3) {
    let [x, y, z] = state.position_km;
    let [vx, vy, vz] = state.velocity_km_s;
    let t = inline_tr(&build_teme_to_gcrs_matrix(ts, skyfield_compat));

    if skyfield_compat {
        let r_au = [x / AU_KM, y / AU_KM, z / AU_KM];
        let r_teme_au = mat3_vec3_mul_fma(&t, &r_au);
        let r_teme = (
            r_teme_au[0] * AU_KM,
            r_teme_au[1] * AU_KM,
            r_teme_au[2] * AU_KM,
        );

        let v_au_d = [
            vx / AU_KM * SECONDS_PER_DAY,
            vy / AU_KM * SECONDS_PER_DAY,
            vz / AU_KM * SECONDS_PER_DAY,
        ];
        let v_teme_au_d = mat3_vec3_mul_fma(&t, &v_au_d);
        let v_teme = (
            v_teme_au_d[0] * AU_KM / SECONDS_PER_DAY,
            v_teme_au_d[1] * AU_KM / SECONDS_PER_DAY,
            v_teme_au_d[2] * AU_KM / SECONDS_PER_DAY,
        );
        (r_teme, v_teme)
    } else {
        let r_gcrs = [x, y, z];
        let r_t = mat3_vec3_mul_unchecked(&t, &r_gcrs);
        let v_gcrs = [vx, vy, vz];
        let v_t = mat3_vec3_mul_unchecked(&t, &v_gcrs);
        ((r_t[0], r_t[1], r_t[2]), (v_t[0], v_t[1], v_t[2]))
    }
}

// ---------------------------------------------------------------------------
// GCRS -> ITRS (Earth-fixed / ECEF)
// ---------------------------------------------------------------------------

/// GCRS to true equator and equinox of date rotation.
///
/// This is the Earth-rotation-free `N * P * B` product already used inside the
/// GCRS to ITRS construction: nutation, precession, and frame bias.
pub fn gcrs_to_true_of_date_matrix(ts: &TimeScales) -> Result<Mat3, FrameTransformError> {
    validate_time_scales(ts)?;
    let (matrix, _dpsi) = gcrs_to_true_of_date_matrix_parts_unchecked(ts);
    validate_mat3("gcrs_to_true_of_date_matrix", matrix)
}

fn gcrs_to_true_of_date_matrix_parts_unchecked(ts: &TimeScales) -> (Mat3, f64) {
    let (dpsi, deps) = skyfield_iau2000a_radians_unchecked(ts.jd_tt);
    let mean_ob = skyfield_mean_obliquity_radians_unchecked(ts.jd_tdb);
    let true_ob = mean_ob + deps;

    let n = build_skyfield_nutation_matrix_unchecked(mean_ob, true_ob, dpsi);
    let p = compute_skyfield_precession_matrix_unchecked(ts.jd_tdb);
    let b = build_icrs_to_j2000();

    (inline_mxmxm(&n, &p, &b), dpsi)
}

/// Build the historical GCRS->ITRS rotation matrix for a given time.
///
/// This combines precession, nutation, and Earth rotation with zero polar
/// motion, preserving the original bit-exact path. Use
/// [`gcrs_to_itrs_matrix_with_polar_motion`] when `xp`/`yp` pole coordinates are
/// available.
pub fn gcrs_to_itrs_matrix(ts: &TimeScales) -> Result<Mat3, FrameTransformError> {
    validate_time_scales(ts)?;
    validate_mat3("gcrs_to_itrs_matrix", gcrs_to_itrs_matrix_unchecked(ts))
}

fn gcrs_to_itrs_matrix_unchecked(ts: &TimeScales) -> Mat3 {
    let (m, dpsi) = gcrs_to_true_of_date_matrix_parts_unchecked(ts);
    let gast = gast_radians(ts, dpsi);

    // GAST rotation takes us from true-equator-equinox to ITRS
    let r_gast = build_rot_z(-gast);

    // GCRS->ITRS = R_z(-GAST) * (N * P * B)
    inline_rxr(&r_gast, &m)
}

/// Build the GCRS->ITRS rotation matrix with explicit polar motion.
///
/// The embedded Earth-orientation table supplies UT1-UTC but not `xp`/`yp`, so
/// callers that do not have pole coordinates should pass [`PolarMotion::ZERO`]
/// or use [`gcrs_to_itrs_matrix`].
pub fn gcrs_to_itrs_matrix_with_polar_motion(
    ts: &TimeScales,
    pole: PolarMotion,
) -> Result<Mat3, FrameTransformError> {
    validate_time_scales(ts)?;
    validate_polar_motion(pole)?;
    validate_mat3(
        "gcrs_to_itrs_matrix",
        gcrs_to_itrs_matrix_with_polar_motion_unchecked(ts, pole),
    )
}

fn gcrs_to_itrs_matrix_with_polar_motion_unchecked(ts: &TimeScales, pole: PolarMotion) -> Mat3 {
    apply_polar_motion_to_itrs_matrix(gcrs_to_itrs_matrix_unchecked(ts), pole)
}

/// Rotation from the **mean equator and equinox of date** to ITRS, i.e.
/// `R_z(-GAST) * N` (nutation + Earth rotation, *without* precession or frame
/// bias).
///
/// This is [`gcrs_to_itrs_matrix`] with the precession (`P`) and frame-bias
/// (`B`) factors removed. Use it for vectors that are already referred to the
/// mean equator/equinox of date (for example the low-precision analytic Sun/Moon
/// series in [`crate::astro::bodies::sun_moon`], whose mean longitude and obliquity are
/// of-date), so precession is not applied a second time. It mirrors the
/// `eci2ecef` (GMST/GAST + nutation) rotation those series are designed to be
/// consumed with, but uses the crate's IAU 2000A nutation and GAST.
pub fn mean_of_date_to_itrs_matrix(ts: &TimeScales) -> Result<Mat3, FrameTransformError> {
    validate_time_scales(ts)?;
    validate_mat3(
        "mean_of_date_to_itrs_matrix",
        mean_of_date_to_itrs_matrix_unchecked(ts),
    )
}

fn mean_of_date_to_itrs_matrix_unchecked(ts: &TimeScales) -> Mat3 {
    let (dpsi, deps) = skyfield_iau2000a_radians_unchecked(ts.jd_tt);
    let mean_ob = skyfield_mean_obliquity_radians_unchecked(ts.jd_tdb);
    let true_ob = mean_ob + deps;

    let n = build_skyfield_nutation_matrix_unchecked(mean_ob, true_ob, dpsi);
    let gast = gast_radians(ts, dpsi);
    let r_gast = build_rot_z(-gast);

    // mean-of-date -> ITRS = R_z(-GAST) * N
    inline_rxr(&r_gast, &n)
}

/// Mean-of-date to ITRS rotation with explicit polar motion.
pub fn mean_of_date_to_itrs_matrix_with_polar_motion(
    ts: &TimeScales,
    pole: PolarMotion,
) -> Result<Mat3, FrameTransformError> {
    validate_time_scales(ts)?;
    validate_polar_motion(pole)?;
    validate_mat3(
        "mean_of_date_to_itrs_matrix",
        mean_of_date_to_itrs_matrix_with_polar_motion_unchecked(ts, pole),
    )
}

fn mean_of_date_to_itrs_matrix_with_polar_motion_unchecked(
    ts: &TimeScales,
    pole: PolarMotion,
) -> Mat3 {
    apply_polar_motion_to_itrs_matrix(mean_of_date_to_itrs_matrix_unchecked(ts), pole)
}

/// Core GCRS->ITRS transform. Returns (x, y, z) in km.
pub fn gcrs_to_itrs_compute(
    x: f64,
    y: f64,
    z: f64,
    ts: &TimeScales,
    skyfield_compat: bool,
) -> Result<(f64, f64, f64), FrameTransformError> {
    validate_vec3("gcrs_position_km", &[x, y, z])?;
    validate_time_scales(ts)?;
    validate_tuple3(
        "itrs_position_km",
        gcrs_to_itrs_compute_unchecked(x, y, z, ts, skyfield_compat),
    )
}

fn gcrs_to_itrs_compute_unchecked(
    x: f64,
    y: f64,
    z: f64,
    ts: &TimeScales,
    skyfield_compat: bool,
) -> (f64, f64, f64) {
    let mat = gcrs_to_itrs_matrix_unchecked(ts);

    if skyfield_compat {
        // Skyfield: mxv(R, pos_au) in AU, then convert to km.
        // For ITRS, scalar (non-FMA) multiply matches einsum's rounding.
        // (Unlike TEME->GCRS where FMA is needed -- the difference is due to
        // the specific matrix/vector values and how rounding interacts.)
        let pos_au = [x / AU_KM, y / AU_KM, z / AU_KM];
        let r = mat3_vec3_mul_unchecked(&mat, &pos_au);
        (r[0] * AU_KM, r[1] * AU_KM, r[2] * AU_KM)
    } else {
        let pos = [x, y, z];
        let r = mat3_vec3_mul_unchecked(&mat, &pos);
        (r[0], r[1], r[2])
    }
}

/// Core GCRS->ITRS transform with explicit polar motion.
pub fn gcrs_to_itrs_compute_with_polar_motion(
    x: f64,
    y: f64,
    z: f64,
    ts: &TimeScales,
    skyfield_compat: bool,
    pole: PolarMotion,
) -> Result<(f64, f64, f64), FrameTransformError> {
    validate_vec3("gcrs_position_km", &[x, y, z])?;
    validate_time_scales(ts)?;
    validate_polar_motion(pole)?;
    validate_tuple3(
        "itrs_position_km",
        gcrs_to_itrs_compute_with_polar_motion_unchecked(x, y, z, ts, skyfield_compat, pole),
    )
}

fn gcrs_to_itrs_compute_with_polar_motion_unchecked(
    x: f64,
    y: f64,
    z: f64,
    ts: &TimeScales,
    skyfield_compat: bool,
    pole: PolarMotion,
) -> (f64, f64, f64) {
    let mat = gcrs_to_itrs_matrix_with_polar_motion_unchecked(ts, pole);

    if skyfield_compat {
        let pos_au = [x / AU_KM, y / AU_KM, z / AU_KM];
        let r = mat3_vec3_mul_unchecked(&mat, &pos_au);
        (r[0] * AU_KM, r[1] * AU_KM, r[2] * AU_KM)
    } else {
        let pos = [x, y, z];
        let r = mat3_vec3_mul_unchecked(&mat, &pos);
        (r[0], r[1], r[2])
    }
}

// ---------------------------------------------------------------------------
// ITRS -> GCRS (Earth-fixed / ECEF back to inertial)
// ---------------------------------------------------------------------------

/// Build the ITRS->GCRS rotation matrix for a given time.
///
/// This is the transpose of [`gcrs_to_itrs_matrix`]: the same precession,
/// nutation, frame-bias, and Earth-rotation pipeline, taken the other way.
pub fn itrs_to_gcrs_matrix(ts: &TimeScales) -> Result<Mat3, FrameTransformError> {
    validate_time_scales(ts)?;
    validate_mat3("itrs_to_gcrs_matrix", itrs_to_gcrs_matrix_unchecked(ts))
}

fn itrs_to_gcrs_matrix_unchecked(ts: &TimeScales) -> Mat3 {
    inline_tr(&gcrs_to_itrs_matrix_unchecked(ts))
}

/// Build the ITRS->GCRS rotation matrix with explicit polar motion.
pub fn itrs_to_gcrs_matrix_with_polar_motion(
    ts: &TimeScales,
    pole: PolarMotion,
) -> Result<Mat3, FrameTransformError> {
    validate_time_scales(ts)?;
    validate_polar_motion(pole)?;
    validate_mat3(
        "itrs_to_gcrs_matrix",
        itrs_to_gcrs_matrix_with_polar_motion_unchecked(ts, pole),
    )
}

fn itrs_to_gcrs_matrix_with_polar_motion_unchecked(ts: &TimeScales, pole: PolarMotion) -> Mat3 {
    inline_tr(&gcrs_to_itrs_matrix_with_polar_motion_unchecked(ts, pole))
}

/// Core ITRS->GCRS transform. Returns (x, y, z) in km.
///
/// Uses the plain (non-FMA, no AU round-trip) km path. The Skyfield AU-scaled
/// `mul_add` path is reserved for the GCRS->ITRS / TEME->GCRS directions that
/// carry the 0-ULP parity contract; this reverse direction is an ordinary
/// matrix-vector product.
pub fn itrs_to_gcrs_compute(
    x: f64,
    y: f64,
    z: f64,
    ts: &TimeScales,
) -> Result<(f64, f64, f64), FrameTransformError> {
    validate_vec3("itrs_position_km", &[x, y, z])?;
    validate_time_scales(ts)?;
    validate_tuple3(
        "gcrs_position_km",
        itrs_to_gcrs_compute_unchecked(x, y, z, ts),
    )
}

fn itrs_to_gcrs_compute_unchecked(x: f64, y: f64, z: f64, ts: &TimeScales) -> (f64, f64, f64) {
    let mat = itrs_to_gcrs_matrix_unchecked(ts);
    let r = mat3_vec3_mul_unchecked(&mat, &[x, y, z]);
    (r[0], r[1], r[2])
}

/// Core ITRS->GCRS transform with explicit polar motion.
pub fn itrs_to_gcrs_compute_with_polar_motion(
    x: f64,
    y: f64,
    z: f64,
    ts: &TimeScales,
    pole: PolarMotion,
) -> Result<(f64, f64, f64), FrameTransformError> {
    validate_vec3("itrs_position_km", &[x, y, z])?;
    validate_time_scales(ts)?;
    validate_polar_motion(pole)?;
    validate_tuple3(
        "gcrs_position_km",
        itrs_to_gcrs_compute_with_polar_motion_unchecked(x, y, z, ts, pole),
    )
}

fn itrs_to_gcrs_compute_with_polar_motion_unchecked(
    x: f64,
    y: f64,
    z: f64,
    ts: &TimeScales,
    pole: PolarMotion,
) -> (f64, f64, f64) {
    let mat = itrs_to_gcrs_matrix_with_polar_motion_unchecked(ts, pole);
    let r = mat3_vec3_mul_unchecked(&mat, &[x, y, z]);
    (r[0], r[1], r[2])
}

// ---------------------------------------------------------------------------
// ITRS -> Geodetic (WGS84 lat/lon/alt)
// ---------------------------------------------------------------------------

/// Convert ECEF/ITRS (km) to geodetic coordinates.
/// Returns (latitude_deg, longitude_deg, altitude_km).
///
/// Replicates Skyfield's exact algorithm (wgs84.subpoint / _compute_latitude)
/// which works in AU with exactly 3 iterations.
pub fn itrs_to_geodetic_compute(
    x: f64,
    y: f64,
    z: f64,
) -> Result<(f64, f64, f64), FrameTransformError> {
    validate_vec3("itrs_position_km", &[x, y, z])?;
    validate_tuple3("geodetic", itrs_to_geodetic_compute_unchecked(x, y, z))
}

fn itrs_to_geodetic_compute_unchecked(x: f64, y: f64, z: f64) -> (f64, f64, f64) {
    // Convert to AU to match Skyfield's computation path.
    let x_au = x / AU_KM;
    let y_au = y / AU_KM;
    let z_au = z / AU_KM;

    let a_au = WGS84_A_KM / AU_KM; // Earth equatorial radius in AU
    let r_xy = (x_au * x_au + y_au * y_au).sqrt();

    // Longitude: match Skyfield's exact normalization:
    // (arctan2(y, x) - pi) % tau - pi
    // Python's % always returns positive; Rust's can be negative.
    let lon_raw = y_au.atan2(x_au);
    let pi = std::f64::consts::PI;
    let mut lon_shifted = (lon_raw - pi) % TAU;
    if lon_shifted < 0.0 {
        lon_shifted += TAU;
    }
    let lon = lon_shifted - pi;

    // Latitude: 3 iterations matching Skyfield exactly
    let mut lat = z_au.atan2(r_xy);
    let mut a_c = 0.0_f64;
    let mut hyp = 0.0_f64;

    for _ in 0..3 {
        let sin_lat = lat.sin();
        let e2_sin_lat = WGS84_E2 * sin_lat;
        a_c = a_au / (1.0 - e2_sin_lat * sin_lat).sqrt();
        hyp = z_au + a_c * e2_sin_lat;
        lat = hyp.atan2(r_xy);
    }

    // Elevation in AU, then convert to km
    let height_au = (hyp * hyp + r_xy * r_xy).sqrt() - a_c;
    let alt = height_au * AU_KM;

    // Skyfield's Angle.degrees uses: radians * 360.0 / tau
    // This gives different rounding than radians * (180.0 / PI).
    (lat * 360.0 / TAU, lon * 360.0 / TAU, alt)
}

fn proj_normal_radius_of_curvature(sinphi: f64) -> f64 {
    if PROJ_WGS84_ES == 0.0 {
        return PROJ_WGS84_A_M;
    }
    PROJ_WGS84_A_M / (1.0 - (PROJ_WGS84_ES * sinphi) * sinphi).sqrt()
}

fn proj_geocentric_radius(cosphi: f64, sinphi: f64) -> f64 {
    ((PROJ_WGS84_A_M * PROJ_WGS84_A_M) * cosphi).hypot((PROJ_WGS84_B_M * PROJ_WGS84_B_M) * sinphi)
        / (PROJ_WGS84_A_M * cosphi).hypot(PROJ_WGS84_B_M * sinphi)
}

/// Convert ECEF meters to `(longitude_degrees, latitude_degrees, altitude_m)`.
///
/// This is an additive PROJ parity variant and does not replace
/// [`itrs_to_geodetic_compute`]. It matches pyproj 3.6.1 / PROJ 9.3.0 for
/// `EPSG:4978 -> EPSG:4979` with `always_xy=True`; its Tier 1 bit fixture is
/// `crates/sidereon-core/tests/fixtures/geodetic/geodetic_proj.json`, generated
/// by `crates/sidereon-core/fixtures-generators/generate_geodetic_proj.py`.
pub fn geodetic_from_ecef_proj(x: f64, y: f64, z: f64) -> Result<[f64; 3], FrameTransformError> {
    validate_vec3("ecef_m", &[x, y, z])?;
    validate_array3("geodetic_proj", geodetic_from_ecef_proj_unchecked(x, y, z))
}

fn geodetic_from_ecef_proj_unchecked(x: f64, y: f64, z: f64) -> [f64; 3] {
    let p = x.hypot(y);

    let y_theta = z * PROJ_WGS84_A_M;
    let x_theta = p * PROJ_WGS84_B_M;
    let norm = y_theta.hypot(x_theta);
    let c = if norm == 0.0 { 1.0 } else { x_theta / norm };
    let s = if norm == 0.0 { 0.0 } else { y_theta / norm };

    let y_phi = z + ((((PROJ_WGS84_E2S * PROJ_WGS84_B_M) * s) * s) * s);
    let x_phi = p - ((((PROJ_WGS84_ES * PROJ_WGS84_A_M) * c) * c) * c);
    let norm_phi = y_phi.hypot(x_phi);
    let mut cosphi = if norm_phi == 0.0 {
        1.0
    } else {
        x_phi / norm_phi
    };
    let mut sinphi = if norm_phi == 0.0 {
        0.0
    } else {
        y_phi / norm_phi
    };

    let phi = if x_phi <= 0.0 {
        cosphi = 0.0;
        if z >= 0.0 {
            sinphi = 1.0;
            PROJ_HALF_PI
        } else {
            sinphi = -1.0;
            -PROJ_HALF_PI
        }
    } else {
        (y_phi / x_phi).atan()
    };

    let lam = y.atan2(x);
    let alt = if cosphi < 1e-6 {
        z.abs() - proj_geocentric_radius(cosphi, sinphi)
    } else {
        p / cosphi - proj_normal_radius_of_curvature(sinphi)
    };

    [lam * PROJ_RAD_TO_DEG, phi * PROJ_RAD_TO_DEG, alt]
}

// ---------------------------------------------------------------------------
// Topocentric (az/el/range) from ground station to satellite
// ---------------------------------------------------------------------------

/// Convert geodetic (lat_deg, lon_deg, alt_km) to ECEF/ITRS (km).
pub fn geodetic_to_itrs(
    lat_deg: f64,
    lon_deg: f64,
    alt_km: f64,
) -> Result<(f64, f64, f64), FrameTransformError> {
    validate_geodetic_degrees_km(lat_deg, lon_deg, alt_km)?;
    validate_tuple3(
        "itrs_position_km",
        geodetic_to_itrs_unchecked(lat_deg, lon_deg, alt_km),
    )
}

fn geodetic_to_itrs_unchecked(lat_deg: f64, lon_deg: f64, alt_km: f64) -> (f64, f64, f64) {
    let lat = lat_deg.to_radians();
    let lon = lon_deg.to_radians();

    let sin_lat = lat.sin();
    let cos_lat = lat.cos();
    let sin_lon = lon.sin();
    let cos_lon = lon.cos();

    let n = WGS84_A_KM / (1.0 - WGS84_E2 * sin_lat * sin_lat).sqrt();

    let x = (n + alt_km) * cos_lat * cos_lon;
    let y = (n + alt_km) * cos_lat * sin_lon;
    let z = (n * (1.0 - WGS84_E2) + alt_km) * sin_lat;

    (x, y, z)
}

/// Compute station ECEF/ITRS position directly in AU.
/// Matches Skyfield's Geoid.latlon which works in AU from the start,
/// avoiding the km->AU_KM division that introduces 1 ULP rounding.
fn geodetic_to_itrs_au(lat_deg: f64, lon_deg: f64, alt_km: f64) -> [f64; 3] {
    let lat = lat_deg * TAU / 360.0;
    let lon = lon_deg * TAU / 360.0;

    let sinphi = lat.sin();
    let cosphi = lat.cos();

    let radius_au = WGS84_A_KM / AU_KM;
    let elevation_au = alt_km / AU_KM;

    let omf2 = (1.0 - WGS84_F) * (1.0 - WGS84_F);
    let c = 1.0 / (cosphi * cosphi + sinphi * sinphi * omf2).sqrt();
    let s = omf2 * c;

    let radius_xy = radius_au * c;
    let xy = (radius_xy + elevation_au) * cosphi;
    let x = xy * lon.cos();
    let y = xy * lon.sin();

    let radius_z = radius_au * s;
    let z = (radius_z + elevation_au) * sinphi;

    [x, y, z]
}

/// Build the ECEF->ENU rotation matrix for a given geodetic position.
fn ecef_to_enu_matrix(lat_deg: f64, lon_deg: f64) -> Mat3 {
    let lat = lat_deg.to_radians();
    let lon = lon_deg.to_radians();

    let sin_lat = lat.sin();
    let cos_lat = lat.cos();
    let sin_lon = lon.sin();
    let cos_lon = lon.cos();

    // ENU rotation matrix:
    // E = [-sin(lon),           cos(lon),          0       ]
    // N = [-sin(lat)*cos(lon), -sin(lat)*sin(lon), cos(lat)]
    // U = [ cos(lat)*cos(lon),  cos(lat)*sin(lon), sin(lat)]
    [
        [-sin_lon, cos_lon, 0.0],
        [-sin_lat * cos_lon, -sin_lat * sin_lon, cos_lat],
        [cos_lat * cos_lon, cos_lat * sin_lon, sin_lat],
    ]
}

/// Compute topocentric az/el/range from a ground station to a satellite.
///
/// Returns (azimuth_deg, elevation_deg, range_km).
pub fn gcrs_to_topocentric_compute(
    sat_gcrs_km: [f64; 3],
    station: &GeodeticStationKm,
    ts: &TimeScales,
    skyfield_compat: bool,
) -> Result<(f64, f64, f64), FrameTransformError> {
    validate_vec3("sat_gcrs_km", &sat_gcrs_km)?;
    validate_geodetic_degrees_km(
        station.latitude_deg,
        station.longitude_deg,
        station.altitude_km,
    )?;
    validate_time_scales(ts)?;
    validate_tuple3(
        "topocentric",
        gcrs_to_topocentric_compute_unchecked(sat_gcrs_km, station, ts, skyfield_compat),
    )
}

fn gcrs_to_topocentric_compute_unchecked(
    sat_gcrs_km: [f64; 3],
    station: &GeodeticStationKm,
    ts: &TimeScales,
    skyfield_compat: bool,
) -> (f64, f64, f64) {
    let [sat_x, sat_y, sat_z] = sat_gcrs_km;
    let station_lat_deg = station.latitude_deg;
    let station_lon_deg = station.longitude_deg;
    let station_alt_km = station.altitude_km;
    if skyfield_compat {
        return gcrs_to_topocentric_skyfield(
            sat_x,
            sat_y,
            sat_z,
            station_lat_deg,
            station_lon_deg,
            station_alt_km,
            ts,
        );
    }

    // Standard path: GCRS->ITRS->ENU topocentric reduction.
    let (sat_itrs_x, sat_itrs_y, sat_itrs_z) =
        gcrs_to_itrs_compute_unchecked(sat_x, sat_y, sat_z, ts, false);
    itrs_to_topocentric_unchecked([sat_itrs_x, sat_itrs_y, sat_itrs_z], station)
}

/// Topocentric az/el/range from a station to an Earth-fixed (ITRS/ECEF) target.
///
/// `target_itrs_km` is the target's geocentric position in the Earth-fixed
/// (ITRS/ECEF) frame, km. Returns `(azimuth_deg, elevation_deg, range_km)`,
/// azimuth measured clockwise from north on `[0, 360)` and elevation on
/// `[-90, 90]`. This is the same station-to-target ENU reduction the satellite
/// look-angle path uses, exposed for callers that already hold an Earth-fixed
/// target vector (for example the analytic Sun/Moon ECEF positions from
/// [`crate::astro::bodies::sun_moon::sun_moon_ecef`]).
pub fn itrs_to_topocentric(
    target_itrs_km: [f64; 3],
    station: &GeodeticStationKm,
) -> Result<(f64, f64, f64), FrameTransformError> {
    validate_vec3("target_itrs_km", &target_itrs_km)?;
    validate_geodetic_degrees_km(
        station.latitude_deg,
        station.longitude_deg,
        station.altitude_km,
    )?;
    validate_tuple3(
        "topocentric",
        itrs_to_topocentric_unchecked(target_itrs_km, station),
    )
}

fn itrs_to_topocentric_unchecked(target_itrs_km: [f64; 3], station: &GeodeticStationKm) -> Vec3 {
    let [target_x, target_y, target_z] = target_itrs_km;
    let (stn_x, stn_y, stn_z) = geodetic_to_itrs_unchecked(
        station.latitude_deg,
        station.longitude_deg,
        station.altitude_km,
    );

    let dx = target_x - stn_x;
    let dy = target_y - stn_y;
    let dz = target_z - stn_z;

    let enu_mat = ecef_to_enu_matrix(station.latitude_deg, station.longitude_deg);
    let enu = mat3_vec3_mul_unchecked(&enu_mat, &[dx, dy, dz]);
    let east = enu[0];
    let north = enu[1];
    let up = enu[2];

    // Range
    let range = (east * east + north * north + up * up).sqrt();

    // Elevation
    let elevation = (up / range).asin().to_degrees();

    // Azimuth (measured clockwise from north). At (and arbitrarily near) the
    // station zenith the east and north components are pure rounding residuals,
    // so azimuth is degenerate and defined to be 0.0 (RTKLIB satazel semantics).
    let horiz_sq = east * east + north * north;
    let mut azimuth = if horiz_sq < AZIMUTH_ZENITH_EPS * range * range {
        0.0
    } else {
        east.atan2(north).to_degrees()
    };
    if azimuth < 0.0 {
        azimuth += 360.0;
    }

    (azimuth, elevation, range)
}

/// Skyfield-compatible topocentric: stays in GCRS AU the entire time.
///
/// Replicates Skyfield's altaz computation:
/// 1. R_lat = rot_y(lat)[::-1]  (row-reversed Y rotation)
/// 2. R_latlon = mxm(R_lat, rot_z(-lon))
/// 3. R_full = mxm(R_latlon, itrs_rotation)
/// 4. station_gcrs_au = transpose(itrs_rotation) * station_itrs_au
/// 5. diff_au = sat_gcrs_au - station_gcrs_au
/// 6. enu_au = mxv(R_full, diff_au)
/// 7. to_spherical(enu_au) -> (range_au, elevation_rad, azimuth_rad)
fn gcrs_to_topocentric_skyfield(
    sat_x: f64,
    sat_y: f64,
    sat_z: f64,
    station_lat_deg: f64,
    station_lon_deg: f64,
    station_alt_km: f64,
    ts: &TimeScales,
) -> (f64, f64, f64) {
    let lat_rad = station_lat_deg * TAU / 360.0;
    let lon_rad = station_lon_deg * TAU / 360.0;

    // Build R_lat = rot_y(lat)[::-1]  (rows reversed)
    let cy = lat_rad.cos();
    let sy = lat_rad.sin();
    // rot_y(lat) = [[cy, 0, sy], [0, 1, 0], [-sy, 0, cy]]
    // [::-1] reverses rows: [[-sy, 0, cy], [0, 1, 0], [cy, 0, sy]]
    let r_lat: Mat3 = [[-sy, 0.0, cy], [0.0, 1.0, 0.0], [cy, 0.0, sy]];

    // R_latlon = mxm(R_lat, rot_z(-lon))
    let rz_neg_lon = build_rot_z(-lon_rad);
    let r_latlon = inline_rxr(&r_lat, &rz_neg_lon);

    // R_full = mxm(R_latlon, itrs_rotation)
    let r_itrs = gcrs_to_itrs_matrix_unchecked(ts);
    let r_full = inline_rxr(&r_latlon, &r_itrs);

    // Station ITRS position directly in AU, matching Skyfield's Geoid.latlon
    // which computes in AU from the start (not km then / AU_KM).
    let stn_itrs_au = geodetic_to_itrs_au(station_lat_deg, station_lon_deg, station_alt_km);

    // Station GCRS AU = transpose(R_itrs) * station_itrs_au
    let r_itrs_t = inline_tr(&r_itrs);
    let stn_gcrs_au = mat3_vec3_mul_unchecked(&r_itrs_t, &stn_itrs_au);

    // Satellite GCRS in AU
    let sat_au = [sat_x / AU_KM, sat_y / AU_KM, sat_z / AU_KM];

    // Difference vector in GCRS AU
    let diff_au = [
        sat_au[0] - stn_gcrs_au[0],
        sat_au[1] - stn_gcrs_au[1],
        sat_au[2] - stn_gcrs_au[2],
    ];

    // Rotate to ENU-ish frame: mxv(R_full, diff_au)
    let enu_au = mat3_vec3_mul_unchecked(&r_full, &diff_au);

    // to_spherical: r, theta (elevation), phi (azimuth)
    let ex = enu_au[0];
    let ey = enu_au[1];
    let ez = enu_au[2];

    let r_au = (ex * ex + ey * ey + ez * ez).sqrt();
    let elevation_rad = ez.atan2((ex * ex + ey * ey).sqrt());
    let mut azimuth_rad = ey.atan2(ex) % TAU;
    if azimuth_rad < 0.0 {
        azimuth_rad += TAU;
    }

    let range_km = r_au * AU_KM;
    let elevation_deg = elevation_rad * 360.0 / TAU;
    let azimuth_deg = azimuth_rad * 360.0 / TAU;

    (azimuth_deg, elevation_deg, range_km)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::astro::time::scales::TimeScales;

    fn assert_mat3_bits_eq(actual: &Mat3, expected: &Mat3) {
        for i in 0..3 {
            for j in 0..3 {
                assert_eq!(
                    actual[i][j].to_bits(),
                    expected[i][j].to_bits(),
                    "matrix[{i}][{j}]: {} vs {}",
                    actual[i][j],
                    expected[i][j]
                );
            }
        }
    }

    fn assert_vec3_bits_eq(actual: [f64; 3], expected: [f64; 3]) {
        for i in 0..3 {
            assert_eq!(
                actual[i].to_bits(),
                expected[i].to_bits(),
                "vector[{i}]: {} vs {}",
                actual[i],
                expected[i]
            );
        }
    }

    #[test]
    fn itrs_to_gcrs_inverts_gcrs_to_itrs() {
        // On a real epoch and a real-magnitude ECI vector, ITRS->GCRS recovers
        // the GCRS->ITRS input bit-for-bit on the plain (non-Skyfield) km path:
        // the two directions must be exact transposes, not just approximately so.
        let ts = TimeScales::from_utc(2020, 6, 24, 12, 34, 56.0).expect("valid UTC instant");
        let (x, y, z) = (4321.0_f64, -5678.0, 3210.0);

        let (ix, iy, iz) =
            gcrs_to_itrs_compute(x, y, z, &ts, false).expect("valid frame transform");
        // The rotation actually moved the vector (it is not a no-op).
        assert!(((ix - x).abs() + (iy - y).abs() + (iz - z).abs()) > 100.0);

        let (bx, by, bz) = itrs_to_gcrs_compute(ix, iy, iz, &ts).expect("valid frame transform");
        assert!((bx - x).abs() < 1e-9, "x {bx} vs {x}");
        assert!((by - y).abs() < 1e-9, "y {by} vs {y}");
        assert!((bz - z).abs() < 1e-9, "z {bz} vs {z}");

        // Magnitude is preserved by the rotation.
        let n0 = (x * x + y * y + z * z).sqrt();
        let n1 = (ix * ix + iy * iy + iz * iz).sqrt();
        assert!((n0 - n1).abs() < 1e-9);
    }

    #[test]
    fn polar_motion_matrix_matches_documented_convention() {
        let pole = PolarMotion::from_arcseconds(0.25, -0.35).expect("valid polar motion");
        let cx = pole.xp_rad.cos();
        let sx = pole.xp_rad.sin();
        let cy = pole.yp_rad.cos();
        let sy = pole.yp_rad.sin();

        let expected = [
            [cx, sx * sy, sx * cy],
            [0.0, cy, -sy],
            [-sx, cx * sy, cx * cy],
        ];
        let got = polar_motion_matrix(pole).expect("valid polar motion matrix");
        assert_mat3_bits_eq(&got, &expected);

        let small_angle = [
            [1.0, 0.0, pole.xp_rad],
            [0.0, 1.0, -pole.yp_rad],
            [-pole.xp_rad, pole.yp_rad, 1.0],
        ];
        for i in 0..3 {
            for j in 0..3 {
                assert!(
                    (got[i][j] - small_angle[i][j]).abs() < 1.0e-11,
                    "matrix[{i}][{j}] {} vs small-angle {}",
                    got[i][j],
                    small_angle[i][j]
                );
            }
        }
    }

    #[test]
    fn gcrs_to_itrs_with_polar_motion_premultiplies_legacy_rotation() {
        let ts = TimeScales::from_utc(2020, 6, 24, 12, 34, 56.0).expect("valid UTC instant");
        let pole = PolarMotion::from_arcseconds(0.18, -0.24).expect("valid polar motion");
        let legacy = gcrs_to_itrs_matrix(&ts).expect("valid frame transform");
        let expected = inline_rxr(
            &polar_motion_matrix(pole).expect("valid polar motion matrix"),
            &legacy,
        );
        let got = gcrs_to_itrs_matrix_with_polar_motion(&ts, pole).expect("valid frame transform");

        assert_mat3_bits_eq(&got, &expected);

        let pos = [4321.0_f64, -5678.0, 3210.0];
        let actual_vec =
            gcrs_to_itrs_compute_with_polar_motion(pos[0], pos[1], pos[2], &ts, false, pole)
                .expect("valid frame transform");
        let expected_vec = mat3_vec3_mul(&expected, &pos).expect("finite matrix-vector product");
        assert_vec3_bits_eq([actual_vec.0, actual_vec.1, actual_vec.2], expected_vec);

        let legacy_vec =
            gcrs_to_itrs_compute(pos[0], pos[1], pos[2], &ts, false).expect("valid transform");
        let delta = (actual_vec.0 - legacy_vec.0).abs()
            + (actual_vec.1 - legacy_vec.1).abs()
            + (actual_vec.2 - legacy_vec.2).abs();
        assert!(
            delta > 1.0e-4,
            "nonzero polar motion should move the vector"
        );

        let inverse =
            itrs_to_gcrs_matrix_with_polar_motion(&ts, pole).expect("valid frame transform");
        assert_mat3_bits_eq(&inverse, &inline_tr(&got));
    }

    #[test]
    fn zero_polar_motion_matches_legacy_transform_bits() {
        let ts = TimeScales::from_utc(2020, 6, 24, 12, 34, 56.0).expect("valid UTC instant");
        let legacy = gcrs_to_itrs_matrix(&ts).expect("valid frame transform");
        let zero = gcrs_to_itrs_matrix_with_polar_motion(&ts, PolarMotion::ZERO)
            .expect("valid frame transform");
        assert_mat3_bits_eq(&zero, &legacy);

        let mean_legacy = mean_of_date_to_itrs_matrix(&ts).expect("valid frame transform");
        let mean_zero = mean_of_date_to_itrs_matrix_with_polar_motion(&ts, PolarMotion::ZERO)
            .expect("valid frame transform");
        assert_mat3_bits_eq(&mean_zero, &mean_legacy);

        let pos = [4321.0_f64, -5678.0, 3210.0];
        for skyfield_compat in [false, true] {
            let legacy_vec = gcrs_to_itrs_compute(pos[0], pos[1], pos[2], &ts, skyfield_compat)
                .expect("valid frame transform");
            let zero_vec = gcrs_to_itrs_compute_with_polar_motion(
                pos[0],
                pos[1],
                pos[2],
                &ts,
                skyfield_compat,
                PolarMotion::ZERO,
            )
            .expect("valid frame transform");
            assert_vec3_bits_eq(
                [zero_vec.0, zero_vec.1, zero_vec.2],
                [legacy_vec.0, legacy_vec.1, legacy_vec.2],
            );
        }

        let legacy_back =
            itrs_to_gcrs_compute(pos[0], pos[1], pos[2], &ts).expect("valid frame transform");
        let zero_back =
            itrs_to_gcrs_compute_with_polar_motion(pos[0], pos[1], pos[2], &ts, PolarMotion::ZERO)
                .expect("valid frame transform");
        assert_vec3_bits_eq(
            [zero_back.0, zero_back.1, zero_back.2],
            [legacy_back.0, legacy_back.1, legacy_back.2],
        );
    }

    #[test]
    fn gcrs_to_teme_inverts_teme_to_gcrs() {
        let ts = TimeScales::from_utc(2020, 6, 24, 12, 34, 56.0).expect("valid UTC instant");
        let teme = TemeStateKm {
            position_km: [6524.834, 6862.875, 6448.296],
            velocity_km_s: [4.901327, 5.533756, -1.976341],
        };

        for skyfield_compat in [false, true] {
            let (gcrs_position, gcrs_velocity) =
                teme_to_gcrs_compute(&teme, &ts, skyfield_compat).expect("valid transform");
            let gcrs = TemeStateKm {
                position_km: [gcrs_position.0, gcrs_position.1, gcrs_position.2],
                velocity_km_s: [gcrs_velocity.0, gcrs_velocity.1, gcrs_velocity.2],
            };
            let (round_position, round_velocity) =
                gcrs_to_teme_compute(&gcrs, &ts, skyfield_compat).expect("valid inverse");

            let pos = [round_position.0, round_position.1, round_position.2];
            let vel = [round_velocity.0, round_velocity.1, round_velocity.2];
            for axis in 0..3 {
                assert!(
                    (pos[axis] - teme.position_km[axis]).abs() <= 1.0e-9,
                    "position axis {axis} mode {skyfield_compat}"
                );
                assert!(
                    (vel[axis] - teme.velocity_km_s[axis]).abs() <= 1.0e-12,
                    "velocity axis {axis} mode {skyfield_compat}"
                );
            }
        }
    }

    #[test]
    fn frame_transforms_reject_nonfinite_time() {
        let mut ts = TimeScales::from_utc(2020, 6, 24, 12, 34, 56.0).expect("valid UTC instant");
        ts.jd_tt = f64::NAN;

        assert!(greenwich_mean_sidereal_time_radians(&ts).is_err());
        assert!(gcrs_to_itrs_matrix(&ts).is_err());
        assert!(itrs_to_gcrs_compute(1.0, 2.0, 3.0, &ts).is_err());
    }

    #[test]
    fn frame_transforms_reject_nonfinite_pole_coordinates() {
        let ts = TimeScales::from_utc(2020, 6, 24, 12, 34, 56.0).expect("valid UTC instant");
        assert!(PolarMotion::from_radians(f64::NAN, 0.0).is_err());
        assert!(PolarMotion::from_arcseconds(0.0, f64::INFINITY).is_err());

        let pole = PolarMotion {
            xp_rad: f64::NAN,
            yp_rad: 0.0,
        };
        assert!(polar_motion_matrix(pole).is_err());
        assert!(gcrs_to_itrs_matrix_with_polar_motion(&ts, pole).is_err());
    }

    #[test]
    fn frame_transforms_reject_nonfinite_vectors() {
        let ts = TimeScales::from_utc(2020, 6, 24, 12, 34, 56.0).expect("valid UTC instant");
        let bad_state = TemeStateKm {
            position_km: [1.0, f64::NAN, 3.0],
            velocity_km_s: [0.1, 0.2, 0.3],
        };
        assert!(teme_to_gcrs_compute(&bad_state, &ts, false).is_err());
        assert!(gcrs_to_itrs_compute(1.0, f64::INFINITY, 3.0, &ts, false).is_err());
        assert!(itrs_to_gcrs_compute(1.0, 2.0, f64::NEG_INFINITY, &ts).is_err());
    }

    #[test]
    fn validated_frame_transform_preserves_valid_bits() {
        let ts = TimeScales::from_utc(2020, 6, 24, 12, 34, 56.0).expect("valid UTC instant");
        let pos = [4321.0_f64, -5678.0, 3210.0];
        let expected = gcrs_to_itrs_compute_unchecked(pos[0], pos[1], pos[2], &ts, true);
        let got =
            gcrs_to_itrs_compute(pos[0], pos[1], pos[2], &ts, true).expect("valid frame transform");
        assert_vec3_bits_eq([got.0, got.1, got.2], [expected.0, expected.1, expected.2]);
    }

    #[test]
    fn geodetic_transforms_reject_invalid_coordinates() {
        assert!(itrs_to_geodetic_compute(f64::NAN, 0.0, 0.0).is_err());
        assert!(geodetic_from_ecef_proj(0.0, f64::INFINITY, 0.0).is_err());
        assert!(geodetic_to_itrs(90.000_001, 0.0, 0.0).is_err());
        assert!(geodetic_to_itrs(0.0, -180.000_001, 0.0).is_err());
        assert!(geodetic_to_itrs(0.0, 0.0, f64::NAN).is_err());
    }

    #[test]
    fn topocentric_transform_rejects_invalid_coordinates() {
        let ts = TimeScales::from_utc(2020, 6, 24, 12, 34, 56.0).expect("valid UTC instant");
        let station = GeodeticStationKm {
            latitude_deg: f64::NAN,
            longitude_deg: 0.0,
            altitude_km: 0.0,
        };
        assert!(gcrs_to_topocentric_compute([7000.0, 0.0, 0.0], &station, &ts, false).is_err());

        let station = GeodeticStationKm {
            latitude_deg: 0.0,
            longitude_deg: 181.0,
            altitude_km: 0.0,
        };
        assert!(gcrs_to_topocentric_compute([7000.0, 0.0, 0.0], &station, &ts, false).is_err());

        let station = GeodeticStationKm {
            latitude_deg: 0.0,
            longitude_deg: 0.0,
            altitude_km: 0.0,
        };
        assert!(
            gcrs_to_topocentric_compute([7000.0, f64::NAN, 0.0], &station, &ts, false).is_err()
        );
    }

    #[test]
    fn topocentric_azimuth_is_zero_at_station_zenith() {
        let ts = TimeScales::from_utc(2020, 6, 24, 12, 34, 56.0).expect("valid UTC instant");
        // Equatorial station: geodetic up coincides with the geocentric radial,
        // so a satellite displaced radially outward is exactly overhead.
        let station = GeodeticStationKm {
            latitude_deg: 0.0,
            longitude_deg: 0.0,
            altitude_km: 0.0,
        };
        let (sx, sy, sz) = geodetic_to_itrs_unchecked(0.0, 0.0, 0.0);
        // Satellite ITRS position 20,000 km straight up (+X at lat0/lon0).
        let sat_itrs = [sx + 20_000.0, sy, sz];
        // Express it in GCRS so the standard path rotates it back to `sat_itrs`.
        let r_itrs = gcrs_to_itrs_matrix_unchecked(&ts);
        let r_itrs_t = inline_tr(&r_itrs);
        let sat_gcrs = mat3_vec3_mul_unchecked(&r_itrs_t, &sat_itrs);

        let (azimuth_deg, elevation_deg, _range_km) =
            gcrs_to_topocentric_compute_unchecked(sat_gcrs, &station, &ts, false);
        assert_eq!(azimuth_deg, 0.0);
        assert!(azimuth_deg.is_finite());
        assert!((elevation_deg - 90.0).abs() < 1e-6);
    }

    #[test]
    fn validated_geodetic_transform_preserves_valid_bits() {
        let (lat, lon, alt) = (51.4779, -0.0015, 0.046);
        let expected = geodetic_to_itrs_unchecked(lat, lon, alt);
        let got = geodetic_to_itrs(lat, lon, alt).expect("valid geodetic coordinates");
        assert_eq!(got.0.to_bits(), expected.0.to_bits());
        assert_eq!(got.1.to_bits(), expected.1.to_bits());
        assert_eq!(got.2.to_bits(), expected.2.to_bits());

        let expected = itrs_to_geodetic_compute_unchecked(got.0, got.1, got.2);
        let roundtrip =
            itrs_to_geodetic_compute(got.0, got.1, got.2).expect("valid ITRS coordinates");
        assert_eq!(roundtrip.0.to_bits(), expected.0.to_bits());
        assert_eq!(roundtrip.1.to_bits(), expected.1.to_bits());
        assert_eq!(roundtrip.2.to_bits(), expected.2.to_bits());
    }

    #[test]
    fn sidereal_time_wrappers_are_in_range_and_consistent() {
        let ts = TimeScales::from_utc(2020, 6, 24, 12, 34, 56.0).expect("valid UTC instant");
        let gmst = greenwich_mean_sidereal_time_radians(&ts).expect("valid sidereal time");
        let gast = greenwich_apparent_sidereal_time_radians(&ts).expect("valid sidereal time");

        // Both land in [0, 2pi).
        assert!((0.0..TAU).contains(&gmst), "gmst {gmst}");
        assert!((0.0..TAU).contains(&gast), "gast {gast}");

        // The equation of the equinoxes is a small (sub-arcminute) offset, so the
        // apparent and mean sidereal times stay close (handle the seam at 2pi).
        let diff = (gast - gmst).rem_euclid(TAU);
        let eq_eq = diff.min(TAU - diff);
        assert!(eq_eq < 1.0e-3, "equation of equinoxes too large: {eq_eq}");

        // The mean wrapper matches the Skyfield-parity IAU-1982 sidereal time.
        let gmst_hours = sidereal_time_hours(ts.jd_whole, ts.ut1_fraction, ts.tdb_fraction);
        assert_eq!(gmst, gmst_hours / 24.0 * TAU);
    }

    #[test]
    fn gmst_from_j2000_seconds_matches_gmst1982_polynomial() {
        // The seconds-based accessor reproduces the GMST-1982 polynomial the TEME
        // sidereal rotation uses, not the public Skyfield-parity GMST. Tolerance
        // allows only the roundoff from reducing the UT1 split to one f64.
        const GMST_TOL_RAD: f64 = 1.0e-11;
        let ts = TimeScales::from_utc(2020, 6, 24, 12, 34, 56.25).expect("valid UTC instant");
        let sec = (ts.jd_ut1 - J2000_JD) * SECONDS_PER_DAY;
        let from_seconds = greenwich_mean_sidereal_time_radians_from_j2000_seconds(sec)
            .expect("valid sidereal time");
        let mut expected = compute_theta_gmst1982(ts.jd_whole, ts.ut1_fraction) % TAU;
        if expected < 0.0 {
            expected += TAU;
        }

        assert!((from_seconds - expected).abs() <= GMST_TOL_RAD);
    }
}