brahe 1.4.0

Brahe is a modern satellite dynamics library for research and engineering applications designed to be easy-to-learn, high-performance, and quick-to-deploy. The north-star of the development is enabling users to solve meaningful problems and answer questions quickly, easily, and correctly.
Documentation
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/*!
Defines the SpaceWeatherProvider trait
*/

use crate::space_weather::types::{SpaceWeatherExtrapolation, SpaceWeatherType};
use crate::time::Epoch;
use crate::utils::errors::BraheError;

/// `SpaceWeatherProvider` is a trait for objects that provide space weather data.
///
/// This trait defines a common interface for all space weather providers. A space weather provider
/// is an object that can provide geomagnetic indices (Kp, Ap) and solar activity data (F10.7 flux,
/// sunspot number).
///
/// Implementations of this trait are expected to provide specific methods for retrieving these parameters,
/// such as `get_kp`, `get_ap`, `get_f107_observed`, etc. These methods should return the requested
/// parameter for a given Modified Julian Date (MJD).
///
/// This trait can be extended to implement custom space weather providers.
///
/// # Example
///
/// ```ignore
/// use brahe::space_weather::SpaceWeatherProvider;
///
/// struct MySpaceWeatherProvider;
///
/// impl SpaceWeatherProvider for MySpaceWeatherProvider {
///     // Implement the methods here
/// }
/// ```
pub trait SpaceWeatherProvider: Send + Sync {
    /// Returns the number of space weather data entries loaded in the provider.
    fn len(&self) -> usize;

    /// Returns true if the provider contains no space weather data entries.
    fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Returns the type of space weather data loaded (CssiSpaceWeather or Static).
    fn sw_type(&self) -> SpaceWeatherType;

    /// Returns true if the provider has been initialized with valid space weather data.
    fn is_initialized(&self) -> bool;

    /// Returns the extrapolation behavior (Zero, Hold, or Error) for out-of-range requests.
    fn extrapolation(&self) -> SpaceWeatherExtrapolation;

    /// Returns the minimum Modified Julian Date covered by the loaded space weather data.
    fn mjd_min(&self) -> f64;

    /// Returns the maximum Modified Julian Date covered by the loaded space weather data.
    fn mjd_max(&self) -> f64;

    /// Returns the last MJD with observed (historical) space weather data.
    fn mjd_last_observed(&self) -> f64;

    /// Returns the last MJD with daily predicted space weather data.
    fn mjd_last_daily_predicted(&self) -> f64;

    /// Returns the last MJD with monthly predicted space weather data.
    fn mjd_last_monthly_predicted(&self) -> f64;

    /// Get the Kp index for a specific 3-hour interval at the given MJD.
    ///
    /// The MJD's fractional day determines which of the 8 daily intervals is returned:
    /// - Index 0: 00:00-03:00 UT
    /// - Index 1: 03:00-06:00 UT
    /// - Index 2: 06:00-09:00 UT
    /// - Index 3: 09:00-12:00 UT
    /// - Index 4: 12:00-15:00 UT
    /// - Index 5: 15:00-18:00 UT
    /// - Index 6: 18:00-21:00 UT
    /// - Index 7: 21:00-24:00 UT
    ///
    /// # Returns
    /// Kp index on the 0.0-9.0 scale
    fn get_kp(&self, mjd: f64) -> Result<f64, BraheError>;

    /// Get all eight 3-hourly Kp indices for the day containing the given MJD.
    ///
    /// # Returns
    /// Array of 8 Kp indices on the 0.0-9.0 scale
    fn get_kp_all(&self, mjd: f64) -> Result<[f64; 8], BraheError>;

    /// Get the daily average Kp index (sum of 8 intervals / 8) for the given MJD.
    ///
    /// # Returns
    /// Daily average Kp index
    fn get_kp_daily(&self, mjd: f64) -> Result<f64, BraheError>;

    /// Get the Ap index for a specific 3-hour interval at the given MJD.
    ///
    /// The MJD's fractional day determines which of the 8 daily intervals is returned.
    ///
    /// # Returns
    /// Ap index (integer value 0-400)
    fn get_ap(&self, mjd: f64) -> Result<f64, BraheError>;

