|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Viewing and manipulating data from FITS tables\n", |
| 8 | + "\n", |
| 9 | + "## Authors\n", |
| 10 | + "Lia Corrales, Kris Stern\n", |
| 11 | + "\n", |
| 12 | + "## Learning Goals\n", |
| 13 | + "* Download a FITS table file from a URL \n", |
| 14 | + "* Open a FITS table file and view table contents\n", |
| 15 | + "* Make a 2D histogram with the table data\n", |
| 16 | + "* Close the FITS file after use\n", |
| 17 | + "\n", |
| 18 | + "## Keywords\n", |
| 19 | + "FITS, file input/output, table, numpy, matplotlib, histogram\n", |
| 20 | + "\n", |
| 21 | + "\n", |
| 22 | + "## Summary\n", |
| 23 | + "\n", |
| 24 | + "This tutorial demonstrates the use of `astropy.utils.data` to download a data file, then uses `astropy.io.fits` and `astropy.table` to open the file. Lastly, `matplotlib` is used to visualize the data as a histogram." |
| 25 | + ] |
| 26 | + }, |
| 27 | + { |
| 28 | + "cell_type": "code", |
| 29 | + "execution_count": null, |
| 30 | + "metadata": {}, |
| 31 | + "outputs": [], |
| 32 | + "source": [ |
| 33 | + "import numpy as np\n", |
| 34 | + "from astropy.io import fits\n", |
| 35 | + "from astropy.table import Table\n", |
| 36 | + "from matplotlib.colors import LogNorm\n", |
| 37 | + "\n", |
| 38 | + "# Set up matplotlib\n", |
| 39 | + "import matplotlib.pyplot as plt\n", |
| 40 | + "\n", |
| 41 | + "%matplotlib inline" |
| 42 | + ] |
| 43 | + }, |
| 44 | + { |
| 45 | + "cell_type": "markdown", |
| 46 | + "metadata": {}, |
| 47 | + "source": [ |
| 48 | + "The following line is needed to download the example FITS files used in this tutorial." |
| 49 | + ] |
| 50 | + }, |
| 51 | + { |
| 52 | + "cell_type": "code", |
| 53 | + "execution_count": null, |
| 54 | + "metadata": {}, |
| 55 | + "outputs": [], |
| 56 | + "source": [ |
| 57 | + "from astropy.utils.data import download_file" |
| 58 | + ] |
| 59 | + }, |
| 60 | + { |
| 61 | + "cell_type": "markdown", |
| 62 | + "metadata": {}, |
| 63 | + "source": [ |
| 64 | + "FITS files often contain large amounts of multi-dimensional data and tables. \n", |
| 65 | + "\n", |
| 66 | + "In this particular example, we'll open a FITS file from a Chandra observation of the Galactic Center. The file contains a list of events with x and y coordinates, energy, and various other pieces of information." |
| 67 | + ] |
| 68 | + }, |
| 69 | + { |
| 70 | + "cell_type": "code", |
| 71 | + "execution_count": null, |
| 72 | + "metadata": {}, |
| 73 | + "outputs": [], |
| 74 | + "source": [ |
| 75 | + "event_filename = download_file(\n", |
| 76 | + " \"http://data.astropy.org/tutorials/FITS-tables/chandra_events.fits\", cache=True\n", |
| 77 | + ")" |
| 78 | + ] |
| 79 | + }, |
| 80 | + { |
| 81 | + "cell_type": "markdown", |
| 82 | + "metadata": {}, |
| 83 | + "source": [ |
| 84 | + "## Opening the FITS file and viewing table contents" |
| 85 | + ] |
| 86 | + }, |
| 87 | + { |
| 88 | + "cell_type": "markdown", |
| 89 | + "metadata": {}, |
| 90 | + "source": [ |
| 91 | + "Since the file is big, let's open it with `memmap=True` to prevent RAM storage issues." |
| 92 | + ] |
| 93 | + }, |
| 94 | + { |
| 95 | + "cell_type": "code", |
| 96 | + "execution_count": null, |
| 97 | + "metadata": {}, |
| 98 | + "outputs": [], |
| 99 | + "source": [ |
| 100 | + "hdu_list = fits.open(event_filename, memmap=True)" |
| 101 | + ] |
| 102 | + }, |
| 103 | + { |
| 104 | + "cell_type": "code", |
| 105 | + "execution_count": null, |
| 106 | + "metadata": {}, |
| 107 | + "outputs": [], |
| 108 | + "source": [ |
| 109 | + "hdu_list.info()" |
| 110 | + ] |
| 111 | + }, |
| 112 | + { |
| 113 | + "cell_type": "markdown", |
| 114 | + "metadata": {}, |
| 115 | + "source": [ |
| 116 | + "In this case, we're interested in reading EVENTS, which contains information about each X-ray photon that hit the detector." |
| 117 | + ] |
| 118 | + }, |
| 119 | + { |
| 120 | + "cell_type": "markdown", |
| 121 | + "metadata": {}, |
| 122 | + "source": [ |
| 123 | + "To find out what information the table contains, let's print the column names." |
| 124 | + ] |
| 125 | + }, |
| 126 | + { |
| 127 | + "cell_type": "code", |
| 128 | + "execution_count": null, |
| 129 | + "metadata": {}, |
| 130 | + "outputs": [], |
| 131 | + "source": [ |
| 132 | + "print(hdu_list[1].columns)" |
| 133 | + ] |
| 134 | + }, |
| 135 | + { |
| 136 | + "cell_type": "markdown", |
| 137 | + "metadata": {}, |
| 138 | + "source": [ |
| 139 | + "Now we'll take this data and convert it into an [astropy table](http://docs.astropy.org/en/stable/table/). While it's possible to access FITS tables directly from the ``.data`` attribute, using [Table](http://docs.astropy.org/en/stable/api/astropy.table.Table.html#astropy.table.Table) tends to make a variety of common tasks more convenient." |
| 140 | + ] |
| 141 | + }, |
| 142 | + { |
| 143 | + "cell_type": "code", |
| 144 | + "execution_count": null, |
| 145 | + "metadata": {}, |
| 146 | + "outputs": [], |
| 147 | + "source": [ |
| 148 | + "evt_data = Table(hdu_list[1].data)" |
| 149 | + ] |
| 150 | + }, |
| 151 | + { |
| 152 | + "cell_type": "markdown", |
| 153 | + "metadata": {}, |
| 154 | + "source": [ |
| 155 | + "For example, a preview of the table is easily viewed by simply running a cell with the table as the last line:" |
| 156 | + ] |
| 157 | + }, |
| 158 | + { |
| 159 | + "cell_type": "code", |
| 160 | + "execution_count": null, |
| 161 | + "metadata": {}, |
| 162 | + "outputs": [], |
| 163 | + "source": [ |
| 164 | + "evt_data" |
| 165 | + ] |
| 166 | + }, |
| 167 | + { |
| 168 | + "cell_type": "markdown", |
| 169 | + "metadata": {}, |
| 170 | + "source": [ |
| 171 | + "We can extract data from the table by referencing the column name. Let's try making a histogram for the energy of each photon, which will give us a sense for the spectrum (folded with the detector's efficiency)." |
| 172 | + ] |
| 173 | + }, |
| 174 | + { |
| 175 | + "cell_type": "code", |
| 176 | + "execution_count": null, |
| 177 | + "metadata": {}, |
| 178 | + "outputs": [], |
| 179 | + "source": [ |
| 180 | + "energy_hist = plt.hist(evt_data[\"energy\"], bins=\"auto\")" |
| 181 | + ] |
| 182 | + }, |
| 183 | + { |
| 184 | + "cell_type": "markdown", |
| 185 | + "metadata": {}, |
| 186 | + "source": [ |
| 187 | + "## Making a 2D histogram with some table data" |
| 188 | + ] |
| 189 | + }, |
| 190 | + { |
| 191 | + "cell_type": "markdown", |
| 192 | + "metadata": {}, |
| 193 | + "source": [ |
| 194 | + "We'll make an image by binning the x and y coordinates of the events into a 2D histogram." |
| 195 | + ] |
| 196 | + }, |
| 197 | + { |
| 198 | + "cell_type": "markdown", |
| 199 | + "metadata": {}, |
| 200 | + "source": [ |
| 201 | + "This particular observation spans five CCD chips. First, we determine the events that only fell on the main (ACIS-I) chips, which have number ids 0, 1, 2, and 3." |
| 202 | + ] |
| 203 | + }, |
| 204 | + { |
| 205 | + "cell_type": "code", |
| 206 | + "execution_count": null, |
| 207 | + "metadata": {}, |
| 208 | + "outputs": [], |
| 209 | + "source": [ |
| 210 | + "ii = np.in1d(evt_data[\"ccd_id\"], [0, 1, 2, 3])\n", |
| 211 | + "np.sum(ii)" |
| 212 | + ] |
| 213 | + }, |
| 214 | + { |
| 215 | + "cell_type": "markdown", |
| 216 | + "metadata": {}, |
| 217 | + "source": [ |
| 218 | + "### Method 1: Use numpy to make a 2D histogram and imshow to display it" |
| 219 | + ] |
| 220 | + }, |
| 221 | + { |
| 222 | + "cell_type": "markdown", |
| 223 | + "metadata": {}, |
| 224 | + "source": [ |
| 225 | + "This method allows us to create an image without stretching:" |
| 226 | + ] |
| 227 | + }, |
| 228 | + { |
| 229 | + "cell_type": "code", |
| 230 | + "execution_count": null, |
| 231 | + "metadata": {}, |
| 232 | + "outputs": [], |
| 233 | + "source": [ |
| 234 | + "NBINS = (100, 100)\n", |
| 235 | + "\n", |
| 236 | + "img_zero, yedges, xedges = np.histogram2d(evt_data[\"x\"][ii], evt_data[\"y\"][ii], NBINS)\n", |
| 237 | + "\n", |
| 238 | + "extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]]\n", |
| 239 | + "\n", |
| 240 | + "plt.imshow(\n", |
| 241 | + " img_zero, extent=extent, interpolation=\"nearest\", cmap=\"gist_yarg\", origin=\"lower\"\n", |
| 242 | + ")\n", |
| 243 | + "\n", |
| 244 | + "plt.xlabel(\"x\")\n", |
| 245 | + "plt.ylabel(\"y\")\n", |
| 246 | + "\n", |
| 247 | + "# To see more color maps\n", |
| 248 | + "# http://wiki.scipy.org/Cookbook/Matplotlib/Show_colormaps" |
| 249 | + ] |
| 250 | + }, |
| 251 | + { |
| 252 | + "cell_type": "markdown", |
| 253 | + "metadata": {}, |
| 254 | + "source": [ |
| 255 | + "### Method 2: Use hist2d with a log-normal color scheme" |
| 256 | + ] |
| 257 | + }, |
| 258 | + { |
| 259 | + "cell_type": "code", |
| 260 | + "execution_count": null, |
| 261 | + "metadata": {}, |
| 262 | + "outputs": [], |
| 263 | + "source": [ |
| 264 | + "NBINS = (100, 100)\n", |
| 265 | + "img_zero_mpl = plt.hist2d(\n", |
| 266 | + " evt_data[\"x\"][ii], evt_data[\"y\"][ii], NBINS, cmap=\"viridis\", norm=LogNorm()\n", |
| 267 | + ")\n", |
| 268 | + "\n", |
| 269 | + "cbar = plt.colorbar(ticks=[1.0, 3.0, 6.0])\n", |
| 270 | + "cbar.ax.set_yticklabels([\"1\", \"3\", \"6\"])\n", |
| 271 | + "\n", |
| 272 | + "plt.xlabel(\"x\")\n", |
| 273 | + "plt.ylabel(\"y\")" |
| 274 | + ] |
| 275 | + }, |
| 276 | + { |
| 277 | + "cell_type": "markdown", |
| 278 | + "metadata": {}, |
| 279 | + "source": [ |
| 280 | + "## Close the FITS file" |
| 281 | + ] |
| 282 | + }, |
| 283 | + { |
| 284 | + "cell_type": "markdown", |
| 285 | + "metadata": {}, |
| 286 | + "source": [ |
| 287 | + "When you're done using a FITS file, it's often a good idea to close it. That way you can be sure it won't continue using up excess memory or file handles on your computer. (This happens automatically when you close Python, but you never know how long that might be...)" |
| 288 | + ] |
| 289 | + }, |
| 290 | + { |
| 291 | + "cell_type": "code", |
| 292 | + "execution_count": null, |
| 293 | + "metadata": {}, |
| 294 | + "outputs": [], |
| 295 | + "source": [ |
| 296 | + "hdu_list.close()" |
| 297 | + ] |
| 298 | + }, |
| 299 | + { |
| 300 | + "cell_type": "markdown", |
| 301 | + "metadata": {}, |
| 302 | + "source": [ |
| 303 | + "## Exercises" |
| 304 | + ] |
| 305 | + }, |
| 306 | + { |
| 307 | + "cell_type": "markdown", |
| 308 | + "metadata": {}, |
| 309 | + "source": [ |
| 310 | + "Make a scatter plot of the same data you histogrammed above. The [plt.scatter](http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.scatter) function is your friend for this. What are the pros and cons of doing it this way?" |
| 311 | + ] |
| 312 | + }, |
| 313 | + { |
| 314 | + "cell_type": "code", |
| 315 | + "execution_count": null, |
| 316 | + "metadata": {}, |
| 317 | + "outputs": [], |
| 318 | + "source": [] |
| 319 | + }, |
| 320 | + { |
| 321 | + "cell_type": "markdown", |
| 322 | + "metadata": {}, |
| 323 | + "source": [ |
| 324 | + "Try the same with the [plt.hexbin](http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.hexbin) plotting function. Which do you think looks better for this kind of data?" |
| 325 | + ] |
| 326 | + }, |
| 327 | + { |
| 328 | + "cell_type": "code", |
| 329 | + "execution_count": null, |
| 330 | + "metadata": {}, |
| 331 | + "outputs": [], |
| 332 | + "source": [] |
| 333 | + }, |
| 334 | + { |
| 335 | + "cell_type": "markdown", |
| 336 | + "metadata": {}, |
| 337 | + "source": [ |
| 338 | + "Choose an energy range to make a slice of the FITS table, then plot it. How does the image change with different energy ranges?" |
| 339 | + ] |
| 340 | + }, |
| 341 | + { |
| 342 | + "cell_type": "code", |
| 343 | + "execution_count": null, |
| 344 | + "metadata": {}, |
| 345 | + "outputs": [], |
| 346 | + "source": [] |
| 347 | + } |
| 348 | + ], |
| 349 | + "metadata": { |
| 350 | + "astropy-tutorials": { |
| 351 | + "author": "Lia R. Corrales <[email protected]>", |
| 352 | + "date": "January 2014", |
| 353 | + "description": "astropy.utils.data to download the file, astropy.io.fits to open and view the file, matplotlib for making both 1D and 2D histograms of the data.", |
| 354 | + "link_name": "Viewing and manipulating data from FITS tables", |
| 355 | + "name": "", |
| 356 | + "published": true |
| 357 | + }, |
| 358 | + "language_info": { |
| 359 | + "codemirror_mode": { |
| 360 | + "name": "ipython" |
| 361 | + }, |
| 362 | + "file_extension": ".py", |
| 363 | + "mimetype": "text/x-python", |
| 364 | + "name": "python", |
| 365 | + "nbconvert_exporter": "python" |
| 366 | + } |
| 367 | + }, |
| 368 | + "nbformat": 4, |
| 369 | + "nbformat_minor": 4 |
| 370 | +} |
0 commit comments