Files
o2o-weather/backend/services/calc/sun.py
T
minaChoi ddf05aeb5c feat: 로딩 스켈레톤·행사 사진·전시·주말 예상 점수·노을 탭 재정리 등 화면 전반 개선
- 화면 전체 스켈레톤, 로딩·완료·실패 사이 레이아웃 고정, 값 없음은 '?'
- 행사 사진·출처 표시, 문화정보 API 전시 연결, 행사 30km 거리 기준
- 각 상세 점수 칸 오늘/내일/토/일, 단기예보 밖 주말은 중기예보 예상 점수
- 6각 차트 축 이름을 좋은 방향으로, 계산 근거 문구 정리
- 아이콘 오용·뜻 모를 표시·빈 데이터·포스트 본문 반복 수정
- 미세먼지 별도 포스트, 노을 명소 전국 155곳 거리 기준, 노을 탭 결론 중심 재구성
- 밤 다크 테마·별똥별, 댓글·좋아요 대기·실패 표시
- 주말 전국 랭킹·당일치기 카드, 계절 이야기 사진 수집
- 문서: 포스트 타입별 내용, 각 데이터 정리, TODO 진행 순서
2026-10-06 16:36:11 +09:00

58 lines
2.1 KiB
Python

import math
from dataclasses import dataclass
from datetime import date, datetime, timedelta, timezone
_SUN_ALTITUDE = -0.833
_CIVIL_ALTITUDE = -6.0
_GOLDEN_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
golden_start_at: datetime
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
h_golden = _hour_angle(lat, decl, _GOLDEN_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),
golden_start_at=at(noon_min + h_golden),
)