Function for finding the center of a set of RA/DEC coordinates

121 views
Skip to first unread message

Emilio Salazar

unread,
Nov 29, 2016, 3:30:56 AM11/29/16
to astropy-dev
Hi all.
Is there any way to calculate the center point of a set of Coordinates (RA, DEC)?

Thanks,
Emilio.

Demitri Muna

unread,
Nov 29, 2016, 3:46:46 AM11/29/16
to astro...@googlegroups.com
Hi Emilio,

On Nov 29, 2016, at 3:30 AM, Emilio Salazar <esal...@gmail.com> wrote:

> Is there any way to calculate the center point of a set of Coordinates (RA, DEC)?

I had a need to do this recently for lat/lon values; you should be able to adapt the code below pretty easily. (Note the difference between radians and degrees... that always trips me up.)

Cheers,
Demitri

_________________________________________
Demitri Muna
http://muna.com

Center for Cosmology and AstroParticle Physics
& Department of Astronomy
Le Ohio State University

My Projects:
http://nightlightapp.io
http://trillianverse.org
http://scicoder.org

---

def center_geolocation(geolocations):
"""
Provide a relatively accurate center lat, lon returned as a list pair, given
a list of list pairs.
ex: in: geolocations = ((lat1,lon1), (lat2,lon2),)
out: (center_lat, center_lon)

Ref: http://stackoverflow.com/questions/6671183/calculate-the-center-point-of-multiple-latitude-longitude-coordinate-pairs
Source: https://gist.github.com/amites/3718961
"""
x = 0
y = 0
z = 0

for lat, lon in geolocations:
lat = float(lat)
lon = float(lon)
x += cos(lat) * cos(lon)
y += cos(lat) * sin(lon)
z += sin(lat)

x = float(x / len(geolocations))
y = float(y / len(geolocations))
z = float(z / len(geolocations))

return (atan2(z, sqrt(x * x + y * y)), atan2(y, x))


Marten van Kerkwijk

unread,
Nov 29, 2016, 9:56:18 AM11/29/16
to astro...@googlegroups.com
Hi Emilio,

In current master, you can do
```
coords.cartesian.mean()
```
(or coords.spherical.mean(), which internally transforms to cartesian,
takes the mean of x, y, and z, and then transforms back)

This will be part of the astropy 1.3 release.

Note that it is only possible to take the mean of the *representation*
of the coordinates. Effectively, this is seen as implying a set of
vectors, for which arithmetic is well-defined. It is much less clear
what, more generally, one would mean by, say, adding or subtracting
two coordinates, so we do not plan on having `coords.mean()`.

All the best,

Marten

Emilio Salazar

unread,
Nov 30, 2016, 4:55:20 AM11/30/16
to astropy-dev
Thanks, guys!

Erik Tollerud

unread,
Dec 9, 2016, 3:01:49 PM12/9/16
to astropy-dev, Emilio Salazar
+1 to Marten's suggestion here, but one bit to add: if you want to get back a regular SkyCoord instead of a representation, that's pretty easy.  You'd do something like this:

# these are the coordinates you want to get the mean of:
sc = SkyCoord(ra=[1,2,3]*u.deg, dec=[1,2,3]*u.deg, frame='fk5', equinox='J1950')
meancoo = SkyCoord(sc.cartesian.mean(), frame=sc.frame)

Then `meancoo` is a regular SkyCoord object in the right frame and everything.

---
Erik T

--
You received this message because you are subscribed to the Google Groups "astropy-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to astropy-dev+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages