Brahe provides a global space weather provider that supplies geomagnetic indices and solar flux data when needed. If you want to skip the details for now, initialize the global provider with defaults:
usebraheasbh;fnmain(){// Initialize with default caching provider (will download data as needed)bh::space_weather::initialize_sw().unwrap();}
Warning
Space weather data MUST be initialized before using any functionality that requires it. If no data is initialized, brahe will panic and terminate the program.
The data is used by atmospheric drag models to compute density variations.
A static provider uses fixed values for all space weather parameters. This is useful for testing or when you want reproducible results with known conditions.
importbraheasbh# Method 1: Default Provider -> Uses packaged data file within Brahesw_file_default=bh.FileSpaceWeatherProvider.from_default_file()bh.set_global_space_weather_provider(sw_file_default)# Method 2: Custom File Path -> Replace with actual file pathifFalse:# Change to True to enable custom file examplesw_file_custom=bh.FileSpaceWeatherProvider.from_file("/path/to/sw19571001.txt",# Replace with actual file path"Hold",# Extrapolation: "Zero", "Hold", or "Error")bh.set_global_space_weather_provider(sw_file_custom)
usebraheasbh;usebh::space_weather::SpaceWeatherExtrapolation;usestd::path::Path;fnmain(){// Method 1: Default Provider -> Uses packaged data file within Braheletsw_file_default=bh::space_weather::FileSpaceWeatherProvider::from_default_file().unwrap();bh::space_weather::set_global_space_weather_provider(sw_file_default);// Method 2: Custom File Path -> Replace with actual file pathiffalse{// Change to true to enable custom file exampleletsw_file_custom=bh::space_weather::FileSpaceWeatherProvider::from_file(Path::new("/path/to/sw19571001.txt"),// Replace with actual file pathSpaceWeatherExtrapolation::Hold,).unwrap();bh::space_weather::set_global_space_weather_provider(sw_file_custom);}}
The caching provider automatically downloads and manages space weather data files from CelesTrak. It checks file age and updates when the cache becomes stale.
importbraheasbh# Method 1: Create with custom settings# - Downloads to ~/.cache/brahe/# - Refreshes if file is older than 24 hourssw_caching=bh.CachingSpaceWeatherProvider(max_age_seconds=86400,# max_age: seconds (86400 = 24 hours)auto_refresh=False,# check on each queryextrapolate="Hold",# extrapolation)bh.set_global_space_weather_provider(sw_caching)# Method 2: Use initialize_sw() which creates a caching providerbh.initialize_sw()
usebraheasbh;usebh::space_weather::SpaceWeatherExtrapolation;fnmain(){// Method 1: Create with custom settings// - Downloads to ~/.cache/brahe/// - Refreshes if file is older than 24 hoursletsw_caching=bh::space_weather::CachingSpaceWeatherProvider::new(None,// cache_dir: None for default86400,// max_age: seconds (86400 = 24 hours)false,// auto_refresh: check on each querySpaceWeatherExtrapolation::Hold,// extrapolation).unwrap();bh::space_weather::set_global_space_weather_provider(sw_caching);// Method 2: Use initialize_sw() which creates a caching providerbh::space_weather::initialize_sw().unwrap();}
importbraheasbhbh.initialize_sw()# Get data for a specific epochepoch=bh.Epoch.from_datetime(2024,1,1,12,0,0.0,0.0,bh.TimeSystem.UTC)mjd=epoch.mjd()# Kp/Ap for specific 3-hour intervalkp=bh.get_global_kp(mjd)ap=bh.get_global_ap(mjd)# Daily averageskp_daily=bh.get_global_kp_daily(mjd)ap_daily=bh.get_global_ap_daily(mjd)# All 8 values for the daykp_all=bh.get_global_kp_all(mjd)# [Kp_00-03, Kp_03-06, ..., Kp_21-24]ap_all=bh.get_global_ap_all(mjd)# F10.7 solar fluxf107=bh.get_global_f107_observed(mjd)f107_adj=bh.get_global_f107_adjusted(mjd)f107_avg=bh.get_global_f107_obs_avg81(mjd)# 81-day centered average# Sunspot numberisn=bh.get_global_sunspot_number(mjd)print(f"Kp: {kp}, Ap: {ap}, F10.7: {f107} sfu, ISN: {isn}")
usebraheasbh;usebh::time::TimeSystem;fnmain(){bh::space_weather::initialize_sw().unwrap();// Get data for a specific epochletepoch=bh::time::Epoch::from_datetime(2024,1,1,12,0,0.0,0.0,TimeSystem::UTC);letmjd=epoch.mjd();// Kp/Ap for specific 3-hour intervalletkp=bh::space_weather::get_global_kp(mjd).unwrap();letap=bh::space_weather::get_global_ap(mjd).unwrap();// Daily averagesletkp_daily=bh::space_weather::get_global_kp_daily(mjd).unwrap();letap_daily=bh::space_weather::get_global_ap_daily(mjd).unwrap();// All 8 values for the dayletkp_all=bh::space_weather::get_global_kp_all(mjd).unwrap();// [Kp_00-03, Kp_03-06, ..., Kp_21-24]letap_all=bh::space_weather::get_global_ap_all(mjd).unwrap();// F10.7 solar fluxletf107=bh::space_weather::get_global_f107_observed(mjd).unwrap();letf107_adj=bh::space_weather::get_global_f107_adjusted(mjd).unwrap();letf107_avg=bh::space_weather::get_global_f107_obs_avg81(mjd).unwrap();// 81-day centered average// Sunspot numberletisn=bh::space_weather::get_global_sunspot_number(mjd).unwrap();println!("Kp: {}, Ap: {}, F10.7: {} sfu, ISN: {}",kp,ap,f107,isn);// Suppress unused variable warningslet_=(kp_daily,ap_daily,kp_all,ap_all,f107_adj,f107_avg);}
The space weather providers also support querying data over a date range, returning a vector of values from before the specific time. This is useful to providing the weather history for drag models.
importbraheasbhbh.initialize_sw()epoch=bh.Epoch.from_datetime(2024,1,1,0,0,0.0,0.0,bh.TimeSystem.UTC)mjd=epoch.mjd()# Get last 30 days of F10.7 dataf107_history=bh.get_global_last_f107(mjd,30)# Get last 7 days of daily Apap_history=bh.get_global_last_daily_ap(mjd,7)# Get epochs for the data pointsepochs=bh.get_global_last_daily_epochs(mjd,7)print(f"Last 7 daily Ap values: {ap_history}")print(f"Last 7 epochs: {[str(e)foreinepochs]}")
usebraheasbh;usebh::time::TimeSystem;fnmain(){bh::space_weather::initialize_sw().unwrap();letepoch=bh::time::Epoch::from_datetime(2024,1,1,0,0,0.0,0.0,TimeSystem::UTC);letmjd=epoch.mjd();// Get last 30 days of F10.7 dataletf107_history=bh::space_weather::get_global_last_f107(mjd,30).unwrap();// Get last 7 days of daily Apletap_history=bh::space_weather::get_global_last_daily_ap(mjd,7).unwrap();// Get epochs for the data pointsletepochs=bh::space_weather::get_global_last_daily_epochs(mjd,7).unwrap();println!("Last 7 daily Ap values: {:?}",ap_history);println!("Last 7 epochs: {:?}",epochs.iter().map(|e|e.to_string()).collect::<Vec<_>>());// Suppress unused variable warningslet_=f107_history;}