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 = libm::cos(angle);
208    let s = libm::sin(angle);
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 = libm::cos(pole.xp_rad);
228    let sx = libm::sin(pole.xp_rad);
229    let cy = libm::cos(pole.yp_rad);
230    let sy = libm::sin(pole.yp_rad);
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 * libm::cos(mean_ob) + 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 = libm::atan2(y_au, 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 = libm::atan2(z_au, 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 = libm::sin(lat);
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 = libm::atan2(hyp, 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    libm::hypot(
882        (PROJ_WGS84_A_M * PROJ_WGS84_A_M) * cosphi,
883        (PROJ_WGS84_B_M * PROJ_WGS84_B_M) * sinphi,
884    ) / libm::hypot(PROJ_WGS84_A_M * cosphi, PROJ_WGS84_B_M * sinphi)
885}
886
887/// Convert ECEF meters to `(longitude_degrees, latitude_degrees, altitude_m)`.
888///
889/// This is an additive PROJ parity variant and does not replace
890/// [`itrs_to_geodetic_compute`]. It matches pyproj 3.6.1 / PROJ 9.3.0 for
891/// `EPSG:4978 -> EPSG:4979` with `always_xy=True`; its Tier 1 bit fixture is
892/// `crates/sidereon-core/tests/fixtures/geodetic/geodetic_proj.json`, generated
893/// by `crates/sidereon-core/fixtures-generators/generate_geodetic_proj.py`.
894pub fn geodetic_from_ecef_proj(x: f64, y: f64, z: f64) -> Result<[f64; 3], FrameTransformError> {
895    validate_vec3("ecef_m", &[x, y, z])?;
896    validate_array3("geodetic_proj", geodetic_from_ecef_proj_unchecked(x, y, z))
897}
898
899fn geodetic_from_ecef_proj_unchecked(x: f64, y: f64, z: f64) -> [f64; 3] {
900    let p = libm::hypot(x, y);
901
902    let y_theta = z * PROJ_WGS84_A_M;
903    let x_theta = p * PROJ_WGS84_B_M;
904    let norm = libm::hypot(y_theta, x_theta);
905    let c = if norm == 0.0 { 1.0 } else { x_theta / norm };
906    let s = if norm == 0.0 { 0.0 } else { y_theta / norm };
907
908    let y_phi = z + ((((PROJ_WGS84_E2S * PROJ_WGS84_B_M) * s) * s) * s);
909    let x_phi = p - ((((PROJ_WGS84_ES * PROJ_WGS84_A_M) * c) * c) * c);
910    let norm_phi = libm::hypot(y_phi, x_phi);
911    let mut cosphi = if norm_phi == 0.0 {
912        1.0
913    } else {
914        x_phi / norm_phi
915    };
916    let mut sinphi = if norm_phi == 0.0 {
917        0.0
918    } else {
919        y_phi / norm_phi
920    };
921
922    let phi = if x_phi <= 0.0 {
923        cosphi = 0.0;
924        if z >= 0.0 {
925            sinphi = 1.0;
926            PROJ_HALF_PI
927        } else {
928            sinphi = -1.0;
929            -PROJ_HALF_PI
930        }
931    } else {
932        libm::atan(y_phi / x_phi)
933    };
934
935    let lam = libm::atan2(y, x);
936    let alt = if cosphi < 1e-6 {
937        z.abs() - proj_geocentric_radius(cosphi, sinphi)
938    } else {
939        p / cosphi - proj_normal_radius_of_curvature(sinphi)
940    };
941
942    [lam * PROJ_RAD_TO_DEG, phi * PROJ_RAD_TO_DEG, alt]
943}
944
945// ---------------------------------------------------------------------------
946// Topocentric (az/el/range) from ground station to satellite
947// ---------------------------------------------------------------------------
948
949/// Convert geodetic (lat_deg, lon_deg, alt_km) to ECEF/ITRS (km).
950pub fn geodetic_to_itrs(
951    lat_deg: f64,
952    lon_deg: f64,
953    alt_km: f64,
954) -> Result<(f64, f64, f64), FrameTransformError> {
955    validate_geodetic_degrees_km(lat_deg, lon_deg, alt_km)?;
956    validate_tuple3(
957        "itrs_position_km",
958        geodetic_to_itrs_unchecked(lat_deg, lon_deg, alt_km),
959    )
960}
961
962fn geodetic_to_itrs_unchecked(lat_deg: f64, lon_deg: f64, alt_km: f64) -> (f64, f64, f64) {
963    let lat = lat_deg.to_radians();
964    let lon = lon_deg.to_radians();
965
966    let sin_lat = libm::sin(lat);
967    let cos_lat = libm::cos(lat);
968    let sin_lon = libm::sin(lon);
969    let cos_lon = libm::cos(lon);
970
971    let n = WGS84_A_KM / (1.0 - WGS84_E2 * sin_lat * sin_lat).sqrt();
972
973    let x = (n + alt_km) * cos_lat * cos_lon;
974    let y = (n + alt_km) * cos_lat * sin_lon;
975    let z = (n * (1.0 - WGS84_E2) + alt_km) * sin_lat;
976
977    (x, y, z)
978}
979
980/// Compute station ECEF/ITRS position directly in AU.
981/// Matches Skyfield's Geoid.latlon which works in AU from the start,
982/// avoiding the km->AU_KM division that introduces 1 ULP rounding.
983fn geodetic_to_itrs_au(lat_deg: f64, lon_deg: f64, alt_km: f64) -> [f64; 3] {
984    let lat = lat_deg * TAU / 360.0;
985    let lon = lon_deg * TAU / 360.0;
986
987    let sinphi = libm::sin(lat);
988    let cosphi = libm::cos(lat);
989
990    let radius_au = WGS84_A_KM / AU_KM;
991    let elevation_au = alt_km / AU_KM;
992
993    let omf2 = (1.0 - WGS84_F) * (1.0 - WGS84_F);
994    let c = 1.0 / (cosphi * cosphi + sinphi * sinphi * omf2).sqrt();
995    let s = omf2 * c;
996
997    let radius_xy = radius_au * c;
998    let xy = (radius_xy + elevation_au) * cosphi;
999    let x = xy * libm::cos(lon);
1000    let y = xy * libm::sin(lon);
1001
1002    let radius_z = radius_au * s;
1003    let z = (radius_z + elevation_au) * sinphi;
1004
1005    [x, y, z]
1006}
1007
1008/// Build the ECEF->ENU rotation matrix for a given geodetic position.
1009fn ecef_to_enu_matrix(lat_deg: f64, lon_deg: f64) -> Mat3 {
1010    let lat = lat_deg.to_radians();
1011    let lon = lon_deg.to_radians();
1012
1013    let sin_lat = libm::sin(lat);
1014    let cos_lat = libm::cos(lat);
1015    let sin_lon = libm::sin(lon);
1016    let cos_lon = libm::cos(lon);
1017
1018    // ENU rotation matrix:
1019    // E = [-sin(lon),           cos(lon),          0       ]
1020    // N = [-sin(lat)*cos(lon), -sin(lat)*sin(lon), cos(lat)]
1021    // U = [ cos(lat)*cos(lon),  cos(lat)*sin(lon), sin(lat)]
1022    [
1023        [-sin_lon, cos_lon, 0.0],
1024        [-sin_lat * cos_lon, -sin_lat * sin_lon, cos_lat],
1025        [cos_lat * cos_lon, cos_lat * sin_lon, sin_lat],
1026    ]
1027}
1028
1029/// Compute topocentric az/el/range from a ground station to a satellite.
1030///
1031/// Returns (azimuth_deg, elevation_deg, range_km).
1032pub fn gcrs_to_topocentric_compute(
1033    sat_gcrs_km: [f64; 3],
1034    station: &GeodeticStationKm,
1035    ts: &TimeScales,
1036    skyfield_compat: bool,
1037) -> Result<(f64, f64, f64), FrameTransformError> {
1038    validate_vec3("sat_gcrs_km", &sat_gcrs_km)?;
1039    validate_geodetic_degrees_km(
1040        station.latitude_deg,
1041        station.longitude_deg,
1042        station.altitude_km,
1043    )?;
1044    validate_time_scales(ts)?;
1045    validate_tuple3(
1046        "topocentric",
1047        gcrs_to_topocentric_compute_unchecked(sat_gcrs_km, station, ts, skyfield_compat),
1048    )
1049}
1050
1051fn gcrs_to_topocentric_compute_unchecked(
1052    sat_gcrs_km: [f64; 3],
1053    station: &GeodeticStationKm,
1054    ts: &TimeScales,
1055    skyfield_compat: bool,
1056) -> (f64, f64, f64) {
1057    let [sat_x, sat_y, sat_z] = sat_gcrs_km;
1058    let station_lat_deg = station.latitude_deg;
1059    let station_lon_deg = station.longitude_deg;
1060    let station_alt_km = station.altitude_km;
1061    if skyfield_compat {
1062        return gcrs_to_topocentric_skyfield(
1063            sat_x,
1064            sat_y,
1065            sat_z,
1066            station_lat_deg,
1067            station_lon_deg,
1068            station_alt_km,
1069            ts,
1070        );
1071    }
1072
1073    // Standard path: GCRS->ITRS->ENU topocentric reduction.
1074    let (sat_itrs_x, sat_itrs_y, sat_itrs_z) =
1075        gcrs_to_itrs_compute_unchecked(sat_x, sat_y, sat_z, ts, false);
1076    itrs_to_topocentric_unchecked([sat_itrs_x, sat_itrs_y, sat_itrs_z], station)
1077}
1078
1079/// Topocentric az/el/range from a station to an Earth-fixed (ITRS/ECEF) target.
1080///
1081/// `target_itrs_km` is the target's geocentric position in the Earth-fixed
1082/// (ITRS/ECEF) frame, km. Returns `(azimuth_deg, elevation_deg, range_km)`,
1083/// azimuth measured clockwise from north on `[0, 360)` and elevation on
1084/// `[-90, 90]`. This is the same station-to-target ENU reduction the satellite
1085/// look-angle path uses, exposed for callers that already hold an Earth-fixed
1086/// target vector (for example the analytic Sun/Moon ECEF positions from
1087/// [`crate::astro::bodies::sun_moon::sun_moon_ecef`]).
1088pub fn itrs_to_topocentric(
1089    target_itrs_km: [f64; 3],
1090    station: &GeodeticStationKm,
1091) -> Result<(f64, f64, f64), FrameTransformError> {
1092    validate_vec3("target_itrs_km", &target_itrs_km)?;
1093    validate_geodetic_degrees_km(
1094        station.latitude_deg,
1095        station.longitude_deg,
1096        station.altitude_km,
1097    )?;
1098    validate_tuple3(
1099        "topocentric",
1100        itrs_to_topocentric_unchecked(target_itrs_km, station),
1101    )
1102}
1103
1104fn itrs_to_topocentric_unchecked(target_itrs_km: [f64; 3], station: &GeodeticStationKm) -> Vec3 {
1105    let [target_x, target_y, target_z] = target_itrs_km;
1106    let (stn_x, stn_y, stn_z) = geodetic_to_itrs_unchecked(
1107        station.latitude_deg,
1108        station.longitude_deg,
1109        station.altitude_km,
1110    );
1111
1112    let dx = target_x - stn_x;
1113    let dy = target_y - stn_y;
1114    let dz = target_z - stn_z;
1115
1116    let enu_mat = ecef_to_enu_matrix(station.latitude_deg, station.longitude_deg);
1117    let enu = mat3_vec3_mul_unchecked(&enu_mat, &[dx, dy, dz]);
1118    let east = enu[0];
1119    let north = enu[1];
1120    let up = enu[2];
1121
1122    // Range
1123    let range = (east * east + north * north + up * up).sqrt();
1124
1125    // Elevation
1126    let elevation = libm::asin(up / range).to_degrees();
1127
1128    // Azimuth (measured clockwise from north). At (and arbitrarily near) the
1129    // station zenith the east and north components are pure rounding residuals,
1130    // so azimuth is degenerate and defined to be 0.0 (RTKLIB satazel semantics).
1131    let horiz_sq = east * east + north * north;
1132    let mut azimuth = if horiz_sq < AZIMUTH_ZENITH_EPS * range * range {
1133        0.0
1134    } else {
1135        libm::atan2(east, north).to_degrees()
1136    };
1137    if azimuth < 0.0 {
1138        azimuth += 360.0;
1139    }
1140
1141    (azimuth, elevation, range)
1142}
1143
1144/// Skyfield-compatible topocentric: stays in GCRS AU the entire time.
1145///
1146/// Replicates Skyfield's altaz computation:
1147/// 1. R_lat = rot_y(lat)[::-1]  (row-reversed Y rotation)
1148/// 2. R_latlon = mxm(R_lat, rot_z(-lon))
1149/// 3. R_full = mxm(R_latlon, itrs_rotation)
1150/// 4. station_gcrs_au = transpose(itrs_rotation) * station_itrs_au
1151/// 5. diff_au = sat_gcrs_au - station_gcrs_au
1152/// 6. enu_au = mxv(R_full, diff_au)
1153/// 7. to_spherical(enu_au) -> (range_au, elevation_rad, azimuth_rad)
1154fn gcrs_to_topocentric_skyfield(
1155    sat_x: f64,
1156    sat_y: f64,
1157    sat_z: f64,
1158    station_lat_deg: f64,
1159    station_lon_deg: f64,
1160    station_alt_km: f64,
1161    ts: &TimeScales,
1162) -> (f64, f64, f64) {
1163    let lat_rad = station_lat_deg * TAU / 360.0;
1164    let lon_rad = station_lon_deg * TAU / 360.0;
1165
1166    // Build R_lat = rot_y(lat)[::-1]  (rows reversed)
1167    let cy = libm::cos(lat_rad);
1168    let sy = libm::sin(lat_rad);
1169    // rot_y(lat) = [[cy, 0, sy], [0, 1, 0], [-sy, 0, cy]]
1170    // [::-1] reverses rows: [[-sy, 0, cy], [0, 1, 0], [cy, 0, sy]]
1171    let r_lat: Mat3 = [[-sy, 0.0, cy], [0.0, 1.0, 0.0], [cy, 0.0, sy]];
1172
1173    // R_latlon = mxm(R_lat, rot_z(-lon))
1174    let rz_neg_lon = build_rot_z(-lon_rad);
1175    let r_latlon = inline_rxr(&r_lat, &rz_neg_lon);
1176
1177    // R_full = mxm(R_latlon, itrs_rotation)
1178    let r_itrs = gcrs_to_itrs_matrix_unchecked(ts);
1179    let r_full = inline_rxr(&r_latlon, &r_itrs);
1180
1181    // Station ITRS position directly in AU, matching Skyfield's Geoid.latlon
1182    // which computes in AU from the start (not km then / AU_KM).
1183    let stn_itrs_au = geodetic_to_itrs_au(station_lat_deg, station_lon_deg, station_alt_km);
1184
1185    // Station GCRS AU = transpose(R_itrs) * station_itrs_au
1186    let r_itrs_t = inline_tr(&r_itrs);
1187    let stn_gcrs_au = mat3_vec3_mul_unchecked(&r_itrs_t, &stn_itrs_au);
1188
1189    // Satellite GCRS in AU
1190    let sat_au = [sat_x / AU_KM, sat_y / AU_KM, sat_z / AU_KM];
1191
1192    // Difference vector in GCRS AU
1193    let diff_au = [
1194        sat_au[0] - stn_gcrs_au[0],
1195        sat_au[1] - stn_gcrs_au[1],
1196        sat_au[2] - stn_gcrs_au[2],
1197    ];
1198
1199    // Rotate to ENU-ish frame: mxv(R_full, diff_au)
1200    let enu_au = mat3_vec3_mul_unchecked(&r_full, &diff_au);
1201
1202    // to_spherical: r, theta (elevation), phi (azimuth)
1203    let ex = enu_au[0];
1204    let ey = enu_au[1];
1205    let ez = enu_au[2];
1206
1207    let r_au = (ex * ex + ey * ey + ez * ez).sqrt();
1208    let elevation_rad = libm::atan2(ez, (ex * ex + ey * ey).sqrt());
1209    let mut azimuth_rad = libm::atan2(ey, ex) % TAU;
1210    if azimuth_rad < 0.0 {
1211        azimuth_rad += TAU;
1212    }
1213
1214    let range_km = r_au * AU_KM;
1215    let elevation_deg = elevation_rad * 360.0 / TAU;
1216    let azimuth_deg = azimuth_rad * 360.0 / TAU;
1217
1218    (azimuth_deg, elevation_deg, range_km)
1219}
1220
1221#[cfg(test)]
1222mod tests {
1223    use super::*;
1224    use crate::astro::time::scales::TimeScales;
1225
1226    fn assert_mat3_bits_eq(actual: &Mat3, expected: &Mat3) {
1227        for i in 0..3 {
1228            for j in 0..3 {
1229                assert_eq!(
1230                    actual[i][j].to_bits(),
1231                    expected[i][j].to_bits(),
1232                    "matrix[{i}][{j}]: {} vs {}",
1233                    actual[i][j],
1234                    expected[i][j]
1235                );
1236            }
1237        }
1238    }
1239
1240    fn assert_vec3_bits_eq(actual: [f64; 3], expected: [f64; 3]) {
1241        for i in 0..3 {
1242            assert_eq!(
1243                actual[i].to_bits(),
1244                expected[i].to_bits(),
1245                "vector[{i}]: {} vs {}",
1246                actual[i],
1247                expected[i]
1248            );
1249        }
1250    }
1251
1252    #[test]
1253    fn itrs_to_gcrs_inverts_gcrs_to_itrs() {
1254        // On a real epoch and a real-magnitude ECI vector, ITRS->GCRS recovers
1255        // the GCRS->ITRS input bit-for-bit on the plain (non-Skyfield) km path:
1256        // the two directions must be exact transposes, not just approximately so.
1257        let ts = TimeScales::from_utc(2020, 6, 24, 12, 34, 56.0).expect("valid UTC instant");
1258        let (x, y, z) = (4321.0_f64, -5678.0, 3210.0);
1259
1260        let (ix, iy, iz) =
1261            gcrs_to_itrs_compute(x, y, z, &ts, false).expect("valid frame transform");
1262        // The rotation actually moved the vector (it is not a no-op).
1263        assert!(((ix - x).abs() + (iy - y).abs() + (iz - z).abs()) > 100.0);
1264
1265        let (bx, by, bz) = itrs_to_gcrs_compute(ix, iy, iz, &ts).expect("valid frame transform");
1266        assert!((bx - x).abs() < 1e-9, "x {bx} vs {x}");
1267        assert!((by - y).abs() < 1e-9, "y {by} vs {y}");
1268        assert!((bz - z).abs() < 1e-9, "z {bz} vs {z}");
1269
1270        // Magnitude is preserved by the rotation.
1271        let n0 = (x * x + y * y + z * z).sqrt();
1272        let n1 = (ix * ix + iy * iy + iz * iz).sqrt();
1273        assert!((n0 - n1).abs() < 1e-9);
1274    }
1275
1276    #[test]
1277    fn polar_motion_matrix_matches_documented_convention() {
1278        let pole = PolarMotion::from_arcseconds(0.25, -0.35).expect("valid polar motion");
1279        let cx = libm::cos(pole.xp_rad);
1280        let sx = libm::sin(pole.xp_rad);
1281        let cy = libm::cos(pole.yp_rad);
1282        let sy = libm::sin(pole.yp_rad);
1283
1284        let expected = [
1285            [cx, sx * sy, sx * cy],
1286            [0.0, cy, -sy],
1287            [-sx, cx * sy, cx * cy],
1288        ];
1289        let got = polar_motion_matrix(pole).expect("valid polar motion matrix");
1290        assert_mat3_bits_eq(&got, &expected);
1291
1292        let small_angle = [
1293            [1.0, 0.0, pole.xp_rad],
1294            [0.0, 1.0, -pole.yp_rad],
1295            [-pole.xp_rad, pole.yp_rad, 1.0],
1296        ];
1297        for i in 0..3 {
1298            for j in 0..3 {
1299                assert!(
1300                    (got[i][j] - small_angle[i][j]).abs() < 1.0e-11,
1301                    "matrix[{i}][{j}] {} vs small-angle {}",
1302                    got[i][j],
1303                    small_angle[i][j]
1304                );
1305            }
1306        }
1307    }
1308
1309    #[test]
1310    fn gcrs_to_itrs_with_polar_motion_premultiplies_legacy_rotation() {
1311        let ts = TimeScales::from_utc(2020, 6, 24, 12, 34, 56.0).expect("valid UTC instant");
1312        let pole = PolarMotion::from_arcseconds(0.18, -0.24).expect("valid polar motion");
1313        let legacy = gcrs_to_itrs_matrix(&ts).expect("valid frame transform");
1314        let expected = inline_rxr(
1315            &polar_motion_matrix(pole).expect("valid polar motion matrix"),
1316            &legacy,
1317        );
1318        let got = gcrs_to_itrs_matrix_with_polar_motion(&ts, pole).expect("valid frame transform");
1319
1320        assert_mat3_bits_eq(&got, &expected);
1321
1322        let pos = [4321.0_f64, -5678.0, 3210.0];
1323        let actual_vec =
1324            gcrs_to_itrs_compute_with_polar_motion(pos[0], pos[1], pos[2], &ts, false, pole)
1325                .expect("valid frame transform");
1326        let expected_vec = mat3_vec3_mul(&expected, &pos).expect("finite matrix-vector product");
1327        assert_vec3_bits_eq([actual_vec.0, actual_vec.1, actual_vec.2], expected_vec);
1328
1329        let legacy_vec =
1330            gcrs_to_itrs_compute(pos[0], pos[1], pos[2], &ts, false).expect("valid transform");
1331        let delta = (actual_vec.0 - legacy_vec.0).abs()
1332            + (actual_vec.1 - legacy_vec.1).abs()
1333            + (actual_vec.2 - legacy_vec.2).abs();
1334        assert!(
1335            delta > 1.0e-4,
1336            "nonzero polar motion should move the vector"
1337        );
1338
1339        let inverse =
1340            itrs_to_gcrs_matrix_with_polar_motion(&ts, pole).expect("valid frame transform");
1341        assert_mat3_bits_eq(&inverse, &inline_tr(&got));
1342    }
1343
1344    #[test]
1345    fn zero_polar_motion_matches_legacy_transform_bits() {
1346        let ts = TimeScales::from_utc(2020, 6, 24, 12, 34, 56.0).expect("valid UTC instant");
1347        let legacy = gcrs_to_itrs_matrix(&ts).expect("valid frame transform");
1348        let zero = gcrs_to_itrs_matrix_with_polar_motion(&ts, PolarMotion::ZERO)
1349            .expect("valid frame transform");
1350        assert_mat3_bits_eq(&zero, &legacy);
1351
1352        let mean_legacy = mean_of_date_to_itrs_matrix(&ts).expect("valid frame transform");
1353        let mean_zero = mean_of_date_to_itrs_matrix_with_polar_motion(&ts, PolarMotion::ZERO)
1354            .expect("valid frame transform");
1355        assert_mat3_bits_eq(&mean_zero, &mean_legacy);
1356
1357        let pos = [4321.0_f64, -5678.0, 3210.0];
1358        for skyfield_compat in [false, true] {
1359            let legacy_vec = gcrs_to_itrs_compute(pos[0], pos[1], pos[2], &ts, skyfield_compat)
1360                .expect("valid frame transform");
1361            let zero_vec = gcrs_to_itrs_compute_with_polar_motion(
1362                pos[0],
1363                pos[1],
1364                pos[2],
1365                &ts,
1366                skyfield_compat,
1367                PolarMotion::ZERO,
1368            )
1369            .expect("valid frame transform");
1370            assert_vec3_bits_eq(
1371                [zero_vec.0, zero_vec.1, zero_vec.2],
1372                [legacy_vec.0, legacy_vec.1, legacy_vec.2],
1373            );
1374        }
1375
1376        let legacy_back =
1377            itrs_to_gcrs_compute(pos[0], pos[1], pos[2], &ts).expect("valid frame transform");
1378        let zero_back =
1379            itrs_to_gcrs_compute_with_polar_motion(pos[0], pos[1], pos[2], &ts, PolarMotion::ZERO)
1380                .expect("valid frame transform");
1381        assert_vec3_bits_eq(
1382            [zero_back.0, zero_back.1, zero_back.2],
1383            [legacy_back.0, legacy_back.1, legacy_back.2],
1384        );
1385    }
1386
1387    #[test]
1388    fn gcrs_to_teme_inverts_teme_to_gcrs() {
1389        let ts = TimeScales::from_utc(2020, 6, 24, 12, 34, 56.0).expect("valid UTC instant");
1390        let teme = TemeStateKm {
1391            position_km: [6524.834, 6862.875, 6448.296],
1392            velocity_km_s: [4.901327, 5.533756, -1.976341],
1393        };
1394
1395        for skyfield_compat in [false, true] {
1396            let (gcrs_position, gcrs_velocity) =
1397                teme_to_gcrs_compute(&teme, &ts, skyfield_compat).expect("valid transform");
1398            let gcrs = TemeStateKm {
1399                position_km: [gcrs_position.0, gcrs_position.1, gcrs_position.2],
1400                velocity_km_s: [gcrs_velocity.0, gcrs_velocity.1, gcrs_velocity.2],
1401            };
1402            let (round_position, round_velocity) =
1403                gcrs_to_teme_compute(&gcrs, &ts, skyfield_compat).expect("valid inverse");
1404
1405            let pos = [round_position.0, round_position.1, round_position.2];
1406            let vel = [round_velocity.0, round_velocity.1, round_velocity.2];
1407            for axis in 0..3 {
1408                assert!(
1409                    (pos[axis] - teme.position_km[axis]).abs() <= 1.0e-9,
1410                    "position axis {axis} mode {skyfield_compat}"
1411                );
1412                assert!(
1413                    (vel[axis] - teme.velocity_km_s[axis]).abs() <= 1.0e-12,
1414                    "velocity axis {axis} mode {skyfield_compat}"
1415                );
1416            }
1417        }
1418    }
1419
1420    #[test]
1421    fn frame_transforms_reject_nonfinite_time() {
1422        let mut ts = TimeScales::from_utc(2020, 6, 24, 12, 34, 56.0).expect("valid UTC instant");
1423        ts.jd_tt = f64::NAN;
1424
1425        assert!(greenwich_mean_sidereal_time_radians(&ts).is_err());
1426        assert!(gcrs_to_itrs_matrix(&ts).is_err());
1427        assert!(itrs_to_gcrs_compute(1.0, 2.0, 3.0, &ts).is_err());
1428    }
1429
1430    #[test]
1431    fn frame_transforms_reject_nonfinite_pole_coordinates() {
1432        let ts = TimeScales::from_utc(2020, 6, 24, 12, 34, 56.0).expect("valid UTC instant");
1433        assert!(PolarMotion::from_radians(f64::NAN, 0.0).is_err());
1434        assert!(PolarMotion::from_arcseconds(0.0, f64::INFINITY).is_err());
1435
1436        let pole = PolarMotion {
1437            xp_rad: f64::NAN,
1438            yp_rad: 0.0,
1439        };
1440        assert!(polar_motion_matrix(pole).is_err());
1441        assert!(gcrs_to_itrs_matrix_with_polar_motion(&ts, pole).is_err());
1442    }
1443
1444    #[test]
1445    fn frame_transforms_reject_nonfinite_vectors() {
1446        let ts = TimeScales::from_utc(2020, 6, 24, 12, 34, 56.0).expect("valid UTC instant");
1447        let bad_state = TemeStateKm {
1448            position_km: [1.0, f64::NAN, 3.0],
1449            velocity_km_s: [0.1, 0.2, 0.3],
1450        };
1451        assert!(teme_to_gcrs_compute(&bad_state, &ts, false).is_err());
1452        assert!(gcrs_to_itrs_compute(1.0, f64::INFINITY, 3.0, &ts, false).is_err());
1453        assert!(itrs_to_gcrs_compute(1.0, 2.0, f64::NEG_INFINITY, &ts).is_err());
1454    }
1455
1456    #[test]
1457    fn validated_frame_transform_preserves_valid_bits() {
1458        let ts = TimeScales::from_utc(2020, 6, 24, 12, 34, 56.0).expect("valid UTC instant");
1459        let pos = [4321.0_f64, -5678.0, 3210.0];
1460        let expected = gcrs_to_itrs_compute_unchecked(pos[0], pos[1], pos[2], &ts, true);
1461        let got =
1462            gcrs_to_itrs_compute(pos[0], pos[1], pos[2], &ts, true).expect("valid frame transform");
1463        assert_vec3_bits_eq([got.0, got.1, got.2], [expected.0, expected.1, expected.2]);
1464    }
1465
1466    #[test]
1467    fn geodetic_transforms_reject_invalid_coordinates() {
1468        assert!(itrs_to_geodetic_compute(f64::NAN, 0.0, 0.0).is_err());
1469        assert!(geodetic_from_ecef_proj(0.0, f64::INFINITY, 0.0).is_err());
1470        assert!(geodetic_to_itrs(90.000_001, 0.0, 0.0).is_err());
1471        assert!(geodetic_to_itrs(0.0, -180.000_001, 0.0).is_err());
1472        assert!(geodetic_to_itrs(0.0, 0.0, f64::NAN).is_err());
1473    }
1474
1475    #[test]
1476    fn topocentric_transform_rejects_invalid_coordinates() {
1477        let ts = TimeScales::from_utc(2020, 6, 24, 12, 34, 56.0).expect("valid UTC instant");
1478        let station = GeodeticStationKm {
1479            latitude_deg: f64::NAN,
1480            longitude_deg: 0.0,
1481            altitude_km: 0.0,
1482        };
1483        assert!(gcrs_to_topocentric_compute([7000.0, 0.0, 0.0], &station, &ts, false).is_err());
1484
1485        let station = GeodeticStationKm {
1486            latitude_deg: 0.0,
1487            longitude_deg: 181.0,
1488            altitude_km: 0.0,
1489        };
1490        assert!(gcrs_to_topocentric_compute([7000.0, 0.0, 0.0], &station, &ts, false).is_err());
1491
1492        let station = GeodeticStationKm {
1493            latitude_deg: 0.0,
1494            longitude_deg: 0.0,
1495            altitude_km: 0.0,
1496        };
1497        assert!(
1498            gcrs_to_topocentric_compute([7000.0, f64::NAN, 0.0], &station, &ts, false).is_err()
1499        );
1500    }
1501
1502    #[test]
1503    fn topocentric_azimuth_is_zero_at_station_zenith() {
1504        let ts = TimeScales::from_utc(2020, 6, 24, 12, 34, 56.0).expect("valid UTC instant");
1505        // Equatorial station: geodetic up coincides with the geocentric radial,
1506        // so a satellite displaced radially outward is exactly overhead.
1507        let station = GeodeticStationKm {
1508            latitude_deg: 0.0,
1509            longitude_deg: 0.0,
1510            altitude_km: 0.0,
1511        };
1512        let (sx, sy, sz) = geodetic_to_itrs_unchecked(0.0, 0.0, 0.0);
1513        // Satellite ITRS position 20,000 km straight up (+X at lat0/lon0).
1514        let sat_itrs = [sx + 20_000.0, sy, sz];
1515        // Express it in GCRS so the standard path rotates it back to `sat_itrs`.
1516        let r_itrs = gcrs_to_itrs_matrix_unchecked(&ts);
1517        let r_itrs_t = inline_tr(&r_itrs);
1518        let sat_gcrs = mat3_vec3_mul_unchecked(&r_itrs_t, &sat_itrs);
1519
1520        let (azimuth_deg, elevation_deg, _range_km) =
1521            gcrs_to_topocentric_compute_unchecked(sat_gcrs, &station, &ts, false);
1522        assert_eq!(azimuth_deg, 0.0);
1523        assert!(azimuth_deg.is_finite());
1524        assert!((elevation_deg - 90.0).abs() < 1e-6);
1525    }
1526
1527    #[test]
1528    fn validated_geodetic_transform_preserves_valid_bits() {
1529        let (lat, lon, alt) = (51.4779, -0.0015, 0.046);
1530        let expected = geodetic_to_itrs_unchecked(lat, lon, alt);
1531        let got = geodetic_to_itrs(lat, lon, alt).expect("valid geodetic coordinates");
1532        assert_eq!(got.0.to_bits(), expected.0.to_bits());
1533        assert_eq!(got.1.to_bits(), expected.1.to_bits());
1534        assert_eq!(got.2.to_bits(), expected.2.to_bits());
1535
1536        let expected = itrs_to_geodetic_compute_unchecked(got.0, got.1, got.2);
1537        let roundtrip =
1538            itrs_to_geodetic_compute(got.0, got.1, got.2).expect("valid ITRS coordinates");
1539        assert_eq!(roundtrip.0.to_bits(), expected.0.to_bits());
1540        assert_eq!(roundtrip.1.to_bits(), expected.1.to_bits());
1541        assert_eq!(roundtrip.2.to_bits(), expected.2.to_bits());
1542    }
1543
1544    #[test]
1545    fn sidereal_time_wrappers_are_in_range_and_consistent() {
1546        let ts = TimeScales::from_utc(2020, 6, 24, 12, 34, 56.0).expect("valid UTC instant");
1547        let gmst = greenwich_mean_sidereal_time_radians(&ts).expect("valid sidereal time");
1548        let gast = greenwich_apparent_sidereal_time_radians(&ts).expect("valid sidereal time");
1549
1550        // Both land in [0, 2pi).
1551        assert!((0.0..TAU).contains(&gmst), "gmst {gmst}");
1552        assert!((0.0..TAU).contains(&gast), "gast {gast}");
1553
1554        // The equation of the equinoxes is a small (sub-arcminute) offset, so the
1555        // apparent and mean sidereal times stay close (handle the seam at 2pi).
1556        let diff = (gast - gmst).rem_euclid(TAU);
1557        let eq_eq = diff.min(TAU - diff);
1558        assert!(eq_eq < 1.0e-3, "equation of equinoxes too large: {eq_eq}");
1559
1560        // The mean wrapper matches the Skyfield-parity IAU-1982 sidereal time.
1561        let gmst_hours = sidereal_time_hours(ts.jd_whole, ts.ut1_fraction, ts.tdb_fraction);
1562        assert_eq!(gmst, gmst_hours / 24.0 * TAU);
1563    }
1564
1565    #[test]
1566    fn gmst_from_j2000_seconds_matches_gmst1982_polynomial() {
1567        // The seconds-based accessor reproduces the GMST-1982 polynomial the TEME
1568        // sidereal rotation uses, not the public Skyfield-parity GMST. Tolerance
1569        // allows only the roundoff from reducing the UT1 split to one f64.
1570        const GMST_TOL_RAD: f64 = 1.0e-11;
1571        let ts = TimeScales::from_utc(2020, 6, 24, 12, 34, 56.25).expect("valid UTC instant");
1572        let sec = (ts.jd_ut1 - J2000_JD) * SECONDS_PER_DAY;
1573        let from_seconds = greenwich_mean_sidereal_time_radians_from_j2000_seconds(sec)
1574            .expect("valid sidereal time");
1575        let mut expected = compute_theta_gmst1982(ts.jd_whole, ts.ut1_fraction) % TAU;
1576        if expected < 0.0 {
1577            expected += TAU;
1578        }
1579
1580        assert!((from_seconds - expected).abs() <= GMST_TOL_RAD);
1581    }
1582}