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)?


2 Answers 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) 

# 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
# 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)
  • 1
    $\begingroup$ 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. $\endgroup$
    – pela
    Feb 12, 2023 at 11:08
  • $\begingroup$ 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 $\endgroup$
    – Atul
    Mar 28, 2023 at 20:49
  • $\begingroup$ 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. $\endgroup$
    – pela
    Apr 20, 2023 at 13:13
  • $\begingroup$ Dear Pela thank you for the feedback its really helpful. i did the required changes. Best $\endgroup$
    – Atul
    Apr 22, 2023 at 1:12

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$



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 \, \, }$$



You must log in to answer this question.

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