sidereon-core 0.22.0

Numerical astrodynamics propagation core plus the GNSS domain layer (SP3, broadcast ephemeris, multi-GNSS positioning, RTK/PPP, ionosphere/troposphere, DOP) behind a default-on gnss feature
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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
//! Single-point positioning and GNSS geometry diagnostics.

use std::collections::{BTreeMap, BTreeSet};
use std::fmt;

use crate::constants::C_M_S;
pub use crate::dop::{dop, Dop, DopError, LineOfSight};
use crate::ephemeris::{BroadcastEphemeris, Sp3};
use crate::id::GnssSystem;
pub use crate::quality::{
    spp_robust_fde_driver, FdeError, FdeOptions, FdeResult, FdeSppError, FdeSppOptions,
};
use crate::rinex::observations::{pseudoranges, ObsEpochTime, ObservationFile, SignalPolicy};
use crate::rtcm::{self, MsmKind};
pub use crate::spp::{
    residual_rms, solve, solve_broadcast, solve_doppler_velocity, solve_spp_batch_parallel,
    solve_spp_batch_serial, solve_with_doppler_velocity, solve_with_fallback, solve_with_policy,
    solve_with_solver, BroadcastReason, Corrections, DopplerObservation, DopplerVelocityInputs,
    EphemerisSource, FallbackError, FixSource, GalileoNequickCoeffs, KlobucharCoeffs, Observation,
    ReceiverSolution, RejectedSat, RejectionReason, RobustConfig, SolutionMetadata, SolveInputs,
    SolvePolicy, SolvePolicyError, SourcedSolution, SppDopplerSolution, SppError, SurfaceMet,
    DEFAULT_HUBER_K, DEFAULT_ROBUST_MAX_OUTER, DEFAULT_ROBUST_OUTER_TOL_M,
    DEFAULT_ROBUST_SCALE_FLOOR_M, ELEVATION_MASK_RAD, SIGMA0_M, TRANSMIT_TIME_ITERATIONS,
};
pub use crate::static_positioning::{
    solve_static, StaticClockBias, StaticCovariance, StaticEpoch, StaticEpochInfluence,
    StaticInfluenceStatus, StaticResidual, StaticSatelliteBatchInfluence, StaticSatelliteInfluence,
    StaticSolution, StaticSolutionMetadata, StaticSolveError, StaticSolveOptions,
};
pub use crate::static_reference_station::{
    solve_static_reference_station_rinex, StaticReferenceCarrierRinexOptions,
    StaticReferenceCarrierSolution, StaticReferenceCodeSolution, StaticReferenceEpochDiagnostic,
    StaticReferenceFixStatus, StaticReferenceModeError, StaticReferenceModeReport,
    StaticReferenceModeStatus, StaticReferenceStationCovariance, StaticReferenceStationError,
    StaticReferenceStationMode, StaticReferenceStationRinexOptions, StaticReferenceStationSolution,
};
use crate::{astro::time, Error as CoreError, GnssSatelliteId};

/// Role-oriented alias for a solved receiver state.
pub type Solution = ReceiverSolution;

/// Error type returned by [`solve`].
pub type Error = SppError;

/// Assembly-time error from building SPP inputs out of a parsed RINEX
/// observation file.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum RinexSppError {
    /// A RINEX observation helper rejected malformed or non-finite input.
    Observation(CoreError),
    /// No initial receiver position was supplied and the observation header did
    /// not carry `APPROX POSITION XYZ`.
    MissingApproxPosition,
}

impl fmt::Display for RinexSppError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Observation(error) => write!(f, "RINEX SPP observation assembly failed: {error}"),
            Self::MissingApproxPosition => {
                f.write_str("RINEX SPP assembly needs APPROX POSITION XYZ or an initial guess")
            }
        }
    }
}

impl std::error::Error for RinexSppError {}

impl From<CoreError> for RinexSppError {
    fn from(error: CoreError) -> Self {
        Self::Observation(error)
    }
}

