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
//! Observation Record specific header fields
use crate::{
epoch::epoch_decompose as epoch_decomposition,
hatanaka::CRINEX,
prelude::{Constellation, Epoch, FormattingError, Observable, TimeScale},
};
use std::{
collections::HashMap,
io::{BufWriter, Write},
};
use itertools::Itertools;
#[cfg(feature = "processing")]
use std::str::FromStr;
#[cfg(feature = "processing")]
use qc_traits::{FilterItem, MaskFilter, MaskOperand};
#[derive(Debug, Clone, Default, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct HeaderFields {
/// Optional CRINEX information
pub crinex: Option<CRINEX>,
/// [Epoch] of first observation. Following content should match.
/// Defines [TimeScale] of following content.
pub timeof_first_obs: Option<Epoch>,
/// [Epoch] of last observation. Following content should match.
/// Defines [TimeScale] of following content.
pub timeof_last_obs: Option<Epoch>,
/// Observables per constellation basis
pub codes: HashMap<Constellation, Vec<Observable>>,
/// True if local clock drift is compensated for
pub clock_offset_applied: bool,
/// Possible observation scaling, used in high precision
/// OBS RINEX (down to nano radians precision).
pub scaling: HashMap<(Constellation, Observable), u16>,
}
impl HeaderFields {
/// Formats [HeaderFields] into [BufWriter].
pub(crate) fn format<W: Write>(
&self,
w: &mut BufWriter<W>,
major: u8,
) -> Result<(), FormattingError> {
if let Some(t) = self.timeof_first_obs {
let (y, m, d, hh, mm, ss, ns) = epoch_decomposition(t);
writeln!(
w,
"{:6} {:5} {:5} {:5} {:5} {:4}.{:07} {:x} TIME OF FIRST OBS",
y,
m,
d,
hh,
mm,
ss,
ns / 100,
t.time_scale,
)?;
}
if let Some(t) = self.timeof_last_obs {
let (y, m, d, hh, mm, ss, ns) = epoch_decomposition(t);
writeln!(
w,
"{:6} {:5} {:5} {:5} {:5} {:4}.{:07} {:x} TIME OF LAST OBS",
y,
m,
d,
hh,
mm,
ss,
ns / 100,
t.time_scale,
)?;
}
match major {
1 | 2 => self.format_v1_observables(w)?,
_ => self.format_v3_observables(w)?,
}
//TODO scaling
//TODO DCBs
Ok(())
}
fn format_v1_observables<W: Write>(&self, w: &mut BufWriter<W>) -> Result<(), FormattingError> {
if let Some((_, observables)) = self.codes.iter().next() {
write!(w, "{:6}", observables.len())?;
let mut modulo = 0;
for (nth, observable) in observables.iter().enumerate() {
if nth > 0 && (nth % 9) == 0 {
write!(w, " ")?;
}
write!(w, " {}", observable)?;
if (nth % 9) == 8 {
writeln!(w, "# / TYPES OF OBSERV")?;
}
modulo = nth % 9;
}
if modulo != 7 {
writeln!(
w,
"{:>width$}",
"# / TYPES OF OBSERV",
width = 79 - 6 - (modulo + 1) * 6
)?;
}
}
Ok(())
}
fn format_v3_observables<W: Write>(&self, w: &mut BufWriter<W>) -> Result<(), FormattingError> {
for constell in self.codes.keys().sorted() {
let observables = self.codes.get(&constell).unwrap();
write!(w, "{:x}{:5}", constell, observables.len())?;
let mut modulo = 0;
for (nth, observable) in observables.iter().enumerate() {
if nth > 0 && (nth % 13) == 0 {
write!(w, " ")?;
}
write!(w, " {}", observable)?;
if (nth % 13) == 12 {
writeln!(w, " SYS / # / OBS TYPES")?;
}
modulo = nth % 13;
}
if modulo != 12 {
writeln!(
w,
"{:>width$}",
"SYS / # / OBS TYPES",
width = 79 - 6 - (modulo + 1) * 4
)?;
}
}
Ok(())
}
/// Add "TIME OF FIRST OBS" field
pub(crate) fn with_crinex(&self, c: CRINEX) -> Self {
let mut s = self.clone();
s.crinex = Some(c);
s
}
/// Add "TIME OF FIRST OBS" field
pub(crate) fn with_timeof_first_obs(&self, epoch: Epoch) -> Self {
let mut s = self.clone();
s.timeof_first_obs = Some(epoch);
s
}
/// Add "TIME OF LAST OBS" field
pub(crate) fn with_timeof_last_obs(&self, epoch: Epoch) -> Self {
let mut s = self.clone();
s.timeof_last_obs = Some(epoch);
s
}
/// Insert a data scaling
pub(crate) fn with_scaling(&mut self, c: Constellation, observable: Observable, scaling: u16) {
self.scaling.insert((c, observable.clone()), scaling);
}
// /// Returns given scaling to apply for given GNSS system
// /// and given observation. Returns 1.0 by default, so it always applies
// pub(crate) fn scaling(&self, c: Constellation, observable: Observable) -> Option<&u16> {
// self.scaling.get(&(c, observable))
// }
}
impl HeaderFields {
/// Timescale helper
pub(crate) fn timescale(&self) -> TimeScale {
match self.timeof_first_obs {
Some(ts) => ts.time_scale,
None => match self.timeof_last_obs {
Some(ts) => ts.time_scale,
None => TimeScale::GPST,
},
}
}
}
#[cfg(feature = "processing")]
impl HeaderFields {
/// Modifies in place Self, when applying preprocessing filter ops
pub(crate) fn mask_mut(&mut self, f: &MaskFilter) {
match f.operand {
MaskOperand::Equals => match &f.item {
FilterItem::EpochItem(epoch) => {
let ts = self.timescale();
self.timeof_first_obs = Some(epoch.to_time_scale(ts));
self.timeof_last_obs = Some(epoch.to_time_scale(ts));
},
FilterItem::SvItem(svs) => {
let constells = svs
.iter()
.map(|sv| sv.constellation)
.unique()
.collect::<Vec<_>>();
self.codes.retain(|c, _| constells.contains(&c));
self.scaling.retain(|(c, _), _| constells.contains(&c));
},
FilterItem::ComplexItem(complex) => {
// try to interprate as [Observable]
let observables = complex
.iter()
.filter_map(|f| {
if let Ok(ob) = Observable::from_str(f) {
Some(ob)
} else {
None
}
})
.collect::<Vec<_>>();
if observables.len() > 0 {
self.codes.retain(|_, obs| {
obs.retain(|ob| observables.contains(&ob));
!obs.is_empty()
});
self.scaling.retain(|(_, c), _| !observables.contains(c));
}
},
FilterItem::ConstellationItem(constells) => {
self.codes.retain(|c, _| constells.contains(&c));
self.scaling.retain(|(c, _), _| constells.contains(&c));
},
_ => {},
},
MaskOperand::NotEquals => match &f.item {
FilterItem::SvItem(svs) => {
let constells = svs
.iter()
.map(|sv| sv.constellation)
.unique()
.collect::<Vec<_>>();
self.codes.retain(|c, _| !constells.contains(&c));
self.scaling.retain(|(c, _), _| !constells.contains(&c));
},
FilterItem::ConstellationItem(constells) => {
self.codes.retain(|c, _| !constells.contains(&c));
self.scaling.retain(|(c, _), _| !constells.contains(&c));
},
FilterItem::ComplexItem(complex) => {
// try to interprate as [Observable]
let observables = complex
.iter()
.filter_map(|f| {
if let Ok(ob) = Observable::from_str(f) {
Some(ob)
} else {
None
}
})
.collect::<Vec<_>>();
if observables.len() > 0 {
self.codes.retain(|_, obs| {
obs.retain(|ob| observables.contains(&ob));
!obs.is_empty()
});
self.scaling.retain(|(_, c), _| !observables.contains(c));
}
},
_ => {},
},
MaskOperand::GreaterThan => match &f.item {
FilterItem::EpochItem(epoch) => {
let ts = self.timescale();
if let Some(t) = self.timeof_first_obs {
if t < *epoch {
self.timeof_first_obs = Some(epoch.to_time_scale(ts));
}
} else {
self.timeof_first_obs = Some(epoch.to_time_scale(ts));
}
},
_ => {},
},
MaskOperand::GreaterEquals => match &f.item {
FilterItem::EpochItem(epoch) => {
let ts = self.timescale();
if let Some(t_first) = self.timeof_first_obs {
if t_first < *epoch {
self.timeof_first_obs = Some(epoch.to_time_scale(ts));
}
} else {
self.timeof_first_obs = Some(epoch.to_time_scale(ts));
}
},
_ => {},
},
MaskOperand::LowerThan => match &f.item {
FilterItem::EpochItem(epoch) => {
let ts = self.timescale();
if let Some(t_last) = self.timeof_last_obs {
if t_last > *epoch {
self.timeof_last_obs = Some(epoch.to_time_scale(ts));
}
} else {
self.timeof_last_obs = Some(*epoch);
}
},
_ => {},
},
MaskOperand::LowerEquals => match &f.item {
FilterItem::EpochItem(epoch) => {
let ts = self.timescale();
if let Some(t_last) = self.timeof_last_obs {
if t_last > *epoch {
self.timeof_last_obs = Some(epoch.to_time_scale(ts));
}
} else {
self.timeof_last_obs = Some(epoch.to_time_scale(ts));
}
},
_ => {},
},
}
}
}