Source:
Almanac for Computers, 1990
published by Nautical Almanac Office
United States Naval Observatory
Washington, DC 20392
Inputs:
day, month, year: date of sunrise/sunset
latitude, longitude: location for sunrise/sunset
zenith: Sun's zenith for sunrise/sunset
offical = 90 degrees 50'
civil = 96 degrees
nautical = 102 degrees
astronomical = 108 degrees
NOTE: longitude is positive for East and negative for West
NOTE: the algorithm assumes the use of a calculator with the
trig functions in "degree" (rather than "radian") mode. Most
programming languages assume radian arguments, requiring back
and forth convertions. The factor is 180/pi. So, for instance,
the equation RA = atan(0.91764 * tan(L)) would be coded as RA
= (180/pi)*atan(0.91764 * tan((pi/180)*L)) to give a degree
answer with a degree input for L.
1. first calculate the day of the year
N1 = floor(275 * month / 9)
N2 = floor((month + 9) / 12)
N3 = (1 + floor((year - 4 * floor(year / 4) + 2) / 3))
N = N1 - (N2 * N3) + day - 30
2. convert the longitude to hour value and calculate an approximate time
lngHour = longitude / 15
if rising time is desired:
t = N + ((6 - lngHour) / 24)
if setting time is desired:
t = N + ((18 - lngHour) / 24)
3. calculate the Sun's mean anomaly
M = (0.9856 * t) - 3.289
4. calculate the Sun's true longitude
L = M + (1.916 * sin(M)) + (0.020 * sin(2 * M)) + 282.634
NOTE: L potentially needs to be adjusted into the range [0,360) by adding/subtracting 360
5a. calculate the Sun's right ascension
RA = atan(0.91764 * tan(L))
NOTE: RA potentially needs to be adjusted into the range [0,360) by adding/subtracting 360
5b. right ascension value needs to be in the same quadrant as L
Lquadrant = (floor( L/90)) * 90
RAquadrant = (floor(RA/90)) * 90
RA = RA + (Lquadrant - RAquadrant)
5c. right ascension value needs to be converted into hours
RA = RA / 15
6. calculate the Sun's declination
sinDec = 0.39782 * sin(L)
cosDec = cos(asin(sinDec))
7a. calculate the Sun's local hour angle
cosH = (cos(zenith) - (sinDec * sin(latitude))) / (cosDec * cos(latitude))
if (cosH > 1)
the sun never rises on this location (on the specified date)
if (cosH < -1)
the sun never sets on this location (on the specified date)
7b. finish calculating H and convert into hours
if if rising time is desired:
H = 360 - acos(cosH)
if setting time is desired:
H = acos(cosH)
H = H / 15
8. calculate local mean time of rising/setting
T = H + RA - (0.06571 * t) - 6.622
9. adjust back to UTC
UT = T - lngHour
NOTE: UT potentially needs to be adjusted into the range [0,24) by adding/subtracting 24
10. convert UT value to local time zone of latitude/longitude
localT = UT + localOffset
import ephem
user = ephem.Observer()
user.lat = '53.143889' # See wikipedia.org/Oldenburg
user.lon = '8.213889' # See wikipedia.org/Oldenburg
user.elevation = 4 # See wikipedia.org/Oldenburg
user.temp = 20 # current air temperature gathered manually
user.pressure = 1019.5 # current air pressure gathered manually
next_sunrise_datetime = user.next_rising(ephem.Sun()).datetime()
next_sunset_datetime = user.next_setting(ephem.Sun()).datetime()
# If it is daytime, we will see a sunset sooner than a sunrise.
it_is_day = next_sunset_datetime < next_sunrise_datetime
print("It's day." if it_is_day else "It's night.")
# If it is nighttime, we will see a sunrise sooner than a sunset.
it_is_night = next_sunrise_datetime < next_sunset_datetime
print("It's night." if it_is_night else "It's day.")
你可以像我一样,使用这个公共域Sun.py模块来计算太阳相对于地球位置的位置。它很旧,但多年来对我很管用。我对它做了一些表面上的修改,使之更能适应Python2.7的最新版本,比如使其中的几个类成为新样式,但在大多数情况下它没有改变。在
这是我创建的一个模块,名为日出集.py,它展示了如何使用它来计算给定地理坐标和时区的特定位置的日出和日落时间。引用的} 抽象基类的实现。在
timezone
模块是datetime
模块文档中描述的^{美国海军天文台对计算日出和日落的算法进行了简要说明,可在以下网址获得:
http://edwilliams.org/sunrise_sunset_algorithm.htm
除了提供日期和位置,您还需要选择一个天顶角(在这个角度上太阳将被视为“升起”或“落下”)—链接的页面有几个选项。在
更新
因为下面的文字已经链接不上了。请注意,包含的公式是伪代码形式,而不是JavaScript。在
PyEphem可以用来计算到下一次日出和日落的时间。在a blog post I found和{a3}的基础上,您的问题可以按如下方式解决。假设我是您的用户,我的位置是Oldenburg (Oldb), Germany。在
注释
lat
和{先决条件
这至少适用于Python2.7(使用
pip-2.7 install pyephem
)和Python3.2(使用pip-3.2 install ephem
)。在确保在系统上运行网络时间协议客户端。E、 g.在Debian Linux上:
^{pr2}$请确保在您的系统上设置了正确的时区。E、 g.在Debian Linux上:
相关问题 更多 >
编程相关推荐