<p>I faced this issue recently, and the solution I came up with was to write a custom query using the <a href="https://www.cosmos.esa.int/web/gaia-users/archive/writing-queries" rel="noreferrer">ADQL documentation</a> and then use <code>astroquery</code> to process the query.</p>
<pre><code>import astroquery.gaia

num_stars = 10

query = f&quot;&quot;&quot;SELECT TOP {num_stars} dr3.ra, dr3.dec, dr3.phot_g_mean_mag, dr3.distance_gspphot
FROM gaiadr3.gaia_source as dr3
ORDER BY dr3.distance_gspphot ASC
&quot;&quot;&quot;

job = astroquery.gaia.Gaia.launch_job(query)

table = job.get_results()

print(table)
</code></pre>
<p>which outputs</p>
<pre><code>        ra                dec         phot_g_mean_mag distance_gspphot
       deg                deg               mag              pc       
------------------ ------------------ --------------- ----------------
217.39232147200883 -62.67607511676666        8.984749           1.3011
269.44850252543836  4.739420051112412       8.1939745           1.8275
165.83095967577933 35.948653032660104        6.551172           2.5453
 282.4587890175222 -23.83709744872712        9.126414           2.9762
 53.22829341517546 -9.458168216292322        3.465752           3.2179
 346.5039166796005  -35.8471642082214       6.5220323           3.2877
176.93768799004127 0.7991199702364985           9.601           3.3731
 316.7484792940004  38.76386244649797        4.766713           3.4904
  316.753662752556  38.75607277205679       5.4506445           3.4947
 280.6830708352289 59.638357907754816        7.854393            3.522
&lt;Table length=10&gt;
      name        dtype  unit                          description                          
---------------- ------- ---- --------------------------------------------------------------
              ra float64  deg                                                Right ascension
             dec float64  deg                                                    Declination
 phot_g_mean_mag float32  mag                                          G-band mean magnitude
