PSK Reporter is now an always-on data feed, so the calibration
corpus that recalibration will eventually train against can grow
hourly from when the firehose started. One row per (hour, band,
0.125° midpoint cell) joins three sources:
* `pskr_spots_hourly` — observed spot density (truth signal)
* `hrrr_profiles` nearest match at the cell midpoint at hour
boundary (atmospheric features: T, Td, PWAT, P, dN/dh, HPBL,
ducting flag)
* `geomagnetic_observations` latest Kp at the hour (space weather)
Predicted scores are intentionally NOT stored — they're a function
of the algorithm version under evaluation. Storing only features
keeps the corpus stable across every weight refit; the recalibrator
computes predictions on demand.
Components:
* Migration `20260504210756_create_pskr_calibration_samples` —
new table + unique index on (hour, band, midpoint_lat, lon),
plus a midpoint spatial index on `pskr_spots_hourly` to keep
the join cheap.
* `Pskr.CalibrationSample` — schema mirror of the table with
the same `(hour, band, midpoint_lat, lon)` unique constraint.
* `Pskr.CalibrationSampler.build_for_hour/1` — pulls all spots
for the hour, snaps midpoints to the propagation grid (0.125°),
builds an in-memory HRRR index over a ±0.07°/±60 min window,
and bulk-upserts samples. Idempotent — re-runs upsert.
* `Workers.PskrCalibrationWorker` — Oban cron entry on the
`:backfill_enqueue` queue. Default args target the previous
full hour; explicit `hour_utc` arg reruns any hour.
* Cron `25 * * * *` — fires past HRRR analysis publish window
(~HH:50→HH:05) and PSKR aggregator's 60s flush.
Forward-only: PSKR has no historical archive, so the corpus only
grows from when the feed started. Recalibration weight refits
should wait for ~30 days / ~10k samples to cover diurnal and
synoptic variability.
Tests cover cell-snapping, HRRR feature joining, Kp stamping,
median-distance aggregation, mode dedup, idempotent reruns, and
the worker's default-hour and explicit-hour args (12 new tests,
all passing).
Backfill pipeline untouched — none of these changes feed into
contact enrichment.
184 lines
5.8 KiB
Elixir
184 lines
5.8 KiB
Elixir
defmodule Microwaveprop.Pskr.CalibrationSamplerTest do
|
|
use Microwaveprop.DataCase, async: true
|
|
|
|
alias Microwaveprop.Pskr.CalibrationSample
|
|
alias Microwaveprop.Pskr.CalibrationSampler
|
|
alias Microwaveprop.Pskr.SpotHourly
|
|
alias Microwaveprop.Repo
|
|
alias Microwaveprop.SpaceWeather.GeomagneticObservation
|
|
alias Microwaveprop.Weather.HrrrProfile
|
|
|
|
@hour ~U[2026-05-04 18:00:00Z]
|
|
|
|
defp insert_spot!(attrs) do
|
|
{:ok, _} =
|
|
%SpotHourly{}
|
|
|> SpotHourly.changeset(
|
|
Map.merge(
|
|
%{
|
|
hour_utc: @hour,
|
|
band: "10000",
|
|
sender_grid: "EM12KL",
|
|
receiver_grid: "DM43ST",
|
|
spot_count: 1,
|
|
midpoint_lat: 33.0,
|
|
midpoint_lon: -97.0,
|
|
distance_km: 200.0,
|
|
modes: ["FT8"]
|
|
},
|
|
attrs
|
|
)
|
|
)
|
|
|> Repo.insert()
|
|
end
|
|
|
|
defp insert_hrrr!(attrs) do
|
|
{:ok, _} =
|
|
%HrrrProfile{}
|
|
|> HrrrProfile.changeset(
|
|
Map.merge(
|
|
%{
|
|
valid_time: @hour,
|
|
lat: 33.0,
|
|
lon: -97.0,
|
|
run_time: @hour,
|
|
surface_temp_c: 25.0,
|
|
surface_dewpoint_c: 18.0,
|
|
pwat_mm: 30.0,
|
|
surface_pressure_mb: 1013.0,
|
|
min_refractivity_gradient: -100.0,
|
|
hpbl_m: 500.0,
|
|
ducting_detected: false,
|
|
profile: []
|
|
},
|
|
attrs
|
|
)
|
|
)
|
|
|> Repo.insert()
|
|
end
|
|
|
|
defp insert_kp!(value) do
|
|
{:ok, _} =
|
|
%GeomagneticObservation{}
|
|
|> GeomagneticObservation.changeset(%{
|
|
valid_time: DateTime.add(@hour, 60, :second),
|
|
kp_index: value
|
|
})
|
|
|> Repo.insert()
|
|
end
|
|
|
|
describe "build_for_hour/1" do
|
|
test "skips and returns 0 when no spots exist for the hour" do
|
|
assert CalibrationSampler.build_for_hour(@hour) == 0
|
|
assert Repo.aggregate(CalibrationSample, :count) == 0
|
|
end
|
|
|
|
test "builds one sample per (band, snapped midpoint) cell" do
|
|
insert_spot!(%{midpoint_lat: 33.01, midpoint_lon: -97.02})
|
|
insert_spot!(%{midpoint_lat: 33.04, midpoint_lon: -97.03, sender_grid: "EM13", spot_count: 3})
|
|
# Distinct grid cell (>0.125° away)
|
|
insert_spot!(%{midpoint_lat: 35.0, midpoint_lon: -97.0, sender_grid: "EM15"})
|
|
insert_hrrr!(%{lat: 33.0, lon: -97.0})
|
|
|
|
assert CalibrationSampler.build_for_hour(@hour) == 2
|
|
[a, b] = CalibrationSample |> Repo.all() |> Enum.sort_by(& &1.midpoint_lat)
|
|
# Two near-midpoints collapsed; one distant cell stayed separate.
|
|
assert a.spot_count == 4
|
|
assert a.distinct_paths == 2
|
|
assert b.spot_count == 1
|
|
end
|
|
|
|
test "snaps midpoints to the 0.125° propagation grid" do
|
|
insert_spot!(%{midpoint_lat: 33.06, midpoint_lon: -97.07})
|
|
insert_hrrr!(%{lat: 33.0, lon: -97.0})
|
|
|
|
CalibrationSampler.build_for_hour(@hour)
|
|
[sample] = Repo.all(CalibrationSample)
|
|
# 33.06 → nearest 0.125° step → 33.0; -97.07 → -97.125
|
|
assert sample.midpoint_lat == 33.0
|
|
assert sample.midpoint_lon == -97.125
|
|
end
|
|
|
|
test "joins HRRR atmospheric features at the midpoint" do
|
|
insert_spot!(%{})
|
|
|
|
insert_hrrr!(%{
|
|
lat: 33.0,
|
|
lon: -97.0,
|
|
surface_temp_c: 22.5,
|
|
surface_dewpoint_c: 16.0,
|
|
pwat_mm: 28.0,
|
|
min_refractivity_gradient: -150.0,
|
|
hpbl_m: 420.0,
|
|
ducting_detected: true
|
|
})
|
|
|
|
CalibrationSampler.build_for_hour(@hour)
|
|
[sample] = Repo.all(CalibrationSample)
|
|
assert sample.surface_temp_c == 22.5
|
|
assert sample.surface_dewpoint_c == 16.0
|
|
assert sample.pwat_mm == 28.0
|
|
assert sample.min_refractivity_gradient == -150.0
|
|
assert sample.hpbl_m == 420.0
|
|
assert sample.hrrr_ducting_detected == true
|
|
end
|
|
|
|
test "leaves HRRR fields nil when no nearby profile exists" do
|
|
insert_spot!(%{})
|
|
# No HRRR profile inserted at all.
|
|
CalibrationSampler.build_for_hour(@hour)
|
|
[sample] = Repo.all(CalibrationSample)
|
|
assert sample.surface_temp_c == nil
|
|
assert sample.hpbl_m == nil
|
|
assert sample.hrrr_ducting_detected == nil
|
|
end
|
|
|
|
test "stamps the latest Kp from SpaceWeather on every sample in the hour" do
|
|
insert_spot!(%{})
|
|
insert_hrrr!(%{lat: 33.0, lon: -97.0})
|
|
insert_kp!(5)
|
|
|
|
CalibrationSampler.build_for_hour(@hour)
|
|
[sample] = Repo.all(CalibrationSample)
|
|
assert sample.kp_index == 5
|
|
end
|
|
|
|
test "computes median distance across the cell's path mix" do
|
|
insert_spot!(%{distance_km: 100.0, sender_grid: "EM10"})
|
|
insert_spot!(%{distance_km: 200.0, sender_grid: "EM11"})
|
|
insert_spot!(%{distance_km: 300.0, sender_grid: "EM12"})
|
|
insert_hrrr!(%{lat: 33.0, lon: -97.0})
|
|
|
|
CalibrationSampler.build_for_hour(@hour)
|
|
[sample] = Repo.all(CalibrationSample)
|
|
assert sample.median_distance_km == 200.0
|
|
end
|
|
|
|
test "deduplicates modes across the cell's spots" do
|
|
insert_spot!(%{modes: ["FT8"], sender_grid: "EM10"})
|
|
insert_spot!(%{modes: ["FT4", "FT8"], sender_grid: "EM11"})
|
|
insert_hrrr!(%{lat: 33.0, lon: -97.0})
|
|
|
|
CalibrationSampler.build_for_hour(@hour)
|
|
[sample] = Repo.all(CalibrationSample)
|
|
assert Enum.sort(sample.modes) == ["FT4", "FT8"]
|
|
end
|
|
|
|
test "is idempotent — re-running the same hour upserts the row" do
|
|
insert_spot!(%{})
|
|
insert_hrrr!(%{lat: 33.0, lon: -97.0})
|
|
|
|
CalibrationSampler.build_for_hour(@hour)
|
|
first_count = Repo.aggregate(CalibrationSample, :count)
|
|
|
|
# Mutate the spot count and re-run; the sample must update, not duplicate.
|
|
Repo.update_all(SpotHourly, set: [spot_count: 99])
|
|
CalibrationSampler.build_for_hour(@hour)
|
|
second_count = Repo.aggregate(CalibrationSample, :count)
|
|
|
|
assert first_count == second_count
|
|
[sample] = Repo.all(CalibrationSample)
|
|
assert sample.spot_count == 99
|
|
end
|
|
end
|
|
end
|