Python 2.7 用python计算朱利安日期
我正在尝试用python创建一个julian日期,并且有很大的困难。没有比这更简单的了吗Python 2.7 用python计算朱利安日期,python-2.7,julian-date,Python 2.7,Julian Date,我正在尝试用python创建一个julian日期,并且有很大的困难。没有比这更简单的了吗 jul = juliandate(year,month,day,hour,minute,second) 哪里的jul大约是2457152.0(小数点随时间变化) 我试过jdcal,但不知道如何添加时间组件(jdcal.gcal2jd()只接受年、月和日) 回答一个问题。不需要图书馆。 答案二是一个库,请尝试 首先通过语法构造DateTime对象 DateTime(year,month=1,day=1,ho
jul = juliandate(year,month,day,hour,minute,second)
哪里的jul大约是2457152.0(小数点随时间变化)
我试过jdcal,但不知道如何添加时间组件(jdcal.gcal2jd()只接受年、月和日) 回答一个问题。不需要图书馆。 答案二是一个库,请尝试 首先通过语法构造DateTime对象
DateTime(year,month=1,day=1,hour=0,minute=0,second=0.0)
然后你可以使用.jdn对象方法来获得你想要的值。一个简单的伪造数字的方法,通过wiki的脚本也不知道。请注意,这是使用Python3.6编写的,所以我不确定它是否能在Python2.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
如果您熟悉datetime库的功能,就不难理解这些。只是为了好玩,你有没有注意到皮耶芬的标志?我怀疑它来自于
我看到的一个帖子似乎有效,但没有测试
下面是使用datetime对象计算两个值的更常见方法
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
def jdn(dto):
"""
给定的datetime对象返回朱利安日数
"""
年份=dto.year
月=dto.month
日=日
非三月=月份<3
如果没有,三月:
年份-=1
月份+=12
fr_y=数学楼层(年份/100)
改革=2-fr_y+数学楼层(fr_y/4)
jjs=天+(
数学下限(365.25*(年+4716))+数学下限(30.6001*(月+1))+改革-1524)
如果jjs<意大利:
jjs-=改革
返回jjs
#结束jdn
def ajd(dto):
"""
给定的datetime对象返回天文朱利安日。
天是从午夜00:00:00+00:00开始的,以天为单位
增值。
"""
jdd=jdn(dto)
日分数=dto.hour/24.0+dto.minute/1440.0+dto.second/86400.0
返回jdd+日分数-0.5
#结束ajd
这可能不是Python中的最佳实践,但您确实询问了如何计算它,而不仅仅是获取或提取它,尽管如果这是您想要的,这些问题最近已经得到了回答。不是纯Python解决方案,但您可以使用SQLite in memory db,它具有
julianday()
函数:
import sqlite3
con = sqlite3.connect(":memory:")
list(con.execute("select julianday('2017-01-01')"))[0][0]
它返回:
2457754.5
这是一个带有日期时间和数学库的纯python解决方案
这是基于海军在这里发现的天文方程式,并用他们自己的计算器进行验证:
最简单的:df['Julian_Dates']=df.index.to_Julian_date()。您需要将日期时间列设置为索引(使用熊猫) 有一种使用Astropy的方法。首先,将时间更改为列表(t)。其次,将该列表更改为astropy time(时间)。最后,计算您的JD或MJD(t.JD t.MJD)。
对于df:
t=Time(DF.JulianDates,format='jd',scale='utc')您能提供代码来查看发生了什么事情,或者您正在使用juliandate库吗?我已经尝试了jdcal,但不知道如何添加时间方面(它应该更改小数)。我还尝试了JDateTime、astropy.time和datetime,但没有成功。上面的代码行只是一个假设——我想看看是否有类似的东西。我想如果你有UNIX时间,你可以在这里找到你想要的东西,JD只是:JD=unixTime/86400+2440587.5你不能向jdcal.gcal2jd添加时间组件吗?先读什么?AttributeError:'datetime.datetime'对象没有属性'jdn'。请注意,此函数仅每秒生成一个新输出,而不是毫秒或更低级别的输出。如果有人有这样一个函数,它可以从datetime对象提供毫秒及以下的分辨率,这可能对某些人(包括我)有用。
import sqlite3
con = sqlite3.connect(":memory:")
list(con.execute("select julianday('2017-01-01')"))[0][0]
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)