mod analysis; mod cli; pub mod fops; mod identification; mod plot;
mod preprocessing;
use preprocessing::preprocess;
mod positioning;
use rinex::{
merge::Merge,
observation::{Combination, Combine},
prelude::*,
split::Split,
};
use rinex_qc::*;
extern crate gnss_rs as gnss;
extern crate gnss_rtk as rtk;
use cli::Cli;
use identification::rinex_identification;
use plot::PlotContext;
use env_logger::{Builder, Target};
#[macro_use]
extern crate log;
use fops::open_with_web_browser;
use std::io::Write;
use std::path::{Path, PathBuf};
use thiserror::Error;
#[derive(Debug, Error)]
pub enum Error {
#[error("rinex error")]
RinexError(#[from] rinex::Error),
#[error("positioning solver error")]
PositioningSolverError(#[from] positioning::solver::Error),
#[error("post processing error")]
PositioningPostProcError(#[from] positioning::post_process::Error),
}
pub(crate) fn context_stem(ctx: &RnxContext) -> String {
let ctx_major_stem: &str = ctx
.rinex_path()
.expect("failed to determine a context name")
.file_stem()
.expect("failed to determine a context name")
.to_str()
.expect("failed to determine a context name");
let primary_stem: Vec<&str> = ctx_major_stem.split('.').collect();
primary_stem[0].to_string()
}
pub fn workspace_path(ctx: &RnxContext, cli: &Cli) -> PathBuf {
match cli.workspace() {
Some(w) => Path::new(w).join(context_stem(ctx)),
None => Path::new(env!("CARGO_MANIFEST_DIR"))
.join("..")
.join("WORKSPACE")
.join(context_stem(ctx)),
}
}
pub fn create_workspace(path: PathBuf) {
std::fs::create_dir_all(&path).unwrap_or_else(|_| {
panic!(
"failed to create Workspace \"{}\": permission denied!",
path.to_string_lossy()
)
});
}
use walkdir::WalkDir;
fn build_context(cli: &Cli) -> RnxContext {
let mut ctx = RnxContext::default();
for dir in cli.input_directories() {
let walkdir = WalkDir::new(dir).max_depth(5);
for entry in walkdir.into_iter().filter_map(|e| e.ok()) {
if !entry.path().is_dir() {
let filepath = entry.path().to_string_lossy().to_string();
let ret = ctx.load(&filepath);
if ret.is_err() {
warn!("failed to load \"{}\": {}", filepath, ret.err().unwrap());
}
}
}
}
for filepath in cli.input_files() {
let ret = ctx.load(filepath);
if ret.is_err() {
warn!("failed to load \"{}\": {}", filepath, ret.err().unwrap());
}
}
ctx
}
fn skyplot_allowed(ctx: &RnxContext, cli: &Cli) -> bool {
if cli.quality_check_only() || cli.positioning() {
return false;
}
let has_nav = ctx.has_navigation_data();
let has_ref_position = ctx.ground_position().is_some() || cli.manual_position().is_some();
if has_nav && !has_ref_position {
info!("missing a reference position for the skyplot view.");
info!("see rinex-cli -h : antenna positions.");
}
has_nav && has_ref_position
}
fn naviplot_allowed(ctx: &RnxContext, cli: &Cli) -> bool {
skyplot_allowed(ctx, cli)
}
fn plot_combinations(obs: &Rinex, cli: &Cli, plot_ctx: &mut PlotContext) {
if cli.multipath() {
let data = obs.code_multipath();
plot::plot_gnss_dcb_mp(&data, plot_ctx, "Code Multipath", "Meters of delay");
info!("code multipath analysis");
}
if cli.if_combination() {
let data = obs.combine(Combination::IonosphereFree);
plot::plot_gnss_combination(
&data,
plot_ctx,
"Ionosphere Free combination",
"Meters of delay",
);
info!("iono free combination");
}
if cli.gf_combination() {
let data = obs.combine(Combination::GeometryFree);
plot::plot_gnss_combination(
&data,
plot_ctx,
"Geometry Free combination",
"Meters of delay",
);
info!("geo free combination");
}
if cli.wl_combination() {
let data = obs.combine(Combination::WideLane);
plot::plot_gnss_combination(&data, plot_ctx, "Wide Lane combination", "Meters of delay");
info!("wide lane combination");
}
if cli.nl_combination() {
let data = obs.combine(Combination::NarrowLane);
plot::plot_gnss_combination(
&data,
plot_ctx,
"Narrow Lane combination",
"Meters of delay",
);
info!("wide lane combination");
}
if cli.mw_combination() {
let data = obs.combine(Combination::MelbourneWubbena);
plot::plot_gnss_combination(
&data,
plot_ctx,
"Melbourne-Wübbena signal combination",
"Meters of Li-Lj delay",
);
info!("melbourne-wubbena combination");
}
}
pub fn main() -> Result<(), Error> {
let mut builder = Builder::from_default_env();
builder
.target(Target::Stdout)
.format_timestamp_secs()
.format_module_path(false)
.init();
let cli = Cli::new();
let quiet = cli.quiet();
let no_graph = cli.no_graph();
let qc_only = cli.quality_check_only();
let qc = cli.quality_check() || qc_only;
let positioning = cli.positioning();
let mut plot_ctx = PlotContext::new();
let mut qc_opts = cli.qc_config();
let mut ctx = build_context(&cli);
let workspace = workspace_path(&ctx, &cli);
info!("workspace is \"{}\"", workspace.to_string_lossy());
create_workspace(workspace.clone());
if ctx.obs_data().is_some() {
info!("observation data loaded");
}
if ctx.nav_data().is_some() {
info!("brdc navigation data loaded");
}
if ctx.sp3_data().is_some() {
info!("sp3 data loaded");
}
if ctx.meteo_data().is_some() {
info!("meteo data loaded");
}
if ctx.ionex_data().is_some() {
info!("ionex data loaded");
}
if let Some(pos) = cli.manual_position() {
let (lat, lon, _) = pos.to_geodetic();
info!(
"using manually defined reference position {} (lat={:.5}°, lon={:.5}°)",
pos, lat, lon
);
} else if let Some(pos) = ctx.ground_position() {
let (lat, lon, _) = pos.to_geodetic();
info!(
"using reference position {} (lat={:.5}°, lon={:.5}°)",
pos, lat, lon
);
} else {
info!("no reference position given or identified");
}
preprocess(&mut ctx, &cli);
if cli.identification() {
rinex_identification(&ctx, &cli);
return Ok(()); }
if !no_graph {
if let Some(obs) = ctx.obs_data() {
plot_combinations(obs, &cli, &mut plot_ctx);
} else {
error!("GNSS combinations requires Observation Data");
}
}
if let Some(rinex_b) = cli.to_merge() {
let rinex = ctx.rinex_data().expect("undefined RINEX data");
let new_rinex = rinex.merge(&rinex_b).expect("failed to merge both files");
let filename = match cli.output_path() {
Some(path) => path.clone(),
None => String::from("merged.rnx"),
};
let path = workspace.clone().join(filename);
let path = path
.as_path()
.to_str()
.expect("failed to generate merged file");
new_rinex
.to_file(path)
.expect("failed to generate merged file");
info!("\"{}\" has been generated", &path);
return Ok(());
}
if let Some(epoch) = cli.split() {
let rinex = ctx.rinex_data().expect("undefined RINEX data");
let (rnx_a, rnx_b) = rinex
.split(epoch)
.expect("failed to split primary rinex file");
let file_suffix = rnx_a
.first_epoch()
.expect("failed to determine file suffix")
.to_string();
let path = format!(
"{}/{}-{}.txt",
workspace.to_string_lossy(),
context_stem(&ctx),
file_suffix
);
rnx_a
.to_file(&path)
.unwrap_or_else(|_| panic!("failed to generate splitted file \"{}\"", path));
let file_suffix = rnx_b
.first_epoch()
.expect("failed to determine file suffix")
.to_string();
let path = format!(
"{}/{}-{}.txt",
workspace.to_string_lossy(),
context_stem(&ctx),
file_suffix
);
rnx_b
.to_file(&path)
.unwrap_or_else(|_| panic!("failed to generate splitted file \"{}\"", path));
return Ok(());
}
if !no_graph {
if skyplot_allowed(&ctx, &cli) {
let nav = ctx.nav_data().unwrap(); let ground_pos = ctx.ground_position().unwrap(); plot::skyplot(nav, ground_pos, &mut plot_ctx);
info!("skyplot view generated");
} else if !no_graph {
info!("skyplot view is not feasible");
}
}
if naviplot_allowed(&ctx, &cli) && !no_graph {
plot::naviplot(&ctx, &mut plot_ctx);
info!("navi plot generated");
}
if cli.cs_graph() && !no_graph {
info!("cs detector");
}
if !qc_only && !positioning && !no_graph {
info!("entering record analysis");
plot::plot_record(&ctx, &mut plot_ctx);
let html_path = workspace_path(&ctx, &cli).join("graphs.html");
let html_path = html_path.to_str().unwrap();
let mut html_fd = std::fs::File::create(html_path)
.unwrap_or_else(|_| panic!("failed to create \"{}\"", &html_path));
write!(html_fd, "{}", plot_ctx.to_html()).expect("failed to render graphs");
info!("graphs rendered in $WORKSPACE/graphs.html");
if !quiet {
open_with_web_browser(html_path);
}
}
if qc {
info!("entering qc mode");
if qc_opts.ground_position.is_none() {
if let Some(pos) = cli.manual_position() {
qc_opts = qc_opts.with_ground_position_ecef(pos.to_ecef_wgs84());
}
}
info!("Classification method : {:?}", qc_opts.classification);
info!("Reference position : {:?}", qc_opts.ground_position);
info!("Minimal SNR : {:?}", qc_opts.min_snr_db);
info!("Elevation mask : {:?}", qc_opts.elev_mask);
info!("Sampling gap tolerance: {:?}", qc_opts.gap_tolerance);
let html_report = QcReport::html(&ctx, qc_opts);
let report_path = workspace.join("report.html");
let mut report_fd = std::fs::File::create(&report_path).unwrap_or_else(|_| {
panic!(
"failed to create report \"{}\" : permission denied",
report_path.to_string_lossy()
)
});
write!(report_fd, "{}", html_report).expect("failed to generate QC summary report");
info!("qc report $WORSPACE/report.html has been generated");
if !quiet {
open_with_web_browser(&report_path.to_string_lossy());
}
}
if positioning {
let results = positioning::solver(&mut ctx, &cli)?;
positioning::post_process(workspace, &cli, &ctx, results)?;
}
Ok(())
}