/// Broadcast correction metadata used while converting RINEX observations into
/// SPP [`SolveInputs`].
///
/// A broadcast navigation product supplies ionosphere coefficients and GLONASS
/// FDMA channels. A precise SP3 product does not, so its default assembly
/// context is zero Klobuchar coefficients and no GLONASS channels. When solving
/// precise SP3 positions with broadcast atmosphere metadata, wrap the SP3 with
/// [`RinexSppSource::with_broadcast_context`].
#[derive(Debug, Clone, PartialEq)]
pub struct RinexSppBroadcastCorrections {
    /// GPS Klobuchar coefficients, also used as the fallback for systems
    /// without a dedicated correction set.
    pub klobuchar: KlobucharCoeffs,
    /// BeiDou-specific Klobuchar coefficients, when a NAV product provides
    /// `BDSA`/`BDSB`.
    pub beidou_klobuchar: Option<KlobucharCoeffs>,
    /// Galileo NeQuick-G coefficients, when a NAV product provides `GAL`.
    pub galileo_nequick: Option<GalileoNequickCoeffs>,
    /// GLONASS FDMA channel numbers keyed by GLONASS slot.
    pub glonass_channels: BTreeMap<u8, i8>,
}

impl Default for RinexSppBroadcastCorrections {
    fn default() -> Self {
        Self {
            klobuchar: zero_klobuchar(),
            beidou_klobuchar: None,
            galileo_nequick: None,
            glonass_channels: BTreeMap::new(),
        }
    }
}

/// Source of non-observation metadata needed during RINEX SPP assembly.
///
/// [`BroadcastEphemeris`] implements this trait with its parsed NAV ionosphere
/// coefficients and GLONASS FDMA channels. [`Sp3`] implements it with empty
/// broadcast metadata so precise-only callers can still assemble
/// troposphere-only or no-correction inputs.
pub trait RinexSppAssemblySource {
    /// Broadcast correction metadata available to RINEX SPP assembly.
    fn rinex_spp_broadcast_corrections(&self) -> RinexSppBroadcastCorrections;
}

impl RinexSppAssemblySource for BroadcastEphemeris {
    fn rinex_spp_broadcast_corrections(&self) -> RinexSppBroadcastCorrections {
        let iono = self.iono_corrections();
        let gps = iono.gps.map(klobuchar_from_alpha_beta);
        RinexSppBroadcastCorrections {
            klobuchar: gps.unwrap_or_else(zero_klobuchar),
            beidou_klobuchar: iono.beidou.map(klobuchar_from_alpha_beta),
            galileo_nequick: iono.galileo,
            glonass_channels: self.glonass_frequency_channels(),
        }
    }
}

impl RinexSppAssemblySource for Sp3 {
    fn rinex_spp_broadcast_corrections(&self) -> RinexSppBroadcastCorrections {
        RinexSppBroadcastCorrections::default()
    }
}

/// Delegating ephemeris source that lets a precise product solve with broadcast
/// NAV metadata during RINEX SPP assembly.
///
/// Use [`Self::with_broadcast_context`] for the common precise-SP3-plus-RINEX-NAV
/// path: satellite position and clocks come from `ephemeris`, while
/// ionosphere coefficients and GLONASS FDMA channels come from `broadcast`.
pub struct RinexSppSource<'a, E: EphemerisSource + ?Sized> {
    ephemeris: &'a E,
    broadcast: Option<&'a BroadcastEphemeris>,
}

impl<'a, E: EphemerisSource + ?Sized> RinexSppSource<'a, E> {
    /// Build a delegating source with no broadcast assembly context.
    #[must_use]
    pub const fn new(ephemeris: &'a E) -> Self {
        Self {
            ephemeris,
            broadcast: None,
        }
    }

    /// Build a delegating source whose ephemeris is used for the solve and whose
    /// broadcast product is used for RINEX assembly metadata.
    #[must_use]
    pub const fn with_broadcast_context(
        ephemeris: &'a E,
        broadcast: &'a BroadcastEphemeris,
    ) -> Self {
        Self {
            ephemeris,
            broadcast: Some(broadcast),
        }
    }

