

<!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.focus &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.focus</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.focus</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="kn">import</span><span class="w"> </span><span class="nn">gnuplotlib</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="nn">gp</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="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="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="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="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="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="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>


<div class="viewcode-block" id="Template">
<a class="viewcode-back" href="../../../noctua.templates.html#noctua.templates.focus.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.focus.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="c1"># starting from half before</span>
        <span class="n">total_lenght</span> <span class="o">=</span> <span class="n">step</span> <span class="o">*</span> <span class="n">repeat</span>
        <span class="n">foc</span><span class="o">.</span><span class="n">position</span> <span class="o">-=</span> <span class="n">total_lenght</span> <span class="o">/</span> <span class="mi">2</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">int</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">array</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</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="n">file_path</span> <span class="o">=</span> <span class="n">foc_path</span><span class="p">(</span><span class="n">now</span><span class="p">)</span>

        <span class="c1"># Initial comment about variables a, b, c</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</span><span class="si">}</span><span class="s2">&quot;</span>
        <span class="n">comment2</span> <span class="o">=</span> <span class="sa">f</span><span class="s2">&quot;rep m2[µm] fwhm[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;Init 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</span> <span class="o">=</span> <span class="n">Box</span><span class="p">()</span>

        <span class="k">for</span> <span class="n">rep</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">repeat</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;Step </span><span class="si">{</span><span class="n">rep</span><span class="si">}</span><span class="s2"> of </span><span class="si">{</span><span class="n">repeat</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">&quot;</span><span class="p">)</span>

            <span class="c1">################################</span>
            <span class="c1">##### Taking a boxed image #####</span>
            <span class="c1">################################</span>

            <span class="n">box</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="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="c1">###############################</span>
            <span class="c1">##### Fitting a 2d Moffat #####</span>
            <span class="c1">###############################</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="c1">##############################</span>
            <span class="c1">##### Filling the arrays #####</span>
            <span class="c1">##############################</span>

            <span class="n">m2</span> <span class="o">=</span> <span class="n">foc</span><span class="o">.</span><span class="n">position</span>
            <span class="n">fwhm</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="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">m2</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">fwhm</span><span class="p">)</span>

            <span class="n">y</span><span class="p">,</span> <span class="n">x</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">indices</span><span class="p">(</span><span class="n">data</span><span class="o">.</span><span class="n">shape</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="s2">&quot;focus&quot;</span><span class="p">:</span> <span class="p">{</span>
                    <span class="s2">&quot;m2&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="s2">&quot;fwhm&quot;</span><span class="p">:</span> <span class="n">fwhm_arr</span><span class="o">.</span><span class="n">tolist</span><span class="p">(),</span>
                <span class="p">},</span>
                <span class="c1"># &quot;data&quot;: data.tolist(),</span>
                <span class="c1"># &quot;fit&quot;: fitted(x,y).tolist(),</span>
                <span class="c1"># &quot;residuals&quot;: np.round( data - fitted(x,y) , 1).tolist(),</span>
            <span class="p">}</span>

            <span class="c1"># log.debug(self.output)</span>
            <span class="c1"># log.info(f&quot;Found: focus={m2}µm, FWHM={fwhm}&#39;&#39; &quot;)</span>

            <span class="c1"># opening all the time and not at begginning just for safety</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">msg</span> <span class="o">=</span> <span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">rep</span><span class="si">:</span><span class="s2">&gt;3</span><span class="si">}</span><span class="s2"> </span><span class="si">{</span><span class="n">m2</span><span class="si">:</span><span class="s2">&gt;10</span><span class="si">}</span><span class="s2"> </span><span class="si">{</span><span class="n">fwhm</span><span class="si">:</span><span class="s2">&gt;10</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">msg</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="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s2">&quot;Writing: &quot;</span> <span class="o">+</span> <span class="n">msg</span><span class="p">)</span>

            <span class="c1">######################</span>
            <span class="c1">##### Moving M2  #####</span>
            <span class="c1">######################</span>

            <span class="n">foc</span><span class="o">.</span><span class="n">position</span> <span class="o">+=</span> <span class="n">step</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;Moving focus py </span><span class="si">{</span><span class="n">step</span><span class="si">}</span><span class="s2">µm, to </span><span class="si">{</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>
            <span class="n">sleep</span><span class="p">(</span><span class="mf">0.3</span><span class="p">)</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="sa">f</span><span class="s2">&quot;focus=</span><span class="si">{</span><span class="n">m2_arr</span><span class="si">}</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;fwhm=</span><span class="si">{</span><span class="n">fwhm_arr</span><span class="si">}</span><span class="s2">&quot;</span><span class="p">)</span>

        <span class="n">gp</span><span class="o">.</span><span class="n">plot</span><span class="p">((</span><span class="n">m2_arr</span><span class="p">,</span> <span class="n">fwhm_arr</span><span class="p">),</span>
                <span class="n">unset</span><span class="o">=</span><span class="s2">&quot;grid&quot;</span><span class="p">,</span>
                <span class="n">_with</span><span class="o">=</span><span class="s2">&quot;linespoints&quot;</span><span class="p">,</span>
                <span class="n">terminal</span><span class="o">=</span><span class="s1">&#39;dumb 90 30&#39;</span><span class="p">,</span>
                <span class="n">xlabel</span><span class="o">=</span><span class="s2">&quot;x=focus [µm], y=fwhm [&#39;&#39;]&quot;</span><span class="p">)</span>

        <span class="c1">#################################</span>
        <span class="c1">##### Back to initial focus #####</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;Setting back 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></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>