Skip to main content

sidereon_core/rinex_nav/
store.rs

1//! Broadcast-store selection and SPP source adapter.
2
3use crate::broadcast::{
4    satellite_state, satellite_state_cnav, satellite_state_cnav_unchecked,
5    satellite_state_unchecked, CnavRates, SatelliteState,
6};
7use crate::constants::{
8    BDS_EPOCH_MINUS_GPS_EPOCH_S, GPST_MINUS_BDT_S, GPS_EPOCH_TO_J2000_S, SECONDS_PER_WEEK,
9};
10use crate::error::{Error, Result as CoreResult};
11use crate::glonass;
12use crate::id::{GnssSatelliteId, GnssSystem};
13use crate::spp::EphemerisSource;
14
15use super::{
16    cnav_ura_nominal_m, is_beidou_geo, parse_glonass, parse_iono_corrections_checked,
17    parse_leap_seconds_checked, parse_nav, BroadcastGroupDelays, BroadcastIssue, BroadcastRecord,
18    CnavParameters, GlonassRecord, IonoCorrections, NavMessage, NavParseError, GLONASS_MAX_AGE_S,
19    MAX_EPHEMERIS_AGE_S,
20};
21
22/// Which navigation-message generation a store prefers when a GPS/QZSS
23/// satellite has both legacy and CNAV-family records.
24#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
25pub enum NavMessagePreference {
26    /// Prefer legacy records and use CNAV-family records only as fallback.
27    #[default]
28    PreferLegacy,
29    /// Prefer CNAV-family records and use legacy records as fallback.
30    PreferModern,
31}
32
33/// A queryable set of parsed broadcast records, usable as an SPP
34/// [`EphemerisSource`].
35///
36/// For a satellite and epoch it selects the record whose reference time `toe` is
37/// nearest in **continuous** GPS time (week number times the week length plus
38/// the seconds of week, not the seconds of week alone), and rejects the query as
39/// having no ephemeris if it falls outside that record's validity window - half
40/// the broadcast GPS curve-fit interval, or the coarse [`MAX_EPHEMERIS_AGE_S`]
41/// fallback for systems that do not broadcast one - so a stale or wrong-week
42/// product cannot silently produce a position.
43///
44/// [`from_nav`](BroadcastStore::from_nav) applies a default usability policy:
45/// healthy GPS LNAV, GPS/QZSS CNAV-family, Galileo I/NAV, BeiDou D1/D2, and
46/// GLONASS records are kept, with CNAV no-prediction URA records excluded.
47/// [`new`](BroadcastStore::new) keeps records verbatim for callers that want
48/// their own policy.
49pub struct BroadcastStore {
50    records: Vec<BroadcastRecord>,
51    glonass: Vec<GlonassRecord>,
52    leap_seconds: Option<f64>,
53    iono: IonoCorrections,
54    message_preference: NavMessagePreference,
55}
56
57impl BroadcastStore {
58    /// Build a store from already-parsed Keplerian records, verbatim (no policy
59    /// filter, no GLONASS records, no leap-second offset, and no ionosphere
60    /// coefficients; use [`from_nav`](Self::from_nav) to capture those).
61    pub fn new(records: Vec<BroadcastRecord>) -> CoreResult<Self> {
62        for record in &records {
63            validate_manual_record(record)?;
64        }
65        Ok(Self {
66            records,
67            glonass: Vec::new(),
68            leap_seconds: None,
69            iono: IonoCorrections::default(),
70            message_preference: NavMessagePreference::default(),
71        })
72    }
73
74    /// Parse a RINEX 3.x/4.xx navigation file and keep the records usable for
75    /// single-frequency positioning under the default policy: healthy GPS LNAV,
76    /// GPS/QZSS CNAV-family records with URA prediction, Galileo I/NAV, BeiDou
77    /// D1/D2, and GLONASS. The header's broadcast ionosphere coefficients (see
78    /// [`iono_corrections`](Self::iono_corrections)) and leap-second offset are
79    /// captured.
80    pub fn from_nav(text: &str) -> Result<Self, NavParseError> {
81        let records = parse_nav(text)?
82            .into_iter()
83            .filter(Self::is_default_usable)
84            .collect();
85        let glonass = parse_glonass(text)?
86            .into_iter()
87            .filter(|r| r.sv_health == 0.0)
88            .collect();
89        Ok(Self {
90            records,
91            glonass,
92            leap_seconds: parse_leap_seconds_checked(text)?,
93            iono: parse_iono_corrections_checked(text)?,
94            message_preference: NavMessagePreference::default(),
95        })
96    }
97
98    /// Set the GPS/QZSS legacy-vs-CNAV selection preference.
99    pub fn set_message_preference(&mut self, preference: NavMessagePreference) {
100        self.message_preference = preference;
101    }
102
103    /// The GPS/QZSS legacy-vs-CNAV selection preference.
104    pub const fn message_preference(&self) -> NavMessagePreference {
105        self.message_preference
106    }
107
108    /// The broadcast ionosphere coefficients parsed from the navigation header
109    /// or RINEX 4 body `> ION` frames (GPS `GPSA`/`GPSB`, BeiDou `BDSA`/`BDSB`,
110    /// and Galileo `GAL`). Empty for a store built with [`new`](Self::new).
111    pub fn iono_corrections(&self) -> IonoCorrections {
112        self.iono
113    }
114
115    /// The held GLONASS records.
116    pub fn glonass_records(&self) -> &[GlonassRecord] {
117        &self.glonass
118    }
119
120    /// The GLONASS FDMA frequency channels carried by the held broadcast
121    /// records, keyed by satellite PRN/slot (`[-7, 6]`).
122    ///
123    /// Lets a consumer source the per-satellite channel numbers - needed to
124    /// scale the GLONASS ionospheric delay per carrier - from the broadcast
125    /// navigation message when an observation file carries no `GLONASS SLOT /
126    /// FRQ #` header records. Each GLONASS satellite broadcasts one channel, so
127    /// the map has at most one entry per slot. The result keys/values match the
128    /// `glonass_slots` layout of [`crate::rinex_obs::ObsHeader`], so a consumer
129    /// can use this map directly where an OBS file would otherwise supply one.
130    pub fn glonass_frequency_channels(&self) -> std::collections::BTreeMap<u8, i8> {
131        self.glonass
132            .iter()
133            .map(|r| (r.satellite_id.prn, r.freq_channel as i8))
134            .collect()
135    }
136
137    /// The default usability policy: healthy and single-frequency-appropriate
138    /// messages. CNAV-family records with no URA prediction are excluded.
139    fn is_default_usable(r: &BroadcastRecord) -> bool {
140        r.sv_health == 0.0
141            && matches!(
142                r.message,
143                NavMessage::GpsLnav
144                    | NavMessage::GpsCnav
145                    | NavMessage::GpsCnav2
146                    | NavMessage::QzssCnav
147                    | NavMessage::QzssCnav2
148                    | NavMessage::GalileoInav
149                    | NavMessage::BeidouD1
150                    | NavMessage::BeidouD2
151            )
152            && (!r.message.is_cnav_family()
153                || r.cnav
154                    .map(|cnav| cnav_ura_nominal_m(cnav.ura_ed_index).is_some())
155                    .unwrap_or(false))
156    }
157
158    /// The held records.
159    pub fn records(&self) -> &[BroadcastRecord] {
160        &self.records
161    }
162
163    /// Select the broadcast record used for `sat` at `t_j2000_s`.
164    ///
165    /// The selection uses the same message preference, native-system time
166    /// mapping, and validity-window checks as
167    /// [`EphemerisSource::position_clock_at_j2000_s`]. Non-Keplerian systems
168    /// return `None`.
169    pub fn select_record_at(
170        &self,
171        sat: GnssSatelliteId,
172        t_j2000_s: f64,
173    ) -> Option<&BroadcastRecord> {
174        let (t_continuous_s, _) = query_continuous_time(sat, t_j2000_s)?;
175        self.select(sat, t_continuous_s)
176    }
177
178    /// Select the valid record for `sat` with a matching GPS issue byte at `t`.
179    pub fn select_by_iode_at(
180        &self,
181        sat: GnssSatelliteId,
182        iode: u8,
183        t_j2000_s: f64,
184    ) -> Option<&BroadcastRecord> {
185        let (t_continuous, _) = query_continuous_time(sat, t_j2000_s)?;
186        self.records
187            .iter()
188            .filter(|r| r.satellite_id == sat)
189            .filter(|r| r.issue_of_data.message == NavMessage::GpsLnav)
190            .filter(|r| r.issue_of_data.issue == u32::from(iode))
191            .filter(|r| (t_continuous - Self::toe_continuous_s(r)).abs() <= Self::half_window_s(r))
192            .min_by(|a, b| {
193                let da = (t_continuous - Self::toe_continuous_s(a)).abs();
194                let db = (t_continuous - Self::toe_continuous_s(b)).abs();
195                da.partial_cmp(&db).unwrap_or(core::cmp::Ordering::Equal)
196            })
197    }
198
199    /// Evaluate a matching issue-specific broadcast record at `t`.
200    pub fn state_by_iode_at(
201        &self,
202        sat: GnssSatelliteId,
203        iode: u8,
204        t_j2000_s: f64,
205    ) -> Option<([f64; 3], f64)> {
206        let (t_continuous, is_geo) = query_continuous_time(sat, t_j2000_s)?;
207        let rec = self.select_by_iode_at(sat, iode, t_j2000_s)?;
208        let sow = t_continuous.rem_euclid(SECONDS_PER_WEEK);
209        let state = evaluate_record_unchecked(rec, sow, is_geo);
210        let position = state.orbit.position().ok()?;
211        Some((position.as_array(), state.clock.dt_clock_total_s))
212    }
213
214    /// Keep only the records matching a predicate (e.g. a custom message/health
215    /// policy on a store built with [`new`](BroadcastStore::new)).
216    pub fn retain(&mut self, keep: impl FnMut(&BroadcastRecord) -> bool) {
217        self.records.retain(keep);
218    }
219
220    /// Continuous native broadcast time of a record's `toe`
221    /// (`week * 604800 + tow` in the record's own scale).
222    fn toe_continuous_s(rec: &BroadcastRecord) -> f64 {
223        f64::from(rec.toe.week) * SECONDS_PER_WEEK + rec.toe.tow_s
224    }
225
226    /// The half-validity window (seconds either side of `toe`) for a record: half
227    /// the broadcast GPS fit interval, or [`MAX_EPHEMERIS_AGE_S`] when no fit
228    /// interval is broadcast (Galileo, BeiDou).
229    fn half_window_s(rec: &BroadcastRecord) -> f64 {
230        match rec.fit_interval_s {
231            Some(fit) => fit / 2.0,
232            None => MAX_EPHEMERIS_AGE_S,
233        }
234    }
235
236    /// The record for `sat` whose native-system `toe` is nearest
237    /// `t_continuous_s` **among those whose validity window covers the
238    /// query** (see [`half_window_s`](Self::half_window_s)). Filtering by
239    /// validity before choosing the nearest means a query just past one record's
240    /// fit interval can still be served by a farther record whose own window is
241    /// wide enough, rather than being rejected outright.
242    fn select(&self, sat: GnssSatelliteId, t_continuous_s: f64) -> Option<&BroadcastRecord> {
243        let mut preferred = None;
244        let mut fallback = None;
245        for record in self.records.iter().filter(|r| r.satellite_id == sat) {
246            if (t_continuous_s - Self::toe_continuous_s(record)).abs() > Self::half_window_s(record)
247            {
248                continue;
249            }
250            if self.is_preferred_family(record) {
251                select_better_candidate(&mut preferred, record, t_continuous_s);
252            } else {
253                select_better_candidate(&mut fallback, record, t_continuous_s);
254            }
255        }
256        preferred.or(fallback)
257    }
258
259    fn is_preferred_family(&self, record: &BroadcastRecord) -> bool {
260        if !matches!(
261            record.satellite_id.system,
262            GnssSystem::Gps | GnssSystem::Qzss
263        ) {
264            return true;
265        }
266        match self.message_preference {
267            NavMessagePreference::PreferLegacy => !record.message.is_cnav_family(),
268            NavMessagePreference::PreferModern => record.message.is_cnav_family(),
269        }
270    }
271
272    /// Select the broadcast record matching a specific issue and message at the
273    /// query epoch.
274    pub fn select_by_issue_at(
275        &self,
276        sat: GnssSatelliteId,
277        issue: BroadcastIssue,
278        nav_message: NavMessage,
279        t_j2000_s: f64,
280    ) -> Option<&BroadcastRecord> {
281        if issue.message != nav_message {
282            return None;
283        }
284        let (t_continuous_s, _) = query_continuous_time(sat, t_j2000_s)?;
285        self.records
286            .iter()
287            .filter(|r| {
288                r.satellite_id == sat
289                    && r.message == nav_message
290                    && r.issue_of_data == issue
291                    && (t_continuous_s - Self::toe_continuous_s(r)).abs() <= Self::half_window_s(r)
292            })
293            .min_by(|a, b| {
294                let da = (t_continuous_s - Self::toe_continuous_s(a)).abs();
295                let db = (t_continuous_s - Self::toe_continuous_s(b)).abs();
296                da.partial_cmp(&db).unwrap_or(core::cmp::Ordering::Equal)
297            })
298    }
299
300    /// The GLONASS record for `sat` nearest the GPST-aligned query `t_j2000_s`
301    /// (within [`GLONASS_MAX_AGE_S`]), with `tk` = query − the record's reference
302    /// epoch in GPS time. Returns `None` if no leap-second offset was parsed (the
303    /// GLONASS UTC epoch then cannot be placed on the GPST timeline).
304    fn select_glonass(
305        &self,
306        sat: GnssSatelliteId,
307        t_j2000_s: f64,
308    ) -> Option<(&GlonassRecord, f64)> {
309        let leap = self.leap_seconds?;
310        let toe_gpst = |r: &GlonassRecord| r.toe_utc_j2000_s + leap;
311        let rec = self
312            .glonass
313            .iter()
314            .filter(|r| r.satellite_id == sat)
315            .min_by(|a, b| {
316                let da = (t_j2000_s - toe_gpst(a)).abs();
317                let db = (t_j2000_s - toe_gpst(b)).abs();
318                da.partial_cmp(&db).unwrap_or(core::cmp::Ordering::Equal)
319            })?;
320        let tk = t_j2000_s - toe_gpst(rec);
321        if tk.abs() <= GLONASS_MAX_AGE_S {
322            Some((rec, tk))
323        } else {
324            None
325        }
326    }
327}
328
329fn validate_manual_record(record: &BroadcastRecord) -> CoreResult<()> {
330    validate_finite(record.toe.tow_s, "record.toe.tow_s")?;
331    validate_finite(record.toc.tow_s, "record.toc.tow_s")?;
332    validate_finite(record.sv_health, "record.sv_health")?;
333    validate_finite(record.sv_accuracy_m, "record.sv_accuracy_m")?;
334    if let Some(fit) = record.fit_interval_s {
335        validate_finite(fit, "record.fit_interval_s")?;
336        if fit <= 0.0 {
337            return Err(invalid_input("record.fit_interval_s", "not positive"));
338        }
339    }
340    validate_group_delays(record.group_delays)?;
341    validate_cnav_presence(record)?;
342    if let Some(cnav) = record.cnav {
343        validate_cnav_parameters(cnav)?;
344    }
345
346    if let Some(cnav) = record.cnav {
347        satellite_state_cnav(
348            &record.elements,
349            &cnav_rates(cnav),
350            &record.clock,
351            &record.constants(),
352            record.elements.toe_sow,
353            record.broadcast_clock_group_delay_s(),
354        )
355        .map(|_| ())
356    } else {
357        satellite_state(
358            &record.elements,
359            &record.clock,
360            &record.constants(),
361            record.elements.toe_sow,
362            record.broadcast_clock_group_delay_s(),
363            is_beidou_geo(record.satellite_id),
364        )
365        .map(|_| ())
366    }
367}
368
369fn validate_group_delays(delays: BroadcastGroupDelays) -> CoreResult<()> {
370    for (field, value) in [
371        ("group_delays.gps_tgd_s", delays.gps_tgd_s),
372        (
373            "group_delays.galileo_bgd_e5a_e1_s",
374            delays.galileo_bgd_e5a_e1_s,
375        ),
376        (
377            "group_delays.galileo_bgd_e5b_e1_s",
378            delays.galileo_bgd_e5b_e1_s,
379        ),
380        ("group_delays.beidou_tgd1_s", delays.beidou_tgd1_s),
381        ("group_delays.beidou_tgd2_s", delays.beidou_tgd2_s),
382        ("group_delays.cnav_isc_l1ca_s", delays.cnav_isc_l1ca_s),
383        ("group_delays.cnav_isc_l2c_s", delays.cnav_isc_l2c_s),
384        ("group_delays.cnav_isc_l5i5_s", delays.cnav_isc_l5i5_s),
385        ("group_delays.cnav_isc_l5q5_s", delays.cnav_isc_l5q5_s),
386        ("group_delays.cnav_isc_l1cd_s", delays.cnav_isc_l1cd_s),
387        ("group_delays.cnav_isc_l1cp_s", delays.cnav_isc_l1cp_s),
388    ] {
389        if let Some(value) = value {
390            validate_finite(value, field)?;
391        }
392    }
393    Ok(())
394}
395
396fn validate_cnav_presence(record: &BroadcastRecord) -> CoreResult<()> {
397    if record.message.is_cnav_family() != record.cnav.is_some() {
398        return Err(invalid_input(
399            "record.cnav",
400            "must be present only for CNAV-family messages",
401        ));
402    }
403    Ok(())
404}
405
406fn validate_cnav_parameters(params: CnavParameters) -> CoreResult<()> {
407    validate_finite(params.adot_m_s, "record.cnav.adot_m_s")?;
408    validate_finite(
409        params.delta_n0_dot_rad_s2,
410        "record.cnav.delta_n0_dot_rad_s2",
411    )?;
412    validate_finite(params.top.tow_s, "record.cnav.top.tow_s")?;
413    validate_finite(
414        params.transmission_time_sow,
415        "record.cnav.transmission_time_sow",
416    )?;
417    if !(-16..=15).contains(&params.ura_ed_index) {
418        return Err(invalid_input("record.cnav.ura_ed_index", "out of range"));
419    }
420    if !(-16..=15).contains(&params.ura_ned0_index) {
421        return Err(invalid_input("record.cnav.ura_ned0_index", "out of range"));
422    }
423    if params.ura_ned1_index > 7 {
424        return Err(invalid_input("record.cnav.ura_ned1_index", "out of range"));
425    }
426    if params.ura_ned2_index > 7 {
427        return Err(invalid_input("record.cnav.ura_ned2_index", "out of range"));
428    }
429    Ok(())
430}
431
432fn cnav_rates(params: CnavParameters) -> CnavRates {
433    CnavRates {
434        adot_m_s: params.adot_m_s,
435        delta_n0_dot_rad_s2: params.delta_n0_dot_rad_s2,
436    }
437}
438
439fn evaluate_record_unchecked(rec: &BroadcastRecord, sow: f64, is_geo: bool) -> SatelliteState {
440    if let Some(cnav) = rec.cnav {
441        satellite_state_cnav_unchecked(
442            &rec.elements,
443            &cnav_rates(cnav),
444            &rec.clock,
445            &rec.constants(),
446            sow,
447            rec.broadcast_clock_group_delay_s(),
448        )
449    } else {
450        satellite_state_unchecked(
451            &rec.elements,
452            &rec.clock,
453            &rec.constants(),
454            sow,
455            rec.broadcast_clock_group_delay_s(),
456            is_geo,
457        )
458    }
459}
460
461fn select_better_candidate<'a>(
462    best: &mut Option<&'a BroadcastRecord>,
463    candidate: &'a BroadcastRecord,
464    t_continuous_s: f64,
465) {
466    let Some(current) = *best else {
467        *best = Some(candidate);
468        return;
469    };
470    if candidate_is_better(candidate, current, t_continuous_s) {
471        *best = Some(candidate);
472    }
473}
474
475fn candidate_is_better(
476    candidate: &BroadcastRecord,
477    current: &BroadcastRecord,
478    t_continuous_s: f64,
479) -> bool {
480    let da = (t_continuous_s - BroadcastStore::toe_continuous_s(candidate)).abs();
481    let db = (t_continuous_s - BroadcastStore::toe_continuous_s(current)).abs();
482    match da.partial_cmp(&db).unwrap_or(core::cmp::Ordering::Equal) {
483        core::cmp::Ordering::Less => true,
484        core::cmp::Ordering::Greater => false,
485        core::cmp::Ordering::Equal => {
486            cnav_tie_rank(candidate.message) < cnav_tie_rank(current.message)
487        }
488    }
489}
490
491const fn cnav_tie_rank(message: NavMessage) -> u8 {
492    match message {
493        NavMessage::GpsCnav | NavMessage::QzssCnav => 0,
494        NavMessage::GpsCnav2 | NavMessage::QzssCnav2 => 1,
495        _ => 0,
496    }
497}
498
499fn validate_finite(value: f64, field: &'static str) -> CoreResult<()> {
500    if value.is_finite() {
501        Ok(())
502    } else {
503        Err(invalid_input(field, "not finite"))
504    }
505}
506
507fn invalid_input(field: &'static str, reason: &'static str) -> Error {
508    Error::InvalidInput(format!("{field} {reason}"))
509}
510
511impl core::str::FromStr for BroadcastStore {
512    type Err = NavParseError;
513
514    fn from_str(s: &str) -> Result<Self, Self::Err> {
515        Self::from_nav(s)
516    }
517}
518
519impl EphemerisSource for BroadcastStore {
520    fn position_clock_at_j2000_s(
521        &self,
522        sat: GnssSatelliteId,
523        t_j2000_s: f64,
524    ) -> Option<([f64; 3], f64)> {
525        // GLONASS is not Keplerian: integrate its broadcast state vector with the
526        // RK4 propagator. Its reference epoch is UTC, mapped onto the GPST-aligned
527        // query via the parsed leap-second offset.
528        if sat.system == GnssSystem::Glonass {
529            let (rec, tk) = self.select_glonass(sat, t_j2000_s)?;
530            let state0 = [
531                rec.pos_m[0],
532                rec.pos_m[1],
533                rec.pos_m[2],
534                rec.vel_m_s[0],
535                rec.vel_m_s[1],
536                rec.vel_m_s[2],
537            ];
538            let state = glonass::propagate(state0, rec.acc_m_s2, tk).ok()?;
539            let clock = glonass::clock_offset_s(rec.clk_bias, rec.gamma_n, tk);
540            return Some(([state[0], state[1], state[2]], clock));
541        }
542
543        // Supported Keplerian systems only; a record from any other system (for
544        // example SBAS or NavIC) reports no ephemeris rather than being evaluated
545        // with the wrong model. (`from_nav` already restricts records, but `new`
546        // accepts arbitrary ones.)
547        // Map the receive instant (J2000, GPST-aligned) onto the satellite
548        // system's continuous time and seconds of week. BeiDou runs on BDT
549        // (= GPST - 14 s) with its week epoch 1356 weeks after the GPS epoch, and
550        // its geostationary satellites take the GEO orbit branch.
551        // `query_continuous_time` returns None for non-Keplerian systems.
552        let (t_continuous, is_geo) = query_continuous_time(sat, t_j2000_s)?;
553
554        let rec = self.select(sat, t_continuous)?;
555        let sow = t_continuous.rem_euclid(SECONDS_PER_WEEK);
556        let state = evaluate_record_unchecked(rec, sow, is_geo);
557        let position = state.orbit.position().ok()?;
558        Some((position.as_array(), state.clock.dt_clock_total_s))
559    }
560}
561
562fn query_continuous_time(sat: GnssSatelliteId, t_j2000_s: f64) -> Option<(f64, bool)> {
563    if !matches!(
564        sat.system,
565        GnssSystem::Gps | GnssSystem::Galileo | GnssSystem::BeiDou | GnssSystem::Qzss
566    ) {
567        return None;
568    }
569    let gpst_continuous = t_j2000_s + GPS_EPOCH_TO_J2000_S;
570    if sat.system == GnssSystem::BeiDou {
571        Some((
572            gpst_continuous - GPST_MINUS_BDT_S - BDS_EPOCH_MINUS_GPS_EPOCH_S,
573            is_beidou_geo(sat),
574        ))
575    } else {
576        Some((gpst_continuous, false))
577    }
578}