forked from python4astronomers/python4astronomers.github.com
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathatpy.html
More file actions
495 lines (453 loc) · 18.4 KB
/
Copy pathatpy.html
File metadata and controls
495 lines (453 loc) · 18.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>ATpy — Python4Astronomers 1.1 documentation</title>
<link rel="stylesheet" href="../_static/sphinxdoc.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: '../',
VERSION: '1.1',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="../_static/jquery.js"></script>
<script type="text/javascript" src="../_static/underscore.js"></script>
<script type="text/javascript" src="../_static/doctools.js"></script>
<link rel="top" title="Python4Astronomers 1.1 documentation" href="../index.html" />
<link rel="up" title="Reading and Writing files" href="files.html" />
<link rel="next" title="Summary and excercise" href="summary.html" />
<link rel="prev" title="Binary formats useful for astronomers" href="binaryfiles.html" />
<script type="text/javascript">
$(document).ready(function(){
$(".flip0").click(function(){
$(".panel0").slideToggle("normal");
});
$(".flip1").click(function(){
$(".panel1").slideToggle("normal");
});
$(".flip2").click(function(){
$(".panel2").slideToggle("normal");
});
$(".flip3").click(function(){
$(".panel3").slideToggle("normal");
});
$(".flip4").click(function(){
$(".panel4").slideToggle("normal");
});
$(".flip5").click(function(){
$(".panel5").slideToggle("normal");
});
$(".flip6").click(function(){
$(".panel6").slideToggle("normal");
});
$(".flip7").click(function(){
$(".panel7").slideToggle("normal");
});
$(".flip8").click(function(){
$(".panel8").slideToggle("normal");
});
$(".flip9").click(function(){
$(".panel9").slideToggle("normal");
});
});
</script>
<style type="text/css">
div.panel0,p.flip0
{
font-size: 0.9em;
margin: 0;
padding: 0.1em 0 0.1em 0.5em;
border-bottom: 1px solid #86989B;
}
p.flip0
{
color: white;
font-weight: bold;
background-color: #AFC1C4;
}
div.panel0
{
display:none;
}
div.panel1,p.flip1
{
font-size: 0.9em;
margin: 0;
padding: 0.1em 0 0.1em 0.5em;
border-bottom: 1px solid #86989B;
}
p.flip1
{
color: white;
font-weight: bold;
background-color: #AFC1C4;
}
div.panel1
{
display:none;
}
div.panel2,p.flip2
{
font-size: 0.9em;
margin: 0;
padding: 0.1em 0 0.1em 0.5em;
border-bottom: 1px solid #86989B;
}
p.flip2
{
color: white;
font-weight: bold;
background-color: #AFC1C4;
}
div.panel2
{
display:none;
}
div.panel3,p.flip3
{
font-size: 0.9em;
margin: 0;
padding: 0.1em 0 0.1em 0.5em;
border-bottom: 1px solid #86989B;
}
p.flip3
{
color: white;
font-weight: bold;
background-color: #AFC1C4;
}
div.panel3
{
display:none;
}
div.panel4,p.flip4
{
font-size: 0.9em;
margin: 0;
padding: 0.1em 0 0.1em 0.5em;
border-bottom: 1px solid #86989B;
}
p.flip4
{
color: white;
font-weight: bold;
background-color: #AFC1C4;
}
div.panel4
{
display:none;
}
div.panel5,p.flip5
{
font-size: 0.9em;
margin: 0;
padding: 0.1em 0 0.1em 0.5em;
border-bottom: 1px solid #86989B;
}
p.flip5
{
color: white;
font-weight: bold;
background-color: #AFC1C4;
}
div.panel5
{
display:none;
}
div.panel6,p.flip6
{
font-size: 0.9em;
margin: 0;
padding: 0.1em 0 0.1em 0.5em;
border-bottom: 1px solid #86989B;
}
p.flip6
{
color: white;
font-weight: bold;
background-color: #AFC1C4;
}
div.panel6
{
display:none;
}
div.panel7,p.flip7
{
font-size: 0.9em;
margin: 0;
padding: 0.1em 0 0.1em 0.5em;
border-bottom: 1px solid #86989B;
}
p.flip7
{
color: white;
font-weight: bold;
background-color: #AFC1C4;
}
div.panel7
{
display:none;
}
div.panel8,p.flip8
{
font-size: 0.9em;
margin: 0;
padding: 0.1em 0 0.1em 0.5em;
border-bottom: 1px solid #86989B;
}
p.flip8
{
color: white;
font-weight: bold;
background-color: #AFC1C4;
}
div.panel8
{
display:none;
}
div.panel9,p.flip9
{
font-size: 0.9em;
margin: 0;
padding: 0.1em 0 0.1em 0.5em;
border-bottom: 1px solid #86989B;
}
p.flip9
{
color: white;
font-weight: bold;
background-color: #AFC1C4;
}
div.panel9
{
display:none;
}
</style>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-18709417-2']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
</head>
<body>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="summary.html" title="Summary and excercise"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="binaryfiles.html" title="Binary formats useful for astronomers"
accesskey="P">previous</a> |</li>
<li><a href="../index.html">Python4Astronomers 1.1 documentation</a> »</li>
<li><a href="files.html" accesskey="U">Reading and Writing files</a> »</li>
</ul>
</div>
<div class="sphinxsidebar">
<div class="sphinxsidebarwrapper">
<h3><a href="../index.html">Table Of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">ATpy</a><ul>
<li><a class="reference internal" href="#reading-in-an-existing-table">Reading in an existing table</a></li>
<li><a class="reference internal" href="#creating-a-table-from-scratch">Creating a table from scratch</a></li>
<li><a class="reference internal" href="#and-much-more">And much more!</a></li>
</ul>
</li>
</ul>
<h4>Previous topic</h4>
<p class="topless"><a href="binaryfiles.html"
title="previous chapter">Binary formats useful for astronomers</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="summary.html"
title="next chapter">Summary and excercise</a></p>
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="../_sources/files/atpy.txt"
rel="nofollow">Show Source</a></li>
</ul>
<div id="searchbox" style="display: none">
<h3>Quick search</h3>
<form class="search" action="../search.html" method="get">
<input type="text" name="q" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
<p class="searchtip" style="font-size: 90%">
Enter search terms or a module, class or function name.
</p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body">
<div class="section" id="atpy">
<h1>ATpy<a class="headerlink" href="#atpy" title="Permalink to this headline">¶</a></h1>
<p><a class="reference external" href="http://atpy.github.com">ATpy</a> is a high-level Python package providing a way to manipulate tables of astronomical data in a uniform way. The two main features of ATpy are:</p>
<ul class="simple">
<li>It provides an abstract Table object that contains tabular data along with
meta-data to describe the columns, and methods to manipulate the table (e.g.
adding/removing/renaming columns, selecting rows, changing values, sorting,
...).</li>
<li>It provides built-in support for reading and writing to several common
file/database formats, including FITS, VO, HDF5, and ASCII tables, as well
as SQLite, MySQL and PostgreSQL databases, with a very simple interface.</li>
</ul>
<div class="section" id="reading-in-an-existing-table">
<h2>Reading in an existing table<a class="headerlink" href="#reading-in-an-existing-table" title="Permalink to this headline">¶</a></h2>
<p>First, download <a class="reference download internal" href="../_downloads/catalog.fits.gz"><tt class="xref download docutils literal"><span class="pre">this</span></tt></a> FITS table, then launch IPython in the directory where you have the file:</p>
<div class="highlight-python"><pre>$ ipython --pylab</pre>
</div>
<p>If you have trouble downloading the file, then start up IPython (<tt class="docutils literal"><span class="pre">ipython</span> <span class="pre">--pylab</span></tt>) and enter:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">urllib2</span>
<span class="n">url</span> <span class="o">=</span> <span class="s">'http://python4astronomers.github.com/_downloads/catalog.fits.gz'</span>
<span class="nb">open</span><span class="p">(</span><span class="s">'catalog.fits.gz'</span><span class="p">,</span> <span class="s">'wb'</span><span class="p">)</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">urllib2</span><span class="o">.</span><span class="n">urlopen</span><span class="p">(</span><span class="n">url</span><span class="p">)</span><span class="o">.</span><span class="n">read</span><span class="p">())</span>
<span class="n">ls</span>
</pre></div>
</div>
<p>Then import <tt class="docutils literal"><span class="pre">atpy</span></tt>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">atpy</span>
</pre></div>
</div>
<p>and read in the table:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">t</span> <span class="o">=</span> <span class="n">atpy</span><span class="o">.</span><span class="n">Table</span><span class="p">(</span><span class="s">'catalog.fits.gz'</span><span class="p">)</span>
</pre></div>
</div>
<p>We can now see what columns are available in the table:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">t</span><span class="o">.</span><span class="n">describe</span><span class="p">()</span>
<span class="go">Table : MY CATALOG</span>
<span class="go">--------------------------------------</span>
<span class="go">| Name | Unit | Type | Format |</span>
<span class="go">--------------------------------------</span>
<span class="go">| SOURCEID | | |S20 | 68s |</span>
<span class="go">| RA | RADIANS | >f8 | 25.17e |</span>
<span class="go">| DEC | RADIANS | >f8 | 25.17e |</span>
<span class="go">| EPOCH | | >f8 | 25.17e |</span>
<span class="go">| LAMBDA | | >f8 | 25.17e |</span>
<span class="go">| ETA | | >f8 | 25.17e |</span>
<span class="go">| DISTANCE | | >f8 | 25.17e |</span>
<span class="go">--------------------------------------</span>
</pre></div>
</div>
<p>and how many lines there are:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="nb">len</span><span class="p">(</span><span class="n">t</span><span class="p">)</span>
<span class="go">1002</span>
</pre></div>
</div>
<p>To access a particular column, use:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">t</span><span class="p">[</span><span class="s">'RA'</span><span class="p">]</span>
<span class="go">array([ 4.64681193, 4.64686867, 4.64682644, ..., 4.64694334,</span>
<span class="go"> 4.64690389, 4.64698136])</span>
</pre></div>
</div>
<p>As you can see, a single column is returned as a Numpy array, so any Numpy operation will work on this:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">t</span><span class="p">[</span><span class="s">'RA'</span><span class="p">]</span><span class="o">.</span><span class="n">min</span><span class="p">()</span>
<span class="go">4.6465723005143351</span>
<span class="gp">>>> </span><span class="n">t</span><span class="p">[</span><span class="s">'RA'</span><span class="p">]</span><span class="o">.</span><span class="n">max</span><span class="p">()</span>
<span class="go">4.6472357622326737</span>
</pre></div>
</div>
<p>and you can then specify a particular row to access a single value:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">t</span><span class="p">[</span><span class="s">'RA'</span><span class="p">][</span><span class="mi">4</span><span class="p">]</span>
<span class="go">4.6467757499903897</span>
</pre></div>
</div>
<p>Let’s select a subset of the table:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">tsub</span> <span class="o">=</span> <span class="n">t</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">t</span><span class="p">[</span><span class="s">'RA'</span><span class="p">]</span> <span class="o"><</span> <span class="mf">4.647</span><span class="p">)</span>
<span class="gp">>>> </span><span class="nb">len</span><span class="p">(</span><span class="n">tsub</span><span class="p">)</span>
<span class="go">742</span>
</pre></div>
</div>
<p>And we can also remove the <tt class="docutils literal"><span class="pre">'DISTANCE'</span></tt> column which we don’t care about:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">tsub</span><span class="o">.</span><span class="n">remove_columns</span><span class="p">(</span><span class="s">'DISTANCE'</span><span class="p">)</span>
</pre></div>
</div>
<p>As you can see, the column is no longer there:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">tsub</span><span class="o">.</span><span class="n">describe</span><span class="p">()</span>
<span class="go">Table : MY CATALOG</span>
<span class="go">--------------------------------------</span>
<span class="go">| Name | Unit | Type | Format |</span>
<span class="go">--------------------------------------</span>
<span class="go">| SOURCEID | | |S20 | 68s |</span>
<span class="go">| RA | RADIANS | >f8 | 25.17e |</span>
<span class="go">| DEC | RADIANS | >f8 | 25.17e |</span>
<span class="go">| EPOCH | | >f8 | 25.17e |</span>
<span class="go">| LAMBDA | | >f8 | 25.17e |</span>
<span class="go">| ETA | | >f8 | 25.17e |</span>
<span class="go">--------------------------------------</span>
</pre></div>
</div>
<p>We can sort the table by Declination:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">tsub</span><span class="o">.</span><span class="n">sort</span><span class="p">(</span><span class="s">'DEC'</span><span class="p">)</span>
</pre></div>
</div>
<p>Finally, let’s write out this sub-table as an IPAC table:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">tsub</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">'subset.tbl'</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="creating-a-table-from-scratch">
<h2>Creating a table from scratch<a class="headerlink" href="#creating-a-table-from-scratch" title="Permalink to this headline">¶</a></h2>
<p>Making a table from scratch is also very easy:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">t</span> <span class="o">=</span> <span class="n">atpy</span><span class="o">.</span><span class="n">Table</span><span class="p">()</span>
<span class="n">t</span><span class="o">.</span><span class="n">add_column</span><span class="p">(</span><span class="s">'time'</span><span class="p">,</span> <span class="p">[</span><span class="mf">1.</span><span class="p">,</span> <span class="mf">2.</span><span class="p">,</span> <span class="mi">3</span><span class="p">])</span>
<span class="n">t</span><span class="o">.</span><span class="n">add_column</span><span class="p">(</span><span class="s">'flux'</span><span class="p">,</span> <span class="p">[</span><span class="mf">7.2</span><span class="p">,</span> <span class="mf">1.1</span><span class="p">,</span> <span class="mf">5.9</span><span class="p">])</span>
</pre></div>
</div>
<p>and you can then write this out to e.g. a FITS file:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">t</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">'simple.fits'</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="and-much-more">
<h2>And much more!<a class="headerlink" href="#and-much-more" title="Permalink to this headline">¶</a></h2>
<p>For more information on how to use ATpy, see the <a class="reference external" href="http://atpy.github.com/#documentation">Documentation</a>.</p>
</div>
</div>
</div>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="summary.html" title="Summary and excercise"
>next</a> |</li>
<li class="right" >
<a href="binaryfiles.html" title="Binary formats useful for astronomers"
>previous</a> |</li>
<li><a href="../index.html">Python4Astronomers 1.1 documentation</a> »</li>
<li><a href="files.html" >Reading and Writing files</a> »</li>
</ul>
</div>
<small>Copyright:
Smithsonian Astrophysical Observatory under terms of
<a rel="license"
href="http://creativecommons.org/licenses/by/3.0/">CC
Attribution 3.0</a></small> <a rel="license"
href="http://creativecommons.org/licenses/by/3.0/"><img alt="Creative Commons
License" style="border-width:0"
src="http://i.creativecommons.org/l/by/3.0/80x15.png" /></a>
</body>
</html>