diff --git a/lib/microwaveprop/weather.ex b/lib/microwaveprop/weather.ex index bab5bed9..36bc4638 100644 --- a/lib/microwaveprop/weather.ex +++ b/lib/microwaveprop/weather.ex @@ -3,6 +3,7 @@ defmodule Microwaveprop.Weather do import Ecto.Query + alias Microwaveprop.Propagation.Grid alias Microwaveprop.Propagation.ProfilesFile alias Microwaveprop.Radio alias Microwaveprop.Radio.Contact @@ -302,10 +303,19 @@ defmodule Microwaveprop.Weather do end @doc """ - Flip `contacts.hrrr_status` from `:queued` to `:complete` for every - contact whose HRRR data is fully present (all path points at the - rounded HRRR hour and grid cell). Same shape as - `reconcile_iemre_statuses/0`. + Reconcile `contacts.hrrr_status` for every `:queued` contact: + + * flips to `:complete` when every path point has an hrrr_profiles + row at the rounded HRRR hour; + * flips to `:unavailable` when any path point falls outside the + HRRR CONUS grid (pos2 or the great-circle midpoint can drift + into Canada, the mid-Atlantic, or the Pacific even when pos1 is + stateside — the Rust hrrr-point-worker returns no profile for + those points and the contact would otherwise sit `:queued` + forever). + + Returns `{:ok, n}` where `n` is the total number of contacts advanced + (completed + oconus). """ @spec reconcile_hrrr_statuses() :: {:ok, non_neg_integer()} def reconcile_hrrr_statuses do @@ -316,15 +326,30 @@ defmodule Microwaveprop.Weather do |> where([c], c.hrrr_status == :queued and not is_nil(c.pos1)) |> Repo.all() - to_complete = Enum.filter(queued, &hrrr_data_fully_present?/1) + {oconus, rest} = Enum.split_with(queued, &contact_has_oconus_path_point?/1) + to_complete = Enum.filter(rest, &hrrr_data_fully_present?/1) - if to_complete == [] do - {:ok, 0} - else - ids = Enum.map(to_complete, & &1.id) - _ = Radio.set_enrichment_status!(ids, :hrrr_status, :complete) - {:ok, length(to_complete)} - end + _ = + if oconus != [] do + ids = Enum.map(oconus, & &1.id) + Radio.set_enrichment_status!(ids, :hrrr_status, :unavailable) + end + + _ = + if to_complete != [] do + ids = Enum.map(to_complete, & &1.id) + Radio.set_enrichment_status!(ids, :hrrr_status, :complete) + end + + {:ok, length(oconus) + length(to_complete)} + end + + defp contact_has_oconus_path_point?(contact) do + contact + |> Radio.contact_path_points() + |> Enum.any?(fn {lat, lon} -> + not Grid.contains?(%{"lat" => lat, "lon" => lon}) + end) end @doc """