datui-lib 0.4.4

Data Exploration in the Terminal (library)
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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
//! GPS logs (NMEA 0183, GPX) as a table. Neither scans in place, so an open reads the
//! file once in pieces, writing rows a batch at a time to a temp Arrow IPC file (claimed
//! via the open's `Writer`) that the dataset scans lazily. A GPX extension field
//! appearing late starts a new segment file with the wider schema, joined with nulls;
//! NMEA is always one segment. Readers are incremental, so a growing log can be
//! followed later.

pub mod gpx;
pub mod nmea;

use std::path::{Path, PathBuf};
use std::sync::atomic::AtomicU64;

use color_eyre::Result;
use color_eyre::eyre::eyre;
use polars::prelude::*;

use crate::cloud::download::TempDownload;
use crate::formats::segments::Converted;
use crate::formats::text_formats::{count, note, read_through};
use crate::loading::unfinished::Writer;
use crate::notes::Note;
use crate::numfmt::group_chrome;
use crate::{FileFormat, OpenOptions};

/// What datui does with an NMEA log: see [`crate::formats::readers`].
pub(crate) const NMEA: crate::formats::readers::Reader = crate::formats::readers::Reader {
    // Several logs are one table.
    convert: Some(|input| {
        let (converted, detail) = convert(
            input.files,
            input.display,
            input.format,
            input.options,
            input.writer,
            input.read,
        )?;
        Ok((converted, Some(std::sync::Arc::new(detail))))
    }),
    scan: crate::formats::readers::read_into,
    signatures: &[crate::formats::readers::Signature {
        says: |head, _| nmea::looks_like(head),
        kind: crate::formats::readers::Kind::Text,
        trusted: crate::formats::readers::Trusted {
            listing: false,
            tables: true,
            ..crate::formats::readers::EVERYWHERE
        },
    }],
    // The tables are the sentence types read, whichever the log holds: listing them
    // reads nothing.
    tables: Some(|_| Ok(nmea_tables())),
    ..crate::formats::readers::BASE
};

/// The tables of an NMEA log, as the home screen lists them and `--table` names them.
fn nmea_tables() -> Vec<crate::formats::sqlite::Table> {
    nmea::Table::ALL
        .into_iter()
        .map(|t| {
            let columns = t.columns().into_iter().map(|(name, _)| name);
            crate::formats::sqlite::Table::plain(t.name(), "table", columns)
        })
        .collect()
}

/// What datui does with a GPX file: see [`crate::formats::readers`].
pub(crate) const GPX: crate::formats::readers::Reader = crate::formats::readers::Reader {
    // Several logs are one table.
    convert: Some(|input| {
        let (converted, detail) = convert(
            input.files,
            input.display,
            input.format,
            input.options,
            input.writer,
            input.read,
        )?;
        Ok((converted, Some(std::sync::Arc::new(detail))))
    }),
    scan: crate::formats::readers::read_into,
    signatures: &[crate::formats::readers::Signature {
        says: |head, _| gpx::looks_like(head),
        kind: crate::formats::readers::Kind::Text,
        trusted: crate::formats::readers::Trusted {
            listing: false,
            ..crate::formats::readers::EVERYWHERE
        },
    }],
    ..crate::formats::readers::BASE
};

/// Read `files` (shown as `display`) as `format` into temp IPC files via `writer`,
/// counting bytes read in `read`. Several files form one table with a leading `file`
/// column; read notes are summed for Info's GPS tab.
pub(crate) fn convert(
    files: &[PathBuf],
    display: &Path,
    format: FileFormat,
    options: &OpenOptions,
    writer: &Writer,
    read: &AtomicU64,
) -> Result<(Converted, crate::formats::text_formats::Detail)> {
    let [file] = files else {
        return convert_many(files, format, options, writer, read);
    };
    let one = convert_one(file, display, format, options, writer, read)?;
    let (notes, other_tables) = one.stats.notes(None, options)?;
    let detail = one.stats.detail(None, &one.extent, options)?;
    Ok((
        Converted {
            lf: one.lf,
            files: one.files,
            notes,
            other_tables,
        },
        detail,
    ))
}

