siderust 0.9.1

High-precision astronomy and satellite mechanics in Rust.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
//! # ORBEX orbit/clock/attitude product reader
//!
//! ORBEX is a flexible, text-based format for distributing precise satellite
//! orbit, clock, and attitude data. This module parses the `#ORB`, `#CLK`,
//! and `#ATT` record types from the `+EPHEMERIS/DATA` block.
//!
//! ## References
//!
//! - IGS ORBEX Format Description (draft, 2020).

use super::{FileLocation, FormatError, ParseMode};
use std::io::{BufRead, BufReader, Read};

/// Single orbit record from an ORBEX `#ORB` block.
///
/// # Examples
///
/// ```
/// use siderust::formats::igs::orbex::OrbexOrbitEntry;
/// let e = OrbexOrbitEntry {
///     sat: "G01".to_string(),
///     epoch_mjd: 60000.25,
///     pos_m: [1e7, 2e6, 3e6],
///     vel_m_s: None,
/// };
/// assert_eq!(e.sat, "G01");
/// ```
#[derive(Debug, Clone)]
pub struct OrbexOrbitEntry {
    /// Satellite identifier (e.g. `"G01"`).
    pub sat: String,
    /// Epoch as modified Julian date.
    pub epoch_mjd: f64,
    /// Position components in meters.
    pub pos_m: [f64; 3],
    /// Velocity components in m/s, if present.
    pub vel_m_s: Option<[f64; 3]>,
}

/// Single clock record from an ORBEX `#CLK` block.
///
/// # Examples
///
/// ```
/// use siderust::formats::igs::orbex::OrbexClockEntry;
/// let e = OrbexClockEntry { sat: "G01".to_string(), epoch_mjd: 60000.25, bias_s: 1e-7 };
/// assert!((e.bias_s - 1e-7).abs() < 1e-15);
/// ```
#[derive(Debug, Clone)]
pub struct OrbexClockEntry {
    /// Satellite identifier.
    pub sat: String,
    /// Epoch as modified Julian date.
    pub epoch_mjd: f64,
    /// Clock bias in seconds.
    pub bias_s: f64,
}

/// Single attitude record from an ORBEX `#ATT` block.
///
/// # Examples
///
/// ```
/// use siderust::formats::igs::orbex::OrbexAttitudeEntry;
/// let e = OrbexAttitudeEntry {
///     sat: "G01".to_string(),
///     epoch_mjd: 60000.25,
///     quaternion: [1.0, 0.0, 0.0, 0.0],
/// };
/// assert_eq!(e.quaternion[0], 1.0);
/// ```
#[derive(Debug, Clone)]
pub struct OrbexAttitudeEntry {
    /// Satellite identifier.
    pub sat: String,
    /// Epoch as modified Julian date.
    pub epoch_mjd: f64,
    /// Quaternion \[w, x, y, z\].
    pub quaternion: [f64; 4],
}

/// Parsed ORBEX product.
///
/// # Examples
///
/// ```
/// use siderust::formats::igs::orbex::OrbexProduct;
/// let prod = OrbexProduct::default();
/// assert!(prod.orbits.is_empty());
/// ```
#[derive(Debug, Default)]
pub struct OrbexProduct {
    /// File version string.
    pub version: String,
    /// List of orbit entries.
    pub orbits: Vec<OrbexOrbitEntry>,
    /// List of clock entries.
    pub clocks: Vec<OrbexClockEntry>,
    /// List of attitude entries.
    pub attitudes: Vec<OrbexAttitudeEntry>,
}

