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::{j2000_seconds, 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    /// Exact seconds since J2000 for each parsed epoch, in the product time
299    /// scale, formed from the epoch record's civil fields with integer
300    /// whole-second arithmetic.
301    epoch_j2000_s: Vec<f64>,
302    /// `epoch_index -> (satellite -> state)`. Parallel to [`Sp3::epochs`].
303    states: Vec<BTreeMap<GnssSatelliteId, Sp3State>>,
304    /// `epoch_index -> (satellite -> native-unit node)`. Parallel to
305    /// [`Sp3::epochs`]; populated **only** from genuine position records. The
306    /// interpolator fits its spline over these (km/us straight from the ASCII,
307    /// exactly as the `scipy`/`gnssanalysis` reference does); reconstructing km
308    /// from the public meters (`km->m->km`) drifts up to 1 ULP and breaks the
309    /// 0-ULP parity. See `sp3/interp.rs`.
310    interp_raw: Vec<BTreeMap<GnssSatelliteId, RawNode>>,
311    /// Free-form `/*` comment lines (notice retained for provenance).
312    pub comments: Vec<String>,
313    /// Count of entries skipped because their satellite token did not parse to a
314    /// representable [`GnssSatelliteId`] (e.g. an extended GLONASS slot like `R28`
315    /// beyond the engine's PRN cap): position/velocity records, plus `+`-header
316    /// satellite declarations. Lets callers tell a clean file
317    /// (`skipped_records == 0`) apart from one carrying unsupported satellites,
318    /// without aborting the whole parse on one such entry. Mirrors
319    /// [`crate::astro::sgp4::TleFile::skipped`].
320    pub skipped_records: usize,
321}
322
323/// Native-unit interpolation node: the file's own km / microseconds, kept
324/// verbatim from the ASCII so the spline fit is bit-identical to the reference.
325/// Private - the public surface is meters/seconds via [`Sp3State`].
326#[derive(Debug, Clone, Copy, PartialEq)]
327struct RawNode {
328    /// ECEF position in native SP3 kilometers (X/Y/Z), exact ASCII->f64.
329    km: [f64; 3],
330    /// Clock offset in native SP3 microseconds (`None` for the bad-clock
331    /// sentinel), exact ASCII->f64.
332    clock_us: Option<f64>,
333    /// Whether this epoch carried the clock-event (`E`) flag (clock-arc split).
334    clock_event: bool,
335}
336
337impl Sp3 {
338    /// Parse an SP3-c or SP3-d byte buffer into a typed product.
339    ///
340    /// `bytes` is the full file content (already decompressed; this crate does
341    /// not do gzip - that is a caller-layer I/O concern). Returns
342    /// [`Error::Parse`] with a human-readable reason on malformed input.
343    pub fn parse(bytes: &[u8]) -> Result<Self> {
344        let text = std::str::from_utf8(bytes)
345            .map_err(|e| Error::Parse(format!("SP3 is not valid UTF-8: {e}")))?;
346        Self::parse_str(text)
347    }
348
349    /// Parse from a `&str` (the UTF-8 fast path used by [`Sp3::parse`]).
350    pub fn parse_str(text: &str) -> Result<Self> {
351        if !text.is_ascii() {
352            return Err(Error::Parse("SP3 product text must be ASCII".into()));
353        }
354        let mut parser = Parser::new();
355        for (index, raw) in text.lines().enumerate() {
356            parser.feed(raw, index + 1)?;
357        }
358        parser.finish()
359    }
360
361    /// The satellites present in this product (from the header satellite list).
362    pub fn satellites(&self) -> &[GnssSatelliteId] {
363        &self.header.satellites
364    }
365
366    /// Number of parsed epochs.
367    pub fn epoch_count(&self) -> usize {
368        self.epochs.len()
369    }
370
371    /// The state of `sat` at the parsed epoch with index `epoch_index`.
372    ///
373    /// Returns [`Error::EpochOutOfRange`] if the index is past the end, or
374    /// [`Error::UnknownSatellite`] if the satellite has no record at that epoch.
375    pub fn state(&self, sat: GnssSatelliteId, epoch_index: usize) -> Result<Sp3State> {
376        let per_epoch = self.states.get(epoch_index).ok_or(Error::EpochOutOfRange)?;
377        per_epoch
378            .get(&sat)
379            .copied()
380            .ok_or(Error::UnknownSatellite(sat))
381    }
382
383    /// All `(satellite, state)` pairs recorded at `epoch_index`, in ascending
384    /// satellite order.
385    pub fn states_at(&self, epoch_index: usize) -> Result<&BTreeMap<GnssSatelliteId, Sp3State>> {
386        self.states.get(epoch_index).ok_or(Error::EpochOutOfRange)
387    }
388}
389
390impl core::str::FromStr for Sp3 {
391    type Err = Error;
392
393    fn from_str(s: &str) -> Result<Self> {
394        Self::parse_str(s)
395    }
396}
397
398#[cfg(test)]
399impl Sp3 {}
400
401/// Parse an SP3 time-system label.
402///
403/// SP3-c/-d encode the time system in the `%c` descriptor line (chars 9-12).
404/// SP3-a is implicitly GPST. Unknown labels error rather than silently
405/// defaulting, so a parity pipeline never mis-attributes an epoch's scale.
406fn time_system_from_label(label: &str) -> Result<Sp3TimeSystem> {
407    match label.trim() {
408        "GPS" => Ok(Sp3TimeSystem::Gps),
409        "GLO" => Ok(Sp3TimeSystem::Glonass),
410        "GAL" => Ok(Sp3TimeSystem::Galileo),
411        "TAI" => Ok(Sp3TimeSystem::Tai),
412        "UTC" => Ok(Sp3TimeSystem::Utc),
413        "QZS" => Ok(Sp3TimeSystem::Qzss),
414        "BDT" | "BDS" => Ok(Sp3TimeSystem::Beidou),
415        "IRN" => Ok(Sp3TimeSystem::Irnss),
416        trimmed => Err(Error::Parse(format!(
417            "unsupported SP3 time system '{trimmed}'"
418        ))),
419    }
420}
421
422/// Compute the integer-day / fraction split Julian date from a Gregorian UTC-ish
423/// civil epoch, with the day fraction carried separately (Skyfield split
424/// convention, matching [`JulianDateSplit`]).
425///
426/// SP3 epoch lines are civil dates in the file's *own* time system; we keep
427/// them in that scale (no leap-second shifting here - that is a conversion
428/// concern handled by the core `scales` machinery, not the parser). The
429/// algorithm is the standard Fliegel-Van Flandern Gregorian-to-JDN, then the
430/// time-of-day fraction. JDN is computed in integer arithmetic so the whole-day
431/// boundary is exact; only the sub-day fraction is floating point.
432fn civil_to_julian_split(civil: validate::ValidCivil) -> Result<JulianDateSplit> {
433    // Canonical civil-to-split conversion: the integer JDN places the `*.5`
434    // civil-midnight boundary and the within-day clock fields become the
435    // fraction. SP3 epochs are civil days in the file's own scale (no leap
436    // second). The carry below is retained for the rare epoch whose seconds
437    // overflow a day.
438    let (mut jd_whole, mut fraction) = split_julian_date(
439        civil.year as i32,
440        civil.month as i32,
441        civil.day as i32,
442        civil.hour as i32,
443        civil.minute as i32,
444        civil.second,
445    );
446    if fraction > 1.0 {
447        let carry = fraction.floor();
448        jd_whole += carry;
449        fraction -= carry;
450    }
451    JulianDateSplit::new(jd_whole, fraction)
452        .map_err(|error| Error::Parse(format!("invalid SP3 epoch Julian date: {error}")))
453}
454
455/// Incremental line-driven SP3 parser state machine.
456struct Parser {
457    version: Option<Sp3Version>,
458    data_type: Option<Sp3DataType>,
459    num_epochs: u64,
460    coordinate_system: String,
461    orbit_type: String,
462    agency: String,
463    gnss_week: u32,
464    seconds_of_week: f64,
465    epoch_interval_s: f64,
466    mjd: u32,
467    mjd_fraction: f64,
468    time_system: Option<Sp3TimeSystem>,
469    /// `+`-line declared satellites, in file order.
470    sat_list: Vec<GnssSatelliteId>,
471    /// `++`-line per-satellite accuracy codes, in satellite-list order.
472    sat_accuracy_codes: Vec<u16>,
473    /// Number of real (non-padding) `+`-line satellite slots seen, including any
474    /// dropped because their token was unrepresentable. The `++` accuracy codes
475    /// are positionally aligned with these declaration slots, so this is the axis
476    /// the accuracy parser walks (not the filtered [`Self::sat_list`]).
477    declared_sat_slots: usize,
478    /// Declaration-slot indices (into the `declared_sat_slots` axis) whose token
479    /// was unrepresentable and dropped from [`Self::sat_list`]. Their `++`
480    /// accuracy columns must be skipped so the surviving satellites keep their own
481    /// codes. Empty for every well-formed file, making the accuracy parse a no-op
482    /// realignment in the common case.
483    dropped_sat_slots: Vec<usize>,
484    /// Cursor along the declaration-slot axis consumed by the `++` accuracy
485    /// parser across one or more `++` lines.
486    accuracy_slot_cursor: usize,
487    /// `%c` descriptor lines seen so far (the first carries the time system).
488    pc_count: u32,
489    /// Header line 1 parsed?
490    have_line1: bool,
491    /// Header line 2 parsed?
492    have_line2: bool,
493    /// Epoch currently being filled.
494    current_epoch: Option<Instant>,
495    epochs: Vec<Instant>,
496    epoch_j2000_s: Vec<f64>,
497    states: Vec<BTreeMap<GnssSatelliteId, Sp3State>>,
498    interp_raw: Vec<BTreeMap<GnssSatelliteId, RawNode>>,
499    comments: Vec<String>,
500    diagnostics: Diagnostics,
501    done: bool,
502}
503
504impl Parser {
505    fn new() -> Self {
506        Self {
507            version: None,
508            data_type: None,
509            num_epochs: 0,
510            coordinate_system: String::new(),
511            orbit_type: String::new(),
512            agency: String::new(),
513            gnss_week: 0,
514            seconds_of_week: 0.0,
515            epoch_interval_s: 0.0,
516            mjd: 0,
517            mjd_fraction: 0.0,
518            time_system: None,
519            sat_list: Vec::new(),
520            sat_accuracy_codes: Vec::new(),
521            declared_sat_slots: 0,
522            dropped_sat_slots: Vec::new(),
523            accuracy_slot_cursor: 0,
524            pc_count: 0,
525            have_line1: false,
526            have_line2: false,
527            current_epoch: None,
528            epochs: Vec::new(),
529            epoch_j2000_s: Vec::new(),
530            states: Vec::new(),
531            interp_raw: Vec::new(),
532            comments: Vec::new(),
533            diagnostics: Diagnostics::new(),
534            done: false,
535        }
536    }
537
538    fn feed(&mut self, raw: &str, line_number: usize) -> Result<()> {
539        if self.done {
540            return Ok(());
541        }
542        // SP3 is fixed-column ASCII; trim only the trailing CR / newline noise,
543        // never leading spaces (columns are significant).
544        let line = raw.trim_end_matches(['\r', '\n']);
545
546        if line == "EOF" {
547            self.done = true;
548            return Ok(());
549        }
550        if line.starts_with("/*") {
551            // Comment line; columns 4.. are the text.
552            if line.len() > 3 {
553                self.comments.push(line[3..].trim_end().to_string());
554            } else {
555                self.comments.push(String::new());
556            }
557            return Ok(());
558        }
559        // Header line 2 (`##`) must be tested before line 1 (`#`).
560        if line.starts_with("##") {
561            self.parse_line2(line)?;
562            return Ok(());
563        }
564        if line.starts_with('#') {
565            self.parse_line1(line)?;
566            return Ok(());
567        }
568        if line.starts_with('+') {
569            self.parse_plus_line(line, line_number)?;
570            return Ok(());
571        }
572        if line.starts_with("%c") {
573            self.parse_pc_line(line)?;
574            return Ok(());
575        }
576        if line.starts_with("%f") || line.starts_with("%i") {
577            // Float/int accuracy descriptor lines - not needed for the typed
578            // state; skipped deterministically.
579            return Ok(());
580        }
581        if line.starts_with('*') {
582            self.parse_epoch_line(line)?;
583            return Ok(());
584        }
585        if line.starts_with('P') {
586            self.parse_position_line(line, line_number)?;
587            return Ok(());
588        }
589        if line.starts_with('V') {
590            self.parse_velocity_line(line, line_number)?;
591            return Ok(());
592        }
593        // Unknown / ignorable line (e.g. `%/`); skip without failing - SP3 has
594        // optional descriptor lines a parser must tolerate.
595        Ok(())
596    }
597
598    /// Header line 1: `#cP2020 ...` / `#dV...`.
599    fn parse_line1(&mut self, line: &str) -> Result<()> {
600        // Minimum well-formed line-1 length per the standard.
601        if line.len() < 55 {
602            return Err(Error::Parse(format!(
603                "SP3 header line 1 too short: {line:?}"
604            )));
605        }
606        let chars: Vec<char> = line.chars().collect();
607        let version = Sp3Version::from_char(chars[1])?;
608        self.version = Some(version);
609        self.data_type = Some(Sp3DataType::from_char(chars[2])?);
610        // SP3-a predates the %c time-system descriptor and is implicitly GPST.
611        // Set it here so a (correct) SP3-a file with no %c line still resolves,
612        // while SP3-b/c/d are left as None until a valid %c line proves the
613        // scale (a missing %c then becomes a hard error, not a GPST default).
614        if matches!(version, Sp3Version::A) {
615            self.time_system = Some(Sp3TimeSystem::Gps);
616        }
617
618        // Column layout per the SP3 standard, matching the (round-trip-tested)
619        // refs/sp3 line-1 reader: num_epochs 32..40, observables 40..45,
620        // coord_system 45..51, orbit_type 51..55, agency 55...
621        self.num_epochs = field(line, 32, 40)
622            .trim()
623            .parse::<u64>()
624            .map_err(|_| Error::Parse(format!("SP3 num_epochs unparsable in {line:?}")))?;
625        self.coordinate_system = field(line, 45, 51).trim().to_string();
626        self.orbit_type = field(line, 51, 55).trim().to_string();
627        self.agency = field_from(line, 55).trim().to_string();
628        self.have_line1 = true;
629        Ok(())
630    }
631
632    /// Header line 2: `## 2276  21600.00000000   900.00000000 60176 0.25...`.
633    fn parse_line2(&mut self, line: &str) -> Result<()> {
634        self.gnss_week = field(line, 3, 7)
635            .trim()
636            .parse::<u32>()
637            .map_err(|_| Error::Parse(format!("SP3 GNSS week unparsable in {line:?}")))?;
638        self.seconds_of_week = field(line, 8, 23)
639            .trim()
640            .parse::<f64>()
641            .map_err(|_| Error::Parse(format!("SP3 seconds-of-week unparsable in {line:?}")))?;
642        self.epoch_interval_s = field(line, 24, 38)
643            .trim()
644            .parse::<f64>()
645            .map_err(|_| Error::Parse(format!("SP3 epoch interval unparsable in {line:?}")))?;
646        self.mjd = field(line, 39, 44)
647            .trim()
648            .parse::<u32>()
649            .map_err(|_| Error::Parse(format!("SP3 MJD unparsable in {line:?}")))?;
650        self.mjd_fraction = strict_f64(field_from(line, 45), "mjd_fraction")
651            .map_err(|error| map_field_error(error, line))?;
652        self.have_line2 = true;
653        Ok(())
654    }
655
656    /// `+` satellite-list line: `+   32   G01G02...` (3-char SV tokens from
657    /// column 9 in groups of 17). Continuation `+` lines append more tokens.
658    fn parse_plus_line(&mut self, line: &str, line_number: usize) -> Result<()> {
659        if line.starts_with("++") {
660            return self.parse_accuracy_line(line);
661        }
662        // SV tokens start at column 9 (0-based), each 3 chars, up to 17 per line.
663        let mut col = 9;
664        while col + 3 <= line.len() {
665            let token = field(line, col, col + 3);
666            let trimmed = token.trim();
667            // Unused satellite slots are zero-filled, not a declaration. The
668            // SP3 zero-fill varies between producers (`  0`, ` 00`, `000`), so
669            // any all-zero (or blank) token is padding - never a satellite,
670            // whose token is a system letter + PRN (or, in SP3-a, a non-zero
671            // numeric PRN). Misreading ` 00` as an unrepresentable satellite
672            // inflates `skipped_records` and breaks the parse/write/parse
673            // round trip (the writer re-emits the canonical `  0`).
674            if trimmed.is_empty() || trimmed.bytes().all(|b| b == b'0') {
675                col += 3;
676                continue;
677            }
678            // This is a real declaration slot; the `++` accuracy codes are aligned
679            // to this axis, so track its index whether or not the token resolves.
680            let slot_index = self.declared_sat_slots;
681            self.declared_sat_slots += 1;
682            if let Some(id) = parse_sv_token(token, self.version) {
683                if !self.sat_list.contains(&id) {
684                    self.sat_list.push(id);
685                }
686            } else {
687                // A declared satellite whose token is not representable (e.g. an
688                // extended GLONASS slot R28 beyond the engine's PRN cap) is
689                // dropped from the satellite list, but counted rather than dropped
690                // silently - consistent with the position/velocity record paths
691                // (see `Sp3::skipped_records`). Record the slot so its accuracy
692                // column is skipped, keeping the surviving codes aligned.
693                self.push_unrepresentable_satellite_skip(line_number, token);
694                self.dropped_sat_slots.push(slot_index);
695            }
696            col += 3;
697        }
698        Ok(())
699    }
700
701    /// `++` per-satellite accuracy-code line: 3-char integer fields from column
702    /// 9, aligned with the `+` declaration slots.
703    ///
704    /// The columns track the `+` declaration order, so a column whose declaration
705    /// slot was dropped (an unrepresentable satellite) is read and discarded, not
706    /// pushed - otherwise the surviving satellites would inherit a neighbour's
707    /// accuracy code. With no dropped slots this is exactly the 1:1 push as before.
708    fn parse_accuracy_line(&mut self, line: &str) -> Result<()> {
709        let mut col = 9;
710        while col + 3 <= line.len() && self.accuracy_slot_cursor < self.declared_sat_slots {
711            let token = field(line, col, col + 3);
712            let trimmed = token.trim();
713            let code = if trimmed.is_empty() {
714                0
715            } else {
716                validate::strict_int::<u16>(trimmed, "satellite_accuracy_code")
717                    .map_err(|error| map_field_error(error, line))?
718            };
719            if !self.dropped_sat_slots.contains(&self.accuracy_slot_cursor) {
720                self.sat_accuracy_codes.push(code);
721            }
722            self.accuracy_slot_cursor += 1;
723            col += 3;
724        }
725        Ok(())
726    }
727
728    /// `%c` descriptor: the first one (chars 9-12) carries the time system.
729    fn parse_pc_line(&mut self, line: &str) -> Result<()> {
730        if self.pc_count == 0 {
731            // SP3-a is implicitly GPST regardless of descriptor content.
732            if matches!(self.version, Some(Sp3Version::A)) {
733                self.time_system = Some(Sp3TimeSystem::Gps);
734            } else if line.len() >= 12 {
735                let label = field(line, 9, 12);
736                let trimmed = label.trim();
737                // STRICT: a blank time-system field on the first %c is not GPST,
738                // it is malformed. Reject rather than silently defaulting so a
739                // precise pipeline never mis-attributes an epoch's scale.
740                if trimmed.is_empty() {
741                    return Err(Error::Parse(format!(
742                        "SP3 %c time system is blank in {line:?}"
743                    )));
744                }
745                self.time_system = Some(time_system_from_label(label)?);
746            } else {
747                // STRICT: a short %c line for SP3-b/c/d carries no time system
748                // we can trust. Reject rather than defaulting to GPST.
749                return Err(Error::Parse(format!(
750                    "SP3 %c descriptor too short to carry a time system: {line:?}"
751                )));
752            }
753        }
754        self.pc_count += 1;
755        Ok(())
756    }
757
758    /// Epoch line: `*  2020  6 24  0  0  0.00000000`.
759    fn parse_epoch_line(&mut self, line: &str) -> Result<()> {
760        // STRICT: by the time we reach data, the time system must be known -
761        // implicitly GPST for SP3-a (set at line 1), or from a valid first %c
762        // line for SP3-b/c/d. A missing/blank/short %c is an error, never GPST.
763        let time_system = self.time_system.ok_or_else(|| {
764            Error::Parse("SP3 epoch encountered with no time system (missing %c descriptor)".into())
765        })?;
766        let scale = time_system.time_scale();
767        // Fields after the leading `*  ` (3 chars), then space-delimited.
768        let body = &line[1..];
769        let mut it = body.split_whitespace();
770        let year: i64 = next_field(&mut it, "epoch year")?;
771        let month: i64 = next_field(&mut it, "epoch month")?;
772        let day: i64 = next_field(&mut it, "epoch day")?;
773        let hour: i64 = next_field(&mut it, "epoch hour")?;
774        let minute: i64 = next_field(&mut it, "epoch minute")?;
775        let seconds: f64 = next_field(&mut it, "epoch seconds")?;
776
777        let civil = validate::civil_datetime_with_second_policy(
778            year,
779            month,
780            day,
781            hour,
782            minute,
783            seconds,
784            time_system.civil_second_policy(),
785        )
786        .map_err(|error| map_field_error(error, line))?;
787        let split = civil_to_julian_split(civil)?;
788        let epoch_j2000_s = j2000_seconds(
789            civil.year as i32,
790            civil.month as i32,
791            civil.day as i32,
792            civil.hour as i32,
793            civil.minute as i32,
794            civil.second,
795        );
796        let epoch = Instant {
797            scale,
798            repr: InstantRepr::JulianDate(split),
799        };
800        self.epochs.push(epoch);
801        self.epoch_j2000_s.push(epoch_j2000_s);
802        self.states.push(BTreeMap::new());
803        self.interp_raw.push(BTreeMap::new());
804        self.current_epoch = Some(epoch);
805        Ok(())
806    }
807
808    /// Position+clock record: `PG01  x  y  z  clk  ...flags`.
809    fn parse_position_line(&mut self, line: &str, line_number: usize) -> Result<()> {
810        if self.current_epoch.is_none() {
811            return Err(Error::Parse(
812                "SP3 position record before any epoch line".into(),
813            ));
814        }
815        if line.len() < 46 {
816            return Err(Error::Parse(format!(
817                "SP3 position record truncated before vector fields in {line:?}"
818            )));
819        }
820        let token = field(line, 1, 4);
821        let Some(sat) = parse_sv_token(token, self.version) else {
822            // A token that does not parse to a representable `GnssSatelliteId`
823            // (e.g. an extended GLONASS slot like R28 beyond the engine's PRN
824            // cap) is an independent, unsupported record. One such record must
825            // not reject the whole file - skip and count it, mirroring nav
826            // `parse_glonass` and `parse_tle_file`.
827            self.push_unrepresentable_satellite_skip(line_number, token);
828            return Ok(());
829        };
830
831        // The header `+` lines are the authoritative satellite declaration; a
832        // position record for an undeclared satellite is malformed. Accepting it
833        // would store a state the writer (which emits only declared satellites)
834        // cannot reproduce, breaking parse/encode/parse round-tripping.
835        if !self.sat_list.contains(&sat) {
836            return Err(Error::Parse(format!(
837                "SP3 position record for satellite {token:?} not in the header satellite list"
838            )));
839        }
840
841        let x_km = parse_coord(line, 4, 18)?;
842        let y_km = parse_coord(line, 18, 32)?;
843        let z_km = parse_coord(line, 32, 46)?;
844
845        // All-zero position is the missing-orbit sentinel: skip the record.
846        if x_km == MISSING_POSITION_KM && y_km == MISSING_POSITION_KM && z_km == MISSING_POSITION_KM
847        {
848            return Ok(());
849        }
850
851        let clock_us = parse_clock_us(line)?;
852        let clock_s = clock_us.map(|us| us * US_TO_S);
853
854        let flags = parse_flags(line);
855
856        let position = ItrfPositionM::new(x_km * KM_TO_M, y_km * KM_TO_M, z_km * KM_TO_M)
857            .map_err(|e| Error::Parse(format!("SP3 invalid position record: {e}")))?;
858        let state = Sp3State {
859            position,
860            clock_s,
861            velocity: None,
862            clock_rate_s_s: None,
863            flags,
864        };
865        let idx = self.states.len() - 1;
866        self.states[idx].insert(sat, state);
867        // Keep the native-unit node for the interpolation path (see RawNode):
868        // the spline must fit the file's own km/us, not the km->m->km round trip.
869        self.interp_raw[idx].insert(
870            sat,
871            RawNode {
872                km: [x_km, y_km, z_km],
873                clock_us,
874                clock_event: flags.clock_event,
875            },
876        );
877        Ok(())
878    }
879
880    /// Velocity record: `VG01  vx  vy  vz  clkrate ...`. Augments the matching
881    /// position record at the current epoch (must follow it).
882    fn parse_velocity_line(&mut self, line: &str, line_number: usize) -> Result<()> {
883        if self.current_epoch.is_none() {
884            return Err(Error::Parse(
885                "SP3 velocity record before any epoch line".into(),
886            ));
887        }
888        if line.len() < 46 {
889            return Err(Error::Parse(format!(
890                "SP3 velocity record truncated before vector fields in {line:?}"
891            )));
892        }
893        let token = field(line, 1, 4);
894        let Some(sat) = parse_sv_token(token, self.version) else {
895            // Unparsable / out-of-range satellite token: skip and count, same
896            // as the position-record path above.
897            self.push_unrepresentable_satellite_skip(line_number, token);
898            return Ok(());
899        };
900
901        // SP3 velocity is in dm/s; read each axis independently (the refs/sp3
902        // crate has a bug here that reuses Y for X - we do not).
903        let vx_dm_s = parse_coord(line, 4, 18)?;
904        let vy_dm_s = parse_coord(line, 18, 32)?;
905        let vz_dm_s = parse_coord(line, 32, 46)?;
906
907        let missing_velocity = vx_dm_s == MISSING_VELOCITY_DM_S
908            && vy_dm_s == MISSING_VELOCITY_DM_S
909            && vz_dm_s == MISSING_VELOCITY_DM_S;
910        let velocity = ItrfVelocityMS::new(
911            vx_dm_s * DM_S_TO_M_S,
912            vy_dm_s * DM_S_TO_M_S,
913            vz_dm_s * DM_S_TO_M_S,
914        )
915        .map_err(|e| Error::Parse(format!("SP3 invalid velocity record: {e}")))?;
916
917        // Clock-rate field shares the clock column; bad-clock sentinel applies.
918        let clock_rate_s_s = parse_clock_us(line)?.map(|rate| rate * CLOCK_RATE_TO_S_PER_S);
919
920        let idx = self.states.len() - 1;
921        match self.states[idx].get_mut(&sat) {
922            Some(state) if !missing_velocity => {
923                state.velocity = Some(velocity);
924                state.clock_rate_s_s = clock_rate_s_s;
925            }
926            Some(_) => {}
927            None => {
928                // A V-record always follows its P-record for the same satellite
929                // at the same epoch (SP3 format invariant). With no preceding
930                // P-record this satellite has NO valid position at this epoch;
931                // synthesizing one (e.g. the geocenter (0,0,0)) would fabricate
932                // an orbit that the all-zero missing-orbit guard exists to
933                // reject, and would leak through the public state()/states_at().
934                // Treat it as malformed and skip - consistent with the parser's
935                // tolerant skipping of other malformed records. No state is
936                // inserted, so the satellite stays UnknownSatellite at this
937                // epoch and no (0,0,0) position is ever exposed.
938            }
939        }
940        Ok(())
941    }
942
943    fn push_unrepresentable_satellite_skip(&mut self, line_number: usize, token: &str) {
944        self.diagnostics.push_skip(Skip {
945            at: RecordRef::at_line(line_number).with_satellite(token),
946            reason: SkipReason::UnrepresentableSatellite,
947        });
948    }
949
950    fn finish(self) -> Result<Sp3> {
951        if !self.have_line1 {
952            return Err(Error::Parse("SP3 missing header line 1".into()));
953        }
954        if !self.have_line2 {
955            return Err(Error::Parse("SP3 missing header line 2".into()));
956        }
957        let version = self
958            .version
959            .ok_or_else(|| Error::Parse("SP3 version not determined".into()))?;
960        let data_type = self
961            .data_type
962            .ok_or_else(|| Error::Parse("SP3 data type not determined".into()))?;
963        // STRICT: SP3-a is implicitly GPST (set at line 1); SP3-b/c/d must have
964        // proved their scale from a valid first %c line. Never default here.
965        let time_system = self.time_system.ok_or_else(|| {
966            Error::Parse(
967                "SP3 time system not determined (missing/short/blank %c descriptor)".into(),
968            )
969        })?;
970        let time_scale = time_system.time_scale();
971
972        let mut satellite_accuracy_codes = self.sat_accuracy_codes;
973        satellite_accuracy_codes.truncate(self.sat_list.len());
974        satellite_accuracy_codes.resize(self.sat_list.len(), 0);
975        let skipped_records = self.diagnostics.skips.len();
976
977        let header = Sp3Header {
978            version,
979            data_type,
980            num_epochs: self.epochs.len() as u64,
981            coordinate_system: self.coordinate_system,
982            orbit_type: self.orbit_type,
983            agency: self.agency,
984            gnss_week: self.gnss_week,
985            seconds_of_week: self.seconds_of_week,
986            epoch_interval_s: self.epoch_interval_s,
987            mjd: self.mjd,
988            mjd_fraction: self.mjd_fraction,
989            time_system,
990            time_scale,
991            satellites: self.sat_list,
992            satellite_accuracy_codes,
993        };
994
995        Ok(Sp3 {
996            header,
997            epochs: self.epochs,
998            epoch_j2000_s: self.epoch_j2000_s,
999            states: self.states,
1000            interp_raw: self.interp_raw,
1001            comments: self.comments,
1002            skipped_records,
1003        })
1004    }
1005}
1006
1007/// Parse a fixed-column float coordinate, mapping failures to a parse error
1008/// that names the offending text.
1009fn parse_coord(line: &str, start: usize, end: usize) -> Result<f64> {
1010    let raw = field(line, start, end).trim();
1011    strict_f64(raw, "coordinate").map_err(|error| map_field_error(error, line))
1012}
1013
1014/// Parse the clock column (chars 46..60). Returns `None` for the bad-clock
1015/// sentinel `999999.999999` or an absent/blank field; `Some(us)` otherwise.
1016fn parse_clock_us(line: &str) -> Result<Option<f64>> {
1017    if line.len() <= 46 {
1018        return Ok(None);
1019    }
1020    let raw = field(line, 46, 60).trim();
1021    if raw.is_empty() {
1022        return Ok(None);
1023    }
1024    let value = strict_f64(raw, "clock").map_err(|error| map_field_error(error, line))?;
1025    // Sentinel: any value at or beyond the bad-clock magnitude is "no estimate".
1026    if value.abs() >= BAD_CLOCK_US {
1027        return Ok(None);
1028    }
1029    Ok(Some(value))
1030}
1031
1032fn map_field_error(error: validate::FieldError, line: &str) -> Error {
1033    Error::Parse(format!("SP3 {error} in {line:?}"))
1034}
1035
1036/// Parse the four status flags from their fixed columns (SP3-c/-d shared
1037/// layout): clock-event col 74 = `E`, clock-prediction col 75 = `P`,
1038/// maneuver col 78 = `M`, orbit-prediction col 79 = `P`.
1039fn parse_flags(line: &str) -> Sp3Flags {
1040    let at = |col: usize, want: char| -> bool { char_at(line, col) == Some(want) };
1041    Sp3Flags {
1042        clock_event: at(74, 'E'),
1043        clock_predicted: at(75, 'P'),
1044        maneuver: at(78, 'M'),
1045        orbit_predicted: at(79, 'P'),
1046    }
1047}
1048
1049/// Parse a 3-char SV token (e.g. `G01`, `C30`, or a bare `  1` in SP3-a) into a
1050/// [`GnssSatelliteId`]. Returns `None` on an unrecognized token.
1051fn parse_sv_token(token: &str, version: Option<Sp3Version>) -> Option<GnssSatelliteId> {
1052    let token = token.trim();
1053    if token.is_empty() {
1054        return None;
1055    }
1056    let first = token.chars().next()?;
1057    if first.is_ascii_digit() {
1058        // SP3-a GPS-only: bare numeric PRN, optionally space-padded.
1059        if matches!(version, Some(Sp3Version::A)) || version.is_none() {
1060            let prn = token.parse::<u8>().ok()?;
1061            if !is_valid_prn(GnssSystem::Gps, prn) {
1062                return None;
1063            }
1064            return GnssSatelliteId::new(GnssSystem::Gps, prn).ok();
1065        }
1066        return None;
1067    }
1068    token.parse::<GnssSatelliteId>().ok()
1069}
1070
1071/// Pull and parse the next whitespace-delimited field from an iterator.
1072fn next_field<T: std::str::FromStr>(
1073    it: &mut std::str::SplitWhitespace<'_>,
1074    what: &str,
1075) -> Result<T> {
1076    let tok = it
1077        .next()
1078        .ok_or_else(|| Error::Parse(format!("SP3 missing {what}")))?;
1079    tok.parse::<T>()
1080        .map_err(|_| Error::Parse(format!("SP3 {what} {tok:?} unparsable")))
1081}
1082
1083mod combine;
1084mod interp;
1085mod interpolant;
1086mod samples;
1087mod verify;
1088mod write;
1089
1090pub use combine::{
1091    align_clock_reference, clock_reference_offset, merge, AgreementMetric, ClockReferenceOffset,
1092    EpochAgreement, MergeCombine, MergeFlag, MergeOptions, MergeReport,
1093};
1094pub use interpolant::{PreciseEphemerisInterpolant, PreciseInterpolantError};
1095pub use samples::{
1096    sp3_ecef_state_to_eci, PreciseEphemerisSample, PreciseEphemerisSamples,
1097    PreciseEphemerisStateSample, PreciseSamplesError,
1098};
1099pub use verify::{
1100    compare_position_series, InterpolationComparison, InterpolationDivergence, ReferenceState,
1101};
1102
1103#[cfg(all(test, sidereon_repo_tests))]
1104mod tests;