1use std::collections::{BTreeSet, HashMap, HashSet};
215use std::f64::consts::PI;
216
217use hpr_atmos::ussa76::Ussa76;
218use hpr_core::gravity::STANDARD_GRAVITY_MPS2;
219
220use crate::error::LogError;
221use crate::log::{Channel, FlightLog, LogFormat, Quantity, Stated};
222use crate::table::{self, Notes, numbered_lines, short};
223
224const FORMAT: &str = "AerospaceNU ground station text";
226
227const LOGGER: &str = "AerospaceNU flight computer board, by its ground station";
229
230const RUN_START: &str = "RUN START";
232
233pub const ALTITUDE_STEP_M: f64 = 1.0 / (1.225 * 9.806_65);
239
240pub const MAX_LINE_BYTES: usize = 65_536;
243
244const MAX_DEPTH: usize = 8;
246
247const MAX_ITEMS: usize = 1_024;
249
250pub const MAX_CHANNELS: usize = 1_024;
252
253const MAX_NAME_CHARS: usize = 64;
255
256const MAX_LISTED: usize = 12;
258
259const ATM_PA: f64 = 101_325.0;
261
262const BAR_PA: f64 = 100_000.0;
264
265const PAD_PA: (f64, f64) = (50_000.0, 110_000.0);
267
268const STEP_HOLDS: f64 = 0.99;
270
271const STEP_FAILS: f64 = 0.10;
273
274const STEP_LEAST_VALUES: usize = 10;
276
277const REFERENCE_SHARE: f64 = 0.005;
279
280const STRETCH_GAP_S: f64 = 2.0;
282
283const CLOCK_SPAN_S: f64 = 20.0;
285
286const CLOCK_RATE: (f64, f64) = (0.8, 1.25);
288
289const NEAREST_S: f64 = 0.5;
291
292const CHECK_CLIMB_M: f64 = 10.0;
294
295const MARGIN: f64 = 0.25;
298
299const GRAVITY_FACTOR: f64 = 1.8;
302
303const COAST_STEP_S: f64 = 1.0;
306
307const COAST_LEAST_SAMPLES: usize = 5;
309
310const COAST_LEAST_S: f64 = 1.0;
312
313const LAUNCH_FORCE: f64 = 3.0;
315
316const LAUNCH_HELD: f64 = 2.0;
318
319const PAD_SAMPLES: usize = 10;
321
322const PAD_LEAST_SAMPLES: usize = 3;
324
325const INTEGRATE_STEP_S: f64 = 0.5;
328
329const INTEGRATED_LEAST: usize = 3;
331
332const GRAVITY_MARGIN: f64 = 0.1;
334
335const GRAVITY_LEAST_S: f64 = 1.0;
337
338const GRAVITY_LEAD: f64 = 2.0;
340
341const GRAVITY_STEP_MAX_S: f64 = 1.0;
343
344const TILT_GRAVITY_MARGIN: f64 = 0.05;
347
348const TILT_AGREE_DEG: f64 = 2.0;
350
351const TILT_LEAST_SAMPLES: usize = 10;
353
354const TURN_STEP_S: f64 = 0.3;
356
357const TURN_STEPS: usize = 10;
359
360const TURN_GRAVITY_MARGIN: f64 = 0.15;
362
363const TURN_LEAST_RAD: f64 = 0.2;
365
366const TURN_LEAST_STRETCHES: usize = 3;
368
369const TURN_FACTOR: f64 = 4.0;
371
372const TILT_WINDOW_S: f64 = 2.0;
374
375const TILT_STEP_S: f64 = 0.5;
377
378const TILT_LEAST_RAD: f64 = 10.0 * PI / 180.0;
380
381const TILT_LEAST_WINDOWS: usize = 3;
383
384const TILT_FACTOR: f64 = 2.0;
387
388pub fn recognizes(text: &str) -> bool {
390 text.lines()
391 .map(str::trim)
392 .find(|line| !line.is_empty())
393 .is_some_and(|line| line.starts_with(RUN_START))
394}
395
396#[derive(Debug, Clone, PartialEq)]
402enum Value {
403 Number(f64),
404 Bool(bool),
405 Text(String),
406 Nothing,
408 List(Vec<Value>),
409 Dict(Vec<(String, Value)>),
410}
411
412#[cfg(test)]
414fn parse_literal(text: &str) -> Result<Value, String> {
415 parse_from(text, 0)
416}
417
418fn parse_from(line: &str, start: usize) -> Result<Value, String> {
421 let mut parser = Parser {
422 text: line,
423 at: start,
424 };
425 let value = parser.value(0)?;
426 parser.skip_space();
427 if parser.at < line.len() {
428 return Err(format!(
429 "text after the literal, at character {} of the line",
430 parser.place(parser.at)
431 ));
432 }
433 Ok(value)
434}
435
436struct Parser<'a> {
439 text: &'a str,
440 at: usize,
441}
442
443impl Parser<'_> {
444 fn peek(&self) -> Option<u8> {
445 self.text.as_bytes().get(self.at).copied()
446 }
447
448 fn place(&self, at: usize) -> usize {
450 self.text
451 .get(..at)
452 .map_or(at, |before| before.chars().count())
453 + 1
454 }
455
456 fn skip_space(&mut self) {
457 while matches!(self.peek(), Some(b' ' | b'\t')) {
458 self.at += 1;
459 }
460 }
461
462 fn value(&mut self, depth: usize) -> Result<Value, String> {
463 self.skip_space();
464 match self.peek() {
465 None => Err("the line ends inside the packet".to_owned()),
466 Some(b'{') => self.dict(depth + 1),
467 Some(b'[') => self.list(depth + 1, b']'),
468 Some(b'(') => self.list(depth + 1, b')'),
469 Some(quote @ (b'\'' | b'"')) => self.string(quote).map(Value::Text),
470 Some(b'-' | b'+' | b'.' | b'0'..=b'9') => self.number(),
471 Some(byte) if byte.is_ascii_alphabetic() => self.word(),
472 Some(_) => Err(format!(
473 "an unexpected character at character {} of the line",
474 self.place(self.at)
475 )),
476 }
477 }
478
479 fn open(&mut self, depth: usize) -> Result<(), String> {
481 if depth > MAX_DEPTH {
482 return Err(format!(
483 "lists or dictionaries nested more than {MAX_DEPTH} deep"
484 ));
485 }
486 self.at += 1;
487 Ok(())
488 }
489
490 fn after_item(&mut self, close: u8) -> Result<(), String> {
492 self.skip_space();
493 match self.peek() {
494 Some(b',') => {
495 self.at += 1;
496 Ok(())
497 }
498 Some(byte) if byte == close => Ok(()),
499 _ => Err(format!(
500 "no comma or `{}` after an item, at character {} of the line",
501 close as char,
502 self.place(self.at)
503 )),
504 }
505 }
506
507 fn dict(&mut self, depth: usize) -> Result<Value, String> {
508 self.open(depth)?;
509 let mut entries: Vec<(String, Value)> = Vec::new();
510 let mut keys: HashSet<String> = HashSet::new();
511 loop {
512 self.skip_space();
513 if self.peek() == Some(b'}') {
514 self.at += 1;
515 return Ok(Value::Dict(entries));
516 }
517 if entries.len() >= MAX_ITEMS {
518 return Err(format!("a dictionary of more than {MAX_ITEMS} items"));
519 }
520 let Value::Text(key) = self.value(depth)? else {
521 return Err("a key that isn't text".to_owned());
522 };
523 if !keys.insert(key.clone()) {
524 return Err(format!("the key {:?} given twice", short(&key)));
525 }
526 self.skip_space();
527 if self.peek() != Some(b':') {
528 return Err(format!(
529 "no `:` after a key, at character {} of the line",
530 self.place(self.at)
531 ));
532 }
533 self.at += 1;
534 let value = self.value(depth)?;
535 entries.push((key, value));
536 self.after_item(b'}')?;
537 }
538 }
539
540 fn list(&mut self, depth: usize, close: u8) -> Result<Value, String> {
541 self.open(depth)?;
542 let mut items = Vec::new();
543 loop {
544 self.skip_space();
545 if self.peek() == Some(close) {
546 self.at += 1;
547 return Ok(Value::List(items));
548 }
549 if items.len() >= MAX_ITEMS {
550 return Err(format!("a list of more than {MAX_ITEMS} items"));
551 }
552 items.push(self.value(depth)?);
553 self.after_item(close)?;
554 }
555 }
556
557 fn word(&mut self) -> Result<Value, String> {
559 let start = self.at;
560 while self
561 .peek()
562 .is_some_and(|byte| byte.is_ascii_alphanumeric() || byte == b'_')
563 {
564 self.at += 1;
565 }
566 match &self.text[start..self.at] {
567 "True" => Ok(Value::Bool(true)),
568 "False" => Ok(Value::Bool(false)),
569 "None" => Ok(Value::Nothing),
570 "nan" => Ok(Value::Number(f64::NAN)),
571 "inf" => Ok(Value::Number(f64::INFINITY)),
572 word => Err(format!("the word {:?}, no Python literal", short(word))),
573 }
574 }
575
576 fn number(&mut self) -> Result<Value, String> {
579 let start = self.at;
580 let negative = self.peek() == Some(b'-');
581 if matches!(self.peek(), Some(b'-' | b'+')) {
582 self.at += 1;
583 }
584 if self.peek().is_some_and(|byte| byte.is_ascii_alphabetic()) {
585 return match self.word()? {
586 Value::Number(value) if negative => Ok(Value::Number(-value)),
587 Value::Number(value) => Ok(Value::Number(value)),
588 _ => Err("a sign before a word that isn't a number".to_owned()),
589 };
590 }
591 let mut digits = self.digits();
592 if self.peek() == Some(b'.') {
593 self.at += 1;
594 digits += self.digits();
595 }
596 if digits == 0 {
597 return Err(format!(
598 "a number without digits, at character {} of the line",
599 self.place(start)
600 ));
601 }
602 if matches!(self.peek(), Some(b'e' | b'E')) {
603 self.at += 1;
604 if matches!(self.peek(), Some(b'-' | b'+')) {
605 self.at += 1;
606 }
607 if self.digits() == 0 {
608 return Err(format!(
609 "an exponent without digits, at character {} of the line",
610 self.place(start)
611 ));
612 }
613 }
614 if self
615 .peek()
616 .is_some_and(|byte| byte.is_ascii_alphanumeric() || byte == b'_' || byte == b'.')
617 {
618 return Err(format!(
619 "a number Python wouldn't write, at character {} of the line",
620 self.place(start)
621 ));
622 }
623 self.text[start..self.at]
624 .parse::<f64>()
625 .map(Value::Number)
626 .map_err(|_| {
627 format!(
628 "a number that doesn't parse, at character {} of the line",
629 self.place(start)
630 )
631 })
632 }
633
634 fn digits(&mut self) -> usize {
635 let start = self.at;
636 while self.peek().is_some_and(|byte| byte.is_ascii_digit()) {
637 self.at += 1;
638 }
639 self.at - start
640 }
641
642 fn string(&mut self, quote: u8) -> Result<String, String> {
644 self.at += 1;
645 let mut out = String::new();
646 loop {
647 let Some(c) = self.text[self.at..].chars().next() else {
648 return Err("a text in quotes runs to the line's end".to_owned());
649 };
650 self.at += c.len_utf8();
651 if c == quote as char {
652 return Ok(out);
653 }
654 if c != '\\' {
655 out.push(c);
656 continue;
657 }
658 let Some(escaped) = self.text[self.at..].chars().next() else {
659 return Err("a text in quotes runs to the line's end".to_owned());
660 };
661 self.at += escaped.len_utf8();
662 match escaped {
663 '\\' | '\'' | '"' => out.push(escaped),
664 'n' => out.push('\n'),
665 'r' => out.push('\r'),
666 't' => out.push('\t'),
667 'a' => out.push('\u{7}'),
668 'b' => out.push('\u{8}'),
669 'f' => out.push('\u{c}'),
670 'v' => out.push('\u{b}'),
671 'x' => out.push(self.code(2, 16)?),
672 'u' => out.push(self.code(4, 16)?),
673 'U' => out.push(self.code(8, 16)?),
674 '0'..='7' => {
675 self.at -= 1;
676 out.push(self.code_up_to(3, 8));
677 }
678 other => {
679 out.push('\\');
680 out.push(other);
681 }
682 }
683 }
684 }
685
686 fn code(&mut self, count: usize, radix: u32) -> Result<char, String> {
688 let digits = self.text.get(self.at..self.at + count).unwrap_or("");
689 if digits.len() != count || !digits.chars().all(|c| c.is_digit(radix)) {
690 return Err(format!(
691 "an escape without its {count} digits, at character {} of the line",
692 self.place(self.at)
693 ));
694 }
695 self.at += count;
696 Ok(u32::from_str_radix(digits, radix)
697 .ok()
698 .and_then(char::from_u32)
699 .unwrap_or(char::REPLACEMENT_CHARACTER))
700 }
701
702 fn code_up_to(&mut self, count: usize, radix: u32) -> char {
704 let start = self.at;
705 while self.at - start < count
706 && self
707 .peek()
708 .is_some_and(|byte| (byte as char).is_digit(radix))
709 {
710 self.at += 1;
711 }
712 u32::from_str_radix(&self.text[start..self.at], radix)
713 .ok()
714 .and_then(char::from_u32)
715 .unwrap_or(char::REPLACEMENT_CHARACTER)
716 }
717}
718
719fn time_of_day(text: &str) -> Option<f64> {
721 let mut parts = text.split(':');
722 let (hours, minutes, seconds) = (parts.next()?, parts.next()?, parts.next()?);
723 if parts.next().is_some() {
724 return None;
725 }
726 let whole = |part: &str, below: u32| {
727 (part.len() == 2 && part.bytes().all(|b| b.is_ascii_digit()))
728 .then(|| part.parse::<u32>().ok())
729 .flatten()
730 .filter(|value| *value < below)
731 };
732 let (seconds_whole, fraction) = seconds.split_once('.').unwrap_or((seconds, ""));
733 if !fraction.bytes().all(|b| b.is_ascii_digit()) || fraction.len() > 9 {
734 return None;
735 }
736 let seconds = f64::from(whole(seconds_whole, 60)?)
737 + if fraction.is_empty() {
738 0.0
739 } else {
740 format!("0.{fraction}").parse::<f64>().ok()?
741 };
742 Some(f64::from(whole(hours, 24)?) * 3_600.0 + f64::from(whole(minutes, 60)?) * 60.0 + seconds)
743}
744
745const POSITION: &str = "Position Data";
750const ORIENTATION: &str = "Orientation";
751const ALT_INFO: &str = "Alt Info & Cfg";
752const GROUND_STATION: [&str; 2] = ["Ground Station GPS", "Groundstation Status"];
753const PASSTHROUGH: &str = "GPS Passthrough";
754
755#[derive(Debug, Clone, Copy, PartialEq, Eq)]
757enum Kept {
758 Temperature,
759 Unstated,
761 GnssAltitude,
762 Euler,
763 Magnetic,
764}
765
766#[derive(Debug, Clone, Copy, PartialEq, Eq)]
768enum Role {
769 Clock,
771 Number,
772 TimeOfDay,
773 Barometer,
774 Reference,
775 Altitude,
776 VerticalSpeed,
777 Latitude,
778 Longitude,
779 Accel(usize),
780 Gyro(usize),
781 Tilt,
782 Kept(Kept),
783 Unknown,
784}
785
786fn role_of(packet: &str, field: &str, listed: bool) -> Role {
788 let ground_station = GROUND_STATION.contains(&packet);
789 match field {
790 "time_stamp_ms" if !listed => Role::Clock,
791 "radio_id"
792 | "lqi"
793 | "software_version"
794 | "board_serial_number"
795 | "rssi"
796 | "flash_usage"
797 | "gps_sats"
798 | "ble_client"
799 | "fcb_state_number"
800 if !listed =>
801 {
802 Role::Number
803 }
804 "pyro_continuity" | "pyro_status" | "orientation_quaternion" if listed => Role::Number,
805 "orientation_rpy" if listed && packet == ORIENTATION => Role::Kept(Kept::Euler),
806 _ if listed => Role::Unknown,
807 "gps_time" if packet == POSITION => Role::TimeOfDay,
808 "fcb_altitude" if packet == POSITION => Role::Altitude,
809 "v_speed" if packet == POSITION => Role::VerticalSpeed,
810 "fcb_latitude" if packet == POSITION => Role::Latitude,
811 "fcb_longitude" if packet == POSITION => Role::Longitude,
812 "temperature" if packet == POSITION => Role::Kept(Kept::Temperature),
813 "gps_alt" if packet == POSITION => Role::Kept(Kept::GnssAltitude),
814 "fcb_battery_voltage" | "ground_speed" | "course_over_ground" if packet == POSITION => {
815 Role::Kept(Kept::Unstated)
816 }
817 "accel_x" if packet == ORIENTATION => Role::Accel(0),
818 "accel_y" if packet == ORIENTATION => Role::Accel(1),
819 "accel_z" if packet == ORIENTATION => Role::Accel(2),
820 "rot_vel_x" if packet == ORIENTATION => Role::Gyro(0),
821 "rot_vel_y" if packet == ORIENTATION => Role::Gyro(1),
822 "rot_vel_z" if packet == ORIENTATION => Role::Gyro(2),
823 "angle_vertical" if packet == ORIENTATION => Role::Tilt,
824 "roll" | "pitch" | "yaw" if packet == ORIENTATION => Role::Kept(Kept::Euler),
825 "magnetic_field_x" | "magnetic_field_y" | "magnetic_field_z" if packet == ORIENTATION => {
826 Role::Kept(Kept::Magnetic)
827 }
828 "barometer_pressure" | "barometer_2_pressure" if packet == ALT_INFO => Role::Barometer,
829 "press_ref" if packet == ALT_INFO => Role::Reference,
830 "ground_temp" if packet == ALT_INFO => Role::Kept(Kept::Temperature),
831 "ground_elev" | "pitot_ducer_press" if packet == ALT_INFO => Role::Kept(Kept::Unstated),
832 "gs_baro" if ground_station => Role::Barometer,
833 "gs_lat" if ground_station => Role::Latitude,
834 "gs_lon" if ground_station => Role::Longitude,
835 "gs_temp" if ground_station => Role::Kept(Kept::Temperature),
836 "gs_alt" if ground_station => Role::Kept(Kept::GnssAltitude),
837 "Latitude" if packet == PASSTHROUGH => Role::Latitude,
838 "Longitude" if packet == PASSTHROUGH => Role::Longitude,
839 "Satellites" | "Hdop" if packet == PASSTHROUGH => Role::Number,
840 "GPS Alt" if packet == PASSTHROUGH => Role::Kept(Kept::GnssAltitude),
841 _ => Role::Unknown,
842 }
843}
844
845#[derive(Debug, Clone, Copy)]
851struct Slot {
852 line: usize,
853 received_s: f64,
855 board_ms: Option<f64>,
857 time_s: f64,
859}
860
861struct Build {
863 column: String,
864 packet: String,
865 role: Role,
866 quantity: Quantity,
867 scale: f64,
868 slots: Vec<usize>,
870 values: Vec<f64>,
872}
873
874impl Build {
875 fn quoted(&self) -> String {
876 format!("`{}`", short(&self.column))
877 }
878
879 fn keep(&mut self) {
881 self.quantity = Quantity::Unrecognized;
882 self.scale = 1.0;
883 }
884
885 fn range(&self) -> Option<(f64, f64)> {
887 let mut finite = self.values.iter().copied().filter(|v| v.is_finite());
888 let first = finite.next()?;
889 Some(finite.fold((first, first), |(lo, hi), v| (lo.min(v), hi.max(v))))
890 }
891}
892
893#[derive(Default)]
895struct Counts {
896 corrupt: usize,
897 first_corrupt: Option<usize>,
898 messages: usize,
899 first_message: Option<(usize, String)>,
900 unparsed: usize,
901 first_unparsed: Option<(usize, String)>,
902 other: usize,
903 first_other: Option<usize>,
904 long: usize,
905 first_long: Option<usize>,
906 runs: usize,
907 first_run: Option<usize>,
908 gaps: usize,
909 first_gap: Option<usize>,
910 left_out: usize,
911 first_left_out: Option<usize>,
912 text_fields: BTreeSet<String>,
913 nested_fields: BTreeSet<String>,
914}
915
916#[derive(Default)]
918struct Reading {
919 slots: Vec<Slot>,
920 channels: Vec<Build>,
921 by_name: HashMap<String, usize>,
922 counts: Counts,
923}
924
925impl Reading {
926 fn push(&mut self, packet: &str, field: &str, listed: bool, value: f64, slot: usize) {
928 let line = self.slots[slot].line;
929 let column = format!("{packet}.{field}");
930 let index = if let Some(&index) = self.by_name.get(&column) {
931 index
932 } else {
933 if self.channels.len() >= MAX_CHANNELS {
934 self.counts.left_out += 1;
935 self.counts.first_left_out.get_or_insert(line);
936 return;
937 }
938 let base = if listed {
941 field.rsplit_once('[').map_or(field, |(key, _)| key)
942 } else {
943 field
944 };
945 let role = role_of(packet, base, listed);
946 let quantity = match role {
947 Role::Number => Quantity::Number,
948 Role::TimeOfDay => Quantity::TimeOfDay,
949 _ => Quantity::Unrecognized,
951 };
952 self.channels.push(Build {
953 column: column.clone(),
954 packet: packet.to_owned(),
955 role,
956 quantity,
957 scale: 1.0,
958 slots: Vec::new(),
959 values: Vec::new(),
960 });
961 self.by_name.insert(column, self.channels.len() - 1);
962 self.channels.len() - 1
963 };
964 let value = if value.is_finite() {
965 value
966 } else {
967 self.counts.gaps += 1;
968 self.counts.first_gap.get_or_insert(line);
969 f64::NAN
970 };
971 self.channels[index].slots.push(slot);
972 self.channels[index].values.push(value);
973 }
974
975 fn field(&mut self, packet: &str, key: &str, value: &Value, slot: usize) {
977 if key.chars().count() > MAX_NAME_CHARS {
978 self.counts.left_out += 1;
979 let line = self.slots[slot].line;
980 self.counts.first_left_out.get_or_insert(line);
981 return;
982 }
983 match value {
984 Value::Number(number) => self.push(packet, key, false, *number, slot),
985 Value::Bool(flag) => self.push(packet, key, false, f64::from(u8::from(*flag)), slot),
986 Value::Nothing => self.push(packet, key, false, f64::NAN, slot),
987 Value::Text(text) => {
988 let number = match key {
989 "rssi" | "Hdop" => text.split_whitespace().next().and_then(|n| n.parse().ok()),
990 "gps_time" => text.split_once(' ').and_then(|(_, time)| time_of_day(time)),
991 _ => {
992 if self.counts.text_fields.len() < MAX_LISTED {
993 self.counts.text_fields.insert(key.to_owned());
994 }
995 return;
996 }
997 };
998 self.push(packet, key, false, number.unwrap_or(f64::NAN), slot);
999 }
1000 Value::List(items) => {
1001 let flat = items
1002 .iter()
1003 .all(|item| matches!(item, Value::Number(_) | Value::Bool(_) | Value::Nothing));
1004 if !flat {
1005 if self.counts.nested_fields.len() < MAX_LISTED {
1006 self.counts.nested_fields.insert(key.to_owned());
1007 }
1008 return;
1009 }
1010 for (index, item) in items.iter().enumerate() {
1011 let value = match item {
1012 Value::Number(number) => *number,
1013 Value::Bool(flag) => f64::from(u8::from(*flag)),
1014 _ => f64::NAN,
1015 };
1016 self.push(packet, &format!("{key}[{index}]"), true, value, slot);
1017 }
1018 }
1019 Value::Dict(_) => {
1020 if self.counts.nested_fields.len() < MAX_LISTED {
1021 self.counts.nested_fields.insert(key.to_owned());
1022 }
1023 }
1024 }
1025 }
1026
1027 fn find(&self, packet: &str, role: Role) -> Option<usize> {
1029 self.channels
1030 .iter()
1031 .position(|c| c.packet == packet && c.role == role)
1032 }
1033
1034 fn named(&self, column: &str) -> Option<usize> {
1036 self.by_name.get(column).copied()
1037 }
1038
1039 fn paired(&self, a: usize, b: usize) -> Vec<(usize, f64, f64)> {
1041 let (a, b) = (&self.channels[a], &self.channels[b]);
1042 let (mut i, mut j) = (0, 0);
1043 let mut out = Vec::new();
1044 while i < a.slots.len() && j < b.slots.len() {
1045 match a.slots[i].cmp(&b.slots[j]) {
1046 std::cmp::Ordering::Less => i += 1,
1047 std::cmp::Ordering::Greater => j += 1,
1048 std::cmp::Ordering::Equal => {
1049 out.push((a.slots[i], a.values[i], b.values[j]));
1050 i += 1;
1051 j += 1;
1052 }
1053 }
1054 }
1055 out
1056 }
1057
1058 fn tripled(&self, at: [usize; 3]) -> Vec<(usize, [f64; 3])> {
1060 let first = self.paired(at[0], at[1]);
1061 let third = &self.channels[at[2]];
1062 let mut out = Vec::new();
1063 let mut k = 0;
1064 for (slot, x, y) in first {
1065 while k < third.slots.len() && third.slots[k] < slot {
1066 k += 1;
1067 }
1068 if k < third.slots.len() && third.slots[k] == slot {
1069 out.push((slot, [x, y, third.values[k]]));
1070 }
1071 }
1072 out
1073 }
1074
1075 fn time(&self, slot: usize) -> f64 {
1076 self.slots[slot].time_s
1077 }
1078}
1079
1080pub fn read(text: &str) -> Result<FlightLog, LogError> {
1089 let mut lines = numbered_lines(text);
1090 let mut start = None;
1091 for (number, line) in lines.by_ref() {
1092 let line = line.trim();
1093 if !line.is_empty() {
1094 start = Some((number, line));
1095 break;
1096 }
1097 }
1098 let Some((start_line, first)) = start else {
1099 return Err(not_this("the file is empty".to_owned()));
1100 };
1101 let Some(rest) = first.strip_prefix(RUN_START) else {
1102 return Err(not_this(format!(
1103 "its first line, {:?}, isn't the ground station's `RUN START` line",
1104 short(first)
1105 )));
1106 };
1107 let Some(start_s) = rest.split_whitespace().nth(1).and_then(time_of_day) else {
1108 return Err(syntax(
1109 start_line,
1110 format!(
1111 "the `RUN START` line, {:?}, gives no date and time of day as `YYYY-MM-DD \
1112 HH:MM:SS`",
1113 short(first)
1114 ),
1115 ));
1116 };
1117
1118 let mut reading = Reading::default();
1119 let mut previous_s = start_s;
1120 let mut day_s = 0.0;
1121 for (number, line) in lines {
1122 let line = line.trim();
1123 if line.is_empty() {
1124 continue;
1125 }
1126 if line.len() > MAX_LINE_BYTES {
1127 reading.counts.long += 1;
1128 reading.counts.first_long.get_or_insert(number);
1129 continue;
1130 }
1131 if line.starts_with(RUN_START) {
1132 reading.counts.runs += 1;
1133 reading.counts.first_run.get_or_insert(number);
1134 continue;
1135 }
1136 let Some((clock, rest)) = line
1137 .split_once(": ")
1138 .and_then(|(clock, rest)| Some((time_of_day(clock)?, rest)))
1139 else {
1140 reading.counts.other += 1;
1141 reading.counts.first_other.get_or_insert(number);
1142 continue;
1143 };
1144 if clock + day_s < previous_s - 43_200.0 {
1146 day_s += 86_400.0;
1147 }
1148 previous_s = clock + day_s;
1149 let received_s = previous_s - start_s;
1150 let Some((name, body)) = rest
1151 .split_once(" {")
1152 .map(|(name, body)| (name.trim(), body))
1153 else {
1154 reading.counts.messages += 1;
1155 reading
1156 .counts
1157 .first_message
1158 .get_or_insert_with(|| (number, short(rest)));
1159 continue;
1160 };
1161 let parsed = if name.is_empty() || name.chars().count() > MAX_NAME_CHARS {
1162 Err("no packet name, or one longer than 64 characters".to_owned())
1163 } else {
1164 parse_from(line, line.len() - body.len() - 1)
1166 };
1167 let fields = match parsed {
1168 Ok(Value::Dict(fields)) => fields,
1169 Ok(_) => Vec::new(),
1171 Err(why) => {
1172 reading.counts.unparsed += 1;
1173 reading.counts.first_unparsed.get_or_insert((number, why));
1174 continue;
1175 }
1176 };
1177 let crc = fields.iter().find(|(key, _)| key == "crc");
1178 if crc.is_some_and(|(_, value)| *value != Value::Text("Good".to_owned())) {
1179 reading.counts.corrupt += 1;
1180 reading.counts.first_corrupt.get_or_insert(number);
1181 continue;
1182 }
1183 let board_ms = fields.iter().find_map(|(key, value)| match value {
1184 Value::Number(ms) if key == "time_stamp_ms" && ms.is_finite() => Some(*ms),
1185 _ => None,
1186 });
1187 let slot = reading.slots.len();
1188 reading.slots.push(Slot {
1189 line: number,
1190 received_s,
1191 board_ms,
1192 time_s: received_s,
1193 });
1194 for (key, value) in &fields {
1195 if key != "crc" {
1196 reading.field(name, key, value, slot);
1197 }
1198 }
1199 }
1200 if reading.slots.is_empty() {
1201 return Err(LogError::NoData { format: FORMAT });
1202 }
1203
1204 let mut notes: Vec<String> = Vec::new();
1205 let clock_read = place_on_clock(&mut reading.slots, &mut notes);
1206 decide_pressures(&mut reading, &mut notes);
1207 decide_reference(&mut reading, &mut notes);
1208 decide_altitude(&mut reading, start_line, &mut notes)?;
1209 decide_speed(&mut reading, &mut notes);
1210 let no_fix = decide_positions(&mut reading, &mut notes);
1211 let one_g = decide_accelerometer(&mut reading, &mut notes);
1212 let tilt = decide_tilt(&mut reading, one_g, &mut notes);
1213 decide_gyroscope(&mut reading, one_g, tilt, &mut notes);
1214 notes_on_kept(&mut reading, &mut notes);
1215
1216 let mut channels = Vec::with_capacity(reading.channels.len());
1218 let mut index_of: HashMap<usize, usize> = HashMap::new();
1219 for (at, build) in reading.channels.iter().enumerate() {
1220 if build.role == Role::Clock && clock_read {
1221 continue;
1222 }
1223 let mut values = Vec::with_capacity(build.values.len());
1224 for (value, slot) in build.values.iter().zip(&build.slots) {
1225 values.push(table::finite(
1226 value * build.scale,
1227 FORMAT,
1228 reading.slots[*slot].line,
1229 &build.column,
1230 )?);
1231 }
1232 index_of.insert(at, channels.len());
1233 channels.push(Channel {
1234 column: build.column.clone(),
1235 quantity: build.quantity,
1236 time_s: build.slots.iter().map(|slot| reading.time(*slot)).collect(),
1237 values,
1238 });
1239 }
1240
1241 let primary = reading
1243 .find(POSITION, Role::Altitude)
1244 .filter(|at| reading.channels[*at].quantity == Quantity::AltitudeAboveGround);
1245 let speed = reading
1246 .find(POSITION, Role::VerticalSpeed)
1247 .filter(|at| reading.channels[*at].quantity == Quantity::Speed);
1248 let (mut time_s, mut altitude_m, mut vertical) = (Vec::new(), Vec::new(), Vec::new());
1249 let output = |at: usize| index_of.get(&at).map(|&k| &channels[k]);
1251 if let Some((primary, channel)) = primary.and_then(|at| Some((at, output(at)?))) {
1252 let build = &reading.channels[primary];
1253 let mut order: Vec<usize> = (0..build.slots.len())
1254 .filter(|&i| channel.time_s[i].is_finite())
1255 .collect();
1256 let backwards = order
1257 .windows(2)
1258 .filter(|pair| channel.time_s[pair[1]] < channel.time_s[pair[0]])
1259 .count();
1260 order.sort_by(|&a, &b| channel.time_s[a].total_cmp(&channel.time_s[b]));
1261 let before = order.len();
1262 order.dedup_by(|later, earlier| channel.time_s[*later] == channel.time_s[*earlier]);
1263 let repeats = before - order.len();
1264 if backwards > 0 || repeats > 0 {
1265 notes.push(format!(
1266 "`{POSITION}` goes back in time {backwards} times and repeats a time {repeats} \
1267 times. The record's track takes its samples in time order, the first at each \
1268 time. The channels keep them all in the file's order"
1269 ));
1270 }
1271 let speed_at = |slot: usize| -> f64 {
1272 let Some((build, written)) =
1273 speed.and_then(|at| Some((&reading.channels[at], output(at)?)))
1274 else {
1275 return f64::NAN;
1276 };
1277 build
1278 .slots
1279 .binary_search(&slot)
1280 .map_or(f64::NAN, |k| written.values[k])
1281 };
1282 for i in order {
1283 time_s.push(channel.time_s[i]);
1284 altitude_m.push(channel.values[i]);
1285 vertical.push(speed_at(build.slots[i]));
1286 }
1287 } else {
1288 notes.push(format!(
1289 "the record has no altitude track: `{POSITION}.fcb_altitude` isn't read in meters \
1290 (above), and the channels hold everything the file does"
1291 ));
1292 }
1293
1294 let mut line_notes = Notes::default();
1295 line_notes_into(&reading.counts, no_fix, &mut line_notes);
1296 notes.extend(line_notes.into_vec());
1297 let vertical_speed_m_s = speed.filter(|_| primary.is_some()).map(|_| vertical);
1298
1299 Ok(FlightLog {
1300 format: LogFormat::AerospaceNuGroundStationText,
1301 logger: LOGGER.to_owned(),
1302 serial_number: None,
1303 firmware: None,
1304 flight_number: None,
1305 stated: Stated::default(),
1306 time_s,
1307 altitude_m,
1308 vertical_speed_m_s,
1309 temperature_k: None,
1310 battery_v: None,
1311 channels,
1312 events: Vec::new(),
1313 notes,
1314 })
1315}
1316
1317fn place_on_clock(slots: &mut [Slot], notes: &mut Vec<String>) -> bool {
1323 let stretches = stretches_of(slots);
1324 let span = |stretch: &[usize]| {
1325 let received = stretch.iter().map(|&at| slots[at].received_s);
1326 let (lo, hi) = received.fold((f64::INFINITY, f64::NEG_INFINITY), |(lo, hi), t| {
1327 (lo.min(t), hi.max(t))
1328 });
1329 hi - lo
1330 };
1331 let Some((longest, received)) = longest_stretch(&stretches, span) else {
1332 return true;
1333 };
1334 let longest = &stretches[longest];
1335 let board = {
1336 let ms = longest.iter().filter_map(|&at| slots[at].board_ms);
1337 let (lo, hi) = ms.fold((f64::INFINITY, f64::NEG_INFINITY), |(lo, hi), t| {
1338 (lo.min(t), hi.max(t))
1339 });
1340 (hi - lo) / 1_000.0
1341 };
1342 let rate = board / received;
1343 let evidence = format!(
1344 "over the longest stretch of packets, {received:.1} s of the ground station's clock, \
1345 the board's advances {board:.1} s read in milliseconds, {rate:.4} for each second. \
1346 The stamps in it keep within {STRETCH_GAP_S} s of one gap from the ground station's \
1347 clock. The band is 0.8 to 1.25 over at least 20 s"
1348 );
1349 if !(received >= CLOCK_SPAN_S && (CLOCK_RATE.0..=CLOCK_RATE.1).contains(&rate)) {
1350 notes.push(format!(
1351 "`time_stamp_ms` kept as written, unchecked against the ground station's clock: \
1352 {evidence}. Every packet takes the time the ground station received it"
1353 ));
1354 return false;
1355 }
1356 notes.push(format!(
1357 "`time_stamp_ms` read in milliseconds, from its name and the ground station's clock: \
1358 {evidence}"
1359 ));
1360 for stretch in &stretches {
1361 let mut gaps: Vec<f64> = stretch
1362 .iter()
1363 .filter_map(|&at| Some(slots[at].received_s - slots[at].board_ms? / 1_000.0))
1364 .collect();
1365 gaps.sort_by(f64::total_cmp);
1366 let Some(&median) = gaps.get(gaps.len() / 2) else {
1368 continue;
1369 };
1370 for &at in stretch {
1371 if let Some(ms) = slots[at].board_ms {
1372 slots[at].time_s = ms / 1_000.0 + median;
1373 }
1374 }
1375 }
1376 if stretches.len() > 1 {
1377 notes.push(format!(
1378 "the board's clock runs in {} stretches, each a steady gap from the ground station's \
1379 clock, as after a restart of the board. Each is placed on the ground station's \
1380 clock by its own median gap",
1381 stretches.len()
1382 ));
1383 }
1384 true
1385}
1386
1387fn stretches_of(slots: &[Slot]) -> Vec<Vec<usize>> {
1390 let mut stretches: Vec<Vec<usize>> = Vec::new();
1391 let mut first_gap = f64::NAN;
1392 for (at, slot) in slots.iter().enumerate() {
1393 let Some(ms) = slot.board_ms else { continue };
1394 let gap = slot.received_s - ms / 1_000.0;
1395 match stretches.last_mut() {
1396 Some(stretch) if (gap - first_gap).abs() <= STRETCH_GAP_S => stretch.push(at),
1397 _ => {
1398 stretches.push(vec![at]);
1399 first_gap = gap;
1400 }
1401 }
1402 }
1403 stretches
1404}
1405
1406fn longest_stretch(
1410 stretches: &[Vec<usize>],
1411 mut span: impl FnMut(&[usize]) -> f64,
1412) -> Option<(usize, f64)> {
1413 let spans: Vec<f64> = stretches.iter().map(|stretch| span(stretch)).collect();
1414 spans
1415 .iter()
1416 .copied()
1417 .enumerate()
1418 .max_by(|a, b| a.1.total_cmp(&b.1))
1419}
1420
1421fn half_ulp(value: f64) -> f64 {
1428 let magnitude = value.abs();
1429 #[allow(
1430 clippy::cast_possible_truncation,
1431 reason = "testing whether it is a 32-bit float"
1432 )]
1433 let single = magnitude as f32;
1434 if f64::from(single) == magnitude && single.is_finite() {
1435 let next = f32::from_bits(single.to_bits() + 1);
1436 0.5 * (f64::from(next) - magnitude)
1437 } else {
1438 0.5 * (f64::from_bits(magnitude.to_bits() + 1) - magnitude)
1439 }
1440}
1441
1442fn on_step(value: f64, per_unit: f64) -> bool {
1444 let pascals = value * per_unit;
1445 let product_rounding = f64::from_bits(pascals.abs().to_bits() + 1) - pascals.abs();
1446 pascals.is_finite()
1447 && (pascals - pascals.round()).abs() <= per_unit * half_ulp(value) + product_rounding
1448}
1449
1450fn steps(values: &[f64]) -> (f64, f64, usize) {
1453 let mut distinct: Vec<f64> = values
1454 .iter()
1455 .copied()
1456 .filter(|v| v.is_finite() && *v != 0.0)
1457 .collect();
1458 distinct.sort_by(f64::total_cmp);
1459 distinct.dedup();
1460 let count = distinct.len();
1461 #[allow(clippy::cast_precision_loss, reason = "counts of samples")]
1462 let share = |per_unit: f64| {
1463 distinct.iter().filter(|v| on_step(**v, per_unit)).count() as f64 / count.max(1) as f64
1464 };
1465 (share(ATM_PA), share(BAR_PA), count)
1466}
1467
1468fn pad_size(value: f64) -> bool {
1470 (PAD_PA.0 / ATM_PA..=PAD_PA.1 / BAR_PA).contains(&value)
1471}
1472
1473fn decide_pressures(reading: &mut Reading, notes: &mut Vec<String>) {
1475 for build in reading
1476 .channels
1477 .iter_mut()
1478 .filter(|c| c.role == Role::Barometer)
1479 {
1480 let first = build
1481 .values
1482 .iter()
1483 .copied()
1484 .find(|v| v.is_finite() && *v != 0.0);
1485 let (atm, bar, count) = steps(&build.values);
1486 let size = first.map_or_else(
1487 || "it holds no number but 0".to_owned(),
1488 |first| format!("its first value is {first}"),
1489 );
1490 let evidence = format!(
1491 "{size}, a pad's pressure in atmospheres or bars if within 0.4935 to 1.1. A value \
1492 counts as a whole number within its own rounding as sent. Of its {count} distinct \
1493 non-zero values, {:.1}% are a whole number of pascals divided by 101,325. Of the same \
1494 values, {:.1}% are a whole number of pascals divided by 100,000",
1495 atm * 100.0,
1496 bar * 100.0
1497 );
1498 if first.is_some_and(pad_size)
1499 && count >= STEP_LEAST_VALUES
1500 && atm >= STEP_HOLDS
1501 && bar <= STEP_FAILS
1502 {
1503 notes.push(format!(
1504 "{} read in atmospheres, from its size and its step: {evidence}",
1505 build.quoted()
1506 ));
1507 build.quantity = Quantity::Pressure;
1508 build.scale = ATM_PA;
1509 } else {
1510 notes.push(format!(
1511 "{} kept as written, its size or its step not an atmosphere's: {evidence}. \
1512 Atmospheres need 99% of at least {STEP_LEAST_VALUES} values on the first step \
1513 and at most 10% on the second",
1514 build.quoted()
1515 ));
1516 build.keep();
1517 }
1518 }
1519}
1520
1521fn decide_reference(reading: &mut Reading, notes: &mut Vec<String>) {
1523 let Some(reference) = reading.find(ALT_INFO, Role::Reference) else {
1524 return;
1525 };
1526 let barometer = reading
1527 .named(&format!("{ALT_INFO}.barometer_pressure"))
1528 .filter(|at| reading.channels[*at].quantity == Quantity::Pressure);
1529 let first = barometer.and_then(|barometer| {
1530 reading
1531 .paired(reference, barometer)
1532 .into_iter()
1533 .find(|(_, r, b)| r.is_finite() && b.is_finite() && *b != 0.0)
1534 });
1535 let quoted = reading.channels[reference].quoted();
1536 let decided = match first {
1537 Some((_, value, beside)) => {
1538 let off = (value / beside - 1.0).abs();
1539 let evidence = format!(
1540 "its first value, {value}, is {:.3}% from `barometer_pressure`'s in the same \
1541 packet, {beside}, read in atmospheres; atmospheres and bars are 1.3% apart",
1542 off * 100.0
1543 );
1544 if pad_size(value) && off <= REFERENCE_SHARE {
1545 notes.push(format!(
1546 "{quoted} read in atmospheres, from its size and the barometer's: {evidence}"
1547 ));
1548 true
1549 } else {
1550 notes.push(format!(
1551 "{quoted} kept as written, its size or the barometer's not an atmosphere's: \
1552 {evidence}; atmospheres need a pad's size and 0.5% at most"
1553 ));
1554 false
1555 }
1556 }
1557 None => {
1558 notes.push(format!(
1559 "{quoted} kept as written, with no barometer read in atmospheres beside it: its \
1560 unit is checked against `barometer_pressure`'s in the same packet"
1561 ));
1562 false
1563 }
1564 };
1565 let build = &mut reading.channels[reference];
1566 if decided {
1567 build.quantity = Quantity::Pressure;
1568 build.scale = ATM_PA;
1569 } else {
1570 build.keep();
1571 }
1572}
1573
1574fn beside(reading: &Reading, barometer: usize, altitude: usize) -> Vec<(f64, f64)> {
1577 let build = &reading.channels[altitude];
1578 let mut heights: Vec<(f64, f64)> = build
1579 .slots
1580 .iter()
1581 .zip(&build.values)
1582 .filter(|(slot, value)| value.is_finite() && reading.time(**slot).is_finite())
1583 .map(|(slot, value)| (reading.time(*slot), *value))
1584 .collect();
1585 heights.sort_by(|a, b| a.0.total_cmp(&b.0));
1586 let build = &reading.channels[barometer];
1587 let mut pairs = Vec::new();
1588 for (slot, value) in build.slots.iter().zip(&build.values) {
1589 let time = reading.time(*slot);
1590 if !(value.is_finite() && *value > 0.0 && time.is_finite()) {
1591 continue;
1592 }
1593 let k = heights.partition_point(|(t, _)| *t < time);
1594 let near = [k.checked_sub(1), Some(k)]
1595 .into_iter()
1596 .flatten()
1597 .filter_map(|k| heights.get(k))
1598 .filter(|(t, _)| (t - time).abs() <= NEAREST_S)
1599 .min_by(|a, b| (a.0 - time).abs().total_cmp(&(b.0 - time).abs()));
1600 if let Some((_, height)) = near {
1601 pairs.push((value * ATM_PA, *height));
1602 }
1603 }
1604 pairs
1605}
1606
1607fn barometer(reading: &Reading) -> Option<usize> {
1609 reading
1610 .named(&format!("{ALT_INFO}.barometer_pressure"))
1611 .filter(|at| reading.channels[*at].quantity == Quantity::Pressure)
1612}
1613
1614fn series(reading: &Reading, at: usize) -> Vec<(f64, f64)> {
1616 let build = &reading.channels[at];
1617 let mut points: Vec<(f64, f64)> = build
1618 .slots
1619 .iter()
1620 .zip(&build.values)
1621 .map(|(slot, value)| (reading.time(*slot), value * build.scale))
1622 .filter(|(time, value)| time.is_finite() && value.is_finite())
1623 .collect();
1624 points.sort_by(|a, b| a.0.total_cmp(&b.0));
1625 points
1626}
1627
1628fn highest(points: &[(f64, f64)]) -> Option<usize> {
1630 let mut best: Option<usize> = None;
1631 for (i, point) in points.iter().enumerate() {
1632 if best.is_none_or(|b| point.1 > points[b].1) {
1633 best = Some(i);
1634 }
1635 }
1636 best
1637}
1638
1639fn near_gravity(deceleration: f64) -> bool {
1641 let share = deceleration / STANDARD_GRAVITY_MPS2;
1642 share.is_finite() && (1.0 / GRAVITY_FACTOR..=GRAVITY_FACTOR).contains(&share)
1643}
1644
1645fn curvature(points: &[(f64, f64)]) -> f64 {
1648 #[allow(clippy::cast_precision_loss, reason = "a count of samples")]
1649 let n = points.len() as f64;
1650 let mean = points.iter().map(|p| p.0).sum::<f64>() / n;
1651 let (mut s, mut y) = ([0.0; 5], [0.0; 3]);
1652 for &(t, z) in points {
1653 let x = t - mean;
1654 let mut power = 1.0;
1655 for (k, sum) in s.iter_mut().enumerate() {
1656 *sum += power;
1657 if let Some(moment) = y.get_mut(k) {
1658 *moment += z * power;
1659 }
1660 power *= x;
1661 }
1662 }
1663 let det = |m: [[f64; 3]; 3]| {
1664 m[0][0] * (m[1][1] * m[2][2] - m[1][2] * m[2][1])
1665 - m[0][1] * (m[1][0] * m[2][2] - m[1][2] * m[2][0])
1666 + m[0][2] * (m[1][0] * m[2][1] - m[1][1] * m[2][0])
1667 };
1668 let normal = [[s[0], s[1], s[2]], [s[1], s[2], s[3]], [s[2], s[3], s[4]]];
1669 let mut quadratic = normal;
1670 for (row, moment) in quadratic.iter_mut().zip(y) {
1671 row[2] = moment;
1672 }
1673 2.0 * det(quadratic) / det(normal)
1674}
1675
1676fn slope(points: &[(f64, f64)]) -> f64 {
1678 #[allow(clippy::cast_precision_loss, reason = "a count of samples")]
1679 let n = points.len() as f64;
1680 let mean_t = points.iter().map(|p| p.0).sum::<f64>() / n;
1681 let mean_v = points.iter().map(|p| p.1).sum::<f64>() / n;
1682 let (mut cross, mut square) = (0.0, 0.0);
1683 for &(t, v) in points {
1684 cross += (t - mean_t) * (v - mean_v);
1685 square += (t - mean_t) * (t - mean_t);
1686 }
1687 cross / square
1688}
1689
1690#[derive(Debug, Clone, Copy, PartialEq)]
1693struct Coast {
1694 samples: usize,
1695 span_s: f64,
1696 deceleration: f64,
1697}
1698
1699impl Coast {
1700 fn of(window: &[(f64, f64)], fit: fn(&[(f64, f64)]) -> f64) -> Option<Self> {
1701 let span_s = window.last()?.0 - window.first()?.0;
1702 (window.len() >= COAST_LEAST_SAMPLES && span_s >= COAST_LEAST_S).then(|| Self {
1703 samples: window.len(),
1704 span_s,
1705 deceleration: -fit(window),
1706 })
1707 }
1708
1709 fn text(found: Option<Self>, what: &str, unit: &str) -> String {
1711 found.map_or_else(
1712 || {
1713 format!(
1714 "The coast holds fewer than {COAST_LEAST_SAMPLES} samples or spans under \
1715 {COAST_LEAST_S} s, too little to fit"
1716 )
1717 },
1718 |coast| {
1719 format!(
1720 "Over the coast, {} samples across {:.1} s, {what} slows it {:.2} {unit}, \
1721 {:.3} g read so. An upward coast slows at least 1 g. The band is a \
1722 factor of {GRAVITY_FACTOR} either way, where feet would read 3.28 times",
1723 coast.samples,
1724 coast.span_s,
1725 coast.deceleration,
1726 coast.deceleration / STANDARD_GRAVITY_MPS2
1727 )
1728 },
1729 )
1730 }
1731}
1732
1733fn altitude_coast(points: &[(f64, f64)]) -> Option<Coast> {
1736 let top = highest(points)?;
1737 let mut fastest: Option<(f64, usize)> = None;
1738 for (i, pair) in points[..=top].windows(2).enumerate() {
1739 let step = pair[1].0 - pair[0].0;
1740 let rate = (pair[1].1 - pair[0].1) / step;
1741 if step > 0.0 && step <= COAST_STEP_S && fastest.is_none_or(|(best, _)| rate > best) {
1742 fastest = Some((rate, i + 1));
1743 }
1744 }
1745 let (_, start) = fastest?;
1746 Coast::of(&points[start..=top], curvature)
1747}
1748
1749fn speed_coast(points: &[(f64, f64)]) -> Option<Coast> {
1752 let top = highest(points)?;
1753 let end = (top..points.len()).find(|&i| points[i].1 <= 0.0)?;
1754 Coast::of(&points[top..=end], slope)
1755}
1756
1757fn decide_altitude(
1760 reading: &mut Reading,
1761 start_line: usize,
1762 notes: &mut Vec<String>,
1763) -> Result<(), LogError> {
1764 let Some(altitude) = reading.find(POSITION, Role::Altitude) else {
1765 return Ok(());
1766 };
1767 let quoted = reading.channels[altitude].quoted();
1768 let Some(barometer) = barometer(reading) else {
1769 notes.push(format!(
1770 "{quoted} kept as written, with no barometer read in atmospheres: its climb is \
1771 checked against `barometer_pressure`'s"
1772 ));
1773 reading.channels[altitude].keep();
1774 return Ok(());
1775 };
1776 let pairs = beside(reading, barometer, altitude);
1777 let atmosphere = Ussa76::standard();
1778 let climbs = pairs.first().copied().and_then(|pad| {
1779 let top = pairs.iter().copied().min_by(|a, b| a.0.total_cmp(&b.0))?;
1780 let high = atmosphere.pressure_altitude_m(top.0).ok()?;
1781 let ground = atmosphere.pressure_altitude_m(pad.0).ok()?;
1782 Some((high - ground, top.1 - pad.1))
1783 });
1784 let (barometer_text, borne) = match climbs {
1785 None => (
1786 format!(
1787 "no `{POSITION}` sample lies within {NEAREST_S} s of a pressure of \
1788 `barometer_pressure` that has an altitude in the standard atmosphere"
1789 ),
1790 false,
1791 ),
1792 Some((climb, _)) if climb < CHECK_CLIMB_M => (
1793 format!(
1794 "the barometer climbs {climb:.2} m in the standard atmosphere, under the \
1795 {CHECK_CLIMB_M} m from which it checks a unit"
1796 ),
1797 false,
1798 ),
1799 Some((climb, column)) => {
1800 let evidence = format!(
1801 "from `barometer_pressure`'s first value to its lowest, each within {NEAREST_S} s \
1802 of a sample of the column, the standard atmosphere climbs {climb:.1} m. The \
1803 column's climb over the same span is {column:.1}, {:.3} times that. Meters need within 25%",
1804 column / climb
1805 );
1806 if (column / climb - 1.0).abs() > MARGIN {
1807 return Err(LogError::Unit {
1808 format: FORMAT,
1809 line: start_line,
1810 message: format!("{quoted} isn't in meters, by the barometer: {evidence}"),
1811 });
1812 }
1813 (evidence, true)
1814 }
1815 };
1816 let coast = altitude_coast(&series(reading, altitude));
1817 let evidence = format!(
1818 "{barometer_text}. {}",
1819 Coast::text(coast, "the column's least-squares parabola", "m/s²")
1820 );
1821 if borne && coast.is_some_and(|coast| near_gravity(coast.deceleration)) {
1822 notes.push(format!(
1823 "{quoted} read in meters, from the barometer and the coast: {evidence}"
1824 ));
1825 reading.channels[altitude].quantity = Quantity::AltitudeAboveGround;
1826 } else {
1827 notes.push(format!(
1828 "{quoted} kept as written, the barometer and the coast not both bearing meters out: \
1829 {evidence}"
1830 ));
1831 reading.channels[altitude].keep();
1832 }
1833 Ok(())
1834}
1835
1836fn ascent(points: &[(f64, f64)]) -> Option<(f64, f64, f64)> {
1840 let mut top: Option<usize> = None;
1841 for (i, point) in points.iter().enumerate() {
1842 if point.1 > 0.0 && top.is_none_or(|t| point.1 < points[t].1) {
1843 top = Some(i);
1844 }
1845 }
1846 let top = top?;
1847 let mut start = top;
1848 while start > 0 && points[start - 1].1 > points[start].1 {
1849 start -= 1;
1850 }
1851 let atmosphere = Ussa76::standard();
1852 let high = atmosphere.pressure_altitude_m(points[top].1).ok()?;
1853 let ground = atmosphere.pressure_altitude_m(points[start].1).ok()?;
1854 Some((points[start].0, points[top].0, high - ground))
1855}
1856
1857fn integral(points: &[(f64, f64)], from: f64, to: f64) -> Option<(f64, f64)> {
1860 if points.first()?.0 > from || points.last()?.0 < to {
1861 return None;
1862 }
1863 let (mut sum, mut longest) = (0.0, 0.0_f64);
1864 for pair in points.windows(2) {
1865 let ((t0, v0), (t1, v1)) = (pair[0], pair[1]);
1866 let (lo, hi) = (t0.max(from), t1.min(to));
1867 if t1 <= t0 || hi <= lo {
1868 continue;
1869 }
1870 let at = |t: f64| v0 + (v1 - v0) * (t - t0) / (t1 - t0);
1871 sum += 0.5 * (at(lo) + at(hi)) * (hi - lo);
1872 longest = longest.max(t1 - t0);
1873 }
1874 Some((sum, longest))
1875}
1876
1877fn decide_speed(reading: &mut Reading, notes: &mut Vec<String>) {
1879 let Some(speed) = reading.find(POSITION, Role::VerticalSpeed) else {
1880 return;
1881 };
1882 let quoted = reading.channels[speed].quoted();
1883 let Some(barometer) = barometer(reading) else {
1884 notes.push(format!(
1885 "{quoted} kept as written, with no barometer read in atmospheres: its integral is \
1886 checked against `barometer_pressure`'s climb"
1887 ));
1888 reading.channels[speed].keep();
1889 return;
1890 };
1891 let points = series(reading, speed);
1892 let climb = ascent(&series(reading, barometer))
1893 .and_then(|(from, to, climb)| Some((from, to, climb, integral(&points, from, to)?)));
1894 let (barometer_text, borne) = match climb {
1895 None => (
1896 "the barometer reads no ascent the column's samples span".to_owned(),
1897 false,
1898 ),
1899 Some((from, to, climb, (moved, longest))) => {
1900 let ratio = climb / moved;
1901 (
1902 format!(
1903 "over the barometer's ascent, the standard atmosphere climbs {climb:.1} m. The \
1904 ascent is the {:.1} s from where the barometer's pressure last began to fall to its \
1905 lowest. The column integrates to {moved:.1}, the speed taken as a straight \
1906 line between samples up to {longest:.1} s apart. The climb is {ratio:.3} \
1907 times that. Meters per second need at least {CHECK_CLIMB_M} m and within 25%",
1908 to - from
1909 ),
1910 climb >= CHECK_CLIMB_M && (ratio - 1.0).abs() <= MARGIN,
1911 )
1912 }
1913 };
1914 let coast = speed_coast(&points);
1915 let evidence = format!(
1916 "{barometer_text}. {}",
1917 Coast::text(coast, "the column's least-squares line", "m/s²")
1918 );
1919 if borne && coast.is_some_and(|coast| near_gravity(coast.deceleration)) {
1920 notes.push(format!(
1921 "{quoted} read in meters per second, from the barometer and the coast: {evidence}"
1922 ));
1923 reading.channels[speed].quantity = Quantity::Speed;
1924 } else {
1925 notes.push(format!(
1926 "{quoted} kept as written, the barometer and the coast not both bearing meters per \
1927 second out: {evidence}"
1928 ));
1929 reading.channels[speed].keep();
1930 }
1931}
1932
1933fn decide_positions(reading: &mut Reading, notes: &mut Vec<String>) -> usize {
1935 let mut no_fix = 0;
1936 let latitudes: Vec<usize> = (0..reading.channels.len())
1937 .filter(|&at| reading.channels[at].role == Role::Latitude)
1938 .collect();
1939 let mut paired_longitudes = Vec::new();
1940 for lat in latitudes {
1941 let packet = reading.channels[lat].packet.clone();
1942 let Some(lon) = reading.find(&packet, Role::Longitude) else {
1943 notes.push(format!(
1944 "{} kept as written, without its longitude: a position is decided as one fix",
1945 reading.channels[lat].quoted()
1946 ));
1947 reading.channels[lat].keep();
1948 continue;
1949 };
1950 paired_longitudes.push(lon);
1951 let names = format!(
1952 "{} and {}",
1953 reading.channels[lat].quoted(),
1954 reading.channels[lon].quoted()
1955 );
1956 let pairs = reading.paired(lat, lon);
1957 let fixes: Vec<(f64, f64)> = pairs
1958 .iter()
1959 .filter(|(_, a, b)| a.is_finite() && b.is_finite() && !(*a == 0.0 && *b == 0.0))
1960 .map(|(_, a, b)| (*a, *b))
1961 .collect();
1962 if fixes.is_empty() {
1963 notes.push(format!(
1964 "{names} kept as written, holding no fix: every position is (0, 0), the \
1965 receiver's mark for none, or a gap"
1966 ));
1967 reading.channels[lat].keep();
1968 reading.channels[lon].keep();
1969 continue;
1970 }
1971 let within = fixes
1972 .iter()
1973 .all(|(a, b)| a.abs() <= 90.0 && b.abs() <= 180.0);
1974 let beyond_radians = fixes
1975 .iter()
1976 .filter(|(a, b)| a.abs() > PI / 2.0 || b.abs() > PI)
1977 .count();
1978 let fraction = |v: f64| v.abs().fract();
1979 let past_minutes = fixes
1980 .iter()
1981 .filter(|(a, b)| fraction(*a).max(fraction(*b)) >= 0.6)
1982 .count();
1983 let most = |count: usize| 2 * count > fixes.len();
1985 let evidence = format!(
1986 "{} fixes, {} within ±90° and ±180°, {beyond_radians} beyond ±π/2 or ±π. \
1987 {past_minutes} of them have a part after the whole degrees of 0.6 or more, where \
1988 degrees and minutes run together stay under 0.6. Degrees need all within and most \
1989 of the fixes past both",
1990 fixes.len(),
1991 if within { "all" } else { "not all" },
1992 );
1993 if within && most(beyond_radians) && most(past_minutes) {
1994 notes.push(format!(
1995 "{names} read in degrees, from their range and their fractions: {evidence}"
1996 ));
1997 for (slot, a, b) in pairs {
1998 if a == 0.0 && b == 0.0 {
1999 no_fix += 1;
2000 for at in [lat, lon] {
2001 let build = &mut reading.channels[at];
2002 if let Ok(k) = build.slots.binary_search(&slot) {
2003 build.values[k] = f64::NAN;
2004 }
2005 }
2006 }
2007 }
2008 reading.channels[lat].quantity = Quantity::Latitude;
2009 reading.channels[lon].quantity = Quantity::Longitude;
2010 } else {
2011 notes.push(format!(
2012 "{names} kept as written, their range or their fractions not degrees': \
2013 {evidence}"
2014 ));
2015 reading.channels[lat].keep();
2016 reading.channels[lon].keep();
2017 }
2018 }
2019 for at in 0..reading.channels.len() {
2020 if reading.channels[at].role == Role::Longitude && !paired_longitudes.contains(&at) {
2021 notes.push(format!(
2022 "{} kept as written, without its latitude: a position is decided as one fix",
2023 reading.channels[at].quoted()
2024 ));
2025 reading.channels[at].keep();
2026 }
2027 }
2028 no_fix
2029}
2030
2031fn shown(value: f64) -> String {
2033 if value != 0.0 && !(1e-4..1e9).contains(&value.abs()) {
2034 format!("{value:e}")
2035 } else {
2036 format!("{value}")
2037 }
2038}
2039
2040fn magnitude(v: [f64; 3]) -> f64 {
2041 (v[0] * v[0] + v[1] * v[1] + v[2] * v[2]).sqrt()
2042}
2043
2044fn axes(reading: &Reading, role: fn(usize) -> Role) -> Option<[usize; 3]> {
2046 Some([
2047 reading.find(ORIENTATION, role(0))?,
2048 reading.find(ORIENTATION, role(1))?,
2049 reading.find(ORIENTATION, role(2))?,
2050 ])
2051}
2052
2053fn listed(reading: &Reading, at: &[usize]) -> String {
2055 let quoted: Vec<String> = at.iter().map(|&i| reading.channels[i].quoted()).collect();
2056 match quoted.as_slice() {
2057 [] => String::new(),
2058 [one] => one.clone(),
2059 [rest @ .., last] => format!("{} and {last}", rest.join(", ")),
2060 }
2061}
2062
2063fn median(mut values: Vec<f64>) -> Option<f64> {
2065 values.sort_by(f64::total_cmp);
2066 values.get(values.len() / 2).copied()
2067}
2068
2069fn accelerometer_climb(reading: &Reading, axes: [usize; 3]) -> Result<(usize, f64), String> {
2072 let Some(barometer) = barometer(reading) else {
2073 return Err("the file has no `barometer_pressure` read in atmospheres".to_owned());
2074 };
2075 let mut samples: Vec<(f64, [f64; 3])> = reading
2076 .tripled(axes)
2077 .into_iter()
2078 .map(|(slot, a)| (reading.time(slot), a))
2079 .filter(|(t, a)| t.is_finite() && a.iter().all(|v| v.is_finite()))
2080 .collect();
2081 samples.sort_by(|a, b| a.0.total_cmp(&b.0));
2082 let force = |i: usize| magnitude(samples[i].1);
2083 let launch = (0..samples.len().saturating_sub(2)).find_map(|i| {
2084 let before = &samples[i.saturating_sub(PAD_SAMPLES)..i];
2085 if before.len() < PAD_LEAST_SAMPLES {
2086 return None;
2087 }
2088 let one_g = median(before.iter().map(|s| magnitude(s.1)).collect())?;
2089 (one_g > 0.0
2090 && force(i) >= LAUNCH_FORCE * one_g
2091 && force(i + 1) >= LAUNCH_HELD * one_g
2092 && force(i + 2) >= LAUNCH_HELD * one_g)
2093 .then_some((i, before))
2094 });
2095 let Some((launch, before)) = launch else {
2096 return Err(format!(
2097 "no launch: no sample reads {LAUNCH_FORCE} times the median of the {PAD_SAMPLES} \
2098 before it and the next two {LAUNCH_HELD} times"
2099 ));
2100 };
2101 let pad: Vec<f64> = (0..3)
2102 .filter_map(|axis| median(before.iter().map(|s| s.1[axis]).collect()))
2103 .collect();
2104 let pad_g = (pad.iter().map(|v| v * v).sum::<f64>()).sqrt();
2105 if pad.len() != 3 || pad_g <= 0.0 {
2106 return Err("the pad's reading has no direction".to_owned());
2107 }
2108 let up: Vec<f64> = pad.iter().map(|v| v / pad_g).collect();
2109 let net = |i: usize| {
2110 let a = samples[i].1;
2111 a[0] * up[0] + a[1] * up[1] + a[2] * up[2] - STANDARD_GRAVITY_MPS2
2112 };
2113 let launched_s = samples[launch].0;
2114 let mut rows: Vec<(f64, f64)> = Vec::new();
2115 let (mut speed, mut climb) = (0.0, 0.0);
2116 for i in launch..samples.len() - 1 {
2117 let step = samples[i + 1].0 - samples[i].0;
2118 if !(step > 0.0 && step <= INTEGRATE_STEP_S) {
2119 break;
2120 }
2121 let next = speed + 0.5 * (net(i) + net(i + 1)) * step;
2122 climb += 0.5 * (speed + next) * step;
2123 speed = next;
2124 if speed.is_nan() || speed <= 0.0 {
2125 break;
2126 }
2127 rows.push((samples[i + 1].0, climb));
2128 }
2129 let pressures = series(reading, barometer);
2130 let atmosphere = Ussa76::standard();
2131 let Some(pad_m) = pressures
2132 .iter()
2133 .rev()
2134 .find(|(t, p)| *t <= launched_s && *p > 0.0)
2135 .and_then(|(_, p)| atmosphere.pressure_altitude_m(*p).ok())
2136 else {
2137 return Err("no pressure on the pad at or before the launch".to_owned());
2138 };
2139 let mut ratios = Vec::new();
2140 for &(t, p) in pressures.iter().filter(|(t, _)| *t > launched_s) {
2141 let k = rows.partition_point(|row| row.0 < t);
2142 let near = [k.checked_sub(1), Some(k)]
2143 .into_iter()
2144 .flatten()
2145 .filter_map(|k| rows.get(k))
2146 .filter(|row| (row.0 - t).abs() <= NEAREST_S)
2147 .min_by(|a, b| (a.0 - t).abs().total_cmp(&(b.0 - t).abs()));
2148 let (Some(&(_, integrated)), Ok(height)) = (near, atmosphere.pressure_altitude_m(p)) else {
2149 continue;
2150 };
2151 if integrated >= CHECK_CLIMB_M {
2152 ratios.push((height - pad_m) / integrated);
2153 }
2154 }
2155 let count = ratios.len();
2156 match median(ratios) {
2157 Some(ratio) if count >= INTEGRATED_LEAST => Ok((count, ratio)),
2158 _ => Err(format!(
2159 "from the launch, it integrates to {climb:.1} m before its speed falls to 0 or a \
2160 step passes {INTEGRATE_STEP_S} s. Past {CHECK_CLIMB_M} m of that climb there are \
2161 {count} pressures, under the {INTEGRATED_LEAST} needed"
2162 )),
2163 }
2164}
2165
2166fn decide_accelerometer(reading: &mut Reading, notes: &mut Vec<String>) -> Option<f64> {
2169 let axes = axes(reading, Role::Accel)?;
2170 let names = listed(reading, &axes);
2171 let samples = reading.tripled(axes);
2172 let (mut in_ms2, mut in_g) = (0.0, 0.0);
2173 for pair in samples.windows(2) {
2174 let step = reading.time(pair[1].0) - reading.time(pair[0].0);
2175 let reading_g = magnitude(pair[0].1);
2176 if !(step > 0.0 && step.is_finite() && reading_g.is_finite()) {
2177 continue;
2178 }
2179 let weight = step.min(GRAVITY_STEP_MAX_S);
2180 if (reading_g - STANDARD_GRAVITY_MPS2).abs() <= GRAVITY_MARGIN * STANDARD_GRAVITY_MPS2 {
2181 in_ms2 += weight;
2182 }
2183 if (reading_g - 1.0).abs() <= GRAVITY_MARGIN {
2184 in_g += weight;
2185 }
2186 }
2187 let pad = in_ms2 >= GRAVITY_LEAST_S && in_ms2 >= GRAVITY_LEAD * in_g;
2188 let climb = accelerometer_climb(reading, axes);
2189 let climbs = climb.as_ref().is_ok_and(|&(_, ratio)| {
2190 ratio.is_finite() && (1.0 / GRAVITY_FACTOR..=GRAVITY_FACTOR).contains(&ratio)
2191 });
2192 let climb_text = match &climb {
2193 Ok((count, ratio)) => format!(
2194 "From the launch, its specific force along the pad's reading, less 9.806 65, \
2195 integrates read in m/s² to a climb. At {count} pressures past {CHECK_CLIMB_M} m \
2196 of it, the barometer's climb in the standard atmosphere is {ratio:.3} times it, as \
2197 a median. The band is a factor of {GRAVITY_FACTOR} either way"
2198 ),
2199 Err(why) => format!("The climb check can't be made: {why}"),
2200 };
2201 let evidence = format!(
2202 "the three axes' magnitude reads within 10% of 9.806 65 for {in_ms2:.1} s of the file's \
2203 time. It reads within 10% of 1 for {in_g:.1} s. A board at rest or under a parachute \
2204 reads 1 g. Meters per second squared need at least 1 s and twice the other's. \
2205 {climb_text}"
2206 );
2207 let decided = pad && climbs;
2208 if decided {
2209 notes.push(format!(
2210 "{names} read in meters per second squared, from the pad's gravity and the \
2211 barometer: {evidence}"
2212 ));
2213 } else {
2214 notes.push(format!(
2215 "{names} kept as written, the pad's gravity and the barometer not both bearing \
2216 meters per second squared out: {evidence}"
2217 ));
2218 }
2219 for at in axes {
2220 if decided {
2221 reading.channels[at].quantity = Quantity::Acceleration;
2222 } else {
2223 reading.channels[at].keep();
2224 }
2225 }
2226 decided.then_some(STANDARD_GRAVITY_MPS2)
2227}
2228
2229fn decide_tilt(reading: &mut Reading, one_g: Option<f64>, notes: &mut Vec<String>) -> bool {
2232 let Some(tilt) = reading.find(ORIENTATION, Role::Tilt) else {
2233 return false;
2234 };
2235 let quoted = reading.channels[tilt].quoted();
2236 let range = reading.channels[tilt].range();
2237 let in_range = range.is_some_and(|(lo, hi)| lo >= 0.0 && hi <= 180.0 && hi > PI);
2238 let range_text = range.map_or_else(
2239 || "it holds no number".to_owned(),
2240 |(lo, hi)| format!("its values run from {} to {}", shown(lo), shown(hi)),
2241 );
2242 let (Some(one_g), Some(accel)) = (one_g, axes(reading, Role::Accel)) else {
2243 notes.push(format!(
2244 "{quoted} kept as written, with no accelerometer read in m/s² to check it by: \
2245 {range_text}"
2246 ));
2247 reading.channels[tilt].keep();
2248 return false;
2249 };
2250 let accel = reading.tripled(accel);
2251 let column = &reading.channels[tilt];
2252 let (mut as_degrees, mut as_radians) = (Vec::new(), Vec::new());
2253 let mut k = 0;
2254 for (slot, a) in accel {
2255 while k < column.slots.len() && column.slots[k] < slot {
2256 k += 1;
2257 }
2258 if k >= column.slots.len() || column.slots[k] != slot {
2259 continue;
2260 }
2261 let m = magnitude(a);
2262 let value = column.values[k];
2263 if !(value.is_finite() && m.is_finite()) || (m - one_g).abs() > TILT_GRAVITY_MARGIN * one_g
2264 {
2265 continue;
2266 }
2267 let from_z = (a[2] / m).clamp(-1.0, 1.0).acos().to_degrees();
2268 as_degrees.push((value - from_z).abs());
2269 as_radians.push((value.to_degrees() - from_z).abs());
2270 }
2271 let median = |mut v: Vec<f64>| {
2272 v.sort_by(f64::total_cmp);
2273 v.get(v.len() / 2).copied().unwrap_or(f64::NAN)
2274 };
2275 let samples = as_degrees.len();
2276 let (degrees, radians) = (median(as_degrees), median(as_radians));
2277 let evidence = format!(
2278 "{range_text}. The accelerometer reads within 5% of 1 g at {samples} samples. At \
2279 these, the median difference from the angle of the accelerometer's reading from the z \
2280 axis is {degrees:.2}° read in degrees. Read in radians, it is {radians:.2}°"
2281 );
2282 if in_range
2283 && samples >= TILT_LEAST_SAMPLES
2284 && degrees <= TILT_AGREE_DEG
2285 && radians > TILT_AGREE_DEG
2286 {
2287 notes.push(format!(
2288 "{quoted} read in degrees, from its range and the accelerometer: {evidence}"
2289 ));
2290 reading.channels[tilt].quantity = Quantity::Angle;
2291 true
2292 } else {
2293 notes.push(format!(
2294 "{quoted} kept as written, its range or the accelerometer not degrees': {evidence}. \
2295 Degrees need 0 to 180 with a value past π, at least {TILT_LEAST_SAMPLES} samples \
2296 and 2° at most"
2297 ));
2298 reading.channels[tilt].keep();
2299 false
2300 }
2301}
2302
2303#[derive(Debug, Clone, Copy, PartialEq)]
2305struct Turning {
2306 stretches: usize,
2307 accelerometer_rad: f64,
2308 gyroscope_rad: f64,
2309}
2310
2311fn turning(reading: &Reading, samples: &[(usize, [f64; 3], [f64; 3])], one_g: f64) -> Turning {
2313 let mut found = Turning {
2314 stretches: 0,
2315 accelerometer_rad: 0.0,
2316 gyroscope_rad: 0.0,
2317 };
2318 let time = |i: usize| reading.time(samples[i].0);
2319 let near_g = |i: usize| {
2320 let m = magnitude(samples[i].1);
2321 m.is_finite() && (m - one_g).abs() <= TURN_GRAVITY_MARGIN * one_g
2322 };
2323 let fast = |i: usize| {
2324 let step = time(i + 1) - time(i);
2325 step > 0.0 && step <= TURN_STEP_S && samples[i].2.iter().all(|v| v.is_finite())
2326 };
2327 let direction = |i: usize| {
2328 let a = samples[i].1;
2329 let m = magnitude(a);
2330 a.map(|v| v / m)
2331 };
2332 let mut start = 0;
2333 while start + TURN_STEPS < samples.len() {
2334 let end = start + TURN_STEPS;
2335 if !((start..end).all(fast) && (start..=end).all(near_g)) {
2336 start += 1;
2337 continue;
2338 }
2339 let (from, to) = (direction(start), direction(end));
2340 let cosine = (from[0] * to[0] + from[1] * to[1] + from[2] * to[2]).clamp(-1.0, 1.0);
2341 let mut gyroscope = 0.0;
2342 for (i, sample) in samples.iter().enumerate().skip(start).take(TURN_STEPS) {
2343 let w = sample.2;
2344 let a = direction(i);
2345 let across = [
2346 w[1] * a[2] - w[2] * a[1],
2347 w[2] * a[0] - w[0] * a[2],
2348 w[0] * a[1] - w[1] * a[0],
2349 ];
2350 gyroscope += magnitude(across) * (time(i + 1) - time(i));
2351 }
2352 found.stretches += 1;
2353 found.accelerometer_rad += cosine.acos();
2354 found.gyroscope_rad += gyroscope;
2355 start = end;
2356 }
2357 found
2358}
2359
2360fn tilt_check(reading: &Reading, samples: &[(usize, f64, [f64; 3])]) -> (usize, f64, f64) {
2363 let time = |i: usize| reading.time(samples[i].0);
2364 let (mut windows, mut tilted, mut turned) = (0, 0.0, 0.0);
2365 let mut start = 0;
2366 while start + 1 < samples.len() {
2367 let mut end = start;
2368 while end + 1 < samples.len()
2369 && time(end + 1) - time(start) <= TILT_WINDOW_S
2370 && time(end + 1) - time(end) > 0.0
2371 && time(end + 1) - time(end) <= TILT_STEP_S
2372 {
2373 end += 1;
2374 }
2375 if end == start {
2376 start += 1;
2377 continue;
2378 }
2379 let change = (samples[end].1 - samples[start].1).to_radians().abs();
2380 if change >= TILT_LEAST_RAD {
2381 let turn: f64 = (start..end)
2382 .map(|i| {
2383 let w = samples[i].2;
2384 (w[0] * w[0] + w[1] * w[1]).sqrt() * (time(i + 1) - time(i))
2385 })
2386 .sum();
2387 if turn.is_finite() {
2388 windows += 1;
2389 tilted += change;
2390 turned += turn;
2391 }
2392 }
2393 start = end;
2394 }
2395 (windows, tilted, turned)
2396}
2397
2398fn decide_gyroscope(
2400 reading: &mut Reading,
2401 one_g: Option<f64>,
2402 tilt_read: bool,
2403 notes: &mut Vec<String>,
2404) {
2405 let Some(gyro) = axes(reading, Role::Gyro) else {
2406 return;
2407 };
2408 let names = listed(reading, &gyro);
2409 let keep = |reading: &mut Reading| {
2410 for at in gyro {
2411 reading.channels[at].keep();
2412 }
2413 };
2414 let (Some(one_g), Some(accel)) = (one_g, axes(reading, Role::Accel)) else {
2415 notes.push(format!(
2416 "{names} kept as written, with no accelerometer read in m/s²: their unit is checked \
2417 against its gravity as the board turns"
2418 ));
2419 keep(reading);
2420 return;
2421 };
2422 let accel = reading.tripled(accel);
2423 let rates = reading.tripled(gyro);
2424 let mut samples = Vec::new();
2425 let mut k = 0;
2426 for (slot, a) in accel {
2427 while k < rates.len() && rates[k].0 < slot {
2428 k += 1;
2429 }
2430 if k < rates.len() && rates[k].0 == slot {
2431 samples.push((slot, a, rates[k].1));
2432 }
2433 }
2434 let found = turning(reading, &samples, one_g);
2435 let ratio = found.gyroscope_rad / found.accelerometer_rad;
2436 let degrees = PI / 180.0;
2437 let within = |ratio: f64| (1.0 / TURN_FACTOR..=TURN_FACTOR).contains(&ratio);
2438 let enough =
2439 found.stretches >= TURN_LEAST_STRETCHES && found.accelerometer_rad >= TURN_LEAST_RAD;
2440 let turning_text = format!(
2441 "over {} stretches of {TURN_STEPS} steps at most {TURN_STEP_S} s apart at about 1 g, the \
2442 accelerometer's reading turns {:.3} rad. Over those stretches the gyroscope turns the \
2443 board across it {:.3} rad read in radians per second",
2444 found.stretches, found.accelerometer_rad, found.gyroscope_rad
2445 );
2446 let tilt = if tilt_read {
2447 reading.find(ORIENTATION, Role::Tilt)
2448 } else {
2449 None
2450 };
2451 let (windows, tilted, turned) = tilt.map_or((0, 0.0, 0.0), |tilt| {
2452 let column = &reading.channels[tilt];
2453 let mut joined = Vec::new();
2454 let mut k = 0;
2455 for &(slot, _, w) in &samples {
2456 while k < column.slots.len() && column.slots[k] < slot {
2457 k += 1;
2458 }
2459 if k < column.slots.len() && column.slots[k] == slot && column.values[k].is_finite() {
2460 joined.push((slot, column.values[k], w));
2461 }
2462 }
2463 tilt_check(reading, &joined)
2464 });
2465 let tilt_text = format!(
2466 "Over {windows} windows of at most {TILT_WINDOW_S} s in which `angle_vertical` moves at \
2467 least 10°, it changes {tilted:.3} rad. In those windows the gyroscope turns the board across its z \
2468 axis {turned:.3} rad read in radians per second and {:.3} rad read in degrees",
2469 turned * degrees
2470 );
2471 let tilt_refutes = |turn: f64| tilted > TILT_FACTOR * turn;
2472 let tilt_decides =
2473 windows >= TILT_LEAST_WINDOWS && tilt_refutes(turned * degrees) && !tilt_refutes(turned);
2474 if enough && within(ratio) && tilt_decides {
2475 notes.push(format!(
2476 "{names} read in radians per second, from the board's turning and its tilt: \
2477 {turning_text}, within a quarter to 4 times. {tilt_text}. So degrees per second \
2478 turn too little for the tilt"
2479 ));
2480 for at in gyro {
2481 reading.channels[at].quantity = Quantity::AngularRate;
2482 reading.channels[at].scale = 180.0 / PI;
2483 }
2484 } else {
2485 notes.push(format!(
2486 "{names} kept as written, the board's turning and its tilt not both radians per \
2487 second's: {turning_text}. {tilt_text}. Radians per second need at least 3 \
2488 stretches, 0.2 rad and a quarter to 4 times. They also need 3 windows whose tilt \
2489 refutes degrees per second"
2490 ));
2491 keep(reading);
2492 }
2493}
2494
2495fn kept_why(kind: Kept) -> (&'static str, &'static str) {
2497 match kind {
2498 Kept::Temperature => (
2499 "in degrees Celsius or Fahrenheit alike",
2500 "its name states no unit, and nothing in the file tells the two apart",
2501 ),
2502 Kept::Unstated => (
2503 "in a unit nothing in the file checks",
2504 "its name states no unit, and no check in the file reaches one",
2505 ),
2506 Kept::GnssAltitude => (
2507 "with no check of its unit but the barometer's",
2508 "a GPS altitude's rise against the barometer's tells meters from feet, but no second \
2509 check independent of the barometer is in the file",
2510 ),
2511 Kept::Euler => (
2512 "with no check of their unit but their range",
2513 "within ±π they read as radians would, but nothing else in the file checks them",
2514 ),
2515 Kept::Magnetic => (
2516 "with no quantity for a magnetic field",
2517 "the record has none, and their names state no unit",
2518 ),
2519 }
2520}
2521
2522fn notes_on_kept(reading: &mut Reading, notes: &mut Vec<String>) {
2525 let mut groups: Vec<(Kept, String, Vec<usize>)> = Vec::new();
2527 for (at, build) in reading.channels.iter().enumerate() {
2528 let Role::Kept(kind) = build.role else {
2529 continue;
2530 };
2531 let grouped = matches!(kind, Kept::Euler | Kept::Magnetic);
2532 match groups
2533 .iter_mut()
2534 .find(|(k, packet, _)| grouped && *k == kind && *packet == build.packet)
2535 {
2536 Some((_, _, members)) => members.push(at),
2537 None => groups.push((kind, build.packet.clone(), vec![at])),
2538 }
2539 }
2540 for (kind, _, members) in groups {
2541 let (decision, why) = kept_why(kind);
2542 let range = members
2543 .iter()
2544 .filter_map(|&at| reading.channels[at].range())
2545 .reduce(|(lo, hi), (l, h)| (lo.min(l), hi.max(h)));
2546 let whose = if members.len() > 1 { "their" } else { "its" };
2547 let range = range.map_or_else(
2548 || format!("{whose} values hold no number"),
2549 |(lo, hi)| format!("{whose} values run from {} to {}", shown(lo), shown(hi)),
2550 );
2551 notes.push(format!(
2552 "{} kept as written, {decision}: {why}; {range}",
2553 listed(reading, &members)
2554 ));
2555 for at in members {
2556 reading.channels[at].keep();
2557 }
2558 }
2559 let mut unknown: Vec<(String, Vec<usize>)> = Vec::new();
2560 for (at, build) in reading.channels.iter().enumerate() {
2561 if build.role != Role::Unknown {
2562 continue;
2563 }
2564 match unknown
2565 .iter_mut()
2566 .find(|(packet, _)| *packet == build.packet)
2567 {
2568 Some((_, members)) => members.push(at),
2569 None => unknown.push((build.packet.clone(), vec![at])),
2570 }
2571 }
2572 let mut unknown_notes = Notes::default();
2573 for (packet, members) in unknown {
2574 let first = &reading.channels[members[0]].column;
2575 let field = first.strip_prefix(&format!("{packet}.")).unwrap_or(first);
2576 unknown_notes.push(if members.len() == 1 {
2577 format!(
2578 "1 field of the `{}` packets, `{}`, isn't one this reader knows: its values are \
2579 kept as written",
2580 short(&packet),
2581 short(field)
2582 )
2583 } else {
2584 format!(
2585 "{} fields of the `{}` packets, such as `{}`, aren't ones this reader knows: \
2586 their values are kept as written",
2587 members.len(),
2588 short(&packet),
2589 short(field)
2590 )
2591 });
2592 }
2593 notes.extend(unknown_notes.into_vec());
2594}
2595
2596fn counted(count: usize, one: &str, many: &str, line: usize) -> String {
2599 if count == 1 {
2600 format!("1 {one}, on line {line},")
2601 } else {
2602 format!("{count} {many}, the first on line {line},")
2603 }
2604}
2605
2606fn agree<'a>(count: usize, one: &'a str, many: &'a str) -> &'a str {
2608 if count == 1 { one } else { many }
2609}
2610
2611fn line_notes_into(counts: &Counts, no_fix: usize, notes: &mut Notes) {
2613 if let Some(line) = counts.first_corrupt {
2614 let n = counts.corrupt;
2615 notes.push(format!(
2616 "{} {} left out: the ground station marks {} `crc` other than `'Good'`, {} bytes \
2617 having failed the radio's checksum",
2618 counted(n, "packet", "packets", line),
2619 agree(n, "is", "are"),
2620 agree(n, "its", "their"),
2621 agree(n, "its", "their"),
2622 ));
2623 }
2624 if let Some((line, text)) = &counts.first_message {
2625 notes.push(if counts.messages == 1 {
2626 format!(
2627 "1 line is the ground station's own message, not a packet, {text:?} on line \
2628 {line}: skipped"
2629 )
2630 } else {
2631 format!(
2632 "{} lines are the ground station's own messages, not packets, such as {text:?} \
2633 on line {line}: skipped",
2634 counts.messages
2635 )
2636 });
2637 }
2638 if let Some((line, why)) = &counts.first_unparsed {
2639 let n = counts.unparsed;
2640 notes.push(format!(
2641 "{} {} parse as a Python literal: {}; skipped",
2642 counted(n, "packet line", "packet lines", *line),
2643 agree(n, "doesn't", "don't"),
2644 short(why)
2645 ));
2646 }
2647 if let Some(line) = counts.first_other {
2648 let n = counts.other;
2649 notes.push(format!(
2650 "{} {} with no time of day `HH:MM:SS`: skipped",
2651 counted(n, "line", "lines", line),
2652 agree(n, "opens", "open")
2653 ));
2654 }
2655 if let Some(line) = counts.first_long {
2656 let n = counts.long;
2657 notes.push(format!(
2658 "{} {} longer than {MAX_LINE_BYTES} bytes: skipped",
2659 counted(n, "line", "lines", line),
2660 agree(n, "is", "are")
2661 ));
2662 }
2663 if let Some(line) = counts.first_run {
2664 let n = counts.runs;
2665 notes.push(format!(
2666 "{} {} skipped: the times follow the first",
2667 counted(n, "more `RUN START` line", "more `RUN START` lines", line),
2668 agree(n, "is", "are")
2669 ));
2670 }
2671 if let Some(line) = counts.first_gap {
2672 let n = counts.gaps;
2673 notes.push(format!(
2674 "{} {} `None`, `nan`, `inf` or text that isn't a number: {} a gap",
2675 counted(n, "value", "values", line),
2676 agree(n, "is", "are"),
2677 agree(n, "it is", "each is")
2678 ));
2679 }
2680 if no_fix > 0 {
2681 notes.push(format!(
2682 "{no_fix} {} of (0, 0), the receiver's mark for no fix, {}",
2683 agree(no_fix, "position", "positions"),
2684 agree(no_fix, "is a gap", "are gaps")
2685 ));
2686 }
2687 if let Some(line) = counts.first_left_out {
2688 let n = counts.left_out;
2689 notes.push(format!(
2690 "{} {} left out: {} longer than {MAX_NAME_CHARS} characters, or past the first \
2691 {MAX_CHANNELS} channels",
2692 counted(n, "value", "values", line),
2693 agree(n, "is", "are"),
2694 agree(n, "its field's name is", "their fields' names are")
2695 ));
2696 }
2697 let names = |set: &BTreeSet<String>| {
2698 set.iter()
2699 .map(|name| format!("`{}`", short(name)))
2700 .collect::<Vec<_>>()
2701 .join(", ")
2702 };
2703 if !counts.text_fields.is_empty() {
2704 notes.push(format!(
2705 "the fields {} hold text, not numbers: they are left out of the channels; `crc` \
2706 decides which packets are read",
2707 names(&counts.text_fields)
2708 ));
2709 }
2710 if !counts.nested_fields.is_empty() {
2711 notes.push(format!(
2712 "the fields {} hold lists of lists or dictionaries: they are left out",
2713 names(&counts.nested_fields)
2714 ));
2715 }
2716}
2717
2718fn not_this(message: String) -> LogError {
2719 LogError::NotThisFormat {
2720 format: FORMAT,
2721 message,
2722 }
2723}
2724
2725fn syntax(line: usize, message: String) -> LogError {
2726 LogError::Syntax {
2727 format: FORMAT,
2728 line,
2729 message,
2730 }
2731}
2732
2733#[cfg(test)]
2734mod tests {
2735 use super::*;
2736
2737 const G: f64 = STANDARD_GRAVITY_MPS2;
2738
2739 #[derive(Debug, Clone, Copy)]
2742 struct Written {
2743 per_pa: f64,
2744 accel: f64,
2745 boost: f64,
2747 gyro: f64,
2748 altitude: f64,
2749 speed: f64,
2750 straight_coast: bool,
2752 steady_coast: bool,
2754 stamp: f64,
2755 latitude: f64,
2756 longitude: f64,
2757 restart_at_s: Option<f64>,
2758 }
2759
2760 impl Default for Written {
2761 fn default() -> Self {
2762 Self {
2763 per_pa: 1.0 / ATM_PA,
2764 accel: 1.0,
2765 boost: 1.0,
2766 gyro: 1.0,
2767 altitude: 1.0,
2768 speed: 1.0,
2769 straight_coast: false,
2770 steady_coast: false,
2771 stamp: 1.0,
2772 latitude: 41.7102,
2773 longitude: -70.8301,
2774 restart_at_s: None,
2775 }
2776 }
2777 }
2778
2779 const BOOST_S: f64 = 6.0;
2781 const BURNOUT_S: f64 = 8.0;
2782 const QUARTERS: u32 = 120;
2783 const COAST_ROLL: f64 = 0.5;
2785
2786 fn apogee_s() -> f64 {
2787 BURNOUT_S + 80.0 / G
2788 }
2789
2790 fn state(t: f64) -> (f64, f64, f64, f64) {
2793 if t < BOOST_S {
2794 (0.0, 0.0, 0.0, 0.0)
2795 } else if t < BURNOUT_S {
2796 let s = t - BOOST_S;
2797 (20.0 * s * s, 40.0 * s, 0.0, 0.0)
2798 } else if t < apogee_s() {
2799 let s = t - BURNOUT_S;
2800 (80.0 + 80.0 * s - 0.5 * G * s * s, 80.0 - G * s, 0.0, 0.0)
2801 } else {
2802 let s = t - apogee_s();
2803 let top = 80.0 + 80.0 * 80.0 / (2.0 * G);
2804 let swing = 0.45 * (0.9 * s).sin();
2805 (top - 15.0 * s, -15.0, swing, 0.45 * 0.9 * (0.9 * s).cos())
2806 }
2807 }
2808
2809 fn pascals(altitude_m: f64) -> f64 {
2812 (101_325.0 * (1.0 - (300.0 + altitude_m) / 44_330.8).powf(1.0 / 0.190_263)).round()
2813 }
2814
2815 fn single(value: f64) -> f64 {
2817 #[allow(clippy::cast_possible_truncation, reason = "the board's 32-bit floats")]
2818 let single = value as f32;
2819 f64::from(single)
2820 }
2821
2822 fn short4(value: f64) -> f64 {
2824 (value * 10_000.0).round() / 10_000.0
2825 }
2826
2827 fn clock(seconds: f64) -> String {
2829 #[allow(
2830 clippy::cast_possible_truncation,
2831 clippy::cast_sign_loss,
2832 reason = "a time of day in microseconds"
2833 )]
2834 let micros = (seconds * 1e6).round() as u64;
2835 let whole = micros / 1_000_000;
2836 format!(
2837 "{:02}:{:02}:{:02}.{:06}",
2838 (whole / 3_600) % 24,
2839 (whole / 60) % 60,
2840 whole % 60,
2841 micros % 1_000_000
2842 )
2843 }
2844
2845 fn py(value: f64) -> String {
2847 format!("{value:?}")
2848 }
2849
2850 fn packet(
2852 received_s: f64,
2853 name: &str,
2854 stamp_ms: Option<f64>,
2855 fields: &[(&str, String)],
2856 ) -> String {
2857 let mut all: Vec<String> = Vec::new();
2858 if let Some(ms) = stamp_ms {
2859 all.extend([
2860 "'radio_id': 1".to_owned(),
2861 "'radio_name': '915 MHz Radio'".to_owned(),
2862 "'rssi': '-60 db'".to_owned(),
2863 "'lqi': 3".to_owned(),
2864 "'crc': 'Good'".to_owned(),
2865 "'board_serial_number': 7".to_owned(),
2866 format!("'time_stamp_ms': {ms:.0}"),
2867 "'callsign': 'N0CALL'".to_owned(),
2868 ]);
2869 }
2870 all.extend(
2871 fields
2872 .iter()
2873 .map(|(key, value)| format!("'{key}': {value}")),
2874 );
2875 format!("{}: {name} {{{}}}\n", clock(received_s), all.join(", "))
2876 }
2877
2878 fn flight(written: Written) -> String {
2881 let start_s = 15.0 * 3_600.0;
2882 let mut text = String::from("\n\nRUN START 2025-01-01 15:00:00\n\n");
2883 text.push_str(&format!(
2884 "{}: Switching radio to band 1\n",
2885 clock(start_s + 1.0)
2886 ));
2887 let pad_pa = pascals(0.0);
2888 for quarter in 0..=QUARTERS {
2889 let t = f64::from(quarter) / 4.0;
2890 let flying = t >= BOOST_S;
2891 let mut kinds = Vec::new();
2892 if flying || quarter % 4 == 0 {
2893 kinds.push(0); }
2895 if (flying && quarter % 2 == 0) || quarter % 4 == 0 {
2896 kinds.push(1); }
2898 if quarter % 4 == 0 {
2899 kinds.push(2); }
2901 if quarter % 8 == 0 {
2902 kinds.extend([3, 4]); }
2904 let board_ms = match written.restart_at_s {
2905 Some(restart) if t >= restart => 2_000.0 + (t - restart) * 1_000.0,
2906 _ => 120_000.0 + t * 1_000.0,
2907 } * written.stamp;
2908 let (z, v, swing, swing_rate) = state(t);
2909 let descending = t >= apogee_s();
2910 let coasting = (BURNOUT_S..apogee_s()).contains(&t);
2911 let top = state(apogee_s()).0;
2912 let written_z = if written.straight_coast && coasting {
2913 80.0 + (top - 80.0) * (t - BURNOUT_S) / (apogee_s() - BURNOUT_S)
2914 } else {
2915 z
2916 };
2917 let written_v = if written.steady_coast && coasting {
2918 40.0
2919 } else {
2920 v
2921 } * written.speed;
2922 let accel_scale = written.accel * if flying { written.boost } else { 1.0 };
2923 let state_number = if descending { "3" } else { "2" }.to_owned();
2924 let (accel, gyro, tilt_deg) = if t < BOOST_S {
2925 let lean = 5.0_f64.to_radians();
2926 ([G * lean.sin(), 0.0, G * lean.cos()], [0.0; 3], 5.0)
2927 } else if t < BURNOUT_S {
2928 ([0.0, 0.0, 40.0 + G], [0.0; 3], 0.0)
2929 } else if !descending {
2930 ([0.0; 3], [0.0, 0.0, COAST_ROLL], 0.0)
2931 } else {
2932 (
2933 [G * swing.sin(), 0.0, G * swing.cos()],
2934 [0.0, swing_rate, 0.0],
2935 swing.abs().to_degrees(),
2936 )
2937 };
2938 for (j, kind) in kinds.into_iter().enumerate() {
2939 #[allow(clippy::cast_precision_loss, reason = "a small count")]
2940 let received = start_s + 2.03 + t + 0.002 * j as f64;
2941 let f = |v: f64| py(short4(v));
2942 let line = match kind {
2943 0 => packet(
2944 received,
2945 ORIENTATION,
2946 Some(board_ms),
2947 &[
2948 ("fcb_state_number", state_number.clone()),
2949 ("rot_vel_x", f(gyro[0] * written.gyro)),
2950 ("rot_vel_y", f(gyro[1] * written.gyro)),
2951 ("rot_vel_z", f(gyro[2] * written.gyro)),
2952 ("accel_x", f(accel[0] * accel_scale)),
2953 ("accel_y", f(accel[1] * accel_scale)),
2954 ("accel_z", f(accel[2] * accel_scale)),
2955 ("magnetic_field_x", f(0.21)),
2956 ("magnetic_field_y", f(-0.13)),
2957 ("magnetic_field_z", f(0.42)),
2958 ("angle_vertical", py((tilt_deg * 10.0).round() / 10.0)),
2959 ("roll", f(0.0)),
2960 ("pitch", f(swing)),
2961 ("yaw", f(0.1)),
2962 (
2963 "orientation_quaternion",
2964 format!(
2965 "[{}, 0.0, {}, 0.0]",
2966 f((swing / 2.0).cos()),
2967 f((swing / 2.0).sin())
2968 ),
2969 ),
2970 ("orientation_rpy", format!("[0.0, {}, 0.1]", f(swing))),
2971 ],
2972 ),
2973 1 => packet(
2974 received,
2975 POSITION,
2976 Some(board_ms),
2977 &[
2978 ("temperature", py(single(25.5))),
2979 ("fcb_altitude", py(single(written_z * written.altitude))),
2980 ("v_speed", py(single(written_v))),
2981 ("fcb_latitude", py(written.latitude + 1e-4 * z / 400.0)),
2982 ("fcb_longitude", py(written.longitude)),
2983 ("gps_alt", py(single(((300.0 + z) * 10.0).round() / 10.0))),
2984 ("fcb_battery_voltage", py(single(7.9))),
2985 ("ground_speed", py(0.0)),
2986 ("course_over_ground", py(0.0)),
2987 ("gps_time", format!("'2025-01-01 20:00:{:02.0}'", t.floor())),
2988 ("gps_sats", "9".to_owned()),
2989 ("fcb_state_number", state_number.clone()),
2990 ],
2991 ),
2992 2 => {
2993 let p = pascals(z);
2994 packet(
2995 received,
2996 ALT_INFO,
2997 Some(board_ms),
2998 &[
2999 ("barometer_pressure", py(single(p * written.per_pa))),
3000 (
3001 "barometer_2_pressure",
3002 py(single((p + 3.0) * written.per_pa)),
3003 ),
3004 ("press_ref", py(single((pad_pa + 0.4) * written.per_pa))),
3005 ("ground_elev", py(0.0)),
3006 ("ground_temp", py(20.0)),
3007 ("pitot_ducer_press", py(0.0)),
3008 ],
3009 )
3010 }
3011 3 => packet(
3012 received,
3013 "Pyro Info",
3014 Some(board_ms),
3015 &[
3016 ("pyro_continuity", "[True, True, False, False]".to_owned()),
3017 ("flash_usage", "10".to_owned()),
3018 ],
3019 ),
3020 _ => {
3021 let fix = t > 0.0;
3022 packet(
3023 received,
3024 GROUND_STATION[0],
3025 None,
3026 &[
3027 ("gs_lat", py(if fix { 41.7101 } else { 0.0 })),
3028 ("gs_lon", py(if fix { -70.8302 } else { 0.0 })),
3029 ("gs_alt", py(300.0)),
3030 ("gs_baro", py((pad_pa + 120.0 + t) * written.per_pa)),
3031 ("gs_temp", py(30.25)),
3032 ],
3033 )
3034 }
3035 };
3036 text.push_str(&line);
3037 }
3038 }
3039 text
3040 }
3041
3042 fn channel<'a>(log: &'a FlightLog, column: &str) -> &'a Channel {
3044 log.channels
3045 .iter()
3046 .find(|c| c.column == column)
3047 .unwrap_or_else(|| panic!("no {column}"))
3048 }
3049
3050 fn note<'a>(log: &'a FlightLog, start: &str) -> &'a str {
3052 let matching: Vec<&String> = log.notes.iter().filter(|n| n.starts_with(start)).collect();
3053 assert_eq!(matching.len(), 1, "{start}: {:#?}", log.notes);
3054 matching[0]
3055 }
3056
3057 fn changed(text: &str, packet: &str, from: &str, to: &str) -> (String, usize) {
3060 let target = text
3061 .lines()
3062 .position(|line| line.contains(packet) && line.contains(from))
3063 .unwrap_or_else(|| panic!("no {from}"));
3064 let mut out = String::new();
3065 for (k, line) in text.lines().enumerate() {
3066 if k == target {
3067 out.push_str(&line.replacen(from, to, 1));
3068 } else {
3069 out.push_str(line);
3070 }
3071 out.push('\n');
3072 }
3073 (out, target + 1)
3074 }
3075
3076 #[test]
3077 fn python_literals_parse() {
3078 let value = parse_literal(
3079 r#"{'a': 1, "b": -2.5e-3, 'c': 'it\'s \\ \x41é\n', 'd': [True, False, None], 'e': (1, nan, -inf, +inf), 'f': {'g': .5,}, 'h': 1., 'i': +3, 'j': 1E+2}"#,
3080 )
3081 .unwrap();
3082 let Value::Dict(entries) = value else {
3083 panic!("{value:?}")
3084 };
3085 assert_eq!(entries[0], ("a".to_owned(), Value::Number(1.0)));
3086 assert_eq!(entries[1], ("b".to_owned(), Value::Number(-0.0025)));
3087 assert_eq!(
3088 entries[2],
3089 ("c".to_owned(), Value::Text("it's \\ Aé\n".to_owned()))
3090 );
3091 assert_eq!(
3092 entries[3].1,
3093 Value::List(vec![Value::Bool(true), Value::Bool(false), Value::Nothing])
3094 );
3095 let Value::List(numbers) = &entries[4].1 else {
3096 panic!("{:?}", entries[4])
3097 };
3098 assert!(matches!(numbers[1], Value::Number(n) if n.is_nan()));
3099 assert_eq!(numbers[2], Value::Number(f64::NEG_INFINITY));
3100 assert_eq!(numbers[3], Value::Number(f64::INFINITY));
3101 assert_eq!(
3102 entries[5].1,
3103 Value::Dict(vec![("g".to_owned(), Value::Number(0.5))])
3104 );
3105 assert_eq!(entries[6].1, Value::Number(1.0));
3106 assert_eq!(entries[7].1, Value::Number(3.0));
3107 assert_eq!(entries[8].1, Value::Number(100.0));
3108 assert_eq!(
3109 parse_literal(r"'\101\0\q'").unwrap(),
3110 Value::Text("A\0\\q".to_owned())
3111 );
3112 assert_eq!(parse_literal(" [] ").unwrap(), Value::List(Vec::new()));
3113 let deep = format!("{}1{}", "[".repeat(MAX_DEPTH), "]".repeat(MAX_DEPTH));
3115 assert!(parse_literal(&deep).is_ok());
3116 let wide = format!("[{}]", vec!["0"; MAX_ITEMS].join(","));
3117 assert!(parse_literal(&wide).is_ok());
3118 }
3119
3120 #[test]
3121 fn malformed_literals_are_refused_by_why() {
3122 let deep = format!(
3123 "{}1{}",
3124 "[".repeat(MAX_DEPTH + 1),
3125 "]".repeat(MAX_DEPTH + 1)
3126 );
3127 let wide = format!("[{}]", vec!["0"; MAX_ITEMS + 1].join(","));
3128 let keys: Vec<String> = (0..=MAX_ITEMS).map(|k| format!("'k{k}': 0")).collect();
3129 let many_keys = format!("{{{}}}", keys.join(","));
3130 let cases = [
3131 ("{'a': 1", "no comma or `}` after an item"),
3132 ("{'a': ", "the line ends inside the packet"),
3133 ("{'a' 1}", "no `:` after a key"),
3134 ("{1: 2}", "a key that isn't text"),
3135 ("{'a': 1, 'a': 2}", "the key \"a\" given twice"),
3136 ("{'a': 1.2.3}", "a number Python wouldn't write"),
3137 ("{'a': 0x10}", "a number Python wouldn't write"),
3138 ("{'a': 1e}", "an exponent without digits"),
3139 ("{'a': -}", "a number without digits"),
3140 ("{'a': -True}", "a sign before a word that isn't a number"),
3141 ("{'a': foo}", "the word \"foo\", no Python literal"),
3142 ("{'a': 'open}", "a text in quotes runs to the line's end"),
3143 ("{'a': '\\x4'}", "an escape without its 2 digits"),
3144 ("{'a': 1} extra", "text after the literal"),
3145 ("{'a': 1 'b': 2}", "no comma or `}` after an item"),
3146 ("{'a': @}", "an unexpected character"),
3147 (
3148 deep.as_str(),
3149 "lists or dictionaries nested more than 8 deep",
3150 ),
3151 (wide.as_str(), "a list of more than 1024 items"),
3152 (many_keys.as_str(), "a dictionary of more than 1024 items"),
3153 ];
3154 for (text, why) in cases {
3155 let error = parse_literal(text).unwrap_err();
3156 assert!(error.starts_with(why), "{text:.40}: {error}");
3157 }
3158 }
3159
3160 #[test]
3161 fn times_of_day_are_strict() {
3162 assert_eq!(
3163 time_of_day("14:45:09.743501"),
3164 Some(14.0 * 3600.0 + 45.0 * 60.0 + 9.743_501)
3165 );
3166 assert_eq!(time_of_day("00:00:00"), Some(0.0));
3167 for bad in [
3168 "24:00:00",
3169 "1:00:00",
3170 "12:60:00",
3171 "12:00:60",
3172 "12:00",
3173 "12:00:00:00",
3174 "aa:bb:cc",
3175 "12:00:0.5",
3176 "12:00:00.-5",
3177 "12:00:00.1234567890",
3178 ] {
3179 assert_eq!(time_of_day(bad), None, "{bad}");
3180 }
3181 }
3182
3183 #[test]
3187 fn the_step_check_tells_atmospheres_from_bars() {
3188 let pascals: Vec<f64> = (0..200).map(|k| 95_000.0 + 37.0 * f64::from(k)).collect();
3189 let atm: Vec<f64> = pascals.iter().map(|p| single(p / ATM_PA)).collect();
3190 let bar: Vec<f64> = pascals.iter().map(|p| single(p / BAR_PA)).collect();
3191 let (on_atm, on_bar, count) = steps(&atm);
3192 assert_eq!(count, 200);
3193 assert!((on_atm - 1.0).abs() < f64::EPSILON, "{on_atm}");
3194 assert!(on_bar < 0.05, "{on_bar}");
3195 let (on_atm, on_bar, _) = steps(&bar);
3196 assert!((on_bar - 1.0).abs() < f64::EPSILON, "{on_bar}");
3197 assert!(on_atm < 0.05, "{on_atm}");
3198 let both: Vec<f64> = (20..36).map(|m| f64::from(m) / 25.0).collect();
3199 let (on_atm, on_bar, _) = steps(&both);
3200 assert!(on_atm > 0.99 && on_bar > 0.99, "{on_atm} {on_bar}");
3201 assert!(on_step(103_587.0 / ATM_PA, ATM_PA));
3203 assert!(!on_step(103_587.01 / ATM_PA, ATM_PA));
3204 assert!((half_ulp(single(0.99)) - 2f64.powi(-25)).abs() < f64::EPSILON);
3206 assert!((half_ulp(single(1.01)) - 2f64.powi(-24)).abs() < f64::EPSILON);
3207 assert!((half_ulp(0.1) - 0.5 * (f64::from_bits(0.1_f64.to_bits() + 1) - 0.1)).abs() == 0.0);
3208 }
3209
3210 #[test]
3213 fn a_flight_reads_every_unit_by_two_checks() {
3214 let log = read(&flight(Written::default())).unwrap();
3215 assert_eq!(log.format, LogFormat::AerospaceNuGroundStationText);
3216 let read = [
3217 ("Alt Info & Cfg.barometer_pressure", Quantity::Pressure),
3218 ("Alt Info & Cfg.barometer_2_pressure", Quantity::Pressure),
3219 ("Alt Info & Cfg.press_ref", Quantity::Pressure),
3220 ("Ground Station GPS.gs_baro", Quantity::Pressure),
3221 ("Position Data.fcb_altitude", Quantity::AltitudeAboveGround),
3222 ("Position Data.v_speed", Quantity::Speed),
3223 ("Position Data.fcb_latitude", Quantity::Latitude),
3224 ("Position Data.fcb_longitude", Quantity::Longitude),
3225 ("Ground Station GPS.gs_lat", Quantity::Latitude),
3226 ("Orientation.accel_z", Quantity::Acceleration),
3227 ("Orientation.rot_vel_y", Quantity::AngularRate),
3228 ("Orientation.angle_vertical", Quantity::Angle),
3229 ("Position Data.gps_time", Quantity::TimeOfDay),
3230 ("Position Data.rssi", Quantity::Number),
3231 ("Pyro Info.pyro_continuity[0]", Quantity::Number),
3232 ("Orientation.orientation_quaternion[2]", Quantity::Number),
3233 ("Position Data.temperature", Quantity::Unrecognized),
3234 ("Position Data.gps_alt", Quantity::Unrecognized),
3235 ("Orientation.pitch", Quantity::Unrecognized),
3236 ("Orientation.orientation_rpy[1]", Quantity::Unrecognized),
3237 ("Orientation.magnetic_field_x", Quantity::Unrecognized),
3238 ];
3239 for (column, expected) in read {
3240 assert_eq!(channel(&log, column).quantity, expected, "{column}");
3241 }
3242 assert!(
3244 log.channels
3245 .iter()
3246 .all(|c| !c.column.ends_with("time_stamp_ms"))
3247 );
3248 let pressure = channel(&log, "Alt Info & Cfg.barometer_pressure");
3250 assert!(
3251 (pressure.values[0] - pascals(0.0)).abs() < 0.01,
3252 "{}",
3253 pressure.values[0]
3254 );
3255 let swing = channel(&log, "Orientation.rot_vel_y");
3256 let fastest = swing.values.iter().copied().fold(0.0, f64::max);
3257 assert!((fastest - 0.405_f64.to_degrees()).abs() < 0.5, "{fastest}");
3258 assert!(log.time_s.windows(2).all(|pair| pair[1] > pair[0]));
3260 assert!(log.altitude_m[0].abs() < f64::EPSILON);
3261 let top = log.altitude_m.iter().copied().fold(0.0, f64::max);
3262 assert!((top - 406.3).abs() < 0.5, "{top}");
3263 assert_eq!(
3264 log.vertical_speed_m_s.as_ref().map(Vec::len),
3265 Some(log.time_s.len())
3266 );
3267 assert!((log.time_s[0] - 2.03).abs() < 0.05, "{}", log.time_s[0]);
3269 for start in [
3270 "`time_stamp_ms` read in milliseconds, from its name and the ground station's clock: ",
3271 "`Alt Info & Cfg.barometer_pressure` read in atmospheres, from its size and its step: ",
3272 "`Alt Info & Cfg.barometer_2_pressure` read in atmospheres, from its size and its \
3273 step: ",
3274 "`Ground Station GPS.gs_baro` read in atmospheres, from its size and its step: ",
3275 "`Alt Info & Cfg.press_ref` read in atmospheres, from its size and the barometer's: ",
3276 "`Position Data.fcb_altitude` read in meters, from the barometer and the coast: ",
3277 "`Position Data.v_speed` read in meters per second, from the barometer and the coast: ",
3278 "`Position Data.fcb_latitude` and `Position Data.fcb_longitude` read in degrees, from \
3279 their range and their fractions: ",
3280 "`Ground Station GPS.gs_lat` and `Ground Station GPS.gs_lon` read in degrees, from \
3281 their range and their fractions: ",
3282 "`Orientation.accel_x`, `Orientation.accel_y` and `Orientation.accel_z` read in meters \
3283 per second squared, from the pad's gravity and the barometer: ",
3284 "`Orientation.angle_vertical` read in degrees, from its range and the accelerometer: ",
3285 "`Orientation.rot_vel_x`, `Orientation.rot_vel_y` and `Orientation.rot_vel_z` read in \
3286 radians per second, from the board's turning and its tilt: ",
3287 "`Position Data.temperature` kept as written, in degrees Celsius or Fahrenheit alike: ",
3288 "`Position Data.gps_alt` kept as written, with no check of its unit but the \
3289 barometer's: ",
3290 "`Orientation.roll`, `Orientation.pitch`, `Orientation.yaw`, \
3291 `Orientation.orientation_rpy[0]`, `Orientation.orientation_rpy[1]` and \
3292 `Orientation.orientation_rpy[2]` kept as written, with no check of their unit but \
3293 their range: ",
3294 "1 position of (0, 0), the receiver's mark for no fix, is a gap",
3295 "1 line is the ground station's own message, not a packet, \"Switching radio to band \
3296 1\" on line 5: skipped",
3297 "the fields `callsign`, `radio_name` hold text, not numbers",
3298 ] {
3299 note(&log, start);
3300 }
3301 assert!(
3302 log.notes.iter().all(|n| !n.contains("assum")),
3303 "{:#?}",
3304 log.notes
3305 );
3306 assert!(channel(&log, "Ground Station GPS.gs_lat").values[0].is_nan());
3308 }
3309
3310 #[test]
3313 fn pressures_in_bars_are_kept_and_the_altitude_with_them() {
3314 let log = read(&flight(Written {
3315 per_pa: 1.0 / BAR_PA,
3316 ..Written::default()
3317 }))
3318 .unwrap();
3319 for column in [
3320 "Alt Info & Cfg.barometer_pressure",
3321 "Alt Info & Cfg.press_ref",
3322 "Ground Station GPS.gs_baro",
3323 "Position Data.fcb_altitude",
3324 "Position Data.v_speed",
3325 ] {
3326 assert_eq!(
3327 channel(&log, column).quantity,
3328 Quantity::Unrecognized,
3329 "{column}"
3330 );
3331 }
3332 let kept = note(
3333 &log,
3334 "`Alt Info & Cfg.barometer_pressure` kept as written, its size or its step not an \
3335 atmosphere's: ",
3336 );
3337 assert!(
3338 kept.contains("100.0% are a whole number of pascals divided by 100,000"),
3339 "{kept}"
3340 );
3341 note(
3342 &log,
3343 "`Alt Info & Cfg.press_ref` kept as written, with no barometer read in atmospheres \
3344 beside it: ",
3345 );
3346 note(
3347 &log,
3348 "`Position Data.fcb_altitude` kept as written, with no barometer read in atmospheres: ",
3349 );
3350 note(
3351 &log,
3352 "`Position Data.v_speed` kept as written, with no barometer read in atmospheres: ",
3353 );
3354 note(
3355 &log,
3356 "`Orientation.accel_x`, `Orientation.accel_y` and `Orientation.accel_z` kept as \
3357 written, the pad's gravity and the barometer not both bearing meters per second \
3358 squared out: ",
3359 );
3360 assert!(log.time_s.is_empty() && log.altitude_m.is_empty());
3361 assert!(log.vertical_speed_m_s.is_none());
3362 note(&log, "the record has no altitude track: ");
3363 }
3364
3365 #[test]
3369 fn pressures_on_both_steps_or_neither_are_kept() {
3370 let mut both = String::from("RUN START 2025-01-01 10:00:00\n");
3371 let mut neither = both.clone();
3372 let mut thousandths = both.clone();
3373 for m in 20..36 {
3374 thousandths.push_str(&format!(
3375 "10:00:{m:02}.0: Alt Info & Cfg {{'barometer_pressure': {}}}\n",
3376 py((95_000.0 + 101.0 * f64::from(m)) / 101.325)
3377 ));
3378 both.push_str(&format!(
3379 "10:00:{m:02}.0: Alt Info & Cfg {{'barometer_pressure': {}}}\n",
3380 py(f64::from(m) / 25.0)
3381 ));
3382 neither.push_str(&format!(
3383 "10:00:{m:02}.0: Alt Info & Cfg {{'barometer_pressure': {}}}\n",
3384 py(single((95_000.5 + 101.0 * f64::from(m)) / ATM_PA))
3385 ));
3386 }
3387 for (text, shares) in [
3388 (
3389 both,
3390 "100.0% are a whole number of pascals divided by 101,325. Of the same values, \
3391 100.0% are a whole number of pascals divided by 100,000",
3392 ),
3393 (
3394 neither,
3395 "0.0% are a whole number of pascals divided by 101,325. Of the same values, \
3396 0.0% are a whole number of pascals divided by 100,000",
3397 ),
3398 (
3399 thousandths,
3400 "100.0% are a whole number of pascals divided by 101,325",
3401 ),
3402 ] {
3403 let log = read(&text).unwrap();
3404 let kept = note(
3405 &log,
3406 "`Alt Info & Cfg.barometer_pressure` kept as written, its size or its step not an \
3407 atmosphere's: ",
3408 );
3409 assert!(kept.contains(shares), "{kept}");
3410 assert!(kept.contains("within its own rounding as sent"), "{kept}");
3411 assert_eq!(
3412 channel(&log, "Alt Info & Cfg.barometer_pressure").quantity,
3413 Quantity::Unrecognized
3414 );
3415 }
3416 }
3417
3418 #[test]
3420 fn an_altitude_the_barometer_refutes_refuses_the_file() {
3421 let error = read(&flight(Written {
3422 altitude: 1.0 / 0.3048,
3423 ..Written::default()
3424 }))
3425 .unwrap_err();
3426 let LogError::Unit { line, message, .. } = &error else {
3427 panic!("{error}")
3428 };
3429 assert_eq!(*line, 3);
3430 assert!(
3431 message.starts_with("`Position Data.fcb_altitude` isn't in meters, by the barometer: ")
3432 && message.contains(" 3.2")
3433 && message.ends_with(" times that. Meters need within 25%"),
3434 "{message}"
3435 );
3436 }
3437
3438 #[test]
3441 fn an_accelerometer_in_g_is_kept_and_what_rests_on_it() {
3442 let log = read(&flight(Written {
3443 accel: 1.0 / G,
3444 ..Written::default()
3445 }))
3446 .unwrap();
3447 let kept = note(
3448 &log,
3449 "`Orientation.accel_x`, `Orientation.accel_y` and `Orientation.accel_z` kept as \
3450 written, the pad's gravity and the barometer not both bearing meters per second \
3451 squared out: ",
3452 );
3453 assert!(
3454 kept.contains("within 10% of 9.806 65 for 0.0 s")
3455 && kept
3456 .contains("Past 10 m of that climb there are 0 pressures, under the 3 needed"),
3457 "{kept}"
3458 );
3459 note(
3460 &log,
3461 "`Orientation.angle_vertical` kept as written, with no accelerometer read in m/s²",
3462 );
3463 note(
3464 &log,
3465 "`Orientation.rot_vel_x`, `Orientation.rot_vel_y` and `Orientation.rot_vel_z` kept as \
3466 written, with no accelerometer read in m/s²",
3467 );
3468 for column in [
3469 "Orientation.accel_z",
3470 "Orientation.angle_vertical",
3471 "Orientation.rot_vel_y",
3472 ] {
3473 assert_eq!(
3474 channel(&log, column).quantity,
3475 Quantity::Unrecognized,
3476 "{column}"
3477 );
3478 }
3479 }
3480
3481 #[test]
3484 fn a_gyroscope_in_degrees_per_second_is_kept() {
3485 let log = read(&flight(Written {
3486 gyro: 180.0 / PI,
3487 ..Written::default()
3488 }))
3489 .unwrap();
3490 note(
3491 &log,
3492 "`Orientation.rot_vel_x`, `Orientation.rot_vel_y` and `Orientation.rot_vel_z` kept as \
3493 written, the board's turning and its tilt not both radians per second's: ",
3494 );
3495 assert_eq!(
3496 channel(&log, "Orientation.rot_vel_y").quantity,
3497 Quantity::Unrecognized
3498 );
3499 assert_eq!(
3501 channel(&log, "Orientation.angle_vertical").quantity,
3502 Quantity::Angle
3503 );
3504 }
3505
3506 #[test]
3508 fn a_tilt_in_radians_is_kept() {
3509 let text = flight(Written::default());
3510 let mut out = String::new();
3511 for line in text.lines() {
3512 let line = match line.split_once("'angle_vertical': ") {
3513 Some((before, after)) => {
3514 let (value, rest) = after.split_once(',').unwrap();
3515 let radians = value.parse::<f64>().unwrap().to_radians();
3516 format!("{before}'angle_vertical': {},{rest}", py(short4(radians)))
3517 }
3518 None => line.to_owned(),
3519 };
3520 out.push_str(&line);
3521 out.push('\n');
3522 }
3523 let log = read(&out).unwrap();
3524 let kept = note(
3525 &log,
3526 "`Orientation.angle_vertical` kept as written, its range or the accelerometer not \
3527 degrees': ",
3528 );
3529 assert!(
3530 kept.contains("its values run from 0 to 0.4485")
3531 && kept.contains("Read in radians, it is 0.03°"),
3532 "{kept}"
3533 );
3534 assert_eq!(
3536 channel(&log, "Orientation.rot_vel_y").quantity,
3537 Quantity::Unrecognized
3538 );
3539 }
3540
3541 #[test]
3544 fn the_tilt_check_refutes_too_slow_a_unit() {
3545 let mut reading = Reading::default();
3546 let mut samples = Vec::new();
3547 for k in 0..41 {
3548 let t = 0.25 * f64::from(k);
3549 reading.slots.push(Slot {
3550 line: 1,
3551 received_s: t,
3552 board_ms: None,
3553 time_s: t,
3554 });
3555 let tilt = 40.0 * (0.5 * t).sin();
3557 let rate = (20.0 * (0.5 * t).cos()).to_radians();
3558 samples.push((reading.slots.len() - 1, tilt, [0.0, rate, 0.0]));
3559 }
3560 let (windows, tilted, turned) = tilt_check(&reading, &samples);
3561 assert!(windows >= TILT_LEAST_WINDOWS, "{windows}");
3562 assert!(tilted <= TILT_FACTOR * turned, "{tilted} {turned}");
3563 assert!(
3564 tilted > TILT_FACTOR * turned * PI / 180.0,
3565 "{tilted} {turned}"
3566 );
3567 let (windows, _, _) = tilt_check(&reading, &samples[..9]);
3568 assert!(windows < TILT_LEAST_WINDOWS, "{windows}");
3569 }
3570
3571 #[test]
3574 fn a_clock_in_other_units_is_kept_and_the_ground_stations_used() {
3575 let log = read(&flight(Written {
3576 stamp: 0.001,
3577 ..Written::default()
3578 }))
3579 .unwrap();
3580 note(
3581 &log,
3582 "`time_stamp_ms` kept as written, unchecked against the ground station's clock: ",
3583 );
3584 let stamps = channel(&log, "Position Data.time_stamp_ms");
3585 assert_eq!(stamps.quantity, Quantity::Unrecognized);
3586 assert!((stamps.values[0] - 120.0).abs() < f64::EPSILON);
3587 assert!((log.time_s[0] - 2.032).abs() < 1e-6, "{}", log.time_s[0]);
3589 assert!(log.time_s.windows(2).all(|pair| pair[1] > pair[0]));
3590 }
3591
3592 #[test]
3595 fn a_restart_is_placed_by_its_own_gap() {
3596 let log = read(&flight(Written {
3597 restart_at_s: Some(3.0),
3598 ..Written::default()
3599 }))
3600 .unwrap();
3601 note(
3602 &log,
3603 "the board's clock runs in 2 stretches, each a steady gap from the ground station's \
3604 clock",
3605 );
3606 let steps: Vec<f64> = log
3607 .time_s
3608 .windows(2)
3609 .map(|pair| pair[1] - pair[0])
3610 .collect();
3611 assert!(
3612 steps.iter().all(|step| (0.4..1.1).contains(step)),
3613 "{steps:?}"
3614 );
3615 assert!(!log.notes.iter().any(|n| n.contains("goes back in time")));
3616 }
3617
3618 #[test]
3621 fn skipped_lines_are_counted_by_kind() {
3622 let mut text = flight(Written::default());
3623 let mut line = text.lines().count();
3624 let mut next = |text: &mut String, added: &str| {
3625 text.push_str(added);
3626 text.push('\n');
3627 line += 1;
3628 line
3629 };
3630 let corrupt = next(
3631 &mut text,
3632 "15:00:40.000000: Position Data {'crc': 'Bad', 'time_stamp_ms': 9999999999, \
3633 'temperature': -3.1e11}",
3634 );
3635 next(
3636 &mut text,
3637 "15:00:40.100000: Invalid message type 26 Could not parse message",
3638 );
3639 let unparsed = next(
3640 &mut text,
3641 "15:00:40.200000: Pyro Info {'crc': 'Good', 'flash_usage': 'é', 'n': 1,,}",
3642 );
3643 let other = next(&mut text, "garbage without a time");
3644 let long = next(
3645 &mut text,
3646 &format!(
3647 "15:00:40.300000: X {{'a': '{}'}}",
3648 "x".repeat(MAX_LINE_BYTES)
3649 ),
3650 );
3651 let run = next(&mut text, "RUN START 2025-01-01 16:00:00");
3652 let gap = next(
3653 &mut text,
3654 "15:00:40.400000: CLI Data {'crc': 'Good', 'time_stamp_ms': 158370, 'cli_string': \
3655 'OK', 'x': nan}",
3656 );
3657 let log = read(&text).unwrap();
3658 note(
3659 &log,
3660 &format!(
3661 "1 packet, on line {corrupt}, is left out: the ground station marks its `crc` \
3662 other than `'Good'`, its bytes"
3663 ),
3664 );
3665 note(&log, "2 lines are the ground station's own messages");
3666 note(
3669 &log,
3670 &format!(
3671 "1 packet line, on line {unparsed}, doesn't parse as a Python literal: an \
3672 unexpected character at character 71 of the line; skipped"
3673 ),
3674 );
3675 note(
3676 &log,
3677 &format!("1 line, on line {other}, opens with no time of day"),
3678 );
3679 note(
3680 &log,
3681 &format!("1 line, on line {long}, is longer than 65536 bytes"),
3682 );
3683 note(
3684 &log,
3685 &format!("1 more `RUN START` line, on line {run}, is skipped"),
3686 );
3687 note(
3688 &log,
3689 &format!(
3690 "1 value, on line {gap}, is `None`, `nan`, `inf` or text that isn't a number: it \
3691 is a gap"
3692 ),
3693 );
3694 note(
3695 &log,
3696 "the fields `callsign`, `cli_string`, `radio_name` hold text",
3697 );
3698 note(
3699 &log,
3700 "1 field of the `CLI Data` packets, `x`, isn't one this reader knows: its values are \
3701 kept as written",
3702 );
3703 let temperature = channel(&log, "Position Data.temperature");
3705 assert!(temperature.values.iter().all(|v| *v > 0.0));
3706 assert!(channel(&log, "CLI Data.x").values[0].is_nan());
3707 }
3708
3709 #[test]
3713 fn a_value_too_large_for_its_conversion_refuses_the_file() {
3714 let (text, line) = changed(
3715 &flight(Written::default()),
3716 ORIENTATION,
3717 "'rot_vel_z': 0.5",
3718 "'rot_vel_z': 1.7e308",
3719 );
3720 assert_eq!(
3721 read(&text).unwrap_err(),
3722 LogError::Syntax {
3723 format: FORMAT,
3724 line,
3725 message: "the column Orientation.rot_vel_z holds a value too large for its unit's \
3726 conversion to SI"
3727 .to_owned(),
3728 }
3729 );
3730 let (text, _) = changed(
3731 &flight(Written {
3732 accel: 1.0 / G,
3733 ..Written::default()
3734 }),
3735 ORIENTATION,
3736 "'rot_vel_z': 0.5",
3737 "'rot_vel_z': 1.7e308",
3738 );
3739 let log = read(&text).unwrap();
3740 let kept = channel(&log, "Orientation.rot_vel_z");
3741 assert!(kept.values.contains(&1.7e308));
3742 }
3743
3744 #[test]
3747 fn positions_that_could_be_degrees_and_minutes_are_kept() {
3748 let log = read(&flight(Written {
3749 latitude: 41.3102,
3750 longitude: -70.4301,
3751 ..Written::default()
3752 }))
3753 .unwrap();
3754 note(
3755 &log,
3756 "`Position Data.fcb_latitude` and `Position Data.fcb_longitude` kept as written, their \
3757 range or their fractions not degrees': ",
3758 );
3759 assert_eq!(
3760 channel(&log, "Position Data.fcb_latitude").quantity,
3761 Quantity::Unrecognized
3762 );
3763 }
3764
3765 #[test]
3768 fn an_altitude_whose_coast_isnt_gravitys_is_kept() {
3769 let log = read(&flight(Written {
3770 straight_coast: true,
3771 ..Written::default()
3772 }))
3773 .unwrap();
3774 let kept = note(
3775 &log,
3776 "`Position Data.fcb_altitude` kept as written, the barometer and the coast not both \
3777 bearing meters out: ",
3778 );
3779 assert!(
3780 kept.contains("Meters need within 25%. Over the coast,"),
3781 "{kept}"
3782 );
3783 assert_eq!(
3784 channel(&log, "Position Data.fcb_altitude").quantity,
3785 Quantity::Unrecognized
3786 );
3787 assert!(log.time_s.is_empty());
3788 assert_eq!(
3790 channel(&log, "Position Data.v_speed").quantity,
3791 Quantity::Speed
3792 );
3793 }
3794
3795 #[test]
3798 fn a_speed_in_feet_per_second_is_kept() {
3799 let log = read(&flight(Written {
3800 speed: 1.0 / 0.3048,
3801 ..Written::default()
3802 }))
3803 .unwrap();
3804 let kept = note(
3805 &log,
3806 "`Position Data.v_speed` kept as written, the barometer and the coast not both bearing \
3807 meters per second out: ",
3808 );
3809 assert!(
3810 kept.contains("The climb is 0.30") && kept.contains(" 3.4"),
3811 "{kept}"
3812 );
3813 assert_eq!(
3814 channel(&log, "Position Data.v_speed").quantity,
3815 Quantity::Unrecognized
3816 );
3817 assert!(log.vertical_speed_m_s.is_none());
3818 assert_eq!(
3819 channel(&log, "Position Data.fcb_altitude").quantity,
3820 Quantity::AltitudeAboveGround
3821 );
3822 }
3823
3824 #[test]
3827 fn a_speed_steady_through_the_coast_is_kept() {
3828 let log = read(&flight(Written {
3829 steady_coast: true,
3830 ..Written::default()
3831 }))
3832 .unwrap();
3833 let kept = note(
3834 &log,
3835 "`Position Data.v_speed` kept as written, the barometer and the coast not both bearing \
3836 meters per second out: ",
3837 );
3838 let ratio = kept
3839 .split("The climb is ")
3840 .nth(1)
3841 .and_then(|rest| rest.split(' ').next())
3842 .and_then(|r| r.parse::<f64>().ok())
3843 .unwrap_or(f64::NAN);
3844 assert!((ratio - 1.0).abs() <= MARGIN, "{kept}");
3845 assert_eq!(
3846 channel(&log, "Position Data.v_speed").quantity,
3847 Quantity::Unrecognized
3848 );
3849 }
3850
3851 #[test]
3854 fn an_accelerometer_whose_climb_the_barometer_refutes_is_kept() {
3855 let log = read(&flight(Written {
3856 boost: 3.0,
3857 ..Written::default()
3858 }))
3859 .unwrap();
3860 let kept = note(
3861 &log,
3862 "`Orientation.accel_x`, `Orientation.accel_y` and `Orientation.accel_z` kept as \
3863 written, the pad's gravity and the barometer not both bearing meters per second \
3864 squared out: ",
3865 );
3866 let ratio = kept
3867 .split("standard atmosphere is ")
3868 .nth(1)
3869 .and_then(|rest| rest.split(' ').next())
3870 .and_then(|r| r.parse::<f64>().ok())
3871 .unwrap_or(f64::NAN);
3872 assert!(ratio < 1.0 / GRAVITY_FACTOR, "{kept}");
3873 assert!(kept.contains("within 10% of 9.806 65 for 6.0 s"), "{kept}");
3874 for column in ["Orientation.accel_z", "Orientation.angle_vertical"] {
3875 assert_eq!(
3876 channel(&log, column).quantity,
3877 Quantity::Unrecognized,
3878 "{column}"
3879 );
3880 }
3881 }
3882
3883 #[test]
3886 fn the_coast_fits_tell_meters_from_feet() {
3887 let mut points: Vec<(f64, f64)> = (0..5).map(|k| (f64::from(k) * 0.2, 0.0)).collect();
3888 for k in 0..=40 {
3889 let t = 1.0 + 0.2 * f64::from(k);
3890 let s = t - 1.0;
3891 points.push((t, 300.0 * s + 60.0 * s - 0.5 * G * s * s));
3892 }
3893 let found = altitude_coast(&points).unwrap();
3894 assert!((found.deceleration - G).abs() < 1e-6, "{found:?}");
3895 assert!(near_gravity(found.deceleration));
3896 let feet: Vec<(f64, f64)> = points.iter().map(|&(t, z)| (t, z / 0.3048)).collect();
3897 let found = altitude_coast(&feet).unwrap();
3898 assert!(!near_gravity(found.deceleration), "{found:?}");
3899 let speeds: Vec<(f64, f64)> = (0..=30)
3900 .map(|k| f64::from(k) * 0.25)
3901 .map(|t| (t, 70.0 - G * t))
3902 .collect();
3903 let found = speed_coast(&speeds).unwrap();
3904 assert!((found.deceleration - G).abs() < 1e-9, "{found:?}");
3905 assert!(!near_gravity(found.deceleration / 0.3048));
3906 assert!(near_gravity(G / 1.79) && near_gravity(G * 1.79));
3907 assert!(!near_gravity(G / 1.81) && !near_gravity(G * 1.81) && !near_gravity(f64::NAN));
3908 assert_eq!(speed_coast(&speeds[26..]), None);
3910 let close: Vec<(f64, f64)> = (0..6)
3911 .map(|k| (0.1 * f64::from(k), 5.0 - f64::from(k)))
3912 .collect();
3913 assert_eq!(speed_coast(&close), None);
3914 }
3915
3916 #[test]
3919 fn the_longest_stretch_is_found_in_linear_work() {
3920 let packets: u32 = 20_000;
3921 let mut slots: Vec<Slot> = (0..packets)
3922 .map(|k| {
3923 let received_s = 0.1 * f64::from(k);
3924 let board_ms = if k < packets / 2 {
3925 received_s * 1_000.0
3926 } else if k % 2 == 0 {
3927 0.0
3928 } else {
3929 100_000_000.0
3930 };
3931 Slot {
3932 line: 1,
3933 received_s,
3934 board_ms: Some(board_ms),
3935 time_s: received_s,
3936 }
3937 })
3938 .collect();
3939 let stretches = stretches_of(&slots);
3940 assert_eq!(stretches.len(), 1 + usize::try_from(packets / 2).unwrap());
3941 let mut visits = 0;
3942 let longest = longest_stretch(&stretches, |stretch| {
3943 visits += stretch.len();
3944 #[allow(clippy::cast_precision_loss, reason = "a count of packets")]
3945 let span = stretch.len() as f64;
3946 span
3947 });
3948 assert_eq!(longest.map(|(at, _)| at), Some(0));
3949 assert_eq!(visits, usize::try_from(packets).unwrap());
3950 let mut notes = Vec::new();
3952 assert!(place_on_clock(&mut slots, &mut notes));
3953 assert!(
3954 notes[0].contains("999.9 s of the ground station's clock"),
3955 "{notes:?}"
3956 );
3957 }
3958
3959 #[test]
3961 fn a_bracket_in_a_key_isnt_a_list_item() {
3962 let mut text = String::from("RUN START 2025-01-01 10:00:00\n");
3963 for k in 0..3 {
3964 text.push_str(&format!(
3965 "10:00:0{k}.0: Position Data {{'crc': 'Good', 'fcb_altitude[1]': 5.0, \
3966 'pyro_status': [1, 0]}}\n"
3967 ));
3968 }
3969 let log = read(&text).unwrap();
3970 note(
3971 &log,
3972 "1 field of the `Position Data` packets, `fcb_altitude[1]`, isn't one this reader \
3973 knows: ",
3974 );
3975 assert!(
3976 !log.notes
3977 .iter()
3978 .any(|n| n.starts_with("`Position Data.fcb_altitude")),
3979 "{:#?}",
3980 log.notes
3981 );
3982 assert_eq!(
3983 channel(&log, "Position Data.fcb_altitude[1]").quantity,
3984 Quantity::Unrecognized
3985 );
3986 assert_eq!(
3988 channel(&log, "Position Data.pyro_status[0]").quantity,
3989 Quantity::Number
3990 );
3991 }
3992
3993 #[test]
3996 fn one_stray_fix_doesnt_make_degrees() {
3997 let text = flight(Written {
3998 latitude: 0.728,
3999 longitude: -1.2362,
4000 ..Written::default()
4001 });
4002 let (text, _) = changed(
4003 &text,
4004 POSITION,
4005 "'fcb_latitude': 0.728,",
4006 "'fcb_latitude': 41.71,",
4007 );
4008 let log = read(&text).unwrap();
4009 let kept = note(
4010 &log,
4011 "`Position Data.fcb_latitude` and `Position Data.fcb_longitude` kept as written, their \
4012 range or their fractions not degrees': ",
4013 );
4014 assert!(kept.contains(", 1 beyond ±π/2 or ±π."), "{kept}");
4015 }
4016
4017 #[test]
4018 fn files_that_arent_this_layout_or_hold_nothing_are_refused() {
4019 assert_eq!(
4020 read("\n\n").unwrap_err(),
4021 not_this("the file is empty".to_owned())
4022 );
4023 let error = read("time,alt\n").unwrap_err();
4024 assert!(
4025 matches!(&error, LogError::NotThisFormat { message, .. }
4026 if message.ends_with("isn't the ground station's `RUN START` line")),
4027 "{error}"
4028 );
4029 let error = read("RUN START sometime\n").unwrap_err();
4030 assert!(
4031 matches!(&error, LogError::Syntax { line: 1, message, .. }
4032 if message.starts_with("the `RUN START` line, ")),
4033 "{error}"
4034 );
4035 assert_eq!(
4036 read("RUN START 2025-01-01 10:00:00\n10:00:01.0: Switching radio to band 1\n")
4037 .unwrap_err(),
4038 LogError::NoData { format: FORMAT }
4039 );
4040 assert!(recognizes("\n RUN START 2025-01-01 10:00:00\n"));
4041 assert!(!recognizes("timestampMs\tpressurePa\n"));
4042 }
4043
4044 #[test]
4046 fn channels_are_bounded() {
4047 let mut text = String::from("RUN START 2025-01-01 10:00:00\n");
4048 for (second, packet) in ["A", "B"].into_iter().enumerate() {
4049 let fields: Vec<String> = (0..600).map(|k| format!("'f{k}': 1")).collect();
4050 text.push_str(&format!(
4051 "10:00:0{second}.0: {packet} {{{}}}\n",
4052 fields.join(", ")
4053 ));
4054 }
4055 let log = read(&text).unwrap();
4056 assert_eq!(log.channels.len(), MAX_CHANNELS);
4057 note(&log, "176 values, the first on line 3, are left out");
4058 note(&log, "600 fields of the `A` packets");
4059 note(&log, "424 fields of the `B` packets");
4060 }
4061
4062 proptest::proptest! {
4063 #[test]
4065 fn any_literal_parses_or_is_refused(text in "[{}\\[\\](),:'\"\\\\a-zA-Z0-9.+\\- ]{0,64}") {
4066 let _ = parse_literal(&text);
4067 }
4068
4069 #[test]
4072 fn any_text_is_read_or_refused(
4073 lines in proptest::collection::vec(
4074 "(1[0-2]:[0-5][0-9]:[0-5][0-9]\\.[0-9]{1,3}: )?(Position Data|Alt Info & Cfg|Orientation|X)? ?\\{?('[a-z_]{1,14}': ?[-0-9.e,'\\[\\]TrueFalsNoan]{0,10}, ?){0,4}\\}?",
4075 0..20,
4076 )
4077 ) {
4078 let text = format!("RUN START 2025-01-01 10:00:00\n{}", lines.join("\n"));
4079 if let Ok(log) = read(&text) {
4080 proptest::prop_assert!(log.time_s.windows(2).all(|pair| pair[1] > pair[0]));
4081 proptest::prop_assert_eq!(log.time_s.len(), log.altitude_m.len());
4082 for channel in &log.channels {
4083 proptest::prop_assert_eq!(channel.time_s.len(), channel.values.len());
4084 }
4085 }
4086 }
4087 }
4088}