Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions ext/TsyganenkoModelsCDAWebExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module TsyganenkoModelsCDAWebExt

using CDAWeb: CDAWeb, getdata, times
using Dates
using TsyganenkoModels: TsyganenkoModels, OMNI, T89, T96, T01, TS04, fill_gaps!, t01_g, ts05_w, TS05_R
using TsyganenkoModels: TsyganenkoModels, OMNI, T89, T96, T01, TS04, fill_gaps!, hourly_mean, t01_g, ts05_w, TS05_R

const STEP = Minute(5)
const PER_DAY = 288
Expand All @@ -16,6 +16,7 @@ struct OMNIDay
symh::Vector{Float64}
by::Vector{Float64}
bz::Vector{Float64}
sw_hourly::Vector{NTuple{3, Float64}} # pdyn, by, bz per UT hour
g::Vector{NTuple{2, Float64}}
w::Vector{NTuple{6, Float64}}
kp::Vector{Float64}
Expand Down Expand Up @@ -43,10 +44,12 @@ function OMNIDay(day::Date)
den, v, by, bz, symh = map(("proton_density", "flow_speed", "BY_GSM", "BZ_GSM", "SYM_H")) do name
fill_gaps!(_series(hro, name, t0, n, STEP))
end
pdyn = @. 1.937e-6 * den * v^2 # 1.16 mₚ in nPa per (cm⁻³ (km/s)²), Tsyganenko's value
idx = NBEFORE .+ (1:PER_DAY)
hourly = CDAWeb.Dataset("OMNI2_H0_MRG1HR")
return OMNIDay(
@.(1.937e-6 * den[idx] * v[idx]^2), symh[idx], by[idx], bz[idx], # 1.16 mₚ in nPa per (cm⁻³ (km/s)²), Tsyganenko's value
pdyn[idx], symh[idx], by[idx], bz[idx],
[map(x -> hourly_mean(x, NBEFORE + 12h), (pdyn, by, bz)) for h in 1:24],
[t01_g(v, by, bz, i) for i in idx],
ts05_w(den, v, bz)[idx],
_series(hourly, "KP", DateTime(day), 24, Hour(1)),
Expand Down Expand Up @@ -77,8 +80,9 @@ function TsyganenkoModels.inputs(::OMNI, ::Type{<:T89}, t)
end

function TsyganenkoModels.inputs(::OMNI, ::Type{<:T96}, t)
d, i, h, t = _sample(t)
return _checked((; pdyn = d.pdyn[i], dst = d.dst[h], byimf = d.by[i], bzimf = d.bz[i]), t)
d, _, h, t = _sample(t)
pdyn, byimf, bzimf = d.sw_hourly[h]
return _checked((; pdyn, dst = d.dst[h], byimf, bzimf), t)
end

function TsyganenkoModels.inputs(::OMNI, ::Type{<:T01}, t)
Expand Down
16 changes: 14 additions & 2 deletions src/driven.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ evalmodel(m::Driven{M}, x, y, z, t) where {M} = evalmodel(M(inputs(m, t)), x, y,
Parameters from 5-min (and, for Kp and Dst, hourly) OMNI data:

- `pdyn = ρV²` with `ρ = 1.16 mₚN` (He/H = 0.04); gaps of at most 3 hours linearly interpolated, as Tsyganenko prepares TS05 inputs.
- `T89`: `iopt` from Kp. `T96`: hourly Dst. `T01`, `TS04`: SYM-H as `dst`, the index their fits used.
- `T01`: `g1`, `g2` averaged over the valid samples in the hour ending at the 5-min sample containing `t`.
- `T89`: `iopt` from Kp. `T01`, `TS04`: SYM-H as `dst`, the index their fits used.
- `T96`: Dst and the means of the valid `pdyn`, `byimf`, `bzimf` samples over the UT hour containing `t`, as its fit tagged each field average with the concurrent hourly OMNI values ([Tsyganenko, 1996](https://ui.adsabs.harvard.edu/abs/1996ESASP.389..181T); [Tsyganenko, 2002b, ¶6](https://doi.org/10.1029/2001JA000220)).
- `T01`: other inputs at the 5-min sample containing `t`, the resolution of its fit, and `g1`, `g2` averaged over the valid samples in the hour ending there.
- `TS04`: `w1`–`w6` accumulated from 13 days before `t`, when the slowest memory (W3) has decayed below `e⁻¹⁰`; samples in unfilled gaps contribute nothing. Tsyganenko's yearly files instead restart the sums after each gap at the first 2-hour quiet period, and on January 1, discarding the decayed memory of earlier driving that the exponential kernel models.
"""
struct OMNI end
Expand All @@ -48,6 +49,17 @@ function fill_gaps!(x; maxgap = 35)
end
end

# Mean of the valid samples among the 12 5-min samples ending at sample `i`, NaN if none
function hourly_mean(x, i)
s, n = 0.0, 0
for j in (i - 11):i
isnan(x[j]) && continue
s += x[j]
n += 1
end
return s / n
end

# T01 (G1, G2) at sample `i` of 5-min series of solar wind speed `v` [km/s] and IMF `by`, `bz` (GSM) [nT].
# T01 eq. (1), (4), Tsyganenko (2002b), https://doi.org/10.1029/2001JA000220: 12 preceding 5-min samples; we include the current one,
# as the TS05 W sums do.
Expand Down
4 changes: 3 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ end

@testset "Driven models" begin
using Dates
using TsyganenkoModels: inputs, fill_gaps!, t01_g, ts05_w
using TsyganenkoModels: fill_gaps!, hourly_mean, t01_g, ts05_w
t = DateTime(2001, 1, 1, 2, 3, 4)
r = GSM(-5.1, 0.3, 2.8)
param = (; pdyn = 2.0, dst = -87.0, byimf = 2.0, bzimf = -5.0)
Expand All @@ -110,6 +110,8 @@ end
x = [NaN, 1.0, NaN, NaN, 4.0, NaN, NaN, NaN, 8.0, NaN]
@test isequal(fill_gaps!(x; maxgap = 2), [NaN, 1, 2, 3, 4, NaN, NaN, NaN, 8, NaN])

@test hourly_mean([100.0; 1:10; NaN; 50.0], 13) == 105 / 11

# Tsyganenko (2002b) §3.1: V = 400 km/s, By = 0, Bz = -5 nT gives G1 ≈ 6, G2 = 10
n = 4000
v, by, bz = fill(400.0, n), zeros(n), fill(-5.0, n)
Expand Down
Loading