格式化坐标字符串#

通过分别处理组件(例如RA和Dec),获得坐标的字符串表示是最有效的方法。

实例#

要获取坐标的字符串表示:

>>> from astropy.coordinates import ICRS
>>> from astropy import units as u
>>> coo = ICRS(187.70592*u.degree, 12.39112*u.degree)
>>> str(coo.ra) + ' ' + str(coo.dec)
'187d42m21.312s 12d23m28.032s'

为了更好地控制格式,可以使用角度' to_string() 方法(见) 使用角度 更多)。例如::

>>> rahmsstr = coo.ra.to_string(u.hour)
>>> str(rahmsstr)
'12h30m49.4208s'
>>> decdmsstr = coo.dec.to_string(u.degree, alwayssign=True)
>>> str(decdmsstr)
'+12d23m28.032s'
>>> rahmsstr + ' ' + decdmsstr
u'12h30m49.4208s +12d23m28.032s'

您也可以使用Python的 format 字符串方法来创建更复杂的字符串表达式,例如IAU样式的坐标甚至完整的句子:

>>> (f'SDSS J{coo.ra.to_string(unit=u.hourangle, sep="", precision=2, pad=True)}'
...  f'{coo.dec.to_string(sep="", precision=2, alwayssign=True, pad=True)}')
'SDSS J123049.42+122328.03'
>>> f'The galaxy M87, at an RA of {coo.ra.hour:.2f} hours and Dec of {coo.dec.deg:.1f} degrees, has an impressive jet.'
'The galaxy M87, at an RA of 12.51 hours and Dec of 12.4 degrees, has an impressive jet.'