    /// The ephemeris source delegated to during the SPP solve.
    #[must_use]
    pub const fn ephemeris(&self) -> &'a E {
        self.ephemeris
    }

    /// Broadcast metadata source, when one was supplied.
    #[must_use]
    pub const fn broadcast_context(&self) -> Option<&'a BroadcastEphemeris> {
        self.broadcast
    }
}

impl<E: EphemerisSource + ?Sized> EphemerisSource for RinexSppSource<'_, E> {
    fn position_clock_at_j2000_s(
        &self,
        sat: GnssSatelliteId,
        t_j2000_s: f64,
    ) -> Option<([f64; 3], f64)> {
        self.ephemeris.position_clock_at_j2000_s(sat, t_j2000_s)
    }
}

impl<E: EphemerisSource + ?Sized> RinexSppAssemblySource for RinexSppSource<'_, E> {
    fn rinex_spp_broadcast_corrections(&self) -> RinexSppBroadcastCorrections {
        self.broadcast
            .map(RinexSppAssemblySource::rinex_spp_broadcast_corrections)
            .unwrap_or_default()
    }
}

/// Options for assembling RINEX observation epochs into SPP [`SolveInputs`].
#[derive(Debug, Clone, PartialEq)]
pub struct RinexSppOptions {
    /// Per-constellation pseudorange-code selection policy.
    pub signal_policy: SignalPolicy,
    /// Correction terms to request in each assembled solve.
    pub corrections: Corrections,
    /// Optional initial guess `[x_m, y_m, z_m, b_m]`. When absent, the RINEX
    /// header's `APPROX POSITION XYZ` provides the position and the clock seed
    /// is zero.
    pub initial_guess: Option<[f64; 4]>,
    /// Optional satellite allow-list. `None` keeps every satellite with a
    /// selected pseudorange.
    pub satellites: Option<BTreeSet<GnssSatelliteId>>,
    /// Surface meteorology for troposphere correction.
    pub met: SurfaceMet,
    /// Optional robust reweighting for every assembled epoch.
    pub robust: Option<RobustConfig>,
}

impl RinexSppOptions {
    /// Build options from an explicit signal policy.
    #[must_use]
    pub fn new(signal_policy: SignalPolicy) -> Self {
        Self {
            signal_policy,
            corrections: Corrections::IONO_TROPO,
            initial_guess: None,
            satellites: None,
            met: SurfaceMet::default(),
            robust: None,
        }
    }

    /// Build options using the default single-frequency signal policy for the
    /// observation file's RINEX version.
    pub fn default_for(obs: &ObservationFile) -> Result<Self, RinexSppError> {
        Ok(Self::new(SignalPolicy::default_for(obs.header().version)?))
    }

    /// Replace the correction request.
    #[must_use]
    pub const fn with_corrections(mut self, corrections: Corrections) -> Self {
        self.corrections = corrections;
        self
    }

    /// Replace the initial solve guess.
    #[must_use]
    pub const fn with_initial_guess(mut self, initial_guess: [f64; 4]) -> Self {
        self.initial_guess = Some(initial_guess);
        self
    }

    /// Restrict assembly to the supplied satellites.
    #[must_use]
    pub fn with_satellites<I>(mut self, satellites: I) -> Self
    where
        I: IntoIterator<Item = GnssSatelliteId>,
    {
        self.satellites = Some(satellites.into_iter().collect());
        self
    }

    /// Replace surface meteorology.
    #[must_use]
    pub const fn with_surface_met(mut self, met: SurfaceMet) -> Self {
        self.met = met;
        self
    }

    /// Replace robust-reweighting config.
    #[must_use]
    pub const fn with_robust(mut self, robust: Option<RobustConfig>) -> Self {
        self.robust = robust;
        self
    }
}

