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