maj via codex
This commit is contained in:
@@ -32,6 +32,46 @@ _DAILY_FIELDS = [
|
||||
"et0_fao_evapotranspiration",
|
||||
]
|
||||
|
||||
_HOURLY_FIELDS = [
|
||||
"soil_temperature_0cm",
|
||||
]
|
||||
|
||||
|
||||
def _to_float(value: Any) -> float | None:
|
||||
if value is None:
|
||||
return None
|
||||
try:
|
||||
return float(value)
|
||||
except (TypeError, ValueError):
|
||||
return None
|
||||
|
||||
|
||||
def _value_at(values: list[Any], index: int, default: Any = None) -> Any:
|
||||
if index < 0 or index >= len(values):
|
||||
return default
|
||||
return values[index]
|
||||
|
||||
|
||||
def _daily_soil_average(raw: dict[str, Any]) -> dict[str, float]:
|
||||
"""Construit un mapping ISO-date -> moyenne de soil_temperature_0cm."""
|
||||
hourly = raw.get("hourly", {})
|
||||
times = hourly.get("time", []) or []
|
||||
soils = hourly.get("soil_temperature_0cm", []) or []
|
||||
by_day: dict[str, list[float]] = {}
|
||||
|
||||
for idx, ts in enumerate(times):
|
||||
soil = _to_float(_value_at(soils, idx))
|
||||
if soil is None or not isinstance(ts, str) or len(ts) < 10:
|
||||
continue
|
||||
day = ts[:10]
|
||||
by_day.setdefault(day, []).append(soil)
|
||||
|
||||
return {
|
||||
day: round(sum(vals) / len(vals), 2)
|
||||
for day, vals in by_day.items()
|
||||
if vals
|
||||
}
|
||||
|
||||
|
||||
def fetch_and_store_forecast(lat: float = METEO_LAT, lon: float = METEO_LON) -> list[dict]:
|
||||
"""Appelle Open-Meteo et retourne la liste des jours (past_days=7 + forecast=8).
|
||||
@@ -50,6 +90,8 @@ def fetch_and_store_forecast(lat: float = METEO_LAT, lon: float = METEO_LON) ->
|
||||
]
|
||||
for field in _DAILY_FIELDS:
|
||||
params.append(("daily", field))
|
||||
for field in _HOURLY_FIELDS:
|
||||
params.append(("hourly", field))
|
||||
|
||||
try:
|
||||
r = httpx.get(url, params=params, timeout=15)
|
||||
@@ -61,22 +103,23 @@ def fetch_and_store_forecast(lat: float = METEO_LAT, lon: float = METEO_LON) ->
|
||||
|
||||
daily = raw.get("daily", {})
|
||||
dates = daily.get("time", [])
|
||||
soil_by_day = _daily_soil_average(raw)
|
||||
now_iso = datetime.now(timezone.utc).isoformat()
|
||||
rows = []
|
||||
|
||||
for i, d in enumerate(dates):
|
||||
code = int(daily.get("weather_code", [0] * len(dates))[i] or 0)
|
||||
code = int(_value_at(daily.get("weather_code", []), i, 0) or 0)
|
||||
row = {
|
||||
"date": d,
|
||||
"t_min": daily.get("temperature_2m_min", [None] * len(dates))[i],
|
||||
"t_max": daily.get("temperature_2m_max", [None] * len(dates))[i],
|
||||
"pluie_mm": daily.get("precipitation_sum", [0] * len(dates))[i] or 0.0,
|
||||
"vent_kmh": daily.get("wind_speed_10m_max", [0] * len(dates))[i] or 0.0,
|
||||
"t_min": _to_float(_value_at(daily.get("temperature_2m_min", []), i)),
|
||||
"t_max": _to_float(_value_at(daily.get("temperature_2m_max", []), i)),
|
||||
"pluie_mm": _to_float(_value_at(daily.get("precipitation_sum", []), i, 0.0)) or 0.0,
|
||||
"vent_kmh": _to_float(_value_at(daily.get("wind_speed_10m_max", []), i, 0.0)) or 0.0,
|
||||
"wmo": code,
|
||||
"label": WMO_LABELS.get(code, f"Code {code}"),
|
||||
"humidite_moy": daily.get("relative_humidity_2m_max", [None] * len(dates))[i],
|
||||
"sol_0cm": None, # soil_temperature_0cm est hourly uniquement
|
||||
"etp_mm": daily.get("et0_fao_evapotranspiration", [None] * len(dates))[i],
|
||||
"humidite_moy": _to_float(_value_at(daily.get("relative_humidity_2m_max", []), i)),
|
||||
"sol_0cm": soil_by_day.get(d),
|
||||
"etp_mm": _to_float(_value_at(daily.get("et0_fao_evapotranspiration", []), i)),
|
||||
"fetched_at": now_iso,
|
||||
}
|
||||
rows.append(row)
|
||||
|
||||
Reference in New Issue
Block a user