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// ---------------------------------------------------------------------------
466// GCRS -> ITRS (Earth-fixed / ECEF)
467// ---------------------------------------------------------------------------
468
469/// GCRS to true equator and equinox of date rotation.
470///
471/// This is the Earth-rotation-free `N * P * B` product already used inside the
472/// GCRS to ITRS construction: nutation, precession, and frame bias.
473pub fn gcrs_to_true_of_date_matrix(ts: &TimeScales) -> Result<Mat3, FrameTransformError> {
474    validate_time_scales(ts)?;
475    let (matrix, _dpsi) = gcrs_to_true_of_date_matrix_parts_unchecked(ts);
476    validate_mat3("gcrs_to_true_of_date_matrix", matrix)
477}
478
479fn gcrs_to_true_of_date_matrix_parts_unchecked(ts: &TimeScales) -> (Mat3, f64) {
480    let (dpsi, deps) = skyfield_iau2000a_radians_unchecked(ts.jd_tt);
481    let mean_ob = skyfield_mean_obliquity_radians_unchecked(ts.jd_tdb);
482    let true_ob = mean_ob + deps;
483
484    let n = build_skyfield_nutation_matrix_unchecked(mean_ob, true_ob, dpsi);
485    let p = compute_skyfield_precession_matrix_unchecked(ts.jd_tdb);
486    let b = build_icrs_to_j2000();
487
488    (inline_mxmxm(&n, &p, &b), dpsi)
489}
490
491/// Build the historical GCRS->ITRS rotation matrix for a given time.
492///
493/// This combines precession, nutation, and Earth rotation with zero polar
494/// motion, preserving the original bit-exact path. Use
495/// [`gcrs_to_itrs_matrix_with_polar_motion`] when `xp`/`yp` pole coordinates are
496/// available.
497pub fn gcrs_to_itrs_matrix(ts: &TimeScales) -> Result<Mat3, FrameTransformError> {
498    validate_time_scales(ts)?;
499    validate_mat3("gcrs_to_itrs_matrix", gcrs_to_itrs_matrix_unchecked(ts))
500}
501
502fn gcrs_to_itrs_matrix_unchecked(ts: &TimeScales) -> Mat3 {
503    let (m, dpsi) = gcrs_to_true_of_date_matrix_parts_unchecked(ts);
504    let gast = gast_radians(ts, dpsi);
505
506    // GAST rotation takes us from true-equator-equinox to ITRS
507    let r_gast = build_rot_z(-gast);
508
509    // GCRS->ITRS = R_z(-GAST) * (N * P * B)
510    inline_rxr(&r_gast, &m)
511}
512
513/// Build the GCRS->ITRS rotation matrix with explicit polar motion.
514///
515/// The embedded Earth-orientation table supplies UT1-UTC but not `xp`/`yp`, so
516/// callers that do not have pole coordinates should pass [`PolarMotion::ZERO`]
517/// or use [`gcrs_to_itrs_matrix`].
518pub fn gcrs_to_itrs_matrix_with_polar_motion(
519    ts: &TimeScales,
520    pole: PolarMotion,
521) -> Result<Mat3, FrameTransformError> {
522    validate_time_scales(ts)?;
523    validate_polar_motion(pole)?;
524    validate_mat3(
525        "gcrs_to_itrs_matrix",
526        gcrs_to_itrs_matrix_with_polar_motion_unchecked(ts, pole),
527    )
528}
529
530fn gcrs_to_itrs_matrix_with_polar_motion_unchecked(ts: &TimeScales, pole: PolarMotion) -> Mat3 {
531    apply_polar_motion_to_itrs_matrix(gcrs_to_itrs_matrix_unchecked(ts), pole)
532}
533
534/// Rotation from the **mean equator and equinox of date** to ITRS, i.e.
535/// `R_z(-GAST) * N` (nutation + Earth rotation, *without* precession or frame
536/// bias).
537///
538/// This is [`gcrs_to_itrs_matrix`] with the precession (`P`) and frame-bias
539/// (`B`) factors removed. Use it for vectors that are already referred to the
540/// mean equator/equinox of date (for example the low-precision analytic Sun/Moon
541/// series in [`crate::astro::bodies::sun_moon`], whose mean longitude and obliquity are
542/// of-date), so precession is not applied a second time. It mirrors the
543/// `eci2ecef` (GMST/GAST + nutation) rotation those series are designed to be
544/// consumed with, but uses the crate's IAU 2000A nutation and GAST.
545pub fn mean_of_date_to_itrs_matrix(ts: &TimeScales) -> Result<Mat3, FrameTransformError> {
546    validate_time_scales(ts)?;
547    validate_mat3(
548        "mean_of_date_to_itrs_matrix",
549        mean_of_date_to_itrs_matrix_unchecked(ts),
550    )
551}
552
553fn mean_of_date_to_itrs_matrix_unchecked(ts: &TimeScales) -> Mat3 {
554    let (dpsi, deps) = skyfield_iau2000a_radians_unchecked(ts.jd_tt);
555    let mean_ob = skyfield_mean_obliquity_radians_unchecked(ts.jd_tdb);
556    let true_ob = mean_ob + deps;
557
558    let n = build_skyfield_nutation_matrix_unchecked(mean_ob, true_ob, dpsi);
559    let gast = gast_radians(ts, dpsi);
560    let r_gast = build_rot_z(-gast);
561
562    // mean-of-date -> ITRS = R_z(-GAST) * N
563    inline_rxr(&r_gast, &n)
564}
565
566/// Mean-of-date to ITRS rotation with explicit polar motion.
567pub fn mean_of_date_to_itrs_matrix_with_polar_motion(
568    ts: &TimeScales,
569    pole: PolarMotion,
570) -> Result<Mat3, FrameTransformError> {
571    validate_time_scales(ts)?;
572    validate_polar_motion(pole)?;
573    validate_mat3(
574        "mean_of_date_to_itrs_matrix",
575        mean_of_date_to_itrs_matrix_with_polar_motion_unchecked(ts, pole),
576    )
577}
578
579fn mean_of_date_to_itrs_matrix_with_polar_motion_unchecked(
580    ts: &TimeScales,
581    pole: PolarMotion,
582) -> Mat3 {
583    apply_polar_motion_to_itrs_matrix(mean_of_date_to_itrs_matrix_unchecked(ts), pole)
584}
585
586/// Core GCRS->ITRS transform. Returns (x, y, z) in km.
587pub fn gcrs_to_itrs_compute(
588    x: f64,
589    y: f64,
590    z: f64,
591    ts: &TimeScales,
592    skyfield_compat: bool,
593) -> Result<(f64, f64, f64), FrameTransformError> {
594    validate_vec3("gcrs_position_km", &[x, y, z])?;
595    validate_time_scales(ts)?;
596    validate_tuple3(
597        "itrs_position_km",
598        gcrs_to_itrs_compute_unchecked(x, y, z, ts, skyfield_compat),
599    )
600}
601
602fn gcrs_to_itrs_compute_unchecked(
603    x: f64,
604    y: f64,
605    z: f64,
606    ts: &TimeScales,
607    skyfield_compat: bool,
608) -> (f64, f64, f64) {
609    let mat = gcrs_to_itrs_matrix_unchecked(ts);
610
611    if skyfield_compat {
612        // Skyfield: mxv(R, pos_au) in AU, then convert to km.
613        // For ITRS, scalar (non-FMA) multiply matches einsum's rounding.
614        // (Unlike TEME->GCRS where FMA is needed -- the difference is due to
615        // the specific matrix/vector values and how rounding interacts.)
616        let pos_au = [x / AU_KM, y / AU_KM, z / AU_KM];
617        let r = mat3_vec3_mul_unchecked(&mat, &pos_au);
618        (r[0] * AU_KM, r[1] * AU_KM, r[2] * AU_KM)
619    } else {
620        let pos = [x, y, z];
621        let r = mat3_vec3_mul_unchecked(&mat, &pos);
622        (r[0], r[1], r[2])
623    }
624}
625
626/// Core GCRS->ITRS transform with explicit polar motion.
627pub fn gcrs_to_itrs_compute_with_polar_motion(
628    x: f64,
629    y: f64,
630    z: f64,
631    ts: &TimeScales,
632    skyfield_compat: bool,
633    pole: PolarMotion,
634) -> Result<(f64, f64, f64), FrameTransformError> {
635    validate_vec3("gcrs_position_km", &[x, y, z])?;
636    validate_time_scales(ts)?;
637    validate_polar_motion(pole)?;
638    validate_tuple3(
639        "itrs_position_km",
640        gcrs_to_itrs_compute_with_polar_motion_unchecked(x, y, z, ts, skyfield_compat, pole),
641    )
642}
643
644fn gcrs_to_itrs_compute_with_polar_motion_unchecked(
645    x: f64,
646    y: f64,
647    z: f64,
648    ts: &TimeScales,
649    skyfield_compat: bool,
650    pole: PolarMotion,
651) -> (f64, f64, f64) {
652    let mat = gcrs_to_itrs_matrix_with_polar_motion_unchecked(ts, pole);
653
654    if skyfield_compat {
655        let pos_au = [x / AU_KM, y / AU_KM, z / AU_KM];
656        let r = mat3_vec3_mul_unchecked(&mat, &pos_au);
657        (r[0] * AU_KM, r[1] * AU_KM, r[2] * AU_KM)
658    } else {
659        let pos = [x, y, z];
660        let r = mat3_vec3_mul_unchecked(&mat, &pos);
661        (r[0], r[1], r[2])
662    }
663}
664
665// ---------------------------------------------------------------------------
666// ITRS -> GCRS (Earth-fixed / ECEF back to inertial)
667// ---------------------------------------------------------------------------
668
669/// Build the ITRS->GCRS rotation matrix for a given time.
670///
671/// This is the transpose of [`gcrs_to_itrs_matrix`]: the same precession,
672/// nutation, frame-bias, and Earth-rotation pipeline, taken the other way.
673pub fn itrs_to_gcrs_matrix(ts: &TimeScales) -> Result<Mat3, FrameTransformError> {
674    validate_time_scales(ts)?;
675    validate_mat3("itrs_to_gcrs_matrix", itrs_to_gcrs_matrix_unchecked(ts))
676}
677
678fn itrs_to_gcrs_matrix_unchecked(ts: &TimeScales) -> Mat3 {
679    inline_tr(&gcrs_to_itrs_matrix_unchecked(ts))
680}
681
682/// Build the ITRS->GCRS rotation matrix with explicit polar motion.
683pub fn itrs_to_gcrs_matrix_with_polar_motion(
684    ts: &TimeScales,
685    pole: PolarMotion,
686) -> Result<Mat3, FrameTransformError> {
687    validate_time_scales(ts)?;
688    validate_polar_motion(pole)?;
689    validate_mat3(
690        "itrs_to_gcrs_matrix",
691        itrs_to_gcrs_matrix_with_polar_motion_unchecked(ts, pole),
692    )
693}
694
695fn itrs_to_gcrs_matrix_with_polar_motion_unchecked(ts: &TimeScales, pole: PolarMotion) -> Mat3 {
696    inline_tr(&gcrs_to_itrs_matrix_with_polar_motion_unchecked(ts, pole))
697}
698
699/// Core ITRS->GCRS transform. Returns (x, y, z) in km.
700///
701/// Uses the plain (non-FMA, no AU round-trip) km path. The Skyfield AU-scaled
702/// `mul_add` path is reserved for the GCRS->ITRS / TEME->GCRS directions that
703/// carry the 0-ULP parity contract; this reverse direction is an ordinary
704/// matrix-vector product.
705pub fn itrs_to_gcrs_compute(
706    x: f64,
707    y: f64,
708    z: f64,
709    ts: &TimeScales,
710) -> Result<(f64, f64, f64), FrameTransformError> {
711    validate_vec3("itrs_position_km", &[x, y, z])?;
712    validate_time_scales(ts)?;
713    validate_tuple3(
714        "gcrs_position_km",
715        itrs_to_gcrs_compute_unchecked(x, y, z, ts),
716    )
717}
718
719fn itrs_to_gcrs_compute_unchecked(x: f64, y: f64, z: f64, ts: &TimeScales) -> (f64, f64, f64) {
720    let mat = itrs_to_gcrs_matrix_unchecked(ts);
721    let r = mat3_vec3_mul_unchecked(&mat, &[x, y, z]);
722    (r[0], r[1], r[2])
723}
724
725/// Core ITRS->GCRS transform with explicit polar motion.
726pub fn itrs_to_gcrs_compute_with_polar_motion(
727    x: f64,
728    y: f64,
729    z: f64,
730    ts: &TimeScales,
731    pole: PolarMotion,
732) -> Result<(f64, f64, f64), FrameTransformError> {
733    validate_vec3("itrs_position_km", &[x, y, z])?;
734    validate_time_scales(ts)?;
735    validate_polar_motion(pole)?;
736    validate_tuple3(
737        "gcrs_position_km",
738        itrs_to_gcrs_compute_with_polar_motion_unchecked(x, y, z, ts, pole),
739    )
740}
741
742fn itrs_to_gcrs_compute_with_polar_motion_unchecked(
743    x: f64,
744    y: f64,
745    z: f64,
746    ts: &TimeScales,
747    pole: PolarMotion,
748) -> (f64, f64, f64) {
749    let mat = itrs_to_gcrs_matrix_with_polar_motion_unchecked(ts, pole);
750    let r = mat3_vec3_mul_unchecked(&mat, &[x, y, z]);
751    (r[0], r[1], r[2])
752}
753
754// ---------------------------------------------------------------------------
755// ITRS -> Geodetic (WGS84 lat/lon/alt)
756// ---------------------------------------------------------------------------
757
758/// Convert ECEF/ITRS (km) to geodetic coordinates.
759/// Returns (latitude_deg, longitude_deg, altitude_km).
760///
761/// Replicates Skyfield's exact algorithm (wgs84.subpoint / _compute_latitude)
762/// which works in AU with exactly 3 iterations.
763pub fn itrs_to_geodetic_compute(
764    x: f64,
765    y: f64,
766    z: f64,
767) -> Result<(f64, f64, f64), FrameTransformError> {
768    validate_vec3("itrs_position_km", &[x, y, z])?;
769    validate_tuple3("geodetic", itrs_to_geodetic_compute_unchecked(x, y, z))
770}
771
772fn itrs_to_geodetic_compute_unchecked(x: f64, y: f64, z: f64) -> (f64, f64, f64) {
773    // Convert to AU to match Skyfield's computation path.
774    let x_au = x / AU_KM;
775    let y_au = y / AU_KM;
776    let z_au = z / AU_KM;
777
778    let a_au = WGS84_A_KM / AU_KM; // Earth equatorial radius in AU
779    let r_xy = (x_au * x_au + y_au * y_au).sqrt();
780
781    // Longitude: match Skyfield's exact normalization:
782    // (arctan2(y, x) - pi) % tau - pi
783    // Python's % always returns positive; Rust's can be negative.
784    let lon_raw = y_au.atan2(x_au);
785    let pi = std::f64::consts::PI;
786    let mut lon_shifted = (lon_raw - pi) % TAU;
787    if lon_shifted < 0.0 {
788        lon_shifted += TAU;
789    }
790    let lon = lon_shifted - pi;
791
792    // Latitude: 3 iterations matching Skyfield exactly
793    let mut lat = z_au.atan2(r_xy);
794    let mut a_c = 0.0_f64;
795    let mut hyp = 0.0_f64;
796
797    for _ in 0..3 {
798        let sin_lat = lat.sin();
799        let e2_sin_lat = WGS84_E2 * sin_lat;
800        a_c = a_au / (1.0 - e2_sin_lat * sin_lat).sqrt();
801        hyp = z_au + a_c * e2_sin_lat;
802        lat = hyp.atan2(r_xy);
803    }
804
805    // Elevation in AU, then convert to km
806    let height_au = (hyp * hyp + r_xy * r_xy).sqrt() - a_c;
807    let alt = height_au * AU_KM;
808
809    // Skyfield's Angle.degrees uses: radians * 360.0 / tau
810    // This gives different rounding than radians * (180.0 / PI).
811    (lat * 360.0 / TAU, lon * 360.0 / TAU, alt)
812}
813
814fn proj_normal_radius_of_curvature(sinphi: f64) -> f64 {
815    if PROJ_WGS84_ES == 0.0 {
816        return PROJ_WGS84_A_M;
817    }
818    PROJ_WGS84_A_M / (1.0 - (PROJ_WGS84_ES * sinphi) * sinphi).sqrt()
819}
820
821fn proj_geocentric_radius(cosphi: f64, sinphi: f64) -> f64 {
822    ((PROJ_WGS84_A_M * PROJ_WGS84_A_M) * cosphi).hypot((PROJ_WGS84_B_M * PROJ_WGS84_B_M) * sinphi)
823        / (PROJ_WGS84_A_M * cosphi).hypot(PROJ_WGS84_B_M * sinphi)
824}
825
826/// Convert ECEF meters to `(longitude_degrees, latitude_degrees, altitude_m)`.
827///
828/// This is an additive PROJ parity variant and does not replace
829/// [`itrs_to_geodetic_compute`]. It matches pyproj 3.6.1 / PROJ 9.3.0 for
830/// `EPSG:4978 -> EPSG:4979` with `always_xy=True`; its Tier 1 bit fixture is
831/// `crates/sidereon-core/tests/fixtures/geodetic/geodetic_proj.json`, generated
832/// by `crates/sidereon-core/fixtures-generators/generate_geodetic_proj.py`.
833pub fn geodetic_from_ecef_proj(x: f64, y: f64, z: f64) -> Result<[f64; 3], FrameTransformError> {
834    validate_vec3("ecef_m", &[x, y, z])?;
835    validate_array3("geodetic_proj", geodetic_from_ecef_proj_unchecked(x, y, z))
836}
837
838fn geodetic_from_ecef_proj_unchecked(x: f64, y: f64, z: f64) -> [f64; 3] {
839    let p = x.hypot(y);
840
841    let y_theta = z * PROJ_WGS84_A_M;
842    let x_theta = p * PROJ_WGS84_B_M;
843    let norm = y_theta.hypot(x_theta);
844    let c = if norm == 0.0 { 1.0 } else { x_theta / norm };
845    let s = if norm == 0.0 { 0.0 } else { y_theta / norm };
846
847    let y_phi = z + ((((PROJ_WGS84_E2S * PROJ_WGS84_B_M) * s) * s) * s);
848    let x_phi = p - ((((PROJ_WGS84_ES * PROJ_WGS84_A_M) * c) * c) * c);
849    let norm_phi = y_phi.hypot(x_phi);
850    let mut cosphi = if norm_phi == 0.0 {
851        1.0
852    } else {
853        x_phi / norm_phi
854    };
855    let mut sinphi = if norm_phi == 0.0 {
856        0.0
857    } else {
858        y_phi / norm_phi
859    };
860
861    let phi = if x_phi <= 0.0 {
862        cosphi = 0.0;
863        if z >= 0.0 {
864            sinphi = 1.0;
865            PROJ_HALF_PI
866        } else {
867            sinphi = -1.0;
868            -PROJ_HALF_PI
869        }
870    } else {
871        (y_phi / x_phi).atan()
872    };
873
874    let lam = y.atan2(x);
875    let alt = if cosphi < 1e-6 {
876        z.abs() - proj_geocentric_radius(cosphi, sinphi)
877    } else {
878        p / cosphi - proj_normal_radius_of_curvature(sinphi)
879    };
880
881    [lam * PROJ_RAD_TO_DEG, phi * PROJ_RAD_TO_DEG, alt]
882}
883
884// ---------------------------------------------------------------------------
885// Topocentric (az/el/range) from ground station to satellite
886// ---------------------------------------------------------------------------
887
888/// Convert geodetic (lat_deg, lon_deg, alt_km) to ECEF/ITRS (km).
889pub fn geodetic_to_itrs(
890    lat_deg: f64,
891    lon_deg: f64,
892    alt_km: f64,
893) -> Result<(f64, f64, f64), FrameTransformError> {
894    validate_geodetic_degrees_km(lat_deg, lon_deg, alt_km)?;
895    validate_tuple3(
896        "itrs_position_km",
897        geodetic_to_itrs_unchecked(lat_deg, lon_deg, alt_km),
898    )
899}
900
901fn geodetic_to_itrs_unchecked(lat_deg: f64, lon_deg: f64, alt_km: f64) -> (f64, f64, f64) {
902    let lat = lat_deg.to_radians();
903    let lon = lon_deg.to_radians();
904
905    let sin_lat = lat.sin();
906    let cos_lat = lat.cos();
907    let sin_lon = lon.sin();
908    let cos_lon = lon.cos();
909
910    let n = WGS84_A_KM / (1.0 - WGS84_E2 * sin_lat * sin_lat).sqrt();
911
912    let x = (n + alt_km) * cos_lat * cos_lon;
913    let y = (n + alt_km) * cos_lat * sin_lon;
914    let z = (n * (1.0 - WGS84_E2) + alt_km) * sin_lat;
915
916    (x, y, z)
917}
918
919/// Compute station ECEF/ITRS position directly in AU.
920/// Matches Skyfield's Geoid.latlon which works in AU from the start,
921/// avoiding the km->AU_KM division that introduces 1 ULP rounding.
922fn geodetic_to_itrs_au(lat_deg: f64, lon_deg: f64, alt_km: f64) -> [f64; 3] {
923    let lat = lat_deg * TAU / 360.0;
924    let lon = lon_deg * TAU / 360.0;
925
926    let sinphi = lat.sin();
927    let cosphi = lat.cos();
928
929    let radius_au = WGS84_A_KM / AU_KM;
930    let elevation_au = alt_km / AU_KM;
931
932    let omf2 = (1.0 - WGS84_F) * (1.0 - WGS84_F);
933    let c = 1.0 / (cosphi * cosphi + sinphi * sinphi * omf2).sqrt();
934    let s = omf2 * c;
935
936    let radius_xy = radius_au * c;
937    let xy = (radius_xy + elevation_au) * cosphi;
938    let x = xy * lon.cos();
939    let y = xy * lon.sin();
940
941    let radius_z = radius_au * s;
942    let z = (radius_z + elevation_au) * sinphi;
943
944    [x, y, z]
945}
946
947/// Build the ECEF->ENU rotation matrix for a given geodetic position.
948fn ecef_to_enu_matrix(lat_deg: f64, lon_deg: f64) -> Mat3 {
949    let lat = lat_deg.to_radians();
950    let lon = lon_deg.to_radians();
951
952    let sin_lat = lat.sin();
953    let cos_lat = lat.cos();
954    let sin_lon = lon.sin();
955    let cos_lon = lon.cos();
956
957    // ENU rotation matrix:
958    // E = [-sin(lon),           cos(lon),          0       ]
959    // N = [-sin(lat)*cos(lon), -sin(lat)*sin(lon), cos(lat)]
960    // U = [ cos(lat)*cos(lon),  cos(lat)*sin(lon), sin(lat)]
961    [
962        [-sin_lon, cos_lon, 0.0],
963        [-sin_lat * cos_lon, -sin_lat * sin_lon, cos_lat],
964        [cos_lat * cos_lon, cos_lat * sin_lon, sin_lat],
965    ]
966}
967
968/// Compute topocentric az/el/range from a ground station to a satellite.
969///
970/// Returns (azimuth_deg, elevation_deg, range_km).
971pub fn gcrs_to_topocentric_compute(
972    sat_gcrs_km: [f64; 3],
973    station: &GeodeticStationKm,
974    ts: &TimeScales,
975    skyfield_compat: bool,
976) -> Result<(f64, f64, f64), FrameTransformError> {
977    validate_vec3("sat_gcrs_km", &sat_gcrs_km)?;
978    validate_geodetic_degrees_km(
979        station.latitude_deg,
980        station.longitude_deg,
981        station.altitude_km,
982    )?;
983    validate_time_scales(ts)?;
984    validate_tuple3(
985        "topocentric",
986        gcrs_to_topocentric_compute_unchecked(sat_gcrs_km, station, ts, skyfield_compat),
987    )
988}
989
990fn gcrs_to_topocentric_compute_unchecked(
991    sat_gcrs_km: [f64; 3],
992    station: &GeodeticStationKm,
993    ts: &TimeScales,
994    skyfield_compat: bool,
995) -> (f64, f64, f64) {
996    let [sat_x, sat_y, sat_z] = sat_gcrs_km;
997    let station_lat_deg = station.latitude_deg;
998    let station_lon_deg = station.longitude_deg;
999    let station_alt_km = station.altitude_km;
1000    if skyfield_compat {
1001        return gcrs_to_topocentric_skyfield(
1002            sat_x,
1003            sat_y,
1004            sat_z,
1005            station_lat_deg,
1006            station_lon_deg,
1007            station_alt_km,
1008            ts,
1009        );
1010    }
1011
1012    // Standard path: GCRS->ITRS->ENU topocentric reduction.
1013    let (sat_itrs_x, sat_itrs_y, sat_itrs_z) =
1014        gcrs_to_itrs_compute_unchecked(sat_x, sat_y, sat_z, ts, false);
1015    itrs_to_topocentric_unchecked([sat_itrs_x, sat_itrs_y, sat_itrs_z], station)
1016}
1017
1018/// Topocentric az/el/range from a station to an Earth-fixed (ITRS/ECEF) target.
1019///
1020/// `target_itrs_km` is the target's geocentric position in the Earth-fixed
1021/// (ITRS/ECEF) frame, km. Returns `(azimuth_deg, elevation_deg, range_km)`,
1022/// azimuth measured clockwise from north on `[0, 360)` and elevation on
1023/// `[-90, 90]`. This is the same station-to-target ENU reduction the satellite
1024/// look-angle path uses, exposed for callers that already hold an Earth-fixed
1025/// target vector (for example the analytic Sun/Moon ECEF positions from
1026/// [`crate::astro::bodies::sun_moon::sun_moon_ecef`]).
1027pub fn itrs_to_topocentric(
1028    target_itrs_km: [f64; 3],
1029    station: &GeodeticStationKm,
1030) -> Result<(f64, f64, f64), FrameTransformError> {
1031    validate_vec3("target_itrs_km", &target_itrs_km)?;
1032    validate_geodetic_degrees_km(
1033        station.latitude_deg,
1034        station.longitude_deg,
1035        station.altitude_km,
1036    )?;
1037    validate_tuple3(
1038        "topocentric",
1039        itrs_to_topocentric_unchecked(target_itrs_km, station),
1040    )
1041}
1042
1043fn itrs_to_topocentric_unchecked(target_itrs_km: [f64; 3], station: &GeodeticStationKm) -> Vec3 {
1044    let [target_x, target_y, target_z] = target_itrs_km;
1045    let (stn_x, stn_y, stn_z) = geodetic_to_itrs_unchecked(
1046        station.latitude_deg,
1047        station.longitude_deg,
1048        station.altitude_km,
1049    );
1050
1051    let dx = target_x - stn_x;
1052    let dy = target_y - stn_y;
1053    let dz = target_z - stn_z;
1054
1055    let enu_mat = ecef_to_enu_matrix(station.latitude_deg, station.longitude_deg);
1056    let enu = mat3_vec3_mul_unchecked(&enu_mat, &[dx, dy, dz]);
1057    let east = enu[0];
1058    let north = enu[1];
1059    let up = enu[2];
1060
1061    // Range
1062    let range = (east * east + north * north + up * up).sqrt();
1063
1064    // Elevation
1065    let elevation = (up / range).asin().to_degrees();
1066
1067    // Azimuth (measured clockwise from north). At (and arbitrarily near) the
1068    // station zenith the east and north components are pure rounding residuals,
1069    // so azimuth is degenerate and defined to be 0.0 (RTKLIB satazel semantics).
1070    let horiz_sq = east * east + north * north;
1071    let mut azimuth = if horiz_sq < AZIMUTH_ZENITH_EPS * range * range {
1072        0.0
1073    } else {
1074        east.atan2(north).to_degrees()
1075    };
1076    if azimuth < 0.0 {
1077        azimuth += 360.0;
1078    }
1079
1080    (azimuth, elevation, range)
1081}
1082
1083/// Skyfield-compatible topocentric: stays in GCRS AU the entire time.
1084///
1085/// Replicates Skyfield's altaz computation:
1086/// 1. R_lat = rot_y(lat)[::-1]  (row-reversed Y rotation)
1087/// 2. R_latlon = mxm(R_lat, rot_z(-lon))
1088/// 3. R_full = mxm(R_latlon, itrs_rotation)
1089/// 4. station_gcrs_au = transpose(itrs_rotation) * station_itrs_au
1090/// 5. diff_au = sat_gcrs_au - station_gcrs_au
1091/// 6. enu_au = mxv(R_full, diff_au)
1092/// 7. to_spherical(enu_au) -> (range_au, elevation_rad, azimuth_rad)
1093fn gcrs_to_topocentric_skyfield(
1094    sat_x: f64,
1095    sat_y: f64,
1096    sat_z: f64,
1097    station_lat_deg: f64,
1098    station_lon_deg: f64,
1099    station_alt_km: f64,
1100    ts: &TimeScales,
1101) -> (f64, f64, f64) {
1102    let lat_rad = station_lat_deg * TAU / 360.0;
1103    let lon_rad = station_lon_deg * TAU / 360.0;
1104
1105    // Build R_lat = rot_y(lat)[::-1]  (rows reversed)
1106    let cy = lat_rad.cos();
1107    let sy = lat_rad.sin();
1108    // rot_y(lat) = [[cy, 0, sy], [0, 1, 0], [-sy, 0, cy]]
1109    // [::-1] reverses rows: [[-sy, 0, cy], [0, 1, 0], [cy, 0, sy]]
1110    let r_lat: Mat3 = [[-sy, 0.0, cy], [0.0, 1.0, 0.0], [cy, 0.0, sy]];
1111
1112    // R_latlon = mxm(R_lat, rot_z(-lon))
1113    let rz_neg_lon = build_rot_z(-lon_rad);
1114    let r_latlon = inline_rxr(&r_lat, &rz_neg_lon);
1115
1116    // R_full = mxm(R_latlon, itrs_rotation)
1117    let r_itrs = gcrs_to_itrs_matrix_unchecked(ts);
1118    let r_full = inline_rxr(&r_latlon, &r_itrs);
1119
1120    // Station ITRS position directly in AU, matching Skyfield's Geoid.latlon
1121    // which computes in AU from the start (not km then / AU_KM).
1122    let stn_itrs_au = geodetic_to_itrs_au(station_lat_deg, station_lon_deg, station_alt_km);
1123
1124    // Station GCRS AU = transpose(R_itrs) * station_itrs_au
1125    let r_itrs_t = inline_tr(&r_itrs);
1126    let stn_gcrs_au = mat3_vec3_mul_unchecked(&r_itrs_t, &stn_itrs_au);
1127
1128    // Satellite GCRS in AU
1129    let sat_au = [sat_x / AU_KM, sat_y / AU_KM, sat_z / AU_KM];
1130
1131    // Difference vector in GCRS AU
1132    let diff_au = [
1133        sat_au[0] - stn_gcrs_au[0],
1134        sat_au[1] - stn_gcrs_au[1],
1135        sat_au[2] - stn_gcrs_au[2],
1136    ];
1137
1138    // Rotate to ENU-ish frame: mxv(R_full, diff_au)
1139    let enu_au = mat3_vec3_mul_unchecked(&r_full, &diff_au);
1140
1141    // to_spherical: r, theta (elevation), phi (azimuth)
1142    let ex = enu_au[0];
1143    let ey = enu_au[1];
1144    let ez = enu_au[2];
1145
1146    let r_au = (ex * ex + ey * ey + ez * ez).sqrt();
1147    let elevation_rad = ez.atan2((ex * ex + ey * ey).sqrt());
1148    let mut azimuth_rad = ey.atan2(ex) % TAU;
1149    if azimuth_rad < 0.0 {
1150        azimuth_rad += TAU;
1151    }
1152
1153    let range_km = r_au * AU_KM;
1154    let elevation_deg = elevation_rad * 360.0 / TAU;
1155    let azimuth_deg = azimuth_rad * 360.0 / TAU;
1156
1157    (azimuth_deg, elevation_deg, range_km)
1158}
1159
1160#[cfg(test)]
1161mod tests {
1162    use super::*;
1163    use crate::astro::time::scales::TimeScales;
1164
1165    fn assert_mat3_bits_eq(actual: &Mat3, expected: &Mat3) {
1166        for i in 0..3 {
1167            for j in 0..3 {
1168                assert_eq!(
1169                    actual[i][j].to_bits(),
1170                    expected[i][j].to_bits(),
1171                    "matrix[{i}][{j}]: {} vs {}",
1172                    actual[i][j],
1173                    expected[i][j]
1174                );
1175            }
1176        }
1177    }
1178
1179    fn assert_vec3_bits_eq(actual: [f64; 3], expected: [f64; 3]) {
1180        for i in 0..3 {
1181            assert_eq!(
1182                actual[i].to_bits(),
1183                expected[i].to_bits(),
1184                "vector[{i}]: {} vs {}",
1185                actual[i],
1186                expected[i]
1187            );
1188        }
1189    }
1190
1191    #[test]
1192    fn itrs_to_gcrs_inverts_gcrs_to_itrs() {
1193        // On a real epoch and a real-magnitude ECI vector, ITRS->GCRS recovers
1194        // the GCRS->ITRS input bit-for-bit on the plain (non-Skyfield) km path:
1195        // the two directions must be exact transposes, not just approximately so.
1196        let ts = TimeScales::from_utc(2020, 6, 24, 12, 34, 56.0).expect("valid UTC instant");
1197        let (x, y, z) = (4321.0_f64, -5678.0, 3210.0);
1198
1199        let (ix, iy, iz) =
1200            gcrs_to_itrs_compute(x, y, z, &ts, false).expect("valid frame transform");
1201        // The rotation actually moved the vector (it is not a no-op).
1202        assert!(((ix - x).abs() + (iy - y).abs() + (iz - z).abs()) > 100.0);
1203
1204        let (bx, by, bz) = itrs_to_gcrs_compute(ix, iy, iz, &ts).expect("valid frame transform");
1205        assert!((bx - x).abs() < 1e-9, "x {bx} vs {x}");
1206        assert!((by - y).abs() < 1e-9, "y {by} vs {y}");
1207        assert!((bz - z).abs() < 1e-9, "z {bz} vs {z}");
1208
1209        // Magnitude is preserved by the rotation.
1210        let n0 = (x * x + y * y + z * z).sqrt();
1211        let n1 = (ix * ix + iy * iy + iz * iz).sqrt();
1212        assert!((n0 - n1).abs() < 1e-9);
1213    }
1214
1215    #[test]
1216    fn polar_motion_matrix_matches_documented_convention() {
1217        let pole = PolarMotion::from_arcseconds(0.25, -0.35).expect("valid polar motion");
1218        let cx = pole.xp_rad.cos();
1219        let sx = pole.xp_rad.sin();
1220        let cy = pole.yp_rad.cos();
1221        let sy = pole.yp_rad.sin();
1222
1223        let expected = [
1224            [cx, sx * sy, sx * cy],
1225            [0.0, cy, -sy],
1226            [-sx, cx * sy, cx * cy],
1227        ];
1228        let got = polar_motion_matrix(pole).expect("valid polar motion matrix");
1229        assert_mat3_bits_eq(&got, &expected);
1230
1231        let small_angle = [
1232            [1.0, 0.0, pole.xp_rad],
1233            [0.0, 1.0, -pole.yp_rad],
1234            [-pole.xp_rad, pole.yp_rad, 1.0],
1235        ];
1236        for i in 0..3 {
1237            for j in 0..3 {
1238                assert!(
1239                    (got[i][j] - small_angle[i][j]).abs() < 1.0e-11,
1240                    "matrix[{i}][{j}] {} vs small-angle {}",
1241                    got[i][j],
1242                    small_angle[i][j]
1243                );
1244            }
1245        }
1246    }
1247
1248    #[test]
1249    fn gcrs_to_itrs_with_polar_motion_premultiplies_legacy_rotation() {
1250        let ts = TimeScales::from_utc(2020, 6, 24, 12, 34, 56.0).expect("valid UTC instant");
1251        let pole = PolarMotion::from_arcseconds(0.18, -0.24).expect("valid polar motion");
1252        let legacy = gcrs_to_itrs_matrix(&ts).expect("valid frame transform");
1253        let expected = inline_rxr(
1254            &polar_motion_matrix(pole).expect("valid polar motion matrix"),
1255            &legacy,
1256        );
1257        let got = gcrs_to_itrs_matrix_with_polar_motion(&ts, pole).expect("valid frame transform");
1258
1259        assert_mat3_bits_eq(&got, &expected);
1260
1261        let pos = [4321.0_f64, -5678.0, 3210.0];
1262        let actual_vec =
1263            gcrs_to_itrs_compute_with_polar_motion(pos[0], pos[1], pos[2], &ts, false, pole)
1264                .expect("valid frame transform");
1265        let expected_vec = mat3_vec3_mul(&expected, &pos).expect("finite matrix-vector product");
1266        assert_vec3_bits_eq([actual_vec.0, actual_vec.1, actual_vec.2], expected_vec);
1267
1268        let legacy_vec =
1269            gcrs_to_itrs_compute(pos[0], pos[1], pos[2], &ts, false).expect("valid transform");
1270        let delta = (actual_vec.0 - legacy_vec.0).abs()
1271            + (actual_vec.1 - legacy_vec.1).abs()
1272            + (actual_vec.2 - legacy_vec.2).abs();
1273        assert!(
1274            delta > 1.0e-4,
1275            "nonzero polar motion should move the vector"
1276        );
1277
1278        let inverse =
1279            itrs_to_gcrs_matrix_with_polar_motion(&ts, pole).expect("valid frame transform");
1280        assert_mat3_bits_eq(&inverse, &inline_tr(&got));
1281    }
1282
1283    #[test]
1284    fn zero_polar_motion_matches_legacy_transform_bits() {
1285        let ts = TimeScales::from_utc(2020, 6, 24, 12, 34, 56.0).expect("valid UTC instant");
1286        let legacy = gcrs_to_itrs_matrix(&ts).expect("valid frame transform");
1287        let zero = gcrs_to_itrs_matrix_with_polar_motion(&ts, PolarMotion::ZERO)
1288            .expect("valid frame transform");
1289        assert_mat3_bits_eq(&zero, &legacy);
1290
1291        let mean_legacy = mean_of_date_to_itrs_matrix(&ts).expect("valid frame transform");
1292        let mean_zero = mean_of_date_to_itrs_matrix_with_polar_motion(&ts, PolarMotion::ZERO)
1293            .expect("valid frame transform");
1294        assert_mat3_bits_eq(&mean_zero, &mean_legacy);
1295
1296        let pos = [4321.0_f64, -5678.0, 3210.0];
1297        for skyfield_compat in [false, true] {
1298            let legacy_vec = gcrs_to_itrs_compute(pos[0], pos[1], pos[2], &ts, skyfield_compat)
1299                .expect("valid frame transform");
1300            let zero_vec = gcrs_to_itrs_compute_with_polar_motion(
1301                pos[0],
1302                pos[1],
1303                pos[2],
1304                &ts,
1305                skyfield_compat,
1306                PolarMotion::ZERO,
1307            )
1308            .expect("valid frame transform");
1309            assert_vec3_bits_eq(
1310                [zero_vec.0, zero_vec.1, zero_vec.2],
1311                [legacy_vec.0, legacy_vec.1, legacy_vec.2],
1312            );
1313        }
1314
1315        let legacy_back =
1316            itrs_to_gcrs_compute(pos[0], pos[1], pos[2], &ts).expect("valid frame transform");
1317        let zero_back =
1318            itrs_to_gcrs_compute_with_polar_motion(pos[0], pos[1], pos[2], &ts, PolarMotion::ZERO)
1319                .expect("valid frame transform");
1320        assert_vec3_bits_eq(
1321            [zero_back.0, zero_back.1, zero_back.2],
1322            [legacy_back.0, legacy_back.1, legacy_back.2],
1323        );
1324    }
1325
1326    #[test]
1327    fn frame_transforms_reject_nonfinite_time() {
1328        let mut ts = TimeScales::from_utc(2020, 6, 24, 12, 34, 56.0).expect("valid UTC instant");
1329        ts.jd_tt = f64::NAN;
1330
1331        assert!(greenwich_mean_sidereal_time_radians(&ts).is_err());
1332        assert!(gcrs_to_itrs_matrix(&ts).is_err());
1333        assert!(itrs_to_gcrs_compute(1.0, 2.0, 3.0, &ts).is_err());
1334    }
1335
1336    #[test]
1337    fn frame_transforms_reject_nonfinite_pole_coordinates() {
1338        let ts = TimeScales::from_utc(2020, 6, 24, 12, 34, 56.0).expect("valid UTC instant");
1339        assert!(PolarMotion::from_radians(f64::NAN, 0.0).is_err());
1340        assert!(PolarMotion::from_arcseconds(0.0, f64::INFINITY).is_err());
1341
1342        let pole = PolarMotion {
1343            xp_rad: f64::NAN,
1344            yp_rad: 0.0,
1345        };
1346        assert!(polar_motion_matrix(pole).is_err());
1347        assert!(gcrs_to_itrs_matrix_with_polar_motion(&ts, pole).is_err());
1348    }
1349
1350    #[test]
1351    fn frame_transforms_reject_nonfinite_vectors() {
1352        let ts = TimeScales::from_utc(2020, 6, 24, 12, 34, 56.0).expect("valid UTC instant");
1353        let bad_state = TemeStateKm {
1354            position_km: [1.0, f64::NAN, 3.0],
1355            velocity_km_s: [0.1, 0.2, 0.3],
1356        };
1357        assert!(teme_to_gcrs_compute(&bad_state, &ts, false).is_err());
1358        assert!(gcrs_to_itrs_compute(1.0, f64::INFINITY, 3.0, &ts, false).is_err());
1359        assert!(itrs_to_gcrs_compute(1.0, 2.0, f64::NEG_INFINITY, &ts).is_err());
1360    }
1361
1362    #[test]
1363    fn validated_frame_transform_preserves_valid_bits() {
1364        let ts = TimeScales::from_utc(2020, 6, 24, 12, 34, 56.0).expect("valid UTC instant");
1365        let pos = [4321.0_f64, -5678.0, 3210.0];
1366        let expected = gcrs_to_itrs_compute_unchecked(pos[0], pos[1], pos[2], &ts, true);
1367        let got =
1368            gcrs_to_itrs_compute(pos[0], pos[1], pos[2], &ts, true).expect("valid frame transform");
1369        assert_vec3_bits_eq([got.0, got.1, got.2], [expected.0, expected.1, expected.2]);
1370    }
1371
1372    #[test]
1373    fn geodetic_transforms_reject_invalid_coordinates() {
1374        assert!(itrs_to_geodetic_compute(f64::NAN, 0.0, 0.0).is_err());
1375        assert!(geodetic_from_ecef_proj(0.0, f64::INFINITY, 0.0).is_err());
1376        assert!(geodetic_to_itrs(90.000_001, 0.0, 0.0).is_err());
1377        assert!(geodetic_to_itrs(0.0, -180.000_001, 0.0).is_err());
1378        assert!(geodetic_to_itrs(0.0, 0.0, f64::NAN).is_err());
1379    }
1380
1381    #[test]
1382    fn topocentric_transform_rejects_invalid_coordinates() {
1383        let ts = TimeScales::from_utc(2020, 6, 24, 12, 34, 56.0).expect("valid UTC instant");
1384        let station = GeodeticStationKm {
1385            latitude_deg: f64::NAN,
1386            longitude_deg: 0.0,
1387            altitude_km: 0.0,
1388        };
1389        assert!(gcrs_to_topocentric_compute([7000.0, 0.0, 0.0], &station, &ts, false).is_err());
1390
1391        let station = GeodeticStationKm {
1392            latitude_deg: 0.0,
1393            longitude_deg: 181.0,
1394            altitude_km: 0.0,
1395        };
1396        assert!(gcrs_to_topocentric_compute([7000.0, 0.0, 0.0], &station, &ts, false).is_err());
1397
1398        let station = GeodeticStationKm {
1399            latitude_deg: 0.0,
1400            longitude_deg: 0.0,
1401            altitude_km: 0.0,
1402        };
1403        assert!(
1404            gcrs_to_topocentric_compute([7000.0, f64::NAN, 0.0], &station, &ts, false).is_err()
1405        );
1406    }
1407
1408    #[test]
1409    fn topocentric_azimuth_is_zero_at_station_zenith() {
1410        let ts = TimeScales::from_utc(2020, 6, 24, 12, 34, 56.0).expect("valid UTC instant");
1411        // Equatorial station: geodetic up coincides with the geocentric radial,
1412        // so a satellite displaced radially outward is exactly overhead.
1413        let station = GeodeticStationKm {
1414            latitude_deg: 0.0,
1415            longitude_deg: 0.0,
1416            altitude_km: 0.0,
1417        };
1418        let (sx, sy, sz) = geodetic_to_itrs_unchecked(0.0, 0.0, 0.0);
1419        // Satellite ITRS position 20,000 km straight up (+X at lat0/lon0).
1420        let sat_itrs = [sx + 20_000.0, sy, sz];
1421        // Express it in GCRS so the standard path rotates it back to `sat_itrs`.
1422        let r_itrs = gcrs_to_itrs_matrix_unchecked(&ts);
1423        let r_itrs_t = inline_tr(&r_itrs);
1424        let sat_gcrs = mat3_vec3_mul_unchecked(&r_itrs_t, &sat_itrs);
1425
1426        let (azimuth_deg, elevation_deg, _range_km) =
1427            gcrs_to_topocentric_compute_unchecked(sat_gcrs, &station, &ts, false);
1428        assert_eq!(azimuth_deg, 0.0);
1429        assert!(azimuth_deg.is_finite());
1430        assert!((elevation_deg - 90.0).abs() < 1e-6);
1431    }
1432
1433    #[test]
1434    fn validated_geodetic_transform_preserves_valid_bits() {
1435        let (lat, lon, alt) = (51.4779, -0.0015, 0.046);
1436        let expected = geodetic_to_itrs_unchecked(lat, lon, alt);
1437        let got = geodetic_to_itrs(lat, lon, alt).expect("valid geodetic coordinates");
1438        assert_eq!(got.0.to_bits(), expected.0.to_bits());
1439        assert_eq!(got.1.to_bits(), expected.1.to_bits());
1440        assert_eq!(got.2.to_bits(), expected.2.to_bits());
1441
1442        let expected = itrs_to_geodetic_compute_unchecked(got.0, got.1, got.2);
1443        let roundtrip =
1444            itrs_to_geodetic_compute(got.0, got.1, got.2).expect("valid ITRS coordinates");
1445        assert_eq!(roundtrip.0.to_bits(), expected.0.to_bits());
1446        assert_eq!(roundtrip.1.to_bits(), expected.1.to_bits());
1447        assert_eq!(roundtrip.2.to_bits(), expected.2.to_bits());
1448    }
1449
1450    #[test]
1451    fn sidereal_time_wrappers_are_in_range_and_consistent() {
1452        let ts = TimeScales::from_utc(2020, 6, 24, 12, 34, 56.0).expect("valid UTC instant");
1453        let gmst = greenwich_mean_sidereal_time_radians(&ts).expect("valid sidereal time");
1454        let gast = greenwich_apparent_sidereal_time_radians(&ts).expect("valid sidereal time");
1455
1456        // Both land in [0, 2pi).
1457        assert!((0.0..TAU).contains(&gmst), "gmst {gmst}");
1458        assert!((0.0..TAU).contains(&gast), "gast {gast}");
1459
1460        // The equation of the equinoxes is a small (sub-arcminute) offset, so the
1461        // apparent and mean sidereal times stay close (handle the seam at 2pi).
1462        let diff = (gast - gmst).rem_euclid(TAU);
1463        let eq_eq = diff.min(TAU - diff);
1464        assert!(eq_eq < 1.0e-3, "equation of equinoxes too large: {eq_eq}");
1465
1466        // The mean wrapper matches the Skyfield-parity IAU-1982 sidereal time.
1467        let gmst_hours = sidereal_time_hours(ts.jd_whole, ts.ut1_fraction, ts.tdb_fraction);
1468        assert_eq!(gmst, gmst_hours / 24.0 * TAU);
1469    }
1470
1471    #[test]
1472    fn gmst_from_j2000_seconds_matches_gmst1982_polynomial() {
1473        // The seconds-based accessor reproduces the GMST-1982 polynomial the TEME
1474        // sidereal rotation uses, not the public Skyfield-parity GMST. Tolerance
1475        // allows only the roundoff from reducing the UT1 split to one f64.
1476        const GMST_TOL_RAD: f64 = 1.0e-11;
1477        let ts = TimeScales::from_utc(2020, 6, 24, 12, 34, 56.25).expect("valid UTC instant");
1478        let sec = (ts.jd_ut1 - J2000_JD) * SECONDS_PER_DAY;
1479        let from_seconds = greenwich_mean_sidereal_time_radians_from_j2000_seconds(sec)
1480            .expect("valid sidereal time");
1481        let mut expected = compute_theta_gmst1982(ts.jd_whole, ts.ut1_fraction) % TAU;
1482        if expected < 0.0 {
1483            expected += TAU;
1484        }
1485
1486        assert!((from_seconds - expected).abs() <= GMST_TOL_RAD);
1487    }
1488}