mod cli; mod fops;
mod positioning;
mod preprocessing;
use preprocessing::preprocess;
mod report;
use report::Report;
use std::path::Path;
use walkdir::WalkDir;
extern crate gnss_rs as gnss;
use gnss_qc::prelude::{QcContext, QcExtraPage};
use rinex::prelude::{MergeError, Rinex};
use sp3::prelude::SP3;
use cli::{Cli, Context, RemoteReferenceSite, Workspace};
use map_3d::{ecef2geodetic, Ellipsoid};
#[cfg(feature = "csv")]
use csv::Error as CsvError;
use env_logger::{Builder, Target};
#[macro_use]
extern crate log;
use thiserror::Error;
#[derive(Debug, Error)]
pub enum Error {
#[error("i/o error")]
StdioError(#[from] std::io::Error),
#[error("rinex error")]
RinexError(#[from] rinex::Error),
#[error("missing OBS RINEX")]
MissingObservationRinex,
#[error("missing (BRDC) NAV RINEX")]
MissingNavigationRinex,
#[error("missing IONEX")]
MissingIONEX,
#[error("missing Meteo RINEX")]
MissingMeteoRinex,
#[error("missing Clock RINEX")]
MissingClockRinex,
#[error("file merge error: {0}")]
MergeError(#[from] MergeError),
#[error("positioning solver error")]
PositioningSolverError(#[from] positioning::Error),
#[cfg(feature = "csv")]
#[error("csv export error")]
CsvError(#[from] CsvError),
}
fn user_data_parsing(
cli: &Cli,
single_files: Vec<&String>,
directories: Vec<&String>,
max_depth: usize,
is_rover: bool,
) -> QcContext {
let mut ctx = QcContext::new(cli.jpl_bpc_update())
.unwrap_or_else(|e| panic!("failed to initialize a context: {}", e));
for dir in directories.iter() {
let walkdir = WalkDir::new(dir).max_depth(max_depth);
for entry in walkdir.into_iter().filter_map(|e| e.ok()) {
if !entry.path().is_dir() {
let path = entry.path();
if let Ok(rinex) = Rinex::from_path(path) {
let loading = ctx.load_rinex(path, rinex);
if loading.is_ok() {
info!("Loading RINEX file \"{}\"", path.display());
} else {
warn!(
"failed to load RINEX file \"{}\": {}",
path.display(),
loading.err().unwrap()
);
}
} else if let Ok(sp3) = SP3::from_path(path) {
let loading = ctx.load_sp3(path, sp3);
if loading.is_ok() {
info!("Loading SP3 file \"{}\"", path.display());
} else {
warn!(
"failed to load SP3 file \"{}\": {}",
path.display(),
loading.err().unwrap()
);
}
} else {
warn!("non supported file format \"{}\"", path.display());
}
}
}
}
for fp in single_files.iter() {
let path = Path::new(fp);
if let Ok(rinex) = Rinex::from_path(path) {
let loading = ctx.load_rinex(path, rinex);
if loading.is_err() {
warn!(
"failed to load RINEX file \"{}\": {}",
path.display(),
loading.err().unwrap()
);
}
} else if let Ok(sp3) = SP3::from_path(path) {
let loading = ctx.load_sp3(path, sp3);
if loading.is_err() {
warn!(
"failed to load SP3 file \"{}\": {}",
path.display(),
loading.err().unwrap()
);
}
} else {
warn!("non supported file format \"{}\"", path.display());
}
}
preprocess(&mut ctx, cli);
match cli.matches.subcommand() {
Some(("rtk", _)) => {
if is_rover {
debug!("ROVER Dataset: {:?}", ctx);
} else {
error!("BASE STATION Dataset: {:?}", ctx);
}
},
_ => {
debug!("{:?}", ctx);
},
}
ctx
}
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 max_recursive_depth = cli.recursive_depth();
let mut data_ctx = user_data_parsing(
&cli,
cli.rover_files(),
cli.rover_directories(),
max_recursive_depth,
true,
);
let ctx_position = data_ctx.reference_position();
let ctx_stem = Context::context_stem(&mut data_ctx);
let rx_ecef = match cli.manual_position() {
Some((x, y, z)) => {
let (lat, lon, _) = ecef2geodetic(x, y, z, Ellipsoid::WGS84);
let (lat_ddeg, lon_ddeg) = (lat.to_degrees(), lon.to_degrees());
info!(
"Manually defined position: {:?} [ECEF] (lat={:.5}°, lon={:.5}°)",
(x, y, z),
lat_ddeg,
lon_ddeg
);
Some((x, y, z))
},
None => {
if let Some(data_pos) = ctx_position {
let (x, y, z) = data_pos.to_ecef_wgs84();
let (lat, lon, _) = ecef2geodetic(x, y, z, Ellipsoid::WGS84);
let (lat_ddeg, lon_ddeg) = (lat.to_degrees(), lon.to_degrees());
info!(
"Position defined in dataset: {:?} [ECEF] (lat={:.5}°, lon={:.5}°)",
(x, y, z),
lat_ddeg,
lon_ddeg
);
Some((x, y, z))
} else {
warn!("No RX position defined");
None
}
},
};
let ctx = Context {
name: ctx_stem.clone(),
data: data_ctx,
reference_site: {
match cli.matches.subcommand() {
Some(("rtk", _)) => {
let data = user_data_parsing(
&cli,
cli.base_station_files(),
cli.base_station_directories(),
max_recursive_depth,
false,
);
if let Some(reference_point) = data.reference_position() {
let (base_x0_m, base_y0_m, base_z0_m) = reference_point.to_ecef_wgs84();
if let Some(rx_ecef) = rx_ecef {
let baseline_m = ((base_x0_m - rx_ecef.0).powi(2)
+ (base_y0_m - rx_ecef.1).powi(2)
+ (base_z0_m - rx_ecef.2).powi(2))
.sqrt();
if baseline_m > 1000.0 {
info!(
"Rover / Reference site baseline projection: {:.3}km",
baseline_m / 1000.0
);
} else {
info!(
"Rover / Reference site baseline projection: {:.3}m",
baseline_m
);
}
}
Some(RemoteReferenceSite {
data,
rx_ecef: Some((base_x0_m, base_y0_m, base_z0_m)),
})
} else {
error!("remote site does not have its geodetic marker defined: current CLI limitation.");
None
}
},
_ => None,
}
},
quiet: cli.matches.get_flag("quiet"),
workspace: Workspace::new(&ctx_stem, &cli),
rx_ecef,
};
if cli.has_fops_output_product() {
ctx.workspace.create_subdir("OUTPUT");
}
let mut extra_pages = Vec::<QcExtraPage>::new();
match cli.matches.subcommand() {
Some(("filegen", submatches)) => {
fops::filegen(&ctx, &cli.matches, submatches)?;
return Ok(());
},
Some(("merge", submatches)) => {
fops::merge(&ctx, submatches)?;
return Ok(());
},
Some(("split", submatches)) => {
fops::split(&ctx, submatches)?;
return Ok(());
},
Some(("tbin", submatches)) => {
fops::time_binning(&ctx, &cli.matches, submatches)?;
return Ok(());
},
Some(("diff", submatches)) => {
fops::diff(&ctx, submatches)?;
return Ok(());
},
Some(("ppp", submatches)) => {
let chapter = positioning::precise_positioning(&cli, &ctx, false, submatches)?;
extra_pages.push(chapter);
},
Some(("rtk", submatches)) => {
let chapter = positioning::precise_positioning(&cli, &ctx, true, submatches)?;
extra_pages.push(chapter);
},
_ => {},
}
let cfg = cli.qc_config();
let mut report = Report::new(&cli, &ctx, cfg);
for extra in extra_pages {
report.customize(extra);
}
report.generate(&cli, &ctx)?;
if !ctx.quiet {
ctx.workspace.open_with_web_browser();
}
Ok(())
}