{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# DEMO Notebook - changing the models used for plots\n",
    "\n",
    "## Using imexam inside a Jupyter notebook with DS9 as your viewer\n",
    "DS9 can be used as a viewer for the imexam package. While the DS9 viewer is active you have access to all the DS9 menu options as well, and imexam will attemp to keep track of changes you make through either the command line or the DS9 menus.\n",
    "\n",
    "This and other demo notebooks can be found on the SpaceTelescope github site: https://github.com/spacetelescope/imexam/\n",
    "\n",
    "\n",
    "Communication with the DS9 display window is done through the XPA. More information on the XPA access points which are available can be found here: http://ds9.si.edu/doc/ref/xpa.html\n",
    "\n",
    "### In this example I have my local environment using the qt5agg backend, with the environment variable QT_API='pyqt'\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Ok, now on to the good stuff!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import imexam"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a=imexam.connect(viewer='ds9') #stars up a DS9 window with no options, wait for it to finish before proceeding to next cell"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a.load_fits('iacs01t4q_flt.fits') #display fits image in ginga window"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a.scale()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a.grab()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### In order to interact directly with the DS9 window, we have to start up a loop which looks for mouse and keyboard press events. In order to get out of this loop, press the \"q\" key"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a.imexam() #start the interactive cursor in the DS9 window, then choose an object and use the 'a' key"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# all of the plots are customizable; the parameters are stored in regular dictionaries\n",
    "a.jimexam()  # j-imexam is used because the j key is what is pressed to get the line fit"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#maybe we want to change the model used for the fit from Gaussian1D to Moffat1D\n",
    "#Setting the parameters requires a tuple of the (key name, parameters, value)\n",
    "a.set_plot_pars('j','func','MexicanHat1D' )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a.jimexam() #to show the dictionary updated"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#now lets repeat the same line fit process above\n",
    "a.imexam()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<p align=\"left\"> <b>You can see that this time, the plotting function used a Mexcan Hat 1D fit for the data.</p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a.unlearn() #you can always go back to the default plot settings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a.close() #quite the ds9 window"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "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
}