/// Read an ORBEX file from a byte source.
///
/// Parses `#ORB`, `#CLK`, and `#ATT` record types. Unknown record types are
/// skipped in both `Strict` and `Permissive` modes (they are a defined
/// extension mechanism in the ORBEX spec).
///
/// # Errors
///
/// Returns [`FormatError::Format`] on parse failure in `Strict` mode,
/// or [`FormatError::Io`] for I/O failures.
///
/// # Examples
///
/// ```
/// use siderust::formats::igs::orbex::read_orbex;
/// use siderust::formats::ParseMode;
///
/// let lines = [
///     "%=ORBEX  0.09", "%%", "+EPHEMERIS/DATA", "#ORB",
///     "## 2024  1 21600.000000000",
///     " G01  1.0E+07  2.0E+06  3.0E+06",
///     "-EPHEMERIS/DATA", "%ENDORBEX",
/// ];
/// let data = lines.join("\n");
/// let prod = read_orbex(data.as_bytes(), ParseMode::Permissive).unwrap();
/// assert_eq!(prod.orbits.len(), 1);
/// ```
pub fn read_orbex<R: Read>(reader: R, mode: ParseMode) -> Result<OrbexProduct, FormatError> {
    let mut product = OrbexProduct::default();
    let buf = BufReader::new(reader);

    #[derive(PartialEq, Clone, Copy)]
    enum Section {
        None,
        Orb,
        Clk,
        Att,
    }

    let mut section = Section::None;
    let mut current_epoch_mjd: f64 = 0.0;

    for (lineno, result) in buf.lines().enumerate() {
        let line = result.map_err(FormatError::Io)?;
        let lineno = lineno + 1;

        if line.starts_with("%=ORBEX") {
            let parts: Vec<&str> = line.split_whitespace().collect();
            if parts.len() >= 2 {
                product.version = parts[1].to_string();
            }
            continue;
        }
        if line.starts_with("%ENDORBEX") || line.starts_with("%EOF") {
            break;
        }
        // Skip comment/description block lines
        if line.starts_with('%') {
            continue;
        }
        if line.starts_with('+') || line.starts_with('-') {
            continue;
        }

        // Epoch header: ## YYYY DDD SSSSS.SSSSSSS (must check before generic '#')
        if line.starts_with("##") {
            let rest = line.trim_start_matches('#').trim();
            let parts: Vec<&str> = rest.split_whitespace().collect();
            if parts.len() >= 3 {
                let year: i32 = parts[0].parse().unwrap_or(2000);
                let doy: u32 = parts[1].parse().unwrap_or(1);
                let sod: f64 = parts[2].parse().unwrap_or(0.0);
                current_epoch_mjd = orbex_epoch_to_mjd(year, doy, sod);
            }
            continue;
        }

        // Section type markers
        if line.starts_with("#ORB") {
            section = Section::Orb;
            continue;
        }
        if line.starts_with("#CLK") {
            section = Section::Clk;
            continue;
        }
        if line.starts_with("#ATT") {
            section = Section::Att;
            continue;
        }
        // Other # markers: switch to none
        if line.starts_with('#') {
            section = Section::None;
            continue;
        }

        // Satellite data lines start with a space then sat id
        if !line.starts_with(' ') {
            continue;
        }
        let parts: Vec<&str> = line.split_whitespace().collect();
        if parts.is_empty() {
            continue;
        }

        match section {
            Section::Orb => {
                if parts.len() < 4 {
                    if mode == ParseMode::Strict {
                        return Err(FormatError::located(
                            "ORBEX §3.3",
                            FileLocation::at_line(lineno),
                            format!("ORB record has {} fields, need ≥ 4", parts.len()),
                        ));
                    }
                    continue;
                }
                let sat = parts[0].to_string();
                let x = parse_f64(parts[1], lineno, "pos_x", mode)?;
                let y = parse_f64(parts[2], lineno, "pos_y", mode)?;
                let z = parse_f64(parts[3], lineno, "pos_z", mode)?;
                let vel_m_s = if parts.len() >= 7 {
                    let vx = parse_f64(parts[4], lineno, "vel_x", mode)?;
                    let vy = parse_f64(parts[5], lineno, "vel_y", mode)?;
                    let vz = parse_f64(parts[6], lineno, "vel_z", mode)?;
                    Some([vx, vy, vz])
                } else {
                    None
                };
                product.orbits.push(OrbexOrbitEntry {
                    sat,
                    epoch_mjd: current_epoch_mjd,
                    pos_m: [x, y, z],
                    vel_m_s,
                });
            }
            Section::Clk => {
                if parts.len() < 2 {
                    if mode == ParseMode::Strict {
                        return Err(FormatError::located(
                            "ORBEX §3.4",
                            FileLocation::at_line(lineno),
                            format!("CLK record has {} fields, need ≥ 2", parts.len()),
                        ));
                    }
                    continue;
                }
                let sat = parts[0].to_string();
                let bias_s = parse_f64(parts[1], lineno, "bias", mode)?;
                product.clocks.push(OrbexClockEntry {
                    sat,
                    epoch_mjd: current_epoch_mjd,
                    bias_s,
                });
            }
            Section::Att => {
                if parts.len() < 5 {
                    if mode == ParseMode::Strict {
                        return Err(FormatError::located(
                            "ORBEX §3.5",
                            FileLocation::at_line(lineno),
                            format!("ATT record has {} fields, need ≥ 5", parts.len()),
                        ));
                    }
                    continue;
                }
                let sat = parts[0].to_string();
                let w = parse_f64(parts[1], lineno, "q_w", mode)?;
                let x = parse_f64(parts[2], lineno, "q_x", mode)?;
                let y = parse_f64(parts[3], lineno, "q_y", mode)?;
                let z = parse_f64(parts[4], lineno, "q_z", mode)?;
                product.attitudes.push(OrbexAttitudeEntry {
                    sat,
                    epoch_mjd: current_epoch_mjd,
                    quaternion: [w, x, y, z],
                });
            }
            Section::None => {}
        }
    }

    Ok(product)
}

fn parse_f64(s: &str, lineno: usize, what: &str, mode: ParseMode) -> Result<f64, FormatError> {
    s.parse::<f64>().map_err(|_| {
        if mode == ParseMode::Strict {
            FormatError::located(
                "ORBEX §3",
                FileLocation::at_line(lineno),
                format!("cannot parse {what}: {s:?}"),
            )
        } else {
            FormatError::Format(format!("ORBEX line {lineno}: cannot parse {what}: {s:?}"))
        }
    })
}

