{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-03-23T17:32:11.435988Z",
     "start_time": "2019-03-23T17:32:11.139551Z"
    }
   },
   "outputs": [],
   "source": [
    "import batoid\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-03-23T17:32:11.614551Z",
     "start_time": "2019-03-23T17:32:11.520982Z"
    }
   },
   "outputs": [],
   "source": [
    "telescope = batoid.Optic.fromYaml(\"HSC.yaml\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-03-23T17:32:11.746319Z",
     "start_time": "2019-03-23T17:32:11.728086Z"
    }
   },
   "outputs": [],
   "source": [
    "def pupil(thx, thy, nside=512):\n",
    "    rays = batoid.RayVector.asGrid(\n",
    "        optic=telescope, wavelength=750e-9,\n",
    "        theta_x=thx, theta_y=thy,\n",
    "        nx=nside, ny=nside\n",
    "    )\n",
    "    rays2 = rays.copy()\n",
    "    telescope.stopSurface.interact(rays2)\n",
    "    telescope.trace(rays)\n",
    "    w = ~rays.vignetted\n",
    "    return rays2.x[w], rays2.y[w]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-03-23T17:32:11.925188Z",
     "start_time": "2019-03-23T17:32:11.907374Z"
    }
   },
   "outputs": [],
   "source": [
    "def drawCircle(ax, cx, cy, r, **kwargs):\n",
    "    t = np.linspace(0, 2*np.pi, 1000)\n",
    "    x = r*np.cos(t)+cx\n",
    "    y = r*np.sin(t)+cy\n",
    "    ax.plot(x, y, **kwargs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-03-23T17:32:12.145744Z",
     "start_time": "2019-03-23T17:32:12.126651Z"
    }
   },
   "outputs": [],
   "source": [
    "def drawRay(ax, cx, cy, width, theta, **kwargs):\n",
    "    R = np.array([[np.cos(theta), -np.sin(theta)], [np.sin(theta), np.cos(theta)]])\n",
    "    \n",
    "    dx = np.linspace(0, 4.1, 1000)\n",
    "    dy = np.ones_like(dx)*width/2\n",
    "\n",
    "    bx = np.copy(dx)\n",
    "    by = -dy\n",
    "    \n",
    "    dx, dy = R.dot(np.vstack([dx, dy]))\n",
    "    bx, by = R.dot(np.vstack([bx, by]))\n",
    "    \n",
    "    dx += cx\n",
    "    dy += cy\n",
    "    bx += cx\n",
    "    by += cy\n",
    "    \n",
    "    ax.plot(dx, dy, **kwargs)\n",
    "    ax.plot(bx, by, **kwargs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-03-23T17:32:12.370369Z",
     "start_time": "2019-03-23T17:32:12.352752Z"
    }
   },
   "outputs": [],
   "source": [
    "def drawRectangle(ax, cx, cy, width, height, **kwargs):\n",
    "    x = width/2*np.array([-1,-1,1,1,-1])\n",
    "    y = height/2*np.array([-1,1,1,-1,-1])\n",
    "    x += cx\n",
    "    y += cy\n",
    "    ax.plot(x, y, **kwargs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-03-23T17:32:12.790991Z",
     "start_time": "2019-03-23T17:32:12.772321Z"
    }
   },
   "outputs": [],
   "source": [
    "from IPython.core.display import display, HTML\n",
    "display(HTML(\"<style>.container { width:100% !important; }</style>\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-03-23T18:03:26.547943Z",
     "start_time": "2019-03-23T18:03:26.521999Z"
    }
   },
   "outputs": [],
   "source": [
    "def modelPlot(thx, thy):\n",
    "    fig, ax = plt.subplots(1, 1, figsize=(30, 30))\n",
    "    ax.scatter(*pupil(thx,thy), s=0.1, c='k')\n",
    "    ax.set_aspect('equal')\n",
    "    # Primary mirror\n",
    "    drawCircle(ax, 0, 0, 4.1, c='r')\n",
    "    # Camera shadow\n",
    "    drawCircle(ax, 17*thx, 17*thy, 0.95, c='r')\n",
    "    # G1 cutoff\n",
    "    drawCircle(ax, -142*thx, -142*thy, 4.25, c='r')\n",
    "    # G4 cutoff\n",
    "    thr = np.hypot(thx, thy)\n",
    "    if thr > np.deg2rad(0.75):\n",
    "        thph = np.arctan2(thy, thx)\n",
    "        cr = -17.76 - 2e3*(thr-np.deg2rad(0.76)) - 1.8e5*(thr-np.deg2rad(0.76))*(thr-np.deg2rad(0.8))\n",
    "        drawCircle(ax, cr*np.cos(thph), cr*np.sin(thph), 20.0, c='r')\n",
    "    \n",
    "    # spider\n",
    "    alpha = 6.75\n",
    "\n",
    "    # TopRing\n",
    "    trRate = 14\n",
    "    drawRay(ax, 0.45+trRate*thx, 0.45+trRate*thy, 0.18, np.deg2rad(90+alpha), c='r')\n",
    "    drawRay(ax, 0.45+trRate*thx, 0.45+trRate*thy, 0.18, np.deg2rad(-alpha), c='r')\n",
    "    drawRay(ax, -0.45+trRate*thx, -0.45+trRate*thy, 0.18, np.deg2rad(180-alpha), c='r')\n",
    "    drawRay(ax, -0.45+trRate*thx, -0.45+trRate*thy, 0.18, np.deg2rad(270+alpha), c='r')\n",
    "\n",
    "\n",
    "    # TertiarySpiderFirstPass\n",
    "    tsfpRate = 2\n",
    "    drawRay(ax, 0.45+tsfpRate*thx, 0.45+tsfpRate*thy, 0.18, np.deg2rad(90+alpha), c='r')\n",
    "    drawRay(ax, 0.45+tsfpRate*thx, 0.45+tsfpRate*thy, 0.18, np.deg2rad(-alpha), c='r')\n",
    "    drawRay(ax, -0.45+tsfpRate*thx, -0.45+tsfpRate*thy, 0.18, np.deg2rad(180-alpha), c='r')\n",
    "    drawRay(ax, -0.45+tsfpRate*thx, -0.45+tsfpRate*thy, 0.18, np.deg2rad(270+alpha), c='r')\n",
    "\n",
    "\n",
    "    # TertiarySpiderSecondPass\n",
    "    tsspRate = -2\n",
    "    drawRay(ax, 0.51+tsspRate*thx, 0.51+tsspRate*thy, 0.18, np.deg2rad(90+alpha), c='r')\n",
    "    drawRay(ax, 0.51+tsspRate*thx, 0.51+tsspRate*thy, 0.18, np.deg2rad(-alpha), c='r')\n",
    "    drawRay(ax, -0.51+tsspRate*thx, -0.51+tsspRate*thy, 0.18, np.deg2rad(180-alpha), c='r')\n",
    "    drawRay(ax, -0.51+tsspRate*thx, -0.51+tsspRate*thy, 0.18, np.deg2rad(270+alpha), c='r')\n",
    "\n",
    "    # FEU\n",
    "    drawRectangle(ax, 15*thx, 15*thy, 0.84, 2.28, c='r')\n",
    "\n",
    "    ax.set_xlim(-5,5)\n",
    "    ax.set_ylim(-5,5)\n",
    "    ax.axvline(c='k')\n",
    "    ax.axhline(c='k')\n",
    "    fig.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-03-23T18:11:45.553548Z",
     "start_time": "2019-03-23T18:11:44.667025Z"
    }
   },
   "outputs": [],
   "source": [
    "telescope.itemDict['SubaruHSC.TopRing'].skip=False\n",
    "telescope.itemDict['SubaruHSC.BottomRing'].skip=False\n",
    "telescope.itemDict['SubaruHSC.TertiarySpiderFirstPass'].skip=False\n",
    "telescope.itemDict['SubaruHSC.TertiarySpiderSecondPass'].skip=False\n",
    "telescope.itemDict['SubaruHSC.FEU'].skip=False\n",
    "\n",
    "# modelPlot(0, np.deg2rad(-0.82))\n",
    "# modelPlot(0, np.deg2rad(-0.77))\n",
    "# modelPlot(0, np.deg2rad(-0.7))\n",
    "# modelPlot(0, np.deg2rad(-0.35))\n",
    "# modelPlot(0, np.deg2rad(0))\n",
    "# modelPlot(0, np.deg2rad(0.35))\n",
    "# modelPlot(0, np.deg2rad(0.7))\n",
    "# modelPlot(0, np.deg2rad(0.77))\n",
    "\n",
    "# modelPlot(np.deg2rad(-0.77), 0)\n",
    "# modelPlot(np.deg2rad(-0.7), 0)\n",
    "# modelPlot(np.deg2rad(-0.35), 0)\n",
    "# modelPlot(np.deg2rad(0), 0)\n",
    "# modelPlot(np.deg2rad(0.35), 0)\n",
    "# modelPlot(np.deg2rad(0.7), 0)\n",
    "# modelPlot(np.deg2rad(0.77), 0)\n",
    "\n",
    "# modelPlot(np.deg2rad(-0.5), np.deg2rad(-0.5))\n",
    "# modelPlot(np.deg2rad(-0.35), np.deg2rad(-0.35))\n",
    "# modelPlot(np.deg2rad(-0.2), np.deg2rad(-0.2))\n",
    "\n",
    "# modelPlot(np.deg2rad(0.5), np.deg2rad(-0.5))\n",
    "# modelPlot(np.deg2rad(0.35), np.deg2rad(-0.35))\n",
    "# modelPlot(np.deg2rad(0.2), np.deg2rad(-0.2))\n",
    "\n",
    "modelPlot(np.deg2rad(0.55), np.deg2rad(0.55))"
   ]
  },
  {
   "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",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "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.8-final"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}