use smallvec::SmallVec;
use sounding_base::Profile::*;
use sounding_base::{DataRow, Profile, Sounding};
use error::AnalysisError::*;
use error::*;
use layers::Layer;
const FREEZING: f64 = 0.0;
pub type Level = DataRow;
pub type Levels = SmallVec<[Level; ::VEC_SIZE]>;
pub fn freezing_levels(snd: &Sounding) -> Result<Levels> {
find_temperature_levels(snd, Temperature, FREEZING)
}
pub fn wet_bulb_zero_levels(snd: &Sounding) -> Result<Levels> {
find_temperature_levels(snd, WetBulb, FREEZING)
}
fn find_temperature_levels(snd: &Sounding, var: Profile, target_t: f64) -> Result<Levels> {
use interpolation::{linear_interp, linear_interpolate_sounding};
debug_assert!(var == Temperature || var == WetBulb);
let mut to_return: Levels = Levels::new();
const TOP_PRESSURE: f64 = 500.0;
let p_profile = snd.get_profile(Pressure);
let t_profile = snd.get_profile(var);
if t_profile.is_empty() || p_profile.is_empty() {
return Err(AnalysisError::MissingProfile);
}
let mut iter = izip!(p_profile, t_profile).filter_map(|pair| {
if pair.0.is_some() && pair.1.is_some() {
let (p, t) = (pair.0.unpack(), pair.1.unpack());
Some((p, t))
} else {
None
}
});
let (bottom_p, bottom_t) = iter.by_ref().next().ok_or(NoDataProfile)?;
iter
.take_while(|&(p, _)| p >= TOP_PRESSURE)
.fold(
Ok((bottom_p, bottom_t)),
|acc: Result<(f64, f64)>, (p, t)| {
if let Ok((last_p, last_t)) = acc {
if last_t <= target_t && t > target_t || last_t > target_t && t <= target_t {
let target_p = linear_interp(target_t, last_t, t, last_p, p);
to_return.push(linear_interpolate_sounding(snd, target_p)?);
}
Ok((p, t))
} else {
acc
}
},
)
.map(|_| to_return)
}
pub fn max_wet_bulb_in_profile(snd: &Sounding) -> Result<Level> {
max_t_aloft(snd, Profile::WetBulb)
}
pub fn max_temperature_in_profile(snd: &Sounding) -> Result<Level> {
max_t_aloft(snd, Profile::Temperature)
}
fn max_t_aloft(snd: &Sounding, var: Profile) -> Result<Level> {
use sounding_base::Profile::*;
debug_assert!(var == Temperature || var == WetBulb);
const TOP_PRESSURE: f64 = 500.0;
let p_profile = snd.get_profile(Pressure);
let t_profile = snd.get_profile(var);
if t_profile.is_empty() || p_profile.is_empty() {
return Err(AnalysisError::MissingProfile);
}
izip!(0usize.., p_profile, t_profile)
.filter_map(|triple| {
if triple.1.is_some() && triple.2.is_some() {
let (i, p, t) = (triple.0, triple.1.unpack(), triple.2.unpack());
if p >= TOP_PRESSURE {
Some((i, t))
} else {
None
}
} else {
None
}
}).fold(Ok((0, ::std::f64::MIN)), |acc: Result<_>, (i, t)| {
if let Ok((_, mx_t)) = acc {
if t > mx_t {
Ok((i, t))
} else {
acc
}
} else {
acc
}
})
.and_then(|(idx, _)| snd.get_data_row(idx).ok_or(InvalidInput))
}
pub fn max_temperature_in_layer(snd: &Sounding, lyr: &Layer) -> Result<Level> {
max_t_in_layer(snd, Profile::Temperature, lyr)
}
pub fn max_wet_bulb_in_layer(snd: &Sounding, lyr: &Layer) -> Result<Level> {
max_t_in_layer(snd, Profile::WetBulb, lyr)
}
fn max_t_in_layer(snd: &Sounding, var: Profile, lyr: &Layer) -> Result<Level> {
use sounding_base::Profile::*;
debug_assert!(var == Temperature || var == WetBulb);
let (bottom_p, top_p) = if lyr.bottom.pressure.is_some() && lyr.top.pressure.is_some() {
(lyr.bottom.pressure.unpack(), lyr.top.pressure.unpack())
} else {
return Err(AnalysisError::InvalidInput);
};
let p_profile = snd.get_profile(Pressure);
let t_profile = snd.get_profile(var);
if t_profile.is_empty() || p_profile.is_empty() {
return Err(AnalysisError::MissingProfile);
}
izip!(0usize.., p_profile, t_profile)
.filter_map(|triple| {
if triple.1.is_some() && triple.2.is_some() {
let (i, p, t) = (triple.0, triple.1.unpack(), triple.2.unpack());
if p >= top_p && p <= bottom_p {
Some((i, t))
} else {
None
}
} else {
None
}
}).fold(Ok((0, ::std::f64::MIN)), |acc: Result<_>, (i, t)| {
if let Ok((_, mx_t)) = acc {
if t > mx_t {
Ok((i, t))
} else {
acc
}
} else {
acc
}
})
.and_then(|(idx, _)| snd.get_data_row(idx).ok_or(InvalidInput))
}