{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-16T04:22:11.040358Z",
     "start_time": "2019-09-16T04:22:10.798707Z"
    }
   },
   "outputs": [],
   "source": [
    "import batoid\n",
    "import time\n",
    "import numpy as np\n",
    "from ipywidgets import interact\n",
    "import ipywidgets as widgets\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-16T04:25:28.478794Z",
     "start_time": "2019-09-16T04:25:28.425092Z"
    }
   },
   "outputs": [],
   "source": [
    "telescope = batoid.Optic.fromYaml(\"HSC.yaml\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-16T04:29:46.221291Z",
     "start_time": "2019-09-16T04:29:45.747308Z"
    }
   },
   "outputs": [],
   "source": [
    "# Get Huygens and FFT PSFs for in-focus telescope and compare\n",
    "airy_radius = 1.22*750e-9/8.2*206265 # arcsec\n",
    "airy_radius /= 0.168 # pixels\n",
    "airy_radius *= 15 # microns\n",
    "\n",
    "th = np.linspace(0, 2*np.pi, 100)\n",
    "cth = airy_radius*np.cos(th)\n",
    "sth = airy_radius*np.sin(th)\n",
    "\n",
    "theta_x = 0.0\n",
    "theta_y = 0.0\n",
    "wavelength = 750e-9\n",
    "\n",
    "fftPSFLattice = batoid.fftPSF(\n",
    "    telescope, theta_x, theta_y, wavelength\n",
    ")\n",
    "fftpsf = fftPSFLattice.array[16:-16, 16:-16]\n",
    "scale = np.sqrt(np.linalg.det(fftPSFLattice.primitiveVectors))\n",
    "extent = scale*fftpsf.shape[0]/2*np.r_[-1., 1., -1., 1.] # meters\n",
    "extent -= scale/2  # shift half pixel\n",
    "extent *= 1e6  # meters -> microns\n",
    "plt.imshow(fftpsf/fftpsf.sum(), extent=extent)\n",
    "plt.plot(cth, sth, c='r', lw=1)\n",
    "plt.colorbar()\n",
    "plt.show()\n",
    "\n",
    "huygensPSFLattice = batoid.huygensPSF(\n",
    "    telescope, theta_x, theta_y, wavelength, nx=32\n",
    ")\n",
    "huygensPSF = huygensPSFLattice.array[16:-16, 16:-16]\n",
    "\n",
    "plt.imshow(huygensPSF/huygensPSF.sum(), extent=extent)\n",
    "plt.plot(cth, sth, c='r', lw=1)\n",
    "plt.colorbar()\n",
    "plt.show()\n",
    "\n",
    "plt.imshow(fftpsf/fftpsf.sum() - huygensPSF/huygensPSF.sum(), extent=extent)\n",
    "plt.colorbar()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-16T04:30:11.460433Z",
     "start_time": "2019-09-16T04:30:11.010041Z"
    }
   },
   "outputs": [],
   "source": [
    "# Get Huygens and FFT PSFs for in-focus telescope and compare\n",
    "airy_radius = 1.22*750e-9/8.2*206265 # arcsec\n",
    "airy_radius /= 0.168 # pixels\n",
    "airy_radius *= 15 # microns\n",
    "\n",
    "th = np.linspace(0, 2*np.pi, 100)\n",
    "cth = airy_radius*np.cos(th)\n",
    "sth = airy_radius*np.sin(th)\n",
    "\n",
    "theta_x = 0.0\n",
    "theta_y = np.deg2rad(0.75)\n",
    "wavelength = 750e-9\n",
    "\n",
    "fftPSFLattice = batoid.fftPSF(\n",
    "    telescope, theta_x, theta_y, wavelength\n",
    ")\n",
    "fftpsf = fftPSFLattice.array[16:-16, 16:-16]\n",
    "scale = np.sqrt(np.linalg.det(fftPSFLattice.primitiveVectors))\n",
    "extent = scale*fftpsf.shape[0]/2*np.r_[-1., 1., -1., 1.] # meters\n",
    "extent -= scale/2  # shift half pixel\n",
    "extent *= 1e6  # meters -> microns\n",
    "plt.imshow(fftpsf/fftpsf.sum(), extent=extent)\n",
    "plt.plot(cth, sth, c='r', lw=1)\n",
    "plt.colorbar()\n",
    "plt.show()\n",
    "\n",
    "huygensPSFLattice = batoid.huygensPSF(\n",
    "    telescope, theta_x, theta_y, wavelength, nx=32\n",
    ")\n",
    "huygensPSF = huygensPSFLattice.array[16:-16, 16:-16]\n",
    "\n",
    "plt.imshow(huygensPSF/huygensPSF.sum(), extent=extent)\n",
    "plt.plot(cth, sth, c='r', lw=1)\n",
    "plt.colorbar()\n",
    "plt.show()\n",
    "\n",
    "plt.imshow(fftpsf/fftpsf.sum() - huygensPSF/huygensPSF.sum(), extent=extent)\n",
    "plt.colorbar()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-16T04:30:56.529786Z",
     "start_time": "2019-09-16T04:30:28.578876Z"
    }
   },
   "outputs": [],
   "source": [
    "# Get Huygens and FFT PSFs for out-of-focus telescope and compare\n",
    "airy_radius = 1.22*750e-9/8.2*206265 # arcsec\n",
    "airy_radius /= 0.168 # pixels\n",
    "airy_radius *= 15 # microns\n",
    "\n",
    "th = np.linspace(0, 2*np.pi, 100)\n",
    "cth = airy_radius*np.cos(th)\n",
    "sth = airy_radius*np.sin(th)\n",
    "\n",
    "# Get Huygens and FFT PSFs for out-of-focus telescope and compare\n",
    "theta_x = 0.0\n",
    "theta_y = 0.0\n",
    "wavelength = 750e-9\n",
    "cam_dz = 200e-6\n",
    "\n",
    "shifted_telescope = (telescope\n",
    "        .withGloballyShiftedOptic(\"SubaruHSC.HSC\", [0, 0, cam_dz])\n",
    ")\n",
    "nx = 256\n",
    "\n",
    "fftPSFLattice = batoid.fftPSF(\n",
    "    shifted_telescope, theta_x, theta_y, wavelength, nx=nx\n",
    ")\n",
    "fftpsf = fftPSFLattice.array[nx//2:-nx//2, nx//2:-nx//2]\n",
    "scale = np.sqrt(np.linalg.det(fftPSFLattice.primitiveVectors))\n",
    "extent = scale*fftpsf.shape[0]/2*np.r_[-1., 1., -1., 1.] # meters\n",
    "extent -= scale/2  # shift half pixel\n",
    "extent *= 1e6  # meters -> microns\n",
    "plt.imshow(fftpsf/fftpsf.sum(), extent=extent)\n",
    "plt.plot(cth, sth, c='r', lw=1)\n",
    "plt.colorbar()\n",
    "plt.show()\n",
    "\n",
    "huygensPSFLattice = batoid.huygensPSF(\n",
    "    shifted_telescope, theta_x, theta_y, wavelength, nx=nx, nxOut=nx//2\n",
    ")\n",
    "huygensPSF = huygensPSFLattice.array\n",
    "\n",
    "plt.imshow(huygensPSF/huygensPSF.sum(), extent=extent)\n",
    "plt.plot(cth, sth, c='r', lw=1)\n",
    "plt.colorbar()\n",
    "plt.show()\n",
    "\n",
    "plt.imshow(fftpsf/fftpsf.sum() - huygensPSF/huygensPSF.sum(), extent=extent)\n",
    "plt.colorbar()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3",
   "metadata": {
    "interpreter": {
     "hash": "aee8b7b246df8f9039afb4144a1f6fd8d2ca17a180786b69acc140d282b71a49"
    }
   }
  },
  "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
}