so I recently wrote a basic code to analyze the distance between two points which are stored in two separate excel files. Now that I have that code working its time to apply the legitimate use and use this code to analyze not just 2 dimensions (x,y) but three (ra,dec,z). In the data sheets now I have stored the coordinates of two separate galaxies. Heres the amended code below:

import math
import pandas
file1 = pandas.read_excel('Book1.xlsx')
file2 = pandas.read_excel('Book2.xlsx')
file1['RA_diff'] = file2['RA'] - file1['RA']
file1['DEC_diff'] = file2['DEC'] - file1['DEC']
file1['Z_diff'] = file2['Z'] - file1['Z']
dist = file1.apply(lambda row: math.hypot(row['RA_diff'], row['DEC_diff', row['Z_diff']]),   axis=1)
if dist.values >= .5:
    print 'no match'
elif dist.values <= .5:
    print True, dist

This code works great with just the RA and DEC but not no much with the Z added in.

My hope is that somebody please give me some pointers on how to shorten this whole process by using the method:

from astropy.coordinates import SkyCoord

Basically: How do I use this tool to my advantage in reading in (from two separate excel documents) two separate coordinates of (ra, dec, z) to determine if the objects have any proximity to one another?

My research has turned up rather fruitless to analyze the points in this way specifically as I mostly simply don't understand the help pages. This is my first ever coding project so please be gentle in your advice, recommendations, help, feedback or ANYTHING else you might be able to offer. Thanks in advance.


1 Answer 1


From the documentation

>>> c1 = SkyCoord(ra=10*u.degree, dec=9*u.degree, distance=10*u.pc, frame='icrs')
>>> c2 = SkyCoord(ra=11*u.degree, dec=10*u.degree, distance=11.5*u.pc, frame='icrs')
>>> c1.separation_3d(c2)  
<Distance 1.5228602415117989 pc>

The rest of the code is just reading the excel files and printing the results. What you have now is incorrect, even without the "z" coordinate. You can't calculate the separation of two points in spherical coordinates by Pythagoras' theorem: eg (30*, 0*) and (30*, 359*) should be very close. You need to convert to cartesian coordinates first, which is what the astropy module does.

  • $\begingroup$ OK thanks you, so what you put above is basically the necessary conversion from Cartesian to spherical coordinates?Also do you still recommend using pandas to read in the excel file, or is there a better way in astropy? $\endgroup$
    – Justin T
    Commented Jul 24, 2016 at 18:04
  • $\begingroup$ The sample code calculates the distance between two points given in spherical polar coordinates. THe points are (10*,9*,10parsecs) and (11*, 10*, 11.5parsecs). I don't know the exact format of your excel files, but the above should be enough to get you started. Astropy can't read excel files, so if pandas works for you, use it. $\endgroup$
    – James K
    Commented Jul 24, 2016 at 20:34
  • $\begingroup$ Right on, so what I turned up on a quick search is that pandas doesn't handle multidimensional tables, so now I'm a little discouraged that all my code is off. But I'll keep plugging away, is there a better option than pandas to read in and compare data from multiple tables in astropy that I can use to get this first part working? $\endgroup$
    – Justin T
    Commented Jul 24, 2016 at 21:19
  • $\begingroup$ I don't know what your tables are like, or why there are two work books. Can you put all your data in one csv file? Anyway, This would be a stackoverflow question, if you can't get it to work yourself $\endgroup$
    – James K
    Commented Jul 24, 2016 at 22:02
  • $\begingroup$ @JustinT As James says, your question about reading files should be on stack overflow, but I will say you should consider different formats for your file. Put your data in a text file (simple as copying and pasting from excel to text file) and use something like numpy.genfromtxt to read it in. $\endgroup$
    – zephyr
    Commented Jul 26, 2016 at 17:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .