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
//! Meteo sensor
use crate::prelude::{Observable, ParsingError};
#[cfg(feature = "nav")]
use anise::{
math::Vector6,
prelude::{Epoch, Frame, Orbit},
};
#[cfg(feature = "qc")]
use maud::{html, Markup, Render};
/// Meteo Observation Sensor
#[derive(Default, Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Sensor {
/// Physics measured by this sensor
pub observable: Observable,
/// Model of this sensor
pub model: Option<String>,
/// Type of sensor
pub sensor_type: Option<String>,
/// Sensor accuracy [°C,..]
pub accuracy: Option<f32>,
/// Posible sensor location (ECEF)
pub position: Option<(f64, f64, f64)>,
/// Possible sensor height eccentricity (m)
pub height: Option<f64>,
}
#[cfg(feature = "qc")]
impl Render for Sensor {
fn render(&self) -> Markup {
html! {
table class="table is-bordered" {
tr {
th { "Observable" }
td { (self.observable.to_string()) }
}
@if let Some(model) = &self.model {
tr {
th { "Model" }
td { (model) }
}
}
@if let Some(sensor) = &self.sensor_type {
tr {
th { "Sensor Type" }
td { (sensor) }
}
}
@if let Some(accuracy) = self.accuracy {
tr {
th { "Sensor Accuracy" }
td { (format!("{:.3E}", accuracy)) }
}
}
@if cfg!(feature = "nav") {
@if let Some((x_ecef_m, y_ecef_m, z_ecef_m)) = self.position {
tr {
th { "Sensor position (ECEF m)" }
td {
(format!("{:.3E}m", x_ecef_m))
}
td {
(format!("{:.3E}m", y_ecef_m))
}
td {
(format!("{:.3E}m", z_ecef_m))
}
}
}
@if let Some(h) = self.height {
tr {
th { "Height" }
td { (format!("{:.3} m", h)) }
}
}
}
}
}
}
}
impl std::str::FromStr for Sensor {
type Err = ParsingError;
fn from_str(content: &str) -> Result<Self, Self::Err> {
let (model, rem) = content.split_at(20);
let (s_type, rem) = rem.split_at(20 + 6);
let (accuracy, rem) = rem.split_at(7 + 4);
let (observable, _) = rem.split_at(2);
Ok(Self {
model: {
if !model.trim().is_empty() {
Some(model.trim().to_string())
} else {
None
}
},
sensor_type: {
if !s_type.trim().is_empty() {
Some(s_type.trim().to_string())
} else {
None
}
},
accuracy: {
if let Ok(f) = f32::from_str(accuracy.trim()) {
Some(f)
} else {
None
}
},
height: None,
position: None,
observable: Observable::from_str(observable.trim())?,
})
}
}
impl std::fmt::Display for Sensor {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
if let Some(model) = &self.model {
write!(f, "{:<width$}", model, width = 20)?
} else {
write!(f, "{:20}", "")?;
}
if let Some(stype) = &self.sensor_type {
write!(f, "{:<width$}", stype, width = 26)?;
} else {
write!(f, "{:26}", "")?;
}
if let Some(accuracy) = self.accuracy {
write!(f, "{:^11.1}", accuracy)?
} else {
write!(f, "{:11}", "")?
}
writeln!(f, "{} SENSOR MOD/TYPE/ACC", self.observable)?;
if let Some((x, y, z)) = self.position {
write!(f, "{:14.4}", x)?;
write!(f, "{:14.4}", y)?;
write!(f, "{:14.4}", z)?;
let h = self.height.unwrap_or(0.0);
write!(f, "{:14.4}", h)?;
writeln!(f, " {} SENSOR POS XYZ/H", self.observable)?
}
Ok(())
}
}
impl Sensor {
/// Define a new [Observable] sensor
pub fn new(observable: Observable) -> Self {
Self::default().with_observable(observable)
}
/// Copies and defines [Sensor] model
pub fn with_model(&self, model: &str) -> Self {
let mut s = self.clone();
s.model = Some(model.to_string());
s
}
/// Copies and defines [Sensor] type
pub fn with_type(&self, stype: &str) -> Self {
let mut s = self.clone();
s.sensor_type = Some(stype.to_string());
s
}
/// Copies and defines [Sensor] [Observable]
pub fn with_observable(&self, observable: Observable) -> Self {
let mut s = self.clone();
s.observable = observable;
s
}
/// Copies and define new sensor position
pub fn with_position(&self, position: (f64, f64, f64)) -> Self {
let mut s = self.clone();
s.position = Some(position);
s
}
/// Copies and define new sensor height eccentricity
pub fn with_height(&self, h: f64) -> Self {
let mut s = self.clone();
s.height = Some(h);
s
}
/// Copies and defines sensor accuracy
pub fn with_accuracy(&self, accuracy: f32) -> Self {
let mut s = self.clone();
s.accuracy = Some(accuracy);
s
}
#[cfg(feature = "nav")]
#[cfg_attr(docsrs, doc(cfg(feature = "nav")))]
/// Expresses [Sensor] position as an [Orbit]
pub fn rx_orbit(&self, t: Epoch, frame: Frame) -> Option<Orbit> {
let (x_ecef_m, y_ecef_m, z_ecef_m) = self.position?;
let pos_vel = Vector6::new(
x_ecef_m / 1000.0,
y_ecef_m / 1000.0,
z_ecef_m / 1000.0,
0.0,
0.0,
0.0,
);
Some(Orbit::from_cartesian_pos_vel(pos_vel, t, frame))
}
}
#[cfg(test)]
// #[cfg(feature = "nav")]
mod test {
use super::*;
// use anise::prelude::{Orbit, Frame};
use std::str::FromStr;
#[test]
fn test_formatting() {
let s = Sensor::new(Observable::Temperature);
assert_eq!(
s.to_string(),
" TD SENSOR MOD/TYPE/ACC\n"
);
let s = s.with_model("PAROSCIENTIFIC");
assert_eq!(
s.to_string(),
"PAROSCIENTIFIC TD SENSOR MOD/TYPE/ACC\n"
);
let s = s.with_observable(Observable::Pressure);
let s = s.with_type("740-16B");
assert_eq!(
s.to_string(),
"PAROSCIENTIFIC 740-16B PR SENSOR MOD/TYPE/ACC\n"
);
let s = s.with_accuracy(0.2);
assert_eq!(
s.to_string(),
"PAROSCIENTIFIC 740-16B 0.2 PR SENSOR MOD/TYPE/ACC\n"
);
// let rx_orbit = Orbit::from_position(
// 0.0,
// 0.0,
// 0.0,
// Default::default(),
// Frame::from_name("EARTH", ""),
// );
// let mut s = s.with_position(rx_orbit);
// s.height = Some(1234.5678);
// assert_eq!(
// s.to_string(),
// "PAROSCIENTIFIC 740-16B 0.2 PR SENSOR MOD/TYPE/ACC
// 0.0000 0.0000 0.0000 1234.5678 PR SENSOR POS XYZ/H\n"
// );
}
#[test]
fn from_str() {
let s = Sensor::from_str(" 0.0 PR ");
assert!(s.is_ok());
let s = s.unwrap();
assert_eq!(s.model, None);
assert_eq!(s.sensor_type, None);
assert_eq!(s.accuracy, Some(0.0));
assert_eq!(s.observable, Observable::Pressure);
let s = Sensor::from_str(
"PAROSCIENTIFIC 740-16B 0.2 PR SENSOR MOD/TYPE/ACC",
);
assert!(s.is_ok());
let s = Sensor::from_str(
" 0.0 PR SENSOR MOD/TYPE/ACC",
);
assert!(s.is_ok());
}
}