/// One assembled RINEX observation epoch and its SPP inputs.
#[derive(Debug, Clone)]
pub struct RinexSppEpochInputs {
    /// Index in [`ObservationFile::epochs`].
    pub epoch_index: usize,
    /// Civil epoch exactly as it appears in the RINEX observation file.
    pub epoch: ObsEpochTime,
    /// Fully assembled SPP inputs for this epoch.
    pub inputs: SolveInputs,
}

/// One RINEX observation epoch paired with its serial SPP solve result.
#[derive(Debug, Clone)]
pub struct RinexSppEpochSolution {
    /// Index in [`ObservationFile::epochs`].
    pub epoch_index: usize,
    /// Civil epoch exactly as it appears in the RINEX observation file.
    pub epoch: ObsEpochTime,
    /// Result from solving the assembled epoch.
    pub solution: Result<ReceiverSolution, SolvePolicyError>,
}

/// One set of assembled RTCM MSM observations and its SPP inputs.
#[derive(Debug, Clone)]
pub struct RtcmSppEpochInputs {
    /// Index in the ordered set of assembled RTCM observation epochs.
    pub epoch_index: usize,
    /// Civil epoch reconstructed from the stream conversion logic.
    pub epoch: ObsEpochTime,
    /// Fully assembled SPP inputs for this epoch.
    pub inputs: SolveInputs,
}

/// Convert RTCM MSM observation messages into SPP-ready epoch inputs.
///
/// Messages are grouped by `(system, epoch_time)` in stream order, so each
/// RTCM epoch produces one `RtcmSppEpochInputs` entry.
pub fn spp_inputs_from_rtcm_msm<S, F>(
    messages: &[rtcm::MsmMessage],
    source: &S,
    options: &RinexSppOptions,
    mut map_epoch: F,
) -> Result<Vec<RtcmSppEpochInputs>, RinexSppError>
where
    S: RinexSppAssemblySource + ?Sized,
    F: FnMut(GnssSystem, u32) -> Option<(f64, ObsEpochTime)>,
{
    if messages.is_empty() {
        return Ok(Vec::new());
    }

    let initial_guess = options.initial_guess.unwrap_or([0.0; 4]);
    let base_corrections = merged_broadcast_corrections_from_source(source);
    let mut groups = Vec::<(GnssSystem, u32, Vec<usize>)>::new();
    let mut group_index = BTreeMap::<(GnssSystem, u32), usize>::new();

    for (index, message) in messages.iter().enumerate() {
        let key = (message.system, message.header.epoch_time);
        let slot = if let Some(index) = group_index.get(&key) {
            *index
        } else {
            let slot = groups.len();
            groups.push((key.0, key.1, Vec::new()));
            group_index.insert(key, slot);
            slot
        };
        groups[slot].2.push(index);
    }

    let mut out = Vec::new();
    for (epoch_index, (system, epoch_time, group_indexes)) in groups.into_iter().enumerate() {
        let Some((t_rx_j2000_s, epoch)) = map_epoch(system, epoch_time) else {
            continue;
        };
        let Some(preferred_codes) = options.signal_policy.codes.get(&system) else {
            continue;
        };
        let preferred_codes = preferred_codes
            .iter()
            .map(String::as_str)
            .collect::<Vec<_>>();

        let Some(message_kind) = group_indexes
            .first()
            .and_then(|index| messages.get(*index))
            .map(|message| message.kind)
        else {
            continue;
        };

        let mut by_satellite = BTreeMap::<u8, Vec<&rtcm::MsmSignal>>::new();
        let mut satellite_cache = BTreeMap::<u8, rtcm::MsmSatellite>::new();
        for message in group_indexes
            .iter()
            .copied()
            .filter_map(|index| messages.get(index))
        {
            for satellite in &message.satellites {
                satellite_cache.insert(satellite.id, *satellite);
            }
            for signal in &message.signals {
                by_satellite
                    .entry(signal.satellite_id)
                    .or_default()
                    .push(signal);
            }
        }

        let mut observations = Vec::new();
        for (satellite_id, signals) in by_satellite {
            let Some(satellite) = satellite_cache.get(&satellite_id) else {
                continue;
            };
            let Some(pseudorange_m) = rtcm_msm_pseudorange_m(
                system,
                message_kind,
                *satellite,
                &signals,
                &preferred_codes,
            ) else {
                continue;
            };
            if let Ok(satellite_id) = GnssSatelliteId::new(system, satellite_id) {
                observations.push(Observation {
                    satellite_id,
                    pseudorange_m,
                });
            }
        }

        if observations.is_empty() {
            continue;
        }

        let t_rx_second_of_day_s =
            time::second_of_day(epoch.hour.into(), epoch.minute.into(), epoch.second);
        let day_of_year = time::day_of_year(
            epoch.year,
            i32::from(epoch.month),
            i32::from(epoch.day),
            epoch.hour.into(),
            epoch.minute.into(),
            epoch.second,
        );

        out.push(RtcmSppEpochInputs {
            epoch_index,
            epoch,
            inputs: SolveInputs {
                observations,
                t_rx_j2000_s,
                t_rx_second_of_day_s,
                day_of_year,
                initial_guess,
                corrections: options.corrections,
                klobuchar: base_corrections.klobuchar,
                beidou_klobuchar: base_corrections.beidou_klobuchar,
                galileo_nequick: base_corrections.galileo_nequick,
                sbas_iono: None,
                glonass_channels: base_corrections.glonass_channels.clone(),
                met: options.met,
                robust: options.robust,
            },
        });
    }

    Ok(out)
}

