[ PROMPT_NODE_26234 ]
wcs_and_other_modules
[ SKILL_DOCUMENTATION ]
# WCS 与其他 Astropy 模块
## 世界坐标系 (astropy.wcs)
WCS 模块用于管理图像中像素坐标与世界坐标(例如天球坐标)之间的转换。
### 从 FITS 读取 WCS
python
from astropy.wcs import WCS
from astropy.io import fits
# 从 FITS 头文件中读取 WCS
with fits.open('image.fits') as hdul:
wcs = WCS(hdul[0].header)
### 像素到世界坐标的转换
python
# 单个像素到世界坐标
world = wcs.pixel_to_world(100, 200) # 返回 SkyCoord
print(f"RA: {world.ra}, Dec: {world.dec}")
# 像素数组
import numpy as np
x_pixels = np.array([100, 200, 300])
y_pixels = np.array([150, 250, 350])
world_coords = wcs.pixel_to_world(x_pixels, y_pixels)
### 世界坐标到像素的转换
python
from astropy.coordinates import SkyCoord
import astropy.units as u
# 单个坐标
coord = SkyCoord(ra=10.5*u.degree, dec=41.2*u.degree)
x, y = wcs.world_to_pixel(coord)
# 坐标数组
coords = SkyCoord(ra=[10, 11, 12]*u.degree, dec=[41, 42, 43]*u.degree)
x_pixels, y_pixels = wcs.world_to_pixel(coords)
### WCS 信息
python
# 打印 WCS 详情
print(wcs)
# 访问关键属性
print(wcs.wcs.crpix) # 参考像素
print(wcs.wcs.crval) # 参考值(世界坐标)
print(wcs.wcs.cd) # CD 矩阵
print(wcs.wcs.ctype) # 坐标类型
# 像素比例
pixel_scale = wcs.proj_plane_pixel_scales() # 返回 Quantity 数组
### 创建 WCS
python
from astropy.wcs import WCS
# 创建新的 WCS
wcs = WCS(naxis=2)
wcs.wcs.crpix = [512.0, 512.0] # 参考像素
wcs.wcs.crval = [10.5, 41.2] # 参考像素处的 RA, Dec
wcs.wcs.ctype = ['RA---TAN', 'DEC--TAN'] # 投影类型
wcs.wcs.cdelt = [-0.0001, 0.0001] # 像素比例 (度/像素)
wcs.wcs.cunit = ['deg', 'deg']
### 覆盖范围 (Footprint)
python
# 计算图像覆盖范围(角坐标)
footprint = wcs.calc_footprint()
# 返回每个角的 [RA, Dec] 数组
## NDData (astropy.nddata)
用于存储带有元数据、不确定性和掩码的 n 维数据集的容器。
### 创建 NDData
python
from astropy.nddata import NDData
import numpy as np
import astropy.units as u
# 基础 NDData
data = np.random.random((100, 100))
ndd = NDData(data)
# 带单位
ndd = NDData(data, unit=u.electron/u.s)
# 带不确定性
from astropy.nddata import StdDevUncertainty
uncertainty = StdDevUncertainty(np.sqrt(data))
ndd = NDData(data, uncertainty=uncertaint