AstroPy SkyCoord extremely slow, how to resovle it?

穿精又带淫゛_ 提交于 2021-01-28 23:34:24

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/36146183/astropy-skycoord-extremely-slow-how-to-resovle-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!