fn rtcm_msm_pseudorange_m(
    system: GnssSystem,
    kind: MsmKind,
    satellite: rtcm::MsmSatellite,
    signals: &[&rtcm::MsmSignal],
    preferred_codes: &[&str],
) -> Option<f64> {
    if satellite.rough_range_ms == 255 {
        return None;
    }
    let rough_ms =
        f64::from(satellite.rough_range_ms) + f64::from(satellite.rough_range_mod1) / 1024.0;

    if let Some(signal) = select_rtcm_signal(system, signals, preferred_codes) {
        let fine_ms = match kind {
            MsmKind::Msm4 => {
                if signal.fine_pseudorange == -16_384 {
                    f64::NAN
                } else {
                    f64::from(signal.fine_pseudorange) / 2_f64.powi(24)
                }
            }
            MsmKind::Msm7 => f64::from(signal.fine_pseudorange) / 2_f64.powi(29),
        };
        if fine_ms.is_finite() {
            return Some((rough_ms + fine_ms) * 1.0e-3 * C_M_S);
        }
    }

    None
}

fn select_rtcm_signal<'a>(
    system: GnssSystem,
    signals: &'a [&'a rtcm::MsmSignal],
    preferred_codes: &[&'a str],
) -> Option<&'a rtcm::MsmSignal> {
    if signals.is_empty() {
        return None;
    }

    if preferred_codes.is_empty() {
        return signals.iter().copied().next();
    }

    preferred_codes
        .iter()
        .find_map(|requested| {
            let normalized = requested
                .strip_prefix('C')
                .or_else(|| requested.strip_prefix('L'))
                .unwrap_or(requested);
            signals.iter().copied().find(|signal| {
                let Some(code) = rtcm::msm_signal_rinex_code(system, signal.signal_id) else {
                    return false;
                };
                code == *requested || code == normalized
            })
        })
        .or_else(|| signals.iter().copied().next())
}

