diff --git a/lib/microwaveprop/workers/rover_path_profile_worker.ex b/lib/microwaveprop/workers/rover_path_profile_worker.ex index cee7b4cc..8f1028cc 100644 --- a/lib/microwaveprop/workers/rover_path_profile_worker.ex +++ b/lib/microwaveprop/workers/rover_path_profile_worker.ex @@ -23,14 +23,21 @@ defmodule Microwaveprop.Workers.RoverPathProfileWorker do import Ecto.Query alias Microwaveprop.Geo + alias Microwaveprop.Propagation.BandConfig alias Microwaveprop.Propagation.ScoresFile alias Microwaveprop.Repo alias Microwaveprop.RoverPlanning.Path alias Microwaveprop.Terrain.ElevationClient alias Microwaveprop.Terrain.TerrainAnalysis + # Default surface absolute humidity (g/m³) — ~7.5 is the + # PathLive fallback when no live HRRR sample is available. Used to + # produce a "baseline" cached H₂O loss; the live `/path` page + # recomputes this from current weather. require Logger + @default_abs_humidity_gm3 7.5 + @impl Oban.Worker def backoff(%Oban.Job{attempt: attempt}) do min(60 * Integer.pow(2, attempt - 1), 3600) @@ -89,7 +96,10 @@ defmodule Microwaveprop.Workers.RoverPathProfileWorker do midlon = (rover.lon + station.lon) / 2 propagation_score = lookup_propagation_score(mission.band_mhz, midlat, midlon) - store_complete(path, profile, analysis, dist_km, bearing, propagation_score) + loss_budget = + compute_loss_budget(dist_km, mission.band_mhz, freq_ghz, analysis.diffraction_db) + + store_complete(path, profile, analysis, dist_km, bearing, propagation_score, loss_budget) {:error, reason} -> store_failed(path, "elevation profile failed: #{inspect(reason)}") @@ -101,7 +111,7 @@ defmodule Microwaveprop.Workers.RoverPathProfileWorker do reraise e, __STACKTRACE__ end - defp store_complete(path, profile, analysis, dist_km, bearing, propagation_score) do + defp store_complete(path, profile, analysis, dist_km, bearing, propagation_score, loss_budget) do points = Enum.map(profile, fn p -> %{ @@ -124,6 +134,10 @@ defmodule Microwaveprop.Workers.RoverPathProfileWorker do "verdict" => to_string(analysis.verdict), "sample_count" => length(profile), "propagation_score" => propagation_score, + "free_space_loss_db" => loss_budget.free_space_loss_db, + "oxygen_loss_db" => loss_budget.oxygen_loss_db, + "humidity_loss_db_baseline" => loss_budget.humidity_loss_db_baseline, + "total_baseline_loss_db" => loss_budget.total_baseline_loss_db, "path_points" => points } @@ -173,6 +187,30 @@ defmodule Microwaveprop.Workers.RoverPathProfileWorker do end end + # Cached baseline link-loss components for a path. Mirrors the + # `compute_loss_budget` math in PathLive but without live HRRR + # weather: humidity defaults to @default_abs_humidity_gm3 and rain is + # treated as zero. The `/path` page recomputes these against current + # conditions; this snapshot is what the rover-planning show page + # renders without needing HRRR access. + defp compute_loss_budget(dist_km, band_mhz, freq_ghz, diffraction_db) do + band_config = BandConfig.get(band_mhz) || BandConfig.get(10_000) + freq_mhz = freq_ghz * 1000 + + fspl = 20 * :math.log10(max(dist_km, 0.001)) + 20 * :math.log10(freq_mhz) + 32.44 + o2_loss = (band_config.o2_db_km || 0.0) * dist_km + h2o_loss = (band_config.h2o_coeff || 0.0) * @default_abs_humidity_gm3 * dist_km + diffraction = diffraction_db * 1.0 + total = fspl + o2_loss + h2o_loss + diffraction + + %{ + free_space_loss_db: Float.round(fspl, 1), + oxygen_loss_db: Float.round(o2_loss, 2), + humidity_loss_db_baseline: Float.round(h2o_loss, 2), + total_baseline_loss_db: Float.round(total, 1) + } + end + # Look up the propagation grid score (0-100) for the midpoint of a # path at the most recent forecast time available on disk. Returns # nil when no scores file exists for this band yet — callers (the diff --git a/lib/microwaveprop_web/live/rover_planning_live/show.ex b/lib/microwaveprop_web/live/rover_planning_live/show.ex index 02a7034e..223f0073 100644 --- a/lib/microwaveprop_web/live/rover_planning_live/show.ex +++ b/lib/microwaveprop_web/live/rover_planning_live/show.ex @@ -231,6 +231,9 @@ defmodule MicrowavepropWeb.RoverPlanningLive.Show do defp format_distance(_), do: "—" + defp format_loss(db) when is_number(db), do: "#{Float.round(db * 1.0, 1)} dB" + defp format_loss(_), do: "—" + # Two-line station identity for the path table. Returns # `{primary, secondary}` — primary is the callsign (or grid when no # callsign), secondary is a Maidenhead grid (stored or derived from @@ -476,6 +479,7 @@ defmodule MicrowavepropWeb.RoverPlanningLive.Show do