/// Each of `files` read as [`convert_one`] reads it, then stacked under a `file`
/// column. A GPX file's fields are typed in that file, and stacked as one type where
/// two files disagree.
fn convert_many(
    files: &[PathBuf],
    format: FileFormat,
    options: &OpenOptions,
    writer: &Writer,
    read: &AtomicU64,
) -> Result<(Converted, crate::formats::text_formats::Detail)> {
    let mut frames = Vec::with_capacity(files.len());
    let mut written = Vec::new();
    let mut stats: Option<Stats> = None;
    let mut extent = Extent::default();
    for file in files {
        let one = convert_one(file, file, format, options, writer, read)
            .map_err(|e| crate::error_display::in_file(file, e))?;
        let name = file
            .file_name()
            .map(|n| n.to_string_lossy().into_owned())
            .unwrap_or_else(|| file.display().to_string());
        frames.push(one.lf.select([lit(name).alias("file"), all().as_expr()]));
        written.extend(one.files);
        extent.merge(&one.extent);
        match &mut stats {
            None => stats = Some(one.stats),
            Some(stats) => stats.absorb(one.stats),
        }
    }
    let stats = stats.ok_or_else(|| eyre!("No GPS logs to read."))?;
    let lf = concat(
        &frames,
        UnionArgs {
            diagonal: true,
            to_supertypes: true,
            ..Default::default()
        },
    )?;
    let (notes, other_tables) = stats.notes(Some(files.len()), options)?;
    let detail = stats.detail(Some(files.len()), &extent, options)?;
    Ok((
        Converted {
            lf,
            files: written,
            notes,
            other_tables,
        },
        detail,
    ))
}

/// What one log's read noticed, of either format.
enum Stats {
    Gpx {
        stats: gpx::Stats,
        /// Files that end inside an element.
        truncated: usize,
    },
    Nmea {
        stats: nmea::Stats,
        /// Files with rows and no sentence that gives the date.
        undated: usize,
    },
}

impl Stats {
    /// Count `other`, another file's, in with these.
    fn absorb(&mut self, other: Stats) {
        match (self, other) {
            (
                Stats::Gpx { stats, truncated },
                Stats::Gpx {
                    stats: more,
                    truncated: also,
                },
            ) => {
                stats.points += more.points;
                stats.tracks += more.tracks;
                stats.routes += more.routes;
                stats.waypoints += more.waypoints;
                stats.fields_dropped += more.fields_dropped;
                stats.long_names += more.long_names;
                stats.bad_times += more.bad_times;
                stats.truncated |= more.truncated;
                *truncated += also;
            }
            (
                Stats::Nmea { stats, undated },
                Stats::Nmea {
                    stats: more,
                    undated: also,
                },
            ) => {
                stats.absorb(&more);
                *undated += also;
            }
            // One format per open.
            _ => {}
        }
    }

    /// The notes for the Notes tab, and the other tables for the Schema tab, of `of`
    /// files (`None` for one).
    fn notes(&self, of: Option<usize>, options: &OpenOptions) -> Result<(Vec<Note>, Vec<String>)> {
        Ok(match self {
            Stats::Gpx { stats, truncated } => (gpx_notes(stats, of, *truncated), Vec::new()),
            Stats::Nmea { stats, undated } => {
                let table = nmea_table(options)?;
                (
                    nmea_notes(stats, of, *undated),
                    nmea_other_tables(stats, table),
                )
            }
        })
    }
}

