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
use std::io::{BufRead, BufReader, Read};
use crate::{
epoch::parse_in_timescale as parse_epoch_in_timescale,
error::ParsingError,
prelude::{
ClockOffset, Duration, Epoch, EpochFlag, GroundStation, Header, Key, Matcher, Measurements,
Observable, Observation, Record, TimeScale, SNR,
},
};
#[cfg(feature = "log")]
use log::{debug, error};
impl Record {
/// Parses the DORIS [Record] content by consuming the [Reader] until the end of stream.
/// This requires reference to previously parsed [Header] section.
pub fn parse<R: Read>(
header: &mut Header,
reader: &mut BufReader<R>,
) -> Result<Self, ParsingError> {
const EPOCH_SIZE: usize = "YYYY MM DD HH MM SS.NNNNNNNNN 0".len();
const CLOCK_OFFSET: usize = 38;
const CLOCK_SIZE: usize = 19;
const MIN_EPOCH_SIZE: usize = EPOCH_SIZE + CLOCK_SIZE + 2;
const OBSERVABLE_WIDTH: usize = 14;
// eos reached: process pending buffer & exit
let mut eos = false;
// current line storage
let mut buf_len = 0;
let mut line_buf = String::with_capacity(128);
// epoch storage
let mut epoch_buf = String::with_capacity(1024);
let mut record = Record::default();
let observables = &header.observables;
let nb_observables = observables.len();
// Iterate and consume, one line at a time
while let Ok(size) = reader.read_line(&mut line_buf) {
if size == 0 {
// reached EOS: consume buffer & exit
eos |= true;
}
let line_len = line_buf.len();
if line_len > 60 {
if line_buf.contains("COMMENT") {
// Comments are stored as is
let comment = line_buf.split_at(60).0.trim_end();
record.comments.push(comment.to_string());
line_buf.clear();
continue; // skip parsing
}
}
// tries to assemble a complete epoch
let mut new_epoch = false;
// new epoch
if line_buf.starts_with('>') || eos {
new_epoch = true;
let mut obs_ptr = 0;
let mut epoch = Epoch::default();
let flag = EpochFlag::default();
let mut station = Option::<&GroundStation>::None;
let mut clock_offset = Option::<ClockOffset>::None;
for (nth, line) in epoch_buf.lines().enumerate() {
let line_len = line.len();
if nth == 0 {
// parse date & time
if line_len < MIN_EPOCH_SIZE {
continue;
}
epoch = parse_epoch_in_timescale(&line[2..2 + EPOCH_SIZE], TimeScale::TAI)?;
let mut measurement = Measurements::default();
// parse clock offset, if any
if line_len >= CLOCK_OFFSET + CLOCK_SIZE {
let clock_offset_secs = &line[CLOCK_OFFSET..CLOCK_OFFSET + CLOCK_SIZE]
.trim()
.parse::<f64>()
.map_err(|_| ParsingError::ClockOffset)?;
let dt = Duration::from_seconds(*clock_offset_secs);
clock_offset = Some(ClockOffset::from_measured_offset(dt));
// clock extrapolation flag
if line_len > CLOCK_OFFSET + CLOCK_SIZE {
if line[CLOCK_OFFSET + CLOCK_SIZE..].trim().eq("1") {
if let Some(clock_offset) = &mut clock_offset {
clock_offset.extrapolated = true;
}
}
}
measurement.satellite_clock_offset = clock_offset;
}
} else {
if line.starts_with("D") {
// new station starting
obs_ptr = 0;
// station identification
let station_id = line[1..3]
.trim()
.parse::<u16>()
.map_err(|_| ParsingError::StationFormat)?;
let matcher = Matcher::ID(station_id);
// identification
if let Some(matching) = header
.ground_stations
.iter()
.filter(|station| station.matches(&matcher))
.reduce(|k, _| k)
{
station = Some(matching);
} else {
#[cfg(feature = "log")]
debug!("unidentified station: #{:02}", station_id);
}
}
// station must be identified
if let Some(station) = station {
// identified
let key = Key { epoch, flag };
let mut offset = 3;
loop {
if offset + OBSERVABLE_WIDTH + 1 < line_len {
let slice = &line[offset..offset + OBSERVABLE_WIDTH];
match slice.trim().parse::<f64>() {
Ok(mut value) => {
let mut observation = Observation::default();
if observables[obs_ptr] == Observable::FrequencyRatio {
value *= 1.0E-11;
}
observation.value = value;
if let Some(measurements) =
record.measurements.get_mut(&key)
{
measurements.add_observation(
station.clone(),
observables[obs_ptr],
observation,
);
} else {
let mut measurements = Measurements::default();
measurements.add_observation(
station.clone(),
observables[obs_ptr],
observation,
);
measurements.satellite_clock_offset = clock_offset;
record
.measurements
.insert(key.clone(), measurements);
}
},
#[cfg(feature = "log")]
Err(e) => {
error!("observation parsing error: {}", e);
},
#[cfg(not(feature = "log"))]
Err(_) => {},
}
}
offset += OBSERVABLE_WIDTH;
if offset + 1 < line_len {
let slice = &line[offset..offset + 1];
// println!("slice \"{}\"", slice);
if let Ok(snr) = slice.trim().parse::<SNR>() {
if let Some(measurements) =
record.measurements.get_mut(&key)
{
// if let Some(observation) = measurements
// .observations
// .get_mut(&observables[obs_ptr])
// {
// observation.snr = Some(snr);
// }
}
}
}
offset += 1;
if offset + 1 < line_len {
// let slice = &line[offset..offset + 1];
// println!("slice \"{}\"", slice);
// if let Ok(flag) = slice.trim().parse::<Flag>() {
// if let Some(measurements) =
// record.measurements.get_mut(&key)
// {
// if let Some(observation) = measurements
// .observations
// .get_mut(&observables[obs_ptr])
// {
// observation.phase_flag = Some(flag);
// }
// }
// }
}
offset += 1;
obs_ptr += 1;
if offset >= line_len {
break;
}
// detect potential errors
if obs_ptr >= nb_observables {
break;
}
}
}
}
} // epoch parsing
} // new epoch
// clear on new epoch detection
if new_epoch {
buf_len = 0;
epoch_buf.clear();
}
// always stack new content
epoch_buf.push_str(&line_buf);
buf_len += size;
line_buf.clear(); // always clear newline buf
if eos {
break;
}
} //while
Ok(record)
}
}