{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-02-11T00:18:37.666102Z",
     "start_time": "2020-02-11T00:18:37.363755Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "import batoid\n",
    "import yaml\n",
    "import os\n",
    "import numpy as np\n",
    "from ipywidgets import interact\n",
    "import ipywidgets as widgets\n",
    "import matplotlib.pyplot as plt\n",
    "from mpl_toolkits.mplot3d import Axes3D"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-02-11T00:18:37.884473Z",
     "start_time": "2020-02-11T00:18:37.794806Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "telescope = batoid.Optic.fromYaml(\"HSC.yaml\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-02-11T00:25:08.428495Z",
     "start_time": "2020-02-11T00:25:08.056273Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "# Sanity check optic shifting\n",
    "@interact(optic=widgets.Dropdown(\n",
    "              options=list(telescope.itemDict.keys()),\n",
    "              value='SubaruHSC.HSC.WFC'\n",
    "          ),\n",
    "          gdx=widgets.FloatSlider(min=-1, max=1, step=0.01, value=0.2),\n",
    "          gdy=widgets.FloatSlider(min=-1, max=1, step=0.01, value=0.0),\n",
    "          gdz=widgets.FloatSlider(min=-1, max=1, step=0.01, value=0.0),\n",
    "          dthx=widgets.FloatSlider(min=-90, max=90, step=5, value=0.0),\n",
    "          dthy=widgets.FloatSlider(min=-90, max=90, step=5, value=0.0),\n",
    "          dthz=widgets.FloatSlider(min=-90, max=90, step=5, value=0.0),\n",
    "          ldx=widgets.FloatSlider(min=-1, max=1, step=0.01, value=0.0),\n",
    "          ldy=widgets.FloatSlider(min=-1, max=1, step=0.01, value=0.0),\n",
    "          ldz=widgets.FloatSlider(min=-1, max=1, step=0.01, value=0.0),\n",
    "          rotOriginX=widgets.FloatText(value=0, step=0.01),\n",
    "          rotOriginY=widgets.FloatText(value=0, step=0.01),\n",
    "          rotOriginZ=widgets.FloatText(value=0, step=0.01)\n",
    "         )\n",
    "def shiftOptic(optic, gdx, gdy, gdz, dthx, dthy, dthz, ldx, ldy, ldz, rotOriginX, rotOriginY, rotOriginZ):\n",
    "    rot = batoid.RotZ(np.pi/180*dthz).dot(batoid.RotX(np.pi/180*dthx)).dot(batoid.RotY(np.pi/180*dthy))\n",
    "    perturbedTelescope = telescope.withGloballyShiftedOptic(optic, [gdx, gdy, gdz])\n",
    "    perturbedTelescope = perturbedTelescope.withLocallyRotatedOptic(optic, rot, [rotOriginX, rotOriginY, rotOriginZ])\n",
    "    perturbedTelescope = perturbedTelescope.withLocallyShiftedOptic(optic, [ldx, ldy, ldz])\n",
    "    fig = plt.figure(figsize=(12, 12))\n",
    "    ax = fig.add_subplot(111, projection='3d')\n",
    "    perturbedTelescope['SubaruHSC.HSC'].draw3d(ax, c='k')\n",
    "    #And redraw the perturbed optic in red\n",
    "    perturbedTelescope[optic].draw3d(ax, c='r')\n",
    "    ax.set_xlim(-0.5, 0.5)\n",
    "    ax.set_ylim(-0.5, 0.5)    \n",
    "    ax.set_zlim(13.5, 15.25)\n",
    "    ax.set_xlabel(\"x\")\n",
    "    ax.set_ylabel(\"y\")\n",
    "    ax.set_zlabel(\"z\")    \n",
    "    plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "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.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
