{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "6026a74b",
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib widget\n",
    "\n",
    "isdark = False\n",
    "\n",
    "from rayoptics.environment import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "c7d740ee",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "opm = OpticalModel()\n",
    "sm  = opm['seq_model']\n",
    "osp = opm['optical_spec']\n",
    "pm = opm['parax_model']\n",
    "\n",
    "osp['pupil'] = PupilSpec(osp, key=['object', 'pupil'], value=16)\n",
    "osp['fov'] = FieldSpec(osp, key=['object', 'angle'], value=0.5, flds=[0., 0.707, 1.], is_relative=True)\n",
    "osp['wvls'] = WvlSpec([('F', 0.5), (587.5618, 1.0), ('C', 0.5)], ref_wl=1)\n",
    "\n",
    "opm.radius_mode = True\n",
    "\n",
    "sm.gaps[0].thi=1e10\n",
    "\n",
    "def calc_curvature(n, fl):\n",
    "    return (n-1)*fl\n",
    "\n",
    "n_bk7 = 1.5168\n",
    "n_lasf9 = 1.85025\n",
    "n_f2 =  1.62005\n",
    "\n",
    "v_bk7 = 64.17\n",
    "v_lasf9 = 32.16\n",
    "v_f2 = 36.43\n",
    "# try for chaining a 3x telescope setup with a second 3x telescope setup\n",
    "f0 = 150\n",
    "f0_0 = (v_bk7-v_f2)*f0/v_bk7\n",
    "f0_1 = -f0_0*v_bk7/v_f2\n",
    "f0_actual = 1/(1/f0_0+1/f0_1)\n",
    "f1 = 60\n",
    "# 1/f = 1/f0 + 1/f1 = 1/f0 - v2/(f0*v1) = (v1-v2)/(v1*f0)\n",
    "# f0 = (v1-v2)*f/v1\n",
    "f1_0 = (v_bk7-v_f2)*f1/v_bk7\n",
    "f1_1 = -f1_0*v_bk7/v_f2\n",
    "\n",
    "f2 = 150\n",
    "f3 = 50\n",
    "\n",
    "f1_stacked = 2*f1\n",
    "\n",
    "r0 = calc_curvature(n_lasf9, f0)\n",
    "r0_0 = calc_curvature(n_bk7, f0_0)\n",
    "r0_1 = calc_curvature(n_f2, f0_1)\n",
    "r1 = calc_curvature(n_lasf9, f1)\n",
    "r1_0 = calc_curvature(n_bk7, f1_0)\n",
    "r1_1 = calc_curvature(n_f2, f1_1)\n",
    "r2 = calc_curvature(n_lasf9, f2)\n",
    "r3 = calc_curvature(n_lasf9, f3)\n",
    "\n",
    "#sm.add_surface([r0_0, 2, 'N-BK7', 'Schott', 42/2])\n",
    "#sm.add_surface([1e9, 2, 'N-F2', 'Schott', 42/2])\n",
    "#sm.add_surface([-r0_1, 180+2*f1])\n",
    "#sm.add_surface([-r0_1, 180+2*36.])\n",
    "\n",
    "sm.add_surface([r1_0, 4, 'N-BK7', 'Schott', 16])\n",
    "sm.add_surface([1e9, 2, 'N-F2', 'Schott', 16])\n",
    "sm.add_surface([-r1_1, 30])\n",
    "#sm.add_surface([1e9, 2, 'N-LASF9', 'Schott', 16])\n",
    "#sm.add_surface([-r1, f2+f3])\n",
    "\n",
    "\n",
    "#sm.add_surface([r2, 2, 'N-LASF9', 'Schott', 16])\n",
    "#sm.add_surface([1e9, f2+f3])\n",
    "#sm.add_surface([1e9, 2, 'N-LASF9', 'Schott', 16])\n",
    "#sm.add_surface([-r3, f3])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "a26f0ff4",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "e840d701f8b64d97a33b0516f03be150",
       "version_major": 2,
       "version_minor": 0
      },
      "image/png": "[encoded data, 30792 characters]",
      "text/html": [
       "\n",
       "            <div style=\"display: inline-block;\">\n",
       "                <div class=\"jupyter-widgets widget-label\" style=\"text-align: center;\">\n",
       "                    Figure\n",
       "                </div>\n",
       "                <img src='data:image/png;base64,[encoded data, 30792 characters]' width=640.0/>\n",
       "            </div>\n",
       "        "
      ],
      "text/plain": [
       "Canvas(toolbar=Toolbar(toolitems=[('Home', 'Reset original view', 'home', 'home'), ('Back', 'Back to previous …"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "opm.update_model()\n",
    "opm.seq_model.gaps[-1].thi = opm.optical_spec.parax_data.fod.bfl\n",
    "opm.update_model()\n",
    "\n",
    "layout_plt = plt.figure(FigureClass=InteractiveLayout, opt_model=opm, is_dark=isdark).plot()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "85fd9885",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "efl               50.17\n",
      "ffl              -54.42\n",
      "pp1              -4.251\n",
      "bfl               42.68\n",
      "ppk               7.488\n",
      "f/#               3.135\n",
      "m            -5.017e-09\n",
      "red          -1.993e+08\n",
      "obj_dist          1e+10\n",
      "obj_ang             0.5\n",
      "enp_dist             -0\n",
      "enp_radius            8\n",
      "na obj            8e-10\n",
      "n obj                 1\n",
      "img_dist          42.68\n",
      "img_ht           0.4378\n",
      "exp_dist         -3.569\n",
      "exp_radius        7.375\n",
      "na img          -0.1575\n",
      "n img                 1\n",
      "optical invariant      0.06981\n"
     ]
    }
   ],
   "source": [
    "pm.first_order_data()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "fbb733ef",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "0aeccd0270894697b29acb97e2d6d816",
       "version_major": 2,
       "version_minor": 0
      },
      "image/png": "[encoded data, 52544 characters]",
      "text/html": [
       "\n",
       "            <div style=\"display: inline-block;\">\n",
       "                <div class=\"jupyter-widgets widget-label\" style=\"text-align: center;\">\n",
       "                    Figure\n",
       "                </div>\n",
       "                <img src='data:image/png;base64,[encoded data, 52544 characters]' width=640.0/>\n",
       "            </div>\n",
       "        "
      ],
      "text/plain": [
       "Canvas(toolbar=Toolbar(toolitems=[('Home', 'Reset original view', 'home', 'home'), ('Back', 'Back to previous …"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "abr_plt = plt.figure(FigureClass=RayFanFigure, opt_model=opm, data_type='Ray',\n",
    "                     scale_type=Fit.All_Same).plot()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "95deb34e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "4022d630702748638ff6fc6112bdf7c1",
       "version_major": 2,
       "version_minor": 0
      },
      "image/png": "[encoded data, 62704 characters]",
      "text/html": [
       "\n",
       "            <div style=\"display: inline-block;\">\n",
       "                <div class=\"jupyter-widgets widget-label\" style=\"text-align: center;\">\n",
       "                    Figure\n",
       "                </div>\n",
       "                <img src='data:image/png;base64,[encoded data, 62704 characters]' width=640.0/>\n",
       "            </div>\n",
       "        "
      ],
      "text/plain": [
       "Canvas(toolbar=Toolbar(toolitems=[('Home', 'Reset original view', 'home', 'home'), ('Back', 'Back to previous …"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "spot_plt = plt.figure(FigureClass=SpotDiagramFigure, opt_model=opm, is_dark=isdark).plot()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "fe1bd365",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "88999462275b4e07912e992b6c6696a0",
       "version_major": 2,
       "version_minor": 0
      },
      "image/png": "[encoded data, 30792 characters]",
      "text/html": [
       "\n",
       "            <div style=\"display: inline-block;\">\n",
       "                <div class=\"jupyter-widgets widget-label\" style=\"text-align: center;\">\n",
       "                    Figure\n",
       "                </div>\n",
       "                <img src='data:image/png;base64,[encoded data, 30792 characters]' width=640.0/>\n",
       "            </div>\n",
       "        "
      ],
      "text/plain": [
       "Canvas(toolbar=Toolbar(toolitems=[('Home', 'Reset original view', 'home', 'home'), ('Back', 'Back to previous …"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "layout_plt = plt.figure(FigureClass=InteractiveLayout, opt_model=opm, is_dark=isdark).plot()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "0e1c606f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.71394997, 0.69072105, 0.67781734])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def dump_dist(p, wi, ray_pkg, fld, wvl, foc):\n",
    "    if ray_pkg is not None:\n",
    "        image_pt = fld.ref_sphere[0]\n",
    "        ray = ray_pkg[0]\n",
    "        dist = foc / ray[-1][1][2]\n",
    "        defocused_pt = ray[-1][0] + dist*ray[-1][1]\n",
    "        t_abr = defocused_pt - image_pt\n",
    "        return np.sqrt(np.sum(t_abr*t_abr))\n",
    "    \n",
    "def spot_rms(sm):\n",
    "    return np.sqrt(np.mean(np.square(sm.trace_grid(dump_dist, 0, form='list', append_if_none=False)[0]), axis=1))\n",
    "\n",
    "spot_rms(sm)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "b0c10933",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "f8be945791884bba8693c3440ce5843b",
       "version_major": 2,
       "version_minor": 0
      },
      "image/png": "[encoded data, 37624 characters]",
      "text/html": [
       "\n",
       "            <div style=\"display: inline-block;\">\n",
       "                <div class=\"jupyter-widgets widget-label\" style=\"text-align: center;\">\n",
       "                    Figure\n",
       "                </div>\n",
       "                <img src='data:image/png;base64,[encoded data, 37624 characters]' width=640.0/>\n",
       "            </div>\n",
       "        "
      ],
      "text/plain": [
       "Canvas(toolbar=Toolbar(toolitems=[('Home', 'Reset original view', 'home', 'home'), ('Back', 'Back to previous …"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "(-5.142571285642823, -4.9824912456228105, -4.902451225612808)"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import rayoptics.optical.model_constants as mc\n",
    "\n",
    "old_gap = opm.seq_model.gaps[-1].thi\n",
    "offsets = np.linspace(-40, 40, 2000)\n",
    "\n",
    "opm.seq_model.gaps[-1].thi = opm.optical_spec.parax_data.fod.bfl\n",
    "opm.update_model()\n",
    "\n",
    "def dump_rays(p, wi, ray_pkg, fld, wvl, foc):\n",
    "    if ray_pkg is not None:\n",
    "        image_pt = fld.ref_sphere[0]\n",
    "        ray = ray_pkg[mc.ray]\n",
    "        v = ray[-1][mc.d][0:2] / ray[-1][mc.d][2]\n",
    "        return [ray[-1][mc.p][0:2] - image_pt[0:2], v]\n",
    "\n",
    "vals, colors = sm.trace_grid(dump_rays, 0, form='list', append_if_none=False)\n",
    "v1, v2, v3 = vals\n",
    "v1_p = v1[:,0]\n",
    "v1_v = v1[:,1]\n",
    "v2_p = v2[:,0]\n",
    "v2_v = v2[:,1]\n",
    "v3_p = v3[:,0]\n",
    "v3_v = v3[:,1]\n",
    "\n",
    "v1_rms = np.zeros(offsets.size)\n",
    "v2_rms = np.zeros(offsets.size)\n",
    "v3_rms = np.zeros(offsets.size)\n",
    "\n",
    "for i in range(offsets.size):\n",
    "    v1_rms[i] = np.sqrt(np.mean(np.square(v1_p+v1_v*offsets[i])))\n",
    "    v2_rms[i] = np.sqrt(np.mean(np.square(v2_p+v2_v*offsets[i])))\n",
    "    v3_rms[i] = np.sqrt(np.mean(np.square(v3_p+v3_v*offsets[i])))\n",
    "\n",
    "opm.seq_model.gaps[-1].thi = old_gap\n",
    "opm.update_model()\n",
    "\n",
    "plt.figure()\n",
    "plt.plot(offsets, v1_rms, color=colors[0])\n",
    "plt.plot(offsets, v2_rms, color=colors[1])\n",
    "plt.plot(offsets, v3_rms, color=colors[2])\n",
    "plt.show()\n",
    "\n",
    "min_b, min_g, min_r = (offsets[np.argmin(v1_rms)], offsets[np.argmin(v2_rms)], offsets[np.argmin(v3_rms)])\n",
    "min_b, min_g, min_r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "5c091777",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.21615536 0.21605082 0.21695656]\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "e6604b0b24e443a8be6d33d385923333",
       "version_major": 2,
       "version_minor": 0
      },
      "image/png": "[encoded data, 74088 characters]",
      "text/html": [
       "\n",
       "            <div style=\"display: inline-block;\">\n",
       "                <div class=\"jupyter-widgets widget-label\" style=\"text-align: center;\">\n",
       "                    Figure\n",
       "                </div>\n",
       "                <img src='data:image/png;base64,[encoded data, 74088 characters]' width=640.0/>\n",
       "            </div>\n",
       "        "
      ],
      "text/plain": [
       "Canvas(toolbar=Toolbar(toolitems=[('Home', 'Reset original view', 'home', 'home'), ('Back', 'Back to previous …"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "opm.seq_model.gaps[-1].thi = opm.optical_spec.parax_data.fod.bfl + (min_r*0.25 + min_g*0.5 + min_b*0.25)\n",
    "opm.update_model()\n",
    "\n",
    "spot_plt = plt.figure(FigureClass=SpotDiagramFigure, opt_model=opm, is_dark=isdark).plot()\n",
    "print(spot_rms(sm))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "4f7eb3b9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "f2fc88441ff74f18a464018e99b7d8ba",
       "version_major": 2,
       "version_minor": 0
      },
      "image/png": "[encoded data, 73564 characters]",
      "text/html": [
       "\n",
       "            <div style=\"display: inline-block;\">\n",
       "                <div class=\"jupyter-widgets widget-label\" style=\"text-align: center;\">\n",
       "                    Figure\n",
       "                </div>\n",
       "                <img src='data:image/png;base64,[encoded data, 73564 characters]' width=640.0/>\n",
       "            </div>\n",
       "        "
      ],
      "text/plain": [
       "Canvas(toolbar=Toolbar(toolitems=[('Home', 'Reset original view', 'home', 'home'), ('Back', 'Back to previous …"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "abr_plt = plt.figure(FigureClass=RayFanFigure, opt_model=opm, data_type='Ray',\n",
    "                     scale_type=Fit.All_Same).plot()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "861f3922",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-0.020010005002504272"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# let's try chaining the two lenses together and seeing how that looks\n",
    "\n",
    "# first let's wrap that focus calculation code into a function\n",
    "\n",
    "\n",
    "def dump_rays(p, wi, ray_pkg, fld, wvl, foc):\n",
    "    if ray_pkg is not None:\n",
    "        image_pt = fld.ref_sphere[0]\n",
    "        ray = ray_pkg[mc.ray]\n",
    "        v = ray[-1][mc.d][0:2] / ray[-1][mc.d][2]\n",
    "        return [ray[-1][mc.p][0:2] - image_pt[0:2], v]\n",
    "    \n",
    "def get_focus(sm, weights=np.array([0.25, 0.5, 0.25])):\n",
    "    offsets = np.linspace(-40, 40, 2000)\n",
    "    vals, colors = sm.trace_grid(dump_rays, 0, form='list', append_if_none=False)\n",
    "\n",
    "    rms = np.zeros((offsets.size, len(vals)), dtype=float)\n",
    "\n",
    "    for i in range(offsets.size):\n",
    "        for j in range(len(vals)):\n",
    "            rms[i][j] = np.sqrt(np.mean(np.square(vals[j][:,0]+vals[j][:,1]*offsets[i])))\n",
    "\n",
    "    return offsets[np.argmin(np.sum(rms*weights, axis=1))]\n",
    "\n",
    "get_focus(sm)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "ef2a5df6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "0cad0f0a1ab94400a94816176652e87b",
       "version_major": 2,
       "version_minor": 0
      },
      "image/png": "[encoded data, 17736 characters]",
      "text/html": [
       "\n",
       "            <div style=\"display: inline-block;\">\n",
       "                <div class=\"jupyter-widgets widget-label\" style=\"text-align: center;\">\n",
       "                    Figure\n",
       "                </div>\n",
       "                <img src='data:image/png;base64,[encoded data, 17736 characters]' width=640.0/>\n",
       "            </div>\n",
       "        "
      ],
      "text/plain": [
       "Canvas(toolbar=Toolbar(toolitems=[('Home', 'Reset original view', 'home', 'home'), ('Back', 'Back to previous …"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "opm2 = OpticalModel()\n",
    "sm2  = opm2['seq_model']\n",
    "osp2 = opm2['optical_spec']\n",
    "pm2 = opm2['parax_model']\n",
    "\n",
    "osp2['pupil'] = PupilSpec(osp2, key=['object', 'pupil'], value=16)\n",
    "osp2['fov'] = FieldSpec(osp2, key=['object', 'angle'], value=0.5, flds=[0., 0.707, 1.], is_relative=True)\n",
    "osp2['wvls'] = WvlSpec([('F', 0.5), (587.5618, 1.0), ('C', 0.5)], ref_wl=1)\n",
    "\n",
    "opm2.radius_mode = True\n",
    "\n",
    "sm2.gaps[0].thi=1e10\n",
    "sm2.add_surface([r0_0, 2, 'N-BK7', 'Schott', 42/2])\n",
    "sm2.add_surface([1e9, 2, 'N-F2', 'Schott', 42/2])\n",
    "sm2.add_surface([-r0_1, f0])\n",
    "\n",
    "opm2.update_model()\n",
    "sm2.gaps[-1].thi += get_focus(sm2)\n",
    "\n",
    "opm2.update_model()\n",
    "\n",
    "layout_plt = plt.figure(FigureClass=InteractiveLayout, opt_model=opm2, is_dark=isdark).plot()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "79f52ef9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "e8f35f6d6c294348bd650cb9483b2d49",
       "version_major": 2,
       "version_minor": 0
      },
      "image/png": "[encoded data, 16108 characters]",
      "text/html": [
       "\n",
       "            <div style=\"display: inline-block;\">\n",
       "                <div class=\"jupyter-widgets widget-label\" style=\"text-align: center;\">\n",
       "                    Figure\n",
       "                </div>\n",
       "                <img src='data:image/png;base64,[encoded data, 16108 characters]' width=640.0/>\n",
       "            </div>\n",
       "        "
      ],
      "text/plain": [
       "Canvas(toolbar=Toolbar(toolitems=[('Home', 'Reset original view', 'home', 'home'), ('Back', 'Back to previous …"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "#use the gap from sm\n",
    "sm2.gaps[-1].thi += sm.gaps[-1].thi\n",
    "\n",
    "sm2.add_surface([r1_1, 2, 'N-F2', 'Schott', 16])\n",
    "sm2.add_surface([1e9, 4, 'N-BK7', 'Schott', 16])\n",
    "sm2.add_surface([-r1_0, sm.gaps[-1].thi])\n",
    "\n",
    "opm2.update_model()\n",
    "layout_plt = plt.figure(FigureClass=InteractiveLayout, opt_model=opm2, is_dark=isdark).plot()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "68f4afb4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "efl               951.4\n",
      "ffl                2559\n",
      "pp1                3510\n",
      "bfl              -280.2\n",
      "ppk                1232\n",
      "f/#               59.46\n",
      "m            -9.514e-08\n",
      "red          -1.051e+07\n",
      "obj_dist          1e+10\n",
      "obj_ang             0.5\n",
      "enp_dist             -0\n",
      "enp_radius            8\n",
      "na obj            8e-10\n",
      "n obj                 1\n",
      "img_dist         -280.2\n",
      "img_ht            8.303\n",
      "exp_dist         -244.4\n",
      "exp_radius        2.974\n",
      "na img        -0.008409\n",
      "n img                 1\n",
      "optical invariant      0.06981\n"
     ]
    }
   ],
   "source": [
    "pm2.first_order_data()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "b1fe8f91",
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "1028ff5f0ff14f30a415986a879a1c3a",
       "version_major": 2,
       "version_minor": 0
      },
      "image/png": "[encoded data, 15712 characters]",
      "text/html": [
       "\n",
       "            <div style=\"display: inline-block;\">\n",
       "                <div class=\"jupyter-widgets widget-label\" style=\"text-align: center;\">\n",
       "                    Figure\n",
       "                </div>\n",
       "                <img src='data:image/png;base64,[encoded data, 15712 characters]' width=640.0/>\n",
       "            </div>\n",
       "        "
      ],
      "text/plain": [
       "Canvas(toolbar=Toolbar(toolitems=[('Home', 'Reset original view', 'home', 'home'), ('Back', 'Back to previous …"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "sm2.insert_surface_and_gap()\n",
    "cur = sm2.cur_surface\n",
    "sm2.ifcs[cur] = ThinLens(power=1/30.)\n",
    "sm2.gaps[cur].thi = 30.\n",
    "\n",
    "opm2.update_model()\n",
    "layout_plt = plt.figure(FigureClass=InteractiveLayout, opt_model=opm2, is_dark=isdark).plot()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "4d32ba03",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.33087284 0.03391144 0.26097283]\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "76948307336f4eef84e6edcebf9e20ae",
       "version_major": 2,
       "version_minor": 0
      },
      "image/png": "[encoded data, 80564 characters]",
      "text/html": [
       "\n",
       "            <div style=\"display: inline-block;\">\n",
       "                <div class=\"jupyter-widgets widget-label\" style=\"text-align: center;\">\n",
       "                    Figure\n",
       "                </div>\n",
       "                <img src='data:image/png;base64,[encoded data, 80564 characters]' width=640.0/>\n",
       "            </div>\n",
       "        "
      ],
      "text/plain": [
       "Canvas(toolbar=Toolbar(toolitems=[('Home', 'Reset original view', 'home', 'home'), ('Back', 'Back to previous …"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "spot_plt = plt.figure(FigureClass=SpotDiagramFigure, opt_model=opm2, is_dark=isdark).plot()\n",
    "print(spot_rms(sm2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "f9ce6fcf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(-0.3801900950475243, array([0.35119223, 0.02588293, 0.23321615]))"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "001351d238a34bc2be683416c8cf5d63",
       "version_major": 2,
       "version_minor": 0
      },
      "image/png": "[encoded data, 78864 characters]",
      "text/html": [
       "\n",
       "            <div style=\"display: inline-block;\">\n",
       "                <div class=\"jupyter-widgets widget-label\" style=\"text-align: center;\">\n",
       "                    Figure\n",
       "                </div>\n",
       "                <img src='data:image/png;base64,[encoded data, 78864 characters]' width=640.0/>\n",
       "            </div>\n",
       "        "
      ],
      "text/plain": [
       "Canvas(toolbar=Toolbar(toolitems=[('Home', 'Reset original view', 'home', 'home'), ('Back', 'Back to previous …"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "adjust = get_focus(sm2)\n",
    "sm2.gaps[-1].thi += adjust\n",
    "\n",
    "opm2.update_model()\n",
    "spot_plt = plt.figure(FigureClass=SpotDiagramFigure, opt_model=opm2, is_dark=isdark).plot()\n",
    "adjust, spot_rms(sm2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f642321b",
   "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.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
