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
//! Observation record plotting
use rinex::{
*,
prelude::*,
observation::*,
};
use super::{
Context, Plot2d,
build_chart, build_plot,
};
use plotters::{
prelude::*,
coord::Shift,
chart::ChartState,
};
use std::collections::HashMap;
macro_rules! code2physics {
($code: expr) => {
if is_phase_carrier_obs_code!($code) {
"Phase".to_string()
} else if is_doppler_obs_code!($code) {
"Doppler".to_string()
} else if is_sig_strength_obs_code!($code) {
"Signal Strength".to_string()
} else {
"Pseudo Range".to_string()
}
}
}
/*
* Builds a plot context for Observation RINEX specificly
*/
pub fn build_context<'a> (dim: (u32, u32), record: &Record) -> Context<'a> {
let mut e0: f64 = 0.0;
let mut t_axis: Vec<f64> = Vec::with_capacity(16384);
let mut plots: HashMap<String,
DrawingArea<BitMapBackend, Shift>>
= HashMap::with_capacity(4);
let mut y_ranges: HashMap<String, (f64,f64)> = HashMap::new();
let mut charts: HashMap<String, ChartState<Plot2d>> = HashMap::new();
// => 1 plot per physics (ie., Observable)
// 1 plot in case clock offsets were provided
for (e_index, ((e, _flag), (clk_offset, vehicules))) in record.iter().enumerate() {
if e_index == 0 {
// store first epoch timestamp
// to scale x_axis proplery (avoids fuzzy rendering)
e0 = e.to_utc_seconds();
}
let t = e.to_utc_seconds() - e0;
t_axis.push(t);
// Build 1 plot in case Receiver Clock Offsets were provided
// Associate 1 chart to each plot, for classical 2D x,y plot
// Grab y range
if let Some(clk_offset) = clk_offset {
let title = "clock-offset.png";
plots.insert(
title.to_string(),
build_plot(title, dim));
if let Some((min,max)) = y_ranges.get_mut(title) {
if clk_offset < min {
*min = *clk_offset;
}
if clk_offset > max {
*max = *clk_offset;
}
} else {
y_ranges.insert("Clock Offset".to_string(),
(*clk_offset,*clk_offset));
}
}
// Build 1 plot per type of observation
// Associate 1 chart to each plot, for classical
//
// Color space: one color per vehicule
// identified by PRN#
for (_, observations) in vehicules {
for (observation, data) in observations {
if is_phase_carrier_obs_code!(observation) {
let file = "phase.png";
if plots.get(file).is_none() {
let plot = build_plot(file, dim);
plots.insert(file.to_string(), plot);
}
let y = data.obs;
if let Some((min,max)) = y_ranges.get_mut("Phase") {
if y < *min {
*min = y;
}
if y > *max {
*max = y;
}
} else {
y_ranges.insert("Phase".to_string(), (y,y));
}
} else if is_doppler_obs_code!(observation) {
let file = "doppler.png";
if plots.get(file).is_none() {
let plot = build_plot(file, dim);
plots.insert(file.to_string(), plot);
}
if let Some((min,max)) = y_ranges.get_mut("Doppler") {
if data.obs < *min {
*min = data.obs;
}
if data.obs > *max {
*max = data.obs;
}
} else {
y_ranges.insert("Doppler".to_string(),
(data.obs,data.obs));
}
} else if is_pseudo_range_obs_code!(observation) {
let file = "pseudo-range.png";
if plots.get(file).is_none() {
let plot = build_plot(file, dim);
plots.insert(file.to_string(), plot);
}
if let Some((min,max)) = y_ranges.get_mut("Pseudo Range") {
if data.obs < *min {
*min = data.obs;
}
if data.obs > *max {
*max = data.obs;
}
} else {
y_ranges.insert("Pseudo Range".to_string(),
(data.obs,data.obs));
}
} else if is_sig_strength_obs_code!(observation) {
let file = "ssi.png";
if plots.get(file).is_none() {
let plot = build_plot(file, dim);
plots.insert(file.to_string(), plot);
}
if let Some((min,max)) = y_ranges.get_mut("Signal Strength") {
if data.obs < *min {
*min = data.obs;
}
if data.obs > *max {
*max = data.obs;
}
} else {
y_ranges.insert("Signal Strength".to_string(),
(data.obs,data.obs));
}
}
}
}
}
// Add 1 chart onto each plot
for (title, plot) in plots.iter() {
let chart_id = match title.as_str() {
"phase.png" => "Phase",
"doppler.png" => "Doppler",
"pseudo-range.png" => "Pseudo Range",
"ssi.png" => "Signal Strength",
"clock-offset.png" => "Clock Offset",
_ => continue,
};
// scale this chart nicely
let range = y_ranges.get(chart_id)
.unwrap();
let chart = build_chart(chart_id, t_axis.clone(), *range, plot);
charts.insert(chart_id.to_string(), chart);
}
Context {
plots,
charts,
t_axis,
}
}
pub fn plot(ctx: &mut Context, record: &Record, _nav_ctx: Option<Rinex>) {
let mut e0: f64 = 0.0;
let cmap = colorous::TURBO; // to differentiate vehicules (PRN#)
let symbols = vec!["x", "t", "o"]; // to differentiate carrier signals
// sorted by Physics, By Carrier number, By vehicule, (x,y)
let mut clk_offset: Vec<(f64,f64)> = Vec::new();
// dataset
// per physics, per carrier signal (symbol)
// per vehicule (color map)
// x: sampling timestamp,
// y: observation (raw),
// bool: loss of lock - CS emphasis
// optionnal(f64): Sv elevation angle, if NAV is provided
let mut dataset: HashMap<String, HashMap<u8, HashMap<Sv, Vec<(bool,f64,f64)>>>> = HashMap::new();
for (e_index, ((epoch, _flag), (clock_offset, vehicules))) in record.iter().enumerate() {
if e_index == 0 {
e0 = epoch.to_utc_seconds();
}
let e = epoch.to_utc_seconds();
let x = e-e0;
if let Some(value) = clock_offset {
clk_offset.push((x, *value));
}
for (sv, observations) in vehicules {
for (observation, data) in observations {
let p_code = &observation[0..1];
let c_code = &observation[1..2]; // carrier code
let c_code = u8::from_str_radix(c_code, 10)
.expect("failed to parse carrier code");
let physics = code2physics!(p_code);
let y = data.obs;
let cycle_slip = match data.lli {
Some(lli) => lli.intersects(LliFlags::LOCK_LOSS),
_ => false,
};
if let Some(data) = dataset.get_mut(&physics) {
if let Some(data) = data.get_mut(&c_code) {
if let Some(data) = data.get_mut(&sv) {
data.push((cycle_slip, x, y));
} else {
data.insert(*sv, vec![(cycle_slip, x, y)]);
}
} else {
let mut map: HashMap<Sv, Vec<(bool,f64,f64)>> = HashMap::new();
map.insert(*sv, vec![(cycle_slip, x, y)]);
data.insert(c_code, map);
}
} else {
let mut map: HashMap<Sv, Vec<(bool,f64,f64)>> = HashMap::new();
map.insert(*sv, vec![(cycle_slip, x, y)]);
let mut mmap: HashMap<u8, HashMap<Sv, Vec<(bool,f64,f64)>>> = HashMap::new();
mmap.insert(c_code, map);
dataset.insert(physics.to_string(), mmap);
}
}
}
}
if let Some(plot) = ctx.plots.get("clock-offset.png") {
let mut chart = ctx.charts.get("Clock Offset")
.expect("faulty plot context: no chart defined for clock offsets")
.clone()
.restore(plot);
chart.draw_series(
clk_offset.iter()
.map(|point| {
TriangleMarker::new(*point, 4,
Into::<ShapeStyle>::into(&BLACK).filled())
.into_dyn()
}))
.expect("failed to plot receiver clock offsets");
chart
.draw_series(LineSeries::new(
clk_offset.iter()
.map(|point| *point),
&BLACK,
))
.expect("failed to plot receiver clock offsets")
.label("Offset [s]")
.legend(move |point| {
TriangleMarker::new(point, 4,
Into::<ShapeStyle>::into(&BLACK).filled())
.into_dyn()
});
chart
.configure_series_labels()
.border_style(&BLACK)
.background_style(WHITE.filled())
.draw()
.expect("failed to draw clock offset labels");
}
/*
* 1 plot per physics
*/
for (physics, carriers) in dataset {
// retrieve dedicated plot
let plot_title: &str = match physics.as_str() {
"Phase" => "phase.png",
"Doppler" => "doppler.png",
"Signal Strength" => "ssi.png",
"Pseudo Range" => "pseudo-range.png",
_ => unreachable!(),
};
let plot = ctx.plots.get(plot_title)
.expect(&format!("faulty context: missing plot for \"{}\" data", physics));
// retrieve associated chart
let mut chart = ctx.charts
.get(&physics)
.expect(&format!("faulty context: missing \"{}\" chart", physics))
.clone()
.restore(plot);
/*
* plot for this kind of data
*/
for (carrier, vehicules) in carriers {
let symbol = symbols[carrier as usize % symbols.len()]; // one symbol per carrier
for (sv, data) in vehicules {
// retrieve color from color map
let color = cmap.eval_rational(sv.prn.into(), 50);
let color = RGBColor { 0: color.r, 1: color.g, 2: color.b };
// plot
/* chart.draw_series(LineSeries::new(
data.iter()
.map(|(_,x,y)| (*x,*y)),
color.clone()))
.expect(&format!("failed to draw {} data", physics)); */
chart.draw_series(
data.iter()
.map(|(cycle_slip,x,y)| {
if *cycle_slip {
match symbol {
"x" => {
Cross::new((*x,*y), 5,
Into::<ShapeStyle>::into(&BLACK).filled().stroke_width(2))
.into_dyn()
},
"o" => {
Circle::new((*x,*y), 5,
Into::<ShapeStyle>::into(&BLACK).filled().stroke_width(2))
.into_dyn()
},
_ => {
TriangleMarker::new((*x,*y), 5,
Into::<ShapeStyle>::into(&BLACK).filled().stroke_width(2))
.into_dyn()
},
}
} else {
match symbol {
"x" => {
Cross::new((*x,*y), 4,
Into::<ShapeStyle>::into(&color).filled())
.into_dyn()
},
"o" => {
Circle::new((*x,*y), 4,
Into::<ShapeStyle>::into(&color).filled())
.into_dyn()
},
_ => {
TriangleMarker::new((*x,*y), 4,
Into::<ShapeStyle>::into(&color).filled())
.into_dyn()
},
}
}
}))
.expect(&format!("failed to draw {} observations", physics))
.label(format!("{}(L{})", sv, carrier))
.legend(move |point| {
match symbol {
"x" => {
Cross::new(point, 4,
Into::<ShapeStyle>::into(&color).filled())
.into_dyn()
},
"o" => {
Circle::new(point, 4,
Into::<ShapeStyle>::into(&color).filled())
.into_dyn()
},
_ => {
TriangleMarker::new(point, 4,
Into::<ShapeStyle>::into(&color).filled())
.into_dyn()
},
}
});
}
}
chart
.configure_series_labels()
.border_style(&BLACK)
.background_style(WHITE.filled())
.draw()
.expect(&format!("failed to draw labels on {} chart", physics));
}
}