use crate::cli::{Cli, Context};
use clap::ArgMatches;
use std::cell::RefCell;
use std::fs::read_to_string;
mod buffer;
pub use buffer::Buffer;
mod eph;
use eph::EphemerisSource;
mod ppp; use ppp::{
post_process::{post_process as ppp_post_process, Error as PPPPostError},
Report as PPPReport,
};
#[cfg(feature = "cggtts")]
mod cggtts;
#[cfg(feature = "cggtts")]
use cggtts::{post_process as cggtts_post_process, Report as CggttsReport};
mod rtk;
pub use rtk::RemoteRTKReference;
mod orbit;
use orbit::Orbits;
mod clock;
use clock::Clock;
pub use clock::ClockStateProvider;
use rinex::{
carrier::Carrier,
prelude::{Constellation, Rinex},
};
use gnss_qc::prelude::QcExtraPage;
use gnss_rtk::prelude::{
BdModel, Carrier as RTKCarrier, Config, Duration, Epoch, Error as RTKError, KbModel, Method,
NgModel, Orbit, PVTSolutionType, Solver,
};
use thiserror::Error;
#[derive(Debug, Error)]
pub enum Error {
#[error("solver error")]
SolverError(#[from] RTKError),
#[error("no solutions: check your settings or input")]
NoSolutions,
#[error("i/o error")]
StdioError(#[from] std::io::Error),
#[error("post process error")]
PPPPost(#[from] PPPPostError),
}
pub fn rtk_carrier_cast(carrier: RTKCarrier) -> Carrier {
match carrier {
RTKCarrier::L2 => Carrier::L2,
RTKCarrier::L5 => Carrier::L5,
RTKCarrier::L6 => Carrier::L6,
RTKCarrier::E1 => Carrier::E1,
RTKCarrier::E5 => Carrier::E5,
RTKCarrier::E6 => Carrier::E6,
RTKCarrier::E5A => Carrier::E5a,
RTKCarrier::E5B => Carrier::E5b,
RTKCarrier::B1I => Carrier::B1I,
RTKCarrier::B2 => Carrier::B2,
RTKCarrier::B3 => Carrier::B3,
RTKCarrier::B2A => Carrier::B2A,
RTKCarrier::B2iB2b => Carrier::B2I,
RTKCarrier::B1aB1c => Carrier::B1A,
RTKCarrier::L1 => Carrier::L1,
}
}
pub fn cast_rtk_carrier(carrier: Carrier) -> RTKCarrier {
match carrier {
Carrier::L2 => RTKCarrier::L2,
Carrier::L5 => RTKCarrier::L5,
Carrier::L6 => RTKCarrier::L6,
Carrier::E1 => RTKCarrier::E1,
Carrier::E5 => RTKCarrier::E5,
Carrier::E6 => RTKCarrier::E6,
Carrier::E5a => RTKCarrier::E5A,
Carrier::E5b => RTKCarrier::E5B,
Carrier::B1I => RTKCarrier::B1I,
Carrier::B2 => RTKCarrier::B2,
Carrier::B3 | Carrier::B3A => RTKCarrier::B3,
Carrier::B2A => RTKCarrier::B2A,
Carrier::B2I | Carrier::B2B => RTKCarrier::B2iB2b,
Carrier::B1A | Carrier::B1C => RTKCarrier::B1aB1c,
Carrier::L1 | _ => RTKCarrier::L1,
}
}
fn rtk_reference_carrier(carrier: RTKCarrier) -> bool {
matches!(
carrier,
RTKCarrier::L1 | RTKCarrier::E1 | RTKCarrier::B1aB1c | RTKCarrier::B1I
)
}
pub fn kb_model(nav: &Rinex, t: Epoch) -> Option<KbModel> {
let (_, sv, kb_model) = nav
.klobuchar_models()
.min_by_key(|(t_i, _, _)| (t - *t_i).abs())?;
Some(KbModel {
h_km: {
match sv.constellation {
Constellation::BeiDou => 375.0,
_ => 350.0,
}
},
alpha: kb_model.alpha,
beta: kb_model.beta,
})
}
pub fn bd_model(nav: &Rinex, t: Epoch) -> Option<BdModel> {
nav.bdgim_models()
.min_by_key(|(t_i, _)| (t - *t_i).abs())
.map(|(_, model)| BdModel { alpha: model.alpha })
}
pub fn ng_model(nav: &Rinex, t: Epoch) -> Option<NgModel> {
nav.nequick_g_models()
.min_by_key(|(t_i, _)| (t - *t_i).abs())
.map(|(_, model)| NgModel { a: model.a })
}
pub fn precise_positioning(
_cli: &Cli,
ctx: &Context,
is_rtk: bool,
matches: &ArgMatches,
) -> Result<QcExtraPage, Error> {
let cfg = match matches.get_one::<String>("cfg") {
Some(fp) => {
let content = read_to_string(fp)
.unwrap_or_else(|e| panic!("failed to read configuration: {}", e));
let mut cfg: Config = serde_json::from_str(&content)
.unwrap_or_else(|e| panic!("failed to parse configuration: {}", e));
#[cfg(feature = "cggtts")]
if matches.get_flag("cggtts") {
cfg.sol_type = PVTSolutionType::TimeOnly;
}
#[cfg(not(feature = "cggtts"))]
if matches.get_flag("cggtts") {
panic!("--cggtts option not available: compile with cggtts option");
}
info!("Using custom solver configuration: {:#?}", cfg);
cfg
},
None => {
let method = Method::default();
let mut cfg = Config::static_ppp_preset(method);
#[cfg(feature = "cggtts")]
if matches.get_flag("cggtts") {
cfg.sol_type = PVTSolutionType::TimeOnly;
}
#[cfg(not(feature = "cggtts"))]
if matches.get_flag("cggtts") {
panic!("--cggtts option not available: compile with cggtts option");
}
info!("Using {:?} default preset: {:#?}", method, cfg);
cfg
},
};
assert!(
ctx.data.observation().is_some(),
"Positioning requires Observation RINEX"
);
if !is_rtk {
assert!(
ctx.data.brdc_navigation().is_some(),
"Positioning requires Navigation RINEX"
);
}
if let Some(obs_rinex) = ctx.data.observation() {
if let Some(obs_header) = &obs_rinex.header.obs {
if let Some(time_of_first_obs) = obs_header.time_of_first_obs {
if let Some(clk_rinex) = ctx.data.clock() {
if let Some(clk_header) = &clk_rinex.header.clock {
if let Some(time_scale) = clk_header.timescale {
if time_scale == time_of_first_obs.time_scale {
info!("Temporal PPP compliancy");
} else {
error!("Working with different timescales in OBS/CLK RINEX is not PPP compatible and will generate tiny errors");
warn!("Consider using OBS/CLK RINEX files expressed in the same timescale for optimal results");
}
}
}
} else if let Some(sp3) = ctx.data.sp3() {
if ctx.data.sp3_has_clock() {
if sp3.time_scale == time_of_first_obs.time_scale {
info!("Temporal PPP compliancy");
} else {
error!("Working with different timescales in OBS/SP3 is not PPP compatible and will generate tiny errors");
if sp3.epoch_interval >= Duration::from_seconds(300.0) {
warn!("Interpolating clock states from low sample rate SP3 will most likely introduce errors");
}
}
}
}
}
}
}
info!("Using {:?} method", cfg.method);
let eph = RefCell::new(EphemerisSource::from_ctx(ctx));
let clocks = Clock::new(&ctx, &eph);
let orbits = Orbits::new(&ctx, &eph);
let mut rtk_reference = RemoteRTKReference::from_ctx(&ctx);
#[cfg(feature = "cggtts")]
let apriori = if matches.get_flag("cggtts") {
if let Some((x, y, z)) = ctx.rx_ecef {
Some(Orbit::from_position(
x / 1.0E3,
y / 1.0E3,
z / 1.0E3,
Epoch::default(),
ctx.data.earth_cef,
))
} else {
panic!(
"--cggtts opmode cannot work without a priori position knowledge.
You either need to specify it manually (see --help), or use RINEX files that define
a static reference position"
);
}
} else {
None
};
#[cfg(not(feature = "cggtts"))]
let apriori = None;
let solver = Solver::new_almanac_frame(
&cfg,
apriori,
orbits,
ctx.data.almanac.clone(),
ctx.data.earth_cef,
);
#[cfg(feature = "cggtts")]
if matches.get_flag("cggtts") {
let tracks = cggtts::resolve(ctx, &eph, clocks, solver, matches)?;
if !tracks.is_empty() {
cggtts_post_process(&ctx, &tracks, matches)?;
let report = CggttsReport::new(&ctx, &tracks);
return Ok(report.formalize());
} else {
error!("solver did not generate a single solution");
error!("verify your input data and configuration setup");
return Err(Error::NoSolutions);
}
}
let solutions = ppp::resolve(ctx, &eph, clocks, &mut rtk_reference, solver);
if !solutions.is_empty() {
ppp_post_process(&ctx, &solutions, matches)?;
let report = PPPReport::new(&cfg, &ctx, &solutions);
Ok(report.formalize())
} else {
error!("solver did not generate a single solution");
error!("verify your input data and configuration setup");
Err(Error::NoSolutions)
}
}