ionex 0.0.1-alpha

IONEX (Ionosphere Maps) parser
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
use crate::{
    error::ParsingError,
    grid::GridSpecs,
    prelude::{Comments, Epoch, Header, Key, Quantized, QuantizedCoordinates, Record, TEC},
};

use std::{
    io::{BufRead, BufReader, Read},
    str::FromStr,
};

fn is_new_tec_map(line: &str) -> bool {
    line.contains("START OF TEC MAP")
}

fn is_new_rms_map(line: &str) -> bool {
    line.contains("START OF RMS MAP")
}

fn is_new_height_map(line: &str) -> bool {
    line.contains("START OF HEIGHT MAP")
}

/// Parses all maps contained in following TEC description.
/// This describes a serie of TEC point on an isosurface.
/// ## Inputs
///   - content: readable content (ASCII UTF-8)
///   - lat_exponent: deduced from IONEX header for coordinates quantization
///   - long_exponent: deduced from IONEX header for coordinates quantization
///   - tec_exponent: kept up to date, for correct data interpretation
///   - epoch: kept up to date, for correct classification
fn parse_tec_map(
    content: &str,
    lat_exponent: i8,
    long_exponent: i8,
    alt_exponent: i8,
    tec_exponent: i8,
    epoch: Epoch,
    record: &mut Record,
) -> Result<(), ParsingError> {
    const NON_AVAILABLE_TEC_KEYWORD: &str = "9999";
    let lines = content.lines();

    let mut long_ptr = 0.0_f64;
    let mut grid_specs = GridSpecs::default();

    for line in lines {
        if line.len() > 60 {
            let (content, marker) = line.split_at(60);

            // Handle special cases
            // * data scaling update
            if marker.contains("LAT/LON1/LON2/DLON/H") {
                grid_specs = GridSpecs::from_str(content)?;
                long_ptr = grid_specs.longitude_space.start; // reset pointer
                continue; // avoid parsing
            } else if marker.contains("END OF TEC MAP") {
                // end of this block
                return Ok(());
            }
        }

        // proceed to parsing
        for item in line.split_ascii_whitespace() {
            // data interpretation
            let item = item.trim();
            if item != NON_AVAILABLE_TEC_KEYWORD {
                if let Ok(tecu) = item.parse::<i64>() {
                    let tec = TEC::from_quantized(tecu, tec_exponent);

                    let quantized_lat = Quantized::new(grid_specs.latitude_ddeg, lat_exponent);
                    let quantized_long = Quantized::new(long_ptr, long_exponent);
                    let quantized_alt = Quantized::new(grid_specs.altitude_km, alt_exponent);

                    let coordinates = QuantizedCoordinates::from_quantized(
                        quantized_lat,
                        quantized_long,
                        quantized_alt,
                    );

                    let key = Key { epoch, coordinates };

                    record.insert(key, tec);
                }
            }

            long_ptr += grid_specs.longitude_space.spacing;
        }
    }

    Ok(())
}

