如果您和我一样,都是认真的程序员。那么当您看到散布在互联网上的随机代码声称解决了一个复杂的天文问题,但不解释其正确性时,您会感到紧张。
您相信必须有权威来源,如书籍 ,其中包含仔细而完整的解决方案。例如:
Meeus, Jean. Astronomical Algorithms. Richmond: Willmann-Bell, 1991. ISBN 0-943396-35-2.
Duffett-Smith, Peter. Practical Astronomy With Your Calculator. 3rd ed. Cambridge:
Cambridge University Press, 1981. ISBN 0-521-28411-2.
您相信广泛使用、经过充分测试、可以纠正错误的开源库(与静态网页不同)。因此,这里提供了基于PyEphem库、使用月相接口的Python解决方案,以回答您的问题。
import datetime
import ephem
from typing import List, Tuple
def get_phase_on_day(year: int, month: int, day: int):
"""Returns a floating-point number from 0-1. where 0=new, 0.5=full, 1=new"""
date = ephem.Date(datetime.date(year,month,day))
nnm = ephem.next_new_moon(date)
pnm = ephem.previous_new_moon(date)
lunation = (date-pnm)/(nnm-pnm)
return lunation
def get_moons_in_year(year: int) -> List[Tuple[ephem.Date, str]]:
"""Returns a list of the full and new moons in a year. The list contains tuples
of either the form (DATE,'full') or the form (DATE,'new')"""
moons=[]
date=ephem.Date(datetime.date(year,1,1))
while date.datetime().year==year:
date=ephem.next_full_moon(date)
moons.append( (date,'full') )
date=ephem.Date(datetime.date(year,1,1))
while date.datetime().year==year:
date=ephem.next_new_moon(date)
moons.append( (date,'new') )
moons.sort(key=lambda x: x[0])
return moons
print(get_phase_on_day(2013,1,1))
print(get_moons_in_year(2013))
这将返回
0.632652265318
[(2013/1/11 19:43:37, 'new'), (2013/1/27 04:38:22, 'full'), (2013/2/10 07:20:06, 'new'), (2013/2/25 20:26:03, 'full'), (2013/3/11 19:51:00, 'new'), (2013/3/27 09:27:18, 'full'), (2013/4/10 09:35:17, 'new'), (2013/4/25 19:57:06, 'full'), (2013/5/10 00:28:22, 'new'), (2013/5/25 04:24:55, 'full'), (2013/6/8 15:56:19, 'new'), (2013/6/23 11:32:15, 'full'), (2013/7/8 07:14:16, 'new'), (2013/7/22 18:15:31, 'full'), (2013/8/6 21:50:40, 'new'), (2013/8/21 01:44:35, 'full'), (2013/9/5 11:36:07, 'new'), (2013/9/19 11:12:49, 'full'), (2013/10/5 00:34:31, 'new'), (2013/10/18 23:37:39, 'full'), (2013/11/3 12:49:57, 'new'), (2013/11/17 15:15:44, 'full'), (2013/12/3 00:22:22, 'new'), (2013/12/17 09:28:05, 'full'), (2014/1/1 11:14:10, 'new'), (2014/1/16 04:52:10, 'full')]