摆脱天体中的单位

Getting rid of units in astropy

我有一个很大的 (262615,3) 值数组,所有值都附加了单位。具体来源于这个函数:

def coordconvert(data):
    from astropy.coordinates import SkyCoord
    from astropy import units as u
    import numpy as np 

    R   = data[:,0]
    ra  = data[:,1]
    dec = data[:,2]

    c = SkyCoord(ra=ra*u.degree,dec=dec*u.degree,distance=R*u.mpc)
    outdata = c.cartesian.xyz

    return outdata

我希望这个数组没有单位,这样我就可以轻松地将它写入文本文件。 在任何人链接 Stack Exchange question 询问类似问题之前,我已经尝试使用 .magnitude 但它不起作用。另外我想补充一点,由于我的数组的性质,如果可能的话,我更喜欢最有效的方法。

数据样本:

 <Quantity -473.698 mpc> <Quantity -38.3794 mpc> <Quantity -1832.23 mpc>
 <Quantity -2269.57 mpc> <Quantity -842.855 mpc> <Quantity -2445.88 mpc>

只需使用value属性,但需要确保指定您感兴趣的坐标部分和单位,例如R.A。值:

from astropy.coordinates import SkyCoord
from astropy import units as u

c = SkyCoord(ra=[0., 1., 100.] *u.degree,dec=[1., 2., -3.]*u.degree)

c.ra

>> <SkyCoord (ICRS): (ra, dec) in deg
>> [(   0.,  1.), (   1.,  2.), ( 100., -3.)]>

c.ra.value
>> array([   0.,    1.,  100.])

您还可以获得不同格式的单位,例如hours/radians/arcmin 角度

c.ra.arcmin
>> array([    0.,    60.,  6000.])

您的 c.cartesian.xyz 是一个 Quantity 对象。它有一个 unit 属性和一个 value 属性。

value 属性是一个 Numpy 数组,我想这就是你想要的。

示例:

>>> from astropy.coordinates import SkyCoord
>>> c = SkyCoord(10, 20, unit='deg')
>>> c.cartesian.xyz
<Quantity [0.92541658, 0.16317591, 0.34202014]>
>>> c.cartesian.xyz.value
array([0.92541658, 0.16317591, 0.34202014])
>>> type(c.cartesian.xyz.value)
numpy.ndarray