<p>As per my understanding one can get the  distance between two object using astropy function. like this</p>
<pre><code># using astropy function the 3d separation 
c1 = SkyCoord(ra1  *u.deg, dec1 *u.deg, distance=d_co1*u.Mpc, frame='icrs') 
c2 = SkyCoord(ra2*u.deg, dec2 *u.deg, distance=d_co2*u.Mpc, frame='icrs') 
# the commoving distance between any two galaxies
sep = c1.separation_3d(c2)
</code></pre>
<p>meanwhile for long distance we do consider the effect of expansion of the universe in that case we  use this method as follows</p>
<pre><code># Comoving distance
D = cosmo.comoving_distance(z_cl) 
D_C=cosmo.comoving_distance(z_halo)

# for member galaxies
ra_rad = RA* np.pi / 180.0  # in radian
dec_rad = DEC * np.pi / 180.0 # in radian
x = D_C * np.cos(dec_rad) * np.cos(ra_rad)
y = D_C * np.cos(dec_rad) * np.sin(ra_rad)
z = D_C * np.sin(dec_rad)

# for cluster
ra_clus = ra_cl* np.pi / 180.0 # in radian
dec_clus = dec_cl * np.pi / 180.0 # in radian

x_cl = D * np.cos(dec_clus) * np.cos(ra_clus)
y_cl = D * np.cos(dec_clus) * np.sin(ra_clus)
z_cl = D * np.sin(dec_clus)

# commoving 3d distance between any two object in space-time
distance=np.sqrt((x_cl-x)**2+(y_cl-y)**2+(z_cl-z)**2)
    
# the physical distance can be calculated as follow:
# D_AB Physical distance to the galaxy B as seen from the galaxy A (ie distance between A and B)
D_AB = np.array(sep)/(1+np.array(zg))

