import statistics from dataclasses import dataclass from datetime import datetime, timedelta from common.enums import Confidence, SkyGrade, SkyType DISTANCES_KM = (20, 50, 100) @dataclass(frozen=True) class Layers: low: float mid: float high: float def score(near20: Layers, near50: Layers, far100: Layers) -> int: """머리 위(20·50km)는 중·상층 구름이 알맞게 있어야 물들고, 먼 쪽(100km)이 막히면 햇빛이 못 들어와 전체가 깎인다.""" canvas = (max(near20.mid, near20.high) + max(near50.mid, near50.high)) / 2 f_canvas = 0.3 if canvas < 10 else max(0.0, 1 - abs(canvas - 50) / 50) f_near = 1 - 0.8 * (near20.low + near50.low) / 200 light = 1 - min(far100.low + 0.5 * far100.mid, 100) / 100 return round(100 * (0.75 * f_canvas + 0.25 * f_near) * (0.25 + 0.75 * light)) def grade(value: int) -> SkyGrade: if value >= 80: return SkyGrade.RED if value >= 60: return SkyGrade.GLOW if value >= 40: return SkyGrade.SOFT return SkyGrade.GRAY def confidence(model_scores: list[int]) -> Confidence: """모델끼리 점수가 비슷할수록 확신이 높다.""" if len(model_scores) < 2: return Confidence.LOW spread = statistics.pstdev(model_scores) return Confidence.HIGH if spread < 8 else Confidence.NORMAL if spread < 15 else Confidence.LOW def best_times(kind: SkyType, sun_at: datetime, twilight_at: datetime, high_cloud: float) -> list[dict]: """노을은 해 진 뒤 10~20분이 가장 붉다. 높은 구름이 많을수록 늦게까지 물든다.""" peak_min = 10 + round(min(high_cloud, 100) / 10) sun_at, twilight_at = sun_at.replace(second=0, microsecond=0), twilight_at.replace(second=0, microsecond=0) if kind is SkyType.SUNSET: return [ {"at": sun_at.isoformat(), "kind": "sun", "label": "해 져요"}, {"at": (sun_at + timedelta(minutes=peak_min)).isoformat(), "kind": "peak", "label": "가장 붉을 때"}, {"at": twilight_at.isoformat(), "kind": "twilight", "label": "보랏빛 하늘"}, ] return [ {"at": twilight_at.isoformat(), "kind": "twilight", "label": "푸른 새벽"}, {"at": (sun_at - timedelta(minutes=peak_min)).isoformat(), "kind": "peak", "label": "가장 붉을 때"}, {"at": sun_at.isoformat(), "kind": "sun", "label": "해 떠요"}, ] GRADE_LABEL = {SkyGrade.RED: "붉음", SkyGrade.GLOW: "고움", SkyGrade.SOFT: "은은함", SkyGrade.GRAY: "흐림"}