将字符串解析为天文坐标
Parsing string as astronomical coordinates
我正在尝试从 csv 文件加载一组坐标。该文件包含加载到 Pandas 数据框中的星系坐标,如以下代码段所示:
我正在尝试从 _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
does not know spherical geometry or sexagesimal (hours, min, sec): if you want to deal with celestial coordinates, see the astropy.coordinates
package.
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)>
我正在尝试从 csv 文件加载一组坐标。该文件包含加载到 Pandas 数据框中的星系坐标,如以下代码段所示:
我正在尝试从 _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
does not know spherical geometry or sexagesimal (hours, min, sec): if you want to deal with celestial coordinates, see theastropy.coordinates
package.
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)>