/// Assemble every non-event RINEX observation epoch with at least one selected
/// pseudorange into SPP [`SolveInputs`].
///
/// The function preserves observation-file epoch order, skips RINEX event
/// epochs (`flag > 1`), selects one single-frequency pseudorange per satellite
/// under [`RinexSppOptions::signal_policy`], derives receive time from the RINEX
/// civil epoch, seeds the receiver from `APPROX POSITION XYZ` unless
/// `initial_guess` is supplied, and combines GLONASS channels from the assembly
/// source with any observation-header `GLONASS SLOT / FRQ #` entries. Observation
/// header channels take precedence.
pub fn spp_inputs_from_rinex_obs<S>(
    obs: &ObservationFile,
    source: &S,
    options: &RinexSppOptions,
) -> Result<Vec<RinexSppEpochInputs>, RinexSppError>
where
    S: RinexSppAssemblySource + ?Sized,
{
    let initial_guess = initial_guess(obs, options)?;
    let base_corrections = merged_broadcast_corrections(obs, source);
    let mut out = Vec::new();

    for (epoch_index, epoch) in obs.epochs().iter().enumerate() {
        if epoch.flag > 1 {
            continue;
        }
        let mut selected = pseudoranges(obs, epoch, &options.signal_policy)?;
        if let Some(allowed) = &options.satellites {
            selected.retain(|(sat, _)| allowed.contains(sat));
        }
        if selected.is_empty() {
            continue;
        }

        let epoch_context = epoch_time_context(epoch.epoch);
        let observations = selected
            .into_iter()
            .map(|(satellite_id, pseudorange_m)| Observation {
                satellite_id,
                pseudorange_m,
            })
            .collect();

        out.push(RinexSppEpochInputs {
            epoch_index,
            epoch: epoch.epoch,
            inputs: SolveInputs {
                observations,
                t_rx_j2000_s: epoch_context.t_rx_j2000_s,
                t_rx_second_of_day_s: epoch_context.t_rx_second_of_day_s,
                day_of_year: epoch_context.day_of_year,
                initial_guess,
                corrections: options.corrections,
                klobuchar: base_corrections.klobuchar,
                beidou_klobuchar: base_corrections.beidou_klobuchar,
                galileo_nequick: base_corrections.galileo_nequick,
                sbas_iono: None,
                glonass_channels: base_corrections.glonass_channels.clone(),
                met: options.met,
                robust: options.robust,
            },
        });
    }

    Ok(out)
}

/// Assemble RINEX SPP epochs and solve them serially against the same source.
///
/// The returned vector has one entry per assembled epoch, not one entry per raw
/// RINEX epoch. Per-epoch solve failures are retained in
/// [`RinexSppEpochSolution::solution`], matching [`solve_spp_batch_serial`].
pub fn solve_spp_from_rinex_obs<S>(
    source: &S,
    obs: &ObservationFile,
    options: &RinexSppOptions,
    with_geodetic: bool,
    policy: SolvePolicy,
) -> Result<Vec<RinexSppEpochSolution>, RinexSppError>
where
    S: EphemerisSource + RinexSppAssemblySource,
{
    let epochs = spp_inputs_from_rinex_obs(obs, source, options)?;
    let inputs = epochs
        .iter()
        .map(|epoch| epoch.inputs.clone())
        .collect::<Vec<_>>();
    let results = solve_spp_batch_serial(source, &inputs, with_geodetic, policy);
    Ok(epochs
        .into_iter()
        .zip(results)
        .map(|(epoch, solution)| RinexSppEpochSolution {
            epoch_index: epoch.epoch_index,
            epoch: epoch.epoch,
            solution,
        })
        .collect())
}

fn klobuchar_from_alpha_beta(value: crate::ephemeris::KlobucharAlphaBeta) -> KlobucharCoeffs {
    KlobucharCoeffs {
        alpha: value.alpha,
        beta: value.beta,
    }
}

const fn zero_klobuchar() -> KlobucharCoeffs {
    KlobucharCoeffs {
        alpha: [0.0; 4],
        beta: [0.0; 4],
    }
}

fn initial_guess(
    obs: &ObservationFile,
    options: &RinexSppOptions,
) -> Result<[f64; 4], RinexSppError> {
    if let Some(initial_guess) = options.initial_guess {
        return Ok(initial_guess);
    }
    let approx = obs
        .header()
        .approx_position_m
        .ok_or(RinexSppError::MissingApproxPosition)?;
    Ok([approx[0], approx[1], approx[2], 0.0])
}

