\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": null, "metadata": { "tags": [ "hide_input" ] }, "outputs": [], "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": null, "metadata": { "tags": [ "hide-input" ] }, "outputs": [], "source": [ "#display the results\n", "print('parallax of row 100: {:.3f}'.format(tab['parallax'][100]))\n", "tab.show_in_notebook(display_length=10)\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": null, "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 " ] }, { "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", "

\n", "However, these would not account for the fact that in a cone search, such as we have done above, the volume element grows as $d^2$, so that could be added to the prior.\n", "

\n", "In addition, one might want to incorporate the knowledge that, looking perpendicular to the Galactic plane, the density of stars is known to drop off, roughly exponentially, let's say with a scale height of 0.5 kpc.\n", "

\n", " Bailer-Jones et al 2018 adopt a prior\n", "$$P(d) = d^2 \\exp(-d/L)$$\n", "to account for these considerations, where $L$ is a scale length that depends on Galactic longitude and latitude. Let's adopt this prior, with $L=0.5$ at the Galactic pole. Note that this is a proper prior, since the exponential drops off faster than the $d^2$ term.\n", "

\n", "Write a function to return the value of this prior:\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def prior(distance,scalelength) :\n", " \"\"\" Return distance prior based on Bailer-Jones formulation\n", " \"\"\"\n", " return # return value of prior" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "OK, now let's construct the posterior PDF. We'll input an array of distances at which to sample the PDF. Write a function to return the posterior PDF for a single star, given the measured parallax and parallax uncertainty:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\n", "def posterior(p,perr,dist,l=0.5):\n", " \"\"\" Calculate posterior PDF P(d|p,perr) for 0

\n", "