# Faster alternative to SkyCoord.seperation?

**URL:** <https://community.openastronomy.org/t/faster-alternative-to-skycoord-seperation/311>\
**Category:** Development\
**Tags:** astropy\
**Created:** [March 17, 2022, 3:15am UTC](https://community.openastronomy.org/t/faster-alternative-to-skycoord-seperation/311 "2022-03-17T03:15:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Axya](https://dub1.discourse-cdn.com/flex005/user_avatar/community.openastronomy.org/axya/32/200_2.png) [@Axya](https://community.openastronomy.org/u/Axya)\
**Post date:** [March 17, 2022, 3:15am UTC](https://community.openastronomy.org/t/faster-alternative-to-skycoord-seperation/311/1 "2022-03-17T03:15:36Z")

</div>

I am using SkyCoord.separation to find the pixels around a given coordinate from an array. I have noticed the separation function takes too long. I have to repeat the calculation on a huge data set and using this function does not seem practical. Is there a faster alternative to SkyCoord.separation which handles arrays? The other similar functions do not seem to take array inputs and can only calculate separation between two sets of coordinates.

For example:  
I have longitude and latitude arrays (size 50,331,648). I need to find the separation of each row with the rest of the array. So I run a loop 50,331,648 times.

Any suggestions would be of great help!  
Thank you.

---

<div class="post-metadata">

**Author:** ![adrn](https://dub1.discourse-cdn.com/flex005/user_avatar/community.openastronomy.org/adrn/32/18_2.png) [@adrn](https://community.openastronomy.org/u/adrn)\
**Post date:** [March 18, 2022, 3:43pm UTC](https://community.openastronomy.org/t/faster-alternative-to-skycoord-seperation/311/2 "2022-03-18T15:43:43Z")

</div>

Hey @Axya,

If I understand correctly: You have an array of ~50 million sky coordinates (longitude and latitude), and you would like to compute the sky separation between all pairs of coordinates? Or am I misunderstanding? That operation by itself is going to be slow because naïvely you will have to do ~(50 million)^2 operations to compute those separations! Can you say more about your goal (i.e. unpack your first sentence a bit more), because it’s possible there is a faster workaround. For example, if you have an array of sky positions, and an array of pixel sky positions, and you want to find the nearest N sky positions to each pixel, you can use the “matching catalogs” functionality in astropy.coordinates [as described here](https://docs.astropy.org/en/stable/coordinates/matchsep.html#astropy-coordinates-matching). This will work if you want to find the nearest N sky positions, or if you want to find all matching sky positions up to some search radius (see the [match\_coordinates\_sky()](https://docs.astropy.org/en/stable/api/astropy.coordinates.match_coordinates_sky.html#astropy.coordinates.match_coordinates_sky) and [search\_around\_sky()](https://docs.astropy.org/en/stable/coordinates/matchsep.html#searching-around-coordinates) methods. These methods will be _much_ faster than trying to compute all pairwise separations. But let me know if I misunderstood your question!

---

<div class="post-metadata">

**Author:** ![Axya](https://dub1.discourse-cdn.com/flex005/user_avatar/community.openastronomy.org/axya/32/200_2.png) [@Axya](https://community.openastronomy.org/u/Axya)\
**Post date:** [March 18, 2022, 4:19pm UTC](https://community.openastronomy.org/t/faster-alternative-to-skycoord-seperation/311/3 "2022-03-18T16:19:20Z")

</div>

Thank you for the response! I am trying to find all the pixels within a certain radius around each coordinate. This is for an all-sky map and not a catalog search.  
I am dealing with a HEALPix map and found healpy.query\_disc after I posted the question. The speed for my loop reduced from 3seconds to 0.4 microseconds! This is more useful for my application as I am trying to extract certain regions around each pixel. I will check these functions you mentioned and see if they perform better.
