{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-08-31T17:29:38.753862Z",
     "start_time": "2019-08-31T17:29:38.522098Z"
    }
   },
   "outputs": [],
   "source": [
    "import batoid\n",
    "import numpy as np\n",
    "from ipywidgets import interact\n",
    "import ipywidgets as widgets\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-08-31T17:29:51.744922Z",
     "start_time": "2019-08-31T17:29:51.722119Z"
    }
   },
   "outputs": [],
   "source": [
    "HSC_telescope = batoid.Optic.fromYaml(\"HSC.yaml\")\n",
    "LSST_telescope = batoid.Optic.fromYaml(\"LSST_r.yaml\")\n",
    "DECam_telescope = batoid.Optic.fromYaml(\"DECam.yaml\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-08-31T17:29:53.868551Z",
     "start_time": "2019-08-31T17:29:53.569507Z"
    }
   },
   "outputs": [],
   "source": [
    "# HSC\n",
    "max_th = 0.74\n",
    "pixSize = 15e-6\n",
    "telescope = HSC_telescope\n",
    "\n",
    "dthx_drx = []\n",
    "dthy_dry = []\n",
    "thetas = np.linspace(0., max_th, 20)\n",
    "for th in thetas:\n",
    "    dth_dr = batoid.dthdr(telescope, np.deg2rad(th), 0, 620e-9)\n",
    "    dthx_drx.append(abs(dth_dr[0,0]))\n",
    "    dthy_dry.append(abs(dth_dr[1,1]))\n",
    "\n",
    "dthx_drx = np.array(dthx_drx) * 206265 * pixSize # arcsec per pixel\n",
    "dthy_dry = np.array(dthy_dry) * 206265 * pixSize # arcsec per pixel\n",
    "\n",
    "fig, (ax1, ax2) = plt.subplots(ncols=2, figsize=(10, 4))\n",
    "ax1.plot(thetas, dthx_drx, c='b', label='radial')\n",
    "ax1.plot(thetas, dthy_dry, c='r', label='tangential')\n",
    "ax1.set_xlabel('radius (deg)')\n",
    "ax1.set_ylabel('pixel size (arcsec)')\n",
    "ax1.legend(loc='lower left')\n",
    "ax1.set_title(\"HSC\")\n",
    "\n",
    "ax2.plot(thetas, np.array(dthx_drx)/np.array(dthy_dry))\n",
    "ax2.set_xlabel('radius (deg)')\n",
    "ax2.set_ylabel('b/a')\n",
    "ax2.set_title(\"HSC\")\n",
    "\n",
    "fig.tight_layout()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-08-31T17:29:54.379861Z",
     "start_time": "2019-08-31T17:29:54.121579Z"
    }
   },
   "outputs": [],
   "source": [
    "# DECam\n",
    "max_th = 1.1\n",
    "pixSize = 15e-6\n",
    "telescope = DECam_telescope\n",
    "\n",
    "dthx_drx = []\n",
    "dthy_dry = []\n",
    "thetas = np.linspace(0., max_th, 20)\n",
    "for th in thetas:\n",
    "    dth_dr = batoid.dthdr(telescope, np.deg2rad(th), 0, 620e-9)\n",
    "    dthx_drx.append(abs(dth_dr[0,0]))\n",
    "    dthy_dry.append(abs(dth_dr[1,1]))\n",
    "\n",
    "dthx_drx = np.array(dthx_drx) * 206265 * pixSize # arcsec per pixel\n",
    "dthy_dry = np.array(dthy_dry) * 206265 * pixSize # arcsec per pixel\n",
    "\n",
    "fig, (ax1, ax2) = plt.subplots(ncols=2, figsize=(10, 4))\n",
    "ax1.plot(thetas, dthx_drx, c='b', label='radial')\n",
    "ax1.plot(thetas, dthy_dry, c='r', label='tangential')\n",
    "ax1.set_xlabel('radius (deg)')\n",
    "ax1.set_ylabel('pixel size (arcsec)')\n",
    "ax1.legend(loc='lower left')\n",
    "ax1.set_title(\"DECam\")\n",
    "\n",
    "ax2.plot(thetas, np.array(dthx_drx)/np.array(dthy_dry))\n",
    "ax2.set_xlabel('radius (deg)')\n",
    "ax2.set_ylabel('b/a')\n",
    "ax2.set_title(\"DECam\")\n",
    "\n",
    "fig.tight_layout()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-08-31T17:29:54.981504Z",
     "start_time": "2019-08-31T17:29:54.731844Z"
    }
   },
   "outputs": [],
   "source": [
    "# LSST\n",
    "max_th = 1.75\n",
    "pixSize = 10e-6\n",
    "telescope = LSST_telescope\n",
    "\n",
    "dthx_drx = []\n",
    "dthy_dry = []\n",
    "thetas = np.linspace(0., max_th, 20)\n",
    "for th in thetas:\n",
    "    dth_dr = batoid.dthdr(telescope, np.deg2rad(th), 0, 620e-9)\n",
    "    dthx_drx.append(abs(dth_dr[0,0]))\n",
    "    dthy_dry.append(abs(dth_dr[1,1]))\n",
    "\n",
    "dthx_drx = np.array(dthx_drx) * 206265 * pixSize # arcsec per pixel\n",
    "dthy_dry = np.array(dthy_dry) * 206265 * pixSize # arcsec per pixel\n",
    "\n",
    "fig, (ax1, ax2) = plt.subplots(ncols=2, figsize=(10, 4))\n",
    "ax1.plot(thetas, dthx_drx, c='b', label='radial')\n",
    "ax1.plot(thetas, dthy_dry, c='r', label='tangential')\n",
    "ax1.set_xlabel('radius (deg)')\n",
    "ax1.set_ylabel('pixel size (arcsec)')\n",
    "ax1.legend(loc='lower left')\n",
    "ax1.set_title(\"LSST\")\n",
    "\n",
    "ax2.plot(thetas, np.array(dthx_drx)/np.array(dthy_dry))\n",
    "ax2.set_xlabel('radius (deg)')\n",
    "ax2.set_ylabel('b/a')\n",
    "ax2.set_title(\"LSST\")\n",
    "\n",
    "fig.tight_layout()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-08-31T17:29:55.840714Z",
     "start_time": "2019-08-31T17:29:55.441007Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "# All on the same plot!\n",
    "# And show residuals to third order fits\n",
    "fig, (ax1, ax2) = plt.subplots(ncols=2, figsize=(12, 6))\n",
    "\n",
    "ax1.set_xlabel('radius (deg)', fontsize=18)\n",
    "ax1.set_ylabel('relative pixel size on sky', fontsize=18)\n",
    "\n",
    "ax2.set_xlabel('radius (deg)', fontsize=18)\n",
    "ax2.set_ylabel('radial/tangential', fontsize=18)\n",
    "\n",
    "fig2, (ax3, ax4) = plt.subplots(ncols=2, figsize=(12, 6))\n",
    "\n",
    "for max_th, pixSize, telescope, color, name in zip(\n",
    "    [0.75, 1.1, 1.75], \n",
    "    [15e-6, 15e-6, 10e-6], \n",
    "    [HSC_telescope, DECam_telescope, LSST_telescope],\n",
    "    ['red', 'blue', 'green'],\n",
    "    ['HSC', 'DECam', 'LSST'],\n",
    "):\n",
    "\n",
    "    dthx_drx = []\n",
    "    dthy_dry = []\n",
    "    thetas = np.linspace(0., max_th, 20)\n",
    "    r = []\n",
    "    for th in thetas:\n",
    "        dth_dr = batoid.dthdr(telescope, np.deg2rad(th), 0, 620e-9)\n",
    "        dthx_drx.append(abs(dth_dr[0,0]))\n",
    "        dthy_dry.append(abs(dth_dr[1,1]))\n",
    "        chiefRay = batoid.RayVector.fromStop(0.0, 0.0, optic=telescope, wavelength=620e-9, theta_x=np.deg2rad(th), theta_y=0)\n",
    "        telescope.trace(chiefRay)\n",
    "        r.append(chiefRay.x[0])\n",
    "    r = np.array(r)\n",
    "    dthx_drx = np.array(dthx_drx) * 206265 * pixSize # arcsec per pixel\n",
    "    dthy_dry = np.array(dthy_dry) * 206265 * pixSize # arcsec per pixel\n",
    "\n",
    "    ax1.plot(thetas, dthx_drx/dthx_drx[0], c=color, ls=':', label=\"{} radial\".format(name))\n",
    "    ax1.plot(thetas, dthy_dry/dthy_dry[0], c=color, label=\"{} tangential\".format(name))\n",
    "    ax2.plot(thetas, np.array(dthx_drx)/np.array(dthy_dry), c=color, label=name)\n",
    "\n",
    "    print(telescope.name)\n",
    "    print(\"th_r: m->rad  \", np.polyfit(r, np.deg2rad(thetas), 3))\n",
    "    print(\"r_th: rad->m  \", np.polyfit(np.deg2rad(thetas), r, 3))\n",
    "    print(\"dthrdr_r: m->arcsec/pix \", np.polyfit(r, dthx_drx, 3))\n",
    "    print(\"dthtdt_r: m->arcsec/pix\", np.polyfit(r, dthy_dry, 3))\n",
    "\n",
    "    ax3.plot(\n",
    "        thetas[1:], \n",
    "        r[1:]/np.poly1d(np.polyfit(np.deg2rad(thetas), r, 3))(np.deg2rad(thetas[1:])), \n",
    "        c=color,\n",
    "        label=\"{} r_th\".format(name)\n",
    "    )\n",
    "    ax4.plot(\n",
    "        r[1:],\n",
    "        np.deg2rad(thetas[1:])/np.poly1d(np.polyfit(r, np.deg2rad(thetas), 3))(r[1:]),\n",
    "        ls='--',\n",
    "        c=color,\n",
    "        label=\"{} th_r\".format(name)\n",
    "    )\n",
    "    ax4.plot(\n",
    "        r[1:],\n",
    "        dthx_drx[1:]/np.poly1d(np.polyfit(r, dthx_drx, 3))(r[1:]), \n",
    "        c=color,\n",
    "        ls=':', label=\"{} dthrdr_r\".format(name)\n",
    "    )\n",
    "    ax4.plot(\n",
    "        r[1:],\n",
    "        dthy_dry[1:]/np.poly1d(np.polyfit(r, dthy_dry, 3))(r[1:]), \n",
    "        c=color,\n",
    "        ls='-', label=\"{} dthtdt_r\".format(name)\n",
    "    )\n",
    "\n",
    "    \n",
    "for ax in (ax1, ax2):\n",
    "    ax.tick_params(axis='both', which='major', labelsize=18)\n",
    "    ax.tick_params(axis='both', which='minor', labelsize=18)\n",
    "\n",
    "ax1.legend(fontsize=14)\n",
    "ax2.legend(fontsize=14)\n",
    "\n",
    "fig.tight_layout()\n",
    "\n",
    "ax3.legend(fontsize=14)\n",
    "ax4.legend(fontsize=14)\n",
    "\n",
    "fig2.tight_layout()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
