1use 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
192const FORMAT: &str = "phone app launch CSV";
194
195const PRESSURE_STEP_HPA: f64 = 1.0 / 16_384.0;
197
198pub const ALTITUDE_STEP_M: f64 = PRESSURE_STEP_HPA * 100.0 / (1.225 * G0_M_S2);
203
204const G0_M_S2: f64 = 9.806_65;
206
207const FOOT_M: f64 = 0.3048;
209
210const PAD_HPA: (f64, f64) = (500.0, 1_100.0);
212
213const MIN_CLIMB_M: f64 = 30.0;
215
216const PRESSURE_MATCH: (f64, f64) = (3.0, 0.02);
219
220const COAST_WINDOW_S: (f64, f64) = (8.0, 3.0);
222
223const MIN_COAST: (f64, usize) = (2.0, 10);
225
226const CLIMB_SPAN_S: f64 = 1.0;
229
230const COAST_SLACK: f64 = 0.7;
232
233const SPEED_GAIN: (f64, f64) = (0.8, 1.25);
235
236const RATE_HALF_SPAN_S: f64 = 0.25;
238
239const REST: (f64, usize) = (1.0, 5);
241
242const REST_G: (f64, f64) = (0.9, 1.1);
244
245const BOOST_RATIO: (f64, f64) = (0.5, 2.0);
247
248const TOTAL_TIE_G: f64 = 0.11;
250
251const GRAVITY_TIE_G: (f64, f64) = (0.95, 1.05);
253
254const ATTITUDE_CHECK: (f64, f64, f64, usize) = (0.03, 0.2, 0.2, 20);
259
260const 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
267const FULL_SCALE_SHARE: f64 = 0.99;
269
270const PLATEAU: (usize, f64, usize) = (5, 0.001, 2);
274
275const FULL_SCALES_DPS: [f64; 5] = [125.0, 250.0, 500.0, 1_000.0, 2_000.0];
277
278const FULL_SCALE_MATCH: f64 = 0.01;
280
281const HEADING_WRAP_DEG: f64 = 300.0;
283
284const HEADING_WRAP_FACTOR: f64 = 2.0;
287
288const STAMP_STEP_S: f64 = 0.0015;
290
291const STAMP_RANGE_MS: (f64, f64) = (946_684_800_000.0, 4_102_444_800_000.0);
293
294const MIN_FIXES: usize = 10;
297
298const DAY_S: f64 = 86_400.0;
300
301#[derive(Debug, Clone, Copy, PartialEq, Eq)]
303enum Kind {
304 Clock,
305 Stamp,
306 GpsAltitude,
307 Latitude,
308 Longitude,
309 Axis,
311 Total,
312 NoGravAxis,
314 NoGravTotal,
315 Gyro,
316 Heading,
317 Yaw,
318 Pitch,
319 Roll,
320 BaroAltitude,
321 Pressure,
322 BaroSpeed,
323 Code,
325 Extreme,
327 Temperature,
328 Kept(&'static str),
330 Unknown,
331}
332
333const 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";
336const TURNS: &str = "by name a count of turns or its clock, which no check in the file ties \
338 to the gyroscope";
339const ACCURACY: &str = "by name the GPS's accuracy, in a unit no source states";
341const EXTERNAL: &str = "by name an external sensor's, in a unit no source states";
343const RESERVED: &str = "reserved, by name; no source states what it holds";
345
346fn 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
385pub 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
393struct Column {
395 kind: Kind,
396 name: String,
397}
398
399struct Table {
401 columns: Vec<Column>,
402 lines: Vec<usize>,
403 time_s: Vec<f64>,
404 values: Vec<Vec<f64>>,
406}
407
408impl Table {
409 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 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
428pub 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
465fn 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
490fn 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 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
563fn 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#[derive(Debug, Clone, Copy, PartialEq)]
578enum Convert {
579 AsWritten,
580 Scale(f64),
581 TimeOfDay,
583}
584
585#[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
597struct Outcome {
599 passed: bool,
600 evidence: String,
601}
602
603impl Outcome {
604 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
617fn 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 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 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 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 accelerations(table, coast.as_ref(), &mut notes, &mut set);
781
782 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 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 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 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
990fn 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
1047fn 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
1062fn 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
1073fn 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
1083fn 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
1100fn 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 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 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
1140fn 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
1155struct Coast {
1157 climb: (f64, f64),
1159 window: Vec<usize>,
1161 window_s: (f64, f64),
1162}
1163
1164fn 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
1203fn 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
1213fn 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
1254fn 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
1299fn 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
1320fn quietest(time: &[f64], axes: [&[f64]; 3], before: usize) -> Option<(usize, f64)> {
1323 let n = before.min(time.len());
1324 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
1369fn 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
1539fn 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
1558fn climb_row(time: &[f64], coast: Option<&Coast>) -> Option<usize> {
1561 coast.map(|c| time.partition_point(|t| *t < c.climb.1))
1562}
1563
1564fn 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
1589fn 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
1595fn 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
1601fn 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
1627fn 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
1653fn 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
1685fn 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 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
1741fn 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
1750fn 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
1772fn 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
1780fn 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
1830fn 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 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
1903fn 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
1953fn 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
1970fn counted(count: usize, one: &str, many: &str) -> String {
1972 format!("{count} {}", if count == 1 { one } else { many })
1973}
1974
1975fn 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
1990fn 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
2024fn 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
2047fn 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
2067fn 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
2085fn 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 const NEWER: &str = include_str!("../tests/samples/phone-launch-newer.csv");
2132 const OLDER: &str = include_str!("../tests/samples/phone-launch-older.csv");
2134
2135 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 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 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 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 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 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 #[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 #[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 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 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 #[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 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 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 #[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 #[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 #[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 #[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 #[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 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 #[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 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 #[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 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 #[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 #[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 #[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 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 #[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 #[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 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 #[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 #[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 #[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 #[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 #[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 #[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 #[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(¶bola).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}