Skip to main content

hpr_flightdata/
altimetercloud.rs

1//! Rocketry Ltd's flight logs as CSV, as a flight page on altimetercloud.com hands them back: a
2//! Nano V1's, or a Mercury's.
3//!
4//! Rocketry Ltd's altimeters, the Nano V1, the Mercury V1 and Mercury Plus, and the Jupiter, upload
5//! their flights to AltimeterCloud, which shows each on a flight page and gives back its log as a
6//! CSV file. Every one has a Bosch BMP581 barometer, and the Nano V1 and Mercury V1 a ±32 g
7//! accelerometer and a ±2,000 °/s gyroscope (*Altimeters*,
8//! <https://www.altimetercloud.com/altimeters/>, read 2026-10-13). Two layouts are in the
9//! project's sample logs. A Nano V1's opens with a flight summary of keys and values, then a blank
10//! line, then its header; its columns are those of the Nano V1 manual's page *CSV Flight log
11//! format* (<https://www.altimetercloud.com/support/nano-v1/flight-log-format/>, read 2026-10-13):
12//!
13//! ```text
14//! Apogee meters,120.50,,Landing meters,-0.40,Burnout time (ms),900,
15//! Apogee time,5.120,,Landing time,30.000,Burnout velocity (m/s),40.000,
16//! …
17//!
18//! Time(ms),Altitude(m),Velocity(m/s),acceleration_x(mG),…,Board temp(C),pitch,roll,yaw,tilt
19//! -2000,0.010,0.00,0,0,0,-1000.0,0,0,0,,0.00,0.00,0.00,0.00,
20//! ```
21//!
22//! A Mercury's opens straight on its header, in lower case, with the channels the Mercury V1
23//! manual says it logs, "temperature, pressure, air density, predicted apogee and air brake
24//! output" beside the Nano's (*Mercury V1 manual*,
25//! <https://www.altimetercloud.com/support/mercuryv1/>, read 2026-10-13):
26//!
27//! ```text
28//! time(ms),altitude(m),velocity_pressure(m/s),acceleration_x(mG),…,bmp_temp(x100),pitch,roll,
29//! yaw,tilt,mt1_temp,apogee_prediction,air_brake_percent,target_alt,air_density,
30//! ```
31//!
32//! The logger is named from the header: a Rocketry Ltd Mercury if it has a column only the
33//! Mercury's layout has (`velocity_pressure(m/s)`, `bmp_temp(x100)`, `mt1_temp`,
34//! `apogee_prediction`, `air_brake_percent`, `target_alt` or `air_density`), else a Rocketry Ltd
35//! Nano V1, whose manual describes the other layout; a note on the record says which.
36//!
37//! **Recognition.** Within the file's first 65 lines, a header whose first name is `Time(ms)`, in
38//! any case, and which names `acceleration_total(mG)` and `tilt` too: following Debrief's reader
39//! (`lib/parsers/altimeterCloud.ts`, MIT, the project owner's own; see `THIRD-PARTY-NOTICES.md`),
40//! those two beside a millisecond clock are this logger's own names, and no other layout among
41//! the project's sample logs has them. The lines above the header are the summary.
42//!
43//! **Columns.** Every column is a channel; a column the reader doesn't know is kept as written,
44//! [`Quantity::Unrecognized`], with a note. Each column holds only the rows that write it: the Nano
45//! writes its board temperature on every 50th row and leaves it blank between (Nano manual), and
46//! the Mercury's slower columns are as sparse, so a channel's clock is the times of its own rows.
47//! The primary track's temperature is on the track's clock, a gap where the row has none.
48//!
49//! | Column | Read as | From |
50//! |---|---|---|
51//! | `Time(ms)`, `time(ms)` | the clock: milliseconds from lift-off, before it negative | the header; Nano manual ("time from lift-off, with lift-off as zero") |
52//! | `Altitude(m)`, `altitude(m)` | [`Quantity::AltitudeAboveGround`], m: the primary track | the header; Nano manual: the height above the pad after the logger's TrueFuse and TruePath processing |
53//! | `Velocity(m/s)`, `velocity_pressure(m/s)` | [`Quantity::Speed`], m/s, up positive: the primary track's vertical speed | the header; Nano manual: the vertical speed of the processed altitude; the Mercury's, by its name, the barometer's |
54//! | `acceleration_x(mG)`, `_y`, `_z` | [`Quantity::Acceleration`], thousandths of standard gravity, 9.806 65 m/s² | the header; Nano manual: along each of the board's axes |
55//! | `acceleration_total(mG)` | [`Quantity::Acceleration`], as the axes, but the acceleration's magnitude less 1 g, −1 g in free fall, along no axis; the record has no quantity for that, and a note says what it is | the header; Nano manual ("with the 1 g of gravity taken out, so it sits near zero at rest") |
56//! | `gyro_x`, `gyro_y`, `gyro_z` | [`Quantity::AngularRate`], °/s | the Nano manual; a note says so |
57//! | `Board temp(C)` | [`Quantity::Temperature`], °C | the header; Nano manual |
58//! | `bmp_temp(x100)` | [`Quantity::Temperature`], hundredths of a °C, when the values bear it out (below) | the name; Debrief's reader; a note says how |
59//! | `pitch`, `roll`, `yaw`, `tilt` | [`Quantity::Angle`], degrees | the Nano manual (attitude angles from their zero on the pad, and the tilt from vertical); a note says so |
60//! | `Pressure(hPa)` | [`Quantity::Pressure`], hPa × 100 Pa | the header; Nano manual |
61//! | `Raw altitude(m)` | [`Quantity::AltitudeAboveGround`], m: the barometer's altitude before processing | the header; Nano manual |
62//! | `air_brake_percent` | [`Quantity::Number`], the percentage as written | the name; Mercury manual ("air brake output"); a note says so |
63//! | `mt1_temp`, `apogee_prediction`, `target_alt`, `air_density` | [`Quantity::Unrecognized`], as written | no source states their units; a prediction and a setting aren't readings, and the record has no quantity for a density; a note each |
64//!
65//! The Mercury's manual describes none of its columns, so where they share the Nano's names, the
66//! Nano's manual is taken for them too, and the notes say so.
67//!
68//! *The centi-degree temperature.* `bmp_temp(x100)` holds the BMP581's temperature times 100, as
69//! its name says and Debrief's reader takes it: 2150 for 21.50 °C, a whole number in every one of
70//! the 4 Mercury files among the project's sample logs as of 2026-10-13. It is read so if every
71//! reading is a whole number, every one divided by 100 lies within the −40 to +85 °C the BMP581
72//! measures, and at least one as written lies beyond it, so that the column can't be in degrees
73//! (Bosch Sensortec, *BMP581 data sheet*, BST-BMP581-DS004-13, revision 1.13, April 2025, table 2,
74//! p. 10: the temperature measurement range, −40 to 85 °C). Otherwise it is kept as written,
75//! [`Quantity::Unrecognized`], with a note.
76//!
77//! **Placeholders.** Two kinds of cell, as of 2026-10-13 in every one of the 11 files among the
78//! project's sample logs, hold a number that isn't a reading; each is a gap, counted in a note:
79//!
80//! - *The accelerometer before it reads.* The first rows, all before lift-off, write 0 on all three
81//!   axes (and −1000 mG for the total, a magnitude of zero less 1 g), where an accelerometer at
82//!   rest reads 1 g. On the leading rows with a negative time whose three axes are all exactly 0,
83//!   the axes and the total are gaps. A row at or after lift-off is kept as written: in free fall
84//!   an accelerometer does read about zero.
85//! - *The temperature after it stops.* The last readings of the board temperature are exactly 0,
86//!   where the reading before was 27 to 44 °C. A run of exactly 0 that ends the column is a gap if
87//!   the reading before it lies more than 5 °C from 0: a board can't cool that far between two
88//!   readings and then hold at exactly 0.00 °C. On a day near freezing the zeros are kept.
89//!
90//! The yaw is kept as written, but a note counts the rows where it reaches ±327.67°: as of
91//! 2026-10-13, in no sample log does the yaw pass ±327.68°, ±32,768 hundredths of a degree, the
92//! range of a signed 16-bit number, though in all 11 it comes within 0.2° of it and jumps by more
93//! than 300° between two rows 22 to 121 times, so a yaw beyond that range isn't written as it
94//! was: it is clipped, or wrapped to the other end.
95//!
96//! **The summary.** The lines above the header are keys each followed by its value, groups of them
97//! parted by an empty cell, as `Apogee meters,120.50,,Landing meters,-0.40,…`; a key with no value
98//! is followed by an empty cell. The reader keeps:
99//!
100//! - `Apogee meters` as the stated apogee ([`Stated::apogee_m`]), m, as the key says;
101//! - `Serial number` and `Firmware`;
102//! - `Burnout time (ms)` and `Ejection time (ms)` as events, `Burnout` and `Ejection`;
103//! - `Apogee time` as an event, `Apogee`, in seconds if the altitude bears it out: if the highest
104//!   altitude's time lies within 0.5 s of it read in seconds. The key states no unit; as of
105//!   2026-10-13, in the 7 files among the project's sample logs with a summary, it lies within
106//!   0.01 s of the highest altitude's time read in seconds, and a thousand times off read in
107//!   milliseconds;
108//! - `Landing time` as an event, `Landing`, in seconds if the altitude bears it out: if, read in
109//!   seconds, it comes after the highest altitude and the altitude there (on the line between the
110//!   rows either side, if no row is at that time) lies within 0.01 m of the summary's `Landing
111//!   meters`, and read in milliseconds it doesn't do both. The Nano manual's page *Flight info
112//!   page* (<https://www.altimetercloud.com/support/nano-v1/flight-info-page/>, read 2026-10-13)
113//!   gives the landing as "when it landed, and the height it came to rest at", but no unit for
114//!   either. Both are written to the millimeter, and the margin is ten times that; as of
115//!   2026-10-13, in the 7 files among the project's sample logs with a summary, the altitude at the
116//!   landing time read in seconds lies within 0.001 m of the landing height, and read in
117//!   milliseconds, on the climb, more than a meter from it. Without a landing height to check, the
118//!   landing time is left out.
119//!
120//!   A note says how each time was read, or why it was left out;
121//! - `Adjusted log`: a value other than `no` gets a note, as AltimeterCloud can hand back a log
122//!   recalculated after the flight with another pressure or temperature (Nano manual: the key
123//!   "reads 'no' for an original flight").
124//!
125//! The events are in time order, as a summary has no order of its own. The other entries, such as
126//! the settings the logger flew with, are counted in a note and left out.
127//!
128//! **Rows.** Every data row ends in a comma (Nano manual), so a row holds one more cell than the
129//! header names, an empty one; the Mercury's header ends in a comma too, its last name empty, and
130//! that name is dropped. A row as wide as the header, or one wider with the last cell empty, is
131//! read cell for cell; a row of another width, a cell that isn't a number, a row with no time or a
132//! time that doesn't increase is refused, naming its line. Blank lines are skipped. Lines may end
133//! in CR, LF or both, and a byte-order mark is dropped. As every row must be as wide as the
134//! header, a short file can't make the reader store more cells than it writes.
135//!
136//! **Where this comes from.** The recognition and the centi-degree temperature follow Debrief's
137//! reader; the columns' units, the clock's zero and the placeholders' rules are the Nano manual's
138//! and the files'; the summary's times are read as the altitude bears them out. Where this reader departs from Debrief's: it keeps every column, where Debrief
139//! leaves the gyroscope, the attitude angles and the Mercury's prediction and air-brake columns out;
140//! it keeps the summary's apogee, serial number and events; and it refuses what Debrief passes over.
141
142use std::collections::HashSet;
143
144use crate::error::LogError;
145use crate::log::{Channel, Event, FlightLog, LogFormat, Quantity, Stated};
146use crate::table::{self, Notes, finite, numbered_lines, short};
147
148/// The format's name in errors.
149const FORMAT: &str = "AltimeterCloud CSV";
150
151/// The most lines, the summary's and blank ones, above the header.
152const MAX_SUMMARY_LINES: usize = 64;
153
154/// Standard gravity, m/s² (CGPM 1901): a thousandth of it is one `mG`.
155const G0_M_S2: f64 = 9.806_65;
156
157/// The temperatures the BMP581 measures, °C (module docs).
158const BMP581_RANGE_C: (f64, f64) = (-40.0, 85.0);
159
160/// How far, °C, from 0 the reading before a closing run of zeros must lie for the zeros to be
161/// gaps (module docs).
162const TEMPERATURE_JUMP_C: f64 = 5.0;
163
164/// How far, s, the stated apogee time may lie from the highest altitude's for it to be read in
165/// seconds (module docs).
166const APOGEE_TIME_MARGIN_S: f64 = 0.5;
167
168/// How far, m, the altitude at the stated landing time may lie from the stated landing height
169/// for the time to be read so (module docs): ten times the millimeter both are written to.
170const LANDING_HEIGHT_MARGIN_M: f64 = 0.01;
171
172/// The yaw, degrees, from which a note counts the rows as at the end of its range (module docs).
173const YAW_END_DEG: f64 = 327.67;
174
175/// What a header column holds.
176#[derive(Debug, Clone, Copy, PartialEq, Eq)]
177enum Kind {
178    Time,
179    Altitude,
180    Velocity,
181    /// One of the accelerometer's three axes.
182    Axis,
183    /// The acceleration's magnitude less 1 g.
184    Total,
185    Gyro,
186    BoardTemperature,
187    CentiTemperature,
188    /// Pitch, roll or tilt.
189    Angle,
190    Yaw,
191    Pressure,
192    RawAltitude,
193    AirBrake,
194    /// A column the reader knows but keeps as written; the words say why.
195    Kept(&'static str),
196    Unknown,
197}
198
199/// The names the reader knows, in lower case, and what each holds.
200const KNOWN: [(&str, Kind); 23] = [
201    ("time(ms)", Kind::Time),
202    ("altitude(m)", Kind::Altitude),
203    ("velocity(m/s)", Kind::Velocity),
204    ("velocity_pressure(m/s)", Kind::Velocity),
205    ("acceleration_x(mg)", Kind::Axis),
206    ("acceleration_y(mg)", Kind::Axis),
207    ("acceleration_z(mg)", Kind::Axis),
208    ("acceleration_total(mg)", Kind::Total),
209    ("gyro_x", Kind::Gyro),
210    ("gyro_y", Kind::Gyro),
211    ("gyro_z", Kind::Gyro),
212    ("board temp(c)", Kind::BoardTemperature),
213    ("bmp_temp(x100)", Kind::CentiTemperature),
214    ("pitch", Kind::Angle),
215    ("roll", Kind::Angle),
216    ("yaw", Kind::Yaw),
217    ("tilt", Kind::Angle),
218    ("pressure(hpa)", Kind::Pressure),
219    ("raw altitude(m)", Kind::RawAltitude),
220    ("air_brake_percent", Kind::AirBrake),
221    (
222        "mt1_temp",
223        Kind::Kept("by its name a temperature, but no source states its unit or what it measures"),
224    ),
225    (
226        "apogee_prediction",
227        Kind::Kept(
228            "the Mercury's predicted apogee (Mercury V1 manual), a prediction rather than a \
229             reading, in a unit no source states",
230        ),
231    ),
232    (
233        "target_alt",
234        Kind::Kept(
235            "by its name the altitude the air brakes aim for, a setting rather than a reading, in \
236             a unit no source states",
237        ),
238    ),
239];
240
241/// The names only a Mercury's layout has, in lower case (module docs).
242const MERCURY_ONLY: [&str; 7] = [
243    "velocity_pressure(m/s)",
244    "bmp_temp(x100)",
245    "mt1_temp",
246    "apogee_prediction",
247    "air_brake_percent",
248    "target_alt",
249    "air_density",
250];
251
252/// What a column named so holds.
253fn kind_of(name: &str) -> Kind {
254    if name.eq_ignore_ascii_case("air_density") {
255        return Kind::Kept(
256            "the Mercury logs the air's density (Mercury V1 manual), in a unit no source states, \
257             and the record has no quantity for a density",
258        );
259    }
260    KNOWN
261        .iter()
262        .find(|(known, _)| name.eq_ignore_ascii_case(known))
263        .map_or(Kind::Unknown, |(_, kind)| *kind)
264}
265
266/// A header column: what it holds and its name as written.
267struct Column {
268    kind: Kind,
269    written: String,
270}
271
272/// Whether a line is this layout's header (module docs, *Recognition*). The cells are looked at
273/// one by one, never collected, so a long line costs no memory.
274fn is_header(line: &str) -> bool {
275    let mut cells = line.split(',').map(str::trim);
276    if !cells
277        .next()
278        .is_some_and(|first| first.eq_ignore_ascii_case("time(ms)"))
279    {
280        return false;
281    }
282    let (mut total, mut tilt) = (false, false);
283    for cell in cells {
284        total |= cell.eq_ignore_ascii_case("acceleration_total(mg)");
285        tilt |= cell.eq_ignore_ascii_case("tilt");
286        if total && tilt {
287            return true;
288        }
289    }
290    false
291}
292
293/// Lines of the file, each with its number from 1.
294type Lines<'a> = Vec<(usize, &'a str)>;
295
296/// The header's line number and text, and the lines above it, if one is within the first
297/// [`MAX_SUMMARY_LINES`] + 1 lines.
298fn find_header(text: &str) -> Option<(usize, &str, Lines<'_>)> {
299    let mut above = Vec::new();
300    for (number, line) in numbered_lines(text).take(MAX_SUMMARY_LINES + 1) {
301        if is_header(line) {
302            return Some((number, line, above));
303        }
304        above.push((number, line));
305    }
306    None
307}
308
309/// Whether the text is in this reader's layout, judged from its header row's names, never the
310/// file's name.
311pub(crate) fn recognizes(text: &str) -> bool {
312    find_header(text).is_some()
313}
314
315/// A summary entry: its line, its key and its value, both trimmed.
316struct Entry<'a> {
317    line: usize,
318    key: &'a str,
319    value: &'a str,
320}
321
322/// The summary's entries: on each line, a key and the cell after it, an empty cell between
323/// groups skipped where a key would be (module docs, *The summary*). An iterator, made afresh for
324/// each look-up, so that a long summary line costs no memory.
325fn summary_entries<'a>(lines: &'a [(usize, &'a str)]) -> impl Iterator<Item = Entry<'a>> + 'a {
326    lines.iter().flat_map(|&(line, text)| {
327        let mut cells = text.split(',').map(str::trim);
328        std::iter::from_fn(move || {
329            loop {
330                let key = cells.next()?;
331                if !key.is_empty() {
332                    return Some(Entry {
333                        line,
334                        key,
335                        value: cells.next().unwrap_or_default(),
336                    });
337                }
338            }
339        })
340    })
341}
342
343/// The rows read: each row's line and time as written, ms, and each column's cells, `NaN` where
344/// the row leaves the cell empty.
345struct Rows {
346    lines: Vec<usize>,
347    time_ms: Vec<f64>,
348    /// Per header column; empty for the time column.
349    values: Vec<Vec<f64>>,
350}
351
352/// Reads a Rocketry Ltd log as AltimeterCloud's CSV writes it, from its text.
353///
354/// # Errors
355///
356/// [`LogError::NotThisFormat`] if no line within the first 65 is this layout's header;
357/// [`LogError::Syntax`] for a header naming a column twice or leaving a name empty, a row of the
358/// wrong width, a cell that isn't a number or is too large to convert to SI, or a time that is
359/// missing or doesn't increase, naming the line; [`LogError::NoData`] if no row follows the
360/// header.
361pub fn read(text: &str) -> Result<FlightLog, LogError> {
362    let Some((header_line, header, above)) = find_header(text) else {
363        return Err(LogError::NotThisFormat {
364            format: FORMAT,
365            message: format!(
366                "none of its first {} lines is an AltimeterCloud header: `Time(ms)` first, and \
367                 `acceleration_total(mG)` and `tilt` among the names",
368                MAX_SUMMARY_LINES + 1
369            ),
370        });
371    };
372    let columns = columns(header, header_line)?;
373    let rows = read_rows(
374        numbered_lines(text).skip_while(|(number, _)| *number <= header_line),
375        &columns,
376    )?;
377    if rows.time_ms.is_empty() {
378        return Err(LogError::NoData { format: FORMAT });
379    }
380    build(&columns, rows, &above)
381}
382
383/// The header's columns; a name given twice, or left empty, is refused. A last empty name, the
384/// line's closing comma, is dropped. The names are read one by one, so a header refused for an
385/// empty name costs no memory for the names after it.
386fn columns(header: &str, line: usize) -> Result<Vec<Column>, LogError> {
387    let mut columns: Vec<Column> = Vec::new();
388    // The names met, in lower case: a set, so that a header of many names isn't checked in
389    // quadratic time.
390    let mut seen: HashSet<String> = HashSet::new();
391    let mut cells = header.split(',').map(str::trim).enumerate().peekable();
392    while let Some((index, cell)) = cells.next() {
393        if cell.is_empty() {
394            if index > 0 && cells.peek().is_none() {
395                break;
396            }
397            return Err(syntax(
398                line,
399                format!("the header's column {} has no name", index + 1),
400            ));
401        }
402        if !seen.insert(cell.to_ascii_lowercase()) {
403            return Err(syntax(
404                line,
405                format!("the header names {:?} twice", short(cell)),
406            ));
407        }
408        columns.push(Column {
409            kind: kind_of(cell),
410            written: cell.to_owned(),
411        });
412    }
413    if !columns.iter().any(|column| column.kind == Kind::Altitude) {
414        return Err(syntax(
415            line,
416            "the header names no altitude, `Altitude(m)`".to_owned(),
417        ));
418    }
419    Ok(columns)
420}
421
422/// Reads the data rows (module docs, *Rows*).
423fn read_rows<'a>(
424    lines: impl Iterator<Item = (usize, &'a str)>,
425    columns: &[Column],
426) -> Result<Rows, LogError> {
427    let width = columns.len();
428    let mut rows = Rows {
429        lines: Vec::new(),
430        time_ms: Vec::new(),
431        values: columns.iter().map(|_| Vec::new()).collect(),
432    };
433    for (number, line) in lines {
434        if line.trim().is_empty() {
435            continue;
436        }
437        // One more than a closing comma allows, so a wider row is still told apart.
438        let cells: Vec<&str> = line.split(',').map(str::trim).take(width + 2).collect();
439        let closing = cells.len() == width + 1 && cells.last().is_some_and(|c| c.is_empty());
440        if cells.len() != width && !closing {
441            return Err(syntax(
442                number,
443                format!(
444                    "the row has {} cells: the header names {width}, and a row may add one empty \
445                     cell after them, its closing comma",
446                    if cells.len() > width + 1 {
447                        format!("more than {}", width + 1)
448                    } else {
449                        cells.len().to_string()
450                    }
451                ),
452            ));
453        }
454        let time_ms = match cells.first() {
455            Some(cell) if !cell.is_empty() => number_in(cell, number, &columns[0].written)?,
456            _ => return Err(syntax(number, "the row has no time".to_owned())),
457        };
458        if let Some(&last) = rows.time_ms.last()
459            && time_ms <= last
460        {
461            return Err(syntax(
462                number,
463                format!("the time {time_ms} ms doesn't come after the one above it, {last} ms"),
464            ));
465        }
466        for (index, (column, cell)) in columns.iter().zip(&cells).enumerate().skip(1) {
467            let value = if cell.is_empty() {
468                f64::NAN
469            } else {
470                number_in(cell, number, &column.written)?
471            };
472            if let Some(store) = rows.values.get_mut(index) {
473                store.push(value);
474            }
475        }
476        rows.lines.push(number);
477        rows.time_ms.push(time_ms);
478    }
479    Ok(rows)
480}
481
482/// A cell as a number, or the error naming its line and column.
483fn number_in(cell: &str, line: usize, column: &str) -> Result<f64, LogError> {
484    table::number(cell, false).ok_or_else(|| {
485        syntax(
486            line,
487            format!("{:?} under {:?} isn't a number", short(cell), short(column)),
488        )
489    })
490}
491
492fn syntax(line: usize, message: String) -> LogError {
493    LogError::Syntax {
494        format: FORMAT,
495        line,
496        message,
497    }
498}
499
500/// The names of the columns of a kind, as a note lists them: `` `a`, `b` and `c` ``.
501fn names_of(columns: &[Column], kinds: &[Kind]) -> Option<String> {
502    let names: Vec<String> = columns
503        .iter()
504        .filter(|column| kinds.contains(&column.kind))
505        .map(|column| format!("`{}`", short(&column.written)))
506        .collect();
507    match names.as_slice() {
508        [] => None,
509        [one] => Some(one.clone()),
510        [rest @ .., last] => Some(format!("{} and {last}", rest.join(", "))),
511    }
512}
513
514/// The record from the rows read: each column's unit, the placeholders, the summary and the
515/// notes.
516///
517/// # Errors
518///
519/// [`LogError::Syntax`] for a value that its unit's conversion makes infinite.
520fn build(
521    columns: &[Column],
522    rows: Rows,
523    summary_lines: &[(usize, &str)],
524) -> Result<FlightLog, LogError> {
525    let mercury = columns.iter().any(|column| {
526        MERCURY_ONLY
527            .iter()
528            .any(|name| column.written.eq_ignore_ascii_case(name))
529    });
530    let time_s: Vec<f64> = rows.time_ms.iter().map(|ms| ms / 1000.0).collect();
531    let mut log = FlightLog {
532        format: LogFormat::AltimeterCloudCsv,
533        logger: if mercury {
534            "Rocketry Ltd Mercury"
535        } else {
536            "Rocketry Ltd Nano V1"
537        }
538        .to_owned(),
539        serial_number: None,
540        firmware: None,
541        flight_number: None,
542        stated: Stated::default(),
543        time_s: Vec::new(),
544        altitude_m: Vec::new(),
545        vertical_speed_m_s: None,
546        temperature_k: None,
547        battery_v: None,
548        channels: Vec::new(),
549        events: Vec::new(),
550        notes: Vec::new(),
551    };
552    log.notes.push(if mercury {
553        "the logger is taken as a Rocketry Ltd Mercury. Its header has columns only the \
554         Mercury's layout has, such as the predicted apogee and the air-brake output its manual \
555         lists"
556            .to_owned()
557    } else {
558        "the logger is taken as a Rocketry Ltd Nano V1: its header is the layout the Nano V1 \
559         manual's page *CSV Flight log format* describes"
560            .to_owned()
561    });
562    let mut line_notes = Notes::default();
563    let manual = if mercury {
564        "the Nano V1 manual, whose layout has the same columns"
565    } else {
566        "the Nano V1 manual"
567    };
568    if let Some(names) = names_of(columns, &[Kind::Gyro]) {
569        log.notes.push(format!(
570            "{names} read in degrees per second, from {manual}: its page *CSV Flight log format* \
571             describes them. Each is the rate of turn about one of the board's axes in °/s, as \
572             whole numbers. The altimeter's gyroscope reads up to ±2,000 °/s"
573        ));
574    }
575    if let Some(names) = names_of(columns, &[Kind::Angle, Kind::Yaw]) {
576        log.notes.push(format!(
577            "{names} read in degrees, from {manual}: its page *CSV Flight log format* says what \
578             each is. The pitch, roll and yaw are attitude angles from their zero on the pad. The \
579             tilt is the angle from vertical. Each is in degrees"
580        ));
581    }
582    if let Some(names) = names_of(columns, &[Kind::Total]) {
583        log.notes.push(format!(
584            "{names} read in thousandths of standard gravity, from its name and {manual}: its page \
585             *CSV Flight log format* describes it. It is the acceleration's magnitude with the \
586             1 g of gravity taken out, so it sits near zero at rest. It is kept so, the magnitude less \
587             1 g, not an acceleration along any axis. It reads −1 g in free fall"
588        ));
589    }
590
591    let pad_rows = accelerometer_pad_rows(columns, &rows);
592    let mut temperature: Option<Vec<f64>> = None;
593    for (index, column) in columns.iter().enumerate() {
594        let Some(values) = rows.values.get(index) else {
595            continue;
596        };
597        let (quantity, scale): (Quantity, fn(f64) -> f64) = match column.kind {
598            Kind::Time => continue,
599            Kind::Altitude | Kind::RawAltitude => (Quantity::AltitudeAboveGround, |v| v),
600            Kind::Velocity => (Quantity::Speed, |v| v),
601            Kind::Axis | Kind::Total => (Quantity::Acceleration, |v| v * G0_M_S2 / 1000.0),
602            Kind::Gyro => (Quantity::AngularRate, |v| v),
603            Kind::Angle | Kind::Yaw => (Quantity::Angle, |v| v),
604            Kind::Pressure => (Quantity::Pressure, |v| v * 100.0),
605            Kind::BoardTemperature => (Quantity::Temperature, |v| v + 273.15),
606            Kind::CentiTemperature => {
607                if centi_degrees(values, &column.written, &mut log.notes) {
608                    (Quantity::Temperature, |v| v / 100.0 + 273.15)
609                } else {
610                    (Quantity::Unrecognized, |v| v)
611                }
612            }
613            Kind::AirBrake => {
614                log.notes.push(format!(
615                    "`{}` read in percent, from its name: the air-brake output the Mercury logs \
616                     (Mercury V1 manual), kept as the percentage written",
617                    short(&column.written)
618                ));
619                (Quantity::Number, |v| v)
620            }
621            Kind::Kept(why) => {
622                log.notes.push(format!(
623                    "`{}` kept as written: {why}",
624                    short(&column.written)
625                ));
626                (Quantity::Unrecognized, |v| v)
627            }
628            Kind::Unknown => {
629                line_notes.push(format!(
630                    "`{}` kept as written, in a unit no source states: this reader doesn't know \
631                     the column",
632                    short(&column.written)
633                ));
634                (Quantity::Unrecognized, |v| v)
635            }
636        };
637        // A finite cell that its unit's conversion makes infinite, such as `1.7e308` mG or hPa, is
638        // refused on its row's line (ADR-258 §3).
639        let mut converted: Vec<f64> = Vec::with_capacity(values.len());
640        for (value, &line) in values.iter().zip(&rows.lines) {
641            converted.push(finite(scale(*value), FORMAT, line, &column.written)?);
642        }
643        if matches!(column.kind, Kind::Axis | Kind::Total) {
644            for value in converted.iter_mut().take(pad_rows) {
645                *value = f64::NAN;
646            }
647        }
648        if quantity == Quantity::Temperature {
649            closing_zeros(&mut converted, &rows.lines, &column.written, &mut log.notes);
650            temperature.get_or_insert_with(|| converted.clone());
651        }
652        if column.kind == Kind::Yaw {
653            yaw_end(values, &rows.lines, &column.written, &mut log.notes);
654        }
655        // The channel holds the rows the file writes the column on (module docs, *Columns*).
656        let (times, kept): (Vec<f64>, Vec<f64>) = values
657            .iter()
658            .zip(&converted)
659            .zip(&time_s)
660            .filter(|((written, _), _)| !written.is_nan())
661            .map(|((_, value), time)| (*time, *value))
662            .unzip();
663        log.channels.push(Channel {
664            column: column.written.clone(),
665            quantity,
666            time_s: times,
667            values: kept,
668        });
669        if column.kind == Kind::Altitude && log.altitude_m.is_empty() {
670            log.altitude_m = converted;
671        } else if column.kind == Kind::Velocity && log.vertical_speed_m_s.is_none() {
672            log.vertical_speed_m_s = Some(converted);
673        }
674    }
675    if pad_rows > 0
676        && let Some(names) = names_of(columns, &[Kind::Axis, Kind::Total])
677    {
678        log.notes.push(format!(
679            "the first {pad_rows} rows, before lift-off, to line {}, write 0 on all three of the \
680             accelerometer's axes, which reads 1 g at rest. It hasn't read yet, so {names} are \
681             gaps on them",
682            rows.lines.get(pad_rows - 1).copied().unwrap_or_default()
683        ));
684    }
685    log.temperature_k = temperature;
686    log.time_s = time_s;
687    summary(&mut log, summary_lines);
688    log.notes.extend(line_notes.into_vec());
689    Ok(log)
690}
691
692/// How many leading rows are the accelerometer's placeholders: before lift-off, with all three
693/// axes exactly 0 (module docs, *Placeholders*). Zero without all three axes.
694fn accelerometer_pad_rows(columns: &[Column], rows: &Rows) -> usize {
695    let axes: Vec<&Vec<f64>> = columns
696        .iter()
697        .zip(&rows.values)
698        .filter(|(column, _)| column.kind == Kind::Axis)
699        .map(|(_, values)| values)
700        .collect();
701    if axes.len() != 3 {
702        return 0;
703    }
704    rows.time_ms
705        .iter()
706        .enumerate()
707        .take_while(|(row, time)| {
708            **time < 0.0 && axes.iter().all(|axis| axis.get(*row) == Some(&0.0))
709        })
710        .count()
711}
712
713/// Whether `bmp_temp(x100)` is in hundredths of a degree Celsius (module docs), with its note.
714fn centi_degrees(values: &[f64], written: &str, notes: &mut Vec<String>) -> bool {
715    let readings: Vec<f64> = values.iter().copied().filter(|v| !v.is_nan()).collect();
716    let (low, high) = BMP581_RANGE_C;
717    let within = |celsius: f64| (low..=high).contains(&celsius);
718    let whole = readings.iter().all(|value| value.fract() == 0.0);
719    let scaled = readings.iter().all(|value| within(value / 100.0));
720    let beyond = readings.iter().filter(|value| !within(**value)).count();
721    let span = span(&readings);
722    if !readings.is_empty() && whole && scaled && beyond > 0 {
723        notes.push(format!(
724            "`{}` read in hundredths of a degree Celsius, from its name and the values: every \
725             reading, {span} as written, is a whole number. Divided by 100, every reading lies \
726             within the {low} to {high} °C the BMP581 measures. As written, {beyond} lie beyond \
727             it",
728            short(written)
729        ));
730        true
731    } else {
732        let why = if readings.is_empty() {
733            "it holds no reading".to_owned()
734        } else if !whole {
735            format!("its readings, {span}, aren't all whole numbers")
736        } else if !scaled {
737            format!(
738                "its readings, {span}, don't all lie within the {low} to {high} °C the BMP581 \
739                 measures when divided by 100"
740            )
741        } else {
742            format!(
743                "its readings, {span}, all lie within the {low} to {high} °C the BMP581 measures \
744                 as written too, so they could be degrees"
745            )
746        };
747        notes.push(format!(
748            "`{}` kept as written, its unit unknown: {why}",
749            short(written)
750        ));
751        false
752    }
753}
754
755/// Turns a closing run of exactly 0 °C into gaps if the reading before it lies more than
756/// [`TEMPERATURE_JUMP_C`] from 0 (module docs, *Placeholders*), with a note. `kelvin` holds the
757/// column's values in K, `NaN` on rows without a reading.
758fn closing_zeros(kelvin: &mut [f64], lines: &[usize], written: &str, notes: &mut Vec<String>) {
759    const ZERO_C_K: f64 = 273.15;
760    let readings: Vec<usize> = (0..kelvin.len())
761        .filter(|&row| !kelvin[row].is_nan())
762        .collect();
763    let zeros = readings
764        .iter()
765        .rev()
766        .take_while(|&&row| kelvin[row] == ZERO_C_K)
767        .count();
768    let Some(&before) = readings
769        .len()
770        .checked_sub(zeros + 1)
771        .and_then(|at| readings.get(at))
772    else {
773        return;
774    };
775    let before_c = kelvin[before] - ZERO_C_K;
776    if zeros == 0 || before_c.abs() <= TEMPERATURE_JUMP_C {
777        return;
778    }
779    let first = readings[readings.len() - zeros];
780    for &row in &readings[readings.len() - zeros..] {
781        kelvin[row] = f64::NAN;
782    }
783    notes.push(format!(
784        "the last {zeros} readings of `{}`, from line {}, are exactly 0 °C after a reading of \
785         {before_c:.2} °C on line {}. A board can't cool so far between two readings and hold \
786         there, so they aren't readings and are gaps",
787        short(written),
788        lines.get(first).copied().unwrap_or_default(),
789        lines.get(before).copied().unwrap_or_default()
790    ));
791}
792
793/// Notes the rows where the yaw reaches the end of its range (module docs, *Placeholders*).
794fn yaw_end(values: &[f64], lines: &[usize], written: &str, notes: &mut Vec<String>) {
795    let at_end = |value: &f64| value.abs() >= YAW_END_DEG;
796    if let Some(first) = values.iter().position(at_end) {
797        notes.push(format!(
798            "`{}` reaches ±{YAW_END_DEG}° or beyond on {} rows, the first on line {}. In no \
799             sample log does the yaw pass ±327.68°, ±32,768 hundredths of a degree, the range of \
800             a signed 16-bit number. So a yaw beyond it isn't written as it was, but clipped or \
801             wrapped to the other end. It is kept as written",
802            short(written),
803            values.iter().filter(|value| at_end(value)).count(),
804            lines.get(first).copied().unwrap_or_default()
805        ));
806    }
807}
808
809/// The least and greatest of the values, as a note writes them.
810fn span(values: &[f64]) -> String {
811    let (low, high) = values
812        .iter()
813        .fold((f64::INFINITY, f64::NEG_INFINITY), |(low, high), &value| {
814            (low.min(value), high.max(value))
815        });
816    if low.is_finite() {
817        format!("from {low} to {high}")
818    } else {
819        "none".to_owned()
820    }
821}
822
823/// Keeps what the summary states (module docs, *The summary*): the apogee, the serial number, the
824/// firmware and the events, with notes. `lines` are the lines above the header.
825fn summary(log: &mut FlightLog, lines: &[(usize, &str)]) {
826    const USED: [&str; 8] = [
827        "Apogee meters",
828        "Apogee time",
829        "Landing time",
830        "Burnout time (ms)",
831        "Ejection time (ms)",
832        "Serial number",
833        "Firmware",
834        "Adjusted log",
835    ];
836    let entry =
837        |key: &str| summary_entries(lines).find(|entry| entry.key.eq_ignore_ascii_case(key));
838    let mut notes = Vec::new();
839    let number = |key: &str, notes: &mut Vec<String>| {
840        let entry = entry(key)?;
841        let value = table::number(entry.value, false);
842        if value.is_none() {
843            notes.push(format!(
844                "the summary's `{key}` on line {}, {:?}, isn't a number: left out",
845                entry.line,
846                short(entry.value)
847            ));
848        }
849        value
850    };
851    log.stated.apogee_m = number("Apogee meters", &mut notes);
852    let text = |key: &str| {
853        entry(key)
854            .map(|entry| short(entry.value))
855            .filter(|value| !value.is_empty())
856    };
857    log.serial_number = text("Serial number");
858    log.firmware = text("Firmware");
859    let mut events = Vec::new();
860    for (key, name) in [
861        ("Burnout time (ms)", "Burnout"),
862        ("Ejection time (ms)", "Ejection"),
863    ] {
864        if let Some(ms) = number(key, &mut notes) {
865            events.push(Event {
866                time_s: ms / 1000.0,
867                name: name.to_owned(),
868            });
869        }
870    }
871    // The highest altitude's time, the first if it is reached twice.
872    let peak_s = log
873        .altitude_m
874        .iter()
875        .enumerate()
876        .filter(|(_, value)| !value.is_nan())
877        .fold(
878            None,
879            |best: Option<(usize, f64)>, (row, &value)| match best {
880                Some((_, top)) if top >= value => best,
881                _ => Some((row, value)),
882            },
883        )
884        .and_then(|(row, _)| log.time_s.get(row).copied());
885    if let Some(apogee_s) = number("Apogee time", &mut notes) {
886        match peak_s {
887            Some(peak_s) if (apogee_s - peak_s).abs() <= APOGEE_TIME_MARGIN_S => {
888                notes.push(format!(
889                    "`Apogee time` read in seconds, from the altitude: the highest altitude is at \
890                     {peak_s} s, within {APOGEE_TIME_MARGIN_S} s of its {apogee_s}. Read in \
891                     milliseconds, it would be at the lift-off"
892                ));
893                events.push(Event {
894                    time_s: apogee_s,
895                    name: "Apogee".to_owned(),
896                });
897            }
898            _ => notes.push(format!(
899                "the summary's `Apogee time`, {apogee_s}, is left out. The key states no unit, and \
900                 read in seconds it isn't within {APOGEE_TIME_MARGIN_S} s of the highest \
901                 altitude's time, {}",
902                peak_s.map_or("which the file doesn't give".to_owned(), |t| format!(
903                    "{t} s"
904                ))
905            )),
906        }
907    }
908    if let Some(landing) = number("Landing time", &mut notes) {
909        let height_m = entry("Landing meters").and_then(|entry| table::number(entry.value, false));
910        match landing_in_seconds(log, landing, height_m, peak_s) {
911            Ok(note) => {
912                notes.push(note);
913                events.push(Event {
914                    time_s: landing,
915                    name: "Landing".to_owned(),
916                });
917            }
918            Err(why) => notes.push(format!(
919                "the summary's `Landing time`, {landing}, is left out: the key states no unit, \
920                 and {why}"
921            )),
922        }
923    }
924    events.sort_by(|a, b| a.time_s.total_cmp(&b.time_s));
925    log.events = events;
926    if let Some(adjusted) = entry("Adjusted log")
927        && !adjusted.value.eq_ignore_ascii_case("no")
928    {
929        notes.push(format!(
930            "the summary's `Adjusted log` is {:?}. AltimeterCloud can hand back a log \
931             recalculated after the flight with another pressure or temperature (Nano V1 \
932             manual). So this one's figures may not be the logger's own",
933            short(adjusted.value)
934        ));
935    }
936    let is_other = |entry: &Entry<'_>| !USED.iter().any(|key| entry.key.eq_ignore_ascii_case(key));
937    if let Some(first) = summary_entries(lines).find(is_other) {
938        notes.push(format!(
939            "the summary's other {} entries, such as `{}`, are left out: the record has no place \
940             for them",
941            summary_entries(lines).filter(is_other).count(),
942            short(first.key)
943        ));
944    }
945    log.notes.extend(notes);
946}
947
948/// The altitude at a time, m: the row's at that time, or on a straight line between the rows
949/// either side of it; `None` outside the log or beside a row without an altitude.
950fn altitude_at(time_s: &[f64], altitude_m: &[f64], at_s: f64) -> Option<f64> {
951    let after = time_s.partition_point(|&time| time < at_s);
952    let (t1, a1) = (*time_s.get(after)?, *altitude_m.get(after)?);
953    let altitude = if t1 == at_s {
954        a1
955    } else {
956        let before = after.checked_sub(1)?;
957        let (t0, a0) = (*time_s.get(before)?, *altitude_m.get(before)?);
958        a0 + (at_s - t0) / (t1 - t0) * (a1 - a0)
959    };
960    altitude.is_finite().then_some(altitude)
961}
962
963/// Whether the summary's landing time, `landing` as written, is in seconds (module docs, *The
964/// summary*): the note saying so, or why it is left out. It is if, read so, it comes after the
965/// highest altitude and the altitude there is the stated landing height, `height_m`, within
966/// [`LANDING_HEIGHT_MARGIN_M`], and read in milliseconds it doesn't do both.
967fn landing_in_seconds(
968    log: &FlightLog,
969    landing: f64,
970    height_m: Option<f64>,
971    peak_s: Option<f64>,
972) -> Result<String, String> {
973    let Some(height_m) = height_m else {
974        return Err(
975            "the summary gives no landing height, `Landing meters`, to check it \
976                    against"
977                .to_owned(),
978        );
979    };
980    let Some(peak_s) = peak_s else {
981        return Err("the log gives no highest altitude to check it against".to_owned());
982    };
983    let at = |time_s: f64| altitude_at(&log.time_s, &log.altitude_m, time_s);
984    let agrees = |time_s: f64| {
985        time_s >= peak_s
986            && at(time_s).is_some_and(|a| (a - height_m).abs() <= LANDING_HEIGHT_MARGIN_M)
987    };
988    // What the altitude says of a reading that doesn't agree, the time as written with its unit.
989    let denies = |time_s: f64, written: &str| {
990        if time_s < peak_s {
991            format!("{written} comes before the highest altitude, at {peak_s} s")
992        } else {
993            match at(time_s) {
994                Some(a) => format!(
995                    "the altitude at {written} is {a:.3} m, not within \
996                     {LANDING_HEIGHT_MARGIN_M} m of the stated landing height, {height_m} m"
997                ),
998                None => format!("the log gives no altitude at {written}"),
999            }
1000        }
1001    };
1002    let ms_s = landing / 1000.0;
1003    match (agrees(landing), agrees(ms_s)) {
1004        (true, false) => Ok(format!(
1005            "`Landing time` read in seconds, from the altitude: at {landing} s, after the highest \
1006             altitude at {peak_s} s, the altitude is {:.3} m. That is within \
1007             {LANDING_HEIGHT_MARGIN_M} m of the summary's `Landing meters`, {height_m} m. That key \
1008             is the height the rocket came to rest at (Nano V1 manual, *Flight info page*). Read \
1009             in milliseconds, {}",
1010            at(landing).unwrap_or(f64::NAN),
1011            denies(ms_s, &format!("{landing} ms"))
1012        )),
1013        (true, true) => Err(format!(
1014            "read in seconds or in milliseconds alike, the altitude there, after the highest, is \
1015             within {LANDING_HEIGHT_MARGIN_M} m of the stated landing height, {height_m} m"
1016        )),
1017        (false, _) => Err(format!(
1018            "read in seconds, {}",
1019            denies(landing, &format!("{landing} s"))
1020        )),
1021    }
1022}
1023
1024#[cfg(test)]
1025mod tests {
1026    use super::*;
1027
1028    const NANO_HEADER: &str = "Time(ms),Altitude(m),Velocity(m/s),acceleration_x(mG),\
1029        acceleration_y(mG),acceleration_z(mG),acceleration_total(mG),gyro_x,gyro_y,gyro_z,\
1030        Board temp(C),pitch,roll,yaw,tilt";
1031
1032    const MERCURY_HEADER: &str = "time(ms),altitude(m),velocity_pressure(m/s),acceleration_x(mG),\
1033        acceleration_y(mG),acceleration_z(mG),acceleration_total(mG),gyro_x,gyro_y,gyro_z,\
1034        bmp_temp(x100),pitch,roll,yaw,tilt,mt1_temp,apogee_prediction,air_brake_percent,\
1035        target_alt,air_density,";
1036
1037    /// A Nano row: time, altitude, the three axes, the temperature cell and the yaw.
1038    fn nano_row(ms: i32, altitude: f64, axes: [i32; 3], temperature: &str, yaw: f64) -> String {
1039        let [x, y, z] = axes;
1040        let total = (f64::from(x * x + y * y + z * z)).sqrt() - 1000.0;
1041        format!(
1042            "{ms},{altitude:.3},0.00,{x},{y},{z},{total:.1},1,-2,3,{temperature},1.00,2.00,{yaw:.2},3.00,"
1043        )
1044    }
1045
1046    fn nano(rows: &[String]) -> String {
1047        format!("{NANO_HEADER}\n{}\n", rows.join("\n"))
1048    }
1049
1050    fn note<'a>(log: &'a FlightLog, start: &str) -> &'a str {
1051        let matching: Vec<&String> = log.notes.iter().filter(|n| n.starts_with(start)).collect();
1052        assert_eq!(matching.len(), 1, "{start}: {:#?}", log.notes);
1053        matching[0]
1054    }
1055
1056    fn has_note(log: &FlightLog, start: &str) -> bool {
1057        log.notes.iter().any(|n| n.starts_with(start))
1058    }
1059
1060    #[test]
1061    fn the_header_is_recognized_by_its_own_names() {
1062        assert!(recognizes(&format!(
1063            "{NANO_HEADER}\n0,0,0,0,0,0,0,0,0,0,,0,0,0,0,\n"
1064        )));
1065        assert!(recognizes(&format!("{MERCURY_HEADER}\n")));
1066        // After a summary block, and in any case.
1067        let summary = "Apogee meters,1.0,,\n".repeat(20);
1068        assert!(recognizes(&format!(
1069            "{summary}\n{}\n",
1070            NANO_HEADER.to_ascii_uppercase()
1071        )));
1072        // Not without the signature, not without the clock first, not past 64 lines.
1073        assert!(!recognizes("Time(ms),Altitude(m),tilt\n0,0,0\n"));
1074        assert!(!recognizes("Time(ms),Altitude(m),acceleration_total(mG)\n"));
1075        assert!(!recognizes(
1076            "Altitude(m),Time(ms),acceleration_total(mG),tilt\n"
1077        ));
1078        assert!(!recognizes("Time (s),Alt (ft)\n0,0\n1,100\n"));
1079        let long = "x,1\n".repeat(MAX_SUMMARY_LINES + 1);
1080        assert!(!recognizes(&format!("{long}{NANO_HEADER}\n")));
1081        let just = "x,1\n".repeat(MAX_SUMMARY_LINES);
1082        assert!(recognizes(&format!("{just}{NANO_HEADER}\n")));
1083    }
1084
1085    #[test]
1086    fn summary_entries_pair_keys_with_values_across_empty_cells() {
1087        let lines = [
1088            (
1089                1,
1090                "Apogee meters,12.5,,Landing meters,-0.1,Burnout time (ms),900,",
1091            ),
1092            (2, "Device tag,Unit A,,Competitor tag,,"),
1093            (3, "Firmware, 1.00,,Lockouts,0,"),
1094        ];
1095        let pairs: Vec<(usize, &str, &str)> = summary_entries(&lines)
1096            .map(|e| (e.line, e.key, e.value))
1097            .collect();
1098        assert_eq!(
1099            pairs,
1100            [
1101                (1, "Apogee meters", "12.5"),
1102                (1, "Landing meters", "-0.1"),
1103                (1, "Burnout time (ms)", "900"),
1104                (2, "Device tag", "Unit A"),
1105                (2, "Competitor tag", ""),
1106                (3, "Firmware", "1.00"),
1107                (3, "Lockouts", "0"),
1108            ]
1109        );
1110    }
1111
1112    #[test]
1113    fn the_clock_is_milliseconds_and_every_column_is_a_channel() {
1114        let text = nano(&[
1115            nano_row(-40, 0.0, [10, 20, -990], "25.00", 0.0),
1116            nano_row(-20, 0.001, [10, 20, -990], "", 0.0),
1117            nano_row(0, 0.002, [500, 20, -3000], "", 0.0),
1118            nano_row(25, 0.5, [600, 20, -4000], "", 0.0),
1119        ]);
1120        let log = read(&text).unwrap();
1121        assert_eq!(log.format, LogFormat::AltimeterCloudCsv);
1122        assert_eq!(log.logger, "Rocketry Ltd Nano V1");
1123        assert_eq!(log.time_s, [-0.04, -0.02, 0.0, 0.025]);
1124        assert_eq!(log.altitude_m, [0.0, 0.001, 0.002, 0.5]);
1125        assert_eq!(log.channels.len(), 14);
1126        // 1000 mG is standard gravity.
1127        let x = &log.channels[2];
1128        assert_eq!(x.column, "acceleration_x(mG)");
1129        assert!((x.values[0] - 10.0 * G0_M_S2 / 1000.0).abs() < 1e-12);
1130        // The temperature's channel holds only its row; the track has gaps.
1131        let temperature = log
1132            .channels
1133            .iter()
1134            .find(|c| c.quantity == Quantity::Temperature)
1135            .unwrap();
1136        assert_eq!(temperature.time_s, [-0.04]);
1137        assert!((temperature.values[0] - 298.15).abs() < 1e-9);
1138        let track = log.temperature_k.as_deref().unwrap();
1139        assert!((track[0] - 298.15).abs() < 1e-9);
1140        assert!(track[1..].iter().all(|t| t.is_nan()));
1141        note(
1142            &log,
1143            "`gyro_x`, `gyro_y` and `gyro_z` read in degrees per second, from the Nano V1 manual: ",
1144        );
1145        note(
1146            &log,
1147            "`pitch`, `roll`, `yaw` and `tilt` read in degrees, from the Nano V1 manual: ",
1148        );
1149        assert!(log.events.is_empty());
1150    }
1151
1152    /// A value near the largest `f64` is finite as written and infinite once converted: in
1153    /// thousandths of g to m/s², in hPa to pascals. Each is refused on its line (ADR-258 §3).
1154    #[test]
1155    fn a_value_its_conversion_makes_infinite_is_refused() {
1156        let text = nano(&[
1157            nano_row(0, 0.0, [10, 20, -990], "", 0.0),
1158            nano_row(25, 0.5, [600, 20, -4000], "", 0.0).replacen(",600,", ",1.7e308,", 1),
1159        ]);
1160        assert_eq!(
1161            read(&text).unwrap_err().to_string(),
1162            "AltimeterCloud CSV line 3: the column acceleration_x(mG) holds a value too large \
1163             for its unit's conversion to SI"
1164        );
1165        let text = format!(
1166            "{NANO_HEADER},pressure(hPa)\n{}1013.25\n{}-1.7e308\n{}1012.5\n",
1167            nano_row(0, 0.0, [10, 20, -990], "", 0.0),
1168            nano_row(25, 0.5, [600, 20, -4000], "", 0.0),
1169            nano_row(50, 1.0, [600, 20, -4000], "", 0.0),
1170        );
1171        assert_eq!(
1172            read(&text).unwrap_err().to_string(),
1173            "AltimeterCloud CSV line 3: the column pressure(hPa) holds a value too large for its \
1174             unit's conversion to SI"
1175        );
1176    }
1177
1178    #[test]
1179    fn rows_of_another_width_or_out_of_order_are_refused_by_line() {
1180        let good = nano_row(0, 0.0, [1, 2, 3], "", 0.0);
1181        let short_row = "10,0.0,0.00";
1182        let error = read(&nano(&[good.clone(), short_row.to_owned()])).unwrap_err();
1183        assert!(matches!(error, LogError::Syntax { line: 3, .. }), "{error}");
1184        let wide = format!("{},9", nano_row(10, 0.0, [1, 2, 3], "", 0.0));
1185        let error = read(&nano(&[good.clone(), wide])).unwrap_err();
1186        assert!(matches!(error, LogError::Syntax { line: 3, .. }), "{error}");
1187        // A row without its closing comma is as wide as the header, and reads.
1188        let bare = nano_row(10, 0.0, [1, 2, 3], "", 0.0);
1189        assert!(
1190            read(&nano(&[
1191                good.clone(),
1192                bare.trim_end_matches(',').to_owned()
1193            ]))
1194            .is_ok()
1195        );
1196        let back = nano_row(0, 0.0, [1, 2, 3], "", 0.0);
1197        let error = read(&nano(&[good.clone(), back])).unwrap_err();
1198        assert!(error.to_string().contains("doesn't come after"), "{error}");
1199        let word = nano_row(10, 0.0, [1, 2, 3], "warm", 0.0);
1200        let error = read(&nano(&[good.clone(), word])).unwrap_err();
1201        assert!(error.to_string().contains("isn't a number"), "{error}");
1202        let no_time = format!(",{}", &nano_row(10, 0.0, [1, 2, 3], "", 0.0)[3..]);
1203        let error = read(&nano(&[good, no_time])).unwrap_err();
1204        assert!(error.to_string().contains("no time"), "{error}");
1205        assert!(matches!(
1206            read(&format!("{NANO_HEADER}\n\n")),
1207            Err(LogError::NoData { .. })
1208        ));
1209    }
1210
1211    #[test]
1212    fn a_header_naming_a_column_twice_or_none_is_refused() {
1213        let twice = format!("{NANO_HEADER},TILT\n");
1214        let error = read(&twice).unwrap_err();
1215        assert!(error.to_string().contains("twice"), "{error}");
1216        let empty = NANO_HEADER.replace(",gyro_y,", ",,");
1217        let error = read(&format!("{empty}\n")).unwrap_err();
1218        assert!(error.to_string().contains("has no name"), "{error}");
1219        let no_altitude = NANO_HEADER.replace("Altitude(m)", "Height");
1220        let error = read(&format!("{no_altitude}\n")).unwrap_err();
1221        assert!(error.to_string().contains("no altitude"), "{error}");
1222    }
1223
1224    /// The leading rows before lift-off whose three axes read exactly 0 are gaps; a row at or
1225    /// after lift-off with zeros is kept, as is a row before lift-off with a nonzero axis.
1226    #[test]
1227    fn the_accelerometers_zeros_on_the_pad_are_gaps_and_only_there() {
1228        let text = nano(&[
1229            nano_row(-60, 0.0, [0, 0, 0], "", 0.0),
1230            nano_row(-40, 0.0, [0, 0, 0], "", 0.0),
1231            nano_row(-20, 0.0, [0, 0, -1000], "", 0.0),
1232            nano_row(0, 0.0, [0, 0, 0], "", 0.0),
1233        ]);
1234        let log = read(&text).unwrap();
1235        let total = log
1236            .channels
1237            .iter()
1238            .find(|c| c.column == "acceleration_total(mG)")
1239            .unwrap();
1240        assert!(total.values[0].is_nan() && total.values[1].is_nan());
1241        assert!(total.values[2].abs() < 1e-12);
1242        assert!((total.values[3] + G0_M_S2).abs() < 1e-12);
1243        let gyro = log.channels.iter().find(|c| c.column == "gyro_x").unwrap();
1244        assert!(gyro.values.iter().all(|v| v.is_finite()));
1245        assert!(
1246            note(&log, "the first 2 rows, before lift-off, to line 3,")
1247                .contains("`acceleration_total(mG)` are gaps")
1248        );
1249
1250        // Zeros only from lift-off on: nothing is a gap.
1251        let text = nano(&[
1252            nano_row(0, 0.0, [0, 0, 0], "", 0.0),
1253            nano_row(20, 0.0, [0, 0, 0], "", 0.0),
1254        ]);
1255        let log = read(&text).unwrap();
1256        assert!(
1257            log.channels
1258                .iter()
1259                .all(|c| c.values.iter().all(|v| !v.is_nan()))
1260        );
1261        assert!(!has_note(&log, "the first"));
1262    }
1263
1264    /// A closing run of exact zeros after a warm reading is a gap; after a reading near 0 °C it
1265    /// is kept.
1266    #[test]
1267    fn closing_zero_temperatures_are_gaps_only_after_a_jump() {
1268        let rows = |before: &str| {
1269            nano(&[
1270                nano_row(0, 0.0, [1, 2, 3], "30.00", 0.0),
1271                nano_row(20, 0.0, [1, 2, 3], before, 0.0),
1272                nano_row(40, 0.0, [1, 2, 3], "0.00", 0.0),
1273                nano_row(60, 0.0, [1, 2, 3], "", 0.0),
1274                nano_row(80, 0.0, [1, 2, 3], "0.00", 0.0),
1275            ])
1276        };
1277        let log = read(&rows("29.50")).unwrap();
1278        let track = log.temperature_k.as_deref().unwrap();
1279        assert!((track[1] - 302.65).abs() < 1e-9);
1280        assert!(track[2].is_nan() && track[4].is_nan());
1281        assert!(
1282            note(&log, "the last 2 readings of `Board temp(C)`, from line 4,")
1283                .contains("29.50 °C on line 3")
1284        );
1285
1286        let log = read(&rows("4.00")).unwrap();
1287        let track = log.temperature_k.as_deref().unwrap();
1288        assert!((track[2] - 273.15).abs() < 1e-9 && (track[4] - 273.15).abs() < 1e-9);
1289        assert!(!has_note(&log, "the last"));
1290    }
1291
1292    fn mercury(temperatures: [&str; 3]) -> String {
1293        let rows: Vec<String> = temperatures
1294            .iter()
1295            .enumerate()
1296            .map(|(row, temperature)| {
1297                format!(
1298                    "{},0.000,0.00,1,2,3,-996.3,0,0,0,{temperature},0.00,0.00,0.00,0.00,,0.00,0,,12000,",
1299                    row * 20
1300                )
1301            })
1302            .collect();
1303        format!("{MERCURY_HEADER}\n{}\n", rows.join("\n"))
1304    }
1305
1306    /// Hundredths of a degree when the values bear it out; kept as written when they could be
1307    /// degrees, aren't whole, or are too hot even divided by 100.
1308    #[test]
1309    fn the_centi_degree_temperature_is_read_only_when_the_values_say_so() {
1310        let log = read(&mercury(["2000", "", "2150"])).unwrap();
1311        assert_eq!(log.logger, "Rocketry Ltd Mercury");
1312        let channel = log
1313            .channels
1314            .iter()
1315            .find(|c| c.column == "bmp_temp(x100)")
1316            .unwrap();
1317        assert_eq!(channel.quantity, Quantity::Temperature);
1318        assert_eq!(channel.time_s, [0.0, 0.04]);
1319        assert!((channel.values[1] - 294.65).abs() < 1e-9);
1320        note(
1321            &log,
1322            "`bmp_temp(x100)` read in hundredths of a degree Celsius, from its name and the values: ",
1323        );
1324        assert!(log.temperature_k.is_some());
1325
1326        for (cells, why) in [
1327            (["20", "21", "22"], "as written too"),
1328            (["2000.5", "2001", "2002"], "aren't all whole numbers"),
1329            (["9000", "9100", "9200"], "don't all lie within"),
1330        ] {
1331            let log = read(&mercury(cells)).unwrap();
1332            let channel = log
1333                .channels
1334                .iter()
1335                .find(|c| c.column == "bmp_temp(x100)")
1336                .unwrap();
1337            assert_eq!(channel.quantity, Quantity::Unrecognized, "{cells:?}");
1338            assert_eq!(channel.values[0], cells[0].parse::<f64>().unwrap());
1339            assert!(
1340                note(&log, "`bmp_temp(x100)` kept as written, its unit unknown: ").contains(why),
1341                "{cells:?}"
1342            );
1343            assert!(log.temperature_k.is_none());
1344        }
1345    }
1346
1347    #[test]
1348    fn the_mercurys_own_columns_are_kept_as_written() {
1349        let log = read(&mercury(["2000", "2000", "2000"])).unwrap();
1350        // The header's closing comma isn't a column.
1351        assert_eq!(log.channels.len(), 19);
1352        let quantity = |name: &str| {
1353            log.channels
1354                .iter()
1355                .find(|c| c.column == name)
1356                .map(|c| (c.quantity, c.values.len()))
1357        };
1358        assert_eq!(quantity("mt1_temp"), Some((Quantity::Unrecognized, 0)));
1359        assert_eq!(
1360            quantity("apogee_prediction"),
1361            Some((Quantity::Unrecognized, 3))
1362        );
1363        assert_eq!(quantity("air_brake_percent"), Some((Quantity::Number, 3)));
1364        assert_eq!(quantity("target_alt"), Some((Quantity::Unrecognized, 0)));
1365        assert_eq!(quantity("air_density"), Some((Quantity::Unrecognized, 3)));
1366        assert_eq!(
1367            quantity("velocity_pressure(m/s)"),
1368            Some((Quantity::Speed, 3))
1369        );
1370        for name in ["mt1_temp", "apogee_prediction", "target_alt", "air_density"] {
1371            note(&log, &format!("`{name}` kept as written: "));
1372        }
1373        note(&log, "`air_brake_percent` read in percent, from its name: ");
1374        assert!(
1375            note(
1376                &log,
1377                "`gyro_x`, `gyro_y` and `gyro_z` read in degrees per second, from "
1378            )
1379            .contains("whose layout has the same columns")
1380        );
1381    }
1382
1383    #[test]
1384    fn an_unknown_column_is_kept_and_its_notes_stay_few() {
1385        let names: Vec<String> = (0..30).map(|i| format!("extra_{i}")).collect();
1386        let header = format!("{NANO_HEADER},{}", names.join(","));
1387        let row = format!(
1388            "{}{}",
1389            nano_row(0, 0.0, [1, 2, 3], "", 0.0),
1390            vec!["7"; 30].join(",")
1391        );
1392        let log = read(&format!("{header}\n{row}\n")).unwrap();
1393        assert_eq!(log.channels.len(), 44);
1394        let extra = log.channels.iter().find(|c| c.column == "extra_0").unwrap();
1395        assert_eq!(extra.quantity, Quantity::Unrecognized);
1396        assert_eq!(extra.values, [7.0]);
1397        let kept = log
1398            .notes
1399            .iter()
1400            .filter(|n| n.contains("this reader doesn't know the column"))
1401            .count();
1402        assert_eq!(kept, 20);
1403        assert!(has_note(&log, "and 10 more notes"));
1404    }
1405
1406    #[test]
1407    fn a_yaw_at_the_end_of_its_range_is_noted() {
1408        let text = nano(&[
1409            nano_row(0, 0.0, [1, 2, 3], "", 300.0),
1410            nano_row(20, 0.0, [1, 2, 3], "", -327.68),
1411        ]);
1412        let log = read(&text).unwrap();
1413        note(
1414            &log,
1415            "`yaw` reaches ±327.67° or beyond on 1 rows, the first on line 3",
1416        );
1417        let text = nano(&[nano_row(0, 0.0, [1, 2, 3], "", 327.66)]);
1418        assert!(!has_note(&read(&text).unwrap(), "`yaw` reaches"));
1419    }
1420
1421    fn with_summary(summary: &str) -> String {
1422        format!(
1423            "{summary}\n\n{}",
1424            nano(&[
1425                nano_row(-20, 0.0, [1, 2, 3], "", 0.0),
1426                nano_row(0, 0.0, [1, 2, 3], "", 0.0),
1427                nano_row(5_000, 100.0, [1, 2, 3], "", 0.0),
1428                nano_row(5_100, 101.0, [1, 2, 3], "", 0.0),
1429                nano_row(5_200, 100.5, [1, 2, 3], "", 0.0),
1430                nano_row(30_000, 0.0, [1, 2, 3], "", 0.0),
1431            ])
1432        )
1433    }
1434
1435    /// The apogee time and the landing time in seconds when the altitude bears them out; left
1436    /// out when it doesn't.
1437    #[test]
1438    fn the_summary_gives_the_apogee_the_serial_and_the_events() {
1439        let summary = "Apogee meters,101.00,,Landing meters,0.0,Burnout time (ms),1200,\n\
1440                       Apogee time,5.150,,Landing time,30.000,\n\
1441                       Serial number,0000-0000-0001,,Ejection time (ms),5300,\n\
1442                       Firmware, 1.00,,Adjusted log,no,\n\
1443                       Device tag,Unit A,,Competitor tag,,";
1444        let log = read(&with_summary(summary)).unwrap();
1445        assert_eq!(log.stated.apogee_m, Some(101.0));
1446        assert_eq!(log.serial_number.as_deref(), Some("0000-0000-0001"));
1447        assert_eq!(log.firmware.as_deref(), Some("1.00"));
1448        let events: Vec<(f64, &str)> = log
1449            .events
1450            .iter()
1451            .map(|e| (e.time_s, e.name.as_str()))
1452            .collect();
1453        assert_eq!(
1454            events,
1455            [
1456                (1.2, "Burnout"),
1457                (5.15, "Apogee"),
1458                (5.3, "Ejection"),
1459                (30.0, "Landing")
1460            ]
1461        );
1462        assert!(
1463            note(&log, "`Apogee time` read in seconds, from the altitude: ").contains("at 5.1 s")
1464        );
1465        assert!(
1466            note(
1467                &log,
1468                "`Landing time` read in seconds, from the altitude: at 30 s, "
1469            )
1470            .contains(
1471                "the altitude is 0.000 m. That is within 0.01 m of the summary's `Landing meters`, 0 m"
1472            )
1473        );
1474        assert!(
1475            note(
1476                &log,
1477                "the summary's other 3 entries, such as `Landing meters`"
1478            )
1479            .contains("left out")
1480        );
1481        assert!(!has_note(&log, "the summary's `Adjusted log`"));
1482
1483        // An apogee time a thousand times off is left out, and a landing time with no landing
1484        // height to check it against.
1485        let summary = "Apogee time,5150,,Landing time,30000,\nAdjusted log,yes,";
1486        let log = read(&with_summary(summary)).unwrap();
1487        assert!(log.events.is_empty());
1488        assert!(note(&log, "the summary's `Apogee time`, 5150, is left out").contains("5.1 s"));
1489        assert!(
1490            note(&log, "the summary's `Landing time`, 30000, is left out: ")
1491                .contains("gives no landing height")
1492        );
1493        note(&log, "the summary's `Adjusted log` is \"yes\"");
1494        assert_eq!(log.stated.apogee_m, None);
1495        let log = read(&with_summary("Apogee meters,high,")).unwrap();
1496        note(
1497            &log,
1498            "the summary's `Apogee meters` on line 1, \"high\", isn't a number",
1499        );
1500    }
1501
1502    /// The landing time in seconds only where the altitude, after the highest, is the stated
1503    /// landing height, and in milliseconds it isn't; between rows, on the line joining them.
1504    #[test]
1505    fn the_landing_time_is_read_in_seconds_only_where_the_altitude_agrees() {
1506        let landing = |height: &str, time: &str| {
1507            let summary = format!("Landing meters,{height},,Landing time,{time},");
1508            read(&with_summary(&summary)).unwrap()
1509        };
1510        let events = |log: &FlightLog| -> Vec<(f64, String)> {
1511            log.events
1512                .iter()
1513                .map(|e| (e.time_s, e.name.clone()))
1514                .collect()
1515        };
1516        // Between the rows at 5.2 s, 100.5 m, and 30 s, 0 m: 4.052 m at 29 s.
1517        let log = landing("4.05", "29.000");
1518        assert_eq!(events(&log), [(29.0, "Landing".to_owned())]);
1519        assert!(
1520            note(
1521                &log,
1522                "`Landing time` read in seconds, from the altitude: at 29 s, "
1523            )
1524            .contains("the altitude is 4.052 m")
1525        );
1526        // A landing height 2 cm off, or a time the log doesn't reach, or one before the highest
1527        // altitude, is left out.
1528        for (height, time, written, why) in [
1529            (
1530                "4.07",
1531                "29.000",
1532                "29",
1533                "the altitude at 29 s is 4.052 m, not within 0.01 m",
1534            ),
1535            ("0.0", "31.000", "31", "the log gives no altitude at 31 s"),
1536            (
1537                "50.0",
1538                "2.600",
1539                "2.6",
1540                "2.6 s comes before the highest altitude, at 5.1 s",
1541            ),
1542        ] {
1543            let log = landing(height, time);
1544            assert!(events(&log).is_empty(), "{time}");
1545            let start = format!("the summary's `Landing time`, {written}, is left out: ");
1546            assert!(note(&log, &start).contains(why), "{time}");
1547            assert!(!has_note(&log, "`Landing time` read in"), "{time}");
1548        }
1549        // A log whose altitude peaks at once, so that read in milliseconds too the landing time
1550        // comes after the highest altitude where the altitude is the landing height: left out.
1551        let text = format!(
1552            "Landing meters,0.0,,Landing time,20.000,\n\n{}",
1553            nano(&[
1554                nano_row(0, 5.0, [1, 2, 3], "", 0.0),
1555                nano_row(10, 0.0, [1, 2, 3], "", 0.0),
1556                nano_row(20_000, 0.0, [1, 2, 3], "", 0.0),
1557            ])
1558        );
1559        let log = read(&text).unwrap();
1560        assert!(log.events.is_empty());
1561        assert!(
1562            note(&log, "the summary's `Landing time`, 20, is left out: ")
1563                .contains("in seconds or in milliseconds alike")
1564        );
1565    }
1566
1567    #[test]
1568    fn the_altitude_at_a_time_is_a_rows_or_on_the_line_between_two() {
1569        let time = [0.0, 1.0, 3.0, 4.0];
1570        let altitude = [0.0, 10.0, 30.0, f64::NAN];
1571        assert_eq!(altitude_at(&time, &altitude, 1.0), Some(10.0));
1572        assert_eq!(altitude_at(&time, &altitude, 2.5), Some(25.0));
1573        assert_eq!(altitude_at(&time, &altitude, 0.0), Some(0.0));
1574        assert_eq!(altitude_at(&time, &altitude, -0.5), None);
1575        assert_eq!(altitude_at(&time, &altitude, 3.5), None);
1576        assert_eq!(altitude_at(&time, &altitude, 5.0), None);
1577    }
1578
1579    /// A long line of commas, above a header or as the whole file, is read without collecting
1580    /// its cells.
1581    #[test]
1582    fn a_long_line_of_commas_is_no_header_and_no_summary() {
1583        let commas = ",".repeat(1 << 20);
1584        assert!(!recognizes(&commas));
1585        assert!(!recognizes(&format!("Time(ms){commas}")));
1586        let log = read(&format!(
1587            "{commas}\n\n{}",
1588            nano(&[nano_row(0, 0.0, [1, 2, 3], "", 0.0)])
1589        ))
1590        .unwrap();
1591        assert!(!has_note(&log, "the summary's other"));
1592        // A header with an empty name in the middle is refused; a closing one is dropped.
1593        let error = read(&format!("{NANO_HEADER}{commas}x\n")).unwrap_err();
1594        assert!(error.to_string().contains("has no name"), "{error}");
1595    }
1596
1597    /// The total acceleration's note says it is the magnitude less 1 g.
1598    #[test]
1599    fn the_total_acceleration_is_noted_as_the_magnitude_less_one_g() {
1600        let log = read(&nano(&[nano_row(0, 0.0, [1, 2, 3], "", 0.0)])).unwrap();
1601        assert!(
1602            note(
1603                &log,
1604                "`acceleration_total(mG)` read in thousandths of standard gravity, from its name \
1605                 and the Nano V1 manual: "
1606            )
1607            .contains("the magnitude less 1 g, not an acceleration along any axis")
1608        );
1609    }
1610
1611    /// Every note on a unit opens as `cargo xtask logs` counts a unit decision: a column in
1612    /// backticks, then `read in` or `kept as written`, then a colon.
1613    #[test]
1614    fn unit_notes_open_as_the_survey_counts_them() {
1615        let log = read(&mercury(["2000", "2010", "2020"])).unwrap();
1616        for note in log.notes.iter().filter(|n| n.starts_with('`')) {
1617            let (decision, _) = note.split_once(": ").unwrap();
1618            assert!(
1619                decision.contains("` read in ") || decision.contains("` kept as written"),
1620                "{note}"
1621            );
1622        }
1623    }
1624}