astropy
它能做什么
Astropy 是天文学家处理数据的一套基础工具。它可以帮助管理度量单位、天球位置、天文数据文件(如 FITS),以及与宇宙和时间相关的计算。
适用场景
- ✓当你需要换算天文单位时(例如从秒差距换算为千米)。
- ✓当你需要处理恒星和星系在天空中的位置时。
- ✓当你需要读写天文数据文件(FITS)时。
技能链接
https://github.com/K-Dense-AI/scientific-agent-skills/blob/main/skills/astropy/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请用 Astropy 把 100 秒差距换算成千米。”
每周 5 个新技能,1 分钟读完。
工作原理
Astropy 的工作方式是,先用单位定义物理量(比如 100 * u.pc),然后使用 .to() 等方法进行换算。对于天空位置,它使用 SkyCoord 等对象来处理。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Astropy
Overview
Astropy is the core Python package for astronomy, providing essential functionality for astronomical research and data analysis. Use astropy for coordinate transformations, unit and quantity calculations, FITS file operations, cosmological calculations, precise time handling, tabular data manipulation, and astronomical image processing.
When to Use This Skill
Use astropy when tasks involve:
- Converting between celestial coordinate systems (ICRS, Galactic, FK5, AltAz, etc.)
- Working with physical units and quantities (converting Jy to mJy, parsecs to km, etc.)
- Reading, writing, or manipulating FITS files (images or tables)
- Cosmological calculations (luminosity distance, lookback time, Hubble parameter)
- Precise time handling with different time scales (UTC, TAI, TT, TDB) and formats (JD, MJD, ISO)
- Table operations (reading catalogs, cross-matching, filtering, joining)
- WCS transformations between pixel and world coordinates
- Astronomical constants and calculations
Quick Start
Targets Astropy 8.0.1. Numerical and small synthetic file examples were executed; blocks using observation/catalog filenames, online services or a GUI are illustrative and require those inputs. See review evidence and sources.
import astropy.units as u
from astropy.coordinates import SkyCoord
from astropy.time import Time
from astropy.io import fits
from astropy.table import Table
from astropy.cosmology import Planck18
# Units and quantities
distance = 100 * u.pc
distance_km = distance.to(u.km)
# Coordinates
coord = SkyCoord(ra=10.5*u.degree, dec=41.2*u.degree, frame='icrs')
coord_galactic = coord.galactic
# Time
t = Time('2023-01-15 12:30:00', scale='utc')
jd = t.jd # Julian Date
# FITS files
data, header = fits.getdata('image.fits', ext=0, header=True) # Select actual image HDU
# Tables
table = Table.read('catalog.fits')
# Cosmology
d_L = Planck18.luminosity_distance(1.0)
Core Capabilities
1. Units and Quantities (astropy.units)
Handle physical quantities with units, perform unit conversions, and ensure dimensional consistency in calculations.
Key operations:
- Create quantities by multiplying values with units
- Convert between units using
.to()method - Perform arithmetic with automatic unit handling
- Use equivalencies for domain-specific conversions (spectral, doppler, parallax)
- Work with logarithmic units (magnitudes, decibels)
See: references/units.md for comprehensive documentation, unit systems, equivalencies, performance optimization, and unit arithmetic.
2. Coordinate Systems (astropy.coordinates)
Represent celestial positions and transform between different coordinate frames.
Key operations:
- Create coordinates with
SkyCoordin any frame (ICRS, Galactic, FK5, AltAz, etc.) - Transform between coordinate systems
- Calculate angular separations and position angles
- Match coordinates to catalogs
- Include distance for 3D coordinate operations
- Handle proper motions and radial velocities
- Query named objects from online databases
See: references/coordinates.md for detailed coordinate frame descriptions, transformations, observer-dependent frames (AltAz), catalog matching, and performance tips.
3. Cosmological Calculations (astropy.cosmology)
Perform cosmological calculations using standard cosmological models.
Key operations:
- Use built-in cosmologies (Planck18, WMAP9, etc.)
- Create custom cosmological models
- Calculate distances (luminosity, comoving, angular diameter)
- Compute ages and lookback times
- Determine Hubble parameter at any redshift
- Calculate density parameters and volumes
- Perform inverse calculations (find z for given distance)
See: references/cosmology.md for available models, distance calculations, time calculations, density parameters, and neutrino effects.
4. FITS File Handling (astropy.io.fits)
Read, write, and manipulate FITS (Flexible Image Transport System) files.
Key operations:
- Open FITS files with context managers
- Access HDUs (Header Data Units) by index or name
- Read and modify headers (keywords, comments, history)
- Work with image data (NumPy arrays)
- Handle table data (binary and ASCII tables)
- Create new FITS files (single or multi-extension)
- Use memory mapping for large files
- Access remote FITS files (S3, HTTP)
…
常见问题
什么是 FITS 文件?
FITS(灵活图像传输系统)是天文学中常用的文件格式,用于存储图像和表格数据。
什么是“单位与物理量”?
它们是附带测量单位的数值,比如“100 秒差距”或“5 开尔文”。Astropy 能确保带单位的计算正确无误。
什么是“天球坐标系”?
这是描述天体在天空中位置的方法,比如 ICRS 或银道坐标系。Astropy 支持在它们之间进行转换。
详情
作者的原始描述
Core Python library for astronomy and astrophysics workflows that need Astropy APIs, including units/quantities, coordinates, FITS I/O, tables, time systems, WCS, and cosmology. Use when implementing or debugging astronomical data analysis code with Astropy.
热度
- GitHub 星标
- 51k
- 排名
- #3427
- 许可证
- MIT
- 更新时间
- 2026/10/15
评论
- …