Skip to main content

hpr_flightdata/
altimax.rs

1//! Rocketronics' AltiMAX altimeters: a flight as the AltiMAX PC software exports it to Excel, a
2//! bare `.xls` worksheet ([`xls`](crate::xls)) whose sheet reads as this table.
3//!
4//! The maker's manuals say only that a flight "can be exported to Excel as altitude values"
5//! (*AltiMAX G4 Manual*, Rocketronics, 2021, p. 52); none describes the columns. One header row
6//! names them, and the one export among the sample logs has these eleven:
7//!
8//! ```text
9//! TIME,PRESS FILTER [hpa],HEIGHT FILTER [m],PRESS RAW [hpa],HEIGHT RAW [m],ACCEL [m/s2],
10//! SPEED [m/s],EVENT,EVENT,VOLT. CHANNEL,VOLTAGE [V]
11//! ```
12//!
13//! **Channels.** Each column is read into the record's SI unit ([`Quantity::unit`]) on the
14//! `TIME` clock:
15//!
16//! | Column | Quantity | Read as |
17//! |---|---|---|
18//! | `TIME` | the clock | seconds, though it names no unit: held below |
19//! | `PRESS FILTER [hpa]`, `PRESS RAW [hpa]` | pressure | the unit its values show, held below |
20//! | `HEIGHT FILTER [m]`, `HEIGHT RAW [m]` | altitude above the pad | m: the altimeter sets its sensors "to "0"" when armed (*AltiMAX G3 User Manual*, p. 10) |
21//! | `ACCEL [m/s2]` | acceleration | m/s², as written: below |
22//! | `SPEED [m/s]` | speed | m/s, as written: below |
23//! | `EVENT`, numbers | a number, as written | 0 on a row with no event |
24//! | `EVENT`, text | the flight's events | below |
25//! | `VOLT. CHANNEL` | a number, as written | which voltage the row's `VOLTAGE [V]` is |
26//! | `VOLTAGE [V]` | one voltage channel for each `VOLT. CHANNEL` number, on its own rows | V |
27//!
28//! A column the reader doesn't know is kept as written, as [`Quantity::Unrecognized`], with a
29//! note. A header of more than [`MAX_COLUMNS`] names is refused.
30//!
31//! **The primary track** is `HEIGHT RAW [m]`, the height from the barometer's own reading; the
32//! filtered height is a channel, as an Eggtimer's filtered altitude is.
33//!
34//! **The accelerometer's columns, as the sample shows them.** No manual describes them. `ACCEL`
35//! is measured along an axis pointing down the rocket: in the sample it reads −0.94 g before
36//! liftoff, where an axis pointing up would read +1 g at rest. `SPEED` is `ACCEL` summed over the
37//! clock, gravity left in: over the sample's boost, the median of its change against `ACCEL`'s is
38//! 1 to within 0.2 %. So `SPEED` isn't the rocket's speed, and the record's vertical speed is
39//! left empty; both columns are kept, signs and all, as channels.
40//!
41//! **Units the file doesn't state, or states wrongly, held to physics.** `TIME` names no unit. The
42//! altimeter stores 200 samples a second until apogee and fewer after it (*AltiMAX G4 Manual*,
43//! p. 8 and p. 10), so the clock is read in seconds when its shortest step is 5 ms, within
44//! 10 %; otherwise the file is refused. The pressure columns say `[hpa]`, but the sample's hold
45//! pascals: six-digit values whose heights follow them in pascals, not in hectopascals. Each
46//! pressure column is read in the unit that puts its first value, on the pad, between 500 and
47//! 1,100 hPa: hPa as stated, or Pa, with a note. The two ranges don't overlap, so only one can;
48//! a value in neither is refused.
49//!
50//! **Events.** The two `EVENT` columns are a code and its name. A row whose code isn't 0 is an
51//! event, named as the text column writes it, such as `BARO-LIFTOFF` or `APOGEE PRESSURE`. The
52//! names follow the manual's list of events
53//! (*AltiMAX G3 User Manual*, p. 15), in the software's own spelling; the codes aren't documented,
54//! so they are kept as written.
55//!
56//! **Voltages.** `VOLT. CHANNEL` is a channel number, one a row, and `VOLTAGE [V]` is that
57//! channel's voltage: the reader gives each number its own voltage channel, `VOLTAGE [V] (channel
58//! 1)` and so on, five in the sample. The software lists five voltages, the battery's, the igniter's and three pyro
59//! channels' (*AltiMAX G4 Manual*, p. 42), but not which number is which, so none is named.
60//!
61//! **Cells.** A row holds as many cells as the header; an empty value is a gap, `NaN`, counted in
62//! a note. The clock must increase from row to row, and a row without a time is refused.
63//!
64//! ```
65//! use hpr_flightdata::altimax;
66//!
67//! // An invented export: three rows, the second an event.
68//! let text = "TIME,PRESS FILTER [hpa],HEIGHT FILTER [m],PRESS RAW [hpa],HEIGHT RAW [m],\
69//!             ACCEL [m/s2],SPEED [m/s],EVENT,EVENT,VOLT. CHANNEL,VOLTAGE [V]\n\
70//!             0.005,98000,0,98000,0,-9.8,0,0,PAD,1,7.4\n\
71//!             0.01,97990,0.8,97989,0.9,40,1,3,BARO-LIFTOFF,2,7.3\n\
72//!             0.015,97975,2.1,97974,2.2,60,2,0,PAD,3,0.1\n";
73//! let log = altimax::read(text)?;
74//! assert_eq!(log.altitude_m, [0.0, 0.9, 2.2]);
75//! assert_eq!(log.events[0].name, "BARO-LIFTOFF");
76//! assert_eq!(log.events[0].time_s, 0.01);
77//! # Ok::<(), hpr_flightdata::error::LogError>(())
78//! ```
79
80use std::collections::HashMap;
81
82use crate::error::LogError;
83use crate::log::{Channel, Event, FlightLog, LogFormat, Quantity, Stated};
84use crate::table::{Notes, finite, numbered_lines, short};
85
86const FORMAT: &str = "AltiMAX Excel export";
87
88/// The first five columns, by which the layout is told.
89const MARKS: [&str; 5] = [
90    "TIME",
91    "PRESS FILTER [hpa]",
92    "HEIGHT FILTER [m]",
93    "PRESS RAW [hpa]",
94    "HEIGHT RAW [m]",
95];
96
97const TIME: &str = "time";
98const HEIGHT_RAW: &str = "height raw [m]";
99const EVENT: &str = "event";
100const VOLT_CHANNEL: &str = "volt. channel";
101const VOLTAGE: &str = "voltage [v]";
102
103/// The clock's shortest step, s: 200 samples a second (*AltiMAX G4 Manual*, p. 8).
104const STEP_S: f64 = 0.005;
105
106/// A pad's pressure, Pa: 500 to 1,100 hPa, the band the other readers hold a pressure to.
107const PAD_PA: std::ops::RangeInclusive<f64> = 50_000.0..=110_000.0;
108
109/// The most voltage channel numbers read: the software lists five voltages (*AltiMAX G4 Manual*,
110/// p. 42); more, from a file the reader doesn't know, are refused rather than made channels.
111const VOLT_CHANNELS: usize = 16;
112
113/// The most columns a header may name: the export the reader was built on names eleven. A header
114/// of more names is refused before they are compared, as the AltOS telemetry CSV, Fluctus and
115/// AeroSpaceNU readers refuse one past their own limits, so that a hostile header can't cost time
116/// growing as the square of its width (#1250).
117pub const MAX_COLUMNS: usize = 256;
118
119/// Whether `text` is an AltiMAX export: its first line that isn't blank opens with the five
120/// columns `TIME,PRESS FILTER [hpa],HEIGHT FILTER [m],PRESS RAW [hpa],HEIGHT RAW [m]`, in any
121/// case.
122pub fn recognizes(text: &str) -> bool {
123    numbered_lines(text)
124        .map(|(_, line)| line)
125        .find(|line| !line.trim().is_empty())
126        .is_some_and(|header| {
127            let names: Vec<&str> = header.split(',').map(str::trim).collect();
128            names.len() >= MARKS.len()
129                && MARKS
130                    .iter()
131                    .zip(&names)
132                    .all(|(mark, name)| mark.eq_ignore_ascii_case(name))
133        })
134}
135
136/// What a column is read as.
137#[derive(Clone, Copy, PartialEq)]
138enum Role {
139    Time,
140    /// A pressure, its unit settled by its first value.
141    Pressure,
142    Value(Quantity, f64),
143    /// The event's name, text.
144    EventName,
145    /// A voltage, split by the voltage channel's numbers.
146    Voltage,
147}
148
149fn role(name: &str) -> Role {
150    match name.trim().to_ascii_lowercase().as_str() {
151        TIME => Role::Time,
152        "press filter [hpa]" | "press raw [hpa]" => Role::Pressure,
153        "height filter [m]" | HEIGHT_RAW => Role::Value(Quantity::AltitudeAboveGround, 1.0),
154        "accel [m/s2]" => Role::Value(Quantity::Acceleration, 1.0),
155        "speed [m/s]" => Role::Value(Quantity::Speed, 1.0),
156        EVENT | VOLT_CHANNEL => Role::Value(Quantity::Number, 1.0),
157        VOLTAGE => Role::Voltage,
158        _ => Role::Value(Quantity::Unrecognized, 1.0),
159    }
160}
161
162/// Reads an AltiMAX export's table.
163///
164/// # Errors
165///
166/// [`LogError::NotThisFormat`] when the first line isn't the export's header ([`recognizes`]);
167/// [`LogError::Syntax`] when the header names more than [`MAX_COLUMNS`] columns or a column twice
168/// (but `EVENT`, a code's and a name's), a row's cells aren't as many as the header's, a time is
169/// missing, isn't a number or doesn't increase, a value isn't a number (`NaN` included) or is too
170/// large for pascals, or `VOLT. CHANNEL` holds more than 16 numbers; [`LogError::Unit`] when the
171/// clock has one row or its shortest step isn't 5 ms, or a pressure's first value is a pad's in
172/// neither hPa nor Pa; [`LogError::NoData`] when no row follows the header.
173pub fn read(text: &str) -> Result<FlightLog, LogError> {
174    let text = text.strip_prefix('\u{feff}').unwrap_or(text);
175    if !recognizes(text) {
176        return Err(LogError::NotThisFormat {
177            format: FORMAT,
178            message: format!(
179                "its first line isn't an AltiMAX export's header, which opens `{}`",
180                MARKS.join(",")
181            ),
182        });
183    }
184    let mut lines = numbered_lines(text).filter(|(_, line)| !line.trim().is_empty());
185    // Invariant: `recognizes` found the header line.
186    let Some((header_line, header)) = lines.next() else {
187        return Err(LogError::NoData { format: FORMAT });
188    };
189    let names: Vec<String> = header
190        .split(',')
191        .take(MAX_COLUMNS + 1)
192        .map(|name| name.trim().to_owned())
193        .collect();
194    if names.len() > MAX_COLUMNS {
195        return Err(syntax(
196            header_line,
197            format!("the header names more than {MAX_COLUMNS} columns"),
198        ));
199    }
200    // Each column is named once, but `EVENT`, a code's and a name's: a second `TIME` or `HEIGHT
201    // RAW [m]` would read rows against the wrong clock or track. The names met so far, in lower
202    // case, with how often: a map, so that a header of many names isn't checked in quadratic
203    // time.
204    let mut seen: HashMap<String, usize> = HashMap::new();
205    for name in &names {
206        let same = seen.entry(name.to_ascii_lowercase()).or_insert(0);
207        if *same > usize::from(name.eq_ignore_ascii_case(EVENT)) {
208            return Err(syntax(
209                header_line,
210                format!("the header names {:?} more than once", short(name)),
211            ));
212        }
213        *same += 1;
214    }
215    // The second column named `EVENT` holds the events' names; the first, their codes.
216    let mut roles: Vec<Role> = names.iter().map(|name| role(name)).collect();
217    if let Some(second) = names
218        .iter()
219        .enumerate()
220        .filter(|(_, name)| name.eq_ignore_ascii_case(EVENT))
221        .nth(1)
222        .map(|(index, _)| index)
223    {
224        roles[second] = Role::EventName;
225    }
226    let code = names
227        .iter()
228        .position(|name| name.eq_ignore_ascii_case(EVENT));
229    let volt_channel = names
230        .iter()
231        .position(|name| name.eq_ignore_ascii_case(VOLT_CHANNEL));
232    let mut notes = Notes::default();
233    for (name, role) in names.iter().zip(&roles) {
234        if *role == Role::Value(Quantity::Unrecognized, 1.0) {
235            notes.push(format!(
236                "the column {:?} isn't one this reader knows, and was kept as written",
237                short(name)
238            ));
239        }
240    }
241
242    let mut time_s: Vec<f64> = Vec::new();
243    let mut columns: Vec<Vec<f64>> = vec![Vec::new(); names.len()];
244    let mut event_names: Vec<String> = Vec::new();
245    let mut gaps = vec![0usize; names.len()];
246    let mut row_lines: Vec<usize> = Vec::new();
247    for (number, line) in lines {
248        row_lines.push(number);
249        let cells: Vec<&str> = line.split(',').map(str::trim).collect();
250        if cells.len() != names.len() {
251            return Err(syntax(
252                number,
253                format!(
254                    "the row has {} cells, and the header names {}",
255                    cells.len(),
256                    names.len()
257                ),
258            ));
259        }
260        let mut name_cell = String::new();
261        for (index, (cell, role)) in cells.iter().zip(&roles).enumerate() {
262            match role {
263                Role::EventName => (*cell).clone_into(&mut name_cell),
264                Role::Time => {
265                    let time = cell.parse::<f64>().ok().filter(|time| time.is_finite());
266                    let Some(time) = time else {
267                        return Err(syntax(
268                            number,
269                            format!("the time {:?} isn't a number", short(cell)),
270                        ));
271                    };
272                    if time_s.last().is_some_and(|last| time <= *last) {
273                        return Err(syntax(
274                            number,
275                            format!(
276                                "the time {time} doesn't follow the row before it, {}",
277                                time_s.last().copied().unwrap_or_default()
278                            ),
279                        ));
280                    }
281                    time_s.push(time);
282                }
283                _ => {
284                    let value = if cell.is_empty() {
285                        gaps[index] += 1;
286                        f64::NAN
287                    } else {
288                        let value = cell.parse::<f64>().ok().filter(|value| !value.is_nan());
289                        let value = value.ok_or_else(|| {
290                            syntax(
291                                number,
292                                format!(
293                                    "{:?}, under {:?}, isn't a number",
294                                    short(cell),
295                                    short(&names[index])
296                                ),
297                            )
298                        })?;
299                        finite(value, FORMAT, number, &names[index])?
300                    };
301                    columns[index].push(value);
302                }
303            }
304        }
305        event_names.push(name_cell);
306    }
307    if time_s.is_empty() {
308        return Err(LogError::NoData { format: FORMAT });
309    }
310    clock_in_seconds(&time_s, header_line)?;
311
312    let mut log = FlightLog {
313        format: LogFormat::AltimaxXls,
314        logger: "Rocketronics AltiMAX".to_owned(),
315        serial_number: None,
316        firmware: None,
317        flight_number: None,
318        stated: Stated::default(),
319        time_s: time_s.clone(),
320        altitude_m: Vec::new(),
321        vertical_speed_m_s: None,
322        temperature_k: None,
323        battery_v: None,
324        channels: Vec::new(),
325        events: Vec::new(),
326        notes: Vec::new(),
327    };
328    for (index, role) in roles.iter().enumerate() {
329        let name = &names[index];
330        let values = &columns[index];
331        match role {
332            Role::Time | Role::EventName => {}
333            Role::Pressure => {
334                let scale = pressure_scale(name, values, header_line, &mut notes)?;
335                let values = values
336                    .iter()
337                    .zip(&row_lines)
338                    .map(|(value, line)| finite(value * scale, FORMAT, *line, name))
339                    .collect::<Result<Vec<f64>, LogError>>()?;
340                log.channels.push(Channel {
341                    column: name.clone(),
342                    quantity: Quantity::Pressure,
343                    time_s: time_s.clone(),
344                    values,
345                });
346            }
347            Role::Value(quantity, scale) => {
348                if name.eq_ignore_ascii_case(HEIGHT_RAW) {
349                    log.altitude_m.clone_from(values);
350                }
351                log.channels.push(Channel {
352                    column: name.clone(),
353                    quantity: *quantity,
354                    time_s: time_s.clone(),
355                    values: values.iter().map(|value| value * scale).collect(),
356                });
357            }
358            Role::Voltage => {
359                let numbers = volt_channel.map(|at| columns[at].as_slice());
360                split_voltages(&mut log, name, values, numbers, header_line)?;
361            }
362        }
363    }
364    if log.altitude_m.is_empty() {
365        return Err(syntax(
366            header_line,
367            "the header names no `HEIGHT RAW [m]`, the altitude above the pad".to_owned(),
368        ));
369    }
370    if let Some(code) = code {
371        for ((time, code), name) in time_s.iter().zip(&columns[code]).zip(&event_names) {
372            if *code != 0.0 && !code.is_nan() {
373                log.events.push(Event {
374                    time_s: *time,
375                    name: name.clone(),
376                });
377            }
378        }
379    }
380    for (index, count) in gaps.iter().enumerate() {
381        if *count > 0 {
382            notes.push(format!(
383                "the column {:?} holds {count} empty cells: they are gaps",
384                short(&names[index])
385            ));
386        }
387    }
388    notes.push(format!(
389        "`TIME` names no unit; read in seconds, its shortest step {STEP_S} s, the 200 samples a \
390         second the maker's manual gives"
391    ));
392    log.notes = notes.into_vec();
393    Ok(log)
394}
395
396/// Gives each voltage channel number its own channel of `values`, on the rows that name it; with
397/// no channel column, the voltages are one channel. More than [`VOLT_CHANNELS`] numbers are
398/// refused: the software lists five voltages.
399fn split_voltages(
400    log: &mut FlightLog,
401    name: &str,
402    values: &[f64],
403    channels: Option<&[f64]>,
404    header_line: usize,
405) -> Result<(), LogError> {
406    let Some(channels) = channels else {
407        log.channels.push(Channel {
408            column: name.to_owned(),
409            quantity: Quantity::Voltage,
410            time_s: log.time_s.clone(),
411            values: values.to_vec(),
412        });
413        return Ok(());
414    };
415    // The rows by channel number, then in order, in one sort; `+ 0.0` takes −0 to 0.
416    let mut rows: Vec<(f64, usize)> = channels
417        .iter()
418        .enumerate()
419        .filter(|(_, number)| number.is_finite())
420        .map(|(row, number)| (number + 0.0, row))
421        .collect();
422    rows.sort_by(|a, b| a.0.total_cmp(&b.0).then(a.1.cmp(&b.1)));
423    let groups: Vec<&[(f64, usize)]> = rows.chunk_by(|a, b| a.0 == b.0).collect();
424    if groups.len() > VOLT_CHANNELS {
425        return Err(syntax(
426            header_line,
427            format!(
428                "`VOLT. CHANNEL` holds {} channel numbers, more than the {VOLT_CHANNELS} this \
429                 reader takes",
430                groups.len()
431            ),
432        ));
433    }
434    for group in groups {
435        // Invariant: `chunk_by` yields no empty group.
436        let number = group[0].0;
437        log.channels.push(Channel {
438            column: format!("{name} (channel {number})"),
439            quantity: Quantity::Voltage,
440            time_s: group.iter().map(|(_, row)| log.time_s[*row]).collect(),
441            values: group.iter().map(|(_, row)| values[*row]).collect(),
442        });
443    }
444    Ok(())
445}
446
447/// Holds the clock, which names no unit, to the altimeter's 200 samples a second: its shortest
448/// step, read in seconds, must be 5 ms within 10 %, and a single row, with no step, is refused.
449fn clock_in_seconds(time_s: &[f64], header_line: usize) -> Result<(), LogError> {
450    let shortest = time_s
451        .windows(2)
452        .map(|pair| pair[1] - pair[0])
453        .fold(f64::INFINITY, f64::min);
454    if time_s.len() < 2 {
455        return Err(LogError::Unit {
456            format: FORMAT,
457            line: header_line,
458            message: "`TIME` names no unit, and with one row it has no step to tell it by"
459                .to_owned(),
460        });
461    }
462    if (shortest - STEP_S).abs() <= 0.1 * STEP_S {
463        return Ok(());
464    }
465    Err(LogError::Unit {
466        format: FORMAT,
467        line: header_line,
468        message: format!(
469            "`TIME` names no unit, and its shortest step, {shortest}, isn't the 5 ms of the 200 \
470             samples a second the altimeter stores, read in seconds"
471        ),
472    })
473}
474
475/// The factor that takes a pressure column to pascals: 100 when its first value is a pad's in hPa,
476/// as the header states, 1 when it is a pad's in Pa, with a note.
477fn pressure_scale(
478    name: &str,
479    values: &[f64],
480    header_line: usize,
481    notes: &mut Notes,
482) -> Result<f64, LogError> {
483    let Some(first) = values.iter().copied().find(|value| value.is_finite()) else {
484        return Ok(100.0);
485    };
486    if PAD_PA.contains(&(first * 100.0)) {
487        Ok(100.0)
488    } else if PAD_PA.contains(&first) {
489        notes.push(format!(
490            "the column {:?} says hPa, but its values are pascals. Its first, on the pad, lies \
491             between 500 and 1,100 hPa only when read in Pa, and was read so",
492            short(name)
493        ));
494        Ok(1.0)
495    } else {
496        Err(LogError::Unit {
497            format: FORMAT,
498            line: header_line,
499            message: format!(
500                "the column {:?}'s first value, {first}, is a pad's pressure in neither hPa nor \
501                 Pa: neither puts it between 500 and 1,100 hPa",
502                short(name)
503            ),
504        })
505    }
506}
507
508fn syntax(line: usize, message: String) -> LogError {
509    LogError::Syntax {
510        format: FORMAT,
511        line,
512        message,
513    }
514}
515
516#[cfg(test)]
517mod tests {
518    use super::*;
519
520    const HEADER: &str = "TIME,PRESS FILTER [hpa],HEIGHT FILTER [m],PRESS RAW [hpa],HEIGHT RAW [m],\
521                          ACCEL [m/s2],SPEED [m/s],EVENT,EVENT,VOLT. CHANNEL,VOLTAGE [V]";
522
523    /// An export of `rows`, each `(time, pressure, height, event code, event, voltage channel,
524    /// voltage)`, its filtered columns the raw ones.
525    fn export(rows: &[(f64, f64, f64, u32, &str, u32, f64)]) -> String {
526        let mut text = format!("{HEADER}\n");
527        for (time, pressure, height, code, event, channel, volts) in rows {
528            text.push_str(&format!(
529                "{time},{pressure},{height},{pressure},{height},-9.8,0,{code},{event},{channel},\
530                 {volts}\n"
531            ));
532        }
533        text
534    }
535
536    fn sample() -> String {
537        export(&[
538            (0.005, 98_000.0, 0.0, 0, "PAD", 1, 7.4),
539            (0.010, 97_990.0, 0.9, 3, "BARO-LIFTOFF", 2, 7.3),
540            (0.015, 97_975.0, 2.2, 0, "PAD", 1, 7.39),
541            (0.040, 97_950.0, 4.3, 9, "APOGEE PRESSURE", 2, 7.29),
542        ])
543    }
544
545    fn channel<'a>(log: &'a FlightLog, column: &str) -> &'a Channel {
546        log.channels
547            .iter()
548            .find(|channel| channel.column == column)
549            .unwrap_or_else(|| panic!("no channel {column}"))
550    }
551
552    #[test]
553    fn an_export_reads_with_its_events_and_voltages() {
554        let log = read(&sample()).unwrap();
555        assert_eq!(log.format, LogFormat::AltimaxXls);
556        assert_eq!(log.time_s, [0.005, 0.01, 0.015, 0.04]);
557        assert_eq!(log.altitude_m, [0.0, 0.9, 2.2, 4.3]);
558        let names: Vec<(f64, &str)> = log
559            .events
560            .iter()
561            .map(|event| (event.time_s, event.name.as_str()))
562            .collect();
563        assert_eq!(names, [(0.01, "BARO-LIFTOFF"), (0.04, "APOGEE PRESSURE")]);
564        // Each voltage channel number on its own rows.
565        let first = channel(&log, "VOLTAGE [V] (channel 1)");
566        assert_eq!(
567            (first.time_s.as_slice(), first.values.as_slice()),
568            (&[0.005, 0.015][..], &[7.4, 7.39][..])
569        );
570        let second = channel(&log, "VOLTAGE [V] (channel 2)");
571        assert_eq!(second.values, [7.3, 7.29]);
572        assert_eq!(channel(&log, "EVENT").values, [0.0, 3.0, 0.0, 9.0]);
573        assert_eq!(
574            channel(&log, "HEIGHT FILTER [m]").quantity,
575            Quantity::AltitudeAboveGround
576        );
577        assert_eq!(log.vertical_speed_m_s, None);
578    }
579
580    #[test]
581    fn a_pressure_is_read_in_the_unit_that_puts_the_pad_in_its_band() {
582        // Pascals under `[hpa]`: read in Pa, with a note.
583        let log = read(&sample()).unwrap();
584        assert_eq!(channel(&log, "PRESS RAW [hpa]").values[0], 98_000.0);
585        assert!(
586            log.notes
587                .iter()
588                .any(|note| note.contains("says hPa, but its values are pascals"))
589        );
590        // Hectopascals as stated: read in hPa, no note.
591        let hpa = export(&[
592            (0.005, 980.0, 0.0, 0, "PAD", 1, 7.4),
593            (0.01, 979.9, 0.9, 0, "PAD", 2, 7.3),
594        ]);
595        let log = read(&hpa).unwrap();
596        assert_eq!(channel(&log, "PRESS RAW [hpa]").values[0], 98_000.0);
597        assert!(!log.notes.iter().any(|note| note.contains("says hPa")));
598        // The band's edges, 500 and 1,100 hPa, in each unit; just past them, neither.
599        for (first, pascals) in [
600            (500.0, 50_000.0),
601            (1_100.0, 110_000.0),
602            (50_000.0, 50_000.0),
603            (110_000.0, 110_000.0),
604        ] {
605            let text = export(&[
606                (0.005, first, 0.0, 0, "PAD", 1, 7.4),
607                (0.01, first, 0.0, 0, "PAD", 1, 7.4),
608            ]);
609            assert_eq!(
610                channel(&read(&text).unwrap(), "PRESS RAW [hpa]").values[0],
611                pascals
612            );
613        }
614        for first in [499.9, 1_100.1, 49_999.0, 110_001.0, 5_000.0] {
615            let text = export(&[
616                (0.005, first, 0.0, 0, "PAD", 1, 7.4),
617                (0.01, first, 0.0, 0, "PAD", 1, 7.4),
618            ]);
619            match read(&text) {
620                Err(LogError::Unit { message, .. }) => {
621                    assert!(message.contains("neither hPa nor Pa"), "{message}");
622                }
623                other => panic!("{first}: expected a unit refusal, got {other:?}"),
624            }
625        }
626    }
627
628    #[test]
629    fn the_clock_is_read_in_seconds_only_at_the_altimeters_rate() {
630        // A clock in milliseconds steps by 5, not 0.005.
631        let ms = export(&[
632            (5.0, 98_000.0, 0.0, 0, "PAD", 1, 7.4),
633            (10.0, 97_990.0, 0.9, 0, "PAD", 2, 7.3),
634        ]);
635        match read(&ms) {
636            Err(LogError::Unit { message, .. }) => assert!(message.contains("5 ms"), "{message}"),
637            other => panic!("expected a unit refusal, got {other:?}"),
638        }
639        // Within 10 % of 5 ms, 4.5 to 5.5 ms, just inside each edge; just past each, not.
640        for (step, reads) in [
641            (0.004_51, true),
642            (0.005_49, true),
643            (0.004_49, false),
644            (0.005_51, false),
645        ] {
646            let text = export(&[
647                (1.0, 98_000.0, 0.0, 0, "PAD", 1, 7.4),
648                (1.0 + step, 97_990.0, 0.9, 0, "PAD", 2, 7.3),
649            ]);
650            assert_eq!(read(&text).is_ok(), reads, "step {step}");
651        }
652    }
653
654    #[test]
655    fn a_broken_row_is_refused_on_its_line() {
656        let mut text = sample();
657        text.push_str("0.03,1,2\n");
658        match read(&text) {
659            Err(LogError::Syntax { line, message, .. }) => {
660                assert_eq!(line, 6);
661                assert!(
662                    message.contains("has 3 cells, and the header names 11"),
663                    "{message}"
664                );
665            }
666            other => panic!("expected a syntax refusal, got {other:?}"),
667        }
668        let back = export(&[
669            (0.01, 98_000.0, 0.0, 0, "PAD", 1, 7.4),
670            (0.005, 97_990.0, 0.9, 0, "PAD", 2, 7.3),
671        ]);
672        assert!(matches!(read(&back), Err(LogError::Syntax { line: 3, .. })));
673        let word = sample().replace("-9.8", "x");
674        assert!(matches!(read(&word), Err(LogError::Syntax { line: 2, .. })));
675    }
676
677    /// A refusal's line and message.
678    fn refused(text: &str) -> (usize, String) {
679        match read(text) {
680            Err(LogError::Syntax { line, message, .. } | LogError::Unit { line, message, .. }) => {
681                (line, message)
682            }
683            other => panic!("expected a refusal, got {other:?}"),
684        }
685    }
686
687    /// A name given twice in another case is the same name; `EVENT` a third time is refused.
688    #[test]
689    fn a_name_twice_in_another_case_or_event_thrice_is_refused() {
690        for (from, to) in [("ACCEL [m/s2]", "time"), ("VOLTAGE [V]", "event")] {
691            let (line, message) = refused(&sample().replacen(from, to, 1));
692            assert_eq!(line, 1, "{to}");
693            assert!(
694                message.contains(&format!("names {to:?} more than once")),
695                "{to}: {message}"
696            );
697        }
698    }
699
700    /// A header of [`MAX_COLUMNS`] names reads; one more is refused, and a header of a million
701    /// distinct names is refused at once rather than checked pair by pair, which grows as the
702    /// square of the names' count: minutes for this one (#1250). The clock times this test alone,
703    /// and nothing it reads reaches a result.
704    #[test]
705    #[allow(clippy::disallowed_types, reason = "the clock times this test alone")]
706    fn a_header_wider_than_the_limit_is_refused_at_once() {
707        let widened = |extra: usize| {
708            let names: String = (0..extra).map(|k| format!(",X{k}")).collect();
709            let cells = ",0".repeat(extra);
710            sample()
711                .lines()
712                .enumerate()
713                .map(|(index, line)| {
714                    let added = if index == 0 {
715                        names.as_str()
716                    } else {
717                        cells.as_str()
718                    };
719                    format!("{line}{added}\n")
720                })
721                .collect::<String>()
722        };
723        let known = HEADER.split(',').count();
724        assert!(read(&widened(MAX_COLUMNS - known)).is_ok());
725        let (line, message) = refused(&widened(MAX_COLUMNS + 1 - known));
726        assert_eq!(line, 1);
727        assert!(
728            message.contains(&format!("more than {MAX_COLUMNS} columns")),
729            "{message}"
730        );
731        let wide = widened(1_000_000);
732        let began = std::time::Instant::now();
733        let (_, message) = refused(&wide);
734        let elapsed = began.elapsed();
735        assert!(
736            message.contains(&format!("more than {MAX_COLUMNS} columns")),
737            "{message}"
738        );
739        assert!(elapsed.as_secs_f64() < 5.0, "{elapsed:?}");
740    }
741
742    /// A second `TIME` or `HEIGHT RAW [m]` would read the rows against the wrong clock or track;
743    /// only `EVENT` is named twice, and not three times.
744    #[test]
745    fn a_column_named_twice_is_refused_but_event() {
746        for (from, to) in [
747            ("ACCEL [m/s2]", "TIME"),
748            ("SPEED [m/s]", "HEIGHT RAW [m]"),
749            ("VOLTAGE [V]", "EVENT"),
750        ] {
751            let (line, message) = refused(&sample().replacen(from, to, 1));
752            assert_eq!(line, 1, "{to}");
753            assert!(
754                message.contains(&format!("names {to:?} more than once")),
755                "{to}: {message}"
756            );
757        }
758    }
759
760    /// A `NaN` cell isn't a gap nobody counted, and a pressure in hPa too large for pascals isn't
761    /// an infinity: both are refused on their lines.
762    #[test]
763    fn a_value_that_isnt_a_finite_number_is_refused_on_its_line() {
764        let (line, message) = refused(&sample().replacen(",-9.8,", ",NaN,", 1));
765        assert_eq!(line, 2);
766        assert!(
767            message.contains("\"NaN\", under \"ACCEL [m/s2]\", isn't a number"),
768            "{message}"
769        );
770        let huge = export(&[
771            (0.005, 980.0, 0.0, 0, "PAD", 1, 7.4),
772            (0.010, 1e307, 0.9, 0, "PAD", 2, 7.3),
773        ]);
774        let (line, message) = refused(&huge);
775        assert_eq!(line, 3);
776        assert!(message.contains("too large"), "{message}");
777    }
778
779    /// The voltages split by channel number, as many as 16; a 17th is refused, not made a channel.
780    #[test]
781    fn more_than_16_voltage_channels_are_refused() {
782        let rows = |count: u32| {
783            export(
784                &(0..count)
785                    .map(|n| (0.005 * f64::from(n + 1), 98_000.0, 0.0, 0, "PAD", n, 7.0))
786                    .collect::<Vec<_>>(),
787            )
788        };
789        let log = read(&rows(16)).unwrap();
790        let voltages = log
791            .channels
792            .iter()
793            .filter(|channel| channel.quantity == Quantity::Voltage);
794        assert_eq!(voltages.count(), 16);
795        let (line, message) = refused(&rows(17));
796        assert_eq!(line, 1);
797        assert!(message.contains("17 channel numbers"), "{message}");
798    }
799
800    #[test]
801    fn a_clock_of_one_row_is_refused() {
802        let (_, message) = refused(&export(&[(0.005, 98_000.0, 0.0, 0, "PAD", 1, 7.4)]));
803        assert!(message.contains("one row"), "{message}");
804    }
805
806    #[test]
807    fn an_empty_cell_is_a_gap_counted_in_a_note() {
808        let text = sample().replacen(",-9.8,", ",,", 1);
809        let log = read(&text).unwrap();
810        assert!(channel(&log, "ACCEL [m/s2]").values[0].is_nan());
811        assert!(
812            log.notes
813                .iter()
814                .any(|note| note.contains("\"ACCEL [m/s2]\" holds 1 empty cells"))
815        );
816    }
817
818    #[test]
819    fn the_layout_is_told_by_its_first_five_columns() {
820        assert!(recognizes(&sample()));
821        assert!(recognizes(&sample().to_ascii_lowercase()));
822        assert!(!recognizes(
823            "TIME,PRESS FILTER [hpa],HEIGHT FILTER [m],PRESS RAW [hpa]\n"
824        ));
825        assert!(!recognizes("Time,Altitude\n0,0\n"));
826        assert!(matches!(
827            read("Time,Altitude\n0,0\n"),
828            Err(LogError::NotThisFormat { .. })
829        ));
830        assert!(matches!(
831            read(&format!("{HEADER}\n")),
832            Err(LogError::NoData { .. })
833        ));
834    }
835}