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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
// SPDX-License-Identifier: AGPL-3.0-or-later
// Copyright (C) 2026 Vallés Puig, Ramon

//! # Stellar Altitude Window Periods
//!
//! Routines for finding time intervals where a **fixed star** (static ICRS
//! direction) is above, below, or within a given altitude range.
//!
//! ## Algorithm
//!
//! Unlike the uniform‑scan approach used for Sun and Moon, this module
//! exploits the fact that a star's altitude varies **sinusoidally** with
//! Earth's rotation (period = one sidereal day):
//!
//! 1. Precess J2000 coordinates to the midpoint of the search window
//!    (precession drift < 0.01″/day — one evaluation suffices).
//! 2. Solve `cos(H₀) = (sin(h) − sin(δ)sin(φ)) / (cos(δ)cos(φ))`
//!    to find threshold crossing hour angles analytically.
//! 3. Convert H₀ to Mjd crossing times via the GST rate.
//! 4. Refine each predicted crossing with Brent's method on the
//!    **full‑precision** evaluator (precession + nutation + GAST).
//! 5. Label crossings and assemble periods via [`math_core::intervals`].
//!
//! ## Performance
//!
//! The analytical approach evaluates the full‑precision altitude function
//! ~10–20 times per day (Brent refinement + crossing classification),
//! compared to ~144 for a 10‑minute uniform scan.  For week‑long searches
//! this yields a **7–10× speedup** over the generic scan engine.
//!
//! ## Precision
//!
//! Crossing times are refined to the same tolerance as the generic engine
//! (default ≈ 86 µs).  Precession at the midpoint introduces < 25″ of RA
//! error over a full year, well within the ±15‑minute Brent bracket.

use crate::calculus::math_core::{intervals, root_finding};
use crate::coordinates::centers::Geodetic;
use crate::coordinates::frames::ECEF;
use crate::time::JulianDate;
use crate::time::{complement_within, ModifiedJulianDate, Period, MJD};
use qtty::*;

use super::star_equations::{StarAltitudeParams, ThresholdResult};

/// Type aliases.
type Mjd = ModifiedJulianDate;

#[inline]
fn opposite_sign(a: Radians, b: Radians) -> bool {
    a.signum() * b.signum() < 0.0
}

// =============================================================================
// Constants
// =============================================================================

/// Half‑width of the Brent refinement bracket around each analytically
/// predicted crossing.  15 minutes is conservative; the analytical
/// prediction is typically accurate to < 10 seconds.
const BRACKET_HALF: Days = Quantity::new(15.0 / 1440.0);

/// Scan step for the fallback / validation scan path (10 minutes).
const SCAN_STEP_FALLBACK: Days = Quantity::new(10.0 / 1440.0);

// ---------------------------------------------------------------------------
// Fixed Star Altitude
// ---------------------------------------------------------------------------

