- 나들이 점수(주말 전국 순위·당일치기)에 지금 날씨 점수와 같은 방식의 바꾸기 표를 적용: 원래 점수를 1년 치 분포의 순위로 보고 평균 60·표준편차 15 정규분포 점수로 바꿈 (weekend_rank.py, outing_score_table.json, scripts/gen_outing_score_table.py) - 나들이 점수 만점 조건을 엄격하게: 체감온도 최적 21°±0.5°, 바람 초속 1m 이하일 때만 100점 (같은 예보를 받는 여러 지역이 한꺼번에 100점이 되던 것을 줄임) - 표 맨 위가 100으로 잘려 비슷한 원래 점수(96·97·99)가 모두 100점이 되던 것을 막기 위해 straighten_top 추가: 표시 점수 83 이하인 마지막 원래 점수에서 100점까지 직선으로 이어, 100점은 구간에서 나올 수 있는 가장 높은 원래 점수(밤은 85)일 때만 나옴. 지금 날씨 점수 표도 다시 만듦 (90점 이상 약 2.4% → 1.5%) - 당일치기 최소 점수는 원래 50점과 같은 날씨 수준(표시 62점)으로 옮김 - 주말 순위 캐시 키를 weekend_rank:v2:<날짜>로 올려 옛 점수 캐시를 쓰지 않게 함 - 테스트 추가: test_outing_score_table, test_weather_score_table(straighten_top·100점 조건) - docs/점수_계산식.md의 지금 날씨 점수·나들이 점수 항목을 새 기준과 숫자로 갱신
77 lines
3.7 KiB
Python
77 lines
3.7 KiB
Python
"""나들이 점수 바꾸기 표(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()
|