use alloc::vec::Vec;
use crate::error::TimeError;
use crate::math;
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct EopSample {
pub mjd: f64,
pub ut1_minus_utc: f64,
pub observed: bool,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum EopSeries {
BulletinA,
BulletinB,
FinalPreferred,
}
#[derive(Clone, Debug)]
pub struct EopTable {
samples: Vec<EopSample>,
}
impl EopTable {
pub fn new(mut samples: Vec<EopSample>) -> Result<Self, TimeError> {
if samples.is_empty() {
return Err(TimeError::EopMissing);
}
if samples
.iter()
.any(|sample| !sample.mjd.is_finite() || !sample.ut1_minus_utc.is_finite())
{
return Err(TimeError::EopParse);
}
samples.sort_by(|a, b| a.mjd.total_cmp(&b.mjd));
samples.dedup_by(|a, b| math::abs(a.mjd - b.mjd) < 1e-9);
Ok(Self { samples })
}
pub fn from_finals2000a(text: &str) -> Result<Self, TimeError> {
Self::from_finals2000a_series(text, EopSeries::BulletinA)
}
pub fn from_finals2000a_series(text: &str, series: EopSeries) -> Result<Self, TimeError> {
let mut samples = Vec::new();
for line in text.lines() {
if line.len() < 68 {
continue;
}
let mjd_field = match line.get(6..15) {
Some(field) => field.trim(),
None => continue,
};
if mjd_field.is_empty() {
continue;
}
let bulletin_a = line
.get(58..68)
.and_then(|field| field.trim().parse::<f64>().ok());
let bulletin_b = line.get(155..165).and_then(|field| {
let text = field.trim();
if text.is_empty() {
None
} else {
text.parse::<f64>().ok()
}
});
let a_observed = line.get(57..58).map(|flag| flag != "P").unwrap_or(true);
let (ut1_minus_utc, observed) = match series {
EopSeries::BulletinA => match bulletin_a {
Some(value) => (value, a_observed),
None => continue,
},
EopSeries::BulletinB => match bulletin_b {
Some(value) => (value, true),
None => continue,
},
EopSeries::FinalPreferred => match bulletin_b {
Some(value) => (value, true),
None => match bulletin_a {
Some(value) => (value, a_observed),
None => continue,
},
},
};
let mjd = match mjd_field.parse::<f64>() {
Ok(value) => value,
Err(_) => continue,
};
samples.push(EopSample {
mjd,
ut1_minus_utc,
observed,
});
}
if samples.is_empty() {
return Err(TimeError::EopParse);
}
Self::new(samples)
}
pub fn observed_only(&self) -> Result<Self, TimeError> {
Self::new(
self.samples
.iter()
.copied()
.filter(|sample| sample.observed)
.collect(),
)
}
pub fn ut1_minus_utc(&self, mjd: f64) -> Result<f64, TimeError> {
if !mjd.is_finite() {
return Err(TimeError::Ut1OutOfRange);
}
let first = self.samples.first().ok_or(TimeError::EopMissing)?;
let last = self.samples.last().ok_or(TimeError::EopMissing)?;
if mjd < first.mjd - 1e-9 || mjd > last.mjd + 1e-9 {
return Err(TimeError::Ut1OutOfRange);
}
let index = self.samples.partition_point(|sample| sample.mjd <= mjd);
if index == 0 {
return Ok(first.ut1_minus_utc);
}
if index >= self.samples.len() {
return Ok(last.ut1_minus_utc);
}
let before = self.samples[index - 1];
let after = self.samples[index];
let span = after.mjd - before.mjd;
if span <= 0.0 {
return Ok(before.ut1_minus_utc);
}
let fraction = (mjd - before.mjd) / span;
Ok(before.ut1_minus_utc + fraction * (after.ut1_minus_utc - before.ut1_minus_utc))
}
pub fn range(&self) -> (f64, f64) {
match (self.samples.first(), self.samples.last()) {
(Some(first), Some(last)) => (first.mjd, last.mjd),
_ => (f64::NAN, f64::NAN),
}
}
pub fn samples(&self) -> &[EopSample] {
&self.samples
}
pub fn len(&self) -> usize {
self.samples.len()
}
pub fn is_empty(&self) -> bool {
self.samples.is_empty()
}
}