/// Parses all RMS maps contained in following content.
/// This describes the RMS value of each TEC previously parsed, for current isosurface.
/// ## Inputs
///   - content: readable content (ASCII UTF-8)
///   - lat_exponent: deduced from IONEX header for coordinates quantization
///   - long_exponent: deduced from IONEX header for coordinates quantization
///   - tec_exponent: kept up to date, for correct data interpretation
///   - epoch: epoch of current map
fn parse_rms_map(
    content: &str,
    lat_exponent: i8,
    long_exponent: i8,
    alt_exponent: i8,
    tec_exponent: i8,
    epoch: Epoch,
    record: &mut Record,
) -> Result<(), ParsingError> {
    const NON_AVAILABLE_TEC_KEYWORD: &str = "9999";

    let lines = content.lines();

    let mut long_ptr = 0.0_f64;
    let mut grid_specs = GridSpecs::default();

    for line in lines {
        if line.len() > 60 {
            let marker = line.split_at(60).1;

            if marker.contains("END OF RMS MAP") {
                return Ok(());
            } else if marker.contains("EXPONENT") {
                // should not have been presented (handled @ higher level)
                continue; // avoid parsing
            } else if marker.contains("START OF") {
                continue; // avoid parsing
            } else if marker.contains("LAT/LON1/LON2/DLON/H") {
                grid_specs = GridSpecs::from_str(content)?;
                long_ptr = grid_specs.longitude_space.start; // reset ptr
                continue; // avoid parsing
            } else if marker.contains("END OF RMS MAP") {
                // end of this block
                return Ok(());
            }
        }

        // proceed to parsing
        for item in line.split_ascii_whitespace() {
            let item = item.trim();

            if item != NON_AVAILABLE_TEC_KEYWORD {
                if let Ok(rms_tecu) = item.parse::<i64>() {
                    let quantized_lat = Quantized::new(grid_specs.latitude_ddeg, lat_exponent);
                    let quantized_long = Quantized::new(long_ptr, long_exponent);
                    let quantized_alt = Quantized::new(grid_specs.altitude_km, alt_exponent);

                    let coordinates = QuantizedCoordinates::from_quantized(
                        quantized_lat,
                        quantized_long,
                        quantized_alt,
                    );

                    // we only augment previously parsed TEC values
                    let key = Key { epoch, coordinates };
                    if let Some(v) = record.get_mut(&key) {
                        v.set_quantized_root_mean_square(rms_tecu, tec_exponent);
                    }
                }
            }

            long_ptr += grid_specs.longitude_space.spacing;
        }
    }
    Ok(())
}

// /// Parses all Height map contained in following content.
// /// Adjust the previously parsed isosurface to turn them into a real volume definition.
// /// ## Inputs
// ///   - content: readable content (ASCII UTF-8)
// ///   - lat_exponent: deduced from IONEX header for coordinates quantization
// ///   - long_exponent: deduced from IONEX header for coordinates quantization
// ///   - tec_exponent: kept up to date, for correct data interpretation
// ///   - epoch: epoch of current map
// pub fn parse_height_map(
//     content: &str,
//     lat_exponent: i8,
//     long_exponent: i8,
//     alt_exponent: i8,
//     tec_exponent: i8,
//     epoch: Epoch,
//     record: &mut Record,
// ) -> Result<(), ParsingError> {
//     let lines = content.lines();
//     let mut epoch = Epoch::default();

//     let mut fixed_lat = 0.0_f64;
//     let (mut long1, mut long_spacing) = (0.0_f64, 0.0_f64);
//     let mut fixed_alt = 0.0_f64;

//     let mut long = 0.0_f64; // current longitude (pointer)

//     for line in lines {
//         if line.len() > 60 {
//             let marker = line.split_at(60).1;
//             if marker.contains("END OF HEIGHT MAP") {
//                 return Ok(());
//             } else if marker.contains("EXPONENT") {
//                 // should not have been presented (handled @ higher level)
//                 continue; // avoid parsing
//             }
//         }

//         // proceed to parsing
//         for item in line.split_ascii_whitespace() {
//             if let Ok(h_km) = item.trim().parse::<i32>() {
//                 // Should adjust the altitude we previously parsed
//             }

//             long += long_spacing;
//         }
//     }
//     Ok(())
// }

pub(crate) fn parse_record<R: Read>(
    header: &mut Header,
    reader: &mut BufReader<R>,
) -> Result<(Record, Comments), ParsingError> {
    Ok((Default::default(), Default::default()))
}

#[cfg(test)]
mod test {
    use super::{is_new_height_map, is_new_rms_map, is_new_tec_map, parse_tec_map, Quantized};

    use crate::prelude::{Epoch, Key, QuantizedCoordinates, Record};

    #[test]
    fn ionex_keywords() {
        assert!(is_new_tec_map(
            "1                                                      START OF TEC MAP"
        ));

        assert!(is_new_rms_map(
            "1                                                      START OF RMS MAP"
        ));

        assert!(is_new_height_map(
            "1                                                   START OF HEIGHT MAP"
        ));
    }

