【问题标题】:Getting rid of units in astropy在天文学中摆脱单位
【发布时间】:2019-01-10 12:55:36
【问题描述】:

我有一个大的(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>

【问题讨论】:

  • 请提供您的数据样本、预期输出以及您试图获得该输出的具体内容,而不是函数(与问题无关)。

标签: python python-3.x numpy astropy


【解决方案1】:

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

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.])

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

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

【讨论】:

  • 我可以做 c.values 吗?整个数组会丢失所有单位吗?
  • 我只是查了一下,找不到这样做的方法,所以你必须为你的坐标数组的每个轴(R.A.,Dec.,距离)做这件事。不过,我很高兴被证明是错误的!
【解决方案2】:

您的 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-01
    • 2020-03-23
    • 2015-02-27
    • 1970-01-01
    相关资源
    最近更新 更多