Skip to main content

hpr_flightdata/
phone_launch.rs

1//! A phone app's `launch….csv`: one row a sample, from the phone's GPS, accelerometer, gyroscope,
2//! magnetometer and, in the newer files, its barometer.
3//!
4//! The app isn't named in its files, and no published description of the format was found. Its
5//! files are named `launch` and a number, and open on a header row whose first names are
6//! `Seconds,Timestamp,AltitudeMSLGPS`:
7//!
8//! ```text
9//! Seconds,Timestamp,AltitudeMSLGPS,Record,Static,Latitude,Longitude,GTotal,Gx,Gy,Gz,GMax,…
10//! 0.000,1941868800000,100,1,12,40.123456,-100.234567,1.000000,0.000000,0.932039,0.362358,…
11//! ```
12//!
13//! Two generations are among the project's sample logs, as of 2026-10-13 27 files: 19 older ones of
14//! 35 columns (8 of them with `Reserved1` and `Reserved2` where the others have `Revolutions` and
15//! `RevApogee`), whose `AltitudeRelativeBarometer` is 0 on every row, and 8 newer ones of 67 or 69
16//! columns, which add a barometer (`hPa`, and a barometric altitude and speed that move), a
17//! temperature, the acceleration with gravity taken out, a satellite count and an external
18//! sensor's columns. The rows come about 20 times a second; the GPS's columns change about once a
19//! second and repeat their last fix between.
20//!
21//! **Recognition.** The first line that isn't blank opens with `Seconds,Timestamp,AltitudeMSLGPS`:
22//! no other layout among the project's sample logs does.
23//!
24//! **Rows.** Every row is as wide as the header; a row of another width, a cell that isn't a
25//! number, a row without a time or a time that doesn't increase is refused, naming its line. An
26//! empty cell is a gap. Blank lines are skipped; lines may end in CR, LF or both, and a byte-order
27//! mark is dropped. A name the header gives twice, or leaves empty, is refused.
28//!
29//! **Columns.** `Seconds` is the clock: seconds from the app's start, as its name says, and the
30//! log's zero is the app's. Every other column is a channel on that clock, in the file's order.
31//! No source states the other columns' units, so each is read in a unit only where two
32//! independent checks on the file's own values agree on it ([ADR-258][adr-258] §2); otherwise it
33//! is kept as written, [`Quantity::Unrecognized`], with a note giving the evidence. A column the
34//! reader doesn't know is kept as written too.
35//!
36//! [adr-258]: https://github.com/nrdptel/fusionspace-eridanus/blob/main/docs/decisions/0258-aerospacenus-board-and-a-phone-apps-csv-before-the-rest.md
37//!
38//! | Column | Read as | Checks (below) |
39//! |---|---|---|
40//! | `Seconds` | the clock, s | its name |
41//! | `Timestamp` | [`Quantity::TimeOfDay`], UTC: a Unix time in ms | its steps against the clock's; its date |
42//! | `hPa` | [`Quantity::Pressure`], hPa × 100 Pa | its name; the pad's pressure a launch site's |
43//! | `AltitudeRelativeBarometer` | [`Quantity::AltitudeAboveGround`], m: the primary track | the pressure altitude of `hPa`; gravity in the coast |
44//! | `VelocityBarometer`, `VelocityBarometerRaw` | [`Quantity::Speed`], m/s: the first the primary track's | the barometric altitude's rate; gravity in the coast |
45//! | `Gx`, `Gy`, `Gz`, `GTotal` | [`Quantity::Acceleration`], g × 9.806 65 m/s² | 1 g at rest; the boost against the barometric climb |
46//! | `NoGravGx`, `NoGravGy`, `NoGravGz`, `NoGravGTotal` | [`Quantity::Acceleration`], g, gravity taken out | the accelerometer's unit and gravity's 1 g between them |
47//! | `Yaw` | [`Quantity::Angle`], radians × 180/π | within ±π; the magnetic field's direction |
48//! | `Roll` | [`Quantity::Angle`], radians × 180/π | within ±π; gravity's direction |
49//! | `Pitch` | [`Quantity::Angle`], radians × 180/π | within ±π/2; gravity's direction |
50//! | `GyroX`, `GyroY`, `GyroZ` | [`Quantity::AngularRate`], rad/s × 180/π | the attitude's turn; a full scale in °/s |
51//! | `Heading` | [`Quantity::Angle`], degrees | within 0 to 360 and past 2π; its wrap's period, 360 |
52//! | `Latitude`, `Longitude` | [`Quantity::Latitude`], [`Quantity::Longitude`], degrees, both or neither | the latitude past π/2 and within 90; the longitude past π and within 180 |
53//! | `AltitudeMSLGPS`, `AltitudeMSLMaxGPS` | [`Quantity::Unrecognized`], as written | no second check (below) |
54//! | `Record`, `Static`, `UsbState`, `Satellites` | [`Quantity::Number`], as written | flags, a state and a count |
55//! | `GMax`, `NoGravGMax`, `AltitudeRelativeMaxBarometer`, `VelocityBarometerMax`, `VelocityBarometerMin` | [`Quantity::Unrecognized`], as written | running extremes, not tied to their columns |
56//! | `Temperature` | [`Quantity::Unrecognized`], as written | °C and °F both possible |
57//! | `MagnetometerX`, `…Y`, `…Z`, `HeadingX`, `…Y`, `…Z` | [`Quantity::Unrecognized`], as written | no quantity for a magnetic field |
58//! | `Revolutions`, `RevApogee`, `RevolutionsClock`, `HorizontalResGPS`, `VerticalResGPS`, `Battery`, `AltitudeRelativeSerial`, `AltitudeRelativeMaxSerial`, `Usb…`, `Reserved…` | [`Quantity::Unrecognized`], as written | no source states their units |
59//!
60//! **The checks.** The two checks of a pair read different numbers: two columns, a column against
61//! another of settled unit or direction, or one column's extent against its steps. Each item below
62//! says what each check reads and which rival units it rules out on its own. A check the file
63//! can't run (too few rows, no barometer, no rest) counts as failed.
64//! - *The pressure.* `hPa` names its unit. The first reading, on the pad, must also be a launch
65//!   site's, 500 to 1,100 hPa, the bounds [`crate::rrc3`] cites (the U.S. Standard Atmosphere's
66//!   pressure about 5,570 m up, and above the highest sea-level pressure on record); otherwise the
67//!   column is kept as written and everything below that needs it fails.
68//! - *The barometric altitude, in meters.* (1) Above the first row it is the pressure altitude of
69//!   `hPa` above the first row's, in the U.S. Standard Atmosphere, 1976 ([`Ussa76`]), within 3 m
70//!   and 2% of the climb, over a climb of at least 30 m: in feet it would be 3.28 times as large.
71//!   (2) In the coast, from 8 s to 3 s before the highest altitude and no earlier than a second
72//!   after the steepest climb, nothing pushes the rocket, and gravity and drag slow it by at
73//!   least 1 g: a parabola fitted to the altitude there must fall at 0.7 g to 0.7 × 3.28 g, so
74//!   that an altitude in feet, falling at 1 g or more in feet per second squared, can't pass. A
75//!   rocket with more drag than that fails closed. As of 2026-10-13, in the 8 newer sample files,
76//!   (1) holds within 1.5 m in all 8 and (2) gives 1.09 g to 1.66 g in the 7 with a coast in the
77//!   file; the eighth starts after its coast.
78//! - *The barometric speeds, in m/s.* (1) Against the barometric altitude's rate, over half a
79//!   second about each row, a least-squares gain of 0.8 to 1.25 (ft/s, mph and km/h are 3.28,
80//!   2.24 and 3.6); (2) in the same coast window, the column falls at 0.7 g to 0.7 × 3.28 g. As of
81//!   2026-10-13, gains of 0.89 to 0.96 and slowings of 1.04 g to 1.66 g in the 7.
82//! - *The accelerations, in g.* (1) The quietest second before the steepest climb, the pad,
83//!   reads 0.9 to 1.1 in the axes' magnitude, as an accelerometer at rest reads 1 g (in m/s², 9.8);
84//!   (2) from that second to the steepest climb, the magnitude less 1 g, times 9.806 65 m/s² and
85//!   summed over time, the speed the boost gave, comes to 0.5 to 2 times the barometric climb rate
86//!   there, an independent sensor's speed. `GTotal` is read with the axes where it is their
87//!   magnitude within 0.11 on every row. The newer files' `NoGrav…` columns are read in g with the
88//!   axes where the median magnitude of the axes less them, the gravity the phone took out, is
89//!   0.95 to 1.05. As of 2026-10-13, (1) gives 1.00 to 1.08 and (2) 0.83 to 1.12 in 6 of the 8
90//!   newer files; of the other two, one reads 1.19 in the second before its climb, the file
91//!   starting at the launch, and one has no barometric climb. The older files have no barometer,
92//!   so (2) can't run: their accelerations, which in 18 of the 19 read 0.94 to 1.06 at their
93//!   quietest second and clip near 2 on each axis, are kept as written. The `NoGrav…` columns'
94//!   median gives 1.00 to 1.12 in the 8, 6 of them within 0.95 to 1.05.
95//! - *The attitude, in radians.* Each angle: (1) every reading within ±π (the pitch, ±π/2), the
96//!   angle's extent, which a column in degrees or gradians fails once the phone turns past 3.14 of
97//!   them from 0; (2) its direction against a sensor that measures it with no unit, which rules out
98//!   every unit but radians on its own. On the rows where the accelerometer reads its rest, within
99//!   3% of the magnitude of its quietest second (before the steepest climb where the barometer
100//!   gives one, else in the whole file), the accelerometer's axes point up whatever their unit, and
101//!   the magnetometer's field, taken across up, points east whatever its unit. The angle read in
102//!   radians must lie from what they give by a median of at most 0.2 rad over at least 20 rows,
103//!   and by at most half the median the angle read in degrees gives. Rows where the angle lies
104//!   within 0.2 of 0 (for the roll, of a half-turn), where an angle in degrees reads nearly as one
105//!   in radians, are left out. The angles are those of Android's `SensorManager.getOrientation`
106//!   (Android API reference). `Pitch`, about x: |sin pitch| is gravity's share along the phone's y
107//!   axis, `|Gy|` over the axes' magnitude. `Roll`, about y: its distance from the nearest
108//!   half-turn is gravity's angle about y, atan(`|Gx|` / `|Gz|`), on rows with the y axis at least
109//!   0.2 from vertical. Neither uses gravity's sign, so axes that point the other way pass too.
110//!   `Yaw`, the azimuth: the angle from magnetic north toward east of the phone's y axis, with east
111//!   the field across up and north up across east, on rows with the y axis at least 0.2 from
112//!   vertical. As of 2026-10-13, (2) gives medians of 0.17 rad or less for the yaw in 10 files, the
113//!   8 newer and 2 of the 19 older; in 16 of the other older ones the yaw lies a median 0.90 to
114//!   3.02 rad from the field's azimuth, and one has too few rows, so their yaw is kept as written.
115//!   For the roll, 0.07 rad or less in 24 files; 0.34 and 0.36 rad in two, and too few rows in
116//!   one, kept.
117//! - *The gyroscope, in rad/s.* (1) With the attitude read in radians, the turn between rows five
118//!   apart (the angle of the rotation from one attitude to the other, each yaw about z, then pitch
119//!   about x, then roll about y) is 0.7 to 1.3 times the gyroscope's rate summed over those rows,
120//!   by the median over at least 10 such spans, those with an axis at its full scale left out; in
121//!   degrees per second it would be 57 times smaller. (2) An axis sits at its largest reading on at
122//!   least 5 rows, its full scale, and that is within 1% of one a MEMS gyroscope offers, ±125,
123//!   ±250, ±500, ±1,000 or ±2,000 °/s (Bosch Sensortec, *BMI160 data sheet*, BST-BMI160-DS000,
124//!   table 2), read in rad/s. As of 2026-10-13, (1) gives 0.80 to 1.02 in the 9 files whose
125//!   attitude is read whole, and the newer files' gyroscopes stop at 1,000 °/s; the older ones'
126//!   never stop at a full scale, and are kept as written.
127//! - *The heading, in degrees.* (1) Its extent: every reading within 0 to 360 and one past 2π.
128//!   Radians, which never pass 2π, always fail it; a unit that runs to more than 360, gradians to
129//!   400, fails it once the heading passes 360 of it. (2) Its period, from its steps: some step
130//!   between rows crosses north by more than 300 and, less 360 in its direction, turns as the
131//!   steps on either side do, of their sign and within a factor of 2 of each. Crossing north, a
132//!   heading steps by its period less its turn in that row. Radians can't step 300; a heading in
133//!   gradians steps 400 less its turn, and less 360 that turns against the rows on either side
134//!   unless the heading turns 80 to 100 gradians a row, 4 to 5 turns a second at 20 rows a second.
135//! - *The position, in degrees.* `Latitude` and `Longitude` are decided together, both read or
136//!   both kept, on two columns' extents: (1) the latitude's largest reading from 0 lies past π/2,
137//!   which radians never pass, and within 90; (2) the longitude's past π and within 180. Radians
138//!   fail both, as do scaled integers (such as 10⁻⁷°). NMEA 0183's degrees and minutes run
139//!   together (4230.0 for 42.5°), and minutes of arc, pass the latitude's only within 1.5° of the
140//!   equator and the longitude's only within 3° of the prime meridian: the pair passes both only
141//!   for a site in the open sea of the Gulf of Guinea. A site in degrees within 1.57° of the
142//!   equator or 3.14° of the prime meridian fails closed, both kept. Neither check can separate a
143//!   unit within about 11% of the degree: the gradian, 0.9°, passes both below 81° of latitude and
144//!   162° of longitude. No check in the file rules it out; the reader takes degrees over it
145//!   because phones' location services give positions in degrees (Android's
146//!   `Location.getLatitude`, Apple's `CLLocationDegrees`).
147//! - *The time stamp, a Unix time in ms.* (1) Each step between rows, divided by 1,000, is the
148//!   clock's step, `Seconds`, within 1.5 ms; (2) the first, read so, falls between 2000 and 2100, where
149//!   read in seconds it would be tens of thousands of years on. It is kept as the time of day in
150//!   UTC, [`Quantity::TimeOfDay`], s after midnight.
151//! - *The GPS altitude, kept as written.* Against the pressure altitude at the GPS's fresh fixes
152//!   (the rows where the altitude or position changes), a least-squares gain near 1 says meters
153//!   and 3.28 feet, but that is one check, and the files hold no second. Fixes once a second, and
154//!   lagging in flight, can't show gravity's pull in the coast. On the pad, the GPS's height above
155//!   sea level against the pad's pressure altitude separates the units only on a high pad. The
156//!   extremes of sea-level pressure on record, 870 to 1,083.8 hPa (WMO, *Records of Weather and
157//!   Climate Extremes*), move the pressure altitude about −570 m to +1,270 m from the height, a
158//!   band 1,840 m wide (wider with the GPS's own error). A reading `N` is `N` m read in meters and
159//!   0.3048 `N` m read in feet, 0.695 `N` m apart, so both readings fall in the band for some
160//!   weather unless `N` tops 1,840 / 0.695, about 2,650: a pad above about 2,650 m if the column is
161//!   in meters, or above 810 m if it is in feet. That check isn't used: no sample file's pad reads
162//!   that high, so it would settle none of them, and the gain it would pair with has no pass band
163//!   of its own yet. The note gives the gain where the file has a barometer. As of 2026-10-13, the
164//!   gain is 0.69 to 1.11 in 7 of the 8 newer files; in the eighth the GPS didn't follow the
165//!   flight.
166//!
167//! **The primary track.** The barometric altitude, where it is read in meters, on every row, its
168//! vertical speed `VelocityBarometer` where that is read in m/s. Otherwise the track is empty, as a
169//! GPS log's is ([ADR-254][adr-254] §2), and the readings withhold on the log; the channels keep
170//! every column. The temperature and battery tracks are always empty: neither column's unit is
171//! settled.
172//!
173//! [adr-254]: https://github.com/nrdptel/fusionspace-eridanus/blob/main/docs/decisions/0254-gps-tracks-first-among-the-other-importers.md
174//!
175//! **The altitude's step.** `hPa` is written from a 32-bit float, which between 512 and 1,024 hPa
176//! steps by 2⁻¹⁴ hPa: every pressure in the 8 newer sample files is a whole number of those. That
177//! is 0.000 51 m of altitude at sea level, [`ALTITUDE_STEP_M`], the barometer's step; the
178//! barometer's own noise is far larger. The track, `AltitudeRelativeBarometer`, is written in finer
179//! steps of its own, as small as 0.25 mm in those files.
180//!
181//! **Values too large.** A cell is refused if its unit's conversion to SI makes it infinite, such
182//! as `1.7e308` in `hPa` ([ADR-258][adr-258] §3).
183
184use std::collections::HashSet;
185
186use hpr_atmos::ussa76::Ussa76;
187
188use crate::error::LogError;
189use crate::log::{Channel, FlightLog, LogFormat, Quantity, Stated};
190use crate::table::{self, Notes, numbered_lines, short};
191
192/// The format's name in errors.
193const FORMAT: &str = "phone app launch CSV";
194
195/// The pressure's step, hPa: a 32-bit float's between 512 and 1,024 hPa, 2⁻¹⁴ hPa (module docs).
196const PRESSURE_STEP_HPA: f64 = 1.0 / 16_384.0;
197
198/// The barometer's step as an altitude, m: the `hPa` column's step, 2⁻¹⁴ hPa, at sea level, where
199/// a pascal is `1 / (ρ₀ g₀)` m with the International Standard Atmosphere's ρ₀ = 1.225 kg/m³ (ISO
200/// 2533:1975) and standard gravity g₀ (CGPM 1901): 0.000 508 m. It is the barometer's step, not the
201/// track's: `AltitudeRelativeBarometer` is written in finer steps, as small as 0.25 mm.
202pub const ALTITUDE_STEP_M: f64 = PRESSURE_STEP_HPA * 100.0 / (1.225 * G0_M_S2);
203
204/// Standard gravity, m/s² (CGPM 1901).
205const G0_M_S2: f64 = 9.806_65;
206
207/// A foot, m (the international foot, 1959).
208const FOOT_M: f64 = 0.3048;
209
210/// The pad's pressure a launch site can have, hPa (module docs; [`crate::rrc3`]'s bounds).
211const PAD_HPA: (f64, f64) = (500.0, 1_100.0);
212
213/// The least climb, m, of the pressure altitude over which the barometric altitude is compared.
214const MIN_CLIMB_M: f64 = 30.0;
215
216/// How far, m and as a share of the climb, the barometric altitude may lie from the pressure
217/// altitude.
218const PRESSURE_MATCH: (f64, f64) = (3.0, 0.02);
219
220/// The coast window, s before the highest altitude: from, to.
221const COAST_WINDOW_S: (f64, f64) = (8.0, 3.0);
222
223/// The least length, s, and rows of the coast window.
224const MIN_COAST: (f64, usize) = (2.0, 10);
225
226/// The span, s, over which the steepest climb is measured; its pairs of rows are at least half of
227/// it apart.
228const CLIMB_SPAN_S: f64 = 1.0;
229
230/// The share of 1 g the coast must slow by at least, in meters; and of 1 g in feet, at most.
231const COAST_SLACK: f64 = 0.7;
232
233/// The gain of a barometric speed against the altitude's rate that reads it in m/s.
234const SPEED_GAIN: (f64, f64) = (0.8, 1.25);
235
236/// Half the span, s, over which the altitude's rate is taken about each row.
237const RATE_HALF_SPAN_S: f64 = 0.25;
238
239/// The quiet span, s, and its least rows, for the accelerometer at rest.
240const REST: (f64, usize) = (1.0, 5);
241
242/// The magnitude, g, an accelerometer at rest reads.
243const REST_G: (f64, f64) = (0.9, 1.1);
244
245/// The boost's speed over the barometric climb rate that reads the accelerometer in g.
246const BOOST_RATIO: (f64, f64) = (0.5, 2.0);
247
248/// How far `GTotal` may lie from the axes' magnitude, g, to be read with them.
249const TOTAL_TIE_G: f64 = 0.11;
250
251/// The median magnitude of the axes less the `NoGrav…` axes, g, that reads those in g.
252const GRAVITY_TIE_G: (f64, f64) = (0.95, 1.05);
253
254/// The attitude's direction checks, gravity's for the pitch and roll and the magnetic field's for
255/// the yaw: the rest band's share, the least angle, rad, a row's angle and axis must keep from
256/// where the comparison says nothing (module docs), the largest median difference, rad, and the
257/// least rows.
258const ATTITUDE_CHECK: (f64, f64, f64, usize) = (0.03, 0.2, 0.2, 20);
259
260/// The gyroscope's rotation check: rows a span, the summed turn's bounds, rad, the ratio's bounds
261/// and the least spans.
262const GYRO_SPAN_ROWS: usize = 5;
263const GYRO_TURN_RAD: (f64, f64) = (0.1, 2.5);
264const GYRO_RATIO: (f64, f64) = (0.7, 1.3);
265const GYRO_MIN_SPANS: usize = 10;
266
267/// A gyroscope axis within this share of its largest reading is at its full scale.
268const FULL_SCALE_SHARE: f64 = 0.99;
269
270/// The least rows at the full scale, how near, as a share, a reading must be to the largest to be
271/// at it, and the least of them in a row: a sensor at its rail holds there, where a turning axis
272/// only passes its peak.
273const PLATEAU: (usize, f64, usize) = (5, 0.001, 2);
274
275/// MEMS gyroscopes' full scales, °/s (Bosch Sensortec, *BMI160 data sheet*, table 2).
276const FULL_SCALES_DPS: [f64; 5] = [125.0, 250.0, 500.0, 1_000.0, 2_000.0];
277
278/// How near, as a share, a plateau must be to a full scale.
279const FULL_SCALE_MATCH: f64 = 0.01;
280
281/// The least step, °, of a heading's wrap across north.
282const HEADING_WRAP_DEG: f64 = 300.0;
283
284/// How far, as a factor either way, a wrap's step less 360° may lie from the steps on either side
285/// of it.
286const HEADING_WRAP_FACTOR: f64 = 2.0;
287
288/// How far, s, a `Timestamp` step over 1,000 may lie from the clock's, `Seconds`.
289const STAMP_STEP_S: f64 = 0.0015;
290
291/// 2000-01-01 and 2100-01-01, 00:00 UTC, in ms since 1970.
292const STAMP_RANGE_MS: (f64, f64) = (946_684_800_000.0, 4_102_444_800_000.0);
293
294/// The least fresh fixes, and the least climb of the pressure altitude among them, m, for the GPS
295/// altitude's gain.
296const MIN_FIXES: usize = 10;
297
298/// A day, s.
299const DAY_S: f64 = 86_400.0;
300
301/// What a header column holds.
302#[derive(Debug, Clone, Copy, PartialEq, Eq)]
303enum Kind {
304    Clock,
305    Stamp,
306    GpsAltitude,
307    Latitude,
308    Longitude,
309    /// One of the accelerometer's axes, `Gx`, `Gy` or `Gz`.
310    Axis,
311    Total,
312    /// One of the axes with gravity taken out.
313    NoGravAxis,
314    NoGravTotal,
315    Gyro,
316    Heading,
317    Yaw,
318    Pitch,
319    Roll,
320    BaroAltitude,
321    Pressure,
322    BaroSpeed,
323    /// A flag, state or count, kept as a number.
324    Code,
325    /// A running extreme of another column.
326    Extreme,
327    Temperature,
328    /// A column kept as written for the reason given.
329    Kept(&'static str),
330    Unknown,
331}
332
333/// Why a magnetometer's column is kept.
334const MAGNETIC: &str = "by name a magnetometer's reading, in a unit no source states, and the \
335                        record has no quantity for a magnetic field";
336/// Why the turn counts are kept.
337const TURNS: &str = "by name a count of turns or its clock, which no check in the file ties \
338                     to the gyroscope";
339/// Why the GPS's accuracies are kept.
340const ACCURACY: &str = "by name the GPS's accuracy, in a unit no source states";
341/// Why the external sensor's columns are kept.
342const EXTERNAL: &str = "by name an external sensor's, in a unit no source states";
343/// Why the reserved columns are kept.
344const RESERVED: &str = "reserved, by name; no source states what it holds";
345
346/// What a column named so holds.
347fn kind_of(name: &str) -> Kind {
348    match name {
349        "Seconds" => Kind::Clock,
350        "Timestamp" => Kind::Stamp,
351        "AltitudeMSLGPS" | "AltitudeMSLMaxGPS" => Kind::GpsAltitude,
352        "Latitude" => Kind::Latitude,
353        "Longitude" => Kind::Longitude,
354        "Gx" | "Gy" | "Gz" => Kind::Axis,
355        "GTotal" => Kind::Total,
356        "NoGravGx" | "NoGravGy" | "NoGravGz" => Kind::NoGravAxis,
357        "NoGravGTotal" => Kind::NoGravTotal,
358        "GyroX" | "GyroY" | "GyroZ" => Kind::Gyro,
359        "Heading" => Kind::Heading,
360        "Yaw" => Kind::Yaw,
361        "Pitch" => Kind::Pitch,
362        "Roll" => Kind::Roll,
363        "AltitudeRelativeBarometer" => Kind::BaroAltitude,
364        "hPa" => Kind::Pressure,
365        "VelocityBarometer" | "VelocityBarometerRaw" => Kind::BaroSpeed,
366        "Record" | "Static" | "UsbState" | "Satellites" => Kind::Code,
367        "GMax"
368        | "NoGravGMax"
369        | "AltitudeRelativeMaxBarometer"
370        | "VelocityBarometerMax"
371        | "VelocityBarometerMin" => Kind::Extreme,
372        "Temperature" => Kind::Temperature,
373        "MagnetometerX" | "MagnetometerY" | "MagnetometerZ" | "HeadingX" | "HeadingY"
374        | "HeadingZ" => Kind::Kept(MAGNETIC),
375        "Revolutions" | "RevApogee" | "RevolutionsClock" => Kind::Kept(TURNS),
376        "HorizontalResGPS" | "VerticalResGPS" => Kind::Kept(ACCURACY),
377        "Battery" => Kind::Kept("by name the battery's state, in a unit no source states"),
378        "AltitudeRelativeSerial" | "AltitudeRelativeMaxSerial" => Kind::Kept(EXTERNAL),
379        _ if name.starts_with("Usb") => Kind::Kept(EXTERNAL),
380        _ if name.starts_with("Reserved") => Kind::Kept(RESERVED),
381        _ => Kind::Unknown,
382    }
383}
384
385/// Whether the text is the app's CSV: a header row opening `Seconds,Timestamp,AltitudeMSLGPS`.
386pub fn recognizes(text: &str) -> bool {
387    text.lines()
388        .map(str::trim)
389        .find(|line| !line.is_empty())
390        .is_some_and(|line| line.starts_with("Seconds,Timestamp,AltitudeMSLGPS"))
391}
392
393/// A header column: what it holds and its name as written.
394struct Column {
395    kind: Kind,
396    name: String,
397}
398
399/// The rows read: each row's line and time, and each column's cells, `NaN` for a gap.
400struct Table {
401    columns: Vec<Column>,
402    lines: Vec<usize>,
403    time_s: Vec<f64>,
404    /// Per header column, the clock's too.
405    values: Vec<Vec<f64>>,
406}
407
408impl Table {
409    /// The cells of the column named so.
410    fn column(&self, name: &str) -> Option<&[f64]> {
411        self.index(name).map(|index| self.values[index].as_slice())
412    }
413
414    fn index(&self, name: &str) -> Option<usize> {
415        self.columns.iter().position(|column| column.name == name)
416    }
417
418    /// The names of the columns of a kind, in the file's order.
419    fn names_of(&self, kinds: &[Kind]) -> Vec<&str> {
420        self.columns
421            .iter()
422            .filter(|column| kinds.contains(&column.kind))
423            .map(|column| column.name.as_str())
424            .collect()
425    }
426}
427
428/// Reads a phone app's `launch….csv` from its text.
429///
430/// # Errors
431///
432/// [`LogError::NotThisFormat`] if the first line that isn't blank isn't the app's header;
433/// [`LogError::Syntax`] for a header naming a column twice or leaving a name empty, a row of the
434/// wrong width, a cell that isn't a number, a time that is missing or doesn't increase, or a value
435/// its unit's conversion makes infinite, naming the line; [`LogError::NoData`] if no row follows
436/// the header.
437pub fn read(text: &str) -> Result<FlightLog, LogError> {
438    let mut lines = numbered_lines(text).skip_while(|(_, line)| line.trim().is_empty());
439    let Some((header_line, header)) = lines.next() else {
440        return Err(not_this_format());
441    };
442    if !header
443        .trim()
444        .starts_with("Seconds,Timestamp,AltitudeMSLGPS")
445    {
446        return Err(not_this_format());
447    }
448    let columns = columns(header, header_line)?;
449    let table = read_rows(lines, columns)?;
450    if table.time_s.is_empty() {
451        return Err(LogError::NoData { format: FORMAT });
452    }
453    build(&table)
454}
455
456fn not_this_format() -> LogError {
457    LogError::NotThisFormat {
458        format: FORMAT,
459        message: "its first line isn't the app's header, opening \
460                  `Seconds,Timestamp,AltitudeMSLGPS`"
461            .to_owned(),
462    }
463}
464
465/// The header's columns; a name given twice, or left empty, is refused.
466fn columns(header: &str, line: usize) -> Result<Vec<Column>, LogError> {
467    let mut columns = Vec::new();
468    let mut seen: HashSet<&str> = HashSet::new();
469    for (index, name) in header.split(',').map(str::trim).enumerate() {
470        if name.is_empty() {
471            return Err(syntax(
472                line,
473                format!("the header's column {} has no name", index + 1),
474            ));
475        }
476        if !seen.insert(name) {
477            return Err(syntax(
478                line,
479                format!("the header names {:?} twice", short(name)),
480            ));
481        }
482        columns.push(Column {
483            kind: kind_of(name),
484            name: name.to_owned(),
485        });
486    }
487    Ok(columns)
488}
489
490/// Reads the data rows (module docs, *Rows*).
491fn read_rows<'a>(
492    lines: impl Iterator<Item = (usize, &'a str)>,
493    columns: Vec<Column>,
494) -> Result<Table, LogError> {
495    let width = columns.len();
496    let mut table = Table {
497        values: columns.iter().map(|_| Vec::new()).collect(),
498        columns,
499        lines: Vec::new(),
500        time_s: Vec::new(),
501    };
502    for (number, line) in lines {
503        if line.trim().is_empty() {
504            continue;
505        }
506        // One more than the header names, so that a wider row is still told apart.
507        let cells: Vec<&str> = line.split(',').map(str::trim).take(width + 1).collect();
508        if cells.len() != width {
509            return Err(syntax(
510                number,
511                format!(
512                    "the row has {} cells where the header names {width}",
513                    if cells.len() > width {
514                        format!("more than {width}")
515                    } else {
516                        cells.len().to_string()
517                    }
518                ),
519            ));
520        }
521        let mut row = Vec::with_capacity(width);
522        for (column, cell) in table.columns.iter().zip(&cells) {
523            row.push(if cell.is_empty() {
524                f64::NAN
525            } else {
526                table::number(cell, false).ok_or_else(|| {
527                    syntax(
528                        number,
529                        format!("{:?} under {:?} isn't a number", short(cell), column.name),
530                    )
531                })?
532            });
533        }
534        let time_s = row[0];
535        if time_s.is_nan() {
536            return Err(syntax(number, "the row has no time".to_owned()));
537        }
538        if let Some(&last) = table.time_s.last()
539            && time_s <= last
540        {
541            return Err(syntax(
542                number,
543                format!("the time {time_s} s doesn't come after the one above it, {last} s"),
544            ));
545        }
546        for (store, value) in table.values.iter_mut().zip(row) {
547            store.push(value);
548        }
549        table.lines.push(number);
550        table.time_s.push(time_s);
551    }
552    Ok(table)
553}
554
555fn syntax(line: usize, message: String) -> LogError {
556    LogError::Syntax {
557        format: FORMAT,
558        line,
559        message,
560    }
561}
562
563/// The names as a note lists them: `` `a`, `b` and `c` ``.
564fn listed(names: &[&str]) -> String {
565    let quoted: Vec<String> = names
566        .iter()
567        .map(|name| format!("`{}`", short(name)))
568        .collect();
569    match quoted.as_slice() {
570        [] => String::new(),
571        [one] => one.clone(),
572        [rest @ .., last] => format!("{} and {last}", rest.join(", ")),
573    }
574}
575
576/// How a column's values become the record's.
577#[derive(Debug, Clone, Copy, PartialEq)]
578enum Convert {
579    AsWritten,
580    Scale(f64),
581    /// A Unix time in ms to the time of day in UTC, s.
582    TimeOfDay,
583}
584
585/// A column's reading: its quantity and conversion.
586#[derive(Debug, Clone, Copy, PartialEq)]
587struct Reading {
588    quantity: Quantity,
589    convert: Convert,
590}
591
592const KEPT: Reading = Reading {
593    quantity: Quantity::Unrecognized,
594    convert: Convert::AsWritten,
595};
596
597/// The outcome of a pair of checks: the evidence, and whether both passed.
598struct Outcome {
599    passed: bool,
600    evidence: String,
601}
602
603impl Outcome {
604    /// Both checks' evidence, the outcome passing only if both did.
605    fn both(first: (bool, String), second: (bool, String)) -> Self {
606        Self {
607            passed: first.0 && second.0,
608            evidence: format!(
609                "(1) {}. (2) {}",
610                first.1,
611                crate::log::capitalized(&second.1)
612            ),
613        }
614    }
615}
616
617/// The record from the rows read: each column's unit decided, the notes and the channels.
618fn build(table: &Table) -> Result<FlightLog, LogError> {
619    let mut readings: Vec<Reading> = vec![KEPT; table.columns.len()];
620    let mut notes: Vec<String> = vec![
621        "the app isn't named in the file. No source describes its format, so every unit is \
622         decided from the file's own values or kept as written"
623            .to_owned(),
624    ];
625    let mut set = |table: &Table, names: &[&str], reading: Reading| {
626        for name in names {
627            if let Some(index) = table.index(name) {
628                readings[index] = reading;
629            }
630        }
631    };
632    let time = &table.time_s;
633
634    // The pressure.
635    let pressure_pa: Option<Vec<f64>> = table.column("hPa").and_then(|hpa| {
636        let first = hpa.iter().copied().find(|v| v.is_finite());
637        let within = first.is_some_and(|p| (PAD_HPA.0..=PAD_HPA.1).contains(&p));
638        let evidence = first.map_or("it holds no reading".to_owned(), |p| {
639            format!("the first reading, on the pad, is {p:.2}")
640        });
641        if within {
642            notes.push(format!(
643                "`hPa` read in hectopascals, from its name and the pad: {evidence}, within the \
644                 {} to {} hPa of a launch site",
645                PAD_HPA.0, PAD_HPA.1
646            ));
647            Some(hpa.iter().map(|v| v * 100.0).collect())
648        } else {
649            notes.push(format!(
650                "`hPa` kept as written, its name refuted by the pad: {evidence}, outside the {} \
651                 to {} hPa of a launch site",
652                PAD_HPA.0, PAD_HPA.1
653            ));
654            None
655        }
656    });
657    if pressure_pa.is_some() {
658        set(
659            table,
660            &["hPa"],
661            Reading {
662                quantity: Quantity::Pressure,
663                convert: Convert::Scale(100.0),
664            },
665        );
666    }
667    let pressure_altitude: Option<Vec<f64>> = pressure_pa.as_ref().map(|pa| {
668        let atmosphere = Ussa76::standard();
669        pa.iter()
670            .map(|p| atmosphere.pressure_altitude_m(*p).unwrap_or(f64::NAN))
671            .collect()
672    });
673
674    // The barometric altitude.
675    let altitude = table.column("AltitudeRelativeBarometer");
676    let coast = altitude.and_then(|alt| coast(time, alt));
677    let mut altitude_m = false;
678    if let Some(alt) = altitude {
679        if alt.iter().all(|v| *v == 0.0 || v.is_nan()) {
680            notes.push(
681                "`AltitudeRelativeBarometer` kept as written, holding no reading: it is 0 on \
682                 every row, as in a file from a phone without a barometer"
683                    .to_owned(),
684            );
685        } else {
686            let outcome = Outcome::both(
687                matches_pressure(alt, pressure_altitude.as_deref()),
688                coast_check(coast.as_ref(), |c| decel_of_altitude(time, alt, c)),
689            );
690            if outcome.passed {
691                altitude_m = true;
692                notes.push(format!(
693                    "`AltitudeRelativeBarometer` read in meters, from the pressure and the \
694                     coast: {}",
695                    outcome.evidence
696                ));
697                set(
698                    table,
699                    &["AltitudeRelativeBarometer"],
700                    Reading {
701                        quantity: Quantity::AltitudeAboveGround,
702                        convert: Convert::Scale(1.0),
703                    },
704                );
705            } else {
706                notes.push(format!(
707                    "`AltitudeRelativeBarometer` kept as written, in a unit two checks don't \
708                     settle: {}",
709                    outcome.evidence
710                ));
711            }
712        }
713    }
714    let coast = coast.filter(|_| altitude_m);
715
716    // The barometric speeds.
717    let mut speed_m_s = false;
718    if let Some(alt) = altitude.filter(|_| altitude_m) {
719        let rate = rate_of(time, alt);
720        for name in table.names_of(&[Kind::BaroSpeed]) {
721            let Some(speed) = table.column(name) else {
722                continue;
723            };
724            let gain = slope(rate.iter().copied().zip(speed.iter().copied()));
725            let first = match gain {
726                Some(gain) => (
727                    (SPEED_GAIN.0..=SPEED_GAIN.1).contains(&gain),
728                    format!(
729                        "against the barometric altitude's rate it has a gain of {gain:.3}, \
730                         where {} to {} reads m/s",
731                        SPEED_GAIN.0, SPEED_GAIN.1
732                    ),
733                ),
734                None => (
735                    false,
736                    "it has no rows beside the barometric altitude's rate".to_owned(),
737                ),
738            };
739            let second = coast_check(coast.as_ref(), |c| {
740                let rows = &c.window;
741                slope(rows.iter().map(|&row| (time[row], speed[row]))).map(|s| -s)
742            });
743            let outcome = Outcome::both(first, second);
744            if outcome.passed {
745                speed_m_s |= name == "VelocityBarometer";
746                notes.push(format!(
747                    "`{}` read in meters per second, from the barometric altitude and the \
748                     coast: {}",
749                    short(name),
750                    outcome.evidence
751                ));
752                set(
753                    table,
754                    &[name],
755                    Reading {
756                        quantity: Quantity::Speed,
757                        convert: Convert::Scale(1.0),
758                    },
759                );
760            } else {
761                notes.push(format!(
762                    "`{}` kept as written, in a unit two checks don't settle: {}",
763                    short(name),
764                    outcome.evidence
765                ));
766            }
767        }
768    } else {
769        let names = table.names_of(&[Kind::BaroSpeed]);
770        if !names.is_empty() {
771            notes.push(format!(
772                "{} kept as written, in a unit two checks don't settle: the barometric altitude \
773                 they would be checked against isn't read in meters",
774                listed(&names)
775            ));
776        }
777    }
778
779    // The accelerometer.
780    accelerations(table, coast.as_ref(), &mut notes, &mut set);
781
782    // The attitude and the gyroscope.
783    let up = rest_directions(table, coast.as_ref());
784    let mut radians = [false; 3];
785    for (slot, name) in ["Yaw", "Pitch", "Roll"].into_iter().enumerate() {
786        let Some(values) = table.column(name) else {
787            continue;
788        };
789        let (outcome, evidence) = match name {
790            "Pitch" => (
791                Outcome::both(
792                    within(values, std::f64::consts::FRAC_PI_2, "±π/2"),
793                    pitch_gravity(up.as_deref(), values),
794                ),
795                "gravity's direction",
796            ),
797            "Roll" => (
798                Outcome::both(
799                    within(values, std::f64::consts::PI, "±π"),
800                    roll_gravity(up.as_deref(), values),
801                ),
802                "gravity's direction",
803            ),
804            _ => (
805                Outcome::both(
806                    within(values, std::f64::consts::PI, "±π"),
807                    yaw_magnetic(table, up.as_deref(), values),
808                ),
809                "the magnetic field's direction",
810            ),
811        };
812        if outcome.passed {
813            radians[slot] = true;
814            notes.push(format!(
815                "`{name}` read in radians, from its range and {evidence}: {}",
816                outcome.evidence
817            ));
818            set(
819                table,
820                &[name],
821                Reading {
822                    quantity: Quantity::Angle,
823                    convert: Convert::Scale(180.0 / std::f64::consts::PI),
824                },
825            );
826        } else {
827            notes.push(format!(
828                "`{name}` kept as written, in a unit two checks don't settle: {}",
829                outcome.evidence
830            ));
831        }
832    }
833    gyroscope(table, radians == [true; 3], &mut notes, &mut set);
834    if let Some(values) = table.column("Heading") {
835        let outcome = Outcome::both(heading_range(values), heading_wraps(values));
836        if outcome.passed {
837            notes.push(format!(
838                "`Heading` read in degrees, from its range and its wrap's period: {}",
839                outcome.evidence
840            ));
841            set(
842                table,
843                &["Heading"],
844                Reading {
845                    quantity: Quantity::Angle,
846                    convert: Convert::Scale(1.0),
847                },
848            );
849        } else {
850            notes.push(format!(
851                "`Heading` kept as written, in a unit two checks don't settle: {}",
852                outcome.evidence
853            ));
854        }
855    }
856
857    // The position, the time stamp and the GPS altitude.
858    let position = table.names_of(&[Kind::Latitude, Kind::Longitude]);
859    if !position.is_empty() {
860        let outcome = Outcome::both(
861            position_range(table, "Latitude", 90.0),
862            position_range(table, "Longitude", 180.0),
863        );
864        if outcome.passed {
865            notes.push(format!(
866                "{} read in degrees, from the latitude's range and the longitude's: {}",
867                listed(&position),
868                outcome.evidence
869            ));
870            for (name, quantity) in [
871                ("Latitude", Quantity::Latitude),
872                ("Longitude", Quantity::Longitude),
873            ] {
874                set(
875                    table,
876                    &[name],
877                    Reading {
878                        quantity,
879                        convert: Convert::Scale(1.0),
880                    },
881                );
882            }
883        } else {
884            notes.push(format!(
885                "{} kept as written, in a unit two checks don't settle: {}",
886                listed(&position),
887                outcome.evidence
888            ));
889        }
890    }
891    if let Some(stamp) = table.column("Timestamp") {
892        let outcome = Outcome::both(stamp_steps(time, stamp), stamp_date(stamp));
893        if outcome.passed {
894            notes.push(format!(
895                "`Timestamp` read in milliseconds since 1970 UTC, from the clock and the date: \
896                 {}; kept as the time of day in UTC",
897                outcome.evidence
898            ));
899            set(
900                table,
901                &["Timestamp"],
902                Reading {
903                    quantity: Quantity::TimeOfDay,
904                    convert: Convert::TimeOfDay,
905                },
906            );
907        } else {
908            notes.push(format!(
909                "`Timestamp` kept as written, in a unit two checks don't settle: {}",
910                outcome.evidence
911            ));
912        }
913    }
914    let gps = table.names_of(&[Kind::GpsAltitude]);
915    if !gps.is_empty() {
916        notes.push(format!(
917            "{} kept as written, in a unit no second check settles: {}",
918            listed(&gps),
919            gps_evidence(table, pressure_altitude.as_deref())
920        ));
921    }
922
923    // The rest, as written.
924    kept_notes(table, &mut notes, &mut set);
925    let mut line_notes = Notes::default();
926    for column in &table.columns {
927        if column.kind == Kind::Unknown {
928            line_notes.push(format!(
929                "`{}` kept as written, in a unit no source states: this reader doesn't know the \
930                 column",
931                short(&column.name)
932            ));
933        }
934    }
935    notes.extend(line_notes.into_vec());
936
937    // The channels.
938    let mut log = FlightLog {
939        format: LogFormat::PhoneLaunchCsv,
940        logger: "phone app".to_owned(),
941        serial_number: None,
942        firmware: None,
943        flight_number: None,
944        stated: Stated::default(),
945        time_s: Vec::new(),
946        altitude_m: Vec::new(),
947        vertical_speed_m_s: None,
948        temperature_k: None,
949        battery_v: None,
950        channels: Vec::new(),
951        events: Vec::new(),
952        notes,
953    };
954    for ((column, values), reading) in table.columns.iter().zip(&table.values).zip(&readings) {
955        if column.kind == Kind::Clock {
956            continue;
957        }
958        let mut converted = Vec::with_capacity(values.len());
959        for (value, line) in values.iter().zip(&table.lines) {
960            let value = match reading.convert {
961                Convert::AsWritten => *value,
962                Convert::Scale(scale) => value * scale,
963                Convert::TimeOfDay => (value / 1000.0).rem_euclid(DAY_S),
964            };
965            converted.push(table::finite(value, FORMAT, *line, &column.name)?);
966        }
967        if column.name == "AltitudeRelativeBarometer" && altitude_m {
968            log.altitude_m.clone_from(&converted);
969            log.time_s.clone_from(time);
970        } else if column.name == "VelocityBarometer" && speed_m_s && altitude_m {
971            log.vertical_speed_m_s = Some(converted.clone());
972        }
973        log.channels.push(Channel {
974            column: column.name.clone(),
975            quantity: reading.quantity,
976            time_s: time.clone(),
977            values: converted,
978        });
979    }
980    if !altitude_m {
981        log.notes.push(
982            "the primary track is empty: the file's barometric altitude isn't read in meters, so \
983             the readings withhold on the log; every column is a channel"
984                .to_owned(),
985        );
986    }
987    Ok(log)
988}
989
990/// The notes on the columns kept as written for their kind, and the codes read as numbers.
991fn kept_notes(
992    table: &Table,
993    notes: &mut Vec<String>,
994    set: &mut impl FnMut(&Table, &[&str], Reading),
995) {
996    let codes = table.names_of(&[Kind::Code]);
997    if !codes.is_empty() {
998        notes.push(format!(
999            "{} kept as written, as numbers with no unit: by their names flags, states or \
1000             counts",
1001            listed(&codes)
1002        ));
1003        set(
1004            table,
1005            &codes,
1006            Reading {
1007                quantity: Quantity::Number,
1008                convert: Convert::AsWritten,
1009            },
1010        );
1011    }
1012    let extremes = table.names_of(&[Kind::Extreme]);
1013    if !extremes.is_empty() {
1014        notes.push(format!(
1015            "{} kept as written, as running extremes. By their names they are the most or least of \
1016             another column so far. The reader doesn't tie them to that column's unit",
1017            listed(&extremes)
1018        ));
1019    }
1020    if let Some(values) = table.column("Temperature") {
1021        notes.push(format!(
1022            "`Temperature` kept as written, in a unit no two checks settle. Its readings, {}, \
1023             would be a phone's warmth in degrees Celsius or a cool day's in degrees Fahrenheit. \
1024             Nothing else in the file tells them apart",
1025            span(values)
1026        ));
1027    }
1028    let mut reasons: Vec<&'static str> = Vec::new();
1029    for column in &table.columns {
1030        if let Kind::Kept(why) = column.kind
1031            && !reasons.contains(&why)
1032        {
1033            reasons.push(why);
1034        }
1035    }
1036    for why in reasons {
1037        let names: Vec<&str> = table
1038            .columns
1039            .iter()
1040            .filter(|column| column.kind == Kind::Kept(why))
1041            .map(|column| column.name.as_str())
1042            .collect();
1043        notes.push(format!("{} kept as written: {why}", listed(&names)));
1044    }
1045}
1046
1047/// The least and greatest of the readings, as a note writes them.
1048fn span(values: &[f64]) -> String {
1049    let (low, high) = values
1050        .iter()
1051        .filter(|v| v.is_finite())
1052        .fold((f64::INFINITY, f64::NEG_INFINITY), |(low, high), &v| {
1053            (low.min(v), high.max(v))
1054        });
1055    if low.is_finite() {
1056        format!("{} to {}", brief(low), brief(high))
1057    } else {
1058        "none".to_owned()
1059    }
1060}
1061
1062/// A value to three decimals at most, as a note writes it: `37.2`, not `37.20000076293945`.
1063fn brief(value: f64) -> String {
1064    let written = format!("{value:.3}");
1065    let trimmed = written.trim_end_matches('0').trim_end_matches('.');
1066    if trimmed == "-0" {
1067        "0".to_owned()
1068    } else {
1069        trimmed.to_owned()
1070    }
1071}
1072
1073/// A value to two decimals, a rounded 0 written without its sign.
1074fn hundredths(value: f64) -> String {
1075    let written = format!("{value:.2}");
1076    if written == "-0.00" {
1077        "0.00".to_owned()
1078    } else {
1079        written
1080    }
1081}
1082
1083/// The least-squares slope of `y` on `x` over the pairs with both finite; `None` with fewer than
1084/// three or no spread in `x`.
1085fn slope(pairs: impl Iterator<Item = (f64, f64)>) -> Option<f64> {
1086    let points: Vec<(f64, f64)> = pairs
1087        .filter(|(x, y)| x.is_finite() && y.is_finite())
1088        .collect();
1089    if points.len() < 3 {
1090        return None;
1091    }
1092    let n = points.len() as f64;
1093    let mean_x = points.iter().map(|p| p.0).sum::<f64>() / n;
1094    let mean_y = points.iter().map(|p| p.1).sum::<f64>() / n;
1095    let sxx: f64 = points.iter().map(|p| (p.0 - mean_x).powi(2)).sum();
1096    let sxy: f64 = points.iter().map(|p| (p.0 - mean_x) * (p.1 - mean_y)).sum();
1097    (sxx > 0.0).then(|| sxy / sxx)
1098}
1099
1100/// The second derivative of a least-squares parabola through the points, `2c` of
1101/// `y = a + b t + c t²`; `None` with fewer than four points or a singular fit.
1102fn curvature(points: &[(f64, f64)]) -> Option<f64> {
1103    if points.len() < 4 {
1104        return None;
1105    }
1106    let n = points.len() as f64;
1107    let mean_t = points.iter().map(|p| p.0).sum::<f64>() / n;
1108    // Sums of powers of the centered time, and of y times them.
1109    let mut s = [0.0_f64; 5];
1110    let mut r = [0.0_f64; 3];
1111    for &(t, y) in points {
1112        let x = t - mean_t;
1113        let mut power = 1.0;
1114        for (k, sum) in s.iter_mut().enumerate() {
1115            *sum += power;
1116            if k < 3 {
1117                r[k] += y * power;
1118            }
1119            power *= x;
1120        }
1121    }
1122    // The normal equations, solved by Cramer's rule.
1123    let m = [[s[0], s[1], s[2]], [s[1], s[2], s[3]], [s[2], s[3], s[4]]];
1124    let det = |m: &[[f64; 3]; 3]| {
1125        m[0][0] * (m[1][1] * m[2][2] - m[1][2] * m[2][1])
1126            - m[0][1] * (m[1][0] * m[2][2] - m[1][2] * m[2][0])
1127            + m[0][2] * (m[1][0] * m[2][1] - m[1][1] * m[2][0])
1128    };
1129    let whole = det(&m);
1130    if whole.abs() < f64::MIN_POSITIVE || !whole.is_finite() {
1131        return None;
1132    }
1133    let mut third = m;
1134    for (row, value) in third.iter_mut().zip(r) {
1135        row[2] = value;
1136    }
1137    Some(2.0 * det(&third) / whole)
1138}
1139
1140/// The median of the finite values; `None` if there are none.
1141fn median(mut values: Vec<f64>) -> Option<f64> {
1142    values.retain(|v| v.is_finite());
1143    if values.is_empty() {
1144        return None;
1145    }
1146    values.sort_by(f64::total_cmp);
1147    let middle = values.len() / 2;
1148    Some(if values.len().is_multiple_of(2) {
1149        (values[middle - 1] + values[middle]) / 2.0
1150    } else {
1151        values[middle]
1152    })
1153}
1154
1155/// The coast before the highest barometric altitude, measured on the column as written.
1156struct Coast {
1157    /// The steepest climb's rate, the column's unit per second, and its middle, s.
1158    climb: (f64, f64),
1159    /// The rows of the window (module docs), and its times, s.
1160    window: Vec<usize>,
1161    window_s: (f64, f64),
1162}
1163
1164/// The coast of an altitude column (module docs, *The barometric altitude*); `None` without a
1165/// highest altitude or a climb before it. The window may be short: [`coast_check`] says so.
1166fn coast(time: &[f64], altitude: &[f64]) -> Option<Coast> {
1167    let apogee = altitude
1168        .iter()
1169        .enumerate()
1170        .filter(|(_, v)| v.is_finite())
1171        .fold(None, |best: Option<(usize, f64)>, (row, &v)| match best {
1172            Some((_, top)) if top >= v => best,
1173            _ => Some((row, v)),
1174        })?
1175        .0;
1176    let mut climb: Option<(f64, f64)> = None;
1177    let mut from = 0;
1178    for to in 0..=apogee {
1179        while time[to] - time[from] > CLIMB_SPAN_S {
1180            from += 1;
1181        }
1182        let dt = time[to] - time[from];
1183        if dt >= CLIMB_SPAN_S / 2.0 {
1184            let rate = (altitude[to] - altitude[from]) / dt;
1185            if rate.is_finite() && climb.is_none_or(|(best, _)| rate > best) {
1186                climb = Some((rate, (time[to] + time[from]) / 2.0));
1187            }
1188        }
1189    }
1190    let climb = climb.filter(|(rate, _)| *rate > 0.0)?;
1191    let start = (time[apogee] - COAST_WINDOW_S.0).max(climb.1 + CLIMB_SPAN_S);
1192    let end = time[apogee] - COAST_WINDOW_S.1;
1193    let window = (0..apogee)
1194        .filter(|&row| (start..=end).contains(&time[row]) && altitude[row].is_finite())
1195        .collect();
1196    Some(Coast {
1197        climb,
1198        window,
1199        window_s: (start, end),
1200    })
1201}
1202
1203/// How fast an altitude column falls in the coast window, the column's unit per second squared.
1204fn decel_of_altitude(time: &[f64], altitude: &[f64], coast: &Coast) -> Option<f64> {
1205    let points: Vec<(f64, f64)> = coast
1206        .window
1207        .iter()
1208        .map(|&row| (time[row], altitude[row]))
1209        .collect();
1210    curvature(&points).map(|c| -c)
1211}
1212
1213/// The coast check: the slowing `decel` measures, in the column's unit per second squared, within
1214/// [`COAST_SLACK`] g to [`COAST_SLACK`] g in feet.
1215fn coast_check(coast: Option<&Coast>, decel: impl FnOnce(&Coast) -> Option<f64>) -> (bool, String) {
1216    let Some(coast) = coast else {
1217        return (
1218            false,
1219            "the file has no climb to a highest altitude to find the coast in".to_owned(),
1220        );
1221    };
1222    let (start, end) = coast.window_s;
1223    if end - start < MIN_COAST.0 || coast.window.len() < MIN_COAST.1 {
1224        return (
1225            false,
1226            format!(
1227                "the coast before the highest altitude holds {} rows over {:.2} s, fewer than {} \
1228                 over {} s. The coast runs from a second after the steepest climb to 3 s before \
1229                 the top",
1230                coast.window.len(),
1231                (end - start).max(0.0),
1232                MIN_COAST.1,
1233                MIN_COAST.0
1234            ),
1235        );
1236    }
1237    let low = COAST_SLACK * G0_M_S2;
1238    let high = COAST_SLACK * G0_M_S2 / FOOT_M;
1239    match decel(coast) {
1240        Some(value) => (
1241            (low..high).contains(&value),
1242            format!(
1243                "in the coast from {start:.2} s to {end:.2} s it slows by {} per second per \
1244                 second, {} g in meters. Gravity and drag give at least 1 g, and {low:.2} to \
1245                 {high:.2} m/s² reads meters",
1246                hundredths(value),
1247                hundredths(value / G0_M_S2)
1248            ),
1249        ),
1250        None => (false, "its coast can't be fitted".to_owned()),
1251    }
1252}
1253
1254/// Check 1 of the barometric altitude: its climb above the first row against the pressure
1255/// altitude's.
1256fn matches_pressure(altitude: &[f64], pressure_altitude: Option<&[f64]>) -> (bool, String) {
1257    let Some(pressure_altitude) = pressure_altitude else {
1258        return (false, "the file has no pressure read in hPa".to_owned());
1259    };
1260    let pairs: Vec<(f64, f64)> = altitude
1261        .iter()
1262        .zip(pressure_altitude)
1263        .map(|(a, p)| (*a, *p))
1264        .filter(|(a, p)| a.is_finite() && p.is_finite())
1265        .collect();
1266    let Some(&(a0, p0)) = pairs.first() else {
1267        return (false, "it has no row beside a pressure altitude".to_owned());
1268    };
1269    let (low, high) = pairs
1270        .iter()
1271        .fold((0.0_f64, 0.0_f64), |(low, high), (_, p)| {
1272            (low.min(p - p0), high.max(p - p0))
1273        });
1274    let climb = high - low;
1275    let off = pairs
1276        .iter()
1277        .map(|(a, p)| ((a - a0) - (p - p0)).abs())
1278        .fold(0.0_f64, f64::max);
1279    let allowed = PRESSURE_MATCH.0 + PRESSURE_MATCH.1 * climb;
1280    if climb < MIN_CLIMB_M {
1281        return (
1282            false,
1283            format!(
1284                "the pressure altitude climbs {climb:.1} m, under the {MIN_CLIMB_M} m that \
1285                 tells meters from feet"
1286            ),
1287        );
1288    }
1289    (
1290        off <= allowed,
1291        format!(
1292            "above the first row it lies at most {off:.2} from the pressure altitude of `hPa` in \
1293             the U.S. Standard Atmosphere, in meters. That altitude climbs {climb:.1} m, and the \
1294             most allowed over that climb is {allowed:.1}"
1295        ),
1296    )
1297}
1298
1299/// The rate of an altitude column at each row, over [`RATE_HALF_SPAN_S`] either side.
1300fn rate_of(time: &[f64], altitude: &[f64]) -> Vec<f64> {
1301    let mut rates = Vec::with_capacity(time.len());
1302    let (mut low, mut high) = (0, 0);
1303    for row in 0..time.len() {
1304        while time[row] - time[low] > RATE_HALF_SPAN_S {
1305            low += 1;
1306        }
1307        while high + 1 < time.len() && time[high + 1] - time[row] <= RATE_HALF_SPAN_S {
1308            high += 1;
1309        }
1310        let dt = time[high] - time[low];
1311        rates.push(if dt > 0.0 {
1312            (altitude[high] - altitude[low]) / dt
1313        } else {
1314            f64::NAN
1315        });
1316    }
1317    rates
1318}
1319
1320/// The quietest stretch of [`REST`]'s span among the rows before `before`, by the summed variance
1321/// of the three axes: its last row and its mean magnitude.
1322fn quietest(time: &[f64], axes: [&[f64]; 3], before: usize) -> Option<(usize, f64)> {
1323    let n = before.min(time.len());
1324    // Running sums of each axis and its square, and of rows with a gap.
1325    let mut sums = vec![[0.0_f64; 6]; n + 1];
1326    let mut gaps = vec![0_usize; n + 1];
1327    for row in 0..n {
1328        let mut next = sums[row];
1329        let mut gap = 0;
1330        for (k, axis) in axes.iter().enumerate() {
1331            let v = axis[row];
1332            if v.is_finite() {
1333                next[2 * k] += v;
1334                next[2 * k + 1] += v * v;
1335            } else {
1336                gap = 1;
1337            }
1338        }
1339        sums[row + 1] = next;
1340        gaps[row + 1] = gaps[row] + gap;
1341    }
1342    let mut best: Option<(f64, usize, [f64; 3])> = None;
1343    let mut end = 0;
1344    for start in 0..n {
1345        end = end.max(start);
1346        while end < n && time[end] - time[start] <= REST.0 {
1347            end += 1;
1348        }
1349        let rows = end - start;
1350        if rows < REST.1 || gaps[end] != gaps[start] {
1351            continue;
1352        }
1353        let count = rows as f64;
1354        let mut variance = 0.0;
1355        let mut mean = [0.0; 3];
1356        for (k, m) in mean.iter_mut().enumerate() {
1357            let s1 = sums[end][2 * k] - sums[start][2 * k];
1358            let s2 = sums[end][2 * k + 1] - sums[start][2 * k + 1];
1359            *m = s1 / count;
1360            variance += s2 / count - *m * *m;
1361        }
1362        if best.is_none_or(|(least, ..)| variance < least) {
1363            best = Some((variance, end - 1, mean));
1364        }
1365    }
1366    best.map(|(_, last, mean)| (last, mean.iter().map(|m| m * m).sum::<f64>().sqrt()))
1367}
1368
1369/// The accelerometer's columns (module docs, *The accelerations*).
1370fn accelerations(
1371    table: &Table,
1372    coast: Option<&Coast>,
1373    notes: &mut Vec<String>,
1374    set: &mut impl FnMut(&Table, &[&str], Reading),
1375) {
1376    let (Some(x), Some(y), Some(z)) = (table.column("Gx"), table.column("Gy"), table.column("Gz"))
1377    else {
1378        let names = table.names_of(&[Kind::Axis, Kind::Total, Kind::NoGravAxis, Kind::NoGravTotal]);
1379        if !names.is_empty() {
1380            notes.push(format!(
1381                "{} kept as written, in a unit two checks don't settle: the file hasn't all three \
1382                 of the accelerometer's axes",
1383                listed(&names)
1384            ));
1385        }
1386        return;
1387    };
1388    let time = &table.time_s;
1389    let magnitude: Vec<f64> = (0..time.len())
1390        .map(|row| (x[row] * x[row] + y[row] * y[row] + z[row] * z[row]).sqrt())
1391        .collect();
1392    let mut group = vec!["Gx", "Gy", "Gz"];
1393    if let Some(total) = table.column("GTotal") {
1394        let off = total
1395            .iter()
1396            .zip(&magnitude)
1397            .filter(|(t, m)| t.is_finite() && m.is_finite())
1398            .map(|(t, m)| (t - m).abs())
1399            .fold(0.0_f64, f64::max);
1400        if off <= TOTAL_TIE_G {
1401            group.push("GTotal");
1402        } else {
1403            notes.push(format!(
1404                "`GTotal` kept as written, not the axes' magnitude: it lies up to {off:.3} from \
1405                 it, more than {TOTAL_TIE_G}"
1406            ));
1407        }
1408    }
1409    let burnout_row = climb_row(time, coast);
1410    let rest = quietest(time, [x, y, z], burnout_row.unwrap_or(time.len()));
1411    let first = match rest {
1412        Some((last, g)) => (
1413            (REST_G.0..=REST_G.1).contains(&g),
1414            format!(
1415                "the quietest second{}, to {:.2} s, reads {g:.3} in the axes' magnitude, where \
1416                 {} to {} reads g at rest",
1417                if burnout_row.is_some() {
1418                    " before the steepest climb"
1419                } else {
1420                    ""
1421                },
1422                time[last],
1423                REST_G.0,
1424                REST_G.1
1425            ),
1426        ),
1427        None => (false, "the file has no quiet second".to_owned()),
1428    };
1429    let second = match (coast, rest, burnout_row) {
1430        (Some(coast), Some((last, _)), Some(burnout_row)) => {
1431            let speed: f64 = (last..burnout_row)
1432                .filter(|&row| row + 1 < time.len() && magnitude[row].is_finite())
1433                .map(|row| (magnitude[row] - 1.0) * G0_M_S2 * (time[row + 1] - time[row]))
1434                .sum();
1435            let ratio = speed / coast.climb.0;
1436            (
1437                (BOOST_RATIO.0..=BOOST_RATIO.1).contains(&ratio),
1438                format!(
1439                    "from then to the steepest climb, at {:.2} s, the magnitude less 1 g, read in \
1440                     g, gives {speed:.1} m/s. That is {ratio:.2} times the barometric climb rate, \
1441                     where {} to {} reads g",
1442                    coast.climb.1, BOOST_RATIO.0, BOOST_RATIO.1
1443                ),
1444            )
1445        }
1446        _ => (
1447            false,
1448            "the file has no barometric climb in meters to check the boost against".to_owned(),
1449        ),
1450    };
1451    let outcome = Outcome::both(first, second);
1452    let reading = Reading {
1453        quantity: Quantity::Acceleration,
1454        convert: Convert::Scale(G0_M_S2),
1455    };
1456    if outcome.passed {
1457        notes.push(format!(
1458            "{} read in standard gravity, from the pad and the boost: {}",
1459            listed(&group),
1460            outcome.evidence
1461        ));
1462        set(table, &group, reading);
1463    } else {
1464        notes.push(format!(
1465            "{} kept as written, in a unit two checks don't settle: {}",
1466            listed(&group),
1467            outcome.evidence
1468        ));
1469    }
1470    let (Some(nx), Some(ny), Some(nz)) = (
1471        table.column("NoGravGx"),
1472        table.column("NoGravGy"),
1473        table.column("NoGravGz"),
1474    ) else {
1475        let names = table.names_of(&[Kind::NoGravAxis, Kind::NoGravTotal]);
1476        if !names.is_empty() {
1477            notes.push(format!(
1478                "{} kept as written, in a unit two checks don't settle: the file hasn't all three \
1479                 axes with gravity taken out",
1480                listed(&names)
1481            ));
1482        }
1483        return;
1484    };
1485    let mut no_grav = vec!["NoGravGx", "NoGravGy", "NoGravGz"];
1486    let gravity = median(
1487        (0..time.len())
1488            .map(|row| {
1489                ((x[row] - nx[row]).powi(2)
1490                    + (y[row] - ny[row]).powi(2)
1491                    + (z[row] - nz[row]).powi(2))
1492                .sqrt()
1493            })
1494            .collect(),
1495    );
1496    if let Some(total) = table.column("NoGravGTotal") {
1497        let off = (0..time.len())
1498            .map(|row| {
1499                (total[row] - (nx[row] * nx[row] + ny[row] * ny[row] + nz[row] * nz[row]).sqrt())
1500                    .abs()
1501            })
1502            .filter(|v| v.is_finite())
1503            .fold(0.0_f64, f64::max);
1504        if off <= TOTAL_TIE_G {
1505            no_grav.push("NoGravGTotal");
1506        } else {
1507            notes.push(format!(
1508                "`NoGravGTotal` kept as written, not the magnitude of the axes with gravity taken \
1509                 out: it lies up to {off:.3} from it, more than {TOTAL_TIE_G}"
1510            ));
1511        }
1512    }
1513    let tied = gravity.is_some_and(|g| (GRAVITY_TIE_G.0..=GRAVITY_TIE_G.1).contains(&g));
1514    let gravity_text = gravity.map_or("none".to_owned(), |g| format!("{g:.3}"));
1515    if outcome.passed && tied {
1516        notes.push(format!(
1517            "{} read in standard gravity, with the accelerometer's axes. The axes less them, the \
1518             gravity the phone took out, have a median magnitude of {gravity_text}. A median of \
1519             {} to {} is 1 g",
1520            listed(&no_grav),
1521            GRAVITY_TIE_G.0,
1522            GRAVITY_TIE_G.1
1523        ));
1524        set(table, &no_grav, reading);
1525    } else {
1526        notes.push(format!(
1527            "{} kept as written, in a unit two checks don't settle: {}; the axes less them have a \
1528             median magnitude of {gravity_text}",
1529            listed(&no_grav),
1530            if outcome.passed {
1531                "the accelerometer's axes are read in g"
1532            } else {
1533                "the accelerometer's axes aren't read in g"
1534            }
1535        ));
1536    }
1537}
1538
1539/// Check 1 of an angle: every reading within `most` either side of 0.
1540fn within(values: &[f64], most: f64, written: &str) -> (bool, String) {
1541    let largest = values
1542        .iter()
1543        .filter(|v| v.is_finite())
1544        .fold(None, |m: Option<f64>, v| {
1545            Some(m.map_or(v.abs(), |m| m.max(v.abs())))
1546        });
1547    match largest {
1548        Some(largest) => (
1549            largest <= most + 1e-6,
1550            format!(
1551                "its largest reading from 0 is {largest:.4}, where radians stay within {written}"
1552            ),
1553        ),
1554        None => (false, "it holds no reading".to_owned()),
1555    }
1556}
1557
1558/// The first row of the steepest climb, where the barometric altitude gives one: the rows before
1559/// it hold the pad.
1560fn climb_row(time: &[f64], coast: Option<&Coast>) -> Option<usize> {
1561    coast.map(|c| time.partition_point(|t| *t < c.climb.1))
1562}
1563
1564/// Where the phone is still, the direction up, as a unit vector along the accelerometer's axes, on
1565/// each row whose magnitude lies within [`ATTITUDE_CHECK`]'s share of the quietest second's before
1566/// the steepest climb (or in the whole file, without a barometric climb); `None` on the others. At
1567/// rest an accelerometer reads the push that holds the phone up, so its axes point up whatever
1568/// their unit. `Err` gives why there are no such rows.
1569fn rest_directions(table: &Table, coast: Option<&Coast>) -> Result<Vec<Option<[f64; 3]>>, String> {
1570    let (Some(x), Some(y), Some(z)) = (table.column("Gx"), table.column("Gy"), table.column("Gz"))
1571    else {
1572        return Err("the file hasn't the accelerometer's three axes".to_owned());
1573    };
1574    let time = &table.time_s;
1575    let before = climb_row(time, coast).unwrap_or(time.len());
1576    let Some((_, rest)) = quietest(time, [x, y, z], before) else {
1577        return Err("the file has no quiet second".to_owned());
1578    };
1579    let share = ATTITUDE_CHECK.0;
1580    Ok((0..time.len())
1581        .map(|row| {
1582            let magnitude = (x[row] * x[row] + y[row] * y[row] + z[row] * z[row]).sqrt();
1583            ((magnitude - rest).abs() <= share * rest && magnitude > 0.0)
1584                .then(|| [x[row] / magnitude, y[row] / magnitude, z[row] / magnitude])
1585        })
1586        .collect())
1587}
1588
1589/// The distance, rad, between two angles on the circle, 0 to π.
1590fn circular(a: f64, b: f64) -> f64 {
1591    let d = (a - b).rem_euclid(std::f64::consts::TAU);
1592    d.min(std::f64::consts::TAU - d)
1593}
1594
1595/// The distance, rad, of an angle from the nearest half-turn, 0 to π/2: `atan |tan a|`.
1596fn from_half_turn(a: f64) -> f64 {
1597    let d = a.rem_euclid(std::f64::consts::PI);
1598    d.min(std::f64::consts::PI - d)
1599}
1600
1601/// A direction check's verdict from the differences on its rows, read in radians and in degrees
1602/// (module docs): the median in radians at most [`ATTITUDE_CHECK`]'s and at most half that in
1603/// degrees, over at least its rows.
1604fn direction_verdict(
1605    radians: Vec<f64>,
1606    degrees: Vec<f64>,
1607    rows_are: &str,
1608    differs: &str,
1609) -> (bool, String) {
1610    let (_, _, most, rows) = ATTITUDE_CHECK;
1611    let count = radians.len();
1612    match (median(radians), median(degrees)) {
1613        (Some(r), Some(d)) if count >= rows => (
1614            r <= most && r <= d / 2.0,
1615            format!(
1616                "on {count} rows where {rows_are}, {differs} by a median {r:.3} read in radians \
1617                 and {d:.3} in degrees"
1618            ),
1619        ),
1620        _ => (
1621            false,
1622            format!("only {count} rows are where {rows_are}, fewer than {rows}"),
1623        ),
1624    }
1625}
1626
1627/// Check 2 of the pitch: gravity's direction where the phone is still (module docs).
1628fn pitch_gravity(up: Result<&[Option<[f64; 3]>], &String>, pitch: &[f64]) -> (bool, String) {
1629    let up = match up {
1630        Ok(up) => up,
1631        Err(why) => return (false, why.clone()),
1632    };
1633    let least = ATTITUDE_CHECK.1;
1634    let mut radians = Vec::new();
1635    let mut degrees = Vec::new();
1636    for (p, u) in pitch.iter().zip(up) {
1637        if let Some(u) = u
1638            && p.abs() >= least
1639        {
1640            let gravity = u[1].abs();
1641            radians.push((p.sin().abs() - gravity).abs());
1642            degrees.push((p.to_radians().sin().abs() - gravity).abs());
1643        }
1644    }
1645    direction_verdict(
1646        radians,
1647        degrees,
1648        &format!("the accelerometer reads its rest and the pitch is at least {least} from level"),
1649        "|sin pitch| differs from gravity's share along y",
1650    )
1651}
1652
1653/// Check 2 of the roll: gravity's direction about the phone's y axis where the phone is still
1654/// (module docs).
1655fn roll_gravity(up: Result<&[Option<[f64; 3]>], &String>, roll: &[f64]) -> (bool, String) {
1656    let up = match up {
1657        Ok(up) => up,
1658        Err(why) => return (false, why.clone()),
1659    };
1660    let least = ATTITUDE_CHECK.1;
1661    let mut radians = Vec::new();
1662    let mut degrees = Vec::new();
1663    for (r, u) in roll.iter().zip(up) {
1664        if let Some(u) = u
1665            && u[0].hypot(u[2]) >= least.sin()
1666            && from_half_turn(*r) >= least
1667        {
1668            let gravity = u[0].abs().atan2(u[2].abs());
1669            radians.push((from_half_turn(*r) - gravity).abs());
1670            degrees.push((from_half_turn(r.to_radians()) - gravity).abs());
1671        }
1672    }
1673    direction_verdict(
1674        radians,
1675        degrees,
1676        &format!(
1677            "the accelerometer reads its rest, its y axis is at least {least} from vertical and \
1678             the roll at least {least} from a half-turn"
1679        ),
1680        "the roll's angle from the nearest half-turn differs from gravity's about y, \
1681         atan(|Gx| / |Gz|),",
1682    )
1683}
1684
1685/// Check 2 of the yaw: the magnetic field's direction where the phone is still (module docs).
1686fn yaw_magnetic(
1687    table: &Table,
1688    up: Result<&[Option<[f64; 3]>], &String>,
1689    yaw: &[f64],
1690) -> (bool, String) {
1691    let up = match up {
1692        Ok(up) => up,
1693        Err(why) => return (false, why.clone()),
1694    };
1695    let (Some(mx), Some(my), Some(mz)) = (
1696        table.column("MagnetometerX"),
1697        table.column("MagnetometerY"),
1698        table.column("MagnetometerZ"),
1699    ) else {
1700        return (
1701            false,
1702            "the file hasn't the magnetometer's three axes".to_owned(),
1703        );
1704    };
1705    let least = ATTITUDE_CHECK.1;
1706    let mut radians = Vec::new();
1707    let mut degrees = Vec::new();
1708    for (row, (a, u)) in yaw.iter().zip(up).enumerate() {
1709        let Some(u) = u else {
1710            continue;
1711        };
1712        if u[1].abs() > least.cos() || a.abs() < least {
1713            continue;
1714        }
1715        // East is the field across up, north up across east; the azimuth is the phone's y axis's
1716        // angle from north toward east.
1717        let field = [mx[row], my[row], mz[row]];
1718        let east = cross(field, *u);
1719        let size = (east[0] * east[0] + east[1] * east[1] + east[2] * east[2]).sqrt();
1720        if !(size.is_finite() && size > 0.0) {
1721            continue;
1722        }
1723        let east = [east[0] / size, east[1] / size, east[2] / size];
1724        let north = cross(*u, east);
1725        let azimuth = east[1].atan2(north[1]);
1726        radians.push(circular(*a, azimuth));
1727        degrees.push(circular(a.to_radians(), azimuth));
1728    }
1729    direction_verdict(
1730        radians,
1731        degrees,
1732        &format!(
1733            "the accelerometer reads its rest, its y axis is at least {least} from vertical and \
1734             the yaw at least {least} from 0"
1735        ),
1736        "the yaw differs from the azimuth of the phone's y axis from magnetic north, which the \
1737         magnetometer's and the accelerometer's directions give,",
1738    )
1739}
1740
1741/// The cross product `a × b`.
1742fn cross(a: [f64; 3], b: [f64; 3]) -> [f64; 3] {
1743    [
1744        a[1] * b[2] - a[2] * b[1],
1745        a[2] * b[0] - a[0] * b[2],
1746        a[0] * b[1] - a[1] * b[0],
1747    ]
1748}
1749
1750/// A rotation matrix from the attitude in radians: yaw about z, then pitch about x, then roll
1751/// about y.
1752fn attitude(yaw: f64, pitch: f64, roll: f64) -> [[f64; 3]; 3] {
1753    let (sy, cy) = yaw.sin_cos();
1754    let (sp, cp) = pitch.sin_cos();
1755    let (sr, cr) = roll.sin_cos();
1756    let z = [[cy, -sy, 0.0], [sy, cy, 0.0], [0.0, 0.0, 1.0]];
1757    let x = [[1.0, 0.0, 0.0], [0.0, cp, -sp], [0.0, sp, cp]];
1758    let y = [[cr, 0.0, sr], [0.0, 1.0, 0.0], [-sr, 0.0, cr]];
1759    multiply(&multiply(&z, &x), &y)
1760}
1761
1762fn multiply(a: &[[f64; 3]; 3], b: &[[f64; 3]; 3]) -> [[f64; 3]; 3] {
1763    let mut product = [[0.0; 3]; 3];
1764    for (i, row) in product.iter_mut().enumerate() {
1765        for (j, cell) in row.iter_mut().enumerate() {
1766            *cell = (0..3).map(|k| a[i][k] * b[k][j]).sum();
1767        }
1768    }
1769    product
1770}
1771
1772/// The angle, rad, of the rotation from one attitude to another.
1773fn turn(a: &[[f64; 3]; 3], b: &[[f64; 3]; 3]) -> f64 {
1774    let trace: f64 = (0..3)
1775        .map(|i| (0..3).map(|k| a[k][i] * b[k][i]).sum::<f64>())
1776        .sum();
1777    ((trace - 1.0) / 2.0).clamp(-1.0, 1.0).acos()
1778}
1779
1780/// The gyroscope's columns (module docs, *The gyroscope*).
1781fn gyroscope(
1782    table: &Table,
1783    attitude_in_radians: bool,
1784    notes: &mut Vec<String>,
1785    set: &mut impl FnMut(&Table, &[&str], Reading),
1786) {
1787    let names = table.names_of(&[Kind::Gyro]);
1788    if names.is_empty() {
1789        return;
1790    }
1791    let axes: Vec<&[f64]> = ["GyroX", "GyroY", "GyroZ"]
1792        .iter()
1793        .filter_map(|name| table.column(name))
1794        .collect();
1795    let first = if axes.len() != 3 {
1796        (false, "the file hasn't all three of its axes".to_owned())
1797    } else if !attitude_in_radians {
1798        (
1799            false,
1800            "the attitude, `Yaw`, `Pitch` and `Roll`, isn't all read in radians".to_owned(),
1801        )
1802    } else {
1803        gyro_turns(table, &axes)
1804    };
1805    let second = full_scale(&names, table);
1806    let outcome = Outcome::both(first, second);
1807    if outcome.passed {
1808        notes.push(format!(
1809            "{} read in radians per second, from the attitude and the full scale: {}",
1810            listed(&names),
1811            outcome.evidence
1812        ));
1813        set(
1814            table,
1815            &names,
1816            Reading {
1817                quantity: Quantity::AngularRate,
1818                convert: Convert::Scale(180.0 / std::f64::consts::PI),
1819            },
1820        );
1821    } else {
1822        notes.push(format!(
1823            "{} kept as written, in a unit two checks don't settle: {}",
1824            listed(&names),
1825            outcome.evidence
1826        ));
1827    }
1828}
1829
1830/// Check 1 of the gyroscope: the attitude's turn over spans of rows against the rate summed.
1831fn gyro_turns(table: &Table, axes: &[&[f64]]) -> (bool, String) {
1832    let (Some(yaw), Some(pitch), Some(roll)) = (
1833        table.column("Yaw"),
1834        table.column("Pitch"),
1835        table.column("Roll"),
1836    ) else {
1837        return (false, "the file hasn't the whole attitude".to_owned());
1838    };
1839    let time = &table.time_s;
1840    // The axes that stop at a full scale, and where.
1841    let full: Vec<Option<f64>> = axes
1842        .iter()
1843        .map(|axis| {
1844            let (most, stops) = plateau(axis);
1845            stops.then_some(most)
1846        })
1847        .collect();
1848    let mut ratios = Vec::new();
1849    let mut end = GYRO_SPAN_ROWS;
1850    while end < time.len() {
1851        let start = end - GYRO_SPAN_ROWS;
1852        let rows = start + 1..=end;
1853        let clipped = rows.clone().any(|row| {
1854            axes.iter().zip(&full).any(|(axis, most)| {
1855                most.is_some_and(|most| axis[row].abs() >= FULL_SCALE_SHARE * most)
1856            })
1857        });
1858        let summed: f64 = rows
1859            .map(|row| {
1860                let rate: f64 = axes
1861                    .iter()
1862                    .map(|axis| axis[row] * axis[row])
1863                    .sum::<f64>()
1864                    .sqrt();
1865                rate * (time[row] - time[row - 1])
1866            })
1867            .sum();
1868        let a = attitude(yaw[start], pitch[start], roll[start]);
1869        let b = attitude(yaw[end], pitch[end], roll[end]);
1870        let angle = turn(&a, &b);
1871        if !clipped
1872            && summed.is_finite()
1873            && angle.is_finite()
1874            && (GYRO_TURN_RAD.0..=GYRO_TURN_RAD.1).contains(&summed)
1875        {
1876            ratios.push(angle / summed);
1877        }
1878        end += GYRO_SPAN_ROWS;
1879    }
1880    let spans = ratios.len();
1881    match median(ratios) {
1882        Some(ratio) if spans >= GYRO_MIN_SPANS => (
1883            (GYRO_RATIO.0..=GYRO_RATIO.1).contains(&ratio),
1884            format!(
1885                "over {spans} spans of {GYRO_SPAN_ROWS} rows, the attitude turns a median {ratio:.3} \
1886                 times the rate summed, read in radians, where {} to {} reads rad/s",
1887                GYRO_RATIO.0, GYRO_RATIO.1
1888            ),
1889        ),
1890        _ => (
1891            false,
1892            format!(
1893                "only {} of {GYRO_SPAN_ROWS} rows turn by {} to {} rad off the full scale, \
1894                 fewer than {GYRO_MIN_SPANS}",
1895                counted(spans, "span", "spans"),
1896                GYRO_TURN_RAD.0,
1897                GYRO_TURN_RAD.1
1898            ),
1899        ),
1900    }
1901}
1902
1903/// Check 2 of the gyroscope: an axis at a MEMS gyroscope's full scale, read in rad/s.
1904fn full_scale(names: &[&str], table: &Table) -> (bool, String) {
1905    let mut found: Option<(String, f64, f64)> = None;
1906    let mut largest_seen = Vec::new();
1907    for name in names {
1908        let Some(values) = table.column(name) else {
1909            continue;
1910        };
1911        let (most, stops) = plateau(values);
1912        let at = values
1913            .iter()
1914            .filter(|v| v.abs() >= most * (1.0 - PLATEAU.1))
1915            .count();
1916        let degrees = most.to_degrees();
1917        largest_seen.push(format!(
1918            "`{name}` {most:.4} on {}",
1919            counted(at, "row", "rows")
1920        ));
1921        if stops
1922            && let Some(scale) = FULL_SCALES_DPS
1923                .iter()
1924                .find(|scale| (degrees - **scale).abs() <= FULL_SCALE_MATCH * **scale)
1925        {
1926            found = Some(((*name).to_owned(), most, *scale));
1927            break;
1928        }
1929    }
1930    match found {
1931        Some((name, most, scale)) => (
1932            true,
1933            format!(
1934                "`{name}` stops at {most:.4} on at least {} rows, {:.1} °/s read in rad/s, a \
1935                 gyroscope's full scale of {scale} °/s",
1936                PLATEAU.0,
1937                most.to_degrees()
1938            ),
1939        ),
1940        None => (
1941            false,
1942            format!(
1943                "no axis stops at a gyroscope's full scale of 125 to 2,000 °/s read in rad/s. \
1944                 A stop is on {} rows or more, {} of them in a row. The largest readings are {}",
1945                PLATEAU.0,
1946                PLATEAU.2,
1947                largest_seen.join(", ")
1948            ),
1949        ),
1950    }
1951}
1952
1953/// The largest reading from 0 of a column, and whether the column stops there: at least
1954/// [`PLATEAU`]'s rows within its share of it, enough of them in a row.
1955fn plateau(values: &[f64]) -> (f64, bool) {
1956    let most = values
1957        .iter()
1958        .filter(|v| v.is_finite())
1959        .fold(0.0_f64, |m, v| m.max(v.abs()));
1960    let at = |v: &f64| v.abs() >= most * (1.0 - PLATEAU.1);
1961    let count = values.iter().filter(|v| at(v)).count();
1962    let (mut run, mut longest) = (0, 0);
1963    for value in values {
1964        run = if at(value) { run + 1 } else { 0 };
1965        longest = longest.max(run);
1966    }
1967    (most, count >= PLATEAU.0 && longest >= PLATEAU.2)
1968}
1969
1970/// `1 row`, `2 rows`: a count with its noun.
1971fn counted(count: usize, one: &str, many: &str) -> String {
1972    format!("{count} {}", if count == 1 { one } else { many })
1973}
1974
1975/// Check 1 of the heading: within 0 to 360 and past 2π.
1976fn heading_range(values: &[f64]) -> (bool, String) {
1977    let readings: Vec<f64> = values.iter().copied().filter(|v| v.is_finite()).collect();
1978    let inside = readings.iter().all(|v| (0.0..=360.0).contains(v));
1979    let largest = readings.iter().fold(f64::NEG_INFINITY, |m, v| m.max(*v));
1980    (
1981        !readings.is_empty() && inside && largest > std::f64::consts::TAU,
1982        format!(
1983            "its readings, {}, {} within 0 to 360, the largest past the 2π radians reach",
1984            span(values),
1985            if inside { "lie" } else { "don't lie" }
1986        ),
1987    )
1988}
1989
1990/// Check 2 of the heading: its wrap's period. A step between rows larger than
1991/// [`HEADING_WRAP_DEG`] that, less 360 in its direction, continues the turn of the steps on either
1992/// side: of their sign and within [`HEADING_WRAP_FACTOR`] of each's size.
1993fn heading_wraps(values: &[f64]) -> (bool, String) {
1994    let count = values
1995        .windows(4)
1996        .filter(|w| {
1997            let (before, step, after) = (w[1] - w[0], w[2] - w[1], w[3] - w[2]);
1998            let rest = step - 360.0_f64.copysign(step);
1999            step.abs() > HEADING_WRAP_DEG
2000                && [before, after].iter().all(|side| {
2001                    side.is_finite()
2002                        && *side != 0.0
2003                        && side.signum() == rest.signum()
2004                        && rest.abs() <= HEADING_WRAP_FACTOR * side.abs()
2005                        && side.abs() <= HEADING_WRAP_FACTOR * rest.abs()
2006                })
2007        })
2008        .count();
2009    (
2010        count > 0,
2011        format!(
2012            "{} north as a heading in degrees does: by more than {HEADING_WRAP_DEG}. Each, \
2013             less 360, turns as the steps on either side do, within a factor of \
2014             {HEADING_WRAP_FACTOR}",
2015            counted(
2016                count,
2017                "step between rows crosses",
2018                "steps between rows cross"
2019            )
2020        ),
2021    )
2022}
2023
2024/// One check of the position: the column's largest reading from 0 past what radians reach,
2025/// `most` in degrees in radians, and within `most`, what degrees reach.
2026fn position_range(table: &Table, name: &str, most: f64) -> (bool, String) {
2027    let what = if name == "Latitude" {
2028        "the latitude"
2029    } else {
2030        "the longitude"
2031    };
2032    let Some(values) = table.column(name) else {
2033        return (false, format!("the file has no `{name}`"));
2034    };
2035    let radian_most = most.to_radians();
2036    let readings: Vec<f64> = values.iter().copied().filter(|v| v.is_finite()).collect();
2037    let largest = readings.iter().fold(0.0_f64, |m, v| m.max(v.abs()));
2038    (
2039        !readings.is_empty() && largest <= most && largest > radian_most,
2040        format!(
2041            "{what}'s largest reading from 0 is {largest:.1}, where radians reach \
2042             {radian_most:.4} and degrees {most}"
2043        ),
2044    )
2045}
2046
2047/// Check 1 of the time stamp: its steps against the clock's, `Seconds`.
2048fn stamp_steps(time: &[f64], stamp: &[f64]) -> (bool, String) {
2049    let mut off = 0.0_f64;
2050    let mut steps = 0;
2051    for row in 1..time.len() {
2052        let step = (stamp[row] - stamp[row - 1]) / 1000.0;
2053        if step.is_finite() {
2054            off = off.max((step - (time[row] - time[row - 1])).abs());
2055            steps += 1;
2056        }
2057    }
2058    (
2059        steps > 0 && off <= STAMP_STEP_S,
2060        format!(
2061            "over {steps} steps between rows, its step over 1,000 lies at most {off:.4} s from \
2062             the clock's, `Seconds`"
2063        ),
2064    )
2065}
2066
2067/// Check 2 of the time stamp: a date between 2000 and 2100 in ms.
2068fn stamp_date(stamp: &[f64]) -> (bool, String) {
2069    match stamp.iter().copied().find(|v| v.is_finite()) {
2070        Some(first) => (
2071            (STAMP_RANGE_MS.0..=STAMP_RANGE_MS.1).contains(&first),
2072            format!(
2073                "its first reading, read in ms since 1970, {} between 2000 and 2100",
2074                if (STAMP_RANGE_MS.0..=STAMP_RANGE_MS.1).contains(&first) {
2075                    "falls"
2076                } else {
2077                    "doesn't fall"
2078                }
2079            ),
2080        ),
2081        None => (false, "it holds no reading".to_owned()),
2082    }
2083}
2084
2085/// The GPS altitude's evidence: its gain against the pressure altitude at the fresh fixes, and
2086/// why that isn't enough (module docs).
2087fn gps_evidence(table: &Table, pressure_altitude: Option<&[f64]>) -> String {
2088    const WHY: &str = "A GPS's fixes once a second can't show gravity's pull in the coast. A \
2089                       pad's height above sea level can't tell meters from feet within the \
2090                       weather's reach";
2091    let (Some(gps), Some(pressure_altitude)) = (table.column("AltitudeMSLGPS"), pressure_altitude)
2092    else {
2093        return format!("the file has no pressure in hPa to compare it with. {WHY}");
2094    };
2095    let latitude = table.column("Latitude");
2096    let longitude = table.column("Longitude");
2097    let changed = |column: Option<&[f64]>, row: usize| {
2098        column.is_some_and(|c| {
2099            c[row].is_finite() && (row == 0 || c[row].to_bits() != c[row - 1].to_bits())
2100        })
2101    };
2102    let fixes: Vec<(f64, f64)> = (0..gps.len())
2103        .filter(|&row| changed(Some(gps), row) || changed(latitude, row) || changed(longitude, row))
2104        .map(|row| (pressure_altitude[row], gps[row]))
2105        .filter(|(p, g)| p.is_finite() && g.is_finite())
2106        .collect();
2107    let (low, high) = fixes
2108        .iter()
2109        .fold((f64::INFINITY, f64::NEG_INFINITY), |(l, h), (p, _)| {
2110            (l.min(*p), h.max(*p))
2111        });
2112    match slope(fixes.iter().copied()) {
2113        Some(gain) if fixes.len() >= MIN_FIXES && high - low >= MIN_CLIMB_M => format!(
2114            "at its {} fresh fixes it rises {gain:.3} times as fast as the pressure altitude, \
2115             near 1 in meters and 3.28 in feet. But that is one check. {WHY}",
2116            fixes.len()
2117        ),
2118        _ => format!(
2119            "its {} fresh fixes span too little of the pressure altitude to compare. {WHY}",
2120            fixes.len()
2121        ),
2122    }
2123}
2124
2125#[cfg(test)]
2126mod tests {
2127    use super::*;
2128
2129    /// The invented newer-layout flight the integration tests read too: 5 rows a second, a pad,
2130    /// 1 s of boost at 15 g net, a drag-free coast to 1,176.8 m at 16 s, a descent at 15 m/s.
2131    const NEWER: &str = include_str!("../tests/samples/phone-launch-newer.csv");
2132    /// The invented older-layout file: 24 rows at 20 Hz on the pad, the phone turning.
2133    const OLDER: &str = include_str!("../tests/samples/phone-launch-older.csv");
2134
2135    /// One row of a file being edited: its names and its cells as written.
2136    struct Row<'a> {
2137        names: &'a [&'a str],
2138        cells: Vec<String>,
2139    }
2140
2141    impl Row<'_> {
2142        fn at(&self, name: &str) -> usize {
2143            self.names.iter().position(|n| *n == name).unwrap()
2144        }
2145
2146        fn get(&self, name: &str) -> f64 {
2147            self.cells[self.at(name)].parse().unwrap()
2148        }
2149
2150        fn set(&mut self, name: &str, value: f64) {
2151            let at = self.at(name);
2152            self.cells[at] = format!("{value}");
2153        }
2154
2155        fn scale(&mut self, name: &str, factor: f64) {
2156            let value = self.get(name);
2157            self.set(name, value * factor);
2158        }
2159    }
2160
2161    /// The text with each data row edited, its header kept.
2162    fn edit(text: &str, change: impl Fn(&mut Row<'_>)) -> String {
2163        let mut lines = text.lines();
2164        let header = lines.next().unwrap();
2165        let names: Vec<&str> = header.split(',').collect();
2166        let mut out = vec![header.to_owned()];
2167        for line in lines {
2168            let mut row = Row {
2169                names: &names,
2170                cells: line.split(',').map(str::to_owned).collect(),
2171            };
2172            change(&mut row);
2173            out.push(row.cells.join(","));
2174        }
2175        out.join("\n") + "\n"
2176    }
2177
2178    /// The columns scaled by a factor on every row.
2179    fn scaled(text: &str, names: &[&str], factor: f64) -> String {
2180        edit(text, |row| {
2181            for name in names {
2182                row.scale(name, factor);
2183            }
2184        })
2185    }
2186
2187    fn log_of(text: &str) -> FlightLog {
2188        read(text).unwrap()
2189    }
2190
2191    /// The one note that opens as given.
2192    fn note<'a>(log: &'a FlightLog, start: &str) -> &'a str {
2193        let matching: Vec<&String> = log.notes.iter().filter(|n| n.starts_with(start)).collect();
2194        assert_eq!(matching.len(), 1, "{start}: {:#?}", log.notes);
2195        matching[0]
2196    }
2197
2198    fn quantity(log: &FlightLog, column: &str) -> Quantity {
2199        log.channels
2200            .iter()
2201            .find(|c| c.column == column)
2202            .unwrap()
2203            .quantity
2204    }
2205
2206    fn values<'a>(log: &'a FlightLog, column: &str) -> &'a [f64] {
2207        &log.channels
2208            .iter()
2209            .find(|c| c.column == column)
2210            .unwrap()
2211            .values
2212    }
2213
2214    /// The syntax error's line and message.
2215    fn refusal(text: &str) -> (usize, String) {
2216        match read(text) {
2217            Err(LogError::Syntax { line, message, .. }) => (line, message),
2218            other => panic!("expected a syntax error, got {other:?}"),
2219        }
2220    }
2221
2222    /// The pressure, hPa, at a height above the invented pad, 100 m above sea level, by the
2223    /// altimeter formula.
2224    fn pressure_hpa(height_m: f64) -> f64 {
2225        1013.25 * (1.0 - (100.0 + height_m) / 44_330.8).powf(1.0 / 0.190_263)
2226    }
2227
2228    #[test]
2229    fn recognizes_the_header_and_nothing_else() {
2230        assert!(recognizes(NEWER));
2231        assert!(recognizes(OLDER));
2232        assert!(recognizes("\n  Seconds,Timestamp,AltitudeMSLGPS,Record\n"));
2233        assert!(!recognizes("Timestamp,Seconds,AltitudeMSLGPS\n"));
2234        assert!(!recognizes("time (ms);deltaTime (ms)\n"));
2235        assert!(matches!(
2236            read("Time,Altitude\n0,0\n"),
2237            Err(LogError::NotThisFormat { .. })
2238        ));
2239        assert!(matches!(read(""), Err(LogError::NotThisFormat { .. })));
2240    }
2241
2242    /// Each refusal, pinned by its line and words.
2243    #[test]
2244    fn malformed_files_are_refused_by_line() {
2245        let header = "Seconds,Timestamp,AltitudeMSLGPS,GTotal";
2246        assert_eq!(
2247            refusal(&format!("{header},\n0,1,2,3,4\n")),
2248            (1, "the header's column 5 has no name".to_owned())
2249        );
2250        assert_eq!(
2251            refusal(&format!("{header},GTotal\n")),
2252            (1, "the header names \"GTotal\" twice".to_owned())
2253        );
2254        assert_eq!(
2255            refusal(&format!("{header}\n0,1,2,3\n\n0.05,1,2\n")),
2256            (4, "the row has 3 cells where the header names 4".to_owned())
2257        );
2258        assert_eq!(
2259            refusal(&format!("{header}\n0,1,2,3,4\n")),
2260            (
2261                2,
2262                "the row has more than 4 cells where the header names 4".to_owned()
2263            )
2264        );
2265        assert_eq!(
2266            refusal(&format!("{header}\n0,1,2,x\n")),
2267            (2, "\"x\" under \"GTotal\" isn't a number".to_owned())
2268        );
2269        assert_eq!(
2270            refusal(&format!("{header}\n,1,2,3\n")),
2271            (2, "the row has no time".to_owned())
2272        );
2273        assert_eq!(
2274            refusal(&format!("{header}\n0.1,1,2,3\n0.1,1,2,3\n")),
2275            (
2276                3,
2277                "the time 0.1 s doesn't come after the one above it, 0.1 s".to_owned()
2278            )
2279        );
2280        assert_eq!(
2281            read(&format!("{header}\n\n")),
2282            Err(LogError::NoData { format: FORMAT })
2283        );
2284    }
2285
2286    /// The infinite-value rule (ADR-258 §3, #518): a finite cell its conversion overflows is
2287    /// refused on its line.
2288    #[test]
2289    fn a_value_its_conversion_overflows_is_refused_by_line() {
2290        let text = edit(NEWER, |row| {
2291            if row.get("Seconds") == 3.0 {
2292                row.set("hPa", 1.7e308);
2293            }
2294        });
2295        // The header is line 1 and the rows start at -2.0 s, 5 a second: 3.0 s is line 27.
2296        assert_eq!(
2297            refusal(&text),
2298            (
2299                27,
2300                "the column hPa holds a value too large for its unit's conversion to SI".to_owned()
2301            )
2302        );
2303        // As written, the same cell under a column kept as written is a value like any other.
2304        let kept = edit(NEWER, |row| {
2305            if row.get("Seconds") == 3.0 {
2306                row.set("UsbV1", 1.7e308);
2307            }
2308        });
2309        assert_eq!(values(&log_of(&kept), "UsbV1")[25], 1.7e308);
2310    }
2311
2312    /// The newer flight: every unit two checks settle is read, the primary track filled.
2313    #[test]
2314    fn the_newer_flight_reads_its_units() {
2315        let log = log_of(NEWER);
2316        assert_eq!(log.format, LogFormat::PhoneLaunchCsv);
2317        assert_eq!(log.time_s.len(), 111);
2318        for (column, expected) in [
2319            ("Timestamp", Quantity::TimeOfDay),
2320            ("AltitudeMSLGPS", Quantity::Unrecognized),
2321            ("AltitudeMSLMaxGPS", Quantity::Unrecognized),
2322            ("Record", Quantity::Number),
2323            ("Latitude", Quantity::Latitude),
2324            ("Longitude", Quantity::Longitude),
2325            ("GTotal", Quantity::Acceleration),
2326            ("Gx", Quantity::Acceleration),
2327            ("GMax", Quantity::Unrecognized),
2328            ("MagnetometerX", Quantity::Unrecognized),
2329            ("GyroY", Quantity::AngularRate),
2330            ("Heading", Quantity::Angle),
2331            ("Yaw", Quantity::Angle),
2332            ("Pitch", Quantity::Angle),
2333            ("Roll", Quantity::Angle),
2334            ("AltitudeRelativeBarometer", Quantity::AltitudeAboveGround),
2335            ("Temperature", Quantity::Unrecognized),
2336            ("NoGravGz", Quantity::Acceleration),
2337            ("NoGravGTotal", Quantity::Acceleration),
2338            ("VelocityBarometer", Quantity::Speed),
2339            ("VelocityBarometerRaw", Quantity::Speed),
2340            ("VelocityBarometerMin", Quantity::Unrecognized),
2341            ("hPa", Quantity::Pressure),
2342            ("Satellites", Quantity::Number),
2343            ("UsbUR8", Quantity::Unrecognized),
2344        ] {
2345            assert_eq!(quantity(&log, column), expected, "{column}");
2346        }
2347        // The highest altitude, 1,176.8 m at 16 s, row 90; the speed there 0.
2348        let top = log
2349            .altitude_m
2350            .iter()
2351            .copied()
2352            .fold(f64::NEG_INFINITY, f64::max);
2353        assert!((top - 1_176.8).abs() < 0.1, "{top}");
2354        assert_eq!(log.vertical_speed_m_s.as_ref().unwrap()[90], 0.0);
2355        // Conversions: 1 g, a pitch of 1.2 rad, 1,000 °/s, the pad's pressure in Pa, 08:00 UTC.
2356        assert!((values(&log, "GTotal")[0] - G0_M_S2).abs() < 1e-9);
2357        assert!((values(&log, "Pitch")[0] - 1.2_f64.to_degrees()).abs() < 1e-9);
2358        assert!((values(&log, "GyroY")[11] - 1_000.0).abs() < 1e-3);
2359        assert!((values(&log, "hPa")[0] - pressure_hpa(0.0) * 100.0).abs() < 0.01);
2360        assert_eq!(values(&log, "Timestamp")[0], 28_800.0);
2361        assert_eq!(values(&log, "Timestamp")[5], 28_801.0);
2362        assert!(note(&log, "`AltitudeMSLGPS`").contains("rises 1.000 times"));
2363    }
2364
2365    /// The barometric altitude in feet fails the pressure check and is kept as written; so is
2366    /// the speed checked against it, and the primary track is empty.
2367    #[test]
2368    fn a_barometric_altitude_in_feet_is_kept_as_written() {
2369        let log = log_of(&scaled(NEWER, &["AltitudeRelativeBarometer"], 1.0 / FOOT_M));
2370        assert_eq!(
2371            quantity(&log, "AltitudeRelativeBarometer"),
2372            Quantity::Unrecognized
2373        );
2374        let kept = note(&log, "`AltitudeRelativeBarometer` kept as written");
2375        assert!(kept.contains("at most 2684."), "{kept}");
2376        assert!(log.altitude_m.is_empty() && log.time_s.is_empty());
2377        assert_eq!(quantity(&log, "VelocityBarometer"), Quantity::Unrecognized);
2378        assert_eq!(quantity(&log, "Gx"), Quantity::Unrecognized);
2379        assert!(note(&log, "the primary track is empty").contains("withhold"));
2380    }
2381
2382    /// An altitude that the pressure bears out but whose coast slows by 0.3 g, less than gravity
2383    /// alone, fails the second check and is kept as written.
2384    #[test]
2385    fn a_coast_slower_than_gravity_keeps_the_altitude_as_written() {
2386        let text = edit(NEWER, |row| {
2387            let height = 0.3 * row.get("AltitudeRelativeBarometer");
2388            row.set("AltitudeRelativeBarometer", height);
2389            row.set("hPa", pressure_hpa(height));
2390        });
2391        let log = log_of(&text);
2392        let kept = note(&log, "`AltitudeRelativeBarometer` kept as written");
2393        assert!(
2394            kept.contains("(1) above the first row it lies at most 0.0"),
2395            "{kept}"
2396        );
2397        assert!(kept.contains("0.30 g in meters"), "{kept}");
2398        assert_eq!(
2399            quantity(&log, "AltitudeRelativeBarometer"),
2400            Quantity::Unrecognized
2401        );
2402    }
2403
2404    /// A pressure the pad refutes as hPa, kilopascals, is kept as written, and nothing that needs
2405    /// it is read.
2406    #[test]
2407    fn a_pressure_in_kilopascals_is_kept_as_written() {
2408        let log = log_of(&scaled(NEWER, &["hPa"], 0.1));
2409        assert!(
2410            note(&log, "`hPa` kept as written").contains("100.13, outside the 500 to 1100 hPa")
2411        );
2412        assert_eq!(quantity(&log, "hPa"), Quantity::Unrecognized);
2413        assert!(
2414            note(&log, "`AltitudeRelativeBarometer` kept as written")
2415                .contains("(1) the file has no pressure read in hPa")
2416        );
2417        assert!(note(&log, "`AltitudeMSLGPS`").contains("no pressure in hPa"));
2418    }
2419
2420    /// A speed in feet per second fails both its checks; a speed flat through the coast fails the
2421    /// second alone.
2422    #[test]
2423    fn a_speed_is_read_only_where_both_checks_hold() {
2424        let log = log_of(&scaled(NEWER, &["VelocityBarometer"], 1.0 / FOOT_M));
2425        assert_eq!(quantity(&log, "VelocityBarometer"), Quantity::Unrecognized);
2426        assert!(log.vertical_speed_m_s.is_none());
2427        let kept = note(&log, "`VelocityBarometer` kept as written");
2428        assert!(kept.contains("gain of 3.28"), "{kept}");
2429        let flat = edit(NEWER, |row| {
2430            let t = row.get("Seconds");
2431            if (7.0..=14.0).contains(&t) {
2432                row.set("VelocityBarometer", 40.0);
2433            }
2434        });
2435        let log = log_of(&flat);
2436        let kept = note(&log, "`VelocityBarometer` kept as written");
2437        assert!(kept.contains("where 0.8 to 1.25 reads m/s. (2)"), "{kept}");
2438        assert!(kept.contains("slows by 0.00"), "{kept}");
2439        assert_eq!(quantity(&log, "VelocityBarometerRaw"), Quantity::Speed);
2440    }
2441
2442    /// Accelerations in m/s² fail the pad's 1 g; a boost three times the barometer's fails the
2443    /// second check alone.
2444    #[test]
2445    fn accelerations_are_read_in_g_only_where_both_checks_hold() {
2446        let all = [
2447            "GTotal",
2448            "Gx",
2449            "Gy",
2450            "Gz",
2451            "NoGravGTotal",
2452            "NoGravGx",
2453            "NoGravGy",
2454            "NoGravGz",
2455        ];
2456        let log = log_of(&scaled(NEWER, &all, G0_M_S2));
2457        let kept = note(&log, "`Gx`, `Gy`, `Gz` and `GTotal` kept as written");
2458        assert!(
2459            kept.contains("reads 9.705 in the axes' magnitude"),
2460            "{kept}"
2461        );
2462        assert_eq!(quantity(&log, "Gx"), Quantity::Unrecognized);
2463        assert_eq!(quantity(&log, "NoGravGx"), Quantity::Unrecognized);
2464        let boost = edit(NEWER, |row| {
2465            let t = row.get("Seconds");
2466            if t > 0.0 && t <= 1.0 {
2467                for name in ["GTotal", "Gx", "Gy", "Gz"] {
2468                    row.scale(name, 3.0);
2469                }
2470            }
2471        });
2472        let log = log_of(&boost);
2473        let kept = note(&log, "`Gx`, `Gy`, `Gz` and `GTotal` kept as written");
2474        // The phone turns on the invented pad, so the quietest second's mean reads a little
2475        // under 1.
2476        assert!(kept.contains("reads 0.990"), "{kept}");
2477        assert!(
2478            kept.contains("3.21 times the barometric climb rate"),
2479            "{kept}"
2480        );
2481    }
2482
2483    /// `GTotal` that isn't the axes' magnitude is kept apart from them.
2484    #[test]
2485    fn a_total_that_isnt_the_axes_magnitude_is_kept() {
2486        let log = log_of(&scaled(NEWER, &["GTotal"], 2.0));
2487        assert!(note(&log, "`GTotal` kept as written").contains("up to 16.000"));
2488        assert_eq!(quantity(&log, "GTotal"), Quantity::Unrecognized);
2489        assert_eq!(quantity(&log, "Gx"), Quantity::Acceleration);
2490    }
2491
2492    /// The yaw turned by a whole number of turns and half a radian, so that it stays within ±π.
2493    fn yaw_turned(text: &str, by: f64) -> String {
2494        edit(text, |row| {
2495            let yaw = row.get("Yaw");
2496            row.set(
2497                "Yaw",
2498                (yaw + by + std::f64::consts::PI).rem_euclid(std::f64::consts::TAU)
2499                    - std::f64::consts::PI,
2500            );
2501        })
2502    }
2503
2504    /// The yaw in degrees fails its range; a yaw half a radian off the magnetic field's azimuth
2505    /// fails the second check alone, and the gyroscope then can't be read either. The field's
2506    /// direction, not its size, is checked: a magnetometer in another unit changes nothing.
2507    #[test]
2508    fn a_yaw_is_read_in_radians_only_where_both_checks_hold() {
2509        let log = log_of(&scaled(NEWER, &["Yaw"], 180.0 / std::f64::consts::PI));
2510        assert!(
2511            note(&log, "`Yaw` kept as written").contains("its largest reading from 0 is 177.98")
2512        );
2513        assert_eq!(quantity(&log, "Yaw"), Quantity::Unrecognized);
2514        assert!(
2515            note(&log, "`GyroX`, `GyroY` and `GyroZ` kept as written")
2516                .contains("isn't all read in radians")
2517        );
2518        let log = log_of(&yaw_turned(NEWER, 0.5));
2519        let kept = note(&log, "`Yaw` kept as written");
2520        assert!(
2521            kept.contains("(1) its largest reading from 0 is 3.1"),
2522            "{kept}"
2523        );
2524        assert!(kept.contains("by a median 0.500 read in radians"), "{kept}");
2525        assert_eq!(quantity(&log, "Yaw"), Quantity::Unrecognized);
2526        let names = ["MagnetometerX", "MagnetometerY", "MagnetometerZ"];
2527        let log = log_of(&scaled(NEWER, &names, 1_000.0));
2528        let read = note(&log, "`Yaw` read in radians");
2529        assert!(read.contains("by a median 0.000 read in radians"), "{read}");
2530        // Without a magnetometer the second check can't run, and fails.
2531        let log = log_of(&scaled(NEWER, &names, 0.0));
2532        assert!(
2533            note(&log, "`Yaw` kept as written").contains("(2) Only 0 rows are where"),
2534            "{:#?}",
2535            log.notes
2536        );
2537    }
2538
2539    /// The pitch in degrees fails its range; a pitch gravity denies fails the second check alone.
2540    #[test]
2541    fn a_pitch_is_read_in_radians_only_where_both_checks_hold() {
2542        let log = log_of(&scaled(NEWER, &["Pitch"], 180.0 / std::f64::consts::PI));
2543        assert_eq!(quantity(&log, "Pitch"), Quantity::Unrecognized);
2544        let log = log_of(&edit(NEWER, |row| row.set("Pitch", 0.5)));
2545        let kept = note(&log, "`Pitch` kept as written");
2546        assert!(
2547            kept.contains("(1) its largest reading from 0 is 0.5000"),
2548            "{kept}"
2549        );
2550        assert!(kept.contains("by a median 0.453 read in radians"), "{kept}");
2551    }
2552
2553    /// The roll in degrees fails its range; a roll half a radian off gravity's direction fails the
2554    /// second check alone. Gravity's direction, not its size, is checked: an accelerometer in m/s²
2555    /// changes nothing.
2556    #[test]
2557    fn a_roll_is_read_in_radians_only_where_both_checks_hold() {
2558        let log = log_of(&scaled(NEWER, &["Roll"], 180.0 / std::f64::consts::PI));
2559        let kept = note(&log, "`Roll` kept as written");
2560        assert!(
2561            kept.contains("its largest reading from 0 is 178.76"),
2562            "{kept}"
2563        );
2564        assert_eq!(quantity(&log, "Roll"), Quantity::Unrecognized);
2565        let log = log_of(&edit(NEWER, |row| {
2566            let roll = row.get("Roll");
2567            row.set("Roll", roll * 0.5);
2568        }));
2569        let kept = note(&log, "`Roll` kept as written");
2570        assert!(
2571            kept.contains("(1) its largest reading from 0 is 1.5"),
2572            "{kept}"
2573        );
2574        assert!(kept.contains("differs from gravity's about y"), "{kept}");
2575        assert_eq!(quantity(&log, "Roll"), Quantity::Unrecognized);
2576        let axes = ["Gx", "Gy", "Gz", "GTotal"];
2577        let log = log_of(&scaled(NEWER, &axes, G0_M_S2));
2578        let read = note(&log, "`Roll` read in radians");
2579        assert!(read.contains("by a median 0.000 read in radians"), "{read}");
2580    }
2581
2582    /// The gyroscope in degrees per second fails the attitude's turn; one that stops at no
2583    /// gyroscope's full scale, 15 rad/s or 859 °/s, fails the second check alone.
2584    #[test]
2585    fn a_gyroscope_is_read_in_rad_per_s_only_where_both_checks_hold() {
2586        let names = ["GyroX", "GyroY", "GyroZ"];
2587        let log = log_of(&scaled(NEWER, &names, 180.0 / std::f64::consts::PI));
2588        let kept = note(&log, "`GyroX`, `GyroY` and `GyroZ` kept as written");
2589        // Summed in degrees, no span's rate falls in the turns the attitude can measure.
2590        assert!(kept.contains("(1) only 0 spans of 5 rows"), "{kept}");
2591        let log = log_of(&edit(NEWER, |row| {
2592            if row.get("GyroY") > 10.0 {
2593                row.set("GyroY", 15.0);
2594            }
2595        }));
2596        let kept = note(&log, "`GyroX`, `GyroY` and `GyroZ` kept as written");
2597        assert!(
2598            kept.contains("where 0.7 to 1.3 reads rad/s. (2) No axis stops"),
2599            "{kept}"
2600        );
2601        assert!(kept.contains("`GyroY` 15.0000 on 5 rows"), "{kept}");
2602        assert_eq!(quantity(&log, "GyroY"), Quantity::Unrecognized);
2603    }
2604
2605    /// The heading in radians or gradians fails its range; one that never crosses north fails the
2606    /// second check alone.
2607    #[test]
2608    fn a_heading_is_read_in_degrees_only_where_both_checks_hold() {
2609        let log = log_of(&scaled(NEWER, &["Heading"], std::f64::consts::PI / 180.0));
2610        assert_eq!(quantity(&log, "Heading"), Quantity::Unrecognized);
2611        let log = log_of(&scaled(NEWER, &["Heading"], 400.0 / 360.0));
2612        assert!(note(&log, "`Heading` kept as written").contains("don't lie within 0 to 360"));
2613        let log = log_of(&edit(NEWER, |row| {
2614            let t = row.get("Seconds");
2615            row.set("Heading", 180.0 + t);
2616        }));
2617        assert!(note(&log, "`Heading` kept as written").contains("(2) 0 steps between rows"));
2618    }
2619
2620    /// The wrap's period, by hand: a heading turning 10 a row that crosses north as degrees do,
2621    /// from 350 to 0, passes; one that crosses as gradians do, from 390 to 0, steps by 30 less
2622    /// than 360 against the turn and fails, as does a jump against the turn on either side.
2623    #[test]
2624    fn the_headings_wrap_has_the_period_of_degrees() {
2625        assert!(heading_wraps(&[330.0, 340.0, 350.0, 0.0, 10.0, 20.0]).0);
2626        assert!(heading_wraps(&[30.0, 20.0, 10.0, 0.0, 350.0, 340.0]).0);
2627        assert!(!heading_wraps(&[370.0, 380.0, 390.0, 0.0, 10.0, 20.0]).0);
2628        assert!(!heading_wraps(&[30.0, 20.0, 10.0, 0.0, 390.0, 380.0]).0);
2629        // The step, less 360, must turn as the rows on either side do, and as fast within 2.
2630        assert!(!heading_wraps(&[360.0, 350.0, 340.0, 0.0, 10.0, 20.0]).0);
2631        assert!(!heading_wraps(&[349.0, 350.0, 351.0, 9.0, 10.0, 11.0]).0);
2632        assert!(!heading_wraps(&[340.0, 350.0, 350.0, 0.0, 10.0, 20.0]).0);
2633    }
2634
2635    /// A position in radians fails both checks; a latitude near the equator fails the first alone
2636    /// and a longitude near the prime meridian the second, and either keeps both, never guessed;
2637    /// so does a latitude in NMEA's degrees and minutes. A pair in degrees and minutes within 1°
2638    /// of (0°, 0°), open sea, passes both: the limit the module docs state.
2639    #[test]
2640    fn a_position_is_read_in_degrees_only_where_both_checks_hold() {
2641        let both = ["Latitude", "Longitude"];
2642        let log = log_of(&scaled(NEWER, &both, std::f64::consts::PI / 180.0));
2643        let kept = note(&log, "`Latitude` and `Longitude` kept as written");
2644        assert!(
2645            kept.contains("(1) the latitude's largest reading from 0 is 0.7"),
2646            "{kept}"
2647        );
2648        assert_eq!(quantity(&log, "Latitude"), Quantity::Unrecognized);
2649        assert_eq!(quantity(&log, "Longitude"), Quantity::Unrecognized);
2650        for (name, value) in [
2651            ("Latitude", 1.25),
2652            ("Longitude", -3.0),
2653            ("Latitude", 4006.0),
2654        ] {
2655            let log = log_of(&edit(NEWER, |row| row.set(name, value)));
2656            assert!(
2657                note(&log, "`Latitude` and `Longitude` kept as written")
2658                    .contains(&format!("largest reading from 0 is {:.1}", value.abs()))
2659            );
2660            assert_eq!(quantity(&log, "Latitude"), Quantity::Unrecognized, "{name}");
2661            assert_eq!(
2662                quantity(&log, "Longitude"),
2663                Quantity::Unrecognized,
2664                "{name}"
2665            );
2666        }
2667        let log = log_of(&edit(NEWER, |row| {
2668            row.set("Latitude", 30.0);
2669            row.set("Longitude", 112.0);
2670        }));
2671        assert_eq!(quantity(&log, "Latitude"), Quantity::Latitude);
2672        assert_eq!(quantity(&log, "Longitude"), Quantity::Longitude);
2673        let mut lines: Vec<String> = NEWER.lines().map(str::to_owned).collect();
2674        for line in &mut lines {
2675            let mut cells: Vec<&str> = line.split(',').collect();
2676            cells.remove(6);
2677            *line = cells.join(",");
2678        }
2679        let log = log_of(&(lines.join("\n") + "\n"));
2680        let kept = note(&log, "`Latitude` kept as written");
2681        assert!(kept.contains("(2) The file has no `Longitude`"), "{kept}");
2682    }
2683
2684    /// A time stamp in seconds fails both checks; one whose steps stray from the clock's fails
2685    /// the first alone.
2686    #[test]
2687    fn a_time_stamp_is_read_only_where_both_checks_hold() {
2688        let log = log_of(&scaled(NEWER, &["Timestamp"], 0.001));
2689        let kept = note(&log, "`Timestamp` kept as written");
2690        assert!(
2691            kept.contains("doesn't fall between 2000 and 2100"),
2692            "{kept}"
2693        );
2694        assert_eq!(quantity(&log, "Timestamp"), Quantity::Unrecognized);
2695        let log = log_of(&edit(NEWER, |row| {
2696            if row.get("Seconds") == 4.0 {
2697                row.scale("Timestamp", 1.0);
2698                let stamp = row.get("Timestamp");
2699                row.set("Timestamp", stamp + 20.0);
2700            }
2701        }));
2702        let kept = note(&log, "`Timestamp` kept as written");
2703        assert!(kept.contains("at most 0.0200 s from"), "{kept}");
2704        assert!(kept.contains("(2) Its first reading, read in ms since 1970, falls"));
2705    }
2706
2707    /// The GPS altitude is kept as written whatever its gain: in meters the one check passes, in
2708    /// feet it doesn't, and one check never settles a unit.
2709    #[test]
2710    fn the_gps_altitude_is_kept_as_written_either_way() {
2711        let log = log_of(NEWER);
2712        assert_eq!(quantity(&log, "AltitudeMSLGPS"), Quantity::Unrecognized);
2713        assert!(note(&log, "`AltitudeMSLGPS` and `AltitudeMSLMaxGPS` kept as written, in a unit no second check settles: at its 23 fresh fixes it rises 1.000").contains("that is one check"));
2714        let feet = scaled(
2715            NEWER,
2716            &["AltitudeMSLGPS", "AltitudeMSLMaxGPS"],
2717            1.0 / FOOT_M,
2718        );
2719        let log = log_of(&feet);
2720        assert_eq!(quantity(&log, "AltitudeMSLGPS"), Quantity::Unrecognized);
2721        assert!(note(&log, "`AltitudeMSLGPS`").contains("rises 3.281 times"));
2722        assert_eq!(values(&log, "AltitudeMSLGPS")[0], 100.0 / FOOT_M);
2723    }
2724
2725    /// The older file: no barometer, so the accelerations' second check can't run and the primary
2726    /// track is empty; the attitude, heading, position and time stamp are read.
2727    #[test]
2728    fn the_older_layout_keeps_what_it_cant_check() {
2729        let log = log_of(OLDER);
2730        assert!(log.altitude_m.is_empty() && log.time_s.is_empty());
2731        assert!(
2732            note(
2733                &log,
2734                "`AltitudeRelativeBarometer` kept as written, holding no reading"
2735            )
2736            .contains("0 on every row")
2737        );
2738        let kept = note(&log, "`Gx`, `Gy`, `Gz` and `GTotal` kept as written");
2739        assert!(
2740            kept.contains("(1) the quietest second, to 1.15 s, reads 0.999"),
2741            "{kept}"
2742        );
2743        assert!(
2744            kept.contains("(2) The file has no barometric climb"),
2745            "{kept}"
2746        );
2747        for (column, expected) in [
2748            ("Yaw", Quantity::Angle),
2749            ("Pitch", Quantity::Angle),
2750            ("Roll", Quantity::Angle),
2751            ("Heading", Quantity::Angle),
2752            ("GyroX", Quantity::Unrecognized),
2753            ("Timestamp", Quantity::TimeOfDay),
2754            ("AltitudeRelativeSerial", Quantity::Unrecognized),
2755            ("Battery", Quantity::Unrecognized),
2756        ] {
2757            assert_eq!(quantity(&log, column), expected, "{column}");
2758        }
2759        assert_eq!(log.channels.len(), 34);
2760    }
2761
2762    /// A column the reader doesn't know is kept as written with a note; an empty cell is a gap.
2763    #[test]
2764    fn unknown_columns_and_empty_cells() {
2765        let mut lines: Vec<String> = OLDER.lines().map(str::to_owned).collect();
2766        lines[0].push_str(",Mystery");
2767        for line in &mut lines[1..] {
2768            line.push_str(",7");
2769        }
2770        lines[3] = lines[3].replacen(",0.85,", ",,", 1);
2771        let log = log_of(&(lines.join("\n") + "\n"));
2772        assert_eq!(quantity(&log, "Mystery"), Quantity::Unrecognized);
2773        assert!(note(&log, "`Mystery` kept as written").contains("doesn't know the column"));
2774        assert!(values(&log, "Battery")[2].is_nan());
2775    }
2776
2777    /// The step, by hand: 100 / 16,384 Pa at 1 / (1.225 × 9.806 65) m a pascal, 0.000 508 07 m.
2778    #[test]
2779    fn hundredths_drop_the_sign_of_a_rounded_zero() {
2780        assert_eq!(hundredths(-0.001), "0.00");
2781        assert_eq!(hundredths(-0.01), "-0.01");
2782        assert_eq!(hundredths(9.806), "9.81");
2783    }
2784
2785    #[test]
2786    fn the_altitude_step_is_the_pressures() {
2787        assert!((ALTITUDE_STEP_M - 0.000_508_070).abs() < 1e-9);
2788        assert!(
2789            (LogFormat::PhoneLaunchCsv.altitude_resolution_m() - ALTITUDE_STEP_M).abs() < 1e-15
2790        );
2791    }
2792
2793    /// The fit helpers against hand values.
2794    #[test]
2795    fn the_fits_recover_known_lines_and_parabolas() {
2796        let line: Vec<(f64, f64)> = (0..10).map(|i| (i as f64, 3.0 + 2.0 * i as f64)).collect();
2797        assert!((slope(line.iter().copied()).unwrap() - 2.0).abs() < 1e-12);
2798        let parabola: Vec<(f64, f64)> = (0..10)
2799            .map(|i| {
2800                let t = 100.0 + 0.2 * i as f64;
2801                (t, 5.0 - 3.0 * t - 4.9 * t * t)
2802            })
2803            .collect();
2804        assert!((curvature(&parabola).unwrap() + 9.8).abs() < 1e-6);
2805        assert_eq!(median(vec![3.0, f64::NAN, 1.0, 2.0]), Some(2.0));
2806        assert_eq!(median(vec![4.0, 1.0, 3.0, 2.0]), Some(2.5));
2807        let quarter = attitude(0.0, 0.0, 0.0);
2808        let turned = attitude(0.5, 0.0, 0.0);
2809        assert!((turn(&quarter, &turned) - 0.5).abs() < 1e-12);
2810        assert_eq!(brief(37.200_000_762_939_45), "37.2");
2811        assert_eq!(brief(-0.000_1), "0");
2812    }
2813}