Skip to content

Instantly share code, notes, and snippets.

@elnjensen
Created November 3, 2018 20:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elnjensen/20e9b7bd9c18444be6ea63b131d5d329 to your computer and use it in GitHub Desktop.
Save elnjensen/20e9b7bd9c18444be6ea63b131d5d329 to your computer and use it in GitHub Desktop.
Example of querying Gaia DR2 from Python
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Example of querying Gaia DR2 from Python. "
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"# Silence warnings from deprecated VO fields in the Gaia astroquery output: \n",
"import warnings\n",
"warnings.filterwarnings(\"ignore\", module='astropy.io.votable.tree')\n",
"warnings.filterwarnings(\"ignore\", message='.*unclosed..socket')\n",
"\n",
"from __future__ import print_function, division\n",
"import astropy.units as u \n",
"from astropy.coordinates import SkyCoord, ICRS\n",
"from astroquery.gaia import Gaia\n"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def gaia_query(ra, dec, radius=5 * u.arcsec):\n",
" '''Return an ADQL query string for Gaia DR2 + geometric distances \n",
" from Bailer-Jones et al. 2018 '''\n",
" query_string = '''SELECT *, DISTANCE(POINT('ICRS',g.ra, g.dec), \n",
" POINT('ICRS', %s, %s)) as r\n",
" FROM gaiadr2.gaia_source AS g, external.gaiadr2_geometric_distance AS d\n",
" WHERE g.source_id = d.source_id AND CONTAINS(POINT('ICRS',g.ra, g.dec), \n",
" CIRCLE('ICRS',%15.10f, %15.10f,%15.10f))=1 ORDER BY r ASC''' % \\\n",
" (ra, dec, ra, dec, radius.to(u.degree).value) \n",
" return(query_string)\n"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"4h21m59.4321s 19d32m06.42s\n"
]
}
],
"source": [
"# Get coordinates by resolving the object name: \n",
"object_name = 'T Tauri'\n",
"coords = SkyCoord.from_name(object_name)\n",
"print(coords.ra.to_string(u.hour), coords.dec.to_string(u.deg))"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Launched query: 'SELECT TOP 2000 *, DISTANCE(POINT('ICRS',g.ra, g.dec), \n",
" POINT('ICRS', 65.49763375, 19.53511667)) as r\n",
" FROM gaiadr2.gaia_source AS g, external.gaiadr2_geometric_distance AS d\n",
" WHERE g.source_id = d.source_id AND CONTAINS(POINT('ICRS',g.ra, g.dec), \n",
" CIRCLE('ICRS', 65.4976337500, 19.5351166700, 0.0013888889))=1 ORDER BY r ASC'\n",
"Retrieving sync. results...\n",
"Query finished.\n",
"\n",
"The distance to T Tauri is 144.32 pc, proper motion is 11.4, -14.8 mas/yr.\n"
]
}
],
"source": [
"query_string = gaia_query(coords.ra.to_value(), coords.dec.to_value())\n",
"job = Gaia.launch_job(query_string, verbose=False)\n",
"a = job.get_results()\n",
"if len(a)==0:\n",
" print(\"No results for star %s.\" % object_name)\n",
"else:\n",
" print(\"\\nThe distance to %s is %0.2f pc, proper motion is %0.1f, %0.1f mas/yr.\" \\\n",
" % (object_name, 1000/a['parallax'][0], a['pmra'], a['pmdec']))"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['solution_id',\n",
" 'designation',\n",
" 'source_id',\n",
" 'random_index',\n",
" 'ref_epoch',\n",
" 'ra',\n",
" 'ra_error',\n",
" 'dec',\n",
" 'dec_error',\n",
" 'parallax',\n",
" 'parallax_error',\n",
" 'parallax_over_error',\n",
" 'pmra',\n",
" 'pmra_error',\n",
" 'pmdec',\n",
" 'pmdec_error',\n",
" 'ra_dec_corr',\n",
" 'ra_parallax_corr',\n",
" 'ra_pmra_corr',\n",
" 'ra_pmdec_corr',\n",
" 'dec_parallax_corr',\n",
" 'dec_pmra_corr',\n",
" 'dec_pmdec_corr',\n",
" 'parallax_pmra_corr',\n",
" 'parallax_pmdec_corr',\n",
" 'pmra_pmdec_corr',\n",
" 'astrometric_n_obs_al',\n",
" 'astrometric_n_obs_ac',\n",
" 'astrometric_n_good_obs_al',\n",
" 'astrometric_n_bad_obs_al',\n",
" 'astrometric_gof_al',\n",
" 'astrometric_chi2_al',\n",
" 'astrometric_excess_noise',\n",
" 'astrometric_excess_noise_sig',\n",
" 'astrometric_params_solved',\n",
" 'astrometric_primary_flag',\n",
" 'astrometric_weight_al',\n",
" 'astrometric_pseudo_colour',\n",
" 'astrometric_pseudo_colour_error',\n",
" 'mean_varpi_factor_al',\n",
" 'astrometric_matched_observations',\n",
" 'visibility_periods_used',\n",
" 'astrometric_sigma5d_max',\n",
" 'frame_rotator_object_type',\n",
" 'matched_observations',\n",
" 'duplicated_source',\n",
" 'phot_g_n_obs',\n",
" 'phot_g_mean_flux',\n",
" 'phot_g_mean_flux_error',\n",
" 'phot_g_mean_flux_over_error',\n",
" 'phot_g_mean_mag',\n",
" 'phot_bp_n_obs',\n",
" 'phot_bp_mean_flux',\n",
" 'phot_bp_mean_flux_error',\n",
" 'phot_bp_mean_flux_over_error',\n",
" 'phot_bp_mean_mag',\n",
" 'phot_rp_n_obs',\n",
" 'phot_rp_mean_flux',\n",
" 'phot_rp_mean_flux_error',\n",
" 'phot_rp_mean_flux_over_error',\n",
" 'phot_rp_mean_mag',\n",
" 'phot_bp_rp_excess_factor',\n",
" 'phot_proc_mode',\n",
" 'bp_rp',\n",
" 'bp_g',\n",
" 'g_rp',\n",
" 'radial_velocity',\n",
" 'radial_velocity_error',\n",
" 'rv_nb_transits',\n",
" 'rv_template_teff',\n",
" 'rv_template_logg',\n",
" 'rv_template_fe_h',\n",
" 'phot_variable_flag',\n",
" 'l',\n",
" 'b',\n",
" 'ecl_lon',\n",
" 'ecl_lat',\n",
" 'priam_flags',\n",
" 'teff_val',\n",
" 'teff_percentile_lower',\n",
" 'teff_percentile_upper',\n",
" 'a_g_val',\n",
" 'a_g_percentile_lower',\n",
" 'a_g_percentile_upper',\n",
" 'e_bp_min_rp_val',\n",
" 'e_bp_min_rp_percentile_lower',\n",
" 'e_bp_min_rp_percentile_upper',\n",
" 'flame_flags',\n",
" 'radius_val',\n",
" 'radius_percentile_lower',\n",
" 'radius_percentile_upper',\n",
" 'lum_val',\n",
" 'lum_percentile_lower',\n",
" 'lum_percentile_upper',\n",
" 'datalink_url',\n",
" 'epoch_photometry_url',\n",
" 'source_id_2',\n",
" 'r_est',\n",
" 'r_lo',\n",
" 'r_hi',\n",
" 'r_len',\n",
" 'result_flag',\n",
" 'modality_flag',\n",
" 'r']"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# The output array a has all the fields from Gaia DR2, plus those\n",
"# from Bailer-Jones et al. and the distance from center r that\n",
"# specified it should calculate: \n",
"a.colnames"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.3"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment