Update documentation

This commit is contained in:
Allen Downey
2021-03-15 11:13:54 -04:00
parent c85b5e63aa
commit 8c0977b145
111 changed files with 3163 additions and 13657 deletions

View File

@@ -5,7 +5,7 @@
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Queries &#8212; Astronomical Data in Python</title>
<title>1. Queries &#8212; Astronomical Data in Python</title>
<link rel="stylesheet" href="_static/css/index.d431a4ee1c1efae0e38bdfebc22debff.css">
@@ -57,7 +57,7 @@
<script async="async" src="_static/sphinx-thebe.js"></script>
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="next" title="Coordinates and units" href="02_coords.html" />
<link rel="next" title="2. Coordinates and Units" href="02_coords.html" />
<link rel="prev" title="Astronomical Data in Python" href="README.html" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
@@ -99,22 +99,22 @@
<ul class="current nav sidenav_l1">
<li class="toctree-l1 current active">
<a class="current reference internal" href="#">
Queries
1. Queries
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="02_coords.html">
Coordinates and units
2. Coordinates and Units
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="03_motion.html">
Proper Motion
3. Proper Motion
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="04_select.html">
Transformation and Selection
4. Transformation and Selection
</a>
</li>
<li class="toctree-l1">
@@ -311,11 +311,6 @@
</li>
</ul>
</li>
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#cleaning-up">
Cleaning up
</a>
</li>
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#formatting-queries">
Formatting queries
@@ -350,7 +345,9 @@
<div>
<div class="section" id="queries">
<h1>Queries<a class="headerlink" href="#queries" title="Permalink to this headline"></a></h1>
<h1>1. Queries<a class="headerlink" href="#queries" title="Permalink to this headline"></a></h1>
<p>This is the first in a series of lessons about working with astronomical data.</p>
<p>As a running example, we will replicate parts of the analysis in a recent paper, “<a class="reference external" href="https://arxiv.org/abs/1805.00425">Off the beaten path: Gaia reveals GD-1 stars outside of the main stream</a>” by Adrian Price-Whelan and Ana Bonaca.</p>
<div class="section" id="outline">
<h2>Outline<a class="headerlink" href="#outline" title="Permalink to this headline"></a></h2>
<p>This lesson demonstrates the steps for selecting and downloading data from the Gaia Database:</p>
@@ -378,19 +375,19 @@ But you might find it easier to learn from <a class="reference external" href="h
<li><p>“Jupyter Lab” is a newer environment with more features.</p></li>
</ul>
<p>For these lessons, you can use either one.</p>
<p>If you are too impatient for the tutorials, heres are the most important things to know:</p>
<p>If you are too impatient for the tutorials, here are the most important things to know:</p>
<ol class="simple">
<li><p>Notebooks are made up of code cells and text cells (and a few other less common kinds). Code cells contain code; text cells, like this one, contain explanatory text written in <a class="reference external" href="https://www.markdownguide.org/">Markdown</a>.</p></li>
<li><p>To run a code cell, click the cell to select it and press Shift-Enter. The output of the code should appear below the cell.</p></li>
<li><p>In general, notebooks only run correctly if you run every code cell in order from top to bottom. If you run cells out of order, you are likely to get errors.</p></li>
<li><p>You can modify existing cells, but then you have to run them again to see the effect.</p></li>
<li><p>You can add new cells, but again, you might have to be careful about the order you run them in.</p></li>
<li><p>You can add new cells, but again, you have to be careful about the order you run them in.</p></li>
<li><p>If you have added or modified cells and the behavior of the notebook seems strange, you can restart the “kernel”, which clears all of the variables and functions you have defined, and run the cells again from the beginning.</p></li>
</ol>
<ul class="simple">
<li><p>If you are using Jupyter notebook, open the Kernel menu and select “Restart and Run All”.</p></li>
<li><p>In Jupyter Lab</p></li>
<li><p>In Colab, open the Runtime menu and select “Restart and run all”</p></li>
<li><p>If you are using Jupyter notebook, open the <code class="docutils literal notranslate"><span class="pre">Kernel</span></code> menu and select “Restart and Run All”.</p></li>
<li><p>In Jupyter Lab, open the <code class="docutils literal notranslate"><span class="pre">Kernel</span></code> menu and select “Restart Kernel and Run All Cells”</p></li>
<li><p>In Colab, open the <code class="docutils literal notranslate"><span class="pre">Runtime</span></code> menu and select “Restart and run all”</p></li>
</ul>
<p>Before you go on, you might want to explore the other menus and the toolbar to see what else you can do.</p>
</div>
@@ -420,7 +417,7 @@ Created TAP+ (v1.2.1) - Connection:
</div>
</div>
</div>
<p>Running this import statement has the effect of creating a <a class="reference external" href="http://www.ivoa.net/documents/TAP/">TAP+</a> connection; TAP stands for “Table Access Protocol”, which is a network protocol for sending queries to the database and getting back the results.</p>
<p>This import statement creates a <a class="reference external" href="http://www.ivoa.net/documents/TAP/">TAP+</a> connection; TAP stands for “Table Access Protocol”, which is a network protocol for sending queries to the database and getting back the results.</p>
</div>
<div class="section" id="databases-and-tables">
<h2>Databases and Tables<a class="headerlink" href="#databases-and-tables" title="Permalink to this headline"></a></h2>
@@ -444,6 +441,7 @@ INFO: Done. [astroquery.utils.tap.core]
</div>
</div>
</div>
<p>The following <code class="docutils literal notranslate"><span class="pre">for</span></code> loop prints the names of the tables.</p>
<div class="cell tag_hide-output tag_truncate-output docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="k">for</span> <span class="n">table</span> <span class="ow">in</span> <span class="n">tables</span><span class="p">:</span>
@@ -454,6 +452,7 @@ INFO: Done. [astroquery.utils.tap.core]
<div class="cell_output docutils container">
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>external.apassdr9
external.gaiadr2_geometric_distance
external.gaiaedr3_distance
external.galex_ais
external.ravedr5_com
external.ravedr5_dr5
@@ -482,17 +481,31 @@ gaiaedr3.agn_cross_id
gaiaedr3.commanded_scan_law
gaiaedr3.dr2_neighbourhood
gaiaedr3.frame_rotator_source
gaiaedr3.allwise_best_neighbour
gaiaedr3.allwise_neighbourhood
gaiaedr3.apassdr9_best_neighbour
gaiaedr3.apassdr9_join
gaiaedr3.apassdr9_neighbourhood
gaiaedr3.gsc23_best_neighbour
gaiaedr3.gsc23_join
gaiaedr3.gsc23_neighbourhood
gaiaedr3.hipparcos2_best_neighbour
gaiaedr3.hipparcos2_neighbourhood
gaiaedr3.panstarrs1_best_neighbour
gaiaedr3.panstarrs1_join
gaiaedr3.panstarrs1_neighbourhood
gaiaedr3.ravedr5_best_neighbour
gaiaedr3.ravedr5_join
gaiaedr3.ravedr5_neighbourhood
gaiaedr3.sdssdr13_best_neighbour
gaiaedr3.sdssdr13_join
gaiaedr3.sdssdr13_neighbourhood
gaiaedr3.skymapperdr2_best_neighbour
gaiaedr3.skymapperdr2_join
gaiaedr3.skymapperdr2_neighbourhood
gaiaedr3.tmass_psc_xsc_best_neighbour
gaiaedr3.tmass_psc_xsc_join
gaiaedr3.tmass_psc_xsc_neighbourhood
gaiaedr3.tycho2tdsc_merge_best_neighbour
gaiaedr3.tycho2tdsc_merge_neighbourhood
gaiaedr3.urat1_best_neighbour
@@ -595,7 +608,7 @@ Parsing table &#39;gaiadr2.gaia_source&#39;...
Done.
</pre></div>
</div>
<div class="output text_plain highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>&lt;astroquery.utils.tap.model.taptable.TapTableMeta at 0x7f2e23f089d0&gt;
<div class="output text_plain highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>&lt;astroquery.utils.tap.model.taptable.TapTableMeta at 0x7f50edd2aeb0&gt;
</pre></div>
</div>
</div>
@@ -732,7 +745,7 @@ epoch_photometry_url
</div>
</div>
</div>
<p>You can probably guess what many of these columns are by looking at the names, but you should resist the temptation to guess.
<p>You can probably infer what many of these columns are by looking at the names, but you should resist the temptation to guess.
To find out what the columns mean, <a class="reference external" href="https://gea.esac.esa.int/archive/documentation/GDR2/Gaia_archive/chap_datamodel/sec_dm_main_tables/ssec_dm_gaia_source.html">read the documentation</a>.</p>
<p>If you want to know what can go wrong when you dont read the documentation, <a class="reference external" href="https://www.vox.com/future-perfect/2019/6/4/18650969/married-women-miserable-fake-paul-dolan-happiness">you might like this article</a>.</p>
<div class="section" id="exercise">
@@ -855,15 +868,16 @@ quality_flag
</div>
<div class="section" id="writing-queries">
<h2>Writing queries<a class="headerlink" href="#writing-queries" title="Permalink to this headline"></a></h2>
<p>By now you might be wondering how we download the actual data. With tables this big, you generally dont. Instead, you use queries to select only the data you want.</p>
<p>By now you might be wondering how we download these tables. With tables this big, you generally dont. Instead, you use queries to select only the data you want.</p>
<p>A query is a string written in a query language like SQL; for the Gaia database, the query language is a dialect of SQL called ADQL.</p>
<p>Heres an example of an ADQL query.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">query1</span> <span class="o">=</span> <span class="s2">&quot;&quot;&quot;SELECT </span>
<span class="s2">TOP 10</span>
<span class="s2">source_id, ref_epoch, ra, dec, parallax </span>
<span class="s2">FROM gaiadr2.gaia_source&quot;&quot;&quot;</span>
<span class="s2">source_id, ra, dec, parallax </span>
<span class="s2">FROM gaiadr2.gaia_source</span>
<span class="s2">&quot;&quot;&quot;</span>
</pre></div>
</div>
</div>
@@ -877,25 +891,26 @@ quality_flag
</ul>
<p>The third line is a list of column names, indicating which columns we want.</p>
<p>In this example, the keywords are capitalized and the column names are lowercase. This is a common style, but it is not required. ADQL and SQL are not case-sensitive.</p>
<p>Also, the query is broken into multiple lines to make it more readable. This is a common style, but not required. Line breaks dont affect the behavior of the query.</p>
<p>To run this query, we use the <code class="docutils literal notranslate"><span class="pre">Gaia</span></code> object, which represents our connection to the Gaia database, and invoke <code class="docutils literal notranslate"><span class="pre">launch_job</span></code>:</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">job1</span> <span class="o">=</span> <span class="n">Gaia</span><span class="o">.</span><span class="n">launch_job</span><span class="p">(</span><span class="n">query1</span><span class="p">)</span>
<span class="n">job1</span>
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">job</span> <span class="o">=</span> <span class="n">Gaia</span><span class="o">.</span><span class="n">launch_job</span><span class="p">(</span><span class="n">query1</span><span class="p">)</span>
<span class="n">job</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<div class="output text_plain highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>&lt;astroquery.utils.tap.model.job.Job at 0x7f2e23f2afa0&gt;
<div class="output text_plain highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>&lt;astroquery.utils.tap.model.job.Job at 0x7f50edd2adc0&gt;
</pre></div>
</div>
</div>
</div>
<p>The result is an object that represents the job running on a Gaia server.</p>
<p>If you print it, it displays metadata for the forthcoming table.</p>
<p>If you print it, it displays metadata for the forthcoming results.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="nb">print</span><span class="p">(</span><span class="n">job1</span><span class="p">)</span>
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="nb">print</span><span class="p">(</span><span class="n">job</span><span class="p">)</span>
</pre></div>
</div>
</div>
@@ -904,14 +919,13 @@ quality_flag
name dtype unit description n_bad
--------- ------- ---- ------------------------------------------------------------------ -----
source_id int64 Unique source identifier (unique within a particular Data Release) 0
ref_epoch float64 yr Reference epoch 0
ra float64 deg Right ascension 0
dec float64 deg Declination 0
parallax float64 mas Parallax 2
Jobid: None
Phase: COMPLETED
Owner: None
Output file: sync_20201229114647.xml.gz
Output file: sync_20210315090602.xml.gz
Results: None
</pre></div>
</div>
@@ -921,8 +935,8 @@ Results: None
<p>However, <code class="docutils literal notranslate"><span class="pre">Phase:</span> <span class="pre">COMPLETED</span></code> indicates that the job is complete, so we can get the results like this:</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">results1</span> <span class="o">=</span> <span class="n">job1</span><span class="o">.</span><span class="n">get_results</span><span class="p">()</span>
<span class="nb">type</span><span class="p">(</span><span class="n">results1</span><span class="p">)</span>
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">results</span> <span class="o">=</span> <span class="n">job</span><span class="o">.</span><span class="n">get_results</span><span class="p">()</span>
<span class="nb">type</span><span class="p">(</span><span class="n">results</span><span class="p">)</span>
</pre></div>
</div>
</div>
@@ -932,8 +946,9 @@ Results: None
</div>
</div>
</div>
<p>The <code class="docutils literal notranslate"><span class="pre">type</span></code> function indicates that the result is an <a class="reference external" href="https://docs.astropy.org/en/stable/table/">Astropy Table</a>.</p>
<p><strong>Optional detail:</strong> Why is <code class="docutils literal notranslate"><span class="pre">table</span></code> repeated three times? The first is the name of the module, the second is the name of the submodule, and the third is the name of the class. Most of the time we only care about the last one. Its like the Linnean name for gorilla, which is <em>Gorilla gorilla gorilla</em>.</p>
<p>The result is an <a class="reference external" href="https://docs.astropy.org/en/stable/table/">Astropy Table</a>, which is similar to a table in an SQL database except:</p>
<p>An Astropy <code class="docutils literal notranslate"><span class="pre">Table</span></code> is similar to a table in an SQL database except:</p>
<ul class="simple">
<li><p>SQL databases are stored on disk drives, so they are persistent; that is, they “survive” even if you turn off the computer. An Astropy <code class="docutils literal notranslate"><span class="pre">Table</span></code> is stored in memory; it disappears when you turn off the computer (or shut down this Jupyter notebook).</p></li>
<li><p>SQL databases are designed to process queries. An Astropy <code class="docutils literal notranslate"><span class="pre">Table</span></code> can perform some query-like operations, like selecting columns and rows. But these operations use Python syntax, not SQL.</p></li>
@@ -941,37 +956,53 @@ Results: None
<p>Jupyter knows how to display the contents of a <code class="docutils literal notranslate"><span class="pre">Table</span></code>.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">results1</span>
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">results</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<div class="output text_html"><i>Table length=10</i>
<table id="table139836148327136" class="table-striped table-bordered table-condensed">
<thead><tr><th>source_id</th><th>ref_epoch</th><th>ra</th><th>dec</th><th>parallax</th></tr></thead>
<thead><tr><th></th><th>yr</th><th>deg</th><th>deg</th><th>mas</th></tr></thead>
<thead><tr><th>int64</th><th>float64</th><th>float64</th><th>float64</th><th>float64</th></tr></thead>
<tr><td>6758509757594141440</td><td>2015.5</td><td>290.4899010727352</td><td>-30.34317218420783</td><td>0.48023816159705535</td></tr>
<tr><td>6758508692437976960</td><td>2015.5</td><td>290.64176055082714</td><td>-30.28972013142225</td><td>2.2625971293368154</td></tr>
<tr><td>6758527036250849280</td><td>2015.5</td><td>290.35703708993327</td><td>-30.193657826181596</td><td>-0.2763960334229464</td></tr>
<tr><td>6758564458298242944</td><td>2015.5</td><td>290.77376294142846</td><td>-29.765368439225238</td><td>0.5907906528352993</td></tr>
<tr><td>6758558612842155776</td><td>2015.5</td><td>290.67550630386245</td><td>-29.92113396078169</td><td>0.2858563565989917</td></tr>
<tr><td>6758556379459921920</td><td>2015.5</td><td>290.5454006212404</td><td>-29.900709054816964</td><td>-1.0012355835832834</td></tr>
<tr><td>6758537520260385152</td><td>2015.5</td><td>290.7341683169994</td><td>-30.158181298418626</td><td>--</td></tr>
<tr><td>6758506355980010624</td><td>2015.5</td><td>290.586950869878</td><td>-30.37137642167569</td><td>0.3769870991981157</td></tr>
<tr><td>6758561606433968128</td><td>2015.5</td><td>290.82697136098506</td><td>-29.75247697212053</td><td>--</td></tr>
<tr><td>6758564183414408448</td><td>2015.5</td><td>290.7133096669958</td><td>-29.781743673679607</td><td>0.9376387942856869</td></tr>
<table id="table139985564118224" class="table-striped table-bordered table-condensed">
<thead><tr><th>source_id</th><th>ra</th><th>dec</th><th>parallax</th></tr></thead>
<thead><tr><th></th><th>deg</th><th>deg</th><th>mas</th></tr></thead>
<thead><tr><th>int64</th><th>float64</th><th>float64</th><th>float64</th></tr></thead>
<tr><td>5887983246081387776</td><td>227.978818386372</td><td>-53.64996962450103</td><td>1.0493172163332998</td></tr>
<tr><td>5887971250213117952</td><td>228.32280834041364</td><td>-53.66270726203726</td><td>0.29455652682279093</td></tr>
<tr><td>5887991866047288704</td><td>228.1582047014091</td><td>-53.454724911639794</td><td>-0.5789179941669236</td></tr>
<tr><td>5887968673232040832</td><td>228.07420888099884</td><td>-53.8064612895961</td><td>0.41030970779603076</td></tr>
<tr><td>5887979844465854720</td><td>228.42547805195946</td><td>-53.48882284470035</td><td>-0.23379683441525864</td></tr>
<tr><td>5887978607515442688</td><td>228.23831627636855</td><td>-53.56328249482688</td><td>-0.9252161956789068</td></tr>
<tr><td>5887978298278520704</td><td>228.26015640396173</td><td>-53.607284412896476</td><td>--</td></tr>
<tr><td>5887995581231772928</td><td>228.12871598211902</td><td>-53.373625663608316</td><td>-0.3325818206439385</td></tr>
<tr><td>5887982043490374016</td><td>227.985260087594</td><td>-53.683444499055575</td><td>0.02878111976456593</td></tr>
<tr><td>5887982971205433856</td><td>227.89884570686218</td><td>-53.67430215342567</td><td>--</td></tr>
</table></div></div>
</div>
<p>Each column has a name, units, and a data type.</p>
<p>For example, the units of <code class="docutils literal notranslate"><span class="pre">ra</span></code> and <code class="docutils literal notranslate"><span class="pre">dec</span></code> are degrees, and their data type is <code class="docutils literal notranslate"><span class="pre">float64</span></code>, which is a 64-bit floating-point number, used to store measurements with a fraction part.</p>
<p>For example, the units of <code class="docutils literal notranslate"><span class="pre">ra</span></code> and <code class="docutils literal notranslate"><span class="pre">dec</span></code> are degrees, and their data type is <code class="docutils literal notranslate"><span class="pre">float64</span></code>, which is a 64-bit <a class="reference external" href="https://en.wikipedia.org/wiki/Floating-point_arithmetic">floating-point number</a>, used to store measurements with a fraction part.</p>
<p>This information comes from the Gaia database, and has been stored in the Astropy <code class="docutils literal notranslate"><span class="pre">Table</span></code> by Astroquery.</p>
<div class="section" id="id1">
<h3>Exercise<a class="headerlink" href="#id1" title="Permalink to this headline"></a></h3>
<p>Read <a class="reference external" href="https://gea.esac.esa.int/archive/documentation/GDR2/Gaia_archive/chap_datamodel/sec_dm_main_tables/ssec_dm_gaia_source.html">the documentation of this table</a> and choose a column that looks interesting to you. Add the column name to the query and run it again. What are the units of the column you selected? What is its data type?</p>
<p>Read <a class="reference external" href="https://gea.esac.esa.int/archive/documentation/GDR2/Gaia_archive/chap_datamodel/sec_dm_main_tables/ssec_dm_gaia_source.html">the documentation</a> of this table and choose a column that looks interesting to you. Add the column name to the query and run it again. What are the units of the column you selected? What is its data type?</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="c1"># Solution</span>
<span class="c1"># Let&#39;s add</span>
<span class="c1">#</span>
<span class="c1"># radial_velocity : Radial velocity (double, Velocity[km/s] )</span>
<span class="c1">#</span>
<span class="c1"># Spectroscopic radial velocity in the solar barycentric </span>
<span class="c1"># reference frame.</span>
<span class="c1">#</span>
<span class="c1"># The radial velocity provided is the median value of the </span>
<span class="c1"># radial velocity measurements at all epochs.</span>
<span class="n">query</span> <span class="o">=</span> <span class="s2">&quot;&quot;&quot;SELECT </span>
<span class="s2">TOP 10</span>
<span class="s2">source_id, ra, dec, parallax, radial_velocity</span>
<span class="s2">FROM gaiadr2.gaia_source</span>
<span class="s2">&quot;&quot;&quot;</span>
</pre></div>
</div>
</div>
@@ -981,18 +1012,20 @@ Results: None
<div class="section" id="asynchronous-queries">
<h2>Asynchronous queries<a class="headerlink" href="#asynchronous-queries" title="Permalink to this headline"></a></h2>
<p><code class="docutils literal notranslate"><span class="pre">launch_job</span></code> asks the server to run the job “synchronously”, which normally means it runs immediately. But synchronous jobs are limited to 2000 rows. For queries that return more rows, you should run “asynchronously”, which mean they might take longer to get started.</p>
<p>If you are not sure how many rows a query will return, you can use the SQL command <code class="docutils literal notranslate"><span class="pre">COUNT</span></code> to find out how many rows are in the result without actually returning them. Well see an example of this later.</p>
<p>The results of an asynchronous query are stored in a file on the server, so you can start a query and come back later to get the results.</p>
<p>For anonymous users, files are kept for three days.</p>
<p>As an example, lets try a query thats similar to <code class="docutils literal notranslate"><span class="pre">query1</span></code>, with two changes:</p>
<p>If you are not sure how many rows a query will return, you can use the SQL command <code class="docutils literal notranslate"><span class="pre">COUNT</span></code> to find out how many rows are in the result without actually returning them. Well see an example in the next lesson.</p>
<p>The results of an asynchronous query are stored in a file on the server, so you can start a query and come back later to get the results.
For anonymous users, files are kept for three days.</p>
<p>As an example, lets try a query thats similar to <code class="docutils literal notranslate"><span class="pre">query1</span></code>, with these changes:</p>
<ul class="simple">
<li><p>It selects the first 3000 rows, so it is bigger than we should run synchronously.</p></li>
<li><p>It selects two additional columns, <code class="docutils literal notranslate"><span class="pre">pmra</span></code> and <code class="docutils literal notranslate"><span class="pre">pmdec</span></code>, which are proper motions along the axes of <code class="docutils literal notranslate"><span class="pre">ra</span></code> and <code class="docutils literal notranslate"><span class="pre">dec</span></code>.</p></li>
<li><p>It uses a new keyword, <code class="docutils literal notranslate"><span class="pre">WHERE</span></code>.</p></li>
</ul>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">query2</span> <span class="o">=</span> <span class="s2">&quot;&quot;&quot;SELECT TOP 3000</span>
<span class="s2">source_id, ref_epoch, ra, dec, parallax</span>
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">query2</span> <span class="o">=</span> <span class="s2">&quot;&quot;&quot;SELECT </span>
<span class="s2">TOP 3000</span>
<span class="s2">source_id, ra, dec, pmra, pmdec, parallax</span>
<span class="s2">FROM gaiadr2.gaia_source</span>
<span class="s2">WHERE parallax &lt; 1</span>
<span class="s2">&quot;&quot;&quot;</span>
@@ -1000,31 +1033,22 @@ Results: None
</div>
</div>
</div>
<p>A <code class="docutils literal notranslate"><span class="pre">WHERE</span></code> clause indicates which rows we want; in this case, the query selects only rows “where” <code class="docutils literal notranslate"><span class="pre">parallax</span></code> is less than 1. This has the effect of selecting stars with relatively low parallax, which are farther away. Well use this clause to exclude nearby stars that are unlikely to be part of GD-1.</p>
<p><code class="docutils literal notranslate"><span class="pre">WHERE</span></code> is one of the most common clauses in ADQL/SQL, and one of the most useful, because it allows us to select only the rows we need from the database.</p>
<p>A <code class="docutils literal notranslate"><span class="pre">WHERE</span></code> clause indicates which rows we want; in this case, the query selects only rows “where” <code class="docutils literal notranslate"><span class="pre">parallax</span></code> is less than 1. This has the effect of selecting stars with relatively low parallax, which are farther away.
Well use this clause to exclude nearby stars that are unlikely to be part of GD-1.</p>
<p><code class="docutils literal notranslate"><span class="pre">WHERE</span></code> is one of the most common clauses in ADQL/SQL, and one of the most useful, because it allows us to download only the rows we need from the database.</p>
<p>We use <code class="docutils literal notranslate"><span class="pre">launch_job_async</span></code> to submit an asynchronous query.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">job2</span> <span class="o">=</span> <span class="n">Gaia</span><span class="o">.</span><span class="n">launch_job_async</span><span class="p">(</span><span class="n">query2</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">job2</span><span class="p">)</span>
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">job</span> <span class="o">=</span> <span class="n">Gaia</span><span class="o">.</span><span class="n">launch_job_async</span><span class="p">(</span><span class="n">query</span><span class="p">)</span>
<span class="n">job</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>INFO: Query finished. [astroquery.utils.tap.core]
&lt;Table length=3000&gt;
name dtype unit description
--------- ------- ---- ------------------------------------------------------------------
source_id int64 Unique source identifier (unique within a particular Data Release)
ref_epoch float64 yr Reference epoch
ra float64 deg Right ascension
dec float64 deg Declination
parallax float64 mas Parallax
Jobid: 1609260407863O
Phase: COMPLETED
Owner: None
Output file: async_20201229114648.vot
Results: None
</pre></div>
</div>
<div class="output text_plain highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>&lt;astroquery.utils.tap.model.job.Job at 0x7f50edd40f40&gt;
</pre></div>
</div>
</div>
@@ -1032,44 +1056,34 @@ Results: None
<p>And here are the results.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">results2</span> <span class="o">=</span> <span class="n">job2</span><span class="o">.</span><span class="n">get_results</span><span class="p">()</span>
<span class="n">results2</span>
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">results</span> <span class="o">=</span> <span class="n">job</span><span class="o">.</span><span class="n">get_results</span><span class="p">()</span>
<span class="n">results</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<div class="output text_html"><i>Table length=3000</i>
<table id="table139836145366016" class="table-striped table-bordered table-condensed">
<thead><tr><th>source_id</th><th>ref_epoch</th><th>ra</th><th>dec</th><th>parallax</th></tr></thead>
<thead><tr><th></th><th>yr</th><th>deg</th><th>deg</th><th>mas</th></tr></thead>
<div class="output text_html"><i>Table length=10</i>
<table id="table139986226873968" class="table-striped table-bordered table-condensed">
<thead><tr><th>source_id</th><th>ra</th><th>dec</th><th>parallax</th><th>radial_velocity</th></tr></thead>
<thead><tr><th></th><th>deg</th><th>deg</th><th>mas</th><th>km / s</th></tr></thead>
<thead><tr><th>int64</th><th>float64</th><th>float64</th><th>float64</th><th>float64</th></tr></thead>
<tr><td>6758509757594141440</td><td>2015.5</td><td>290.4899010727352</td><td>-30.34317218420783</td><td>0.48023816159705535</td></tr>
<tr><td>6758527036250849280</td><td>2015.5</td><td>290.35703708993327</td><td>-30.193657826181596</td><td>-0.2763960334229464</td></tr>
<tr><td>6758564458298242944</td><td>2015.5</td><td>290.77376294142846</td><td>-29.765368439225238</td><td>0.5907906528352993</td></tr>
<tr><td>6758558612842155776</td><td>2015.5</td><td>290.67550630386245</td><td>-29.92113396078169</td><td>0.2858563565989917</td></tr>
<tr><td>6758556379459921920</td><td>2015.5</td><td>290.5454006212404</td><td>-29.900709054816964</td><td>-1.0012355835832834</td></tr>
<tr><td>6758506355980010624</td><td>2015.5</td><td>290.586950869878</td><td>-30.37137642167569</td><td>0.3769870991981157</td></tr>
<tr><td>6758564183414408448</td><td>2015.5</td><td>290.7133096669958</td><td>-29.781743673679607</td><td>0.9376387942856869</td></tr>
<tr><td>6758529789322653184</td><td>2015.5</td><td>290.35449465190385</td><td>-30.10231481465004</td><td>-1.3305133038319952</td></tr>
<tr><td>6758507485555336064</td><td>2015.5</td><td>290.6745961953205</td><td>-30.35965148061028</td><td>0.057302711920868686</td></tr>
<tr><td>...</td><td>...</td><td>...</td><td>...</td><td>...</td></tr>
<tr><td>4660464894038295424</td><td>2015.5</td><td>79.74509945051724</td><td>-66.49656012737759</td><td>0.1631257821260955</td></tr>
<tr><td>4660463519650480128</td><td>2015.5</td><td>80.06150680482719</td><td>-66.42891601595986</td><td>-0.5822669736733328</td></tr>
<tr><td>4660487674554071040</td><td>2015.5</td><td>80.03536662790846</td><td>-66.33136792885134</td><td>-1.4978065466579966</td></tr>
<tr><td>4660494718300886784</td><td>2015.5</td><td>79.6181997343967</td><td>-66.17472335318212</td><td>0.1488652133861382</td></tr>
<tr><td>4660476473274159360</td><td>2015.5</td><td>79.12508291180023</td><td>-66.35644844757275</td><td>0.10671540523101529</td></tr>
<tr><td>4660471010093940224</td><td>2015.5</td><td>79.1908378165463</td><td>-66.48916840294096</td><td>0.8152150079365807</td></tr>
<tr><td>4660498394807167744</td><td>2015.5</td><td>80.23431532382547</td><td>-66.08688681815092</td><td>-0.14060596426163252</td></tr>
<tr><td>4660498738391722880</td><td>2015.5</td><td>80.20205898946918</td><td>-66.04392313295126</td><td>0.1894642263668475</td></tr>
<tr><td>4660474068091096960</td><td>2015.5</td><td>78.93686107060341</td><td>-66.45795576745896</td><td>0.4665513634059366</td></tr>
<tr><td>4660437303162697728</td><td>2015.5</td><td>80.63872120225027</td><td>-66.46144560233674</td><td>-1.1319163320535979</td></tr>
<tr><td>5895270396817359872</td><td>213.08433715252883</td><td>-56.64104701005694</td><td>2.041947005434917</td><td>--</td></tr>
<tr><td>5895272561481374080</td><td>213.2606587905109</td><td>-56.55044401535715</td><td>0.15693467895110133</td><td>--</td></tr>
<tr><td>5895247410183786368</td><td>213.38479712976664</td><td>-56.97008551185148</td><td>-0.19017525742552605</td><td>--</td></tr>
<tr><td>5895249226912448000</td><td>213.41587389088238</td><td>-56.849596577635786</td><td>--</td><td>--</td></tr>
<tr><td>5895261875598904576</td><td>213.5508930114549</td><td>-56.61037780154348</td><td>-0.29471722363529257</td><td>--</td></tr>
<tr><td>5895258302187834624</td><td>213.87631129557286</td><td>-56.678537259039906</td><td>0.6468437015289753</td><td>--</td></tr>
<tr><td>5895247444506644992</td><td>213.33215109206796</td><td>-56.975347759380995</td><td>0.390215490234287</td><td>--</td></tr>
<tr><td>5895259470417635968</td><td>213.78815034206346</td><td>-56.64585047451594</td><td>0.953377710788918</td><td>--</td></tr>
<tr><td>5895264899260932352</td><td>213.21521027193236</td><td>-56.78420864489118</td><td>--</td><td>--</td></tr>
<tr><td>5895265925746051584</td><td>213.17082359534547</td><td>-56.74540885107754</td><td>0.2986918215101751</td><td>--</td></tr>
</table></div></div>
</div>
<p>You might notice that some values of <code class="docutils literal notranslate"><span class="pre">parallax</span></code> are negative. As <a class="reference external" href="https://www.cosmos.esa.int/web/gaia/archive-tips#negative%20parallax">this FAQ explains</a>, “Negative parallaxes are caused by errors in the observations.” Negative parallaxes have “no physical meaning,” but they can be a “useful diagnostic on the quality of the astrometric solution.”</p>
<p>You might notice that some values of <code class="docutils literal notranslate"><span class="pre">parallax</span></code> are negative. As <a class="reference external" href="https://www.cosmos.esa.int/web/gaia/archive-tips#negative%20parallax">this FAQ explains</a>, “Negative parallaxes are caused by errors in the observations.” They have “no physical meaning,” but they can be a “useful diagnostic on the quality of the astrometric solution.”</p>
<div class="section" id="id2">
<h3>Exercise<a class="headerlink" href="#id2" title="Permalink to this headline"></a></h3>
<p>The clauses in a query have to be in the right order. Go back and change the order of the clauses in <code class="docutils literal notranslate"><span class="pre">query2</span></code> and run it again.</p>
<p>The query should fail, but notice that you dont get much useful debugging information.</p>
<p>The clauses in a query have to be in the right order. Go back and change the order of the clauses in <code class="docutils literal notranslate"><span class="pre">query2</span></code> and run it again.
The modified query should fail, but notice that you dont get much useful debugging information.</p>
<p>For this reason, developing and debugging ADQL queries can be really hard. A few suggestions that might help:</p>
<ul class="simple">
<li><p>Whenever possible, start with a working query, either an example you find online or a query you have used in the past.</p></li>
@@ -1080,6 +1094,15 @@ Results: None
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="c1"># Solution</span>
<span class="c1"># In this example, the WHERE clause is in the wrong place</span>
<span class="n">query</span> <span class="o">=</span> <span class="s2">&quot;&quot;&quot;SELECT </span>
<span class="s2">TOP 3000</span>
<span class="s2">WHERE parallax &lt; 1</span>
<span class="s2">source_id, ref_epoch, ra, dec, parallax</span>
<span class="s2">FROM gaiadr2.gaia_source</span>
<span class="s2">&quot;&quot;&quot;</span>
</pre></div>
</div>
</div>
@@ -1127,14 +1150,14 @@ Be careful to keep your Python out of your ADQL!</p>
<div class="section" id="id3">
<h3>Exercise<a class="headerlink" href="#id3" title="Permalink to this headline"></a></h3>
<p><a class="reference external" href="https://www.w3schools.com/sql/sql_operators.asp">Read about SQL operators here</a> and then modify the previous query to select rows where <code class="docutils literal notranslate"><span class="pre">bp_rp</span></code> is between <code class="docutils literal notranslate"><span class="pre">-0.75</span></code> and <code class="docutils literal notranslate"><span class="pre">2</span></code>.</p>
<p>You can <a class="reference external" href="https://gea.esac.esa.int/archive/documentation/GDR2/Gaia_archive/chap_datamodel/sec_dm_main_tables/ssec_dm_gaia_source.html">read about this variable here</a>.</p>
<div class="cell tag_hide-cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="c1"># Solution</span>
<span class="c1"># Here&#39;s a solution using &gt; and &lt; operators</span>
<span class="n">query</span> <span class="o">=</span> <span class="s2">&quot;&quot;&quot;SELECT TOP 10</span>
<span class="n">query</span> <span class="o">=</span> <span class="s2">&quot;&quot;&quot;SELECT </span>
<span class="s2">TOP 10</span>
<span class="s2">source_id, ref_epoch, ra, dec, parallax</span>
<span class="s2">FROM gaiadr2.gaia_source</span>
<span class="s2">WHERE parallax &lt; 1 </span>
@@ -1143,7 +1166,8 @@ Be careful to keep your Python out of your ADQL!</p>
<span class="c1"># And here&#39;s a solution using the BETWEEN operator</span>
<span class="n">query</span> <span class="o">=</span> <span class="s2">&quot;&quot;&quot;SELECT TOP 10</span>
<span class="n">query</span> <span class="o">=</span> <span class="s2">&quot;&quot;&quot;SELECT </span>
<span class="s2">TOP 10</span>
<span class="s2">source_id, ref_epoch, ra, dec, parallax</span>
<span class="s2">FROM gaiadr2.gaia_source</span>
<span class="s2">WHERE parallax &lt; 1 </span>
@@ -1153,51 +1177,23 @@ Be careful to keep your Python out of your ADQL!</p>
</div>
</div>
</div>
<p><code class="docutils literal notranslate"><span class="pre">bp_rp</span></code> contains BP-RP color, which is the difference between two other columns, <code class="docutils literal notranslate"><span class="pre">phot_bp_mean_mag</span></code> and <code class="docutils literal notranslate"><span class="pre">phot_rp_mean_mag</span></code>.
You can <a class="reference external" href="https://gea.esac.esa.int/archive/documentation/GDR2/Gaia_archive/chap_datamodel/sec_dm_main_tables/ssec_dm_gaia_source.html">read about this variable here</a>.</p>
<p>This <a class="reference external" href="https://sci.esa.int/web/gaia/-/60198-gaia-hertzsprung-russell-diagram">Hertzsprung-Russell diagram</a> shows the BP-RP color and luminosity of stars in the Gaia catalog (Copyright: ESA/Gaia/DPAC, CC BY-SA 3.0 IGO).</p>
<a class="reference internal image-reference" href="https://github.com/AllenDowney/AstronomicalData/raw/main/images/1567214809100-ESA_Gaia_DR2_HRD_Gaia_625.jpg"><img alt="https://github.com/AllenDowney/AstronomicalData/raw/main/images/1567214809100-ESA_Gaia_DR2_HRD_Gaia_625.jpg" src="https://github.com/AllenDowney/AstronomicalData/raw/main/images/1567214809100-ESA_Gaia_DR2_HRD_Gaia_625.jpg" style="width: 300px;" /></a>
<p>Selecting stars with <code class="docutils literal notranslate"><span class="pre">bp-rp</span></code> less than 2 excludes many <a class="reference external" href="https://xkcd.com/2360/">class M dwarf stars</a>, which are low temperature, low luminosity. A star like that at GD-1s distance would be hard to detect, so if it is detected, it it more likely to be in the foreground.</p>
</div>
</div>
<div class="section" id="cleaning-up">
<h2>Cleaning up<a class="headerlink" href="#cleaning-up" title="Permalink to this headline"></a></h2>
<p>Asynchronous jobs have a <code class="docutils literal notranslate"><span class="pre">jobid</span></code>.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">job1</span><span class="o">.</span><span class="n">jobid</span><span class="p">,</span> <span class="n">job2</span><span class="o">.</span><span class="n">jobid</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<div class="output text_plain highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>(None, &#39;1609260407863O&#39;)
</pre></div>
</div>
</div>
</div>
<p>Which you can use to remove the job from the server.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">Gaia</span><span class="o">.</span><span class="n">remove_jobs</span><span class="p">([</span><span class="n">job2</span><span class="o">.</span><span class="n">jobid</span><span class="p">])</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Removed jobs: &#39;[&#39;1609260407863O&#39;]&#39;.
</pre></div>
</div>
</div>
</div>
<p>If you dont remove it job from the server, it will be removed eventually, so dont feel too bad if you dont clean up after yourself.</p>
</div>
<div class="section" id="formatting-queries">
<h2>Formatting queries<a class="headerlink" href="#formatting-queries" title="Permalink to this headline"></a></h2>
<p>So far the queries have been string “literals”, meaning that the entire string is part of the program.
<p>The queries we have written so far are string “literals”, meaning that the entire string is part of the program.
But writing queries yourself can be slow, repetitive, and error-prone.</p>
<p>It is often a good idea to write Python code that assembles a query for you. One useful tool for that is the <a class="reference external" href="https://www.w3schools.com/python/ref_string_format.asp">string <code class="docutils literal notranslate"><span class="pre">format</span></code> method</a>.</p>
<p>It is often better to write Python code that assembles a query for you. One useful tool for that is the <a class="reference external" href="https://www.w3schools.com/python/ref_string_format.asp">string <code class="docutils literal notranslate"><span class="pre">format</span></code> method</a>.</p>
<p>As an example, well divide the previous query into two parts; a list of column names and a “base” for the query that contains everything except the column names.</p>
<p>Heres the list of columns well select.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">columns</span> <span class="o">=</span> <span class="s1">&#39;source_id, ra, dec, pmra, pmdec, parallax, radial_velocity&#39;</span>
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">columns</span> <span class="o">=</span> <span class="s1">&#39;source_id, ra, dec, pmra, pmdec, parallax&#39;</span>
</pre></div>
</div>
</div>
@@ -1205,7 +1201,8 @@ But writing queries yourself can be slow, repetitive, and error-prone.</p>
<p>And heres the base; its a string that contains at least one format specifier in curly brackets (braces).</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">query3_base</span> <span class="o">=</span> <span class="s2">&quot;&quot;&quot;SELECT TOP 10 </span>
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">query3_base</span> <span class="o">=</span> <span class="s2">&quot;&quot;&quot;SELECT </span>
<span class="s2">TOP 10 </span>
<span class="si">{columns}</span><span class="s2"></span>
<span class="s2">FROM gaiadr2.gaia_source</span>
<span class="s2">WHERE parallax &lt; 1</span>
@@ -1224,6 +1221,8 @@ But writing queries yourself can be slow, repetitive, and error-prone.</p>
</div>
</div>
</div>
<p>In this example, the variable that contains the column names and the variable in the format specifier have the same name.
Thats not required, but it is a common style.</p>
<p>The result is a string with line breaks. If you display it, the line breaks appear as <code class="docutils literal notranslate"><span class="pre">\n</span></code>.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
@@ -1232,7 +1231,7 @@ But writing queries yourself can be slow, repetitive, and error-prone.</p>
</div>
</div>
<div class="cell_output docutils container">
<div class="output text_plain highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>&#39;SELECT TOP 10 \nsource_id, ra, dec, pmra, pmdec, parallax, radial_velocity\nFROM gaiadr2.gaia_source\nWHERE parallax &lt; 1\n AND bp_rp BETWEEN -0.75 AND 2\n&#39;
<div class="output text_plain highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>&#39;SELECT \nTOP 10 \nsource_id, ra, dec, pmra, pmdec\nFROM gaiadr2.gaia_source\nWHERE parallax &lt; 1\n AND bp_rp BETWEEN -0.75 AND 2\n&#39;
</pre></div>
</div>
</div>
@@ -1245,8 +1244,9 @@ But writing queries yourself can be slow, repetitive, and error-prone.</p>
</div>
</div>
<div class="cell_output docutils container">
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>SELECT TOP 10
source_id, ra, dec, pmra, pmdec, parallax, radial_velocity
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>SELECT
TOP 10
source_id, ra, dec, pmra, pmdec
FROM gaiadr2.gaia_source
WHERE parallax &lt; 1
AND bp_rp BETWEEN -0.75 AND 2
@@ -1258,26 +1258,24 @@ WHERE parallax &lt; 1
<p>Lets run it and see if it works:</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">job3</span> <span class="o">=</span> <span class="n">Gaia</span><span class="o">.</span><span class="n">launch_job</span><span class="p">(</span><span class="n">query3</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">job3</span><span class="p">)</span>
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">job</span> <span class="o">=</span> <span class="n">Gaia</span><span class="o">.</span><span class="n">launch_job</span><span class="p">(</span><span class="n">query3</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">job</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>&lt;Table length=10&gt;
name dtype unit description n_bad
--------------- ------- -------- ------------------------------------------------------------------ -----
source_id int64 Unique source identifier (unique within a particular Data Release) 0
ra float64 deg Right ascension 0
dec float64 deg Declination 0
pmra float64 mas / yr Proper motion in right ascension direction 0
pmdec float64 mas / yr Proper motion in declination direction 0
parallax float64 mas Parallax 0
radial_velocity float64 km / s Radial velocity 10
name dtype unit description
--------- ------- -------- ------------------------------------------------------------------
source_id int64 Unique source identifier (unique within a particular Data Release)
ra float64 deg Right ascension
dec float64 deg Declination
pmra float64 mas / yr Proper motion in right ascension direction
pmdec float64 mas / yr Proper motion in declination direction
Jobid: None
Phase: COMPLETED
Owner: None
Output file: sync_20201229114653.xml.gz
Output file: sync_20210315091929.xml.gz
Results: None
</pre></div>
</div>
@@ -1285,27 +1283,27 @@ Results: None
</div>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">results3</span> <span class="o">=</span> <span class="n">job3</span><span class="o">.</span><span class="n">get_results</span><span class="p">()</span>
<span class="n">results3</span>
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">results</span> <span class="o">=</span> <span class="n">job</span><span class="o">.</span><span class="n">get_results</span><span class="p">()</span>
<span class="n">results</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<div class="output text_html"><i>Table length=10</i>
<table id="table139836148327184" class="table-striped table-bordered table-condensed">
<thead><tr><th>source_id</th><th>ra</th><th>dec</th><th>pmra</th><th>pmdec</th><th>parallax</th><th>radial_velocity</th></tr></thead>
<thead><tr><th></th><th>deg</th><th>deg</th><th>mas / yr</th><th>mas / yr</th><th>mas</th><th>km / s</th></tr></thead>
<thead><tr><th>int64</th><th>float64</th><th>float64</th><th>float64</th><th>float64</th><th>float64</th><th>float64</th></tr></thead>
<tr><td>4660466371507774848</td><td>79.49100199261952</td><td>-66.41411777672668</td><td>2.6073927760139983</td><td>-1.1968907801968522</td><td>-0.13534455558687877</td><td>--</td></tr>
<tr><td>4660498704031984000</td><td>80.19346436358076</td><td>-66.04907675232856</td><td>1.644062563874402</td><td>-1.954975321006136</td><td>-0.1774586376397</td><td>--</td></tr>
<tr><td>4660458949824817024</td><td>79.68666778641992</td><td>-66.52220959662557</td><td>1.4777697275880581</td><td>0.3328961930362448</td><td>-0.030149510331454386</td><td>--</td></tr>
<tr><td>4660451974773926528</td><td>79.89972073493868</td><td>-66.75037858071191</td><td>2.4635048563021065</td><td>-0.7474574729840501</td><td>-0.005219591141134416</td><td>--</td></tr>
<tr><td>4660474033731357056</td><td>78.93214036467484</td><td>-66.46483644591667</td><td>1.2369118132455594</td><td>-1.7187143034638677</td><td>0.6018032937392243</td><td>--</td></tr>
<tr><td>4660454105077874688</td><td>79.91045649235578</td><td>-66.68106846135971</td><td>1.4747855407533677</td><td>0.022464361420165786</td><td>-0.19949109843083218</td><td>--</td></tr>
<tr><td>4660494855753740544</td><td>79.63101881952036</td><td>-66.15063315813536</td><td>1.6227863263222113</td><td>0.20490893261905152</td><td>0.0013030724292998944</td><td>--</td></tr>
<tr><td>4660439536571308160</td><td>80.24817961417894</td><td>-66.46303270664092</td><td>1.8652105429518493</td><td>-0.5618584442373111</td><td>-0.10578442360531497</td><td>--</td></tr>
<tr><td>4660470185440466560</td><td>79.25942306061864</td><td>-66.51120777549605</td><td>3.373561172003989</td><td>0.25326243152876704</td><td>-0.6932631338638376</td><td>--</td></tr>
<tr><td>4660441460690365568</td><td>80.38862155241748</td><td>-66.34404494135046</td><td>1.6588478675146066</td><td>-0.09646951910977163</td><td>-0.2100859303087445</td><td>--</td></tr>
<table id="table139985564118512" class="table-striped table-bordered table-condensed">
<thead><tr><th>source_id</th><th>ra</th><th>dec</th><th>pmra</th><th>pmdec</th></tr></thead>
<thead><tr><th></th><th>deg</th><th>deg</th><th>mas / yr</th><th>mas / yr</th></tr></thead>
<thead><tr><th>int64</th><th>float64</th><th>float64</th><th>float64</th><th>float64</th></tr></thead>
<tr><td>5895272561481374080</td><td>213.2606587905109</td><td>-56.55044401535715</td><td>0.3894438898301715</td><td>1.2299266281737415</td></tr>
<tr><td>5895261875598904576</td><td>213.5508930114549</td><td>-56.61037780154348</td><td>0.16203641364393007</td><td>-4.672602679543312</td></tr>
<tr><td>5895247444506644992</td><td>213.33215109206796</td><td>-56.975347759380995</td><td>-7.474003156859284</td><td>-3.538080792097856</td></tr>
<tr><td>5895259470417635968</td><td>213.78815034206346</td><td>-56.64585047451594</td><td>-5.287202255231853</td><td>-0.8163762113468646</td></tr>
<tr><td>5895265925746051584</td><td>213.17082359534547</td><td>-56.74540885107754</td><td>-7.880749306158471</td><td>-4.8585444120179595</td></tr>
<tr><td>5895260913525974528</td><td>213.66936020541976</td><td>-56.66655190442016</td><td>-4.7820929042428215</td><td>-1.5566420086447643</td></tr>
<tr><td>5895264212062283008</td><td>213.7755742121852</td><td>-56.51570859067397</td><td>-6.657690998559842</td><td>-1.7616494482071872</td></tr>
<tr><td>5895253457497979136</td><td>213.30929960610283</td><td>-56.78849448744587</td><td>-5.242106718924749</td><td>-0.18655636353898095</td></tr>
<tr><td>4143614130253524096</td><td>269.1749117455479</td><td>-18.53415139972117</td><td>2.6164274510804826</td><td>1.3244248889980894</td></tr>
<tr><td>4065443904433108992</td><td>273.26868565443743</td><td>-24.421651815402857</td><td>-1.663096652191022</td><td>-2.6514745376067683</td></tr>
</table></div></div>
</div>
<p>Good so far.</p>
@@ -1317,25 +1315,27 @@ Results: None
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="c1"># Solution</span>
<span class="n">query4_base</span> <span class="o">=</span> <span class="s2">&quot;&quot;&quot;SELECT TOP 10</span>
<span class="n">query_base</span> <span class="o">=</span> <span class="s2">&quot;&quot;&quot;SELECT </span>
<span class="s2">TOP 10</span>
<span class="si">{columns}</span><span class="s2"></span>
<span class="s2">FROM gaiadr2.gaia_source</span>
<span class="s2">WHERE parallax &lt; </span><span class="si">{max_parallax}</span><span class="s2"> AND </span>
<span class="s2">bp_rp BETWEEN -0.75 AND 2</span>
<span class="s2">&quot;&quot;&quot;</span>
<span class="n">query4</span> <span class="o">=</span> <span class="n">query4_base</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">columns</span><span class="o">=</span><span class="n">columns</span><span class="p">,</span>
<span class="n">query</span> <span class="o">=</span> <span class="n">query_base</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">columns</span><span class="o">=</span><span class="n">columns</span><span class="p">,</span>
<span class="n">max_parallax</span><span class="o">=</span><span class="mf">0.5</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">query</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>SELECT TOP 10
source_id, ref_epoch, ra, dec, parallax
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>SELECT
TOP 10
source_id, ra, dec, pmra, pmdec
FROM gaiadr2.gaia_source
WHERE parallax &lt; 1
AND bp_rp BETWEEN -0.75 AND 2
WHERE parallax &lt; 0.5 AND
bp_rp BETWEEN -0.75 AND 2
</pre></div>
</div>
</div>
@@ -1360,22 +1360,16 @@ WHERE parallax &lt; 1
<li><p>Read the metadata and the documentation to make sure you understand the tables, their columns, and what they mean.</p></li>
<li><p>Develop queries incrementally: start with something simple, test it, and add a little bit at a time.</p></li>
<li><p>Use ADQL features like <code class="docutils literal notranslate"><span class="pre">TOP</span></code> and <code class="docutils literal notranslate"><span class="pre">COUNT</span></code> to test before you run a query that might return a lot of data.</p></li>
<li><p>If you know your query will return fewer than 3000 rows, you can run it synchronously, which might complete faster (but it doesnt seem to make much difference). If it might return more than 3000 rows, you should run it asynchronously.</p></li>
<li><p>If you know your query will return fewer than 2000 rows, you can run it synchronously, which might complete faster. If it might return more than 2000 rows, you should run it asynchronously.</p></li>
<li><p>ADQL and SQL are not case-sensitive, so you dont have to capitalize the keywords, but you should.</p></li>
<li><p>ADQL and SQL dont require you to break a query into multiple lines, but you should.</p></li>
</ul>
<p>Jupyter notebooks can be good for developing and testing code, but they have some drawbacks. In particular, if you run the cells out of order, you might find that variables dont have the values you expect.</p>
<p>There are a few things you can do to mitigate these problems:</p>
<p>To mitigate these problems:</p>
<ul class="simple">
<li><p>Make each section of the notebook self-contained. Try not to use the same variable name in more than one section.</p></li>
<li><p>Keep notebooks short. Look for places where you can break your analysis into phases with one notebook per phase.</p></li>
</ul>
<blockquote>
<div><p>One of the other tables well use is
<code class="docutils literal notranslate"><span class="pre">gaiadr2.panstarrs1_original_valid</span></code>. Use <code class="docutils literal notranslate"><span class="pre">load_table</span></code> to get the
metadata for this table. How many columns are there and what are
their names?</p>
</div></blockquote>
</div>
</div>
@@ -1408,7 +1402,7 @@ their names?</p>
<div class='prev-next-bottom'>
<a class='left-prev' id="prev-link" href="README.html" title="previous page">Astronomical Data in Python</a>
<a class='right-next' id="next-link" href="02_coords.html" title="next page">Coordinates and units</a>
<a class='right-next' id="next-link" href="02_coords.html" title="next page">2. Coordinates and Units</a>
</div>
<footer class="footer mt-5 mt-md-0">