    #[test]
    fn tec_map_parsing() {
        let mut record = Record::default();

        let tec_exponent = -1;
        let lat_exponent = Quantized::find_exponent(2.5);
        let long_exponent = Quantized::find_exponent(5.0);
        let alt_exponent = Quantized::find_exponent(0.0);

        let epoch = Epoch::from_gregorian_utc_at_midnight(2017, 1, 1);

        let content =
            "     1                                                      START OF TEC MAP    
  2017     1     1     0     0     0                        EPOCH OF CURRENT MAP
    87.5-180.0 180.0   5.0 450.0                            LAT/LON1/LON2/DLON/H
   33   33   32   32   32   31   31   30   30   30   29   29   28   28   28   27
   27   27   26   26   26   26   26   26   26   26   26   26   26   26   26   26
   27   27   27   28   28   29   29   30   30   31   31   32   32   33   33   33
   34   34   35   35   35   35   36   36   36   36   36   36   36   36   36   35
   35   35   35   35   34   34   34   33   33
    85.0-180.0 180.0   5.0 450.0                            LAT/LON1/LON2/DLON/H
   36   36   35   35   34   34   33   33   32   31   31   30   29   28   28   27
   26   25   25   24   24   23   23   22   22   22   22   22   22   23   23   24
   24   25   25   26   27   28   29   29   30   31   32   33   34   35   36   37
   38   39   39   40   41   41   41   41   42   42   42   41   41   41   41   40
   40   40   39   39   38   38   37   37   36
    27.5-180.0 180.0   5.0 450.0                            LAT/LON1/LON2/DLON/H
   235  230  222  212  200  187  173  157  141  126  110   95   92   92   92   92
    92   92   92   92   92   92   92   92   92   92   92   92   92   92   92   92
    92   92   92   92   92   92   92   92   92   92   92   92   92   92   92   92
    92   92   92   92   92   92   92   92   92  104  120  136  151  166  180  193
   205  215  224  231  236  239  240  239  235
     2.5-180.0 180.0   5.0 450.0                            LAT/LON1/LON2/DLON/H
   364  370  374  378  380  380  378  375  370  364  356  346  336  324  311  298
   283  269  253  238  222  207  191  175  159  143  127  111   96   92   92   92
    92   92   92   92   92   92   92   92   92   92   92   92   92   92   92   92
    92   92   92   92   92  106  124  141  158  175  191  207  223  238  252  266
   280  293  305  317  328  339  348  356  364
    -2.5-180.0 180.0   5.0 450.0                            LAT/LON1/LON2/DLON/H
   363  370  375  380  383  385  385  384  381  376  370  363  354  343  332  319
   305  291  276  260  244  227  210  194  176  159  143  126  109   93   92   92
    92   92   92   92   92   92   92   92   92   92   92   92   92   92   92   92
    92   92   92   92  103  120  136  152  168  183  198  212  226  240  253  266
   279  291  303  315  326  336  346  355  363
     1                                                      END OF TEC MAP      ";

        parse_tec_map(
            content,
            lat_exponent,
            long_exponent,
            alt_exponent,
            tec_exponent,
            epoch,
            &mut record,
        )
        .unwrap();

        for (coordinates, quantized_tecu) in [
            (
                QuantizedCoordinates::new(
                    87.5,
                    lat_exponent,
                    -180.0,
                    long_exponent,
                    450.0,
                    alt_exponent,
                ),
                33,
            ),
            (
                QuantizedCoordinates::new(
                    87.5,
                    lat_exponent,
                    -175.0,
                    long_exponent,
                    450.0,
                    alt_exponent,
                ),
                33,
            ),
            (
                QuantizedCoordinates::new(
                    87.5,
                    lat_exponent,
                    -170.0,
                    long_exponent,
                    450.0,
                    alt_exponent,
                ),
                32,
            ),
            (
                QuantizedCoordinates::new(
                    87.5,
                    lat_exponent,
                    170.0,
                    long_exponent,
                    450.0,
                    alt_exponent,
                ),
                34,
            ),
            (
                QuantizedCoordinates::new(
                    87.5,
                    lat_exponent,
                    175.0,
                    long_exponent,
                    450.0,
                    alt_exponent,
                ),
                33,
            ),
            (
                QuantizedCoordinates::new(
                    87.5,
                    lat_exponent,
                    180.0,
                    long_exponent,
                    450.0,
                    alt_exponent,
                ),
                33,
            ),
            (
                QuantizedCoordinates::new(
                    85.0,
                    lat_exponent,
                    -180.0,
                    long_exponent,
                    450.0,
                    alt_exponent,
                ),
                36,
            ),
            (
                QuantizedCoordinates::new(
                    85.0,
                    lat_exponent,
                    -175.0,
                    long_exponent,
                    450.0,
                    alt_exponent,
                ),
                36,
            ),
            (
                QuantizedCoordinates::new(
                    85.0,
                    lat_exponent,
                    -170.0,
                    long_exponent,
                    450.0,
                    alt_exponent,
                ),
                35,
            ),
            (
                QuantizedCoordinates::new(
                    85.0,
                    lat_exponent,
                    170.0,
                    long_exponent,
                    450.0,
                    alt_exponent,
                ),
                37,
            ),
            (
                QuantizedCoordinates::new(
                    85.0,
                    lat_exponent,
                    175.0,
                    long_exponent,
                    450.0,
                    alt_exponent,
                ),
                37,
            ),
            (
                QuantizedCoordinates::new(
                    85.0,
                    lat_exponent,
                    180.0,
                    long_exponent,
                    450.0,
                    alt_exponent,
                ),
                36,
            ),
            (
                QuantizedCoordinates::new(
                    27.5,
                    lat_exponent,
                    170.0,
                    long_exponent,
                    450.0,
                    alt_exponent,
                ),
                240,
            ),
            (
                QuantizedCoordinates::new(
                    27.5,
                    lat_exponent,
                    175.0,
                    long_exponent,
                    450.0,
                    alt_exponent,
                ),
                239,
            ),
            (
                QuantizedCoordinates::new(
                    27.5,
                    lat_exponent,
                    180.0,
                    long_exponent,
                    450.0,
                    alt_exponent,
                ),
                235,
            ),
            (
                QuantizedCoordinates::new(
                    2.5,
                    lat_exponent,
                    -170.0,
                    long_exponent,
                    450.0,
                    alt_exponent,
                ),
                374,
            ),
            (
                QuantizedCoordinates::new(
                    2.5,
                    lat_exponent,
                    170.0,
                    long_exponent,
                    450.0,
                    alt_exponent,
                ),
                348,
            ),
            (
                QuantizedCoordinates::new(
                    2.5,
                    lat_exponent,
                    175.0,
                    long_exponent,
                    450.0,
                    alt_exponent,
                ),
                356,
            ),
            (
                QuantizedCoordinates::new(
                    2.5,
                    lat_exponent,
                    180.0,
                    long_exponent,
                    450.0,
                    alt_exponent,
                ),
                364,
            ),
            (
                QuantizedCoordinates::new(
                    -2.5,
                    lat_exponent,
                    -170.0,
                    long_exponent,
                    450.0,
                    alt_exponent,
                ),
                375,
            ),
            (
                QuantizedCoordinates::new(
                    -2.5,
                    lat_exponent,
                    170.0,
                    long_exponent,
                    450.0,
                    alt_exponent,
                ),
                346,
            ),
            (
                QuantizedCoordinates::new(
                    -2.5,
                    lat_exponent,
                    175.0,
                    long_exponent,
                    450.0,
                    alt_exponent,
                ),
                355,
            ),
            (
                QuantizedCoordinates::new(
                    -2.5,
                    lat_exponent,
                    180.0,
                    long_exponent,
                    450.0,
                    alt_exponent,
                ),
                363,
            ),
        ] {
            let key = Key { epoch, coordinates };

            let tec = record
                .get(&key)
                .expect(&format!("missing value at {:#?}", key));

            let tecu = tec.tecu();
            let expected = quantized_tecu as f64 * 10.0_f64.powi(tec_exponent as i32);
            let err = (tecu - expected).abs();

            assert!(err < 1.0E-6);
        }
    }
}