{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-16T04:55:58.088797Z",
     "start_time": "2019-09-16T04:55:57.839995Z"
    }
   },
   "outputs": [],
   "source": [
    "import batoid\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from ipywidgets import interact\n",
    "import ipywidgets as widgets\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-16T04:55:58.094492Z",
     "start_time": "2019-09-16T04:55:58.090086Z"
    }
   },
   "outputs": [],
   "source": [
    "# Let's model my 12-inch f/5 Newtonian reflector.\n",
    "# 12-inch is approx 0.3 m\n",
    "# f/5 then means the focal length is 1.5 m\n",
    "# Which then means the parabolic equation is:\n",
    "# z = (x^2+y^2) / (4*1.5)\n",
    "# In the other notebook, we just used a Paraboloid\n",
    "# Let's try an Asphere here though, to see what happens\n",
    "# When the conic constant is not exactly -1, and when\n",
    "# the higher order aspheric constants are non-zero.\n",
    "\n",
    "D = 0.3\n",
    "fn = 5\n",
    "f = D * fn  # focal length\n",
    "R = 2*f\n",
    "\n",
    "def makeTelescope(defocus=0.0): # defocus in mm\n",
    "    telescope = batoid.CompoundOptic(\n",
    "        items = [\n",
    "            batoid.Mirror(\n",
    "                batoid.Asphere(R, -0.95, [1e-6, 1e-12]),\n",
    "                name=\"Mirror\"\n",
    "            ),\n",
    "            batoid.Detector(\n",
    "                batoid.Plane(),\n",
    "                name=\"detector\",\n",
    "                coordSys=batoid.CoordSys(origin=[0,0,f+0.001*defocus])\n",
    "            )\n",
    "        ]\n",
    "    )\n",
    "    return telescope\n",
    "\n",
    "plate_scale = 1./f # radians / m  approximate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-16T04:55:58.506704Z",
     "start_time": "2019-09-16T04:55:58.207707Z"
    }
   },
   "outputs": [],
   "source": [
    "@interact(theta_x=widgets.FloatSlider(min=-1,max=1,step=0.01,value=0.1),\n",
    "          theta_y=widgets.FloatSlider(min=-1,max=1,step=0.01,value=-0.3),\n",
    "          focus=widgets.FloatSlider(min=-0.5, max=0.5, step=0.01,value=0.1))\n",
    "def spot(theta_x, theta_y, focus):\n",
    "    \"\"\"Display a spot diagram for a Newtonian telescope.\n",
    "\n",
    "    @param theta_x  Field angle in degrees\n",
    "    @param theta_y  Field angle in degrees\n",
    "    @param focus    Defocus distance in mm\n",
    "    \"\"\"\n",
    "    telescope = makeTelescope(focus)\n",
    "    rays = batoid.RayVector.asPolar(\n",
    "        backDist=1.0, nrad=40, naz=160, wavelength=500e-9,\n",
    "        theta_x=np.deg2rad(theta_x), theta_y=np.deg2rad(theta_y),\n",
    "        outer=D/2\n",
    "    )\n",
    "    telescope.trace(rays)\n",
    "    w = ~rays.vignetted\n",
    "    x, y = np.array(rays.x[w]), np.array(rays.y[w])\n",
    "    x -= np.mean(x)\n",
    "    y -= np.mean(y)\n",
    "    x *= plate_scale*206265\n",
    "    y *= plate_scale*206265\n",
    "\n",
    "    plt.figure(figsize=(4.5,4))\n",
    "    plt.scatter(x, y, s=1, alpha=0.5)\n",
    "    plt.xlim(-10, 10)\n",
    "    plt.ylim(-10, 10)\n",
    "    plt.title(r\"$\\theta_x = {:4.2f}\\,,\\theta_y = {:4.2f}\\,, f={:4.2f}$\".format(theta_x, theta_y, focus))\n",
    "    plt.xlabel(\"arcseconds\")\n",
    "    plt.ylabel(\"arcseconds\")\n",
    "    plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.8.8"
  },
  "widgets": {
   "state": {
    "58940a2b5c9449f5bf5644709da55ee6": {
     "views": [
      {
       "cell_index": 2
      }
     ]
    }
   },
   "version": "1.2.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
