Mosh Mosh -4 years ago 119
Python Question

astlib coordinates code to astropy coordinates code

I'm trying to fix this code which used

to convert coordinates to use the
coordinate modules. Here's a copy of the code:

from astLib import astCoords as coords

#Convert B1950 coordinates (as given)
for i in range(len(clra)):
clra[i], cldec[i] = coords.convertCoords(
'B1950', 'J2000', clra1950[i],
cldec1950[i], 1950

#Convert input coords to Galactic coords
lgal,bgal = radians(coords.convertCoords(
'J2000', 'GALACTIC', ra,

I need help with 2 things.

  1. What are the proper imports for
    if I want to change from
    , and the proper imports to go from
    to galactic coordinates?

  2. in the areas that start with
    , What are the functions and arguments from astropy that go in its place. In other words, what do I replace it with?

Also, I had done some research into this issue. I found this link here:

It describes the notation for
coordinate related functions. However, there is a lot there, and I'm not sure what to use and how to use it.

Answer Source

This is one way to do it:

In [1]: import numpy as np

In [2]: from astropy import units as u

In [3]: from astropy.coordinates import SkyCoord, FK4, FK5, Galactic

In [4]: clra = np.zeros(3)

In [5]: cldec = np.zeros(3)

In [6]: c1 = SkyCoord(clra * u.deg, cldec * u.deg, frame=FK4)

In [7]: c2 = c1.transform_to(FK5(equinox='J2000'))

In [8]: c3 = c2.transform_to(Galactic)

In [9]: print(
[ 97.74220094  97.74220094  97.74220094]

In [10]: print(
[-60.18102359 -60.18102359 -60.18102359]

Note that you can convert c1 to Galactic directly too.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download