Huanian Zhang
Huanian Zhang

Reputation: 860

AstroPy SkyCoord extremely slow, how to resovle it?

I am using AstroPy SkyCoord to do conversion from equatorial coordinates to galactic coordinates with millions data, it is extremely slow. Anyone has idea to speed it up, otherwise it takes forever to run the whole dataset. The code is below:

from astropy import units as u
from astropy.coordinates import SkyCoord
import numpy as np

ra1 = np.loadtxt('data.txt',usecols=(0,))
dec1 = np.loadtxt('data.txt',usecols=(1,))
size = len(ra1)
for i in range(size):
    ra = ra1[i]
    dec = dec1[i]
    c = SkyCoord(ra*u.degree, dec*u.degree)
    cc = c.galactic
    b = cc.b.degree
    l = cc.l.degree

Upvotes: 2

Views: 1557

Answers (1)

user707650
user707650

Reputation:

I loop over the whole data, but do the conversion one by one.

Don't do that. Think vector-wise, just like numpy. Most routines in astropy are meant to be used vector-wise.

Thus:

from astropy import units as u
from astropy.coordinates import SkyCoord
import numpy as np

c = SkyCoord(np.array(ra1)*u.degree, np.array(dec1)*u.degree)
cc = c.galactic
b = cc.b.degree
l = cc.l.degree

and don't loop over it.

c, cc, b and l will all be arrays (albeit some are SkyCoord arrays), with the same length as ra1 and dec1.

For a 180,000 on your machine, this should take less than a second to run.


Hardly ever should you have to run a for-loop in Python when your data (list) grows to more than 10,000 or 100,000 elements. Use numpy (or astropy here), or if there is no other option, seek out Cython or even code it in C. (Or use PyPi, but that loses a lot of library compatibilities.)

Python is not fast when looping over (large) lists/arrays, and it was never meant to be.

Upvotes: 13

Related Questions