1use crate::error::LogError;
97use crate::log::{Channel, FlightLog, LogFormat, Quantity, Stated};
98
99const FORMAT: &str = "R-DAS text";
101
102const LOGGER: &str = "AED Electronics R-DAS";
104
105const CONFIGURATION: &str = "*** Configuration ***";
107
108const RAW_DATA: &str = "*** Raw data ***";
110
111const CONFIGURATION_LINES: usize = 200;
115
116pub const ALTITUDE_STEP_M: f64 = 90.0 / (1.225 * 9.806_65);
121
122const MAX_COUNT: f64 = 1_023.0;
124
125const CLOCK_MARGIN: f64 = 0.01;
127
128const QUOTED_LINES: usize = 40;
131const QUOTED_CHARS: usize = 200;
132
133pub fn recognizes(text: &str) -> bool {
136 let mut lines = text
137 .lines()
138 .take(CONFIGURATION_LINES)
139 .map(str::trim)
140 .skip_while(|line| line.is_empty());
141 lines.next() == Some(CONFIGURATION) && lines.any(|line| line == RAW_DATA)
142}
143
144struct Configuration {
146 rate_hz: Option<(f64, usize)>,
148 inputs: Option<(Vec<String>, usize)>,
150 firmware: Option<String>,
152 lines: Vec<String>,
154}
155
156fn column_of(input: &str) -> Option<String> {
159 match input {
160 "Acceleration" => Some("Acc".to_owned()),
161 "Pressure" => Some("Prs".to_owned()),
162 "the digital inputs" => Some("Dig".to_owned()),
163 "ADC0" | "ADC1" | "ADC2" | "ADC3" | "ADC4" | "ADC5" => Some(input.to_owned()),
165 _ => None,
166 }
167}
168
169fn configuration<'a>(
171 lines: impl Iterator<Item = (usize, &'a str)>,
172) -> Result<Configuration, LogError> {
173 let mut configuration = Configuration {
174 rate_hz: None,
175 inputs: None,
176 firmware: None,
177 lines: Vec::new(),
178 };
179 for (number, line) in lines {
180 let line = line.trim();
181 if line.is_empty() || line == CONFIGURATION {
182 continue;
183 }
184 configuration.lines.push(line.to_owned());
185 if let Some(rest) = line.strip_prefix("Data is sampled at ") {
186 let rate = rest
187 .strip_suffix(" Hz.")
188 .and_then(|rate| rate.trim().parse::<f64>().ok())
189 .filter(|rate| rate.is_finite() && *rate > 0.0)
190 .ok_or_else(|| {
191 syntax(
192 number,
193 format!("a sample rate that isn't one: {:?}", cut(line)),
194 )
195 })?;
196 if configuration.rate_hz.replace((rate, number)).is_some() {
197 return Err(syntax(number, "a second sample rate".to_owned()));
198 }
199 } else if let Some(rest) = line.strip_prefix("Inputs that were sampled: ") {
200 let rest = rest.strip_suffix('.').unwrap_or(rest);
201 let columns = rest
202 .split(',')
203 .map(|input| {
204 column_of(input.trim()).ok_or_else(|| {
205 syntax(
206 number,
207 format!("an input the reader doesn't know: {:?}", cut(input.trim())),
208 )
209 })
210 })
211 .collect::<Result<Vec<_>, _>>()?;
212 if let Some(twice) = columns
213 .iter()
214 .enumerate()
215 .find_map(|(k, column)| columns[..k].contains(column).then_some(column))
216 {
217 return Err(syntax(number, format!("an input listed twice: `{twice}`")));
218 }
219 if configuration.inputs.replace((columns, number)).is_some() {
220 return Err(syntax(number, "a second list of inputs".to_owned()));
221 }
222 } else if let Some((_, version)) = line.split_once("with firmware version ") {
223 let version = version.trim().trim_end_matches('.').trim();
224 if !version.is_empty() {
225 configuration.firmware = Some(version.to_owned());
226 }
227 }
228 }
229 Ok(configuration)
230}
231
232pub fn read(text: &str) -> Result<FlightLog, LogError> {
248 if !recognizes(text) {
249 return Err(LogError::NotThisFormat {
250 format: FORMAT,
251 message: format!("the file doesn't open with `{CONFIGURATION}` and `{RAW_DATA}`"),
252 });
253 }
254 let mut lines = text
255 .lines()
256 .enumerate()
257 .map(|(index, line)| (index + 1, line));
258 let config_lines: Vec<(usize, &str)> = lines
259 .by_ref()
260 .take_while(|(_, line)| line.trim() != RAW_DATA)
261 .collect();
262 let configuration = configuration(config_lines.into_iter())?;
263
264 let Some((header_line, header)) = lines.by_ref().find(|(_, line)| !line.trim().is_empty())
266 else {
267 return Err(LogError::NoData { format: FORMAT });
268 };
269 let names: Vec<&str> = header.split_whitespace().collect();
270 if names.first() != Some(&"Time") {
271 return Err(syntax(
272 header_line,
273 format!(
274 "a table header that doesn't open with `Time`: {:?}",
275 cut(header.trim())
276 ),
277 ));
278 }
279 let names = &names[1..];
280 let (inputs, inputs_line) = configuration
281 .inputs
282 .as_ref()
283 .ok_or_else(|| LogError::Unit {
284 format: FORMAT,
285 line: header_line,
286 message: "the configuration lists no inputs, so nothing says what the table's columns \
287 hold"
288 .to_owned(),
289 })?;
290 if names.iter().copied().ne(inputs.iter().map(String::as_str)) {
291 return Err(syntax(
292 header_line,
293 format!(
294 "the table's columns, {}, aren't the inputs line {inputs_line} lists, {}",
295 cut(&names.join(" ")),
296 cut(&inputs.join(" "))
297 ),
298 ));
299 }
300 let digital = names.iter().position(|name| *name == "Dig");
301
302 let mut time_s = Vec::new();
303 let mut values: Vec<Vec<f64>> = vec![Vec::new(); names.len()];
304 let mut gaps = vec![0usize; names.len()];
305 let mut first_gap = None;
306 for (number, line) in lines {
307 if line.trim().is_empty() {
308 continue;
309 }
310 let cells: Vec<&str> = line.split_whitespace().collect();
311 if cells.len() != names.len() + 1 {
312 return Err(syntax(
313 number,
314 format!(
315 "a row of {} cells where the header names {}",
316 cells.len(),
317 names.len() + 1
318 ),
319 ));
320 }
321 let time = cells[0]
322 .parse::<f64>()
323 .ok()
324 .filter(|time| time.is_finite())
325 .ok_or_else(|| {
326 syntax(
327 number,
328 format!("a `Time` that isn't a number: {:?}", cut(cells[0])),
329 )
330 })?;
331 if time_s.last().is_some_and(|last| time <= *last) {
332 return Err(syntax(
333 number,
334 format!("a `Time`, {time}, not after the row before's"),
335 ));
336 }
337 time_s.push(time);
338 for (k, cell) in cells[1..].iter().enumerate() {
339 let value = if Some(k) == digital {
340 cell.bytes()
342 .all(|b| b.is_ascii_hexdigit())
343 .then(|| u32::from_str_radix(cell, 16).ok())
344 .flatten()
345 .map(f64::from)
346 } else {
347 let value = cell.parse::<f64>().ok().filter(|value| value.is_finite());
348 if let Some(value) = value
349 && !(value.fract() == 0.0 && (0.0..=MAX_COUNT).contains(&value))
350 {
351 return Err(syntax(
352 number,
353 format!(
354 "`{}` holds {:?}, not a count of the 10-bit converter, a whole \
355 number from 0 to 1,023: a file of converted values isn't read",
356 names[k],
357 cut(cell)
358 ),
359 ));
360 }
361 value
362 };
363 values[k].push(value.unwrap_or_else(|| {
364 gaps[k] += 1;
365 first_gap.get_or_insert(number);
366 f64::NAN
367 }));
368 }
369 }
370 if time_s.is_empty() {
371 return Err(LogError::NoData { format: FORMAT });
372 }
373
374 let mut notes = vec![clock(&configuration, &time_s, header_line)?];
375 let counts: Vec<String> = names
376 .iter()
377 .filter(|name| **name != "Dig")
378 .map(|name| format!("`{name}`"))
379 .collect();
380 if !counts.is_empty() {
381 notes.push(format!(
382 "{} kept as written: the converters' counts, which the file states no calibration \
383 for",
384 counts.join(", ")
385 ));
386 }
387 if digital.is_some() {
388 notes.push(
389 "`Dig` read in hexadecimal and kept as its number: the digital inputs and the \
390 flight's phase, which the file doesn't name bit by bit"
391 .to_owned(),
392 );
393 }
394 let total_gaps: usize = gaps.iter().sum();
395 if let Some(line) = first_gap {
396 notes.push(if total_gaps == 1 {
397 format!("1 cell that isn't a number read as a gap, on line {line}")
398 } else {
399 format!("{total_gaps} cells that aren't numbers read as gaps, the first on line {line}")
400 });
401 }
402 let shown = configuration.lines.len().min(QUOTED_LINES);
403 let mut quoted: Vec<String> = configuration.lines[..shown]
404 .iter()
405 .map(|line| cut(line))
406 .collect();
407 if configuration.lines.len() > shown {
408 quoted.push(format!(
409 "and {} more lines",
410 configuration.lines.len() - shown
411 ));
412 }
413 notes.push(format!(
414 "the configuration, as written: {}",
415 quoted.join(" | ")
416 ));
417 notes.push(
418 "the file holds counts and no altitude, so the record's primary track is empty: every \
419 column is a channel on the file's clock"
420 .to_owned(),
421 );
422
423 let channels = names
424 .iter()
425 .zip(values)
426 .map(|(name, values)| Channel {
427 column: (*name).to_owned(),
428 quantity: if *name == "Dig" {
429 Quantity::Number
430 } else {
431 Quantity::Unrecognized
432 },
433 time_s: time_s.clone(),
434 values,
435 })
436 .collect();
437
438 Ok(FlightLog {
439 format: LogFormat::RdasText,
440 logger: LOGGER.to_owned(),
441 serial_number: None,
442 firmware: configuration.firmware,
443 flight_number: None,
444 stated: Stated::default(),
445 time_s: Vec::new(),
446 altitude_m: Vec::new(),
447 vertical_speed_m_s: None,
448 temperature_k: None,
449 battery_v: None,
450 channels,
451 events: Vec::new(),
452 notes,
453 })
454}
455
456fn clock(
458 configuration: &Configuration,
459 time_s: &[f64],
460 header_line: usize,
461) -> Result<String, LogError> {
462 let unit = |message: String| LogError::Unit {
463 format: FORMAT,
464 line: header_line,
465 message,
466 };
467 let (rate_hz, rate_line) = configuration.rate_hz.ok_or_else(|| {
468 unit(
469 "the configuration states no sample rate, which the clock is checked against"
470 .to_owned(),
471 )
472 })?;
473 let (Some(first), Some(last)) = (time_s.first(), time_s.last()) else {
474 return Err(LogError::NoData { format: FORMAT });
475 };
476 if time_s.len() < 2 {
477 return Err(unit(
478 "a single row, whose clock can't be checked against the stated rate".to_owned(),
479 ));
480 }
481 let step_s = (last - first) / (time_s.len() - 1) as f64;
482 let ratio = step_s * rate_hz;
483 if (ratio - 1.0).abs() > CLOCK_MARGIN {
484 return Err(unit(format!(
485 "`Time` steps by {step_s} on average, {ratio:.3} times the {rate_hz} Hz line \
486 {rate_line} states, not within 1%: nothing says the clock counts seconds"
487 )));
488 }
489 let interval_s = 1.0 / rate_hz;
490 let uneven = time_s
491 .windows(2)
492 .filter(|pair| {
493 let step = pair[1] - pair[0];
494 step < 0.5 * interval_s || step > 1.5 * interval_s
495 })
496 .count();
497 let mut note = format!(
498 "`Time` read in seconds: it steps by {step_s:.6} on average, the {rate_hz} Hz line \
499 {rate_line} states within 1% ({ratio:.4} times its {interval_s:.6} s)"
500 );
501 if uneven > 0 {
502 note.push_str(&format!(
503 "; {uneven} steps are under half or over one and a half of it"
504 ));
505 }
506 Ok(note)
507}
508
509fn cut(text: &str) -> String {
511 text.chars().take(QUOTED_CHARS).collect()
512}
513
514fn syntax(line: usize, message: String) -> LogError {
516 LogError::Syntax {
517 format: FORMAT,
518 line,
519 message,
520 }
521}
522
523#[cfg(test)]
524mod tests {
525 use super::*;
526
527 fn file(config: &[&str], header: &str, rows: &[&str]) -> String {
529 let mut lines = vec![CONFIGURATION, ""];
530 lines.extend_from_slice(config);
531 lines.extend_from_slice(&["", RAW_DATA, "", header]);
532 lines.extend_from_slice(rows);
533 lines.join("\n") + "\n"
534 }
535
536 const RATE: &str = "Data is sampled at 100 Hz.";
537 const INPUTS: &str = "Inputs that were sampled: Acceleration, Pressure, the digital inputs.";
538 const HEADER: &str = " Time Acc Prs Dig";
539
540 fn rows(n: usize) -> Vec<String> {
542 (0..n)
543 .map(|k| format!("{:7.3} 510 800 0010", k as f64 * 0.01))
544 .collect()
545 }
546
547 fn good(n: usize) -> String {
548 let rows = rows(n);
549 let rows: Vec<&str> = rows.iter().map(String::as_str).collect();
550 file(&[RATE, INPUTS], HEADER, &rows)
551 }
552
553 fn refusal(text: &str, kind: &str) -> String {
555 match (read(text), kind) {
556 (Err(LogError::Unit { message, .. }), "unit") => message,
557 (Err(LogError::Syntax { message, line, .. }), "syntax") => {
558 format!("line {line}: {message}")
559 }
560 (Err(LogError::NoData { .. }), "no data") => String::new(),
561 (other, _) => panic!("expected a {kind} refusal, got {other:?}"),
562 }
563 }
564
565 #[test]
566 fn the_layout_is_recognized_by_its_two_markers() {
567 assert!(recognizes(&good(3)));
568 assert!(recognizes(&format!(
569 "\r\n\r\n{}",
570 good(3).replace('\n', "\r\n")
571 )));
572 assert!(!recognizes(
574 "*** Configuration ***\nData is sampled at 100 Hz.\n"
575 ));
576 let late = format!("{CONFIGURATION}\n{}{RAW_DATA}\n", "x\n".repeat(199));
578 assert!(!recognizes(&late));
579 let in_time = format!("{CONFIGURATION}\n{}{RAW_DATA}\n", "x\n".repeat(198));
580 assert!(recognizes(&in_time));
581 assert!(!recognizes(&format!("Time,Altitude\n{}", good(3))));
583 assert!(matches!(
584 read("Time,Altitude\n0,0\n"),
585 Err(LogError::NotThisFormat { .. })
586 ));
587 }
588
589 #[test]
590 fn a_good_file_reads_its_firmware_and_notes_the_clock() {
591 let text = file(
592 &[
593 RATE,
594 INPUTS,
595 "and downloaded from an RDAS board with firmware version 4.1",
596 ],
597 HEADER,
598 &[
599 " 0.000 510 800 0000",
600 " 0.010 511 801 001F",
601 " 0.020 512 802 0010",
602 ],
603 );
604 let log = read(&text).unwrap();
605 assert_eq!(log.firmware.as_deref(), Some("4.1"));
606 assert_eq!(log.channels[2].values, [0.0, 31.0, 16.0]);
607 assert!(
608 log.notes[0].starts_with("`Time` read in seconds: it steps by 0.010000 on average")
609 );
610 assert_eq!(log.format.altitude_resolution_m(), ALTITUDE_STEP_M);
611 }
612
613 #[test]
614 fn a_clock_off_the_stated_rate_by_more_than_one_percent_is_refused() {
615 let near = file(
617 &[RATE, INPUTS],
618 HEADER,
619 &["0 1 1 0", "0.01009 1 1 0", "0.02018 1 1 0"],
620 );
621 assert!(read(&near).is_ok());
622 let early = file(
623 &[RATE, INPUTS],
624 HEADER,
625 &["0 1 1 0", "0.00991 1 1 0", "0.01982 1 1 0"],
626 );
627 assert!(read(&early).is_ok());
628 for step in ["0.01011", "0.00989"] {
630 let second = format!("{step} 1 1 0");
631 let text = file(&[RATE, INPUTS], HEADER, &["0 1 1 0", &second]);
632 let message = refusal(&text, "unit");
633 assert!(message.contains("not within 1%"), "{message}");
634 }
635 let millis = file(
637 &[RATE, INPUTS],
638 HEADER,
639 &["0 1 1 0", "10 1 1 0", "20 1 1 0"],
640 );
641 assert!(refusal(&millis, "unit").contains("1000.000 times the 100 Hz line"));
642 }
643
644 #[test]
645 fn uneven_steps_are_counted() {
646 let mut rows = rows(200);
649 rows.remove(100);
650 let mut more = super::tests::rows(1);
651 more[0] = format!("{:7.3} 510 800 0010", 2.0);
652 rows.extend(more);
653 let rows: Vec<&str> = rows.iter().map(String::as_str).collect();
654 let log = read(&file(&[RATE, INPUTS], HEADER, &rows)).unwrap();
655 assert!(
656 log.notes[0].ends_with("; 1 steps are under half or over one and a half of it"),
657 "{}",
658 log.notes[0]
659 );
660 }
661
662 #[test]
663 fn a_configuration_without_its_rate_or_inputs_is_refused() {
664 let rows = ["0 1 1 0", "0.01 1 1 0"];
665 let message = refusal(&file(&[INPUTS], HEADER, &rows), "unit");
666 assert!(message.contains("states no sample rate"), "{message}");
667 let message = refusal(&file(&[RATE], HEADER, &rows), "unit");
668 assert!(message.contains("lists no inputs"), "{message}");
669 let message = refusal(&file(&[RATE, RATE, INPUTS], HEADER, &rows), "syntax");
670 assert!(message.contains("a second sample rate"), "{message}");
671 let message = refusal(&file(&[RATE, INPUTS, INPUTS], HEADER, &rows), "syntax");
672 assert!(message.contains("a second list of inputs"), "{message}");
673 for rate in [
674 "Data is sampled at 0 Hz.",
675 "Data is sampled at fast Hz.",
676 "Data is sampled at 100 Hz",
677 ] {
678 let message = refusal(&file(&[rate, INPUTS], HEADER, &rows), "syntax");
679 assert!(
680 message.contains("a sample rate that isn't one"),
681 "{message}"
682 );
683 }
684 let gyro = "Inputs that were sampled: Acceleration, Gyro.";
685 let message = refusal(&file(&[RATE, gyro], HEADER, &rows), "syntax");
686 assert!(
687 message.contains("an input the reader doesn't know: \"Gyro\""),
688 "{message}"
689 );
690 let analog = "Inputs that were sampled: ADC0, ADC5.";
692 let ok = file(&[RATE, analog], "Time ADC0 ADC5", &["0 1 1", "0.01 1 1"]);
693 assert_eq!(read(&ok).unwrap().channels.len(), 2);
694 for input in ["ADC6", "ADC00", "ADC"] {
695 let inputs = format!("Inputs that were sampled: ADC0, {input}.");
696 let header = format!("Time ADC0 {input}");
697 let text = file(&[RATE, &inputs], &header, &["0 1 1", "0.01 1 1"]);
698 let message = refusal(&text, "syntax");
699 let expected = format!("an input the reader doesn't know: \"{input}\"");
700 assert!(message.contains(&expected), "{message}");
701 }
702 let twice = "Inputs that were sampled: the digital inputs, the digital inputs.";
704 let text = file(
705 &[RATE, twice],
706 "Time Dig Dig",
707 &["0 0010 0010", "0.01 0010 0010"],
708 );
709 let message = refusal(&text, "syntax");
710 assert!(
711 message.contains("an input listed twice: `Dig`"),
712 "{message}"
713 );
714 }
715
716 #[test]
717 fn a_header_must_name_the_listed_inputs() {
718 let rows = ["0 1 1 0", "0.01 1 1 0"];
719 let message = refusal(
720 &file(&[RATE, INPUTS], " Time Prs Acc Dig", &rows),
721 "syntax",
722 );
723 assert!(
724 message.starts_with(
725 "line 8: the table's columns, Prs Acc Dig, aren't the inputs line 4 lists"
726 ),
727 "{message}"
728 );
729 let message = refusal(&file(&[RATE, INPUTS], " Time Acc Prs", &rows), "syntax");
730 assert!(message.contains("aren't the inputs"), "{message}");
731 let message = refusal(
732 &file(&[RATE, INPUTS], " T Acc Prs Dig", &rows),
733 "syntax",
734 );
735 assert!(message.contains("doesn't open with `Time`"), "{message}");
736 }
737
738 #[test]
739 fn rows_must_be_whole_and_in_time_order() {
740 let message = refusal(
741 &file(&[RATE, INPUTS], HEADER, &["0 1 1 0", "0.01 1 1"]),
742 "syntax",
743 );
744 assert!(
745 message.starts_with("line 10: a row of 3 cells where the header names 4"),
746 "{message}"
747 );
748 let message = refusal(
749 &file(&[RATE, INPUTS], HEADER, &["0.01 1 1 0", "0.01 1 1 0"]),
750 "syntax",
751 );
752 assert!(
753 message.starts_with("line 10: a `Time`, 0.01, not after"),
754 "{message}"
755 );
756 let message = refusal(
757 &file(&[RATE, INPUTS], HEADER, &["0 1 1 0", "inf 1 1 0"]),
758 "syntax",
759 );
760 assert!(
761 message.contains("a `Time` that isn't a number"),
762 "{message}"
763 );
764 let message = refusal(&file(&[RATE, INPUTS], HEADER, &["0 1 1 0"]), "unit");
765 assert!(message.contains("a single row"), "{message}");
766 refusal(&file(&[RATE, INPUTS], HEADER, &[]), "no data");
767 refusal(
768 &format!("{CONFIGURATION}\n{RATE}\n{INPUTS}\n{RAW_DATA}\n\n"),
769 "no data",
770 );
771 }
772
773 #[test]
774 fn cells_that_arent_numbers_are_gaps() {
775 let text = file(
776 &[RATE, INPUTS],
777 HEADER,
778 &["0 1 1 0", "0.01 x 1 G", "0.02 1 1e400 +10"],
779 );
780 let log = read(&text).unwrap();
781 assert!(log.channels[0].values[1].is_nan() && log.channels[2].values[1].is_nan());
782 assert!(log.channels[1].values[2].is_nan() && log.channels[2].values[2].is_nan());
784 assert!(log.notes.contains(
785 &"4 cells that aren't numbers read as gaps, the first on line 10".to_owned()
786 ));
787 }
788
789 #[test]
790 fn a_count_must_be_a_whole_number_the_converter_can_write() {
791 let ok = file(&[RATE, INPUTS], HEADER, &["0 0 1023 0", "0.01 1 1 0"]);
792 assert!(read(&ok).is_ok());
793 for cell in ["1024", "-1", "1.5", "101.3"] {
794 let row = format!("0.01 {cell} 1 0");
795 let message = refusal(&file(&[RATE, INPUTS], HEADER, &["0 1 1 0", &row]), "syntax");
796 assert!(
797 message.starts_with(&format!("line 10: `Acc` holds \"{cell}\", not a count")),
798 "{message}"
799 );
800 }
801 }
802
803 #[test]
804 fn quoted_text_is_bounded() {
805 let long = "y".repeat(10_000);
806 let many: Vec<String> = (0..150).map(|k| format!("Setting {k}.")).collect();
807 let mut config: Vec<&str> = vec![RATE, INPUTS, &long];
808 config.extend(many.iter().map(String::as_str));
809 let log = read(&file(&config, HEADER, &["0 1 1 0", "0.01 1 1 0"])).unwrap();
810 let quoted = log
811 .notes
812 .iter()
813 .find(|n| n.starts_with("the configuration"))
814 .unwrap();
815 assert!(
816 quoted.ends_with(" | and 113 more lines"),
817 "{}",
818 "ed[quoted.len() - 60..]
819 );
820 assert!(quoted.len() < 40 * (QUOTED_CHARS + 3) + 100);
821 let rate = format!("Data is sampled at {long}");
823 let message = refusal(&file(&[&rate, INPUTS], HEADER, &["0 1 1 0"]), "syntax");
824 assert!(message.len() < QUOTED_CHARS + 60, "{}", message.len());
825 }
826}