"""나들이 점수 바꾸기 표(services/calc/outing_score_table.json)를 다시 만든다. 나들이 점수(index_calc.outing: 낮 10~17시, 기온·비·하늘·바람)의 원래 점수를 1년 치 분포의 순위로 보고, 평균 60·표준편차 15 정규분포의 같은 위치 점수로 바꾼다 (지금 날씨 점수와 같은 방식, scripts/gen_weather_score_table.py). 1년 치 날씨는 Open-Meteo 과거 예보로, region.csv 에서 8개마다 1곳(32곳)을 쓴다. 하늘은 구름양 40%·80% 이상을 구름 많음·흐림으로 본다. 나들이 점수 규칙(OUTING_*)이 바뀌면 실행: cd backend && python scripts/gen_outing_score_table.py """ import csv import json import os import sys import time from datetime import date, timedelta sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) from common.enums import RainType, Sky # noqa: E402 from common.logger import LOG # noqa: E402 from scripts.gen_weather_score_table import _WEATHER, DAYS, EVERY, MEAN, SIGMA, fetch, straighten_top, table # noqa: E402 from services.calc.index_calc import OUTING_HOURS, Hour, outing # noqa: E402 BACKEND = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) REGIONS = os.path.join(BACKEND, "seed", "region.csv") OUT = os.path.join(BACKEND, "services", "calc", "outing_score_table.json") _HOURLY = "apparent_temperature,precipitation,precipitation_probability,wind_speed_10m,cloud_cover" def region_scores(lat: float, lon: float, start: date, end: date) -> list[int]: """한 지점의 날마다 나들이 점수(원래 점수). 10~17시가 모두 있는 날만.""" base = dict(latitude=lat, longitude=lon, start_date=start.isoformat(), end_date=end.isoformat(), timezone="Asia/Seoul") w = fetch(_WEATHER, base | dict(hourly=_HOURLY, wind_speed_unit="ms"))["hourly"] at_index = {t: i for i, t in enumerate(w["time"])} out = [] day = start while day <= end: hours = [] for h in range(*OUTING_HOURS): i = at_index.get(f"{day.isoformat()}T{h:02d}:00") if i is None or w["apparent_temperature"][i] is None: break cloud = w["cloud_cover"][i] or 0 hours.append(Hour( hour=h, feels_c=w["apparent_temperature"][i], rain_type=RainType.RAIN if (w["precipitation"][i] or 0) >= 0.1 else RainType.NONE, rain_prob=int(w["precipitation_probability"][i] or 0), wind_ms=w["wind_speed_10m"][i], humidity=50, sky=Sky.CLOUDY if cloud >= 80 else Sky.PARTLY if cloud >= 40 else Sky.CLEAR, )) result = outing(hours) if len(hours) == OUTING_HOURS[1] - OUTING_HOURS[0] else None if result is not None: out.append(result.score) day += timedelta(days=1) return out def main() -> None: end = date.today() - timedelta(days=2) start = end - timedelta(days=DAYS) regions = list(csv.DictReader(open(REGIONS, encoding="utf-8")))[::EVERY] scores: list[int] = [] for r in regions: scores += region_scores(float(r["lat"]), float(r["lon"]), start, end) LOG.info(f"[outing-table] {r['name']} 완료") time.sleep(1) doc = { "about": f"나들이 점수 바꾸기 표: 원래 점수 0~100 → 표시 점수. 평균 {MEAN}·표준편차 {SIGMA}. scripts/gen_outing_score_table.py 가 만든다", "period": [start.isoformat(), end.isoformat()], "regions": len(regions), "samples": len(scores), "table": straighten_top(table(scores)), # 100점은 원래 점수 100(완벽한 날)일 때만 } with open(OUT, "w", encoding="utf-8") as f: json.dump(doc, f, ensure_ascii=False, indent=1) print(len(regions), "regions", len(scores), "samples") if __name__ == "__main__": main()