wfdisc 0.1.0

Reader for CSS 3.0 / NNSA KB Core wfdisc seismological waveform descriptor files. Cross-validated against ObsPy.
Documentation
//! `cargo run --example dump -- <path/to/file.wfdisc>`
//!
//! Streams a wfdisc descriptor file, printing one line of summary per
//! row, then attempts to decode the binary samples and reports a small
//! statistical fingerprint per trace.

use std::env;
use std::path::PathBuf;
use std::process::ExitCode;

use wfdisc::{Samples, WfdiscReader};

fn main() -> ExitCode {
    let mut args = env::args().skip(1);
    let Some(arg) = args.next() else {
        eprintln!("usage: dump <path/to/file.wfdisc>");
        return ExitCode::from(2);
    };
    let path = PathBuf::from(arg);

    let reader = match WfdiscReader::open(&path) {
        Ok(r) => r,
        Err(e) => {
            eprintln!("failed to open {}: {e}", path.display());
            return ExitCode::FAILURE;
        }
    };

    let base_dir = reader
        .base_dir()
        .map(|p| p.to_path_buf())
        .unwrap_or_else(|| PathBuf::from("."));

    let mut total_rows = 0usize;
    let mut total_samples = 0usize;
    for (i, row) in reader.enumerate() {
        let row = match row {
            Ok(r) => r,
            Err(e) => {
                eprintln!("row {i}: parse error: {e}");
                return ExitCode::FAILURE;
            }
        };
        total_rows += 1;
        println!(
            "{i:>3}  {sta:<6}  {chan:<8}  {dtype}  nsamp={nsamp:>6}  rate={rate:>7.2}Hz  start={start:>17.3}  -> {dfile} @ {foff}",
            i = i,
            sta = row.sta,
            chan = row.chan,
            dtype = row.datatype,
            nsamp = row.nsamp,
            rate = row.samprate,
            start = row.time,
            dfile = row.dfile,
            foff = row.foff,
        );
        match row.read_samples(&base_dir) {
            Ok(samples) => {
                total_samples += samples.len();
                let fp = fingerprint(&samples);
                println!(
                    "      samples({kind}) min={min:.4}  max={max:.4}  mean={mean:.4}",
                    kind = samples.kind(),
                    min = fp.min,
                    max = fp.max,
                    mean = fp.mean,
                );
            }
            Err(e) => {
                println!("      <samples unavailable: {e}>");
            }
        }
    }
    println!("---");
    println!("rows  : {total_rows}");
    println!("samps : {total_samples}");
    ExitCode::SUCCESS
}

struct Fingerprint {
    min: f64,
    max: f64,
    mean: f64,
}

fn fingerprint(s: &Samples) -> Fingerprint {
    let v = s.to_f64();
    if v.is_empty() {
        return Fingerprint {
            min: 0.0,
            max: 0.0,
            mean: 0.0,
        };
    }
    let mut min = f64::INFINITY;
    let mut max = f64::NEG_INFINITY;
    let mut sum = 0.0f64;
    for &x in &v {
        if x < min {
            min = x;
        }
        if x > max {
            max = x;
        }
        sum += x;
    }
    Fingerprint {
        min,
        max,
        mean: sum / v.len() as f64,
    }
}