Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 月球/月球相位算法_Python_C_Algorithm_Calendar_Astronomy - Fatal编程技术网

Python 月球/月球相位算法

Python 月球/月球相位算法,python,c,algorithm,calendar,astronomy,Python,C,Algorithm,Calendar,Astronomy,有人知道计算某一日期的月相或年龄的算法吗?或者找到某一年中新月/满月的日期吗 谷歌搜索告诉我答案在一些天文学书籍中,但我真的不想买一整本书,因为我只需要一页 更新: 我应该把我关于谷歌搜索的陈述限定得更好一点。我确实找到了只在某些时间段内有效的解决方案(比如1900年代);基于trig的解决方案在计算上比我想要的要昂贵 在他的Python书中,S Lott有几种计算给定年份复活节的算法,大多数都不到十行代码,有些算法可以在公历的所有日子里工作。寻找三月的满月是寻找复活节的关键,所以我想应该有一个

有人知道计算某一日期的月相或年龄的算法吗?或者找到某一年中新月/满月的日期吗

谷歌搜索告诉我答案在一些天文学书籍中,但我真的不想买一整本书,因为我只需要一页

更新:

我应该把我关于谷歌搜索的陈述限定得更好一点。我确实找到了只在某些时间段内有效的解决方案(比如1900年代);基于trig的解决方案在计算上比我想要的要昂贵


在他的Python书中,S Lott有几种计算给定年份复活节的算法,大多数都不到十行代码,有些算法可以在公历的所有日子里工作。寻找三月的满月是寻找复活节的关键,所以我想应该有一个算法,它不需要三角,适用于公历中的所有日期。

我想你在谷歌上搜索错了:


不久前,我为Python移植了一些代码。我本来打算链接到它,但结果是它同时从网上掉了下来,所以我不得不把它掸掉灰尘,再上传一次。看看哪个是从中派生出来的


我找不到这方面的参考资料,也找不到准确的时间跨度,但作者似乎非常严格。这意味着是的,它确实使用了trig,但我无法想象你会用它来做什么,这会使它在计算上受到限制。Python函数调用开销可能比trig操作的成本更高。计算机的运算速度相当快

代码中使用的算法来自以下来源:

米厄斯,琼。天文算法。里士满:威尔曼·贝尔,1991年。ISBN 0-943396-35-2。

必备品;如果你只买一本书,一定要买这本。算法是以数学方式呈现的,不是以计算机程序的形式呈现的,但实现本书中许多算法的源代码可以从出版商处单独订购QuickBasic、Turbo Pascal或C。Meeus提供了许多计算示例,这些示例对于调试代码至关重要,并且经常提出几种在准确性、速度、复杂性和长期(世纪和千年)有效性之间进行不同权衡的算法

彼得·达菲特·史密斯。实用天文学与你的计算器。剑桥:剑桥大学出版社,1981年。ISBN 0-521-28411-2。

尽管标题中有“计算器”一词;如果您对开发计算行星位置、轨道、日食等的软件感兴趣,这是一个有价值的参考。与Meeus相比,Meeus提供了更多的背景信息,这有助于那些尚未精通天文学的人学习经常令人困惑的术语。与Meeus提供的算法相比,给出的算法更简单,精度更低,但适用于大多数实际工作。

另外,[],这是一个Python包,但具有

从-1369到+2950的精度<0.05英寸。
使用表格查找技术限制对三角函数的调用


我知道你在寻找Python,但如果你能理解C,有一个开源项目叫做C,它做得很好。

如果你不需要高精度,你可以使用阴历(或阴历)类(例如,
HijriCalendar
ChineseLunisolarCalendar
,在Microsoft.NET中)要计算任何日期的(近似)月相,作为日历的“月中日”属性,作为农历(或日月)日历日,始终对应于月相(例如,第1天是新月,第15天是满月,等等)

