Files
o2o-weather/backend/services/calc/sun.py
T
minaChoiandClaude Opus 5.5 e134295ce5 feat: 날씨각 백엔드 API (날씨·노을·지수·주말·계절·피드·로그인·반응)
- FastAPI + PostgreSQL + Redis, web·worker 분리, compose 에 backend·worker·postgres·redis 추가
- 기상청 단기·초단기·중기·특보, 에어코리아, Open-Meteo, TourAPI, 특일정보 연동 (첫 조회 때 받고 캐시)
- 매일 03:00 worker 가 전국 공통 피드 칸을 정하고 지난 데이터를 정리
- 시드: 시군구 256곳, 지수 23개, 노을 대표 지점·명소, 데이트 명소, 읽을거리
- pytest 66개 (테스트 전용 DB·Redis 사용)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-02 11:32:22 +09:00

54 lines
2.0 KiB
Python

import math
from dataclasses import dataclass
from datetime import date, datetime, timedelta, timezone
_SUN_ALTITUDE = -0.833
_CIVIL_ALTITUDE = -6.0
@dataclass(frozen=True)
class SunTimes:
sunrise_at: datetime
sunset_at: datetime
dusk_end_at: datetime
sunset_azimuth: int
dawn_start_at: datetime
sunrise_azimuth: int
def _solar(d: date, lon: float) -> tuple[float, float]:
"""NOAA 근사식. (태양 적위 도, 남중 시각 UTC 분)."""
g = 2 * math.pi / 365 * (d.timetuple().tm_yday - 1)
eqtime = 229.18 * (
0.000075 + 0.001868 * math.cos(g) - 0.032077 * math.sin(g) - 0.014615 * math.cos(2 * g) - 0.040849 * math.sin(2 * g)
)
decl = (
0.006918 - 0.399912 * math.cos(g) + 0.070257 * math.sin(g) - 0.006758 * math.cos(2 * g)
+ 0.000907 * math.sin(2 * g) - 0.002697 * math.cos(3 * g) + 0.00148 * math.sin(3 * g)
)
return math.degrees(decl), 720 - 4 * lon - eqtime
def _hour_angle(lat: float, decl: float, altitude: float) -> float:
phi, dl = math.radians(lat), math.radians(decl)
cos_h = (math.sin(math.radians(altitude)) - math.sin(phi) * math.sin(dl)) / (math.cos(phi) * math.cos(dl))
return math.degrees(math.acos(max(-1.0, min(1.0, cos_h))))
def sun_times(d: date, lat: float, lon: float) -> SunTimes:
decl, noon_min = _solar(d, lon)
base = datetime(d.year, d.month, d.day, tzinfo=timezone.utc)
at = lambda minutes: base + timedelta(minutes=minutes) # noqa: E731
h_sun = _hour_angle(lat, decl, _SUN_ALTITUDE) * 4
h_civil = _hour_angle(lat, decl, _CIVIL_ALTITUDE) * 4
phi, dl = math.radians(lat), math.radians(decl)
azimuth = math.degrees(math.acos(max(-1.0, min(1.0, math.sin(dl) / math.cos(phi)))))
return SunTimes(
sunrise_at=at(noon_min - h_sun),
sunset_at=at(noon_min + h_sun),
dusk_end_at=at(noon_min + h_civil),
sunset_azimuth=round(360 - azimuth),
dawn_start_at=at(noon_min - h_civil),
sunrise_azimuth=round(azimuth),
)