1
- use crate :: prelude:: { Duration , Epoch , TimeScale , TrackData } ;
1
+ use crate :: prelude:: { Duration , Epoch , IonosphericData , TimeScale , TrackData } ;
2
2
use polyfit_rs:: polyfit_rs:: polyfit;
3
3
use std:: collections:: BTreeMap ;
4
4
use thiserror:: Error ;
@@ -59,14 +59,22 @@ pub struct FitData {
59
59
}
60
60
61
61
impl SVTracker {
62
+ /* has msio data */
63
+ fn has_msio ( & self ) -> bool {
64
+ self . buffer
65
+ . values ( )
66
+ . filter ( |data| data. msio . is_some ( ) )
67
+ . count ( )
68
+ > 0
69
+ }
62
70
/// Try to fit a track. You need to provide the ongoing IOE.
63
71
pub fn fit (
64
72
& self ,
65
73
ioe : u16 ,
66
74
trk_duration : Duration ,
67
75
sampling_period : Duration ,
68
76
trk_midpoint : Epoch ,
69
- ) -> Result < ( ( f64 , f64 ) , TrackData ) , FitError > {
77
+ ) -> Result < ( ( f64 , f64 ) , TrackData , Option < IonosphericData > ) , FitError > {
70
78
// verify tracking completion
71
79
// complete if we have enough measurements
72
80
let expected_nb =
@@ -209,21 +217,6 @@ impl SVTracker {
209
217
let ( smdi, smdi_b) = ( fit[ 1 ] , fit[ 0 ] ) ;
210
218
let mdio = smdi * t_mid_s + smdi_b;
211
219
212
- let fit = polyfit (
213
- & t_xs,
214
- & self
215
- . buffer
216
- . values ( )
217
- . map ( |f| f. msio . unwrap_or ( 0.0_f64 ) )
218
- . collect :: < Vec < _ > > ( )
219
- . as_slice ( ) ,
220
- 1 ,
221
- )
222
- . map_err ( |_| FitError :: LinearRegressionFailure ) ?;
223
-
224
- let ( smsi, smsi_b) = ( fit[ 1 ] , fit[ 0 ] ) ;
225
- let msio = smsi * t_mid_s + smsi_b;
226
-
227
220
let trk_data = TrackData {
228
221
refsv,
229
222
srsv,
@@ -237,7 +230,36 @@ impl SVTracker {
237
230
smdi,
238
231
} ;
239
232
240
- Ok ( ( ( elev, azi) , trk_data) )
233
+ let iono_data = match self . has_msio ( ) {
234
+ false => None ,
235
+ true => {
236
+ let fit = polyfit (
237
+ & t_xs,
238
+ & self
239
+ . buffer
240
+ . values ( )
241
+ . map ( |f| f. msio . unwrap ( ) )
242
+ . collect :: < Vec < _ > > ( )
243
+ . as_slice ( ) ,
244
+ 1 ,
245
+ )
246
+ . map_err ( |_| FitError :: LinearRegressionFailure ) ?;
247
+
248
+ let ( smsi, smsi_b) = ( fit[ 1 ] , fit[ 0 ] ) ;
249
+ let msio = smsi * t_mid_s + smsi_b;
250
+
251
+ let mut isg = 0.0_f64 ;
252
+ let msio_fit: Vec < _ > = t_xs. iter ( ) . map ( |t_s| smsi * t_s + smsi_b) . collect ( ) ;
253
+ for msio_fit in msio_fit {
254
+ isg += ( msio_fit - msio) . powi ( 2 ) ;
255
+ }
256
+ isg = isg. sqrt ( ) ;
257
+
258
+ Some ( IonosphericData { msio, smsi, isg } )
259
+ } ,
260
+ } ;
261
+
262
+ Ok ( ( ( elev, azi) , trk_data, iono_data) )
241
263
}
242
264
243
265
/// Latch a new measurement at given UTC Epoch.
0 commit comments