Skip to main content

sidereon_core/sp3/
mod.rs

1//! SP3-c / SP3-d precise-ephemeris parser.
2//!
3//! Parses the IGS SP3 precise orbit/clock format, both **SP3-c** and **SP3-d**
4//! (Hilla 2016), into a typed [`Sp3`] product. The parser is multi-GNSS,
5//! handles position/clock records plus optional velocity records,
6//! missing-value sentinels, predicted / clock-event / maneuver flags, and a
7//! system-aware [`GnssSatelliteId`]; the product's time system is read from the
8//! header.
9//!
10//! # Build vs adopt
11//!
12//! The spec permits using the `sp3` crate (MPL-2.0) as a deterministic byte
13//! reader, OR hand-rolling the record parsing. **This module hand-rolls it**,
14//! deliberately:
15//!
16//! - The `refs/sp3` crate hard-depends on `hifitime` for its `Epoch`,
17//!   `TimeScale`, and `Duration`, and on `flate2`. `sidereon-core` models time
18//!   with the **core crate's own** [`Instant`] / [`TimeScale`]
19//!   family, which is hifitime-free; adopting the `sp3` crate would
20//!   invert that and pull a parallel time stack into the GNSS layer.
21//! - The `sp3` crate also carries its own `SV` / `Constellation` identifiers
22//!   that duplicate this crate's [`GnssSatelliteId`] / [`GnssSystem`].
23//! - The SP3 record grammar is small, fixed-column, and fully specified, so a
24//!   byte reader is low-risk. (Note: the `refs/sp3` velocity parser at
25//!   `parsing.rs:241-245` has an axis bug - it reuses the Y component for X;
26//!   this module reads each axis independently and is unit-tested for it.)
27//!
28//! Parsing only is adopted-grade work; it is **not** a contested float recipe.
29//! The interpolation that consumes this product is built
30//! separately to match the `scipy.interpolate` reference and is out of scope
31//! for this module.
32//!
33//! # Units
34//!
35//! SP3 stores positions in **kilometers** and clock offsets in **microseconds**
36//! (velocities in dm/s, clock-rate in 1e-4 us/s). This parser converts at parse
37//! time to the crate's internal SI base units - positions in **meters**
38//! (`km * 1000.0`), clocks in **seconds** (`us * 1e-6`), velocities in **m/s**
39//! (`(dm/s) * 1e-1`), clock-rate in **s/s** (`(1e-4 us/s) * 1e-10`). Each scale
40//! factor is applied as a single multiply so the operation order is fixed for
41//! the clock-unit-conversion golden test.
42//!
43//! # Frames
44//!
45//! Positions/velocities are returned as frame-tagged [`ItrfPositionM`] /
46//! [`ItrfVelocityMS`], never a bare `position_m`.
47
48use std::collections::BTreeMap;
49
50use crate::astro::time::civil::split_julian_date;
51use crate::astro::time::model::{Instant, InstantRepr, JulianDateSplit, TimeScale};
52
53use crate::constants::{KM_TO_M, US_TO_S};
54use crate::format::columns::{
55    char_at, raw_field as field, raw_field_from as field_from, strict_f64,
56};
57use crate::format::{Diagnostics, RecordRef, Skip, SkipReason};
58use crate::frame::{ItrfPositionM, ItrfVelocityMS};
59use crate::id::{is_valid_prn, GnssSatelliteId, GnssSystem};
60use crate::validate;
61use crate::{Error, Result};
62
63/// SP3 missing/bad position component sentinel, in kilometers.
64///
65/// SP3 writes a satellite with no usable orbit as a position record of exactly
66/// `0.000000 0.000000 0.000000`. We treat an all-zero position as "missing"
67/// (matching the `refs/sp3` validity guard at `parsing.rs:186`): a satellite is
68/// never legitimately at the geocenter.
69const MISSING_POSITION_KM: f64 = 0.0;
70/// SP3 missing velocity component sentinel, in decimeters per second.
71///
72/// Velocity products still carry a `V` record for each `P` record. When no
73/// velocity estimate exists, the record uses the all-zero vector sentinel rather
74/// than being omitted; do not surface that as a fabricated stationary satellite.
75const MISSING_VELOCITY_DM_S: f64 = 0.0;
76
77/// SP3 bad-clock sentinel, in microseconds: `999999.999999`.
78///
79/// A clock value at or above this magnitude means "no clock estimate"; it is
80/// surfaced as `clock_s = None`, not converted.
81const BAD_CLOCK_US: f64 = 999_999.999_999;
82
83/// SP3 velocity records are in decimeters per second; dm/s -> m/s is `* 0.1`.
84const DM_S_TO_M_S: f64 = 1.0e-1;
85/// SP3 clock-rate is in 1e-4 microseconds/second; -> s/s is `* 1e-10`.
86const CLOCK_RATE_TO_S_PER_S: f64 = 1.0e-10;
87
88/// SP3 format version.
89#[derive(Debug, Clone, Copy, PartialEq, Eq)]
90pub enum Sp3Version {
91    /// SP3-a (legacy, GPS-only).
92    A,
93    /// SP3-b.
94    B,
95    /// SP3-c.
96    C,
97    /// SP3-d (multi-GNSS, Hilla 2016).
98    D,
99}
100
101impl Sp3Version {
102    fn from_char(c: char) -> Result<Self> {
103        match c {
104            'a' | 'A' => Ok(Sp3Version::A),
105            'b' | 'B' => Ok(Sp3Version::B),
106            'c' | 'C' => Ok(Sp3Version::C),
107            'd' | 'D' => Ok(Sp3Version::D),
108            other => Err(Error::Parse(format!("unknown SP3 version '{other}'"))),
109        }
110    }
111}
112
113/// What kind of records the file carries.
114#[derive(Debug, Clone, Copy, PartialEq, Eq)]
115pub enum Sp3DataType {
116    /// Position + clock records only (`#?P...`).
117    Position,
118    /// Position + velocity (+ clock + clock-rate) records (`#?V...`).
119    Velocity,
120}
121
122impl Sp3DataType {
123    fn from_char(c: char) -> Result<Self> {
124        match c {
125            'P' => Ok(Sp3DataType::Position),
126            'V' => Ok(Sp3DataType::Velocity),
127            other => Err(Error::Parse(format!("unknown SP3 data type '{other}'"))),
128        }
129    }
130}
131
132/// SP3 time-system labels from the `%c` descriptor.
133///
134/// The core [`TimeScale`] model does not distinguish every SP3 label as its own
135/// global scale. Keep the exact SP3 label here so products using GLONASS, QZSS,
136/// or IRNSS time are accepted and can be serialized without being silently
137/// relabeled.
138#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
139pub enum Sp3TimeSystem {
140    /// GPS time (`GPS`).
141    Gps,
142    /// GLONASS UTC time system (`GLO`).
143    Glonass,
144    /// Galileo system time (`GAL`).
145    Galileo,
146    /// International Atomic Time (`TAI`).
147    Tai,
148    /// Coordinated Universal Time (`UTC`).
149    Utc,
150    /// QZSS time (`QZS`).
151    Qzss,
152    /// BeiDou time (`BDT`).
153    Beidou,
154    /// IRNSS / NavIC time (`IRN`).
155    Irnss,
156}
157
158impl Sp3TimeSystem {
159    /// Canonical three-character SP3 label.
160    pub fn label(self) -> &'static str {
161        match self {
162            Sp3TimeSystem::Gps => "GPS",
163            Sp3TimeSystem::Glonass => "GLO",
164            Sp3TimeSystem::Galileo => "GAL",
165            Sp3TimeSystem::Tai => "TAI",
166            Sp3TimeSystem::Utc => "UTC",
167            Sp3TimeSystem::Qzss => "QZS",
168            Sp3TimeSystem::Beidou => "BDT",
169            Sp3TimeSystem::Irnss => "IRN",
170        }
171    }
172
173    /// Core time scale used to tag parsed [`Instant`] values.
174    ///
175    /// For labels the core model has exactly, this is the direct equivalent. For
176    /// SP3-only labels, the exact product label remains available through
177    /// [`Sp3Header::time_system`], and this value preserves the existing
178    /// interpolation-axis API until the global time model grows those scales.
179    pub fn time_scale(self) -> TimeScale {
180        match self {
181            Sp3TimeSystem::Gps | Sp3TimeSystem::Irnss => TimeScale::Gpst,
182            // QZSST is the exact core scale for the SP3 "QZS" label (nominally
183            // synchronous with GPST); IRNSS has no distinct core scale yet.
184            Sp3TimeSystem::Qzss => TimeScale::Qzsst,
185            Sp3TimeSystem::Glonass | Sp3TimeSystem::Utc => TimeScale::Utc,
186            Sp3TimeSystem::Galileo => TimeScale::Gst,
187            Sp3TimeSystem::Tai => TimeScale::Tai,
188            Sp3TimeSystem::Beidou => TimeScale::Bdt,
189        }
190    }
191
192    fn civil_second_policy(self) -> validate::CivilSecondPolicy {
193        match self {
194            Sp3TimeSystem::Glonass | Sp3TimeSystem::Utc => validate::CivilSecondPolicy::UtcLike,
195            Sp3TimeSystem::Gps
196            | Sp3TimeSystem::Galileo
197            | Sp3TimeSystem::Tai
198            | Sp3TimeSystem::Qzss
199            | Sp3TimeSystem::Beidou
200            | Sp3TimeSystem::Irnss => validate::CivilSecondPolicy::Continuous,
201        }
202    }
203}
204
205/// Per-record quality / status flags (SP3-c columns 75-80, SP3-d same layout).
206///
207/// All four flags are independent and any combination may appear (e.g. a
208/// predicted orbit during a maneuver). They are surfaced verbatim from the
209/// record and never alter the parsed numbers.
210#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
211pub struct Sp3Flags {
212    /// `E` in the clock-event column: a clock discontinuity occurred near this
213    /// epoch; clock interpolation across it is unsafe.
214    pub clock_event: bool,
215    /// `P` in the clock-prediction column: the clock is predicted, not fitted.
216    pub clock_predicted: bool,
217    /// `M` in the maneuver column: the satellite was being maneuvered; the
218    /// state is not suitable for precise navigation.
219    pub maneuver: bool,
220    /// `P` in the orbit-prediction column: the orbit is predicted, not fitted.
221    pub orbit_predicted: bool,
222}
223
224/// A single satellite state at one SP3 epoch.
225///
226/// This is the spec's `Sp3State { position: ItrfPositionM, clock_s, velocity?,
227/// clock_rate?, flags }`. The frame/units are encoded in the
228/// member types; missing optional values are `None` rather than sentinels.
229#[derive(Debug, Clone, Copy, PartialEq)]
230pub struct Sp3State {
231    /// Satellite position in the ITRF/IGS ECEF frame, meters.
232    pub position: ItrfPositionM,
233    /// Satellite clock offset in **seconds** (`None` if the bad-clock sentinel
234    /// `999999.999999` us was recorded).
235    pub clock_s: Option<f64>,
236    /// Satellite velocity in the ITRF/IGS ECEF frame, m/s (present only for
237    /// velocity products).
238    pub velocity: Option<ItrfVelocityMS>,
239    /// Satellite clock rate in **seconds per second** (present only for
240    /// velocity products that carry a clock-rate field).
241    pub clock_rate_s_s: Option<f64>,
242    /// Per-record status flags.
243    pub flags: Sp3Flags,
244}
245
246/// Parsed SP3 header.
247#[derive(Debug, Clone, PartialEq)]
248pub struct Sp3Header {
249    /// SP3 format version (`a`/`b`/`c`/`d`).
250    pub version: Sp3Version,
251    /// Whether the file carries velocity records.
252    pub data_type: Sp3DataType,
253    /// Number of parsed epochs in the canonical product.
254    pub num_epochs: u64,
255    /// Coordinate-system / IGS-realization label (e.g. `IGS14`, `ITRF2`).
256    pub coordinate_system: String,
257    /// Orbit-type label (e.g. `FIT`, `BHN`).
258    pub orbit_type: String,
259    /// Producing agency.
260    pub agency: String,
261    /// GNSS week number (in the file's time system).
262    pub gnss_week: u32,
263    /// Seconds of week of the first epoch.
264    pub seconds_of_week: f64,
265    /// Nominal epoch spacing in seconds.
266    pub epoch_interval_s: f64,
267    /// Modified Julian Day of the first epoch (integer part).
268    pub mjd: u32,
269    /// Fractional day of the first epoch.
270    pub mjd_fraction: f64,
271    /// Time system label the epochs are expressed in. For SP3-b/c/d this is read
272    /// strictly from the first `%c` descriptor (a missing/short/blank descriptor
273    /// is a parse error, never a silent GPST default); SP3-a is implicitly GPST.
274    pub time_system: Sp3TimeSystem,
275    /// Core [`TimeScale`] used to tag parsed [`Instant`] values. See
276    /// [`Sp3Header::time_system`] for the exact SP3 label when the product uses
277    /// a standard SP3 time system that is not modeled as a distinct core scale.
278    pub time_scale: TimeScale,
279    /// The satellite list declared in the `+` header lines.
280    pub satellites: Vec<GnssSatelliteId>,
281    /// Per-satellite accuracy exponent codes from the `++` header lines,
282    /// index-aligned with [`Sp3Header::satellites`].
283    pub satellite_accuracy_codes: Vec<u16>,
284}
285
286/// A parsed SP3 precise-ephemeris product.
287///
288/// Construct with [`Sp3::parse`]. Epochs are stored in ascending order; each
289/// epoch maps satellite -> [`Sp3State`]. Per-satellite/per-epoch access is via
290/// [`Sp3::state`]; arbitrary-epoch interpolation is built separately to match
291/// the parity reference and is not part of this parser.
292#[derive(Debug, Clone, PartialEq)]
293pub struct Sp3 {
294    /// The parsed header.
295    pub header: Sp3Header,
296    /// Epochs in ascending time order, tagged with the header time scale.
297    pub epochs: Vec<Instant>,
298    /// `epoch_index -> (satellite -> state)`. Parallel to [`Sp3::epochs`].
299    states: Vec<BTreeMap<GnssSatelliteId, Sp3State>>,
300    /// `epoch_index -> (satellite -> native-unit node)`. Parallel to
301    /// [`Sp3::epochs`]; populated **only** from genuine position records. The
302    /// interpolator fits its spline over these (km/us straight from the ASCII,
303    /// exactly as the `scipy`/`gnssanalysis` reference does); reconstructing km
304    /// from the public meters (`km->m->km`) drifts up to 1 ULP and breaks the
305    /// 0-ULP parity. See `sp3/interp.rs`.
306    interp_raw: Vec<BTreeMap<GnssSatelliteId, RawNode>>,
307    /// Free-form `/*` comment lines (notice retained for provenance).
308    pub comments: Vec<String>,
309    /// Count of entries skipped because their satellite token did not parse to a
310    /// representable [`GnssSatelliteId`] (e.g. an extended GLONASS slot like `R28`
311    /// beyond the engine's PRN cap): position/velocity records, plus `+`-header
312    /// satellite declarations. Lets callers tell a clean file
313    /// (`skipped_records == 0`) apart from one carrying unsupported satellites,
314    /// without aborting the whole parse on one such entry. Mirrors
315    /// [`crate::astro::sgp4::TleFile::skipped`].
316    pub skipped_records: usize,
317}
318
319/// Native-unit interpolation node: the file's own km / microseconds, kept
320/// verbatim from the ASCII so the spline fit is bit-identical to the reference.
321/// Private - the public surface is meters/seconds via [`Sp3State`].
322#[derive(Debug, Clone, Copy, PartialEq)]
323struct RawNode {
324    /// ECEF position in native SP3 kilometers (X/Y/Z), exact ASCII->f64.
325    km: [f64; 3],
326    /// Clock offset in native SP3 microseconds (`None` for the bad-clock
327    /// sentinel), exact ASCII->f64.
328    clock_us: Option<f64>,
329    /// Whether this epoch carried the clock-event (`E`) flag (clock-arc split).
330    clock_event: bool,
331}
332
333impl Sp3 {
334    /// Parse an SP3-c or SP3-d byte buffer into a typed product.
335    ///
336    /// `bytes` is the full file content (already decompressed; this crate does
337    /// not do gzip - that is a caller-layer I/O concern). Returns
338    /// [`Error::Parse`] with a human-readable reason on malformed input.
339    pub fn parse(bytes: &[u8]) -> Result<Self> {
340        let text = std::str::from_utf8(bytes)
341            .map_err(|e| Error::Parse(format!("SP3 is not valid UTF-8: {e}")))?;
342        Self::parse_str(text)
343    }
344
345    /// Parse from a `&str` (the UTF-8 fast path used by [`Sp3::parse`]).
346    pub fn parse_str(text: &str) -> Result<Self> {
347        if !text.is_ascii() {
348            return Err(Error::Parse("SP3 product text must be ASCII".into()));
349        }
350        let mut parser = Parser::new();
351        for (index, raw) in text.lines().enumerate() {
352            parser.feed(raw, index + 1)?;
353        }
354        parser.finish()
355    }
356
357    /// The satellites present in this product (from the header satellite list).
358    pub fn satellites(&self) -> &[GnssSatelliteId] {
359        &self.header.satellites
360    }
361
362    /// Number of parsed epochs.
363    pub fn epoch_count(&self) -> usize {
364        self.epochs.len()
365    }
366
367    /// The state of `sat` at the parsed epoch with index `epoch_index`.
368    ///
369    /// Returns [`Error::EpochOutOfRange`] if the index is past the end, or
370    /// [`Error::UnknownSatellite`] if the satellite has no record at that epoch.
371    pub fn state(&self, sat: GnssSatelliteId, epoch_index: usize) -> Result<Sp3State> {
372        let per_epoch = self.states.get(epoch_index).ok_or(Error::EpochOutOfRange)?;
373        per_epoch
374            .get(&sat)
375            .copied()
376            .ok_or(Error::UnknownSatellite(sat))
377    }
378
379    /// All `(satellite, state)` pairs recorded at `epoch_index`, in ascending
380    /// satellite order.
381    pub fn states_at(&self, epoch_index: usize) -> Result<&BTreeMap<GnssSatelliteId, Sp3State>> {
382        self.states.get(epoch_index).ok_or(Error::EpochOutOfRange)
383    }
384}
385
386impl core::str::FromStr for Sp3 {
387    type Err = Error;
388
389    fn from_str(s: &str) -> Result<Self> {
390        Self::parse_str(s)
391    }
392}
393
394#[cfg(test)]
395impl Sp3 {}
396
397/// Parse an SP3 time-system label.
398///
399/// SP3-c/-d encode the time system in the `%c` descriptor line (chars 9-12).
400/// SP3-a is implicitly GPST. Unknown labels error rather than silently
401/// defaulting, so a parity pipeline never mis-attributes an epoch's scale.
402fn time_system_from_label(label: &str) -> Result<Sp3TimeSystem> {
403    match label.trim() {
404        "GPS" => Ok(Sp3TimeSystem::Gps),
405        "GLO" => Ok(Sp3TimeSystem::Glonass),
406        "GAL" => Ok(Sp3TimeSystem::Galileo),
407        "TAI" => Ok(Sp3TimeSystem::Tai),
408        "UTC" => Ok(Sp3TimeSystem::Utc),
409        "QZS" => Ok(Sp3TimeSystem::Qzss),
410        "BDT" | "BDS" => Ok(Sp3TimeSystem::Beidou),
411        "IRN" => Ok(Sp3TimeSystem::Irnss),
412        trimmed => Err(Error::Parse(format!(
413            "unsupported SP3 time system '{trimmed}'"
414        ))),
415    }
416}
417
418/// Compute the integer-day / fraction split Julian date from a Gregorian UTC-ish
419/// civil epoch, with the day fraction carried separately (Skyfield split
420/// convention, matching [`JulianDateSplit`]).
421///
422/// SP3 epoch lines are civil dates in the file's *own* time system; we keep
423/// them in that scale (no leap-second shifting here - that is a conversion
424/// concern handled by the core `scales` machinery, not the parser). The
425/// algorithm is the standard Fliegel-Van Flandern Gregorian-to-JDN, then the
426/// time-of-day fraction. JDN is computed in integer arithmetic so the whole-day
427/// boundary is exact; only the sub-day fraction is floating point.
428fn civil_to_julian_split(civil: validate::ValidCivil) -> Result<JulianDateSplit> {
429    // Canonical civil-to-split conversion: the integer JDN places the `*.5`
430    // civil-midnight boundary and the within-day clock fields become the
431    // fraction. SP3 epochs are civil days in the file's own scale (no leap
432    // second). The carry below is retained for the rare epoch whose seconds
433    // overflow a day.
434    let (mut jd_whole, mut fraction) = split_julian_date(
435        civil.year as i32,
436        civil.month as i32,
437        civil.day as i32,
438        civil.hour as i32,
439        civil.minute as i32,
440        civil.second,
441    );
442    if fraction > 1.0 {
443        let carry = fraction.floor();
444        jd_whole += carry;
445        fraction -= carry;
446    }
447    JulianDateSplit::new(jd_whole, fraction)
448        .map_err(|error| Error::Parse(format!("invalid SP3 epoch Julian date: {error}")))
449}
450
451/// Incremental line-driven SP3 parser state machine.
452struct Parser {
453    version: Option<Sp3Version>,
454    data_type: Option<Sp3DataType>,
455    num_epochs: u64,
456    coordinate_system: String,
457    orbit_type: String,
458    agency: String,
459    gnss_week: u32,
460    seconds_of_week: f64,
461    epoch_interval_s: f64,
462    mjd: u32,
463    mjd_fraction: f64,
464    time_system: Option<Sp3TimeSystem>,
465    /// `+`-line declared satellites, in file order.
466    sat_list: Vec<GnssSatelliteId>,
467    /// `++`-line per-satellite accuracy codes, in satellite-list order.
468    sat_accuracy_codes: Vec<u16>,
469    /// Number of real (non-padding) `+`-line satellite slots seen, including any
470    /// dropped because their token was unrepresentable. The `++` accuracy codes
471    /// are positionally aligned with these declaration slots, so this is the axis
472    /// the accuracy parser walks (not the filtered [`Self::sat_list`]).
473    declared_sat_slots: usize,
474    /// Declaration-slot indices (into the `declared_sat_slots` axis) whose token
475    /// was unrepresentable and dropped from [`Self::sat_list`]. Their `++`
476    /// accuracy columns must be skipped so the surviving satellites keep their own
477    /// codes. Empty for every well-formed file, making the accuracy parse a no-op
478    /// realignment in the common case.
479    dropped_sat_slots: Vec<usize>,
480    /// Cursor along the declaration-slot axis consumed by the `++` accuracy
481    /// parser across one or more `++` lines.
482    accuracy_slot_cursor: usize,
483    /// `%c` descriptor lines seen so far (the first carries the time system).
484    pc_count: u32,
485    /// Header line 1 parsed?
486    have_line1: bool,
487    /// Header line 2 parsed?
488    have_line2: bool,
489    /// Epoch currently being filled.
490    current_epoch: Option<Instant>,
491    epochs: Vec<Instant>,
492    states: Vec<BTreeMap<GnssSatelliteId, Sp3State>>,
493    interp_raw: Vec<BTreeMap<GnssSatelliteId, RawNode>>,
494    comments: Vec<String>,
495    diagnostics: Diagnostics,
496    done: bool,
497}
498
499impl Parser {
500    fn new() -> Self {
501        Self {
502            version: None,
503            data_type: None,
504            num_epochs: 0,
505            coordinate_system: String::new(),
506            orbit_type: String::new(),
507            agency: String::new(),
508            gnss_week: 0,
509            seconds_of_week: 0.0,
510            epoch_interval_s: 0.0,
511            mjd: 0,
512            mjd_fraction: 0.0,
513            time_system: None,
514            sat_list: Vec::new(),
515            sat_accuracy_codes: Vec::new(),
516            declared_sat_slots: 0,
517            dropped_sat_slots: Vec::new(),
518            accuracy_slot_cursor: 0,
519            pc_count: 0,
520            have_line1: false,
521            have_line2: false,
522            current_epoch: None,
523            epochs: Vec::new(),
524            states: Vec::new(),
525            interp_raw: Vec::new(),
526            comments: Vec::new(),
527            diagnostics: Diagnostics::new(),
528            done: false,
529        }
530    }
531
532    fn feed(&mut self, raw: &str, line_number: usize) -> Result<()> {
533        if self.done {
534            return Ok(());
535        }
536        // SP3 is fixed-column ASCII; trim only the trailing CR / newline noise,
537        // never leading spaces (columns are significant).
538        let line = raw.trim_end_matches(['\r', '\n']);
539
540        if line == "EOF" {
541            self.done = true;
542            return Ok(());
543        }
544        if line.starts_with("/*") {
545            // Comment line; columns 4.. are the text.
546            if line.len() > 3 {
547                self.comments.push(line[3..].trim_end().to_string());
548            } else {
549                self.comments.push(String::new());
550            }
551            return Ok(());
552        }
553        // Header line 2 (`##`) must be tested before line 1 (`#`).
554        if line.starts_with("##") {
555            self.parse_line2(line)?;
556            return Ok(());
557        }
558        if line.starts_with('#') {
559            self.parse_line1(line)?;
560            return Ok(());
561        }
562        if line.starts_with('+') {
563            self.parse_plus_line(line, line_number)?;
564            return Ok(());
565        }
566        if line.starts_with("%c") {
567            self.parse_pc_line(line)?;
568            return Ok(());
569        }
570        if line.starts_with("%f") || line.starts_with("%i") {
571            // Float/int accuracy descriptor lines - not needed for the typed
572            // state; skipped deterministically.
573            return Ok(());
574        }
575        if line.starts_with('*') {
576            self.parse_epoch_line(line)?;
577            return Ok(());
578        }
579        if line.starts_with('P') {
580            self.parse_position_line(line, line_number)?;
581            return Ok(());
582        }
583        if line.starts_with('V') {
584            self.parse_velocity_line(line, line_number)?;
585            return Ok(());
586        }
587        // Unknown / ignorable line (e.g. `%/`); skip without failing - SP3 has
588        // optional descriptor lines a parser must tolerate.
589        Ok(())
590    }
591
592    /// Header line 1: `#cP2020 ...` / `#dV...`.
593    fn parse_line1(&mut self, line: &str) -> Result<()> {
594        // Minimum well-formed line-1 length per the standard.
595        if line.len() < 55 {
596            return Err(Error::Parse(format!(
597                "SP3 header line 1 too short: {line:?}"
598            )));
599        }
600        let chars: Vec<char> = line.chars().collect();
601        let version = Sp3Version::from_char(chars[1])?;
602        self.version = Some(version);
603        self.data_type = Some(Sp3DataType::from_char(chars[2])?);
604        // SP3-a predates the %c time-system descriptor and is implicitly GPST.
605        // Set it here so a (correct) SP3-a file with no %c line still resolves,
606        // while SP3-b/c/d are left as None until a valid %c line proves the
607        // scale (a missing %c then becomes a hard error, not a GPST default).
608        if matches!(version, Sp3Version::A) {
609            self.time_system = Some(Sp3TimeSystem::Gps);
610        }
611
612        // Column layout per the SP3 standard, matching the (round-trip-tested)
613        // refs/sp3 line-1 reader: num_epochs 32..40, observables 40..45,
614        // coord_system 45..51, orbit_type 51..55, agency 55...
615        self.num_epochs = field(line, 32, 40)
616            .trim()
617            .parse::<u64>()
618            .map_err(|_| Error::Parse(format!("SP3 num_epochs unparsable in {line:?}")))?;
619        self.coordinate_system = field(line, 45, 51).trim().to_string();
620        self.orbit_type = field(line, 51, 55).trim().to_string();
621        self.agency = field_from(line, 55).trim().to_string();
622        self.have_line1 = true;
623        Ok(())
624    }
625
626    /// Header line 2: `## 2276  21600.00000000   900.00000000 60176 0.25...`.
627    fn parse_line2(&mut self, line: &str) -> Result<()> {
628        self.gnss_week = field(line, 3, 7)
629            .trim()
630            .parse::<u32>()
631            .map_err(|_| Error::Parse(format!("SP3 GNSS week unparsable in {line:?}")))?;
632        self.seconds_of_week = field(line, 8, 23)
633            .trim()
634            .parse::<f64>()
635            .map_err(|_| Error::Parse(format!("SP3 seconds-of-week unparsable in {line:?}")))?;
636        self.epoch_interval_s = field(line, 24, 38)
637            .trim()
638            .parse::<f64>()
639            .map_err(|_| Error::Parse(format!("SP3 epoch interval unparsable in {line:?}")))?;
640        self.mjd = field(line, 39, 44)
641            .trim()
642            .parse::<u32>()
643            .map_err(|_| Error::Parse(format!("SP3 MJD unparsable in {line:?}")))?;
644        self.mjd_fraction = strict_f64(field_from(line, 45), "mjd_fraction")
645            .map_err(|error| map_field_error(error, line))?;
646        self.have_line2 = true;
647        Ok(())
648    }
649
650    /// `+` satellite-list line: `+   32   G01G02...` (3-char SV tokens from
651    /// column 9 in groups of 17). Continuation `+` lines append more tokens.
652    fn parse_plus_line(&mut self, line: &str, line_number: usize) -> Result<()> {
653        if line.starts_with("++") {
654            return self.parse_accuracy_line(line);
655        }
656        // SV tokens start at column 9 (0-based), each 3 chars, up to 17 per line.
657        let mut col = 9;
658        while col + 3 <= line.len() {
659            let token = field(line, col, col + 3);
660            let trimmed = token.trim();
661            // Unused satellite slots are zero-filled, not a declaration. The
662            // SP3 zero-fill varies between producers (`  0`, ` 00`, `000`), so
663            // any all-zero (or blank) token is padding - never a satellite,
664            // whose token is a system letter + PRN (or, in SP3-a, a non-zero
665            // numeric PRN). Misreading ` 00` as an unrepresentable satellite
666            // inflates `skipped_records` and breaks the parse/write/parse
667            // round trip (the writer re-emits the canonical `  0`).
668            if trimmed.is_empty() || trimmed.bytes().all(|b| b == b'0') {
669                col += 3;
670                continue;
671            }
672            // This is a real declaration slot; the `++` accuracy codes are aligned
673            // to this axis, so track its index whether or not the token resolves.
674            let slot_index = self.declared_sat_slots;
675            self.declared_sat_slots += 1;
676            if let Some(id) = parse_sv_token(token, self.version) {
677                if !self.sat_list.contains(&id) {
678                    self.sat_list.push(id);
679                }
680            } else {
681                // A declared satellite whose token is not representable (e.g. an
682                // extended GLONASS slot R28 beyond the engine's PRN cap) is
683                // dropped from the satellite list, but counted rather than dropped
684                // silently - consistent with the position/velocity record paths
685                // (see `Sp3::skipped_records`). Record the slot so its accuracy
686                // column is skipped, keeping the surviving codes aligned.
687                self.push_unrepresentable_satellite_skip(line_number, token);
688                self.dropped_sat_slots.push(slot_index);
689            }
690            col += 3;
691        }
692        Ok(())
693    }
694
695    /// `++` per-satellite accuracy-code line: 3-char integer fields from column
696    /// 9, aligned with the `+` declaration slots.
697    ///
698    /// The columns track the `+` declaration order, so a column whose declaration
699    /// slot was dropped (an unrepresentable satellite) is read and discarded, not
700    /// pushed - otherwise the surviving satellites would inherit a neighbour's
701    /// accuracy code. With no dropped slots this is exactly the 1:1 push as before.
702    fn parse_accuracy_line(&mut self, line: &str) -> Result<()> {
703        let mut col = 9;
704        while col + 3 <= line.len() && self.accuracy_slot_cursor < self.declared_sat_slots {
705            let token = field(line, col, col + 3);
706            let trimmed = token.trim();
707            let code = if trimmed.is_empty() {
708                0
709            } else {
710                validate::strict_int::<u16>(trimmed, "satellite_accuracy_code")
711                    .map_err(|error| map_field_error(error, line))?
712            };
713            if !self.dropped_sat_slots.contains(&self.accuracy_slot_cursor) {
714                self.sat_accuracy_codes.push(code);
715            }
716            self.accuracy_slot_cursor += 1;
717            col += 3;
718        }
719        Ok(())
720    }
721
722    /// `%c` descriptor: the first one (chars 9-12) carries the time system.
723    fn parse_pc_line(&mut self, line: &str) -> Result<()> {
724        if self.pc_count == 0 {
725            // SP3-a is implicitly GPST regardless of descriptor content.
726            if matches!(self.version, Some(Sp3Version::A)) {
727                self.time_system = Some(Sp3TimeSystem::Gps);
728            } else if line.len() >= 12 {
729                let label = field(line, 9, 12);
730                let trimmed = label.trim();
731                // STRICT: a blank time-system field on the first %c is not GPST,
732                // it is malformed. Reject rather than silently defaulting so a
733                // precise pipeline never mis-attributes an epoch's scale.
734                if trimmed.is_empty() {
735                    return Err(Error::Parse(format!(
736                        "SP3 %c time system is blank in {line:?}"
737                    )));
738                }
739                self.time_system = Some(time_system_from_label(label)?);
740            } else {
741                // STRICT: a short %c line for SP3-b/c/d carries no time system
742                // we can trust. Reject rather than defaulting to GPST.
743                return Err(Error::Parse(format!(
744                    "SP3 %c descriptor too short to carry a time system: {line:?}"
745                )));
746            }
747        }
748        self.pc_count += 1;
749        Ok(())
750    }
751
752    /// Epoch line: `*  2020  6 24  0  0  0.00000000`.
753    fn parse_epoch_line(&mut self, line: &str) -> Result<()> {
754        // STRICT: by the time we reach data, the time system must be known -
755        // implicitly GPST for SP3-a (set at line 1), or from a valid first %c
756        // line for SP3-b/c/d. A missing/blank/short %c is an error, never GPST.
757        let time_system = self.time_system.ok_or_else(|| {
758            Error::Parse("SP3 epoch encountered with no time system (missing %c descriptor)".into())
759        })?;
760        let scale = time_system.time_scale();
761        // Fields after the leading `*  ` (3 chars), then space-delimited.
762        let body = &line[1..];
763        let mut it = body.split_whitespace();
764        let year: i64 = next_field(&mut it, "epoch year")?;
765        let month: i64 = next_field(&mut it, "epoch month")?;
766        let day: i64 = next_field(&mut it, "epoch day")?;
767        let hour: i64 = next_field(&mut it, "epoch hour")?;
768        let minute: i64 = next_field(&mut it, "epoch minute")?;
769        let seconds: f64 = next_field(&mut it, "epoch seconds")?;
770
771        let civil = validate::civil_datetime_with_second_policy(
772            year,
773            month,
774            day,
775            hour,
776            minute,
777            seconds,
778            time_system.civil_second_policy(),
779        )
780        .map_err(|error| map_field_error(error, line))?;
781        let split = civil_to_julian_split(civil)?;
782        let epoch = Instant {
783            scale,
784            repr: InstantRepr::JulianDate(split),
785        };
786        self.epochs.push(epoch);
787        self.states.push(BTreeMap::new());
788        self.interp_raw.push(BTreeMap::new());
789        self.current_epoch = Some(epoch);
790        Ok(())
791    }
792
793    /// Position+clock record: `PG01  x  y  z  clk  ...flags`.
794    fn parse_position_line(&mut self, line: &str, line_number: usize) -> Result<()> {
795        if self.current_epoch.is_none() {
796            return Err(Error::Parse(
797                "SP3 position record before any epoch line".into(),
798            ));
799        }
800        if line.len() < 46 {
801            return Err(Error::Parse(format!(
802                "SP3 position record truncated before vector fields in {line:?}"
803            )));
804        }
805        let token = field(line, 1, 4);
806        let Some(sat) = parse_sv_token(token, self.version) else {
807            // A token that does not parse to a representable `GnssSatelliteId`
808            // (e.g. an extended GLONASS slot like R28 beyond the engine's PRN
809            // cap) is an independent, unsupported record. One such record must
810            // not reject the whole file - skip and count it, mirroring nav
811            // `parse_glonass` and `parse_tle_file`.
812            self.push_unrepresentable_satellite_skip(line_number, token);
813            return Ok(());
814        };
815
816        // The header `+` lines are the authoritative satellite declaration; a
817        // position record for an undeclared satellite is malformed. Accepting it
818        // would store a state the writer (which emits only declared satellites)
819        // cannot reproduce, breaking parse/encode/parse round-tripping.
820        if !self.sat_list.contains(&sat) {
821            return Err(Error::Parse(format!(
822                "SP3 position record for satellite {token:?} not in the header satellite list"
823            )));
824        }
825
826        let x_km = parse_coord(line, 4, 18)?;
827        let y_km = parse_coord(line, 18, 32)?;
828        let z_km = parse_coord(line, 32, 46)?;
829
830        // All-zero position is the missing-orbit sentinel: skip the record.
831        if x_km == MISSING_POSITION_KM && y_km == MISSING_POSITION_KM && z_km == MISSING_POSITION_KM
832        {
833            return Ok(());
834        }
835
836        let clock_us = parse_clock_us(line)?;
837        let clock_s = clock_us.map(|us| us * US_TO_S);
838
839        let flags = parse_flags(line);
840
841        let position = ItrfPositionM::new(x_km * KM_TO_M, y_km * KM_TO_M, z_km * KM_TO_M)
842            .map_err(|e| Error::Parse(format!("SP3 invalid position record: {e}")))?;
843        let state = Sp3State {
844            position,
845            clock_s,
846            velocity: None,
847            clock_rate_s_s: None,
848            flags,
849        };
850        let idx = self.states.len() - 1;
851        self.states[idx].insert(sat, state);
852        // Keep the native-unit node for the interpolation path (see RawNode):
853        // the spline must fit the file's own km/us, not the km->m->km round trip.
854        self.interp_raw[idx].insert(
855            sat,
856            RawNode {
857                km: [x_km, y_km, z_km],
858                clock_us,
859                clock_event: flags.clock_event,
860            },
861        );
862        Ok(())
863    }
864
865    /// Velocity record: `VG01  vx  vy  vz  clkrate ...`. Augments the matching
866    /// position record at the current epoch (must follow it).
867    fn parse_velocity_line(&mut self, line: &str, line_number: usize) -> Result<()> {
868        if self.current_epoch.is_none() {
869            return Err(Error::Parse(
870                "SP3 velocity record before any epoch line".into(),
871            ));
872        }
873        if line.len() < 46 {
874            return Err(Error::Parse(format!(
875                "SP3 velocity record truncated before vector fields in {line:?}"
876            )));
877        }
878        let token = field(line, 1, 4);
879        let Some(sat) = parse_sv_token(token, self.version) else {
880            // Unparsable / out-of-range satellite token: skip and count, same
881            // as the position-record path above.
882            self.push_unrepresentable_satellite_skip(line_number, token);
883            return Ok(());
884        };
885
886        // SP3 velocity is in dm/s; read each axis independently (the refs/sp3
887        // crate has a bug here that reuses Y for X - we do not).
888        let vx_dm_s = parse_coord(line, 4, 18)?;
889        let vy_dm_s = parse_coord(line, 18, 32)?;
890        let vz_dm_s = parse_coord(line, 32, 46)?;
891
892        let missing_velocity = vx_dm_s == MISSING_VELOCITY_DM_S
893            && vy_dm_s == MISSING_VELOCITY_DM_S
894            && vz_dm_s == MISSING_VELOCITY_DM_S;
895        let velocity = ItrfVelocityMS::new(
896            vx_dm_s * DM_S_TO_M_S,
897            vy_dm_s * DM_S_TO_M_S,
898            vz_dm_s * DM_S_TO_M_S,
899        )
900        .map_err(|e| Error::Parse(format!("SP3 invalid velocity record: {e}")))?;
901
902        // Clock-rate field shares the clock column; bad-clock sentinel applies.
903        let clock_rate_s_s = parse_clock_us(line)?.map(|rate| rate * CLOCK_RATE_TO_S_PER_S);
904
905        let idx = self.states.len() - 1;
906        match self.states[idx].get_mut(&sat) {
907            Some(state) if !missing_velocity => {
908                state.velocity = Some(velocity);
909                state.clock_rate_s_s = clock_rate_s_s;
910            }
911            Some(_) => {}
912            None => {
913                // A V-record always follows its P-record for the same satellite
914                // at the same epoch (SP3 format invariant). With no preceding
915                // P-record this satellite has NO valid position at this epoch;
916                // synthesizing one (e.g. the geocenter (0,0,0)) would fabricate
917                // an orbit that the all-zero missing-orbit guard exists to
918                // reject, and would leak through the public state()/states_at().
919                // Treat it as malformed and skip - consistent with the parser's
920                // tolerant skipping of other malformed records. No state is
921                // inserted, so the satellite stays UnknownSatellite at this
922                // epoch and no (0,0,0) position is ever exposed.
923            }
924        }
925        Ok(())
926    }
927
928    fn push_unrepresentable_satellite_skip(&mut self, line_number: usize, token: &str) {
929        self.diagnostics.push_skip(Skip {
930            at: RecordRef::at_line(line_number).with_satellite(token),
931            reason: SkipReason::UnrepresentableSatellite,
932        });
933    }
934
935    fn finish(self) -> Result<Sp3> {
936        if !self.have_line1 {
937            return Err(Error::Parse("SP3 missing header line 1".into()));
938        }
939        if !self.have_line2 {
940            return Err(Error::Parse("SP3 missing header line 2".into()));
941        }
942        let version = self
943            .version
944            .ok_or_else(|| Error::Parse("SP3 version not determined".into()))?;
945        let data_type = self
946            .data_type
947            .ok_or_else(|| Error::Parse("SP3 data type not determined".into()))?;
948        // STRICT: SP3-a is implicitly GPST (set at line 1); SP3-b/c/d must have
949        // proved their scale from a valid first %c line. Never default here.
950        let time_system = self.time_system.ok_or_else(|| {
951            Error::Parse(
952                "SP3 time system not determined (missing/short/blank %c descriptor)".into(),
953            )
954        })?;
955        let time_scale = time_system.time_scale();
956
957        let mut satellite_accuracy_codes = self.sat_accuracy_codes;
958        satellite_accuracy_codes.truncate(self.sat_list.len());
959        satellite_accuracy_codes.resize(self.sat_list.len(), 0);
960        let skipped_records = self.diagnostics.skips.len();
961
962        let header = Sp3Header {
963            version,
964            data_type,
965            num_epochs: self.epochs.len() as u64,
966            coordinate_system: self.coordinate_system,
967            orbit_type: self.orbit_type,
968            agency: self.agency,
969            gnss_week: self.gnss_week,
970            seconds_of_week: self.seconds_of_week,
971            epoch_interval_s: self.epoch_interval_s,
972            mjd: self.mjd,
973            mjd_fraction: self.mjd_fraction,
974            time_system,
975            time_scale,
976            satellites: self.sat_list,
977            satellite_accuracy_codes,
978        };
979
980        Ok(Sp3 {
981            header,
982            epochs: self.epochs,
983            states: self.states,
984            interp_raw: self.interp_raw,
985            comments: self.comments,
986            skipped_records,
987        })
988    }
989}
990
991/// Parse a fixed-column float coordinate, mapping failures to a parse error
992/// that names the offending text.
993fn parse_coord(line: &str, start: usize, end: usize) -> Result<f64> {
994    let raw = field(line, start, end).trim();
995    strict_f64(raw, "coordinate").map_err(|error| map_field_error(error, line))
996}
997
998/// Parse the clock column (chars 46..60). Returns `None` for the bad-clock
999/// sentinel `999999.999999` or an absent/blank field; `Some(us)` otherwise.
1000fn parse_clock_us(line: &str) -> Result<Option<f64>> {
1001    if line.len() <= 46 {
1002        return Ok(None);
1003    }
1004    let raw = field(line, 46, 60).trim();
1005    if raw.is_empty() {
1006        return Ok(None);
1007    }
1008    let value = strict_f64(raw, "clock").map_err(|error| map_field_error(error, line))?;
1009    // Sentinel: any value at or beyond the bad-clock magnitude is "no estimate".
1010    if value.abs() >= BAD_CLOCK_US {
1011        return Ok(None);
1012    }
1013    Ok(Some(value))
1014}
1015
1016fn map_field_error(error: validate::FieldError, line: &str) -> Error {
1017    Error::Parse(format!("SP3 {error} in {line:?}"))
1018}
1019
1020/// Parse the four status flags from their fixed columns (SP3-c/-d shared
1021/// layout): clock-event col 74 = `E`, clock-prediction col 75 = `P`,
1022/// maneuver col 78 = `M`, orbit-prediction col 79 = `P`.
1023fn parse_flags(line: &str) -> Sp3Flags {
1024    let at = |col: usize, want: char| -> bool { char_at(line, col) == Some(want) };
1025    Sp3Flags {
1026        clock_event: at(74, 'E'),
1027        clock_predicted: at(75, 'P'),
1028        maneuver: at(78, 'M'),
1029        orbit_predicted: at(79, 'P'),
1030    }
1031}
1032
1033/// Parse a 3-char SV token (e.g. `G01`, `C30`, or a bare `  1` in SP3-a) into a
1034/// [`GnssSatelliteId`]. Returns `None` on an unrecognized token.
1035fn parse_sv_token(token: &str, version: Option<Sp3Version>) -> Option<GnssSatelliteId> {
1036    let token = token.trim();
1037    if token.is_empty() {
1038        return None;
1039    }
1040    let first = token.chars().next()?;
1041    if first.is_ascii_digit() {
1042        // SP3-a GPS-only: bare numeric PRN, optionally space-padded.
1043        if matches!(version, Some(Sp3Version::A)) || version.is_none() {
1044            let prn = token.parse::<u8>().ok()?;
1045            if !is_valid_prn(GnssSystem::Gps, prn) {
1046                return None;
1047            }
1048            return GnssSatelliteId::new(GnssSystem::Gps, prn).ok();
1049        }
1050        return None;
1051    }
1052    token.parse::<GnssSatelliteId>().ok()
1053}
1054
1055/// Pull and parse the next whitespace-delimited field from an iterator.
1056fn next_field<T: std::str::FromStr>(
1057    it: &mut std::str::SplitWhitespace<'_>,
1058    what: &str,
1059) -> Result<T> {
1060    let tok = it
1061        .next()
1062        .ok_or_else(|| Error::Parse(format!("SP3 missing {what}")))?;
1063    tok.parse::<T>()
1064        .map_err(|_| Error::Parse(format!("SP3 {what} {tok:?} unparsable")))
1065}
1066
1067mod combine;
1068mod interp;
1069mod samples;
1070mod write;
1071
1072pub use combine::{
1073    align_clock_reference, clock_reference_offset, merge, AgreementMetric, ClockReferenceOffset,
1074    EpochAgreement, MergeCombine, MergeFlag, MergeOptions, MergeReport,
1075};
1076pub use samples::{PreciseEphemerisSample, PreciseEphemerisSamples, PreciseSamplesError};
1077
1078#[cfg(all(test, sidereon_repo_tests))]
1079mod tests;