张金龙的博客分享 http://blog.sciencenet.cn/u/zjlcas 物种适应性、分布与进化

博文

天文计算PyEphem指南

已有 13121 次阅读 2010-1-17 00:09 |个人分类:特别转载|系统分类:科研笔记| 天文, Python, 星历表

PyEphem (http://rhodesmill.org/pyephem/tutorial.html)(githubPypi)是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

输入如下命令:

pip install ephem

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日天王星的位置和亮度

import ephem 
u = ephem.Uranus() 
# 天王星
u.compute('2010/1/16') 
print (u.ra, u.dec, u.mag) 
# 赤经、赤纬、亮度
print (ephem.constellation(u)) 
# 所在星座
# print (u.rise_time)
23:37:29.43 -3:14:13.6 5.9
('Psc', 'Pisces')

2.2 计算2010年1月16日木星的位置和亮度

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 计算火星在近日点和远日点速度的差异

import ephem

def hpos(body): return body.hlong, body.hlat

ma0 = ephem.Mars('1976/05/21')    
# 火星在远日点ma1 = ephem.Mars('1976/05/22')
print (ephem.separation(hpos(ma0), hpos(ma1)))
mp0 = ephem.Mars('1975/06/13')    
# 火星在远日点mp1 = ephem.Mars('1975/06/14')
print (ephem.separation(hpos(mp0), hpos(mp1)))
0:26:11.4
0:38:05.2

2.4 地方时

import ephem
d = ephem.Date('1984/12/21 15:00')
ephem.localtime(d) 
# 地方时print (ephem.localtime(d).ctime())
Fri Dec 21 23:00:00 1984

2.5 时间转换

##
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 日期转换

import ephem
### 赋值类型
### 注意时间以 1899/12/31 12:00:00 作为起点
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()) 
# 显示为 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 计算某时刻太阳和月亮的高度和方位

import ephem
gatech = ephem.Observer()print(gatech) 
# 查看观测者位置信息# 2010/1/15 16:20:56 日环食
gatech.long, gatech.lat = '126', '40' 
# 观测者位置重新赋值gatech.date = '2010/1/15 16:20:56'  
# 观测者时间重新赋值
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 计算每隔五分钟太阳的方位角和高度

import ephem
gatech.date = '1984/5/31 00:00'   
# 20:00 EDTsun.compute(gatech)
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 通过轨道根数计算小行星和彗星的位置和亮度

## ephem database format格式 小行星或彗星的轨道根数
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 通过人造卫星的轨道根数 计算卫星出没时刻

## 轨道根数为TLE格式
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 计算在不同历元下,恒星的坐标

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




https://wap.sciencenet.cn/blog-255662-287895.html

上一篇:在Ubuntu Linux中安装R
下一篇:进化时间的估算:r8s软件使用指南
收藏 IP: 14.0.164.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...
扫一扫,分享此博文

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-3-29 02:17

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部