

<!DOCTYPE html>
<html class="writer-html5" lang="en" data-content_root="../../../">
<head>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>noctua.templates.focus2 &mdash; Noctua 0.1 documentation</title>
      <link rel="stylesheet" type="text/css" href="../../../_static/pygments.css?v=b86133f3" />
      <link rel="stylesheet" type="text/css" href="../../../_static/css/theme.css?v=e59714d7" />

  
      <script src="../../../_static/jquery.js?v=5d32c60e"></script>
      <script src="../../../_static/_sphinx_javascript_frameworks_compat.js?v=2cd50e6c"></script>
      <script src="../../../_static/documentation_options.js?v=2709fde1"></script>
      <script src="../../../_static/doctools.js?v=9bcbadda"></script>
      <script src="../../../_static/sphinx_highlight.js?v=dc90522c"></script>
    <script src="../../../_static/js/theme.js"></script>
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" /> 
</head>

<body class="wy-body-for-nav"> 
  <div class="wy-grid-for-nav">
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >

          
          
          <a href="../../../index.html" class="icon icon-home">
            Noctua
          </a>
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" aria-label="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>
        </div><div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu">
              <p class="caption" role="heading"><span class="caption-text">Contents:</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../modules.html">noctua</a></li>
</ul>

        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"><nav class="wy-nav-top" aria-label="Mobile navigation menu" >
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../index.html">Noctua</a>
      </nav>

      <div class="wy-nav-content">
        <div class="rst-content">
          <div role="navigation" aria-label="Page navigation">
  <ul class="wy-breadcrumbs">
      <li><a href="../../../index.html" class="icon icon-home" aria-label="Home"></a></li>
          <li class="breadcrumb-item"><a href="../../index.html">Module code</a></li>
      <li class="breadcrumb-item active">noctua.templates.focus2</li>
      <li class="wy-breadcrumbs-aside">
      </li>
  </ul>
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
             
  <h1>Source code for noctua.templates.focus2</h1><div class="highlight"><pre>
<span></span><span class="ch">#!/usr/bin/env python3</span>
<span class="c1"># -*- coding: utf-8 -*-</span>

<span class="c1"># System modules</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">time</span><span class="w"> </span><span class="kn">import</span> <span class="n">sleep</span>

<span class="c1"># Third-party modules</span>
<span class="c1"># import gnuplotlib as gp # No longer needed</span>
<span class="kn">import</span><span class="w"> </span><span class="nn">numpy</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="nn">np</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">astropy.io</span><span class="w"> </span><span class="kn">import</span> <span class="n">fits</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">astropy.time</span><span class="w"> </span><span class="kn">import</span> <span class="n">Time</span>

<span class="c1"># Other templates</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">..config.constants</span><span class="w"> </span><span class="kn">import</span> <span class="n">pixscale</span><span class="p">,</span> <span class="n">temp_fits</span>  <span class="c1"># Corrected relative import</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">..devices</span><span class="w"> </span><span class="kn">import</span> <span class="n">cam</span><span class="p">,</span> <span class="n">foc</span>  <span class="c1"># Corrected relative import</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">..utils.analysis</span><span class="w"> </span><span class="kn">import</span> <span class="n">fit_star</span>  <span class="c1"># Corrected relative import</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">..utils.logger</span><span class="w"> </span><span class="kn">import</span> <span class="n">log</span>  <span class="c1"># Corrected relative import</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">..utils.structure</span><span class="w"> </span><span class="kn">import</span> <span class="n">foc_path</span>  <span class="c1"># Corrected relative import</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">.basetemplate</span><span class="w"> </span><span class="kn">import</span> <span class="n">BaseTemplate</span>  <span class="c1"># Corrected relative import</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">.box</span><span class="w"> </span><span class="kn">import</span> <span class="n">Template</span> <span class="k">as</span> <span class="n">Box</span>  <span class="c1"># Corrected relative import</span>


<div class="viewcode-block" id="simple_ascii_plot">
<a class="viewcode-back" href="../../../noctua.templates.html#noctua.templates.focus2.simple_ascii_plot">[docs]</a>
<span class="k">def</span><span class="w"> </span><span class="nf">simple_ascii_plot</span><span class="p">(</span>
        <span class="n">x_values</span><span class="p">,</span>
        <span class="n">y_values</span><span class="p">,</span>
        <span class="n">x_label</span><span class="o">=</span><span class="s2">&quot;X&quot;</span><span class="p">,</span>
        <span class="n">y_label</span><span class="o">=</span><span class="s2">&quot;Y&quot;</span><span class="p">,</span>
        <span class="n">title</span><span class="o">=</span><span class="s2">&quot;Plot&quot;</span><span class="p">,</span>
        <span class="n">width</span><span class="o">=</span><span class="mi">50</span><span class="p">,</span>
        <span class="n">height</span><span class="o">=</span><span class="mi">15</span><span class="p">):</span>