    /// Get all eight 3-hourly Ap indices for the day containing the given MJD.
    ///
    /// # Returns
    /// Array of 8 Ap indices
    fn get_ap_all(&self, mjd: f64) -> Result<[f64; 8], BraheError>;

    /// Get the daily average Ap index for the given MJD.
    ///
    /// # Returns
    /// Daily average Ap index
    fn get_ap_daily(&self, mjd: f64) -> Result<f64, BraheError>;

    /// Get the observed 10.7 cm solar radio flux for the given MJD.
    ///
    /// # Returns
    /// F10.7 flux in solar flux units (sfu)
    fn get_f107_observed(&self, mjd: f64) -> Result<f64, BraheError>;

    /// Get the adjusted 10.7 cm solar radio flux for the given MJD.
    ///
    /// The adjusted value normalizes the observed flux to 1 AU.
    ///
    /// # Returns
    /// Adjusted F10.7 flux in solar flux units (sfu)
    fn get_f107_adjusted(&self, mjd: f64) -> Result<f64, BraheError>;

    /// Get the observed 81-day centered average F10.7 flux for the given MJD.
    ///
    /// # Returns
    /// 81-day centered average F10.7 in sfu
    fn get_f107_obs_avg81(&self, mjd: f64) -> Result<f64, BraheError>;

    /// Get the adjusted 81-day centered average F10.7 flux for the given MJD.
    ///
    /// # Returns
    /// 81-day centered average adjusted F10.7 in sfu
    fn get_f107_adj_avg81(&self, mjd: f64) -> Result<f64, BraheError>;

    /// Get the International Sunspot Number for the given MJD.
    ///
    /// # Returns
    /// International Sunspot Number
    fn get_sunspot_number(&self, mjd: f64) -> Result<u32, BraheError>;

    /// Get the last N 3-hourly Kp values ending at or before the given MJD.
    ///
    /// Returns a vector with the oldest value first and newest last.
    /// Each value corresponds to one 3-hour interval.
    ///
    /// # Arguments
    /// - `mjd`: Modified Julian Date (end point)
    /// - `n`: Number of 3-hourly values to return
    ///
    /// # Returns
    /// Vector of Kp indices (0.0-9.0 scale), oldest first
    fn get_last_kp(&self, mjd: f64, n: usize) -> Result<Vec<f64>, BraheError>;

    /// Get the last N 3-hourly Ap values ending at or before the given MJD.
    ///
    /// Returns a vector with the oldest value first and newest last.
    /// Each value corresponds to one 3-hour interval.
    ///
    /// # Arguments
    /// - `mjd`: Modified Julian Date (end point)
    /// - `n`: Number of 3-hourly values to return
    ///
    /// # Returns
    /// Vector of Ap indices, oldest first
    fn get_last_ap(&self, mjd: f64, n: usize) -> Result<Vec<f64>, BraheError>;

    /// Get the last N daily average Kp values ending at or before the given MJD.
    ///
    /// Returns a vector with the oldest value first and newest last.
    /// Each value is the daily average (kp_sum / 8).
    ///
    /// # Arguments
    /// - `mjd`: Modified Julian Date (end point)
    /// - `n`: Number of daily values to return
    ///
    /// # Returns
    /// Vector of daily average Kp indices, oldest first
    fn get_last_daily_kp(&self, mjd: f64, n: usize) -> Result<Vec<f64>, BraheError>;

    /// Get the last N daily average Ap values ending at or before the given MJD.
    ///
    /// Returns a vector with the oldest value first and newest last.
    /// Each value is the daily average Ap.
    ///
    /// # Arguments
    /// - `mjd`: Modified Julian Date (end point)
    /// - `n`: Number of daily values to return
    ///
    /// # Returns
    /// Vector of daily average Ap indices, oldest first
    fn get_last_daily_ap(&self, mjd: f64, n: usize) -> Result<Vec<f64>, BraheError>;

