1use std::collections::HashSet;
143
144use crate::error::LogError;
145use crate::log::{Channel, Event, FlightLog, LogFormat, Quantity, Stated};
146use crate::table::{self, Notes, finite, numbered_lines, short};
147
148const FORMAT: &str = "AltimeterCloud CSV";
150
151const MAX_SUMMARY_LINES: usize = 64;
153
154const G0_M_S2: f64 = 9.806_65;
156
157const BMP581_RANGE_C: (f64, f64) = (-40.0, 85.0);
159
160const TEMPERATURE_JUMP_C: f64 = 5.0;
163
164const APOGEE_TIME_MARGIN_S: f64 = 0.5;
167
168const LANDING_HEIGHT_MARGIN_M: f64 = 0.01;
171
172const YAW_END_DEG: f64 = 327.67;
174
175#[derive(Debug, Clone, Copy, PartialEq, Eq)]
177enum Kind {
178 Time,
179 Altitude,
180 Velocity,
181 Axis,
183 Total,
185 Gyro,
186 BoardTemperature,
187 CentiTemperature,
188 Angle,
190 Yaw,
191 Pressure,
192 RawAltitude,
193 AirBrake,
194 Kept(&'static str),
196 Unknown,
197}
198
199const KNOWN: [(&str, Kind); 23] = [
201 ("time(ms)", Kind::Time),
202 ("altitude(m)", Kind::Altitude),
203 ("velocity(m/s)", Kind::Velocity),
204 ("velocity_pressure(m/s)", Kind::Velocity),
205 ("acceleration_x(mg)", Kind::Axis),
206 ("acceleration_y(mg)", Kind::Axis),
207 ("acceleration_z(mg)", Kind::Axis),
208 ("acceleration_total(mg)", Kind::Total),
209 ("gyro_x", Kind::Gyro),
210 ("gyro_y", Kind::Gyro),
211 ("gyro_z", Kind::Gyro),
212 ("board temp(c)", Kind::BoardTemperature),
213 ("bmp_temp(x100)", Kind::CentiTemperature),
214 ("pitch", Kind::Angle),
215 ("roll", Kind::Angle),
216 ("yaw", Kind::Yaw),
217 ("tilt", Kind::Angle),
218 ("pressure(hpa)", Kind::Pressure),
219 ("raw altitude(m)", Kind::RawAltitude),
220 ("air_brake_percent", Kind::AirBrake),
221 (
222 "mt1_temp",
223 Kind::Kept("by its name a temperature, but no source states its unit or what it measures"),
224 ),
225 (
226 "apogee_prediction",
227 Kind::Kept(
228 "the Mercury's predicted apogee (Mercury V1 manual), a prediction rather than a \
229 reading, in a unit no source states",
230 ),
231 ),
232 (
233 "target_alt",
234 Kind::Kept(
235 "by its name the altitude the air brakes aim for, a setting rather than a reading, in \
236 a unit no source states",
237 ),
238 ),
239];
240
241const MERCURY_ONLY: [&str; 7] = [
243 "velocity_pressure(m/s)",
244 "bmp_temp(x100)",
245 "mt1_temp",
246 "apogee_prediction",
247 "air_brake_percent",
248 "target_alt",
249 "air_density",
250];
251
252fn kind_of(name: &str) -> Kind {
254 if name.eq_ignore_ascii_case("air_density") {
255 return Kind::Kept(
256 "the Mercury logs the air's density (Mercury V1 manual), in a unit no source states, \
257 and the record has no quantity for a density",
258 );
259 }
260 KNOWN
261 .iter()
262 .find(|(known, _)| name.eq_ignore_ascii_case(known))
263 .map_or(Kind::Unknown, |(_, kind)| *kind)
264}
265
266struct Column {
268 kind: Kind,
269 written: String,
270}
271
272fn is_header(line: &str) -> bool {
275 let mut cells = line.split(',').map(str::trim);
276 if !cells
277 .next()
278 .is_some_and(|first| first.eq_ignore_ascii_case("time(ms)"))
279 {
280 return false;
281 }
282 let (mut total, mut tilt) = (false, false);
283 for cell in cells {
284 total |= cell.eq_ignore_ascii_case("acceleration_total(mg)");
285 tilt |= cell.eq_ignore_ascii_case("tilt");
286 if total && tilt {
287 return true;
288 }
289 }
290 false
291}
292
293type Lines<'a> = Vec<(usize, &'a str)>;
295
296fn find_header(text: &str) -> Option<(usize, &str, Lines<'_>)> {
299 let mut above = Vec::new();
300 for (number, line) in numbered_lines(text).take(MAX_SUMMARY_LINES + 1) {
301 if is_header(line) {
302 return Some((number, line, above));
303 }
304 above.push((number, line));
305 }
306 None
307}
308
309pub(crate) fn recognizes(text: &str) -> bool {
312 find_header(text).is_some()
313}
314
315struct Entry<'a> {
317 line: usize,
318 key: &'a str,
319 value: &'a str,
320}
321
322fn summary_entries<'a>(lines: &'a [(usize, &'a str)]) -> impl Iterator<Item = Entry<'a>> + 'a {
326 lines.iter().flat_map(|&(line, text)| {
327 let mut cells = text.split(',').map(str::trim);
328 std::iter::from_fn(move || {
329 loop {
330 let key = cells.next()?;
331 if !key.is_empty() {
332 return Some(Entry {
333 line,
334 key,
335 value: cells.next().unwrap_or_default(),
336 });
337 }
338 }
339 })
340 })
341}
342
343struct Rows {
346 lines: Vec<usize>,
347 time_ms: Vec<f64>,
348 values: Vec<Vec<f64>>,
350}
351
352pub fn read(text: &str) -> Result<FlightLog, LogError> {
362 let Some((header_line, header, above)) = find_header(text) else {
363 return Err(LogError::NotThisFormat {
364 format: FORMAT,
365 message: format!(
366 "none of its first {} lines is an AltimeterCloud header: `Time(ms)` first, and \
367 `acceleration_total(mG)` and `tilt` among the names",
368 MAX_SUMMARY_LINES + 1
369 ),
370 });
371 };
372 let columns = columns(header, header_line)?;
373 let rows = read_rows(
374 numbered_lines(text).skip_while(|(number, _)| *number <= header_line),
375 &columns,
376 )?;
377 if rows.time_ms.is_empty() {
378 return Err(LogError::NoData { format: FORMAT });
379 }
380 build(&columns, rows, &above)
381}
382
383fn columns(header: &str, line: usize) -> Result<Vec<Column>, LogError> {
387 let mut columns: Vec<Column> = Vec::new();
388 let mut seen: HashSet<String> = HashSet::new();
391 let mut cells = header.split(',').map(str::trim).enumerate().peekable();
392 while let Some((index, cell)) = cells.next() {
393 if cell.is_empty() {
394 if index > 0 && cells.peek().is_none() {
395 break;
396 }
397 return Err(syntax(
398 line,
399 format!("the header's column {} has no name", index + 1),
400 ));
401 }
402 if !seen.insert(cell.to_ascii_lowercase()) {
403 return Err(syntax(
404 line,
405 format!("the header names {:?} twice", short(cell)),
406 ));
407 }
408 columns.push(Column {
409 kind: kind_of(cell),
410 written: cell.to_owned(),
411 });
412 }
413 if !columns.iter().any(|column| column.kind == Kind::Altitude) {
414 return Err(syntax(
415 line,
416 "the header names no altitude, `Altitude(m)`".to_owned(),
417 ));
418 }
419 Ok(columns)
420}
421
422fn read_rows<'a>(
424 lines: impl Iterator<Item = (usize, &'a str)>,
425 columns: &[Column],
426) -> Result<Rows, LogError> {
427 let width = columns.len();
428 let mut rows = Rows {
429 lines: Vec::new(),
430 time_ms: Vec::new(),
431 values: columns.iter().map(|_| Vec::new()).collect(),
432 };
433 for (number, line) in lines {
434 if line.trim().is_empty() {
435 continue;
436 }
437 let cells: Vec<&str> = line.split(',').map(str::trim).take(width + 2).collect();
439 let closing = cells.len() == width + 1 && cells.last().is_some_and(|c| c.is_empty());
440 if cells.len() != width && !closing {
441 return Err(syntax(
442 number,
443 format!(
444 "the row has {} cells: the header names {width}, and a row may add one empty \
445 cell after them, its closing comma",
446 if cells.len() > width + 1 {
447 format!("more than {}", width + 1)
448 } else {
449 cells.len().to_string()
450 }
451 ),
452 ));
453 }
454 let time_ms = match cells.first() {
455 Some(cell) if !cell.is_empty() => number_in(cell, number, &columns[0].written)?,
456 _ => return Err(syntax(number, "the row has no time".to_owned())),
457 };
458 if let Some(&last) = rows.time_ms.last()
459 && time_ms <= last
460 {
461 return Err(syntax(
462 number,
463 format!("the time {time_ms} ms doesn't come after the one above it, {last} ms"),
464 ));
465 }
466 for (index, (column, cell)) in columns.iter().zip(&cells).enumerate().skip(1) {
467 let value = if cell.is_empty() {
468 f64::NAN
469 } else {
470 number_in(cell, number, &column.written)?
471 };
472 if let Some(store) = rows.values.get_mut(index) {
473 store.push(value);
474 }
475 }
476 rows.lines.push(number);
477 rows.time_ms.push(time_ms);
478 }
479 Ok(rows)
480}
481
482fn number_in(cell: &str, line: usize, column: &str) -> Result<f64, LogError> {
484 table::number(cell, false).ok_or_else(|| {
485 syntax(
486 line,
487 format!("{:?} under {:?} isn't a number", short(cell), short(column)),
488 )
489 })
490}
491
492fn syntax(line: usize, message: String) -> LogError {
493 LogError::Syntax {
494 format: FORMAT,
495 line,
496 message,
497 }
498}
499
500fn names_of(columns: &[Column], kinds: &[Kind]) -> Option<String> {
502 let names: Vec<String> = columns
503 .iter()
504 .filter(|column| kinds.contains(&column.kind))
505 .map(|column| format!("`{}`", short(&column.written)))
506 .collect();
507 match names.as_slice() {
508 [] => None,
509 [one] => Some(one.clone()),
510 [rest @ .., last] => Some(format!("{} and {last}", rest.join(", "))),
511 }
512}
513
514fn build(
521 columns: &[Column],
522 rows: Rows,
523 summary_lines: &[(usize, &str)],
524) -> Result<FlightLog, LogError> {
525 let mercury = columns.iter().any(|column| {
526 MERCURY_ONLY
527 .iter()
528 .any(|name| column.written.eq_ignore_ascii_case(name))
529 });
530 let time_s: Vec<f64> = rows.time_ms.iter().map(|ms| ms / 1000.0).collect();
531 let mut log = FlightLog {
532 format: LogFormat::AltimeterCloudCsv,
533 logger: if mercury {
534 "Rocketry Ltd Mercury"
535 } else {
536 "Rocketry Ltd Nano V1"
537 }
538 .to_owned(),
539 serial_number: None,
540 firmware: None,
541 flight_number: None,
542 stated: Stated::default(),
543 time_s: Vec::new(),
544 altitude_m: Vec::new(),
545 vertical_speed_m_s: None,
546 temperature_k: None,
547 battery_v: None,
548 channels: Vec::new(),
549 events: Vec::new(),
550 notes: Vec::new(),
551 };
552 log.notes.push(if mercury {
553 "the logger is taken as a Rocketry Ltd Mercury. Its header has columns only the \
554 Mercury's layout has, such as the predicted apogee and the air-brake output its manual \
555 lists"
556 .to_owned()
557 } else {
558 "the logger is taken as a Rocketry Ltd Nano V1: its header is the layout the Nano V1 \
559 manual's page *CSV Flight log format* describes"
560 .to_owned()
561 });
562 let mut line_notes = Notes::default();
563 let manual = if mercury {
564 "the Nano V1 manual, whose layout has the same columns"
565 } else {
566 "the Nano V1 manual"
567 };
568 if let Some(names) = names_of(columns, &[Kind::Gyro]) {
569 log.notes.push(format!(
570 "{names} read in degrees per second, from {manual}: its page *CSV Flight log format* \
571 describes them. Each is the rate of turn about one of the board's axes in °/s, as \
572 whole numbers. The altimeter's gyroscope reads up to ±2,000 °/s"
573 ));
574 }
575 if let Some(names) = names_of(columns, &[Kind::Angle, Kind::Yaw]) {
576 log.notes.push(format!(
577 "{names} read in degrees, from {manual}: its page *CSV Flight log format* says what \
578 each is. The pitch, roll and yaw are attitude angles from their zero on the pad. The \
579 tilt is the angle from vertical. Each is in degrees"
580 ));
581 }
582 if let Some(names) = names_of(columns, &[Kind::Total]) {
583 log.notes.push(format!(
584 "{names} read in thousandths of standard gravity, from its name and {manual}: its page \
585 *CSV Flight log format* describes it. It is the acceleration's magnitude with the \
586 1 g of gravity taken out, so it sits near zero at rest. It is kept so, the magnitude less \
587 1 g, not an acceleration along any axis. It reads −1 g in free fall"
588 ));
589 }
590
591 let pad_rows = accelerometer_pad_rows(columns, &rows);
592 let mut temperature: Option<Vec<f64>> = None;
593 for (index, column) in columns.iter().enumerate() {
594 let Some(values) = rows.values.get(index) else {
595 continue;
596 };
597 let (quantity, scale): (Quantity, fn(f64) -> f64) = match column.kind {
598 Kind::Time => continue,
599 Kind::Altitude | Kind::RawAltitude => (Quantity::AltitudeAboveGround, |v| v),
600 Kind::Velocity => (Quantity::Speed, |v| v),
601 Kind::Axis | Kind::Total => (Quantity::Acceleration, |v| v * G0_M_S2 / 1000.0),
602 Kind::Gyro => (Quantity::AngularRate, |v| v),
603 Kind::Angle | Kind::Yaw => (Quantity::Angle, |v| v),
604 Kind::Pressure => (Quantity::Pressure, |v| v * 100.0),
605 Kind::BoardTemperature => (Quantity::Temperature, |v| v + 273.15),
606 Kind::CentiTemperature => {
607 if centi_degrees(values, &column.written, &mut log.notes) {
608 (Quantity::Temperature, |v| v / 100.0 + 273.15)
609 } else {
610 (Quantity::Unrecognized, |v| v)
611 }
612 }
613 Kind::AirBrake => {
614 log.notes.push(format!(
615 "`{}` read in percent, from its name: the air-brake output the Mercury logs \
616 (Mercury V1 manual), kept as the percentage written",
617 short(&column.written)
618 ));
619 (Quantity::Number, |v| v)
620 }
621 Kind::Kept(why) => {
622 log.notes.push(format!(
623 "`{}` kept as written: {why}",
624 short(&column.written)
625 ));
626 (Quantity::Unrecognized, |v| v)
627 }
628 Kind::Unknown => {
629 line_notes.push(format!(
630 "`{}` kept as written, in a unit no source states: this reader doesn't know \
631 the column",
632 short(&column.written)
633 ));
634 (Quantity::Unrecognized, |v| v)
635 }
636 };
637 let mut converted: Vec<f64> = Vec::with_capacity(values.len());
640 for (value, &line) in values.iter().zip(&rows.lines) {
641 converted.push(finite(scale(*value), FORMAT, line, &column.written)?);
642 }
643 if matches!(column.kind, Kind::Axis | Kind::Total) {
644 for value in converted.iter_mut().take(pad_rows) {
645 *value = f64::NAN;
646 }
647 }
648 if quantity == Quantity::Temperature {
649 closing_zeros(&mut converted, &rows.lines, &column.written, &mut log.notes);
650 temperature.get_or_insert_with(|| converted.clone());
651 }
652 if column.kind == Kind::Yaw {
653 yaw_end(values, &rows.lines, &column.written, &mut log.notes);
654 }
655 let (times, kept): (Vec<f64>, Vec<f64>) = values
657 .iter()
658 .zip(&converted)
659 .zip(&time_s)
660 .filter(|((written, _), _)| !written.is_nan())
661 .map(|((_, value), time)| (*time, *value))
662 .unzip();
663 log.channels.push(Channel {
664 column: column.written.clone(),
665 quantity,
666 time_s: times,
667 values: kept,
668 });
669 if column.kind == Kind::Altitude && log.altitude_m.is_empty() {
670 log.altitude_m = converted;
671 } else if column.kind == Kind::Velocity && log.vertical_speed_m_s.is_none() {
672 log.vertical_speed_m_s = Some(converted);
673 }
674 }
675 if pad_rows > 0
676 && let Some(names) = names_of(columns, &[Kind::Axis, Kind::Total])
677 {
678 log.notes.push(format!(
679 "the first {pad_rows} rows, before lift-off, to line {}, write 0 on all three of the \
680 accelerometer's axes, which reads 1 g at rest. It hasn't read yet, so {names} are \
681 gaps on them",
682 rows.lines.get(pad_rows - 1).copied().unwrap_or_default()
683 ));
684 }
685 log.temperature_k = temperature;
686 log.time_s = time_s;
687 summary(&mut log, summary_lines);
688 log.notes.extend(line_notes.into_vec());
689 Ok(log)
690}
691
692fn accelerometer_pad_rows(columns: &[Column], rows: &Rows) -> usize {
695 let axes: Vec<&Vec<f64>> = columns
696 .iter()
697 .zip(&rows.values)
698 .filter(|(column, _)| column.kind == Kind::Axis)
699 .map(|(_, values)| values)
700 .collect();
701 if axes.len() != 3 {
702 return 0;
703 }
704 rows.time_ms
705 .iter()
706 .enumerate()
707 .take_while(|(row, time)| {
708 **time < 0.0 && axes.iter().all(|axis| axis.get(*row) == Some(&0.0))
709 })
710 .count()
711}
712
713fn centi_degrees(values: &[f64], written: &str, notes: &mut Vec<String>) -> bool {
715 let readings: Vec<f64> = values.iter().copied().filter(|v| !v.is_nan()).collect();
716 let (low, high) = BMP581_RANGE_C;
717 let within = |celsius: f64| (low..=high).contains(&celsius);
718 let whole = readings.iter().all(|value| value.fract() == 0.0);
719 let scaled = readings.iter().all(|value| within(value / 100.0));
720 let beyond = readings.iter().filter(|value| !within(**value)).count();
721 let span = span(&readings);
722 if !readings.is_empty() && whole && scaled && beyond > 0 {
723 notes.push(format!(
724 "`{}` read in hundredths of a degree Celsius, from its name and the values: every \
725 reading, {span} as written, is a whole number. Divided by 100, every reading lies \
726 within the {low} to {high} °C the BMP581 measures. As written, {beyond} lie beyond \
727 it",
728 short(written)
729 ));
730 true
731 } else {
732 let why = if readings.is_empty() {
733 "it holds no reading".to_owned()
734 } else if !whole {
735 format!("its readings, {span}, aren't all whole numbers")
736 } else if !scaled {
737 format!(
738 "its readings, {span}, don't all lie within the {low} to {high} °C the BMP581 \
739 measures when divided by 100"
740 )
741 } else {
742 format!(
743 "its readings, {span}, all lie within the {low} to {high} °C the BMP581 measures \
744 as written too, so they could be degrees"
745 )
746 };
747 notes.push(format!(
748 "`{}` kept as written, its unit unknown: {why}",
749 short(written)
750 ));
751 false
752 }
753}
754
755fn closing_zeros(kelvin: &mut [f64], lines: &[usize], written: &str, notes: &mut Vec<String>) {
759 const ZERO_C_K: f64 = 273.15;
760 let readings: Vec<usize> = (0..kelvin.len())
761 .filter(|&row| !kelvin[row].is_nan())
762 .collect();
763 let zeros = readings
764 .iter()
765 .rev()
766 .take_while(|&&row| kelvin[row] == ZERO_C_K)
767 .count();
768 let Some(&before) = readings
769 .len()
770 .checked_sub(zeros + 1)
771 .and_then(|at| readings.get(at))
772 else {
773 return;
774 };
775 let before_c = kelvin[before] - ZERO_C_K;
776 if zeros == 0 || before_c.abs() <= TEMPERATURE_JUMP_C {
777 return;
778 }
779 let first = readings[readings.len() - zeros];
780 for &row in &readings[readings.len() - zeros..] {
781 kelvin[row] = f64::NAN;
782 }
783 notes.push(format!(
784 "the last {zeros} readings of `{}`, from line {}, are exactly 0 °C after a reading of \
785 {before_c:.2} °C on line {}. A board can't cool so far between two readings and hold \
786 there, so they aren't readings and are gaps",
787 short(written),
788 lines.get(first).copied().unwrap_or_default(),
789 lines.get(before).copied().unwrap_or_default()
790 ));
791}
792
793fn yaw_end(values: &[f64], lines: &[usize], written: &str, notes: &mut Vec<String>) {
795 let at_end = |value: &f64| value.abs() >= YAW_END_DEG;
796 if let Some(first) = values.iter().position(at_end) {
797 notes.push(format!(
798 "`{}` reaches ±{YAW_END_DEG}° or beyond on {} rows, the first on line {}. In no \
799 sample log does the yaw pass ±327.68°, ±32,768 hundredths of a degree, the range of \
800 a signed 16-bit number. So a yaw beyond it isn't written as it was, but clipped or \
801 wrapped to the other end. It is kept as written",
802 short(written),
803 values.iter().filter(|value| at_end(value)).count(),
804 lines.get(first).copied().unwrap_or_default()
805 ));
806 }
807}
808
809fn span(values: &[f64]) -> String {
811 let (low, high) = values
812 .iter()
813 .fold((f64::INFINITY, f64::NEG_INFINITY), |(low, high), &value| {
814 (low.min(value), high.max(value))
815 });
816 if low.is_finite() {
817 format!("from {low} to {high}")
818 } else {
819 "none".to_owned()
820 }
821}
822
823fn summary(log: &mut FlightLog, lines: &[(usize, &str)]) {
826 const USED: [&str; 8] = [
827 "Apogee meters",
828 "Apogee time",
829 "Landing time",
830 "Burnout time (ms)",
831 "Ejection time (ms)",
832 "Serial number",
833 "Firmware",
834 "Adjusted log",
835 ];
836 let entry =
837 |key: &str| summary_entries(lines).find(|entry| entry.key.eq_ignore_ascii_case(key));
838 let mut notes = Vec::new();
839 let number = |key: &str, notes: &mut Vec<String>| {
840 let entry = entry(key)?;
841 let value = table::number(entry.value, false);
842 if value.is_none() {
843 notes.push(format!(
844 "the summary's `{key}` on line {}, {:?}, isn't a number: left out",
845 entry.line,
846 short(entry.value)
847 ));
848 }
849 value
850 };
851 log.stated.apogee_m = number("Apogee meters", &mut notes);
852 let text = |key: &str| {
853 entry(key)
854 .map(|entry| short(entry.value))
855 .filter(|value| !value.is_empty())
856 };
857 log.serial_number = text("Serial number");
858 log.firmware = text("Firmware");
859 let mut events = Vec::new();
860 for (key, name) in [
861 ("Burnout time (ms)", "Burnout"),
862 ("Ejection time (ms)", "Ejection"),
863 ] {
864 if let Some(ms) = number(key, &mut notes) {
865 events.push(Event {
866 time_s: ms / 1000.0,
867 name: name.to_owned(),
868 });
869 }
870 }
871 let peak_s = log
873 .altitude_m
874 .iter()
875 .enumerate()
876 .filter(|(_, value)| !value.is_nan())
877 .fold(
878 None,
879 |best: Option<(usize, f64)>, (row, &value)| match best {
880 Some((_, top)) if top >= value => best,
881 _ => Some((row, value)),
882 },
883 )
884 .and_then(|(row, _)| log.time_s.get(row).copied());
885 if let Some(apogee_s) = number("Apogee time", &mut notes) {
886 match peak_s {
887 Some(peak_s) if (apogee_s - peak_s).abs() <= APOGEE_TIME_MARGIN_S => {
888 notes.push(format!(
889 "`Apogee time` read in seconds, from the altitude: the highest altitude is at \
890 {peak_s} s, within {APOGEE_TIME_MARGIN_S} s of its {apogee_s}. Read in \
891 milliseconds, it would be at the lift-off"
892 ));
893 events.push(Event {
894 time_s: apogee_s,
895 name: "Apogee".to_owned(),
896 });
897 }
898 _ => notes.push(format!(
899 "the summary's `Apogee time`, {apogee_s}, is left out. The key states no unit, and \
900 read in seconds it isn't within {APOGEE_TIME_MARGIN_S} s of the highest \
901 altitude's time, {}",
902 peak_s.map_or("which the file doesn't give".to_owned(), |t| format!(
903 "{t} s"
904 ))
905 )),
906 }
907 }
908 if let Some(landing) = number("Landing time", &mut notes) {
909 let height_m = entry("Landing meters").and_then(|entry| table::number(entry.value, false));
910 match landing_in_seconds(log, landing, height_m, peak_s) {
911 Ok(note) => {
912 notes.push(note);
913 events.push(Event {
914 time_s: landing,
915 name: "Landing".to_owned(),
916 });
917 }
918 Err(why) => notes.push(format!(
919 "the summary's `Landing time`, {landing}, is left out: the key states no unit, \
920 and {why}"
921 )),
922 }
923 }
924 events.sort_by(|a, b| a.time_s.total_cmp(&b.time_s));
925 log.events = events;
926 if let Some(adjusted) = entry("Adjusted log")
927 && !adjusted.value.eq_ignore_ascii_case("no")
928 {
929 notes.push(format!(
930 "the summary's `Adjusted log` is {:?}. AltimeterCloud can hand back a log \
931 recalculated after the flight with another pressure or temperature (Nano V1 \
932 manual). So this one's figures may not be the logger's own",
933 short(adjusted.value)
934 ));
935 }
936 let is_other = |entry: &Entry<'_>| !USED.iter().any(|key| entry.key.eq_ignore_ascii_case(key));
937 if let Some(first) = summary_entries(lines).find(is_other) {
938 notes.push(format!(
939 "the summary's other {} entries, such as `{}`, are left out: the record has no place \
940 for them",
941 summary_entries(lines).filter(is_other).count(),
942 short(first.key)
943 ));
944 }
945 log.notes.extend(notes);
946}
947
948fn altitude_at(time_s: &[f64], altitude_m: &[f64], at_s: f64) -> Option<f64> {
951 let after = time_s.partition_point(|&time| time < at_s);
952 let (t1, a1) = (*time_s.get(after)?, *altitude_m.get(after)?);
953 let altitude = if t1 == at_s {
954 a1
955 } else {
956 let before = after.checked_sub(1)?;
957 let (t0, a0) = (*time_s.get(before)?, *altitude_m.get(before)?);
958 a0 + (at_s - t0) / (t1 - t0) * (a1 - a0)
959 };
960 altitude.is_finite().then_some(altitude)
961}
962
963fn landing_in_seconds(
968 log: &FlightLog,
969 landing: f64,
970 height_m: Option<f64>,
971 peak_s: Option<f64>,
972) -> Result<String, String> {
973 let Some(height_m) = height_m else {
974 return Err(
975 "the summary gives no landing height, `Landing meters`, to check it \
976 against"
977 .to_owned(),
978 );
979 };
980 let Some(peak_s) = peak_s else {
981 return Err("the log gives no highest altitude to check it against".to_owned());
982 };
983 let at = |time_s: f64| altitude_at(&log.time_s, &log.altitude_m, time_s);
984 let agrees = |time_s: f64| {
985 time_s >= peak_s
986 && at(time_s).is_some_and(|a| (a - height_m).abs() <= LANDING_HEIGHT_MARGIN_M)
987 };
988 let denies = |time_s: f64, written: &str| {
990 if time_s < peak_s {
991 format!("{written} comes before the highest altitude, at {peak_s} s")
992 } else {
993 match at(time_s) {
994 Some(a) => format!(
995 "the altitude at {written} is {a:.3} m, not within \
996 {LANDING_HEIGHT_MARGIN_M} m of the stated landing height, {height_m} m"
997 ),
998 None => format!("the log gives no altitude at {written}"),
999 }
1000 }
1001 };
1002 let ms_s = landing / 1000.0;
1003 match (agrees(landing), agrees(ms_s)) {
1004 (true, false) => Ok(format!(
1005 "`Landing time` read in seconds, from the altitude: at {landing} s, after the highest \
1006 altitude at {peak_s} s, the altitude is {:.3} m. That is within \
1007 {LANDING_HEIGHT_MARGIN_M} m of the summary's `Landing meters`, {height_m} m. That key \
1008 is the height the rocket came to rest at (Nano V1 manual, *Flight info page*). Read \
1009 in milliseconds, {}",
1010 at(landing).unwrap_or(f64::NAN),
1011 denies(ms_s, &format!("{landing} ms"))
1012 )),
1013 (true, true) => Err(format!(
1014 "read in seconds or in milliseconds alike, the altitude there, after the highest, is \
1015 within {LANDING_HEIGHT_MARGIN_M} m of the stated landing height, {height_m} m"
1016 )),
1017 (false, _) => Err(format!(
1018 "read in seconds, {}",
1019 denies(landing, &format!("{landing} s"))
1020 )),
1021 }
1022}
1023
1024#[cfg(test)]
1025mod tests {
1026 use super::*;
1027
1028 const NANO_HEADER: &str = "Time(ms),Altitude(m),Velocity(m/s),acceleration_x(mG),\
1029 acceleration_y(mG),acceleration_z(mG),acceleration_total(mG),gyro_x,gyro_y,gyro_z,\
1030 Board temp(C),pitch,roll,yaw,tilt";
1031
1032 const MERCURY_HEADER: &str = "time(ms),altitude(m),velocity_pressure(m/s),acceleration_x(mG),\
1033 acceleration_y(mG),acceleration_z(mG),acceleration_total(mG),gyro_x,gyro_y,gyro_z,\
1034 bmp_temp(x100),pitch,roll,yaw,tilt,mt1_temp,apogee_prediction,air_brake_percent,\
1035 target_alt,air_density,";
1036
1037 fn nano_row(ms: i32, altitude: f64, axes: [i32; 3], temperature: &str, yaw: f64) -> String {
1039 let [x, y, z] = axes;
1040 let total = (f64::from(x * x + y * y + z * z)).sqrt() - 1000.0;
1041 format!(
1042 "{ms},{altitude:.3},0.00,{x},{y},{z},{total:.1},1,-2,3,{temperature},1.00,2.00,{yaw:.2},3.00,"
1043 )
1044 }
1045
1046 fn nano(rows: &[String]) -> String {
1047 format!("{NANO_HEADER}\n{}\n", rows.join("\n"))
1048 }
1049
1050 fn note<'a>(log: &'a FlightLog, start: &str) -> &'a str {
1051 let matching: Vec<&String> = log.notes.iter().filter(|n| n.starts_with(start)).collect();
1052 assert_eq!(matching.len(), 1, "{start}: {:#?}", log.notes);
1053 matching[0]
1054 }
1055
1056 fn has_note(log: &FlightLog, start: &str) -> bool {
1057 log.notes.iter().any(|n| n.starts_with(start))
1058 }
1059
1060 #[test]
1061 fn the_header_is_recognized_by_its_own_names() {
1062 assert!(recognizes(&format!(
1063 "{NANO_HEADER}\n0,0,0,0,0,0,0,0,0,0,,0,0,0,0,\n"
1064 )));
1065 assert!(recognizes(&format!("{MERCURY_HEADER}\n")));
1066 let summary = "Apogee meters,1.0,,\n".repeat(20);
1068 assert!(recognizes(&format!(
1069 "{summary}\n{}\n",
1070 NANO_HEADER.to_ascii_uppercase()
1071 )));
1072 assert!(!recognizes("Time(ms),Altitude(m),tilt\n0,0,0\n"));
1074 assert!(!recognizes("Time(ms),Altitude(m),acceleration_total(mG)\n"));
1075 assert!(!recognizes(
1076 "Altitude(m),Time(ms),acceleration_total(mG),tilt\n"
1077 ));
1078 assert!(!recognizes("Time (s),Alt (ft)\n0,0\n1,100\n"));
1079 let long = "x,1\n".repeat(MAX_SUMMARY_LINES + 1);
1080 assert!(!recognizes(&format!("{long}{NANO_HEADER}\n")));
1081 let just = "x,1\n".repeat(MAX_SUMMARY_LINES);
1082 assert!(recognizes(&format!("{just}{NANO_HEADER}\n")));
1083 }
1084
1085 #[test]
1086 fn summary_entries_pair_keys_with_values_across_empty_cells() {
1087 let lines = [
1088 (
1089 1,
1090 "Apogee meters,12.5,,Landing meters,-0.1,Burnout time (ms),900,",
1091 ),
1092 (2, "Device tag,Unit A,,Competitor tag,,"),
1093 (3, "Firmware, 1.00,,Lockouts,0,"),
1094 ];
1095 let pairs: Vec<(usize, &str, &str)> = summary_entries(&lines)
1096 .map(|e| (e.line, e.key, e.value))
1097 .collect();
1098 assert_eq!(
1099 pairs,
1100 [
1101 (1, "Apogee meters", "12.5"),
1102 (1, "Landing meters", "-0.1"),
1103 (1, "Burnout time (ms)", "900"),
1104 (2, "Device tag", "Unit A"),
1105 (2, "Competitor tag", ""),
1106 (3, "Firmware", "1.00"),
1107 (3, "Lockouts", "0"),
1108 ]
1109 );
1110 }
1111
1112 #[test]
1113 fn the_clock_is_milliseconds_and_every_column_is_a_channel() {
1114 let text = nano(&[
1115 nano_row(-40, 0.0, [10, 20, -990], "25.00", 0.0),
1116 nano_row(-20, 0.001, [10, 20, -990], "", 0.0),
1117 nano_row(0, 0.002, [500, 20, -3000], "", 0.0),
1118 nano_row(25, 0.5, [600, 20, -4000], "", 0.0),
1119 ]);
1120 let log = read(&text).unwrap();
1121 assert_eq!(log.format, LogFormat::AltimeterCloudCsv);
1122 assert_eq!(log.logger, "Rocketry Ltd Nano V1");
1123 assert_eq!(log.time_s, [-0.04, -0.02, 0.0, 0.025]);
1124 assert_eq!(log.altitude_m, [0.0, 0.001, 0.002, 0.5]);
1125 assert_eq!(log.channels.len(), 14);
1126 let x = &log.channels[2];
1128 assert_eq!(x.column, "acceleration_x(mG)");
1129 assert!((x.values[0] - 10.0 * G0_M_S2 / 1000.0).abs() < 1e-12);
1130 let temperature = log
1132 .channels
1133 .iter()
1134 .find(|c| c.quantity == Quantity::Temperature)
1135 .unwrap();
1136 assert_eq!(temperature.time_s, [-0.04]);
1137 assert!((temperature.values[0] - 298.15).abs() < 1e-9);
1138 let track = log.temperature_k.as_deref().unwrap();
1139 assert!((track[0] - 298.15).abs() < 1e-9);
1140 assert!(track[1..].iter().all(|t| t.is_nan()));
1141 note(
1142 &log,
1143 "`gyro_x`, `gyro_y` and `gyro_z` read in degrees per second, from the Nano V1 manual: ",
1144 );
1145 note(
1146 &log,
1147 "`pitch`, `roll`, `yaw` and `tilt` read in degrees, from the Nano V1 manual: ",
1148 );
1149 assert!(log.events.is_empty());
1150 }
1151
1152 #[test]
1155 fn a_value_its_conversion_makes_infinite_is_refused() {
1156 let text = nano(&[
1157 nano_row(0, 0.0, [10, 20, -990], "", 0.0),
1158 nano_row(25, 0.5, [600, 20, -4000], "", 0.0).replacen(",600,", ",1.7e308,", 1),
1159 ]);
1160 assert_eq!(
1161 read(&text).unwrap_err().to_string(),
1162 "AltimeterCloud CSV line 3: the column acceleration_x(mG) holds a value too large \
1163 for its unit's conversion to SI"
1164 );
1165 let text = format!(
1166 "{NANO_HEADER},pressure(hPa)\n{}1013.25\n{}-1.7e308\n{}1012.5\n",
1167 nano_row(0, 0.0, [10, 20, -990], "", 0.0),
1168 nano_row(25, 0.5, [600, 20, -4000], "", 0.0),
1169 nano_row(50, 1.0, [600, 20, -4000], "", 0.0),
1170 );
1171 assert_eq!(
1172 read(&text).unwrap_err().to_string(),
1173 "AltimeterCloud CSV line 3: the column pressure(hPa) holds a value too large for its \
1174 unit's conversion to SI"
1175 );
1176 }
1177
1178 #[test]
1179 fn rows_of_another_width_or_out_of_order_are_refused_by_line() {
1180 let good = nano_row(0, 0.0, [1, 2, 3], "", 0.0);
1181 let short_row = "10,0.0,0.00";
1182 let error = read(&nano(&[good.clone(), short_row.to_owned()])).unwrap_err();
1183 assert!(matches!(error, LogError::Syntax { line: 3, .. }), "{error}");
1184 let wide = format!("{},9", nano_row(10, 0.0, [1, 2, 3], "", 0.0));
1185 let error = read(&nano(&[good.clone(), wide])).unwrap_err();
1186 assert!(matches!(error, LogError::Syntax { line: 3, .. }), "{error}");
1187 let bare = nano_row(10, 0.0, [1, 2, 3], "", 0.0);
1189 assert!(
1190 read(&nano(&[
1191 good.clone(),
1192 bare.trim_end_matches(',').to_owned()
1193 ]))
1194 .is_ok()
1195 );
1196 let back = nano_row(0, 0.0, [1, 2, 3], "", 0.0);
1197 let error = read(&nano(&[good.clone(), back])).unwrap_err();
1198 assert!(error.to_string().contains("doesn't come after"), "{error}");
1199 let word = nano_row(10, 0.0, [1, 2, 3], "warm", 0.0);
1200 let error = read(&nano(&[good.clone(), word])).unwrap_err();
1201 assert!(error.to_string().contains("isn't a number"), "{error}");
1202 let no_time = format!(",{}", &nano_row(10, 0.0, [1, 2, 3], "", 0.0)[3..]);
1203 let error = read(&nano(&[good, no_time])).unwrap_err();
1204 assert!(error.to_string().contains("no time"), "{error}");
1205 assert!(matches!(
1206 read(&format!("{NANO_HEADER}\n\n")),
1207 Err(LogError::NoData { .. })
1208 ));
1209 }
1210
1211 #[test]
1212 fn a_header_naming_a_column_twice_or_none_is_refused() {
1213 let twice = format!("{NANO_HEADER},TILT\n");
1214 let error = read(&twice).unwrap_err();
1215 assert!(error.to_string().contains("twice"), "{error}");
1216 let empty = NANO_HEADER.replace(",gyro_y,", ",,");
1217 let error = read(&format!("{empty}\n")).unwrap_err();
1218 assert!(error.to_string().contains("has no name"), "{error}");
1219 let no_altitude = NANO_HEADER.replace("Altitude(m)", "Height");
1220 let error = read(&format!("{no_altitude}\n")).unwrap_err();
1221 assert!(error.to_string().contains("no altitude"), "{error}");
1222 }
1223
1224 #[test]
1227 fn the_accelerometers_zeros_on_the_pad_are_gaps_and_only_there() {
1228 let text = nano(&[
1229 nano_row(-60, 0.0, [0, 0, 0], "", 0.0),
1230 nano_row(-40, 0.0, [0, 0, 0], "", 0.0),
1231 nano_row(-20, 0.0, [0, 0, -1000], "", 0.0),
1232 nano_row(0, 0.0, [0, 0, 0], "", 0.0),
1233 ]);
1234 let log = read(&text).unwrap();
1235 let total = log
1236 .channels
1237 .iter()
1238 .find(|c| c.column == "acceleration_total(mG)")
1239 .unwrap();
1240 assert!(total.values[0].is_nan() && total.values[1].is_nan());
1241 assert!(total.values[2].abs() < 1e-12);
1242 assert!((total.values[3] + G0_M_S2).abs() < 1e-12);
1243 let gyro = log.channels.iter().find(|c| c.column == "gyro_x").unwrap();
1244 assert!(gyro.values.iter().all(|v| v.is_finite()));
1245 assert!(
1246 note(&log, "the first 2 rows, before lift-off, to line 3,")
1247 .contains("`acceleration_total(mG)` are gaps")
1248 );
1249
1250 let text = nano(&[
1252 nano_row(0, 0.0, [0, 0, 0], "", 0.0),
1253 nano_row(20, 0.0, [0, 0, 0], "", 0.0),
1254 ]);
1255 let log = read(&text).unwrap();
1256 assert!(
1257 log.channels
1258 .iter()
1259 .all(|c| c.values.iter().all(|v| !v.is_nan()))
1260 );
1261 assert!(!has_note(&log, "the first"));
1262 }
1263
1264 #[test]
1267 fn closing_zero_temperatures_are_gaps_only_after_a_jump() {
1268 let rows = |before: &str| {
1269 nano(&[
1270 nano_row(0, 0.0, [1, 2, 3], "30.00", 0.0),
1271 nano_row(20, 0.0, [1, 2, 3], before, 0.0),
1272 nano_row(40, 0.0, [1, 2, 3], "0.00", 0.0),
1273 nano_row(60, 0.0, [1, 2, 3], "", 0.0),
1274 nano_row(80, 0.0, [1, 2, 3], "0.00", 0.0),
1275 ])
1276 };
1277 let log = read(&rows("29.50")).unwrap();
1278 let track = log.temperature_k.as_deref().unwrap();
1279 assert!((track[1] - 302.65).abs() < 1e-9);
1280 assert!(track[2].is_nan() && track[4].is_nan());
1281 assert!(
1282 note(&log, "the last 2 readings of `Board temp(C)`, from line 4,")
1283 .contains("29.50 °C on line 3")
1284 );
1285
1286 let log = read(&rows("4.00")).unwrap();
1287 let track = log.temperature_k.as_deref().unwrap();
1288 assert!((track[2] - 273.15).abs() < 1e-9 && (track[4] - 273.15).abs() < 1e-9);
1289 assert!(!has_note(&log, "the last"));
1290 }
1291
1292 fn mercury(temperatures: [&str; 3]) -> String {
1293 let rows: Vec<String> = temperatures
1294 .iter()
1295 .enumerate()
1296 .map(|(row, temperature)| {
1297 format!(
1298 "{},0.000,0.00,1,2,3,-996.3,0,0,0,{temperature},0.00,0.00,0.00,0.00,,0.00,0,,12000,",
1299 row * 20
1300 )
1301 })
1302 .collect();
1303 format!("{MERCURY_HEADER}\n{}\n", rows.join("\n"))
1304 }
1305
1306 #[test]
1309 fn the_centi_degree_temperature_is_read_only_when_the_values_say_so() {
1310 let log = read(&mercury(["2000", "", "2150"])).unwrap();
1311 assert_eq!(log.logger, "Rocketry Ltd Mercury");
1312 let channel = log
1313 .channels
1314 .iter()
1315 .find(|c| c.column == "bmp_temp(x100)")
1316 .unwrap();
1317 assert_eq!(channel.quantity, Quantity::Temperature);
1318 assert_eq!(channel.time_s, [0.0, 0.04]);
1319 assert!((channel.values[1] - 294.65).abs() < 1e-9);
1320 note(
1321 &log,
1322 "`bmp_temp(x100)` read in hundredths of a degree Celsius, from its name and the values: ",
1323 );
1324 assert!(log.temperature_k.is_some());
1325
1326 for (cells, why) in [
1327 (["20", "21", "22"], "as written too"),
1328 (["2000.5", "2001", "2002"], "aren't all whole numbers"),
1329 (["9000", "9100", "9200"], "don't all lie within"),
1330 ] {
1331 let log = read(&mercury(cells)).unwrap();
1332 let channel = log
1333 .channels
1334 .iter()
1335 .find(|c| c.column == "bmp_temp(x100)")
1336 .unwrap();
1337 assert_eq!(channel.quantity, Quantity::Unrecognized, "{cells:?}");
1338 assert_eq!(channel.values[0], cells[0].parse::<f64>().unwrap());
1339 assert!(
1340 note(&log, "`bmp_temp(x100)` kept as written, its unit unknown: ").contains(why),
1341 "{cells:?}"
1342 );
1343 assert!(log.temperature_k.is_none());
1344 }
1345 }
1346
1347 #[test]
1348 fn the_mercurys_own_columns_are_kept_as_written() {
1349 let log = read(&mercury(["2000", "2000", "2000"])).unwrap();
1350 assert_eq!(log.channels.len(), 19);
1352 let quantity = |name: &str| {
1353 log.channels
1354 .iter()
1355 .find(|c| c.column == name)
1356 .map(|c| (c.quantity, c.values.len()))
1357 };
1358 assert_eq!(quantity("mt1_temp"), Some((Quantity::Unrecognized, 0)));
1359 assert_eq!(
1360 quantity("apogee_prediction"),
1361 Some((Quantity::Unrecognized, 3))
1362 );
1363 assert_eq!(quantity("air_brake_percent"), Some((Quantity::Number, 3)));
1364 assert_eq!(quantity("target_alt"), Some((Quantity::Unrecognized, 0)));
1365 assert_eq!(quantity("air_density"), Some((Quantity::Unrecognized, 3)));
1366 assert_eq!(
1367 quantity("velocity_pressure(m/s)"),
1368 Some((Quantity::Speed, 3))
1369 );
1370 for name in ["mt1_temp", "apogee_prediction", "target_alt", "air_density"] {
1371 note(&log, &format!("`{name}` kept as written: "));
1372 }
1373 note(&log, "`air_brake_percent` read in percent, from its name: ");
1374 assert!(
1375 note(
1376 &log,
1377 "`gyro_x`, `gyro_y` and `gyro_z` read in degrees per second, from "
1378 )
1379 .contains("whose layout has the same columns")
1380 );
1381 }
1382
1383 #[test]
1384 fn an_unknown_column_is_kept_and_its_notes_stay_few() {
1385 let names: Vec<String> = (0..30).map(|i| format!("extra_{i}")).collect();
1386 let header = format!("{NANO_HEADER},{}", names.join(","));
1387 let row = format!(
1388 "{}{}",
1389 nano_row(0, 0.0, [1, 2, 3], "", 0.0),
1390 vec!["7"; 30].join(",")
1391 );
1392 let log = read(&format!("{header}\n{row}\n")).unwrap();
1393 assert_eq!(log.channels.len(), 44);
1394 let extra = log.channels.iter().find(|c| c.column == "extra_0").unwrap();
1395 assert_eq!(extra.quantity, Quantity::Unrecognized);
1396 assert_eq!(extra.values, [7.0]);
1397 let kept = log
1398 .notes
1399 .iter()
1400 .filter(|n| n.contains("this reader doesn't know the column"))
1401 .count();
1402 assert_eq!(kept, 20);
1403 assert!(has_note(&log, "and 10 more notes"));
1404 }
1405
1406 #[test]
1407 fn a_yaw_at_the_end_of_its_range_is_noted() {
1408 let text = nano(&[
1409 nano_row(0, 0.0, [1, 2, 3], "", 300.0),
1410 nano_row(20, 0.0, [1, 2, 3], "", -327.68),
1411 ]);
1412 let log = read(&text).unwrap();
1413 note(
1414 &log,
1415 "`yaw` reaches ±327.67° or beyond on 1 rows, the first on line 3",
1416 );
1417 let text = nano(&[nano_row(0, 0.0, [1, 2, 3], "", 327.66)]);
1418 assert!(!has_note(&read(&text).unwrap(), "`yaw` reaches"));
1419 }
1420
1421 fn with_summary(summary: &str) -> String {
1422 format!(
1423 "{summary}\n\n{}",
1424 nano(&[
1425 nano_row(-20, 0.0, [1, 2, 3], "", 0.0),
1426 nano_row(0, 0.0, [1, 2, 3], "", 0.0),
1427 nano_row(5_000, 100.0, [1, 2, 3], "", 0.0),
1428 nano_row(5_100, 101.0, [1, 2, 3], "", 0.0),
1429 nano_row(5_200, 100.5, [1, 2, 3], "", 0.0),
1430 nano_row(30_000, 0.0, [1, 2, 3], "", 0.0),
1431 ])
1432 )
1433 }
1434
1435 #[test]
1438 fn the_summary_gives_the_apogee_the_serial_and_the_events() {
1439 let summary = "Apogee meters,101.00,,Landing meters,0.0,Burnout time (ms),1200,\n\
1440 Apogee time,5.150,,Landing time,30.000,\n\
1441 Serial number,0000-0000-0001,,Ejection time (ms),5300,\n\
1442 Firmware, 1.00,,Adjusted log,no,\n\
1443 Device tag,Unit A,,Competitor tag,,";
1444 let log = read(&with_summary(summary)).unwrap();
1445 assert_eq!(log.stated.apogee_m, Some(101.0));
1446 assert_eq!(log.serial_number.as_deref(), Some("0000-0000-0001"));
1447 assert_eq!(log.firmware.as_deref(), Some("1.00"));
1448 let events: Vec<(f64, &str)> = log
1449 .events
1450 .iter()
1451 .map(|e| (e.time_s, e.name.as_str()))
1452 .collect();
1453 assert_eq!(
1454 events,
1455 [
1456 (1.2, "Burnout"),
1457 (5.15, "Apogee"),
1458 (5.3, "Ejection"),
1459 (30.0, "Landing")
1460 ]
1461 );
1462 assert!(
1463 note(&log, "`Apogee time` read in seconds, from the altitude: ").contains("at 5.1 s")
1464 );
1465 assert!(
1466 note(
1467 &log,
1468 "`Landing time` read in seconds, from the altitude: at 30 s, "
1469 )
1470 .contains(
1471 "the altitude is 0.000 m. That is within 0.01 m of the summary's `Landing meters`, 0 m"
1472 )
1473 );
1474 assert!(
1475 note(
1476 &log,
1477 "the summary's other 3 entries, such as `Landing meters`"
1478 )
1479 .contains("left out")
1480 );
1481 assert!(!has_note(&log, "the summary's `Adjusted log`"));
1482
1483 let summary = "Apogee time,5150,,Landing time,30000,\nAdjusted log,yes,";
1486 let log = read(&with_summary(summary)).unwrap();
1487 assert!(log.events.is_empty());
1488 assert!(note(&log, "the summary's `Apogee time`, 5150, is left out").contains("5.1 s"));
1489 assert!(
1490 note(&log, "the summary's `Landing time`, 30000, is left out: ")
1491 .contains("gives no landing height")
1492 );
1493 note(&log, "the summary's `Adjusted log` is \"yes\"");
1494 assert_eq!(log.stated.apogee_m, None);
1495 let log = read(&with_summary("Apogee meters,high,")).unwrap();
1496 note(
1497 &log,
1498 "the summary's `Apogee meters` on line 1, \"high\", isn't a number",
1499 );
1500 }
1501
1502 #[test]
1505 fn the_landing_time_is_read_in_seconds_only_where_the_altitude_agrees() {
1506 let landing = |height: &str, time: &str| {
1507 let summary = format!("Landing meters,{height},,Landing time,{time},");
1508 read(&with_summary(&summary)).unwrap()
1509 };
1510 let events = |log: &FlightLog| -> Vec<(f64, String)> {
1511 log.events
1512 .iter()
1513 .map(|e| (e.time_s, e.name.clone()))
1514 .collect()
1515 };
1516 let log = landing("4.05", "29.000");
1518 assert_eq!(events(&log), [(29.0, "Landing".to_owned())]);
1519 assert!(
1520 note(
1521 &log,
1522 "`Landing time` read in seconds, from the altitude: at 29 s, "
1523 )
1524 .contains("the altitude is 4.052 m")
1525 );
1526 for (height, time, written, why) in [
1529 (
1530 "4.07",
1531 "29.000",
1532 "29",
1533 "the altitude at 29 s is 4.052 m, not within 0.01 m",
1534 ),
1535 ("0.0", "31.000", "31", "the log gives no altitude at 31 s"),
1536 (
1537 "50.0",
1538 "2.600",
1539 "2.6",
1540 "2.6 s comes before the highest altitude, at 5.1 s",
1541 ),
1542 ] {
1543 let log = landing(height, time);
1544 assert!(events(&log).is_empty(), "{time}");
1545 let start = format!("the summary's `Landing time`, {written}, is left out: ");
1546 assert!(note(&log, &start).contains(why), "{time}");
1547 assert!(!has_note(&log, "`Landing time` read in"), "{time}");
1548 }
1549 let text = format!(
1552 "Landing meters,0.0,,Landing time,20.000,\n\n{}",
1553 nano(&[
1554 nano_row(0, 5.0, [1, 2, 3], "", 0.0),
1555 nano_row(10, 0.0, [1, 2, 3], "", 0.0),
1556 nano_row(20_000, 0.0, [1, 2, 3], "", 0.0),
1557 ])
1558 );
1559 let log = read(&text).unwrap();
1560 assert!(log.events.is_empty());
1561 assert!(
1562 note(&log, "the summary's `Landing time`, 20, is left out: ")
1563 .contains("in seconds or in milliseconds alike")
1564 );
1565 }
1566
1567 #[test]
1568 fn the_altitude_at_a_time_is_a_rows_or_on_the_line_between_two() {
1569 let time = [0.0, 1.0, 3.0, 4.0];
1570 let altitude = [0.0, 10.0, 30.0, f64::NAN];
1571 assert_eq!(altitude_at(&time, &altitude, 1.0), Some(10.0));
1572 assert_eq!(altitude_at(&time, &altitude, 2.5), Some(25.0));
1573 assert_eq!(altitude_at(&time, &altitude, 0.0), Some(0.0));
1574 assert_eq!(altitude_at(&time, &altitude, -0.5), None);
1575 assert_eq!(altitude_at(&time, &altitude, 3.5), None);
1576 assert_eq!(altitude_at(&time, &altitude, 5.0), None);
1577 }
1578
1579 #[test]
1582 fn a_long_line_of_commas_is_no_header_and_no_summary() {
1583 let commas = ",".repeat(1 << 20);
1584 assert!(!recognizes(&commas));
1585 assert!(!recognizes(&format!("Time(ms){commas}")));
1586 let log = read(&format!(
1587 "{commas}\n\n{}",
1588 nano(&[nano_row(0, 0.0, [1, 2, 3], "", 0.0)])
1589 ))
1590 .unwrap();
1591 assert!(!has_note(&log, "the summary's other"));
1592 let error = read(&format!("{NANO_HEADER}{commas}x\n")).unwrap_err();
1594 assert!(error.to_string().contains("has no name"), "{error}");
1595 }
1596
1597 #[test]
1599 fn the_total_acceleration_is_noted_as_the_magnitude_less_one_g() {
1600 let log = read(&nano(&[nano_row(0, 0.0, [1, 2, 3], "", 0.0)])).unwrap();
1601 assert!(
1602 note(
1603 &log,
1604 "`acceleration_total(mG)` read in thousandths of standard gravity, from its name \
1605 and the Nano V1 manual: "
1606 )
1607 .contains("the magnitude less 1 g, not an acceleration along any axis")
1608 );
1609 }
1610
1611 #[test]
1614 fn unit_notes_open_as_the_survey_counts_them() {
1615 let log = read(&mercury(["2000", "2010", "2020"])).unwrap();
1616 for note in log.notes.iter().filter(|n| n.starts_with('`')) {
1617 let (decision, _) = note.split_once(": ").unwrap();
1618 assert!(
1619 decision.contains("` read in ") || decision.contains("` kept as written"),
1620 "{note}"
1621 );
1622 }
1623 }
1624}