prop/test/microwaveprop/weather/hrrr_profile_lookup_test.exs
Graham McIntire a4cd7632eb
feat(skewt): hover crosshair + DB fallback for missing on-disk profiles
Two follow-ups to the earlier /skewt work:

1. Interactive crosshair (matches the SPC-viewer feel from the
   reference screenshot). Mousing over the diagram now drops a
   horizontal pressure cursor with a top-left readout box showing the
   pressure at the cursor's altitude (mb), the height in m and ft,
   and the linearly interpolated temperature and dewpoint at that
   level. Two coloured dots track the T and Td traces along the
   cursor so the user can read the spread visually.

   Mechanics: SkewtSvg.geometry/0 exposes the plot geometry (left,
   right, top, bottom, p_bottom, p_top, t_min, t_max, skew) as JSON;
   SkewtSvg emits an initially-hidden <g class="skewt-cursor"> layer.
   A new Skewt hook in assets/js/app.ts inverts pressure_y to recover
   pressure from the cursor's viewBox-Y, walks the (descending-pres-
   sorted) profile to interp T/Td/height, and updates the elements.
   The hook also handles mouseleave to hide the cursor and uses the
   SVG's screen-CTM so the math stays correct under any container
   aspect-ratio resize.

2. DB fallback for `available_valid_times`/`load_profile`. When the
   on-disk `ProfilesFile` has nothing for the location-and-window
   (true on dev, transient on prod between chain runs), SkewtLive
   now falls back to `HrrrProfileLookup.{list_valid_times_near,
   read_point_near}/3` against the `hrrr_profiles` Postgres table.
   The lookup uses the same ±0.07° spatial tolerance and the
   `(lat, lon, valid_time)` unique index that `Weather.find_nearest_
   hrrr/3` already relies on, so the queries are index-only.

   Returned shape matches `ProfilesFile.read_point/3` so SkewtLive
   reads from either source transparently. NaiveDateTime → UTC
   normalisation is centralised in `ensure_utc/1` so the LiveView
   sees `%DateTime{time_zone: "Etc/UTC"}` regardless of which side
   produced the value.

Tests: 6/6 new HrrrProfileLookupTest cases (list/limit/tolerance,
read nearest cell, nil for far cells, picks closest among matches),
3/3 SkewtLiveTest still green, 2,894 total Elixir tests + 221
properties green via mix test (one pre-existing flaky test in
admin/contact_edit_live re-runs clean). mix credo --strict clean.
mix assets.build clean.
2026-04-25 13:17:58 -05:00

119 lines
4.6 KiB
Elixir