/// Compute altitude of a fixed RA/Dec object from an observer site.
///
/// Uses IAU 2006 precession + IAU 2000B nutation (via NPB matrix),
/// ERA-based GMST, and the standard equatorial→horizontal formula.
pub(crate) fn fixed_star_altitude_rad(
    mjd: ModifiedJulianDate,
    site: &crate::coordinates::centers::Geodetic<crate::coordinates::frames::ECEF>,
    ra_j2000: qtty::Degrees,
    dec_j2000: qtty::Degrees,
) -> qtty::Radians {
    use crate::astro::earth_rotation::jd_ut1_from_tt_eop;
    use crate::astro::nutation::nutation_iau2000b;
    use crate::astro::precession::precession_nutation_matrix;
    use crate::astro::sidereal::gast_iau2006;
    use crate::coordinates::transform::AstroContext;
    use qtty::Radian;

    let jd: JulianDate = mjd.into();

    // Convert J2000 RA/Dec → unit Cartesian
    let ra_rad = ra_j2000.to::<Radian>().value();
    let dec_rad = dec_j2000.to::<Radian>().value();
    let (sin_dec, cos_dec) = dec_rad.sin_cos();
    let (sin_ra, cos_ra) = ra_rad.sin_cos();
    let x_j2000 = cos_dec * cos_ra;
    let y_j2000 = cos_dec * sin_ra;
    let z_j2000 = sin_dec;

    // Apply full NPB matrix (IAU 2006 precession + IAU 2000B nutation)
    let nut = nutation_iau2000b(jd);
    let npb = precession_nutation_matrix(jd, nut.dpsi, nut.deps);
    let [x_tod, y_tod, z_tod] = npb.apply_array([x_j2000, y_j2000, z_j2000]);

    // Extract true-of-date RA and Dec
    let ra_tod = y_tod.atan2(x_tod);
    let r_xy = (x_tod * x_tod + y_tod * y_tod).sqrt();
    let dec_tod = z_tod.atan2(r_xy);

    // True-of-date RA/Dec requires apparent sidereal time (GAST).
    let ctx: AstroContext = AstroContext::default();
    let eop = ctx.eop_at(jd);
    let jd_ut1 = jd_ut1_from_tt_eop(jd, &eop);
    let gast = gast_iau2006(jd_ut1, jd, nut.dpsi, nut.true_obliquity());
    let lst_rad = gast.value() + site.lon.to::<Radian>().value();
    let ha = (lst_rad - ra_tod).rem_euclid(std::f64::consts::TAU);

    // Equatorial → horizontal altitude
    let lat = site.lat.to::<Radian>().value();
    let sin_alt = dec_tod.sin() * lat.sin() + dec_tod.cos() * lat.cos() * ha.cos();
    qtty::Radians::new(sin_alt.asin())
}

// =============================================================================
// Core: analytical bracket + Brent refinement
// =============================================================================

/// Build a full‑precision altitude closure for a fixed star.
#[inline]
fn make_star_fn<'a>(
    ra_j2000: Degrees,
    dec_j2000: Degrees,
    site: &'a Geodetic<ECEF>,
) -> impl Fn(Mjd) -> Radians + 'a {
    move |t: Mjd| -> Radians { fixed_star_altitude_rad(t, site, ra_j2000, dec_j2000) }
}

/// Find all crossings of a single threshold, refined to full precision.
///
/// Returns chronologically sorted [`LabeledCrossing`]s and whether the
/// function is above threshold at `period.start`.
fn find_crossings_analytical(
    ra_j2000: Degrees,
    dec_j2000: Degrees,
    site: &Geodetic<ECEF>,
    period: Period<MJD>,
    threshold: Radians,
) -> (Vec<intervals::LabeledCrossing>, bool) {
    let thr = threshold;
    let f = make_star_fn(ra_j2000, dec_j2000, site);

    let start_above = f(period.start) > thr;

    // Build analytical model at the period midpoint
    let start_jd: JulianDate = period.start.into();
    let end_jd: JulianDate = period.end.into();
    let mid_jd = start_jd.mean(end_jd);
    let equatorial_j2000 =
        crate::coordinates::spherical::direction::EquatorialMeanJ2000::new(ra_j2000, dec_j2000);
    let params = StarAltitudeParams::from_j2000(equatorial_j2000, site, mid_jd);

    match params.threshold_ha(thr) {
        ThresholdResult::AlwaysAbove => {
            // Verify at both endpoints with the full evaluator
            let end_above = f(period.end) > thr;
            if start_above && end_above {
                (Vec::new(), true)
            } else {
                // Precession drift moved the star across the threshold —
                // fall back to uniform scan for this edge case.
                let mut crossings = intervals::find_crossings(period, SCAN_STEP_FALLBACK, &f, thr);
                let labeled = intervals::label_crossings(&mut crossings, &f, thr);
                (labeled, start_above)
            }
        }
        ThresholdResult::NeverAbove => {
            let end_above = f(period.end) > thr;
            if !start_above && !end_above {
                (Vec::new(), false)
            } else {
                let mut crossings = intervals::find_crossings(period, SCAN_STEP_FALLBACK, &f, thr);
                let labeled = intervals::label_crossings(&mut crossings, &f, thr);
                (labeled, start_above)
            }
        }
        ThresholdResult::Crossings { h0 } => {
            let predicted = params.predict_crossings(period, h0);

            // Shifted altitude: g(t) = f(t) − threshold
            let g = |t: Mjd| -> Radians { f(t) - thr };

            let mut refined: Vec<Mjd> = Vec::with_capacity(predicted.len());

            for (t_pred, _dir) in &predicted {
                let lo = (*t_pred - BRACKET_HALF).max(period.start);
                let hi = (*t_pred + BRACKET_HALF).min(period.end);

                if (hi - lo) < Days::new(1e-12) {
                    continue; // degenerate bracket at boundary
                }

                let g_lo = g(lo);
                let g_hi = g(hi);

                if opposite_sign(g_lo, g_hi) {
                    if let Some(root) =
                        root_finding::brent_with_values(Period::new(lo, hi), g_lo, g_hi, g)
                    {
                        if root >= period.start && root <= period.end {
                            refined.push(root);
                        }
                    }
                }
            }

            let labeled = intervals::label_crossings(&mut refined, &f, thr);
            (labeled, start_above)
        }
    }
}

