You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Julia中如何计算日出日落时间?是否存在类似Python suntime、R suntimes的相关包?

Great question! While it might not have popped up right away on JuliaHub, there are actually a couple of solid Julia packages that can handle sunrise/sunset calculations—plus, if you’d rather avoid dependencies, you can implement the core astronomical algorithm manually. Here’s a breakdown of your options:

Existing Julia Packages

1. AstroLib.jl

This is a robust astronomy-focused package that includes a riseset function specifically designed to compute sunrise and sunset times. Here’s how you’d use it to replicate your Python example:

First, install the package if you haven’t already:

using Pkg
Pkg.add("AstroLib")

Then calculate sunset time for a given latitude, longitude, and date:

using AstroLib, Dates

# Define your coordinates and target date
LAT = 40.7128  # New York City latitude
LON = -74.0060 # New York City longitude
today = Dates.today()

# Compute sunrise and sunset (returns UTC times by default)
sunrise_utc, sunset_utc = riseset(LAT, LON, today)

# Convert to local time (example for UTC-5/Eastern Standard Time)
sunset_local = sunset_utc - Dates.Hour(5)

println("Local sunset time: ", sunset_local)

2. SunCalc.jl

Ported from the popular JavaScript SunCalc library, this package offers a range of solar position calculations including sunrise/sunset. Install it with:

Pkg.add("SunCalc")

Usage example (returns local time by default):

using SunCalc, Dates

LAT = 40.7128
LON = -74.0060
today = Dates.now()

# Get sunset time as a DateTime object
sunset_time = SunCalc.get_sunset_time(LAT, LON, today)

println("Local sunset time: ", sunset_time)

Manual Implementation (If Packages Aren’t Ideal)

If you prefer to avoid external dependencies, you can implement the NOAA sunrise/sunset algorithm directly. Here’s a simplified version that calculates sunset time in UTC:

using Dates

function calculate_sunset(lat::Float64, lon::Float64, date::Date)
    # Convert date to Julian day
    jd = Dates.datetime2julian(DateTime(date))
    
    # Julian century from J2000.0
    t = (jd - 2451545.0) / 36525.0
    
    # Sun's mean longitude
    L = mod(280.46646 + t*(36000.76983 + t*0.0003032), 360.0)
    
    # Sun's mean anomaly
    g = mod(357.52911 + t*(35999.05029 - t*0.0001537), 360.0)
    
    # Sun's ecliptic longitude
    lambda = L + 1.914602*t*sind(g) + 0.019993*t*t*sind(2*g) + 0.000289*t*t*t*sind(3*g)
    
    # Obliquity of the ecliptic
    epsilon = 23 + (26 + (21.448 - t*(46.8150 + t*(0.00059 - t*0.001813)))/60)/60
    epsilon_rad = deg2rad(epsilon)
    
    # Sun's right ascension and declination
    alpha = atan2(cosd(epsilon)*sind(lambda), cosd(lambda))
    delta = asind(sind(epsilon)*sind(lambda))
    
    # Local sidereal time
    lst = mod((jd - 2451545.0)*24 + 100.46 + 0.985647*jd + lon, 24)
    
    # Hour angle for sunset (using 90.833° for atmospheric refraction)
    h0 = acosd(cosd(90.833)/(cosd(lat)*cosd(delta)) - tand(lat)*tand(delta))
    
    # Sunset time in UTC hours
    sunset_hours = (lst - rad2deg(alpha) - h0)/15.0
    sunset_hours = mod(sunset_hours, 24.0)
    
    # Convert to DateTime object
    hours = floor(Int, sunset_hours)
    minutes = floor(Int, (sunset_hours - hours)*60)
    seconds = round(Int, ((sunset_hours - hours)*60 - minutes)*60)
    sunset_utc = DateTime(date) + Dates.Hour(hours) + Dates.Minute(minutes) + Dates.Second(seconds)
    
    return sunset_utc
end

# Example usage
LAT = 40.7128
LON = -74.0060
today = Dates.today()
sunset_utc = calculate_sunset(LAT, LON, today)

println("UTC sunset time: ", sunset_utc)

This manual implementation follows the NOAA algorithm and gives you full control over the calculation without relying on external packages.

内容的提问来源于stack exchange,提问作者Thomas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 23:07:42