

<!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.observation &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.observation</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.observation</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">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="p">(</span><span class="n">camera_state</span><span class="p">,</span> <span class="n">filter_name</span><span class="p">,</span> <span class="n">filter_number</span><span class="p">,</span>
                                <span class="n">filter_state</span><span class="p">,</span> <span class="n">frame_number</span><span class="p">,</span> <span class="n">frame_type</span><span class="p">,</span>
                                <span class="n">image_state</span><span class="p">,</span> <span class="n">temp_fits</span><span class="p">)</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">dom</span><span class="p">,</span> <span class="n">tel</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">..utils.coordinates</span><span class="w"> </span><span class="kn">import</span> <span class="n">apply_offset</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">.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">.fillheader</span><span class="w"> </span><span class="kn">import</span> <span class="n">Template</span> <span class="k">as</span> <span class="n">FillHeader</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">.lampsoff</span><span class="w"> </span><span class="kn">import</span> <span class="n">Template</span> <span class="k">as</span> <span class="n">LampsOff</span>


<div class="viewcode-block" id="Template">
<a class="viewcode-back" href="../../../noctua.templates.html#noctua.templates.observation.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;observation&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">description</span> <span class="o">=</span> <span class="s2">&quot;Observes a field&quot;</span>
        <span class="c1"># self.box = 300</span>
        <span class="c1"># self.recenter = False</span>
        <span class="c1"># #self.domeslewing = None</span>
        <span class="c1"># if not hasattr(self, &quot;domeslewing&quot;):</span>
        <span class="c1">#     log.warning(&quot;domeslewing attribute undefined, setting to False&quot;)</span>
        <span class="c1">#     self.domeslewing = False</span>

