use crate::prelude::Quantized;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct TEC {
tecu: Quantized,
rms: Option<Quantized>,
height: Option<Quantized>,
}
impl TEC {
pub fn from_tecu(tecu: f64) -> Self {
Self {
rms: None,
height: None,
tecu: Quantized::new_auto_scaled(tecu),
}
}
pub fn from_tec_m2(tec: f64) -> Self {
let tecu = tec / 10.0E16;
Self {
rms: None,
height: None,
tecu: Quantized::new_auto_scaled(tecu),
}
}
pub fn with_rms(mut self, rms: f64) -> Self {
self.rms = Some(Quantized::new_auto_scaled(rms));
self
}
pub(crate) fn from_quantized(tecu: i64, exponent: i8) -> Self {
Self {
rms: None,
height: None,
tecu: Quantized {
quantized: tecu,
exponent: -exponent,
},
}
}
pub(crate) fn set_quantized_root_mean_square(&mut self, rms: i64, exponent: i8) {
self.rms = Some(Quantized {
exponent: -exponent,
quantized: rms,
});
}
pub fn tecu(&self) -> f64 {
self.tecu.real_value_f64()
}
pub fn tec(&self) -> f64 {
self.tecu() * 10.0E16
}
pub fn root_mean_square(&self) -> Option<f64> {
let rms = self.rms?;
Some(rms.real_value_f64())
}
}
#[cfg(test)]
mod test {
use super::TEC;
#[test]
fn quantized_tec() {
let tec = TEC::from_quantized(30, -1);
assert_eq!(tec.tecu(), 3.0);
assert_eq!(tec.tec(), 3.0 * 10E16);
let tec = TEC::from_quantized(30, -2);
assert_eq!(tec.tecu(), 0.3);
assert_eq!(tec.tec(), 0.3 * 10E16);
let tec = TEC::from_tec_m2(1.0 * 10E16);
assert_eq!(tec.tecu(), 1.0);
assert_eq!(tec.tec(), 1.0 * 10E16);
assert_eq!(tec, TEC::from_tecu(1.0));
let tec = TEC::from_tec_m2(3.5 * 10E16);
assert_eq!(tec.tecu(), 3.5);
assert_eq!(tec.tec(), 3.5 * 10E16);
assert_eq!(tec, TEC::from_tecu(3.5));
let tec = TEC::from_tec_m2(190355078157525800.0);
assert_eq!(tec.tec(), 1.903550781575258e17);
}
}