fn orbex_epoch_to_mjd(year: i32, doy: u32, sod: f64) -> f64 {
    let epoch = match chrono::NaiveDate::from_yo_opt(year, doy.max(1)) {
        Some(d) => d,
        None => return 0.0,
    };
    let mjd_ref = chrono::NaiveDate::from_ymd_opt(1858, 11, 17).expect("valid MJD epoch");
    let days = (epoch - mjd_ref).num_days() as f64;
    days + sod / 86400.0
}

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

    fn orb_only_text() -> String {
        [
            "%=ORBEX  0.09",
            "%%",
            "+EPHEMERIS/DATA",
            "#ORB",
            "## 2024 21 21600.000000000",
            " G01  1.0E+07  2.0E+06  3.0E+06",
            "-EPHEMERIS/DATA",
            "%ENDORBEX",
        ]
        .join("\n")
    }

    #[test]
    fn read_orbex_orb_entry() {
        let prod = read_orbex(orb_only_text().as_bytes(), ParseMode::Strict).unwrap();
        assert_eq!(prod.orbits.len(), 1);
        assert_eq!(prod.orbits[0].sat, "G01");
        assert_eq!(prod.orbits[0].pos_m[0], 1.0e7);
        assert!(prod.orbits[0].vel_m_s.is_none());
    }

    #[test]
    fn read_orbex_orb_with_velocity() {
        let text = [
            "%=ORBEX  0.09",
            "%%",
            "+EPHEMERIS/DATA",
            "#ORB",
            "## 2024 21 0.0",
            " G02  1.0E+07  0.0  0.0  100.0  200.0  300.0",
            "-EPHEMERIS/DATA",
            "%ENDORBEX",
        ]
        .join("\n");
        let prod = read_orbex(text.as_bytes(), ParseMode::Strict).unwrap();
        assert_eq!(prod.orbits.len(), 1);
        let vel = prod.orbits[0].vel_m_s.unwrap();
        assert_eq!(vel[0], 100.0);
        assert_eq!(vel[1], 200.0);
        assert_eq!(vel[2], 300.0);
    }

    #[test]
    fn read_orbex_clk_entry() {
        let text = [
            "%=ORBEX  0.09",
            "%%",
            "+EPHEMERIS/DATA",
            "#CLK",
            "## 2024 21 0.0",
            " G01  1.5E-7",
            "-EPHEMERIS/DATA",
            "%ENDORBEX",
        ]
        .join("\n");
        let prod = read_orbex(text.as_bytes(), ParseMode::Strict).unwrap();
        assert_eq!(prod.clocks.len(), 1);
        assert_eq!(prod.clocks[0].sat, "G01");
        assert!((prod.clocks[0].bias_s - 1.5e-7).abs() < 1e-15);
    }

    #[test]
    fn read_orbex_att_entry() {
        let text = [
            "%=ORBEX  0.09",
            "%%",
            "+EPHEMERIS/DATA",
            "#ATT",
            "## 2024 21 0.0",
            " G01  1.0  0.0  0.0  0.0",
            "-EPHEMERIS/DATA",
            "%ENDORBEX",
        ]
        .join("\n");
        let prod = read_orbex(text.as_bytes(), ParseMode::Strict).unwrap();
        assert_eq!(prod.attitudes.len(), 1);
        assert_eq!(prod.attitudes[0].quaternion[0], 1.0);
    }

    #[test]
    fn read_orbex_short_record_strict_is_error() {
        let text = [
            "%=ORBEX  0.09",
            "+EPHEMERIS/DATA",
            "#ORB",
            "## 2024 21 0.0",
            " G01  1.0E+07",
            "-EPHEMERIS/DATA",
            "%ENDORBEX",
        ]
        .join("\n");
        assert!(read_orbex(text.as_bytes(), ParseMode::Strict).is_err());
    }

    #[test]
    fn read_orbex_short_record_permissive_skips() {
        let text = [
            "%=ORBEX  0.09",
            "+EPHEMERIS/DATA",
            "#ORB",
            "## 2024 21 0.0",
            " G01  1.0E+07",
            "-EPHEMERIS/DATA",
            "%ENDORBEX",
        ]
        .join("\n");
        let prod = read_orbex(text.as_bytes(), ParseMode::Permissive).unwrap();
        assert!(prod.orbits.is_empty());
    }

    #[test]
    fn read_orbex_unknown_section_sets_none() {
        let text = [
            "%=ORBEX  0.09",
            "+EPHEMERIS/DATA",
            "#FOO",
            "## 2024 21 0.0",
            " G01  1.0E+07  2.0  3.0",
            "-EPHEMERIS/DATA",
            "%ENDORBEX",
        ]
        .join("\n");
        let prod = read_orbex(text.as_bytes(), ParseMode::Permissive).unwrap();
        assert!(prod.orbits.is_empty());
        assert!(prod.clocks.is_empty());
        assert!(prod.attitudes.is_empty());
    }

    #[test]
    fn read_orbex_version_parsed() {
        let prod = read_orbex(orb_only_text().as_bytes(), ParseMode::Permissive).unwrap();
        assert_eq!(prod.version, "0.09");
    }
}