将字符串解析为天文坐标

问题描述

我正在尝试从csv文件中加载一组坐标。该文件包含星系坐标,该星系坐标如以下代码片段所示加载到Pandas数据框中:

enter image description here

我正在尝试从_RAJ2000_DEJ2000列中提取坐标,以便可以将它们与另一个文件交叉匹配。我的代码:

import numpy as np
from astropy import units as u
from astropy.coordinates import SkyCoord as coord
import quasar_functions as qf

# c1 = coord('5h23m34.5s','-69d45m22s',distance = 70*u.kpc,frame = 'icrs')
# c2 = coord('0h52m44.8s','-72d49m43s',distance = 80*u.kpc,frame = 'fk5')

# sep = c1.separation(c2)
# sep_3d = c1.separation_3d(c2)

data = ...# here's where I call my loading function
ra1,dec1 = data['_RAJ2000'],data['_DEJ2000']
ooi1_ra,ooi1_dec = ra1[22],ra1[60]
object1_coords = coord(ra1[22]*u.hour,dec1[22]*u.degree)
object2_coords = coord(ra1[60]*u.hour,dec1[60]*u.degree)

但我遇到错误:

ValueError: '01 24 45.98328' did not parse as unit: Syntax error parsing unit '01 24 45.98328' If
this is meant to be a custom unit,define it with 'u.def_unit'. To have it recognized inside a file
reader or other code,enable it with 'u.add_enabled_units'. For details,see
https://docs.astropy.org/en/latest/units/combining_and_defining.html

我不想将其定义为自定义单元;我宁愿Astropy本机读取(如果可能的话),或者修改字符串以使Astropy可以处理坐标。我认为RA以h / m / s为单位,DE以度为单位。

解决方法

您正在做的事情等同于:

>>> '01 24 45.98328' * u.hour

表示试图将字符串'01 24 45.98328'转换为以小时为单位的数量。同上,但以度为单位。我可以看到其背后的逻辑,也许应该予以支持。但是每个the docs的原始单位都不知道如何处理不同的坐标系表示形式:

astropy.units不知道球形几何或六进制(小时,分钟,秒):如果要处理天体坐标,请参见astropy.coordinates包。

TL; DR SkyCoord本身处理解析的坐标格式,您可以直接将坐标指定为SkyCoord的单位:

>>> SkyCoord(ra='01 24 45.98328',dec='+22 30 04.3700',unit=(u.hourangle,u.deg))
<SkyCoord (ICRS): (ra,dec) in deg
    (21.191597,22.50121389)>

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...