我正在嘗試在python中創建朱利安日期並進行重大斗爭。有沒有沒有那麼簡單:在python中計算朱利安日期
jul = juliandate(year,month,day,hour,minute,second)
其中jul會像2457152.0(隨着時間的變化小數)?
我試過jdcal,但不知道如何添加時間組件(jdcal.gcal2jd()只接受年,月和日)。
我正在嘗試在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
找到你想要的東西。你不能添加時間組件到jdcal.gcal2jd,雖然 – edub
庫嘗試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' –
一個簡單的fudge通過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。繼續前進吧。
你可以使用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)
如果熟悉日期時間庫的用法,就不難了解這些。爲了好玩,您是否注意到PyEphem徽標?我懷疑它來自像this
一個職位,我看到似乎工作,但沒有測試是jiffyclub
現在,這裏是計算使用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
它可能不是Python中的最佳實踐,但你沒有問如何計算它不只是得到它或將其解壓縮但如果這是你想要這些問題就有了答案爲晚什麼。
不是一個純粹的Python的解決方案,但你可以使用具有julianday()
功能在內存數據庫SQLite的:
import sqlite3
con = sqlite3.connect(":memory:")
list(con.execute("select julianday('2017-01-01')"))[0][0]
返回:2457754.5
能否請您提供的代碼,看看發生了什麼事情或你是否在使用圖書館進行分享? – lmiguelvargasf
我試過jdcal,但無法弄清楚如何添加時間方面(它應該改變小數)。我也嘗試過JDateTime,astropy.time和datetime,但沒有運氣。上面的代碼行只是一個假設 - 我試圖看看是否有這樣的事情。 – edub
我想你可以在這個[link](https://oneau.wordpress.com/2011/08/30/jdcal/) – lmiguelvargasf