use crate::{
error::Result,
sounding::Sounding,
};
use itertools::{izip, Itertools};
use metfor::{mixing_ratio, Mm, Quantity};
#[inline]
pub fn precipitable_water(snd: &Sounding) -> Result<Mm> {
let p_profile = snd.pressure_profile();
let dp_profile = snd.dew_point_profile();
let integrated_mw = izip!(p_profile, dp_profile)
.filter(|(p, dp)| p.is_some() && dp.is_some())
.map(|(p, dp)| (p.unpack(), dp.unpack()))
.filter_map(|(p, dp)| mixing_ratio(dp, p).map(|mw| (p, mw)))
.tuple_windows::<(_, _)>()
.fold(0.0, |mut acc_mw, ((p0, mw0), (p1, mw1))| {
let dp = p0 - p1;
acc_mw += (mw0 + mw1) * dp.unpack();
acc_mw
});
Ok(Mm(integrated_mw / 9.81 / 997.0 * 100_000.0 / 2.0))
}