distance_gspphot float32   pc Distance from GSP-Phot Aeneas best library using BP/RP spectra
<span class="math-container">```</span>
</code></pre>

CC BY-SA 4.0
2023-03-20T23:44:08.680Z
Thanks, that's great. I guess I'll have to try to familiarize myself with how to get all the rows output to a single file as well, but given the correct query I suspect that will be easier to find out.
Outis Nemo
CC BY-SA 4.0
2023-03-20T23:48:30.557Z
For writing to a file, one option is to use the [`write()`](https://docs.astropy.org/en/stable/table/io.html) method of the resulting `astropy.table.Table` instance
Roy Smart
CC BY-SA 4.0
2023-03-21T00:08:33.637Z
Thanks a lot. I find that pretty easily in the docs too. I think I'm understanding the query language a bit better now, it's not that different from database query languages. I'm wondering one thing, though: I just added a WHERE clause to set a minimum distance, which is working as expected, and I reach the very end of the list by doing so, giving me no stars beyond ~36,000 parsec (~120,000 light-years); is that correct? Given that this is roughly the width of the Milky Way, and that I doubt any individual stars outside our own galaxy are included, I guess that sounds right to me.
Outis Nemo
CC BY-SA 4.0
2023-03-21T00:18:34.513Z
I think that's correct. Section 5.5 of [this paper](https://iopscience.iop.org/article/10.3847/1538-3881/abd806#ajabd806s5) says that they assume all the sources are single stars in the Milky Way.
Roy Smart
CC BY-SA 4.0
2023-03-23T01:26:32.700Z
I was tinkering a bit more on this now, and much to my chagrin it seems that some of the closest stars to us are missing from the results; for example, where is Sirius in the above table in your example (which are the same results I'm getting)? Another star I looked for in vain in the results was Arcturus. Is there something about how this works that I'm completely missing?
Outis Nemo
CC BY-SA 4.0
2023-03-23T01:48:22.333Z
I didn't know this, but apparently bright stars are excluded from Gaia archive since they saturate the detector https://www.cosmos.esa.int/web/gaia-users/archive/faq#accordion-kzsv_-7
Roy Smart
CC BY-SA 4.0
2023-03-20T23:23:58.020Z
2023-03-20T23:23:58.020Z
Roy Smart
<p>I've been trying to find this out for a while now, but I'm making zero progress. I really struggle to find good documentation for the query language, and even in the astroquery library I can't find out how to do it. I've worked with a lot of APIs before, but this seems rather arcane to me. I've tried using the search on Gaia's own site, but the query language there is even more confusing. And nowhere on <a href="https://astroquery.readthedocs.io/en/latest/gaia/gaia.html#query-object" rel="noreferrer">this</a> site can I find anything about what I'm trying to do.</p>
<p>Any help is appreciated.</p>

<astrophysics><distances><python><gaia>
How can I get a list of the closest x stars and their distances from the Gaia ESA Archive using Python?
Outis Nemo
# How can I get a list of the closest x stars and their distances from the Gaia ESA Archive using Python?

(Asked by: username_0 on 2023-03-20)

I've been trying to find this out for a while now, but I'm making zero progress. I really struggle to find good documentation for the query language, and even in the astroquery library I can't find out how to do it. I've worked with a lot of APIs before, but this seems rather arcane to me. I've tried using the search on Gaia's own site, but the query language there is even more confusing. And nowhere on [this](https://astroquery.readthedocs.io/en/latest/gaia/gaia.html#query-object) site can I find anything about what I'm trying to do.

Any help is appreciated.

**Tags:** astrophysics, distances, python, gaia


---

## 1 answers

### Answer by username_1 on 2023-03-20. Score: 7 | Accepted answer

I faced this issue recently, and the solution I came up with was to write a custom query using the ADQL documentation and then use astroquery to process the query.
import astroquery.gaia

num_stars = 10

query = f"""SELECT TOP {num_stars} dr3.ra, dr3.dec, dr3.phot_g_mean_mag, dr3.distance_gspphot
FROM gaiadr3.gaia_source as dr3
ORDER BY dr3.distance_gspphot ASC
"""

job = astroquery.gaia.Gaia.launch_job(query)

table = job.get_results()

print(table)

which outputs
        ra                dec         phot_g_mean_mag distance_gspphot
       deg                deg               mag              pc       
------------------ ------------------ --------------- ----------------
217.39232147200883 -62.67607511676666        8.984749           1.3011
269.44850252543836  4.739420051112412       8.1939745           1.8275
165.83095967577933 35.948653032660104        6.551172           2.5453
 282.4587890175222 -23.83709744872712        9.126414           2.9762
 53.22829341517546 -9.458168216292322        3.465752           3.2179
 346.5039166796005  -35.8471642082214       6.5220323           3.2877
176.93768799004127 0.7991199702364985           9.601           3.3731
 316.7484792940004  38.76386244649797        4.766713           3.4904
  316.753662752556  38.75607277205679       5.4506445           3.4947
 280.6830708352289 59.638357907754816        7.854393            3.522

      name        dtype  unit                          description                          
---------------- ------- ---- --------------------------------------------------------------
              ra float64  deg                                                Right ascension
             dec float64  deg                                                    Declination
 phot_g_mean_mag float32  mag                                          G-band mean magnitude
distance_gspphot float32   pc Distance from GSP-Phot Aeneas best library using BP/RP spectra
```

> **Comments:**
> **username_0**: Thanks, that's great. I guess I'll have to try to familiarize myself with how to get all the rows output to a single file as well, but given the correct query I suspect that will be easier to find out.  
> **username_1**: For writing to a file, one option is to use the [`write()`](https://docs.astropy.org/en/stable/table/io.html) method of the resulting `astropy.table.Table` instance  
> **username_0**: Thanks a lot. I find that pretty easily in the docs too. I think I'm understanding the query language a bit better now, it's not that different from database query languages. I'm wondering one thing, though: I just added a WHERE clause to set a minimum distance, which is working as expected, and I reach the very end of the list by doing so, giving me no stars beyond ~36,000 parsec (~120,000 light-years); is that correct? Given that this is roughly the width of the Milky Way, and that I doubt any individual stars outside our own galaxy are included, I guess that sounds right to me.  
> **username_1**: I think that's correct. Section 5.5 of [this paper](https://iopscience.iop.org/article/10.3847/1538-3881/abd806#ajabd806s5) says that they assume all the sources are single stars in the Milky Way.  
> **username_0**: I was tinkering a bit more on this now, and much to my chagrin it seems that some of the closest stars to us are missing from the results; for example, where is Sirius in the above table in your example (which are the same results I'm getting)? Another star I looked for in vain in the results was Arcturus. Is there something about how this works that I'm completely missing?  
> **username_1**: I didn't know this, but apparently bright stars are excluded from Gaia archive since they saturate the detector https://www.cosmos.esa.int/web/gaia-users/archive/faq#accordion-kzsv_-7  

---

