Commit ·
b00faa3
1
Parent(s): 664512d
Add NWS client, Toto forecast wrapper, and Gradio app
Browse files- src/nws.py: two-step /points → forecastHourly fetcher; returns a
UTC-indexed DataFrame with columns aligned to Ecowitt (temp_f, humidity).
- src/forecast.py: Toto 2.0 (4M) wrapper. Lazy-imports torch/toto2 so the
module is importable without those installed; loads on first call.
- src/plotting.py: per-metric Plotly figure (history + Toto p10/p50/p90
band + NWS overlay + 'now' marker).
- app.py: Gradio Blocks app — three metric plots, 1h TTL cache, refresh
button.
- requirements.txt: pin torch>=2.4.0 and toto-2 from the DataDog/toto repo.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
- app.py +122 -0
- requirements.txt +5 -0
- src/forecast.py +121 -0
- src/nws.py +121 -0
- src/plotting.py +69 -0
app.py
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Toto weather forecasting demo — Gradio app for HuggingFace Spaces.
|
| 2 |
+
|
| 3 |
+
Pulls live data from an Ecowitt GW3000B home weather station, runs Datadog's
|
| 4 |
+
Toto 2.0 (smallest, 4M params) to forecast the next 24h of temperature,
|
| 5 |
+
humidity, and pressure, and shows it next to the National Weather Service
|
| 6 |
+
forecast for the same window.
|
| 7 |
+
"""
|
| 8 |
+
|
| 9 |
+
from __future__ import annotations
|
| 10 |
+
|
| 11 |
+
import os
|
| 12 |
+
import time
|
| 13 |
+
from datetime import datetime, timedelta, timezone
|
| 14 |
+
|
| 15 |
+
import gradio as gr
|
| 16 |
+
import pandas as pd
|
| 17 |
+
|
| 18 |
+
from src import ecowitt, nws
|
| 19 |
+
from src.forecast import forecast_series
|
| 20 |
+
from src.plotting import metric_figure
|
| 21 |
+
|
| 22 |
+
CACHE_TTL_SECONDS = 60 * 60 # 1 hour
|
| 23 |
+
HISTORY_DAYS = 7
|
| 24 |
+
HORIZON_HOURS = 24
|
| 25 |
+
|
| 26 |
+
# Three metrics to forecast. Maps Ecowitt history column → plot config.
|
| 27 |
+
METRICS = [
|
| 28 |
+
{"col": "temp_f", "title": "Outdoor temperature", "y": "°F", "nws_col": "temp_f"},
|
| 29 |
+
{"col": "humidity", "title": "Outdoor humidity", "y": "%", "nws_col": "humidity"},
|
| 30 |
+
{"col": "pressure_inhg", "title": "Barometric pressure", "y": "inHg", "nws_col": None},
|
| 31 |
+
]
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
# --- tiny TTL cache (Gradio has no @st.cache_data equivalent) -------------
|
| 35 |
+
_cache: dict[tuple, tuple[float, object]] = {}
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def cached(ttl: int):
|
| 39 |
+
def deco(fn):
|
| 40 |
+
def wrapper(*args, **kwargs):
|
| 41 |
+
key = (fn.__name__, args, tuple(sorted(kwargs.items())))
|
| 42 |
+
now = time.time()
|
| 43 |
+
hit = _cache.get(key)
|
| 44 |
+
if hit and now - hit[0] < ttl:
|
| 45 |
+
return hit[1]
|
| 46 |
+
out = fn(*args, **kwargs)
|
| 47 |
+
_cache[key] = (now, out)
|
| 48 |
+
return out
|
| 49 |
+
return wrapper
|
| 50 |
+
return deco
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
# --- data fetchers ---------------------------------------------------------
|
| 54 |
+
@cached(CACHE_TTL_SECONDS)
|
| 55 |
+
def fetch_history() -> pd.DataFrame:
|
| 56 |
+
cfg = ecowitt.EcowittConfig.from_env()
|
| 57 |
+
end = datetime.now(timezone.utc).replace(tzinfo=None)
|
| 58 |
+
start = end - timedelta(days=HISTORY_DAYS)
|
| 59 |
+
raw = ecowitt.fetch_history(cfg, start, end, cycle_type="30min", call_back="outdoor,pressure")
|
| 60 |
+
return ecowitt.history_to_dataframe(raw, resample="1h")
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
@cached(CACHE_TTL_SECONDS)
|
| 64 |
+
def fetch_nws() -> pd.DataFrame:
|
| 65 |
+
lat = float(os.environ["LAT"])
|
| 66 |
+
lon = float(os.environ["LON"])
|
| 67 |
+
return nws.hourly_forecast_df(lat, lon, hours=HORIZON_HOURS)
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
# --- main refresh ---------------------------------------------------------
|
| 71 |
+
def refresh():
|
| 72 |
+
history = fetch_history()
|
| 73 |
+
nws_df = fetch_nws()
|
| 74 |
+
now = pd.Timestamp.now(tz="UTC").floor("h")
|
| 75 |
+
|
| 76 |
+
figs = []
|
| 77 |
+
for m in METRICS:
|
| 78 |
+
series = history[m["col"]].dropna()
|
| 79 |
+
toto = forecast_series(series, horizon=HORIZON_HOURS)
|
| 80 |
+
nws_series = (
|
| 81 |
+
nws_df[m["nws_col"]] if (m["nws_col"] and m["nws_col"] in nws_df.columns) else None
|
| 82 |
+
)
|
| 83 |
+
figs.append(
|
| 84 |
+
metric_figure(
|
| 85 |
+
history=series.tail(HISTORY_DAYS * 24),
|
| 86 |
+
toto=toto,
|
| 87 |
+
nws=nws_series,
|
| 88 |
+
title=m["title"],
|
| 89 |
+
y_label=m["y"],
|
| 90 |
+
now=now,
|
| 91 |
+
)
|
| 92 |
+
)
|
| 93 |
+
return figs[0], figs[1], figs[2]
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
# --- UI -------------------------------------------------------------------
|
| 97 |
+
HOOK = (
|
| 98 |
+
"**Language models predict the next token. "
|
| 99 |
+
"What if you could predict the future with the same technology?**"
|
| 100 |
+
)
|
| 101 |
+
SUBTITLE = (
|
| 102 |
+
"Live readings from my Ecowitt GW3000B + a 24h forecast from "
|
| 103 |
+
"[Datadog's Toto 2.0 (4M)](https://huggingface.co/Datadog/Toto-2.0-4m), "
|
| 104 |
+
"compared against the [NWS hourly forecast](https://www.weather.gov/documentation/services-web-api)."
|
| 105 |
+
)
|
| 106 |
+
|
| 107 |
+
with gr.Blocks(title="Toto Weather Forecast") as demo:
|
| 108 |
+
gr.Markdown("# Toto on my home weather station")
|
| 109 |
+
gr.Markdown(HOOK)
|
| 110 |
+
gr.Markdown(SUBTITLE)
|
| 111 |
+
refresh_btn = gr.Button("Refresh forecast", variant="primary")
|
| 112 |
+
temp_plot = gr.Plot(label="Temperature")
|
| 113 |
+
humidity_plot = gr.Plot(label="Humidity")
|
| 114 |
+
pressure_plot = gr.Plot(label="Pressure")
|
| 115 |
+
|
| 116 |
+
outputs = [temp_plot, humidity_plot, pressure_plot]
|
| 117 |
+
demo.load(refresh, outputs=outputs)
|
| 118 |
+
refresh_btn.click(refresh, outputs=outputs)
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
if __name__ == "__main__":
|
| 122 |
+
demo.launch()
|
requirements.txt
CHANGED
|
@@ -4,3 +4,8 @@ numpy
|
|
| 4 |
plotly
|
| 5 |
requests
|
| 6 |
python-dotenv
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
plotly
|
| 5 |
requests
|
| 6 |
python-dotenv
|
| 7 |
+
|
| 8 |
+
# Toto 2.0 inference. The package isn't on PyPI; install from the
|
| 9 |
+
# DataDog/toto repo's toto2/ subdirectory.
|
| 10 |
+
torch>=2.4.0
|
| 11 |
+
toto-2 @ git+https://github.com/DataDog/toto.git#subdirectory=toto2
|
src/forecast.py
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Toto 2.0 inference wrapper.
|
| 2 |
+
|
| 3 |
+
We use the smallest Toto 2.0 variant (4M params) for speed on CPU. The model
|
| 4 |
+
is downloaded from the HuggingFace Hub on first use and cached.
|
| 5 |
+
|
| 6 |
+
API confirmed against DataDog/toto's `toto2/notebooks/quick_start.ipynb`:
|
| 7 |
+
|
| 8 |
+
from toto2 import Toto2Model
|
| 9 |
+
model = Toto2Model.from_pretrained("Datadog/Toto-2.0-4m", map_location=device)
|
| 10 |
+
quantiles = model.forecast(
|
| 11 |
+
{"target": ..., "target_mask": ..., "series_ids": ...},
|
| 12 |
+
horizon=H,
|
| 13 |
+
)
|
| 14 |
+
# quantiles shape: (9, batch, n_var, horizon)
|
| 15 |
+
# quantile levels: [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]
|
| 16 |
+
"""
|
| 17 |
+
|
| 18 |
+
from __future__ import annotations
|
| 19 |
+
|
| 20 |
+
from dataclasses import dataclass
|
| 21 |
+
|
| 22 |
+
import numpy as np
|
| 23 |
+
import pandas as pd
|
| 24 |
+
|
| 25 |
+
DEFAULT_MODEL_ID = "Datadog/Toto-2.0-4m"
|
| 26 |
+
|
| 27 |
+
# Index into the 9-quantile output.
|
| 28 |
+
Q10_IDX = 0
|
| 29 |
+
Q50_IDX = 4
|
| 30 |
+
Q90_IDX = 8
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
@dataclass
|
| 34 |
+
class TotoForecast:
|
| 35 |
+
"""One metric's forecast.
|
| 36 |
+
|
| 37 |
+
`index` is a future-timestamp DatetimeIndex; `median`, `p10`, `p90` are
|
| 38 |
+
pandas Series aligned to it.
|
| 39 |
+
"""
|
| 40 |
+
median: pd.Series
|
| 41 |
+
p10: pd.Series
|
| 42 |
+
p90: pd.Series
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
_MODEL_CACHE: dict[str, object] = {}
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
def load_model(model_id: str = DEFAULT_MODEL_ID, device: str = "cpu"):
|
| 49 |
+
"""Lazy-load + cache the Toto model. Imports torch lazily so this module
|
| 50 |
+
is importable in environments without torch (local dev on Intel mac)."""
|
| 51 |
+
if model_id in _MODEL_CACHE:
|
| 52 |
+
return _MODEL_CACHE[model_id]
|
| 53 |
+
|
| 54 |
+
import torch # noqa: PLC0415
|
| 55 |
+
from toto2 import Toto2Model # noqa: PLC0415
|
| 56 |
+
|
| 57 |
+
actual_device = device if (device != "cuda" or torch.cuda.is_available()) else "cpu"
|
| 58 |
+
model = Toto2Model.from_pretrained(model_id, map_location=actual_device)
|
| 59 |
+
model = model.to(actual_device).eval()
|
| 60 |
+
_MODEL_CACHE[model_id] = model
|
| 61 |
+
return model
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
def _series_freq(series: pd.Series) -> pd.Timedelta:
|
| 65 |
+
"""Infer the spacing of a regular time series; default to 1 hour."""
|
| 66 |
+
if len(series.index) < 2:
|
| 67 |
+
return pd.Timedelta("1h")
|
| 68 |
+
diffs = pd.Series(series.index).diff().dropna()
|
| 69 |
+
if diffs.empty:
|
| 70 |
+
return pd.Timedelta("1h")
|
| 71 |
+
return diffs.median()
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
def forecast_series(
|
| 75 |
+
series: pd.Series,
|
| 76 |
+
horizon: int = 24,
|
| 77 |
+
model_id: str = DEFAULT_MODEL_ID,
|
| 78 |
+
device: str = "cpu",
|
| 79 |
+
) -> TotoForecast:
|
| 80 |
+
"""Univariate forecast for one metric.
|
| 81 |
+
|
| 82 |
+
`series` must be regularly-spaced and have a DatetimeIndex (UTC). Returns
|
| 83 |
+
median, p10, p90 over `horizon` future steps at the same cadence.
|
| 84 |
+
"""
|
| 85 |
+
import torch # noqa: PLC0415
|
| 86 |
+
|
| 87 |
+
if series.empty:
|
| 88 |
+
raise ValueError("Cannot forecast an empty series")
|
| 89 |
+
|
| 90 |
+
clean = series.astype(float).interpolate(limit_direction="both")
|
| 91 |
+
arr = clean.to_numpy(dtype=np.float32)
|
| 92 |
+
|
| 93 |
+
target = torch.from_numpy(arr).unsqueeze(0).unsqueeze(0) # (1, 1, T)
|
| 94 |
+
target_mask = torch.ones_like(target, dtype=torch.bool)
|
| 95 |
+
series_ids = torch.zeros(1, 1, dtype=torch.long)
|
| 96 |
+
|
| 97 |
+
model = load_model(model_id, device=device)
|
| 98 |
+
target = target.to(device)
|
| 99 |
+
target_mask = target_mask.to(device)
|
| 100 |
+
series_ids = series_ids.to(device)
|
| 101 |
+
|
| 102 |
+
with torch.no_grad():
|
| 103 |
+
quantiles = model.forecast(
|
| 104 |
+
{"target": target, "target_mask": target_mask, "series_ids": series_ids},
|
| 105 |
+
horizon=horizon,
|
| 106 |
+
)
|
| 107 |
+
# quantiles: (9, 1, 1, horizon) → grab three quantile slices
|
| 108 |
+
q = quantiles.detach().cpu().numpy()
|
| 109 |
+
p10 = q[Q10_IDX, 0, 0]
|
| 110 |
+
p50 = q[Q50_IDX, 0, 0]
|
| 111 |
+
p90 = q[Q90_IDX, 0, 0]
|
| 112 |
+
|
| 113 |
+
freq = _series_freq(clean)
|
| 114 |
+
last_ts = clean.index[-1]
|
| 115 |
+
future_idx = pd.date_range(start=last_ts + freq, periods=horizon, freq=freq, tz=last_ts.tz)
|
| 116 |
+
|
| 117 |
+
return TotoForecast(
|
| 118 |
+
median=pd.Series(p50, index=future_idx, name=f"{series.name}_median"),
|
| 119 |
+
p10=pd.Series(p10, index=future_idx, name=f"{series.name}_p10"),
|
| 120 |
+
p90=pd.Series(p90, index=future_idx, name=f"{series.name}_p90"),
|
| 121 |
+
)
|
src/nws.py
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""National Weather Service forecast client.
|
| 2 |
+
|
| 3 |
+
Two-step flow per https://www.weather.gov/documentation/services-web-api:
|
| 4 |
+
|
| 5 |
+
GET /points/{lat},{lon} → properties.forecastHourly (URL)
|
| 6 |
+
GET <forecastHourly URL> → properties.periods[] (hourly forecast)
|
| 7 |
+
|
| 8 |
+
A `User-Agent` header is required; NWS uses it as a contact string and may
|
| 9 |
+
block requests without one. No auth, no API key.
|
| 10 |
+
|
| 11 |
+
Run standalone:
|
| 12 |
+
|
| 13 |
+
python -m src.nws # uses LAT / LON from .env
|
| 14 |
+
python -m src.nws --hours 24
|
| 15 |
+
"""
|
| 16 |
+
|
| 17 |
+
from __future__ import annotations
|
| 18 |
+
|
| 19 |
+
import argparse
|
| 20 |
+
import os
|
| 21 |
+
import sys
|
| 22 |
+
from datetime import datetime
|
| 23 |
+
from typing import Any
|
| 24 |
+
|
| 25 |
+
import pandas as pd
|
| 26 |
+
import requests
|
| 27 |
+
|
| 28 |
+
from . import ecowitt # for _load_dotenv_if_present
|
| 29 |
+
|
| 30 |
+
# NWS asks for a contact string. Update if you fork.
|
| 31 |
+
USER_AGENT = "toto-weather-demo/0.1 (https://huggingface.co/spaces; lettieri.christopher@gmail.com)"
|
| 32 |
+
|
| 33 |
+
POINTS_URL = "https://api.weather.gov/points/{lat},{lon}"
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def _get(url: str, timeout: int = 30) -> dict[str, Any]:
|
| 37 |
+
r = requests.get(url, headers={"User-Agent": USER_AGENT, "Accept": "application/geo+json"}, timeout=timeout)
|
| 38 |
+
r.raise_for_status()
|
| 39 |
+
return r.json()
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def fetch_forecast_hourly_url(lat: float, lon: float) -> str:
|
| 43 |
+
"""First leg: resolve the forecast grid for this lat/lon."""
|
| 44 |
+
body = _get(POINTS_URL.format(lat=lat, lon=lon))
|
| 45 |
+
url = body.get("properties", {}).get("forecastHourly")
|
| 46 |
+
if not url:
|
| 47 |
+
raise RuntimeError(f"No forecastHourly URL in /points response: {body}")
|
| 48 |
+
return url
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
def fetch_hourly_periods(forecast_hourly_url: str) -> list[dict]:
|
| 52 |
+
body = _get(forecast_hourly_url)
|
| 53 |
+
return body.get("properties", {}).get("periods", []) or []
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
def _f_from_period(p: dict) -> float | None:
|
| 57 |
+
"""Return temperature in °F regardless of how NWS reports it."""
|
| 58 |
+
val = p.get("temperature")
|
| 59 |
+
if val is None:
|
| 60 |
+
return None
|
| 61 |
+
unit = (p.get("temperatureUnit") or "").upper()
|
| 62 |
+
if unit == "F":
|
| 63 |
+
return float(val)
|
| 64 |
+
if unit == "C":
|
| 65 |
+
return float(val) * 9.0 / 5.0 + 32.0
|
| 66 |
+
return float(val)
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
def _quantity_value(node: dict | None) -> float | None:
|
| 70 |
+
"""NWS quantity nodes look like {'unitCode': 'wmoUnit:percent', 'value': 65}."""
|
| 71 |
+
if not isinstance(node, dict):
|
| 72 |
+
return None
|
| 73 |
+
v = node.get("value")
|
| 74 |
+
return None if v is None else float(v)
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
def hourly_forecast_df(lat: float, lon: float, hours: int = 48) -> pd.DataFrame:
|
| 78 |
+
"""Return a UTC-indexed DataFrame with NWS forecast columns aligned to
|
| 79 |
+
Ecowitt's column names where possible (`temp_f`, `humidity`)."""
|
| 80 |
+
url = fetch_forecast_hourly_url(lat, lon)
|
| 81 |
+
periods = fetch_hourly_periods(url)
|
| 82 |
+
if not periods:
|
| 83 |
+
return pd.DataFrame()
|
| 84 |
+
|
| 85 |
+
rows = []
|
| 86 |
+
for p in periods[:hours]:
|
| 87 |
+
# startTime is ISO-8601 with offset, e.g. "2026-05-10T14:00:00-04:00"
|
| 88 |
+
ts = pd.to_datetime(p["startTime"], utc=True)
|
| 89 |
+
rows.append(
|
| 90 |
+
{
|
| 91 |
+
"ts": ts,
|
| 92 |
+
"temp_f": _f_from_period(p),
|
| 93 |
+
"humidity": _quantity_value(p.get("relativeHumidity")),
|
| 94 |
+
"dewpoint_c": _quantity_value(p.get("dewpoint")),
|
| 95 |
+
"precip_prob": _quantity_value(p.get("probabilityOfPrecipitation")),
|
| 96 |
+
"wind_speed": p.get("windSpeed"),
|
| 97 |
+
"wind_direction": p.get("windDirection"),
|
| 98 |
+
"short_forecast": p.get("shortForecast"),
|
| 99 |
+
}
|
| 100 |
+
)
|
| 101 |
+
df = pd.DataFrame(rows).set_index("ts").sort_index()
|
| 102 |
+
return df
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
def main(argv: list[str]) -> int:
|
| 106 |
+
parser = argparse.ArgumentParser(description=__doc__)
|
| 107 |
+
parser.add_argument("--hours", type=int, default=24)
|
| 108 |
+
args = parser.parse_args(argv)
|
| 109 |
+
|
| 110 |
+
ecowitt._load_dotenv_if_present()
|
| 111 |
+
lat = float(os.environ["LAT"])
|
| 112 |
+
lon = float(os.environ["LON"])
|
| 113 |
+
|
| 114 |
+
df = hourly_forecast_df(lat, lon, hours=args.hours)
|
| 115 |
+
print(df.to_string())
|
| 116 |
+
print(f"\nshape: {df.shape}, range: {df.index.min()} → {df.index.max()}")
|
| 117 |
+
return 0
|
| 118 |
+
|
| 119 |
+
|
| 120 |
+
if __name__ == "__main__":
|
| 121 |
+
sys.exit(main(sys.argv[1:]))
|
src/plotting.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Plotly figure builders for the Toto weather demo."""
|
| 2 |
+
|
| 3 |
+
from __future__ import annotations
|
| 4 |
+
|
| 5 |
+
import pandas as pd
|
| 6 |
+
import plotly.graph_objects as go
|
| 7 |
+
|
| 8 |
+
from .forecast import TotoForecast
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def metric_figure(
|
| 12 |
+
history: pd.Series,
|
| 13 |
+
toto: TotoForecast,
|
| 14 |
+
nws: pd.Series | None,
|
| 15 |
+
title: str,
|
| 16 |
+
y_label: str,
|
| 17 |
+
now: pd.Timestamp | None = None,
|
| 18 |
+
) -> go.Figure:
|
| 19 |
+
fig = go.Figure()
|
| 20 |
+
|
| 21 |
+
# Past actuals
|
| 22 |
+
fig.add_trace(
|
| 23 |
+
go.Scatter(
|
| 24 |
+
x=history.index, y=history.values,
|
| 25 |
+
name="Ecowitt (past)", mode="lines",
|
| 26 |
+
line=dict(color="#222", width=2),
|
| 27 |
+
)
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
# Toto p10–p90 band
|
| 31 |
+
fig.add_trace(
|
| 32 |
+
go.Scatter(
|
| 33 |
+
x=list(toto.p90.index) + list(toto.p10.index[::-1]),
|
| 34 |
+
y=list(toto.p90.values) + list(toto.p10.values[::-1]),
|
| 35 |
+
fill="toself", fillcolor="rgba(31,119,180,0.18)",
|
| 36 |
+
line=dict(width=0), hoverinfo="skip",
|
| 37 |
+
name="Toto 10–90% interval",
|
| 38 |
+
)
|
| 39 |
+
)
|
| 40 |
+
# Toto median
|
| 41 |
+
fig.add_trace(
|
| 42 |
+
go.Scatter(
|
| 43 |
+
x=toto.median.index, y=toto.median.values,
|
| 44 |
+
name="Toto median", mode="lines",
|
| 45 |
+
line=dict(color="#1f77b4", width=2, dash="dash"),
|
| 46 |
+
)
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
if nws is not None and not nws.empty:
|
| 50 |
+
fig.add_trace(
|
| 51 |
+
go.Scatter(
|
| 52 |
+
x=nws.index, y=nws.values,
|
| 53 |
+
name="NWS forecast", mode="lines",
|
| 54 |
+
line=dict(color="#d62728", width=2, dash="dot"),
|
| 55 |
+
)
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
if now is not None:
|
| 59 |
+
fig.add_vline(x=now, line=dict(color="#888", dash="dot", width=1))
|
| 60 |
+
|
| 61 |
+
fig.update_layout(
|
| 62 |
+
title=title,
|
| 63 |
+
xaxis_title="Time (UTC)",
|
| 64 |
+
yaxis_title=y_label,
|
| 65 |
+
hovermode="x unified",
|
| 66 |
+
margin=dict(l=40, r=20, t=50, b=40),
|
| 67 |
+
legend=dict(orientation="h", yanchor="bottom", y=1.02, xanchor="right", x=1),
|
| 68 |
+
)
|
| 69 |
+
return fig
|