1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634
use horrorshow::{box_html, RenderBox};
use itertools::Itertools;
use std::collections::HashMap;
use std::str::FromStr;
extern crate gnss_rs as gnss;
use gnss::prelude::SV;
use crate::QcOpts;
//use rinex::carrier;
use rinex::carrier::Carrier;
use rinex::observation::SNR;
use rinex::prelude::{Epoch, EpochFlag, Observable, Rinex};
use rinex::preprocessing::Derivative;
use rinex_qc_traits::HtmlReport;
use statrs::statistics::Statistics;
/*
* GNSS signal special formatting
*/
fn report_signals(list: &Vec<Carrier>) -> String {
let mut s = String::with_capacity(3 * list.len());
for index in 0..list.len() - 1 {
s.push_str(&format!(
"{} ({:.3} MHz), ",
list[index],
list[index].frequency_mhz()
));
}
s.push_str(&format!(
"{} ({:.3} MHz)",
list[list.len() - 1],
list[list.len() - 1].frequency_mhz()
));
s
}
/*
* Report RX Clock drift analysis
*/
fn report_clock_drift(data: &Vec<(Epoch, f64)>) -> Box<dyn RenderBox + '_> {
box_html! {
@ if data.is_empty() {
table(class="table is-bordered") {
tr {
th {
: "Unfeasible"
}
td {
: "Missing Data"
}
}
}
} else {
table(class="table is-bordered") {
tr {
th {
: "Epoch"
}
th {
: "Mean Clock drift [s/s]"
}
}
//@ for (epoch, drift) in data {
// tr {
// td {
// : epoch.to_string()
// }
// td {
// : format!("{:e}", drift)
// }
// }
//}
}
}
}
}
/*
* Epoch anomalies formatter
*/
fn report_anomalies<'a>(
cs: &'a Vec<Epoch>,
power: &'a Vec<Epoch>,
other: &'a Vec<(Epoch, EpochFlag)>,
) -> Box<dyn RenderBox + 'a> {
box_html! {
tr {
th {
: "Power Failures"
}
@ if power.is_empty() {
td {
: "None"
}
} else {
td {
: format!("{:?}", power)
}
tr {
th {
: "Longest"
}
td {
//: power.iter().max_by(|(_, d1), (_, d2)| d1.cmp(d2)).unwrap().to_string()
: "TODO"
}
td {
: "Average Duration"
}
td {
: "TODO"
}
}
}
}
tr {
th {
: "Cycle slips"
}
@ if cs.is_empty() {
td {
: "None"
}
} else {
td {
: format!("{:?}", cs)
}
}
}
tr {
th {
: "Other anomalies"
}
@ if other.is_empty() {
td {
: "None"
}
} else {
td {
: "Epoch"
}
td {
: "Event"
}
@ for (e, event) in other {
td {
: ""
}
td {
: format!("{}", e)
}
@ if *event == EpochFlag::AntennaBeingMoved {
td {
: "Antenna Being Moved"
}
} else if *event == EpochFlag::NewSiteOccupation {
td {
: "New Site Occupation"
}
} else if *event == EpochFlag::ExternalEvent {
td {
: "External Event"
}
} else {
td {
: "Other"
}
}
}
}
}
}
}
/*
* Epoch Epoch completion,
* defined as at least 1 SV with PR + PH observed on both L1 and
* "rhs" signal,
* also SNR condition for both signals above current mask
*/
fn report_epoch_completion(
total: usize,
total_with_obs: usize,
complete: &HashMap<(SV, Carrier), usize>,
) -> Box<dyn RenderBox + '_> {
box_html! {
table(class="table is-bordered") {
tr {
th {
: "Total#"
}
td {
: total.to_string()
}
}
tr {
th {
: "w/ observations"
}
td {
: format!("{} ({}%)", total_with_obs, total_with_obs * 100 / total)
}
}
tr {
td {
b {
: "Complete"
}
p {
: "Epochs with at least Phase + PR"
}
p {
: "in dual frequency, with"
}
p {
: "both SNR and elev above masks"
}
}
td {
@ for mut chunk in &complete.iter().chunks(8) {
p {
@ while let Some(((sv, carrier), count)) = chunk.next() {
p {
: format!("{:X} {}/L1 | {} ({}%)", sv, carrier, count, count * 100 / total)
}
}
}
}
}
}
}
}
}
/*
* SNR analysis report
*/
fn report_snr_statistics(
snr_stats: &HashMap<Observable, ((Epoch, f64), (Epoch, f64))>,
) -> Box<dyn RenderBox + '_> {
box_html! {
tr {
td {
: ""
}
@ for (observable, _) in snr_stats {
@ if observable.is_phase_observable() || observable.is_pseudorange_observable() {
td {
: observable.to_string()
}
}
}
}
tr {
th {
: "Best"
}
@ for (observable, (_, max)) in snr_stats {
@ if observable.is_phase_observable() || observable.is_pseudorange_observable() {
td {
b {
: format!("{:e}", SNR::from_str(&format!("{}", max.1)).unwrap())
}
p {
: format!("@{}", max.0)
}
}
}
}
}
tr {
th {
: "Worst"
}
@ for (observable, (min, _)) in snr_stats {
@ if observable.is_phase_observable() || observable.is_pseudorange_observable() {
td {
b {
: format!("{:e}", SNR::from_str(&format!("{}", min.1)).unwrap())
}
p {
: format!("@{}", min.0)
}
}
}
}
}
}
}
/*
* Reports statistical analysis results for SSx observations
*/
fn report_ssi_statistics(ssi_stats: &HashMap<Observable, (f64, f64)>) -> Box<dyn RenderBox + '_> {
box_html! {
table(class="table is-bordered") {
thead {
tr {
td {
: ""
}
@ for (signal, _) in ssi_stats {
th {
: signal.to_string()
}
}
}
}
tbody {
tr {
th {
: "Mean"
}
@ for (_, (mean, _)) in ssi_stats {
td {
: format!("{:.3} dB", mean)
}
}
}
tr {
th {
: "Deviation" // (σ)"
}
@ for (_, (_, std)) in ssi_stats {
td {
: format!("{:.3} dB", std)
}
}
}
}
}
}
}
#[derive(Debug, Clone)]
/// OBS RINEX specific QC analysis.
/// Full OBS RINEX analysis requires both the "obs" and "processing" features.
pub struct QcObsAnalysis {
/// Identified Observables
observables: Vec<String>,
/// Identified Signals
signals: Vec<Carrier>,
/// Codes that were idenfitied
codes: Vec<String>,
/// true if doppler observations were identified
has_doppler: bool,
/// CS anomalies
cs_anomalies: Vec<Epoch>,
/// Epochs where power failures took place, and their duration
power_failures: Vec<Epoch>,
/// Other abnormal events, by chronological epochs
other_anomalies: Vec<(Epoch, EpochFlag)>,
/// Total number of epochs identified
total_epochs: usize,
/// Epochs with at least 1 observation
total_with_obs: usize,
/// Complete epoch counter with respect to given signal (other than L1) per SV
complete_epochs: HashMap<(SV, Carrier), usize>,
/// Min. & Max. SNR (signal @ epoch)
snr_stats: HashMap<Observable, ((Epoch, f64), (Epoch, f64))>,
/// SSI statistical analysis (mean, stddev)
ssi_stats: HashMap<Observable, (f64, f64)>,
/// RX clock drift
clock_drift: Vec<(Epoch, f64)>,
}
impl QcObsAnalysis {
pub fn new(rnx: &Rinex, opts: &QcOpts) -> Self {
let doppler_obs = rnx.observable().filter(|obs| obs.is_doppler_observable());
let mut observables: Vec<String> = rnx.observable().map(|obs| obs.to_string()).collect();
let mut signals: Vec<_> = rnx.carrier().unique().collect();
let mut codes: Vec<_> = rnx.code().map(|c| c.to_string()).collect();
let cs_anomalies: Vec<_> = rnx
.epoch_anomalies()
.filter_map(|(e, flag)| {
if flag == EpochFlag::CycleSlip {
Some(e)
} else {
None
}
})
.collect();
let power_failures: Vec<_> = rnx
.epoch_anomalies()
.filter_map(|(e, flag)| {
if flag == EpochFlag::PowerFailure {
Some(e)
} else {
None
}
})
.collect();
let other_anomalies: Vec<_> = rnx
.epoch_anomalies()
.filter_map(|(e, flag)| {
if flag != EpochFlag::PowerFailure && flag != EpochFlag::CycleSlip {
Some((e, flag))
} else {
None
}
})
.collect();
let mut total_epochs = rnx.epoch().count();
let mut complete_epochs: HashMap<(SV, Carrier), usize> = HashMap::new();
for (_, complete) in rnx.complete_epoch(Some(SNR::from(opts.min_snr_db))) {
for (sv, carrier) in complete {
if let Some(counter) = complete_epochs.get_mut(&(sv, carrier)) {
*counter += 1;
} else {
complete_epochs.insert((sv, carrier), 1);
}
}
}
let mut epoch_with_obs: Vec<Epoch> = Vec::new();
if let Some(r) = rnx.record.as_obs() {
total_epochs = r.len();
for ((epoch, _flag), (_clk, svs)) in r {
for (_sv, observables) in svs {
if !observables.is_empty() && !epoch_with_obs.contains(epoch) {
epoch_with_obs.push(*epoch);
}
}
}
}
// append ssi: drop vehicle differentiation
let mut ssi: HashMap<Observable, Vec<f64>> = HashMap::new();
for (_, _, obs, value) in rnx.ssi() {
if let Some(values) = ssi.get_mut(obs) {
values.push(value);
} else {
ssi.insert(obs.clone(), vec![value]);
}
}
/*
* SSI statistical analysis: {mean, stddev,}
* per signal: we do not differentiate vehicles
*/
let ssi_stats: HashMap<Observable, (f64, f64)> = ssi
.iter()
.map(|(obs, values)| (obs.clone(), (values.mean(), values.std_dev())))
.collect();
// append snr: drop vehicle differentiation
let mut snr: HashMap<Observable, Vec<(Epoch, f64)>> = HashMap::new();
for ((e, _), _, obs, snr_value) in rnx.snr() {
let snr_f64: f64 = (snr_value as u8).into();
if let Some(values) = snr.get_mut(obs) {
values.push((e, snr_f64));
} else {
snr.insert(obs.clone(), vec![(e, snr_f64)]);
}
}
/*
* SNR analysis: {min, max}
* per signal: we do not differentiate vehicles
*/
let mut snr_stats: HashMap<Observable, ((Epoch, f64), (Epoch, f64))> = HashMap::new();
for (obs, data) in snr {
let values: Vec<f64> = data.iter().map(|(_e, value)| *value).collect();
let min = values.clone().min();
let epoch_min = data.iter().find(|(_e, value)| *value == min).unwrap().0;
let max = values.clone().max();
let epoch_max = data.iter().find(|(_e, value)| *value == max).unwrap().0;
snr_stats.insert(obs, ((epoch_min, min), (epoch_max, max)));
}
/*
* sort, prior reporting
*/
codes.sort();
observables.sort();
signals.sort();
//complete_epochs.sort();
Self {
codes,
signals,
observables,
has_doppler: doppler_obs.count() > 0,
cs_anomalies,
power_failures,
other_anomalies,
total_epochs,
total_with_obs: epoch_with_obs.len(),
complete_epochs,
snr_stats,
ssi_stats,
clock_drift: {
let rx_clock: Vec<_> = rnx
.recvr_clock()
.map(|((e, _flag), value)| (e, value))
.collect();
let der = Derivative::new(1);
let rx_clock_drift: Vec<(Epoch, f64)> = der.eval(rx_clock);
//TODO
//let mov = Averager::mov(opts.clock_drift_window);
//mov.eval(rx_clock_drift)
rx_clock_drift
},
}
}
}
impl HtmlReport for QcObsAnalysis {
fn to_html(&self) -> String {
unreachable!("never used by itself")
}
fn to_inline_html(&self) -> Box<dyn RenderBox + '_> {
box_html! {
tr {
th {
: "Signals"
}
td {
: report_signals(&self.signals)
}
}
tr {
th {
: "Codes"
}
td {
@ for mut chunks in &self.codes.iter().chunks(12) {
p {
@ while let Some(code) = chunks.next() {
p {
: format!("{}, ", code)
}
}
}
}
}
}
tr {
th {
: "Observables"
}
td {
@ for mut chunks in &self.observables.iter().chunks(12) {
p {
@ while let Some(observable) = chunks.next() {
p {
: format!("{}, ", observable)
}
}
}
}
}
}
tr {
th {
: "Has Doppler"
}
@ if self.has_doppler {
td {
: "True"
}
} else {
td {
: "False"
}
}
}
tr {
table(class="table is-bordered") {
thead {
th {
: "Anomalies"
}
}
tbody {
: report_anomalies(&self.cs_anomalies, &self.power_failures, &self.other_anomalies)
}
}
}
tr {
table(class="table is-bordered") {
thead {
th {
: "Epochs"
}
}
tbody {
: report_epoch_completion(self.total_epochs, self.total_with_obs, &self.complete_epochs)
}
}
}
tr {
table(class="table is-bordered") {
thead {
th {
: "SNR"
}
}
tbody {
: report_snr_statistics(&self.snr_stats)
}
}
}
tr {
table(class="table is-bordered") {
thead {
th {
: "SSI"
}
}
tbody {
: report_ssi_statistics(&self.ssi_stats)
}
}
}
tr {
table(class="table is-bordered") {
thead {
th {
: "(RX) Clock Drift"
}
}
tbody {
: report_clock_drift(&self.clock_drift)
}
}
}
}
}
}