{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import batoid\n",
    "import matplotlib.pyplot as plt\n",
    "import ipyvolume as ipv\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "telescope = batoid.Optic.fromYaml(\"LSST_r.yaml\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# Make refractive interfaces partially reflective\n",
    "for surface in telescope.itemDict.values():\n",
    "    if isinstance(surface, batoid.RefractiveInterface):\n",
    "        surface.forwardCoating = batoid.SimpleCoating(0.02, 0.98)\n",
    "        surface.reverseCoating = batoid.SimpleCoating(0.02, 0.98)\n",
    "    if isinstance(surface, batoid.Detector):\n",
    "        surface.forwardCoating = batoid.SimpleCoating(0.02, 0.98)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "rays = batoid.RayVector.asPolar(\n",
    "    telescope, wavelength=620e-9, \n",
    "    theta_x=0.0, theta_y=np.deg2rad(1.5),\n",
    "    naz=1000, nrad=300\n",
    ")\n",
    "rForward, rReverse = telescope.traceSplit(rays, minFlux=1e-4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "for i, rr in enumerate(rForward[:5]):\n",
    "    plt.hexbin(rr.x, rr.y, extent=[-0.35, 0.35, -0.35, 0.35], gridsize=150)\n",
    "    # plot approximate focal plane radius\n",
    "    th = np.linspace(0, 2*np.pi, 1000)\n",
    "    plt.plot(0.32*np.cos(th), 0.32*np.sin(th), c='r')\n",
    "    # plot direct path location on focal plane\n",
    "    # direct path will be rForward with fewest number of things in \"path\"\n",
    "    w = np.argmin([len(rrr.path) for rrr in rForward])\n",
    "    plt.scatter(np.mean(rForward[w].x), np.mean(rForward[w].y), marker='+', color='m')\n",
    "    plt.gca().set_aspect(\"equal\")\n",
    "    plt.title(f\"ghost {i} path {rr.path}\")\n",
    "    plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# Pick a few ghosts and look in more detail\n",
    "ghost = rForward[13]\n",
    "for item in ghost.path:\n",
    "    print(item)\n",
    "plt.hexbin(ghost.x, ghost.y, gridsize=1000)\n",
    "plt.gca().set_aspect('equal')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "ghost = rForward[13]\n",
    "\n",
    "# polar rays make a bit of a confusing plot\n",
    "# rays = batoid.RayVector.asPolar(\n",
    "#     telescope, wavelength=620e-9,\n",
    "#     theta_x=0.0, theta_y=np.deg2rad(1.5),\n",
    "#     nrad=10, naz=30\n",
    "# )\n",
    "\n",
    "# but an xy-fan of rays is okay\n",
    "rays = batoid.concatenateRayVectors([\n",
    "    batoid.RayVector.asGrid(\n",
    "        telescope, wavelength=620e-9, \n",
    "        theta_x=0.0, theta_y=np.deg2rad(1.5), \n",
    "        nx=1, ny=30\n",
    "    ),\n",
    "    batoid.RayVector.asGrid(\n",
    "        telescope, wavelength=620e-9, \n",
    "        theta_x=0.0, theta_y=np.deg2rad(1.5), \n",
    "        nx=30, ny=1\n",
    "    )\n",
    "])\n",
    "tf = telescope.traceFull(rays, path=ghost.path)\n",
    "\n",
    "ipv.figure(width=700, height=500)\n",
    "telescope.draw3d(ipv, color='black')\n",
    "batoid.drawTrace3d(ipv, tf, c='r')\n",
    "ipv.xlim(-1, 1)\n",
    "ipv.ylim(-1, 1)\n",
    "ipv.zlim(3, 5)\n",
    "ipv.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.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
