Space Weather¶
Space weather types, providers, and query functions.
See the Space Weather User Guide for concepts and usage.
Types¶
Type definitions for Space Weather data.
Provides the core data type for space weather storage and lookup:
- :class:
SpaceWeatherData: Immutable container holding sorted space weather arrays for JIT-compatible lookup viajnp.searchsorted.
SpaceWeatherData is a :class:~typing.NamedTuple, which JAX treats as a
pytree automatically. This means it works seamlessly with jax.jit,
jax.vmap, and jax.lax control flow primitives.
SpaceWeatherData
¶
Bases: NamedTuple
Space weather data for JIT-compatible lookups.
Stores space weather values as sorted JAX arrays, enabling O(log n) lookup
inside jax.jit via jnp.searchsorted. Missing values (e.g. Kp/Ap
in monthly-predicted regions) are stored as NaN.
Attributes:
| Name | Type | Description |
|---|---|---|
mjd |
Array
|
Sorted Modified Julian Dates (one per day), shape |
kp |
Array
|
3-hourly Kp indices (0.0-9.0 scale), shape |
ap |
Array
|
3-hourly Ap indices, shape |
ap_daily |
Array
|
Daily average Ap index, shape |
f107_obs |
Array
|
Observed 10.7 cm solar radio flux [sfu], shape |
f107_adj |
Array
|
Adjusted 10.7 cm solar radio flux [sfu], shape |
f107_obs_ctr81 |
Array
|
81-day centered average observed F10.7, shape |
f107_obs_lst81 |
Array
|
81-day last average observed F10.7, shape |
f107_adj_ctr81 |
Array
|
81-day centered average adjusted F10.7, shape |
f107_adj_lst81 |
Array
|
81-day last average adjusted F10.7, shape |
mjd_min |
Array
|
Scalar, first MJD in the dataset. |
mjd_max |
Array
|
Scalar, last MJD in the dataset. |
Query Functions¶
JIT-compatible space weather lookup functions.
All functions use only JAX primitives (jnp.searchsorted, array indexing,
jnp.where) and are fully compatible with jax.jit, jax.vmap,
and jax.grad.
get_sw_ap(sw, mjd)
¶
Query 3-hourly Ap index at the given MJD.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sw
|
SpaceWeatherData
|
Space weather dataset. |
required |
mjd
|
ArrayLike
|
Modified Julian Date to query. |
required |
Returns:
| Type | Description |
|---|---|
Array
|
Ap index for the 3-hour interval containing the MJD. |
get_sw_ap_array(sw, mjd)
¶
Build the 7-element NRLMSISE-00 AP array.
Constructs the magnetic activity array required by the NRLMSISE-00 model:
[0]: Daily Ap[1]: Current 3-hour Ap[2]: 3-hour Ap at -3h[3]: 3-hour Ap at -6h[4]: 3-hour Ap at -9h[5]: Average of eight 3-hour Ap from 12-33h prior[6]: Average of eight 3-hour Ap from 36-57h prior
All lookups use compile-time known offsets (unrolled at trace time).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sw
|
SpaceWeatherData
|
Space weather dataset. |
required |
mjd
|
ArrayLike
|
Modified Julian Date to query. |
required |
Returns:
| Type | Description |
|---|---|
Array
|
Array of shape |
get_sw_ap_daily(sw, mjd)
¶
Query daily average Ap index at the given MJD.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sw
|
SpaceWeatherData
|
Space weather dataset. |
required |
mjd
|
ArrayLike
|
Modified Julian Date to query. |
required |
Returns:
| Type | Description |
|---|---|
Array
|
Daily average Ap index. |
get_sw_f107_adj(sw, mjd)
¶
Query adjusted F10.7 solar flux at the given MJD.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sw
|
SpaceWeatherData
|
Space weather dataset. |
required |
mjd
|
ArrayLike
|
Modified Julian Date to query. |
required |
Returns:
| Type | Description |
|---|---|
Array
|
Adjusted F10.7 flux [sfu]. |
get_sw_f107_obs(sw, mjd)
¶
Query observed F10.7 solar flux at the given MJD.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sw
|
SpaceWeatherData
|
Space weather dataset. |
required |
mjd
|
ArrayLike
|
Modified Julian Date to query. |
required |
Returns:
| Type | Description |
|---|---|
Array
|
Observed F10.7 flux [sfu]. |
get_sw_f107_obs_ctr81(sw, mjd)
¶
Query 81-day centered average observed F10.7 at the given MJD.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sw
|
SpaceWeatherData
|
Space weather dataset. |
required |
mjd
|
ArrayLike
|
Modified Julian Date to query. |
required |
Returns:
| Type | Description |
|---|---|
Array
|
81-day centered average observed F10.7 flux [sfu]. |
get_sw_f107_obs_lst81(sw, mjd)
¶
Query 81-day last average observed F10.7 at the given MJD.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sw
|
SpaceWeatherData
|
Space weather dataset. |
required |
mjd
|
ArrayLike
|
Modified Julian Date to query. |
required |
Returns:
| Type | Description |
|---|---|
Array
|
81-day last average observed F10.7 flux [sfu]. |
get_sw_kp(sw, mjd)
¶
Query 3-hourly Kp index at the given MJD.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sw
|
SpaceWeatherData
|
Space weather dataset. |
required |
mjd
|
ArrayLike
|
Modified Julian Date to query. |
required |
Returns:
| Type | Description |
|---|---|
Array
|
Kp index (0.0-9.0) for the 3-hour interval containing the MJD. |
Providers¶
Factory functions for creating SpaceWeatherData instances.
Provides convenience constructors for common space weather configurations:
- :func:
static_space_weather: Constant space weather values (useful for testing or when specific values are known). - :func:
zero_space_weather: All-zero space weather. - :func:
load_sw_from_file: Load from CSSI format file. - :func:
load_default_sw: Load bundledsw19571001.txtdata. - :func:
load_cached_sw: Load from a local cache, downloading fresh data from CelesTrak when stale.
load_cached_sw(filepath=None, *, max_age_days=_DEFAULT_MAX_AGE_DAYS)
¶
Load space weather data from a local cache, downloading when stale.
Checks whether the cached file at filepath exists and is younger than max_age_days. If the file is missing or stale, a fresh copy is downloaded from CelesTrak. If the download fails or the file cannot be parsed, the bundled default data is returned so this function never raises on network issues.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filepath
|
str | Path | None
|
Path to the cached SW file. When |
None
|
max_age_days
|
float
|
Maximum acceptable age of the cached file in days. Defaults to 7. |
_DEFAULT_MAX_AGE_DAYS
|
Returns:
| Type | Description |
|---|---|
SpaceWeatherData
|
SpaceWeatherData loaded from the cached (or freshly downloaded) file, |
SpaceWeatherData
|
or the bundled default data as a fallback. |
Examples:
load_default_sw()
¶
Load the bundled default space weather data (sw19571001.txt).
Uses importlib.resources to locate the data file bundled with the
package.
Returns:
| Type | Description |
|---|---|
SpaceWeatherData
|
SpaceWeatherData loaded from the bundled |
Examples:
load_sw_from_file(filepath)
¶
Load space weather data from a CSSI format file.
Parses the file, converts lists to JAX arrays, and computes metadata scalars (mjd_min, mjd_max).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filepath
|
str | Path
|
Path to a CSSI space weather file
(e.g. |
required |
Returns:
| Type | Description |
|---|---|
SpaceWeatherData
|
SpaceWeatherData ready for JIT-compatible lookups. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the file does not exist. |
ValueError
|
If no valid data is found. |
Examples:
static_space_weather(ap=4.0, f107=150.0, f107a=150.0, kp=1.0, mjd_min=0.0, mjd_max=99999.0)
¶
Create a SpaceWeatherData with constant values.
Useful for testing or when specific constant space weather values are known. The resulting dataset contains two points (at mjd_min and mjd_max) with identical values, so lookups return the constant everywhere.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ap
|
float
|
Constant Ap index. Default: 4.0. |
4.0
|
f107
|
float
|
Constant F10.7 flux [sfu]. Default: 150.0. |
150.0
|
f107a
|
float
|
Constant 81-day average F10.7 flux [sfu]. Default: 150.0. |
150.0
|
kp
|
float
|
Constant Kp index. Default: 1.0. |
1.0
|
mjd_min
|
float
|
Start of the valid MJD range. Default: 0.0. |
0.0
|
mjd_max
|
float
|
End of the valid MJD range. Default: 99999.0. |
99999.0
|
Returns:
| Type | Description |
|---|---|
SpaceWeatherData
|
SpaceWeatherData with constant values. |
Examples:
zero_space_weather()
¶
Create a SpaceWeatherData with all-zero values.
Returns:
| Type | Description |
|---|---|
SpaceWeatherData
|
SpaceWeatherData with all values set to zero. |
Examples: