天文データ解析入門 その10 (astropy.coordinates.SkyCoordのあれこれ)

本記事では、[astropy.coordinates.SkyCoord](https://docs.astropy.org/en/stable/api/astropy.coordinates.SkyCoord.html) の簡単な使い方について紹介します。

まず必要なものを import します。

```python
from astropy.coordinates import SkyCoord
from astropy.coordinates import ICRS, Galactic, FK4, FK5
import astropy.units as u
import numpy as np
```

## 座標オブジェクトの定義
### FK5

```python
coords = "12h34m56.7s +12d34m56.7s" # スペースの位置に注意
c = SkyCoord(coords, frame=FK5, unit="deg") # unit=u.deg でもok
print(c)
#<SkyCoord (FK5: equinox=J2000.000): (ra, dec) in deg
#    (188.73625, 12.58241667)>
```


```python
coords = "12:34:56.7 +12:34:56.7" # : でもいける
c = SkyCoord(coords, frame=FK5, unit=(u.hourangle, u.deg))
coords = "12 34 56.7 +12 34 56.7" # スペースでもいける
c = SkyCoord(coords, frame=FK5, unit=(u.hourangle, u.deg))
coords = "188.73625 12.58241667" # degree でもいける
c = SkyCoord(coords, frame=FK5, unit=u.deg)

coords = ["1h12m45.6s +1d23m45.6s", "12h34m56.7s +12d34m56.7s"] # list や numpy array も食べられる
c = SkyCoord(coords, frame=FK5, unit=u.deg)
print(c)
#<SkyCoord (FK5: equinox=J2000.000): (ra, dec) in deg
#    [( 18.19   ,  1.396     ), (188.73625, 12.58241667)]>
```
### Galactic

```python
coords = "12.34 0.12"
c = SkyCoord(coords, frame=Galactic, unit="deg")
#<SkyCoord (Galactic): (l, b) in deg
#    (12.34, 0.12)>

c = SkyCoord(12.34, 0.12, frame=Galactic, unit="deg")

c = SkyCoord("12h34m56.7s +0d12m34s", frame=Galactic)
print(c)
#<SkyCoord (Galactic): (l, b) in deg
#    (188.73625, 0.20944444)>
```
オブジェクト内に obstime や radial_velocity などを格納することも可能。


## 座標の取り出し
FK5

```python
coords = "12h34m56.7s +12d34m56.7s"
c = SkyCoord(coords, frame=FK5, unit="deg")
print(c.ra)
print(c.dec)
print(c.ra.deg)
print(c.dec.deg)
#188d44m10.5s
#12d34m56.7s
#188.73624999999998
#12.582416666666667
```
Galactic

```python
coords = "12.34 0.12"
c = SkyCoord(coords, frame=Galactic, unit="deg")
print(c.l)
print(c.b)
print(c.l.deg)
print(c.b.deg)
#12d20m24s
#0d07m12s
#12.34
#0.12
```

時分秒などへの変換
```python
c.to_string(style="decimal")
c.to_string(style="dms")
c.to_string(style="hmsdms")
```
## 座標の変換

```python
coords = "12h34m56.7s +12d34m56.7s"
c = SkyCoord(coords, frame=FK5, unit="deg")
print(c.galactic.l.deg)
print(c.galactic.b.deg)
#287.2526296864695
#74.94982709188524

coords = "12.34 0.12"
c = SkyCoord(coords, frame=Galactic, unit="deg")
print(c.fk5.ra.deg)
print(c.fk5.dec.deg)
#273.0271677731397
#-18.183520254020582
```
## 天体名から座標を取得

```python
W51 = SkyCoord.from_name('W51')
print(W51)
#<SkyCoord (ICRS): (ra, dec) in deg
#    (290.95833333, 14.1)>
```
## Az–El (方位角–仰角) を取得

```python
from astropy.coordinates import EarthLocation, AltAz
from astropy.time import Time
nobeyama = EarthLocation(lon=138.472555556*u.deg, lat=35.9446944444*u.deg, height=1350*u.m)
utcoffset = 9*u.hour
obstime = Time('2021-6-11 00:00:00') - utcoffset

W51 = SkyCoord.from_name('W51')
W51altaz = W51.transform_to(AltAz(obstime=obstime, location=nobeyama))
print(W51altaz)
print(W51altaz.az.deg) #Az
print(W51altaz.alt.deg) # El
#<SkyCoord (AltAz: obstime=2021-06-10 15:00:00.000, location=(-3870981.01680331, 3428061.63171939, #3724017.76068491) m, pressure=0.0 hPa, temperature=0.0 deg_C, relative_humidity=0.0, obswl=1.0 micron): (az, #alt) in deg
#    (123.1085355, 56.40766096)>
#123.10853550436268
#56.4076609561065
```
以上です。

リンク
[目次](https://qiita.com/Shinji_Fujita/items/ecb869e5b6a9fa468483)