#similarly the physical distance D_BA the distance of the galaxy A as seen from B
D_BA = np.array(sep)/(1+z_cl)
<span class="math-container">```</span>
</code></pre>

CC BY-SA 4.0
2023-02-12T11:08:10.043Z
Are you sure this function can be used for cosmological distances? I think it's made for local distances that don't take into account the expansion of the Universe.
pela
CC BY-SA 4.0
2023-03-28T20:49:43.463Z
Dear i admit that one is for local distances therefore i have written the one which take accounf for the expansion of the universe. please do correct me if possible
Atul
CC BY-SA 4.0
2023-04-20T13:13:10.013Z
In comoving coordinates, SkyCoords should work as intended. To convert a comoving distance $d_\mathrm{com}$ to a physical distance $d_\mathrm{phy} = a\,d_\mathrm{com} = d_\mathrm{com}/(1+z)$, you then just need to specify in _whose_ reference frame you want to calculate it, i.e. whose $z$ to use.
pela
CC BY-SA 4.0
2023-04-22T01:12:59.697Z
Dear Pela thank you for the feedback its really helpful. i did the required changes. Best
Atul
CC BY-SA 4.0
2023-01-13T03:24:05.340Z
2023-04-22T01:11:16.990Z
2023-04-22T01:11:16.990Z
Atul
<p>The general algorithm for the calculation is as follows:</p>
<p>First object Right Ascension, Declination and Redshift: <span class="math-container">$\quad \alpha_1, \ \delta_1, \ z_1$</span></p>
<p>Second object Right Ascension, Declination and Redshift: <span class="math-container">$\alpha_2, \ \delta_2, \ z_2$</span></p>
<p>Applying spherical trigonometry, the cosine of the angle <span class="math-container">$\theta$</span> between both objects is:</p>
<p><span class="math-container">$$\cos \theta = \sin \delta_1 \sin \delta_2+\cos \delta_1 \cos \delta_2 \cos(\alpha_2-\alpha_1)$$</span></p>
<p>For a flat universe, as ours appears to be: <span class="math-container">$\Omega_{K_0}=0$</span>.</p>
<p>For <span class="math-container">$z_i &lt; 100$</span> we can neglect <span class="math-container">$\Omega_{R_0}$</span>. Then, the distances to us now can be calculated using:</p>
<p><span class="math-container">$$d_1=\frac{c}{H_0} \int_0^{z_1} \dfrac{dx}{\sqrt{\Omega_{M_0}(1+x)^3+\Omega_{\Lambda_0}}}$$</span></p>
<p><span class="math-container">$$d_2=\frac{c}{H_0} \int_0^{z_2} \dfrac{dx}{\sqrt{\Omega_{M_0}(1+x)^3+\Omega_{\Lambda_0}}}$$</span></p>
<p><span class="math-container">$c=299 \ 792 \ 458 \ m/s$</span></p>
<p>According to the results-2018 of the Planck Mission, the best values of the cosmological parameters are:</p>
<p><span class="math-container">$H_0=67.66 \ (km/s)/Mpc$</span></p>
<p><span class="math-container">$\Omega_{M_0}=0.3111$</span></p>
<p><span class="math-container">$\Omega_{\Lambda_0}=0.6889$</span></p>
<p>This integral has no elementary primitive, but can be easily calculated by numerical methods.</p>
<p>Finally, to calculate the distance &quot;<span class="math-container">$d$</span>&quot; between both objects now, use the law of cosines:</p>
<p><span class="math-container">$$d=\sqrt{d_1^2+d_2^2-2 \ d_1 d_2 \cos \theta \, \, }$$</span></p>
<p>Regards</p>

CC BY-SA 4.0
2023-04-22T19:32:39.107Z
2023-04-24T14:05:43.080Z
2023-04-24T14:05:43.080Z
Albert
<p>I have Redshift (<code>z</code>), <code>RA</code> and <code>Dec</code> positions for two points in a galaxy catalogue (say <strong>P1</strong> = [<code>z1</code>=4, <code>RA1</code>=140, <code>Dec1</code>=2] and <strong>P2</strong> = [<code>z2</code>=6, <code>RA2</code>=150, <code>Dec2</code>=5], where <code>RA</code> and <code>Dec</code> are given in degrees). How can I find the <strong>comoving distance</strong> between these two points using any package in <strong>python</strong> (like <strong>AstroPy</strong>, <strong>CosmoloPy</strong> etc)? </p>

<p>I am assuming a Flat-LCDM model with the cosmology <code>H0=70, Om0=0.3, Ob0=0.05</code>.</p>

<p>Presently, I am using this:</p>

<pre><code>import cosmolopy.distance as cd
cosmo = {'omega_M_0' : 0.3, 'omega_lambda_0' : 0.7, 'h' : 0.70}
cosmo = cd.set_omega_k_0(cosmo)
z1 = 4.0
z2 = 6.0
d_co = cd.comoving_distance(z2, **cosmo)
</code></pre>

<p>Specifically, my questions are these:</p>

<p>(i) In the said example, how do I use information of the starting redshift <code>z1</code>?</p>

<p>(ii) Do I need to use information of [<code>RA1</code>, <code>Dec1</code>] and [<code>RA2</code>, <code>Dec2</code>] for an accurate calculation of the comoving separation between <code>P1</code> and <code>P2</code> (I need to find the comoving distance to a great degree of accuracy)? </p>

CC BY-SA 4.0
2023-03-29T11:09:43.693Z
You can see an answer in: https://physics.stackexchange.com/a/409488/193705
Albert
<cosmology><distances><coordinate><redshift><python>
Comoving distance between two points [(RA1, Dec1, z1) and (RA2, Dec2, z2)]
Ling Guo
# Comoving distance between two points [(RA1, Dec1, z1) and (RA2, Dec2, z2)]

(Asked by: username_0 on 2018-07-08)

I have Redshift (`z`), `RA` and `Dec` positions for two points in a galaxy catalogue (say **P1** = [`z1`=4, `RA1`=140, `Dec1`=2] and **P2** = [`z2`=6, `RA2`=150, `Dec2`=5], where `RA` and `Dec` are given in degrees). How can I find the **comoving distance** between these two points using any package in **python** (like **AstroPy**, **CosmoloPy** etc)?

I am assuming a Flat-LCDM model with the cosmology `H0=70, Om0=0.3, Ob0=0.05`.

Presently, I am using this:

```
import cosmolopy.distance as cd
cosmo = {'omega_M_0' : 0.3, 'omega_lambda_0' : 0.7, 'h' : 0.70}
cosmo = cd.set_omega_k_0(cosmo)
z1 = 4.0
z2 = 6.0
d_co = cd.comoving_distance(z2, **cosmo)