<span class="w">    </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Generates a simple ASCII plot for the terminal.</span>
<span class="sd">    Scales y_values to fit within the height.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="nb">len</span><span class="p">(</span><span class="n">x_values</span><span class="p">)</span> <span class="ow">or</span> <span class="ow">not</span> <span class="nb">len</span><span class="p">(</span>
            <span class="n">y_values</span><span class="p">)</span> <span class="ow">or</span> <span class="nb">len</span><span class="p">(</span><span class="n">x_values</span><span class="p">)</span> <span class="o">!=</span> <span class="nb">len</span><span class="p">(</span><span class="n">y_values</span><span class="p">):</span>
        <span class="n">log</span><span class="o">.</span><span class="n">warning</span><span class="p">(</span><span class="s2">&quot;ASCII Plot: Invalid or empty data for plotting.&quot;</span><span class="p">)</span>
        <span class="k">return</span> <span class="s2">&quot;&quot;</span>

    <span class="n">plot_str</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="n">plot_str</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">title</span><span class="si">:</span><span class="s2">^</span><span class="si">{</span><span class="n">width</span><span class="si">}}</span><span class="s2">&quot;</span><span class="p">)</span>
    <span class="n">plot_str</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s2">&quot;&quot;</span><span class="p">)</span>  <span class="c1"># Empty line</span>

    <span class="n">min_y</span><span class="p">,</span> <span class="n">max_y</span> <span class="o">=</span> <span class="nb">min</span><span class="p">(</span><span class="n">y_values</span><span class="p">),</span> <span class="nb">max</span><span class="p">(</span><span class="n">y_values</span><span class="p">)</span>
    <span class="n">y_range</span> <span class="o">=</span> <span class="n">max_y</span> <span class="o">-</span> <span class="n">min_y</span>
    <span class="k">if</span> <span class="n">y_range</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>  <span class="c1"># Avoid division by zero if all y values are the same</span>
        <span class="n">y_range</span> <span class="o">=</span> <span class="mf">1.0</span>

    <span class="c1"># Create the plot grid (transposed for easier filling)</span>
    <span class="n">grid</span> <span class="o">=</span> <span class="p">[[</span><span class="s1">&#39; &#39;</span> <span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">width</span><span class="p">)]</span> <span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">height</span><span class="p">)]</span>

    <span class="c1"># Y-axis labels and scale</span>
    <span class="n">y_axis_label_width</span> <span class="o">=</span> <span class="mi">8</span>  <span class="c1"># max width for y-axis numbers</span>
    <span class="n">plot_width_effective</span> <span class="o">=</span> <span class="n">width</span> <span class="o">-</span> <span class="n">y_axis_label_width</span> <span class="o">-</span> <span class="mi">1</span>  <span class="c1"># -1 for the axis itself</span>

    <span class="c1"># Determine x positions (scaled to fit plot_width_effective)</span>
    <span class="c1"># This simple version assumes x_values are somewhat evenly spaced or just for labeling</span>
    <span class="c1"># For true x-axis scaling, it&#39;s more complex for ASCII.</span>
    <span class="c1"># We will place points at somewhat regular intervals across the width.</span>

    <span class="n">num_points</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">x_values</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">num_points</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
        <span class="k">return</span> <span class="s2">&quot;&quot;</span>

    <span class="c1"># Map y_values to plot height</span>
    <span class="n">scaled_y</span> <span class="o">=</span> <span class="p">[</span><span class="nb">int</span><span class="p">(((</span><span class="n">y</span> <span class="o">-</span> <span class="n">min_y</span><span class="p">)</span> <span class="o">/</span> <span class="n">y_range</span><span class="p">)</span> <span class="o">*</span> <span class="p">(</span><span class="n">height</span> <span class="o">-</span> <span class="mi">1</span><span class="p">))</span> <span class="k">for</span> <span class="n">y</span> <span class="ow">in</span> <span class="n">y_values</span><span class="p">]</span>

    <span class="c1"># Create a simple bar-like representation for each point</span>
    <span class="c1"># This is a very basic representation.</span>
    <span class="c1"># We will map each x_value to a column if possible, or group them.</span>

    <span class="c1"># For this simple plot, we&#39;ll iterate through y_values and represent them as bars</span>
    <span class="c1"># at distinct x positions if possible.</span>

    <span class="c1"># Let&#39;s make a bar chart where each x_value has its own &quot;bar&quot;</span>
    <span class="c1"># The width of each bar will be plot_width_effective / num_points</span>

    <span class="n">bar_segment_width</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">plot_width_effective</span> <span class="o">//</span> <span class="n">num_points</span><span class="p">)</span>

    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">height</span><span class="p">):</span>
        <span class="n">row_str</span> <span class="o">=</span> <span class="s2">&quot;&quot;</span>
        <span class="c1"># Y-axis label</span>
        <span class="n">y_val_at_row</span> <span class="o">=</span> <span class="n">max_y</span> <span class="o">-</span> \
            <span class="p">(</span><span class="n">i</span> <span class="o">*</span> <span class="n">y_range</span> <span class="o">/</span> <span class="p">(</span><span class="n">height</span> <span class="o">-</span> <span class="mi">1</span> <span class="k">if</span> <span class="n">height</span> <span class="o">&gt;</span> <span class="mi">1</span> <span class="k">else</span> <span class="mi">1</span><span class="p">))</span>
        <span class="n">row_str</span> <span class="o">+=</span> <span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">y_val_at_row</span><span class="si">:</span><span class="s2">&lt;</span><span class="si">{</span><span class="n">y_axis_label_width</span><span class="si">}</span><span class="s2">.2f</span><span class="si">}</span><span class="s2">|&quot;</span>

        <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">num_points</span><span class="p">):</span>
            <span class="c1"># Check if the current row corresponds to the bar height for this point</span>
            <span class="c1"># scaled_y is 0 at min_y, height-1 at max_y.</span>
            <span class="c1"># Grid is top-down, so we need to invert.</span>
            <span class="k">if</span> <span class="p">(</span><span class="n">height</span> <span class="o">-</span> <span class="mi">1</span> <span class="o">-</span> <span class="n">scaled_y</span><span class="p">[</span><span class="n">j</span><span class="p">])</span> <span class="o">&lt;=</span> <span class="n">i</span><span class="p">:</span>
                <span class="n">row_str</span> <span class="o">+=</span> <span class="s2">&quot;*&quot;</span> <span class="o">*</span> <span class="n">bar_segment_width</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">row_str</span> <span class="o">+=</span> <span class="s2">&quot; &quot;</span> <span class="o">*</span> <span class="n">bar_segment_width</span>
        <span class="n">plot_str</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">row_str</span><span class="o">.</span><span class="n">rstrip</span><span class="p">())</span>

    <span class="c1"># X-axis (very simplified)</span>
    <span class="n">plot_str</span><span class="o">.</span><span class="n">append</span><span class="p">(</span>
        <span class="s2">&quot;-&quot;</span> <span class="o">*</span>
        <span class="n">y_axis_label_width</span> <span class="o">+</span>
        <span class="s2">&quot;+&quot;</span> <span class="o">+</span>
        <span class="s2">&quot;-&quot;</span> <span class="o">*</span>
        <span class="p">(</span><span class="n">plot_width_effective</span><span class="p">))</span>

    <span class="c1"># X-axis labels (approximate positions)</span>
    <span class="c1"># This is tricky to align perfectly in ASCII.</span>
    <span class="c1"># We&#39;ll just show min and max x for simplicity or label a few points.</span>
    <span class="k">if</span> <span class="n">num_points</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
        <span class="n">x_labels_line</span> <span class="o">=</span> <span class="s2">&quot; &quot;</span> <span class="o">*</span> <span class="p">(</span><span class="n">y_axis_label_width</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">num_points</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
            <span class="n">x_labels_line</span> <span class="o">+=</span> <span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">x_values</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="si">:</span><span class="s2">^</span><span class="si">{</span><span class="n">plot_width_effective</span><span class="si">}</span><span class="s2">.1f</span><span class="si">}</span><span class="s2">&quot;</span>
        <span class="k">elif</span> <span class="n">num_points</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">:</span>
            <span class="n">first_x_label</span> <span class="o">=</span> <span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">x_values</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="si">:</span><span class="s2">.1f</span><span class="si">}</span><span class="s2">&quot;</span>
            <span class="n">last_x_label</span> <span class="o">=</span> <span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">x_values</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><span class="si">:</span><span class="s2">.1f</span><span class="si">}</span><span class="s2">&quot;</span>
            <span class="n">middle_space</span> <span class="o">=</span> <span class="n">plot_width_effective</span> <span class="o">-</span> \
                <span class="nb">len</span><span class="p">(</span><span class="n">first_x_label</span><span class="p">)</span> <span class="o">-</span> <span class="nb">len</span><span class="p">(</span><span class="n">last_x_label</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">middle_space</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">middle_space</span> <span class="o">=</span> <span class="mi">0</span>
            <span class="n">x_labels_line</span> <span class="o">+=</span> <span class="n">first_x_label</span> <span class="o">+</span> <span class="s2">&quot; &quot;</span> <span class="o">*</span> <span class="n">middle_space</span> <span class="o">+</span> <span class="n">last_x_label</span>
        <span class="n">plot_str</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">x_labels_line</span><span class="p">)</span>
        <span class="n">plot_str</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">x_label</span><span class="si">:</span><span class="s2">^</span><span class="si">{</span><span class="n">width</span><span class="si">}}</span><span class="s2">&quot;</span><span class="p">)</span>

    <span class="n">plot_str</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s2">&quot;&quot;</span><span class="p">)</span>
    <span class="n">plot_str</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">y_label</span><span class="si">}</span><span class="s2"> vs </span><span class="si">{</span><span class="n">x_label</span><span class="si">}</span><span class="s2">&quot;</span><span class="p">)</span>
    <span class="k">return</span> <span class="s2">&quot;</span><span class="se">\n</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">plot_str</span><span class="p">)</span></div>



<div class="viewcode-block" id="Template">
<a class="viewcode-back" href="../../../noctua.templates.html#noctua.templates.focus2.Template">[docs]</a>
<span class="k">class</span><span class="w"> </span><span class="nc">Template</span><span class="p">(</span><span class="n">BaseTemplate</span><span class="p">):</span>
    <span class="k">def</span><span class="w"> </span><span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="s2">&quot;focus&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">description</span> <span class="o">=</span> <span class="s2">&quot;Fit boxed images to get optimal focus&quot;</span>

<div class="viewcode-block" id="Template.content">
<a class="viewcode-back" href="../../../noctua.templates.html#noctua.templates.focus2.Template.content">[docs]</a>
    <span class="k">def</span><span class="w"> </span><span class="nf">content</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">params</span><span class="p">):</span>

        <span class="c1">########################</span>
        <span class="c1">##### Params check #####</span>
        <span class="c1">########################</span>

        <span class="k">try</span><span class="p">:</span>
            <span class="n">objname</span> <span class="o">=</span> <span class="n">params</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;object&quot;</span><span class="p">)</span> <span class="ow">or</span> <span class="s2">&quot;test&quot;</span>
            <span class="n">filt</span> <span class="o">=</span> <span class="n">params</span><span class="p">[</span><span class="s2">&quot;filter&quot;</span><span class="p">]</span>
            <span class="n">binning</span> <span class="o">=</span> <span class="n">params</span><span class="p">[</span><span class="s2">&quot;binning&quot;</span><span class="p">]</span>
            <span class="n">exptime</span> <span class="o">=</span> <span class="n">params</span><span class="p">[</span><span class="s2">&quot;exptime&quot;</span><span class="p">]</span>
            <span class="n">center</span> <span class="o">=</span> <span class="n">params</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;center&quot;</span><span class="p">)</span> <span class="ow">or</span> <span class="n">cam</span><span class="o">.</span><span class="n">center</span>
            <span class="n">width</span> <span class="o">=</span> <span class="n">params</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;width&quot;</span><span class="p">)</span> <span class="ow">or</span> <span class="mi">400</span> <span class="o">//</span> <span class="n">binning</span>
            <span class="n">height</span> <span class="o">=</span> <span class="n">params</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;height&quot;</span><span class="p">)</span> <span class="ow">or</span> <span class="mi">400</span> <span class="o">//</span> <span class="n">binning</span>
            <span class="n">repeat</span> <span class="o">=</span> <span class="n">params</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;repeat&quot;</span><span class="p">)</span> <span class="ow">or</span> <span class="mi">10</span>
            <span class="n">step</span> <span class="o">=</span> <span class="n">params</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;step&quot;</span><span class="p">)</span> <span class="ow">or</span> <span class="mi">10</span>

        <span class="k">except</span> <span class="ne">KeyError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
            <span class="n">msg</span> <span class="o">=</span> <span class="sa">f</span><span class="s2">&quot;Parameter </span><span class="si">{</span><span class="n">e</span><span class="si">}</span><span class="s2"> not found&quot;</span>
            <span class="n">log</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">error</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span>
            <span class="k">return</span>

        <span class="c1"># for Box</span>
        <span class="n">fixed</span> <span class="o">=</span> <span class="p">{</span>
            <span class="s2">&quot;objname&quot;</span><span class="p">:</span> <span class="n">objname</span><span class="p">,</span>
            <span class="s2">&quot;repeat&quot;</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span>
            <span class="s2">&quot;center&quot;</span><span class="p">:</span> <span class="n">center</span><span class="p">,</span>
            <span class="s2">&quot;width&quot;</span><span class="p">:</span> <span class="n">width</span><span class="p">,</span>
            <span class="s2">&quot;height&quot;</span><span class="p">:</span> <span class="n">height</span><span class="p">,</span>
        <span class="p">}</span>
        <span class="n">params</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">fixed</span><span class="p">)</span>

        <span class="c1">#######################################</span>
        <span class="c1">##### Preparing to the focus loop #####</span>
        <span class="c1">#######################################</span>

        <span class="c1"># Getting initial focus</span>
        <span class="n">original_foc</span> <span class="o">=</span> <span class="n">foc</span><span class="o">.</span><span class="n">position</span>
        <span class="k">if</span> <span class="n">original_foc</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">log</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="s2">&quot;Could not get initial focus position. Aborting focus.&quot;</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">error</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s2">&quot;Initial focus position is None.&quot;</span><span class="p">)</span>
            <span class="k">return</span>

        <span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;Initial focus position: </span><span class="si">{</span><span class="n">original_foc</span><span class="si">}</span><span class="s2"> µm&quot;</span><span class="p">)</span>

        <span class="c1"># starting from half before</span>
        <span class="c1"># Corrected: (repeat-1) steps for &#39;repeat&#39; points</span>
        <span class="n">total_length_focus_scan</span> <span class="o">=</span> <span class="n">step</span> <span class="o">*</span> <span class="p">(</span><span class="n">repeat</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span>
        <span class="n">start_focus</span> <span class="o">=</span> <span class="n">original_foc</span> <span class="o">-</span> <span class="n">total_length_focus_scan</span> <span class="o">/</span> <span class="mi">2</span>

        <span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span>
            <span class="sa">f</span><span class="s2">&quot;Starting focus scan from </span><span class="si">{</span>
<span class="w">                </span><span class="n">start_focus</span><span class="si">:</span><span class="s2">.0f</span><span class="si">}</span><span class="s2"> µm to </span><span class="si">{</span>
<span class="w">                </span><span class="n">start_focus</span><span class="w"> </span><span class="o">+</span>
<span class="w">                </span><span class="n">total_length_focus_scan</span><span class="si">:</span><span class="s2">.0f</span><span class="si">}</span><span class="s2"> µm in </span><span class="si">{</span><span class="n">repeat</span><span class="si">}</span><span class="s2"> steps of </span><span class="si">{</span><span class="n">step</span><span class="si">}</span><span class="s2"> µm.&quot;</span><span class="p">)</span>

        <span class="c1"># Move to the start of the focus scan</span>
        <span class="n">foc</span><span class="o">.</span><span class="n">position</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="n">start_focus</span><span class="p">))</span>
        <span class="c1"># Add a small delay for the focuser to settle if it&#39;s a physical device</span>
        <span class="n">sleep</span><span class="p">(</span><span class="mf">1.0</span><span class="p">)</span>  <span class="c1"># Adjust as needed, or check foc.is_moving if available</span>

        <span class="c1"># Preparing empty arrays for values</span>
        <span class="n">m2_arr</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([],</span> <span class="n">dtype</span><span class="o">=</span><span class="nb">float</span><span class="p">)</span>  <span class="c1"># Use float for focus positions</span>
        <span class="n">fwhm_arr</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([],</span> <span class="n">dtype</span><span class="o">=</span><span class="nb">float</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">output</span> <span class="o">=</span> <span class="p">[]</span>

        <span class="c1"># Preparing the focus file path</span>
        <span class="n">now_time</span> <span class="o">=</span> <span class="n">Time</span><span class="o">.</span><span class="n">now</span><span class="p">()</span><span class="o">.</span><span class="n">isot</span>
        <span class="c1"># Ensure foc_path is from ..utils.structure</span>
        <span class="n">file_path</span> <span class="o">=</span> <span class="n">foc_path</span><span class="p">(</span><span class="n">now_time</span><span class="p">)</span>

        <span class="c1"># Initial comment about variables</span>
        <span class="n">comment1</span> <span class="o">=</span> <span class="sa">f</span><span class="s2">&quot;Focus procedure of </span><span class="si">{</span><span class="n">now_time</span><span class="si">}</span><span class="s2"> for object &#39;</span><span class="si">{</span><span class="n">objname</span><span class="si">}</span><span class="s2">&#39; with filter &#39;</span><span class="si">{</span><span class="n">filt</span><span class="si">}</span><span class="s2">&#39;&quot;</span>
        <span class="n">comment2</span> <span class="o">=</span> <span class="sa">f</span><span class="s2">&quot;Step Focus[µm] FWHM[arcsec] Peak[ADU] BG[ADU] X[px] Y[px]&quot;</span>

        <span class="c1"># Write the initial comment if the file doesn&#39;t exist</span>
        <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">file_path</span><span class="p">,</span> <span class="s2">&quot;a+&quot;</span><span class="p">)</span> <span class="k">as</span> <span class="n">file</span><span class="p">:</span>
            <span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;Using focus data file: </span><span class="si">{</span><span class="n">file_path</span><span class="si">}</span><span class="s2">&quot;</span><span class="p">)</span>
            <span class="n">file</span><span class="o">.</span><span class="n">seek</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">file</span><span class="o">.</span><span class="n">read</span><span class="p">(</span><span class="mi">1</span><span class="p">):</span>
                <span class="n">file</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;# </span><span class="si">{</span><span class="n">comment1</span><span class="si">}</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span>
                <span class="n">file</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;# </span><span class="si">{</span><span class="n">comment2</span><span class="si">}</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span>

        <span class="c1"># Instanciating a Box template</span>
        <span class="n">box_template</span> <span class="o">=</span> <span class="n">Box</span><span class="p">()</span>

        <span class="k">for</span> <span class="n">rep_idx</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">repeat</span><span class="p">):</span>
            <span class="n">current_focus_target</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="n">start_focus</span> <span class="o">+</span> <span class="n">rep_idx</span> <span class="o">*</span> <span class="n">step</span><span class="p">))</span>
            <span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span>
                <span class="sa">f</span><span class="s2">&quot;Focus step </span><span class="si">{</span>
<span class="w">                    </span><span class="n">rep_idx</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="si">}</span><span class="s2">/</span><span class="si">{</span><span class="n">repeat</span><span class="si">}</span><span class="s2"> :: Target M2 Focus: </span><span class="si">{</span><span class="n">current_focus_target</span><span class="si">}</span><span class="s2"> µm&quot;</span><span class="p">)</span>

            <span class="c1"># Set focus position for this step</span>
            <span class="n">foc</span><span class="o">.</span><span class="n">position</span> <span class="o">=</span> <span class="n">current_focus_target</span>
            <span class="c1"># Check if focuser is moving (if available) or sleep</span>
            <span class="c1"># This is important to ensure the image is taken at the correct</span>
            <span class="c1"># focus</span>
            <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">foc</span><span class="p">,</span> <span class="s1">&#39;is_moving&#39;</span><span class="p">):</span>
                <span class="k">while</span> <span class="n">foc</span><span class="o">.</span><span class="n">is_moving</span><span class="p">:</span>
                    <span class="n">log</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;Waiting for focuser to settle...&quot;</span><span class="p">)</span>
                    <span class="n">sleep</span><span class="p">(</span><span class="mf">0.2</span><span class="p">)</span>  <span class="c1"># Short sleep while checking</span>
                    <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">check_pause_or_abort</span><span class="p">():</span>
                        <span class="k">return</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">sleep</span><span class="p">(</span><span class="mf">1.0</span><span class="p">)</span>  <span class="c1"># Generic delay if is_moving is not available</span>

            <span class="c1"># Get actual focus position after movement</span>
            <span class="n">actual_m2_pos</span> <span class="o">=</span> <span class="n">foc</span><span class="o">.</span><span class="n">position</span>
            <span class="k">if</span> <span class="n">actual_m2_pos</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">log</span><span class="o">.</span><span class="n">warning</span><span class="p">(</span>
                    <span class="sa">f</span><span class="s2">&quot;Could not read M2 position at step </span><span class="si">{</span>
<span class="w">                        </span><span class="n">rep_idx</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="si">}</span><span class="s2">. Skipping this point.&quot;</span><span class="p">)</span>
                <span class="k">continue</span>

            <span class="n">m2_arr</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">m2_arr</span><span class="p">,</span> <span class="n">actual_m2_pos</span><span class="p">)</span>

            <span class="c1">################################</span>
            <span class="c1">##### Taking a boxed image #####</span>
            <span class="c1">################################</span>
            <span class="c1"># Update exptime and filter in params for the box template, if they</span>
            <span class="c1"># can change</span>
            <span class="n">params</span><span class="p">[</span><span class="s2">&quot;exptime&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">exptime</span>
            <span class="n">params</span><span class="p">[</span><span class="s2">&quot;filter&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">filt</span>
            <span class="n">params</span><span class="p">[</span><span class="s2">&quot;binning&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">binning</span>

            <span class="n">box_template</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">params</span><span class="p">)</span>  <span class="c1"># saves temp.fits boxed image</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">check_pause_or_abort</span><span class="p">():</span>
                <span class="k">return</span>

            <span class="k">try</span><span class="p">:</span>
                <span class="n">data</span> <span class="o">=</span> <span class="n">fits</span><span class="o">.</span><span class="n">getdata</span><span class="p">(</span><span class="n">temp_fits</span><span class="p">)</span>
            <span class="k">except</span> <span class="ne">FileNotFoundError</span><span class="p">:</span>
                <span class="n">log</span><span class="o">.</span><span class="n">error</span><span class="p">(</span>
                    <span class="sa">f</span><span class="s2">&quot;Focus: </span><span class="si">{</span><span class="n">temp_fits</span><span class="si">}</span><span class="s2"> not found after box exposure. Skipping point.&quot;</span><span class="p">)</span>
                <span class="c1"># Add NaN for missing FWHM</span>
                <span class="n">fwhm_arr</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">fwhm_arr</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span><span class="p">)</span>
                <span class="k">continue</span>

            <span class="c1">###############################</span>
            <span class="c1">##### Fitting a 2d Moffat #####</span>
            <span class="c1">###############################</span>
            <span class="n">current_fwhm_arcsec</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span>  <span class="c1"># Default to NaN</span>
            <span class="n">peak_adu</span><span class="p">,</span> <span class="n">bg_adu</span><span class="p">,</span> <span class="n">fit_x</span><span class="p">,</span> <span class="n">fit_y</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span>

            <span class="k">try</span><span class="p">:</span>
                <span class="c1"># Estimate background from corners if possible, or use a fixed guess</span>
                <span class="c1"># For simplicity, using a fixed guess for now</span>
                <span class="n">background_estimation</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">median</span><span class="p">(</span>
                    <span class="n">data</span><span class="p">)</span>  <span class="c1"># A slightly better guess</span>
                <span class="n">fitted</span> <span class="o">=</span> <span class="n">fit_star</span><span class="p">(</span>
                    <span class="n">data</span><span class="p">,</span>
                    <span class="n">model</span><span class="o">=</span><span class="s2">&quot;moffat&quot;</span><span class="p">,</span>
                    <span class="n">background_estimation</span><span class="o">=</span><span class="n">background_estimation</span><span class="p">)</span>
                <span class="k">if</span> <span class="n">fitted</span> <span class="ow">and</span> <span class="nb">hasattr</span><span class="p">(</span>
                        <span class="n">fitted</span><span class="p">,</span>
                        <span class="s1">&#39;fwhm&#39;</span><span class="p">)</span> <span class="ow">and</span> <span class="nb">hasattr</span><span class="p">(</span>
                        <span class="n">fitted</span><span class="p">,</span>
                        <span class="s1">&#39;xc&#39;</span><span class="p">):</span>
                    <span class="n">current_fwhm_arcsec</span> <span class="o">=</span> <span class="n">fitted</span><span class="o">.</span><span class="n">fwhm</span> <span class="o">*</span> <span class="n">pixscale</span> <span class="o">*</span> <span class="n">binning</span>  <span class="c1"># FWHM in arcsec</span>
                    <span class="n">peak_adu</span> <span class="o">=</span> <span class="n">fitted</span><span class="o">.</span><span class="n">peak</span>
                    <span class="n">bg_adu</span> <span class="o">=</span> <span class="n">fitted</span><span class="o">.</span><span class="n">background</span>
                    <span class="n">fit_x</span> <span class="o">=</span> <span class="n">fitted</span><span class="o">.</span><span class="n">xc</span>
                    <span class="n">fit_y</span> <span class="o">=</span> <span class="n">fitted</span><span class="o">.</span><span class="n">yc</span>
                    <span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span>
                        <span class="sa">f</span><span class="s2">&quot;  -&gt; Fit: FWHM=</span><span class="si">{</span>
<span class="w">                            </span><span class="n">current_fwhm_arcsec</span><span class="si">:</span><span class="s2">.2f</span><span class="si">}</span><span class="se">\&quot;</span><span class="s2">, Peak=</span><span class="si">{</span>
<span class="w">                            </span><span class="n">peak_adu</span><span class="si">:</span><span class="s2">.0f</span><span class="si">}</span><span class="s2">, BG=</span><span class="si">{</span>
<span class="w">                            </span><span class="n">bg_adu</span><span class="si">:</span><span class="s2">.0f</span><span class="si">}</span><span class="s2">, X=</span><span class="si">{</span>
<span class="w">                            </span><span class="n">fit_x</span><span class="si">:</span><span class="s2">.1f</span><span class="si">}</span><span class="s2">, Y=</span><span class="si">{</span>
<span class="w">                            </span><span class="n">fit_y</span><span class="si">:</span><span class="s2">.1f</span><span class="si">}</span><span class="s2">&quot;</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">log</span><span class="o">.</span><span class="n">warning</span><span class="p">(</span>
                        <span class="s2">&quot;  -&gt; Fit failed or did not return expected attributes.&quot;</span><span class="p">)</span>
            <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
                <span class="n">log</span><span class="o">.</span><span class="n">warning</span><span class="p">(</span>
                    <span class="sa">f</span><span class="s2">&quot;  -&gt; Star fitting error at focus </span><span class="si">{</span><span class="n">actual_m2_pos</span><span class="si">}</span><span class="s2"> µm: </span><span class="si">{</span><span class="n">e</span><span class="si">}</span><span class="s2">&quot;</span><span class="p">)</span>

            <span class="n">fwhm_arr</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">fwhm_arr</span><span class="p">,</span> <span class="n">current_fwhm_arcsec</span><span class="p">)</span>

            <span class="bp">self</span><span class="o">.</span><span class="n">output</span> <span class="o">=</span> <span class="p">{</span>  <span class="c1"># Update self.output for potential real-time display/API</span>
                <span class="s2">&quot;focus_run&quot;</span><span class="p">:</span> <span class="p">{</span>
                    <span class="s2">&quot;m2_positions&quot;</span><span class="p">:</span> <span class="n">m2_arr</span><span class="o">.</span><span class="n">tolist</span><span class="p">(),</span>
                    <span class="c1"># Replace NaN for JSON</span>
                    <span class="s2">&quot;fwhm_arcsec&quot;</span><span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">nan_to_num</span><span class="p">(</span><span class="n">fwhm_arr</span><span class="p">,</span> <span class="n">nan</span><span class="o">=-</span><span class="mf">1.0</span><span class="p">)</span><span class="o">.</span><span class="n">tolist</span><span class="p">(),</span>
                    <span class="s2">&quot;current_point&quot;</span><span class="p">:</span> <span class="p">{</span>
                        <span class="s2">&quot;m2&quot;</span><span class="p">:</span> <span class="n">actual_m2_pos</span><span class="p">,</span>
                        <span class="s2">&quot;fwhm&quot;</span><span class="p">:</span> <span class="n">current_fwhm_arcsec</span> <span class="k">if</span> <span class="ow">not</span> <span class="n">np</span><span class="o">.</span><span class="n">isnan</span><span class="p">(</span><span class="n">current_fwhm_arcsec</span><span class="p">)</span> <span class="k">else</span> <span class="o">-</span><span class="mf">1.0</span><span class="p">,</span>
                        <span class="s2">&quot;peak&quot;</span><span class="p">:</span> <span class="n">peak_adu</span> <span class="k">if</span> <span class="ow">not</span> <span class="n">np</span><span class="o">.</span><span class="n">isnan</span><span class="p">(</span><span class="n">peak_adu</span><span class="p">)</span> <span class="k">else</span> <span class="o">-</span><span class="mf">1.0</span><span class="p">,</span>
                        <span class="s2">&quot;background&quot;</span><span class="p">:</span> <span class="n">bg_adu</span> <span class="k">if</span> <span class="ow">not</span> <span class="n">np</span><span class="o">.</span><span class="n">isnan</span><span class="p">(</span><span class="n">bg_adu</span><span class="p">)</span> <span class="k">else</span> <span class="o">-</span><span class="mf">1.0</span><span class="p">,</span>
                        <span class="s2">&quot;fit_x_px&quot;</span><span class="p">:</span> <span class="n">fit_x</span> <span class="k">if</span> <span class="ow">not</span> <span class="n">np</span><span class="o">.</span><span class="n">isnan</span><span class="p">(</span><span class="n">fit_x</span><span class="p">)</span> <span class="k">else</span> <span class="o">-</span><span class="mf">1.0</span><span class="p">,</span>
                        <span class="s2">&quot;fit_y_px&quot;</span><span class="p">:</span> <span class="n">fit_y</span> <span class="k">if</span> <span class="ow">not</span> <span class="n">np</span><span class="o">.</span><span class="n">isnan</span><span class="p">(</span><span class="n">fit_y</span><span class="p">)</span> <span class="k">else</span> <span class="o">-</span><span class="mf">1.0</span><span class="p">,</span>
                    <span class="p">}</span>
                <span class="p">}</span>
            <span class="p">}</span>

            <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">file_path</span><span class="p">,</span> <span class="s2">&quot;a&quot;</span><span class="p">)</span> <span class="k">as</span> <span class="n">file</span><span class="p">:</span>
                <span class="n">line_data</span> <span class="o">=</span> <span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span>
<span class="w">                    </span><span class="n">rep_idx</span><span class="w"> </span><span class="o">+</span>
<span class="w">                    </span><span class="mi">1</span><span class="si">:</span><span class="s2">&lt;4d</span><span class="si">}</span><span class="s2"> </span><span class="si">{</span>
<span class="w">                    </span><span class="n">actual_m2_pos</span><span class="si">:</span><span class="s2">&lt;10.1f</span><span class="si">}</span><span class="s2"> </span><span class="si">{</span>
<span class="w">                    </span><span class="n">current_fwhm_arcsec</span><span class="si">:</span><span class="s2">&lt;10.2f</span><span class="si">}</span><span class="s2"> </span><span class="si">{</span>
<span class="w">                    </span><span class="n">peak_adu</span><span class="si">:</span><span class="s2">&lt;9.0f</span><span class="si">}</span><span class="s2"> </span><span class="si">{</span>
<span class="w">                    </span><span class="n">bg_adu</span><span class="si">:</span><span class="s2">&lt;7.0f</span><span class="si">}</span><span class="s2"> </span><span class="si">{</span>
<span class="w">                        </span><span class="n">fit_x</span><span class="si">:</span><span class="s2">&lt;7.1f</span><span class="si">}</span><span class="s2"> </span><span class="si">{</span>
<span class="w">                            </span><span class="n">fit_y</span><span class="si">:</span><span class="s2">&lt;7.1f</span><span class="si">}</span><span class="s2">&quot;</span>
                <span class="n">file</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">line_data</span> <span class="o">+</span> <span class="s2">&quot;</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span>

            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">check_pause_or_abort</span><span class="p">():</span>
                <span class="k">return</span>

        <span class="c1">##############################</span>
        <span class="c1">##### Showing the result #####</span>
        <span class="c1">##############################</span>

        <span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s2">&quot;Focus scan complete. Results:&quot;</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">m2_arr</span><span class="p">)):</span>
            <span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="sa">f</span><span class="s2">&quot;  Focus: </span><span class="si">{</span><span class="n">m2_arr</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="si">:</span><span class="s2">.1f</span><span class="si">}</span><span class="s2"> µm, FWHM: </span><span class="si">{</span><span class="n">fwhm_arr</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="si">:</span><span class="s2">.2f</span><span class="si">}</span><span class="se">\&quot;</span><span class="s2">&quot;</span><span class="p">)</span>

        <span class="c1"># Generate and print ASCII plot</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">m2_arr</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">1</span> <span class="ow">and</span> <span class="nb">len</span><span class="p">(</span><span class="n">fwhm_arr</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">:</span>
            <span class="c1"># Filter out NaN values for plotting and finding minimum</span>
            <span class="n">valid_indices</span> <span class="o">=</span> <span class="o">~</span><span class="n">np</span><span class="o">.</span><span class="n">isnan</span><span class="p">(</span><span class="n">fwhm_arr</span><span class="p">)</span>
            <span class="n">plot_m2</span> <span class="o">=</span> <span class="n">m2_arr</span><span class="p">[</span><span class="n">valid_indices</span><span class="p">]</span>
            <span class="n">plot_fwhm</span> <span class="o">=</span> <span class="n">fwhm_arr</span><span class="p">[</span><span class="n">valid_indices</span><span class="p">]</span>

            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">plot_m2</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">ascii_chart</span> <span class="o">=</span> <span class="n">simple_ascii_plot</span><span class="p">(</span>
                    <span class="n">plot_m2</span><span class="p">,</span> <span class="n">plot_fwhm</span><span class="p">,</span>
                    <span class="n">x_label</span><span class="o">=</span><span class="s2">&quot;Focus M2 (µm)&quot;</span><span class="p">,</span> <span class="n">y_label</span><span class="o">=</span><span class="s2">&quot;FWHM (arcsec)&quot;</span><span class="p">,</span>
                    <span class="n">title</span><span class="o">=</span><span class="sa">f</span><span class="s2">&quot;Focus Curve - </span><span class="si">{</span><span class="n">objname</span><span class="si">}</span><span class="s2"> (</span><span class="si">{</span><span class="n">filt</span><span class="si">}</span><span class="s2">)&quot;</span><span class="p">,</span>
                    <span class="n">width</span><span class="o">=</span><span class="mi">80</span><span class="p">,</span> <span class="n">height</span><span class="o">=</span><span class="mi">20</span>
                <span class="p">)</span>
                <span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s2">&quot;</span><span class="se">\n</span><span class="s2">&quot;</span> <span class="o">+</span> <span class="n">ascii_chart</span><span class="p">)</span>

                <span class="c1"># Find best focus</span>
                <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">plot_fwhm</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
                    <span class="n">min_fwhm_idx</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">argmin</span><span class="p">(</span><span class="n">plot_fwhm</span><span class="p">)</span>
                    <span class="n">best_focus_val</span> <span class="o">=</span> <span class="n">plot_m2</span><span class="p">[</span><span class="n">min_fwhm_idx</span><span class="p">]</span>
                    <span class="n">best_fwhm_val</span> <span class="o">=</span> <span class="n">plot_fwhm</span><span class="p">[</span><span class="n">min_fwhm_idx</span><span class="p">]</span>
                    <span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span>
                        <span class="sa">f</span><span class="s2">&quot;Optimal focus found at </span><span class="si">{</span>
<span class="w">                            </span><span class="n">best_focus_val</span><span class="si">:</span><span class="s2">.1f</span><span class="si">}</span><span class="s2"> µm with FWHM </span><span class="si">{</span>
<span class="w">                            </span><span class="n">best_fwhm_val</span><span class="si">:</span><span class="s2">.2f</span><span class="si">}</span><span class="se">\&quot;</span><span class="s2">.&quot;</span><span class="p">)</span>
                    <span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span>
                        <span class="sa">f</span><span class="s2">&quot;Moving M2 to optimal focus: </span><span class="si">{</span>
<span class="w">                            </span><span class="n">best_focus_val</span><span class="si">:</span><span class="s2">.0f</span><span class="si">}</span><span class="s2"> µm.&quot;</span><span class="p">)</span>
                    <span class="n">foc</span><span class="o">.</span><span class="n">position</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="nb">round</span><span class="p">(</span><span class="n">best_focus_val</span><span class="p">))</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">log</span><span class="o">.</span><span class="n">warning</span><span class="p">(</span>
                        <span class="s2">&quot;No valid FWHM data points to determine optimal focus. Restoring original.&quot;</span><span class="p">)</span>
                    <span class="n">foc</span><span class="o">.</span><span class="n">position</span> <span class="o">=</span> <span class="n">original_foc</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">log</span><span class="o">.</span><span class="n">warning</span><span class="p">(</span>
                    <span class="s2">&quot;No valid data points to plot or determine optimal focus. Restoring original.&quot;</span><span class="p">)</span>
                <span class="n">foc</span><span class="o">.</span><span class="n">position</span> <span class="o">=</span> <span class="n">original_foc</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">log</span><span class="o">.</span><span class="n">warning</span><span class="p">(</span>
                <span class="s2">&quot;Not enough data points for a plot or to determine optimal focus. Restoring original focus.&quot;</span><span class="p">)</span>
            <span class="n">foc</span><span class="o">.</span><span class="n">position</span> <span class="o">=</span> <span class="n">original_foc</span>

        <span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span>
            <span class="sa">f</span><span class="s2">&quot;Focus procedure finished. M2 position set to: </span><span class="si">{</span>
<span class="w">                </span><span class="n">foc</span><span class="o">.</span><span class="n">position</span><span class="si">}</span><span class="s2"> µm.&quot;</span><span class="p">)</span></div>
</div>

</pre></div>

           </div>
          </div>
          <footer>

  <hr/>

  <div role="contentinfo">
    <p>&#169; Copyright 2025, Davide Ricci.</p>
  </div>

  Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
    <a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
    provided by <a href="https://readthedocs.org">Read the Docs</a>.
   

</footer>
        </div>
      </div>
    </section>
  </div>
  <script>
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script> 

</body>
</html>