如果你像我一样,你会努力成为一名细心的程序员。因此,当你看到散布在互联网上的随机代码声称要解决一个复杂的天文问题,但却无法解释为什么解决方案是正确的时,你会感到紧张

您认为必须有权威来源,如包含仔细、完整解决方案的书籍。例如:

《天文算法》。里士满:威尔曼·贝尔,1991年。ISBN 0-943396-35-2

彼得·达菲特·史密斯:《使用计算器的实用天文学》,第三版,剑桥: 剑桥大学出版社,1981年。ISBN 0-521-28411-2

您信任广泛使用、经过良好测试的开源库,这些库可以纠正错误(与静态网页不同)

#!/usr/bin/python
import datetime
import ephem

def get_phase_on_day(year,month,day):
  """Returns a floating-point number from 0-1. where 0=new, 0.5=full, 1=new"""
  #Ephem stores its date numbers as floating points, which the following uses
  #to conveniently extract the percent time between one new moon and the next
  #This corresponds (somewhat roughly) to the phase of the moon.

  #Use Year, Month, Day as arguments
  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)

  #Note that there is a ephem.Moon().phase() command, but this returns the
  #percentage of the moon which is illuminated. This is not really what we want.

  return lunation

def get_moons_in_year(year):
  """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,01,01))
  while date.datetime().year==year:
    date=ephem.next_full_moon(date)
    moons.append( (date,'full') )

  date=ephem.Date(datetime.date(year,01,01))
  while date.datetime().year==year:
    date=ephem.next_new_moon(date)
    moons.append( (date,'new') )

  #Note that previous_first_quarter_moon() and previous_last_quarter_moon()
  #are also methods

  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')]

Pyephem默认使用协调世界时(UTC)。我想要一个程序,可以生成在太平洋时区准确的满月列表。下面的代码将计算给定年份的满月,然后使用ephem.localtime()进行调整方法来校准到所需的时区。它似乎也正确地解释了夏令时。感谢Richard,此代码与他编写的代码类似

#!/usr/bin/python
import datetime
import ephem
import os
import time

# Set time zone to pacific
os.environ['TZ'] = 'US/Pacific'
time.tzset()

print("Time zone calibrated to", os.environ['TZ'])

def get_full_moons_in_year(year):
    """
    Generate a list of full moons for a given year calibrated to the local time zone
    :param year: year to determine the list of full moons
    :return: list of dates as strings in the format YYYY-mm-dd
    """
    moons = []

    date = ephem.Date(datetime.date(year - 1, 12, 31))
    end_date = ephem.Date(datetime.date(year + 1, 1, 1))

    while date <= end_date:
        date = ephem.next_full_moon(date)

        # Convert the moon dates to the local time zone, add to list if moon date still falls in desired year
        local_date = ephem.localtime(date)
        if local_date.year == year:
            # Append the date as a string to the list for easier comparison later
            moons.append(local_date.strftime("%Y-%m-%d"))

    return moons

moons = get_full_moons_in_year(2015)
print(moons)

@杰克:哇!+1…记忆。我不知道我从哪里知道这个名字:“本·达格利什”。他是当年C64上最好的(我说是其中之一)8位音乐作曲家之一。很高兴找到与他完全无关的链接(我不得不在他的网站上搜索,直到找到C64链接,才想起我从哪里知道这个名字).-1 StackOverflow的一个很好的功能是,人们可以到这里来获取答案,而不是答案的链接。此外,您的顶部链接已断开,您的第二个链接没有引用,以使细心的程序员相信其中列出的算法是正确的,您的第三个链接有引用,但代码都已从ori更改ginals。第四个链接也有同样的问题。谁的谷歌?不同的人的点击率确实不同。O
Time zone calibrated to US/Pacific
['2015-01-04', '2015-02-03', '2015-03-05', '2015-04-04', '2015-05-03', '2015-06-02', '2015-07-01', '2015-07-31', '2015-08-29', '2015-09-27', '2015-10-27', '2015-11-25', '2015-12-25']