rinex-cli 0.5.0

Command line tool parse and analyze RINEX data
//! Command line tool to parse and analyze `RINEX` files.    
//! Refer to README for command line arguments.    
//! Based on crate <https://github.com/gwbres/rinex>     
//! Homepage: <https://github.com/gwbres/rinex-cli>

mod cli; // command line interface 
mod teqc; // `teqc` operations
mod plot; // plotting operations
mod retain; // record filtering 
mod filter; // record filtering
mod resampling; // record resampling
mod analysis; // basic analysis operations
mod identification; // high level identification/macros
mod file_generation; // RINEX to file macro

use rinex::{*, split::Split, merge::Merge};
use cli::Cli;
use retain::retain_filters;
use filter::apply_filters;
use resampling::record_resampling;
use identification::basic_identification;

pub mod parser; // command line parsing utilities

pub fn main() -> Result<(), rinex::Error> {
    let cli = Cli::new();
    let pretty = cli.pretty();

    let fp = cli.input_path();

    // RINEX data 
    let mut rnx = Rinex::from_file(fp)?;
    let mut nav_context = cli.nav_context();

    /*
     * Preprocessing, filtering, resampling..
     */
    if cli.resampling() { // resampling requested
        record_resampling(&mut rnx, cli.resampling_ops());
        if let Some(ref mut nav) = nav_context {
            record_resampling(nav, cli.resampling_ops());
        }
    }
    if cli.retain() { // retain data of interest
        retain_filters(&mut rnx, cli.retain_flags(), cli.retain_ops());
        if let Some(ref mut nav) = nav_context {
            retain_filters(nav, cli.retain_flags(), cli.retain_ops());
        }
    }
    if cli.filter() { // apply desired filters
        apply_filters(&mut rnx, cli.filter_ops());
        if let Some(ref mut nav) = nav_context {
            apply_filters(nav, cli.filter_ops());
        }
    }

    /*
     * Observation RINEX:
     *  align phase origins at this point
     *  this allows easy GNSS combination and processing,
     *  also gives a more meaningful record plot
     */
    rnx.observation_align_phase_origins_mut();

    /*
     * Basic file identification
     */
    if cli.basic_identification() {
        basic_identification(&rnx, cli.identification_ops(), pretty);
        return Ok(());
    }
    
    /*
     * SV per Epoch analysis requested
     */
    if cli.sv_epoch() {
        analysis::sv_epoch::analyze(&rnx, &mut nav_context, cli.plot_dimensions());
        return Ok(());
    }
    /*
     * Epoch histogram analysis
     */
    if cli.epoch_histogram() {
        analysis::epoch_histogram(&rnx, cli.plot_dimensions());
        return Ok(());
    }

    /*
     * DCB analysis requested
     */
    if cli.dcb() {
        let dims = cli.plot_dimensions();
        let mut data = rnx.observation_phase_dcb();
        for (op, inner) in rnx.observation_pseudorange_dcb() {
            data.insert(op.clone(), inner.clone());
        }
        plot::plot_gnss_recombination(
            dims, 
            "dcb.png", 
            "Differential Code Biases",
            "DBCs [n.a]",
            &data);
    }
    
    /*
     * Code Multipath analysis
     */
    if cli.multipath() {
        let dims = cli.plot_dimensions();
        let data = rnx.observation_code_multipath();
        plot::plot_gnss_recombination(
            dims,
            "mp.png",
            "Code Multipath Biases",
            "MP [n.a]",
            &data);
    }
    /*
     * [GF] recombination visualization requested
     */
    if cli.gf_recombination() {
        let data = rnx.observation_gf_combinations();
        let dims = cli.plot_dimensions();
        plot::plot_gnss_recombination(
            dims, 
            "gf.png",
            "Geometry Free signal combination",
            "Meters of Li-Lj delay",
            &data);
    }
    /*
     * [WL] recombination
     */
    if cli.wl_recombination() {
        let data = rnx.observation_wl_combinations();
        let dims = cli.plot_dimensions();
        plot::plot_gnss_recombination(
            dims, 
            "wl.png",
            "Wide Lane signal combination",
            "Meters of Li-Lj delay",
            &data);
    }
    /*
     * [NL] recombination
     */
    if cli.nl_recombination() {
        let data = rnx.observation_nl_combinations();
        let dims = cli.plot_dimensions();
        plot::plot_gnss_recombination(
            dims, 
            "nl.png",
            "Narrow Lane signal combination",
            "Meters of Li-Lj delay",
            &data);
    }
    /*
     * [MW] recombination
     */
    if cli.mw_recombination() {
        let data = rnx.observation_mw_combinations();
        let dims = cli.plot_dimensions();
        plot::plot_gnss_recombination(
            dims, 
            "mw.png",
            "Melbourne-Wübbena signal combination",
            "Meters of Li-Lj delay",
            &data);
    }
    
    /*
     * teqc [MERGE]
     */
    if let Some(rnx_b) = cli.merge() {
        // we're merging (A)+(B) into (C)
        let mut rnx_b = Rinex::from_file(rnx_b)?;
        // [0] apply similar conditions to RNX_b
        if cli.resampling() {
            record_resampling(&mut rnx_b, cli.resampling_ops());
        }
        if cli.retain() {
            retain_filters(&mut rnx_b, cli.retain_flags(), cli.retain_ops());
        }
        if cli.filter() {
            apply_filters(&mut rnx_b, cli.filter_ops());
        }
        // [1] proceed to merge
        rnx.merge_mut(&rnx_b)
            .expect("`merge` operation failed");
        // [2] generate new file
        file_generation::generate(&rnx, cli.output_path(), "merged.rnx")
            .unwrap();
        // [*] stop here, special mode: no further analysis allowed
        return Ok(());
    }
    
    /*
     * teqc [SPLIT]
     */
    if let Some(epoch) = cli.split() {
        let (a,b) = rnx.split(epoch)
            .expect(&format!("failed to split \"{}\" into two", fp));

        let name = format!("{}-{}.rnx", cli.input_path(), a.epochs()[0]);
        file_generation::generate(&a, None, &name)
            .unwrap();
        
        let name = format!("{}-{}.rnx", cli.input_path(), b.epochs()[0]);
        file_generation::generate(&b, None, &name)
            .unwrap();
        
        // [*] stop here, special mode: no further analysis allowed
        return Ok(());
    }

    /*
     * Record analysis / visualization
     */
    let dims = cli.plot_dimensions();
    let mut ctx = plot::record::Context::new(dims, &rnx); 
    plot::record::plot(&mut ctx, &rnx, nav_context); 
    Ok(())
}// main