在 python 中计算儒略日期

Calculating julian date in python

我正在尝试在 python 中创建一个儒略日期,但遇到了很大的困难。有没有像这样简单的东西:

jul = juliandate(year,month,day,hour,minute,second)

哪里的 jul 会像 2457152.0(小数随时间变化)?

我试过 jdcal,但不知道如何添加时间组件(jdcal.gcal2jd() 只接受年、月和日)。

回答 Extract day of year and Julian day from a string date in python 中的一个。不需要图书馆。 答案二是来自 https://pypi.python.org/pypi/jdcal

的库

尝试https://www.egenix.com/products/python/mxBase/mxDateTime/

首先通过语法构造一个DateTime对象

DateTime(year,month=1,day=1,hour=0,minute=0,second=0.0)

然后您可以使用'.jdn'对象方法来获取您要查找的值。

通过 wiki 简单地捏造数字脚本也不知道。请注意,这是使用 Python 3.6 编写的,所以我不确定它是否适用于 Python 2.7,但这也是一个老问题。

def julian_day(now):
    """
    1. Get current values for year, month, and day
    2. Same for time and make it a day fraction
    3. Calculate the julian day number via   https://en.wikipedia.org/wiki/Julian_day
    4. Add the day fraction to the julian day number

    """
    year = now.year
    month = now.month
    day = now.day
    day_fraction = now.hour + now.minute / 60.0 + now.second / 3600.0 / 24.0

    # The value 'march_on' will be 1 for January and February, and 0 for other months.
    march_on = math.floor((14 - month) / 12)
    year = year + 4800 - march_on
    # And 'month' will be 0 for March and 11 for February. 0 - 11 months
    month = month + 12 * march_on - 3

    y_quarter = math.floor(year / 4)
    jdn = day + math.floor((month * 153 + 2) / 5) + 365 * year + y_quarter

    julian = year < 1582 or year == (1582 and month < 10) or (month == 10 and day < 15)
    if julian:
        reform = 32083 # might need adjusting so needs a test
    else:
        reform = math.floor(year / 100) + math.floor(year / 400) + 32030.1875 # fudged this

    return jdn - reform + day_fraction

一般来说,这只是为了自己尝试,因为最常见的算法给我带来了麻烦。这行得通,如果您搜索它并使用它编写您的脚本,因为它有多种语言版本。但是这个在文档中有一些步骤来尽量保持简单。最大的决定是您要多久查找一次公历改革之前的日期。这就是为什么我还没有测试过它,而是继续玩它,因为它需要大量的按摩。 :-D 至少我认为它符合 PEP8,即使它不符合最佳实践。继续 pylint 它。

您可以只使用 PyEphem 之类的源代码包或其他任何东西,但您仍然想知道它发生了什么,以便您可以编写自己的测试。我会 link 那个 PyEphem 给你,但是有很多现成的包有 Julian Day 计算。

如果您经常使用这些类型的数字,最好的办法是获取常量列表,例如 J2000。

datetime.datetime(2000, 1, 1, 12, 0, 0, tzinfo=datetime.timezone.utc)

datetime.datetime.toordinal() + 1721425 - 0.5 # not tested

# or even
datetime.datetime(1970, 1, 1, tzinfo=datetime.timezone.utc)

如果您熟悉 datetime 库的功能,那么理解这些并不难。只是为了好玩,你注意到 PyEphem 标志了吗?我怀疑它来自 this

之类的东西

我看到的一个 post 似乎有效但没有测试是 jiffyclub

下面是使用日期时间对象计算两个值的更常用方法。

def jdn(dto):
"""
Given datetime object returns Julian Day Number
"""
year = dto.year
month = dto.month
day = dto.day

not_march = month < 3
if not_march:
    year -= 1
    month += 12

fr_y = math.floor(year / 100)
reform = 2 - fr_y + math.floor(fr_y / 4)
jjs = day + (
    math.floor(365.25 * (year + 4716)) + math.floor(30.6001 * (month + 1)) + reform - 1524)
if jjs < ITALY:
    jjs -= reform

return jjs
# end jdn

def ajd(dto):
"""
Given datetime object returns Astronomical Julian Day.
Day is from midnight 00:00:00+00:00 with day fractional
value added.
"""
jdd = jdn(dto)
day_fraction = dto.hour / 24.0 + dto.minute / 1440.0 + dto.second / 86400.0
return jdd + day_fraction - 0.5
# end ajd

这可能不是 Python 中的最佳做法,但您确实问过如何计算它,而不仅仅是获取或提取它,尽管如果您想要的话,这些问题最近已经得到解答。

不是纯粹的 Python 解决方案,但您可以使用内存数据库中的 SQLite,它具有 julianday() 函数:

import sqlite3
con = sqlite3.connect(":memory:")
list(con.execute("select julianday('2017-01-01')"))[0][0]

哪个returns:2457754.5

给你 - 带有日期时间和数学库的纯 python 解决方案。

这是基于在这里找到的海军天文方程并用他们自己的计算器验证的:http://aa.usno.navy.mil/faq/docs/JD_Formula.php

import datetime
import math

def get_julian_datetime(date):
    """
    Convert a datetime object into julian float.
    Args:
        date: datetime-object of date in question

    Returns: float - Julian calculated datetime.
    Raises: 
        TypeError : Incorrect parameter type
        ValueError: Date out of range of equation
    """

    # Ensure correct format
    if not isinstance(date, datetime.datetime):
        raise TypeError('Invalid type for parameter "date" - expecting datetime')
    elif date.year < 1801 or date.year > 2099:
        raise ValueError('Datetime must be between year 1801 and 2099')

    # Perform the calculation
    julian_datetime = 367 * date.year - int((7 * (date.year + int((date.month + 9) / 12.0))) / 4.0) + int(
        (275 * date.month) / 9.0) + date.day + 1721013.5 + (
                          date.hour + date.minute / 60.0 + date.second / math.pow(60,
                                                                                  2)) / 24.0 - 0.5 * math.copysign(
        1, 100 * date.year + date.month - 190002.5) + 0.5

    return julian_datetime

用法示例:

# Set the same example as the Naval site.
example_datetime = datetime.datetime(1877, 8, 11, 7, 30, 0)
print get_julian_datetime(example_datetime)

最简单的:df['Julian_Dates']=df.index.to_julian_date()。您需要将日期时间列设置为索引(使用 Pandas)。

有一种使用 Astropy 的方法。首先,将您的时间更改为列表 (t)。其次,将该列表更改为天体时间(Time)。最后,计算您的 JD 或 MJD (t.jd t.mjd)。
https://docs.astropy.org/en/stable/time/

对于 df: t = 时间(DF.JulianDates,格式='jd',比例='utc')