

<!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.fillheader &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.fillheader</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.fillheader</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"># Third-party modules</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</span><span class="w"> </span><span class="kn">import</span> <span class="n">units</span> <span class="k">as</span> <span class="n">u</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">alt</span><span class="p">,</span> <span class="n">lat</span><span class="p">,</span> <span class="n">lon</span><span class="p">,</span> <span class="n">pixscale</span><span class="p">,</span> <span class="n">rotangle</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">dom</span><span class="p">,</span> <span class="n">foc</span><span class="p">,</span> <span class="n">lamp</span><span class="p">,</span> <span class="n">light</span><span class="p">,</span> <span class="n">rot</span><span class="p">,</span> <span class="n">tel</span><span class="p">,</span> <span class="n">tel_temp</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">save_filename</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>


<div class="viewcode-block" id="Template">
<a class="viewcode-back" href="../../../noctua.templates.html#noctua.templates.fillheader.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;fillheader&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">description</span> <span class="o">=</span> <span class="s2">&quot;Fills the header of observed FITS files&quot;</span>

<div class="viewcode-block" id="Template.content">
<a class="viewcode-back" href="../../../noctua.templates.html#noctua.templates.fillheader.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">save</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;save&quot;</span><span class="p">)</span> <span class="ow">or</span> <span class="kc">True</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">ob</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;ob&quot;</span><span class="p">)</span> <span class="ow">or</span> <span class="kc">None</span>
            <span class="n">tpl</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;template&quot;</span><span class="p">)</span> <span class="ow">or</span> <span class="kc">None</span>
            <span class="n">radec</span> <span class="o">=</span> <span class="n">params</span><span class="p">[</span><span class="s2">&quot;radec&quot;</span><span class="p">]</span>
            <span class="n">altaz</span> <span class="o">=</span> <span class="n">params</span><span class="p">[</span><span class="s2">&quot;altaz&quot;</span><span class="p">]</span>
            <span class="n">utc</span> <span class="o">=</span> <span class="n">params</span><span class="p">[</span><span class="s2">&quot;utc&quot;</span><span class="p">]</span>
            <span class="n">lst</span> <span class="o">=</span> <span class="n">params</span><span class="p">[</span><span class="s2">&quot;lst&quot;</span><span class="p">]</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">log</span><span class="o">.</span><span class="n">error</span><span class="p">(</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="p">)</span>
            <span class="k">return</span>

        <span class="c1">#########################</span>
        <span class="c1">##### Header update #####</span>
        <span class="c1">#########################</span>

        <span class="n">filename</span> <span class="o">=</span> <span class="n">temp_fits</span>
        <span class="n">gps</span> <span class="o">=</span> <span class="n">Time</span><span class="p">(</span><span class="n">utc</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="k">try</span><span class="p">:</span>
            <span class="k">with</span> <span class="n">fits</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="s1">&#39;update&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">h</span><span class="p">:</span>
                <span class="k">for</span> <span class="n">hdu</span> <span class="ow">in</span> <span class="n">h</span><span class="p">:</span>

                    <span class="c1">######################</span>
                    <span class="c1">##### Basic info #####</span>
                    <span class="c1">######################</span>

                    <span class="n">now1</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"># log.debug(f&quot;CONST keys start {(Time.now()-now).sec :.2f}&quot;)</span>
                    <span class="n">hdu</span><span class="o">.</span><span class="n">header</span><span class="p">[</span><span class="s1">&#39;object&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">objname</span><span class="p">,</span> <span class="s2">&quot;Object name&quot;</span>
                    <span class="n">hdu</span><span class="o">.</span><span class="n">header</span><span class="p">[</span><span class="s1">&#39;latitude&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">lat</span><span class="p">,</span> <span class="s2">&quot;[deg] Telescope latitude N&quot;</span>
                    <span class="n">hdu</span><span class="o">.</span><span class="n">header</span><span class="p">[</span><span class="s1">&#39;longitud&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">lon</span><span class="p">,</span> <span class="s2">&quot;[deg] Telescope longitude E from Greeenwich&quot;</span>
                    <span class="n">hdu</span><span class="o">.</span><span class="n">header</span><span class="p">[</span><span class="s1">&#39;altitude&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">alt</span><span class="p">,</span> <span class="s2">&quot;[m] Telescope height from sea level&quot;</span>
                    <span class="n">hdu</span><span class="o">.</span><span class="n">header</span><span class="p">[</span><span class="s1">&#39;equinox&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="mi">2000</span><span class="p">,</span> <span class="s2">&quot;Equinox&quot;</span>
                    <span class="n">hdu</span><span class="o">.</span><span class="n">header</span><span class="p">[</span><span class="s1">&#39;date&#39;</span><span class="p">]</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="p">)</span><span class="o">.</span><span class="n">isot</span><span class="p">,</span> <span class="s2">&quot;(UTC) Date the file was written&quot;</span>
                    <span class="c1"># log.debug(f&quot;CONST  keys end {(Time.now()-now).sec :.2f}&quot;)</span>

                    <span class="c1">########################</span>
                    <span class="c1">##### Cabinet info #####</span>
                    <span class="c1">########################</span>

                    <span class="c1"># now = Time.now()</span>
                    <span class="c1"># log.debug(f&quot;CABINET keys start {(Time.now()-now).sec :.2f}&quot;)</span>

                    <span class="c1"># Asking to the cabinet basic info (ra, dec, alt, az, utc)</span>
                    <span class="n">hdu</span><span class="o">.</span><span class="n">header</span><span class="p">[</span><span class="s1">&#39;alt&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">altaz</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="s2">&quot;[deg] Altitude at start&quot;</span>
                    <span class="n">hdu</span><span class="o">.</span><span class="n">header</span><span class="p">[</span><span class="s1">&#39;az&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">altaz</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="s2">&quot;[deg] Azimuth clockwise from N at start&quot;</span>
                    <span class="n">hdu</span><span class="o">.</span><span class="n">header</span><span class="p">[</span><span class="s1">&#39;ra&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">radec</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">*</span> \
                        <span class="mf">15.0</span><span class="p">,</span> <span class="s2">&quot;[deg] Right ascension J2000 at start&quot;</span>
                    <span class="n">hdu</span><span class="o">.</span><span class="n">header</span><span class="p">[</span><span class="s1">&#39;dec&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">radec</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="s2">&quot;[deg] Declination J2000 at start&quot;</span>
                    <span class="n">hdu</span><span class="o">.</span><span class="n">header</span><span class="p">[</span><span class="s1">&#39;mjd-obs&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">gps</span><span class="o">.</span><span class="n">mjd</span><span class="p">,</span> <span class="s2">&quot;[day] (MJD) Date the exposure was started&quot;</span>
                    <span class="n">hdu</span><span class="o">.</span><span class="n">header</span><span class="p">[</span><span class="s1">&#39;date-obs&#39;</span><span class="p">]</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="o">-</span>
                                                      <span class="mi">4</span><span class="p">],</span> <span class="s2">&quot;(UTC) Date the exposure was started&quot;</span>
                    <span class="n">hdu</span><span class="o">.</span><span class="n">header</span><span class="p">[</span><span class="s1">&#39;lst&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">lst</span><span class="p">,</span> <span class="s2">&quot;Local Sidereal Time&quot;</span>
                    <span class="n">hdu</span><span class="o">.</span><span class="n">header</span><span class="p">[</span><span class="s1">&#39;ha&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">lst</span> <span class="o">-</span> <span class="n">radec</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="s2">&quot;Hour Angle at start&quot;</span>

                    <span class="n">alt_rad</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">radians</span><span class="p">(</span><span class="n">altaz</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
                    <span class="n">sec_z</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span> <span class="k">if</span> <span class="n">np</span><span class="o">.</span><span class="n">isclose</span><span class="p">(</span>
                        <span class="n">np</span><span class="o">.</span><span class="n">sin</span><span class="p">(</span><span class="n">alt_rad</span><span class="p">),</span> <span class="mi">0</span><span class="p">)</span> <span class="k">else</span> <span class="nb">round</span><span class="p">(</span>
                        <span class="mi">1</span> <span class="o">/</span> <span class="n">np</span><span class="o">.</span><span class="n">sin</span><span class="p">(</span><span class="n">alt_rad</span><span class="p">),</span> <span class="mi">2</span><span class="p">)</span>
                    <span class="n">hdu</span><span class="o">.</span><span class="n">header</span><span class="p">[</span><span class="s1">&#39;airmass&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">sec_z</span><span class="p">,</span> <span class="s2">&quot;Airmass at start&quot;</span>

                    <span class="n">focrel</span> <span class="o">=</span> <span class="n">foc</span><span class="o">.</span><span class="n">position</span>
                    <span class="n">focabs</span> <span class="o">=</span> <span class="n">foc</span><span class="o">.</span><span class="n">absolute</span>

                    <span class="n">temperatures</span> <span class="o">=</span> <span class="n">tel_temp</span><span class="o">.</span><span class="n">temperature</span>
                    <span class="n">cover</span> <span class="o">=</span> <span class="n">tel</span><span class="o">.</span><span class="n">cover</span>

                    <span class="n">hdu</span><span class="o">.</span><span class="n">header</span><span class="p">[</span><span class="s1">&#39;telfocus&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">focrel</span><span class="p">,</span> <span class="s2">&quot;[mm] M2 Relative position&quot;</span>
                    <span class="n">hdu</span><span class="o">.</span><span class="n">header</span><span class="p">[</span><span class="s1">&#39;hierarch TEL FOC REL&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">focrel</span><span class="p">,</span> <span class="s2">&quot;[mm] M2 Relative position&quot;</span>
                    <span class="n">hdu</span><span class="o">.</span><span class="n">header</span><span class="p">[</span><span class="s1">&#39;hierarch TEL FOC ABS&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">focabs</span><span class="p">,</span> <span class="s2">&quot;[mm] M2 Absolute position&quot;</span>
                    <span class="n">hdu</span><span class="o">.</span><span class="n">header</span><span class="p">[</span><span class="s1">&#39;hierarch TEL M1 TEMP&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">temperatures</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="s2">&quot;[C] M1 temperature&quot;</span>
                    <span class="n">hdu</span><span class="o">.</span><span class="n">header</span><span class="p">[</span><span class="s1">&#39;hierarch TEL M2 TEMP&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">temperatures</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="s2">&quot;[C] M2 temperature&quot;</span>
                    <span class="n">hdu</span><span class="o">.</span><span class="n">header</span><span class="p">[</span><span class="s1">&#39;hierarch TEL M3 TEMP&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">temperatures</span><span class="p">[</span><span class="mi">2</span><span class="p">],</span> <span class="s2">&quot;[C] M3 temperature&quot;</span>
                    <span class="n">hdu</span><span class="o">.</span><span class="n">header</span><span class="p">[</span><span class="s1">&#39;hierarch TEL COVER&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">cover</span><span class="p">,</span> <span class="s2">&quot;M1 cover. 1.0 if full open, 0.0 if close&quot;</span>

                    <span class="c1"># log.debug(f&quot;CABINET keys end {(Time.now()-now).sec :.2f}&quot;)</span>

                    <span class="c1">######################</span>
                    <span class="c1">##### ASCOM info #####</span>
                    <span class="c1">######################</span>

                    <span class="c1"># now = Time.now()</span>
                    <span class="c1"># log.debug(f&quot;ASCOM keys start {(Time.now()-now).sec :.2f}&quot;)</span>

                    <span class="n">rotpos</span> <span class="o">=</span> <span class="n">rot</span><span class="o">.</span><span class="n">position</span>
                    <span class="n">rotabs</span> <span class="o">=</span> <span class="n">rot</span><span class="o">.</span><span class="n">absolute</span>

                    <span class="n">hdu</span><span class="o">.</span><span class="n">header</span><span class="p">[</span><span class="s1">&#39;hierarch TEL ROT REL&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">rotpos</span><span class="p">,</span> <span class="s2">&quot;[deg] Relative rotator position angle&quot;</span>
                    <span class="n">hdu</span><span class="o">.</span><span class="n">header</span><span class="p">[</span><span class="s1">&#39;hierarch TEL ROT ABS&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">rotabs</span><span class="p">,</span> <span class="s2">&quot;[deg] Absolute rotator position angle&quot;</span>
                    <span class="n">hdu</span><span class="o">.</span><span class="n">header</span><span class="p">[</span><span class="s1">&#39;hierarch TEL OFFSET ALT&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">tel</span><span class="o">.</span><span class="n">offset</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="s2">&quot;[rad] Offset in alt&quot;</span>
                    <span class="n">hdu</span><span class="o">.</span><span class="n">header</span><span class="p">[</span><span class="s1">&#39;hierarch TEL OFFSET AZ&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">tel</span><span class="o">.</span><span class="n">offset</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="s2">&quot;[rad] Offset in az&quot;</span>
                    <span class="n">hdu</span><span class="o">.</span><span class="n">header</span><span class="p">[</span><span class="s1">&#39;hierarch TEL PARKED&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">tel</span><span class="o">.</span><span class="n">park</span><span class="p">,</span> <span class="s2">&quot;True if telescope is parked&quot;</span>
                    <span class="n">hdu</span><span class="o">.</span><span class="n">header</span><span class="p">[</span><span class="s1">&#39;hierarch DOM AZIMUTH&#39;</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="s2">&quot;[deg] Dome Azimuth clockwise from N&quot;</span>
                    <span class="n">hdu</span><span class="o">.</span><span class="n">header</span><span class="p">[</span><span class="s1">&#39;hierarch DOM SHUTTER&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">dom</span><span class="o">.</span><span class="n">open</span><span class="p">,</span> <span class="s2">&quot;Dome Shutter. False if close, True if open&quot;</span>
                    <span class="n">hdu</span><span class="o">.</span><span class="n">header</span><span class="p">[</span><span class="s1">&#39;hierarch DOM SLAVED&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">dom</span><span class="o">.</span><span class="n">slave</span><span class="p">,</span> <span class="s2">&quot;True if dome slaved to telescope at start&quot;</span>
                    <span class="n">hdu</span><span class="o">.</span><span class="n">header</span><span class="p">[</span><span class="s1">&#39;hierarch DOM LAMP&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">lamp</span><span class="o">.</span><span class="n">state</span><span class="p">,</span> <span class="s2">&quot;Flat lamp. True if on, False if off&quot;</span>
                    <span class="n">hdu</span><span class="o">.</span><span class="n">header</span><span class="p">[</span><span class="s1">&#39;hierarch DOM LIGHT&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">light</span><span class="o">.</span><span class="n">state</span><span class="p">,</span> <span class="s2">&quot;Dome light. True if on, False if off&quot;</span>
                    <span class="c1"># log.debug(f&quot;ASCOM keys end {(Time.now()-now).sec :.2f}&quot;)</span>

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

                    <span class="c1"># Calling all from camera&#39;s GetImageParamter.cgi</span>

                    <span class="n">ca</span> <span class="o">=</span> <span class="n">cam</span><span class="o">.</span><span class="n">all</span>

                    <span class="n">xpixscale</span> <span class="o">=</span> <span class="nb">round</span><span class="p">(</span><span class="n">pixscale</span> <span class="o">*</span> <span class="nb">int</span><span class="p">(</span><span class="n">ca</span><span class="p">[</span><span class="s2">&quot;binning&quot;</span><span class="p">][</span><span class="mi">0</span><span class="p">]),</span> <span class="mi">3</span><span class="p">)</span>
                    <span class="n">ypixscale</span> <span class="o">=</span> <span class="nb">round</span><span class="p">(</span><span class="n">pixscale</span> <span class="o">*</span> <span class="nb">int</span><span class="p">(</span><span class="n">ca</span><span class="p">[</span><span class="s2">&quot;binning&quot;</span><span class="p">][</span><span class="mi">1</span><span class="p">]),</span> <span class="mi">3</span><span class="p">)</span>
                    <span class="n">camx</span> <span class="o">=</span> <span class="n">ca</span><span class="p">[</span><span class="s2">&quot;xrange&quot;</span><span class="p">]</span>
                    <span class="n">camy</span> <span class="o">=</span> <span class="n">ca</span><span class="p">[</span><span class="s2">&quot;yrange&quot;</span><span class="p">]</span>

                    <span class="n">hdu</span><span class="o">.</span><span class="n">header</span><span class="p">[</span><span class="s1">&#39;hierarch CAM AMBIENT&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">ca</span><span class="p">[</span><span class="s2">&quot;ambient&quot;</span><span class="p">],</span> <span class="s2">&quot;[C] Camera ambient temperature&quot;</span>
                    <span class="n">hdu</span><span class="o">.</span><span class="n">header</span><span class="p">[</span><span class="s1">&#39;hierarch CAM FAN&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">ca</span><span class="p">[</span><span class="s2">&quot;fan&quot;</span><span class="p">],</span> <span class="s2">&quot;Cooler power in percent of usage&quot;</span>
                    <span class="n">hdu</span><span class="o">.</span><span class="n">header</span><span class="p">[</span><span class="s1">&#39;hierarch CAM COOLER&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">ca</span><span class="p">[</span><span class="s2">&quot;cooler&quot;</span><span class="p">],</span> <span class="s2">&quot;Camera cooler. True if on, False if off&quot;</span>
                    <span class="n">hdu</span><span class="o">.</span><span class="n">header</span><span class="p">[</span><span class="s1">&#39;hierarch CAM TEMP&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">ca</span><span class="p">[</span><span class="s2">&quot;temperature&quot;</span><span class="p">],</span> <span class="s2">&quot;[C] Camera temperature&quot;</span>
                    <span class="n">hdu</span><span class="o">.</span><span class="n">header</span><span class="p">[</span><span class="s1">&#39;hierarch CAM XPIXSCALE&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">xpixscale</span><span class="p">,</span> <span class="s2">&quot;[arcsec/px] X scale of px in current binning&quot;</span>
                    <span class="n">hdu</span><span class="o">.</span><span class="n">header</span><span class="p">[</span><span class="s1">&#39;hierarch CAM YPIXSCALE&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">ypixscale</span><span class="p">,</span> <span class="s2">&quot;[arcsec/px] Y scale of px in current binning&quot;</span>
                    <span class="n">hdu</span><span class="o">.</span><span class="n">header</span><span class="p">[</span><span class="s1">&#39;hierarch CAM XRANGE&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span>
                        <span class="n">camx</span><span class="p">),</span> <span class="s2">&quot;[x_start, x_end] in current binning&quot;</span>
                    <span class="n">hdu</span><span class="o">.</span><span class="n">header</span><span class="p">[</span><span class="s1">&#39;hierarch CAM YRANGE&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span>
                        <span class="n">camy</span><span class="p">),</span> <span class="s2">&quot;[y_start, y_end] in current binning&quot;</span>
                    <span class="c1"># #log.debug(f&quot;CAM keys end {(Time.now()-now).sec :.2f}&quot;</span>

                    <span class="c1">########################</span>
                    <span class="c1">##### WCS keywords #####</span>
                    <span class="c1">########################</span>

                    <span class="n">xdegscale</span> <span class="o">=</span> <span class="n">xpixscale</span> <span class="o">*</span> <span class="n">u</span><span class="o">.</span><span class="n">arcsec</span><span class="o">.</span><span class="n">to</span><span class="p">(</span><span class="n">u</span><span class="o">.</span><span class="n">deg</span><span class="p">)</span>
                    <span class="n">ydegscale</span> <span class="o">=</span> <span class="n">ypixscale</span> <span class="o">*</span> <span class="n">u</span><span class="o">.</span><span class="n">arcsec</span><span class="o">.</span><span class="n">to</span><span class="p">(</span><span class="n">u</span><span class="o">.</span><span class="n">deg</span><span class="p">)</span>
                    <span class="n">angle</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">deg2rad</span><span class="p">(</span><span class="n">rotpos</span> <span class="o">+</span> <span class="n">rotangle</span><span class="p">)</span>
                    <span class="n">flip</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span>

                    <span class="n">hdu</span><span class="o">.</span><span class="n">header</span><span class="p">[</span><span class="s2">&quot;WCSAXES&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="mi">2</span>
                    <span class="n">hdu</span><span class="o">.</span><span class="n">header</span><span class="p">[</span><span class="s2">&quot;CRPIX1&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span>
                        <span class="n">camx</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">-</span> <span class="n">camx</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span> <span class="o">/</span> <span class="mf">2.</span><span class="p">,</span> <span class="s2">&quot;Pixel coordinate of reference point&quot;</span>
                    <span class="n">hdu</span><span class="o">.</span><span class="n">header</span><span class="p">[</span><span class="s2">&quot;CRPIX2&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span>
                        <span class="n">camy</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">-</span> <span class="n">camy</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span> <span class="o">/</span> <span class="mf">2.</span><span class="p">,</span> <span class="s2">&quot;Pixel coordinate of reference point&quot;</span>
                    <span class="n">hdu</span><span class="o">.</span><span class="n">header</span><span class="p">[</span><span class="s2">&quot;PC1_1&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">cos</span><span class="p">(</span>
                        <span class="n">angle</span><span class="p">),</span> <span class="s2">&quot;Coordinate transformation matrix element&quot;</span>
                    <span class="n">hdu</span><span class="o">.</span><span class="n">header</span><span class="p">[</span><span class="s2">&quot;PC2_1&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">sin</span><span class="p">(</span>
                        <span class="n">angle</span><span class="p">),</span> <span class="s2">&quot;Coordinate transformation matrix element&quot;</span>
                    <span class="n">hdu</span><span class="o">.</span><span class="n">header</span><span class="p">[</span><span class="s2">&quot;PC1_2&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">sin</span><span class="p">(</span>
                        <span class="n">angle</span><span class="p">),</span> <span class="s2">&quot;Coordinate transformation matrix element&quot;</span>
                    <span class="n">hdu</span><span class="o">.</span><span class="n">header</span><span class="p">[</span><span class="s2">&quot;PC2_2&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">flip</span> <span class="o">*</span> \
                        <span class="n">np</span><span class="o">.</span><span class="n">cos</span><span class="p">(</span><span class="n">angle</span><span class="p">),</span> <span class="s2">&quot;Coordinate transformation matrix element&quot;</span>
                    <span class="n">hdu</span><span class="o">.</span><span class="n">header</span><span class="p">[</span><span class="s2">&quot;CDELT1&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">xdegscale</span><span class="p">,</span> <span class="s2">&quot;[deg] Coordinate increment at reference point&quot;</span>
                    <span class="n">hdu</span><span class="o">.</span><span class="n">header</span><span class="p">[</span><span class="s2">&quot;CDELT2&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">ydegscale</span><span class="p">,</span> <span class="s2">&quot;[deg] Coordinate increment at reference point&quot;</span>
                    <span class="n">hdu</span><span class="o">.</span><span class="n">header</span><span class="p">[</span><span class="s2">&quot;CUNIT1&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;deg&#39;</span><span class="p">,</span> <span class="s2">&quot;Units of coordinate increment and value&quot;</span>
                    <span class="n">hdu</span><span class="o">.</span><span class="n">header</span><span class="p">[</span><span class="s2">&quot;CUNIT2&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;deg&#39;</span><span class="p">,</span> <span class="s2">&quot;Units of coordinate increment and value&quot;</span>
                    <span class="n">hdu</span><span class="o">.</span><span class="n">header</span><span class="p">[</span><span class="s2">&quot;CTYPE1&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;RA---TAN&#39;</span><span class="p">,</span> <span class="s2">&quot;Right ascension, gnomonic projection&quot;</span>
                    <span class="n">hdu</span><span class="o">.</span><span class="n">header</span><span class="p">[</span><span class="s2">&quot;CTYPE2&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;DEC--TAN&#39;</span><span class="p">,</span> <span class="s2">&quot;Declination, gnomonic projection&quot;</span>
                    <span class="n">hdu</span><span class="o">.</span><span class="n">header</span><span class="p">[</span><span class="s2">&quot;CRVAL1&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">radec</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">*</span> <span class="mf">15.0</span>
                    <span class="n">hdu</span><span class="o">.</span><span class="n">header</span><span class="p">[</span><span class="s2">&quot;CRVAL2&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">radec</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
                    <span class="n">hdu</span><span class="o">.</span><span class="n">header</span><span class="p">[</span><span class="s2">&quot;RADESYS&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="s2">&quot;ICRS&quot;</span>

                    <span class="c1"># wcss = WCS()</span>
                    <span class="c1"># wcss.wcs.ctype = [&quot;RA---TAN&quot;, &quot;DEC--TAN&quot;]</span>

                    <span class="c1"># wcss.wcs.cd = scale*np.array([[np.cos(angle), np.sin(angle)],</span>
                    <span class="c1">#                         [np.sin(angle), flip*np.cos(angle)]])</span>

                    <span class="c1"># wcss.wcs.cunit = [&#39;deg&#39;, &#39;deg&#39;]</span>

                    <span class="c1"># wcss.wcs.crval = [radec,</span>
                    <span class="c1">#                   radec*15]</span>

                    <span class="c1"># wcss.wcs.crpix = [ (camx[1]-camx[0])/2.,</span>
                    <span class="c1">#                    (camy[1]-camy[0])/2.]</span>

                    <span class="c1"># hdu.header.update(wcss.to_header())</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;Header updated in </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">now1</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="k">except</span> <span class="ne">FileNotFoundError</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">error</span><span class="p">(</span><span class="s2">&quot;File not found&quot;</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="n">e</span><span class="p">)</span>

        <span class="c1">###############################</span>
        <span class="c1">##### Saving in data path #####</span>
        <span class="c1">###############################</span>

        <span class="k">if</span> <span class="n">save</span><span class="p">:</span>
            <span class="n">outpath</span> <span class="o">=</span> <span class="n">save_filename</span><span class="p">(</span><span class="n">filename</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;Copying </span><span class="si">{</span><span class="n">filename</span><span class="si">}</span><span class="s2"> as </span><span class="si">{</span><span class="n">outpath</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="nb">str</span><span class="p">(</span><span class="n">outpath</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;fillheader </span><span class="si">{</span><span class="bp">self</span><span class="o">.</span><span class="n">filename</span><span class="si">}</span><span class="s2">&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>