{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# DEMO Notebook\n",
    "## Using imexam inside a Jupyter notebook with Ginga\n",
    "### The Ginga HTML5 canvas viewer can be used as a viewer for the imexam package. While the Ginga viewer is active you have access to all the imexam as well as all the Ginga functions the HTML viewer provides. Some of the examples of Ginga functionality were taken directly from a Ginga demo notebook available on the Ginga github repo.\n",
    "\n",
    "### This and other demo notebooks can be found on the SpaceTelescope github site: https://github.com/spacetelescope/imexam/\n",
    "\n",
    "### If you are running with Python 3, ginga also requires that pillow be installed. You'll see a blank viewer popup in the html window if this is the case. You can \"conda install pillow\" and that should fix the problem.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import imexam"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Start up a new tab with the ginga HTML5 viewer\n",
    "#### You can disconnect the tab and place it anywhere else on your screen that you like"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a=imexam.connect(viewer='ginga')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a.load_fits('iacs01t4q_flt.fits') #display fits image in a separate browser window"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## When using a Ginga window the imexam() function is event driven. \n",
    "### This means that while looking at the image, pressing the \"i\" key will enter you into imexam mode. Subsequent pressing of the \"q\" key will put you back in regular viewing mode.\n",
    "\n",
    "### While in imexam mode, the analysis keys are mapped to the imexam functions and the results will be returned in the notebook"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#No list of commands is printed with the event driven imexam, but you can always \n",
    "#see what the available commands are by issuing the imexam() call:\n",
    "a.imexam()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<p align=\"left\">The text printed above the plot: \"read: e at x y\" , means that the \"e\" key was pressed at those image coordinates. The \"e\" instructs imexam to make a contour plot.</p>\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# all of the plots are customizable; the parameters are stored in regular dictionaries\n",
    "a.eimexam()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#example of accessing the plot parameter dictionary directly for customization\n",
    "a.set_plot_pars('e','title','My custom plot title')\n",
    "a.set_plot_pars('e','floor',0.)\n",
    "a.set_plot_pars('e','ncontours',4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a.imexam()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# you can  save a copy of the current viewing window\n",
    "a.window.ginga_view.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Besides making plots you can also get basic aperture photometry using the \"a\" key and radial plots with \"r\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#You can also request a numpy array of the data being viewed:\n",
    "data=a.get_data()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#you can look at the header as well\n",
    "a.get_header()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## If you are using the ginga viewer, you can return the full Ginga image object and use any\n",
    "of the methods which are enabled for it. You can look here for the Ginga quick reference: \n",
    "http://ginga.readthedocs.org/en/latest/quickref.html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "img=a.get_image()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "type(img)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "img.height, img.width, img.pixtoradec(100,100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "img.pixtoradec(100,100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "canvas=a.window.ginga_view.add_canvas()\n",
    "canvas.delete_all_objects()\n",
    "canvas.set_drawtype('rectangle')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Now you can go to the viewer and draw a rectangle selection box, use the right mouse button"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a.window.ginga_view.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from ginga.util import iqcalc\n",
    "iq = iqcalc.IQCalc()\n",
    "\n",
    "#find all the peaks in the rectangle area\n",
    "r = canvas.objects[0]\n",
    "data = img.cutout_shape(r)\n",
    "peaks = iq.find_bright_peaks(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "peaks[:10] #show the last 10 peaks detected in the cutout"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "objs = iq.evaluate_peaks(peaks, data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "o1=objs[0]\n",
    "o1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# pixel coords are for cutout, so add back in origin of cutout\n",
    "#  to get full data coords RA, DEC of first object\n",
    "x1, y1, x2, y2 = r.get_llur()\n",
    "img.pixtoradec(x1+o1.objx, y1+o1.objy)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Draw circles around all objects\n",
    "Circle = canvas.get_draw_class('circle')\n",
    "for obj in objs:\n",
    "    x, y = x1+obj.objx, y1+obj.objy\n",
    "    if r.contains(x, y):\n",
    "        canvas.add(Circle(x, y, radius=10, color='yellow'))\n",
    "        \n",
    "# set pan and zoom to center\n",
    "a.window.ginga_view.set_pan((x1+x2)/2, (y1+y2)/2)\n",
    "a.window.ginga_view.scale_to(0.75, 0.75)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a.window.ginga_view.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a.close() #for ginga, there isn't an automatic window close for the HTML5 canvas"
   ]
  },
  {
   "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.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
