siderust 0.6.0

High-precision astronomy and satellite mechanics in Rust.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
// SPDX-License-Identifier: AGPL-3.0-or-later
// Copyright (C) 2026 Vallés Puig, Ramon

//! # Observational Direction Types
//!
//! This module defines direction types with explicit observational state markers.
//! A direction can be either `Astrometric` (geometric) or `Apparent` (observed).
//!
//! ## Type-Level Observational State
//!
//! Rather than storing state as runtime metadata, we use wrapper types:
//!
//! - `Astrometric<D>`: A geometric direction, not corrected for observer motion
//! - `Apparent<D>`: A direction as observed, with aberration applied
//!
//! This makes the physical meaning unambiguous at compile time.
//!
//! ## Conversions
//!
//! Converting between states requires an `ObserverState`:
//!
//! ```rust
//! use siderust::coordinates::observation::{Astrometric, Apparent, ObserverState};
//! use siderust::coordinates::spherical::direction::EquatorialMeanJ2000;
//! use siderust::time::JulianDate;
//! use qtty::*;
//!
//! let geo_dir = Astrometric::new(EquatorialMeanJ2000::new(45.0 * DEG, 20.0 * DEG));
//! let obs = ObserverState::geocentric(JulianDate::J2000);
//!
//! // Explicit conversion with observer state
//! let app_dir: Apparent<EquatorialMeanJ2000> = geo_dir.to_apparent(&obs);
//!
//! // Can convert back
//! let geo_again: Astrometric<EquatorialMeanJ2000> = app_dir.to_astrometric(&obs);
//! ```

use super::ObserverState;
use crate::astro::aberration::{
    apply_aberration_to_direction_with_velocity, remove_aberration_from_direction_with_velocity,
};
use crate::coordinates::cartesian;
use crate::coordinates::frames::{EquatorialMeanJ2000, MutableFrame};
use crate::coordinates::spherical;
use crate::coordinates::transform::TransformFrame;

// =============================================================================
// Astrometric Direction
// =============================================================================

/// A direction representing the geometric (astrometric) position of a celestial object.
///
/// Astrometric directions are:
/// - Corrected for proper motion and parallax (if applicable)
/// - **Not** corrected for aberration or other observer-motion effects
/// - The "true" direction to the object in space
///
/// To obtain an apparent direction (as observed), call `to_apparent()` with
/// an `ObserverState`.
///
/// # Type Parameter
///
/// - `D`: The underlying direction type (e.g., `spherical::direction::EquatorialMeanJ2000`)
#[derive(Debug, Clone, Copy)]
pub struct Astrometric<D> {
    direction: D,
}

impl<D> Astrometric<D> {
    /// Creates a new astrometric direction from a geometric direction.
    ///
    /// # Arguments
    ///
    /// * `direction` - The geometric direction
    pub fn new(direction: D) -> Self {
        Self { direction }
    }

    /// Returns a reference to the underlying direction.
    pub fn direction(&self) -> &D {
        &self.direction
    }

    /// Consumes the wrapper and returns the underlying direction.
    pub fn into_inner(self) -> D {
        self.direction
    }
}

impl<F: MutableFrame> Astrometric<spherical::Direction<F>> {
    /// Converts this astrometric direction to an apparent direction.
    ///
    /// This applies aberration based on the observer's velocity.
    ///
    /// # Arguments
    ///
    /// * `obs` - The observer state (provides velocity for aberration)
    ///
    /// # Example
    ///
    /// ```rust
    /// use siderust::coordinates::observation::{Astrometric, Apparent, ObserverState};
    /// use siderust::coordinates::spherical::direction::EquatorialMeanJ2000;
    /// use siderust::time::JulianDate;
    /// use qtty::*;
    ///
    /// let astrometric = Astrometric::new(EquatorialMeanJ2000::new(0.0 * DEG, 0.0 * DEG));
    /// let obs = ObserverState::geocentric(JulianDate::J2000);
    /// let apparent: Apparent<EquatorialMeanJ2000> = astrometric.to_apparent(&obs);
    /// ```
    pub fn to_apparent(self, obs: &ObserverState) -> Apparent<spherical::Direction<F>>
    where
        cartesian::Direction<EquatorialMeanJ2000>: TransformFrame<cartesian::Direction<F>>,
        cartesian::Direction<F>: TransformFrame<cartesian::Direction<EquatorialMeanJ2000>>,
    {
        // Convert to cartesian for vector operations
        let cart_dir = self.direction.to_cartesian();

        // Transform to equatorial frame for aberration
        let cart_eq: cartesian::Direction<EquatorialMeanJ2000> = cart_dir.to_frame();

        // Get observer velocity
        let vel = obs.velocity();

        let apparent_eq = apply_aberration_to_direction_with_velocity(cart_eq, vel);

        // Transform back to original frame
        let apparent_cart: cartesian::Direction<F> = apparent_eq.to_frame();
        let apparent_sph = apparent_cart.to_spherical();

        Apparent::new(apparent_sph)
    }
}

