mod analysis; mod cli; pub mod fops; mod identification; mod plot;
mod preprocessing;
use preprocessing::preprocess;
use rinex::{
merge::Merge,
observation::{Combine, Dcb, IonoDelay, Mp},
prelude::RinexType,
prelude::*,
split::Split,
};
use rinex_qc::*;
use cli::Cli;
use identification::rinex_identification;
use plot::PlotContext;
extern crate pretty_env_logger;
#[macro_use]
extern crate log;
use clap::parser::ValuesRef;
use fops::open_with_web_browser;
use sp3::{prelude::SP3, Merge as SP3Merge};
use std::io::Write;
use std::path::{Path, PathBuf};
fn workspace_path(ctx: &QcContext) -> PathBuf {
let primary_stem: &str = ctx
.primary_path()
.file_stem()
.expect("failed to determine Workspace")
.to_str()
.expect("failed to determine Workspace");
let primary_stem: Vec<&str> = primary_stem.split(".").collect();
Path::new(env!("CARGO_MANIFEST_DIR"))
.join("workspace")
.join(primary_stem[0])
}
pub fn create_workspace(path: PathBuf) {
std::fs::create_dir_all(&path).expect(&format!(
"failed to create Workspace \"{}\": permission denied!",
path.to_string_lossy()
));
}
fn build_extra_rinex_data(
paths: Option<ValuesRef<'_, String>>,
rtype: RinexType,
) -> Option<QcExtraData<Rinex>> {
if let Some(paths) = paths {
let mut ctx = QcExtraData::<Rinex>::default();
for path in paths {
if let Ok(new) = Rinex::from_file(path) {
if new.header.rinex_type != rtype {
let stem = Path::new(path).file_stem().unwrap();
error!("\"{}\" : invalid {} RINEX", stem.to_string_lossy(), rtype);
continue;
}
if ctx.paths().is_empty() {
ctx = QcExtraData {
paths: vec![Path::new(path).to_path_buf()],
data: new.clone(),
};
trace!("{} file \"{}\"", rtype, path);
} else {
match ctx.data_mut().merge_mut(&new) {
Ok(_) => {
trace!("{} file \"{}\"", rtype, path);
ctx.paths.push(Path::new(path).to_path_buf());
},
Err(e) => error!("failed to parse nav file \"{}\" : {:?}", path, e),
}
}
} else {
let stem = Path::new(path).file_stem().unwrap();
error!("\"{}\" : invalid {} RINEX", stem.to_string_lossy(), rtype);
}
}
Some(ctx)
} else {
None }
}
fn build_extra_sp3_data(paths: Option<ValuesRef<'_, String>>) -> Option<QcExtraData<SP3>> {
if let Some(paths) = paths {
let mut ctx = QcExtraData::<SP3>::default();
for path in paths {
if let Ok(new) = SP3::from_file(path) {
if ctx.paths().is_empty() {
ctx = QcExtraData {
paths: vec![Path::new(path).to_path_buf()],
data: new.clone(),
};
trace!("SP3 file \"{}\"", path);
} else {
match ctx.data_mut().merge_mut(&new) {
Ok(_) => {
trace!("SP3 file \"{}\"", path);
ctx.paths.push(Path::new(path).to_path_buf());
},
Err(e) => error!("failed to parse sp3 file \"{}\" : {:?}", path, e),
}
}
} else {
let stem = Path::new(path).file_stem().unwrap();
error!("\"{}\" : invalid SP3", stem.to_string_lossy());
}
}
Some(ctx)
} else {
None }
}
fn create_context(cli: &Cli) -> QcContext {
QcContext {
primary: {
let path = cli.input_path();
QcPrimaryData {
path: Path::new(path).to_path_buf(),
data: Rinex::from_file(path).expect("failed to parse primary RINEX file"),
}
},
nav: build_extra_rinex_data(cli.nav_paths(), RinexType::NavigationData),
atx: build_extra_rinex_data(cli.atx_paths(), RinexType::AntennaData),
sp3: build_extra_sp3_data(cli.sp3_paths()),
}
}
fn skyplot_allowed(ctx: &QcContext, cli: &Cli) -> bool {
if cli.quality_check_only() {
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
}
pub fn main() -> Result<(), rinex::Error> {
pretty_env_logger::init_timed();
let cli = Cli::new();
let quiet = cli.quiet();
let qc_only = cli.quality_check_only();
let qc = cli.quality_check() || qc_only;
let mut plot_ctx = PlotContext::new();
let mut qc_opts = cli.qc_config();
let mut ctx = create_context(&cli);
let workspace = workspace_path(&ctx);
info!("workspace is \"{}\"", workspace.to_string_lossy());
create_workspace(workspace.clone());
if let Some(pos) = cli.manual_position() {
info!("using manually defined reference position {}", pos);
} else if let Some(pos) = ctx.ground_position() {
info!("using reference position {}", pos);
} else {
info!("no reference position given or identified");
}
preprocess(&mut ctx, &cli);
if cli.identification() {
rinex_identification(&ctx, &cli);
return Ok(()); }
if cli.sv_epoch() {
info!("sv/epoch analysis");
analysis::sv_epoch(&ctx, &mut plot_ctx);
}
if cli.sampling_histogram() {
info!("sample rate histogram analysis");
analysis::sampling::histogram(&ctx, &mut plot_ctx);
}
if cli.dcb() {
let data = ctx
.primary_data()
.observation_phase_align_origin()
.observation_phase_carrier_cycles()
.dcb();
plot::plot_gnss_dcb(
&mut plot_ctx,
"Differential Code Biases",
"DBCs [n.a]",
&data,
);
info!("--dcb analysis");
}
if cli.multipath() {
warn!("--mp analysis not available yet");
}
if cli.gf_recombination() {
let data = ctx
.primary_data()
.observation_phase_align_origin()
.geo_free();
plot::plot_gnss_recombination(
&mut plot_ctx,
"Geometry Free signal combination",
"Meters of Li-Lj delay",
&data,
);
info!("--gf recombination");
}
if cli.iono_detector() {
let data = ctx.primary_data().iono_delay(Duration::from_seconds(360.0));
plot::plot_iono_detector(&mut plot_ctx, &data);
info!("--iono detector");
}
if cli.wl_recombination() {
let data = ctx.primary_data().wide_lane();
plot::plot_gnss_recombination(
&mut plot_ctx,
"Wide Lane signal combination",
"Meters of Li-Lj delay",
&data,
);
info!("--wl recombination");
}
if cli.nl_recombination() {
let data = ctx.primary_data().narrow_lane();
plot::plot_gnss_recombination(
&mut plot_ctx,
"Narrow Lane signal combination",
"Meters of Li-Lj delay",
&data,
);
info!("--nl recombination");
}
if cli.mw_recombination() {
let data = ctx.primary_data().melbourne_wubbena();
plot::plot_gnss_recombination(
&mut plot_ctx,
"Melbourne-Wübbena signal combination",
"Meters of Li-Lj delay",
&data,
);
info!("--mw recombination");
}
if let Some(rinex_b) = cli.to_merge() {
info!("merging files..");
let new_rinex = ctx
.primary_data()
.merge(&rinex_b)
.expect("merging operation failed");
let path = workspace.clone().join("merged.rnx");
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 (rnx_a, rnx_b) = ctx
.primary_data()
.split(epoch)
.expect("failed to split primary rinex file");
let file_stem = ctx
.primary_path()
.file_stem()
.expect("failed to determine file prefix")
.to_str()
.expect("failed to determine file prefix");
let file_suffix = rnx_a
.first_epoch()
.expect("failed to determine file suffix")
.to_string();
let path = format!(
"{}/{}-{}.txt",
workspace.to_string_lossy(),
file_stem,
file_suffix
);
rnx_a
.to_file(&path)
.expect(&format!("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(),
file_stem,
file_suffix
);
rnx_b
.to_file(&path)
.expect(&format!("failed to generate splitted file \"{}\"", path));
return Ok(());
}
if skyplot_allowed(&ctx, &cli) {
plot::skyplot(&ctx, &mut plot_ctx);
info!("skyplot view generated");
}
if cli.cs_graph() {
info!("cs detector");
}
if !qc_only {
info!("entering record analysis");
plot::plot_record(&ctx, &mut plot_ctx);
}
if !qc_only {
let html_path = workspace_path(&ctx).join("graphs.html");
let html_path = html_path.to_str().unwrap();
let mut html_fd = std::fs::File::create(html_path)
.expect(&format!("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).expect(&format!(
"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());
}
}
Ok(())
}