prop/lib/mix/tasks/hrrr_native_derive.ex
Graham McIntire 864a91fc5c Phase 2 tasks 2.1-2.4: BL turbulence feature computations
Inversion detection module (Propagation.Inversion):
- find_inversion_top/1 walks the native profile to locate the first
  temperature inversion (surface-based or elevated)
- bulk_richardson/3 computes the Richardson number across the
  inversion layer (Ri < 0.25 = turbulent, > 1 = laminar/good)
- shear_magnitude/3 computes the wind shear vector magnitude
- potential_temperature/2 for θ = T*(P0/P)^0.286

Theta-e module (Weather.ThetaE):
- Bolton (1980) equivalent potential temperature
- dewpoint_from_spfh/2 via Magnus-Tetens inversion
- theta_e_jump/3 for the thermodynamic decoupling metric

mix hrrr_native_derive_fields populates inversion_top_m,
bulk_richardson, theta_e_jump_k, and shear_at_top_ms on existing
hrrr_native_profiles rows.

First real data: 2022-08-20 12Z TX profile shows inversion at
186 m, Ri = 0.16 (turbulent), θ_e jump = 0.33 K — consistent with
marginal propagation conditions at that hour.
2026-04-10 08:21:23 -05:00

91 lines
2.5 KiB
Elixir

defmodule Mix.Tasks.HrrrNativeDeriveFields do
@shortdoc "Compute derived turbulence fields on hrrr_native_profiles"
@moduledoc """
Walks all `hrrr_native_profiles` rows that are missing derived
fields (bulk_richardson is nil) and populates them from the raw
level arrays using the Inversion and ThetaE modules.
Idempotent: rows that already have derived fields are skipped.
mix hrrr_native_derive_fields # process all pending
mix hrrr_native_derive_fields --limit 100
"""
use Mix.Task
import Ecto.Query
alias Microwaveprop.Propagation.Inversion
alias Microwaveprop.Repo
alias Microwaveprop.Weather.HrrrNativeProfile
alias Microwaveprop.Weather.ThetaE
@impl Mix.Task
def run(argv) do
Mix.Task.run("app.start")
{opts, _, _} = OptionParser.parse(argv, switches: [limit: :integer])
limit = Keyword.get(opts, :limit, 10_000)
profiles =
HrrrNativeProfile
|> where([p], is_nil(p.bulk_richardson) and p.level_count > 2)
|> limit(^limit)
|> Repo.all()
Mix.shell().info("Deriving fields for #{length(profiles)} profiles...")
count =
Enum.count(profiles, fn profile ->
derived = derive(profile)
if derived do
{1, _} =
HrrrNativeProfile
|> where([p], p.id == ^profile.id)
|> Repo.update_all(set: derived)
true
else
false
end
end)
Mix.shell().info("Updated #{count} profiles with derived turbulence fields.")
end
defp derive(profile) do
p = %{
heights_m: profile.heights_m,
temp_k: profile.temp_k,
spfh: profile.spfh,
pressure_pa: profile.pressure_pa,
u_wind_ms: profile.u_wind_ms,
v_wind_ms: profile.v_wind_ms,
tke_m2s2: profile.tke_m2s2,
level_count: profile.level_count
}
case Inversion.find_inversion_top(p) do
{:ok, %{height_m: top_h, level_idx: top_idx, base_idx: base_idx}} ->
ri = Inversion.bulk_richardson(p, base_idx, top_idx)
shear = Inversion.shear_magnitude(p, base_idx, top_idx)
theta_e_jump = ThetaE.theta_e_jump(p, base_idx, top_idx)
[
inversion_top_m: top_h,
bulk_richardson: ri,
shear_at_top_ms: shear,
theta_e_jump_k: theta_e_jump
]
:none ->
# No inversion — still fill nulls so we know the row was processed
[
inversion_top_m: nil,
bulk_richardson: nil,
shear_at_top_ms: nil,
theta_e_jump_k: nil
]
end
end
end