{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Simple FITS file manipulation in Python with astropy.io.fits"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Load the fits package from astropy.\n",
    "\n",
    "Use a helper function to get a remote file.\n",
    "\n",
    "Open the FITS file and display the information about the first Header Data Unit List."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from astropy.io import fits\n",
    "from astropy.utils.data import get_pkg_data_filename\n",
    "image_file = get_pkg_data_filename('tutorials/FITS-images/HorseHead.fits')\n",
    "hdul = fits.open(image_file)\n",
    "hdul.info()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Display the Header of the FITS file."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "hdul[0].header"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Set up mpld3 so we can zoom, pan, select images in the notebook.\n",
    "\n",
    "Import Matplotlib pyplot and a style file from astropy.visualization.\n",
    "\n",
    "Extract the image data from the FITS head data unit and print its shape."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "import mpld3\n",
    "mpld3.enable_notebook()\n",
    "import matplotlib.pyplot as plt\n",
    "from astropy.visualization import astropy_mpl_style\n",
    "plt.style.use(astropy_mpl_style)\n",
    "image_data = hdul[0].data\n",
    "print(image_data.shape)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Make a pyplot figure and display the image data using default color table.\n",
    "\n",
    "We set the 'origin' to 'lower' so the 0,0 point will be in the lower left instead of upper left."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.figure()\n",
    "plt.imshow(image_data, origin='lower')\n",
    "plt.colorbar()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Change the color map we are using to 'gray'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.figure()\n",
    "plt.imshow(image_data, cmap = 'gray', origin='lower')\n",
    "plt.colorbar()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Print out a few statistics about the image."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "print('Min:', np.min(image_data))\n",
    "print('Max:', np.max(image_data))\n",
    "print('Mean:', np.mean(image_data))\n",
    "print('Stdev:', np.std(image_data))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Plot a histogram of the image using 100 bins."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "NBINS = 100\n",
    "histogram = plt.hist(image_data.flatten(), NBINS)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Use \"LogNorm\" to normalize the data to 0 to 1 using a log scale."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from matplotlib.colors import LogNorm\n",
    "plt.imshow(image_data, cmap='gray', norm=LogNorm())\n",
    "cbar = plt.colorbar(ticks=[5.e3,1.e4,2.e4])\n",
    "cbar.ax.set_yticklabels(['5,000','10,000','20,000'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now, plot the image in a figure with two supplots and plot the 100th column in the second supplot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig1, (ax, ax2) = plt.subplots(2, 1, figsize=(12,12))\n",
    "ax.imshow(image_data, cmap='gray')\n",
    "\n",
    "plot_data = image_data[100, :]\n",
    "x = np.arange(len(plot_data))\n",
    "ax2.plot(x, plot_data)"
   ]
  },
  {
   "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.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