```

Specifically, my questions are these:

(i) In the said example, how do I use information of the starting redshift `z1`?

(ii) Do I need to use information of [`RA1`, `Dec1`] and [`RA2`, `Dec2`] for an accurate calculation of the comoving separation between `P1` and `P2` (I need to find the comoving distance to a great degree of accuracy)?

**Tags:** cosmology, distances, coordinate, redshift, python

> **Comments:**
> **username_1**: You can see an answer in: https://physics.stackexchange.com/a/409488/193705  

---

## 2 answers

### Answer by username_2 on 2023-01-13. Score: 2

As per my understanding one can get the distance between two object using astropy function. like this

```
# using astropy function the 3d separation 
c1 = SkyCoord(ra1  *u.deg, dec1 *u.deg, distance=d_co1*u.Mpc, frame='icrs') 
c2 = SkyCoord(ra2*u.deg, dec2 *u.deg, distance=d_co2*u.Mpc, frame='icrs') 
# the commoving distance between any two galaxies
sep = c1.separation_3d(c2)

```

meanwhile for long distance we do consider the effect of expansion of the universe in that case we use this method as follows

```
# Comoving distance
D = cosmo.comoving_distance(z_cl) 
D_C=cosmo.comoving_distance(z_halo)

# for member galaxies
ra_rad = RA* np.pi / 180.0  # in radian
dec_rad = DEC * np.pi / 180.0 # in radian
x = D_C * np.cos(dec_rad) * np.cos(ra_rad)
y = D_C * np.cos(dec_rad) * np.sin(ra_rad)
z = D_C * np.sin(dec_rad)

# for cluster
ra_clus = ra_cl* np.pi / 180.0 # in radian
dec_clus = dec_cl * np.pi / 180.0 # in radian

x_cl = D * np.cos(dec_clus) * np.cos(ra_clus)
y_cl = D * np.cos(dec_clus) * np.sin(ra_clus)
z_cl = D * np.sin(dec_clus)

# commoving 3d distance between any two object in space-time
distance=np.sqrt((x_cl-x)**2+(y_cl-y)**2+(z_cl-z)**2)
    
# the physical distance can be calculated as follow:
# D_AB Physical distance to the galaxy B as seen from the galaxy A (ie distance between A and B)
D_AB = np.array(sep)/(1+np.array(zg))

#similarly the physical distance D_BA the distance of the galaxy A as seen from B
D_BA = np.array(sep)/(1+z_cl)
```

```

> **Comments:**
> **username_3**: Are you sure this function can be used for cosmological distances? I think it's made for local distances that don't take into account the expansion of the Universe.  
> **username_2**: Dear i admit that one is for local distances therefore i have written the one which take accounf for the expansion of the universe. please do correct me if possible  
> **username_3**: In comoving coordinates, SkyCoords should work as intended. To convert a comoving distance $d_\mathrm{com}$ to a physical distance $d_\mathrm{phy} = a\,d_\mathrm{com} = d_\mathrm{com}/(1+z)$, you then just need to specify in _whose_ reference frame you want to calculate it, i.e. whose $z$ to use.  
> **username_2**: Dear username_3 thank you for the feedback its really helpful. i did the required changes. Best  

---

### Answer by username_1 on 2023-04-22. Score: 1

The general algorithm for the calculation is as follows:

First object Right Ascension, Declination and Redshift: $\quad \alpha\_1, \ \delta\_1, \ z\_1$

Second object Right Ascension, Declination and Redshift: $\alpha\_2, \ \delta\_2, \ z\_2$

Applying spherical trigonometry, the cosine of the angle $\theta$ between both objects is:

$$\cos \theta = \sin \delta\_1 \sin \delta\_2+\cos \delta\_1 \cos \delta\_2 \cos(\alpha\_2-\alpha\_1)$$

For a flat universe, as ours appears to be: $\Omega\_{K\_0}=0$.

For $z\_i < 100$ we can neglect $\Omega\_{R\_0}$. Then, the distances to us now can be calculated using:

$$d\_1=\frac{c}{H\_0} \int\_0^{z\_1} \dfrac{dx}{\sqrt{\Omega\_{M\_0}(1+x)^3+\Omega\_{\Lambda\_0}}}$$

$$d\_2=\frac{c}{H\_0} \int\_0^{z\_2} \dfrac{dx}{\sqrt{\Omega\_{M\_0}(1+x)^3+\Omega\_{\Lambda\_0}}}$$

$c=299 \ 792 \ 458 \ m/s$

According to the results-2018 of the Planck Mission, the best values of the cosmological parameters are:

$H\_0=67.66 \ (km/s)/Mpc$

$\Omega\_{M\_0}=0.3111$

$\Omega\_{\Lambda\_0}=0.6889$

This integral has no elementary primitive, but can be easily calculated by numerical methods.

Finally, to calculate the distance "$d$" between both objects now, use the law of cosines:

$$d=\sqrt{d\_1^2+d\_2^2-2 \ d\_1 d\_2 \cos \theta \, \, }$$

Regards


---