// =============================================================================
// Public API
// =============================================================================

/// Finds periods when a fixed star is **above** `threshold` inside `period`.
///
/// Uses the analytical sinusoidal model for O(1) bracket discovery per
/// sidereal cycle, refined by Brent's method on the full‑precision
/// evaluator (precession + nutation + GAST → equatorial → horizontal).
///
/// # Arguments
///
/// * `ra_j2000`  — right ascension in J2000 equatorial coordinates
/// * `dec_j2000` — declination in J2000 equatorial coordinates
/// * `site`      — observer location on Earth
/// * `period`    — time window to search
/// * `threshold` — altitude threshold (e.g. 0° for the geometric horizon)
pub(crate) fn find_star_above_periods(
    ra_j2000: Degrees,
    dec_j2000: Degrees,
    site: Geodetic<ECEF>,
    period: Period<MJD>,
    threshold: Degrees,
) -> Vec<Period<MJD>> {
    let thr = threshold.to::<Radian>();
    let f = make_star_fn(ra_j2000, dec_j2000, &site);

    let (labeled, start_above) = find_crossings_analytical(ra_j2000, dec_j2000, &site, period, thr);

    intervals::build_above_periods(&labeled, period, start_above, &f, thr)
}

/// Finds periods when a fixed star is **below** `threshold` inside `period`.
///
/// Complement of [`find_star_above_periods`] within `period`.
pub(crate) fn find_star_below_periods(
    ra_j2000: Degrees,
    dec_j2000: Degrees,
    site: Geodetic<ECEF>,
    period: Period<MJD>,
    threshold: Degrees,
) -> Vec<Period<MJD>> {
    let above = find_star_above_periods(ra_j2000, dec_j2000, site, period, threshold);
    complement_within(period, &above)
}

/// Finds periods when a fixed star's altitude is within `[min, max]`.
///
/// Computed as `above(min) ∩ complement(above(max))`.
pub(crate) fn find_star_range_periods(
    ra_j2000: Degrees,
    dec_j2000: Degrees,
    site: Geodetic<ECEF>,
    period: Period<MJD>,
    range: (Degrees, Degrees),
) -> Vec<Period<MJD>> {
    let above_min = find_star_above_periods(ra_j2000, dec_j2000, site, period, range.0);
    let above_max = find_star_above_periods(ra_j2000, dec_j2000, site, period, range.1);
    let below_max = intervals::complement(period, &above_max);
    intervals::intersect(&above_min, &below_max)
}

// =============================================================================
// Scan‑based variants (for comparison / validation)
// =============================================================================

#[cfg(test)]
/// Finds periods where star is above threshold using the **generic
/// 10‑minute scan** + Brent approach.
///
/// Prefer [`find_star_above_periods`] for production use; this function
/// is provided for validation and performance comparison.
fn find_star_above_periods_scan(
    ra_j2000: Degrees,
    dec_j2000: Degrees,
    site: Geodetic<ECEF>,
    period: Period<MJD>,
    threshold: Degrees,
) -> Vec<Period<MJD>> {
    let thr = threshold.to::<Radian>();
    let f = make_star_fn(ra_j2000, dec_j2000, &site);
    intervals::above_threshold_periods(period, SCAN_STEP_FALLBACK, &f, thr)
}

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

#[cfg(test)]
mod tests {
    use super::*;