impl Stats {
    /// The GPS tab of the Info panel, for `of` files (`None` for one) whose rows
    /// covered `extent`.
    fn detail(
        &self,
        of: Option<usize>,
        extent: &Extent,
        options: &OpenOptions,
    ) -> Result<crate::formats::text_formats::Detail> {
        use crate::formats::model_files::MetaValue;
        let middot = crate::glyphs::get().middot;
        let mut lines = Vec::new();
        let (list_title, list) = match self {
            Stats::Gpx { stats, .. } => {
                lines.push(
                    [
                        count(stats.points, "point", "points"),
                        count(stats.tracks, "track", "tracks"),
                        count(stats.routes, "route", "routes"),
                        count(stats.waypoints, "waypoint", "waypoints"),
                    ]
                    .join(&format!(" {middot} ")),
                );
                ("", Vec::new())
            }
            Stats::Nmea { stats, .. } => {
                let table = nmea_table(options)?;
                lines.push(format!(
                    "{} of {} {middot} {} of {}",
                    count(stats.rows, "row", "rows"),
                    table.name(),
                    count(stats.sentences, "sentence", "sentences"),
                    count(stats.lines, "line", "lines"),
                ));
                let mut list: Vec<(String, MetaValue)> = stats
                    .types
                    .iter()
                    .map(|(t, n)| (t.clone(), MetaValue::Text(group_chrome(*n as usize))))
                    .collect();
                if stats.other_types > 0 {
                    list.push((
                        "other".to_string(),
                        MetaValue::Text(group_chrome(stats.other_types as usize)),
                    ));
                }
                ("Sentences", list)
            }
        };
        if let Some(n) = of {
            lines.push(count(n as u64, "file", "files"));
        }
        lines.extend(extent.lines());
        Ok(crate::formats::text_formats::Detail {
            tab: crate::formats::text_formats::tab(FileFormat::Nmea),
            lines,
            list_title,
            list,
            ..Default::default()
        })
    }
}

/// When and where a log's rows are: the least and most of their `time`, `lat` and
/// `lon`, taken from each batch as it is written, so nothing is read twice.
#[derive(Debug, Clone, Default, PartialEq)]
struct Extent {
    /// Milliseconds since the epoch, UTC.
    time: Option<(i64, i64)>,
    lat: Option<(f64, f64)>,
    lon: Option<(f64, f64)>,
}

impl Extent {
    fn absorb(&mut self, df: &DataFrame) {
        let time = df
            .column("time")
            .ok()
            .and_then(|c| c.as_materialized_series().datetime().ok().cloned())
            .and_then(|t| Some((t.physical().min()?, t.physical().max()?)));
        let span = |name: &str| {
            let c = df.column(name).ok()?;
            let v = c.as_materialized_series().f64().ok()?;
            Some((v.min()?, v.max()?))
        };
        self.merge(&Extent {
            time,
            lat: span("lat"),
            lon: span("lon"),
        });
    }

    fn merge(&mut self, other: &Extent) {
        fn wider<T: PartialOrd + Copy>(a: Option<(T, T)>, b: Option<(T, T)>) -> Option<(T, T)> {
            match (a, b) {
                (Some((a0, a1)), Some((b0, b1))) => {
                    Some((if b0 < a0 { b0 } else { a0 }, if b1 > a1 { b1 } else { a1 }))
                }
                (a, b) => a.or(b),
            }
        }
        self.time = wider(self.time, other.time);
        self.lat = wider(self.lat, other.lat);
        self.lon = wider(self.lon, other.lon);
    }

    /// `Time: 2024-05-01 12:00:00 to 13:05:12 UTC (1:05:12.000)` and
    /// `Bounds: 51.40000 to 51.60000 N, -0.20000 to 0.10000 E`.
    fn lines(&self) -> Vec<String> {
        let mut lines = Vec::new();
        if let Some((first, last)) = self.time {
            let at = |ms: i64| chrono::DateTime::from_timestamp_millis(ms);
            if let (Some(a), Some(b)) = (at(first), at(last)) {
                let end = if a.date_naive() == b.date_naive() {
                    b.format("%H:%M:%S").to_string()
                } else {
                    b.format("%Y-%m-%d %H:%M:%S").to_string()
                };
                lines.push(format!(
                    "Time: {} to {end} UTC ({})",
                    a.format("%Y-%m-%d %H:%M:%S"),
                    crate::widgets::info::clock((last - first) as f64 / 1000.0)
                ));
            }
        }
        if let (Some((lat0, lat1)), Some((lon0, lon1))) = (self.lat, self.lon) {
            lines.push(format!(
                "Bounds: latitude {lat0:.5} to {lat1:.5}, longitude {lon0:.5} to {lon1:.5}"
            ));
        }
        lines
    }
}

