如何在 Python 中使用 skyfield 计算太阳在天空中的位置

以下代码使用 skyfield 库计算太阳在天空中的位置(以方位角/高度坐标)。注意你需要下载 de421.bsp

sun_position.py
from skyfield import api
from skyfield import almanac
from datetime import datetime
from datetime import timedelta
import dateutil.parser
from calendar import monthrange

ts = api.load.timescale()
ephem = api.load_file('de421.bsp')

sun = ephem["Sun"]
earth = ephem["Earth"]

# 计算慕尼黑附近随机位置的日出和日落
location = api.Topos('48.324777 N', '11.405610 E', elevation_m=519)
# 计算从 <location> 处的观察者看到的太阳位置
sun_pos = (earth + location).at(ts.now()).observe(sun).apparent()
# 计算太阳位置的视高度和方位角
altitude, azimuth, distance = sun_pos.altaz()

# 打印结果(示例)
print(f"Altitude: {altitude.degrees:.4f} °")
print(f"Azimuth: {azimuth.degrees:.4f} °")

示例输出

sun_position_example_output.txt
Altitude: ... °
Azimuth: 48.4141 °

影响计算精度的因素

这种计算位置的方式考虑了:

但它不考虑:


Check out similar posts by category: Physics, Python, Skyfield