    /// Get the last N daily observed F10.7 values ending at or before the given MJD.
    ///
    /// Returns a vector with the oldest value first and newest last.
    ///
    /// # Arguments
    /// - `mjd`: Modified Julian Date (end point)
    /// - `n`: Number of daily values to return
    ///
    /// # Returns
    /// Vector of F10.7 values in sfu, oldest first
    fn get_last_f107(&self, mjd: f64, n: usize) -> Result<Vec<f64>, BraheError>;

    /// Get the epochs for the last N 3-hourly Kp/Ap intervals ending at or before the given MJD.
    ///
    /// Returns a vector with the oldest epoch first and newest last.
    /// Each epoch is at the start of a 3-hour UT interval (00:00, 03:00, ..., 21:00).
    ///
    /// # Arguments
    /// - `mjd`: Modified Julian Date (end point)
    /// - `n`: Number of 3-hourly epochs to return
    ///
    /// # Returns
    /// Vector of Epoch objects, oldest first
    fn get_last_kpap_epochs(&self, mjd: f64, n: usize) -> Result<Vec<Epoch>, BraheError>;

    /// Get the epochs for the last N daily values ending at or before the given MJD.
    ///
    /// Returns a vector with the oldest epoch first and newest last.
    /// Each epoch is at 00:00 UT for the given day.
    ///
    /// # Arguments
    /// - `mjd`: Modified Julian Date (end point)
    /// - `n`: Number of daily epochs to return
    ///
    /// # Returns
    /// Vector of Epoch objects, oldest first
    fn get_last_daily_epochs(&self, mjd: f64, n: usize) -> Result<Vec<Epoch>, BraheError>;
}

#[cfg(test)]
#[cfg_attr(coverage_nightly, coverage(off))]
mod tests {
    use super::*;

    // Test implementation of SpaceWeatherProvider for testing the default is_empty() method
    struct MockSpaceWeatherProvider {
        len: usize,
    }

    impl SpaceWeatherProvider for MockSpaceWeatherProvider {
        fn len(&self) -> usize {
            self.len
        }

        fn sw_type(&self) -> SpaceWeatherType {
            SpaceWeatherType::Static
        }

        fn is_initialized(&self) -> bool {
            true
        }

        fn extrapolation(&self) -> SpaceWeatherExtrapolation {
            SpaceWeatherExtrapolation::Hold
        }

        fn mjd_min(&self) -> f64 {
            0.0
        }

        fn mjd_max(&self) -> f64 {
            0.0
        }

        fn mjd_last_observed(&self) -> f64 {
            0.0
        }

        fn mjd_last_daily_predicted(&self) -> f64 {
            0.0
        }

        fn mjd_last_monthly_predicted(&self) -> f64 {
            0.0
        }

        fn get_kp(&self, _mjd: f64) -> Result<f64, BraheError> {
            Ok(0.0)
        }

        fn get_kp_all(&self, _mjd: f64) -> Result<[f64; 8], BraheError> {
            Ok([0.0; 8])
        }

        fn get_kp_daily(&self, _mjd: f64) -> Result<f64, BraheError> {
            Ok(0.0)
        }

        fn get_ap(&self, _mjd: f64) -> Result<f64, BraheError> {
            Ok(0.0)
        }

        fn get_ap_all(&self, _mjd: f64) -> Result<[f64; 8], BraheError> {
            Ok([0.0; 8])
        }

        fn get_ap_daily(&self, _mjd: f64) -> Result<f64, BraheError> {
            Ok(0.0)
        }

        fn get_f107_observed(&self, _mjd: f64) -> Result<f64, BraheError> {
            Ok(0.0)
        }

        fn get_f107_adjusted(&self, _mjd: f64) -> Result<f64, BraheError> {
            Ok(0.0)
        }

        fn get_f107_obs_avg81(&self, _mjd: f64) -> Result<f64, BraheError> {
            Ok(0.0)
        }

        fn get_f107_adj_avg81(&self, _mjd: f64) -> Result<f64, BraheError> {
            Ok(0.0)
        }

        fn get_sunspot_number(&self, _mjd: f64) -> Result<u32, BraheError> {
            Ok(0)
        }

        fn get_last_kp(&self, _mjd: f64, n: usize) -> Result<Vec<f64>, BraheError> {
            Ok(vec![0.0; n])
        }

