{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "SELECT selectList \n", "FROM fromClause \n", "[WHERE conditions] \n", "\n", "\n", "Basically, the selectList gives the quantities you want to download, the fromClause gives the table where the quantities are located, and the optional WHERE clause gives some conditions for which objects you want to download.\n", "\n", "\n", "ADQL is an extension of SQL, in particular, defines some useful geometric functions, e.g. CONTAINS() can be used to select objects within some distance of some point in the sky, e.g.\n", "
\n", "WHERE 1=CONTAINS(POINT('ICRS', ra, dec), CIRCLE('ICRS', tablera, tabledec, radius)\n", "\n", "would select points where the database coordinate variables (tablera and tabledec, but you will need to use the appropriate column names) fall within some distance of the ra/dec specified in the query.\n", "\n", "For a little more information on ADQL/SQL see https://www.cosmos.esa.int/documents/915837/915858/ADQL_handson_slides.pdf" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " Let's write a query to get the parallaxes of all stars within 0.5 degree of the north Galactic pole. Look at the schema to get the column names for ra, dec, and parallax, and try to complete the following SQL query. Give it a quick try, then move on to the next cell to see a valid query:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The following cell gives and implements the query" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "tags": [ "hide_input" ] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "SELECT ra, dec, parallax, parallax_error\n", " FROM gaiaedr3.gaia_source\n", " WHERE 1=CONTAINS(\n", " POINT('ICRS', 192.859478, 27.128252),\n", " CIRCLE('ICRS',ra, dec, 0.500000))\n", "INFO: Query finished. [astroquery.utils.tap.core]\n", "Returned 2052 objects\n" ] } ], "source": [ "from astroquery.gaia import Gaia\n", "from astropy.coordinates import SkyCoord\n", "import astropy.units as u\n", "\n", "# get RA/DEC at the Galactic pole, which has l=0, b=90\n", "coord = SkyCoord(0, 90, unit=(u.degree, u.degree), frame='galactic')\n", "ra=coord.icrs.ra.value\n", "dec=coord.icrs.dec.value\n", "\n", "# search radius\n", "rad=0.5\n", "\n", "#form the SQL query\n", "query=\"\"\"SELECT ra, dec, parallax, parallax_error\n", " FROM gaiaedr3.gaia_source\n", " WHERE 1=CONTAINS(\n", " POINT('ICRS', {:f}, {:f}),\n", " CIRCLE('ICRS',ra, dec, {:f}))\"\"\".format(ra,dec,rad)\n", "print(query)\n", "\n", "# run the query and get the results\n", "job = Gaia.launch_job_async(query)\n", "tab = job.get_results()\n", "print('Returned {:d} objects'.format(len(tab)))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The results are returned in an astropy table. You can display them in a convenient notebook interface. Individual columns can be referenced using tab['columnname'], e.g. tab['parallax']. If you are more familiar/comfortable with pandas data frame, tables can easily be converted to pandas." ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "tags": [ "hide-input" ] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "parallax of row 100: 0.294\n" ] } ], "source": [ "#display the results\n", "print('parallax of row 100: {:.3f}'.format(tab['parallax'][100]))\n", "tab.show_in_notebook(display_length=10)\n", "tab.write('gaia.fits')\n", "\n", "#if you prefer to convert to pandas, use the following\n", "#ptab = tab.to_pandas()\n", "#print(ptab['parallax'][100])\n", "#print(ptab)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's solve for Bayesian posterior probability distribution for distance to our stars. \n", "$$P(d|p) = {P(p|d)P(d)\\over P(p)}$$\n", "We will avoid any calculation of $P(p)$ and just normalize our posterior PDF.\n", "First, we need to formulate an expression for the likelihood, $P(p|d)$. For GAIA, the parallax uncertainties are expected to be distributed according to a normal distribution. For a gaussian uncertainty in measurement parallax, we then have:\n", "$$P(p|d) = {1\\over \\sigma_p\\sqrt{2\\pi}} \\exp{-0.5 (p-{1\\over d})^2 \\over \\sigma_p^2}$$\n", "Write a function that will calculate this likelihood:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "def likelihood(parallax,parallax_err,distance) :\n", " \"\"\" Return likelihood of a parallax, given a distance and parallax uncertainty\n", " assume parallax in mas, and distance in kpc\n", " \"\"\"\n", " # return value of likelihood\n", " return 1/np.sqrt(2*np.pi)/parallax_err*np.exp(-0.5*(parallax-1./distance)**2/parallax_err**2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We have to decide what to use for a prior. One could use just a positivity constraint, which would lead to an improper prior (since the integral to infinity is infinite), or one could impose a maximum distance, perhaps motivated by the faint limit of gaia (around g=20) and the absolute magnitude of the faintest stars. \n", "