impl<F: MutableFrame> Astrometric<cartesian::Direction<F>> {
    /// Converts this astrometric direction to an apparent direction.
    ///
    /// This applies aberration based on the observer's velocity.
    pub fn to_apparent(self, obs: &ObserverState) -> Apparent<cartesian::Direction<F>>
    where
        cartesian::Direction<EquatorialMeanJ2000>: TransformFrame<cartesian::Direction<F>>,
        cartesian::Direction<F>: TransformFrame<cartesian::Direction<EquatorialMeanJ2000>>,
    {
        // Transform to equatorial frame for aberration
        let cart_eq: cartesian::Direction<EquatorialMeanJ2000> = self.direction.to_frame();

        // Get observer velocity
        let vel = obs.velocity();

        let apparent_eq = apply_aberration_to_direction_with_velocity(cart_eq, vel);

        // Transform back to original frame
        let apparent_cart: cartesian::Direction<F> = apparent_eq.to_frame();

        Apparent::new(apparent_cart)
    }
}

// =============================================================================
// Apparent Direction
// =============================================================================

/// A direction representing the apparent (observed) position of a celestial object.
///
/// Apparent directions include:
/// - All astrometric corrections (proper motion, parallax)
/// - Aberration correction based on observer velocity
/// - (Future) Other observation effects like gravitational deflection
///
/// The direction has been shifted from its geometric (astrometric) position
/// due to the finite speed of light and the observer's motion.
///
/// # Type Parameter
///
/// - `D`: The underlying direction type (e.g., `spherical::direction::EquatorialMeanJ2000`)
#[derive(Debug, Clone, Copy)]
pub struct Apparent<D> {
    direction: D,
}

impl<D> Apparent<D> {
    /// Creates a new apparent direction.
    ///
    /// # Note
    ///
    /// You typically obtain apparent directions by calling `to_apparent()`
    /// on an `Astrometric` direction. This constructor is provided for
    /// cases where you have a known apparent position.
    pub fn new(direction: D) -> Self {
        Self { direction }
    }

    /// Returns a reference to the underlying direction.
    pub fn direction(&self) -> &D {
        &self.direction
    }

    /// Consumes the wrapper and returns the underlying direction.
    pub fn into_inner(self) -> D {
        self.direction
    }
}

impl<F: MutableFrame> Apparent<spherical::Direction<F>> {
    /// Converts this apparent direction back to an astrometric direction.
    ///
    /// This removes aberration based on the observer's velocity.
    ///
    /// # Arguments
    ///
    /// * `obs` - The observer state (provides velocity for aberration removal)
    pub fn to_astrometric(self, obs: &ObserverState) -> Astrometric<spherical::Direction<F>>
    where
        cartesian::Direction<EquatorialMeanJ2000>: TransformFrame<cartesian::Direction<F>>,
        cartesian::Direction<F>: TransformFrame<cartesian::Direction<EquatorialMeanJ2000>>,
    {
        // Convert to cartesian for vector operations
        let cart_dir = self.direction.to_cartesian();

        // Transform to equatorial frame for aberration
        let cart_eq: cartesian::Direction<EquatorialMeanJ2000> = cart_dir.to_frame();

        // Get observer velocity
        let vel = obs.velocity();

        let astrometric_eq = remove_aberration_from_direction_with_velocity(cart_eq, vel);

        // Transform back to original frame
        let astrometric_cart: cartesian::Direction<F> = astrometric_eq.to_frame();
        let astrometric_sph = astrometric_cart.to_spherical();

        Astrometric::new(astrometric_sph)
    }
}

impl<F: MutableFrame> Apparent<cartesian::Direction<F>> {
    /// Converts this apparent direction back to an astrometric direction.
    ///
    /// This removes aberration based on the observer's velocity.
    pub fn to_astrometric(self, obs: &ObserverState) -> Astrometric<cartesian::Direction<F>>
    where
        cartesian::Direction<EquatorialMeanJ2000>: TransformFrame<cartesian::Direction<F>>,
        cartesian::Direction<F>: TransformFrame<cartesian::Direction<EquatorialMeanJ2000>>,
    {
        // Transform to equatorial frame for aberration
        let cart_eq: cartesian::Direction<EquatorialMeanJ2000> = self.direction.to_frame();

        // Get observer velocity
        let vel = obs.velocity();

        let astrometric_eq = remove_aberration_from_direction_with_velocity(cart_eq, vel);

        // Transform back to original frame
        let astrometric_cart: cartesian::Direction<F> = astrometric_eq.to_frame();

        Astrometric::new(astrometric_cart)
    }
}

// =============================================================================
// Display implementations
// =============================================================================

impl<D: std::fmt::Display> std::fmt::Display for Astrometric<D> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "Astrometric({})", self.direction)
    }
}

impl<D: std::fmt::Display> std::fmt::Display for Apparent<D> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "Apparent({})", self.direction)
    }
}

impl<D: std::fmt::LowerExp> std::fmt::LowerExp for Astrometric<D> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "Astrometric(")?;
        std::fmt::LowerExp::fmt(&self.direction, f)?;
        write!(f, ")")
    }
}

impl<D: std::fmt::LowerExp> std::fmt::LowerExp for Apparent<D> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "Apparent(")?;
        std::fmt::LowerExp::fmt(&self.direction, f)?;
        write!(f, ")")
    }
}

impl<D: std::fmt::UpperExp> std::fmt::UpperExp for Astrometric<D> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "Astrometric(")?;
        std::fmt::UpperExp::fmt(&self.direction, f)?;
        write!(f, ")")
    }
}

impl<D: std::fmt::UpperExp> std::fmt::UpperExp for Apparent<D> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "Apparent(")?;
        std::fmt::UpperExp::fmt(&self.direction, f)?;
        write!(f, ")")
    }
}

// =============================================================================
// Tests
// =============================================================================

#[cfg(test)]
mod tests {
    use super::*;
    use crate::time::JulianDate;
    use qtty::*;

    #[test]
    fn test_astrometric_to_apparent_introduces_shift() {
        let jd = JulianDate::J2000;
        let obs = ObserverState::geocentric(jd);

        // Create an astrometric direction
        let astrometric = Astrometric::new(spherical::direction::EquatorialMeanJ2000::new(
            0.0 * DEG,
            0.0 * DEG,
        ));

        // Convert to apparent
        let apparent: Apparent<spherical::direction::EquatorialMeanJ2000> =
            astrometric.to_apparent(&obs);

        // The shift should be on the order of 20 arcseconds (aberration constant)
        let original = astrometric.direction();
        let shifted = apparent.direction();

        // Handle azimuth wrap-around (e.g., 359.999° vs 0.001° is actually 0.002° apart)
        let mut delta_ra = (shifted.azimuth - original.azimuth).abs();
        if delta_ra > 180.0 {
            delta_ra = Degrees::new(360.0) - delta_ra;
        }
        let delta_dec = (shifted.polar - original.polar).abs();

        // At least one should have changed
        assert!(
            delta_ra > 0.0 || delta_dec > 0.0,
            "Expected aberration to introduce a shift"
        );

        // Shift should be small (less than 1 degree, typically ~20 arcsec = 0.006 deg)
        assert!(
            delta_ra < 0.1 && delta_dec < 0.1,
            "Aberration shift too large: dRA={}, dDec={}",
            delta_ra,
            delta_dec
        );
    }

    #[test]
    fn test_roundtrip_preserves_direction() {
        let jd = JulianDate::J2000;
        let obs = ObserverState::geocentric(jd);

        // Create an astrometric direction
        let original = Astrometric::new(spherical::direction::EquatorialMeanJ2000::new(
            45.0 * DEG,
            30.0 * DEG,
        ));

        // Convert to apparent and back
        let apparent = original.to_apparent(&obs);
        let recovered: Astrometric<spherical::direction::EquatorialMeanJ2000> =
            apparent.to_astrometric(&obs);

        // Should be very close to original
        let orig_dir = original.direction();
        let rec_dir = recovered.direction();

        let delta_ra = (rec_dir.azimuth - orig_dir.azimuth).abs();
        let delta_dec = (rec_dir.polar - orig_dir.polar).abs();

        // Tolerance of 1e-6 degrees ≈ 0.003 arcseconds (numerical precision limit)
        assert!(
            delta_ra < 1e-6,
            "RA not preserved in roundtrip: delta = {}",
            delta_ra
        );
        assert!(
            delta_dec < 1e-6,
            "Dec not preserved in roundtrip: delta = {}",
            delta_dec
        );
    }

    #[test]
    fn test_apparent_at_pole() {
        let jd = JulianDate::J2000;
        let obs = ObserverState::geocentric(jd);

        // At the north pole
        let astrometric = Astrometric::new(spherical::direction::EquatorialMeanJ2000::new(
            0.0 * DEG,
            90.0 * DEG,
        ));

        let apparent = astrometric.to_apparent(&obs);

        // Declination should decrease slightly
        assert!(
            apparent.direction().polar < 90.0,
            "Declination at pole should decrease due to aberration"
        );
    }

    #[test]
    fn test_type_safety() {
        // This test verifies that you can't accidentally mix astrometric and apparent
        let jd = JulianDate::J2000;
        let obs = ObserverState::geocentric(jd);

        let astrometric = Astrometric::new(spherical::direction::EquatorialMeanJ2000::new(
            10.0 * DEG,
            20.0 * DEG,
        ));
        let apparent = astrometric.to_apparent(&obs);

        // These are different types - can't be confused
        let _astrometric_inner: &spherical::direction::EquatorialMeanJ2000 =
            astrometric.direction();
        let _apparent_inner: &spherical::direction::EquatorialMeanJ2000 = apparent.direction();

        // The wrappers make the distinction explicit
        assert!(format!("{}", astrometric).contains("Astrometric"));
        assert!(format!("{}", apparent).contains("Apparent"));
    }
}