chore: main 병합 — 노을 명소 가산·공기 보정 변경과 노을 등급 문구 작업 합치기
This commit is contained in:
commit
934566d45c
7 files changed
+146
-64
No files matched your search
@@ -6,10 +6,12 @@ from typing import Optional
|
||||
from common.enums import Confidence, SkyGrade, SkySituation, SkyType
|
||||
|
||||
DISTANCES_KM = (20, 100)
|
||||
# 물들 구름이 하나도 없을 때의 canvas 값. 50% 까지 곧게 올라가 맑은 날도 구름 몇 % 차이로 점수가 갈린다 (맑은 하늘 40점 = 은은함).
|
||||
_CLEAR_CANVAS = 0.2
|
||||
# 물들 구름이 하나도 없을 때의 canvas 값. 50% 까지 곧게 올라가 맑은 날도 구름 몇 % 차이로 점수가 갈린다 (맑은 하늘 55점 = 은은함).
|
||||
_CLEAR_CANVAS = 0.4
|
||||
# 해 지기(뜨기) 이만큼 전 사이에 비가 왔다가 그 시각엔 개면 공기가 맑고 구름이 남아 더 붉다.
|
||||
RAIN_BEFORE_HOURS = (2, 6)
|
||||
# 서쪽이 바다·강·전망대로 트여 해가 지평선까지 보이는 곳은 산·건물에 가리지 않아 노을이 더 잘 보인다.
|
||||
OPEN_WEST_BONUS = 1.1
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
@@ -26,6 +28,7 @@ class Air:
|
||||
humidity: float | None = None
|
||||
rain_before: int | None = None # RAIN_BEFORE_HOURS 사이 가장 높은 비 확률
|
||||
rain_now: int | None = None
|
||||
open_west: bool = False # 가까이 서쪽이 트인 노을 명소가 있음 (노을만)
|
||||
|
||||
|
||||
def score(near20: Layers, far100: Layers) -> int:
|
||||
@@ -37,21 +40,32 @@ def score(near20: Layers, far100: Layers) -> int:
|
||||
return round(100 * (0.75 * f_canvas + 0.25 * f_near) * (0.25 + 0.75 * light))
|
||||
|
||||
|
||||
def _ramp(v: float, bad: float, good: float, low: float) -> float:
|
||||
"""good 쪽은 1.0, bad 쪽은 low, 사이는 직선."""
|
||||
t = max(0.0, min(1.0, (v - bad) / (good - bad)))
|
||||
return low + (1 - low) * t
|
||||
# 공기 보정 기준점 (값, 배수). 평소 공기(가시거리 20km, 습도 60%)가 ×1.0 이고 좋으면 올리고 나쁘면 내린다. 양 끝 밖은 끝 값 그대로.
|
||||
_VISIBILITY_FACTOR = ((5, 0.8), (20, 1.0), (40, 1.12))
|
||||
_HUMIDITY_FACTOR = ((40, 1.06), (60, 1.0), (90, 0.85))
|
||||
|
||||
|
||||
def _interp(v: float, points: tuple[tuple[float, float], ...]) -> float:
|
||||
"""기준점 사이를 직선으로 잇는다."""
|
||||
if v <= points[0][0]:
|
||||
return points[0][1]
|
||||
for (x0, f0), (x1, f1) in zip(points, points[1:]):
|
||||
if v <= x1:
|
||||
return f0 + (f1 - f0) * (v - x0) / (x1 - x0)
|
||||
return points[-1][1]
|
||||
|
||||
|
||||
def air_factor(air: Air) -> float:
|
||||
"""공기로 구름 점수를 보정하는 배수. 뿌연 공기·높은 습도는 색을 흐리게 하고, 비 갠 뒤는 더 붉다."""
|
||||
"""공기로 구름 점수를 보정하는 배수. 맑고 건조하면 올리고 뿌옇거나 습하면 내린다. 비 갠 뒤는 더 붉다."""
|
||||
f = 1.0
|
||||
if air.visibility_km is not None:
|
||||
f *= _ramp(air.visibility_km, 5, 40, 0.6)
|
||||
f *= _interp(air.visibility_km, _VISIBILITY_FACTOR)
|
||||
if air.humidity is not None:
|
||||
f *= _ramp(air.humidity, 90, 50, 0.75)
|
||||
f *= _interp(air.humidity, _HUMIDITY_FACTOR)
|
||||
if air.rain_before is not None and air.rain_before >= 60 and (air.rain_now or 0) < 40:
|
||||
f *= 1.15
|
||||
if air.open_west:
|
||||
f *= OPEN_WEST_BONUS
|
||||
return f
|
||||
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import asyncio
|
||||
import math
|
||||
import statistics
|
||||
from dataclasses import dataclass, field
|
||||
from dataclasses import dataclass, field, replace
|
||||
from datetime import date, datetime, timedelta, timezone
|
||||
from typing import Optional
|
||||
|
||||
@@ -31,6 +31,7 @@ DAYS = 3
|
||||
SPOT_RADIUS_KM = EVENT_RADIUS_KM
|
||||
SPOT_LIMIT = 3
|
||||
_MARKER_TTL = 24 * 3600
|
||||
OPEN_WEST_KM = 20 # 이 안에 서쪽이 트인 명소가 있으면 노을 가산 (부안 채석강·진도 세방낙조가 구 중심에서 10km 넘게 떨어져 있어 20km)
|
||||
_background: set[asyncio.Task] = set()
|
||||
|
||||
|
||||
@@ -222,11 +223,19 @@ def _air(
|
||||
)
|
||||
|
||||
|
||||
def _open_west(region: Region, spots: list[SunsetSpot]) -> bool:
|
||||
"""구 중심 OPEN_WEST_KM 안에 서쪽이 트인 노을 명소가 있는지."""
|
||||
lat, lon = float(region.lat), float(region.lon)
|
||||
return any(haversine_km(lat, lon, float(p.lat), float(p.lon)) <= OPEN_WEST_KM for p in spots)
|
||||
|
||||
|
||||
def _score_rows(
|
||||
region: Region, today: date, clouds: dict, now: datetime,
|
||||
hourly: Optional[dict[tuple[str, datetime], WeatherHourly]] = None, om: Optional[dict[tuple[str, datetime], OpenMeteoHourly]] = None,
|
||||
open_west: bool = False,
|
||||
) -> list[dict]:
|
||||
"""구름 점수(모델 평균)에 공기 보정(가시거리·습도·비 갠 뒤)을 곱한다. 공기 값이 없으면 구름 점수 그대로."""
|
||||
"""구름 점수(모델 평균)에 공기 보정(가시거리·습도·비 갠 뒤)을 곱한다. 공기 값이 없으면 구름 점수 그대로.
|
||||
open_west 면 노을에만 서쪽이 트인 곳 가산을 더한다."""
|
||||
rows = []
|
||||
for kind in (SkyType.SUNSET, SkyType.SUNRISE):
|
||||
for i in range(DAYS):
|
||||
@@ -244,7 +253,7 @@ def _score_rows(
|
||||
model_layers.append(layers)
|
||||
if not per_model:
|
||||
continue
|
||||
air = _air(region.code, sun_at, hourly or {}, om or {})
|
||||
air = replace(_air(region.code, sun_at, hourly or {}, om or {}), open_west=open_west and kind is SkyType.SUNSET)
|
||||
factor = air_factor(air)
|
||||
value = min(100, round(statistics.mean(per_model) * factor))
|
||||
sit = situation(*(mean_layers([m[k] for m in model_layers]) for k in range(len(DISTANCES_KM))))
|
||||
@@ -417,7 +426,10 @@ class SkyService:
|
||||
LOG.error(f"[sky] air openmeteo: {err.name}")
|
||||
hourly = {(w.region_code, w.forecast_at): w for w in hourly_rows or []}
|
||||
om = {(o.region_code, o.forecast_at): o for o in om_rows or []}
|
||||
score_rows = [row for r in regions for row in _score_rows(r, today, clouds, now, hourly, om)]
|
||||
err, west_spots = await DB_SESSION_MNG.execute_lambda(lambda s: self.crud.west_open_spots(s))
|
||||
if err is not ErrorType.SUCCESS:
|
||||
LOG.error(f"[sky] west spots: {err.name}")
|
||||
score_rows = [row for r in regions for row in _score_rows(r, today, clouds, now, hourly, om, _open_west(r, west_spots or []))]
|
||||
err = await DB_SESSION_MNG.execute_lambda_run([
|
||||
lambda s: self.crud.upsert_clouds(s, cloud_rows),
|
||||
lambda s: self.crud.upsert_scores(s, score_rows),
|
||||
|
||||
Reference in new issue
Block a user