PyEphem (http://rhodesmill.org/pyephem/tutorial.html)(github, Pypi)是Python下的一个软件包,计算准确度很高,可用来计算不同坐标系下太阳、月亮、行星、彗星及人造卫星的位置,亮度,出没时刻,中天时刻等,为天文爱好者制作星历表提供了很大方便。初次接触该软件可能觉得无从下手,本文做简要介绍。
1. 下载和安装软件
1.1 安装Anaconda Python
由于pyephem依赖于NumPy, SciPy等,所以这里建议直接安装Anaconda Python。pyephem 最早是在python2下开发的, 但是已经成功移植到python3下面,所以建议下载和安装anaconda3。
下载Anaconda Python https://anaconda.org/。
1.2 安装pyephem
通过开始>CMD,通过pip安装 pyephem
输入如下命令:
1.3 运行Jupyter Notebook
在开始CMD中,输入 jupyter notebook
, 在自动打开的浏览器(即 http://localhost:8888/tree
)页面的右上角点击 New, 新建一个Jupyter记事本。
点击加号图标, 新建一个代码区域, 将以下各Python代码块拷贝到代码区,点击左侧的运行符号即可。
运行完成后,点击 File>save as>选择.ipynb,保存为Jupyter Notebook。关于运行 Jupyter Notebook, 请参考 知乎上的专栏。
2 代码举例
2.1 计算2010年1月16日天王星的位置和亮度
1 2 3 4 5 6 7 8 9 10 11
| import ephem u = ephem.Uranus()
u.compute('2010/1/16') print (u.ra, u.dec, u.mag)
print (ephem.constellation(u))
23:37:29.43 -3:14:13.6 5.9 ('Psc', 'Pisces')
|
2.2 计算2010年1月16日木星的位置和亮度
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| import ephem j = ephem.Jupiter('2010/1/16')
n = ephem.Neptune('2010/1/16')
print ("Jupiter") print ("RA:", j.ra, ", \nDEC", j.dec, ", \nMAG:", j.mag) print ("Neptune")print ("RA:", n.ra, ", \nDEC:", n.dec, ", \nMAG:", n.mag) print ("Separation between Jupiter and Neptune:", ephem.separation(j, n))
Jupiter RA: 22:07:54.83 , DEC -12:30:01.9 , MAG: -1.91 Neptune RA: 21:49:56.21 , DEC: -13:33:46.0 , MAG: 7.97 Separation between Jupiter and Neptune 4:30:19.5
|
2.3 计算火星在近日点和远日点速度的差异
1 2 3 4 5 6 7 8 9 10 11 12
| import ephem
def hpos(body): return body.hlong, body.hlat
ma0 = ephem.Mars('1976/05/21')
print (ephem.separation(hpos(ma0), hpos(ma1))) mp0 = ephem.Mars('1975/06/13')
print (ephem.separation(hpos(mp0), hpos(mp1))) 0:26:11.4 0:38:05.2
|
2.4 地方时
1 2 3 4 5
| import ephem d = ephem.Date('1984/12/21 15:00') ephem.localtime(d)
Fri Dec 21 23:00:00 1984
|
2.5 时间转换
1 2 3 4 5 6 7 8
| import ephem d = ephem.Date('1950/2/28') print (d + 1)
print (ephem.Date(d + 1)) 18321.5 1950/3/1 00:00:00
|
2.6 日期转换
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| import ephem
d = ephem.Date(34530.34375) d = ephem.Date('1994/7/16.84375') d = ephem.Date('1994/7/16 20:15') d = ephem.Date((1994, 7, 16.84375)) d = ephem.Date((1994, 7, 16, 20, 15, 0)) print ('as a float: %f\nas a string: "%s"' % (d, d)) print (d.triple())
print (d.tuple())
as a float: 34530.343750 as a string: "1994/7/16 20:15:00" (1994, 7, 16.84375) (1994, 7, 16, 20, 15, 0.0)
|
2.7 计算某时刻太阳和月亮的高度和方位
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| import ephem gatech = ephem.Observer()print(gatech)
gatech.long, gatech.lat = '126', '40'
sun, moon = ephem.Sun(), ephem.Moon() sun.compute(gatech) moon.compute(gatech) print (sun.alt, sun.az) print (moon.alt, moon.az) print (ephem.separation((sun.az, sun.alt), (moon.az, moon.alt))) print (sun.size, moon.size, sun.size - moon.size) -69:36:06.8 24:22:55.8 -69:24:12.6 12:32:15.7 4:08:44.6 1950.9619140625 1743.300048828125 207.661865234375
|
2.8 计算每隔五分钟太阳的方位角和高度
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| import ephem gatech.date = '1984/5/31 00:00'
for i in range(8): old_az, old_alt = sun.az, sun.alt gatech.date += ephem.minute * 5. sun.compute(gatech) sep = ephem.separation((old_az, old_alt), (sun.az, sun.alt))
print(gatech.date, sun.alt, sep) print(gatech.next_setting(sun)) print(gatech.next_setting(sun)) print(sun.alt, sun.az) 1984/5/31 00:40:00 49:09:10.0 1:09:32.8 1984/5/31 10:58:06 1984/5/31 10:58:06 -0:15:46.3 300:05:46.5
|
2.9 通过轨道根数计算小行星和彗星的位置和亮度
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| yh = ephem.readdb("C/2002 Y1 (Juels-Holvorcem),e,103.7816," "166.2194,128.8232,242.5695,0.0002609,0.99705756,0.0000," "04/13.2508/2003,2000,g 6.5,4.0")
yh.compute('2003/4/11') print (yh.name) print (yh.ra, yh.dec) print (ephem.constellation(yh), yh.mag) print (yh) C/2002 Y1 (Juels-Holvorcem) 0:22:44.58 26:49:48.1 ('And', 'Andromeda') 5.96 <ephem.EllipticalBody 'C/2002 Y1 (Juels-Holvorcem)' at 0x1043dc5b0>
|
2.10 通过人造卫星的轨道根数 计算卫星出没时刻
1 2 3 4 5 6 7 8 9
| iss = ephem.readtle("ISS (ZARYA)","1 25544U 98067A 03097.78853147 .00021906 00000-0 28403-3 0 8652", "2 25544 51.6361 13.7980 0004256 35.6671 59.2566 15.58778559250029") gatech.date = '2003/3/23'iss.compute(gatech) print (iss.rise_time, iss.transit_time, iss.set_time) gatech.date = '2003/3/23 8:00'iss.compute(gatech) print (iss.rise_time, iss.transit_time, iss.set_time) 2003/3/23 10:48:45 2003/3/23 10:53:09 2003/3/23 10:57:34 2003/3/23 10:48:45 2003/3/23 10:53:09 2003/3/23 10:57:34
|
2.11 计算在不同历元下,恒星的坐标
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| polaris = ephem.readdb("Polaris,f|M|F7,2:31:48.704,89:15:50.72,2.02,2000") polaris.compute() print (polaris.dec)
print (ephem.degrees(ephem.degrees('90') - polaris.a_dec)) polaris.compute(epoch='2100') print (polaris.dec)
thuban = ephem.readdb("Thuban,f|V|A0,14:4:23.3,64:22:33,3.65,2000") thuban.compute()print (thuban.dec)thuban.compute(epoch='-2800') print (thuban.dec) polaris.compute(epoch='-2800') print (polaris.dec) 89:20:08.2 0:44:09.3 89:20:08.2 64:17:38.4 64:17:38.4 89:20:08.2
|