/// The NMEA table `--table` names, the fixes when it names none.
fn nmea_table(options: &OpenOptions) -> Result<nmea::Table> {
    match options.table.as_deref() {
        None => Ok(nmea::Table::Fixes),
        Some(name) => nmea::Table::from_name(name).ok_or_else(|| {
            eyre!(
                "no table \"{name}\". --table names one of an NMEA log's tables: {}.",
                nmea::Table::ALL.map(nmea::Table::name).join(", ")
            )
        }),
    }
}

/// One log read: the frame over its segments, the files they are, and its counts.
struct One {
    lf: LazyFrame,
    files: Vec<TempDownload>,
    stats: Stats,
    extent: Extent,
}

/// Read `file` (named `display` to the user) as `format` into temporary IPC files.
fn convert_one(
    file: &Path,
    display: &Path,
    format: FileFormat,
    options: &OpenOptions,
    writer: &Writer,
    read: &AtomicU64,
) -> Result<One> {
    let mut extent = Extent::default();
    match format {
        FileFormat::Gpx => {
            let mut gpx = gpx::GpxReader::new();
            let (lf, files) = read_through(file, options, writer, read, &mut gpx, |df| {
                extent.absorb(df)
            })?;
            let stats = gpx.stats().clone();
            Ok(One {
                lf: type_gpx_fields(lf, gpx.fields()),
                files,
                extent,
                stats: Stats::Gpx {
                    truncated: usize::from(stats.truncated),
                    stats,
                },
            })
        }
        FileFormat::Nmea => {
            let mut log = nmea::NmeaReader::new(nmea_table(options)?);
            let (lf, files) = read_through(file, options, writer, read, &mut log, |df| {
                extent.absorb(df)
            })?;
            if log.stats().sentences == 0 {
                return Err(crate::error_display::FileError::new(
                    display,
                    "no NMEA sentences: no line starts with $ and a sentence address.",
                )
                .into());
            }
            let stats = log.stats().clone();
            Ok(One {
                lf,
                files,
                extent,
                stats: Stats::Nmea {
                    undated: usize::from(!stats.dated && stats.rows > 0),
                    stats,
                },
            })
        }
        other => Err(eyre!("{} is not a GPS format.", other.name())),
    }
}

impl crate::formats::text_formats::BatchReader for gpx::GpxReader {
    fn push(&mut self, piece: &[u8]) -> Result<()> {
        self.push(piece).map_err(|e| eyre!(e))
    }

    fn take_batch(&mut self) -> PolarsResult<Option<DataFrame>> {
        self.take_batch()
    }

    fn finish(&mut self) -> Result<DataFrame> {
        self.finish().map_err(|e| eyre!(e))
    }
}

impl crate::formats::text_formats::BatchReader for nmea::NmeaReader {
    fn push(&mut self, piece: &[u8]) -> Result<()> {
        self.push(piece);
        Ok(())
    }

    fn take_batch(&mut self) -> PolarsResult<Option<DataFrame>> {
        self.take_batch()
    }

    fn finish(&mut self) -> Result<DataFrame> {
        Ok(self.finish()?)
    }
}

/// A GPX field column as numbers when every value in it was one.
fn type_gpx_fields(lf: LazyFrame, fields: &[gpx::FieldColumn]) -> LazyFrame {
    let casts: Vec<Expr> = fields
        .iter()
        .filter_map(|f| {
            let to = if f.integers {
                DataType::Int64
            } else if f.numbers {
                DataType::Float64
            } else {
                return None;
            };
            Some(col(f.name.as_str()).cast(to))
        })
        .collect();
    if casts.is_empty() {
        lf
    } else {
        lf.with_columns(casts)
    }
}

