座標文字列のフォーマットを設定する

コンポーネント(たとえば,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 Stringメソッドは、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.'