feat: 밤 구간(21~06시) Open-Meteo 햇빛 값(일조·일사·자외선) 저장 제외, 지수 계산은 0으로 처리
This commit is contained in:
1 parent
1a1df4bb87
commit
9dacc86f47
6 files changed
+50
-9
No files matched your search
+14
-3
@@ -20,6 +20,14 @@ WEATHER_VARS = (
|
||||
"sunshine_duration", "shortwave_radiation", "soil_temperature_0cm",
|
||||
)
|
||||
SLOT_HOURS = 3
|
||||
# 해가 없는 밤 구간(21~06시 = 끝 시각 KST 0·3·6시)은 햇빛 값(일조·일사·자외선)을 저장하지 않는다.
|
||||
DARK_SLOT_END_KST = (0, 3, 6)
|
||||
_SUN_FIELDS = ("uv_index", "uv_index_clear_sky", "sunshine_s", "shortwave_avg_wm2", "shortwave_max_wm2")
|
||||
_KST_OFFSET = timedelta(hours=9)
|
||||
|
||||
|
||||
def is_dark_slot(end: datetime) -> bool:
|
||||
return (end.astimezone(timezone.utc) + _KST_OFFSET).hour in DARK_SLOT_END_KST
|
||||
|
||||
|
||||
async def fetch_clouds(client: httpx.AsyncClient, points: list[tuple[float, float]], days: int = 3) -> list[dict]:
|
||||
@@ -88,7 +96,7 @@ def _round(v, digits: int = 0):
|
||||
|
||||
def parse_weather(data: dict) -> list[dict]:
|
||||
"""시간별 응답을 3시간 구간(끝 시각 GMT 0·3·6…)으로 묶는다. 일조는 합, 일사는 평균·최대, 나머지는 끝 시각 값.
|
||||
직전 3시간이 다 있는 구간만 만든다 (응답 맨 앞처럼 일부만 있는 구간은 건너뜀)."""
|
||||
직전 3시간이 다 있는 구간만 만든다 (응답 맨 앞처럼 일부만 있는 구간은 건너뜀). 밤 구간은 햇빛 값을 비운다."""
|
||||
h = data["hourly"]
|
||||
times = [datetime.fromisoformat(t).replace(tzinfo=timezone.utc) for t in h["time"]]
|
||||
index = {t: i for i, t in enumerate(times)}
|
||||
@@ -101,7 +109,7 @@ def parse_weather(data: dict) -> list[dict]:
|
||||
continue
|
||||
sun = [h["sunshine_duration"][j] for j in span]
|
||||
rad = [h["shortwave_radiation"][j] for j in span]
|
||||
rows.append({
|
||||
row = {
|
||||
"forecast_at": end,
|
||||
"uv_index": _round(h["uv_index"][i], 2), "uv_index_clear_sky": _round(h["uv_index_clear_sky"][i], 2),
|
||||
"visibility_m": _round(h["visibility"][i]), "wind_gust_ms": _round(h["wind_gusts_10m"][i], 1),
|
||||
@@ -109,5 +117,8 @@ def parse_weather(data: dict) -> list[dict]:
|
||||
"sunshine_s": None if None in sun else int(round(sum(sun))),
|
||||
"shortwave_avg_wm2": None if None in rad else int(round(sum(rad) / len(rad))),
|
||||
"shortwave_max_wm2": None if None in rad else int(round(max(rad))),
|
||||
})
|
||||
}
|
||||
if is_dark_slot(end):
|
||||
row |= dict.fromkeys(_SUN_FIELDS)
|
||||
rows.append(row)
|
||||
return rows
|
||||
@@ -19,6 +19,7 @@ from services.air_service import GRADE_LABEL, AirService
|
||||
from services.calc.index_calc import AXES, Context, DayForecast, Hour, Result, estimate, evaluate
|
||||
from services.calc.moon import illumination
|
||||
from services.calc.sun import sun_times
|
||||
from services.external.open_meteo import is_dark_slot
|
||||
from services.warning_service import WarningService
|
||||
from services.weather_service import WeatherService
|
||||
|
||||
@@ -100,16 +101,18 @@ def _nearest_slot_end_hour(h: int) -> int:
|
||||
|
||||
|
||||
def _om_fields(instant: Optional[OpenMeteoHourly], covering: Optional[OpenMeteoHourly]) -> dict:
|
||||
"""3시간 구간 값을 지수 계산 입력으로 바꾼다. 없으면 빈 값(점수에서 '정보 없음')."""
|
||||
"""3시간 구간 값을 지수 계산 입력으로 바꾼다. 없으면 빈 값(점수에서 '정보 없음'). 밤 구간은 햇빛 값을 받지 않으므로 0으로 본다."""
|
||||
fields = {}
|
||||
if instant is not None:
|
||||
fields |= {
|
||||
"uv": float(instant.uv_index) if instant.uv_index is not None else None,
|
||||
"uv": 0.0 if is_dark_slot(instant.forecast_at) else float(instant.uv_index) if instant.uv_index is not None else None,
|
||||
"visibility_km": instant.visibility_m / 1000 if instant.visibility_m is not None else None,
|
||||
"gust_ms": float(instant.wind_gust_ms) if instant.wind_gust_ms is not None else None,
|
||||
"ground_c": float(instant.soil_temp_c) if instant.soil_temp_c is not None else None,
|
||||
}
|
||||
if covering is not None and covering.sunshine_s is not None:
|
||||
if covering is not None and is_dark_slot(covering.forecast_at):
|
||||
fields["sunshine_ratio"] = 0.0
|
||||
elif covering is not None and covering.sunshine_s is not None:
|
||||
fields["sunshine_ratio"] = min(covering.sunshine_s / 10800, 1.0)
|
||||
return fields
|
||||
|
||||
|
||||
Reference in new issue
Block a user