fn merged_broadcast_corrections<S>(
    obs: &ObservationFile,
    source: &S,
) -> RinexSppBroadcastCorrections
where
    S: RinexSppAssemblySource + ?Sized,
{
    let mut corrections = source.rinex_spp_broadcast_corrections();
    corrections.glonass_channels.extend(
        obs.header()
            .glonass_slots
            .iter()
            .map(|(&slot, &channel)| (slot, channel)),
    );
    corrections
}

fn merged_broadcast_corrections_from_source<S>(source: &S) -> RinexSppBroadcastCorrections
where
    S: RinexSppAssemblySource + ?Sized,
{
    source.rinex_spp_broadcast_corrections()
}

struct EpochTimeContext {
    t_rx_j2000_s: f64,
    t_rx_second_of_day_s: f64,
    day_of_year: f64,
}

fn epoch_time_context(epoch: ObsEpochTime) -> EpochTimeContext {
    let year = epoch.year;
    let month = i32::from(epoch.month);
    let day = i32::from(epoch.day);
    let hour = i32::from(epoch.hour);
    let minute = i32::from(epoch.minute);
    EpochTimeContext {
        t_rx_j2000_s: time::j2000_seconds(year, month, day, hour, minute, epoch.second),
        t_rx_second_of_day_s: time::second_of_day(hour, minute, epoch.second),
        day_of_year: time::day_of_year(year, month, day, hour, minute, epoch.second),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::rinex_obs::SignalPolicy;
    use crate::rtcm::{MsmHeader, MsmKind, MsmMessage, MsmSatellite, MsmSignal};

    #[derive(Default)]
    struct NoCorrections;

    impl RinexSppAssemblySource for NoCorrections {
        fn rinex_spp_broadcast_corrections(&self) -> RinexSppBroadcastCorrections {
            RinexSppBroadcastCorrections::default()
        }
    }

    fn synthetic_rtcm_messages() -> Vec<MsmMessage> {
        vec![MsmMessage {
            message_number: 1074,
            system: GnssSystem::Gps,
            kind: MsmKind::Msm4,
            header: MsmHeader {
                reference_station_id: 0,
                epoch_time: 12_345,
                multiple_message: false,
                iods: 1,
                reserved: 0,
                clock_steering: 0,
                external_clock: 0,
                divergence_free_smoothing: false,
                smoothing_interval: 0,
            },
            satellites: vec![MsmSatellite {
                id: 1,
                rough_range_ms: 100,
                rough_range_mod1: 512,
                extended_info: None,
                rough_phase_range_rate_m_s: None,
            }],
            signals: vec![
                MsmSignal {
                    satellite_id: 1,
                    signal_id: 1,
                    fine_pseudorange: 1 << 24,
                    lock_time_indicator: 0,
                    half_cycle_ambiguity: false,
                    cnr: 0,
                    fine_phase_range: 0,
                    fine_phase_range_rate: None,
                },
                MsmSignal {
                    satellite_id: 1,
                    signal_id: 2,
                    fine_pseudorange: 0,
                    lock_time_indicator: 0,
                    half_cycle_ambiguity: false,
                    cnr: 0,
                    fine_phase_range: 0,
                    fine_phase_range_rate: None,
                },
            ],
        }]
    }

    #[test]
    fn rtcm_msm_helper_assembles_single_epoch_with_signal_selection() {
        let messages = synthetic_rtcm_messages();
        let options = RinexSppOptions::new(SignalPolicy::default_for(3.03).expect("policy"));
        let inputs =
            spp_inputs_from_rtcm_msm(&messages, &NoCorrections, &options, |_system, _raw| {
                Some((
                    1_234_567.0,
                    ObsEpochTime {
                        year: 2026,
                        month: 7,
                        day: 7,
                        hour: 0,
                        minute: 0,
                        second: 0.0,
                    },
                ))
            })
            .expect("convert");

        assert_eq!(inputs.len(), 1);
        let epoch = &inputs[0];
        assert_eq!(epoch.inputs.observations.len(), 1);
        assert_eq!(epoch.inputs.observations[0].satellite_id.to_string(), "G01");
        assert_eq!(
            epoch.inputs.observations[0].pseudorange_m as i64,
            30_129_142
        );
    }
}