<div class="viewcode-block" id="Template.content">
<a class="viewcode-back" href="../../../noctua.templates.html#noctua.templates.observation.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">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">1</span>
            <span class="n">frametype</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;frametype&quot;</span><span class="p">)</span> <span class="ow">or</span> <span class="s2">&quot;Light&quot;</span>
            <span class="n">binning</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;binning&quot;</span><span class="p">)</span> <span class="ow">or</span> <span class="n">cam</span><span class="o">.</span><span class="n">binning</span>
            <span class="n">filt</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;filter&quot;</span><span class="p">)</span> <span class="ow">or</span> <span class="n">filter_name</span><span class="p">[</span><span class="n">cam</span><span class="o">.</span><span class="n">filter</span><span class="p">]</span>
            <span class="n">xystart</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;xystart&quot;</span><span class="p">)</span> <span class="ow">or</span> <span class="n">cam</span><span class="o">.</span><span class="n">xystart</span>
            <span class="n">xyend</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;xyend&quot;</span><span class="p">)</span> <span class="ow">or</span> <span class="n">cam</span><span class="o">.</span><span class="n">xyend</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="bp">self</span><span class="o">.</span><span class="n">domeslewing</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;domeslewing&quot;</span><span class="p">)</span> <span class="ow">or</span> <span class="kc">False</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">recenter</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;recenter&quot;</span><span class="p">)</span> <span class="ow">or</span> <span class="kc">False</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">box</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;box&quot;</span><span class="p">)</span> <span class="ow">or</span> <span class="mi">300</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">#########################################</span>
        <span class="c1">##### Switch off lamps if not Flat  #####</span>
        <span class="c1">#########################################</span>

        <span class="k">if</span> <span class="n">frametype</span> <span class="o">!=</span> <span class="s2">&quot;Flat&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;Not a &#39;Flat&#39; frametype: switching off lamps&quot;</span><span class="p">)</span>
            <span class="n">lao</span> <span class="o">=</span> <span class="n">LampsOff</span><span class="p">()</span>
            <span class="n">lao</span><span class="o">.</span><span class="n">run</span><span class="p">({})</span>

        <span class="k">if</span> <span class="n">frametype</span> <span class="o">!=</span> <span class="s2">&quot;Light&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;Not a &#39;Light&#39; frametype: switching off recentering&quot;</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">recenter</span> <span class="o">=</span> <span class="kc">False</span>

        <span class="c1">########################</span>
        <span class="c1">##### Camera setup #####</span>
        <span class="c1">########################</span>

        <span class="c1"># Bringing back the camera to Idle state.</span>
        <span class="k">if</span> <span class="n">cam</span><span class="o">.</span><span class="n">state</span> <span class="o">!=</span> <span class="mi">0</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;Resetting camera from state </span><span class="si">{</span><span class="n">camera_state</span><span class="p">[</span><span class="n">cam</span><span class="o">.</span><span class="n">state</span><span class="p">]</span><span class="si">}</span><span class="s2">.&quot;</span><span class="p">)</span>
            <span class="n">cam</span><span class="o">.</span><span class="n">abort</span><span class="p">()</span>
            <span class="n">sleep</span><span class="p">(</span><span class="mf">0.1</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;Camera is </span><span class="si">{</span><span class="n">camera_state</span><span class="p">[</span><span class="n">cam</span><span class="o">.</span><span class="n">state</span><span class="p">]</span><span class="si">}</span><span class="s2">&quot;</span><span class="p">)</span>

        <span class="c1"># Changing binning if not the same as the current one</span>
        <span class="k">if</span> <span class="n">binning</span> <span class="o">!=</span> <span class="n">cam</span><span class="o">.</span><span class="n">binning</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;Changing binning to </span><span class="si">{</span><span class="n">binning</span><span class="si">}</span><span class="s2">&quot;</span><span class="p">)</span>
            <span class="n">cam</span><span class="o">.</span><span class="n">binning</span> <span class="o">=</span> <span class="p">[</span><span class="n">binning</span><span class="p">,</span> <span class="n">binning</span><span class="p">]</span>
            <span class="n">sleep</span><span class="p">(</span><span class="mf">0.1</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;Camera is </span><span class="si">{</span><span class="n">camera_state</span><span class="p">[</span><span class="n">cam</span><span class="o">.</span><span class="n">state</span><span class="p">]</span><span class="si">}</span><span class="s2">&quot;</span><span class="p">)</span>

        <span class="c1"># Changing filter if not the same as the current one</span>
        <span class="c1"># On bad ctrl-c, filter can be 15 (?!)</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;Filter is number </span><span class="si">{</span><span class="n">cam</span><span class="o">.</span><span class="n">filter</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">warning</span><span class="p">(</span>
            <span class="sa">f</span><span class="s2">&quot;Filter is </span><span class="si">{</span><span class="n">filter_name</span><span class="p">[</span><span class="n">cam</span><span class="o">.</span><span class="n">filter</span><span class="p">]</span><span class="si">}</span><span class="s2">, we have selected </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="k">if</span> <span class="n">filter_number</span><span class="p">[</span><span class="n">filt</span><span class="p">]</span> <span class="o">!=</span> <span class="n">cam</span><span class="o">.</span><span class="n">filter</span><span class="p">:</span>
            <span class="n">msg</span> <span class="o">=</span> <span class="sa">f</span><span class="s2">&quot;Changing filter to </span><span class="si">{</span><span class="n">filt</span><span class="si">}</span><span class="s2">, &quot;</span>
            <span class="n">msg</span> <span class="o">+=</span> <span class="sa">f</span><span class="s2">&quot;Which is number </span><span class="si">{</span><span class="n">filter_number</span><span class="p">[</span><span class="n">filt</span><span class="p">]</span><span class="si">}</span><span class="s2">&quot;</span>
            <span class="n">log</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span>

            <span class="c1"># Setting filter based on its number</span>
            <span class="n">cam</span><span class="o">.</span><span class="n">filter</span> <span class="o">=</span> <span class="n">filter_number</span><span class="p">[</span><span class="n">filt</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"># Check until filter is not moving</span>
            <span class="n">status</span> <span class="o">=</span> <span class="mi">1</span>
            <span class="k">while</span> <span class="n">status</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">:</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">try</span><span class="p">:</span>
                    <span class="n">status</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">cam</span><span class="o">.</span><span class="n">is_moving</span><span class="p">)</span>
                <span class="k">except</span> <span class="ne">ValueError</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;Error trying to contact the camera: </span><span class="si">{</span><span class="n">e</span><span class="si">}</span><span class="s2">&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">if</span> <span class="n">status</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>  <span class="c1"># i.e. error in moving filter</span>
                    <span class="n">msg</span> <span class="o">=</span> <span class="sa">f</span><span class="s2">&quot;Filter </span><span class="si">{</span><span class="n">filter_state</span><span class="p">[</span><span class="n">status</span><span class="p">]</span><span class="si">}</span><span class="s2">&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="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="sa">f</span><span class="s2">&quot;Filter </span><span class="si">{</span><span class="n">filter_state</span><span class="p">[</span><span class="n">status</span><span class="p">]</span><span class="si">}</span><span class="s2">&quot;</span><span class="p">)</span>
                    <span class="n">sleep</span><span class="p">(</span><span class="mf">0.75</span><span class="p">)</span>

        <span class="c1"># Changing windowing</span>
        <span class="k">if</span> <span class="n">xystart</span> <span class="ow">and</span> <span class="n">xyend</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;Changing windowding to xystart:</span><span class="si">{</span><span class="n">xystart</span><span class="si">}</span><span class="s2">, xyend:</span><span class="si">{</span><span class="n">xyend</span><span class="si">}</span><span class="s2">&quot;</span><span class="p">)</span>
            <span class="n">cam</span><span class="o">.</span><span class="n">xystart</span> <span class="o">=</span> <span class="n">xystart</span>
            <span class="n">cam</span><span class="o">.</span><span class="n">xyend</span> <span class="o">=</span> <span class="n">xyend</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">msg</span> <span class="o">=</span> <span class="sa">f</span><span class="s2">&quot;No windowing: full frame in bininng </span><span class="si">{</span><span class="n">binning</span><span class="si">}</span><span class="s2">&quot;</span>
            <span class="n">log</span><span class="o">.</span><span class="n">warning</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span>
            <span class="n">cam</span><span class="o">.</span><span class="n">full_frame</span><span class="p">()</span>

        <span class="c1">#########################</span>
        <span class="c1">##### Exposure loop #####</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;Starting </span><span class="si">{</span><span class="n">repeat</span><span class="si">}</span><span class="s2"> </span><span class="si">{</span><span class="n">frametype</span><span class="si">}</span><span class="s2"> frames of </span><span class="si">{</span><span class="n">exptime</span><span class="si">}</span><span class="s2">s&quot;</span><span class="p">)</span>

        <span class="c1"># Looping on the number of exposures</span>
        <span class="k">for</span> <span class="n">exposure</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="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"># RA, DEC, ALT, AZ, UTC are asked to cabinet</span>
            <span class="c1"># simultaneously in order to avoid latency.</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;Asking cabinet time and coordinates&quot;</span><span class="p">)</span>
            <span class="n">basic_cabinet</span> <span class="o">=</span> <span class="n">tel</span><span class="o">.</span><span class="n">coordinates</span>
            <span class="n">gps</span> <span class="o">=</span> <span class="n">Time</span><span class="p">(</span><span class="n">tel</span><span class="o">.</span><span class="n">coordinates</span><span class="p">[</span><span class="s2">&quot;utc&quot;</span><span class="p">],</span> <span class="nb">format</span><span class="o">=</span><span class="s2">&quot;unix&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;Starting Exposure </span><span class="si">{</span><span class="n">exposure</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"># ##### Dome Slewing #####</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;Dome slewing is </span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">domeslewing</span><span class="si">}</span><span class="s2">&quot;</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">frametype</span> <span class="o">==</span> <span class="s2">&quot;Light&quot;</span><span class="p">:</span>
                <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">domeslewing</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;Check Dome position&quot;</span><span class="p">)</span>
                    <span class="k">while</span> <span class="p">(</span><span class="nb">abs</span><span class="p">(</span><span class="n">tel</span><span class="o">.</span><span class="n">coordinates</span><span class="p">[</span><span class="s2">&quot;altaz&quot;</span><span class="p">][</span><span class="mi">1</span><span class="p">]</span> <span class="o">-</span> <span class="n">dom</span><span class="o">.</span><span class="n">azimuth</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">3</span><span class="p">):</span>
                        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">domeslewing</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;Stop slewing anyway&quot;</span><span class="p">)</span>
                            <span class="k">return</span>

                        <span class="n">dom</span><span class="o">.</span><span class="n">azimuth</span> <span class="o">=</span> <span class="n">tel</span><span class="o">.</span><span class="n">coordinates</span><span class="p">[</span><span class="s2">&quot;altaz&quot;</span><span class="p">][</span><span class="mi">1</span><span class="p">]</span>
                        <span class="n">sleep</span><span class="p">(</span><span class="mf">0.5</span><span class="p">)</span>
                        <span class="k">while</span> <span class="n">dom</span><span class="o">.</span><span class="n">is_moving</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="n">log</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span>
                                    <span class="s2">&quot;check_pause_or_abort returned true. I&#39;m here&quot;</span><span class="p">)</span>
                                <span class="k">return</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;Waiting for Dome to Stop&quot;</span><span class="p">)</span>
                            <span class="n">sleep</span><span class="p">(</span><span class="mf">0.5</span><span class="p">)</span>

            <span class="c1">##########################</span>
            <span class="c1">##### Start exposure #####</span>
            <span class="c1">##########################</span>

            <span class="k">try</span><span class="p">:</span>
                <span class="n">cam</span><span class="o">.</span><span class="n">start</span><span class="p">(</span><span class="n">exptime</span><span class="p">,</span>
                          <span class="n">frame_number</span><span class="p">[</span><span class="n">frametype</span><span class="p">],</span>
                          <span class="n">datetime</span><span class="o">=</span><span class="n">gps</span><span class="o">.</span><span class="n">isot</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">msg</span> <span class="o">=</span> <span class="sa">f</span><span class="s2">&quot;An error occurred : </span><span class="si">{</span><span class="n">e</span><span class="si">}</span><span class="s2">&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"># Some debug time counter</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="c1"># Checking if the exposure is still running</span>
            <span class="n">status</span> <span class="o">=</span> <span class="mi">1</span>
            <span class="k">while</span> <span class="n">status</span> <span class="o">!=</span> <span class="mi">0</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="n">log</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;check_pause_or_abort returned true. I&#39;m here&quot;</span><span class="p">)</span>
                    <span class="k">return</span>

                <span class="k">try</span><span class="p">:</span>
                    <span class="n">status</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">cam</span><span class="o">.</span><span class="n">state</span><span class="p">)</span>
                <span class="k">except</span> <span class="ne">TypeError</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;Error trying to read camera status: </span><span class="si">{</span><span class="n">e</span><span class="si">}</span><span class="s2">&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">if</span> <span class="n">status</span> <span class="o">==</span> <span class="mi">5</span><span class="p">:</span>  <span class="c1"># i.e. error in camera</span>
                    <span class="n">msg</span> <span class="o">=</span> <span class="sa">f</span><span class="s2">&quot;Camera </span><span class="si">{</span><span class="n">camera_state</span><span class="p">[</span><span class="n">status</span><span class="p">]</span><span class="si">}</span><span class="s2">&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="k">else</span><span class="p">:</span>
                    <span class="n">elapsed</span> <span class="o">=</span> <span class="p">(</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">now</span><span class="p">)</span><span class="o">.</span><span class="n">sec</span>
                    <span class="n">msg</span> <span class="o">=</span> <span class="sa">f</span><span class="s2">&quot;Camera </span><span class="si">{</span>
<span class="w">                        </span><span class="n">camera_state</span><span class="p">[</span><span class="n">status</span><span class="p">]</span><span class="si">}</span><span class="s2">, </span><span class="si">{</span>
<span class="w">                        </span><span class="n">elapsed</span><span class="si">:</span><span class="s2">.1f</span><span class="si">}</span><span class="s2">/</span><span class="si">{</span><span class="n">exptime</span><span class="si">}</span><span class="s2">s &quot;</span>
                    <span class="n">msg</span> <span class="o">+=</span> <span class="sa">f</span><span class="s2">&quot;of exposure </span><span class="si">{</span><span class="n">exposure</span><span class="si">}</span><span class="s2">/</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="n">log</span><span class="o">.</span><span class="n">warning</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span>
                    <span class="n">sleep</span><span class="p">(</span><span class="mf">0.75</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;check_pause_or_abort before the download&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="n">log</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;check_pause_or_abort returned true. I&#39;m here&quot;</span><span class="p">)</span>
                <span class="k">return</span>

            <span class="c1">################################</span>
            <span class="c1">##### Download from camera #####</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;Downloading frame as </span><span class="si">{</span><span class="n">temp_fits</span><span class="si">}</span><span class="s2">&quot;</span><span class="p">)</span>

            <span class="c1"># Checking if the image is ready to be downloaded</span>
            <span class="k">while</span> <span class="n">cam</span><span class="o">.</span><span class="n">ready</span> <span class="o">!=</span> <span class="mi">1</span><span class="p">:</span>
                <span class="n">sleep</span><span class="p">(</span><span class="mf">0.5</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="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;Buffer still </span><span class="si">{</span><span class="n">image_state</span><span class="p">[</span><span class="n">cam</span><span class="o">.</span><span class="n">ready</span><span class="p">]</span><span class="si">}</span><span class="s2">&quot;</span><span class="p">)</span>
                    <span class="k">return</span>

            <span class="n">cam</span><span class="o">.</span><span class="n">download</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;Elapsed </span><span class="si">{</span><span class="p">(</span><span class="n">Time</span><span class="o">.</span><span class="n">now</span><span class="p">()</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="n">now</span><span class="p">)</span><span class="o">.</span><span class="n">sec</span><span class="si">:</span><span class="s2">.2f</span><span class="si">}</span><span class="s2">&quot;</span><span class="p">)</span>

            <span class="c1">##################################</span>
            <span class="c1">##### Update header and save #####</span>
            <span class="c1">##################################</span>

            <span class="n">fih_params</span> <span class="o">=</span> <span class="p">{</span>
                <span class="s2">&quot;object&quot;</span><span class="p">:</span> <span class="n">objname</span><span class="p">,</span>
                <span class="c1"># Add in some way also ob_file and template name from json</span>
            <span class="p">}</span>

            <span class="n">fih_params</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">basic_cabinet</span><span class="p">)</span>  <span class="c1"># radec, altaz, utc, lst</span>

            <span class="n">fih</span> <span class="o">=</span> <span class="n">FillHeader</span><span class="p">()</span>

            <span class="n">fih</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">fih_params</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="sa">f</span><span class="s2">&quot;observation </span><span class="si">{</span><span class="n">fih</span><span class="o">.</span><span class="n">filename</span><span class="si">}</span><span class="s2">&quot;</span><span class="p">)</span>

            <span class="bp">self</span><span class="o">.</span><span class="n">filename</span> <span class="o">=</span> <span class="n">fih</span><span class="o">.</span><span class="n">filename</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">filenames</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">fih</span><span class="o">.</span><span class="n">filename</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="sa">f</span><span class="s2">&quot;filenames from obs tpl </span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">filenames</span><span class="si">}</span><span class="s2">&quot;</span><span class="p">)</span>

            <span class="c1">######################################</span>
            <span class="c1">##### Calculate and apply offset #####</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;Recenter is </span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">recenter</span><span class="si">}</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">recenter</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;... with box=</span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">box</span><span class="si">}</span><span class="s2">px&quot;</span><span class="p">)</span>

            <span class="k">if</span> <span class="n">frametype</span> <span class="o">==</span> <span class="s2">&quot;Light&quot;</span><span class="p">:</span>
                <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">recenter</span><span class="p">:</span>
                    <span class="k">try</span><span class="p">:</span>
                        <span class="n">apply_offset</span><span class="p">(</span>
                            <span class="n">fits_file</span><span class="o">=</span><span class="n">temp_fits</span><span class="p">,</span> <span class="n">box</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">box</span><span class="p">,</span> <span class="n">display</span><span class="o">=</span><span class="kc">False</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;Cannot apply offset: </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="k">return</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>