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
use crate::prelude::SP3;
#[cfg(feature = "processing")]
#[cfg_attr(docsrs, doc(cfg(feature = "processing")))]
impl SP3 {
/// Substract rhs [SP3] to this [SP3], returning a new "residual" [SP3].
/// All common components are modified and replaced by residual value.
/// That applies to both coordinates and temporal values.
/// This may be used in SP3 residual analysis, where one compares [SP3] from
/// one laboratory to another. Use with care, because the resulting values
/// are obviously not what a standard [SP3] should physically represent.
/// ```
/// use sp3::prelude::SP3;
///
/// // this dataset comes with position vectors and clock states
/// let sp3 = SP3::from_gzip_file("data/SP3/C/EMR0OPSULT_20232391800_02D_15M_ORB.SP3.gz")
/// .unwrap();
///
/// let null_sp3 = sp3.substract(&sp3);
///
/// // Verify that self-self is null residual
/// for (_, v) in null_sp3.data.iter() {
/// // residual position is null
/// assert_eq!(v.position_km, (0.0, 0.0, 0.0));
///
/// // residual clock exists
/// let clock_us = v.clock_us.unwrap();
/// assert_eq!(clock_us, 0.0);
/// }
/// ```
pub fn substract(&self, rhs: &Self) -> Self {
let mut s = self.clone();
s.substract_mut(rhs);
s
}
/// Substract rhs [SP3] to Self, with mutable access. Refer to [Self::substract].
pub fn substract_mut(&mut self, rhs: &Self) {
self.data.retain(|k, v| {
if let Some(v_rhs) = rhs.data.get(&k) {
*v -= *v_rhs;
true
} else {
false
}
});
}
}
#[cfg(test)]
#[cfg(feature = "flate2")]
mod test {
use crate::prelude::SP3;
#[test]
fn substract_null_sp3() {
let parsed =
SP3::from_gzip_file("data/SP3/C/GRG0MGXFIN_20201770000_01D_15M_ORB.SP3.gz").unwrap();
let null_sp3 = parsed.substract(&parsed);
for (k, v) in null_sp3.data.iter() {
assert_eq!(
v.position_km,
(0.0, 0.0, 0.0),
"{}({}) - position is not null",
k.epoch,
k.sv
);
assert_eq!(
v.clock_us,
Some(0.0),
"{}({}) - clock offset is not null",
k.epoch,
k.sv
);
assert!(
v.velocity_km_s.is_none(),
"{}({}) - velocity should not exist",
k.epoch,
k.sv
);
assert!(
v.clock_drift_ns.is_none(),
"{}({}) - clock drift should not exist",
k.epoch,
k.sv
);
}
}
}