rinex 0.22.0

RINEX file parsing, analysis and production
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
use itertools::Itertools;

use std::io::{BufWriter, Write};

use crate::{
    epoch::epoch_decompose as epoch_decomposition,
    error::FormattingError,
    navigation::{NavFrame, NavFrameType, NavKey, Record},
    prelude::{Constellation, Header},
};

pub(crate) struct NavFormatter {
    value: f64,
    width: usize,
    precision: usize,
}

impl NavFormatter {
    pub fn new(value: f64) -> Self {
        Self {
            value,
            width: 15,
            precision: 12,
        }
    }

    pub fn new_iono_alpha_beta(value: f64) -> Self {
        Self {
            value,
            width: 3,
            precision: 4,
        }
    }

    pub fn new_time_system_correction_v2(value: f64) -> Self {
        Self {
            value,
            width: 17,
            precision: 12,
        }
    }

    pub fn new_time_system_correction_v3_offset(value: f64) -> Self {
        Self {
            value,
            width: 14,
            precision: 10,
        }
    }

    pub fn new_time_system_correction_v3_drift(value: f64) -> Self {
        Self {
            value,
            width: 13,
            precision: 9,
        }
    }
}

impl std::fmt::Display for NavFormatter {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        let value = self.value;
        let sign_str = if value.is_sign_positive() { " " } else { "" };
        let formatted = format!(
            "{:width$.precision$E}",
            value,
            width = self.width,
            precision = self.precision
        );

        // reformat exponent
        let parts = formatted.split('E').collect::<Vec<_>>();

        if parts.len() == 2 {
            let (base, exponent) = (parts[0], parts[1]);
            let exp_sign = if exponent.starts_with('-') { "-" } else { "+" };
            let exp_value = exponent
                .trim_start_matches(&['+', '-'][..])
                .parse::<i32>()
                .unwrap();
            let formatted_exponent = format!("{}{:02}", exp_sign, exp_value);
            write!(f, "{}{}E{}", sign_str, base, formatted_exponent)
        } else {
            write!(f, "{}", formatted)
        }
    }
}

fn format_epoch_v2v3<W: Write>(
    w: &mut BufWriter<W>,
    k: &NavKey,
    v2: bool,
    file_constell: &Constellation,
) -> std::io::Result<()> {
    let (yyyy, m, d, hh, mm, ss, nanos) = epoch_decomposition(k.epoch);

    let decis = nanos / 100_000;

    if v2 && *file_constell != Constellation::Mixed {
        write!(
            w,
            "{:02} {:02} {:02} {:02} {:02} {:02} {:2}.{:01}",
            k.sv.prn,
            yyyy - 2000,
            m,
            d,
            hh,
            mm,
            ss,
            decis
        )
    } else {
        write!(
            w,
            "{:x} {:04} {:02} {:02} {:02} {:02} {:02}",
            k.sv, yyyy, m, d, hh, mm, ss
        )
    }
}

fn format_epoch_v4<W: Write>(w: &mut BufWriter<W>, k: &NavKey) -> std::io::Result<()> {
    let (yyyy, m, d, hh, mm, ss, _) = epoch_decomposition(k.epoch);
    match k.frmtype {
        NavFrameType::Ephemeris => {
            write!(
                w,
                "> EPH {:x} {}\n{:x} {:04} {:02} {:02} {:02} {:02} {:02}",
                k.sv, k.msgtype, k.sv, yyyy, m, d, hh, mm, ss
            )
        },
        NavFrameType::IonosphereModel => {
            write!(
                w,
                "> ION {:x} {}\n        {:04} {:02} {:02} {:02} {:02} {:02}",
                k.sv, k.msgtype, yyyy, m, d, hh, mm, ss
            )
        },
        NavFrameType::SystemTimeOffset => {
            write!(
                w,
                "> STO {:x} {}\n        {:04} {:02} {:02} {:02} {:02} {:02}",
                k.sv, k.msgtype, yyyy, m, d, hh, mm, ss
            )
        },
        NavFrameType::EarthOrientation => {
            write!(
                w,
                "> EOP {:x} {}\n        {:04} {:02} {:02} {:02} {:02} {:02}",
                k.sv, k.msgtype, yyyy, m, d, hh, mm, ss
            )
        },
    }
}

