name: astronomy-data-toolkit title: 天文数据分析(Astropy) description: 当用 Python 处理天文/天体物理数据(坐标变换、单位换算、FITS 读写、表格、时间系统、WCS、宇宙学)时使用;用 Astropy 编写或调试分析代码并产出量纲一致、可复现的结果;不适用于通用数据清洗或非天文数值计算(改用 pandas/numpy)。触发词:astropy、天文数据、astronomy、SkyCoord、坐标变换、FITS、WCS、宇宙学、cosmology、天文单位、Quantity domain: 领域/science triggers: [astropy, 天文数据, astronomy, SkyCoord, 坐标变换, FITS, WCS, 宇宙学, cosmology, 天文单位, Quantity] tags: [astropy, astronomy, fits, coordinates, cosmology, wcs, units, python, science] level: 进阶 status: stable agents: [claude-code, codex, cursor, gemini-cli] tools: [astropy, astropy.units, astropy.coordinates, astropy.io.fits, astropy.table, astropy.time, astropy.wcs, astropy.cosmology, numpy, uv] requires: [] related: [materials-science-toolkit, cheminformatics-toolkit, sympy-symbolic-math] combines_with: [matplotlib-visualization, guided-statistical-analysis] license: MIT source: K-Dense-AI/scientific-agent-skills source_license: MIT
何时使用
当任务涉及以下天文/天体物理数据处理时使用 Astropy:
- 天球坐标系互转(ICRS、Galactic、FK5、AltAz 等)
- 物理单位与量纲换算(Jy↔mJy、pc↔km,含光谱/多普勒/视差等价关系)
- 读写或操作 FITS 文件(图像与表格)
- 宇宙学计算(光度距离、共动距离、回溯时间、哈勃参数)
- 高精度时间处理(UTC/TAI/TT/TDB 时标,JD/MJD/ISO 格式)
- 天体表/星表操作(读取、交叉匹配、过滤、连接、分组)
- WCS 像素坐标与世界坐标互转
不该用边界:通用 CSV/表格清洗、与天文无关的纯数值计算,直接用 pandas/numpy 更合适,本条仅在需要 Astropy 的领域 API(单位感知、坐标框架、FITS/WCS、宇宙学)时引入。
步骤
- 安装并锁版本(可复现):
uv pip install "astropy==7.2.0";需绘图等可选依赖时用"astropy[recommended]==7.2.0",广覆盖才用[all]。要求 Python 3.11+,依赖 NumPy、PyERFA、PyYAML;用隔离虚拟环境,勿以管理员权限安装。 - 按需导入子模块(见「指令」)。
- 凡数值带单位的量一律附单位(用
* u.<unit>),换算用.to(),保证量纲一致。 - FITS 文件用
with fits.open(...)上下文管理器确保关闭;大文件可用内存映射。 - 批量坐标/时间用数组而非循环,性能更好。
- 做坐标变换前先确认源 frame;AltAz 需提供
obstime与EarthLocation。 - 宇宙学选定合适模型(如
Planck18);有单位的表用QTable;缺失值用掩码列。
指令
import astropy.units as u
from astropy.coordinates import SkyCoord, EarthLocation, AltAz, match_coordinates_sky
from astropy.time import Time
from astropy.io import fits
from astropy.table import Table, QTable
from astropy.cosmology import Planck18
from astropy.wcs import WCS
常用调用:
- 单位:
d = 100 * u.pc; d.to(u.km) - 坐标:
SkyCoord(ra=10.5*u.deg, dec=41.2*u.deg, frame='icrs').galactic - 时间:
Time('2023-01-15 12:30:00').jd - FITS:
fits.getdata('x.fits')/fits.getheader('x.fits') - 表格:
Table.read('catalog.fits') - 宇宙学:
Planck18.luminosity_distance(z=1.0)
示例
坐标转 AltAz(需时间与地点):
c = SkyCoord(ra='05h23m34.5s', dec='-69d45m22s', frame='icrs')
print(c.galactic.l.deg, c.galactic.b.deg)
aa = AltAz(obstime=Time('2023-06-15 23:00:00'),
location=EarthLocation(lat=40*u.deg, lon=-120*u.deg))
c_altaz = c.transform_to(aa)
print(c_altaz.alt.deg, c_altaz.az.deg)
星表交叉匹配:
coords1 = SkyCoord(ra=cat1['RA']*u.deg, dec=cat1['DEC']*u.deg)
coords2 = SkyCoord(ra=cat2['RA']*u.deg, dec=cat2['DEC']*u.deg)
idx, sep, _ = coords1.match_to_catalog_sky(coords2)
matches = sep < 1 * u.arcsec
cat1_matched, cat2_matched = cat1[matches], cat2[idx[matches]]
宇宙学距离:
z = 1.5
print(Planck18.luminosity_distance(z), Planck18.angular_diameter_distance(z))
print(Planck18.age(z).to(u.Gyr), Planck18.lookback_time(z).to(u.Gyr))
注意事项
- 网络访问要显式:
SkyCoord.from_name()、EarthLocation.of_site(refresh_cache=True)、EarthLocation.of_address()、download_file()、远程 FITS 读取及部分 IERS 时间/坐标转换会联网或更新本地缓存。勿把敏感目标名、地址、URL、专有文件路径发往第三方服务。 - 可复现:共享环境锁版本(如
astropy==7.2.0),更新前先看 release notes。 - 7.x 版本变更:Astropy 7.0 已移除过时 FITS API,如
(Bin)Table.update、_ExtensionHDU、_NonstandardExtHDU及CompImageHDU的tile_size参数;CompImageHeader已弃用——新代码勿用这些遗留写法。 - 时标要显式(UTC/TT/TDB),高精度计时尤其重要;用 WCS 转换前先校验其有效性;昂贵计算(如宇宙学距离)可缓存。
- 当前研究的稳定版:Astropy 7.2.0(2025-11-25 发布)。
互见
- 官方文档:https://docs.astropy.org/en/stable/ ;教程:https://learn.astropy.org/
- 源条目附有分模块参考:
references/units.md、coordinates.md、cosmology.md、fits.md、tables.md、time.md、wcs_and_other_modules.md(涵盖 NDData/CCDData、建模、可视化、常数、卷积、统计)。
本条采编自 K-Dense-AI/scientific-agent-skills(MIT)。