From 3857ef8d14ea0b208e4fadc1827e3d6717749473 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=84=B1=EA=B2=BD?= Date: Thu, 8 Oct 2026 11:59:08 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EB=85=B8=EC=9D=84=20=EA=B3=B5=EA=B8=B0?= =?UTF-8?q?=20=EB=B3=B4=EC=A0=95=EC=9D=84=20=ED=8F=89=EC=86=8C=20=EA=B3=B5?= =?UTF-8?q?=EA=B8=B0=20=EA=B8=B0=EC=A4=80=EC=A0=90=20=EC=A4=91=EC=8B=AC?= =?UTF-8?q?=EC=9C=BC=EB=A1=9C=20=EB=B0=94=EA=BF=94=20=EB=A7=91=EC=9D=80=20?= =?UTF-8?q?=EB=82=A0=20=EC=A0=90=EC=88=98=EA=B0=80=20=ED=95=9C=20=EC=A0=90?= =?UTF-8?q?=EC=97=90=20=EB=AA=B0=EB=A6=AC=EC=A7=80=20=EC=95=8A=EA=B2=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/docs/openmeteo-data.md | 6 +++--- backend/services/calc/sky_score.py | 23 ++++++++++++++++------- backend/tests/test_sky_api.py | 2 +- backend/tests/test_sky_score.py | 7 ++++--- 4 files changed, 24 insertions(+), 14 deletions(-) diff --git a/backend/docs/openmeteo-data.md b/backend/docs/openmeteo-data.md index 015f3dc..be49e6f 100644 --- a/backend/docs/openmeteo-data.md +++ b/backend/docs/openmeteo-data.md @@ -48,9 +48,9 @@ light = 1 - min(low100 + 0.5×mid100, 100) / 100 ``` - 맑은 하늘(구름 0%)은 55점(은은함)이고, 구름이 몇 %만 있어도 점수가 갈린다. 예전에는 10% 미만을 0.3으로 고정해 맑은 날 전국이 48점에 몰리고, 10%에서 점수가 오히려 떨어졌다. -- 공기 보정 (`air_factor`, 구 중심 해 질·뜰 무렵 값. 값이 없으면 ×1.0): - - 가시거리(`openmeteo_hourly.visibility_m`, 가장 가까운 3시간 구간): 20km 이상 ×1.0 ~ 5km 이하 ×0.8. 뿌연 공기는 색을 흐린다. - - 습도(`weather_hourly.humidity`, 가장 가까운 정시): 70% 이하 ×1.0 ~ 90% 이상 ×0.85. 평소 날씨는 깎지 않고 아주 뿌옇거나 습한 날만 깎는다. +- 공기 보정 (`air_factor`, 구 중심 해 질·뜰 무렵 값. 값이 없으면 ×1.0). 평소 공기(가시거리 20km·습도 60%)를 ×1.0 으로 두고 좋으면 올리고 나쁘면 내린다. 맑은 날 전국이 한 점수(구름 0% = 55점)에 몰리지 않게 하려는 것: + - 가시거리(`openmeteo_hourly.visibility_m`, 가장 가까운 3시간 구간): 5km 이하 ×0.8 · 20km ×1.0 · 40km 이상 ×1.12, 사이는 직선. 뿌연 공기는 색을 흐리고 맑은 공기는 더 선명하다. + - 습도(`weather_hourly.humidity`, 가장 가까운 정시): 40% 이하 ×1.06 · 60% ×1.0 · 90% 이상 ×0.85, 사이는 직선. - 비 갠 뒤: 2~6시간 전 비 확률 60% 이상이고 그 시각 비 확률 40% 미만이면 ×1.15. - 모델마다 따로 계산해 **평균**을 최종 점수로 쓰고, 모델 점수의 표준편차로 신뢰도를 정한다 (8 미만 높음, 15 미만 보통, 그 이상 낮음). 모델이 2개 미만이면 낮음. 신뢰도는 공기 보정 전 구름 점수로 본다. - 등급: 80 이상 붉음, 60 이상 고움, 40 이상 은은함, 그 미만 흐림. diff --git a/backend/services/calc/sky_score.py b/backend/services/calc/sky_score.py index 701b923..63dfaf0 100644 --- a/backend/services/calc/sky_score.py +++ b/backend/services/calc/sky_score.py @@ -36,19 +36,28 @@ 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, 20, 0.8) + f *= _interp(air.visibility_km, _VISIBILITY_FACTOR) if air.humidity is not None: - f *= _ramp(air.humidity, 90, 70, 0.85) + 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 return f diff --git a/backend/tests/test_sky_api.py b/backend/tests/test_sky_api.py index c5fc60b..c8be86e 100644 --- a/backend/tests/test_sky_api.py +++ b/backend/tests/test_sky_api.py @@ -161,7 +161,7 @@ def test_score_rows_apply_air_at_sunset_and_keep_cloud_score_without_air(): om = {("R", sky_service._nearest_slot(hour)): SimpleNamespace(visibility_m=5000)} hazy = sky_service._score_rows(region, today, clouds, now, hourly, om)[0]["score"] assert hazy == round(plain * 0.8 * 0.85) - rain = {("R", hour - timedelta(hours=3)): SimpleNamespace(humidity=50, rain_prob=80), ("R", hour): SimpleNamespace(humidity=50, rain_prob=10)} + rain = {("R", hour - timedelta(hours=3)): SimpleNamespace(humidity=60, rain_prob=80), ("R", hour): SimpleNamespace(humidity=60, rain_prob=10)} assert sky_service._score_rows(region, today, clouds, now, rain, {})[0]["score"] == min(100, round(plain * 1.15)) diff --git a/backend/tests/test_sky_score.py b/backend/tests/test_sky_score.py index 056f2a8..5fd277e 100644 --- a/backend/tests/test_sky_score.py +++ b/backend/tests/test_sky_score.py @@ -24,11 +24,12 @@ def test_score_rises_steadily_with_a_few_clouds(): assert scores == sorted(scores) and len(set(scores)) == len(scores) # 맑은 날도 구름 몇 %로 갈리고 10%에서 꺾이지 않는다 -def test_air_factor_hazy_humid_lowers_and_clearing_rain_raises(): +def test_air_factor_centers_on_usual_air_and_clearing_rain_raises(): assert air_factor(Air()) == 1.0 - assert air_factor(Air(visibility_km=20, humidity=70)) == 1.0 + assert air_factor(Air(visibility_km=20, humidity=60)) == 1.0 # 평소 공기 assert air_factor(Air(visibility_km=5)) == 0.8 and air_factor(Air(humidity=90)) == 0.85 - assert 0.8 < air_factor(Air(visibility_km=12.5)) < 1.0 + assert air_factor(Air(visibility_km=40)) == 1.12 and air_factor(Air(humidity=40)) == 1.06 # 맑고 건조하면 올라간다 + assert 0.8 < air_factor(Air(visibility_km=12.5)) < 1.0 < air_factor(Air(visibility_km=30)) < 1.12 assert air_factor(Air(rain_before=70, rain_now=10)) == 1.15 assert air_factor(Air(rain_before=70, rain_now=60)) == 1.0 # 아직 비 오는 중이면 가산 없음