    fn greenwich() -> Geodetic<ECEF> {
        Geodetic::<ECEF>::new(
            Degrees::new(0.0),
            Degrees::new(51.4769),
            Quantity::<qtty::Meter>::new(0.0),
        )
    }

    fn roque() -> Geodetic<ECEF> {
        Geodetic::<ECEF>::new(
            Degrees::new(-17.892),
            Degrees::new(28.762),
            Quantity::<qtty::Meter>::new(2396.0),
        )
    }

    fn period_7d() -> Period<MJD> {
        Period::new(
            ModifiedJulianDate::new(60000.0),
            ModifiedJulianDate::new(60007.0),
        )
    }

    #[test]
    fn polaris_always_above_horizon() {
        let periods = find_star_above_periods(
            Degrees::new(37.95),
            Degrees::new(89.26),
            greenwich(),
            period_7d(),
            Degrees::new(0.0),
        );
        assert_eq!(periods.len(), 1, "Polaris should be continuously above");
        let dur = periods[0].duration_days();
        assert!(
            (dur - Days::new(7.0)).abs() < Days::new(0.01),
            "should span full 7 days, got {}",
            dur
        );
    }

    #[test]
    fn sirius_rises_and_sets() {
        let periods = find_star_above_periods(
            Degrees::new(101.287),
            Degrees::new(-16.716),
            greenwich(),
            period_7d(),
            Degrees::new(0.0),
        );
        assert!(
            periods.len() >= 6 && periods.len() <= 8,
            "expected ~7 above‑horizon periods for Sirius, got {}",
            periods.len()
        );
        for p in &periods {
            let hours = p.duration_days().to::<Hour>();
            // First/last period may be truncated by the window boundary
            assert!(
                hours > 0.1 && hours < 18.0,
                "unreasonable above‑horizon duration: {} h",
                hours
            );
        }
    }

    #[test]
    fn never_visible_star() {
        let periods = find_star_above_periods(
            Degrees::new(0.0),
            Degrees::new(-80.0),
            greenwich(),
            period_7d(),
            Degrees::new(0.0),
        );
        assert!(periods.is_empty(), "Dec=−80° should never rise at 51°N");
    }

    #[test]
    fn above_plus_below_covers_full_period() {
        let site = greenwich();
        let period = period_7d();
        let ra = Degrees::new(101.287);
        let dec = Degrees::new(-16.716);

        let above = find_star_above_periods(ra, dec, site, period, Degrees::new(0.0));
        let below = find_star_below_periods(ra, dec, site, period, Degrees::new(0.0));

        let total_above: Days = above.iter().map(|p| p.duration_days()).sum();
        let total_below: Days = below.iter().map(|p| p.duration_days()).sum();
        assert!(
            (total_above + total_below - Days::new(7.0)).abs() < Days::new(0.01),
            "above + below should cover 7 days, got {}",
            total_above + total_below
        );
    }

    #[test]
    fn range_periods_sirius() {
        let periods = find_star_range_periods(
            Degrees::new(101.287),
            Degrees::new(-16.716),
            roque(),
            period_7d(),
            (Degrees::new(10.0), Degrees::new(30.0)),
        );
        assert!(!periods.is_empty(), "should find range periods for Sirius");
    }

    #[test]
    fn analytical_matches_scan() {
        let site = roque();
        let period = Period::new(
            ModifiedJulianDate::new(60000.0),
            ModifiedJulianDate::new(60003.0),
        );
        let ra = Degrees::new(101.287);
        let dec = Degrees::new(-16.716);
        let thr = Degrees::new(0.0);

        let analytical = find_star_above_periods(ra, dec, site, period, thr);
        let scan = find_star_above_periods_scan(ra, dec, site, period, thr);

        assert_eq!(
            analytical.len(),
            scan.len(),
            "analytical and scan should find same count: {} vs {}",
            analytical.len(),
            scan.len()
        );

        let tolerance = Minutes::new(1.0).to::<Day>();
        for (a, s) in analytical.iter().zip(scan.iter()) {
            assert!(
                (a.start - s.start).abs() < tolerance,
                "start times differ by {} d",
                (a.start - s.start).abs()
            );
            assert!(
                (a.end - s.end).abs() < tolerance,
                "end times differ by {} d",
                (a.end - s.end).abs()
            );
        }
    }
}