#[must_use]
pub fn barometric_pressure_from_altitude(elevation_m: f64, temp_c: f64) -> f64 {
let temp_k = temp_c + 273.15;
let lapse = 0.0065; let base = 1.0 - lapse * elevation_m / (temp_k + lapse * elevation_m);
let pressure_kpa = 101.325 * base.powf(5.2561);
(pressure_kpa * 10.0).round()
}
#[must_use]
pub fn co2_correction(
raw_co2: f64,
pressure_hpa: f64,
temp_c: f64,
std_curve: Option<(f64, f64)>,
) -> f64 {
let corrected = match std_curve {
Some((slope, intercept)) => raw_co2 * slope + intercept,
None => raw_co2,
};
corrected * pressure_hpa * 298.0 / (1013.0 * (273.0 + temp_c))
}
#[must_use]
pub fn reach_depth_stats(depths: &[f64]) -> (f64, f64) {
(super::common::mean(depths), super::common::std_dev(depths))
}
#[must_use]
pub fn select_pressure(field_pressure: Option<f64>, altitude_pressure: Option<f64>) -> Option<f64> {
if let Some(fp) = field_pressure {
if (700.0..=1050.0).contains(&fp) {
return Some(fp);
}
}
altitude_pressure
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_barometric_pressure_sea_level() {
let result = barometric_pressure_from_altitude(0.0, 15.0);
assert!(
(result - 1013.0).abs() < 1.0,
"expected ~1013 at sea level, got {result}"
);
}
#[test]
fn test_barometric_pressure_high_altitude() {
let result = barometric_pressure_from_altitude(2000.0, 10.0);
assert!(
(result - 795.0).abs() < 10.0,
"expected ~795 at 2000m, got {result}"
);
}
#[test]
fn test_co2_correction_no_curve() {
let result = co2_correction(500.0, 900.0, 15.0, None);
let expected = 500.0 * 900.0 * 298.0 / (1013.0 * 288.0);
assert!(
(result - expected).abs() < 0.001,
"expected {expected}, got {result}"
);
}
#[test]
fn test_co2_correction_with_curve() {
let result = co2_correction(500.0, 900.0, 15.0, Some((1.1, -5.0)));
let corrected = 500.0 * 1.1 + (-5.0);
let expected = corrected * 900.0 * 298.0 / (1013.0 * 288.0);
assert!(
(result - expected).abs() < 0.001,
"expected {expected}, got {result}"
);
}
#[test]
fn test_select_pressure_valid_field() {
assert_eq!(select_pressure(Some(950.0), Some(800.0)), Some(950.0));
}
#[test]
fn test_select_pressure_out_of_range() {
assert_eq!(select_pressure(Some(600.0), Some(800.0)), Some(800.0));
}
#[test]
fn test_select_pressure_no_field() {
assert_eq!(select_pressure(None, Some(800.0)), Some(800.0));
}
}