pub fn format<W: Write>(
    writer: &mut BufWriter<W>,
    rec: &Record,
    header: &Header,
) -> Result<(), FormattingError> {
    let version = header.version;

    let v2 = version.major < 3;
    let v4 = version.major > 3;

    let file_constell = header
        .constellation
        .ok_or(FormattingError::UndefinedConstellation)?;

    // in chronological order
    for epoch in rec.iter().map(|(k, _v)| k.epoch).unique().sorted() {
        // per sorted constellations
        for constell in rec
            .iter()
            .filter_map(|(k, _v)| {
                if k.epoch == epoch {
                    Some(k.sv.constellation)
                } else {
                    None
                }
            })
            .unique()
            .sorted()
        {
            // per sorted SV
            for sv in rec
                .iter()
                .filter_map(|(k, _v)| {
                    if k.epoch == epoch && k.sv.constellation == constell {
                        Some(k.sv)
                    } else {
                        None
                    }
                })
                .unique()
                .sorted()
            {
                // per sorted frame type
                for frmtype in rec
                    .iter()
                    .filter_map(|(k, _v)| {
                        if k.epoch == epoch && k.sv == sv {
                            Some(k.frmtype)
                        } else {
                            None
                        }
                    })
                    .unique()
                    .sorted()
                {
                    // format this entry
                    if let Some((k, v)) = rec
                        .iter()
                        .filter(|(k, _v)| k.epoch == epoch && k.sv == sv && k.frmtype == frmtype)
                        .reduce(|k, _| k)
                    {
                        // format epoch
                        if v4 {
                            format_epoch_v4(writer, k)?;
                        } else {
                            format_epoch_v2v3(writer, k, v2, &file_constell)?;
                        }

                        // format entry
                        match v {
                            NavFrame::EPH(eph) => eph.format(writer, k.sv, version, k.msgtype)?,
                            _ => {},
                        };
                    }
                }
            }
        }
    }
    Ok(())
}

#[cfg(test)]
mod test {

    use super::{format_epoch_v2v3, format_epoch_v4, NavFormatter};
    use crate::navigation::{NavFrameType, NavKey, NavMessageType};
    use crate::prelude::{Constellation, Epoch, SV};
    use crate::tests::formatting::Utf8Buffer;
    use std::io::BufWriter;
    use std::str::FromStr;

    #[test]
    fn nav_formatter() {
        for (value, expected) in [
            (0.0, " 0.000000000000E+00"),
            (1.0, " 1.000000000000E+00"),
            (9.9, " 9.900000000000E+00"),
            (-1.0, "-1.000000000000E+00"),
            (-10.0, "-1.000000000000E+01"),
            (-0.123, "-1.230000000000E-01"),
            (0.123, " 1.230000000000E-01"),
        ] {
            let formatted = NavFormatter::new(value);
            assert_eq!(formatted.to_string(), expected);
        }
    }

    #[test]
    fn system_time_corr_v2_formatter() {
        for (value, expected) in [(-1.862645149231E-09, "-1.862645149231E-09")] {
            let formatted = NavFormatter::new_time_system_correction_v2(value);
            assert_eq!(formatted.to_string(), expected);
        }
    }

    #[test]
    fn system_time_corr_v3_formatter() {
        for (value, expected) in [
            (0.0000000000E+00, " 0.0000000000E+00"),
            (2.1536834538E-09, " 2.1536834538E-09"),
        ] {
            let formatted = NavFormatter::new_time_system_correction_v3_offset(value);
            assert_eq!(formatted.to_string(), expected);
        }

        for (value, expected) in [
            (-3.019806627E-14, "-3.019806627E-14"),
            (-9.769962617E-15, "-9.769962617E-15"),
        ] {
            let formatted = NavFormatter::new_time_system_correction_v3_drift(value);
            assert_eq!(formatted.to_string(), expected);
        }
    }

