1- use crate :: prelude:: { Duration , Epoch , TimeScale , TrackData } ;
1+ use crate :: prelude:: { Duration , Epoch , IonosphericData , TimeScale , TrackData } ;
22use polyfit_rs:: polyfit_rs:: polyfit;
33use std:: collections:: BTreeMap ;
44use thiserror:: Error ;
@@ -59,14 +59,22 @@ pub struct FitData {
5959}
6060
6161impl 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+ }
6270 /// Try to fit a track. You need to provide the ongoing IOE.
6371 pub fn fit (
6472 & self ,
6573 ioe : u16 ,
6674 trk_duration : Duration ,
6775 sampling_period : Duration ,
6876 trk_midpoint : Epoch ,
69- ) -> Result < ( ( f64 , f64 ) , TrackData ) , FitError > {
77+ ) -> Result < ( ( f64 , f64 ) , TrackData , Option < IonosphericData > ) , FitError > {
7078 // verify tracking completion
7179 // complete if we have enough measurements
7280 let expected_nb =
@@ -209,21 +217,6 @@ impl SVTracker {
209217 let ( smdi, smdi_b) = ( fit[ 1 ] , fit[ 0 ] ) ;
210218 let mdio = smdi * t_mid_s + smdi_b;
211219
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-
227220 let trk_data = TrackData {
228221 refsv,
229222 srsv,
@@ -237,7 +230,36 @@ impl SVTracker {
237230 smdi,
238231 } ;
239232
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) )
241263 }
242264
243265 /// Latch a new measurement at given UTC Epoch.
0 commit comments