/// What reading an NMEA log noticed; of `of` logs, `undated` of them without a date.
fn nmea_notes(stats: &nmea::Stats, of: Option<usize>, undated: usize) -> Vec<Note> {
    let mut notes = Vec::new();
    let of_lines = match of {
        None => format!("of {}", count(stats.lines, "line", "lines")),
        Some(n) => format!(
            "of {} in {}",
            count(stats.lines, "line", "lines"),
            count(n as u64, "log", "logs")
        ),
    };
    if stats.skipped > 0 {
        notes.push(note(
            format!(
                "{} left out: not NMEA",
                count(stats.skipped, "line", "lines")
            ),
            of_lines.clone(),
        ));
    }
    if stats.bad_checksums > 0 {
        notes.push(note(
            format!(
                "checksum failed: {} {} checksum_ok false",
                count(stats.bad_checksums, "sentence", "sentences"),
                crate::glyphs::get().middot
            ),
            format!("of {}", count(stats.sentences, "sentence", "sentences")),
        ));
    }
    if undated > 0 {
        let summary = match of {
            None => format!(
                "no RMC or ZDA date {} time empty",
                crate::glyphs::get().middot
            ),
            Some(n) => format!(
                "{undated} of {n} logs without an RMC or ZDA date {} time empty",
                crate::glyphs::get().middot
            ),
        };
        notes.push(note(summary, of_lines.clone()));
    }
    notes
}

/// An NMEA log's other tables (as `--table` names them, with their sentences) when one
/// holds rows `table` lacks (GSA, GSV, ZDA, or another type's table); empty for a
/// fixes-only log.
fn nmea_other_tables(stats: &nmea::Stats, table: nmea::Table) -> Vec<String> {
    use nmea::Table;
    let typed = |t: Table| !matches!(t, Table::Fixes | Table::Sentences);
    let merged = |t: Table| matches!(t, Table::Gga | Table::Rmc | Table::Vtg | Table::Gll);
    let worth = Table::ALL.into_iter().any(|t| {
        t != table && typed(t) && stats.of(t.name()) > 0 && !(table == Table::Fixes && merged(t))
    });
    if !worth {
        return Vec::new();
    }
    Table::ALL
        .into_iter()
        .filter(|t| *t != table)
        .filter_map(|t| match typed(t) {
            false => Some(t.name().to_string()),
            true => {
                let n = stats.of(t.name());
                (n > 0).then(|| format!("{} {}", t.name(), group_chrome(n as usize)))
            }
        })
        .collect()
}

/// What reading a GPX file noticed; of `of` files, `truncated` of them cut short.
fn gpx_notes(stats: &gpx::Stats, of: Option<usize>, truncated: usize) -> Vec<Note> {
    let mut notes = Vec::new();
    let of_points = match of {
        None => format!("of {}", count(stats.points, "point", "points")),
        Some(n) => format!(
            "of {} in {}",
            count(stats.points, "point", "points"),
            count(n as u64, "file", "files")
        ),
    };
    if truncated > 0 {
        let summary = match of {
            None => format!(
                "file cut short inside an element {} points before it shown",
                crate::glyphs::get().middot
            ),
            Some(n) => format!(
                "{truncated} of {n} files cut short inside an element {} points before it shown",
                crate::glyphs::get().middot
            ),
        };
        notes.push(note(summary, of_points.clone()));
    }
    if stats.bad_times > 0 {
        notes.push(note(
            format!(
                "{} left empty: not ISO 8601",
                count(stats.bad_times, "time", "times")
            ),
            of_points.clone(),
        ));
    }
    if stats.fields_dropped > 0 {
        notes.push(note(
            crate::limits::left_out(
                &count(stats.fields_dropped, "field value", "field values"),
                crate::limits::get().gpx_fields,
                "gpx_fields",
            ),
            of_points.clone(),
        ));
    }
    if stats.long_names > 0 {
        notes.push(note(
            format!(
                "{} left out: field name too long",
                count(stats.long_names, "field value", "field values"),
            ),
            of_points,
        ));
    }
    notes
}

#[cfg(test)]
mod tests;