Skip to main content

hpr_flightdata/
rdas.rs

1//! AED Electronics' R-DAS flight computer's raw data, as its Windows software exports a flight
2//! to text: a `*** Configuration ***` section, then `*** Raw data ***` and a table of the
3//! converters' counts.
4//!
5//! The R-DAS records its accelerometer and its barometer, and as its owner chooses its four
6//! digital inputs and six analog inputs from an expansion port, each through a 10-bit converter
7//! over 0 to 5 V, at up to 200 samples a second (*R-DAS User Manual*, V1.5, AED Electronics,
8//! 2001, p. 7, p. 14). It keeps the 2 s before liftoff and stores from there (p. 6). Its Windows
9//! software exports a downloaded flight as text, either raw or converted with the calibration
10//! values set in the software; the raw file's header "contains a summary of the measurement
11//! configuration", and its values are "measured ADC-values", each step 5 V / 1,024 = 4.883 mV,
12//! for the reader to convert with the calibration values (p. 14). The manual shows no file and
13//! names no column. The sample logs hold the raw export only, 54 files from a college course's
14//! flights in 2008: 28 recorded on board at 200 Hz, and 26 at 10 Hz, by their names the course's
15//! telemetry records of the same flights, in the same layout but for a configuration whose
16//! `Trigger:` line is blank and which names no firmware.
17//!
18//! **Recognition.** The first line that isn't blank is `*** Configuration ***`, and a line
19//! `*** Raw data ***` comes within the file's first 200 lines; no other layout HPR Sim reads opens
20//! so.
21//!
22//! **The configuration.** Two of its lines are needed, and a file without either is refused
23//! ([`LogError::Unit`]): `Data is sampled at N Hz.`, which the clock is checked against, and
24//! `Inputs that were sampled: …`, which says what the table's columns hold: `Acceleration` is
25//! `Acc`, `Pressure` is `Prs`, `the digital inputs` is `Dig`, and `ADC0` to `ADC5` are
26//! themselves. The table's header, after `Time`, must name those columns in that order, and a
27//! list naming an input the reader doesn't know, or one twice, is refused ([`LogError::Syntax`]).
28//! A line holding `with firmware version N` gives [`FlightLog::firmware`], the last if several do. Every line of the configuration that
29//! isn't blank, its trigger and recovery settings among them, is kept in a note as written, at
30//! most 40 lines of 200 characters.
31//!
32//! **Columns.** The file states no calibration, and the manual leaves the conversions to values
33//! set in the software, which the file doesn't hold, so no column is converted:
34//!
35//! | Column | Quantity | As read |
36//! |---|---|---|
37//! | `Time` | the clock, s | from liftoff: checked against the stated rate (below) |
38//! | `Acc`, `Prs`, `ADC0` … `ADC5` | [`Quantity::Unrecognized`] | the converter's counts, whole numbers 0 to 1,023, as written |
39//! | `Dig` | [`Quantity::Number`] | a hexadecimal word, kept as its number |
40//!
41//! `Acc` is the accelerometer, an Analog Devices ADXL150 for ±50 g, and `Prs` the barometer, a
42//! Motorola MPXS4100A for 20 to 105 kPa, in the manual's R-DAS (p. 7); but the file states
43//! neither the calibration values nor which board and sensors wrote it, and others have been
44//! fitted to the R-DAS (a college course's pressure lab names an MPXH6115A on its R-DAS: *E80
45//! Pressure and Temperature Lab*, Harvey Mudd College,
46//! <https://pages.hmc.edu/spjut/NewE80/PresTempLab.html>, section 2), so the counts aren't taken
47//! through that sensor's data sheet. The manual says the digital byte holds the four digital
48//! inputs and, in its other four bits, the flight's phase: before liftoff, flight, drogue and main
49//! (p. 14), and, of firmware 2.2, that "bit 3 is set when timed recovery was used" (p. 22); it gives
50//! no other bit. The files write a word of four hexadecimal digits, not a byte, and never set bit 3
51//! of either byte: they set `0x80` and `0x8000`. In 27 of the 28 on-board sample files the word
52//! reads `0000` from the first row, `0010` from within 0.25 s after the zero, then `0020`, `0030`
53//! and, near the file's end, `8030`; the 28th writes `00B0` and `80B0` in place of the last two.
54//! The low byte's high bits look like the phases in order, but that rests on the files, not a
55//! document, so the word is kept as written and no event is taken from it.
56//!
57//! **The clock.** `Time` counts from liftoff, `t = 0` in the manual's figure 2.2, the g-switch, 2.5 g
58//! held for 0.25 s, firing at 0.25 s (p. 10, p. 11): every sample file's first row is at −2.000,
59//! the 2 s before liftoff the manual says it keeps. That it counts seconds rests on the
60//! stated rate: the clock's average step, from the first row to the last, must be the stated
61//! rate's interval within 1%, or the file is refused ([`LogError::Unit`]); a note gives both. In
62//! every sample file the step is the interval exactly, 0.005 s at 200 Hz and 0.1 s at 10 Hz. A
63//! step under half the interval or over one and a half of it is counted in the note.
64//!
65//! **The primary track.** The file holds counts and no altitude, so [`FlightLog::time_s`] and
66//! [`FlightLog::altitude_m`] are empty, as for the Raven's raw `.FIPa`, and every column is a
67//! channel on the file's clock.
68//!
69//! **Cells.** Every row must hold as many cells as the header names, its `Time` a number greater
70//! than the row before's, and every count that is a number a whole one from 0 to 1,023, which
71//! keeps a file of converted values from passing for counts; otherwise the file is refused,
72//! naming the line. Any other cell that isn't a number, or for `Dig` a word of hexadecimal digits,
73//! is a gap (`NaN`), counted in a note. Lines may end in LF or CR LF; a file whose lines end in a
74//! lone CR isn't recognized.
75//!
76//! An invented file, in the layout the sample files share:
77//!
78//! ```text
79//! *** Configuration ***
80//!
81//! Trigger: G-switch.
82//! Data is sampled at 200 Hz.
83//! Inputs that were sampled: Acceleration, Pressure, the digital inputs, ADC0, ADC1.
84//!
85//! This data was generated with version 4.1 of the RDAS windows software,
86//! and downloaded from an RDAS board with firmware version 4.1
87//!
88//!
89//! *** Raw data ***
90//!
91//!    Time  Acc  Prs  Dig ADC0 ADC1
92//!  -2.000  510  800 0000  500  250
93//!  -1.995  511  800 0000  501  250
94//! ```
95
96use crate::error::LogError;
97use crate::log::{Channel, FlightLog, LogFormat, Quantity, Stated};
98
99/// The format's name in errors.
100const FORMAT: &str = "R-DAS text";
101
102/// The logger, as the record names it.
103const LOGGER: &str = "AED Electronics R-DAS";
104
105/// The line that opens the file.
106const CONFIGURATION: &str = "*** Configuration ***";
107
108/// The line that ends the configuration and opens the table.
109const RAW_DATA: &str = "*** Raw data ***";
110
111/// How many lines, from the first, may come before [`RAW_DATA`]: the sample files' configuration
112/// takes 13; a file whose marker comes later isn't read as this layout, so that recognizing it
113/// never walks a whole large file.
114const CONFIGURATION_LINES: usize = 200;
115
116/// The barometer's step, as the format's resolution ([`LogFormat::altitude_resolution_m`]):
117/// the file writes no altitude, but its pressure moves by the barometer's count, 0.09 kPa in the
118/// manual's R-DAS (p. 7), 7.5 m near sea level (`Δh = Δp / (ρ₀ g₀)`, as
119/// [`LogFormat::altitude_resolution_m`] takes it). A board with another sensor steps otherwise.
120pub const ALTITUDE_STEP_M: f64 = 90.0 / (1.225 * 9.806_65);
121
122/// The largest count of the R-DAS's 10-bit converter.
123const MAX_COUNT: f64 = 1_023.0;
124
125/// How far the clock's average step may lie from the stated rate's, as a share of it.
126const CLOCK_MARGIN: f64 = 0.01;
127
128/// How many configuration lines a note quotes, and how many characters of each, so that a hostile
129/// file can't make the record far larger than the samples it holds.
130const QUOTED_LINES: usize = 40;
131const QUOTED_CHARS: usize = 200;
132
133/// Whether a file's text is in this layout: its first line that isn't blank is
134/// `*** Configuration ***`, and a `*** Raw data ***` line comes within its first 200 lines.
135pub fn recognizes(text: &str) -> bool {
136    let mut lines = text
137        .lines()
138        .take(CONFIGURATION_LINES)
139        .map(str::trim)
140        .skip_while(|line| line.is_empty());
141    lines.next() == Some(CONFIGURATION) && lines.any(|line| line == RAW_DATA)
142}
143
144/// What the configuration says, as the reader uses it.
145struct Configuration {
146    /// The stated sample rate, Hz, and its line.
147    rate_hz: Option<(f64, usize)>,
148    /// The inputs the configuration lists, each as the table's column names it, and the line.
149    inputs: Option<(Vec<String>, usize)>,
150    /// The board's firmware version, as written.
151    firmware: Option<String>,
152    /// The configuration's lines that aren't blank, as written, for the note.
153    lines: Vec<String>,
154}
155
156/// The column the table writes for an input the configuration names, or `None` for an input the
157/// reader doesn't know.
158fn column_of(input: &str) -> Option<String> {
159    match input {
160        "Acceleration" => Some("Acc".to_owned()),
161        "Pressure" => Some("Prs".to_owned()),
162        "the digital inputs" => Some("Dig".to_owned()),
163        // The six analog inputs of the expansion port (manual, p. 7, p. 14).
164        "ADC0" | "ADC1" | "ADC2" | "ADC3" | "ADC4" | "ADC5" => Some(input.to_owned()),
165        _ => None,
166    }
167}
168
169/// Reads the configuration's lines, `lines` numbered from 1.
170fn configuration<'a>(
171    lines: impl Iterator<Item = (usize, &'a str)>,
172) -> Result<Configuration, LogError> {
173    let mut configuration = Configuration {
174        rate_hz: None,
175        inputs: None,
176        firmware: None,
177        lines: Vec::new(),
178    };
179    for (number, line) in lines {
180        let line = line.trim();
181        if line.is_empty() || line == CONFIGURATION {
182            continue;
183        }
184        configuration.lines.push(line.to_owned());
185        if let Some(rest) = line.strip_prefix("Data is sampled at ") {
186            let rate = rest
187                .strip_suffix(" Hz.")
188                .and_then(|rate| rate.trim().parse::<f64>().ok())
189                .filter(|rate| rate.is_finite() && *rate > 0.0)
190                .ok_or_else(|| {
191                    syntax(
192                        number,
193                        format!("a sample rate that isn't one: {:?}", cut(line)),
194                    )
195                })?;
196            if configuration.rate_hz.replace((rate, number)).is_some() {
197                return Err(syntax(number, "a second sample rate".to_owned()));
198            }
199        } else if let Some(rest) = line.strip_prefix("Inputs that were sampled: ") {
200            let rest = rest.strip_suffix('.').unwrap_or(rest);
201            let columns = rest
202                .split(',')
203                .map(|input| {
204                    column_of(input.trim()).ok_or_else(|| {
205                        syntax(
206                            number,
207                            format!("an input the reader doesn't know: {:?}", cut(input.trim())),
208                        )
209                    })
210                })
211                .collect::<Result<Vec<_>, _>>()?;
212            if let Some(twice) = columns
213                .iter()
214                .enumerate()
215                .find_map(|(k, column)| columns[..k].contains(column).then_some(column))
216            {
217                return Err(syntax(number, format!("an input listed twice: `{twice}`")));
218            }
219            if configuration.inputs.replace((columns, number)).is_some() {
220                return Err(syntax(number, "a second list of inputs".to_owned()));
221            }
222        } else if let Some((_, version)) = line.split_once("with firmware version ") {
223            let version = version.trim().trim_end_matches('.').trim();
224            if !version.is_empty() {
225                configuration.firmware = Some(version.to_owned());
226            }
227        }
228    }
229    Ok(configuration)
230}
231
232/// Reads a log in this layout.
233///
234/// # Errors
235///
236/// - [`LogError::NotThisFormat`] if the file doesn't open with `*** Configuration ***` and a
237///   `*** Raw data ***` line within its first 200 lines.
238/// - [`LogError::Syntax`] for a configuration line the reader can't take (a sample rate that
239///   isn't a positive number, an input it doesn't know or listed twice, a rate or a list stated
240///   twice), a table header that doesn't open with `Time` or doesn't name the inputs the
241///   configuration lists, a row not as wide as the header, a `Time` that isn't a number greater
242///   than the row before's, or a count that is a number but not a whole one from 0 to 1,023.
243/// - [`LogError::NoData`] without a row.
244/// - [`LogError::Unit`] where the configuration states no sample rate or lists no inputs, the
245///   file holds a single row, or the clock's average step isn't the stated rate's within 1%:
246///   then nothing in the file says the clock counts seconds.
247pub fn read(text: &str) -> Result<FlightLog, LogError> {
248    if !recognizes(text) {
249        return Err(LogError::NotThisFormat {
250            format: FORMAT,
251            message: format!("the file doesn't open with `{CONFIGURATION}` and `{RAW_DATA}`"),
252        });
253    }
254    let mut lines = text
255        .lines()
256        .enumerate()
257        .map(|(index, line)| (index + 1, line));
258    let config_lines: Vec<(usize, &str)> = lines
259        .by_ref()
260        .take_while(|(_, line)| line.trim() != RAW_DATA)
261        .collect();
262    let configuration = configuration(config_lines.into_iter())?;
263
264    // The table's header: the first line after the marker that isn't blank.
265    let Some((header_line, header)) = lines.by_ref().find(|(_, line)| !line.trim().is_empty())
266    else {
267        return Err(LogError::NoData { format: FORMAT });
268    };
269    let names: Vec<&str> = header.split_whitespace().collect();
270    if names.first() != Some(&"Time") {
271        return Err(syntax(
272            header_line,
273            format!(
274                "a table header that doesn't open with `Time`: {:?}",
275                cut(header.trim())
276            ),
277        ));
278    }
279    let names = &names[1..];
280    let (inputs, inputs_line) = configuration
281        .inputs
282        .as_ref()
283        .ok_or_else(|| LogError::Unit {
284            format: FORMAT,
285            line: header_line,
286            message: "the configuration lists no inputs, so nothing says what the table's columns \
287                  hold"
288                .to_owned(),
289        })?;
290    if names.iter().copied().ne(inputs.iter().map(String::as_str)) {
291        return Err(syntax(
292            header_line,
293            format!(
294                "the table's columns, {}, aren't the inputs line {inputs_line} lists, {}",
295                cut(&names.join(" ")),
296                cut(&inputs.join(" "))
297            ),
298        ));
299    }
300    let digital = names.iter().position(|name| *name == "Dig");
301
302    let mut time_s = Vec::new();
303    let mut values: Vec<Vec<f64>> = vec![Vec::new(); names.len()];
304    let mut gaps = vec![0usize; names.len()];
305    let mut first_gap = None;
306    for (number, line) in lines {
307        if line.trim().is_empty() {
308            continue;
309        }
310        let cells: Vec<&str> = line.split_whitespace().collect();
311        if cells.len() != names.len() + 1 {
312            return Err(syntax(
313                number,
314                format!(
315                    "a row of {} cells where the header names {}",
316                    cells.len(),
317                    names.len() + 1
318                ),
319            ));
320        }
321        let time = cells[0]
322            .parse::<f64>()
323            .ok()
324            .filter(|time| time.is_finite())
325            .ok_or_else(|| {
326                syntax(
327                    number,
328                    format!("a `Time` that isn't a number: {:?}", cut(cells[0])),
329                )
330            })?;
331        if time_s.last().is_some_and(|last| time <= *last) {
332            return Err(syntax(
333                number,
334                format!("a `Time`, {time}, not after the row before's"),
335            ));
336        }
337        time_s.push(time);
338        for (k, cell) in cells[1..].iter().enumerate() {
339            let value = if Some(k) == digital {
340                // Hexadecimal digits only: `from_str_radix` would also take a sign.
341                cell.bytes()
342                    .all(|b| b.is_ascii_hexdigit())
343                    .then(|| u32::from_str_radix(cell, 16).ok())
344                    .flatten()
345                    .map(f64::from)
346            } else {
347                let value = cell.parse::<f64>().ok().filter(|value| value.is_finite());
348                if let Some(value) = value
349                    && !(value.fract() == 0.0 && (0.0..=MAX_COUNT).contains(&value))
350                {
351                    return Err(syntax(
352                        number,
353                        format!(
354                            "`{}` holds {:?}, not a count of the 10-bit converter, a whole \
355                             number from 0 to 1,023: a file of converted values isn't read",
356                            names[k],
357                            cut(cell)
358                        ),
359                    ));
360                }
361                value
362            };
363            values[k].push(value.unwrap_or_else(|| {
364                gaps[k] += 1;
365                first_gap.get_or_insert(number);
366                f64::NAN
367            }));
368        }
369    }
370    if time_s.is_empty() {
371        return Err(LogError::NoData { format: FORMAT });
372    }
373
374    let mut notes = vec![clock(&configuration, &time_s, header_line)?];
375    let counts: Vec<String> = names
376        .iter()
377        .filter(|name| **name != "Dig")
378        .map(|name| format!("`{name}`"))
379        .collect();
380    if !counts.is_empty() {
381        notes.push(format!(
382            "{} kept as written: the converters' counts, which the file states no calibration \
383             for",
384            counts.join(", ")
385        ));
386    }
387    if digital.is_some() {
388        notes.push(
389            "`Dig` read in hexadecimal and kept as its number: the digital inputs and the \
390             flight's phase, which the file doesn't name bit by bit"
391                .to_owned(),
392        );
393    }
394    let total_gaps: usize = gaps.iter().sum();
395    if let Some(line) = first_gap {
396        notes.push(if total_gaps == 1 {
397            format!("1 cell that isn't a number read as a gap, on line {line}")
398        } else {
399            format!("{total_gaps} cells that aren't numbers read as gaps, the first on line {line}")
400        });
401    }
402    let shown = configuration.lines.len().min(QUOTED_LINES);
403    let mut quoted: Vec<String> = configuration.lines[..shown]
404        .iter()
405        .map(|line| cut(line))
406        .collect();
407    if configuration.lines.len() > shown {
408        quoted.push(format!(
409            "and {} more lines",
410            configuration.lines.len() - shown
411        ));
412    }
413    notes.push(format!(
414        "the configuration, as written: {}",
415        quoted.join(" | ")
416    ));
417    notes.push(
418        "the file holds counts and no altitude, so the record's primary track is empty: every \
419         column is a channel on the file's clock"
420            .to_owned(),
421    );
422
423    let channels = names
424        .iter()
425        .zip(values)
426        .map(|(name, values)| Channel {
427            column: (*name).to_owned(),
428            quantity: if *name == "Dig" {
429                Quantity::Number
430            } else {
431                Quantity::Unrecognized
432            },
433            time_s: time_s.clone(),
434            values,
435        })
436        .collect();
437
438    Ok(FlightLog {
439        format: LogFormat::RdasText,
440        logger: LOGGER.to_owned(),
441        serial_number: None,
442        firmware: configuration.firmware,
443        flight_number: None,
444        stated: Stated::default(),
445        time_s: Vec::new(),
446        altitude_m: Vec::new(),
447        vertical_speed_m_s: None,
448        temperature_k: None,
449        battery_v: None,
450        channels,
451        events: Vec::new(),
452        notes,
453    })
454}
455
456/// The clock's check against the stated rate, as a note; a refusal where it fails.
457fn clock(
458    configuration: &Configuration,
459    time_s: &[f64],
460    header_line: usize,
461) -> Result<String, LogError> {
462    let unit = |message: String| LogError::Unit {
463        format: FORMAT,
464        line: header_line,
465        message,
466    };
467    let (rate_hz, rate_line) = configuration.rate_hz.ok_or_else(|| {
468        unit(
469            "the configuration states no sample rate, which the clock is checked against"
470                .to_owned(),
471        )
472    })?;
473    let (Some(first), Some(last)) = (time_s.first(), time_s.last()) else {
474        return Err(LogError::NoData { format: FORMAT });
475    };
476    if time_s.len() < 2 {
477        return Err(unit(
478            "a single row, whose clock can't be checked against the stated rate".to_owned(),
479        ));
480    }
481    let step_s = (last - first) / (time_s.len() - 1) as f64;
482    let ratio = step_s * rate_hz;
483    if (ratio - 1.0).abs() > CLOCK_MARGIN {
484        return Err(unit(format!(
485            "`Time` steps by {step_s} on average, {ratio:.3} times the {rate_hz} Hz line \
486             {rate_line} states, not within 1%: nothing says the clock counts seconds"
487        )));
488    }
489    let interval_s = 1.0 / rate_hz;
490    let uneven = time_s
491        .windows(2)
492        .filter(|pair| {
493            let step = pair[1] - pair[0];
494            step < 0.5 * interval_s || step > 1.5 * interval_s
495        })
496        .count();
497    let mut note = format!(
498        "`Time` read in seconds: it steps by {step_s:.6} on average, the {rate_hz} Hz line \
499         {rate_line} states within 1% ({ratio:.4} times its {interval_s:.6} s)"
500    );
501    if uneven > 0 {
502        note.push_str(&format!(
503            "; {uneven} steps are under half or over one and a half of it"
504        ));
505    }
506    Ok(note)
507}
508
509/// Text from the file cut to [`QUOTED_CHARS`] characters, for a note or an error.
510fn cut(text: &str) -> String {
511    text.chars().take(QUOTED_CHARS).collect()
512}
513
514/// A refusal at a line of the file.
515fn syntax(line: usize, message: String) -> LogError {
516    LogError::Syntax {
517        format: FORMAT,
518        line,
519        message,
520    }
521}
522
523#[cfg(test)]
524mod tests {
525    use super::*;
526
527    /// A file with the configuration's lines `config`, a header `header` and rows `rows`.
528    fn file(config: &[&str], header: &str, rows: &[&str]) -> String {
529        let mut lines = vec![CONFIGURATION, ""];
530        lines.extend_from_slice(config);
531        lines.extend_from_slice(&["", RAW_DATA, "", header]);
532        lines.extend_from_slice(rows);
533        lines.join("\n") + "\n"
534    }
535
536    const RATE: &str = "Data is sampled at 100 Hz.";
537    const INPUTS: &str = "Inputs that were sampled: Acceleration, Pressure, the digital inputs.";
538    const HEADER: &str = "   Time  Acc  Prs  Dig";
539
540    /// Rows every 0.01 s from 0, as many as `n`.
541    fn rows(n: usize) -> Vec<String> {
542        (0..n)
543            .map(|k| format!("{:7.3} 510 800 0010", k as f64 * 0.01))
544            .collect()
545    }
546
547    fn good(n: usize) -> String {
548        let rows = rows(n);
549        let rows: Vec<&str> = rows.iter().map(String::as_str).collect();
550        file(&[RATE, INPUTS], HEADER, &rows)
551    }
552
553    /// The message of a refusal of the kind `kind`, which must be the one returned.
554    fn refusal(text: &str, kind: &str) -> String {
555        match (read(text), kind) {
556            (Err(LogError::Unit { message, .. }), "unit") => message,
557            (Err(LogError::Syntax { message, line, .. }), "syntax") => {
558                format!("line {line}: {message}")
559            }
560            (Err(LogError::NoData { .. }), "no data") => String::new(),
561            (other, _) => panic!("expected a {kind} refusal, got {other:?}"),
562        }
563    }
564
565    #[test]
566    fn the_layout_is_recognized_by_its_two_markers() {
567        assert!(recognizes(&good(3)));
568        assert!(recognizes(&format!(
569            "\r\n\r\n{}",
570            good(3).replace('\n', "\r\n")
571        )));
572        // No table marker, or one past the first 200 lines, isn't the layout.
573        assert!(!recognizes(
574            "*** Configuration ***\nData is sampled at 100 Hz.\n"
575        ));
576        // The marker on line 201, then on line 200.
577        let late = format!("{CONFIGURATION}\n{}{RAW_DATA}\n", "x\n".repeat(199));
578        assert!(!recognizes(&late));
579        let in_time = format!("{CONFIGURATION}\n{}{RAW_DATA}\n", "x\n".repeat(198));
580        assert!(recognizes(&in_time));
581        // The configuration's marker must come first.
582        assert!(!recognizes(&format!("Time,Altitude\n{}", good(3))));
583        assert!(matches!(
584            read("Time,Altitude\n0,0\n"),
585            Err(LogError::NotThisFormat { .. })
586        ));
587    }
588
589    #[test]
590    fn a_good_file_reads_its_firmware_and_notes_the_clock() {
591        let text = file(
592            &[
593                RATE,
594                INPUTS,
595                "and downloaded from an RDAS board with firmware version 4.1",
596            ],
597            HEADER,
598            &[
599                "  0.000 510 800 0000",
600                "  0.010 511 801 001F",
601                "  0.020 512 802 0010",
602            ],
603        );
604        let log = read(&text).unwrap();
605        assert_eq!(log.firmware.as_deref(), Some("4.1"));
606        assert_eq!(log.channels[2].values, [0.0, 31.0, 16.0]);
607        assert!(
608            log.notes[0].starts_with("`Time` read in seconds: it steps by 0.010000 on average")
609        );
610        assert_eq!(log.format.altitude_resolution_m(), ALTITUDE_STEP_M);
611    }
612
613    #[test]
614    fn a_clock_off_the_stated_rate_by_more_than_one_percent_is_refused() {
615        // 0.01009 s a step at 100 Hz is 1.009 intervals: read.
616        let near = file(
617            &[RATE, INPUTS],
618            HEADER,
619            &["0 1 1 0", "0.01009 1 1 0", "0.02018 1 1 0"],
620        );
621        assert!(read(&near).is_ok());
622        let early = file(
623            &[RATE, INPUTS],
624            HEADER,
625            &["0 1 1 0", "0.00991 1 1 0", "0.01982 1 1 0"],
626        );
627        assert!(read(&early).is_ok());
628        // 1.011 and 0.989 intervals: refused.
629        for step in ["0.01011", "0.00989"] {
630            let second = format!("{step} 1 1 0");
631            let text = file(&[RATE, INPUTS], HEADER, &["0 1 1 0", &second]);
632            let message = refusal(&text, "unit");
633            assert!(message.contains("not within 1%"), "{message}");
634        }
635        // A clock in milliseconds is 1,000 intervals a step.
636        let millis = file(
637            &[RATE, INPUTS],
638            HEADER,
639            &["0 1 1 0", "10 1 1 0", "20 1 1 0"],
640        );
641        assert!(refusal(&millis, "unit").contains("1000.000 times the 100 Hz line"));
642    }
643
644    #[test]
645    fn uneven_steps_are_counted() {
646        // A dropped sample: one step of 0.02 s, two intervals, among 0.01 s ones; the average stays
647        // within 1% of the interval over a long file.
648        let mut rows = rows(200);
649        rows.remove(100);
650        let mut more = super::tests::rows(1);
651        more[0] = format!("{:7.3} 510 800 0010", 2.0);
652        rows.extend(more);
653        let rows: Vec<&str> = rows.iter().map(String::as_str).collect();
654        let log = read(&file(&[RATE, INPUTS], HEADER, &rows)).unwrap();
655        assert!(
656            log.notes[0].ends_with("; 1 steps are under half or over one and a half of it"),
657            "{}",
658            log.notes[0]
659        );
660    }
661
662    #[test]
663    fn a_configuration_without_its_rate_or_inputs_is_refused() {
664        let rows = ["0 1 1 0", "0.01 1 1 0"];
665        let message = refusal(&file(&[INPUTS], HEADER, &rows), "unit");
666        assert!(message.contains("states no sample rate"), "{message}");
667        let message = refusal(&file(&[RATE], HEADER, &rows), "unit");
668        assert!(message.contains("lists no inputs"), "{message}");
669        let message = refusal(&file(&[RATE, RATE, INPUTS], HEADER, &rows), "syntax");
670        assert!(message.contains("a second sample rate"), "{message}");
671        let message = refusal(&file(&[RATE, INPUTS, INPUTS], HEADER, &rows), "syntax");
672        assert!(message.contains("a second list of inputs"), "{message}");
673        for rate in [
674            "Data is sampled at 0 Hz.",
675            "Data is sampled at fast Hz.",
676            "Data is sampled at 100 Hz",
677        ] {
678            let message = refusal(&file(&[rate, INPUTS], HEADER, &rows), "syntax");
679            assert!(
680                message.contains("a sample rate that isn't one"),
681                "{message}"
682            );
683        }
684        let gyro = "Inputs that were sampled: Acceleration, Gyro.";
685        let message = refusal(&file(&[RATE, gyro], HEADER, &rows), "syntax");
686        assert!(
687            message.contains("an input the reader doesn't know: \"Gyro\""),
688            "{message}"
689        );
690        // The port's six analog inputs are ADC0 to ADC5.
691        let analog = "Inputs that were sampled: ADC0, ADC5.";
692        let ok = file(&[RATE, analog], "Time ADC0 ADC5", &["0 1 1", "0.01 1 1"]);
693        assert_eq!(read(&ok).unwrap().channels.len(), 2);
694        for input in ["ADC6", "ADC00", "ADC"] {
695            let inputs = format!("Inputs that were sampled: ADC0, {input}.");
696            let header = format!("Time ADC0 {input}");
697            let text = file(&[RATE, &inputs], &header, &["0 1 1", "0.01 1 1"]);
698            let message = refusal(&text, "syntax");
699            let expected = format!("an input the reader doesn't know: \"{input}\"");
700            assert!(message.contains(&expected), "{message}");
701        }
702        // An input listed twice would make two columns of one name.
703        let twice = "Inputs that were sampled: the digital inputs, the digital inputs.";
704        let text = file(
705            &[RATE, twice],
706            "Time Dig Dig",
707            &["0 0010 0010", "0.01 0010 0010"],
708        );
709        let message = refusal(&text, "syntax");
710        assert!(
711            message.contains("an input listed twice: `Dig`"),
712            "{message}"
713        );
714    }
715
716    #[test]
717    fn a_header_must_name_the_listed_inputs() {
718        let rows = ["0 1 1 0", "0.01 1 1 0"];
719        let message = refusal(
720            &file(&[RATE, INPUTS], "   Time  Prs  Acc  Dig", &rows),
721            "syntax",
722        );
723        assert!(
724            message.starts_with(
725                "line 8: the table's columns, Prs Acc Dig, aren't the inputs line 4 lists"
726            ),
727            "{message}"
728        );
729        let message = refusal(&file(&[RATE, INPUTS], "   Time  Acc  Prs", &rows), "syntax");
730        assert!(message.contains("aren't the inputs"), "{message}");
731        let message = refusal(
732            &file(&[RATE, INPUTS], "   T  Acc  Prs  Dig", &rows),
733            "syntax",
734        );
735        assert!(message.contains("doesn't open with `Time`"), "{message}");
736    }
737
738    #[test]
739    fn rows_must_be_whole_and_in_time_order() {
740        let message = refusal(
741            &file(&[RATE, INPUTS], HEADER, &["0 1 1 0", "0.01 1 1"]),
742            "syntax",
743        );
744        assert!(
745            message.starts_with("line 10: a row of 3 cells where the header names 4"),
746            "{message}"
747        );
748        let message = refusal(
749            &file(&[RATE, INPUTS], HEADER, &["0.01 1 1 0", "0.01 1 1 0"]),
750            "syntax",
751        );
752        assert!(
753            message.starts_with("line 10: a `Time`, 0.01, not after"),
754            "{message}"
755        );
756        let message = refusal(
757            &file(&[RATE, INPUTS], HEADER, &["0 1 1 0", "inf 1 1 0"]),
758            "syntax",
759        );
760        assert!(
761            message.contains("a `Time` that isn't a number"),
762            "{message}"
763        );
764        let message = refusal(&file(&[RATE, INPUTS], HEADER, &["0 1 1 0"]), "unit");
765        assert!(message.contains("a single row"), "{message}");
766        refusal(&file(&[RATE, INPUTS], HEADER, &[]), "no data");
767        refusal(
768            &format!("{CONFIGURATION}\n{RATE}\n{INPUTS}\n{RAW_DATA}\n\n"),
769            "no data",
770        );
771    }
772
773    #[test]
774    fn cells_that_arent_numbers_are_gaps() {
775        let text = file(
776            &[RATE, INPUTS],
777            HEADER,
778            &["0 1 1 0", "0.01 x 1 G", "0.02 1 1e400 +10"],
779        );
780        let log = read(&text).unwrap();
781        assert!(log.channels[0].values[1].is_nan() && log.channels[2].values[1].is_nan());
782        // A sign isn't a hexadecimal digit.
783        assert!(log.channels[1].values[2].is_nan() && log.channels[2].values[2].is_nan());
784        assert!(log.notes.contains(
785            &"4 cells that aren't numbers read as gaps, the first on line 10".to_owned()
786        ));
787    }
788
789    #[test]
790    fn a_count_must_be_a_whole_number_the_converter_can_write() {
791        let ok = file(&[RATE, INPUTS], HEADER, &["0 0 1023 0", "0.01 1 1 0"]);
792        assert!(read(&ok).is_ok());
793        for cell in ["1024", "-1", "1.5", "101.3"] {
794            let row = format!("0.01 {cell} 1 0");
795            let message = refusal(&file(&[RATE, INPUTS], HEADER, &["0 1 1 0", &row]), "syntax");
796            assert!(
797                message.starts_with(&format!("line 10: `Acc` holds \"{cell}\", not a count")),
798                "{message}"
799            );
800        }
801    }
802
803    #[test]
804    fn quoted_text_is_bounded() {
805        let long = "y".repeat(10_000);
806        let many: Vec<String> = (0..150).map(|k| format!("Setting {k}.")).collect();
807        let mut config: Vec<&str> = vec![RATE, INPUTS, &long];
808        config.extend(many.iter().map(String::as_str));
809        let log = read(&file(&config, HEADER, &["0 1 1 0", "0.01 1 1 0"])).unwrap();
810        let quoted = log
811            .notes
812            .iter()
813            .find(|n| n.starts_with("the configuration"))
814            .unwrap();
815        assert!(
816            quoted.ends_with(" | and 113 more lines"),
817            "{}",
818            &quoted[quoted.len() - 60..]
819        );
820        assert!(quoted.len() < 40 * (QUOTED_CHARS + 3) + 100);
821        // An error quoting a long line quotes 200 characters of it.
822        let rate = format!("Data is sampled at {long}");
823        let message = refusal(&file(&[&rate, INPUTS], HEADER, &["0 1 1 0"]), "syntax");
824        assert!(message.len() < QUOTED_CHARS + 60, "{}", message.len());
825    }
826}