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
use rinex::prelude::*;
use rinex::{geodetic, wgs84};
use rinex_qc_traits::HtmlReport;

use horrorshow::{box_html, RenderBox};

// #[cfg(feature = "serde")]
// use std::str::FromStr;

#[cfg(feature = "serde")]
use serde::{
    //de::Error,
    //Serializer,
    Deserialize,
    //Deserializer,
    Serialize,
};

#[derive(Default, Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum CsStrategy {
    /// Study CS events and report them
    #[default]
    Study,
    /// Study CS events and repair them
    StudyAndRepair,
}

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Deserialize))]
pub struct ProcessingOpts {
    /// Cs analysis/reporting strategy
    pub cs: CsStrategy,
    /// Ionospheric variation tolerance
    pub iono_rate_tolerance: f64,
    pub iono_rate_tolerance_dt: Duration,
    /// Clock Drift Moving average window slot
    pub clock_drift_window: Duration,
    /// Increment of the elelavtion mask
    pub elev_mask_increment: f64,
}

impl Default for ProcessingOpts {
    fn default() -> Self {
        Self {
            cs: CsStrategy::default(),
            iono_rate_tolerance: 400.0E-2_f64,
            iono_rate_tolerance_dt: Duration::from_seconds(60.0_f64),
            clock_drift_window: Duration::from_seconds(600.0_f64),
            elev_mask_increment: 10.0_f64,
        }
    }
}

/// Qc Report classification method
#[derive(Default, Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
/// Classify the QC report by desired data set
pub enum QcClassification {
    /// Report per GNSS system
    #[default]
    GNSS,
    /// Report per SV
    SV,
    /// Report per Physics (Observable, Orbit..)
    Physics,
}

impl std::fmt::Display for QcClassification {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            QcClassification::GNSS => f.write_str("GNSS Constellations"),
            QcClassification::SV => f.write_str("Satellite Vehicles"),
            QcClassification::Physics => f.write_str("Physics"),
        }
    }
}

#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Deserialize))]
pub struct QcOpts {
    #[cfg_attr(feature = "serde", serde(default))]
    #[cfg_attr(docs, doc(cfg(feature = "processing")))]
    pub classification: QcClassification,
    /// Minimum SNR level to consider in our analysis.
    /// For example, this is used when determining whether
    /// an epoch is "complete" or not.
    #[cfg_attr(feature = "serde", serde(default))]
    pub min_snr_db: f64,
    /// Elevation mask
    pub elev_mask: Option<f64>,
    /// Min. duration tolerated, so it is not reported as a data gap.
    /// If None: dominant sample rate is prefered.
    pub gap_tolerance: Option<Duration>,
    /// Manually defined Ground position (ECEF)
    pub ground_position: Option<GroundPosition>,
    /// Window duration to be used, during RX clock drift analysis
    #[cfg_attr(feature = "serde", serde(default = "default_drift_window"))]
    pub clock_drift_window: Duration,
}

impl QcOpts {
    pub fn with_classification(&self, classification: QcClassification) -> Self {
        let mut s = self.clone();
        s.classification = classification;
        s
    }

    pub fn with_min_snr(&self, snr_db: f64) -> Self {
        let mut s = self.clone();
        s.min_snr_db = snr_db;
        s
    }

    pub fn with_ground_position_ecef(&self, pos: (f64, f64, f64)) -> Self {
        let mut s = self.clone();
        s.ground_position = Some(wgs84!(pos.0, pos.1, pos.2));
        s
    }

    pub fn with_ground_position_geo(&self, pos: (f64, f64, f64)) -> Self {
        let mut s = self.clone();
        s.ground_position = Some(geodetic!(pos.0, pos.1, pos.2));
        s
    }
}

fn default_drift_window() -> Duration {
    Duration::from_seconds(3600.0)
}

impl Default for QcOpts {
    fn default() -> Self {
        Self {
            gap_tolerance: None,
            ground_position: None,
            min_snr_db: 20.0, // dB
            elev_mask: None,
            classification: QcClassification::default(),
            clock_drift_window: default_drift_window(),
        }
    }
}

impl HtmlReport for QcOpts {
    fn to_html(&self) -> String {
        panic!("qcopts cannot be rendered on its own")
    }
    fn to_inline_html(&self) -> Box<dyn RenderBox + '_> {
        box_html! {
            tr {
                th {
                    : "Classification"
                }
                th {
                    : format!("{}", self.classification)
                }
            }
            tr {
                th {
                    : "Min. SNR"
                }
                td {
                    : format!("{} dB", self.min_snr_db)
                }
            }
            tr {
                th {
                    : "Elevation mask"
                }
                @ if let Some(mask) = self.elev_mask {
                    td {
                        : format!("{} °", mask)
                    }
                } else {
                    td {
                        : "None"
                    }
                }
            }
            tr {
                th {
                    : "Data gap"
                }
                @ if let Some(tol) = self.gap_tolerance {
                    td {
                        : format!("{} tolerance", tol)
                    }
                } else {
                    td {
                        : "No tolerance"
                    }
                }
            }
            tr {
                th {
                    : "Clock Drift Window"
                }
                td {
                    : self.clock_drift_window.to_string()
                }
            }
        }
    }
}

#[cfg(feature = "serde")]
#[cfg(test)]
mod test {
    use super::*;
    #[test]
    fn qc_opts_serdes() {
        let content = r#"
			{
				"classification": "GNSS"
			}"#;
        let _opts: QcOpts = serde_json::from_str(content).unwrap();

        let content = r#"
			{
				"classification": "SV"
			}"#;
        let _opts: QcOpts = serde_json::from_str(content).unwrap();

        /*let content = r#"
            {
                "statistics": {
                    "window": "10 seconds"
                }
            }"#;

        let opts: QcOpts = serde_json::from_str(content).unwrap();
        assert_eq!(opts.reporting, ReportingStrategy::PerSv);
        assert_eq!(opts.statistics, Some(StatisticsOpts {
            window: Slot::Duration(Duration::from_seconds(10.0)),
        }));
        assert!(opts.processing.is_none());

        let content = r#"
            {
                "statistics": {
                    "window": "10 %"
                }
            }"#;

        let opts: QcOpts = serde_json::from_str(content).unwrap();
        assert_eq!(opts.reporting, ReportingStrategy::PerSignal);
        assert_eq!(opts.statistics, Some(StatisticsOpts {
            window: Slot::Percentage(10.0_f64),
        }));
        assert!(opts.processing.is_none());
        */
    }
}