        fn get_last_ap(&self, _mjd: f64, n: usize) -> Result<Vec<f64>, BraheError> {
            Ok(vec![0.0; n])
        }

        fn get_last_daily_kp(&self, _mjd: f64, n: usize) -> Result<Vec<f64>, BraheError> {
            Ok(vec![0.0; n])
        }

        fn get_last_daily_ap(&self, _mjd: f64, n: usize) -> Result<Vec<f64>, BraheError> {
            Ok(vec![0.0; n])
        }

        fn get_last_f107(&self, _mjd: f64, n: usize) -> Result<Vec<f64>, BraheError> {
            Ok(vec![0.0; n])
        }

        fn get_last_kpap_epochs(&self, mjd: f64, n: usize) -> Result<Vec<Epoch>, BraheError> {
            let mut epochs = Vec::with_capacity(n);
            for i in 0..n {
                epochs.push(Epoch::from_mjd(
                    mjd - (i as f64 * 0.125),
                    crate::time::TimeSystem::UTC,
                ));
            }
            epochs.reverse();
            Ok(epochs)
        }

        fn get_last_daily_epochs(&self, mjd: f64, n: usize) -> Result<Vec<Epoch>, BraheError> {
            let mut epochs = Vec::with_capacity(n);
            for i in 0..n {
                epochs.push(Epoch::from_mjd(
                    mjd.floor() - i as f64,
                    crate::time::TimeSystem::UTC,
                ));
            }
            epochs.reverse();
            Ok(epochs)
        }
    }

    #[test]
    fn test_space_weather_provider_is_empty_default() {
        // Test that default is_empty() returns true when len() == 0
        let empty_provider = MockSpaceWeatherProvider { len: 0 };
        assert!(empty_provider.is_empty());

        // Test that default is_empty() returns false when len() > 0
        let non_empty_provider = MockSpaceWeatherProvider { len: 10 };
        assert!(!non_empty_provider.is_empty());

        // Test boundary case with len() == 1
        let single_entry_provider = MockSpaceWeatherProvider { len: 1 };
        assert!(!single_entry_provider.is_empty());
    }

    #[test]
    fn test_mock_space_weather_provider_trait_methods() {
        // Test all SpaceWeatherProvider trait methods on MockSpaceWeatherProvider
        let provider = MockSpaceWeatherProvider { len: 5 };

        // Test metadata methods
        assert_eq!(provider.len(), 5);
        assert!(!provider.is_empty());
        assert_eq!(provider.sw_type(), SpaceWeatherType::Static);
        assert!(provider.is_initialized());
        assert_eq!(provider.extrapolation(), SpaceWeatherExtrapolation::Hold);

        // Test MJD range methods
        assert_eq!(provider.mjd_min(), 0.0);
        assert_eq!(provider.mjd_max(), 0.0);
        assert_eq!(provider.mjd_last_observed(), 0.0);
        assert_eq!(provider.mjd_last_daily_predicted(), 0.0);
        assert_eq!(provider.mjd_last_monthly_predicted(), 0.0);

        // Test space weather data retrieval methods
        let test_mjd = 60000.0;
        assert_eq!(provider.get_kp(test_mjd).unwrap(), 0.0);
        assert_eq!(provider.get_kp_all(test_mjd).unwrap(), [0.0; 8]);
        assert_eq!(provider.get_kp_daily(test_mjd).unwrap(), 0.0);
        assert_eq!(provider.get_ap(test_mjd).unwrap(), 0.0);
        assert_eq!(provider.get_ap_all(test_mjd).unwrap(), [0.0; 8]);
        assert_eq!(provider.get_ap_daily(test_mjd).unwrap(), 0.0);
        assert_eq!(provider.get_f107_observed(test_mjd).unwrap(), 0.0);
        assert_eq!(provider.get_f107_adjusted(test_mjd).unwrap(), 0.0);
        assert_eq!(provider.get_f107_obs_avg81(test_mjd).unwrap(), 0.0);
        assert_eq!(provider.get_f107_adj_avg81(test_mjd).unwrap(), 0.0);
        assert_eq!(provider.get_sunspot_number(test_mjd).unwrap(), 0);
    }
}