<p>From the <a href="http://docs.astropy.org/en/stable/coordinates/index.html" rel="nofollow">documentation</a></p>

<pre><code>&gt;&gt;&gt; c1 = SkyCoord(ra=10*u.degree, dec=9*u.degree, distance=10*u.pc, frame='icrs')
&gt;&gt;&gt; c2 = SkyCoord(ra=11*u.degree, dec=10*u.degree, distance=11.5*u.pc, frame='icrs')
&gt;&gt;&gt; c1.separation_3d(c2)  
&lt;Distance 1.5228602415117989 pc&gt;
</code></pre>

<p>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.</p>

CC BY-SA 3.0
2016-07-24T18:04:38.053Z
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?
Justin T
CC BY-SA 3.0
2016-07-24T20:34:45.160Z
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.
James K
CC BY-SA 3.0
2016-07-24T21:19:54.157Z
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?
Justin T
CC BY-SA 3.0
2016-07-24T22:02:59.567Z
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
James K
CC BY-SA 3.0
2016-07-26T17:24:44.240Z
@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.
zephyr
CC BY-SA 3.0
2016-07-26T19:18:06.397Z
@zephyr Thanks for the tip! Also JamesK I will keep looking around the astropy stuff, thanks for a point in the right direction!
Justin T
CC BY-SA 3.0
2016-07-24T08:10:08.333Z
2016-07-24T08:10:08.333Z
James K
<p>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:</p>

<pre><code>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 &gt;= .5:
    print 'no match'
elif dist.values &lt;= .5:
    print True, dist
</code></pre>

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

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

<pre><code>from astropy.coordinates import SkyCoord
</code></pre>

<p>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?</p>

<p>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. </p>

<galaxy><distances><data-analysis><python>
Using SkyChoord to read in multiple coordinates.
Justin T
# Using SkyChoord to read in multiple coordinates.

(Asked by: username_0 on 2016-07-24)

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.

**Tags:** galaxy, distances, data-analysis, python


---

## 1 answers

### Answer by username_1 on 2016-07-24. Score: 2 | Accepted answer

From the [documentation](http://docs.astropy.org/en/stable/coordinates/index.html)

```
>>> 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.

> **Comments:**
> **username_0**: 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?  
> **username_1**: 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.  
> **username_0**: 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?  
> **username_1**: 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  
> **username_2**: @username_0 As username_1 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.  
> **username_0**: @username_2 Thanks for the tip! Also username_1 I will keep looking around the astropy stuff, thanks for a point in the right direction!  

---

