Skip to main content

sidereon_core/astro/frames/
transforms.rs

1//! Coordinate transformation pipeline.
2//!
3//! TEME -> GCRS replicates Skyfield's exact computation path including AU/day
4//! unit scaling for bit-exact (0 ULP) parity.
5//!
6//! Also provides GCRS -> ITRS, ITRS -> geodetic (WGS84), and topocentric
7//! (az/el/range) transformations.
8//!
9//! The pure compute functions live here in the core crate; the Rustler
10//! decode/encode shims that used to wrap them stay in `orbis_nif` as glue, so
11//! no domain formula lives in the NIF layer. The numerics, summation order,
12//! transcendental sequence, and the single sanctioned `mul_add` site
13//! (`mat3_vec3_mul_fma`) are preserved exactly so the existing Skyfield 0-ULP
14//! parity holds.
15
16use crate::astro::frames::nutation::{
17    build_skyfield_nutation_matrix_unchecked,
18    skyfield_equation_of_the_equinoxes_complimentary_terms_unchecked,
19    skyfield_iau2000a_radians_unchecked, skyfield_mean_obliquity_radians_unchecked,
20};
21use crate::astro::frames::precession::{
22    build_icrs_to_j2000, compute_skyfield_precession_matrix_unchecked,
23};
24use crate::astro::math::mat3::{inline_mxmxm, inline_rxr, inline_tr, Mat3};
25use crate::astro::time::{civil, scales::TimeScales};
26use crate::astro::{
27    constants::astro::AU_KM,
28    constants::earth::{WGS84_A_KM, WGS84_E2, WGS84_F},
29    constants::geometry::AZIMUTH_ZENITH_EPS,
30    constants::models::proj::{
31        HALF_PI as PROJ_HALF_PI, RAD_TO_DEG as PROJ_RAD_TO_DEG, WGS84_A_M as PROJ_WGS84_A_M,
32        WGS84_B_M as PROJ_WGS84_B_M, WGS84_E2S as PROJ_WGS84_E2S, WGS84_ES as PROJ_WGS84_ES,
33    },
34    constants::time::{DAYS_PER_JULIAN_CENTURY, J2000_JD, SECONDS_PER_DAY},
35};
36
37const TAU: f64 = std::f64::consts::TAU;
38const ARCSECONDS_TO_RADIANS: f64 = 4.848_136_811_095_36e-6;
39
40/// Error returned when public frame-transform inputs are outside the valid domain.
41#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
42pub enum FrameTransformError {
43    /// A transform input was non-finite or otherwise invalid.
44    #[error("invalid frame transform {field}: {reason}")]
45    InvalidInput {
46        field: &'static str,
47        reason: &'static str,
48    },
49}
50
51fn invalid_input(field: &'static str, reason: &'static str) -> FrameTransformError {
52    FrameTransformError::InvalidInput { field, reason }
53}
54
55fn validate_finite(field: &'static str, value: f64) -> Result<(), FrameTransformError> {
56    if value.is_finite() {
57        Ok(())
58    } else {
59        Err(invalid_input(field, "must be finite"))
60    }
61}
62
63fn validate_vec3(field: &'static str, values: &[f64; 3]) -> Result<(), FrameTransformError> {
64    for value in values {
65        if !value.is_finite() {
66            return Err(invalid_input(field, "components must be finite"));
67        }
68    }
69    Ok(())
70}
71
72fn validate_tuple3(field: &'static str, values: Vec3) -> Result<Vec3, FrameTransformError> {
73    if values.0.is_finite() && values.1.is_finite() && values.2.is_finite() {
74        Ok(values)
75    } else {
76        Err(invalid_input(field, "components must be finite"))
77    }
78}
79
80fn validate_array3(field: &'static str, values: [f64; 3]) -> Result<[f64; 3], FrameTransformError> {
81    validate_vec3(field, &values)?;
82    Ok(values)
83}
84
85fn validate_mat3(field: &'static str, values: Mat3) -> Result<Mat3, FrameTransformError> {
86    for row in &values {
87        validate_vec3(field, row)?;
88    }
89    Ok(values)
90}
91
92fn validate_time_scales(ts: &TimeScales) -> Result<(), FrameTransformError> {
93    validate_finite("jd_whole", ts.jd_whole)?;
94    validate_finite("ut1_fraction", ts.ut1_fraction)?;
95    validate_finite("tt_fraction", ts.tt_fraction)?;
96    validate_finite("tdb_fraction", ts.tdb_fraction)?;
97    validate_finite("jd_ut1", ts.jd_ut1)?;
98    validate_finite("jd_tt", ts.jd_tt)?;
99    validate_finite("jd_tdb", ts.jd_tdb)
100}
101
102fn validate_polar_motion(pole: PolarMotion) -> Result<(), FrameTransformError> {
103    validate_finite("xp_rad", pole.xp_rad)?;
104    validate_finite("yp_rad", pole.yp_rad)
105}
106
107fn validate_geodetic_degrees_km(
108    latitude_deg: f64,
109    longitude_deg: f64,
110    altitude_km: f64,
111) -> Result<(), FrameTransformError> {
112    validate_finite("latitude_deg", latitude_deg)?;
113    if !(-90.0..=90.0).contains(&latitude_deg) {
114        return Err(invalid_input("latitude_deg", "must be in [-90, 90]"));
115    }
116    validate_finite("longitude_deg", longitude_deg)?;
117    if !(-180.0..=180.0).contains(&longitude_deg) {
118        return Err(invalid_input("longitude_deg", "must be in [-180, 180]"));
119    }
120    validate_finite("altitude_km", altitude_km)
121}
122
123/// A bare Cartesian triple (km or km/s depending on context).
124///
125/// This is the internal compute-layer return shape. Typed input structs
126/// ([`TemeStateKm`], [`GeodeticStationKm`]) bundle the public entry points'
127/// arguments, but the numerics below operate on raw triples to preserve the
128/// original operation order exactly.
129pub type Vec3 = (f64, f64, f64);
130
131/// TEME-frame position and velocity (km, km/s): the input to
132/// [`teme_to_gcrs_compute`].
133pub struct TemeStateKm {
134    pub position_km: [f64; 3],
135    pub velocity_km_s: [f64; 3],
136}
137
138/// Geodetic ground-station position (WGS84) for topocentric look angles.
139pub struct GeodeticStationKm {
140    pub latitude_deg: f64,
141    pub longitude_deg: f64,
142    pub altitude_km: f64,
143}
144
145/// Polar-motion coordinates of the Celestial Intermediate Pole.
146///
147/// `xp_rad` and `yp_rad` are radians. The embedded EOP table currently carries
148/// UT1-UTC only, so the historical transforms use [`PolarMotion::ZERO`] by
149/// default. Precision callers with pole coordinates should use the explicit
150/// `*_with_polar_motion` entry points below.
151#[derive(Debug, Clone, Copy, PartialEq)]
152pub struct PolarMotion {
153    pub xp_rad: f64,
154    pub yp_rad: f64,
155}
156
157impl PolarMotion {
158    /// No polar-motion rotation; preserves the historical transform exactly.
159    pub const ZERO: Self = Self {
160        xp_rad: 0.0,
161        yp_rad: 0.0,
162    };
163
164    /// Construct polar-motion coordinates from radians.
165    pub fn from_radians(xp_rad: f64, yp_rad: f64) -> Result<Self, FrameTransformError> {
166        validate_finite("xp_rad", xp_rad)?;
167        validate_finite("yp_rad", yp_rad)?;
168        Ok(Self { xp_rad, yp_rad })
169    }
170
171    /// Construct polar-motion coordinates from arcseconds.
172    pub fn from_arcseconds(xp_arcsec: f64, yp_arcsec: f64) -> Result<Self, FrameTransformError> {
173        validate_finite("xp_arcsec", xp_arcsec)?;
174        validate_finite("yp_arcsec", yp_arcsec)?;
175        Self::from_radians(
176            xp_arcsec * ARCSECONDS_TO_RADIANS,
177            yp_arcsec * ARCSECONDS_TO_RADIANS,
178        )
179    }
180
181    fn is_zero(self) -> bool {
182        self.xp_rad == 0.0 && self.yp_rad == 0.0
183    }
184}
185
186impl Default for PolarMotion {
187    fn default() -> Self {
188        Self::ZERO
189    }
190}
191
192/// Final matrix-vector multiply using explicit FMA.
193/// This matches numpy's vectorized behavior and is the ONLY place
194/// where f64::mul_add() should be used.
195fn mat3_vec3_mul_fma(r: &Mat3, p: &[f64; 3]) -> [f64; 3] {
196    let mut result = [0.0_f64; 3];
197    for i in 0..3 {
198        let sum = r[i][0] * p[0];
199        let sum = f64::mul_add(r[i][1], p[1], sum);
200        let sum = f64::mul_add(r[i][2], p[2], sum);
201        result[i] = sum;
202    }
203    result
204}
205
206fn build_rot_z(angle: f64) -> Mat3 {
207    let c = angle.cos();
208    let s = angle.sin();
209    [[c, -s, 0.0], [s, c, 0.0], [0.0, 0.0, 1.0]]
210}
211
212/// IERS polar-motion matrix, omitting the tiny TIO locator term `s'`.
213///
214/// The matrix maps TIRS pseudo-Earth-fixed coordinates to ITRS:
215/// `W = R_y(xp) * R_x(yp)`, whose small-angle form is
216/// `[[1, 0, xp], [0, 1, -yp], [-xp, yp, 1]]`.
217pub fn polar_motion_matrix(pole: PolarMotion) -> Result<Mat3, FrameTransformError> {
218    validate_polar_motion(pole)?;
219    validate_mat3("polar_motion_matrix", polar_motion_matrix_unchecked(pole))
220}
221
222fn polar_motion_matrix_unchecked(pole: PolarMotion) -> Mat3 {
223    if pole.is_zero() {
224        return [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]];
225    }
226
227    let cx = pole.xp_rad.cos();
228    let sx = pole.xp_rad.sin();
229    let cy = pole.yp_rad.cos();
230    let sy = pole.yp_rad.sin();
231
232    [
233        [cx, sx * sy, sx * cy],
234        [0.0, cy, -sy],
235        [-sx, cx * sy, cx * cy],
236    ]
237}
238
239fn apply_polar_motion_to_itrs_matrix(mat: Mat3, pole: PolarMotion) -> Mat3 {
240    if pole.is_zero() {
241        mat
242    } else {
243        inline_rxr(&polar_motion_matrix_unchecked(pole), &mat)
244    }
245}
246
247fn earth_rotation_angle(jd_whole: f64, ut1_fraction: f64) -> f64 {
248    let days_since_j2000 = jd_whole - J2000_JD + ut1_fraction;
249    // Force separate rounded operations to match Skyfield/Python's path.
250    let spins_since_j2000: f64 = {
251        let v = 0.00273781191135448 * days_since_j2000;
252        // Use black_box-like pattern to prevent optimization
253        let v_stored: f64 = v;
254        v_stored
255    };
256    let th = 0.7790572732640 + spins_since_j2000;
257    let mut result = (th % 1.0 + jd_whole % 1.0 + ut1_fraction) % 1.0;
258    if result < 0.0 {
259        result += 1.0;
260    }
261    result
262}
263
264fn compute_theta_gmst1982(jd_whole: f64, ut1_fraction: f64) -> f64 {
265    let t = (jd_whole - J2000_JD + ut1_fraction) / DAYS_PER_JULIAN_CENTURY;
266    let g = 67310.54841 + (8640184.812866 + (0.093104 + (-6.2e-6) * t) * t) * t;
267    let mut theta = ((jd_whole % 1.0) + ut1_fraction + (g / SECONDS_PER_DAY) % 1.0) % 1.0 * TAU;
268    if theta < 0.0 {
269        theta += TAU;
270    }
271    theta
272}
273
274fn sidereal_time_hours(jd_whole: f64, ut1_fraction: f64, tdb_fraction: f64) -> f64 {
275    let theta = earth_rotation_angle(jd_whole, ut1_fraction);
276    let t = (jd_whole - J2000_JD + tdb_fraction) / DAYS_PER_JULIAN_CENTURY;
277    let st = 0.014506
278        + ((((-0.0000000368 * t - 0.000029956) * t - 0.00000044) * t + 1.3915817) * t
279            + 4612.156534)
280            * t;
281    let mut result = (st / 54000.0 + theta * 24.0) % 24.0;
282    if result < 0.0 {
283        result += 24.0;
284    }
285    result
286}
287
288fn gast_radians(ts: &TimeScales, dpsi: f64) -> f64 {
289    let gmst_hours = sidereal_time_hours(ts.jd_whole, ts.ut1_fraction, ts.tdb_fraction);
290    let mean_ob = skyfield_mean_obliquity_radians_unchecked(ts.jd_tdb);
291    let c_terms = skyfield_equation_of_the_equinoxes_complimentary_terms_unchecked(ts.jd_tt);
292    let eq_eq = dpsi * mean_ob.cos() + c_terms;
293    let mut gast_hours = (gmst_hours + eq_eq / TAU * 24.0) % 24.0;
294    if gast_hours < 0.0 {
295        gast_hours += 24.0;
296    }
297    gast_hours / 24.0 * TAU
298}
299
300/// Greenwich Mean Sidereal Time for an instant, radians in `[0, 2pi)`.
301///
302/// The IAU-1982 GMST used internally by the frame pipeline, surfaced as a public
303/// entry point. This is a thin wrapper over the existing private sidereal-time
304/// computation: it adds no new numerics, so the value is bit-identical to the
305/// quantity the transforms consume.
306pub fn greenwich_mean_sidereal_time_radians(ts: &TimeScales) -> Result<f64, FrameTransformError> {
307    validate_time_scales(ts)?;
308    let radians = greenwich_mean_sidereal_time_radians_unchecked(ts);
309    validate_finite("gmst_radians", radians)?;
310    Ok(radians)
311}
312
313fn greenwich_mean_sidereal_time_radians_unchecked(ts: &TimeScales) -> f64 {
314    let hours = sidereal_time_hours(ts.jd_whole, ts.ut1_fraction, ts.tdb_fraction);
315    hours / 24.0 * TAU
316}
317
318/// IAU-1982 GMST in radians from continuous seconds past J2000.
319///
320/// The input epoch is treated as UT1 for this thin drag helper. It reuses the
321/// same `compute_theta_gmst1982` polynomial as
322/// [`greenwich_mean_sidereal_time_radians`] and adds no new sidereal-time math.
323pub fn greenwich_mean_sidereal_time_radians_from_j2000_seconds(
324    sec: f64,
325) -> Result<f64, FrameTransformError> {
326    validate_finite("sec", sec)?;
327    let (jd_whole, ut1_fraction) = civil::split_julian_date_add_seconds(J2000_JD, 0.0, sec);
328    let mut radians = compute_theta_gmst1982(jd_whole, ut1_fraction) % TAU;
329    if radians < 0.0 {
330        radians += TAU;
331    }
332    validate_finite("gmst_radians", radians)?;
333    Ok(radians)
334}
335
336/// Greenwich Apparent Sidereal Time for an instant, radians in `[0, 2pi)`.
337///
338/// GMST plus the equation of the equinoxes (nutation in longitude projected on
339/// the true equator, with the IAU 2000 complementary terms). A thin wrapper over
340/// the existing private GAST computation; bit-identical to the value the
341/// GCRS<->ITRS transforms apply.
342pub fn greenwich_apparent_sidereal_time_radians(
343    ts: &TimeScales,
344) -> Result<f64, FrameTransformError> {
345    validate_time_scales(ts)?;
346    let radians = greenwich_apparent_sidereal_time_radians_unchecked(ts);
347    validate_finite("gast_radians", radians)?;
348    Ok(radians)
349}
350
351fn greenwich_apparent_sidereal_time_radians_unchecked(ts: &TimeScales) -> f64 {
352    let (dpsi, _deps) = skyfield_iau2000a_radians_unchecked(ts.jd_tt);
353    gast_radians(ts, dpsi)
354}
355
356/// Build the TEME->GCRS rotation matrix T from time scales.
357fn build_teme_to_gcrs_matrix(ts: &TimeScales, skyfield_compat: bool) -> Mat3 {
358    let (dpsi, deps) = skyfield_iau2000a_radians_unchecked(ts.jd_tt);
359    let mean_ob = skyfield_mean_obliquity_radians_unchecked(ts.jd_tdb);
360    let true_ob = mean_ob + deps;
361
362    let n = build_skyfield_nutation_matrix_unchecked(mean_ob, true_ob, dpsi);
363    let p = compute_skyfield_precession_matrix_unchecked(ts.jd_tdb);
364    let b = build_icrs_to_j2000();
365
366    // Skyfield uses Kahan-compensated triple product (matching numpy einsum).
367    // Direct mode uses standard sequential multiply (more precise).
368    let m = if skyfield_compat {
369        inline_mxmxm(&n, &p, &b)
370    } else {
371        let np = inline_rxr(&n, &p);
372        inline_rxr(&np, &b)
373    };
374
375    let gast = gast_radians(ts, dpsi);
376    let theta = compute_theta_gmst1982(ts.jd_whole, ts.ut1_fraction);
377    let angle = theta - gast;
378
379    let r = build_rot_z(angle);
380    let g = inline_rxr(&r, &m);
381    inline_tr(&g)
382}
383
384/// Build the TEME->GCRS rotation matrix T from time scales.
385pub(crate) fn teme_to_gcrs_matrix(ts: &TimeScales, skyfield_compat: bool) -> Mat3 {
386    build_teme_to_gcrs_matrix(ts, skyfield_compat)
387}
388
389/// Standard (non-FMA) matrix-vector multiply.
390pub fn mat3_vec3_mul(r: &Mat3, p: &[f64; 3]) -> Result<[f64; 3], FrameTransformError> {
391    validate_mat3("matrix", *r)?;
392    validate_vec3("vector", p)?;
393    validate_array3("matrix_vector_product", mat3_vec3_mul_unchecked(r, p))
394}
395
396pub(crate) fn mat3_vec3_mul_unchecked(r: &Mat3, p: &[f64; 3]) -> [f64; 3] {
397    let mut result = [0.0_f64; 3];
398    for i in 0..3 {
399        let mut sum = 0.0;
400        for j in 0..3 {
401            sum += r[i][j] * p[j];
402        }
403        result[i] = sum;
404    }
405    result
406}
407
408/// Core TEME->GCRS transform. Returns ((px,py,pz), (vx,vy,vz)).
409pub fn teme_to_gcrs_compute(
410    state: &TemeStateKm,
411    ts: &TimeScales,
412    skyfield_compat: bool,
413) -> Result<(Vec3, Vec3), FrameTransformError> {
414    validate_time_scales(ts)?;
415    validate_vec3("position_km", &state.position_km)?;
416    validate_vec3("velocity_km_s", &state.velocity_km_s)?;
417    let (position, velocity) = teme_to_gcrs_compute_unchecked(state, ts, skyfield_compat);
418    Ok((
419        validate_tuple3("gcrs_position_km", position)?,
420        validate_tuple3("gcrs_velocity_km_s", velocity)?,
421    ))
422}
423
424fn teme_to_gcrs_compute_unchecked(
425    state: &TemeStateKm,
426    ts: &TimeScales,
427    skyfield_compat: bool,
428) -> (Vec3, Vec3) {
429    let [x, y, z] = state.position_km;
430    let [vx, vy, vz] = state.velocity_km_s;
431    let t = build_teme_to_gcrs_matrix(ts, skyfield_compat);
432
433    if skyfield_compat {
434        // AU/day scaling + FMA multiply matching Skyfield's _at() path.
435        let r_au = [x / AU_KM, y / AU_KM, z / AU_KM];
436        let r_gcrs_au = mat3_vec3_mul_fma(&t, &r_au);
437        let r_gcrs = (
438            r_gcrs_au[0] * AU_KM,
439            r_gcrs_au[1] * AU_KM,
440            r_gcrs_au[2] * AU_KM,
441        );
442
443        let v_au_d = [
444            vx / AU_KM * SECONDS_PER_DAY,
445            vy / AU_KM * SECONDS_PER_DAY,
446            vz / AU_KM * SECONDS_PER_DAY,
447        ];
448        let v_gcrs_au_d = mat3_vec3_mul_fma(&t, &v_au_d);
449        let v_gcrs = (
450            v_gcrs_au_d[0] * AU_KM / SECONDS_PER_DAY,
451            v_gcrs_au_d[1] * AU_KM / SECONDS_PER_DAY,
452            v_gcrs_au_d[2] * AU_KM / SECONDS_PER_DAY,
453        );
454        (r_gcrs, v_gcrs)
455    } else {
456        // Direct km/s multiply -- no AU round-trip, no FMA.
457        let r_teme = [x, y, z];
458        let r_g = mat3_vec3_mul_unchecked(&t, &r_teme);
459        let v_teme = [vx, vy, vz];
460        let v_g = mat3_vec3_mul_unchecked(&t, &v_teme);
461        ((r_g[0], r_g[1], r_g[2]), (v_g[0], v_g[1], v_g[2]))
462    }
463}
464
465/// Core GCRS->TEME transform. Returns ((px,py,pz), (vx,vy,vz)).
466///
467/// This is the rotational inverse of [`teme_to_gcrs_compute`]: it uses the
468/// transpose of the same orthogonal matrix and adds no new precession or
469/// nutation math.
470pub fn gcrs_to_teme_compute(
471    state: &TemeStateKm,
472    ts: &TimeScales,
473    skyfield_compat: bool,
474) -> Result<(Vec3, Vec3), FrameTransformError> {
475    validate_time_scales(ts)?;
476    validate_vec3("position_km", &state.position_km)?;
477    validate_vec3("velocity_km_s", &state.velocity_km_s)?;
478    let (position, velocity) = gcrs_to_teme_compute_unchecked(state, ts, skyfield_compat);
479    Ok((
480        validate_tuple3("teme_position_km", position)?,
481        validate_tuple3("teme_velocity_km_s", velocity)?,
482    ))
483}
484
485fn gcrs_to_teme_compute_unchecked(
486    state: &TemeStateKm,
487    ts: &TimeScales,
488    skyfield_compat: bool,
489) -> (Vec3, Vec3) {
490    let [x, y, z] = state.position_km;
491    let [vx, vy, vz] = state.velocity_km_s;
492    let t = inline_tr(&build_teme_to_gcrs_matrix(ts, skyfield_compat));
493
494    if skyfield_compat {
495        let r_au = [x / AU_KM, y / AU_KM, z / AU_KM];
496        let r_teme_au = mat3_vec3_mul_fma(&t, &r_au);
497        let r_teme = (
498            r_teme_au[0] * AU_KM,
499            r_teme_au[1] * AU_KM,
500            r_teme_au[2] * AU_KM,
501        );
502
503        let v_au_d = [
504            vx / AU_KM * SECONDS_PER_DAY,
505            vy / AU_KM * SECONDS_PER_DAY,
506            vz / AU_KM * SECONDS_PER_DAY,
507        ];
508        let v_teme_au_d = mat3_vec3_mul_fma(&t, &v_au_d);
509        let v_teme = (
510            v_teme_au_d[0] * AU_KM / SECONDS_PER_DAY,
511            v_teme_au_d[1] * AU_KM / SECONDS_PER_DAY,
512            v_teme_au_d[2] * AU_KM / SECONDS_PER_DAY,
513        );
514        (r_teme, v_teme)
515    } else {
516        let r_gcrs = [x, y, z];
517        let r_t = mat3_vec3_mul_unchecked(&t, &r_gcrs);
518        let v_gcrs = [vx, vy, vz];
519        let v_t = mat3_vec3_mul_unchecked(&t, &v_gcrs);
520        ((r_t[0], r_t[1], r_t[2]), (v_t[0], v_t[1], v_t[2]))
521    }
522}
523
524// ---------------------------------------------------------------------------
525// GCRS -> ITRS (Earth-fixed / ECEF)
526// ---------------------------------------------------------------------------
527
528/// GCRS to true equator and equinox of date rotation.
529///
530/// This is the Earth-rotation-free `N * P * B` product already used inside the
531/// GCRS to ITRS construction: nutation, precession, and frame bias.
532pub fn gcrs_to_true_of_date_matrix(ts: &TimeScales) -> Result<Mat3, FrameTransformError> {
533    validate_time_scales(ts)?;
534    let (matrix, _dpsi) = gcrs_to_true_of_date_matrix_parts_unchecked(ts);
535    validate_mat3("gcrs_to_true_of_date_matrix", matrix)
536}
537
538fn gcrs_to_true_of_date_matrix_parts_unchecked(ts: &TimeScales) -> (Mat3, f64) {
539    let (dpsi, deps) = skyfield_iau2000a_radians_unchecked(ts.jd_tt);
540    let mean_ob = skyfield_mean_obliquity_radians_unchecked(ts.jd_tdb);
541    let true_ob = mean_ob + deps;
542
543    let n = build_skyfield_nutation_matrix_unchecked(mean_ob, true_ob, dpsi);
544    let p = compute_skyfield_precession_matrix_unchecked(ts.jd_tdb);
545    let b = build_icrs_to_j2000();
546
547    (inline_mxmxm(&n, &p, &b), dpsi)
548}
549
550/// Build the historical GCRS->ITRS rotation matrix for a given time.
551///
552/// This combines precession, nutation, and Earth rotation with zero polar
553/// motion, preserving the original bit-exact path. Use
554/// [`gcrs_to_itrs_matrix_with_polar_motion`] when `xp`/`yp` pole coordinates are
555/// available.
556pub fn gcrs_to_itrs_matrix(ts: &TimeScales) -> Result<Mat3, FrameTransformError> {
557    validate_time_scales(ts)?;
558    validate_mat3("gcrs_to_itrs_matrix", gcrs_to_itrs_matrix_unchecked(ts))
559}
560
561fn gcrs_to_itrs_matrix_unchecked(ts: &TimeScales) -> Mat3 {
562    let (m, dpsi) = gcrs_to_true_of_date_matrix_parts_unchecked(ts);
563    let gast = gast_radians(ts, dpsi);
564
565    // GAST rotation takes us from true-equator-equinox to ITRS
566    let r_gast = build_rot_z(-gast);
567
568    // GCRS->ITRS = R_z(-GAST) * (N * P * B)
569    inline_rxr(&r_gast, &m)
570}
571
572/// Build the GCRS->ITRS rotation matrix with explicit polar motion.
573///
574/// The embedded Earth-orientation table supplies UT1-UTC but not `xp`/`yp`, so
575/// callers that do not have pole coordinates should pass [`PolarMotion::ZERO`]
576/// or use [`gcrs_to_itrs_matrix`].
577pub fn gcrs_to_itrs_matrix_with_polar_motion(
578    ts: &TimeScales,
579    pole: PolarMotion,
580) -> Result<Mat3, FrameTransformError> {
581    validate_time_scales(ts)?;
582    validate_polar_motion(pole)?;
583    validate_mat3(
584        "gcrs_to_itrs_matrix",
585        gcrs_to_itrs_matrix_with_polar_motion_unchecked(ts, pole),
586    )
587}
588
589fn gcrs_to_itrs_matrix_with_polar_motion_unchecked(ts: &TimeScales, pole: PolarMotion) -> Mat3 {
590    apply_polar_motion_to_itrs_matrix(gcrs_to_itrs_matrix_unchecked(ts), pole)
591}
592
593/// Rotation from the **mean equator and equinox of date** to ITRS, i.e.
594/// `R_z(-GAST) * N` (nutation + Earth rotation, *without* precession or frame
595/// bias).
596///
597/// This is [`gcrs_to_itrs_matrix`] with the precession (`P`) and frame-bias
598/// (`B`) factors removed. Use it for vectors that are already referred to the
599/// mean equator/equinox of date (for example the low-precision analytic Sun/Moon
600/// series in [`crate::astro::bodies::sun_moon`], whose mean longitude and obliquity are
601/// of-date), so precession is not applied a second time. It mirrors the
602/// `eci2ecef` (GMST/GAST + nutation) rotation those series are designed to be
603/// consumed with, but uses the crate's IAU 2000A nutation and GAST.
604pub fn mean_of_date_to_itrs_matrix(ts: &TimeScales) -> Result<Mat3, FrameTransformError> {
605    validate_time_scales(ts)?;
606    validate_mat3(
607        "mean_of_date_to_itrs_matrix",
608        mean_of_date_to_itrs_matrix_unchecked(ts),
609    )
610}
611
612fn mean_of_date_to_itrs_matrix_unchecked(ts: &TimeScales) -> Mat3 {
613    let (dpsi, deps) = skyfield_iau2000a_radians_unchecked(ts.jd_tt);
614    let mean_ob = skyfield_mean_obliquity_radians_unchecked(ts.jd_tdb);
615    let true_ob = mean_ob + deps;
616
617    let n = build_skyfield_nutation_matrix_unchecked(mean_ob, true_ob, dpsi);
618    let gast = gast_radians(ts, dpsi);
619    let r_gast = build_rot_z(-gast);
620
621    // mean-of-date -> ITRS = R_z(-GAST) * N
622    inline_rxr(&r_gast, &n)
623}
624
625/// Mean-of-date to ITRS rotation with explicit polar motion.
626pub fn mean_of_date_to_itrs_matrix_with_polar_motion(
627    ts: &TimeScales,
628    pole: PolarMotion,
629) -> Result<Mat3, FrameTransformError> {
630    validate_time_scales(ts)?;
631    validate_polar_motion(pole)?;
632    validate_mat3(
633        "mean_of_date_to_itrs_matrix",
634        mean_of_date_to_itrs_matrix_with_polar_motion_unchecked(ts, pole),
635    )
636}
637
638fn mean_of_date_to_itrs_matrix_with_polar_motion_unchecked(
639    ts: &TimeScales,
640    pole: PolarMotion,
641) -> Mat3 {
642    apply_polar_motion_to_itrs_matrix(mean_of_date_to_itrs_matrix_unchecked(ts), pole)
643}
644
645/// Core GCRS->ITRS transform. Returns (x, y, z) in km.
646pub fn gcrs_to_itrs_compute(
647    x: f64,
648    y: f64,
649    z: f64,
650    ts: &TimeScales,
651    skyfield_compat: bool,
652) -> Result<(f64, f64, f64), FrameTransformError> {
653    validate_vec3("gcrs_position_km", &[x, y, z])?;
654    validate_time_scales(ts)?;
655    validate_tuple3(
656        "itrs_position_km",
657        gcrs_to_itrs_compute_unchecked(x, y, z, ts, skyfield_compat),
658    )
659}
660
661fn gcrs_to_itrs_compute_unchecked(
662    x: f64,
663    y: f64,
664    z: f64,
665    ts: &TimeScales,
666    skyfield_compat: bool,
667) -> (f64, f64, f64) {
668    let mat = gcrs_to_itrs_matrix_unchecked(ts);
669
670    if skyfield_compat {
671        // Skyfield: mxv(R, pos_au) in AU, then convert to km.
672        // For ITRS, scalar (non-FMA) multiply matches einsum's rounding.
673        // (Unlike TEME->GCRS where FMA is needed -- the difference is due to
674        // the specific matrix/vector values and how rounding interacts.)
675        let pos_au = [x / AU_KM, y / AU_KM, z / AU_KM];
676        let r = mat3_vec3_mul_unchecked(&mat, &pos_au);
677        (r[0] * AU_KM, r[1] * AU_KM, r[2] * AU_KM)
678    } else {
679        let pos = [x, y, z];
680        let r = mat3_vec3_mul_unchecked(&mat, &pos);
681        (r[0], r[1], r[2])
682    }
683}
684
685/// Core GCRS->ITRS transform with explicit polar motion.
686pub fn gcrs_to_itrs_compute_with_polar_motion(
687    x: f64,
688    y: f64,
689    z: f64,
690    ts: &TimeScales,
691    skyfield_compat: bool,
692    pole: PolarMotion,
693) -> Result<(f64, f64, f64), FrameTransformError> {
694    validate_vec3("gcrs_position_km", &[x, y, z])?;
695    validate_time_scales(ts)?;
696    validate_polar_motion(pole)?;
697    validate_tuple3(
698        "itrs_position_km",
699        gcrs_to_itrs_compute_with_polar_motion_unchecked(x, y, z, ts, skyfield_compat, pole),
700    )
701}
702
703fn gcrs_to_itrs_compute_with_polar_motion_unchecked(
704    x: f64,
705    y: f64,
706    z: f64,
707    ts: &TimeScales,
708    skyfield_compat: bool,
709    pole: PolarMotion,
710) -> (f64, f64, f64) {
711    let mat = gcrs_to_itrs_matrix_with_polar_motion_unchecked(ts, pole);
712
713    if skyfield_compat {
714        let pos_au = [x / AU_KM, y / AU_KM, z / AU_KM];
715        let r = mat3_vec3_mul_unchecked(&mat, &pos_au);
716        (r[0] * AU_KM, r[1] * AU_KM, r[2] * AU_KM)
717    } else {
718        let pos = [x, y, z];
719        let r = mat3_vec3_mul_unchecked(&mat, &pos);
720        (r[0], r[1], r[2])
721    }
722}
723
724// ---------------------------------------------------------------------------
725// ITRS -> GCRS (Earth-fixed / ECEF back to inertial)
726// ---------------------------------------------------------------------------
727
728/// Build the ITRS->GCRS rotation matrix for a given time.
729///
730/// This is the transpose of [`gcrs_to_itrs_matrix`]: the same precession,
731/// nutation, frame-bias, and Earth-rotation pipeline, taken the other way.
732pub fn itrs_to_gcrs_matrix(ts: &TimeScales) -> Result<Mat3, FrameTransformError> {
733    validate_time_scales(ts)?;
734    validate_mat3("itrs_to_gcrs_matrix", itrs_to_gcrs_matrix_unchecked(ts))
735}
736
737fn itrs_to_gcrs_matrix_unchecked(ts: &TimeScales) -> Mat3 {
738    inline_tr(&gcrs_to_itrs_matrix_unchecked(ts))
739}
740
741/// Build the ITRS->GCRS rotation matrix with explicit polar motion.
742pub fn itrs_to_gcrs_matrix_with_polar_motion(
743    ts: &TimeScales,
744    pole: PolarMotion,
745) -> Result<Mat3, FrameTransformError> {
746    validate_time_scales(ts)?;
747    validate_polar_motion(pole)?;
748    validate_mat3(
749        "itrs_to_gcrs_matrix",
750        itrs_to_gcrs_matrix_with_polar_motion_unchecked(ts, pole),
751    )
752}
753
754fn itrs_to_gcrs_matrix_with_polar_motion_unchecked(ts: &TimeScales, pole: PolarMotion) -> Mat3 {
755    inline_tr(&gcrs_to_itrs_matrix_with_polar_motion_unchecked(ts, pole))
756}
757
758/// Core ITRS->GCRS transform. Returns (x, y, z) in km.
759///
760/// Uses the plain (non-FMA, no AU round-trip) km path. The Skyfield AU-scaled
761/// `mul_add` path is reserved for the GCRS->ITRS / TEME->GCRS directions that
762/// carry the 0-ULP parity contract; this reverse direction is an ordinary
763/// matrix-vector product.
764pub fn itrs_to_gcrs_compute(
765    x: f64,
766    y: f64,
767    z: f64,
768    ts: &TimeScales,
769) -> Result<(f64, f64, f64), FrameTransformError> {
770    validate_vec3("itrs_position_km", &[x, y, z])?;
771    validate_time_scales(ts)?;
772    validate_tuple3(
773        "gcrs_position_km",
774        itrs_to_gcrs_compute_unchecked(x, y, z, ts),
775    )
776}
777
778fn itrs_to_gcrs_compute_unchecked(x: f64, y: f64, z: f64, ts: &TimeScales) -> (f64, f64, f64) {
779    let mat = itrs_to_gcrs_matrix_unchecked(ts);
780    let r = mat3_vec3_mul_unchecked(&mat, &[x, y, z]);
781    (r[0], r[1], r[2])
782}
783
784/// Core ITRS->GCRS transform with explicit polar motion.
785pub fn itrs_to_gcrs_compute_with_polar_motion(
786    x: f64,
787    y: f64,
788    z: f64,
789    ts: &TimeScales,
790    pole: PolarMotion,
791) -> Result<(f64, f64, f64), FrameTransformError> {
792    validate_vec3("itrs_position_km", &[x, y, z])?;
793    validate_time_scales(ts)?;
794    validate_polar_motion(pole)?;
795    validate_tuple3(
796        "gcrs_position_km",
797        itrs_to_gcrs_compute_with_polar_motion_unchecked(x, y, z, ts, pole),
798    )
799}
800
801fn itrs_to_gcrs_compute_with_polar_motion_unchecked(
802    x: f64,
803    y: f64,
804    z: f64,
805    ts: &TimeScales,
806    pole: PolarMotion,
807) -> (f64, f64, f64) {
808    let mat = itrs_to_gcrs_matrix_with_polar_motion_unchecked(ts, pole);
809    let r = mat3_vec3_mul_unchecked(&mat, &[x, y, z]);
810    (r[0], r[1], r[2])
811}
812
813// ---------------------------------------------------------------------------
814// ITRS -> Geodetic (WGS84 lat/lon/alt)
815// ---------------------------------------------------------------------------
816
817/// Convert ECEF/ITRS (km) to geodetic coordinates.
818/// Returns (latitude_deg, longitude_deg, altitude_km).
819///
820/// Replicates Skyfield's exact algorithm (wgs84.subpoint / _compute_latitude)
821/// which works in AU with exactly 3 iterations.
822pub fn itrs_to_geodetic_compute(
823    x: f64,
824    y: f64,
825    z: f64,
826) -> Result<(f64, f64, f64), FrameTransformError> {
827    validate_vec3("itrs_position_km", &[x, y, z])?;
828    validate_tuple3("geodetic", itrs_to_geodetic_compute_unchecked(x, y, z))
829}
830
831fn itrs_to_geodetic_compute_unchecked(x: f64, y: f64, z: f64) -> (f64, f64, f64) {
832    // Convert to AU to match Skyfield's computation path.
833    let x_au = x / AU_KM;
834    let y_au = y / AU_KM;
835    let z_au = z / AU_KM;
836
837    let a_au = WGS84_A_KM / AU_KM; // Earth equatorial radius in AU
838    let r_xy = (x_au * x_au + y_au * y_au).sqrt();
839
840    // Longitude: match Skyfield's exact normalization:
841    // (arctan2(y, x) - pi) % tau - pi
842    // Python's % always returns positive; Rust's can be negative.
843    let lon_raw = y_au.atan2(x_au);
844    let pi = std::f64::consts::PI;
845    let mut lon_shifted = (lon_raw - pi) % TAU;
846    if lon_shifted < 0.0 {
847        lon_shifted += TAU;
848    }
849    let lon = lon_shifted - pi;
850
851    // Latitude: 3 iterations matching Skyfield exactly
852    let mut lat = z_au.atan2(r_xy);
853    let mut a_c = 0.0_f64;
854    let mut hyp = 0.0_f64;
855
856    for _ in 0..3 {
857        let sin_lat = lat.sin();
858        let e2_sin_lat = WGS84_E2 * sin_lat;
859        a_c = a_au / (1.0 - e2_sin_lat * sin_lat).sqrt();
860        hyp = z_au + a_c * e2_sin_lat;
861        lat = hyp.atan2(r_xy);
862    }
863
864    // Elevation in AU, then convert to km
865    let height_au = (hyp * hyp + r_xy * r_xy).sqrt() - a_c;
866    let alt = height_au * AU_KM;
867
868    // Skyfield's Angle.degrees uses: radians * 360.0 / tau
869    // This gives different rounding than radians * (180.0 / PI).
870    (lat * 360.0 / TAU, lon * 360.0 / TAU, alt)
871}
872
873fn proj_normal_radius_of_curvature(sinphi: f64) -> f64 {
874    if PROJ_WGS84_ES == 0.0 {
875        return PROJ_WGS84_A_M;
876    }
877    PROJ_WGS84_A_M / (1.0 - (PROJ_WGS84_ES * sinphi) * sinphi).sqrt()
878}
879
880fn proj_geocentric_radius(cosphi: f64, sinphi: f64) -> f64 {
881    ((PROJ_WGS84_A_M * PROJ_WGS84_A_M) * cosphi).hypot((PROJ_WGS84_B_M * PROJ_WGS84_B_M) * sinphi)
882        / (PROJ_WGS84_A_M * cosphi).hypot(PROJ_WGS84_B_M * sinphi)
883}
884
885/// Convert ECEF meters to `(longitude_degrees, latitude_degrees, altitude_m)`.
886///
887/// This is an additive PROJ parity variant and does not replace
888/// [`itrs_to_geodetic_compute`]. It matches pyproj 3.6.1 / PROJ 9.3.0 for
889/// `EPSG:4978 -> EPSG:4979` with `always_xy=True`; its Tier 1 bit fixture is
890/// `crates/sidereon-core/tests/fixtures/geodetic/geodetic_proj.json`, generated
891/// by `crates/sidereon-core/fixtures-generators/generate_geodetic_proj.py`.
892pub fn geodetic_from_ecef_proj(x: f64, y: f64, z: f64) -> Result<[f64; 3], FrameTransformError> {
893    validate_vec3("ecef_m", &[x, y, z])?;
894    validate_array3("geodetic_proj", geodetic_from_ecef_proj_unchecked(x, y, z))
895}
896
897fn geodetic_from_ecef_proj_unchecked(x: f64, y: f64, z: f64) -> [f64; 3] {
898    let p = x.hypot(y);
899
900    let y_theta = z * PROJ_WGS84_A_M;
901    let x_theta = p * PROJ_WGS84_B_M;
902    let norm = y_theta.hypot(x_theta);
903    let c = if norm == 0.0 { 1.0 } else { x_theta / norm };
904    let s = if norm == 0.0 { 0.0 } else { y_theta / norm };
905
906    let y_phi = z + ((((PROJ_WGS84_E2S * PROJ_WGS84_B_M) * s) * s) * s);
907    let x_phi = p - ((((PROJ_WGS84_ES * PROJ_WGS84_A_M) * c) * c) * c);
908    let norm_phi = y_phi.hypot(x_phi);
909    let mut cosphi = if norm_phi == 0.0 {
910        1.0
911    } else {
912        x_phi / norm_phi
913    };
914    let mut sinphi = if norm_phi == 0.0 {
915        0.0
916    } else {
917        y_phi / norm_phi
918    };
919
920    let phi = if x_phi <= 0.0 {
921        cosphi = 0.0;
922        if z >= 0.0 {
923            sinphi = 1.0;
924            PROJ_HALF_PI
925        } else {
926            sinphi = -1.0;
927            -PROJ_HALF_PI
928        }
929    } else {
930        (y_phi / x_phi).atan()
931    };
932
933    let lam = y.atan2(x);
934    let alt = if cosphi < 1e-6 {
935        z.abs() - proj_geocentric_radius(cosphi, sinphi)
936    } else {
937        p / cosphi - proj_normal_radius_of_curvature(sinphi)
938    };
939
940    [lam * PROJ_RAD_TO_DEG, phi * PROJ_RAD_TO_DEG, alt]
941}
942
943// ---------------------------------------------------------------------------
944// Topocentric (az/el/range) from ground station to satellite
945// ---------------------------------------------------------------------------
946
947/// Convert geodetic (lat_deg, lon_deg, alt_km) to ECEF/ITRS (km).
948pub fn geodetic_to_itrs(
949    lat_deg: f64,
950    lon_deg: f64,
951    alt_km: f64,
952) -> Result<(f64, f64, f64), FrameTransformError> {
953    validate_geodetic_degrees_km(lat_deg, lon_deg, alt_km)?;
954    validate_tuple3(
955        "itrs_position_km",
956        geodetic_to_itrs_unchecked(lat_deg, lon_deg, alt_km),
957    )
958}
959
960fn geodetic_to_itrs_unchecked(lat_deg: f64, lon_deg: f64, alt_km: f64) -> (f64, f64, f64) {
961    let lat = lat_deg.to_radians();
962    let lon = lon_deg.to_radians();
963
964    let sin_lat = lat.sin();
965    let cos_lat = lat.cos();
966    let sin_lon = lon.sin();
967    let cos_lon = lon.cos();
968
969    let n = WGS84_A_KM / (1.0 - WGS84_E2 * sin_lat * sin_lat).sqrt();
970
971    let x = (n + alt_km) * cos_lat * cos_lon;
972    let y = (n + alt_km) * cos_lat * sin_lon;
973    let z = (n * (1.0 - WGS84_E2) + alt_km) * sin_lat;
974
975    (x, y, z)
976}
977
978/// Compute station ECEF/ITRS position directly in AU.
979/// Matches Skyfield's Geoid.latlon which works in AU from the start,
980/// avoiding the km->AU_KM division that introduces 1 ULP rounding.
981fn geodetic_to_itrs_au(lat_deg: f64, lon_deg: f64, alt_km: f64) -> [f64; 3] {
982    let lat = lat_deg * TAU / 360.0;
983    let lon = lon_deg * TAU / 360.0;
984
985    let sinphi = lat.sin();
986    let cosphi = lat.cos();
987
988    let radius_au = WGS84_A_KM / AU_KM;
989    let elevation_au = alt_km / AU_KM;
990
991    let omf2 = (1.0 - WGS84_F) * (1.0 - WGS84_F);
992    let c = 1.0 / (cosphi * cosphi + sinphi * sinphi * omf2).sqrt();
993    let s = omf2 * c;
994
995    let radius_xy = radius_au * c;
996    let xy = (radius_xy + elevation_au) * cosphi;
997    let x = xy * lon.cos();
998    let y = xy * lon.sin();
999
1000    let radius_z = radius_au * s;
1001    let z = (radius_z + elevation_au) * sinphi;
1002
1003    [x, y, z]
1004}
1005
1006/// Build the ECEF->ENU rotation matrix for a given geodetic position.
1007fn ecef_to_enu_matrix(lat_deg: f64, lon_deg: f64) -> Mat3 {
1008    let lat = lat_deg.to_radians();
1009    let lon = lon_deg.to_radians();
1010
1011    let sin_lat = lat.sin();
1012    let cos_lat = lat.cos();
1013    let sin_lon = lon.sin();
1014    let cos_lon = lon.cos();
1015
1016    // ENU rotation matrix:
1017    // E = [-sin(lon),           cos(lon),          0       ]
1018    // N = [-sin(lat)*cos(lon), -sin(lat)*sin(lon), cos(lat)]
1019    // U = [ cos(lat)*cos(lon),  cos(lat)*sin(lon), sin(lat)]
1020    [
1021        [-sin_lon, cos_lon, 0.0],
1022        [-sin_lat * cos_lon, -sin_lat * sin_lon, cos_lat],
1023        [cos_lat * cos_lon, cos_lat * sin_lon, sin_lat],
1024    ]
1025}
1026
1027/// Compute topocentric az/el/range from a ground station to a satellite.
1028///
1029/// Returns (azimuth_deg, elevation_deg, range_km).
1030pub fn gcrs_to_topocentric_compute(
1031    sat_gcrs_km: [f64; 3],
1032    station: &GeodeticStationKm,
1033    ts: &TimeScales,
1034    skyfield_compat: bool,
1035) -> Result<(f64, f64, f64), FrameTransformError> {
1036    validate_vec3("sat_gcrs_km", &sat_gcrs_km)?;
1037    validate_geodetic_degrees_km(
1038        station.latitude_deg,
1039        station.longitude_deg,
1040        station.altitude_km,
1041    )?;
1042    validate_time_scales(ts)?;
1043    validate_tuple3(
1044        "topocentric",
1045        gcrs_to_topocentric_compute_unchecked(sat_gcrs_km, station, ts, skyfield_compat),
1046    )
1047}
1048
1049fn gcrs_to_topocentric_compute_unchecked(
1050    sat_gcrs_km: [f64; 3],
1051    station: &GeodeticStationKm,
1052    ts: &TimeScales,
1053    skyfield_compat: bool,
1054) -> (f64, f64, f64) {
1055    let [sat_x, sat_y, sat_z] = sat_gcrs_km;
1056    let station_lat_deg = station.latitude_deg;
1057    let station_lon_deg = station.longitude_deg;
1058    let station_alt_km = station.altitude_km;
1059    if skyfield_compat {
1060        return gcrs_to_topocentric_skyfield(
1061            sat_x,
1062            sat_y,
1063            sat_z,
1064            station_lat_deg,
1065            station_lon_deg,
1066            station_alt_km,
1067            ts,
1068        );
1069    }
1070
1071    // Standard path: GCRS->ITRS->ENU topocentric reduction.
1072    let (sat_itrs_x, sat_itrs_y, sat_itrs_z) =
1073        gcrs_to_itrs_compute_unchecked(sat_x, sat_y, sat_z, ts, false);
1074    itrs_to_topocentric_unchecked([sat_itrs_x, sat_itrs_y, sat_itrs_z], station)
1075}
1076
1077/// Topocentric az/el/range from a station to an Earth-fixed (ITRS/ECEF) target.
1078///
1079/// `target_itrs_km` is the target's geocentric position in the Earth-fixed
1080/// (ITRS/ECEF) frame, km. Returns `(azimuth_deg, elevation_deg, range_km)`,
1081/// azimuth measured clockwise from north on `[0, 360)` and elevation on
1082/// `[-90, 90]`. This is the same station-to-target ENU reduction the satellite
1083/// look-angle path uses, exposed for callers that already hold an Earth-fixed
1084/// target vector (for example the analytic Sun/Moon ECEF positions from
1085/// [`crate::astro::bodies::sun_moon::sun_moon_ecef`]).
1086pub fn itrs_to_topocentric(
1087    target_itrs_km: [f64; 3],
1088    station: &GeodeticStationKm,
1089) -> Result<(f64, f64, f64), FrameTransformError> {
1090    validate_vec3("target_itrs_km", &target_itrs_km)?;
1091    validate_geodetic_degrees_km(
1092        station.latitude_deg,
1093        station.longitude_deg,
1094        station.altitude_km,
1095    )?;
1096    validate_tuple3(
1097        "topocentric",
1098        itrs_to_topocentric_unchecked(target_itrs_km, station),
1099    )
1100}
1101
1102fn itrs_to_topocentric_unchecked(target_itrs_km: [f64; 3], station: &GeodeticStationKm) -> Vec3 {
1103    let [target_x, target_y, target_z] = target_itrs_km;
1104    let (stn_x, stn_y, stn_z) = geodetic_to_itrs_unchecked(
1105        station.latitude_deg,
1106        station.longitude_deg,
1107        station.altitude_km,
1108    );
1109
1110    let dx = target_x - stn_x;
1111    let dy = target_y - stn_y;
1112    let dz = target_z - stn_z;
1113
1114    let enu_mat = ecef_to_enu_matrix(station.latitude_deg, station.longitude_deg);
1115    let enu = mat3_vec3_mul_unchecked(&enu_mat, &[dx, dy, dz]);
1116    let east = enu[0];
1117    let north = enu[1];
1118    let up = enu[2];
1119
1120    // Range
1121    let range = (east * east + north * north + up * up).sqrt();
1122
1123    // Elevation
1124    let elevation = (up / range).asin().to_degrees();
1125
1126    // Azimuth (measured clockwise from north). At (and arbitrarily near) the
1127    // station zenith the east and north components are pure rounding residuals,
1128    // so azimuth is degenerate and defined to be 0.0 (RTKLIB satazel semantics).
1129    let horiz_sq = east * east + north * north;
1130    let mut azimuth = if horiz_sq < AZIMUTH_ZENITH_EPS * range * range {
1131        0.0
1132    } else {
1133        east.atan2(north).to_degrees()
1134    };
1135    if azimuth < 0.0 {
1136        azimuth += 360.0;
1137    }
1138
1139    (azimuth, elevation, range)
1140}
1141
1142/// Skyfield-compatible topocentric: stays in GCRS AU the entire time.
1143///
1144/// Replicates Skyfield's altaz computation:
1145/// 1. R_lat = rot_y(lat)[::-1]  (row-reversed Y rotation)
1146/// 2. R_latlon = mxm(R_lat, rot_z(-lon))
1147/// 3. R_full = mxm(R_latlon, itrs_rotation)
1148/// 4. station_gcrs_au = transpose(itrs_rotation) * station_itrs_au
1149/// 5. diff_au = sat_gcrs_au - station_gcrs_au
1150/// 6. enu_au = mxv(R_full, diff_au)
1151/// 7. to_spherical(enu_au) -> (range_au, elevation_rad, azimuth_rad)
1152fn gcrs_to_topocentric_skyfield(
1153    sat_x: f64,
1154    sat_y: f64,
1155    sat_z: f64,
1156    station_lat_deg: f64,
1157    station_lon_deg: f64,
1158    station_alt_km: f64,
1159    ts: &TimeScales,
1160) -> (f64, f64, f64) {
1161    let lat_rad = station_lat_deg * TAU / 360.0;
1162    let lon_rad = station_lon_deg * TAU / 360.0;
1163
1164    // Build R_lat = rot_y(lat)[::-1]  (rows reversed)
1165    let cy = lat_rad.cos();
1166    let sy = lat_rad.sin();
1167    // rot_y(lat) = [[cy, 0, sy], [0, 1, 0], [-sy, 0, cy]]
1168    // [::-1] reverses rows: [[-sy, 0, cy], [0, 1, 0], [cy, 0, sy]]
1169    let r_lat: Mat3 = [[-sy, 0.0, cy], [0.0, 1.0, 0.0], [cy, 0.0, sy]];
1170
1171    // R_latlon = mxm(R_lat, rot_z(-lon))
1172    let rz_neg_lon = build_rot_z(-lon_rad);
1173    let r_latlon = inline_rxr(&r_lat, &rz_neg_lon);
1174
1175    // R_full = mxm(R_latlon, itrs_rotation)
1176    let r_itrs = gcrs_to_itrs_matrix_unchecked(ts);
1177    let r_full = inline_rxr(&r_latlon, &r_itrs);
1178
1179    // Station ITRS position directly in AU, matching Skyfield's Geoid.latlon
1180    // which computes in AU from the start (not km then / AU_KM).
1181    let stn_itrs_au = geodetic_to_itrs_au(station_lat_deg, station_lon_deg, station_alt_km);
1182
1183    // Station GCRS AU = transpose(R_itrs) * station_itrs_au
1184    let r_itrs_t = inline_tr(&r_itrs);
1185    let stn_gcrs_au = mat3_vec3_mul_unchecked(&r_itrs_t, &stn_itrs_au);
1186
1187    // Satellite GCRS in AU
1188    let sat_au = [sat_x / AU_KM, sat_y / AU_KM, sat_z / AU_KM];
1189
1190    // Difference vector in GCRS AU
1191    let diff_au = [
1192        sat_au[0] - stn_gcrs_au[0],
1193        sat_au[1] - stn_gcrs_au[1],
1194        sat_au[2] - stn_gcrs_au[2],
1195    ];
1196
1197    // Rotate to ENU-ish frame: mxv(R_full, diff_au)
1198    let enu_au = mat3_vec3_mul_unchecked(&r_full, &diff_au);
1199
1200    // to_spherical: r, theta (elevation), phi (azimuth)
1201    let ex = enu_au[0];
1202    let ey = enu_au[1];
1203    let ez = enu_au[2];
1204
1205    let r_au = (ex * ex + ey * ey + ez * ez).sqrt();
1206    let elevation_rad = ez.atan2((ex * ex + ey * ey).sqrt());
1207    let mut azimuth_rad = ey.atan2(ex) % TAU;
1208    if azimuth_rad < 0.0 {
1209        azimuth_rad += TAU;
1210    }
1211
1212    let range_km = r_au * AU_KM;
1213    let elevation_deg = elevation_rad * 360.0 / TAU;
1214    let azimuth_deg = azimuth_rad * 360.0 / TAU;
1215
1216    (azimuth_deg, elevation_deg, range_km)
1217}
1218
1219#[cfg(test)]
1220mod tests {
1221    use super::*;
1222    use crate::astro::time::scales::TimeScales;
1223
1224    fn assert_mat3_bits_eq(actual: &Mat3, expected: &Mat3) {
1225        for i in 0..3 {
1226            for j in 0..3 {
1227                assert_eq!(
1228                    actual[i][j].to_bits(),
1229                    expected[i][j].to_bits(),
1230                    "matrix[{i}][{j}]: {} vs {}",
1231                    actual[i][j],
1232                    expected[i][j]
1233                );
1234            }
1235        }
1236    }
1237
1238    fn assert_vec3_bits_eq(actual: [f64; 3], expected: [f64; 3]) {
1239        for i in 0..3 {
1240            assert_eq!(
1241                actual[i].to_bits(),
1242                expected[i].to_bits(),
1243                "vector[{i}]: {} vs {}",
1244                actual[i],
1245                expected[i]
1246            );
1247        }
1248    }
1249
1250    #[test]
1251    fn itrs_to_gcrs_inverts_gcrs_to_itrs() {
1252        // On a real epoch and a real-magnitude ECI vector, ITRS->GCRS recovers
1253        // the GCRS->ITRS input bit-for-bit on the plain (non-Skyfield) km path:
1254        // the two directions must be exact transposes, not just approximately so.
1255        let ts = TimeScales::from_utc(2020, 6, 24, 12, 34, 56.0).expect("valid UTC instant");
1256        let (x, y, z) = (4321.0_f64, -5678.0, 3210.0);
1257
1258        let (ix, iy, iz) =
1259            gcrs_to_itrs_compute(x, y, z, &ts, false).expect("valid frame transform");
1260        // The rotation actually moved the vector (it is not a no-op).
1261        assert!(((ix - x).abs() + (iy - y).abs() + (iz - z).abs()) > 100.0);
1262
1263        let (bx, by, bz) = itrs_to_gcrs_compute(ix, iy, iz, &ts).expect("valid frame transform");
1264        assert!((bx - x).abs() < 1e-9, "x {bx} vs {x}");
1265        assert!((by - y).abs() < 1e-9, "y {by} vs {y}");
1266        assert!((bz - z).abs() < 1e-9, "z {bz} vs {z}");
1267
1268        // Magnitude is preserved by the rotation.
1269        let n0 = (x * x + y * y + z * z).sqrt();
1270        let n1 = (ix * ix + iy * iy + iz * iz).sqrt();
1271        assert!((n0 - n1).abs() < 1e-9);
1272    }
1273
1274    #[test]
1275    fn polar_motion_matrix_matches_documented_convention() {
1276        let pole = PolarMotion::from_arcseconds(0.25, -0.35).expect("valid polar motion");
1277        let cx = pole.xp_rad.cos();
1278        let sx = pole.xp_rad.sin();
1279        let cy = pole.yp_rad.cos();
1280        let sy = pole.yp_rad.sin();
1281
1282        let expected = [
1283            [cx, sx * sy, sx * cy],
1284            [0.0, cy, -sy],
1285            [-sx, cx * sy, cx * cy],
1286        ];
1287        let got = polar_motion_matrix(pole).expect("valid polar motion matrix");
1288        assert_mat3_bits_eq(&got, &expected);
1289
1290        let small_angle = [
1291            [1.0, 0.0, pole.xp_rad],
1292            [0.0, 1.0, -pole.yp_rad],
1293            [-pole.xp_rad, pole.yp_rad, 1.0],
1294        ];
1295        for i in 0..3 {
1296            for j in 0..3 {
1297                assert!(
1298                    (got[i][j] - small_angle[i][j]).abs() < 1.0e-11,
1299                    "matrix[{i}][{j}] {} vs small-angle {}",
1300                    got[i][j],
1301                    small_angle[i][j]
1302                );
1303            }
1304        }
1305    }
1306
1307    #[test]
1308    fn gcrs_to_itrs_with_polar_motion_premultiplies_legacy_rotation() {
1309        let ts = TimeScales::from_utc(2020, 6, 24, 12, 34, 56.0).expect("valid UTC instant");
1310        let pole = PolarMotion::from_arcseconds(0.18, -0.24).expect("valid polar motion");
1311        let legacy = gcrs_to_itrs_matrix(&ts).expect("valid frame transform");
1312        let expected = inline_rxr(
1313            &polar_motion_matrix(pole).expect("valid polar motion matrix"),
1314            &legacy,
1315        );
1316        let got = gcrs_to_itrs_matrix_with_polar_motion(&ts, pole).expect("valid frame transform");
1317
1318        assert_mat3_bits_eq(&got, &expected);
1319
1320        let pos = [4321.0_f64, -5678.0, 3210.0];
1321        let actual_vec =
1322            gcrs_to_itrs_compute_with_polar_motion(pos[0], pos[1], pos[2], &ts, false, pole)
1323                .expect("valid frame transform");
1324        let expected_vec = mat3_vec3_mul(&expected, &pos).expect("finite matrix-vector product");
1325        assert_vec3_bits_eq([actual_vec.0, actual_vec.1, actual_vec.2], expected_vec);
1326
1327        let legacy_vec =
1328            gcrs_to_itrs_compute(pos[0], pos[1], pos[2], &ts, false).expect("valid transform");
1329        let delta = (actual_vec.0 - legacy_vec.0).abs()
1330            + (actual_vec.1 - legacy_vec.1).abs()
1331            + (actual_vec.2 - legacy_vec.2).abs();
1332        assert!(
1333            delta > 1.0e-4,
1334            "nonzero polar motion should move the vector"
1335        );
1336
1337        let inverse =
1338            itrs_to_gcrs_matrix_with_polar_motion(&ts, pole).expect("valid frame transform");
1339        assert_mat3_bits_eq(&inverse, &inline_tr(&got));
1340    }
1341
1342    #[test]
1343    fn zero_polar_motion_matches_legacy_transform_bits() {
1344        let ts = TimeScales::from_utc(2020, 6, 24, 12, 34, 56.0).expect("valid UTC instant");
1345        let legacy = gcrs_to_itrs_matrix(&ts).expect("valid frame transform");
1346        let zero = gcrs_to_itrs_matrix_with_polar_motion(&ts, PolarMotion::ZERO)
1347            .expect("valid frame transform");
1348        assert_mat3_bits_eq(&zero, &legacy);
1349
1350        let mean_legacy = mean_of_date_to_itrs_matrix(&ts).expect("valid frame transform");
1351        let mean_zero = mean_of_date_to_itrs_matrix_with_polar_motion(&ts, PolarMotion::ZERO)
1352            .expect("valid frame transform");
1353        assert_mat3_bits_eq(&mean_zero, &mean_legacy);
1354
1355        let pos = [4321.0_f64, -5678.0, 3210.0];
1356        for skyfield_compat in [false, true] {
1357            let legacy_vec = gcrs_to_itrs_compute(pos[0], pos[1], pos[2], &ts, skyfield_compat)
1358                .expect("valid frame transform");
1359            let zero_vec = gcrs_to_itrs_compute_with_polar_motion(
1360                pos[0],
1361                pos[1],
1362                pos[2],
1363                &ts,
1364                skyfield_compat,
1365                PolarMotion::ZERO,
1366            )
1367            .expect("valid frame transform");
1368            assert_vec3_bits_eq(
1369                [zero_vec.0, zero_vec.1, zero_vec.2],
1370                [legacy_vec.0, legacy_vec.1, legacy_vec.2],
1371            );
1372        }
1373
1374        let legacy_back =
1375            itrs_to_gcrs_compute(pos[0], pos[1], pos[2], &ts).expect("valid frame transform");
1376        let zero_back =
1377            itrs_to_gcrs_compute_with_polar_motion(pos[0], pos[1], pos[2], &ts, PolarMotion::ZERO)
1378                .expect("valid frame transform");
1379        assert_vec3_bits_eq(
1380            [zero_back.0, zero_back.1, zero_back.2],
1381            [legacy_back.0, legacy_back.1, legacy_back.2],
1382        );
1383    }
1384
1385    #[test]
1386    fn gcrs_to_teme_inverts_teme_to_gcrs() {
1387        let ts = TimeScales::from_utc(2020, 6, 24, 12, 34, 56.0).expect("valid UTC instant");
1388        let teme = TemeStateKm {
1389            position_km: [6524.834, 6862.875, 6448.296],
1390            velocity_km_s: [4.901327, 5.533756, -1.976341],
1391        };
1392
1393        for skyfield_compat in [false, true] {
1394            let (gcrs_position, gcrs_velocity) =
1395                teme_to_gcrs_compute(&teme, &ts, skyfield_compat).expect("valid transform");
1396            let gcrs = TemeStateKm {
1397                position_km: [gcrs_position.0, gcrs_position.1, gcrs_position.2],
1398                velocity_km_s: [gcrs_velocity.0, gcrs_velocity.1, gcrs_velocity.2],
1399            };
1400            let (round_position, round_velocity) =
1401                gcrs_to_teme_compute(&gcrs, &ts, skyfield_compat).expect("valid inverse");
1402
1403            let pos = [round_position.0, round_position.1, round_position.2];
1404            let vel = [round_velocity.0, round_velocity.1, round_velocity.2];
1405            for axis in 0..3 {
1406                assert!(
1407                    (pos[axis] - teme.position_km[axis]).abs() <= 1.0e-9,
1408                    "position axis {axis} mode {skyfield_compat}"
1409                );
1410                assert!(
1411                    (vel[axis] - teme.velocity_km_s[axis]).abs() <= 1.0e-12,
1412                    "velocity axis {axis} mode {skyfield_compat}"
1413                );
1414            }
1415        }
1416    }
1417
1418    #[test]
1419    fn frame_transforms_reject_nonfinite_time() {
1420        let mut ts = TimeScales::from_utc(2020, 6, 24, 12, 34, 56.0).expect("valid UTC instant");
1421        ts.jd_tt = f64::NAN;
1422
1423        assert!(greenwich_mean_sidereal_time_radians(&ts).is_err());
1424        assert!(gcrs_to_itrs_matrix(&ts).is_err());
1425        assert!(itrs_to_gcrs_compute(1.0, 2.0, 3.0, &ts).is_err());
1426    }
1427
1428    #[test]
1429    fn frame_transforms_reject_nonfinite_pole_coordinates() {
1430        let ts = TimeScales::from_utc(2020, 6, 24, 12, 34, 56.0).expect("valid UTC instant");
1431        assert!(PolarMotion::from_radians(f64::NAN, 0.0).is_err());
1432        assert!(PolarMotion::from_arcseconds(0.0, f64::INFINITY).is_err());
1433
1434        let pole = PolarMotion {
1435            xp_rad: f64::NAN,
1436            yp_rad: 0.0,
1437        };
1438        assert!(polar_motion_matrix(pole).is_err());
1439        assert!(gcrs_to_itrs_matrix_with_polar_motion(&ts, pole).is_err());
1440    }
1441
1442    #[test]
1443    fn frame_transforms_reject_nonfinite_vectors() {
1444        let ts = TimeScales::from_utc(2020, 6, 24, 12, 34, 56.0).expect("valid UTC instant");
1445        let bad_state = TemeStateKm {
1446            position_km: [1.0, f64::NAN, 3.0],
1447            velocity_km_s: [0.1, 0.2, 0.3],
1448        };
1449        assert!(teme_to_gcrs_compute(&bad_state, &ts, false).is_err());
1450        assert!(gcrs_to_itrs_compute(1.0, f64::INFINITY, 3.0, &ts, false).is_err());
1451        assert!(itrs_to_gcrs_compute(1.0, 2.0, f64::NEG_INFINITY, &ts).is_err());
1452    }
1453
1454    #[test]
1455    fn validated_frame_transform_preserves_valid_bits() {
1456        let ts = TimeScales::from_utc(2020, 6, 24, 12, 34, 56.0).expect("valid UTC instant");
1457        let pos = [4321.0_f64, -5678.0, 3210.0];
1458        let expected = gcrs_to_itrs_compute_unchecked(pos[0], pos[1], pos[2], &ts, true);
1459        let got =
1460            gcrs_to_itrs_compute(pos[0], pos[1], pos[2], &ts, true).expect("valid frame transform");
1461        assert_vec3_bits_eq([got.0, got.1, got.2], [expected.0, expected.1, expected.2]);
1462    }
1463
1464    #[test]
1465    fn geodetic_transforms_reject_invalid_coordinates() {
1466        assert!(itrs_to_geodetic_compute(f64::NAN, 0.0, 0.0).is_err());
1467        assert!(geodetic_from_ecef_proj(0.0, f64::INFINITY, 0.0).is_err());
1468        assert!(geodetic_to_itrs(90.000_001, 0.0, 0.0).is_err());
1469        assert!(geodetic_to_itrs(0.0, -180.000_001, 0.0).is_err());
1470        assert!(geodetic_to_itrs(0.0, 0.0, f64::NAN).is_err());
1471    }
1472
1473    #[test]
1474    fn topocentric_transform_rejects_invalid_coordinates() {
1475        let ts = TimeScales::from_utc(2020, 6, 24, 12, 34, 56.0).expect("valid UTC instant");
1476        let station = GeodeticStationKm {
1477            latitude_deg: f64::NAN,
1478            longitude_deg: 0.0,
1479            altitude_km: 0.0,
1480        };
1481        assert!(gcrs_to_topocentric_compute([7000.0, 0.0, 0.0], &station, &ts, false).is_err());
1482
1483        let station = GeodeticStationKm {
1484            latitude_deg: 0.0,
1485            longitude_deg: 181.0,
1486            altitude_km: 0.0,
1487        };
1488        assert!(gcrs_to_topocentric_compute([7000.0, 0.0, 0.0], &station, &ts, false).is_err());
1489
1490        let station = GeodeticStationKm {
1491            latitude_deg: 0.0,
1492            longitude_deg: 0.0,
1493            altitude_km: 0.0,
1494        };
1495        assert!(
1496            gcrs_to_topocentric_compute([7000.0, f64::NAN, 0.0], &station, &ts, false).is_err()
1497        );
1498    }
1499
1500    #[test]
1501    fn topocentric_azimuth_is_zero_at_station_zenith() {
1502        let ts = TimeScales::from_utc(2020, 6, 24, 12, 34, 56.0).expect("valid UTC instant");
1503        // Equatorial station: geodetic up coincides with the geocentric radial,
1504        // so a satellite displaced radially outward is exactly overhead.
1505        let station = GeodeticStationKm {
1506            latitude_deg: 0.0,
1507            longitude_deg: 0.0,
1508            altitude_km: 0.0,
1509        };
1510        let (sx, sy, sz) = geodetic_to_itrs_unchecked(0.0, 0.0, 0.0);
1511        // Satellite ITRS position 20,000 km straight up (+X at lat0/lon0).
1512        let sat_itrs = [sx + 20_000.0, sy, sz];
1513        // Express it in GCRS so the standard path rotates it back to `sat_itrs`.
1514        let r_itrs = gcrs_to_itrs_matrix_unchecked(&ts);
1515        let r_itrs_t = inline_tr(&r_itrs);
1516        let sat_gcrs = mat3_vec3_mul_unchecked(&r_itrs_t, &sat_itrs);
1517
1518        let (azimuth_deg, elevation_deg, _range_km) =
1519            gcrs_to_topocentric_compute_unchecked(sat_gcrs, &station, &ts, false);
1520        assert_eq!(azimuth_deg, 0.0);
1521        assert!(azimuth_deg.is_finite());
1522        assert!((elevation_deg - 90.0).abs() < 1e-6);
1523    }
1524
1525    #[test]
1526    fn validated_geodetic_transform_preserves_valid_bits() {
1527        let (lat, lon, alt) = (51.4779, -0.0015, 0.046);
1528        let expected = geodetic_to_itrs_unchecked(lat, lon, alt);
1529        let got = geodetic_to_itrs(lat, lon, alt).expect("valid geodetic coordinates");
1530        assert_eq!(got.0.to_bits(), expected.0.to_bits());
1531        assert_eq!(got.1.to_bits(), expected.1.to_bits());
1532        assert_eq!(got.2.to_bits(), expected.2.to_bits());
1533
1534        let expected = itrs_to_geodetic_compute_unchecked(got.0, got.1, got.2);
1535        let roundtrip =
1536            itrs_to_geodetic_compute(got.0, got.1, got.2).expect("valid ITRS coordinates");
1537        assert_eq!(roundtrip.0.to_bits(), expected.0.to_bits());
1538        assert_eq!(roundtrip.1.to_bits(), expected.1.to_bits());
1539        assert_eq!(roundtrip.2.to_bits(), expected.2.to_bits());
1540    }
1541
1542    #[test]
1543    fn sidereal_time_wrappers_are_in_range_and_consistent() {
1544        let ts = TimeScales::from_utc(2020, 6, 24, 12, 34, 56.0).expect("valid UTC instant");
1545        let gmst = greenwich_mean_sidereal_time_radians(&ts).expect("valid sidereal time");
1546        let gast = greenwich_apparent_sidereal_time_radians(&ts).expect("valid sidereal time");
1547
1548        // Both land in [0, 2pi).
1549        assert!((0.0..TAU).contains(&gmst), "gmst {gmst}");
1550        assert!((0.0..TAU).contains(&gast), "gast {gast}");
1551
1552        // The equation of the equinoxes is a small (sub-arcminute) offset, so the
1553        // apparent and mean sidereal times stay close (handle the seam at 2pi).
1554        let diff = (gast - gmst).rem_euclid(TAU);
1555        let eq_eq = diff.min(TAU - diff);
1556        assert!(eq_eq < 1.0e-3, "equation of equinoxes too large: {eq_eq}");
1557
1558        // The mean wrapper matches the Skyfield-parity IAU-1982 sidereal time.
1559        let gmst_hours = sidereal_time_hours(ts.jd_whole, ts.ut1_fraction, ts.tdb_fraction);
1560        assert_eq!(gmst, gmst_hours / 24.0 * TAU);
1561    }
1562
1563    #[test]
1564    fn gmst_from_j2000_seconds_matches_gmst1982_polynomial() {
1565        // The seconds-based accessor reproduces the GMST-1982 polynomial the TEME
1566        // sidereal rotation uses, not the public Skyfield-parity GMST. Tolerance
1567        // allows only the roundoff from reducing the UT1 split to one f64.
1568        const GMST_TOL_RAD: f64 = 1.0e-11;
1569        let ts = TimeScales::from_utc(2020, 6, 24, 12, 34, 56.25).expect("valid UTC instant");
1570        let sec = (ts.jd_ut1 - J2000_JD) * SECONDS_PER_DAY;
1571        let from_seconds = greenwich_mean_sidereal_time_radians_from_j2000_seconds(sec)
1572            .expect("valid sidereal time");
1573        let mut expected = compute_theta_gmst1982(ts.jd_whole, ts.ut1_fraction) % TAU;
1574        if expected < 0.0 {
1575            expected += TAU;
1576        }
1577
1578        assert!((from_seconds - expected).abs() <= GMST_TOL_RAD);
1579    }
1580}