You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Astropy星表交叉匹配报错:空格分隔HMS/DMS坐标处理求助

问题描述

想要用Astropy对两个星表做交叉匹配,但星表的赤经(RA)和赤纬(Dec)是h m s和d m s格式(空格分隔),运行代码时抛出错误:

TypeError: unsupported operand type(s) for *: 'MaskedColumn' and 'Unit'

原代码如下:

from astropy import units as u
from astropy.coordinates import SkyCoord
from astropy.table import Table
import numpy as np

from astroquery.vizier import Vizier
import astropy.units as u

Vizier.ROW_LIMIT = -1
result = Vizier.query_region(SkyCoord.from_name('NGC 147',frame='icrs'),
                                 radius=10*u.arcmin,
                                 catalog='J/ApJS/216/10/dustgsc')

from tabulate import tabulate
with open('Boyer_Spitzer.txt', 'w') as f:
    f.write(tabulate(result[0]))

catalogs = Vizier.get_catalogs('J/A+A/445/69/table2')

with open('Sohn06.txt', 'w') as f1:
    f1.write(tabulate(catalogs[0]))

Boyer=result[0]
Sohn=catalogs[0]

coo_B15 = SkyCoord(Boyer['RAJ2000']*u.deg, Boyer['DEJ2000']*u.deg)
coo_S06 = SkyCoord(Sohn['RAJ2000']*u.deg, Sohn['DEJ2000']*u.deg)

idx_B15, d2d_B15, d3d_B15 = coo_S06.match_to_catalog_sky(coo_B15)
解决方案

错误根源是你直接将时分秒格式的字符串列当作数值乘以u.deg,Astropy无法处理这种操作。无需手动替换空格为冒号,SkyCoord本身支持解析空格分隔的时分秒/度分秒格式,直接指定参数即可。

方法1:直接用SkyCoord解析时分秒/度分秒格式

修改创建SkyCoord的代码,明确指定RA的单位为u.hourangle(对应h m s格式),Dec的单位为u.deg(d m s格式会被自动识别):

# 处理Boyer星表的坐标
coo_B15 = SkyCoord(ra=Boyer['RAJ2000'], dec=Boyer['DEJ2000'], 
                   unit=(u.hourangle, u.deg), frame='icrs')

# 处理Sohn星表的坐标
coo_S06 = SkyCoord(ra=Sohn['RAJ2000'], dec=Sohn['DEJ2000'], 
                   unit=(u.hourangle, u.deg), frame='icrs')

SkyCoord会自动将空格分隔的h m s转换为小时角后转为度,d m s直接转为度,无需手动处理格式。

方法2:手动替换空格为冒号(可选)

如果一定要先转换格式,可对列中字符串做替换后再解析:

# 替换Boyer星表的坐标空格为冒号
ra_boyer = [ra_str.replace(' ', ':') for ra_str in Boyer['RAJ2000']]
dec_boyer = [dec_str.replace(' ', ':') for dec_str in Boyer['DEJ2000']]
coo_B15 = SkyCoord(ra_boyer, dec_boyer, frame='icrs')

# 替换Sohn星表的坐标空格为冒号
ra_sohn = [ra_str.replace(' ', ':') for ra_str in Sohn['RAJ2000']]
dec_sohn = [dec_str.replace(' ', ':') for dec_str in Sohn['DEJ2000']]
coo_S06 = SkyCoord(ra_sohn, dec_sohn, frame='icrs')

修改后执行交叉匹配代码即可正常运行:

idx_B15, d2d_B15, d3d_B15 = coo_S06.match_to_catalog_sky(coo_B15)

内容的提问来源于stack exchange,提问作者hamid

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 12:01:05