【问题标题】:Calculating julian date in python在python中计算朱利安日期
【发布时间】:2015-09-17 11:32:11
【问题描述】:

我正在尝试在 python 中创建一个朱利安日期并且遇到了重大困难。有没有像这样简单的东西:

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

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

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

【问题讨论】:

  • 您能否提供代码以查看发生了什么,或者您是否正在使用 juliandate 库?
  • 我试过jdcal,但不知道如何添加时间方面(它应该改变小数)。我也尝试过 JDateTime、astropy.time 和 datetime,但没有成功。上面的代码行只是一个假设 - 我正在尝试看看是否有类似的东西。
  • 我猜你可以在link找到你想要的东西
  • 如果你有UNIX时间,JD就是:jd = unixTime/86400 + 2440587.5

标签: python-2.7 julian-date


【解决方案1】:

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

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

返回:2457754.5

【讨论】:

    【解决方案2】:

    给你 - 一个带有日期时间和数学库的纯 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)
    

    【讨论】:

    • 请注意,这个函数每秒只产生一个新的输出,而不是毫秒级或以下。如果有人有这样的功能,它还可以从日期时间对象中提供毫秒及以下的分辨率,这可能对某些人(包括我)有用。
    【解决方案3】:

    最简单的: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,format='jd',scale='utc')

    【讨论】:

      【解决方案4】:

      【讨论】:

      • 你不能向 jdcal.gcal2jd 添加时间组件
      【解决方案5】:

      通过 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 之类的源代码包或其他任何东西,但您仍然想知道它发生了什么,以便您可以编写自己的测试。我会为你链接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

      我看到的一个帖子似乎有效但没有测试是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 中的最佳实践,但您确实问过如何计算它,而不仅仅是获取或提取它,尽管如果这是您想要的,那么这些问题最近已经得到解答。

      【讨论】:

        【解决方案6】:

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

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

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

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

        【讨论】:

        • 休?先读什么? AttributeError: 'datetime.datetime' 对象没有属性 'jdn'
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-09-29
        相关资源
        最近更新 更多