做作业的时候需要把数据进行转化,做了两个儒略日和公历相互转换的函数,作为日常的记录,共勉之!
函数的主要功能时将儒略日进行转换,儒略日的起点订在公元前4713年(天文学上记为 -4712年)1月1日格林威治时间平午(世界时12:00),即JD 0指定为UT时间B.C.4713年1月1日12:00到UT时间B.C.4713年1月2日12:00的24小时。每一天赋予了一个唯一的数字,顺数而下,如:1996年1月1日12:00:00的儒略日是2450084。这个日期是考虑了太阳、月亮的轨道运行周期,以及当时收税的间隔而订出来的。
说明:
由于儒略日数字位数太多,国际天文学联合会于1973年采用简化儒略日(MJD),其定义为 MJD = JD - 2400000.5。MJD相应的起点是1858年11月17日世界时0时。 例如1979年10月1日零时儒略日数为2,444,147.5。天文年历附表载有各年每月零日世界时12时的儒略日数。
def mjd2cal(mjd):
# 从简化儒略日计算公历年月日时分秒
# 返回的cal是保存3位小数的年份时间表以及 年月日时分秒 列表
# 公元1582年10月4日24:00点之前使用儒略历,公元1582年10月15日00:00点之后使用公历
J = mjd + 2400000.5
if (J < 1721423.5):
# 公元1月1日0时
BC = 1;
else:
BC = 0;
if (J < 2299160.5):
# 1582.10.4. 24:00 前
j0 = math.floor(J + 0.5)
dd = J + 0.5 - j0
else:
# 不是闰年的年数
n1 = math.floor((J - 2342031.5) / 36524.25 / 4) + 1 # 1700.3.1.0
n2 = math.floor((J - 2378555.5) / 36524.25 / 4) + 1 # 1800.3.1.0
n3 = math.floor((J - 2415079.5) / 36524.25 / 4) + 1 # 1900.3.1.0
j0 = n1 + n2 + n3 + J + 10
dd = j0 + 0.5 - math.floor(j0 + 0.5)
j0 = math.floor(j0 + 0.5)
j0 = j0 + 32083
year0 = math.ceil(j0 / 365.25) - 1
year = year0 - 4800
day = j0 - math.floor(year0 * 365.25)
month = math.floor((day - 0.6) / 30.6) + 3
day = day - round((month - 3) * 30.6)
if (month > 12):
month = month - 12
year = year + 1
year = year - BC
sec = round(dd * 86400)
hour = math.floor(sec / 3600)
sec = sec - hour * 3600
minute = math.floor(sec / 60)
sec = sec - minute * 60
## 判断是不是闰年,计算精确到小数点后三位的
day0 = [0,31,59,90,120,151,181,212,243,273,304,334]
if year % 4 == 0 and year % 100 != 0 or year % 400 == 0:
yy = round(year + (day0[month-1]+1+day+hour/24+minute/1440)/366, 3)
else:
yy = round(year + (day0[month-1]+day+hour/24+minute/1440)/365, 3)
return [yy, year, month, day, hour, minute, sec]
In: mjd2cal(53930)
Out: [2006.619, 2006, 7, 14, 0, 0, 0]
def cal2mjd(cal):
# cal2jd 将公历年月日时分秒转换到简化儒略日。
# 输入公历时间列表,返回儒略日
if(len(cal)<6):
for i in range(len(cal),6):
cal.append(0)
year = cal[0]
month = cal[1]
day = cal[2]+(cal[3]*3600+cal[4]*60+cal[5])/86400;
y = year+4800
m = month
if( year < 0 ):
print('Year is wrong')
return False
if( m <= 2):
# 1,2月视为前一年13,14月
m = m + 12
y = y - 1
e = math.floor(30.6 * (m+1))
a = math.floor(y/100)
# 教皇格雷戈里十三世于1582年2月24日以教皇训令颁布,将1582年10月5日至14抹掉。1582年10月4日过完后第二天是10月15日
if( year <1582 )or(year==1582 and month<10)or(year==1582 and month==10 and day<15):
b = -38
else:
b = math.floor((a/4) - a)
c = math.floor(365.25 * y)
jd = b + c + e + day - 32167.5
mjd = jd - 2400000.5
return mjd
In: [2006,7,14,0,0,0]
Out: 53930