|
| 1 | +use crate::util::read_any_attribute_to_string; |
| 2 | +use chrono::{DateTime, NaiveDateTime, Utc}; |
| 3 | +use hdf5::types::VarLenUnicode; |
| 4 | +use ndarray::Array1; |
| 5 | +use plotinator_log_if::{ |
| 6 | + hdf5::SkytemHdf5, |
| 7 | + prelude::{GeoSpatialDataBuilder, Plotable}, |
| 8 | + rawplot::RawPlot, |
| 9 | +}; |
| 10 | +use serde::{Deserialize, Serialize}; |
| 11 | +use std::path::Path; |
| 12 | + |
| 13 | +const ALTITUDE_VALID_RANGE: (f64, f64) = (0.0, 500.0); |
| 14 | + |
| 15 | +#[derive(Debug, Clone, Serialize, Deserialize)] |
| 16 | +pub struct AltimeterMinMax { |
| 17 | + starting_timestamp_utc: DateTime<Utc>, |
| 18 | + dataset_description: String, |
| 19 | + raw_plots: Vec<RawPlot>, |
| 20 | + metadata: Vec<(String, String)>, |
| 21 | +} |
| 22 | + |
| 23 | +impl AltimeterMinMax { |
| 24 | + fn process_sensor( |
| 25 | + h5: &hdf5::File, |
| 26 | + sensor_id: u8, |
| 27 | + sensor_type: &str, |
| 28 | + raw_plots: &mut Vec<RawPlot>, |
| 29 | + ) -> anyhow::Result<()> { |
| 30 | + let times: Vec<u64> = h5.dataset(&format!("timestamp_{sensor_id}"))?.read_raw()?; |
| 31 | + |
| 32 | + for (suffix, dataset_prefix) in [("min", "height_min"), ("max", "height_max")] { |
| 33 | + let heights: Array1<f32> = h5 |
| 34 | + .dataset(&format!("{dataset_prefix}_{sensor_id}"))? |
| 35 | + .read_1d()?; |
| 36 | + let heights: Vec<f64> = heights.into_iter().map(|h| h.into()).collect(); |
| 37 | + let legend_name = format!("{sensor_type}-{suffix}-{sensor_id}"); |
| 38 | + |
| 39 | + if let Some(plot) = GeoSpatialDataBuilder::new(legend_name) |
| 40 | + .timestamp(×) |
| 41 | + .altitude_from_laser(heights) |
| 42 | + .altitude_valid_range(ALTITUDE_VALID_RANGE) |
| 43 | + .build_into_rawplot()? |
| 44 | + { |
| 45 | + raw_plots.push(plot); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + Ok(()) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +impl SkytemHdf5 for AltimeterMinMax { |
| 54 | + const DESCRIPTIVE_NAME: &str = "Generic Altimeter Min/Max"; |
| 55 | + |
| 56 | + fn from_path(path: impl AsRef<Path>) -> anyhow::Result<Self> { |
| 57 | + let h5 = hdf5::File::open(path)?; |
| 58 | + let sensor_count = h5.attr("sensor_count")?.read_scalar::<u8>()?; |
| 59 | + let sensor_type = h5 |
| 60 | + .attr("sensor_type")? |
| 61 | + .read_scalar::<VarLenUnicode>()? |
| 62 | + .to_string(); |
| 63 | + let starting_timestamp = h5 |
| 64 | + .attr("timestamp")? |
| 65 | + .read_scalar::<VarLenUnicode>()? |
| 66 | + .to_string(); |
| 67 | + let starting_timestamp_utc: DateTime<Utc> = |
| 68 | + NaiveDateTime::parse_from_str(&starting_timestamp, "%Y%m%d_%H%M%S")?.and_utc(); |
| 69 | + |
| 70 | + let metadata: Vec<(String, String)> = h5 |
| 71 | + .attr_names()? |
| 72 | + .into_iter() |
| 73 | + .filter_map(|attr_name| { |
| 74 | + let attr = h5.attr(&attr_name).ok()?; |
| 75 | + let attr_val = read_any_attribute_to_string(&attr).ok()?; |
| 76 | + Some((attr_name, attr_val)) |
| 77 | + }) |
| 78 | + .collect(); |
| 79 | + |
| 80 | + let mut raw_plots = vec![]; |
| 81 | + for sensor_id in 1..=sensor_count { |
| 82 | + Self::process_sensor(&h5, sensor_id, &sensor_type, &mut raw_plots)?; |
| 83 | + } |
| 84 | + |
| 85 | + Ok(Self { |
| 86 | + starting_timestamp_utc, |
| 87 | + dataset_description: "Generic Altimeter(s) min/max".to_owned(), |
| 88 | + raw_plots, |
| 89 | + metadata, |
| 90 | + }) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +impl Plotable for AltimeterMinMax { |
| 95 | + fn raw_plots(&self) -> &[RawPlot] { |
| 96 | + &self.raw_plots |
| 97 | + } |
| 98 | + |
| 99 | + fn first_timestamp(&self) -> DateTime<Utc> { |
| 100 | + self.starting_timestamp_utc |
| 101 | + } |
| 102 | + |
| 103 | + fn descriptive_name(&self) -> &str { |
| 104 | + Self::DESCRIPTIVE_NAME |
| 105 | + } |
| 106 | + |
| 107 | + fn labels(&self) -> Option<&[plotinator_log_if::prelude::PlotLabels]> { |
| 108 | + None |
| 109 | + } |
| 110 | + |
| 111 | + fn metadata(&self) -> Option<Vec<(String, String)>> { |
| 112 | + Some(self.metadata.clone()) |
| 113 | + } |
| 114 | +} |
0 commit comments