Skip to content

Commit c0368f3

Browse files
authored
Merge pull request #179 from eblur/tutorial_workshop_PR1
Finished tutorials from UW-Madison Tutorial Code Sprinting Workshop
2 parents 5b37c2c + 117d5e5 commit c0368f3

File tree

13 files changed

+3108
-2
lines changed

13 files changed

+3108
-2
lines changed

conda-environment.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@ dependencies:
77
- python=3.6
88
- IPython=6.1.0
99
- astropy=2.0
10-
- astroquery=0.3.6
10+
- astroquery=0.3.7
1111
- matplotlib=2.0.2
1212
- numpy=1.13.1
1313
- scipy=0.19 # needed for coordinates cross-matching
1414
- jupyter=1.0
1515
- notebook=5.0
16+
- aplpy # used in FITS-cube tutorial
17+
- spectral-cube # used in FITS-cube tutorial
18+
- reproject # used in FITS-cube tutorial

pip-requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
IPython==6.1.0
22
astropy==2.0.2
3-
astroquery==0.3.6
3+
astroquery==0.3.7
44
matplotlib==2.0.2
55
numpy==1.13.1
66
jupyter==1.0
Lines changed: 347 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,347 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Getting Started with astropy.coordinates"
8+
]
9+
},
10+
{
11+
"cell_type": "markdown",
12+
"metadata": {},
13+
"source": [
14+
"## Authors:\n",
15+
"Erik Tollerud, Kelle Cruz, Stephen Pardy\n",
16+
"\n",
17+
"## Learning Goals\n",
18+
"- Create `astropy.coordinates.SkyCoord` objects using names and coordinates\n",
19+
"- Interact with a `SkyCoord` object and access its attributes\n",
20+
"- Use a `SkyCoord` object to query a database\n",
21+
"\n",
22+
"## Keywords:\n",
23+
"astropy.coordinates, OOP\n",
24+
"\n",
25+
"In this tutorial, we're going to investigate the area of the sky around the picturesque group of galaxies named \"Hickson Compact Group 7\", download an image and do something with its coordinates."
26+
]
27+
},
28+
{
29+
"cell_type": "markdown",
30+
"metadata": {},
31+
"source": [
32+
"## Imports"
33+
]
34+
},
35+
{
36+
"cell_type": "code",
37+
"execution_count": null,
38+
"metadata": {
39+
"collapsed": true
40+
},
41+
"outputs": [],
42+
"source": [
43+
"# Python standard-library\n",
44+
"from urllib.parse import urlencode\n",
45+
"from urllib.request import urlretrieve\n",
46+
"\n",
47+
"# Third-party dependencies\n",
48+
"from astropy import units as u\n",
49+
"from astropy.coordinates import SkyCoord\n",
50+
"from astropy.table import Table\n",
51+
"import numpy as np\n",
52+
"from IPython.display import Image\n",
53+
"\n",
54+
"# Set up matplotlib and use a nicer set of plot parameters\n",
55+
"from astropy.visualization import astropy_mpl_style\n",
56+
"import matplotlib.pyplot as plt\n",
57+
"plt.style.use(astropy_mpl_style)\n",
58+
"%matplotlib inline"
59+
]
60+
},
61+
{
62+
"cell_type": "markdown",
63+
"metadata": {},
64+
"source": [
65+
"## Describing on-sky locations with `coordinates`"
66+
]
67+
},
68+
{
69+
"cell_type": "markdown",
70+
"metadata": {},
71+
"source": [
72+
"The `SkyCoord` class in the `astropy.coordinates` package is used to represent celestial coordinates. First, we'll make a SkyCoord object based on our object's name, \"Hickson Compact Group 7\", or \"HCG 7\" for short. Most astronomical object names can be found by [SESAME](http://cdsweb.u-strasbg.fr/cgi-bin/Sesame), a service which queries Simbad, NED, and VizieR and returns the object's type and its J2000 position. This service can be used via the `SkyCoord.from_name()` [class method](https://julien.danjou.info/blog/2013/guide-python-static-class-abstract-methods):"
73+
]
74+
},
75+
{
76+
"cell_type": "code",
77+
"execution_count": null,
78+
"metadata": {
79+
"collapsed": true
80+
},
81+
"outputs": [],
82+
"source": [
83+
"# initialize a SkyCood object named hcg7_center at the location of HCG 7\n",
84+
"hcg7_center = SkyCoord.from_name('HCG 7')"
85+
]
86+
},
87+
{
88+
"cell_type": "markdown",
89+
"metadata": {},
90+
"source": [
91+
"<div class=\"alert alert-info\">\n",
92+
"Note that this requires an internet connection. If you don't have one, execute this line instead:\n",
93+
"</div>"
94+
]
95+
},
96+
{
97+
"cell_type": "code",
98+
"execution_count": null,
99+
"metadata": {
100+
"collapsed": true
101+
},
102+
"outputs": [],
103+
"source": [
104+
"# uncomment and run this line if you don't have an internet connection\n",
105+
"# hcg7_center = SkyCoord(9.81625*u.deg, 0.88806*u.deg, frame='icrs')"
106+
]
107+
},
108+
{
109+
"cell_type": "code",
110+
"execution_count": null,
111+
"metadata": {},
112+
"outputs": [],
113+
"source": [
114+
"type(hcg7_center)"
115+
]
116+
},
117+
{
118+
"cell_type": "markdown",
119+
"metadata": {},
120+
"source": [
121+
"Show the available methods and attributes of the SkyCoord object we've created called `hcg7_center`"
122+
]
123+
},
124+
{
125+
"cell_type": "code",
126+
"execution_count": null,
127+
"metadata": {},
128+
"outputs": [],
129+
"source": [
130+
"dir(hcg7_center)"
131+
]
132+
},
133+
{
134+
"cell_type": "markdown",
135+
"metadata": {},
136+
"source": [
137+
"Show the RA and Dec."
138+
]
139+
},
140+
{
141+
"cell_type": "code",
142+
"execution_count": null,
143+
"metadata": {},
144+
"outputs": [],
145+
"source": [
146+
"print(hcg7_center.ra, hcg7_center.dec)\n",
147+
"print(hcg7_center.ra.hour, hcg7_center.dec)"
148+
]
149+
},
150+
{
151+
"cell_type": "markdown",
152+
"metadata": {},
153+
"source": [
154+
"We see that, according to SESAME, HCG 7 is located at ra = 9.849 deg and dec = 0.878 deg. "
155+
]
156+
},
157+
{
158+
"cell_type": "markdown",
159+
"metadata": {},
160+
"source": [
161+
"This object we just created has various useful ways of accessing the information contained within it. In particular, the ``ra`` and ``dec`` attributes are specialized [``Quantity``](http://docs.astropy.org/en/stable/units/index.html) objects (actually, a subclass called [``Angle``](http://docs.astropy.org/en/stable/api/astropy.coordinates.Angle.html), which in turn is subclassed by [``Latitude``](http://docs.astropy.org/en/stable/api/astropy.coordinates.Latitude.html) and [``Longitude``](http://docs.astropy.org/en/stable/api/astropy.coordinates.Longitude.html)). These objects store angles and provide pretty representations of those angles, as well as some useful attributes to quickly convert to common angle units:"
162+
]
163+
},
164+
{
165+
"cell_type": "code",
166+
"execution_count": null,
167+
"metadata": {},
168+
"outputs": [],
169+
"source": [
170+
"type(hcg7_center.ra), type(hcg7_center.dec)"
171+
]
172+
},
173+
{
174+
"cell_type": "code",
175+
"execution_count": null,
176+
"metadata": {},
177+
"outputs": [],
178+
"source": [
179+
"hcg7_center.ra, hcg7_center.dec"
180+
]
181+
},
182+
{
183+
"cell_type": "code",
184+
"execution_count": null,
185+
"metadata": {},
186+
"outputs": [],
187+
"source": [
188+
"hcg7_center"
189+
]
190+
},
191+
{
192+
"cell_type": "code",
193+
"execution_count": null,
194+
"metadata": {
195+
"collapsed": true
196+
},
197+
"outputs": [],
198+
"source": [
199+
"hcg7_center.ra.hour"
200+
]
201+
},
202+
{
203+
"cell_type": "markdown",
204+
"metadata": {},
205+
"source": [
206+
"SkyCoord will also accept string-formatted coordinates either as separate strings for ra/dec or a single string. You'll have to give units, though, if they aren't part of the string itself."
207+
]
208+
},
209+
{
210+
"cell_type": "code",
211+
"execution_count": null,
212+
"metadata": {
213+
"collapsed": true,
214+
"scrolled": false
215+
},
216+
"outputs": [],
217+
"source": [
218+
"SkyCoord('0h39m15.9s', '0d53m17.016s', frame='icrs')"
219+
]
220+
},
221+
{
222+
"cell_type": "code",
223+
"execution_count": null,
224+
"metadata": {
225+
"collapsed": true
226+
},
227+
"outputs": [],
228+
"source": [
229+
"hcg7_center.ra.hour"
230+
]
231+
},
232+
{
233+
"cell_type": "markdown",
234+
"metadata": {},
235+
"source": [
236+
"# Download an image\n",
237+
"Now that we have a `SkyCoord` object, we can try to use it to access data from the [Sloan Digitial Sky Survey](http://www.sdss.org/) (SDSS). Let's start by trying to get a picture using the SDSS image cutout service to make sure HCG7 is in the SDSS footprint and has good image quality.\n",
238+
"\n",
239+
"This requires an internet connection, but if it fails, don't worry: the file is included in the repository so you can just let it use the local file``'HCG7_SDSS_cutout.jpg'``, defined at the top of the cell. "
240+
]
241+
},
242+
{
243+
"cell_type": "code",
244+
"execution_count": null,
245+
"metadata": {
246+
"collapsed": true
247+
},
248+
"outputs": [],
249+
"source": [
250+
"# tell the SDSS service how big of a cutout we want\n",
251+
"im_size = 12*u.arcmin # get a 12 arcmin square\n",
252+
"im_pixels = 1024 \n",
253+
"cutoutbaseurl = 'http://skyservice.pha.jhu.edu/DR12/ImgCutout/getjpeg.aspx'\n",
254+
"query_string = urlencode(dict(ra=hcg7_center.ra.deg, \n",
255+
" dec=hcg7_center.dec.deg, \n",
256+
" width=im_pixels, height=im_pixels, \n",
257+
" scale=im_size.to(u.arcsec).value/im_pixels))\n",
258+
"url = cutoutbaseurl + '?' + query_string\n",
259+
"\n",
260+
"# this downloads the image to your disk\n",
261+
"urlretrieve(url, 'HCG7_SDSS_cutout.jpg')"
262+
]
263+
},
264+
{
265+
"cell_type": "code",
266+
"execution_count": null,
267+
"metadata": {
268+
"collapsed": true
269+
},
270+
"outputs": [],
271+
"source": [
272+
"Image('HCG7_SDSS_cutout.jpg')"
273+
]
274+
},
275+
{
276+
"cell_type": "markdown",
277+
"metadata": {},
278+
"source": [
279+
"Very pretty!"
280+
]
281+
},
282+
{
283+
"cell_type": "markdown",
284+
"metadata": {},
285+
"source": [
286+
"### Exercise 1"
287+
]
288+
},
289+
{
290+
"cell_type": "markdown",
291+
"metadata": {},
292+
"source": [
293+
"Create a `SkyCoord` of some other astronomical object you find interesting. Using only a single method/function call, get a string with the RA/Dec in the form 'HH:MM:SS.S DD:MM:SS.S'. Check your answer against an academic paper or some web site like [SIMBAD](http://simbad.u-strasbg.fr/simbad/) that will show you sexigesimal coordinates for the object.\n",
294+
"\n",
295+
"(Hint: `SkyCoord.to_string()` might be worth reading up on)"
296+
]
297+
},
298+
{
299+
"cell_type": "code",
300+
"execution_count": null,
301+
"metadata": {
302+
"collapsed": true
303+
},
304+
"outputs": [],
305+
"source": []
306+
},
307+
{
308+
"cell_type": "markdown",
309+
"metadata": {},
310+
"source": [
311+
"Now get an image of that object from the Digitized Sky Survey and download it and/or show it in the notebook. Bonus points if you figure out the (one-line) trick to get it to display in the notebook *without* ever downloading the file yourself.\n",
312+
"\n",
313+
"(Hint: STScI has an easy-to-access [copy of the DSS](https://archive.stsci.edu/dss/). The pattern to follow for the web URL is ``http://archive.stsci.edu/cgi-bin/dss_search?f=GIF&ra=RA&dec=DEC``)"
314+
]
315+
},
316+
{
317+
"cell_type": "code",
318+
"execution_count": null,
319+
"metadata": {
320+
"collapsed": true
321+
},
322+
"outputs": [],
323+
"source": []
324+
}
325+
],
326+
"metadata": {
327+
"kernelspec": {
328+
"display_name": "Python [default]",
329+
"language": "python",
330+
"name": "python3"
331+
},
332+
"language_info": {
333+
"codemirror_mode": {
334+
"name": "ipython",
335+
"version": 3
336+
},
337+
"file_extension": ".py",
338+
"mimetype": "text/x-python",
339+
"name": "python",
340+
"nbconvert_exporter": "python",
341+
"pygments_lexer": "ipython3",
342+
"version": "3.6.2"
343+
}
344+
},
345+
"nbformat": 4,
346+
"nbformat_minor": 2
347+
}

0 commit comments

Comments
 (0)