{
 "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": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "150.0 17.29156926912888 -30.45841339555312\n"
     ]
    }
   ],
   "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 = 40\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",
    "print(f0_actual, f1_0, f1_1)\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, 6, '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",
    "#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": "98b58e1952da43188327834bb626eb8e",
       "version_major": 2,
       "version_minor": 0
      },
      "image/png": "[encoded data, 16780 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, 16780 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",
    "\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               142.6\n",
      "ffl              -145.8\n",
      "pp1              -3.188\n",
      "bfl                 137\n",
      "ppk               5.616\n",
      "f/#               8.914\n",
      "m            -1.426e-08\n",
      "red          -7.011e+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            137\n",
      "img_ht            1.245\n",
      "exp_dist         -117.5\n",
      "exp_radius        7.825\n",
      "na img           -0.056\n",
      "n img                 1\n",
      "optical invariant      0.06981\n"
     ]
    }
   ],
   "source": [
    "pm.first_order_data()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "8f39b24f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "ca0173a4b23649319b1f4c863f8bf331",
       "version_major": 2,
       "version_minor": 0
      },
      "image/png": "[encoded data, 17220 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, 17220 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": [
    "# add a focusing mirror\n",
    "#sm.add_surface([100, 2, 1.85, 16])\n",
    "#sm.add_surface([1e9, 36])\n",
    "\n",
    "opm.update_model()\n",
    "\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": 6,
   "id": "fbb733ef",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "137.01040075032165"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "3eb4ff0e3ef74936817a854bcda60aa5",
       "version_major": 2,
       "version_minor": 0
      },
      "image/png": "[encoded data, 59548 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, 59548 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 -= 0.1\n",
    "opm.update_model()\n",
    "#spot_plt = plt.figure(FigureClass=SpotDiagramFigure, opt_model=opm, scale_type=Fit.User_Scale,\n",
    "#                      user_scale_value=0.1, is_dark=isdark).plot()\n",
    "abr_plt = plt.figure(FigureClass=RayFanFigure, opt_model=opm, data_type='Ray',\n",
    "                     scale_type=Fit.All_Same).plot()\n",
    "opm.seq_model.gaps[-1].thi"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "95deb34e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "137.01040075032165"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "f19416a806b7449ea25b9e07108190ba",
       "version_major": 2,
       "version_minor": 0
      },
      "image/png": "[encoded data, 74508 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, 74508 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 -= 0.1\n",
    "opm.update_model()\n",
    "spot_plt = plt.figure(FigureClass=SpotDiagramFigure, opt_model=opm, is_dark=isdark).plot()\n",
    "\n",
    "opm.seq_model.gaps[-1].thi"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "fe1bd365",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "d1c3dbff124a42308f23a57f64f81b07",
       "version_major": 2,
       "version_minor": 0
      },
      "image/png": "[encoded data, 17220 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, 17220 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": 9,
   "id": "622fd615",
   "metadata": {},
   "outputs": [],
   "source": [
    "def dump_pts(p, wi, ray_pkg, fld, wvl, foc):\n",
    "    if ray_pkg is not None:\n",
    "        ray = ray_pkg[0][-1]\n",
    "        return ray_pkg[0][-1][0]\n",
    "               \n",
    "#sm.trace_grid(dump_pts, 0, form='list', append_if_none=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "0e1c606f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.11280863 0.10780063 0.10210127]\n"
     ]
    }
   ],
   "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",
    "spot_rms = np.sqrt(np.mean(np.square(sm.trace_grid(dump_dist, 0, form='list', append_if_none=False)[0]), axis=1))\n",
    "print(spot_rms)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "9acb4d3f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\ndef calc_focus(opm, sm):\\n    old_gap = opm.seq_model.gaps[-1].thi\\n    offsets = np.linspace(-20, 20, 100)\\n    spots = np.zeros(offsets.shape)\\n    for i in range(offsets.shape[0]):\\n        opm.seq_model.gaps[-1].thi = opm.optical_spec.parax_data.fod.bfl + offsets[i]\\n        opm.update_model()\\n        spots[i] = np.sum(spot_rms(sm))\\n    opm.seq_model.gaps[-1].thi = old_gap\\n    opm.update_model()\\n    \\n    return offsets[np.argmin(spots)]\\n\\nf = calc_focus(opm, sm)\\nprint(f)\\n'"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "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",
    "def calc_focus(opm, sm):\n",
    "    old_gap = opm.seq_model.gaps[-1].thi\n",
    "    offsets = np.linspace(-20, 20, 100)\n",
    "    spots = np.zeros(offsets.shape)\n",
    "    for i in range(offsets.shape[0]):\n",
    "        opm.seq_model.gaps[-1].thi = opm.optical_spec.parax_data.fod.bfl + offsets[i]\n",
    "        opm.update_model()\n",
    "        spots[i] = np.sum(spot_rms(sm))\n",
    "    opm.seq_model.gaps[-1].thi = old_gap\n",
    "    opm.update_model()\n",
    "    \n",
    "    return offsets[np.argmin(spots)]\n",
    "\n",
    "f = calc_focus(opm, sm)\n",
    "print(f)\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "052f8df2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.16672045 0.1716864  0.1773614 ]\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "5f3a0239d48e43a784bb0547b0be36ca",
       "version_major": 2,
       "version_minor": 0
      },
      "image/png": "[encoded data, 74340 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, 74340 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 - 6.6666\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": 13,
   "id": "b0c10933",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "27f7df9f6b7845db852a3553cd5c1631",
       "version_major": 2,
       "version_minor": 0
      },
      "image/png": "[encoded data, 39744 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, 39744 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": [
       "(-2.6626626626626617, -2.5025025025025016, -2.3823823823823815)"
      ]
     },
     "execution_count": 13,
     "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(-20, 20, 1000)\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": 14,
   "id": "ed7d84be",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.03725505 0.03611947 0.03574278]\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "c82f5d3a4ed6417f95227afabf9ceb41",
       "version_major": 2,
       "version_minor": 0
      },
      "image/png": "[encoded data, 71648 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, 71648 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\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": 15,
   "id": "5a84c0f5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.03617745 0.03572421 0.03615391]\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "dbb42cd42d914017bae5cb1f98aa3c02",
       "version_major": 2,
       "version_minor": 0
      },
      "image/png": "[encoded data, 78080 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, 78080 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_g\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": 16,
   "id": "b46ba6dd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.03573303 0.03621866 0.03768028]\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "05ba772a07ed420789f5ff9182877373",
       "version_major": 2,
       "version_minor": 0
      },
      "image/png": "[encoded data, 87268 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, 87268 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_b\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": 17,
   "id": "5c091777",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.03611575 0.03572091 0.03621733]\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "7ddc1d3ba3194f2b9c6ac1275dea77eb",
       "version_major": 2,
       "version_minor": 0
      },
      "image/png": "[encoded data, 78520 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, 78520 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": 18,
   "id": "6850fb6c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "0429eee7438447e7bcc4f472aa0d9f40",
       "version_major": 2,
       "version_minor": 0
      },
      "image/png": "[encoded data, 77052 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, 77052 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()"
   ]
  }
 ],
 "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
}