View File

@@ -5,7 +5,7 @@
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Coordinates and units &#8212; Astronomical Data in Python</title>
<title>2. Coordinates and Units &#8212; Astronomical Data in Python</title>
<link rel="stylesheet" href="_static/css/index.d431a4ee1c1efae0e38bdfebc22debff.css">
@@ -57,8 +57,8 @@
<script async="async" src="_static/sphinx-thebe.js"></script>
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="next" title="Proper Motion" href="03_motion.html" />
<link rel="prev" title="Queries" href="01_query.html" />
<link rel="next" title="3. Proper Motion" href="03_motion.html" />
<link rel="prev" title="1. Queries" href="01_query.html" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="docsearch:language" content="en" />
@@ -99,22 +99,22 @@
<ul class="current nav sidenav_l1">
<li class="toctree-l1">
<a class="reference internal" href="01_query.html">
Queries
1. Queries
</a>
</li>
<li class="toctree-l1 current active">
<a class="current reference internal" href="#">
Coordinates and units
2. Coordinates and Units
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="03_motion.html">
Proper Motion
3. Proper Motion
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="04_select.html">
Transformation and Selection
4. Transformation and Selection
</a>
</li>
<li class="toctree-l1">
@@ -244,8 +244,8 @@
</a>
</li>
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#selecting-a-region">
Selecting a region
<a class="reference internal nav-link" href="#working-with-units">
Working with Units
</a>
<ul class="nav section-nav flex-column">
<li class="toc-h3 nav-item toc-entry">
@@ -255,23 +255,30 @@
</li>
</ul>
</li>
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#selecting-a-region">
Selecting a Region
</a>
<ul class="nav section-nav flex-column">
<li class="toc-h3 nav-item toc-entry">
<a class="reference internal nav-link" href="#id1">
Exercise
</a>
</li>
</ul>
</li>
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#getting-gd-1-data">
Getting GD-1 Data
</a>
</li>
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#working-with-coordinates">
Working with coordinates
</a>
</li>
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#transforming-coordinates">
Transforming coordinates
</a>
<ul class="nav section-nav flex-column">
<li class="toc-h3 nav-item toc-entry">
<a class="reference internal nav-link" href="#id1">
<a class="reference internal nav-link" href="#id2">
Exercise
</a>
</li>
@@ -283,8 +290,13 @@
</a>
</li>
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#selecting-a-polygon">
Selecting a polygon
<a class="reference internal nav-link" href="#defining-a-polygon">
Defining a polygon
</a>
</li>
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#assembling-the-query">
Assembling the query
</a>
</li>
<li class="toc-h2 nav-item toc-entry">
@@ -314,11 +326,9 @@
<div>
<div class="section" id="coordinates-and-units">
<h1>Coordinates and units<a class="headerlink" href="#coordinates-and-units" title="Permalink to this headline"></a></h1>
<p>This is the second in a series of notebooks related to astronomy data.</p>
<p>As a running example, we are replicating parts of the analysis in a recent paper, “<a class="reference external" href="https://arxiv.org/abs/1805.00425">Off the beaten path: Gaia reveals GD-1 stars outside of the main stream</a>” by Adrian M. Price-Whelan and Ana Bonaca.</p>
<p>In the first notebook, we wrote ADQL queries and used them to select and download data from the Gaia server.</p>
<p>In this notebook, well pick up where we left off and write a query to select stars from the region of the sky where we expect GD-1 to be.</p>
<h1>2. Coordinates and Units<a class="headerlink" href="#coordinates-and-units" title="Permalink to this headline"></a></h1>
<p>In the previous lesson, we wrote ADQL queries and used them to select and download data from the Gaia server.</p>
<p>In this lesson, well pick up where we left off and write a query to select stars from a particular region of the sky.</p>
<div class="section" id="outline">
<h2>Outline<a class="headerlink" href="#outline" title="Permalink to this headline"></a></h2>
<p>Well start with an example that does a “cone search”; that is, it selects stars that appear in a circular region of the sky.</p>
@@ -337,129 +347,10 @@
<li><p>Download the results of a query and store them in a file.</p></li>
</ul>
</div>
<div class="section" id="selecting-a-region">
<h2>Selecting a region<a class="headerlink" href="#selecting-a-region" title="Permalink to this headline"></a></h2>
<p>One of the most common ways to restrict a query is to select stars in a particular region of the sky.</p>
<p>For example, heres a query from the <a class="reference external" href="https://gea.esac.esa.int/archive-help/adql/examples/index.html">Gaia archive documentation</a> that selects all the objects … in a circular region centered at (266.41683, -29.00781) with a search radius of 5 arcmin (0.08333 deg).”</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">query</span> <span class="o">=</span> <span class="s2">&quot;&quot;&quot;</span>
<span class="s2">SELECT </span>
<span class="s2">TOP 10 source_id</span>
<span class="s2">FROM gaiadr2.gaia_source</span>
<span class="s2">WHERE 1=CONTAINS(</span>
<span class="s2"> POINT(ra, dec),</span>
<span class="s2"> CIRCLE(266.41683, -29.00781, 0.08333333))</span>
<span class="s2">&quot;&quot;&quot;</span>
</pre></div>
</div>
</div>
</div>
<p>This query uses three keywords that are specific to ADQL (not SQL):</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">POINT</span></code>: a location in <a class="reference external" href="https://en.wikipedia.org/wiki/International_Celestial_Reference_System">ICRS coordinates</a>, specified in degrees of right ascension and declination.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">CIRCLE</span></code>: a circle where the first two values are the coordinates of the center and the third is the radius in degrees.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">CONTAINS</span></code>: a function that returns <code class="docutils literal notranslate"><span class="pre">1</span></code> if a <code class="docutils literal notranslate"><span class="pre">POINT</span></code> is contained in a shape and <code class="docutils literal notranslate"><span class="pre">0</span></code> otherwise.</p></li>
</ul>
<p>Here is the <a class="reference external" href="http://www.ivoa.net/documents/ADQL/20180112/PR-ADQL-2.1-20180112.html#tth_sEc4.2.12">documentation of <code class="docutils literal notranslate"><span class="pre">CONTAINS</span></code></a>.</p>
<p>A query like this is called a cone search because it selects stars in a cone.</p>
<p>Heres how we run it.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">astroquery.gaia</span> <span class="kn">import</span> <span class="n">Gaia</span>
<span class="n">job</span> <span class="o">=</span> <span class="n">Gaia</span><span class="o">.</span><span class="n">launch_job</span><span class="p">(</span><span class="n">query</span><span class="p">)</span>
<span class="n">job</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Created TAP+ (v1.2.1) - Connection:
Host: gea.esac.esa.int
Use HTTPS: True
Port: 443
SSL Port: 443
Created TAP+ (v1.2.1) - Connection:
Host: geadata.esac.esa.int
Use HTTPS: True
Port: 443
SSL Port: 443
</pre></div>
</div>
<div class="output text_plain highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>&lt;astroquery.utils.tap.model.job.Job at 0x7f59bd93e490&gt;
</pre></div>
</div>
</div>
</div>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">result</span> <span class="o">=</span> <span class="n">job</span><span class="o">.</span><span class="n">get_results</span><span class="p">()</span>
<span class="n">result</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<div class="output text_html"><i>Table length=10</i>
<table id="table140023409350736" class="table-striped table-bordered table-condensed">
<thead><tr><th>source_id</th></tr></thead>
<thead><tr><th>int64</th></tr></thead>
<tr><td>4057468321929794432</td></tr>
<tr><td>4057468287575835392</td></tr>
<tr><td>4057482027171038976</td></tr>
<tr><td>4057470349160630656</td></tr>
<tr><td>4057470039924301696</td></tr>
<tr><td>4057469868125641984</td></tr>
<tr><td>4057468351995073024</td></tr>
<tr><td>4057469661959554560</td></tr>
<tr><td>4057470520960672640</td></tr>
<tr><td>4057470555320409600</td></tr>
</table></div></div>
</div>
<div class="section" id="exercise">
<h3>Exercise<a class="headerlink" href="#exercise" title="Permalink to this headline"></a></h3>
<p>When you are debugging queries like this, you can use <code class="docutils literal notranslate"><span class="pre">TOP</span></code> to limit the size of the results, but then you still dont know how big the results will be.</p>
<p>An alternative is to use <code class="docutils literal notranslate"><span class="pre">COUNT</span></code>, which asks for the number of rows that would be selected, but it does not return them.</p>
<p>In the previous query, replace <code class="docutils literal notranslate"><span class="pre">TOP</span> <span class="pre">10</span> <span class="pre">source_id</span></code> with <code class="docutils literal notranslate"><span class="pre">COUNT(source_id)</span></code> and run the query again. How many stars has Gaia identified in the cone we searched?</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="c1"># Solution</span>
<span class="n">query</span> <span class="o">=</span> <span class="s2">&quot;&quot;&quot;</span>
<span class="s2">SELECT </span>
<span class="s2">COUNT(source_id)</span>
<span class="s2">FROM gaiadr2.gaia_source</span>
<span class="s2">WHERE 1=CONTAINS(</span>
<span class="s2"> POINT(ra, dec),</span>
<span class="s2"> CIRCLE(266.41683, -29.00781, 0.08333333))</span>
<span class="s2">&quot;&quot;&quot;</span>
</pre></div>
</div>
</div>
</div>
</div>
</div>
<div class="section" id="getting-gd-1-data">
<h2>Getting GD-1 Data<a class="headerlink" href="#getting-gd-1-data" title="Permalink to this headline"></a></h2>
<p>From the Price-Whelan and Bonaca paper, we will try to reproduce Figure 1, which includes this representation of stars likely to belong to GD-1:</p>
<img alt="https://github.com/datacarpentry/astronomy-python/raw/gh-pages/fig/gd1-4.png" src="https://github.com/datacarpentry/astronomy-python/raw/gh-pages/fig/gd1-4.png" />
<p>The axes of this figure are defined so the x-axis is aligned with the stars in GD-1, and the y-axis is perpendicular.</p>
<ul class="simple">
<li><p>Along the x-axis (<span class="math notranslate nohighlight">\(\phi_1\)</span>) the figure extends from -100 to 20 degrees.</p></li>
<li><p>Along the y-axis (<span class="math notranslate nohighlight">\(\phi_2\)</span>) the figure extends from about -8 to 4 degrees.</p></li>
</ul>
<p>Ideally, we would select all stars from this rectangle, but there are more than 10 million of them, so</p>
<ul class="simple">
<li><p>That would be difficult to work with,</p></li>
<li><p>As anonymous Gaia users, we are limited to 3 million rows in a single query, and</p></li>
<li><p>While we are developing and testing code, it will be faster to work with a smaller dataset.</p></li>
</ul>
<p>So well start by selecting stars in a smaller rectangle near the center of GD-1, from -55 to -45 degrees <span class="math notranslate nohighlight">\(\phi_1\)</span> and -8 to 4 degrees <span class="math notranslate nohighlight">\(\phi_2\)</span>.</p>
<p>But first we lets see how to represent quantities with units like degrees.</p>
</div>
<div class="section" id="working-with-coordinates">
<h2>Working with coordinates<a class="headerlink" href="#working-with-coordinates" title="Permalink to this headline"></a></h2>
<p>Coordinates are physical quantities, which means that they have two parts, a value and a unit.</p>
<p>For example, the coordinate <span class="math notranslate nohighlight">\(30^{\circ}\)</span> has value 30 and its units are degrees.</p>
<div class="section" id="working-with-units">
<h2>Working with Units<a class="headerlink" href="#working-with-units" title="Permalink to this headline"></a></h2>
<p>The measurements we will work with are physical quantities, which means that they have two parts, a value and a unit.
For example, the coordinate <span class="math notranslate nohighlight">\(30^{\circ}\)</span> has value 30 and its units are degrees.</p>
<p>Until recently, most scientific computation was done with values only; units were left out of the program altogether, <a class="reference external" href="https://en.wikipedia.org/wiki/Mars_Climate_Orbiter#Cause_of_failure">often with catastrophic results</a>.</p>
<p>Astropy provides tools for including units explicitly in computations, which makes it possible to detect errors before they cause disasters.</p>
<p>To use Astropy units, we import them like this:</p>
@@ -1487,8 +1378,8 @@ Created TAP+ (v1.2.1) - Connection:
<p>To create a quantity, we multiply a value by a unit.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">quantity</span> <span class="o">=</span> <span class="mi">30</span> <span class="o">*</span> <span class="n">u</span><span class="o">.</span><span class="n">degree</span>
<span class="nb">type</span><span class="p">(</span><span class="n">quantity</span><span class="p">)</span>
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">angle</span> <span class="o">=</span> <span class="mi">10</span> <span class="o">*</span> <span class="n">u</span><span class="o">.</span><span class="n">degree</span>
<span class="nb">type</span><span class="p">(</span><span class="n">angle</span><span class="p">)</span>
</pre></div>
</div>
</div>
@@ -1498,24 +1389,210 @@ Created TAP+ (v1.2.1) - Connection:
</div>
</div>
</div>
<p>The result is a <code class="docutils literal notranslate"><span class="pre">Quantity</span></code> object.</p>
<p>Jupyter knows how to display <code class="docutils literal notranslate"><span class="pre">Quantities</span></code> like this:</p>
<p>The result is a <code class="docutils literal notranslate"><span class="pre">Quantity</span></code> object.
Jupyter knows how to display <code class="docutils literal notranslate"><span class="pre">Quantities</span></code> like this:</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">quantity</span>
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">angle</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<div class="output text_latex math notranslate nohighlight">
\[30 \; \mathrm{{}^{\circ}}\]</div>
\[10 \; \mathrm{{}^{\circ}}\]</div>
</div>
</div>
<p>Quantities provide a method called <code class="docutils literal notranslate"><span class="pre">to</span></code> that converts to other units. For example, we can compute the number of arcminutes in <code class="docutils literal notranslate"><span class="pre">angle</span></code>:</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">angle_arcmin</span> <span class="o">=</span> <span class="n">angle</span><span class="o">.</span><span class="n">to</span><span class="p">(</span><span class="n">u</span><span class="o">.</span><span class="n">arcmin</span><span class="p">)</span>
<span class="n">angle_arcmin</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<div class="output text_latex math notranslate nohighlight">
\[600 \; \mathrm{{}^{\prime}}\]</div>
</div>
</div>
<p>If you add quantities, Astropy converts them to compatible units, if possible:</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">angle</span> <span class="o">+</span> <span class="mi">30</span> <span class="o">*</span> <span class="n">u</span><span class="o">.</span><span class="n">arcmin</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<div class="output text_latex math notranslate nohighlight">
\[10.5 \; \mathrm{{}^{\circ}}\]</div>
</div>
</div>
<p>If the units are not compatible, you get an error.
For example:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">angle</span> <span class="o">+</span> <span class="mi">5</span> <span class="o">*</span> <span class="n">u</span><span class="o">.</span><span class="n">second</span>
</pre></div>
</div>
<p>causes a <code class="docutils literal notranslate"><span class="pre">UnitConversionError</span></code>.</p>
<div class="section" id="exercise">
<h3>Exercise<a class="headerlink" href="#exercise" title="Permalink to this headline"></a></h3>
<p>Create a quantity that represents 5 <a class="reference external" href="https://en.wikipedia.org/wiki/Minute_and_second_of_arc">arcminutes</a> and assign it to a variable called <code class="docutils literal notranslate"><span class="pre">radius</span></code>.</p>
<p>Then convert it to degrees.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="c1">## Solution</span>
<span class="n">radius</span> <span class="o">=</span> <span class="mi">5</span> <span class="o">*</span> <span class="n">u</span><span class="o">.</span><span class="n">arcmin</span>
<span class="nb">print</span><span class="p">(</span><span class="n">radius</span><span class="p">)</span>
<span class="n">radius</span><span class="o">.</span><span class="n">to</span><span class="p">(</span><span class="n">u</span><span class="o">.</span><span class="n">degree</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>5.0 arcmin
</pre></div>
</div>
<div class="output text_latex math notranslate nohighlight">
\[0.083333333 \; \mathrm{{}^{\circ}}\]</div>
</div>
</div>
</div>
</div>
<div class="section" id="selecting-a-region">
<h2>Selecting a Region<a class="headerlink" href="#selecting-a-region" title="Permalink to this headline"></a></h2>
<p>One of the most common ways to restrict a query is to select stars in a particular region of the sky.
For example, heres a query from the <a class="reference external" href="https://gea.esac.esa.int/archive-help/adql/examples/index.html">Gaia archive documentation</a> that selects objects in a circular region centered at (88.8, 7.4) with a search radius of 5 arcmin (0.08333 deg).</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">query_cone</span> <span class="o">=</span> <span class="s2">&quot;&quot;&quot;SELECT </span>
<span class="s2">TOP 10 </span>
<span class="s2">source_id</span>
<span class="s2">FROM gaiadr2.gaia_source</span>
<span class="s2">WHERE 1=CONTAINS(</span>
<span class="s2"> POINT(ra, dec),</span>
<span class="s2"> CIRCLE(88.8, 7.4, 0.08333333))</span>
<span class="s2">&quot;&quot;&quot;</span>
</pre></div>
</div>
</div>
</div>
<p>This query uses three keywords that are specific to ADQL (not SQL):</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">POINT</span></code>: a location in <a class="reference external" href="https://en.wikipedia.org/wiki/International_Celestial_Reference_System">ICRS coordinates</a>, specified in degrees of right ascension and declination.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">CIRCLE</span></code>: a circle where the first two values are the coordinates of the center and the third is the radius in degrees.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">CONTAINS</span></code>: a function that returns <code class="docutils literal notranslate"><span class="pre">1</span></code> if a <code class="docutils literal notranslate"><span class="pre">POINT</span></code> is contained in a shape and <code class="docutils literal notranslate"><span class="pre">0</span></code> otherwise.</p></li>
</ul>
<p>Here is the <a class="reference external" href="http://www.ivoa.net/documents/ADQL/20180112/PR-ADQL-2.1-20180112.html#tth_sEc4.2.12">documentation of <code class="docutils literal notranslate"><span class="pre">CONTAINS</span></code></a>.</p>
<p>A query like this is called a cone search because it selects stars in a cone.
Heres how we run it.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">astroquery.gaia</span> <span class="kn">import</span> <span class="n">Gaia</span>
<span class="n">job</span> <span class="o">=</span> <span class="n">Gaia</span><span class="o">.</span><span class="n">launch_job</span><span class="p">(</span><span class="n">query_cone</span><span class="p">)</span>
<span class="n">job</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>Created TAP+ (v1.2.1) - Connection:
Host: gea.esac.esa.int
Use HTTPS: True
Port: 443
SSL Port: 443
Created TAP+ (v1.2.1) - Connection:
Host: geadata.esac.esa.int
Use HTTPS: True
Port: 443
SSL Port: 443
</pre></div>
</div>
<div class="output text_plain highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>&lt;astroquery.utils.tap.model.job.Job at 0x7f277785fa30&gt;
</pre></div>
</div>
</div>
</div>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">results</span> <span class="o">=</span> <span class="n">job</span><span class="o">.</span><span class="n">get_results</span><span class="p">()</span>
<span class="n">results</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<div class="output text_html"><i>Table length=10</i>
<table id="table139807485721280" class="table-striped table-bordered table-condensed">
<thead><tr><th>source_id</th></tr></thead>
<thead><tr><th>int64</th></tr></thead>
<tr><td>3322773965056065536</td></tr>
<tr><td>3322773758899157120</td></tr>
<tr><td>3322774068134271104</td></tr>
<tr><td>3322773930696320512</td></tr>
<tr><td>3322774377374425728</td></tr>
<tr><td>3322773724537891456</td></tr>
<tr><td>3322773724537891328</td></tr>
<tr><td>3322773930696321792</td></tr>
<tr><td>3322773724537890944</td></tr>
<tr><td>3322773930696322176</td></tr>
</table></div></div>
</div>
<div class="section" id="id1">
<h3>Exercise<a class="headerlink" href="#id1" title="Permalink to this headline"></a></h3>
<p>When you are debugging queries like this, you can use <code class="docutils literal notranslate"><span class="pre">TOP</span></code> to limit the size of the results, but then you still dont know how big the results will be.</p>
<p>An alternative is to use <code class="docutils literal notranslate"><span class="pre">COUNT</span></code>, which asks for the number of rows that would be selected, but it does not return them.</p>
<p>In the previous query, replace <code class="docutils literal notranslate"><span class="pre">TOP</span> <span class="pre">10</span> <span class="pre">source_id</span></code> with <code class="docutils literal notranslate"><span class="pre">COUNT(source_id)</span></code> and run the query again. How many stars has Gaia identified in the cone we searched?</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="c1"># Solution</span>
<span class="n">query</span> <span class="o">=</span> <span class="s2">&quot;&quot;&quot;SELECT </span>
<span class="s2">COUNT(source_id)</span>
<span class="s2">FROM gaiadr2.gaia_source</span>
<span class="s2">WHERE 1=CONTAINS(</span>
<span class="s2"> POINT(ra, dec),</span>
<span class="s2"> CIRCLE(88.8, 7.4, 0.08333333))</span>
<span class="s2">&quot;&quot;&quot;</span>
<span class="n">job</span> <span class="o">=</span> <span class="n">Gaia</span><span class="o">.</span><span class="n">launch_job</span><span class="p">(</span><span class="n">query</span><span class="p">)</span>
<span class="n">results</span> <span class="o">=</span> <span class="n">job</span><span class="o">.</span><span class="n">get_results</span><span class="p">()</span>
<span class="n">results</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<div class="output text_html"><i>Table length=1</i>
<table id="table139808112576400" class="table-striped table-bordered table-condensed">
<thead><tr><th>count</th></tr></thead>
<thead><tr><th>int64</th></tr></thead>
<tr><td>594</td></tr>
</table></div></div>
</div>
</div>
</div>
<div class="section" id="getting-gd-1-data">
<h2>Getting GD-1 Data<a class="headerlink" href="#getting-gd-1-data" title="Permalink to this headline"></a></h2>
<p>From the Price-Whelan and Bonaca paper, we will try to reproduce Figure 1, which includes this representation of stars likely to belong to GD-1:</p>
<img alt="https://github.com/datacarpentry/astronomy-python/raw/gh-pages/fig/gd1-4.png" src="https://github.com/datacarpentry/astronomy-python/raw/gh-pages/fig/gd1-4.png" />
<p>The axes of this figure are defined so the x-axis is aligned with the stars in GD-1, and the y-axis is perpendicular.</p>
<ul class="simple">
<li><p>Along the x-axis (<span class="math notranslate nohighlight">\(\phi_1\)</span>) the figure extends from -100 to 20 degrees.</p></li>
<li><p>Along the y-axis (<span class="math notranslate nohighlight">\(\phi_2\)</span>) the figure extends from about -8 to 4 degrees.</p></li>
</ul>
<p>Ideally, we would select all stars from this rectangle, but there are more than 10 million of them, so</p>
<ul class="simple">
<li><p>That would be difficult to work with,</p></li>
<li><p>As anonymous Gaia users, we are limited to 3 million rows in a single query, and</p></li>
<li><p>While we are developing and testing code, it will be faster to work with a smaller dataset.</p></li>
</ul>
<p>So well start by selecting stars in a smaller rectangle near the center of GD-1, from -55 to -45 degrees <span class="math notranslate nohighlight">\(\phi_1\)</span> and -8 to 4 degrees <span class="math notranslate nohighlight">\(\phi_2\)</span>.</p>
<p>But first we lets see how to represent these coordinates with Astropy.</p>
</div>
<div class="section" id="transforming-coordinates">
<h2>Transforming coordinates<a class="headerlink" href="#transforming-coordinates" title="Permalink to this headline"></a></h2>
<p>Astropy provides a <code class="docutils literal notranslate"><span class="pre">SkyCoord</span></code> object that represents sky coordinates relative to a specified frame.</p>
<p>The following example creates a <code class="docutils literal notranslate"><span class="pre">SkyCoord</span></code> object that represents the approximate coordinates of <a class="reference external" href="http://simbad.u-strasbg.fr/simbad/sim-basic?Ident=Betelgeuse">Betelgeuse</a> (alf Ori) in the ICRS frame.</p>
<p><a class="reference external" href="https://www.iers.org/IERS/EN/Science/ICRS/ICRS.html">ICRS</a> is the
“International Celestial Reference System”, adopted in 1997 by the International Astronomical Union.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">astropy.coordinates</span> <span class="kn">import</span> <span class="n">SkyCoord</span>
@@ -1551,8 +1628,9 @@ For example, we can transform <code class="docutils literal notranslate"><span c
</div>
</div>
</div>
<p>Notice that in the Galactic frame, the coordinates are called <code class="docutils literal notranslate"><span class="pre">l</span></code> and <code class="docutils literal notranslate"><span class="pre">b</span></code>, not <code class="docutils literal notranslate"><span class="pre">ra</span></code> and <code class="docutils literal notranslate"><span class="pre">dec</span></code>.</p>
<p>To transform to and from GD-1 coordinates, well use a frame defined by <a class="reference external" href="https://gala-astro.readthedocs.io/en/latest/">Gala</a>, which is an Astropy-affiliated library that provides tools for galactic dynamics.</p>
<p>Gala provides <code class="docutils literal notranslate"><span class="pre">GD1Koposov10</span></code>, which is “<a class="reference external" href="https://gala-astro.readthedocs.io/en/latest/_modules/gala/coordinates/gd1.html">a Heliocentric spherical coordinate system defined by the orbit of the GD-1 stream</a></p>
<p>Gala provides <a class="reference external" href="https://gala-astro.readthedocs.io/en/latest/_modules/gala/coordinates/gd1.html"><code class="docutils literal notranslate"><span class="pre">GD1Koposov10</span></code></a>, which is “a Heliocentric spherical coordinate system defined by the orbit of the GD-1 stream”.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">gala.coordinates</span> <span class="kn">import</span> <span class="n">GD1Koposov10</span>
@@ -1583,8 +1661,10 @@ For example, we can transform <code class="docutils literal notranslate"><span c
</div>
</div>
</div>
<div class="section" id="id1">
<h3>Exercise<a class="headerlink" href="#id1" title="Permalink to this headline"></a></h3>
<p>Notice that the coordinates are called <code class="docutils literal notranslate"><span class="pre">phi1</span></code> and <code class="docutils literal notranslate"><span class="pre">phi2</span></code>.
These are the coordinates shown in the figure from the paper, above.</p>
<div class="section" id="id2">
<h3>Exercise<a class="headerlink" href="#id2" title="Permalink to this headline"></a></h3>
<p>Lets find the location of GD-1 in ICRS coordinates.</p>
<ol class="simple">
<li><p>Create a <code class="docutils literal notranslate"><span class="pre">SkyCoord</span></code> object at 0°, 0° in the GD-1 frame.</p></li>
@@ -1595,16 +1675,22 @@ For example, we can transform <code class="docutils literal notranslate"><span c
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="c1"># Solution</span>
<span class="n">coord_gd1</span> <span class="o">=</span> <span class="n">SkyCoord</span><span class="p">(</span><span class="mi">0</span><span class="o">*</span><span class="n">u</span><span class="o">.</span><span class="n">degree</span><span class="p">,</span> <span class="mi">0</span><span class="o">*</span><span class="n">u</span><span class="o">.</span><span class="n">degree</span><span class="p">,</span> <span class="n">frame</span><span class="o">=</span><span class="n">gd1_frame</span><span class="p">)</span>
<span class="n">origin_gd1</span> <span class="o">=</span> <span class="n">SkyCoord</span><span class="p">(</span><span class="mi">0</span><span class="o">*</span><span class="n">u</span><span class="o">.</span><span class="n">degree</span><span class="p">,</span> <span class="mi">0</span><span class="o">*</span><span class="n">u</span><span class="o">.</span><span class="n">degree</span><span class="p">,</span> <span class="n">frame</span><span class="o">=</span><span class="n">gd1_frame</span><span class="p">)</span>
<span class="c1"># OR</span>
<span class="n">origin_gd1</span> <span class="o">=</span> <span class="n">SkyCoord</span><span class="p">(</span><span class="n">phi1</span><span class="o">=</span><span class="mi">0</span><span class="o">*</span><span class="n">u</span><span class="o">.</span><span class="n">degree</span><span class="p">,</span>
<span class="n">phi2</span><span class="o">=</span><span class="mi">0</span><span class="o">*</span><span class="n">u</span><span class="o">.</span><span class="n">degree</span><span class="p">,</span>
<span class="n">frame</span><span class="o">=</span><span class="n">gd1_frame</span><span class="p">)</span>
<span class="c1"># Note: because ICRS is built into Astropy, </span>
<span class="c1"># we can identify it by name</span>
<span class="n">coord_gd1</span><span class="o">.</span><span class="n">transform_to</span><span class="p">(</span><span class="s1">&#39;icrs&#39;</span><span class="p">)</span>
<span class="c1"># we can identify it by string name</span>
<span class="n">origin_gd1</span><span class="o">.</span><span class="n">transform_to</span><span class="p">(</span><span class="s1">&#39;icrs&#39;</span><span class="p">)</span>
<span class="c1"># More formally, we could instantiate it</span>
<span class="kn">from</span> <span class="nn">astropy.coordinates</span> <span class="kn">import</span> <span class="n">ICRS</span>
<span class="n">icrs_frame</span> <span class="o">=</span> <span class="n">ICRS</span><span class="p">()</span>
<span class="n">coord_gd1</span><span class="o">.</span><span class="n">transform_to</span><span class="p">(</span><span class="n">icrs_frame</span><span class="p">)</span>
<span class="n">origin_gd1</span><span class="o">.</span><span class="n">transform_to</span><span class="p">(</span><span class="n">icrs_frame</span><span class="p">)</span>
</pre></div>
</div>
</div>
@@ -1615,6 +1701,7 @@ For example, we can transform <code class="docutils literal notranslate"><span c
</div>
</div>
</div>
<p>Notice that the origin of the GD-1 frame maps to <code class="docutils literal notranslate"><span class="pre">ra=200</span></code>, exactly, in ICRS. Thats by design.</p>
</div>
</div>
<div class="section" id="selecting-a-rectangle">
@@ -1631,7 +1718,7 @@ For example, we can transform <code class="docutils literal notranslate"><span c
</div>
</div>
</div>
<p>To represent a rectangle, well use two lists of coordinates and multiply by their units.</p>
<p>To create a rectangle, well use the following function, which takes the lower and upper bounds as parameters.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">make_rectangle</span><span class="p">(</span><span class="n">x1</span><span class="p">,</span> <span class="n">x2</span><span class="p">,</span> <span class="n">y1</span><span class="p">,</span> <span class="n">y2</span><span class="p">):</span>
@@ -1643,6 +1730,7 @@ For example, we can transform <code class="docutils literal notranslate"><span c
</div>
</div>
</div>
<p>The return value is a tuple containing a list of coordinates in <code class="docutils literal notranslate"><span class="pre">phi1</span></code> followed by a list of coordinates in <code class="docutils literal notranslate"><span class="pre">phi2</span></code>.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">phi1_rect</span><span class="p">,</span> <span class="n">phi2_rect</span> <span class="o">=</span> <span class="n">make_rectangle</span><span class="p">(</span>
@@ -1651,13 +1739,11 @@ For example, we can transform <code class="docutils literal notranslate"><span c
</div>
</div>
</div>
<p><code class="docutils literal notranslate"><span class="pre">phi1_rect</span></code> and <code class="docutils literal notranslate"><span class="pre">phi2_rect</span></code> represent the coordinates of the corners of a rectangle in the GD-1 frame.</p>
<p>In order to use them in a Gaia query, we have to convert them to ICRS.</p>
<p><code class="docutils literal notranslate"><span class="pre">phi1_rect</span></code> and <code class="docutils literal notranslate"><span class="pre">phi2_rect</span></code> contains the coordinates of the corners of a rectangle in the GD-1 frame.</p>
<p>In order to use them in a Gaia query, we have to convert them to ICRS. First well put them into a <code class="docutils literal notranslate"><span class="pre">SkyCoord</span></code> object.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">gala.coordinates</span> <span class="k">as</span> <span class="nn">gc</span>
<span class="n">corners</span> <span class="o">=</span> <span class="n">SkyCoord</span><span class="p">(</span><span class="n">phi1</span><span class="o">=</span><span class="n">phi1_rect</span><span class="p">,</span> <span class="n">phi2</span><span class="o">=</span><span class="n">phi2_rect</span><span class="p">,</span> <span class="n">frame</span><span class="o">=</span><span class="n">gd1_frame</span><span class="p">)</span>
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">corners</span> <span class="o">=</span> <span class="n">SkyCoord</span><span class="p">(</span><span class="n">phi1</span><span class="o">=</span><span class="n">phi1_rect</span><span class="p">,</span> <span class="n">phi2</span><span class="o">=</span><span class="n">phi2_rect</span><span class="p">,</span> <span class="n">frame</span><span class="o">=</span><span class="n">gd1_frame</span><span class="p">)</span>
<span class="n">corners</span>
</pre></div>
</div>
@@ -1672,9 +1758,7 @@ For example, we can transform <code class="docutils literal notranslate"><span c
<p>Now we can use <code class="docutils literal notranslate"><span class="pre">transform_to</span></code> to convert to ICRS coordinates.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">astropy.coordinates</span> <span class="k">as</span> <span class="nn">coord</span>
<span class="n">corners_icrs</span> <span class="o">=</span> <span class="n">corners</span><span class="o">.</span><span class="n">transform_to</span><span class="p">(</span><span class="s1">&#39;icrs&#39;</span><span class="p">)</span>
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">corners_icrs</span> <span class="o">=</span> <span class="n">corners</span><span class="o">.</span><span class="n">transform_to</span><span class="p">(</span><span class="s1">&#39;icrs&#39;</span><span class="p">)</span>
<span class="n">corners_icrs</span>
</pre></div>
</div>
@@ -1688,10 +1772,10 @@ For example, we can transform <code class="docutils literal notranslate"><span c
</div>
</div>
</div>
<p>Notice that a rectangle in one coordinate system is not necessarily a rectangle in another. In this example, the result is a polygon.</p>
<p>Notice that a rectangle in one coordinate system is not necessarily a rectangle in another. In this example, the result is a (non-rectangular) polygon.</p>
</div>
<div class="section" id="selecting-a-polygon">
<h2>Selecting a polygon<a class="headerlink" href="#selecting-a-polygon" title="Permalink to this headline"></a></h2>
<div class="section" id="defining-a-polygon">
<h2>Defining a polygon<a class="headerlink" href="#defining-a-polygon" title="Permalink to this headline"></a></h2>
<p>In order to use this polygon as part of an ADQL query, we have to convert it to a string with a comma-separated list of coordinates, as in this example:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">POLYGON(143.65, 20.98, </span>
@@ -1701,7 +1785,52 @@ For example, we can transform <code class="docutils literal notranslate"><span c
<span class="sd">&quot;&quot;&quot;</span>
</pre></div>
</div>
<p>The following function does the job:</p>
<p><code class="docutils literal notranslate"><span class="pre">SkyCoord</span></code> provides <code class="docutils literal notranslate"><span class="pre">to_string</span></code>, which produces a list of strings.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">t</span> <span class="o">=</span> <span class="n">corners_icrs</span><span class="o">.</span><span class="n">to_string</span><span class="p">()</span>
<span class="n">t</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<div class="output text_plain highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>[&#39;146.275 19.2619&#39;,
&#39;135.422 25.8774&#39;,
&#39;141.603 34.3048&#39;,
&#39;152.817 27.1361&#39;,
&#39;146.275 19.2619&#39;]
</pre></div>
</div>
</div>
</div>
<p>We can use the Python string function <code class="docutils literal notranslate"><span class="pre">join</span></code> to join <code class="docutils literal notranslate"><span class="pre">t</span></code> into a single string (with spaces between the pairs):</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">s</span> <span class="o">=</span> <span class="s1">&#39; &#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">t</span><span class="p">)</span>
<span class="n">s</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<div class="output text_plain highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>&#39;146.275 19.2619 135.422 25.8774 141.603 34.3048 152.817 27.1361 146.275 19.2619&#39;
</pre></div>
</div>
</div>
</div>
<p>Thats almost what we need, but we have to replace the spaces with commas.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">s</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39; &#39;</span><span class="p">,</span> <span class="s1">&#39;, &#39;</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<div class="output text_plain highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>&#39;146.275, 19.2619, 135.422, 25.8774, 141.603, 34.3048, 152.817, 27.1361, 146.275, 19.2619&#39;
</pre></div>
</div>
</div>
</div>
<p>The following function combines these steps.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">skycoord_to_string</span><span class="p">(</span><span class="n">skycoord</span><span class="p">):</span>
@@ -1713,9 +1842,7 @@ For example, we can transform <code class="docutils literal notranslate"><span c
</div>
</div>
</div>
<p><code class="docutils literal notranslate"><span class="pre">SkyCoord</span></code> provides <code class="docutils literal notranslate"><span class="pre">to_string</span></code>, which returns a list of strings.</p>
<p>We use <code class="docutils literal notranslate"><span class="pre">join</span></code> to make a single string with spaces between the coordinates.</p>
<p>Then we use <code class="docutils literal notranslate"><span class="pre">replace</span></code> to add commas between the coordinates.</p>
<p>Heres how we use it.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">point_list</span> <span class="o">=</span> <span class="n">skycoord_to_string</span><span class="p">(</span><span class="n">corners_icrs</span><span class="p">)</span>
@@ -1729,18 +1856,38 @@ For example, we can transform <code class="docutils literal notranslate"><span c
</div>
</div>
</div>
<p>Before we can assemble the query, we need <code class="docutils literal notranslate"><span class="pre">columns</span></code> again (as we saw in the previous notebook).</p>
</div>
<div class="section" id="assembling-the-query">
<h2>Assembling the query<a class="headerlink" href="#assembling-the-query" title="Permalink to this headline"></a></h2>
<p>Now were ready to assemble the query.
We need <code class="docutils literal notranslate"><span class="pre">columns</span></code> again (as we saw in the previous lesson).</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">columns</span> <span class="o">=</span> <span class="s1">&#39;source_id, ra, dec, pmra, pmdec, parallax, parallax_error, radial_velocity&#39;</span>
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">columns</span> <span class="o">=</span> <span class="s1">&#39;source_id, ra, dec, pmra, pmdec, parallax&#39;</span>
</pre></div>
</div>
</div>
</div>
<p>Heres the base for the query, with format specifiers for <code class="docutils literal notranslate"><span class="pre">columns</span></code> and <code class="docutils literal notranslate"><span class="pre">point_list</span></code>.</p>
<p>And heres the query base we used in the previous lesson:</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">query_base</span> <span class="o">=</span> <span class="s2">&quot;&quot;&quot;SELECT </span><span class="si">{columns}</span><span class="s2"></span>
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">query3_base</span> <span class="o">=</span> <span class="s2">&quot;&quot;&quot;SELECT </span>
<span class="s2">TOP 10 </span>
<span class="si">{columns}</span><span class="s2"></span>
<span class="s2">FROM gaiadr2.gaia_source</span>
<span class="s2">WHERE parallax &lt; 1</span>
<span class="s2"> AND bp_rp BETWEEN -0.75 AND 2</span>
<span class="s2">&quot;&quot;&quot;</span>
</pre></div>
</div>
</div>
</div>
<p>Now well add a <code class="docutils literal notranslate"><span class="pre">WHERE</span></code> clause to select stars in the polygon we defined.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">query4_base</span> <span class="o">=</span> <span class="s2">&quot;&quot;&quot;SELECT</span>
<span class="s2">TOP 10</span>
<span class="si">{columns}</span><span class="s2"></span>
<span class="s2">FROM gaiadr2.gaia_source</span>
<span class="s2">WHERE parallax &lt; 1</span>
<span class="s2"> AND bp_rp BETWEEN -0.75 AND 2 </span>
@@ -1751,17 +1898,20 @@ For example, we can transform <code class="docutils literal notranslate"><span c
</div>
</div>
</div>
<p>And heres the result:</p>
<p>The query base contains format specifiers for <code class="docutils literal notranslate"><span class="pre">columns</span></code> and <code class="docutils literal notranslate"><span class="pre">point_list</span></code>.</p>
<p>Well use <code class="docutils literal notranslate"><span class="pre">format</span></code> to fill in these values.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">query</span> <span class="o">=</span> <span class="n">query_base</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">columns</span><span class="o">=</span><span class="n">columns</span><span class="p">,</span>
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">query4</span> <span class="o">=</span> <span class="n">query4_base</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">columns</span><span class="o">=</span><span class="n">columns</span><span class="p">,</span>
<span class="n">point_list</span><span class="o">=</span><span class="n">point_list</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">query</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">query4</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>SELECT source_id, ra, dec, pmra, pmdec, parallax, parallax_error, radial_velocity
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>SELECT
TOP 10
source_id, ra, dec, pmra, pmdec
FROM gaiadr2.gaia_source
WHERE parallax &lt; 1
AND bp_rp BETWEEN -0.75 AND 2
@@ -1772,10 +1922,97 @@ WHERE parallax &lt; 1
</div>
</div>
<p>As always, we should take a minute to proof-read the query before we launch it.</p>
<p>The result will be bigger than our previous queries, so it will take a little longer.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">job</span> <span class="o">=</span> <span class="n">Gaia</span><span class="o">.</span><span class="n">launch_job_async</span><span class="p">(</span><span class="n">query</span><span class="p">)</span>
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">job</span> <span class="o">=</span> <span class="n">Gaia</span><span class="o">.</span><span class="n">launch_job_async</span><span class="p">(</span><span class="n">query4</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">job</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>INFO: Query finished. [astroquery.utils.tap.core]
&lt;Table length=10&gt;
name dtype unit description
--------- ------- -------- ------------------------------------------------------------------
source_id int64 Unique source identifier (unique within a particular Data Release)
ra float64 deg Right ascension
dec float64 deg Declination
pmra float64 mas / yr Proper motion in right ascension direction
pmdec float64 mas / yr Proper motion in declination direction
Jobid: 1615815873808O
Phase: COMPLETED
Owner: None
Output file: async_20210315094433.vot
Results: None
</pre></div>
</div>
</div>
</div>
<p>Here are the results.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">results</span> <span class="o">=</span> <span class="n">job</span><span class="o">.</span><span class="n">get_results</span><span class="p">()</span>
<span class="n">results</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<div class="output text_html"><i>Table length=10</i>
<table id="table139807251332016" class="table-striped table-bordered table-condensed">
<thead><tr><th>source_id</th><th>ra</th><th>dec</th><th>pmra</th><th>pmdec</th></tr></thead>
<thead><tr><th></th><th>deg</th><th>deg</th><th>mas / yr</th><th>mas / yr</th></tr></thead>
<thead><tr><th>int64</th><th>float64</th><th>float64</th><th>float64</th><th>float64</th></tr></thead>
<tr><td>637987125186749568</td><td>142.48301935991023</td><td>21.75771616932985</td><td>-2.5168384683875766</td><td>2.941813096629439</td></tr>
<tr><td>638285195917112960</td><td>142.25452941346344</td><td>22.476168171141378</td><td>2.6627020143457996</td><td>-12.165984395577347</td></tr>
<tr><td>638073505568978688</td><td>142.64528557468074</td><td>22.16693224953078</td><td>18.30674739454163</td><td>-7.950659620550862</td></tr>
<tr><td>638086386175786752</td><td>142.57739430926034</td><td>22.22791951401365</td><td>0.9877856720147953</td><td>-2.584105480335548</td></tr>
<tr><td>638049655615392384</td><td>142.58913564478618</td><td>22.110783166677418</td><td>0.24443878227817095</td><td>-4.941079187010136</td></tr>
<tr><td>638267565075964032</td><td>141.81762228999614</td><td>22.375696125322275</td><td>-3.413174589660796</td><td>1.8838892877285924</td></tr>
<tr><td>638028902333511168</td><td>143.18339801317677</td><td>22.2512465812369</td><td>7.848511762712128</td><td>-21.391145547787154</td></tr>
<tr><td>638085767700610432</td><td>142.9347319464589</td><td>22.46244080823965</td><td>-3.6585960944321476</td><td>-12.486419770278376</td></tr>
<tr><td>638299863230178304</td><td>142.26769745823267</td><td>22.640183776884836</td><td>-1.8168370892218297</td><td>1.0537342990941316</td></tr>
<tr><td>637973067758974208</td><td>142.89551292869012</td><td>21.612824100339875</td><td>-8.645166256559063</td><td>-44.41164172204947</td></tr>
</table></div></div>
</div>
<p>Finally, we can remove <code class="docutils literal notranslate"><span class="pre">TOP</span> <span class="pre">10</span></code> run the query again.</p>
<p>The result is bigger than our previous queries, so it will take a little longer.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">query5_base</span> <span class="o">=</span> <span class="s2">&quot;&quot;&quot;SELECT</span>
<span class="si">{columns}</span><span class="s2"></span>
<span class="s2">FROM gaiadr2.gaia_source</span>
<span class="s2">WHERE parallax &lt; 1</span>
<span class="s2"> AND bp_rp BETWEEN -0.75 AND 2 </span>
<span class="s2"> AND 1 = CONTAINS(POINT(ra, dec), </span>
<span class="s2"> POLYGON(</span><span class="si">{point_list}</span><span class="s2">))</span>
<span class="s2">&quot;&quot;&quot;</span>
</pre></div>
</div>
</div>
</div>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">query5</span> <span class="o">=</span> <span class="n">query5_base</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">columns</span><span class="o">=</span><span class="n">columns</span><span class="p">,</span>
<span class="n">point_list</span><span class="o">=</span><span class="n">point_list</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">query5</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>SELECT
source_id, ra, dec, pmra, pmdec
FROM gaiadr2.gaia_source
WHERE parallax &lt; 1
AND bp_rp BETWEEN -0.75 AND 2
AND 1 = CONTAINS(POINT(ra, dec),
POLYGON(146.275, 19.2619, 135.422, 25.8774, 141.603, 34.3048, 152.817, 27.1361, 146.275, 19.2619))
</pre></div>
</div>
</div>
</div>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">job</span> <span class="o">=</span> <span class="n">Gaia</span><span class="o">.</span><span class="n">launch_job_async</span><span class="p">(</span><span class="n">query5</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">job</span><span class="p">)</span>
</pre></div>
</div>
@@ -1783,26 +2020,22 @@ WHERE parallax &lt; 1
<div class="cell_output docutils container">
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>INFO: Query finished. [astroquery.utils.tap.core]
&lt;Table length=140339&gt;
name dtype unit description n_bad
--------------- ------- -------- ------------------------------------------------------------------ ------
source_id int64 Unique source identifier (unique within a particular Data Release) 0
ra float64 deg Right ascension 0
dec float64 deg Declination 0
pmra float64 mas / yr Proper motion in right ascension direction 0
pmdec float64 mas / yr Proper motion in declination direction 0
parallax float64 mas Parallax 0
parallax_error float64 mas Standard error of parallax 0
radial_velocity float64 km / s Radial velocity 139373
Jobid: 1609277169233O
name dtype unit description
--------- ------- -------- ------------------------------------------------------------------
source_id int64 Unique source identifier (unique within a particular Data Release)
ra float64 deg Right ascension
dec float64 deg Declination
pmra float64 mas / yr Proper motion in right ascension direction
pmdec float64 mas / yr Proper motion in declination direction
Jobid: 1615815886707O
Phase: COMPLETED
Owner: None
Output file: async_20201229162609.vot
Output file: async_20210315094446.vot
Results: None
</pre></div>
</div>
</div>
</div>
<p>Here are the results.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">results</span> <span class="o">=</span> <span class="n">job</span><span class="o">.</span><span class="n">get_results</span><span class="p">()</span>
@@ -1833,23 +2066,22 @@ Results: None
</div>
<p>Because the filename ends with <code class="docutils literal notranslate"><span class="pre">fits</span></code>, the table is written in the <a class="reference external" href="https://en.wikipedia.org/wiki/FITS">FITS format</a>, which preserves the metadata associated with the table.</p>
<p>If the file already exists, the <code class="docutils literal notranslate"><span class="pre">overwrite</span></code> argument causes it to be overwritten.</p>
<p>To see how big the file is, we can use <code class="docutils literal notranslate"><span class="pre">ls</span></code> with the <code class="docutils literal notranslate"><span class="pre">-lh</span></code> option, which prints information about the file including its size in human-readable form.</p>
<p>We can use <code class="docutils literal notranslate"><span class="pre">getsize</span></code> to confirm that the file exists and check the size:</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="o">!</span>ls -lh gd1_results.fits
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">os.path</span> <span class="kn">import</span> <span class="n">getsize</span>
<span class="n">MB</span> <span class="o">=</span> <span class="mi">1024</span> <span class="o">*</span> <span class="mi">1024</span>
<span class="n">getsize</span><span class="p">(</span><span class="n">filename</span><span class="p">)</span> <span class="o">/</span> <span class="n">MB</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>-rw-rw-r-- 1 downey downey 8.6M Dec 29 11:47 gd1_results.fits
<div class="output text_plain highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>5.36407470703125
</pre></div>
</div>
</div>
</div>
<p>The file is about 8.6 MB. If you are using Windows, <code class="docutils literal notranslate"><span class="pre">ls</span></code> might not work; in that case, try:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>!dir gd1_results.fits
</pre></div>
</div>
</div>
<div class="section" id="summary">
<h2>Summary<a class="headerlink" href="#summary" title="Permalink to this headline"></a></h2>
@@ -1860,7 +2092,7 @@ Results: None
<h2>Best practices<a class="headerlink" href="#best-practices" title="Permalink to this headline"></a></h2>
<ul class="simple">
<li><p>For measurements with units, use <code class="docutils literal notranslate"><span class="pre">Quantity</span></code> objects that represent units explicitly and check for errors.</p></li>
<li><p>Use the <code class="docutils literal notranslate"><span class="pre">format</span></code> function to compose queries; it is often faster and less error-prone.</p></li>
<li><p>Use the <code class="docutils literal notranslate"><span class="pre">format</span></code> function to compose queries; code written this way is easier to read and less error-prone.</p></li>
<li><p>Develop queries incrementally: start with something simple, test it, and add a little bit at a time.</p></li>
<li><p>Once you have a query working, save the data in a local file. If you shut down the notebook and come back to it later, you can reload the file; you dont have to run the query again.</p></li>
</ul>
@@ -1895,8 +2127,8 @@ Results: None
<div class='prev-next-bottom'>
<a class='left-prev' id="prev-link" href="01_query.html" title="previous page">Queries</a>
<a class='right-next' id="next-link" href="03_motion.html" title="next page">Proper Motion</a>
<a class='left-prev' id="prev-link" href="01_query.html" title="previous page">1. Queries</a>
<a class='right-next' id="next-link" href="03_motion.html" title="next page">3. Proper Motion</a>
</div>
<footer class="footer mt-5 mt-md-0">

File diff suppressed because it is too large Load Diff

View File

@@ -5,7 +5,7 @@
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Transformation and Selection &#8212; Astronomical Data in Python</title>
<title>4. Transformation and Selection &#8212; Astronomical Data in Python</title>
<link rel="stylesheet" href="_static/css/index.d431a4ee1c1efae0e38bdfebc22debff.css">
@@ -58,7 +58,7 @@
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="next" title="Joining Tables" href="05_join.html" />
<link rel="prev" title="Proper Motion" href="03_motion.html" />
<link rel="prev" title="3. Proper Motion" href="03_motion.html" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="docsearch:language" content="en" />
@@ -99,22 +99,22 @@
<ul class="current nav sidenav_l1">
<li class="toctree-l1">
<a class="reference internal" href="01_query.html">
Queries
1. Queries
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="02_coords.html">
Coordinates and units
2. Coordinates and Units
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="03_motion.html">
Proper Motion
3. Proper Motion
</a>
</li>
<li class="toctree-l1 current active">
<a class="current reference internal" href="#">
Transformation and Selection
4. Transformation and Selection
</a>
</li>
<li class="toctree-l1">
@@ -262,6 +262,11 @@
<a class="reference internal nav-link" href="#assembling-the-query">
Assembling the query
</a>
</li>
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#selecting-proper-motion">
Selecting proper motion
</a>
<ul class="nav section-nav flex-column">
<li class="toc-h3 nav-item toc-entry">
<a class="reference internal nav-link" href="#exercise">
@@ -312,21 +317,18 @@
<div>
<div class="section" id="transformation-and-selection">
<h1>Transformation and Selection<a class="headerlink" href="#transformation-and-selection" title="Permalink to this headline"></a></h1>
<p>This is the fourth in a series of notebooks related to astronomy data.</p>
<p>As a running example, we are replicating parts of the analysis in a recent paper, <a class="reference external" href="https://arxiv.org/abs/1805.00425">Off the beaten path: Gaia reveals GD-1 stars outside of the main stream</a>” by Adrian M. Price-Whelan and Ana Bonaca.</p>
<p>In the first lesson, we wrote ADQL queries and used them to select and download data from the Gaia server.</p>
<p>In the second lesson, we write a query to select stars from the region of the sky where we expect GD-1 to be, and save the results in a FITS file.</p>
<p>In the third lesson, we read that data back and identified stars with the proper motion we expect for GD-1.</p>
<h1>4. Transformation and Selection<a class="headerlink" href="#transformation-and-selection" title="Permalink to this headline"></a></h1>
<p>In the previous lesson, we identified stars with the proper motion we expect for GD-1.</p>
<p>Now well do the same selection in an ADQL query, which will make it possible to work with a larger region of the sky and still download less data.</p>
<div class="section" id="outline">
<h2>Outline<a class="headerlink" href="#outline" title="Permalink to this headline"></a></h2>
<p>Here are the steps in this lesson:</p>
<ol class="simple">
<li><p>Using data from the previous lesson, well identify the values of proper motion for stars likely to be in GD-1.</p></li>
<li><p>Then well compose an ADQL query that selects stars based on proper motion, so we can download only the data we need.</p></li>
<li><p>Well also see how to write the results to a CSV file.</p></li>
</ol>
<p>That will make it possible to search a bigger region of the sky in a single query.</p>
<p>That will make it possible to search a bigger region of the sky in a single query.
Well also see how to write the results to a CSV file.</p>
<p>After completing this lesson, you should be able to</p>
<ul class="simple">
<li><p>Transform proper motions from one frame to another.</p></li>
@@ -343,7 +345,7 @@
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">os</span>
<span class="kn">from</span> <span class="nn">wget</span> <span class="kn">import</span> <span class="n">download</span>
<span class="n">filename</span> <span class="o">=</span> <span class="s1">&#39;gd1_dataframe.hdf5&#39;</span>
<span class="n">filename</span> <span class="o">=</span> <span class="s1">&#39;gd1_data.hdf&#39;</span>
<span class="n">path</span> <span class="o">=</span> <span class="s1">&#39;https://github.com/AllenDowney/AstronomicalData/raw/main/data/&#39;</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">filename</span><span class="p">):</span>
@@ -405,6 +407,29 @@
</div>
</div>
</div>
<p>Since well need to plot proper motion several times, well use the following function.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">matplotlib.pyplot</span> <span class="k">as</span> <span class="nn">plt</span>
<span class="k">def</span> <span class="nf">plot_proper_motion</span><span class="p">(</span><span class="n">df</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;Plot proper motion.</span>
<span class="sd"> </span>
<span class="sd"> df: DataFrame with `pm_phi1` and `pm_phi2`</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">x</span> <span class="o">=</span> <span class="n">df</span><span class="p">[</span><span class="s1">&#39;pm_phi1&#39;</span><span class="p">]</span>
<span class="n">y</span> <span class="o">=</span> <span class="n">df</span><span class="p">[</span><span class="s1">&#39;pm_phi2&#39;</span><span class="p">]</span>
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="s1">&#39;ko&#39;</span><span class="p">,</span> <span class="n">markersize</span><span class="o">=</span><span class="mf">0.3</span><span class="p">,</span> <span class="n">alpha</span><span class="o">=</span><span class="mf">0.3</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">xlabel</span><span class="p">(</span><span class="s1">&#39;Proper motion phi1 (GD1 frame)&#39;</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">ylabel</span><span class="p">(</span><span class="s1">&#39;Proper motion phi2 (GD1 frame)&#39;</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">xlim</span><span class="p">(</span><span class="o">-</span><span class="mi">12</span><span class="p">,</span> <span class="mi">8</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">ylim</span><span class="p">(</span><span class="o">-</span><span class="mi">10</span><span class="p">,</span> <span class="mi">10</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<p>The following figure shows:</p>
<ul class="simple">
<li><p>Proper motion for the stars we selected along the center line of GD-1,</p></li>
@@ -413,40 +438,30 @@
</ul>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">matplotlib.pyplot</span> <span class="k">as</span> <span class="nn">plt</span>
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">plot_proper_motion</span><span class="p">(</span><span class="n">centerline_df</span><span class="p">)</span>
<span class="n">pm1</span> <span class="o">=</span> <span class="n">centerline_df</span><span class="p">[</span><span class="s1">&#39;pm_phi1&#39;</span><span class="p">]</span>
<span class="n">pm2</span> <span class="o">=</span> <span class="n">centerline_df</span><span class="p">[</span><span class="s1">&#39;pm_phi2&#39;</span><span class="p">]</span>
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">pm1</span><span class="p">,</span> <span class="n">pm2</span><span class="p">,</span> <span class="s1">&#39;ko&#39;</span><span class="p">,</span> <span class="n">markersize</span><span class="o">=</span><span class="mf">0.3</span><span class="p">,</span> <span class="n">alpha</span><span class="o">=</span><span class="mf">0.3</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">pm1_rect</span><span class="p">,</span> <span class="n">pm2_rect</span><span class="p">)</span>
<span class="n">pm1</span> <span class="o">=</span> <span class="n">selected_df</span><span class="p">[</span><span class="s1">&#39;pm_phi1&#39;</span><span class="p">]</span>
<span class="n">pm2</span> <span class="o">=</span> <span class="n">selected_df</span><span class="p">[</span><span class="s1">&#39;pm_phi2&#39;</span><span class="p">]</span>
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">pm1</span><span class="p">,</span> <span class="n">pm2</span><span class="p">,</span> <span class="s1">&#39;gx&#39;</span><span class="p">,</span> <span class="n">markersize</span><span class="o">=</span><span class="mf">0.3</span><span class="p">,</span> <span class="n">alpha</span><span class="o">=</span><span class="mf">0.3</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">pm1_rect</span><span class="p">,</span> <span class="n">pm2_rect</span><span class="p">,</span> <span class="s1">&#39;-&#39;</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">xlabel</span><span class="p">(</span><span class="s1">&#39;Proper motion phi1 (GD1 frame)&#39;</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">ylabel</span><span class="p">(</span><span class="s1">&#39;Proper motion phi2 (GD1 frame)&#39;</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">xlim</span><span class="p">(</span><span class="o">-</span><span class="mi">12</span><span class="p">,</span> <span class="mi">8</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">ylim</span><span class="p">(</span><span class="o">-</span><span class="mi">10</span><span class="p">,</span> <span class="mi">10</span><span class="p">);</span>
<span class="n">x</span> <span class="o">=</span> <span class="n">selected_df</span><span class="p">[</span><span class="s1">&#39;pm_phi1&#39;</span><span class="p">]</span>
<span class="n">y</span> <span class="o">=</span> <span class="n">selected_df</span><span class="p">[</span><span class="s1">&#39;pm_phi2&#39;</span><span class="p">]</span>
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="s1">&#39;gx&#39;</span><span class="p">,</span> <span class="n">markersize</span><span class="o">=</span><span class="mf">0.3</span><span class="p">,</span> <span class="n">alpha</span><span class="o">=</span><span class="mf">0.3</span><span class="p">);</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<img alt="_images/04_select_13_0.png" src="_images/04_select_13_0.png" />
<img alt="_images/04_select_15_0.png" src="_images/04_select_15_0.png" />
</div>
</div>
<p>Now well make the same plot using proper motions in the ICRS frame, which are stored in columns <code class="docutils literal notranslate"><span class="pre">pmra</span></code> and <code class="docutils literal notranslate"><span class="pre">pmdec</span></code>.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">pm1</span> <span class="o">=</span> <span class="n">centerline_df</span><span class="p">[</span><span class="s1">&#39;pmra&#39;</span><span class="p">]</span>
<span class="n">pm2</span> <span class="o">=</span> <span class="n">centerline_df</span><span class="p">[</span><span class="s1">&#39;pmdec&#39;</span><span class="p">]</span>
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">pm1</span><span class="p">,</span> <span class="n">pm2</span><span class="p">,</span> <span class="s1">&#39;ko&#39;</span><span class="p">,</span> <span class="n">markersize</span><span class="o">=</span><span class="mf">0.3</span><span class="p">,</span> <span class="n">alpha</span><span class="o">=</span><span class="mf">0.3</span><span class="p">)</span>
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">x</span> <span class="o">=</span> <span class="n">centerline_df</span><span class="p">[</span><span class="s1">&#39;pmra&#39;</span><span class="p">]</span>
<span class="n">y</span> <span class="o">=</span> <span class="n">centerline_df</span><span class="p">[</span><span class="s1">&#39;pmdec&#39;</span><span class="p">]</span>
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="s1">&#39;ko&#39;</span><span class="p">,</span> <span class="n">markersize</span><span class="o">=</span><span class="mf">0.3</span><span class="p">,</span> <span class="n">alpha</span><span class="o">=</span><span class="mf">0.3</span><span class="p">)</span>
<span class="n">pm1</span> <span class="o">=</span> <span class="n">selected_df</span><span class="p">[</span><span class="s1">&#39;pmra&#39;</span><span class="p">]</span>
<span class="n">pm2</span> <span class="o">=</span> <span class="n">selected_df</span><span class="p">[</span><span class="s1">&#39;pmdec&#39;</span><span class="p">]</span>
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">pm1</span><span class="p">,</span> <span class="n">pm2</span><span class="p">,</span> <span class="s1">&#39;gx&#39;</span><span class="p">,</span> <span class="n">markersize</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">alpha</span><span class="o">=</span><span class="mf">0.3</span><span class="p">)</span>
<span class="n">x</span> <span class="o">=</span> <span class="n">selected_df</span><span class="p">[</span><span class="s1">&#39;pmra&#39;</span><span class="p">]</span>
<span class="n">y</span> <span class="o">=</span> <span class="n">selected_df</span><span class="p">[</span><span class="s1">&#39;pmdec&#39;</span><span class="p">]</span>
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="s1">&#39;gx&#39;</span><span class="p">,</span> <span class="n">markersize</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">alpha</span><span class="o">=</span><span class="mf">0.3</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">xlabel</span><span class="p">(</span><span class="s1">&#39;Proper motion ra (ICRS frame)&#39;</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">ylabel</span><span class="p">(</span><span class="s1">&#39;Proper motion dec (ICRS frame)&#39;</span><span class="p">)</span>
@@ -457,7 +472,7 @@
</div>
</div>
<div class="cell_output docutils container">
<img alt="_images/04_select_15_0.png" src="_images/04_select_15_0.png" />
<img alt="_images/04_select_17_0.png" src="_images/04_select_17_0.png" />
</div>
</div>
<p>The proper motions of the selected stars are more spread out in this frame, which is why it was preferable to do the selection in the GD-1 frame.</p>
@@ -498,7 +513,7 @@
</div>
</div>
<div class="cell_output docutils container">
<div class="output text_plain highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>&lt;scipy.spatial.qhull.ConvexHull at 0x7fa7c4c03a90&gt;
<div class="output text_plain highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>&lt;scipy.spatial.qhull.ConvexHull at 0x7fb34c626a60&gt;
</pre></div>
</div>
</div>
@@ -558,13 +573,13 @@
<p>The following figure shows proper motion in ICRS again, along with the convex hull we just computed.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">pm1</span> <span class="o">=</span> <span class="n">centerline_df</span><span class="p">[</span><span class="s1">&#39;pmra&#39;</span><span class="p">]</span>
<span class="n">pm2</span> <span class="o">=</span> <span class="n">centerline_df</span><span class="p">[</span><span class="s1">&#39;pmdec&#39;</span><span class="p">]</span>
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">pm1</span><span class="p">,</span> <span class="n">pm2</span><span class="p">,</span> <span class="s1">&#39;ko&#39;</span><span class="p">,</span> <span class="n">markersize</span><span class="o">=</span><span class="mf">0.3</span><span class="p">,</span> <span class="n">alpha</span><span class="o">=</span><span class="mf">0.3</span><span class="p">)</span>
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">x</span> <span class="o">=</span> <span class="n">centerline_df</span><span class="p">[</span><span class="s1">&#39;pmra&#39;</span><span class="p">]</span>
<span class="n">y</span> <span class="o">=</span> <span class="n">centerline_df</span><span class="p">[</span><span class="s1">&#39;pmdec&#39;</span><span class="p">]</span>
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="s1">&#39;ko&#39;</span><span class="p">,</span> <span class="n">markersize</span><span class="o">=</span><span class="mf">0.3</span><span class="p">,</span> <span class="n">alpha</span><span class="o">=</span><span class="mf">0.3</span><span class="p">)</span>
<span class="n">pm1</span> <span class="o">=</span> <span class="n">selected_df</span><span class="p">[</span><span class="s1">&#39;pmra&#39;</span><span class="p">]</span>
<span class="n">pm2</span> <span class="o">=</span> <span class="n">selected_df</span><span class="p">[</span><span class="s1">&#39;pmdec&#39;</span><span class="p">]</span>
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">pm1</span><span class="p">,</span> <span class="n">pm2</span><span class="p">,</span> <span class="s1">&#39;gx&#39;</span><span class="p">,</span> <span class="n">markersize</span><span class="o">=</span><span class="mf">0.3</span><span class="p">,</span> <span class="n">alpha</span><span class="o">=</span><span class="mf">0.3</span><span class="p">)</span>
<span class="n">x</span> <span class="o">=</span> <span class="n">selected_df</span><span class="p">[</span><span class="s1">&#39;pmra&#39;</span><span class="p">]</span>
<span class="n">y</span> <span class="o">=</span> <span class="n">selected_df</span><span class="p">[</span><span class="s1">&#39;pmdec&#39;</span><span class="p">]</span>
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="s1">&#39;gx&#39;</span><span class="p">,</span> <span class="n">markersize</span><span class="o">=</span><span class="mf">0.3</span><span class="p">,</span> <span class="n">alpha</span><span class="o">=</span><span class="mf">0.3</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">pmra_poly</span><span class="p">,</span> <span class="n">pmdec_poly</span><span class="p">)</span>
@@ -577,7 +592,7 @@
</div>
</div>
<div class="cell_output docutils container">
<img alt="_images/04_select_29_0.png" src="_images/04_select_29_0.png" />
<img alt="_images/04_select_31_0.png" src="_images/04_select_31_0.png" />
</div>
</div>
<p>So <code class="docutils literal notranslate"><span class="pre">pm_vertices</span></code> represents the polygon we want to select.
@@ -585,10 +600,10 @@ The next step is to use it as part of an ADQL query.</p>
</div>
<div class="section" id="assembling-the-query">
<h2>Assembling the query<a class="headerlink" href="#assembling-the-query" title="Permalink to this headline"></a></h2>
<p>Heres the base string we used for the query in the previous lesson.</p>
<p>In Lesson 2 we used the following query to select stars in a polygonal region.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">query_base</span> <span class="o">=</span> <span class="s2">&quot;&quot;&quot;SELECT </span>
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">query5_base</span> <span class="o">=</span> <span class="s2">&quot;&quot;&quot;SELECT</span>
<span class="si">{columns}</span><span class="s2"></span>
<span class="s2">FROM gaiadr2.gaia_source</span>
<span class="s2">WHERE parallax &lt; 1</span>
@@ -600,11 +615,111 @@ The next step is to use it as part of an ADQL query.</p>
</div>
</div>
</div>
<p>And here are the changes well make in this lesson:</p>
<p>In this lesson well make two changes:</p>
<ol class="simple">
<li><p>We will add another clause to select stars whose proper motion is in the polygon we just computed, <code class="docutils literal notranslate"><span class="pre">pm_vertices</span></code>.</p></li>
<li><p>We will select stars with coordinates in a larger region.</p></li>
<li><p>Well select stars with coordinates in a larger region.</p></li>
<li><p>Well add another clause to select stars whose proper motion is in the polygon we just computed, <code class="docutils literal notranslate"><span class="pre">pm_vertices</span></code>.</p></li>
</ol>
<p>Here are the coordinates of the larger rectangle in the GD-1 frame.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">astropy.units</span> <span class="k">as</span> <span class="nn">u</span>
<span class="n">phi1_min</span> <span class="o">=</span> <span class="o">-</span><span class="mi">70</span> <span class="o">*</span> <span class="n">u</span><span class="o">.</span><span class="n">degree</span>
<span class="n">phi1_max</span> <span class="o">=</span> <span class="o">-</span><span class="mi">20</span> <span class="o">*</span> <span class="n">u</span><span class="o">.</span><span class="n">degree</span>
<span class="n">phi2_min</span> <span class="o">=</span> <span class="o">-</span><span class="mi">5</span> <span class="o">*</span> <span class="n">u</span><span class="o">.</span><span class="n">degree</span>
<span class="n">phi2_max</span> <span class="o">=</span> <span class="mi">5</span> <span class="o">*</span> <span class="n">u</span><span class="o">.</span><span class="n">degree</span>
</pre></div>
</div>
</div>
</div>
<p>We selected these bounds by trial and error, defining the largest region we can process in a single query.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">phi1_rect</span><span class="p">,</span> <span class="n">phi2_rect</span> <span class="o">=</span> <span class="n">make_rectangle</span><span class="p">(</span>
<span class="n">phi1_min</span><span class="p">,</span> <span class="n">phi1_max</span><span class="p">,</span> <span class="n">phi2_min</span><span class="p">,</span> <span class="n">phi2_max</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<p>Heres how we transform it to ICRS, as we saw in Lesson 2.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">gala.coordinates</span> <span class="kn">import</span> <span class="n">GD1Koposov10</span>
<span class="kn">from</span> <span class="nn">astropy.coordinates</span> <span class="kn">import</span> <span class="n">SkyCoord</span>
<span class="n">gd1_frame</span> <span class="o">=</span> <span class="n">GD1Koposov10</span><span class="p">()</span>
<span class="n">corners</span> <span class="o">=</span> <span class="n">SkyCoord</span><span class="p">(</span><span class="n">phi1</span><span class="o">=</span><span class="n">phi1_rect</span><span class="p">,</span>
<span class="n">phi2</span><span class="o">=</span><span class="n">phi2_rect</span><span class="p">,</span>
<span class="n">frame</span><span class="o">=</span><span class="n">gd1_frame</span><span class="p">)</span>
<span class="n">corners_icrs</span> <span class="o">=</span> <span class="n">corners</span><span class="o">.</span><span class="n">transform_to</span><span class="p">(</span><span class="s1">&#39;icrs&#39;</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<p>To use <code class="docutils literal notranslate"><span class="pre">corners_icrs</span></code> as part of an ADQL query, we have to convert it to a string.<br />
Heres the function from Lesson 2 we used to do that.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">skycoord_to_string</span><span class="p">(</span><span class="n">skycoord</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;Convert SkyCoord to string.&quot;&quot;&quot;</span>
<span class="n">t</span> <span class="o">=</span> <span class="n">skycoord</span><span class="o">.</span><span class="n">to_string</span><span class="p">()</span>
<span class="n">s</span> <span class="o">=</span> <span class="s1">&#39; &#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">t</span><span class="p">)</span>
<span class="k">return</span> <span class="n">s</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39; &#39;</span><span class="p">,</span> <span class="s1">&#39;, &#39;</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">point_list</span> <span class="o">=</span> <span class="n">skycoord_to_string</span><span class="p">(</span><span class="n">corners_icrs</span><span class="p">)</span>
<span class="n">point_list</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<div class="output text_plain highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>&#39;135.306, 8.39862, 126.51, 13.4449, 163.017, 54.2424, 172.933, 46.4726, 135.306, 8.39862&#39;
</pre></div>
</div>
</div>
</div>
<p>Here again are the columns we want to select.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">columns</span> <span class="o">=</span> <span class="s1">&#39;source_id, ra, dec, pmra, pmdec, parallax&#39;</span>
</pre></div>
</div>
</div>
</div>
<p>Now we have everything we need to assemble the query.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">query5</span> <span class="o">=</span> <span class="n">query5_base</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">columns</span><span class="o">=</span><span class="n">columns</span><span class="p">,</span>
<span class="n">point_list</span><span class="o">=</span><span class="n">point_list</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">query5</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>SELECT
source_id, ra, dec, pmra, pmdec, parallax
FROM gaiadr2.gaia_source
WHERE parallax &lt; 1
AND bp_rp BETWEEN -0.75 AND 2
AND 1 = CONTAINS(POINT(ra, dec),
POLYGON(135.306, 8.39862, 126.51, 13.4449, 163.017, 54.2424, 172.933, 46.4726, 135.306, 8.39862))
</pre></div>
</div>
</div>
</div>
<p>But dont try to run that query.
Because it selects a larger region, there are too many stars to handle in a single query.
Until we select by proper motion, that is.</p>
</div>
<div class="section" id="selecting-proper-motion">
<h2>Selecting proper motion<a class="headerlink" href="#selecting-proper-motion" title="Permalink to this headline"></a></h2>
<p>Now were ready to add a <code class="docutils literal notranslate"><span class="pre">WHERE</span></code> clause to select stars whose proper motion falls in the polygon defined by <code class="docutils literal notranslate"><span class="pre">pm_vertices</span></code>.</p>
<p>To use <code class="docutils literal notranslate"><span class="pre">pm_vertices</span></code> as part of an ADQL query, we have to convert it to a string.
Using <code class="docutils literal notranslate"><span class="pre">flatten</span></code> and <code class="docutils literal notranslate"><span class="pre">array2string</span></code>, we can almost get the format we need.</p>
<div class="cell docutils container">
@@ -636,92 +751,14 @@ Using <code class="docutils literal notranslate"><span class="pre">flatten</span
</div>
</div>
</div>
<p>Well add this string to the query soon, but first lets compute the other polygon, the one that specifies the region of the sky we want.</p>
<p>Here are the coordinates of the rectangle well select, in the GD-1 frame.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">astropy.units</span> <span class="k">as</span> <span class="nn">u</span>
<span class="n">phi1_min</span> <span class="o">=</span> <span class="o">-</span><span class="mi">70</span> <span class="o">*</span> <span class="n">u</span><span class="o">.</span><span class="n">degree</span>
<span class="n">phi1_max</span> <span class="o">=</span> <span class="o">-</span><span class="mi">20</span> <span class="o">*</span> <span class="n">u</span><span class="o">.</span><span class="n">degree</span>
<span class="n">phi2_min</span> <span class="o">=</span> <span class="o">-</span><span class="mi">5</span> <span class="o">*</span> <span class="n">u</span><span class="o">.</span><span class="n">degree</span>
<span class="n">phi2_max</span> <span class="o">=</span> <span class="mi">5</span> <span class="o">*</span> <span class="n">u</span><span class="o">.</span><span class="n">degree</span>
</pre></div>
</div>
</div>
</div>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">phi1_rect</span><span class="p">,</span> <span class="n">phi2_rect</span> <span class="o">=</span> <span class="n">make_rectangle</span><span class="p">(</span>
<span class="n">phi1_min</span><span class="p">,</span> <span class="n">phi1_max</span><span class="p">,</span> <span class="n">phi2_min</span><span class="p">,</span> <span class="n">phi2_max</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<p>Heres how we transform it to ICRS, as we saw in the previous lesson.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">gala.coordinates</span> <span class="kn">import</span> <span class="n">GD1Koposov10</span>
<span class="kn">from</span> <span class="nn">astropy.coordinates</span> <span class="kn">import</span> <span class="n">SkyCoord</span>
<span class="n">corners</span> <span class="o">=</span> <span class="n">SkyCoord</span><span class="p">(</span><span class="n">phi1</span><span class="o">=</span><span class="n">phi1_rect</span><span class="p">,</span>
<span class="n">phi2</span><span class="o">=</span><span class="n">phi2_rect</span><span class="p">,</span>
<span class="n">frame</span><span class="o">=</span><span class="n">GD1Koposov10</span><span class="p">)</span>
<span class="n">corners_icrs</span> <span class="o">=</span> <span class="n">corners</span><span class="o">.</span><span class="n">transform_to</span><span class="p">(</span><span class="s1">&#39;icrs&#39;</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<p>To use <code class="docutils literal notranslate"><span class="pre">corners_icrs</span></code> as part of an ADQL query, we have to convert it to a string. Heres how we do that, as we saw in the previous lesson.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">skycoord_to_string</span><span class="p">(</span><span class="n">skycoord</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;Convert SkyCoord to string.&quot;&quot;&quot;</span>
<span class="n">t</span> <span class="o">=</span> <span class="n">skycoord</span><span class="o">.</span><span class="n">to_string</span><span class="p">()</span>
<span class="n">s</span> <span class="o">=</span> <span class="s1">&#39; &#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">t</span><span class="p">)</span>
<span class="k">return</span> <span class="n">s</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39; &#39;</span><span class="p">,</span> <span class="s1">&#39;, &#39;</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">point_list</span> <span class="o">=</span> <span class="n">skycoord_to_string</span><span class="p">(</span><span class="n">corners_icrs</span><span class="p">)</span>
<span class="n">point_list</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<div class="output text_plain highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>&#39;135.306, 8.39862, 126.51, 13.4449, 163.017, 54.2424, 172.933, 46.4726, 135.306, 8.39862&#39;
</pre></div>
</div>
</div>
</div>
<p>Now we have everything we need to assemble the query.
Heres the base query from the previous lesson again:</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">query_base</span> <span class="o">=</span> <span class="s2">&quot;&quot;&quot;SELECT </span>
<span class="si">{columns}</span><span class="s2"></span>
<span class="s2">FROM gaiadr2.gaia_source</span>
<span class="s2">WHERE parallax &lt; 1</span>
<span class="s2"> AND bp_rp BETWEEN -0.75 AND 2 </span>
<span class="s2"> AND 1 = CONTAINS(POINT(ra, dec), </span>
<span class="s2"> POLYGON(</span><span class="si">{point_list}</span><span class="s2">))</span>
<span class="s2">&quot;&quot;&quot;</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="exercise">
<h3>Exercise<a class="headerlink" href="#exercise" title="Permalink to this headline"></a></h3>
<p>Modify <code class="docutils literal notranslate"><span class="pre">query_base</span></code> by adding a new clause to select stars whose coordinates of proper motion, <code class="docutils literal notranslate"><span class="pre">pmra</span></code> and <code class="docutils literal notranslate"><span class="pre">pmdec</span></code>, fall within the polygon defined by <code class="docutils literal notranslate"><span class="pre">pm_point_list</span></code>.</p>
<p>Define <code class="docutils literal notranslate"><span class="pre">query6_base</span></code>, starting with <code class="docutils literal notranslate"><span class="pre">query5_base</span></code> and adding a new clause to select stars whose coordinates of proper motion, <code class="docutils literal notranslate"><span class="pre">pmra</span></code> and <code class="docutils literal notranslate"><span class="pre">pmdec</span></code>, fall within the polygon defined by <code class="docutils literal notranslate"><span class="pre">pm_point_list</span></code>.</p>
<div class="cell tag_hide-cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="c1"># Solution</span>
<span class="n">query_base</span> <span class="o">=</span> <span class="s2">&quot;&quot;&quot;SELECT </span>
<span class="n">query6_base</span> <span class="o">=</span> <span class="s2">&quot;&quot;&quot;SELECT </span>
<span class="si">{columns}</span><span class="s2"></span>
<span class="s2">FROM gaiadr2.gaia_source</span>
<span class="s2">WHERE parallax &lt; 1</span>
@@ -735,32 +772,24 @@ Heres the base query from the previous lesson again:</p>
</div>
</div>
</div>
<p>Here again are the columns we want to select.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">columns</span> <span class="o">=</span> <span class="s1">&#39;source_id, ra, dec, pmra, pmdec, parallax, radial_velocity&#39;</span>
</pre></div>
</div>
</div>
</div>
</div>
<div class="section" id="id1">
<h3>Exercise<a class="headerlink" href="#id1" title="Permalink to this headline"></a></h3>
<p>Use <code class="docutils literal notranslate"><span class="pre">format</span></code> to format <code class="docutils literal notranslate"><span class="pre">query_base</span></code> and define <code class="docutils literal notranslate"><span class="pre">query</span></code>, filling in the values of <code class="docutils literal notranslate"><span class="pre">columns</span></code>, <code class="docutils literal notranslate"><span class="pre">point_list</span></code>, and <code class="docutils literal notranslate"><span class="pre">pm_point_list</span></code>.</p>
<p>Use <code class="docutils literal notranslate"><span class="pre">format</span></code> to format <code class="docutils literal notranslate"><span class="pre">query6_base</span></code> and define <code class="docutils literal notranslate"><span class="pre">query6</span></code>, filling in the values of <code class="docutils literal notranslate"><span class="pre">columns</span></code>, <code class="docutils literal notranslate"><span class="pre">point_list</span></code>, and <code class="docutils literal notranslate"><span class="pre">pm_point_list</span></code>.</p>
<div class="cell tag_hide-cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="c1"># Solution</span>
<span class="n">query</span> <span class="o">=</span> <span class="n">query_base</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">columns</span><span class="o">=</span><span class="n">columns</span><span class="p">,</span>
<span class="n">query6</span> <span class="o">=</span> <span class="n">query6_base</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">columns</span><span class="o">=</span><span class="n">columns</span><span class="p">,</span>
<span class="n">point_list</span><span class="o">=</span><span class="n">point_list</span><span class="p">,</span>
<span class="n">pm_point_list</span><span class="o">=</span><span class="n">pm_point_list</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">query</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">query6</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>SELECT
source_id, ra, dec, pmra, pmdec, parallax, radial_velocity
source_id, ra, dec, pmra, pmdec, parallax
FROM gaiadr2.gaia_source
WHERE parallax &lt; 1
AND bp_rp BETWEEN -0.75 AND 2
@@ -777,7 +806,7 @@ WHERE parallax &lt; 1
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">astroquery.gaia</span> <span class="kn">import</span> <span class="n">Gaia</span>
<span class="n">job</span> <span class="o">=</span> <span class="n">Gaia</span><span class="o">.</span><span class="n">launch_job_async</span><span class="p">(</span><span class="n">query</span><span class="p">)</span>
<span class="n">job</span> <span class="o">=</span> <span class="n">Gaia</span><span class="o">.</span><span class="n">launch_job_async</span><span class="p">(</span><span class="n">query6</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">job</span><span class="p">)</span>
</pre></div>
</div>
@@ -785,19 +814,18 @@ WHERE parallax &lt; 1
<div class="cell_output docutils container">
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>INFO: Query finished. [astroquery.utils.tap.core]
&lt;Table length=7345&gt;
name dtype unit description n_bad
--------------- ------- -------- ------------------------------------------------------------------ -----
source_id int64 Unique source identifier (unique within a particular Data Release) 0
ra float64 deg Right ascension 0
dec float64 deg Declination 0
pmra float64 mas / yr Proper motion in right ascension direction 0
pmdec float64 mas / yr Proper motion in declination direction 0
parallax float64 mas Parallax 0
radial_velocity float64 km / s Radial velocity 7294
Jobid: 1609278364817O
name dtype unit description
--------- ------- -------- ------------------------------------------------------------------
source_id int64 Unique source identifier (unique within a particular Data Release)
ra float64 deg Right ascension
dec float64 deg Declination
pmra float64 mas / yr Proper motion in right ascension direction
pmdec float64 mas / yr Proper motion in declination direction
parallax float64 mas Parallax
Jobid: 1610562623566O
Phase: COMPLETED
Owner: None
Output file: async_20201229164604.vot
Output file: async_20210113133023.vot
Results: None
</pre></div>
</div>
@@ -817,6 +845,7 @@ Results: None
</div>
</div>
</div>
<p>We call the results <code class="docutils literal notranslate"><span class="pre">candidate_table</span></code> because it contains stars that are good candidates for GD-1.</p>
</div>
</div>
<div class="section" id="plotting-one-more-time">
@@ -834,11 +863,12 @@ Results: None
</div>
</div>
<div class="cell_output docutils container">
<img alt="_images/04_select_58_0.png" src="_images/04_select_58_0.png" />
<img alt="_images/04_select_64_0.png" src="_images/04_select_64_0.png" />
</div>
</div>
<p>Here we can see why it was useful to transform these coordinates. In ICRS, it is more difficult to identity the stars near the centerline of GD-1.</p>
<p>So, before we move on to the next step, lets collect the code we used to transform the coordinates and make a Pandas <code class="docutils literal notranslate"><span class="pre">DataFrame</span></code>:</p>
<p>So lets transform the results back to the GD-1 frame.</p>
<p>Heres the code we used to transform the coordinates and make a Pandas <code class="docutils literal notranslate"><span class="pre">DataFrame</span></code>, wrapped in a function.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">gala.coordinates</span> <span class="kn">import</span> <span class="n">reflex_correct</span>
@@ -858,20 +888,21 @@ Results: None
<span class="n">distance</span><span class="o">=</span><span class="mi">8</span><span class="o">*</span><span class="n">u</span><span class="o">.</span><span class="n">kpc</span><span class="p">,</span>
<span class="n">radial_velocity</span><span class="o">=</span><span class="mi">0</span><span class="o">*</span><span class="n">u</span><span class="o">.</span><span class="n">km</span><span class="o">/</span><span class="n">u</span><span class="o">.</span><span class="n">s</span><span class="p">)</span>
<span class="n">transformed</span> <span class="o">=</span> <span class="n">skycoord</span><span class="o">.</span><span class="n">transform_to</span><span class="p">(</span><span class="n">GD1Koposov10</span><span class="p">)</span>
<span class="n">gd1_coord</span> <span class="o">=</span> <span class="n">reflex_correct</span><span class="p">(</span><span class="n">transformed</span><span class="p">)</span>
<span class="n">gd1_frame</span> <span class="o">=</span> <span class="n">GD1Koposov10</span><span class="p">()</span>
<span class="n">transformed</span> <span class="o">=</span> <span class="n">skycoord</span><span class="o">.</span><span class="n">transform_to</span><span class="p">(</span><span class="n">gd1_frame</span><span class="p">)</span>
<span class="n">skycoord_gd1</span> <span class="o">=</span> <span class="n">reflex_correct</span><span class="p">(</span><span class="n">transformed</span><span class="p">)</span>
<span class="n">df</span> <span class="o">=</span> <span class="n">table</span><span class="o">.</span><span class="n">to_pandas</span><span class="p">()</span>
<span class="n">df</span><span class="p">[</span><span class="s1">&#39;phi1&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">gd1_coord</span><span class="o">.</span><span class="n">phi1</span>
<span class="n">df</span><span class="p">[</span><span class="s1">&#39;phi2&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">gd1_coord</span><span class="o">.</span><span class="n">phi2</span>
<span class="n">df</span><span class="p">[</span><span class="s1">&#39;pm_phi1&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">gd1_coord</span><span class="o">.</span><span class="n">pm_phi1_cosphi2</span>
<span class="n">df</span><span class="p">[</span><span class="s1">&#39;pm_phi2&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">gd1_coord</span><span class="o">.</span><span class="n">pm_phi2</span>
<span class="n">df</span><span class="p">[</span><span class="s1">&#39;phi1&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">skycoord_gd1</span><span class="o">.</span><span class="n">phi1</span>
<span class="n">df</span><span class="p">[</span><span class="s1">&#39;phi2&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">skycoord_gd1</span><span class="o">.</span><span class="n">phi2</span>
<span class="n">df</span><span class="p">[</span><span class="s1">&#39;pm_phi1&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">skycoord_gd1</span><span class="o">.</span><span class="n">pm_phi1_cosphi2</span>
<span class="n">df</span><span class="p">[</span><span class="s1">&#39;pm_phi2&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">skycoord_gd1</span><span class="o">.</span><span class="n">pm_phi2</span>
<span class="k">return</span> <span class="n">df</span>
</pre></div>
</div>
</div>
</div>
<p>Heres how we can use this function:</p>
<p>Heres how we use it:</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">candidate_df</span> <span class="o">=</span> <span class="n">make_dataframe</span><span class="p">(</span><span class="n">candidate_table</span><span class="p">)</span>
@@ -884,34 +915,34 @@ Results: None
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">x</span> <span class="o">=</span> <span class="n">candidate_df</span><span class="p">[</span><span class="s1">&#39;phi1&#39;</span><span class="p">]</span>
<span class="n">y</span> <span class="o">=</span> <span class="n">candidate_df</span><span class="p">[</span><span class="s1">&#39;phi2&#39;</span><span class="p">]</span>
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="s1">&#39;ko&#39;</span><span class="p">,</span> <span class="n">markersize</span><span class="o">=</span><span class="mf">0.5</span><span class="p">,</span> <span class="n">alpha</span><span class="o">=</span><span class="mf">0.5</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">xlabel</span><span class="p">(</span><span class="s1">&#39;ra (degree GD1)&#39;</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">ylabel</span><span class="p">(</span><span class="s1">&#39;dec (degree GD1)&#39;</span><span class="p">);</span>
<span class="n">plt</span><span class="o">.</span><span class="n">xlabel</span><span class="p">(</span><span class="s1">&#39;phi1 (degree GD1)&#39;</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">ylabel</span><span class="p">(</span><span class="s1">&#39;phi2 (degree GD1)&#39;</span><span class="p">);</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<img alt="_images/04_select_64_0.png" src="_images/04_select_64_0.png" />
<img alt="_images/04_select_70_0.png" src="_images/04_select_70_0.png" />
</div>
</div>
<p>Were starting to see GD-1 more clearly.</p>
<p>We can compare this figure with one of these panels in Figure 1 from the original paper:</p>
<p>We can compare this figure with this panel from Figure 1 from the original paper:</p>
<a class="reference internal image-reference" href="https://github.com/datacarpentry/astronomy-python/raw/gh-pages/fig/gd1-2.png"><img alt="https://github.com/datacarpentry/astronomy-python/raw/gh-pages/fig/gd1-2.png" src="https://github.com/datacarpentry/astronomy-python/raw/gh-pages/fig/gd1-2.png" style="height: 150px;" /></a>
<p>This panel shows stars selected based on proper motion only, so it is comparable to our figure (although notice that it covers a wider region).</p>
<p>In the next lesson, we will use photometry data from Pan-STARRS to do a second round of filtering, and see if we can replicate this panel.</p>
<a class="reference internal image-reference" href="https://github.com/datacarpentry/astronomy-python/raw/gh-pages/fig/gd1-4.png"><img alt="https://github.com/datacarpentry/astronomy-python/raw/gh-pages/fig/gd1-4.png" src="https://github.com/datacarpentry/astronomy-python/raw/gh-pages/fig/gd1-4.png" style="height: 150px;" /></a>
<p>The top panel shows stars selected based on proper motion only, so it is comparable to our figure (although notice that it covers a wider region).</p>
<p>In the next lesson, we will use photometry data from Pan-STARRS to do a second round of filtering, and see if we can replicate the bottom panel.</p>
<p>Well also learn how to add annotations like the ones in the figure from the paper, and customize the style of the figure to present the results clearly and compellingly.</p>
<p>Later well see how to add annotations like the ones in the figure and customize the style of the figure to present the results clearly and compellingly.</p>
</div>
<div class="section" id="saving-the-dataframe">
<h2>Saving the DataFrame<a class="headerlink" href="#saving-the-dataframe" title="Permalink to this headline"></a></h2>
<p>Lets save this <code class="docutils literal notranslate"><span class="pre">DataFrame</span></code> so we can pick up where we left off without running this query again.</p>
<p>The HDF file should already exist, so well add <code class="docutils literal notranslate"><span class="pre">candidate_df</span></code> to it.</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">filename</span> <span class="o">=</span> <span class="s1">&#39;gd1_candidates.hdf5&#39;</span>
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">filename</span> <span class="o">=</span> <span class="s1">&#39;gd1_data.hdf&#39;</span>
<span class="n">candidate_df</span><span class="o">.</span><span class="n">to_hdf</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="s1">&#39;candidate_df&#39;</span><span class="p">,</span> <span class="n">mode</span><span class="o">=</span><span class="s1">&#39;w&#39;</span><span class="p">)</span>
<span class="n">candidate_df</span><span class="o">.</span><span class="n">to_hdf</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="s1">&#39;candidate_df&#39;</span><span class="p">)</span>
</pre></div>
</div>
</div>
@@ -919,18 +950,18 @@ Results: None
<p>We can use <code class="docutils literal notranslate"><span class="pre">ls</span></code> to confirm that the file exists and check the size:</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="o">!</span>ls -lh gd1_candidates.hdf5
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="o">!</span>ls -lh gd1_data.hdf
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>-rw-rw-r-- 1 downey downey 698K Dec 29 16:46 gd1_candidates.hdf5
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>-rw-rw-r-- 1 downey downey 3.3M Jan 13 13:32 gd1_data.hdf
</pre></div>
</div>
</div>
</div>
<p>If you are using Windows, <code class="docutils literal notranslate"><span class="pre">ls</span></code> might not work; in that case, try:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>!dir gd1_candidates.hdf5
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>!dir gd1_data.hdf
</pre></div>
</div>
</div>
@@ -938,13 +969,13 @@ Results: None
<h2>CSV<a class="headerlink" href="#csv" title="Permalink to this headline"></a></h2>
<p>Pandas can write a variety of other formats, <a class="reference external" href="https://pandas.pydata.org/pandas-docs/stable/user_guide/io.html">which you can read about here</a>.</p>
<p>We wont cover all of them, but one other important one is <a class="reference external" href="https://en.wikipedia.org/wiki/Comma-separated_values">CSV</a>, which stands for “comma-separated values”.</p>
<p>CSV is a plain-text format with minimal formatting requirements, so it can be read and written by pretty much any tool that works with data. In that sense, it is the “least common denominator” of data formats.</p>
<p>CSV is a plain-text format that can be read and written by pretty much any tool that works with data. In that sense, it is the “least common denominator” of data formats.</p>
<p>However, it has an important limitation: some information about the data gets lost in translation, notably the data types. If you read a CSV file from someone else, you might need some additional information to make sure you are getting it right.</p>
<p>Also, CSV files tend to be big, and slow to read and write.</p>
<p>With those caveats, heres how to write one:</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">candidate_df</span><span class="o">.</span><span class="n">to_csv</span><span class="p">(</span><span class="s1">&#39;gd1_candidates.csv&#39;</span><span class="p">)</span>
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">candidate_df</span><span class="o">.</span><span class="n">to_csv</span><span class="p">(</span><span class="s1">&#39;gd1_data.csv&#39;</span><span class="p">)</span>
</pre></div>
</div>
</div>
@@ -952,37 +983,45 @@ Results: None
<p>We can check the file size like this:</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="o">!</span>ls -lh gd1_candidates.csv
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="o">!</span>ls -lh gd1_data.csv
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>-rw-rw-r-- 1 downey downey 1.4M Dec 29 16:46 gd1_candidates.csv
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>-rw-rw-r-- 1 downey downey 1.4M Jan 13 13:33 gd1_data.csv
</pre></div>
</div>
</div>
</div>
<p>The CSV file about 2 times bigger than the HDF5 file (so thats not that bad, really).</p>
<p>We can see the first few lines like this:</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="o">!</span>head -3 gd1_candidates.csv
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="o">!</span>head -3 gd1_data.csv
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>,source_id,ra,dec,pmra,pmdec,parallax,radial_velocity,phi1,phi2,pm_phi1,pm_phi2
0,635559124339440000,137.58671691646745,19.1965441084838,-3.770521900009566,-12.490481778113859,0.7913934419894347,,-59.63048941944402,-1.2164852515042963,-7.361362712597496,-0.592632882064492
1,635860218726658176,138.5187065217173,19.09233926905897,-5.941679495793577,-11.346409129876392,0.30745551377348623,,-59.247329893833296,-2.016078400820631,-7.527126084640531,1.7487794924176672
<div class="output stream highlight-myst-ansi notranslate"><div class="highlight"><pre><span></span>,source_id,ra,dec,pmra,pmdec,parallax,phi1,phi2,pm_phi1,pm_phi2
0,635559124339440000,137.58671691646745,19.1965441084838,-3.770521900009566,-12.490481778113859,0.7913934419894347,-59.63048941944402,-1.2164852515042963,-7.361362712597496,-0.592632882064492
1,635860218726658176,138.5187065217173,19.09233926905897,-5.941679495793577,-11.346409129876392,0.30745551377348623,-59.247329893833296,-2.016078400820631,-7.527126084640531,1.7487794924176672
</pre></div>
</div>
</div>
</div>
<p>On Windows you can use</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>!dir gd1_data.csv
</pre></div>
</div>
<p>to confirm that the file exists and</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>!type gd1_data.csv
</pre></div>
</div>
<p>to view the contents.</p>
<p>The CSV file contains the names of the columns, but not the data types.</p>
<p>We can read the CSV file back like this:</p>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">read_back_csv</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="s1">&#39;gd1_candidates.csv&#39;</span><span class="p">)</span>
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">read_back_csv</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="s1">&#39;gd1_data.csv&#39;</span><span class="p">)</span>
</pre></div>
</div>
</div>
@@ -1019,7 +1058,6 @@ Results: None
<th>pmra</th>
<th>pmdec</th>
<th>parallax</th>
<th>radial_velocity</th>
<th>phi1</th>
<th>phi2</th>
<th>pm_phi1</th>
@@ -1035,7 +1073,6 @@ Results: None
<td>-3.770522</td>
<td>-12.490482</td>
<td>0.791393</td>
<td>NaN</td>
<td>-59.630489</td>
<td>-1.216485</td>
<td>-7.361363</td>
@@ -1049,7 +1086,6 @@ Results: None
<td>-5.941679</td>
<td>-11.346409</td>
<td>0.307456</td>
<td>NaN</td>
<td>-59.247330</td>
<td>-2.016078</td>
<td>-7.527126</td>
@@ -1063,7 +1099,6 @@ Results: None
<td>-3.897001</td>
<td>-12.702780</td>
<td>0.779463</td>
<td>NaN</td>
<td>-59.133391</td>
<td>-2.306901</td>
<td>-7.560608</td>
@@ -1105,7 +1140,6 @@ Results: None
<th>pmra</th>
<th>pmdec</th>
<th>parallax</th>
<th>radial_velocity</th>
<th>phi1</th>
<th>phi2</th>
<th>pm_phi1</th>
@@ -1122,7 +1156,6 @@ Results: None
<td>-3.770522</td>
<td>-12.490482</td>
<td>0.791393</td>
<td>NaN</td>
<td>-59.630489</td>
<td>-1.216485</td>
<td>-7.361363</td>
@@ -1137,7 +1170,6 @@ Results: None
<td>-5.941679</td>
<td>-11.346409</td>
<td>0.307456</td>
<td>NaN</td>
<td>-59.247330</td>
<td>-2.016078</td>
<td>-7.527126</td>
@@ -1152,7 +1184,6 @@ Results: None
<td>-3.897001</td>
<td>-12.702780</td>
<td>0.779463</td>
<td>NaN</td>
<td>-59.133391</td>
<td>-2.306901</td>
<td>-7.560608</td>
@@ -1212,7 +1243,7 @@ Results: None
<div class='prev-next-bottom'>
<a class='left-prev' id="prev-link" href="03_motion.html" title="previous page">Proper Motion</a>
<a class='left-prev' id="prev-link" href="03_motion.html" title="previous page">3. Proper Motion</a>
<a class='right-next' id="next-link" href="05_join.html" title="next page">Joining Tables</a>
</div>

View File

@@ -58,7 +58,7 @@
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="next" title="Photometry" href="06_photo.html" />
<link rel="prev" title="Transformation and Selection" href="04_select.html" />
<link rel="prev" title="4. Transformation and Selection" href="04_select.html" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="docsearch:language" content="en" />
@@ -99,22 +99,22 @@
<ul class="current nav sidenav_l1">
<li class="toctree-l1">
<a class="reference internal" href="01_query.html">
Queries
1. Queries
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="02_coords.html">
Coordinates and units
2. Coordinates and Units
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="03_motion.html">
Proper Motion
3. Proper Motion
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="04_select.html">
Transformation and Selection
4. Transformation and Selection
</a>
</li>
<li class="toctree-l1 current active">
@@ -522,7 +522,7 @@ The size of the file is about 750 KB, so thats not too bad.</p>
</pre></div>
</div>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="c1"># This one should cause an error</span>
<span class="n">table_id</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s1">&#39;candidate_df.xml&#39;</span><span class="p">,</span>
<span class="n">column</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s1">&#39;candidate_df.xml&#39;</span><span class="p">,</span>
<span class="nb">format</span><span class="o">=</span><span class="s1">&#39;votable&#39;</span><span class="p">,</span>
<span class="n">overwrite</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
</pre></div>
@@ -1110,7 +1110,7 @@ dtype: float64
<div class='prev-next-bottom'>
<a class='left-prev' id="prev-link" href="04_select.html" title="previous page">Transformation and Selection</a>
<a class='left-prev' id="prev-link" href="04_select.html" title="previous page">4. Transformation and Selection</a>
<a class='right-next' id="next-link" href="06_photo.html" title="next page">Photometry</a>
</div>

View File

@@ -99,22 +99,22 @@
<ul class="current nav sidenav_l1">
<li class="toctree-l1">
<a class="reference internal" href="01_query.html">
Queries
1. Queries
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="02_coords.html">
Coordinates and units
2. Coordinates and Units
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="03_motion.html">
Proper Motion
3. Proper Motion
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="04_select.html">
Transformation and Selection
4. Transformation and Selection
</a>
</li>
<li class="toctree-l1">

View File

@@ -98,22 +98,22 @@
<ul class="current nav sidenav_l1">
<li class="toctree-l1">
<a class="reference internal" href="01_query.html">
Queries
1. Queries
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="02_coords.html">
Coordinates and units
2. Coordinates and Units
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="03_motion.html">
Proper Motion
3. Proper Motion
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="04_select.html">
Transformation and Selection
4. Transformation and Selection
</a>
</li>
<li class="toctree-l1">

View File

@@ -57,7 +57,7 @@
<script async="async" src="_static/sphinx-thebe.js"></script>
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="next" title="Queries" href="01_query.html" />
<link rel="next" title="1. Queries" href="01_query.html" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="docsearch:language" content="en" />
@@ -98,22 +98,22 @@
<ul class="nav sidenav_l1">
<li class="toctree-l1">
<a class="reference internal" href="01_query.html">
Queries
1. Queries
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="02_coords.html">
Coordinates and units
2. Coordinates and Units
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="03_motion.html">
Proper Motion
3. Proper Motion
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="04_select.html">
Transformation and Selection
4. Transformation and Selection
</a>
</li>
<li class="toctree-l1">
@@ -342,7 +342,7 @@ If you are on a Mac or Linux machine, you should be able to
use any Terminal.<br />
If you are on Windows, you might have to use the Anaconda Prompt,
which you can find under the Start menu.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">conda</span> <span class="n">install</span> <span class="n">jupyter</span> <span class="n">numpy</span> <span class="n">scipy</span> <span class="n">pandas</span> <span class="n">matplotlib</span> <span class="n">seaborn</span> <span class="n">libopenblas</span>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">conda</span> <span class="n">install</span> <span class="n">jupyter</span> <span class="n">numpy</span> <span class="n">scipy</span> <span class="n">pandas</span> <span class="n">pytables</span> <span class="n">matplotlib</span> <span class="n">seaborn</span> <span class="n">libopenblas</span>
<span class="n">conda</span> <span class="n">install</span> <span class="o">-</span><span class="n">c</span> <span class="n">conda</span><span class="o">-</span><span class="n">forge</span> <span class="n">astropy</span> <span class="n">astroquery</span> <span class="n">gala</span> <span class="n">python</span><span class="o">-</span><span class="n">wget</span>
</pre></div>
</div>
@@ -424,7 +424,7 @@ If they work and you get no error messages, <strong>you are all set</strong>.</p
<div class='prev-next-bottom'>
<a class='right-next' id="next-link" href="01_query.html" title="next page">Queries</a>
<a class='right-next' id="next-link" href="01_query.html" title="next page">1. Queries</a>
</div>
<footer class="footer mt-5 mt-md-0">

Binary file not shown.

Before

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 15 KiB

After

Width:  |  Height:  |  Size: 80 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 15 KiB

BIN
_images/03_motion_112_0.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 9.6 KiB

BIN
_images/03_motion_30_0.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.5 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 113 KiB

BIN
_images/03_motion_45_0.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 51 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 113 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 113 KiB

BIN
_images/03_motion_69_0.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

BIN
_images/03_motion_71_0.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 60 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 79 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 79 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 79 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 80 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 80 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 80 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 80 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 80 KiB

After

Width:  |  Height:  |  Size: 80 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 82 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 82 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 80 KiB

After

Width:  |  Height:  |  Size: 82 KiB

View File

Before

Width:  |  Height:  |  Size: 80 KiB

After

Width:  |  Height:  |  Size: 80 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 79 KiB

View File

Before

Width:  |  Height:  |  Size: 79 KiB

After

Width:  |  Height:  |  Size: 79 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 36 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 36 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 36 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 41 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 41 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 41 KiB

After

Width:  |  Height:  |  Size: 36 KiB

BIN
_images/04_select_70_0.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 41 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 30 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 9.5 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 27 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 30 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 29 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 25 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 28 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 31 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 9.6 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 150 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 62 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 34 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 140 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 147 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 140 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 147 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 9.8 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 33 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 80 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 79 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 80 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 81 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 35 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 32 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 113 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 32 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 9.7 KiB

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -421,7 +421,7 @@
"\n",
"```\n",
"# This one should cause an error\n",
"table_id.write('candidate_df.xml', \n",
"column.write('candidate_df.xml', \n",
" format='votable', \n",
" overwrite=True)\n",
"```"

View File

@@ -196,7 +196,7 @@ If you are on Windows, you might have to use the Anaconda Prompt,
which you can find under the Start menu.
```
conda install jupyter numpy scipy pandas matplotlib seaborn libopenblas
conda install jupyter numpy scipy pandas pytables matplotlib seaborn libopenblas
conda install -c conda-forge astropy astroquery gala python-wget
```

File diff suppressed because one or more lines are too long

View File

@@ -1,272 +0,0 @@
# Astronomical Data in Python
*Astronomical Data in Python* is an introduction to tools and practices for working with astronomical data. Topics covered include:
* Writing queries that select and download data from a database.
* Using data stored in an Astropy `Table` or Pandas `DataFrame`.
* Working with coordinates and other quantities with units.
* Storing data in various formats.
* Performing database join operations that combine data from multiple tables.
* Visualizing data and preparing publication-quality figures.
As a running example, we will replicate part of the analysis in a recent paper, "[Off the beaten path: Gaia reveals GD-1 stars outside of the main stream](https://arxiv.org/abs/1805.00425)" by Adrian M. Price-Whelan and Ana Bonaca.
This material was developed in collaboration with [The Carpentries](https://carpentries.org/) and the Astronomy Curriculum Development Committee, and supported by funding from the American Institute of Physics through the American Astronomical Society.
I am grateful for contributions from the members of the committee -- Azalee Bostroem, Rodolfo Montez, and Phil Rosenfield -- and from Erin Becker, Brett Morris and Adrian Price-Whelan.
The original format of this material is a series of Jupyter notebooks. Using the
links below, you can read the notebooks on NBViewer or run them on Colab. If you
want to run the notebooks in your own environment, you can download them from
this repository and follow the instructions below to set up your environment.
This material is also available in the form of [Carpentries lessons](https://datacarpentry.github.io/astronomy-python), but you should be
aware that these versions might diverge in the future.
**Prerequisites**
This material should be accessible to people familiar with basic Python, but not necessarily the libraries we will use, like Astropy or Pandas. If you are familiar with Python lists and dictionaries, and you know how to write a function that takes parameters and returns a value, that should be enough.
We assume that you are familiar with astronomy at the undergraduate level, but we will not assume specialized knowledge of the datasets or analysis methods we'll use.
### Notebook 1
This notebook demonstrates the following steps:
1. Making a connection to the Gaia server,
2. Exploring information about the database and the tables it contains,
3. Writing a query and sending it to the server, and finally
4. Downloading the response from the server as an Astropy `Table`.
[Run Notebook 1 on Colab](https://colab.research.google.com/github/AllenDowney/AstronomicalData/blob/main/01_query.ipynb)
[or click here to read it on NBViewer](https://nbviewer.jupyter.org/github/AllenDowney/AstronomicalData/blob/main/01_query.ipynb)
### Notebook 2
This notebook starts with an example that does a "cone search"; that is, it selects stars that appear in a circular region of the sky.
Then, to select stars in the vicinity of GD-1, we:
* Use `Quantity` objects to represent measurements with units.
* Use the `Gala` library to convert coordinates from one frame to another.
* Use the ADQL keywords `POLYGON`, `CONTAINS`, and `POINT` to select stars that fall within a polygonal region.
* Submit a query and download the results.
* Store the results in a FITS file.
[Run Notebook 2 on Colab](https://colab.research.google.com/github/AllenDowney/AstronomicalData/blob/main/02_coords.ipynb)
[or click here to read it on NBViewer](https://nbviewer.jupyter.org/github/AllenDowney/AstronomicalData/blob/main/02_coords.ipynb)
### Notebook 3
Here are the steps in this notebook:
1. We'll read back the results from the previous notebook, which we saved in a FITS file.
2. Then we'll transform the coordinates and proper motion data from ICRS back to the coordinate frame of GD-1.
3. We'll put those results into a Pandas `DataFrame`, which we'll use to select stars near the centerline of GD-1.
4. Plotting the proper motion of those stars, we'll identify a region of proper motion for stars that are likely to be in GD-1.
5. Finally, we'll select and plot the stars whose proper motion is in that region.
[Run Notebook 3 on Colab](https://colab.research.google.com/github/AllenDowney/AstronomicalData/blob/main/03_motion.ipynb)
[or click here to read it on NBViewer](https://nbviewer.jupyter.org/github/AllenDowney/AstronomicalData/blob/main/03_motion.ipynb)
### Notebook 4
Here are the steps in this notebook:
1. Using data from the previous notebook, we'll identify the values of proper motion for stars likely to be in GD-1.
2. Then we'll compose an ADQL query that selects stars based on proper motion, so we can download only the data we need.
3. We'll also see how to write the results to a CSV file.
That will make it possible to search a bigger region of the sky in a single query.
[Run Notebook 4 on Colab](https://colab.research.google.com/github/AllenDowney/AstronomicalData/blob/main/04_select.ipynb)
[or click here to read it on NBViewer](https://nbviewer.jupyter.org/github/AllenDowney/AstronomicalData/blob/main/04_select.ipynb)
### Notebook 5
Here are the steps in this notebook:
1. We'll reload the candidate stars we identified in the previous notebook.
2. Then we'll run a query on the Gaia server that uploads the table of candidates and uses a `JOIN` operation to select photometry data for the candidate stars.
3. We'll write the results to a file for use in the next notebook.
[Run Notebook 5 on Colab](https://colab.research.google.com/github/AllenDowney/AstronomicalData/blob/main/05_join.ipynb)
[or click here to read it on NBViewer](https://nbviewer.jupyter.org/github/AllenDowney/AstronomicalData/blob/main/05_join.ipynb)
### Notebook 6
Here are the steps in this notebook:
1. We'll reload the data from the previous notebook and make a color-magnitude diagram.
2. Then we'll specify a polygon in the diagram that contains stars with the photometry we expect.
3. Then we'll merge the photometry data with the list of candidate stars, storing the result in a Pandas `DataFrame`.
[Run Notebook 6 on Colab](https://colab.research.google.com/github/AllenDowney/AstronomicalData/blob/main/06_photo.ipynb)
[or click here to read it on NBViewer](https://nbviewer.jupyter.org/github/AllenDowney/AstronomicalData/blob/main/06_photo.ipynb)
### Notebook 7
Here are the steps in this notebook:
1. Starting with the figure from the previous notebook, we'll add annotations to present the results more clearly.
2. The we'll see several ways to customize figures to make them more appealing and effective.
3. Finally, we'll see how to make a figure with multiple panels or subplots.
[Run Notebook 7 on Colab](https://colab.research.google.com/github/AllenDowney/AstronomicalData/blob/main/07_plot.ipynb)
[or click here to read it on NBViewer](https://nbviewer.jupyter.org/github/AllenDowney/AstronomicalData/blob/main/07_plot.ipynb)
## Installation instructions
If you plan to run these notebooks on Colab, you don't have to
install anything; you can use the links in the
previous section to open and run them.
If you want to run the notebooks in your own environment,
you might have to do some setup.
You will need to install Python, Jupyter, and some additional
libraries.
If you don't already have Jupyter, we recommend installing
Anaconda, which is a Python distribution that
contains everything you need to run the workshop code.
It is easy to install on Windows, Mac, and Linux,
and because it does a user-level install, it will not
interfere with other Python installations.
[Information about installing Anaconda is here](https://www.anaconda.com/distribution/).
If you have the choice of Python 2 or 3, choose Python 3.
Now, there are two ways to get the libraries you need:
* Option 1: You can install them in an existing Conda environment.
* Option 2: You can create a new Conda environment.
Installing libraries in an existing environment is simpler,
but if you use the same environment for many projects,
it will get big, complicated, and prone to package conflicts.
**Option 1:** *Installing libraries in an existing Conda
environment*
Most of the libraries we need can be installed using Conda,
by running the following commands in a Terminal.
If you are on a Mac or Linux machine, you should be able to
use any Terminal.
If you are on Windows, you might have to use the Anaconda Prompt,
which you can find under the Start menu.
```
conda install jupyter numpy scipy pandas matplotlib seaborn libopenblas
conda install -c conda-forge astropy astroquery gala python-wget
```
**Option 2:** *Creating a new Conda environment*
To create a new Conda environment, you'll need to download
an environment file from our repository. On Mac or Linux,
you can download it using `wget` on the command line:
```
wget https://raw.githubusercontent.com/AllenDowney/AstronomicalData/main/environment.yml
```
Or you can [download it using this link](https://raw.githubusercontent.com/AllenDowney/AstronomicalData/main/environment.yml).
In a Terminal or Jupyter Prompt, make sure you are in folder
where `environment.yml` is stored, and run:
```
conda env create -f environment.yml
```
Then, to activate the environment you just created, run:
```
conda activate AstronomicalData
```
**Run Jupyter**
If you are not familiar with Jupyter, you can [run a tutorial by clicking here](https://jupyter.org/try).
Then select "Try Classic Notebook". It will open a notebook with instructions for getting started.
Or you can run this [introductory notebook on Colab](https://colab.research.google.com/notebooks/intro.ipynb).
Before you launch Jupyter, [download this notebook](https://raw.githubusercontent.com/AllenDowney/AstronomicalData/main/test_setup.ipynb), which contains code to test your environment.
Or you can use `wget` to download it on the command line, like this:
```
wget https://raw.githubusercontent.com/AllenDowney/AstronomicalData/main/test_setup.ipynb
```
To start Jupyter, run:
```
jupyter notebook
```
Jupyter should launch your default browser or open a tab in an existing browser window.
If not, the Jupyter server should print a URL you can use. For example, when I launch Jupyter, I get
```
$ jupyter notebook
[I 10:03:20.115 NotebookApp] Serving notebooks from local directory: /home/username
[I 10:03:20.115 NotebookApp] 0 active kernels
[I 10:03:20.115 NotebookApp] The Jupyter Notebook is running at: http://localhost:8888/
[I 10:03:20.115 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
```
In this example, the URL is [http://localhost:8888](http://localhost:8888).
When you start your server, you might get a different URL.
Whatever it is, if you paste it into a browser, you should should see a home page with a list of directories.
Now open the notebook you downloaded and run the cells that contain `import` statements.
If they work and you get no error messages, **you are all set**.
If you get error messages about missing packages, you can install the packages you need using Conda or `pip`.
If you run into problems with these instructions, let us know and we will make corrections. Good luck!

View File

@@ -1,57 +0,0 @@
# Astronomical Data in Python
*Astronomical Data in Python* is an introduction to tools and practices for working with astronomical data. Topics covered include:
* Writing queries that select and download data from a database.
* Using data stored in an Astropy `Table` or Pandas `DataFrame`.
* Working with coordinates and other quantities with units.
* Storing data in various formats.
* Performing database join operations that combine data from multiple tables.
* Visualizing data and preparing publication-quality figures.
As a running example, we will replicate part of the analysis in a recent paper, "[Off the beaten path: Gaia reveals GD-1 stars outside of the main stream](https://arxiv.org/abs/1805.00425)" by Adrian M. Price-Whelan and Ana Bonaca.
As the abstract explains, "Using data from the Gaia second data release combined with Pan-STARRS photometry, we present a sample of highly-probable members of the longest cold stream in the Milky Way, GD-1."
GD-1 is a [stellar stream](https://en.wikipedia.org/wiki/List_of_stellar_streams), which is "an association of stars orbiting a galaxy that was once a globular cluster or dwarf galaxy that has now been torn apart and stretched out along its orbit by tidal forces."
[This article in *Science* magazine](https://www.sciencemag.org/news/2018/10/streams-stars-reveal-galaxy-s-violent-history-and-perhaps-its-unseen-dark-matter) explains some of the background, including the process that led to the paper and a discussion of the scientific implications:
* "The streams are particularly useful for ... galactic archaeology --- rewinding the cosmic clock to reconstruct the assembly of the Milky Way."
* "They also are being used as exquisitely sensitive scales to measure the galaxy's mass."
* "... the streams are well-positioned to reveal the presence of dark matter ... because the streams are so fragile, theorists say, collisions with marauding clumps of dark matter could leave telltale scars, potential clues to its nature."
## Data
The datasets we will work with are:
* [Gaia](https://en.wikipedia.org/wiki/Gaia_(spacecraft)), which is "a space observatory of the European Space Agency (ESA), launched in 2013 ... designed for astrometry: measuring the positions, distances and motions of stars with unprecedented precision", and
* [Pan-STARRS](https://en.wikipedia.org/wiki/Pan-STARRS), The Panoramic Survey Telescope and Rapid Response System, which is a survey designed to monitor the sky for transient objects, producing a catalog with accurate astronometry and photometry of detected sources.
Both of these datasets are very large, which can make them challenging to work with. It might not be possible, or practical, to download the entire dataset.
One of the goals of this workshop is to provide tools for working with large datasets.
## Prerequisites
These notebooks are meant for people who are familiar with basic Python, but not necessarily the libraries we will use, like Astropy or Pandas. If you are familiar with Python lists and dictionaries, and you know how to write a function that takes parameters and returns a value, you know enough Python to get started.
We assume that you have some familiarity with operating systems, like the ability to use a command-line interface. But we don't assume you have any prior experience with databases.
We assume that you are familiar with astronomy at the undergraduate level, but we will not assume specialized knowledge of the datasets or analysis methods we'll use.
## Acknowledgements
This material was developed in collaboration with [The Carpentries](https://carpentries.org/) and the Astronomy Curriculum Development Committee, and supported by funding from the American Institute of Physics through the American Astronomical Society.
I am grateful for contributions from the members of the committee -- Azalee Bostroem, Rodolfo Montez, and Phil Rosenfield -- and from Erin Becker, Brett Morris and Adrian Price-Whelan.
This material is also available in the form of [Carpentries lessons](https://datacarpentry.github.io/astronomy-python), but you should be
aware that these versions might diverge in the future.

View File

@@ -1,79 +0,0 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# The Notebook of Last Resort"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If you are not able to get everything installed that we need for the workshop, you have the option of running this notebook on Colab.\n",
"\n",
"The following cell installs the libraries we need that are not already in the Colab runtime environment."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"tags": [
"hide-cell"
]
},
"outputs": [],
"source": [
"# If we're running on Colab, install libraries\n",
"\n",
"import sys\n",
"IN_COLAB = 'google.colab' in sys.modules\n",
"\n",
"if IN_COLAB:\n",
" !pip install astroquery astro-gala"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"That should be everything you need. \n",
"\n",
"Before you get started, you probably want to press the Save button!\n",
"That will allow you to save your copy of this notebook in your Google Drive.\n",
"\n",
"If you don't save this notebook, any changes you make will be lost if you close the browser window or leave it idle too long.\n",
"\n",
"Now you can type code and run it in the following cells."
]
},
{
"cell_type": "raw",
"metadata": {},
"source": []
}
],
"metadata": {
"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.8.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -1,270 +0,0 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Data Carpentry Astronomy Workshop\n",
"\n",
"Please run this notebook before the workshop. There are three sections:\n",
"\n",
"1. A short introduction to Jupyter, with pointers to more resources.\n",
"\n",
"2. `import` statements to check whether you have everything installed that we need.\n",
"\n",
"3. A cell where you will paste a line of code you copy from Slack, to check for a potential problem with \"smart\" quotes.\n",
"\n",
"At the end there's a link to a survey where you can let us know you're done, or if you have any problems."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Introduction to Jupyter\n",
"\n",
"This is a Jupyter notebook, which is a computational document that contains text, code, and results.\n",
"\n",
"There are several development environments you can use to work with notebooks. Currently the two most common are:\n",
"\n",
"* Jupyter Classic Notebook, and\n",
"\n",
"* JupyterLab, which is a newer environment with some improved features, but also some limitations. \n",
"\n",
"During the workshop, we will use the Classic Notebook environment. If you are new to Jupyter, we suggest you should, too.\n",
"\n",
"If you are familiar with JupyterLab and you would rather use it for the workshop, that's fine. Just be aware that there will be differences between your environment and ours.\n",
"\n",
"If you are new to Jupyter, you can [read about it here](https://jupyter.org/try) and follow the tutorial called \"Try Classic Notebook\".\n",
"\n",
"You also might like [this tutorial from DataQuest](https://www.dataquest.io/blog/jupyter-notebook-tutorial/).\n",
"\n",
"The following sections introduce the features you will need for the workshop."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Selecting and running cells\n",
"\n",
"Notebooks are divided into cells that contain either text or code.\n",
"\n",
"This cell is text; the following cell is code:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"print('Hello')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To select a cell, click in the left margin next to the cell. You should see a blue frame surrounding the selected cell.\n",
"\n",
"To edit a code cell, click inside the cell. You should see a green frame around the selected cell, and you should see a cursor inside the cell.\n",
"\n",
"To edit a text cell, double-click inside the cell. Again, you should see a green frame around the selected cell, and you should see a cursor inside the cell.\n",
"\n",
"Text cells use the Markdown typesetting language, which [you can read about here](https://www.markdownguide.org/).\n",
"\n",
"To run a cell, hold down Shift and press Enter.\n",
"\n",
"* If you run a text cell, Jupyter formats the text and displays the result.\n",
"\n",
"* If you run a code cell, Jupyter runs the code in the cell and displays the result, if any.\n",
"\n",
"To try it out, select the previous code cell and press Shift-Enter. It should run the code and print `Hello`.\n",
"\n",
"Then edit this cell, change some of the text, and press Shift-Enter to format it."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Adding and removing cells\n",
"\n",
"You can add and remove cells from a notebook using the menu at the top of the page and the tool bar below the menu.\n",
"\n",
"Try the following exercises:\n",
"\n",
"1. From the Insert menu select \"Insert cell below\" to add a cell below this one. By default, you get a code cell, as you can see in the pull-down menu that says \"Code\".\n",
"\n",
"2. In the new cell, add a line of Python code and run it.\n",
"\n",
"3. Add another cell, select the new cell, and then click on the pull-down menu that says \"Code\". Select \"Markdown\". This makes the new cell a text cell.\n",
"\n",
"4. In the new cell, type some text and format it.\n",
"\n",
"5. Use the arrow buttons in the tool bar to move cells up and down.\n",
"\n",
"6. Use the cut, copy, and paste buttons to delete, add, and move cells.\n",
"\n",
"As you make changes, Jupyter saves your notebook automatically, but if you want to make sure, you can press the save button, which looks like a floppy disk from the 1990s.\n",
"\n",
"Finally, when you are done with a notebook, select \"Close and Halt\" from the File menu."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Check your installation\n",
"\n",
"Run the following cells to import the libraries we need for the workshop.\n",
"\n",
"If any of the libraries are missing, you'll get an error message.\n",
"\n",
"If you don't get any error messages, you are all set."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from wget import download"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import numpy as np"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's make sure you have a recent version of Pandas.\n",
"\n",
"If the following cell causes an error, you probably have an old version of Pandas. Please update it before the workshop."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"pd.Series([0]).to_numpy()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import matplotlib as mpl\n",
"import matplotlib.pyplot as plt\n",
"from matplotlib.path import Path\n",
"from matplotlib.patches import Polygon"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import astropy.coordinates as coord\n",
"import astropy.units as u\n",
"from astropy.table import Table"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import gala.coordinates as gc"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Note: running this import statement opens a connection\n",
"# to a Gaia server, so it will fail if you are not connected\n",
"# to the internet.\n",
"\n",
"from astroquery.gaia import Gaia"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Check for code-pasting problems\n",
"\n",
"During the workshop, we might put code on Slack and ask you to cut and paste it into the notebook.\n",
"\n",
"If you are on a Mac, you might encounter a problem with smart quotes.\n",
"\n",
"To check, following this link to [our Slack workspace](https://app.slack.com/client/T01GF8N96TD/C01G8AS0QBG/details/pins) and find the pinned message with the setup instructions.\n",
"It contains a line of Python code.\n",
"\n",
"Copy the code from Slack and paste it in the cell below.\n",
"If it runs without producing an error, you are all set.\n",
"\n",
"Otherwise, you might have to change your system settings so it does not convert straight quotes to smart quotes.\n",
"If you have trouble with this, let us know and we will provide more details."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Survey\n",
"\n",
"Please fill out [this survey](https://forms.gle/aWikZ88xA1Rcvsvx8) to let us know when you are done."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"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.8.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}

View File

@@ -1,7 +1,7 @@
pre { line-height: 125%; }
td.linenos pre { color: #000000; background-color: #f0f0f0; padding-left: 5px; padding-right: 5px; }
span.linenos { color: #000000; background-color: #f0f0f0; padding-left: 5px; padding-right: 5px; }
td.linenos pre.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight { background: #eeffcc; }

View File

@@ -1,516 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Plotting Functions &#8212; Astronomical Data in Python</title>
<link rel="stylesheet" href="../_static/css/index.d431a4ee1c1efae0e38bdfebc22debff.css">
<link rel="stylesheet"
href="../_static/vendor/fontawesome/5.13.0/css/all.min.css">
<link rel="preload" as="font" type="font/woff2" crossorigin
href="../_static/vendor/fontawesome/5.13.0/webfonts/fa-solid-900.woff2">
<link rel="preload" as="font" type="font/woff2" crossorigin
href="../_static/vendor/fontawesome/5.13.0/webfonts/fa-brands-400.woff2">
<link rel="stylesheet"
href="../_static/vendor/open-sans_all/1.44.1/index.css">
<link rel="stylesheet"
href="../_static/vendor/lato_latin-ext/1.44.1/index.css">
<link rel="stylesheet" href="../_static/sphinx-book-theme.bfb7730f9caf2ec0b46a44615585038c.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<link rel="stylesheet" type="text/css" href="../_static/togglebutton.css" />
<link rel="stylesheet" type="text/css" href="../_static/copybutton.css" />
<link rel="stylesheet" type="text/css" href="../_static/mystnb.css" />
<link rel="stylesheet" type="text/css" href="../_static/sphinx-thebe.css" />
<link rel="stylesheet" type="text/css" href="../_static/panels-main.c949a650a448cc0ae9fd3441c0e17fb0.css" />
<link rel="stylesheet" type="text/css" href="../_static/panels-variables.06eb56fa6e07937060861dad626602ad.css" />
<link rel="preload" as="script" href="../_static/js/index.30270b6e4c972e43c488.js">
<script id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
<script src="../_static/jquery.js"></script>
<script src="../_static/underscore.js"></script>
<script src="../_static/doctools.js"></script>
<script src="../_static/language_data.js"></script>
<script src="../_static/togglebutton.js"></script>
<script src="../_static/clipboard.min.js"></script>
<script src="../_static/copybutton.js"></script>
<script >var togglebuttonSelector = '.toggle, .admonition.dropdown, .tag_hide_input div.cell_input, .tag_hide-input div.cell_input, .tag_hide_output div.cell_output, .tag_hide-output div.cell_output, .tag_hide_cell.cell, .tag_hide-cell.cell';</script>
<script src="../_static/sphinx-book-theme.be0a4a0c39cd630af62a2fcf693f3f06.js"></script>
<script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
<script type="text/x-mathjax-config">MathJax.Hub.Config({"tex2jax": {"inlineMath": [["\\(", "\\)"]], "displayMath": [["\\[", "\\]"]], "processRefs": false, "processEnvironments": false}})</script>
<script async="async" src="https://unpkg.com/thebelab@latest/lib/index.js"></script>
<script >
const thebe_selector = ".thebe"
const thebe_selector_input = "pre"
const thebe_selector_output = ".output"
</script>
<script async="async" src="../_static/sphinx-thebe.js"></script>
<link rel="index" title="Index" href="../genindex.html" />
<link rel="search" title="Search" href="../search.html" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="docsearch:language" content="en" />
</head>
<body data-spy="scroll" data-target="#bd-toc-nav" data-offset="80">
<div class="container-xl">
<div class="row">
<div class="col-12 col-md-3 bd-sidebar site-navigation show" id="site-navigation">
<div class="navbar-brand-box">
<a class="navbar-brand text-wrap" href="../index.html">
<h1 class="site-logo" id="site-title">Astronomical Data in Python</h1>
</a>
</div>
<form class="bd-search d-flex align-items-center" action="../search.html" method="get">
<i class="icon fas fa-search"></i>
<input type="search" class="form-control" name="q" id="search-input" placeholder="Search this book..." aria-label="Search this book..." autocomplete="off" >
</form>
<nav class="bd-links" id="bd-docs-nav" aria-label="Main navigation">
<ul class="nav sidenav_l1">
<li class="toctree-l1">
<a class="reference internal" href="../README.html">
Astronomical Data in Python
</a>
</li>
</ul>
<ul class="nav sidenav_l1">
<li class="toctree-l1">
<a class="reference internal" href="../01_query.html">
Queries
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="../02_coords.html">
Coordinates and units
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="../03_motion.html">
Chapter 3
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="../04_select.html">
Chapter 4
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="../05_join.html">
Chapter 5
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="../06_photo.html">
Chapter 6
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="../07_plot.html">
Chapter 7
</a>
</li>
</ul>
</nav>
<!-- To handle the deprecated key -->
<div class="navbar_extra_footer">
Powered by <a href="https://jupyterbook.org">Jupyter Book</a>
</div>
</div>
<main class="col py-md-3 pl-md-4 bd-content overflow-auto" role="main">
<div class="row topbar fixed-top container-xl">
<div class="col-12 col-md-3 bd-topbar-whitespace site-navigation show">
</div>
<div class="col pl-2 topbar-main">
<button id="navbar-toggler" class="navbar-toggler ml-0" type="button" data-toggle="collapse"
data-toggle="tooltip" data-placement="bottom" data-target=".site-navigation" aria-controls="navbar-menu"
aria-expanded="true" aria-label="Toggle navigation" aria-controls="site-navigation"
title="Toggle navigation" data-toggle="tooltip" data-placement="left">
<i class="fas fa-bars"></i>
<i class="fas fa-arrow-left"></i>
<i class="fas fa-arrow-up"></i>
</button>
<div class="dropdown-buttons-trigger">
<button id="dropdown-buttons-trigger" class="btn btn-secondary topbarbtn" aria-label="Download this page"><i
class="fas fa-download"></i></button>
<div class="dropdown-buttons">
<!-- ipynb file if we had a myst markdown file -->
<!-- Download raw file -->
<a class="dropdown-buttons" href="../_sources/class_notebooks/lesson_7.ipynb"><button type="button"
class="btn btn-secondary topbarbtn" title="Download source file" data-toggle="tooltip"
data-placement="left">.ipynb</button></a>
<!-- Download PDF via print -->
<button type="button" id="download-print" class="btn btn-secondary topbarbtn" title="Print to PDF"
onClick="window.print()" data-toggle="tooltip" data-placement="left">.pdf</button>
</div>
</div>
<!-- Source interaction buttons -->
<div class="dropdown-buttons-trigger">
<button id="dropdown-buttons-trigger" class="btn btn-secondary topbarbtn"
aria-label="Connect with source repository"><i class="fab fa-github"></i></button>
<div class="dropdown-buttons sourcebuttons">
<a class="repository-button"
href="https://github.com/AllenDowney/AstronomicalData"><button type="button" class="btn btn-secondary topbarbtn"
data-toggle="tooltip" data-placement="left" title="Source repository"><i
class="fab fa-github"></i>repository</button></a>
</div>
</div>
<!-- Full screen (wrap in <a> to have style consistency -->
<a class="full-screen-button"><button type="button" class="btn btn-secondary topbarbtn" data-toggle="tooltip"
data-placement="bottom" onclick="toggleFullScreen()" title="Fullscreen mode"><i
class="fas fa-expand"></i></button></a>
<!-- Launch buttons -->
<div class="dropdown-buttons-trigger">
<button id="dropdown-buttons-trigger" class="btn btn-secondary topbarbtn"
aria-label="Launch interactive content"><i class="fas fa-rocket"></i></button>
<div class="dropdown-buttons">
<a class="binder-button" href="https://mybinder.org/v2/gh/AllenDowney/AstronomicalData/master?urlpath=tree/class_notebooks/lesson_7.ipynb"><button type="button"
class="btn btn-secondary topbarbtn" title="Launch Binder" data-toggle="tooltip"
data-placement="left"><img class="binder-button-logo"
src="../_static/images/logo_binder.svg"
alt="Interact on binder">Binder</button></a>
<a class="colab-button" href="https://colab.research.google.com/github/AllenDowney/AstronomicalData/blob/master/class_notebooks/lesson_7.ipynb"><button type="button" class="btn btn-secondary topbarbtn"
title="Launch Colab" data-toggle="tooltip" data-placement="left"><img class="colab-button-logo"
src="../_static/images/logo_colab.png"
alt="Interact on Colab">Colab</button></a>
</div>
</div>
</div>
<!-- Table of contents -->
<div class="d-none d-md-block col-md-2 bd-toc show">
<div class="tocsection onthispage pt-5 pb-3">
<i class="fas fa-list"></i> Contents
</div>
<nav id="bd-toc-nav">
<ul class="nav section-nav flex-column">
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#upper-left">
Upper left
</a>
</li>
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#upper-right">
Upper right
</a>
</li>
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#lower-left">
Lower left
</a>
</li>
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#lower-right">
Lower right
</a>
</li>
</ul>
</nav>
</div>
</div>
<div id="main-content" class="row">
<div class="col-12 col-md-9 pl-md-3 pr-md-0">
<div>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</span>
<span class="kn">import</span> <span class="nn">matplotlib.pyplot</span> <span class="k">as</span> <span class="nn">plt</span>
<span class="o">%</span><span class="k">matplotlib</span> inline
<span class="kn">from</span> <span class="nn">matplotlib.patches</span> <span class="kn">import</span> <span class="n">Polygon</span>
<span class="kn">from</span> <span class="nn">astropy.table</span> <span class="kn">import</span> <span class="n">Table</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="plotting-functions">
<h1>Plotting Functions<a class="headerlink" href="#plotting-functions" title="Permalink to this headline"></a></h1>
<div class="section" id="upper-left">
<h2>Upper left<a class="headerlink" href="#upper-left" title="Permalink to this headline"></a></h2>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">plot_first_selection</span><span class="p">(</span><span class="n">df</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;Plot spatial distribution of stars selected based on Proper Motion</span>
<span class="sd"> </span>
<span class="sd"> df: Tale or DataFrame with proper motion selected coordinates in GD-1 reference frame</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">x</span> <span class="o">=</span> <span class="n">df</span><span class="p">[</span><span class="s1">&#39;phi1&#39;</span><span class="p">]</span>
<span class="n">y</span> <span class="o">=</span> <span class="n">df</span><span class="p">[</span><span class="s1">&#39;phi2&#39;</span><span class="p">]</span>
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="s1">&#39;ko&#39;</span><span class="p">,</span> <span class="n">markersize</span><span class="o">=</span><span class="mf">0.3</span><span class="p">,</span> <span class="n">alpha</span><span class="o">=</span><span class="mf">0.3</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">xlabel</span><span class="p">(</span><span class="s1">&#39;$\phi_1$ [deg]&#39;</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">ylabel</span><span class="p">(</span><span class="s1">&#39;$\phi_2$ [deg]&#39;</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">title</span><span class="p">(</span><span class="s1">&#39;Proper motion selection&#39;</span><span class="p">,</span> <span class="n">fontsize</span><span class="o">=</span><span class="s1">&#39;medium&#39;</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">axis</span><span class="p">(</span><span class="s1">&#39;equal&#39;</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">filename_pm_selected</span> <span class="o">=</span> <span class="s1">&#39;gd1_candidates.hdf5&#39;</span>
<span class="n">candidate_df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_hdf</span><span class="p">(</span><span class="n">filename_pm_selected</span><span class="p">,</span> <span class="s1">&#39;candidate_df&#39;</span><span class="p">)</span>
<span class="n">plot_first_selection</span><span class="p">(</span><span class="n">candidate_df</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<img alt="../_images/lesson_7_4_0.png" src="../_images/lesson_7_4_0.png" />
</div>
</div>
</div>
<div class="section" id="upper-right">
<h2>Upper right<a class="headerlink" href="#upper-right" title="Permalink to this headline"></a></h2>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">plot_proper_motion</span><span class="p">(</span><span class="n">df</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;Plot proper motion of spatially selected stars in GD-1</span>
<span class="sd"> </span>
<span class="sd"> df: Table or DataFrame with spatially selected proper motions in GD-1 reference frame</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">pm1</span> <span class="o">=</span> <span class="n">df</span><span class="p">[</span><span class="s1">&#39;pm_phi1&#39;</span><span class="p">]</span>
<span class="n">pm2</span> <span class="o">=</span> <span class="n">df</span><span class="p">[</span><span class="s1">&#39;pm_phi2&#39;</span><span class="p">]</span>
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">pm1</span><span class="p">,</span> <span class="n">pm2</span><span class="p">,</span> <span class="s1">&#39;ko&#39;</span><span class="p">,</span> <span class="n">markersize</span><span class="o">=</span><span class="mf">0.3</span><span class="p">,</span> <span class="n">alpha</span><span class="o">=</span><span class="mf">0.3</span><span class="p">)</span>
<span class="n">poly</span> <span class="o">=</span> <span class="n">Polygon</span><span class="p">(</span><span class="n">vertices</span><span class="p">,</span> <span class="n">closed</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
<span class="n">facecolor</span><span class="o">=</span><span class="s1">&#39;C1&#39;</span><span class="p">,</span> <span class="n">alpha</span><span class="o">=</span><span class="mf">0.4</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">gca</span><span class="p">()</span><span class="o">.</span><span class="n">add_patch</span><span class="p">(</span><span class="n">poly</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">xlabel</span><span class="p">(</span><span class="s1">&#39;$\mu_{\phi_1} [\mathrm{mas~yr}^{-1}]$&#39;</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">ylabel</span><span class="p">(</span><span class="s1">&#39;$\mu_{\phi_2} [\mathrm{mas~yr}^{-1}]$&#39;</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">xlim</span><span class="p">(</span><span class="o">-</span><span class="mi">12</span><span class="p">,</span> <span class="mi">8</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">ylim</span><span class="p">(</span><span class="o">-</span><span class="mi">10</span><span class="p">,</span> <span class="mi">10</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">pm1_min</span> <span class="o">=</span> <span class="o">-</span><span class="mf">8.9</span>
<span class="n">pm1_max</span> <span class="o">=</span> <span class="o">-</span><span class="mf">6.9</span>
<span class="n">pm2_min</span> <span class="o">=</span> <span class="o">-</span><span class="mf">2.2</span>
<span class="n">pm2_max</span> <span class="o">=</span> <span class="mf">1.0</span>
<span class="n">pm1_rect</span> <span class="o">=</span> <span class="p">[</span><span class="n">pm1_min</span><span class="p">,</span> <span class="n">pm1_min</span><span class="p">,</span> <span class="n">pm1_max</span><span class="p">,</span> <span class="n">pm1_max</span><span class="p">]</span>
<span class="n">pm2_rect</span> <span class="o">=</span> <span class="p">[</span><span class="n">pm2_min</span><span class="p">,</span> <span class="n">pm2_max</span><span class="p">,</span> <span class="n">pm2_max</span><span class="p">,</span> <span class="n">pm2_min</span><span class="p">]</span>
</pre></div>
</div>
</div>
</div>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">vertices</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">transpose</span><span class="p">([</span><span class="n">pm1_rect</span><span class="p">,</span> <span class="n">pm2_rect</span><span class="p">])</span>
</pre></div>
</div>
</div>
</div>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">filename_pm</span> <span class="o">=</span> <span class="s1">&#39;gd1_dataframe.hdf5&#39;</span>
<span class="n">centerline</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_hdf</span><span class="p">(</span><span class="n">filename_pm</span><span class="p">,</span> <span class="s1">&#39;centerline&#39;</span><span class="p">)</span>
<span class="n">plot_proper_motion</span><span class="p">(</span><span class="n">centerline</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<img alt="../_images/lesson_7_9_0.png" src="../_images/lesson_7_9_0.png" />
</div>
</div>
</div>
<div class="section" id="lower-left">
<h2>Lower left<a class="headerlink" href="#lower-left" title="Permalink to this headline"></a></h2>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">plot_second_selection</span><span class="p">(</span><span class="n">df</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;Plot spatial distribution of photometry selected points</span>
<span class="sd"> </span>
<span class="sd"> df: Table or DataFrame with photometry selected positions in GD1 reference frame</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">x</span> <span class="o">=</span> <span class="n">df</span><span class="p">[</span><span class="s1">&#39;phi1&#39;</span><span class="p">]</span>
<span class="n">y</span> <span class="o">=</span> <span class="n">df</span><span class="p">[</span><span class="s1">&#39;phi2&#39;</span><span class="p">]</span>
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="s1">&#39;ko&#39;</span><span class="p">,</span> <span class="n">markersize</span><span class="o">=</span><span class="mf">0.7</span><span class="p">,</span> <span class="n">alpha</span><span class="o">=</span><span class="mf">0.9</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">xlabel</span><span class="p">(</span><span class="s1">&#39;$\phi_1$ [deg]&#39;</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">ylabel</span><span class="p">(</span><span class="s1">&#39;$\phi_2$ [deg]&#39;</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">title</span><span class="p">(</span><span class="s1">&#39;Proper motion + photometry selection&#39;</span><span class="p">,</span> <span class="n">fontsize</span><span class="o">=</span><span class="s1">&#39;medium&#39;</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">axis</span><span class="p">(</span><span class="s1">&#39;equal&#39;</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">filename</span> <span class="o">=</span> <span class="s1">&#39;gd1_merged.hdf5&#39;</span>
<span class="n">selected</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_hdf</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="s1">&#39;selected&#39;</span><span class="p">)</span>
<span class="n">plot_second_selection</span><span class="p">(</span><span class="n">selected</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<img alt="../_images/lesson_7_12_0.png" src="../_images/lesson_7_12_0.png" />
</div>
</div>
</div>
<div class="section" id="lower-right">
<h2>Lower right<a class="headerlink" href="#lower-right" title="Permalink to this headline"></a></h2>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">plot_cmd</span><span class="p">(</span><span class="n">table</span><span class="p">):</span>
<span class="sd">&quot;&quot;&quot;Plot a color magnitude diagram.</span>
<span class="sd"> </span>
<span class="sd"> table: Table or DataFrame with photometry data</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">y</span> <span class="o">=</span> <span class="n">table</span><span class="p">[</span><span class="s1">&#39;g_mean_psf_mag&#39;</span><span class="p">]</span>
<span class="n">x</span> <span class="o">=</span> <span class="n">table</span><span class="p">[</span><span class="s1">&#39;g_mean_psf_mag&#39;</span><span class="p">]</span> <span class="o">-</span> <span class="n">table</span><span class="p">[</span><span class="s1">&#39;i_mean_psf_mag&#39;</span><span class="p">]</span>
<span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="s1">&#39;ko&#39;</span><span class="p">,</span> <span class="n">markersize</span><span class="o">=</span><span class="mf">0.3</span><span class="p">,</span> <span class="n">alpha</span><span class="o">=</span><span class="mf">0.3</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">xlim</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span> <span class="mf">1.5</span><span class="p">])</span>
<span class="n">plt</span><span class="o">.</span><span class="n">ylim</span><span class="p">([</span><span class="mi">14</span><span class="p">,</span> <span class="mi">22</span><span class="p">])</span>
<span class="n">plt</span><span class="o">.</span><span class="n">gca</span><span class="p">()</span><span class="o">.</span><span class="n">invert_yaxis</span><span class="p">()</span>
<span class="n">plt</span><span class="o">.</span><span class="n">ylabel</span><span class="p">(</span><span class="s1">&#39;$g_0$&#39;</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">xlabel</span><span class="p">(</span><span class="s1">&#39;$(g-i)_0$&#39;</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">filename_poly</span> <span class="o">=</span> <span class="s1">&#39;gd1_polygon.hdf5&#39;</span>
<span class="n">coords_df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_hdf</span><span class="p">(</span><span class="n">filename_poly</span><span class="p">,</span> <span class="s1">&#39;coords_df&#39;</span><span class="p">)</span>
<span class="n">coords</span> <span class="o">=</span> <span class="n">coords_df</span><span class="o">.</span><span class="n">to_numpy</span><span class="p">()</span>
</pre></div>
</div>
</div>
</div>
<div class="cell docutils container">
<div class="cell_input docutils container">
<div class="highlight-ipython3 notranslate"><div class="highlight"><pre><span></span><span class="n">merged</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_hdf</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="s1">&#39;merged&#39;</span><span class="p">)</span>
<span class="n">plot_cmd</span><span class="p">(</span><span class="n">merged</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="cell_output docutils container">
<img alt="../_images/lesson_7_16_0.png" src="../_images/lesson_7_16_0.png" />
</div>
</div>
</div>
</div>
<script type="text/x-thebe-config">
{
requestKernel: true,
binderOptions: {
repo: "binder-examples/jupyter-stacks-datascience",
ref: "master",
},
codeMirrorConfig: {
theme: "abcdef",
mode: "python"
},
kernelOptions: {
kernelName: "python3",
path: "./class_notebooks"
},
predefinedOutput: true
}
</script>
<script>kernelName = 'python3'</script>
</div>
</div>
</div>
<div class='prev-next-bottom'>
</div>
<footer class="footer mt-5 mt-md-0">
<div class="container">
<p>
By Allen B. Downey<br/>
&copy; Copyright 2020.<br/>
</p>
</div>
</footer>
</main>
</div>
</div>
<script src="../_static/js/index.30270b6e4c972e43c488.js"></script>
</body>
</html>

Some files were not shown because too many files have changed in this diff Show More