defmodule Microwaveprop.Weather.HrrrProfileLookupTest do
use Microwaveprop.DataCase, async: true
alias Microwaveprop.Weather.HrrrProfile
alias Microwaveprop.Weather.HrrrProfileLookup
describe "list_valid_times_near/3" do
test "returns valid_times of every profile within the spatial tolerance" do
insert_profile!(%{lat: 32.625, lon: -97.125, valid_time: ~U[2026-04-25 14:00:00Z]})
insert_profile!(%{lat: 32.625, lon: -97.125, valid_time: ~U[2026-04-25 15:00:00Z]})
# Far cell — must be excluded.
insert_profile!(%{lat: 40.000, lon: -80.000, valid_time: ~U[2026-04-25 14:00:00Z]})
assert [t1, t2] = HrrrProfileLookup.list_valid_times_near(32.65, -97.13, [])
assert DateTime.to_iso8601(t1) == "2026-04-25T14:00:00Z"
assert DateTime.to_iso8601(t2) == "2026-04-25T15:00:00Z"
end
test "limit option caps the number of times returned (most recent first kept)" do
for h <- 0..4 do
valid_time = DateTime.add(~U[2026-04-25 12:00:00Z], h * 3600, :second)
insert_profile!(%{lat: 32.625, lon: -97.125, valid_time: valid_time})
end
times = HrrrProfileLookup.list_valid_times_near(32.65, -97.13, limit: 3)
assert length(times) == 3
# Capped to the 3 most recent — but still sorted ascending in the
# returned list so SkewtLive's selector renders left-to-right.
assert Enum.map(times, &DateTime.to_iso8601/1) == [
"2026-04-25T14:00:00Z",
"2026-04-25T15:00:00Z",
"2026-04-25T16:00:00Z"
]
end
test "tolerance option widens the spatial search box" do
insert_profile!(%{lat: 32.625, lon: -97.125, valid_time: ~U[2026-04-25 14:00:00Z]})
# 0.20° away, outside the default 0.07° box but inside 0.25°.
insert_profile!(%{lat: 32.825, lon: -97.125, valid_time: ~U[2026-04-25 15:00:00Z]})
tight = HrrrProfileLookup.list_valid_times_near(32.625, -97.125, tolerance_deg: 0.07)
wide = HrrrProfileLookup.list_valid_times_near(32.625, -97.125, tolerance_deg: 0.25)
assert length(tight) == 1
assert length(wide) == 2
end
end
describe "read_point_near/3" do
test "returns the profile of the nearest cell at the requested valid_time" do
insert_profile!(%{
lat: 32.625,
lon: -97.125,
valid_time: ~U[2026-04-25 14:00:00Z],
profile: [
%{"pres" => 1000.0, "hght" => 100.0, "tmpc" => 20.0, "dwpc" => 15.0},
%{"pres" => 850.0, "hght" => 1500.0, "tmpc" => 12.0, "dwpc" => 9.0}
],
surface_temp_c: 20.5,
surface_dewpoint_c: 15.0,
surface_pressure_mb: 1013.0
})
assert %{} = cell = HrrrProfileLookup.read_point_near(~U[2026-04-25 14:00:00Z], 32.65, -97.13)
# Returned cell preserves the profile array shape SkewtSvg expects.
# Postgres jsonb collapses 1000.0 → 1000 on round-trip; accept
# either since the renderer/normalizer handles both.
[%{"pres" => pres} | _] = cell[:profile]
assert pres == 1000.0 or pres == 1000
# Surface scalars are surfaced for SoundingParams.derive/1 reuse.
assert cell[:surface_temp_c] == 20.5
assert cell[:surface_pressure_mb] == 1013.0
end
test "returns nil if no profile is within tolerance at that valid_time" do
assert HrrrProfileLookup.read_point_near(~U[2026-04-25 14:00:00Z], 32.65, -97.13) == nil
end
test "picks the spatially closest cell when multiple match" do
insert_profile!(%{
lat: 32.65,
lon: -97.13,
valid_time: ~U[2026-04-25 14:00:00Z],
profile: [%{"pres" => 1000.0, "hght" => 100.0, "tmpc" => 25.0, "dwpc" => 18.0}]
})
insert_profile!(%{
lat: 32.69,
lon: -97.18,
valid_time: ~U[2026-04-25 14:00:00Z],
profile: [%{"pres" => 1000.0, "hght" => 100.0, "tmpc" => 30.0, "dwpc" => 25.0}]
})
cell = HrrrProfileLookup.read_point_near(~U[2026-04-25 14:00:00Z], 32.65, -97.13)
# Closest cell is the first one (exact match).
assert [%{"tmpc" => 25.0}] = cell[:profile]
end
end
defp insert_profile!(attrs) do
defaults = %{
profile: [%{"pres" => 1000.0, "hght" => 100.0, "tmpc" => 25.0, "dwpc" => 18.0}],
run_time: nil,
hpbl_m: 800.0,
pwat_mm: 25.0,
surface_temp_c: 25.0,
surface_dewpoint_c: 18.0,
surface_pressure_mb: 1013.0,
surface_refractivity: 320.0,
min_refractivity_gradient: -90.0,
ducting_detected: false,
duct_characteristics: [],
is_grid_point: false
}
%HrrrProfile{}
|> HrrrProfile.changeset(Map.merge(defaults, attrs))
|> Microwaveprop.Repo.insert!()
end
end