Skip to main content

hpr_flightdata/
aerospacenu_fcb.rs

1//! AerospaceNU's flight computer board's CSV: the club's table of a flight, one row per packet
2//! the board logged, under a header whose first name is empty.
3//!
4//! The flight computer board (FCB) is a flight computer of Northeastern University's rocketry
5//! club, AerospaceNU, with two STMicroelectronics LSM9DS1 inertial units (each an accelerometer,
6//! a gyroscope and a magnetometer), a high-g accelerometer, one or two barometers and a GPS. The
7//! club publishes its flights' logs, each board's binary log beside a CSV its tools made from it
8//! (AerospaceNU, *rocket-flight-data*, `README.md`, read 2026-10-12). This reader reads that CSV.
9//! No published document describes the columns or their units, so each unit here comes from the
10//! column's name or from two checks the reader runs on the file's values that agree, as below;
11//! where they don't, the column is kept as written. 20 such files are among the project's sample
12//! logs, under eight headers of 73 to 87 columns: the oldest open with `timestamp_s, timestamp_ms`,
13//! the later ones with `packetType`, then one clock column or both; the 73-column layout, the
14//! board's second version (FCBV2), has one IMU and one barometer and adds servo columns. An
15//! invented row of the 87-column layout, cut short:
16//!
17//! ```text
18//! ,packetType,timestamp_ms,imu1_accel_x,imu1_accel_y,imu1_accel_z,imu1_gyro_x,…
19//! 1000,0,600000,0,0,1366,0,0,0,-310,1220,1405,2,2,1368,…
20//! ```
21//!
22//! **Recognition.** The first line that isn't blank is a header whose first name is empty, the
23//! club's row-number column, and which names `packetType`, `timestamp_ms` or `timestamp_s`.
24//! Names and cells are split at commas.
25//!
26//! **Columns.** Each name the reader knows, what it is read as, and where its unit comes from;
27//! every column but the clock is kept as a channel on the clock, under the file's name. A column
28//! the reader doesn't know is kept as written, [`Quantity::Unrecognized`], with a note (the first
29//! 20 such notes kept, the rest counted); a header naming a column twice, with an empty name past
30//! the first, or with more than [`MAX_COLUMNS`] names is refused:
31//!
32//! | Column | Quantity | Unit, and from where |
33//! |---|---|---|
34//! | the first, unnamed | [`Quantity::Number`] | the club's row number, as written |
35//! | `timestamp_ms`, `timestamp_s` | the clock | milliseconds or seconds, from its spacing and the climb (below), whatever its name says; the other, if it holds only 0, is kept as written |
36//! | `baro1_pres`, `baro2_pres`, `baro_pres_avg` | [`Quantity::Pressure`] | atmospheres, × 101,325 Pa, from the size and the step (below) |
37//! | `imu1_accel_*_real`, `imu2_accel_*_real` | [`Quantity::Acceleration`] | m/s² or standard gravities, from the pad's gravity and the sensor's scale (below) |
38//! | `imu_accel_*_avg` | [`Quantity::Acceleration`] | the IMUs' unit, from the pad's gravity and the mean (below) |
39//! | `high_g_accel_*_real` | [`Quantity::Acceleration`] | as the IMUs' scaled columns (below); kept as written in every sample file |
40//! | `acc_x`, `acc_y`, `acc_z` | [`Quantity::Acceleration`] | m/s² or standard gravities, from the pad's gravity and the climb (below): the board's own estimate, in axes the file doesn't state |
41//! | `imu1_gyro_*_real`, `imu2_gyro_*_real` | [`Quantity::AngularRate`] | radians or degrees per second, from the board's turning and the sensor's scale (below) |
42//! | `imu_gyro_*_avg` | [`Quantity::AngularRate`] | the IMUs' unit, from the turning and the mean (below) |
43//! | `pos_z` | [`Quantity::AltitudeAboveGround`]: the primary track | meters or feet, from the pressure's climb and the accelerometer's (below) |
44//! | `baro_height`, `baroalt_agl`, `baro1_alt` | [`Quantity::AltitudeAboveGround`] | as `pos_z` |
45//! | `gps_alt` | [`Quantity::GnssAltitude`] | meters or feet, from the pressure's climb and the accelerometer's at the GPS's fresh fixes (below); a 0, the receiver's mark for no fix, is a gap |
46//! | `vel_z` | [`Quantity::Speed`], up positive: [`FlightLog::vertical_speed_m_s`] | m/s or ft/s, from `pos_z` and the accelerometer (below) |
47//! | `gps_lat`, `gps_long` | [`Quantity::Latitude`], [`Quantity::Longitude`] | NMEA 0183's degrees and minutes or degrees, from the latitudes' and the longitudes' ranges, where the file writes a negative value to show its hemisphere (below); a position (0, 0), no fix, is a gap |
48//! | `gps_lat_mod`, `gps_long_mod` | [`Quantity::Latitude`], [`Quantity::Longitude`] | degrees, where they are `gps_lat` and `gps_long`, once decided, in degrees (below) |
49//! | `dynamixel_degrees`, `dynamixel_degrees_1` | [`Quantity::Angle`] | degrees, from their names: a servo's angle |
50//! | `packetType`, `state`, `pyro_cont`, `pyro_status`, `trigger_status`, `gps_fix_quality`, `gps_sats_tracked`, `gps_hdop`, `q_x` … `q_w` | [`Quantity::Number`] | codes, flags, counts, a dilution of precision and an attitude quaternion's parts, as written: no published source gives the codes' meanings |
51//! | `imu1_accel_*`, `imu1_gyro_*`, `imu1_mag_*`, the same of `imu2`, `high_g_accel_*` | [`Quantity::Unrecognized`] | a sensor's raw counts, kept as written: the `_real` columns hold them scaled |
52//! | `imu1_mag_*_real`, `imu2_mag_*_real`, `imu_mag_*_avg` | [`Quantity::Unrecognized`] | kept as written: a magnetic field, a quantity HPR Sim doesn't hold |
53//! | `baro1_temp`, `baro2_temp`, `baro_temp_avg` | [`Quantity::Unrecognized`] | kept as written: the names state no unit, and degrees Celsius and Fahrenheit both put the values within a barometer's working −40 to 85 °C; no other column holds a temperature to tell them apart |
54//! | `battery_voltage`, `battery_voltage_1`, `battery_voltage_2`, `dynamixel_length`, `dynamixel_length_1` | [`Quantity::Unrecognized`] | kept as written: the names state no unit and nothing in the file checks one |
55//! | `heading`, `vtg`, `pos_x`, `pos_y`, `vel_x`, `vel_y` | [`Quantity::Unrecognized`] | kept as written: the names state no unit, and in the sample files they hold only 0 but for one file's `vel_x` |
56//!
57//! Each unit a check decides gets a note saying on what evidence, and each column kept as written
58//! a note saying why. Every decision rests on two checks that must agree and that the weather or
59//! the flight can't meet in the wrong unit; a check without enough data to tell decides nothing.
60//! Most checks set a column against a physical mark (1 g at rest, a barometer's whole pascals, a
61//! sensor's published sensitivity, the board's turning) or against a column whose unit is already
62//! decided (the pressure, an accelerometer). Some set a column against its siblings: the IMUs'
63//! averages are checked against the IMUs' scaled columns, `vel_z` against `pos_z`, and the GPS's
64//! `_mod` companions against `gps_lat` and `gps_long`; each such check counts only once the
65//! sibling is read in a unit by its own two checks, so no counted check relates two columns that
66//! share one unknown unit. The checks, with what they found in the 20 sample files:
67//!
68//! - *The pressures' size and step.* A pad lies 0.5 to 1.1 from sea level's pressure in
69//!   atmospheres or bars, and far from it in pascals, hectopascals, kilopascals, psi or inches or
70//!   millimeters of mercury: the first check is the median of the first 10 rows' values. The
71//!   board's barometers read whole pascals, and the club divides them by 101,325; so the second
72//!   check is that each cell lies within a millionth of a step of a whole number of steps of
73//!   1/101,325, or of 1/202,650 for an average of two barometers, for every cell written to
74//!   enough decimals that its printing, at most half a unit in its last place, takes under half
75//!   that tolerance (at least 10 cells). In bars, a whole number of pascals over 100,000, the cells
76//!   miss the steps by up to half a step. A pressure on both grids or on neither is kept as
77//!   written. In the sample files 1,517 to 21,291 cells test per column and every barometer that
78//!   reads is in atmospheres; `baro_pres_avg` takes half steps where it averages two. One
79//!   barometer reads 0 throughout and is kept as written.
80//! - *The clock's spacing and the climb.* The board logs a row every 15 or 16 ms in flight and
81//!   about every 100 ms on the pad; the first check is that the rows' median step, in the unit,
82//!   lies within 5 to 500 ms. The second integrates the first IMU's raw accelerometer (else the
83//!   second's), in its pad reading's 1 g so that its counts' unit divides out, along the pad
84//!   reading's direction less gravity, from the launch (the first row of 3 g, the next two at 2
85//!   g) to the pressure's apogee in the U.S. Standard Atmosphere, 1976 ([`hpr_atmos::ussa76`]);
86//!   the median, over the rows past 30 m, of the pressure's climb over the integrated one must lie
87//!   within a factor of 10 of 1. Seconds read as milliseconds, or the reverse, put it 10⁶ off.
88//!   The unit both checks bear out is taken; with none, or two, the file is refused
89//!   ([`LogError::Unit`]): nothing in it has a time. Either clock column can hold either unit: in
90//!   six sample files `timestamp_s` holds milliseconds, in two seconds, and in the other twelve
91//!   `timestamp_ms` holds milliseconds; the ratio is 0.57 to 1.15 in milliseconds, and 0.83 and
92//!   3.26 in the two in seconds.
93//! - *The pad's gravity and the sensor's scale.* A board at rest reads 1 g: the magnitude of the
94//!   three axes' pad readings within 10% of 9.806 65 in m/s² or of 1 in standard gravities is the
95//!   first check. The second: each scaled column is its raw counts times one factor (within a
96//!   millionth), and that factor is, in the unit, one of the LSM9DS1's accelerometer
97//!   sensitivities, 0.061, 0.122, 0.244 or 0.732 mg a count (*LSM9DS1*, STMicroelectronics,
98//!   DocID025715 Rev 3, 2015, Table 3, `LA_So`). The reader never rescales by a factor it
99//!   infers. In the sample files the factor is 0.732 mg in m/s² throughout, and the IMUs read
100//!   9.11 to 9.97 at rest, but for the FCBV2's, which reads 14.7 (1.5 g: its counts are another
101//!   part's), four whose second IMU reads 0.27 to 0.54, and one second IMU that reads 0; these
102//!   are kept as written. The high-g accelerometer's factor, 0.029 42, is none of the LSM9DS1's,
103//!   so it is kept as written in every file.
104//! - *The IMUs' average.* `imu_accel_*_avg` must read 1 g at rest in the unit of the IMUs' scaled
105//!   columns, all of which must be read in it, and be their mean at every row, within a
106//!   millionth. Where an IMU is kept as written, so is the average; in the sample files, where
107//!   one IMU reads near 0, the average reads half of 1 g.
108//! - *The board's estimate `acc_*`.* Its pad reading must be 1 g within 10%, and the pressure's
109//!   climb over the one it integrates to, as the clock's check but read in the unit, must lie
110//!   within a factor of 1.8 of 1 in that unit and in no other; read in the wrong one, the
111//!   specific force is 9.8 times too large or too small against the gravity taken off it. In the
112//!   sample files it reads 9.11 to 9.97 at rest and the ratio is 0.74 to 1.10, in m/s².
113//! - *The board's turning and the gyroscope's scale.* As [`crate::sillygoose`] makes the turning
114//!   check: over stretches of 10 steps at most 0.05 s apart with the raw accelerometer within 15%
115//!   of 1 g, the angle its direction turns against the gyroscope's turn across that direction,
116//!   |ω × â| dt; the unit, radians or degrees per second, whose turn lies within a quarter to 4
117//!   times the accelerometer's, from at least 3 stretches and 0.2 rad. The second check: the
118//!   scaled columns are their raw counts times one factor, one of the LSM9DS1's gyroscope
119//!   sensitivities, 8.75, 17.5 or 70 m°/s a count (Table 3, `G_So`), in the unit. In the sample
120//!   files the factor is 17.5 or 70 m°/s in rad/s, and the turning ratio 0.54 to 2.34 where it is
121//!   read; one file's is 6.6 and 8.4, and two have no stretches to check, so those are kept as
122//!   written. `imu_gyro_*_avg` must bear the IMUs' unit out by the turning and be their mean.
123//! - *The climbs: `pos_z`, `baro_height`, `baroalt_agl`, `baro1_alt`.* First, the column's climb
124//!   from its pad reading to its highest (its 5-row running median) over the pressure's, which
125//!   must be at least 30 m, must lie within 25% of 1 in meters or in feet. Second, its climb over
126//!   the one the accelerometer integrates to, as above, with the first of `acc_*`, the IMUs'
127//!   scaled columns and their average read in a unit, must lie within a factor of 1.8 of 1:
128//!   1.8² = 3.24 is under the 3.28 between meters and feet. The pressure and the accelerometer
129//!   are decided by their own checks above, so neither shares the column's unknown unit. In the
130//!   sample files `pos_z` is read in meters in every file, its gain 0.90 to 1.21 and its ratio 0.82
131//!   to 1.11.
132//! - *`gps_alt`.* A receiver may write 0, or hold an old value, until its first fix, so a pad
133//!   reading taken from the first rows can be no altitude at all: subtracted from the climb, a
134//!   pad of 0 at a site 1,000 m up would inflate both checks above alike. So the GPS's checks use
135//!   only its fresh fixes, the rows past the first where it holds a number other than 0 that
136//!   differs from the row before's, and each fits its own line with a free offset, so that no pad
137//!   reading enters either. First, the line's slope on the pressure's climb, at the fresh fixes up
138//!   to the apogee, at least 3 over at least 30 m of climb, must lie within 25% of 1 in meters or
139//!   in feet. Second, its slope on the climb the accelerometer integrates to, as above, at the
140//!   fresh fixes from the launch, at least 5 over at least 30 m, must lie within a factor of 1.8.
141//!   Where the first rows hold a number other than 0, their median, the pad reading, must lie
142//!   within a quarter of the GPS's rise over the fixes of where the first line puts the pad: a
143//!   value further off is one held from before the first fix, and the column is kept as written,
144//!   as a pad that far off would move a gain taken through it by more than its whole 25% margin.
145//!   Where the column is read, each 0 is a gap, counted in a note. In the sample files every GPS
146//!   altitude but one that reads 0 throughout holds a number other than 0 from the first row;
147//!   `gps_alt` is read in meters in 12, its gains 0.91 to 1.10 and
148//!   its ratios 0.86 to 1.39, its pad reading within 6% of its rise of where the line puts the
149//!   pad, and kept as written in 8, where it reads 0, has too few fresh fixes, or climbs far short
150//!   of the pressure or the accelerometer.
151//! - *`vel_z`.* First, over the steps at most 0.05 s long, the least-squares slope of `pos_z`'s
152//!   change, in meters, on `vel_z` times the step must lie within 25% of 1 in m/s or of 0.3048 in
153//!   ft/s, from at least 10 m of movement. Second, the median, over the rows past 10 m/s, of it
154//!   over the speed the accelerometer integrates to must lie within a factor of 1.8. In the sample
155//!   files the slope is 0.92 to 1.00 and the ratio 0.80 to 1.24 in m/s; one file's `vel_z` reads 0
156//!   throughout and one's slope is 0.59, kept as written.
157//! - *The GPS position.* NMEA 0183's degrees and minutes write a latitude of 42.5° as 4230.0, so
158//!   they put every latitude more than 1° from the equator past 90 and every longitude more than
159//!   2° from the prime meridian past 180, which no degrees reach. The checks take only the fixes:
160//!   a position (0, 0) is the receiver's mark for none, and where the position is read it is a gap,
161//!   counted in a note. Degrees and minutes: most latitudes pass ±90 and, a second check on
162//!   another measurement, most longitudes pass ±180; every value's last two digits before the
163//!   point, its minutes, are under 60; and converted, every latitude lies within ±90 and every
164//!   longitude within ±180. "Most" keeps one corrupt fix from deciding a file. Degrees: every
165//!   latitude lies within ±90 and, the second check, every longitude within ±180, which in
166//!   degrees and minutes would put the place within 1° of the equator and 2° of the prime meridian
167//!   at once, at sea in the Gulf of Guinea. Otherwise both are kept as written.
168//!   `gps_lat_mod` and `gps_long_mod` count in neither decision: they are read in degrees only
169//!   where `gps_lat` and `gps_long` are read and, at every fix, they hold them in degrees within a
170//!   millionth (converted from degrees and minutes, or as written), and are kept as written
171//!   otherwise. In the sample files 15 positions are read in degrees and minutes, every fix past
172//!   ±90 and ±180, and their companions in degrees; 4 in degrees, every fix within, whose
173//!   companions in 3 hold the values converted as if they were degrees and minutes, and are kept,
174//!   and in 1 copy them, and are read; in one the GPS reads 0 throughout.
175//! - *The GPS hemisphere.* NMEA 0183 writes the hemisphere in a field of its own beside each
176//!   coordinate (N or S, E or W); the board's columns have no such field, so the sign is all
177//!   that can carry it. A file that writes a negative value in `gps_lat` or `gps_long` shows its
178//!   signs, and the position is read by the checks above, each coordinate with its sign. Where
179//!   every value in both is 0 or more, a place south of the equator or west of the prime
180//!   meridian can't be told from its mirror north or east, so both, and their companions, are
181//!   kept as written, with a note giving how the checks above would read them, so that a person
182//!   who knows the site can sign them; no hemisphere is guessed from anything else. In the
183//!   sample files every one of the 19 with a fix writes its longitudes negative, west, and its
184//!   latitudes positive, and is read; none is kept for want of a hemisphere.
185//!
186//! **The primary track.** The record's altitude is `pos_z`, the board's filtered altitude above
187//! the pad, where it is read in a unit; otherwise the track is empty and a note says so, as no
188//! other column is the board's altitude above the pad in a decided unit. The vertical speed is
189//! `vel_z` where it is read. The track takes the rows in time order, the first at each time,
190//! leaving out a row without a time; a note counts those put in order or left out, and the
191//! channels keep every row in the file's order. Where `pos_z` reads more than 100 m off zero at
192//! the track's first row, a note says so. No temperature or battery voltage is the record's, as
193//! none is read in a unit. **Events:** none. `state` is the board's state code, a
194//! [`Quantity::Number`]; its codes' meanings aren't published, so no event is drawn from it.
195//!
196//! **Cells.** An empty cell, or one that isn't a number, is a gap (`NaN`), counted in a note. A
197//! value too large to hold once converted to its unit, such as an acceleration of 1.7 × 10³⁰⁸ in
198//! standard gravities or of 1.7 × 10³⁰⁸ atmospheres, is refused, naming its line. A row with
199//! fewer cells than the header names has the rest as gaps, counted in a note; a row with more is
200//! refused, naming its line, as is a file whose rows hold under a quarter of the cells the header
201//! names (past the first 65,536), so that a short file can't make the reader store far more gaps
202//! than it writes cells. Lines may end in CR, LF or both, blank lines are skipped, and a
203//! byte-order mark is dropped.
204
205use std::collections::BTreeSet;
206use std::f64::consts::PI;
207
208use hpr_atmos::ussa76::Ussa76;
209use hpr_core::gravity::STANDARD_GRAVITY_MPS2;
210
211use crate::error::LogError;
212use crate::log::{Channel, FlightLog, LogFormat, Quantity, Stated};
213use crate::table::{self, Notes, number, numbered_lines, short};
214
215/// The format's name in errors.
216const FORMAT: &str = "AerospaceNU flight computer CSV";
217
218/// The logger's name: the files don't name the board or its version.
219const LOGGER: &str = "AerospaceNU flight computer board";
220
221/// The barometric altitude's step, m ([`crate::log::LogFormat::altitude_resolution_m`]): the
222/// track is the board's altitude estimate `pos_z`, written to 15 or more digits, but the
223/// barometer it rests on reads whole pascals (its pressure is a whole number of pascals over
224/// 101,325, below), and one pascal is `1 / (ρ₀ g₀)` = 0.083 m of altitude near sea level, with the
225/// International Standard Atmosphere's ρ₀ = 1.225 kg/m³ (ISO 2533:1975) and g₀ = 9.806 65 m/s².
226pub const ALTITUDE_STEP_M: f64 = 1.0 / (1.225 * STANDARD_GRAVITY_MPS2);
227
228/// The most names a header may have: the board's files have 73 to 87.
229pub const MAX_COLUMNS: usize = 256;
230
231/// The rows at the file's start that are the pad: the median of each column over them is its
232/// reading at rest. Each of the 20 sample files holds at least 32 rows before the launch.
233const PAD_ROWS: usize = 10;
234
235/// Pascals in a standard atmosphere and in a bar.
236const ATM_PA: f64 = 101_325.0;
237const BAR_PA: f64 = 100_000.0;
238
239/// The pressures a pad can read in atmospheres or bars: 0.5 to 1.1, a pad from 5,500 m up to below
240/// sea level in a high. Pascals, hectopascals, kilopascals, psi and inches or millimeters of
241/// mercury put a pad far from 1.
242const PAD_PRESSURE: (f64, f64) = (0.5, 1.1);
243
244/// How far, in steps, a cell may lie from a whole number of a pressure's steps.
245const STEP_TOLERANCE: f64 = 1e-6;
246
247/// The fewest cells that must test the step for it to decide.
248const LEAST_STEP_CELLS: usize = 10;
249
250/// The board's logging, as a step between rows, s: 2 to 200 rows a second. The board logs a row
251/// every 15 or 16 ms in flight and every 100 ms or so on the pad.
252const ROW_STEP_S: (f64, f64) = (0.005, 0.5);
253
254/// How many times off the climb check may find the clock's climb and still bear its unit out:
255/// milliseconds and seconds put it a million times apart.
256const CLOCK_FACTOR: f64 = 10.0;
257
258/// The specific force, in its pad reading, from which a row is the launch: 3 g, held for the next
259/// two rows at 2 g.
260const LAUNCH_G: f64 = 3.0;
261const LAUNCH_HELD_G: f64 = 2.0;
262
263/// The least climb, m, integrated from the accelerometer, at which a row counts in a climb ratio,
264/// and the least climb of the pressure for a gain to be computed.
265const LEAST_CLIMB_M: f64 = 30.0;
266
267/// The least speed, m/s, integrated from the accelerometer, at which a row counts in a speed ratio.
268const LEAST_SPEED_M_S: f64 = 10.0;
269
270/// The fewest rows a ratio's median takes.
271const LEAST_ROWS: usize = 5;
272
273/// How many times off a length, a speed or an acceleration may be from the accelerometer's climb
274/// and still bear its unit out: 1.8² = 3.24 is under the 3.28 between meters and feet, so at most
275/// one of the two is within it.
276const LENGTH_FACTOR: f64 = 1.8;
277
278/// How far, as a share, a length's or a speed's gain may lie from 1 against a column of known
279/// unit.
280const GAIN_MARGIN: f64 = 0.25;
281
282/// How far, as a share, a reading at rest may be from 1 g.
283const GRAVITY_MARGIN: f64 = 0.1;
284
285/// The relative difference within which two computations of the same value agree: the files'
286/// values are the club's computations in `f64` or `f32`, a `f32` within 1.2 × 10⁻⁷.
287const SAME: f64 = 1e-6;
288
289/// LSM9DS1's accelerometer's sensitivities, g a count, at ±2, ±4, ±8 and ±16 g (*LSM9DS1*,
290/// STMicroelectronics, DocID025715 Rev 3, 2015, Table 3, `LA_So`).
291const ACCEL_SCALES_G: [f64; 4] = [0.061e-3, 0.122e-3, 0.244e-3, 0.732e-3];
292
293/// LSM9DS1's gyroscope's sensitivities, °/s a count, at ±245, ±500 and ±2,000 °/s (Table 3,
294/// `G_So`).
295const GYRO_SCALES_DPS: [f64; 3] = [8.75e-3, 17.5e-3, 70e-3];
296
297/// The longest step, s, of the turning and speed checks: the board's flight logging is 15 or 16
298/// ms.
299const FAST_STEP_S: f64 = 0.05;
300
301/// The turning check, as SillyGoose's reader makes it ([`crate::sillygoose`]): stretches of so
302/// many steps, the accelerometer within this share of 1 g, at least so many stretches and so much
303/// turning, and a unit taken if the gyroscope's turn is within this factor of the
304/// accelerometer's.
305const TURN_STEPS: usize = 10;
306const TURN_GRAVITY_MARGIN: f64 = 0.15;
307const TURN_LEAST_STRETCHES: usize = 3;
308const TURN_LEAST_RAD: f64 = 0.2;
309const TURN_FACTOR: f64 = 4.0;
310
311/// The least movement, m, the speed integrates over the fast steps for the speed's check against
312/// `pos_z` to decide.
313const SPEED_LEAST_M: f64 = 10.0;
314
315/// How far, m, the track may read from zero at its first sample before a note says the record
316/// doesn't start at the board's zero.
317const GROUND_OFF_M: f64 = 100.0;
318
319/// A foot, m.
320const FOOT_M: f64 = 0.3048;
321
322/// What a column is.
323#[derive(Debug, Clone, Copy, PartialEq, Eq)]
324enum Role {
325    /// The unnamed first column: the row's number in the club's table.
326    Index,
327    /// `timestamp_ms` or `timestamp_s`.
328    Clock,
329    /// A sensor's raw counts, such as `imu1_accel_x` or `high_g_accel_z`.
330    Raw,
331    /// An IMU's accelerometer, scaled: `imu1_accel_x_real`; the IMU and the axis.
332    AccelReal(u8, usize),
333    /// An IMU's gyroscope, scaled.
334    GyroReal(u8, usize),
335    /// The IMUs' accelerometers averaged: `imu_accel_x_avg`.
336    AccelAverage(usize),
337    /// The IMUs' gyroscopes averaged.
338    GyroAverage(usize),
339    /// A magnetometer, scaled or averaged.
340    Magnetic,
341    /// The high-g accelerometer, scaled: `high_g_accel_x_real`.
342    HighG(usize),
343    /// `baro1_pres`, `baro2_pres` or `baro_pres_avg`.
344    Pressure,
345    /// `baro1_temp`, `baro2_temp` or `baro_temp_avg`.
346    BaroTemperature,
347    /// `baro_height`, `baroalt_agl` or `baro1_alt`: the board's altitude from its barometer.
348    BaroAltitude,
349    Latitude,
350    Longitude,
351    /// `gps_lat_mod`: the club's conversion of `gps_lat`.
352    LatitudeMod,
353    LongitudeMod,
354    GpsAltitude,
355    Battery,
356    /// `dynamixel_degrees`: a servo's angle, its unit in its name.
357    ServoAngle,
358    /// A column no check settles: `heading`, `vtg`, `pos_x`, `pos_y`, `vel_x`, `vel_y`,
359    /// `dynamixel_length`.
360    Unsettled,
361    /// `pos_z`: the board's altitude estimate.
362    Height,
363    /// `vel_z`: the board's vertical speed estimate.
364    Climb,
365    /// `acc_x`, `acc_y`, `acc_z`: the board's acceleration estimate.
366    Estimate(usize),
367    /// A code, flag, count or quaternion part, as written.
368    Number,
369    Unknown,
370}
371
372/// The axis a name's part names.
373fn axis_of(part: &str) -> Option<usize> {
374    ["x", "y", "z"].iter().position(|axis| *axis == part)
375}
376
377/// What a column is, by its name.
378fn role_of(name: &str) -> Role {
379    match name {
380        "timestamp_ms" | "timestamp_s" => return Role::Clock,
381        "packetType" | "state" | "pyro_cont" | "pyro_status" | "trigger_status"
382        | "gps_fix_quality" | "gps_sats_tracked" | "gps_hdop" | "q_x" | "q_y" | "q_z" | "q_w" => {
383            return Role::Number;
384        }
385        "baro1_pres" | "baro2_pres" | "baro_pres_avg" => return Role::Pressure,
386        "baro1_temp" | "baro2_temp" | "baro_temp_avg" => return Role::BaroTemperature,
387        "baro_height" | "baroalt_agl" | "baro1_alt" => return Role::BaroAltitude,
388        "gps_lat" => return Role::Latitude,
389        "gps_long" => return Role::Longitude,
390        "gps_lat_mod" => return Role::LatitudeMod,
391        "gps_long_mod" => return Role::LongitudeMod,
392        "gps_alt" => return Role::GpsAltitude,
393        "battery_voltage" | "battery_voltage_1" | "battery_voltage_2" => return Role::Battery,
394        "dynamixel_degrees" | "dynamixel_degrees_1" => return Role::ServoAngle,
395        "dynamixel_length" | "dynamixel_length_1" | "heading" | "vtg" | "pos_x" | "pos_y"
396        | "vel_x" | "vel_y" => return Role::Unsettled,
397        "pos_z" => return Role::Height,
398        "vel_z" => return Role::Climb,
399        _ => {}
400    }
401    let parts: Vec<&str> = name.split('_').collect();
402    match parts.as_slice() {
403        ["acc", axis] => axis_of(axis).map_or(Role::Unknown, Role::Estimate),
404        [imu, "accel" | "gyro" | "mag", axis] if matches!(*imu, "imu1" | "imu2") => {
405            axis_of(axis).map_or(Role::Unknown, |_| Role::Raw)
406        }
407        [imu, sensor, axis, "real"] if matches!(*imu, "imu1" | "imu2") => {
408            let number = if *imu == "imu1" { 1 } else { 2 };
409            match (*sensor, axis_of(axis)) {
410                ("accel", Some(axis)) => Role::AccelReal(number, axis),
411                ("gyro", Some(axis)) => Role::GyroReal(number, axis),
412                ("mag", Some(_)) => Role::Magnetic,
413                _ => Role::Unknown,
414            }
415        }
416        ["imu", sensor, axis, "avg"] => match (*sensor, axis_of(axis)) {
417            ("accel", Some(axis)) => Role::AccelAverage(axis),
418            ("gyro", Some(axis)) => Role::GyroAverage(axis),
419            ("mag", Some(_)) => Role::Magnetic,
420            _ => Role::Unknown,
421        },
422        ["high", "g", "accel", axis] => axis_of(axis).map_or(Role::Unknown, |_| Role::Raw),
423        ["high", "g", "accel", axis, "real"] => axis_of(axis).map_or(Role::Unknown, Role::HighG),
424        _ => Role::Unknown,
425    }
426}
427
428/// How a column's values become its quantity's unit.
429#[derive(Debug, Clone, Copy, PartialEq)]
430enum Conversion {
431    /// Times a factor.
432    Scale(f64),
433    /// NMEA's degrees and minutes, `ddmm.mmmm`, to degrees.
434    DegreesMinutes,
435}
436
437impl Conversion {
438    fn apply(self, value: f64) -> f64 {
439        match self {
440            Self::Scale(scale) => value * scale,
441            Self::DegreesMinutes => degrees_minutes(value),
442        }
443    }
444}
445
446/// NMEA 0183's `ddmm.mmmm`, degrees times 100 plus minutes, in degrees, its sign kept.
447fn degrees_minutes(value: f64) -> f64 {
448    let magnitude = value.abs();
449    let degrees = (magnitude / 100.0).floor();
450    (degrees + (magnitude - 100.0 * degrees) / 60.0).copysign(value)
451}
452
453/// One data column.
454struct Column {
455    name: String,
456    role: Role,
457    /// The values as written, `NaN` for a gap.
458    values: Vec<f64>,
459    /// For a pressure, each cell's last written decimal place: the digits after its point, less
460    /// its exponent; `None` for a gap.
461    places: Vec<Option<i32>>,
462    /// What the column is read as: [`Quantity::Unrecognized`], kept as written, until a check
463    /// decides its unit.
464    quantity: Quantity,
465    conversion: Conversion,
466}
467
468impl Column {
469    fn new(name: &str, role: Role) -> Self {
470        let quantity = match role {
471            Role::Index | Role::Number => Quantity::Number,
472            Role::ServoAngle => Quantity::Angle,
473            _ => Quantity::Unrecognized,
474        };
475        Self {
476            name: name.to_owned(),
477            role,
478            values: Vec::new(),
479            places: Vec::new(),
480            quantity,
481            conversion: Conversion::Scale(1.0),
482        }
483    }
484
485    /// Reads the column as a quantity, its values times a factor.
486    fn read_as(&mut self, quantity: Quantity, scale: f64) {
487        self.quantity = quantity;
488        self.conversion = Conversion::Scale(scale);
489    }
490
491    /// The column's name, cut short, in backticks.
492    fn quoted(&self) -> String {
493        format!("`{}`", short(&self.name))
494    }
495
496    /// Whether no value is a number other than 0.
497    fn all_zero(&self) -> bool {
498        self.values.iter().all(|v| !v.is_finite() || *v == 0.0)
499    }
500
501    /// The median of its first [`PAD_ROWS`] numbers: its reading on the pad.
502    fn pad(&self) -> Option<f64> {
503        median(self.values.iter().take(PAD_ROWS).copied())
504    }
505}
506
507/// The median of the numbers among some values; `None` if none is.
508fn median(values: impl Iterator<Item = f64>) -> Option<f64> {
509    let mut finite: Vec<f64> = values.filter(|v| v.is_finite()).collect();
510    if finite.is_empty() {
511        return None;
512    }
513    finite.sort_by(f64::total_cmp);
514    Some(finite[finite.len() / 2])
515}
516
517/// A cell's last written decimal place: the digits after its point, less its exponent.
518fn decimal_places(cell: &str) -> i32 {
519    let cell = cell.trim();
520    let (mantissa, exponent) = match cell.find(['e', 'E']) {
521        Some(at) => (&cell[..at], cell[at + 1..].parse::<i32>().unwrap_or(0)),
522        None => (cell, 0),
523    };
524    let after_point = mantissa
525        .split_once('.')
526        .map_or(0, |(_, fraction)| fraction.len());
527    i32::try_from(after_point)
528        .unwrap_or(i32::MAX)
529        .saturating_sub(exponent)
530}
531
532/// The names of the columns at some places, as a sentence.
533fn listed(columns: &[Column], at: &[usize]) -> String {
534    let quoted: Vec<String> = at.iter().map(|&i| columns[i].quoted()).collect();
535    match quoted.as_slice() {
536        [] => String::new(),
537        [one] => one.clone(),
538        [rest @ .., last] => format!("{} and {last}", rest.join(", ")),
539    }
540}
541
542fn magnitude(v: [f64; 3]) -> f64 {
543    (v[0] * v[0] + v[1] * v[1] + v[2] * v[2]).sqrt()
544}
545
546fn dot(a: [f64; 3], b: [f64; 3]) -> f64 {
547    a[0] * b[0] + a[1] * b[1] + a[2] * b[2]
548}
549
550/// Whether a ratio lies within a factor of 1, either way; `NaN` doesn't.
551fn within_factor(ratio: f64, factor: f64) -> bool {
552    ratio >= 1.0 / factor && ratio <= factor
553}
554
555/// Whether a gain lies within a share of 1; `NaN` doesn't.
556fn within_margin(gain: f64, margin: f64) -> bool {
557    (gain - 1.0).abs() <= margin
558}
559
560/// Whether the text is the board's CSV: a header row whose first cell is empty and that names
561/// `packetType` or a `timestamp_ms` or `timestamp_s` column.
562pub fn recognizes(text: &str) -> bool {
563    let first = text
564        .trim_start_matches('\u{feff}')
565        .lines()
566        .map(str::trim)
567        .find(|line| !line.is_empty())
568        .unwrap_or("");
569    first.starts_with(',')
570        && first
571            .split(',')
572            .any(|name| matches!(name.trim(), "packetType" | "timestamp_ms" | "timestamp_s"))
573}
574
575fn syntax(line: usize, message: String) -> LogError {
576    LogError::Syntax {
577        format: FORMAT,
578        line,
579        message,
580    }
581}
582
583fn not_this(message: String) -> LogError {
584    LogError::NotThisFormat {
585        format: FORMAT,
586        message,
587    }
588}
589
590/// The file's rows, read.
591struct Table {
592    header_line: usize,
593    columns: Vec<Column>,
594    /// Each row's line in the file.
595    lines: Vec<usize>,
596    gaps: usize,
597    first_gap: Option<usize>,
598    short_rows: usize,
599    first_short: Option<usize>,
600}
601
602/// Reads the header and the rows.
603fn read_table(text: &str) -> Result<Table, LogError> {
604    let mut lines = numbered_lines(text).filter(|(_, line)| !line.trim().is_empty());
605    let Some((header_line, header)) = lines.next() else {
606        return Err(not_this("the file is empty".to_owned()));
607    };
608    let header = header.trim();
609    if !recognizes(header) {
610        return Err(not_this(format!(
611            "its header, {:?}, isn't the board's: an empty first name, then names such as \
612             `packetType` and `timestamp_ms`",
613            short(header)
614        )));
615    }
616    let mut columns: Vec<Column> = Vec::new();
617    let mut seen: BTreeSet<&str> = BTreeSet::new();
618    for (index, name) in header.split(',').map(str::trim).enumerate() {
619        if index >= MAX_COLUMNS {
620            return Err(syntax(
621                header_line,
622                format!("the header names more than {MAX_COLUMNS} columns"),
623            ));
624        }
625        if index > 0 && name.is_empty() {
626            return Err(syntax(
627                header_line,
628                format!("the header's column {} has no name", index + 1),
629            ));
630        }
631        if !seen.insert(name) {
632            return Err(syntax(
633                header_line,
634                format!("the header names {:?} twice", short(name)),
635            ));
636        }
637        let role = if index == 0 {
638            Role::Index
639        } else {
640            role_of(name)
641        };
642        columns.push(Column::new(name, role));
643    }
644    let width = columns.len();
645
646    let mut table = Table {
647        header_line,
648        columns,
649        lines: Vec::new(),
650        gaps: 0,
651        first_gap: None,
652        short_rows: 0,
653        first_short: None,
654    };
655    let mut cells_written = 0usize;
656    for (line_number, line) in lines {
657        // One more than the header names, so that a wider row is told apart without collecting
658        // the whole of a long line.
659        let row: Vec<&str> = line.trim_end().split(',').take(width + 1).collect();
660        if row.len() > width {
661            return Err(syntax(
662                line_number,
663                format!("the row has more than {width} cells, where the header names {width}"),
664            ));
665        }
666        if row.len() < width {
667            table.short_rows += 1;
668            table.first_short.get_or_insert(line_number);
669        }
670        cells_written = cells_written.saturating_add(row.len());
671        table.lines.push(line_number);
672        for (index, column) in table.columns.iter_mut().enumerate() {
673            let cell = row.get(index);
674            let value = match cell {
675                Some(cell) => number(cell, false).unwrap_or_else(|| {
676                    table.gaps += 1;
677                    table.first_gap.get_or_insert(line_number);
678                    f64::NAN
679                }),
680                None => f64::NAN,
681            };
682            column.values.push(value);
683            if column.role == Role::Pressure {
684                column.places.push(
685                    cell.filter(|_| value.is_finite())
686                        .map(|c| decimal_places(c)),
687                );
688            }
689        }
690        if crate::log::too_sparse(width, table.lines.len(), cells_written) {
691            return Err(syntax(
692                line_number,
693                format!(
694                    "{} rows hold {cells_written} of the {} cells the header's {width} columns \
695                     name: too few for the header to be this file's",
696                    table.lines.len(),
697                    table.lines.len().saturating_mul(width)
698                ),
699            ));
700        }
701    }
702    Ok(table)
703}
704
705/// Reads the board's CSV.
706///
707/// # Errors
708///
709/// [`LogError::NotThisFormat`] if the first line that isn't blank isn't the board's header;
710/// [`LogError::Syntax`] for a header naming more than [`MAX_COLUMNS`] columns, a column with no
711/// name but the first, a name given twice, a row with more cells than the header names, a file
712/// whose rows hold too few of the cells its header names, two clock columns that both hold times,
713/// or a value too large to hold once converted to its unit; [`LogError::NoData`] if no row has a
714/// time; [`LogError::Unit`] if the clock's unit isn't decided (module docs).
715pub fn read(text: &str) -> Result<FlightLog, LogError> {
716    let mut table = read_table(text)?;
717    let header_line = table.header_line;
718    let mut decisions: Vec<String> = Vec::new();
719
720    // The clock: whichever of `timestamp_ms` and `timestamp_s` holds times.
721    let clocks: Vec<usize> = (0..table.columns.len())
722        .filter(|&i| table.columns[i].role == Role::Clock)
723        .collect();
724    let ticking: Vec<usize> = clocks
725        .iter()
726        .copied()
727        .filter(|&i| !table.columns[i].all_zero())
728        .collect();
729    let clock = match ticking.as_slice() {
730        [one] => *one,
731        [] => {
732            return Err(syntax(
733                header_line,
734                "no clock column, `timestamp_ms` or `timestamp_s`, holds times".to_owned(),
735            ));
736        }
737        _ => {
738            return Err(syntax(
739                header_line,
740                format!(
741                    "{} both hold times: the reader can't tell which is the board's clock",
742                    listed(&table.columns, &ticking)
743                ),
744            ));
745        }
746    };
747
748    let pressure = decide_pressures(&mut table.columns, &mut decisions);
749    let pressure_m = pressure_altitudes(&table.columns, pressure);
750    let raw = raw_accelerometer(&table.columns);
751    let (time_s, clock_note) = decide_clock(&table.columns, clock, &pressure_m, raw, header_line)?;
752    decisions.push(clock_note);
753    let flight = Flight::new(
754        time_s,
755        pressure_m,
756        raw.map(|(axes, pad)| (axes, pad, &table.columns)),
757    );
758
759    let columns = &mut table.columns;
760    decide_imu_accelerometers(columns, &mut decisions);
761    decide_estimate(columns, &flight, &mut decisions);
762    decide_gyroscopes(columns, &flight, &mut decisions);
763    let reference = reference_accelerometer(columns);
764    decide_lengths(columns, &flight, reference, &mut decisions);
765    decide_climb(columns, &flight, reference, &mut decisions);
766    decide_position(columns, &mut decisions);
767    keep_the_rest(columns, clock, &mut decisions);
768
769    build(table, clock, flight, decisions)
770}
771
772/// What the flight's checks share: the decided clock, the pressure's altitude and the launch.
773struct Flight {
774    /// Each row's time, s.
775    time_s: Vec<f64>,
776    /// The pressure altitude's 5-row running median, against a barometer's spikes.
777    smooth_m: Vec<f64>,
778    /// The pressure altitude on the pad, m.
779    pad_m: Option<f64>,
780    /// The row of the highest smoothed pressure altitude.
781    apogee: Option<usize>,
782    /// The launch's row, and the raw accelerometer's direction on the pad.
783    launch: Option<(usize, [usize; 3], [f64; 3])>,
784}
785
786impl Flight {
787    fn new(
788        time_s: Vec<f64>,
789        pressure_m: Vec<f64>,
790        raw: Option<([usize; 3], [f64; 3], &Vec<Column>)>,
791    ) -> Self {
792        let smooth_m = running_median(&pressure_m);
793        let pad_m = median(pressure_m.iter().take(PAD_ROWS).copied());
794        let apogee = highest(&smooth_m);
795        let launch = raw.and_then(|(axes, pad, columns)| {
796            launch_row(columns, axes, pad).map(|row| (row, axes, pad))
797        });
798        Self {
799            time_s,
800            smooth_m,
801            pad_m,
802            apogee,
803            launch,
804        }
805    }
806
807    /// The pressure's climb above the pad at a row, smoothed.
808    fn climb_m(&self, row: usize) -> f64 {
809        self.pad_m.map_or(f64::NAN, |pad| self.smooth_m[row] - pad)
810    }
811}
812
813/// A 5-row running median, `NaN` where the window holds no number.
814fn running_median(values: &[f64]) -> Vec<f64> {
815    (0..values.len())
816        .map(|row| {
817            let from = row.saturating_sub(2);
818            let to = (row + 3).min(values.len());
819            median(values[from..to].iter().copied()).unwrap_or(f64::NAN)
820        })
821        .collect()
822}
823
824/// The row of the highest number, the first if several.
825fn highest(values: &[f64]) -> Option<usize> {
826    let mut best: Option<usize> = None;
827    for (row, value) in values.iter().enumerate() {
828        if value.is_finite() && best.is_none_or(|b| *value > values[b]) {
829            best = Some(row);
830        }
831    }
832    best
833}
834
835/// Decides each pressure's unit (module docs); the column the altitude checks use, if any is read:
836/// `baro_pres_avg`, else `baro1_pres`, else `baro2_pres`.
837fn decide_pressures(columns: &mut [Column], decisions: &mut Vec<String>) -> Option<usize> {
838    for column in columns.iter_mut().filter(|c| c.role == Role::Pressure) {
839        decisions.push(decide_pressure(column));
840    }
841    ["baro_pres_avg", "baro1_pres", "baro2_pres"]
842        .iter()
843        .find_map(|name| {
844            columns
845                .iter()
846                .position(|c| c.name == *name && c.quantity == Quantity::Pressure)
847        })
848}
849
850/// Whether every cell of a column precise enough to test lies on a step of `1 / (m × per)`, for
851/// m = 1 or 2 (half steps, an average of two): the m, if so, and how many cells were tested at it.
852fn on_step(column: &Column, per: f64) -> (Option<u32>, usize) {
853    let mut most_tested = 0;
854    for m in [1u32, 2] {
855        let steps = f64::from(m) * per;
856        let mut tested = 0usize;
857        let mut on = true;
858        for (value, place) in column.values.iter().zip(&column.places) {
859            let Some(place) = place else { continue };
860            // A cell written too short to test: its printing alone could take half the tolerance.
861            if *value == 0.0 || steps * 0.5 * 10f64.powi(-*place) > STEP_TOLERANCE / 2.0 {
862                continue;
863            }
864            tested += 1;
865            let in_steps = value * steps;
866            let off = (in_steps - in_steps.round()).abs();
867            // Written to fail closed: a value that overflows, its distance `NaN`, is off.
868            if off.is_nan() || off > STEP_TOLERANCE {
869                on = false;
870                break;
871            }
872        }
873        if on && tested >= LEAST_STEP_CELLS {
874            return (Some(m), tested);
875        }
876        most_tested = most_tested.max(tested);
877    }
878    (None, most_tested)
879}
880
881/// Decides one pressure's unit: atmospheres if its size and its step both say so (module docs).
882fn decide_pressure(column: &mut Column) -> String {
883    let name = column.quoted();
884    if column.all_zero() {
885        return format!("{name} kept as written: every value is 0, a barometer that isn't working");
886    }
887    let Some(pad) = column.pad() else {
888        return format!("{name} kept as written: no value on the pad");
889    };
890    let size = (PAD_PRESSURE.0..=PAD_PRESSURE.1).contains(&pad);
891    let (atm, tested) = on_step(column, ATM_PA);
892    let (bar, _) = on_step(column, BAR_PA);
893    match (size, atm, bar) {
894        (true, Some(m), None) => {
895            let step = if m == 1 {
896                "a whole number of pascals over 101,325"
897            } else {
898                "a whole or half number of pascals over 101,325, as an average of two whole ones \
899                 is"
900            };
901            column.read_as(Quantity::Pressure, ATM_PA);
902            format!(
903                "{name} read in atmospheres, from its size and its step: on the pad it reads \
904                 {pad}. That is within the 0.5 to 1.1 that only atmospheres and bars of the units a \
905                 barometer is written in put a pad. Each of its {tested} cells written \
906                 precisely enough to test is {step} within a millionth of a step. That is so of a \
907                 barometer's pascals written in atmospheres, and not of those written in bars, \
908                 over 100,000"
909            )
910        }
911        _ => {
912            column.quantity = Quantity::Unrecognized;
913            let size_says = if size {
914                format!("on the pad it reads {pad}, near 1 as atmospheres or bars put a pad")
915            } else {
916                format!("on the pad it reads {pad}, outside the 0.5 to 1.1 of atmospheres or bars")
917            };
918            let step_says = match (atm, bar) {
919                (Some(_), Some(_)) => {
920                    "Its values lie on the steps of both atmospheres and bars".to_owned()
921                }
922                (None, Some(_)) => "Its values lie on bars' steps of 1/100,000, not on \
923                                    atmospheres' of a pascal over 101,325"
924                    .to_owned(),
925                (None, None) => format!(
926                    "Of its cells written precisely enough to test ({tested}), not all lie on \
927                     a whole number of pascals over 101,325 or over 100,000. Or too few are \
928                     written precisely enough to tell"
929                ),
930                (Some(_), None) => "Its values lie on atmospheres' steps".to_owned(),
931            };
932            format!(
933                "{name} kept as written: its size and its step don't both say atmospheres: \
934                 {size_says}. {step_says}"
935            )
936        }
937    }
938}
939
940/// Each row's pressure altitude in the U.S. Standard Atmosphere, 1976, m, from the pressure the
941/// checks use; `NaN` where it has none.
942fn pressure_altitudes(columns: &[Column], pressure: Option<usize>) -> Vec<f64> {
943    let rows = columns.first().map_or(0, |c| c.values.len());
944    let Some(at) = pressure else {
945        return vec![f64::NAN; rows];
946    };
947    let atmosphere = Ussa76::standard();
948    let column = &columns[at];
949    column
950        .values
951        .iter()
952        .map(|value| {
953            let pa = column.conversion.apply(*value);
954            if pa.is_finite() && pa > 0.0 {
955                atmosphere.pressure_altitude_m(pa).unwrap_or(f64::NAN)
956            } else {
957                f64::NAN
958            }
959        })
960        .collect()
961}
962
963/// The three columns of a sensor, if the file has all three, found by a role per axis.
964fn triple(columns: &[Column], role: impl Fn(usize) -> Role) -> Option<[usize; 3]> {
965    let find = |axis| columns.iter().position(|c| c.role == role(axis));
966    Some([find(0)?, find(1)?, find(2)?])
967}
968
969/// The three columns with these names, if the file has all three.
970fn named(columns: &[Column], names: [String; 3]) -> Option<[usize; 3]> {
971    let find = |name: &String| columns.iter().position(|c| &c.name == name);
972    Some([find(&names[0])?, find(&names[1])?, find(&names[2])?])
973}
974
975/// A sensor's values at a row, as written.
976fn vector(columns: &[Column], axes: [usize; 3], row: usize) -> [f64; 3] {
977    axes.map(|at| columns[at].values[row])
978}
979
980/// A sensor's reading on the pad: each axis's median over [`PAD_ROWS`].
981fn pad_vector(columns: &[Column], axes: [usize; 3]) -> Option<[f64; 3]> {
982    Some([
983        columns[axes[0]].pad()?,
984        columns[axes[1]].pad()?,
985        columns[axes[2]].pad()?,
986    ])
987}
988
989/// The raw accelerometer the launch and the clock's check use, `imu1`'s or else `imu2`'s, and its
990/// reading on the pad: in counts, its own unit, which the checks divide out.
991fn raw_accelerometer(columns: &[Column]) -> Option<([usize; 3], [f64; 3])> {
992    ["imu1", "imu2"].iter().find_map(|imu| {
993        let axes = named(columns, ["x", "y", "z"].map(|a| format!("{imu}_accel_{a}")))?;
994        let pad = pad_vector(columns, axes)?;
995        (magnitude(pad) > 0.0).then_some((axes, pad))
996    })
997}
998
999/// The launch's row: the first whose specific force, in its pad reading, is at least
1000/// [`LAUNCH_G`], the next two rows' at least [`LAUNCH_HELD_G`].
1001fn launch_row(columns: &[Column], axes: [usize; 3], pad: [f64; 3]) -> Option<usize> {
1002    let one_g = magnitude(pad);
1003    let rows = columns[axes[0]].values.len();
1004    let force = |row: usize| magnitude(vector(columns, axes, row)) / one_g;
1005    (0..rows).find(|&row| {
1006        force(row) >= LAUNCH_G && (row + 1..(row + 3).min(rows)).all(|r| force(r) >= LAUNCH_HELD_G)
1007    })
1008}
1009
1010/// How an accelerometer's values become m/s².
1011#[derive(Debug, Clone, Copy)]
1012enum ToMs2 {
1013    /// Times a factor.
1014    Scale(f64),
1015    /// In its pad reading, 1 g: unit-free.
1016    PadIsOneG,
1017}
1018
1019/// One row of an integrated climb: the row, the speed, m/s, and the climb, m.
1020type Integrated = (usize, f64, f64);
1021
1022/// The climb an accelerometer integrates to (module docs): from the launch's row, its specific
1023/// force along its pad reading's direction, less gravity, integrated once to a speed and again to
1024/// a climb, row by row on `times` times `scale` seconds, until the pressure's apogee or until the
1025/// speed falls back to 0.
1026fn integrate(
1027    columns: &[Column],
1028    axes: [usize; 3],
1029    to_ms2: ToMs2,
1030    times: &[f64],
1031    scale: f64,
1032    launch: usize,
1033    stop: usize,
1034) -> Vec<Integrated> {
1035    let Some(pad) = pad_vector(columns, axes) else {
1036        return Vec::new();
1037    };
1038    let one_g = magnitude(pad);
1039    if one_g == 0.0 {
1040        return Vec::new();
1041    }
1042    let up = pad.map(|v| v / one_g);
1043    let factor = match to_ms2 {
1044        ToMs2::Scale(factor) => factor,
1045        ToMs2::PadIsOneG => STANDARD_GRAVITY_MPS2 / one_g,
1046    };
1047    let mut rows = Vec::new();
1048    let (mut speed, mut climb) = (0.0, 0.0);
1049    for row in launch..stop.min(times.len().saturating_sub(1)) {
1050        let step = (times[row + 1] - times[row]) * scale;
1051        let force = dot(vector(columns, axes, row), up) * factor;
1052        if !(step > 0.0 && step.is_finite() && force.is_finite()) {
1053            continue;
1054        }
1055        speed += (force - STANDARD_GRAVITY_MPS2) * step;
1056        climb += speed * step;
1057        if row > launch && speed <= 0.0 {
1058            break;
1059        }
1060        rows.push((row + 1, speed, climb));
1061    }
1062    rows
1063}
1064
1065/// The median, over the integrated rows whose climb reaches [`LEAST_CLIMB_M`], of a quantity at the
1066/// row over the climb; `None` with under [`LEAST_ROWS`] such rows.
1067fn climb_ratio(integrated: &[Integrated], of: impl Fn(usize) -> f64) -> Option<f64> {
1068    let ratios = integrated
1069        .iter()
1070        .filter(|(_, _, climb)| *climb >= LEAST_CLIMB_M)
1071        .map(|&(row, _, climb)| of(row) / climb);
1072    let ratios: Vec<f64> = ratios.filter(|r| r.is_finite()).collect();
1073    (ratios.len() >= LEAST_ROWS)
1074        .then(|| median(ratios.into_iter()))
1075        .flatten()
1076}
1077
1078/// The same over the rows whose speed reaches [`LEAST_SPEED_M_S`], of a quantity over the speed.
1079fn speed_ratio(integrated: &[Integrated], of: impl Fn(usize) -> f64) -> Option<f64> {
1080    let ratios = integrated
1081        .iter()
1082        .filter(|(_, speed, _)| *speed >= LEAST_SPEED_M_S)
1083        .map(|&(row, speed, _)| of(row) / speed);
1084    let ratios: Vec<f64> = ratios.filter(|r| r.is_finite()).collect();
1085    (ratios.len() >= LEAST_ROWS)
1086        .then(|| median(ratios.into_iter()))
1087        .flatten()
1088}
1089
1090/// Decides the clock's unit, milliseconds or seconds, from its spacing and the climb (module
1091/// docs): each row's time, s, and the note; refused if the two don't agree on one unit.
1092fn decide_clock(
1093    columns: &[Column],
1094    clock: usize,
1095    pressure_m: &[f64],
1096    raw: Option<([usize; 3], [f64; 3])>,
1097    header_line: usize,
1098) -> Result<(Vec<f64>, String), LogError> {
1099    let column = &columns[clock];
1100    let times = &column.values;
1101    let step = median(
1102        times
1103            .windows(2)
1104            .map(|pair| pair[1] - pair[0])
1105            .filter(|step| *step > 0.0),
1106    );
1107    let units = [("milliseconds", 1e-3), ("seconds", 1.0)];
1108    let spaced = |scale: f64| {
1109        step.is_some_and(|step| (ROW_STEP_S.0..=ROW_STEP_S.1).contains(&(step * scale)))
1110    };
1111    let smooth = running_median(pressure_m);
1112    let pad_m = median(pressure_m.iter().take(PAD_ROWS).copied());
1113    let launch = raw.and_then(|(axes, pad)| launch_row(columns, axes, pad));
1114    let apogee = highest(&smooth);
1115    let climbed = |scale: f64| -> Option<f64> {
1116        let ((axes, _), launch, apogee, pad_m) = (raw?, launch?, apogee?, pad_m?);
1117        let integrated = integrate(
1118            columns,
1119            axes,
1120            ToMs2::PadIsOneG,
1121            times,
1122            scale,
1123            launch,
1124            apogee,
1125        );
1126        climb_ratio(&integrated, |row| smooth[row] - pad_m)
1127    };
1128    let found: Vec<(f64, Option<f64>)> = units.iter().map(|(_, s)| (*s, climbed(*s))).collect();
1129    let name = column.quoted();
1130    let step_text = step.map_or_else(|| "no step".to_owned(), |s| format!("{s}"));
1131    let climb_text = |ratio: Option<f64>| {
1132        ratio.map_or_else(
1133            || "none, too little to tell".to_owned(),
1134            |r| format!("{r:.3}"),
1135        )
1136    };
1137    let evidence = format!(
1138        "its rows' median step is {step_text} as written. The pressure reads a climb from the \
1139         launch, and the raw accelerometer integrates to another in its pad reading's 1 g. The \
1140         first over the second is, read in milliseconds, {}, and read in seconds, {}. A row \
1141         every 5 to 500 ms, as the board logs, and a ratio within a factor of {CLOCK_FACTOR} of \
1142         1 are taken",
1143        climb_text(found[0].1),
1144        climb_text(found[1].1)
1145    );
1146    let agreed: Vec<usize> = (0..units.len())
1147        .filter(|&i| {
1148            spaced(units[i].1) && found[i].1.is_some_and(|r| within_factor(r, CLOCK_FACTOR))
1149        })
1150        .collect();
1151    match agreed.as_slice() {
1152        [i] => {
1153            let (unit, scale) = units[*i];
1154            let time_s = if scale == 1.0 {
1155                times.clone()
1156            } else {
1157                times.iter().map(|t| t / 1_000.0).collect()
1158            };
1159            Ok((
1160                time_s,
1161                format!("{name} read in {unit}, from its spacing and the climb: {evidence}"),
1162            ))
1163        }
1164        _ => Err(LogError::Unit {
1165            format: FORMAT,
1166            line: header_line,
1167            message: format!(
1168                "the clock {name} is in no unit both its spacing and the climb bear out: \
1169                 {evidence}"
1170            ),
1171        }),
1172    }
1173}
1174
1175/// An accelerometer's unit: m/s² or standard gravities, and the factor to m/s².
1176const ACCEL_UNITS: [(&str, f64); 2] = [
1177    ("meters per second squared", 1.0),
1178    ("standard gravities, × 9.806 65 m/s²", STANDARD_GRAVITY_MPS2),
1179];
1180
1181/// Which of [`ACCEL_UNITS`] reads 1 g at rest: the magnitude of a sensor's pad reading within
1182/// [`GRAVITY_MARGIN`] of 9.806 65 or of 1.
1183fn gravity_unit(columns: &[Column], axes: [usize; 3]) -> (Option<usize>, Option<f64>) {
1184    let Some(pad) = pad_vector(columns, axes) else {
1185        return (None, None);
1186    };
1187    let reading = magnitude(pad);
1188    let unit = ACCEL_UNITS.iter().position(|(_, scale)| {
1189        within_margin(reading * scale / STANDARD_GRAVITY_MPS2, GRAVITY_MARGIN)
1190    });
1191    (unit, Some(reading))
1192}
1193
1194/// Whether every number of a column is its raw count's times one factor, within [`SAME`]: `None`
1195/// if not, `Some(None)` if every count is 0 and so is every number (any factor fits), and
1196/// `Some(Some(factor))` otherwise.
1197fn factor_of(column: &Column, raw: &Column) -> Option<Option<f64>> {
1198    let mut found: Option<f64> = None;
1199    for (value, count) in column.values.iter().zip(&raw.values) {
1200        if !(value.is_finite() && count.is_finite()) {
1201            continue;
1202        }
1203        if *count == 0.0 {
1204            if *value != 0.0 {
1205                return None;
1206            }
1207            continue;
1208        }
1209        let ratio = value / count;
1210        match found {
1211            None => found = Some(ratio),
1212            Some(f) if ((ratio - f) / f).abs() <= SAME => {}
1213            Some(_) => return None,
1214        }
1215    }
1216    Some(found)
1217}
1218
1219/// The one factor a sensor's three scaled columns are their raw counts times (module docs): every
1220/// axis must fit it, and at least one must have a count that isn't 0 to set it.
1221fn common_factor(columns: &[Column], axes: [usize; 3]) -> Option<f64> {
1222    let mut common: Option<f64> = None;
1223    for at in axes {
1224        let raw = raw_of(columns, at)?;
1225        let Some(factor) = factor_of(&columns[at], &columns[raw])? else {
1226            continue;
1227        };
1228        match common {
1229            None => common = Some(factor),
1230            Some(f) if ((factor - f) / f).abs() <= SAME => {}
1231            Some(_) => return None,
1232        }
1233    }
1234    common
1235}
1236
1237/// The raw column of a scaled one: its name without `_real`.
1238fn raw_of(columns: &[Column], at: usize) -> Option<usize> {
1239    let raw = columns[at].name.strip_suffix("_real")?;
1240    columns.iter().position(|c| c.name == raw)
1241}
1242
1243/// Whether a scale is one of a list's within [`SAME`].
1244fn listed_scale(scale: f64, list: &[f64]) -> Option<f64> {
1245    list.iter()
1246        .copied()
1247        .find(|known| ((scale - known) / known).abs() <= SAME)
1248}
1249
1250/// Decides each IMU's and the high-g accelerometer's scaled columns, then the IMUs' average
1251/// (module docs).
1252fn decide_imu_accelerometers(columns: &mut [Column], decisions: &mut Vec<String>) {
1253    let mut decided: Vec<(u8, Option<usize>)> = Vec::new();
1254    for imu in [1u8, 2] {
1255        if let Some(axes) = triple(columns, |a| Role::AccelReal(imu, a)) {
1256            let unit = decide_scaled_accelerometer(columns, axes, decisions);
1257            decided.push((imu, unit));
1258        }
1259    }
1260    if let Some(axes) = triple(columns, Role::HighG) {
1261        decide_scaled_accelerometer(columns, axes, decisions);
1262    }
1263    let Some(axes) = triple(columns, Role::AccelAverage) else {
1264        return;
1265    };
1266    let names = listed(columns, &axes);
1267    let (unit, reading) = gravity_unit(columns, axes);
1268    let inputs = decided.iter().map(|(_, unit)| *unit).collect::<Vec<_>>();
1269    let same_unit = inputs
1270        .first()
1271        .copied()
1272        .flatten()
1273        .filter(|u| inputs.iter().all(|i| *i == Some(*u)));
1274    let mean = same_unit.is_some()
1275        && (0..3).all(|axis| is_mean(columns, axes[axis], |imu| Role::AccelReal(imu, axis)));
1276    let reading = reading.map_or_else(|| "nothing".to_owned(), |r| format!("{r:.3}"));
1277    match (unit, same_unit, mean) {
1278        (Some(unit), Some(input), true) if unit == input => {
1279            for at in axes {
1280                columns[at].read_as(Quantity::Acceleration, ACCEL_UNITS[unit].1);
1281            }
1282            decisions.push(format!(
1283                "{names} read in {}, from the pad's gravity and the IMUs: at rest they read \
1284                 {reading}, 1 g in that unit within 10%. Each is the mean of the IMUs' scaled \
1285                 columns, read in it",
1286                ACCEL_UNITS[unit].0
1287            ));
1288        }
1289        _ => {
1290            let why = if same_unit.is_none() {
1291                "the IMUs' scaled columns they average aren't all read in one unit, so neither is \
1292                 their mean"
1293            } else if !mean {
1294                "they aren't the mean of the IMUs' scaled columns"
1295            } else {
1296                "they don't read 1 g at rest in the IMUs' unit"
1297            };
1298            decisions.push(format!(
1299                "{names} kept as written: the pad's gravity and the IMUs don't both bear one unit \
1300                 out: {why}; at rest they read {reading}"
1301            ));
1302        }
1303    }
1304}
1305
1306/// Whether a column is the mean of the IMUs' columns of a role, every row within [`SAME`].
1307fn is_mean(columns: &[Column], at: usize, role: impl Fn(u8) -> Role) -> bool {
1308    let inputs: Vec<usize> = [1u8, 2]
1309        .iter()
1310        .filter_map(|imu| columns.iter().position(|c| c.role == role(*imu)))
1311        .collect();
1312    if inputs.is_empty() {
1313        return false;
1314    }
1315    let count = inputs.len() as f64;
1316    let mut any = false;
1317    for row in 0..columns[at].values.len() {
1318        let value = columns[at].values[row];
1319        let mean = inputs.iter().map(|&i| columns[i].values[row]).sum::<f64>() / count;
1320        if !(value.is_finite() && mean.is_finite()) {
1321            continue;
1322        }
1323        any = true;
1324        let off = (value - mean).abs();
1325        if off.is_nan() || off > SAME * value.abs().max(1.0) {
1326            return false;
1327        }
1328    }
1329    any
1330}
1331
1332/// Decides a scaled accelerometer's three columns by the pad's gravity and the sensor's scale
1333/// (module docs); the unit, if decided.
1334fn decide_scaled_accelerometer(
1335    columns: &mut [Column],
1336    axes: [usize; 3],
1337    decisions: &mut Vec<String>,
1338) -> Option<usize> {
1339    let names = listed(columns, &axes);
1340    if axes.iter().all(|&at| columns[at].all_zero()) {
1341        decisions.push(format!(
1342            "{names} kept as written: every value is 0, a sensor that isn't working"
1343        ));
1344        return None;
1345    }
1346    let (gravity, reading) = gravity_unit(columns, axes);
1347    let common = common_factor(columns, axes);
1348    let scale_unit = common.and_then(|factor| {
1349        ACCEL_UNITS.iter().position(|(_, to_ms2)| {
1350            listed_scale(factor * to_ms2 / STANDARD_GRAVITY_MPS2, &ACCEL_SCALES_G).is_some()
1351        })
1352    });
1353    let reading = reading.map_or_else(|| "nothing".to_owned(), |r| format!("{r:.3}"));
1354    let factor = common.map_or_else(
1355        || "The columns aren't their raw counts times one factor".to_owned(),
1356        |f| format!("Each column is its raw count times {f:.7}"),
1357    );
1358    match (gravity, scale_unit) {
1359        (Some(g), Some(s)) if g == s => {
1360            for at in axes {
1361                columns[at].read_as(Quantity::Acceleration, ACCEL_UNITS[g].1);
1362            }
1363            decisions.push(format!(
1364                "{names} read in {}, from the pad's gravity and the sensor's scale: at rest they \
1365                 read {reading}, 1 g in that unit within 10%. {factor}, in that unit one of \
1366                 the counts of STMicroelectronics' LSM9DS1 (0.061, 0.122, 0.244 or 0.732 mg)",
1367                ACCEL_UNITS[g].0
1368            ));
1369            Some(g)
1370        }
1371        _ => {
1372            let scale_says = match scale_unit {
1373                Some(s) => format!(
1374                    "{factor}, one of the LSM9DS1's counts read in {}",
1375                    ACCEL_UNITS[s].0
1376                ),
1377                None => format!("{factor}, none of the LSM9DS1's counts in either unit"),
1378            };
1379            decisions.push(format!(
1380                "{names} kept as written: the pad's gravity and the sensor's scale don't both \
1381                 bear one unit out. At rest they read {reading}, 1 g within 10% in {}. \
1382                 {scale_says}",
1383                gravity.map_or("neither m/s² nor g", |g| if g == 0 {
1384                    "m/s²"
1385                } else {
1386                    "g"
1387                })
1388            ));
1389            None
1390        }
1391    }
1392}
1393
1394/// Decides the board's estimate `acc_*` by the pad's gravity and the climb (module docs).
1395fn decide_estimate(columns: &mut [Column], flight: &Flight, decisions: &mut Vec<String>) {
1396    let Some(axes) = triple(columns, Role::Estimate) else {
1397        return;
1398    };
1399    let names = listed(columns, &axes);
1400    let (gravity, reading) = gravity_unit(columns, axes);
1401    // The climb integrated in each unit: gravity is taken off in m/s², so a unit's climb isn't
1402    // another's times a factor.
1403    let ratios: Vec<Option<f64>> = ACCEL_UNITS
1404        .iter()
1405        .map(|(_, scale)| match (flight.launch, flight.apogee) {
1406            (Some((launch, _, _)), Some(apogee)) => {
1407                let integrated = integrate(
1408                    columns,
1409                    axes,
1410                    ToMs2::Scale(*scale),
1411                    &flight.time_s,
1412                    1.0,
1413                    launch,
1414                    apogee,
1415                );
1416                climb_ratio(&integrated, |row| flight.climb_m(row))
1417            }
1418            _ => None,
1419        })
1420        .collect();
1421    let climbed = (0..ACCEL_UNITS.len())
1422        .filter(|&u| ratios[u].is_some_and(|r| within_factor(r, LENGTH_FACTOR)))
1423        .collect::<Vec<_>>();
1424    let climbed = match climbed.as_slice() {
1425        [one] => Some(*one),
1426        _ => None,
1427    };
1428    let reading = reading.map_or_else(|| "nothing".to_owned(), |r| format!("{r:.3}"));
1429    let text = |r: Option<f64>| {
1430        r.map_or_else(
1431            || "none, too little to tell".to_owned(),
1432            |r| format!("{r:.3}"),
1433        )
1434    };
1435    let evidence = format!(
1436        "at rest they read {reading}. The pressure's climb from the launch over the one they \
1437         integrate to is, read in m/s², {}, and read in g, {}. A reading of 1 g within 10% and a \
1438         ratio within a factor of {LENGTH_FACTOR} of 1 are taken",
1439        text(ratios[0]),
1440        text(ratios[1])
1441    );
1442    match (gravity, climbed) {
1443        (Some(g), Some(c)) if g == c => {
1444            for at in axes {
1445                columns[at].read_as(Quantity::Acceleration, ACCEL_UNITS[g].1);
1446            }
1447            decisions.push(format!(
1448                "{names} read in {}, from the pad's gravity and the climb: {evidence}",
1449                ACCEL_UNITS[g].0
1450            ));
1451        }
1452        _ => decisions.push(format!(
1453            "{names} kept as written: the pad's gravity and the climb don't both bear one unit out: \
1454             {evidence}"
1455        )),
1456    }
1457}
1458
1459/// What the turning check found.
1460#[derive(Debug, Clone, Copy, PartialEq)]
1461struct Turning {
1462    stretches: usize,
1463    /// The accelerometer's direction's turn, rad.
1464    accelerometer_rad: f64,
1465    /// The gyroscope's turn across that direction, its rates read in rad/s, rad.
1466    gyroscope_rad: f64,
1467}
1468
1469/// The turning check (module docs), with the raw accelerometer's direction.
1470fn turning(columns: &[Column], flight: &Flight, gyro: [usize; 3]) -> Turning {
1471    let mut found = Turning {
1472        stretches: 0,
1473        accelerometer_rad: 0.0,
1474        gyroscope_rad: 0.0,
1475    };
1476    let Some((_, accel, pad)) = flight.launch else {
1477        return found;
1478    };
1479    let one_g = magnitude(pad);
1480    let times = &flight.time_s;
1481    let near_g = |row: usize| {
1482        let reading = magnitude(vector(columns, accel, row)) / one_g;
1483        reading.is_finite() && (reading - 1.0).abs() <= TURN_GRAVITY_MARGIN
1484    };
1485    let fast = |row: usize| {
1486        let step = times[row + 1] - times[row];
1487        step > 0.0
1488            && step <= FAST_STEP_S
1489            && vector(columns, gyro, row).iter().all(|v| v.is_finite())
1490    };
1491    let direction = |row: usize| {
1492        let a = vector(columns, accel, row);
1493        let m = magnitude(a);
1494        a.map(|v| v / m)
1495    };
1496    let mut start = 0;
1497    while start + TURN_STEPS < times.len() {
1498        let end = start + TURN_STEPS;
1499        if !((start..end).all(fast) && (start..=end).all(near_g)) {
1500            start += 1;
1501            continue;
1502        }
1503        let cosine = dot(direction(start), direction(end)).clamp(-1.0, 1.0);
1504        let mut gyroscope = 0.0;
1505        for row in start..end {
1506            let w = vector(columns, gyro, row);
1507            let a = direction(row);
1508            let across = [
1509                w[1] * a[2] - w[2] * a[1],
1510                w[2] * a[0] - w[0] * a[2],
1511                w[0] * a[1] - w[1] * a[0],
1512            ];
1513            gyroscope += magnitude(across) * (times[row + 1] - times[row]);
1514        }
1515        found.stretches += 1;
1516        found.accelerometer_rad += cosine.acos();
1517        found.gyroscope_rad += gyroscope;
1518        start = end;
1519    }
1520    found
1521}
1522
1523/// A gyroscope's unit: rad/s or °/s, and the factor to °/s.
1524const GYRO_UNITS: [(&str, f64); 2] = [
1525    ("radians per second", 180.0 / PI),
1526    ("degrees per second", 1.0),
1527];
1528
1529/// Which of [`GYRO_UNITS`] the turning check bears out, and its evidence.
1530fn turning_unit(columns: &[Column], flight: &Flight, gyro: [usize; 3]) -> (Option<usize>, String) {
1531    let found = turning(columns, flight, gyro);
1532    let ratio = found.gyroscope_rad / found.accelerometer_rad;
1533    let enough =
1534        found.stretches >= TURN_LEAST_STRETCHES && found.accelerometer_rad >= TURN_LEAST_RAD;
1535    let unit = GYRO_UNITS
1536        .iter()
1537        .position(|(_, to_dps)| enough && within_factor(ratio * to_dps * PI / 180.0, TURN_FACTOR));
1538    let evidence = format!(
1539        "over {} stretches of {TURN_STEPS} steps at most {FAST_STEP_S} s apart with the \
1540         accelerometer within 15% of 1 g, its direction turns {:.3} rad. Over the same \
1541         stretches, the rates turn the board across that direction {:.3} rad read in radians \
1542         per second, {:.3} rad read in degrees. The \
1543         unit whose turn is within a quarter to 4 times the accelerometer's is taken, from 3 \
1544         stretches and 0.2 rad",
1545        found.stretches,
1546        found.accelerometer_rad,
1547        found.gyroscope_rad,
1548        found.gyroscope_rad * PI / 180.0
1549    );
1550    (unit, evidence)
1551}
1552
1553/// Decides each IMU's scaled gyroscope by the board's turning and the sensor's scale, then the
1554/// IMUs' average by the turning and the mean (module docs).
1555fn decide_gyroscopes(columns: &mut [Column], flight: &Flight, decisions: &mut Vec<String>) {
1556    let mut inputs: Vec<Option<usize>> = Vec::new();
1557    for imu in [1u8, 2] {
1558        let Some(axes) = triple(columns, |a| Role::GyroReal(imu, a)) else {
1559            continue;
1560        };
1561        let names = listed(columns, &axes);
1562        if axes.iter().all(|&at| columns[at].all_zero()) {
1563            decisions.push(format!(
1564                "{names} kept as written: every value is 0, a sensor that isn't working"
1565            ));
1566            inputs.push(None);
1567            continue;
1568        }
1569        let (turned, evidence) = turning_unit(columns, flight, axes);
1570        let common = common_factor(columns, axes);
1571        let scaled = common.and_then(|factor| {
1572            GYRO_UNITS
1573                .iter()
1574                .position(|(_, to_dps)| listed_scale(factor * to_dps, &GYRO_SCALES_DPS).is_some())
1575        });
1576        let factor = common.map_or_else(
1577            || "The columns aren't their raw counts times one factor".to_owned(),
1578            |f| format!("Each column is its raw count times {f:.7}"),
1579        );
1580        match (turned, scaled) {
1581            (Some(t), Some(s)) if t == s => {
1582                for at in axes {
1583                    columns[at].read_as(Quantity::AngularRate, GYRO_UNITS[t].1);
1584                }
1585                decisions.push(format!(
1586                    "{names} read in {}, from the board's turning and the sensor's scale: \
1587                     {evidence}. {factor}, in that unit one of the counts of \
1588                     STMicroelectronics' LSM9DS1 (8.75, 17.5 or 70 m°/s)",
1589                    GYRO_UNITS[t].0
1590                ));
1591                inputs.push(Some(t));
1592            }
1593            _ => {
1594                let scale_says = match scaled {
1595                    Some(s) => format!(
1596                        "{factor}, one of the LSM9DS1's counts read in {}",
1597                        GYRO_UNITS[s].0
1598                    ),
1599                    None => format!("{factor}, none of the LSM9DS1's counts in either unit"),
1600                };
1601                decisions.push(format!(
1602                    "{names} kept as written: the board's turning and the sensor's scale don't \
1603                     agree on a unit: {evidence}. {scale_says}"
1604                ));
1605                inputs.push(None);
1606            }
1607        }
1608    }
1609    let Some(axes) = triple(columns, Role::GyroAverage) else {
1610        return;
1611    };
1612    let names = listed(columns, &axes);
1613    let same_unit = inputs
1614        .first()
1615        .copied()
1616        .flatten()
1617        .filter(|u| inputs.iter().all(|i| *i == Some(*u)));
1618    let mean = same_unit.is_some()
1619        && (0..3).all(|axis| is_mean(columns, axes[axis], |imu| Role::GyroReal(imu, axis)));
1620    let (turned, evidence) = turning_unit(columns, flight, axes);
1621    match (turned, same_unit, mean) {
1622        (Some(t), Some(input), true) if t == input => {
1623            for at in axes {
1624                columns[at].read_as(Quantity::AngularRate, GYRO_UNITS[t].1);
1625            }
1626            decisions.push(format!(
1627                "{names} read in {}, from the board's turning and the IMUs: {evidence}. Each \
1628                 is the mean of the IMUs' scaled columns, read in it",
1629                GYRO_UNITS[t].0
1630            ));
1631        }
1632        _ => {
1633            let why = if same_unit.is_none() {
1634                "the IMUs' scaled columns they average aren't all read in one unit, so neither is \
1635                 their mean"
1636            } else if !mean {
1637                "they aren't the mean of the IMUs' scaled columns"
1638            } else {
1639                "the turning doesn't bear out the IMUs' unit"
1640            };
1641            decisions.push(format!(
1642                "{names} kept as written: the board's turning and the IMUs don't both bear one \
1643                 unit out: {why}; {evidence}"
1644            ));
1645        }
1646    }
1647}
1648
1649/// The accelerometer the lengths' and the speed's checks integrate, read in a unit: `acc_*`, else
1650/// an IMU's scaled columns, else their average; with its factor to m/s².
1651fn reference_accelerometer(columns: &[Column]) -> Option<([usize; 3], f64)> {
1652    let roles: [&dyn Fn(usize) -> Role; 4] = [
1653        &Role::Estimate,
1654        &|a| Role::AccelReal(1, a),
1655        &|a| Role::AccelReal(2, a),
1656        &Role::AccelAverage,
1657    ];
1658    roles.iter().find_map(|role| {
1659        let axes = triple(columns, role)?;
1660        let column = &columns[axes[0]];
1661        match (column.quantity, column.conversion) {
1662            (Quantity::Acceleration, Conversion::Scale(scale)) => Some((axes, scale)),
1663            _ => None,
1664        }
1665    })
1666}
1667
1668/// A length's units: meters and feet, and the factor to meters.
1669const LENGTH_UNITS: [(&str, f64); 2] = [("meters", 1.0), ("feet", FOOT_M)];
1670
1671/// The accelerometer's integrated rows, if the flight and the accelerometer allow.
1672fn integrated(
1673    columns: &[Column],
1674    flight: &Flight,
1675    reference: Option<([usize; 3], f64)>,
1676) -> Vec<Integrated> {
1677    match (reference, flight.launch, flight.apogee) {
1678        (Some((axes, scale)), Some((launch, _, _)), Some(apogee)) => integrate(
1679            columns,
1680            axes,
1681            ToMs2::Scale(scale),
1682            &flight.time_s,
1683            1.0,
1684            launch,
1685            apogee,
1686        ),
1687        _ => Vec::new(),
1688    }
1689}
1690
1691/// Decides `pos_z`'s and the barometric altitudes' units (module docs); the GPS altitude's is
1692/// [`decide_gps_altitude`]'s.
1693fn decide_lengths(
1694    columns: &mut [Column],
1695    flight: &Flight,
1696    reference: Option<([usize; 3], f64)>,
1697    decisions: &mut Vec<String>,
1698) {
1699    let rows = integrated(columns, flight, reference);
1700    let pressure_climb = flight.apogee.map(|row| flight.climb_m(row));
1701    for column in columns.iter_mut() {
1702        let role = column.role;
1703        if role == Role::GpsAltitude {
1704            decide_gps_altitude(column, flight, &rows, decisions);
1705            continue;
1706        }
1707        if !matches!(role, Role::Height | Role::BaroAltitude) {
1708            continue;
1709        }
1710        let name = column.quoted();
1711        if column.all_zero() {
1712            decisions.push(format!(
1713                "{name} kept as written: every value is 0, no reading"
1714            ));
1715            continue;
1716        }
1717        let Some(pad) = column.pad() else {
1718            decisions.push(format!("{name} kept as written: no value on the pad"));
1719            continue;
1720        };
1721        // Check 1: the gain against the pressure's climb.
1722        let smooth = running_median(&column.values);
1723        let top = highest(&smooth).map(|row| smooth[row] - pad);
1724        let gain = match (top, pressure_climb) {
1725            (Some(top), Some(climb)) if climb >= LEAST_CLIMB_M => Some(top / climb),
1726            _ => None,
1727        };
1728        // Check 2: the climb against the accelerometer's.
1729        let ratio = climb_ratio(&rows, |row| column.values[row] - pad);
1730        let by_gain = LENGTH_UNITS
1731            .iter()
1732            .position(|(_, s)| gain.is_some_and(|g| within_margin(g * s, GAIN_MARGIN)));
1733        let by_climb = LENGTH_UNITS
1734            .iter()
1735            .position(|(_, s)| ratio.is_some_and(|r| within_factor(r * s, LENGTH_FACTOR)));
1736        let evidence = format!(
1737            "its climb from the pad to its highest over the pressure's in the standard atmosphere \
1738             is {} as written. Its climb from the launch over the one the accelerometer \
1739             integrates to is {}. A gain within 25% of 1 and a ratio within a factor of \
1740             {LENGTH_FACTOR} are taken",
1741            measured(gain),
1742            measured(ratio)
1743        );
1744        match (by_gain, by_climb) {
1745            (Some(g), Some(c)) if g == c => {
1746                column.read_as(Quantity::AltitudeAboveGround, LENGTH_UNITS[g].1);
1747                decisions.push(format!(
1748                    "{name} read in {}, from the pressure's climb and the accelerometer's: \
1749                     {evidence}",
1750                    LENGTH_UNITS[g].0
1751                ));
1752            }
1753            _ => decisions.push(format!(
1754                "{name} kept as written: the pressure's climb and the accelerometer's don't \
1755                 agree on a unit: {evidence}"
1756            )),
1757        }
1758    }
1759}
1760
1761/// A measured value in a note, or that there was too little to measure it.
1762fn measured(x: Option<f64>) -> String {
1763    x.map_or_else(
1764        || "none, too little to tell".to_owned(),
1765        |x| format!("{x:.3}"),
1766    )
1767}
1768
1769/// Whether a GPS altitude's row is a fresh fix: past the first row, a number other than 0, the
1770/// receiver's mark for no fix, that differs from the row before's. A value held unchanged, before
1771/// the first fix or between two, is none.
1772fn fresh_fix(values: &[f64], row: usize) -> bool {
1773    let value = values[row];
1774    row > 0 && value.is_finite() && value != 0.0 && value != values[row - 1]
1775}
1776
1777/// A least-squares line `y = intercept + slope x`, its intercept free.
1778#[derive(Debug, Clone, Copy, PartialEq)]
1779struct Line {
1780    slope: f64,
1781    intercept: f64,
1782    /// How many points it was fitted to.
1783    points: usize,
1784    /// The points' spread in x, greatest less least.
1785    span: f64,
1786}
1787
1788/// The least-squares line through points, its intercept free; `None` under `least` points or
1789/// with a spread in x under [`LEAST_CLIMB_M`].
1790fn fit_line(points: &[(f64, f64)], least: usize) -> Option<Line> {
1791    if points.is_empty() || points.len() < least {
1792        return None;
1793    }
1794    let count = points.len() as f64;
1795    let (low, high) = points
1796        .iter()
1797        .fold((f64::INFINITY, f64::NEG_INFINITY), |(lo, hi), p| {
1798            (lo.min(p.0), hi.max(p.0))
1799        });
1800    let span = high - low;
1801    // Written to fail closed: a `NaN` spread is too little.
1802    if span.is_nan() || span < LEAST_CLIMB_M {
1803        return None;
1804    }
1805    let mean_x = points.iter().map(|p| p.0).sum::<f64>() / count;
1806    let mean_y = points.iter().map(|p| p.1).sum::<f64>() / count;
1807    let (mut cross, mut square) = (0.0, 0.0);
1808    for (x, y) in points {
1809        cross += (x - mean_x) * (y - mean_y);
1810        square += (x - mean_x) * (x - mean_x);
1811    }
1812    let slope = cross / square;
1813    let intercept = mean_y - slope * mean_x;
1814    (slope.is_finite() && intercept.is_finite()).then_some(Line {
1815        slope,
1816        intercept,
1817        points: points.len(),
1818        span,
1819    })
1820}
1821
1822/// Decides the GPS altitude's unit (module docs): its rise at its fresh fixes against the
1823/// pressure's climb and against the accelerometer's, each a line fitted with its own free offset,
1824/// so that neither rests on a value the receiver wrote before its first fix; and its pad reading,
1825/// where it has one other than 0, must lie near where the first line puts the pad.
1826fn decide_gps_altitude(
1827    column: &mut Column,
1828    flight: &Flight,
1829    integrated: &[Integrated],
1830    decisions: &mut Vec<String>,
1831) {
1832    let name = column.quoted();
1833    if column.all_zero() {
1834        decisions.push(format!(
1835            "{name} kept as written: every value is 0, the receiver's mark for no fix"
1836        ));
1837        return;
1838    }
1839    let values = &column.values;
1840    // Check 1: against the pressure's climb, at the fresh fixes up to the apogee.
1841    let by_pressure: Vec<(f64, f64)> = flight.apogee.map_or_else(Vec::new, |apogee| {
1842        (0..=apogee.min(values.len().saturating_sub(1)))
1843            .filter(|&row| fresh_fix(values, row))
1844            .map(|row| (flight.climb_m(row), values[row]))
1845            .filter(|(climb, _)| climb.is_finite())
1846            .collect()
1847    });
1848    let pressure_line = fit_line(&by_pressure, 3);
1849    // Check 2: against the accelerometer's climb, at the fresh fixes from the launch.
1850    let by_accelerometer: Vec<(f64, f64)> = integrated
1851        .iter()
1852        .filter(|(row, _, _)| *row < values.len() && fresh_fix(values, *row))
1853        .map(|&(row, _, climb)| (climb, values[row]))
1854        .collect();
1855    let accelerometer_line = fit_line(&by_accelerometer, LEAST_ROWS);
1856    let gain = pressure_line.map(|line| line.slope);
1857    let ratio = accelerometer_line.map(|line| line.slope);
1858    let by_gain = LENGTH_UNITS
1859        .iter()
1860        .position(|(_, s)| gain.is_some_and(|g| within_margin(g * s, GAIN_MARGIN)));
1861    let by_climb = LENGTH_UNITS
1862        .iter()
1863        .position(|(_, s)| ratio.is_some_and(|r| within_factor(r * s, LENGTH_FACTOR)));
1864    // The pad: the median of the first rows' numbers other than 0, if any. It must lie within a
1865    // quarter of the GPS's own rise over the fixes of where the first line puts the pad, or it is
1866    // a value held from before the first fix.
1867    let pad = median(values.iter().take(PAD_ROWS).copied().filter(|v| *v != 0.0));
1868    let held = match (pad, pressure_line) {
1869        (Some(pad), Some(line)) => {
1870            let off = (pad - line.intercept).abs();
1871            let allowed = GAIN_MARGIN * (line.slope * line.span).abs();
1872            // Written to fail closed: a `NaN` distance is too far.
1873            (off.is_nan() || off > allowed).then_some((pad, line.intercept, allowed))
1874        }
1875        _ => None,
1876    };
1877    let fixes = |line: Option<Line>| line.map_or(0, |l| l.points);
1878    let evidence = format!(
1879        "its rise over the pressure's climb in the standard atmosphere is {} as written. The \
1880         rise is fitted as a line with a free offset through the column's {} fresh fixes up to \
1881         the apogee. Its rise over the climb the accelerometer integrates to is {}, from a line \
1882         through the column's {} fresh fixes from the launch. A gain within 25% of 1 and a ratio within a factor of \
1883         {LENGTH_FACTOR} are taken, each over a climb of at least {LEAST_CLIMB_M} m",
1884        measured(gain),
1885        fixes(pressure_line),
1886        measured(ratio),
1887        fixes(accelerometer_line)
1888    );
1889    match (by_gain, by_climb, held) {
1890        (_, _, Some((pad, start, allowed))) => decisions.push(format!(
1891            "{name} kept as written: its pad reading isn't where its climb starts, a value held \
1892             from before its first fix. On the pad it reads {pad}, and its fixes' line puts the \
1893             pad at {start:.1}. That is more than {allowed:.1} from the pad reading, a quarter of \
1894             its rise over the fixes; {evidence}"
1895        )),
1896        (Some(g), Some(c), None) if g == c => {
1897            column.read_as(Quantity::GnssAltitude, LENGTH_UNITS[g].1);
1898            let mut no_fix = 0usize;
1899            for value in &mut column.values {
1900                if *value == 0.0 {
1901                    *value = f64::NAN;
1902                    no_fix += 1;
1903                }
1904            }
1905            decisions.push(format!(
1906                "{name} read in {}, from the pressure's climb and the accelerometer's: {evidence}",
1907                LENGTH_UNITS[g].0
1908            ));
1909            if no_fix > 0 {
1910                decisions.push(format!(
1911                    "{no_fix} rows of {name} hold 0, the receiver's mark for no fix: each is a gap"
1912                ));
1913            }
1914        }
1915        _ => decisions.push(format!(
1916            "{name} kept as written: the pressure's climb and the accelerometer's don't agree on \
1917             a unit: {evidence}"
1918        )),
1919    }
1920}
1921
1922/// Decides `vel_z`'s unit by `pos_z`'s change and the accelerometer's speed (module docs).
1923fn decide_climb(
1924    columns: &mut [Column],
1925    flight: &Flight,
1926    reference: Option<([usize; 3], f64)>,
1927    decisions: &mut Vec<String>,
1928) {
1929    let Some(at) = columns.iter().position(|c| c.role == Role::Climb) else {
1930        return;
1931    };
1932    let name = columns[at].quoted();
1933    if columns[at].all_zero() {
1934        decisions.push(format!(
1935            "{name} kept as written: every value is 0, no reading"
1936        ));
1937        return;
1938    }
1939    // Check 1: against `pos_z`'s change, read in meters.
1940    let height = columns
1941        .iter()
1942        .position(|c| c.role == Role::Height && c.quantity == Quantity::AltitudeAboveGround);
1943    let slope = height.and_then(|h| {
1944        let Conversion::Scale(to_m) = columns[h].conversion else {
1945            return None;
1946        };
1947        let (v, z, t) = (&columns[at].values, &columns[h].values, &flight.time_s);
1948        let (mut cross, mut square, mut moved) = (0.0, 0.0, 0.0);
1949        for row in 0..t.len().saturating_sub(1) {
1950            let step = t[row + 1] - t[row];
1951            let traveled = 0.5 * (v[row] + v[row + 1]) * step;
1952            let risen = (z[row + 1] - z[row]) * to_m;
1953            if !(step > 0.0 && step <= FAST_STEP_S && traveled.is_finite() && risen.is_finite()) {
1954                continue;
1955            }
1956            cross += traveled * risen;
1957            square += traveled * traveled;
1958            moved += traveled.abs();
1959        }
1960        (moved >= SPEED_LEAST_M).then_some(cross / square)
1961    });
1962    // Check 2: against the accelerometer's speed.
1963    let rows = integrated(columns, flight, reference);
1964    let ratio = speed_ratio(&rows, |row| columns[at].values[row]);
1965    // The slope is of meters on the speed as written: in feet a second it is 0.3048.
1966    let by_slope = LENGTH_UNITS
1967        .iter()
1968        .position(|(_, s)| slope.is_some_and(|k| within_margin(k / s, GAIN_MARGIN)));
1969    let by_speed = LENGTH_UNITS
1970        .iter()
1971        .position(|(_, s)| ratio.is_some_and(|r| within_factor(r * s, LENGTH_FACTOR)));
1972    let text = |x: Option<f64>| {
1973        x.map_or_else(
1974            || "none, too little to tell".to_owned(),
1975            |x| format!("{x:.3}"),
1976        )
1977    };
1978    let evidence = format!(
1979        "over the steps at most {FAST_STEP_S} s long, `pos_z`'s change in meters is {} times what \
1980         it integrates to as written. From the launch, the column is {} times the speed the \
1981         accelerometer integrates to. A slope within 25% of 1 and a ratio within a factor of \
1982         {LENGTH_FACTOR} are taken",
1983        text(slope),
1984        text(ratio)
1985    );
1986    match (by_slope, by_speed) {
1987        (Some(s), Some(r)) if s == r => {
1988            columns[at].read_as(Quantity::Speed, LENGTH_UNITS[s].1);
1989            let unit = if s == 0 {
1990                "meters per second"
1991            } else {
1992                "feet per second"
1993            };
1994            decisions.push(format!(
1995                "{name} read in {unit}, from `pos_z` and the accelerometer: {evidence}"
1996            ));
1997        }
1998        _ => decisions.push(format!(
1999            "{name} kept as written: `pos_z` and the accelerometer don't both bear one unit out: \
2000             {evidence}"
2001        )),
2002    }
2003}
2004
2005/// How a GPS's latitudes or longitudes lie against the bound degrees reach, 90 or 180: at its
2006/// fixes, how many there are and how many pass it.
2007#[derive(Debug, Clone, Copy)]
2008struct Range {
2009    fixes: usize,
2010    beyond: usize,
2011}
2012
2013impl Range {
2014    fn of(values: impl Iterator<Item = f64>, bound: f64) -> Self {
2015        let (mut fixes, mut beyond) = (0, 0);
2016        for value in values {
2017            fixes += 1;
2018            // Written to fail closed: a `NaN` passes no bound and is never among the fixes.
2019            if value.is_nan() || value.abs() > bound {
2020                beyond += 1;
2021            }
2022        }
2023        Self { fixes, beyond }
2024    }
2025
2026    /// Whether most pass the bound, as degrees and minutes put every coordinate but those within
2027    /// 1° of the equator (a latitude) or 2° of the prime meridian (a longitude).
2028    fn says_degrees_minutes(self) -> bool {
2029        self.fixes > 0 && 2 * self.beyond > self.fixes
2030    }
2031
2032    /// Whether none passes it, as no degrees do.
2033    fn says_degrees(self) -> bool {
2034        self.fixes > 0 && self.beyond == 0
2035    }
2036}
2037
2038/// How the GPS's position is read.
2039#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2040enum Position {
2041    DegreesMinutes,
2042    Degrees,
2043}
2044
2045/// Decides the GPS's position: NMEA's degrees and minutes, or degrees, from the ranges of its
2046/// latitudes and its longitudes and, for degrees and minutes, the minutes (module docs). A
2047/// position (0, 0) is the receiver's mark for no fix: it takes no part in the checks and, where
2048/// the position is read, is a gap. `gps_lat_mod` and `gps_long_mod` are checked against the
2049/// position once it is decided, and count in no check of it. Where no value of `gps_lat` or
2050/// `gps_long` is negative, the file shows no hemisphere, and all four are kept as written.
2051fn decide_position(columns: &mut [Column], decisions: &mut Vec<String>) {
2052    let find = |role: Role| columns.iter().position(|c| c.role == role);
2053    let (Some(lat), Some(long)) = (find(Role::Latitude), find(Role::Longitude)) else {
2054        return;
2055    };
2056    let mods = match (find(Role::LatitudeMod), find(Role::LongitudeMod)) {
2057        (Some(a), Some(b)) => Some((a, b)),
2058        (Some(lone), None) | (None, Some(lone)) => {
2059            decisions.push(format!(
2060                "{} kept as written: its companion of the other coordinate is missing, and the \
2061                 two are checked as one position",
2062                columns[lone].quoted()
2063            ));
2064            None
2065        }
2066        (None, None) => None,
2067    };
2068    let names = listed(columns, &[lat, long]);
2069    let mod_names = mods.map(|(a, b)| listed(columns, &[a, b]));
2070    let keep_mods = |decisions: &mut Vec<String>| {
2071        if let Some(mod_names) = &mod_names {
2072            decisions.push(format!(
2073                "{mod_names} kept as written: {names} aren't read in a unit, and a companion is \
2074                 checked only against a position that is"
2075            ));
2076        }
2077    };
2078    let rows = columns[lat].values.len();
2079    let pair = |row: usize| (columns[lat].values[row], columns[long].values[row]);
2080    let no_fix = |row: usize| pair(row) == (0.0, 0.0);
2081    let fixed: Vec<usize> = (0..rows)
2082        .filter(|&row| {
2083            let (a, b) = pair(row);
2084            a.is_finite() && b.is_finite() && !no_fix(row)
2085        })
2086        .collect();
2087    if fixed.is_empty() {
2088        decisions.push(format!(
2089            "{names} kept as written: no row holds a fix: every position is (0, 0), the \
2090             receiver's mark for none, or a gap"
2091        ));
2092        keep_mods(decisions);
2093        return;
2094    }
2095    let latitudes = Range::of(fixed.iter().map(|&row| pair(row).0), 90.0);
2096    let longitudes = Range::of(fixed.iter().map(|&row| pair(row).1), 180.0);
2097    let fixes = fixed.len();
2098    let minutes_ok = fixed.iter().all(|&row| {
2099        let (a, b) = pair(row);
2100        a.abs() % 100.0 < 60.0 && b.abs() % 100.0 < 60.0
2101    });
2102    let converted_ok = fixed.iter().all(|&row| {
2103        let (a, b) = pair(row);
2104        degrees_minutes(a).abs() <= 90.0 && degrees_minutes(b).abs() <= 180.0
2105    });
2106    let ranges = format!(
2107        "of its {fixes} fixes, {} latitudes pass ±90 and {} longitudes ±180, which no degrees do",
2108        latitudes.beyond, longitudes.beyond
2109    );
2110    let minutes = if minutes_ok {
2111        "Every value's last two digits before the point, its minutes in degrees and minutes, \
2112         are under 60"
2113    } else {
2114        "Some value's last two digits before the point are 60 or more, which no minutes are"
2115    };
2116    // How the ranges read the position, and the note's words after the columns' names: "read
2117    // in …" where they decide a unit, "kept as written: …" where they don't.
2118    let (decided, said) = if latitudes.says_degrees_minutes() && longitudes.says_degrees_minutes() {
2119        if minutes_ok && converted_ok {
2120            (
2121                Some(Position::DegreesMinutes),
2122                format!(
2123                    "read in NMEA 0183's degrees and minutes, ddmm.mmmm, from their ranges and \
2124                     their minutes: {ranges}. Most of each pass those limits, as degrees and minutes write any \
2125                     place more than 1° from the equator and 2° from the prime meridian. \
2126                     {minutes}. And converted, every latitude lies within ±90 and every \
2127                     longitude within ±180"
2128                ),
2129            )
2130        } else {
2131            let converted = if converted_ok {
2132                ""
2133            } else {
2134                ". Converted from degrees and minutes, a latitude passes ±90 or a longitude ±180"
2135            };
2136            (
2137                None,
2138                format!(
2139                    "kept as written: their ranges say degrees and minutes, but their values \
2140                     aren't all degrees and minutes: {ranges}. {minutes}{converted}"
2141                ),
2142            )
2143        }
2144    } else if latitudes.says_degrees() && longitudes.says_degrees() {
2145        let minutes = if minutes_ok {
2146            "Each value's last two digits before the point are under 60, as degrees and \
2147             minutes' would be, so they tell nothing here"
2148        } else {
2149            "Some value's last two digits before the point are 60 or more, which no degrees and \
2150             minutes are"
2151        };
2152        (
2153            Some(Position::Degrees),
2154            format!(
2155                "read in degrees, from the latitudes' range and the longitudes': each of its \
2156                 {fixes} fixes has its latitude within ±90 and its longitude within ±180. \
2157                 Degrees and minutes would put the place within 1° of the equator and 2° of the \
2158                 prime meridian. Being within both at once happens only at sea in the Gulf of Guinea. {minutes}"
2159            ),
2160        )
2161    } else {
2162        (
2163            None,
2164            format!(
2165                "kept as written: the latitudes' range and the longitudes' don't both bear one \
2166                 unit out: {ranges}. Degrees and minutes need most of each to, and degrees none"
2167            ),
2168        )
2169    };
2170    // The hemisphere: NMEA 0183 writes it in a field of its own beside each coordinate (N or S,
2171    // E or W), and the board's columns drop it. A file that writes a negative value in either
2172    // column shows its signs; one whose every value is 0 or more can't tell a place south of
2173    // the equator or west of the prime meridian from its mirror, so it is kept as written, its
2174    // note saying how its ranges would read it. No hemisphere is guessed from anything else.
2175    let signed = [lat, long]
2176        .iter()
2177        .any(|&at| columns[at].values.iter().any(|&v| v < 0.0));
2178    if !signed {
2179        decisions.push(format!(
2180            "{names} kept as written, with no hemisphere: every value in them is 0 or more and \
2181             the file writes no hemisphere. NMEA 0183 writes the hemisphere in a field of its \
2182             own. Without a hemisphere, a place south of the equator or west of the prime meridian reads as its \
2183             mirror north or east. A person who knows the site can sign them. Unsigned, they \
2184             would be {said}"
2185        ));
2186        if let Some(mod_names) = &mod_names {
2187            decisions.push(format!(
2188                "{mod_names} kept as written, with no hemisphere: {names} are kept for want of \
2189                 one, and a companion is read only beside a position that is"
2190            ));
2191        }
2192        return;
2193    }
2194    decisions.push(format!("{names} {said}"));
2195    let Some(decided) = decided else {
2196        keep_mods(decisions);
2197        return;
2198    };
2199    let conversion = match decided {
2200        Position::DegreesMinutes => Conversion::DegreesMinutes,
2201        Position::Degrees => Conversion::Scale(1.0),
2202    };
2203    // The companions: each, at every fix, the value read as decided, within a millionth.
2204    let companions_hold = mods.is_some_and(|(lat_mod, long_mod)| {
2205        [(lat, lat_mod), (long, long_mod)]
2206            .iter()
2207            .all(|&(value, companion)| {
2208                fixed.iter().all(|&row| {
2209                    let expected = conversion.apply(columns[value].values[row]);
2210                    let held = columns[companion].values[row];
2211                    (expected - held).abs() <= SAME * expected.abs().max(1.0)
2212                })
2213            })
2214    });
2215    let blank = (0..rows).filter(|&row| no_fix(row)).collect::<Vec<_>>();
2216    let mut read = vec![lat, long];
2217    for (at, quantity) in [(lat, Quantity::Latitude), (long, Quantity::Longitude)] {
2218        columns[at].quantity = quantity;
2219        columns[at].conversion = conversion;
2220    }
2221    if let (Some((lat_mod, long_mod)), Some(mod_names)) = (mods, &mod_names) {
2222        let read_as = match decided {
2223            Position::DegreesMinutes => "read in degrees and minutes, converted to degrees",
2224            Position::Degrees => "read in degrees, as written",
2225        };
2226        if companions_hold {
2227            columns[lat_mod].read_as(Quantity::Latitude, 1.0);
2228            columns[long_mod].read_as(Quantity::Longitude, 1.0);
2229            read.extend([lat_mod, long_mod]);
2230            decisions.push(format!(
2231                "{mod_names} read in degrees, from {names}: at every fix each is its companion, \
2232                 {read_as}"
2233            ));
2234        } else {
2235            let holds = if decided == Position::Degrees
2236                && fixed.iter().all(|&row| {
2237                    let expected = degrees_minutes(columns[lat].values[row]);
2238                    let held = columns[lat_mod].values[row];
2239                    (expected - held).abs() <= SAME * expected.abs().max(1.0)
2240                }) {
2241                "they hold the values converted as if they were degrees and minutes, which they \
2242                 aren't"
2243            } else {
2244                "they aren't their companions read as decided"
2245            };
2246            decisions.push(format!(
2247                "{mod_names} kept as written: {names} are {read_as}, and {holds}"
2248            ));
2249        }
2250    }
2251    if !blank.is_empty() {
2252        for &at in &read {
2253            for &row in &blank {
2254                columns[at].values[row] = f64::NAN;
2255            }
2256        }
2257        decisions.push(format!(
2258            "{} rows of {names} hold the position (0, 0), the receiver's mark for no fix: each is \
2259             a gap",
2260            blank.len()
2261        ));
2262    }
2263}
2264
2265/// Notes on the columns kept as written that no check decides: raw counts, magnetometers,
2266/// temperatures, batteries and the columns no check settles; and on the clock column that isn't
2267/// the clock, and those the reader doesn't know.
2268fn keep_the_rest(columns: &[Column], clock: usize, decisions: &mut Vec<String>) {
2269    // The raw counts and the magnetometers, a note a sensor.
2270    let mut sensors: Vec<(String, Vec<usize>)> = Vec::new();
2271    for (at, column) in columns.iter().enumerate() {
2272        if matches!(column.role, Role::Raw | Role::Magnetic) {
2273            // The name without its axis: `imu1_mag_x_real` and its kin are `imu1_mag_real`.
2274            let group: Vec<&str> = column
2275                .name
2276                .split('_')
2277                .filter(|part| axis_of(part).is_none())
2278                .collect();
2279            let group = group.join("_");
2280            match sensors.iter_mut().find(|(g, _)| *g == group) {
2281                Some((_, at_list)) => at_list.push(at),
2282                None => sensors.push((group, vec![at])),
2283            }
2284        }
2285    }
2286    for (_, at) in &sensors {
2287        let names = listed(columns, at);
2288        let why = if columns[at[0]].role == Role::Raw {
2289            "a sensor's raw counts, which the columns named with `_real` hold scaled"
2290        } else {
2291            "a magnetometer's reading, a quantity HPR Sim doesn't hold"
2292        };
2293        decisions.push(format!("{names} kept as written: {why}"));
2294    }
2295    let mut unknown = Notes::default();
2296    for (at, column) in columns.iter().enumerate() {
2297        let name = column.quoted();
2298        match column.role {
2299            Role::Clock if at != clock => decisions.push(format!(
2300                "{name} kept as written: {}",
2301                if column.all_zero() {
2302                    "every value is 0; the other clock column holds the times"
2303                } else {
2304                    "the other clock column holds the times"
2305                }
2306            )),
2307            Role::BaroTemperature => decisions.push(format!(
2308                "{name} kept as written: {}",
2309                temperature_why(column)
2310            )),
2311            Role::Battery => decisions.push(format!(
2312                "{name} kept as written: its name states no unit, and nothing in the file checks \
2313                 one"
2314            )),
2315            Role::Unsettled => decisions.push(format!(
2316                "{name} kept as written: {}",
2317                if column.all_zero() {
2318                    "every value is 0, and its name states no unit"
2319                } else {
2320                    "its name states no unit, and nothing in the file checks one"
2321                }
2322            )),
2323            Role::Unknown => unknown.push(format!(
2324                "the column {name} isn't one this reader knows; its values are kept as written"
2325            )),
2326            _ => {}
2327        }
2328    }
2329    decisions.extend(unknown.into_vec());
2330}
2331
2332/// Why a barometer's temperature is kept as written: its name states no unit, and the values
2333/// can't tell degrees Celsius from Fahrenheit where both put them within a barometer's working
2334/// range, −40 to 85 °C (−40 to 185 °F).
2335fn temperature_why(column: &Column) -> String {
2336    if column.all_zero() {
2337        return "every value is 0, and its name states no unit".to_owned();
2338    }
2339    let numbers = column.values.iter().copied().filter(|v| v.is_finite());
2340    let (low, high) = numbers.fold((f64::INFINITY, f64::NEG_INFINITY), |(lo, hi), v| {
2341        (lo.min(v), hi.max(v))
2342    });
2343    let celsius = low >= -40.0 && high <= 85.0;
2344    let fahrenheit = low >= -40.0 && high <= 185.0;
2345    if celsius && fahrenheit {
2346        format!(
2347            "its name states no unit. Its values, {low} to {high}, lie within a barometer's \
2348             working −40 to 85 °C read in degrees Celsius or in Fahrenheit. No other column holds \
2349             a temperature to tell the two apart"
2350        )
2351    } else {
2352        format!(
2353            "its name states no unit. Its values, {low} to {high}, lie outside a barometer's \
2354             working −40 to 85 °C in degrees Celsius or in Fahrenheit. No other column holds a \
2355             temperature to check a unit by"
2356        )
2357    }
2358}
2359
2360/// The record from the decided columns: every channel, the primary track and the notes.
2361fn build(
2362    table: Table,
2363    clock: usize,
2364    flight: Flight,
2365    decisions: Vec<String>,
2366) -> Result<FlightLog, LogError> {
2367    let Table {
2368        header_line,
2369        columns,
2370        lines,
2371        gaps,
2372        first_gap,
2373        short_rows,
2374        first_short,
2375    } = table;
2376    let times = flight.time_s;
2377    if !times.iter().any(|t| t.is_finite()) {
2378        return Err(LogError::NoData { format: FORMAT });
2379    }
2380    // Every channel but the clock, in its unit; a value too large to hold once converted is
2381    // refused.
2382    let mut channels: Vec<Channel> = Vec::with_capacity(columns.len());
2383    let mut channel_of: Vec<Option<usize>> = Vec::with_capacity(columns.len());
2384    for (at, column) in columns.iter().enumerate() {
2385        if at == clock {
2386            channel_of.push(None);
2387            continue;
2388        }
2389        let mut values = Vec::with_capacity(column.values.len());
2390        for (value, line) in column.values.iter().zip(&lines) {
2391            values.push(table::finite(
2392                column.conversion.apply(*value),
2393                FORMAT,
2394                *line,
2395                &column.name,
2396            )?);
2397        }
2398        channel_of.push(Some(channels.len()));
2399        channels.push(Channel {
2400            column: column.name.clone(),
2401            quantity: column.quantity,
2402            time_s: times.clone(),
2403            values,
2404        });
2405    }
2406
2407    // The primary track: `pos_z`, read in meters, its samples in time order, the first at each
2408    // time.
2409    let mut order: Vec<usize> = (0..times.len()).filter(|&i| times[i].is_finite()).collect();
2410    let untimed = times.len() - order.len();
2411    let backwards = order
2412        .windows(2)
2413        .filter(|pair| times[pair[1]] < times[pair[0]])
2414        .count();
2415    order.sort_by(|&a, &b| times[a].total_cmp(&times[b]));
2416    let before = order.len();
2417    order.dedup_by(|later, earlier| times[*later] == times[*earlier]);
2418    let repeats = before - order.len();
2419    let track_of = |role: Role, quantity: Quantity| {
2420        columns
2421            .iter()
2422            .position(|c| c.role == role && c.quantity == quantity)
2423            .and_then(|at| channel_of[at])
2424            .map(|channel| {
2425                order
2426                    .iter()
2427                    .map(|&row| channels[channel].values[row])
2428                    .collect::<Vec<f64>>()
2429            })
2430    };
2431    let mut notes = decisions;
2432    let (time_s, altitude_m) = match track_of(Role::Height, Quantity::AltitudeAboveGround) {
2433        Some(altitude) => (order.iter().map(|&row| times[row]).collect(), altitude),
2434        None => {
2435            notes.push(
2436                "the record's primary track is empty: `pos_z`, the board's altitude, isn't read \
2437                 in a unit; every column is kept as a channel"
2438                    .to_owned(),
2439            );
2440            (Vec::new(), Vec::new())
2441        }
2442    };
2443    let vertical_speed_m_s = if time_s.is_empty() {
2444        None
2445    } else {
2446        track_of(Role::Climb, Quantity::Speed)
2447    };
2448    if backwards > 0 || repeats > 0 || untimed > 0 {
2449        notes.push(format!(
2450            "the clock goes back {backwards} times and repeats a time {repeats} times, and \
2451             {untimed} samples have no time. The record's track takes the samples in time order, \
2452             the first at each time, and leaves out those without one. The channels keep them all"
2453        ));
2454    }
2455    if let Some(ground) = altitude_m.iter().copied().find(|v| v.is_finite())
2456        && ground.abs() > GROUND_OFF_M
2457    {
2458        notes.push(format!(
2459            "`pos_z` reads {ground:.2} m at the track's first sample, not near zero: the record \
2460             starts away from the board's zero"
2461        ));
2462    }
2463    let mut line_notes = Notes::default();
2464    if gaps > 0 {
2465        line_notes.push(format!(
2466            "{gaps} cells, the first on line {}, are empty or not numbers: each is a gap",
2467            first_gap.unwrap_or(header_line)
2468        ));
2469    }
2470    if short_rows > 0 {
2471        line_notes.push(format!(
2472            "{short_rows} rows, the first on line {}, hold fewer cells than the header names: the \
2473             cells they leave out are gaps",
2474            first_short.unwrap_or(header_line)
2475        ));
2476    }
2477    notes.extend(line_notes.into_vec());
2478
2479    Ok(FlightLog {
2480        format: LogFormat::AerospaceNuFcbCsv,
2481        logger: LOGGER.to_owned(),
2482        serial_number: None,
2483        firmware: None,
2484        flight_number: None,
2485        stated: Stated::default(),
2486        time_s,
2487        altitude_m,
2488        vertical_speed_m_s,
2489        temperature_k: None,
2490        battery_v: None,
2491        channels,
2492        events: Vec::new(),
2493        notes,
2494    })
2495}
2496
2497#[cfg(test)]
2498mod tests {
2499    use super::*;
2500
2501    /// A pressure column from cells as written.
2502    fn pressure(cells: &[String]) -> Column {
2503        let mut column = Column::new("baro1_pres", Role::Pressure);
2504        for cell in cells {
2505            let value: f64 = cell.parse().unwrap();
2506            column.values.push(value);
2507            column.places.push(Some(decimal_places(cell)));
2508        }
2509        column
2510    }
2511
2512    /// Whole pascals from 97,000 up, times `1 / per`, written as Rust writes the nearest `f64`,
2513    /// as the club's tools write theirs: to 15 or more digits where the product isn't short.
2514    fn whole_pascals(per: f64, half: bool) -> Vec<String> {
2515        (0..40)
2516            .map(|k| {
2517                let pa = 97_000.0 + f64::from(k) * 7.0 + if half { 0.5 } else { 0.0 };
2518                format!("{:?}", pa * (1.0 / per))
2519            })
2520            .collect()
2521    }
2522
2523    #[test]
2524    fn degrees_and_minutes_become_degrees_with_their_sign() {
2525        assert_eq!(degrees_minutes(4230.0), 42.5);
2526        assert_eq!(degrees_minutes(-7145.0), -71.75);
2527        assert_eq!(degrees_minutes(15.0), 0.25);
2528        assert_eq!(degrees_minutes(0.0), 0.0);
2529    }
2530
2531    #[test]
2532    fn a_cells_last_decimal_place_counts_its_exponent() {
2533        assert_eq!(decimal_places("0.9707179866765359"), 16);
2534        assert_eq!(decimal_places("12"), 0);
2535        assert_eq!(decimal_places("1.5e-3"), 4);
2536        assert_eq!(decimal_places("1.25E2"), 0);
2537        assert_eq!(decimal_places(" 3.250 "), 3);
2538    }
2539
2540    #[test]
2541    fn columns_take_their_roles_from_their_names() {
2542        assert_eq!(role_of("timestamp_s"), Role::Clock);
2543        assert_eq!(role_of("imu2_accel_y_real"), Role::AccelReal(2, 1));
2544        assert_eq!(role_of("imu1_gyro_z_real"), Role::GyroReal(1, 2));
2545        assert_eq!(role_of("imu_accel_x_avg"), Role::AccelAverage(0));
2546        assert_eq!(role_of("imu_mag_x_avg"), Role::Magnetic);
2547        assert_eq!(role_of("imu1_mag_y_real"), Role::Magnetic);
2548        assert_eq!(role_of("high_g_accel_z_real"), Role::HighG(2));
2549        assert_eq!(role_of("high_g_accel_z"), Role::Raw);
2550        assert_eq!(role_of("imu1_accel_w"), Role::Unknown);
2551        assert_eq!(role_of("imu3_accel_x_real"), Role::Unknown);
2552        assert_eq!(role_of("acc_z"), Role::Estimate(2));
2553        assert_eq!(role_of("state"), Role::Number);
2554        assert_eq!(role_of("baroalt_agl"), Role::BaroAltitude);
2555        assert_eq!(role_of("mystery"), Role::Unknown);
2556    }
2557
2558    #[test]
2559    fn recognition_needs_the_empty_first_name_and_a_known_column() {
2560        assert!(recognizes(",packetType,timestamp_ms,imu1_accel_x\n"));
2561        assert!(recognizes("\u{feff}\n,timestamp_s,timestamp_ms\n"));
2562        assert!(!recognizes("packetType,timestamp_ms\n"));
2563        assert!(!recognizes(",time,altitude\n"));
2564    }
2565
2566    /// Atmospheres' step holds whole pascals; bars' doesn't, nor the reverse.
2567    #[test]
2568    fn whole_pascals_lie_on_atmospheres_step_and_not_on_bars() {
2569        let atm = pressure(&whole_pascals(ATM_PA, false));
2570        assert_eq!(on_step(&atm, ATM_PA), (Some(1), 40));
2571        assert_eq!(on_step(&atm, BAR_PA).0, None);
2572        let bar = pressure(&whole_pascals(BAR_PA, false));
2573        assert_eq!(on_step(&bar, BAR_PA).0, Some(1));
2574        assert_eq!(on_step(&bar, ATM_PA).0, None);
2575        // An average of two whole readings: half steps.
2576        let half = pressure(&whole_pascals(ATM_PA, true));
2577        assert_eq!(on_step(&half, ATM_PA), (Some(2), 40));
2578    }
2579
2580    /// The tolerance is a millionth of a step: a cell 10⁻⁵ of a pascal off is off, as a f64's
2581    /// printing, 10⁻¹¹ of one, never is.
2582    #[test]
2583    fn the_step_tolerance_is_a_millionth_of_a_step() {
2584        let mut cells = whole_pascals(ATM_PA, false);
2585        cells[7] = format!("{:?}", (97_049.0 + 1e-5) / ATM_PA);
2586        assert_eq!(on_step(&pressure(&cells), ATM_PA).0, None);
2587        cells[7] = format!("{:?}", (97_049.0 + 1e-7) / ATM_PA);
2588        assert_eq!(on_step(&pressure(&cells), ATM_PA).0, Some(1));
2589    }
2590
2591    /// A cell too short to test is left out, as is one too large for its printing to place it on
2592    /// a step (the record refuses it once converted); under 10 tested cells decide nothing.
2593    #[test]
2594    fn short_cells_are_not_tested_and_overflow_fails_closed() {
2595        let short: Vec<String> = whole_pascals(ATM_PA, false)
2596            .iter()
2597            .map(|c| format!("{:.6}", c.parse::<f64>().unwrap()))
2598            .collect();
2599        assert_eq!(on_step(&pressure(&short), ATM_PA), (None, 0));
2600        let few = pressure(&whole_pascals(ATM_PA, false)[..9]);
2601        assert_eq!(on_step(&few, ATM_PA), (None, 9));
2602        let mut cells = whole_pascals(ATM_PA, false);
2603        let tested = on_step(&pressure(&cells), ATM_PA).1;
2604        cells[3] = "1.7e308".to_owned();
2605        assert_eq!(on_step(&pressure(&cells), ATM_PA), (Some(1), tested - 1));
2606    }
2607
2608    #[test]
2609    fn a_pressure_is_read_in_atmospheres_on_its_size_and_its_step() {
2610        let mut atm = pressure(&whole_pascals(ATM_PA, false));
2611        let note = decide_pressure(&mut atm);
2612        assert!(note.starts_with("`baro1_pres` read in atmospheres, from its size and its step: "));
2613        assert_eq!(atm.quantity, Quantity::Pressure);
2614        assert_eq!(atm.conversion.apply(atm.values[0]), 97_000.0);
2615
2616        let mut bar = pressure(&whole_pascals(BAR_PA, false));
2617        let note = decide_pressure(&mut bar);
2618        assert!(
2619            note.contains("Its values lie on bars' steps of 1/100,000"),
2620            "{note}"
2621        );
2622        assert_eq!(bar.quantity, Quantity::Unrecognized);
2623
2624        let pascals: Vec<String> = (0..40).map(|k| format!("{}.0", 97_000 + k)).collect();
2625        let mut pa = pressure(&pascals);
2626        let note = decide_pressure(&mut pa);
2627        assert!(
2628            note.contains("outside the 0.5 to 1.1 of atmospheres or bars"),
2629            "{note}"
2630        );
2631        assert_eq!(pa.quantity, Quantity::Unrecognized);
2632    }
2633
2634    /// A scaled cell where its count is 0 must be 0; an axis whose counts are all 0 fits any
2635    /// factor.
2636    #[test]
2637    fn a_factor_holds_at_every_count() {
2638        let column = |name: &str, values: &[f64]| {
2639            let mut c = Column::new(name, role_of(name));
2640            c.values = values.to_vec();
2641            c
2642        };
2643        let raw = column("imu1_accel_x", &[0.0, 2.0, 4.0]);
2644        assert_eq!(
2645            factor_of(&column("imu1_accel_x_real", &[0.0, 1.0, 2.0]), &raw),
2646            Some(Some(0.5))
2647        );
2648        assert_eq!(
2649            factor_of(&column("imu1_accel_x_real", &[0.1, 1.0, 2.0]), &raw),
2650            None
2651        );
2652        let zeros = column("imu1_accel_y", &[0.0; 3]);
2653        assert_eq!(
2654            factor_of(&column("imu1_accel_y_real", &[0.0; 3]), &zeros),
2655            Some(None)
2656        );
2657    }
2658
2659    #[test]
2660    fn nan_is_within_no_factor_or_margin() {
2661        assert!(!within_factor(f64::NAN, 10.0));
2662        assert!(!within_margin(f64::NAN, 0.25));
2663        assert!(within_factor(0.11, 10.0) && !within_factor(0.09, 10.0));
2664    }
2665
2666    /// A line with a free intercept takes the slope whatever the offset: a site 1,500 m up
2667    /// written in feet still rises 3.28 ft a meter. Too few points or too short a climb fit none.
2668    #[test]
2669    fn a_free_intercept_takes_the_slope_whatever_the_offset() {
2670        let points: Vec<(f64, f64)> = (0..10)
2671            .map(|i| {
2672                let climb = 30.0 + 20.0 * f64::from(i);
2673                (climb, (1_500.0 + climb) / FOOT_M)
2674            })
2675            .collect();
2676        let line = fit_line(&points, 3).unwrap();
2677        assert!((line.slope - 1.0 / FOOT_M).abs() < 1e-9, "{line:?}");
2678        assert!((line.intercept - 1_500.0 / FOOT_M).abs() < 1e-6, "{line:?}");
2679        assert_eq!((line.points, line.span), (10, 180.0));
2680        assert!(fit_line(&points[..2], 3).is_none());
2681        assert!(
2682            fit_line(&points[..2], 2).is_none(),
2683            "a 20 m climb is under 30 m"
2684        );
2685        assert!(fit_line(&points[..3], 3).is_some());
2686        assert!(fit_line(&[(f64::NAN, 1.0); 5], 3).is_none());
2687    }
2688
2689    /// A GPS altitude's fresh fix is a number other than 0 that differs from the row before's.
2690    #[test]
2691    fn a_fresh_fix_is_a_new_number_other_than_0() {
2692        let values = [0.0, 0.0, 250.0, 250.0, 251.0, f64::NAN, 251.0, 0.0];
2693        let fresh: Vec<usize> = (0..values.len())
2694            .filter(|&row| fresh_fix(&values, row))
2695            .collect();
2696        assert_eq!(fresh, [2, 4, 6]);
2697    }
2698}