Golden Hour Calculator

Find golden hour and blue hour photography windows based on your location and date.

์ž์ฃผ ๋ฌป๋Š” ์งˆ๋ฌธ

์ฝ”๋“œ ๊ตฌํ˜„

import math
from datetime import datetime, timedelta, timezone

def sun_times(lat: float, lon: float, date: datetime) -> dict:
    """Calculate sunrise, sunset and golden hour times."""
    # Day of year
    N = date.timetuple().tm_yday

    # Mean longitude and anomaly
    L = (280.460 + 0.9856474 * N) % 360
    g = math.radians((357.528 + 0.9856003 * N) % 360)

    # Ecliptic longitude
    lam = math.radians(L + 1.915 * math.sin(g) + 0.020 * math.sin(2 * g))

    # Declination
    decl = math.asin(math.sin(math.radians(23.439)) * math.sin(lam))

    # Hour angle for sunrise/sunset (sun altitude = -0.833ยฐ)
    cos_ha = (math.sin(math.radians(-0.833)) - math.sin(math.radians(lat)) * math.sin(decl)) /              (math.cos(math.radians(lat)) * math.cos(decl))

    if abs(cos_ha) > 1:
        return {"error": "Midnight sun or polar night"}

    ha = math.degrees(math.acos(cos_ha))

    # Equation of time correction (simplified)
    B = math.radians(360 / 365 * (N - 81))
    eot = 9.87 * math.sin(2 * B) - 7.53 * math.cos(B) - 1.5 * math.sin(B)

    # Solar noon in minutes from midnight UTC
    solar_noon = 720 - 4 * lon - eot

    sunrise_min = solar_noon - 4 * ha
    sunset_min  = solar_noon + 4 * ha

    # Golden hour bounds: sun at 6ยฐ above horizon
    cos_gh = (math.sin(math.radians(6)) - math.sin(math.radians(lat)) * math.sin(decl)) /              (math.cos(math.radians(lat)) * math.cos(decl))
    gh_ha = math.degrees(math.acos(max(-1, min(1, cos_gh))))

    golden_end_morn = solar_noon - 4 * gh_ha
    golden_start_eve = solar_noon + 4 * gh_ha

    def mins_to_str(m):
        h, mn = divmod(int(m), 60)
        return f"{h:02d}:{mn:02d}"

    return {
        "sunrise": mins_to_str(sunrise_min),
        "golden_hour_morning_end": mins_to_str(golden_end_morn),
        "solar_noon": mins_to_str(solar_noon),
        "golden_hour_evening_start": mins_to_str(golden_start_eve),
        "sunset": mins_to_str(sunset_min),
    }

# New York City
result = sun_times(40.7128, -74.0060, datetime(2024, 6, 21))
for k, v in result.items():
    print(f"{k}: {v}")

Comments & Feedback

Comments are powered by Giscus. Sign in with GitHub to leave a comment.