Add Weather.sync_stations! to import ASOS/RAOB stations from IEM
This commit is contained in:
parent
68fdd1d2b6
commit
45026437da
1 changed files with 31 additions and 0 deletions
|
|
@ -3,7 +3,10 @@ defmodule Microwaveprop.Weather do
|
|||
|
||||
import Ecto.Query
|
||||
|
||||
require Logger
|
||||
|
||||
alias Microwaveprop.Repo
|
||||
alias Microwaveprop.Weather.IemClient
|
||||
alias Microwaveprop.Weather.HrrrProfile
|
||||
alias Microwaveprop.Weather.IemreObservation
|
||||
alias Microwaveprop.Weather.SolarIndex
|
||||
|
|
@ -93,6 +96,34 @@ defmodule Microwaveprop.Weather do
|
|||
|> MapSet.new()
|
||||
end
|
||||
|
||||
def sync_stations! do
|
||||
asos = for s <- ~w(AK AL AR AZ CA CO CT DE FL GA HI IA ID IL IN KS KY LA MA MD ME MI MN MO MS MT NC ND NE NH NJ NM NV NY OH OK OR PA RI SC SD TN TX UT VA VT WA WI WV WY), do: "#{s}_ASOS"
|
||||
|
||||
for network <- asos ++ ["RAOB"] do
|
||||
type = if String.contains?(network, "ASOS"), do: "asos", else: "sounding"
|
||||
|
||||
case IemClient.fetch_network(network) do
|
||||
{:ok, stations} ->
|
||||
for s <- stations do
|
||||
%Station{}
|
||||
|> Station.changeset(Map.put(s, :station_type, type))
|
||||
|> Repo.insert(on_conflict: :nothing, conflict_target: [:station_code, :station_type])
|
||||
end
|
||||
|
||||
Logger.info("Synced #{length(stations)} stations from #{network}")
|
||||
|
||||
{:error, e} ->
|
||||
Logger.warning("Failed to sync #{network}: #{inspect(e)}")
|
||||
end
|
||||
|
||||
Process.sleep(200)
|
||||
end
|
||||
|
||||
count = Repo.aggregate(Station, :count)
|
||||
Logger.info("Weather stations sync complete: #{count} total")
|
||||
:ok
|
||||
end
|
||||
|
||||
def nearby_stations(lat, lon, station_type, radius_km) do
|
||||
dlat = radius_km / @km_per_deg_lat
|
||||
dlon = radius_km / (@km_per_deg_lat * :math.cos(lat * :math.pi() / 180))
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue