76 lines
2.3 KiB
Rust
76 lines
2.3 KiB
Rust
|
use crate::config::{Data, FLOW_COL, FLOW_SHEET, TIME_COL, WGHT_COL, WGHT_SHEET};
|
||
|
use crate::time::{cell_to_naivetime, deltatime};
|
||
|
|
||
|
use calamine::{open_workbook, Reader, Xlsx};
|
||
|
|
||
|
use chrono::NaiveTime;
|
||
|
|
||
|
pub fn process_sheet(
|
||
|
path: &str,
|
||
|
worksheet: usize,
|
||
|
time_col: usize,
|
||
|
data_col: usize,
|
||
|
) -> Vec<(f64, f64)> {
|
||
|
let mut workbook: Xlsx<_> =
|
||
|
open_workbook(path).unwrap_or_else(|_| panic!("Cannot open file at path \"{}\"", path));
|
||
|
|
||
|
if let Some(Ok(range)) = workbook.worksheet_range_at(worksheet) {
|
||
|
let starting_time: NaiveTime = cell_to_naivetime(range[(1, time_col)].get_string());
|
||
|
|
||
|
let time_range = range.range(
|
||
|
(1, time_col as u32),
|
||
|
(range.height() as u32 - 1, time_col as u32),
|
||
|
);
|
||
|
let weight_range = range.range(
|
||
|
(1, data_col as u32),
|
||
|
(range.height() as u32 - 1, data_col as u32),
|
||
|
);
|
||
|
|
||
|
// println!("time column cells: {:?}", time_range.cells().next());
|
||
|
// println!("time column strings: {:?}", time_range.cells().map(|c| c.2.get_string().unwrap()).collect::<Vec<&str>>());
|
||
|
|
||
|
let map_time_range = time_range.cells().map(|c| {
|
||
|
let timestamp = cell_to_naivetime(c.2.get_string());
|
||
|
let deltatime = deltatime(timestamp, starting_time);
|
||
|
let std_duration = deltatime.to_std().unwrap();
|
||
|
|
||
|
std_duration.as_secs_f32() as f64
|
||
|
});
|
||
|
|
||
|
let map_weight_range = weight_range.cells().map(|c| {
|
||
|
c.2.get_float().unwrap_or_else(|| {
|
||
|
panic!(
|
||
|
"Can't get float value of weight column at position ({},{})",
|
||
|
c.0, c.1
|
||
|
)
|
||
|
})
|
||
|
});
|
||
|
|
||
|
map_time_range.zip(map_weight_range).collect()
|
||
|
} else {
|
||
|
vec![]
|
||
|
}
|
||
|
}
|
||
|
|
||
|
pub fn load_data(path: &str, cutoff: Option<f64>) -> Option<Data> {
|
||
|
let mut w = process_sheet(path, WGHT_SHEET, TIME_COL, WGHT_COL);
|
||
|
let mut fr = process_sheet(path, FLOW_SHEET, TIME_COL, FLOW_COL);
|
||
|
|
||
|
if let Some(cutoff_val) = cutoff {
|
||
|
if cutoff_val != -1.0 {
|
||
|
w.retain(|x| x.0 < cutoff_val);
|
||
|
fr.retain(|x| x.0 < cutoff_val);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if !w.is_empty() && !fr.is_empty() {
|
||
|
let data = Data {
|
||
|
weight: w,
|
||
|
flowrate: fr,
|
||
|
};
|
||
|
Some(data)
|
||
|
} else {
|
||
|
None
|
||
|
}
|
||
|
}
|