    #[test]
    fn nav_fmt_v2v3() {
        let gal = Constellation::Galileo;
        let buf = Utf8Buffer::new(1024);
        let mut writer = BufWriter::new(buf);

        let key = NavKey {
            epoch: Epoch::from_str("2023-01-01T00:00:00 UTC").unwrap(),
            sv: SV::from_str("E01").unwrap(),
            frmtype: NavFrameType::from_str("EOP").unwrap(),
            msgtype: NavMessageType::from_str("LNAV").unwrap(),
        };

        format_epoch_v2v3(&mut writer, &key, true, &gal).unwrap();

        let inner = writer.into_inner().unwrap();

        let utf8_ascii = inner.to_ascii_utf8();

        assert_eq!(&utf8_ascii, "01 23 01 01 00 00  0.0");
    }

    #[test]
    fn navfmt_v4_ephemeris() {
        let buf = Utf8Buffer::new(1024);
        let mut writer = BufWriter::new(buf);

        let key = NavKey {
            epoch: Epoch::from_str("2023-03-12T00:00:00 UTC").unwrap(),
            sv: SV::from_str("G01").unwrap(),
            frmtype: NavFrameType::from_str("EPH").unwrap(),
            msgtype: NavMessageType::from_str("LNAV").unwrap(),
        };

        format_epoch_v4(&mut writer, &key).unwrap();

        let inner = writer.into_inner().unwrap();

        let utf8_ascii = inner.to_ascii_utf8();

        assert_eq!(
            &utf8_ascii,
            "> EPH G01 LNAV
G01 2023 03 12 00 00 00"
        );
    }

    #[test]
    fn navfmt_v4_iono() {
        let buf = Utf8Buffer::new(1024);
        let mut writer = BufWriter::new(buf);

        let key = NavKey {
            epoch: Epoch::from_str("2023-03-12T00:08:54 UTC").unwrap(),
            sv: SV::from_str("G12").unwrap(),
            frmtype: NavFrameType::from_str("ION").unwrap(),
            msgtype: NavMessageType::from_str("LNAV").unwrap(),
        };

        format_epoch_v4(&mut writer, &key).unwrap();

        let inner = writer.into_inner().unwrap();

        let utf8_ascii = inner.to_ascii_utf8();

        assert_eq!(
            &utf8_ascii,
            "> ION G12 LNAV
        2023 03 12 00 08 54"
        );
    }

    #[test]
    fn navfmt_v4_systime() {
        let buf = Utf8Buffer::new(1024);
        let mut writer = BufWriter::new(buf);

        let key = NavKey {
            epoch: Epoch::from_str("2023-03-12T00:20:00 UTC").unwrap(),
            sv: SV::from_str("C21").unwrap(),
            frmtype: NavFrameType::from_str("STO").unwrap(),
            msgtype: NavMessageType::from_str("CNVX").unwrap(),
        };

        format_epoch_v4(&mut writer, &key).unwrap();

        let inner = writer.into_inner().unwrap();

        let utf8_ascii = inner.to_ascii_utf8();

        assert_eq!(
            &utf8_ascii,
            "> STO C21 CNVX
        2023 03 12 00 20 00"
        );
    }

    #[test]
    fn navfmt_v4_eop() {
        let buf = Utf8Buffer::new(1024);
        let mut writer = BufWriter::new(buf);

        let key = NavKey {
            epoch: Epoch::from_str("2023-03-14T16:51:12 UTC").unwrap(),
            sv: SV::from_str("G27").unwrap(),
            frmtype: NavFrameType::from_str("EOP").unwrap(),
            msgtype: NavMessageType::from_str("CNVX").unwrap(),
        };

        format_epoch_v4(&mut writer, &key).unwrap();

        let inner = writer.into_inner().unwrap();

        let utf8_ascii = inner.to_ascii_utf8();

        assert_eq!(
            &utf8_ascii,
            "> EOP G27 CNVX
        2023 03 14 16 51 12"
        );
    }
}