% \iffalse meta-comment
%
% Copyright (C) 2025 Valentin Dao
% 
% This file may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either
% version 1.3 of this license or (at your option) any later
% version. The latest version of this license is in:
% 
%   http://www.latex-project.org/lppl.txt
% 
% and version 1.3c or later is part of all distributions of
% LaTeX version 2008-05-04 or later.
% 
% This work has the LPPL maintenance status 'maintained'
% and the current maintainer is Valentin Dao (vdao.texdev@gmail.com).
% 
% This work consists of the files intexgral.ins, intexgral-fr.dtx and intexgral-en.dtx, along with the derived files intexgral.sty, intexgral-fr.pdf, and intexgral-en.pdf.
% 
% The repository of this work can be found at:
% 
%   https://github.com/ankaa3908/intexgral
%
%<*driver>
% \fi
% \iffalse
\documentclass[11pt, a4paper]{l3doc}

\usepackage{fontspec}

\setmainfont{ChronicleTextG3}[
  Extension             = .otf,
  Renderer              = HarfBuzz,
  UprightFont           = *-Roman-Pro,
  UprightFeatures       = { Kerning = On },
  ItalicFont            = *-Italic-Pro,
  BoldFont              = *-Bold-Pro,
  BoldItalicFont        = *-BoldIta-Pro,
]

\setsansfont{Whitney}[
  Extension        = .otf,
  UprightFont      = *-Medium,
  ItalicFont       = *-MediumItalic,
  BoldFont         = *-Bold,
  BoldItalicFont   = *-BoldItalic,
  UprightFeatures  = 
    {
      StylisticSet = 1, 
      StylisticSet = 10, 
      StylisticSet = 11, 
    },
  BoldFeatures     = 
    {
      StylisticSet = 1, 
      StylisticSet = 10, 
      StylisticSet = 11, 
    },
  ItalicFeatures   = 
    {
      StylisticSet = 1, 
      StylisticSet = 10, 
      StylisticSet = 11, 
      StylisticSet = 8, 
      StylisticSet = 9  
    }
]

\setmonofont{MonaspaceNeon}[
  Extension              = .otf,
  Renderer               = HarfBuzz,
  Scale                  = 0.8,
  UprightFont            = *-Regular,
  UprightFeatures        = { Kerning = On },
  ItalicFont             = *-Italic,
  BoldFont               = *-Bold,
  BoldItalicFont         = *-BoldItalic,
]

\usepackage{microtype}

\usepackage{fancyhdr}
\fancyhf{}
\fancyfoot[C]{\small\sffamily\itshape---\space\thepage\space\kern0.4mm\/---}
\renewcommand{\headrulewidth}{0pt}
\pagestyle{fancy}

\usepackage{fits-astro}

\usepackage[silence]{codedescribe}
\usepackage{hologo}

\usepackage[svgnames]{xcolor}

\definecolor{RoyalBlue}{RGB}{0, 35, 102}
\definecolor{RoyalRed}{RGB}{157, 16, 45}
\definecolor{RoyalGreen}{HTML}{48845F}
\definecolor{urlcolour}{HTML}{B42F5E}

\definecolor{packagecolour}{HTML}{B86B00}
\definecolor{keycolour}{HTML}{004766}
\definecolor{macrocolour}{HTML}{047658}
\definecolor{macrocolour2}{HTML}{630476}

\defobjectfmt{macro}{code}{color=macrocolour}
\defobjectfmt{pkg}{pkg}{color=packagecolour, font=\sffamily}

\newcodekey{fits}{
  ruleht        = 0,
  texcsstyle    = \bfseries\color{RoyalBlue},
  texcs         =
    [2]{
      loadheader,
      showheader,
      headerkey,
      setheaderkey,
      gsetheaderkey,
    },
  texcs         = 
    [3]{
      flameHalpha,
      exptime,
      observer,
    },
  texcsstyle   = [2]{\color{macrocolour}},
  texcsstyle   = [3]{\color{macrocolour2}},
  codeprefix    = {},
  resultprefix  = {}
}

\lstset{
  gobble=2,
}

\usepackage{titlesec}
\titleformat*{\section}{\sffamily\LARGE\bfseries}
\titleformat*{\subsection}{\sffamily\Large\bfseries}

\usepackage{changelog}
\usepackage{enumitem}
\renewenvironment{changelogitemize}
  {\begin{itemize}[label=\raisebox{0.5ex}{$\scriptscriptstyle\blacktriangleright$}]}
  {\end{itemize}}

\setlength{\parindent}{0pt}

\usepackage{polyglossia}
\setmainlanguage{english}

\usepackage[perpage]{footmisc}

\usepackage{hyperref}
\hypersetup{
  pdfauthor   = {Valentin Dao},
  pdftitle    = {The fits package},
  pdfcreator  = {LuaLaTeX with hyperref package},
  colorlinks  = true,
  linkcolor   = RoyalBlue,
  urlcolor    = urlcolour,
}

\directlua{
  local path = '../../build.lua'
  dofile(path)
  local new_summary = uploadconfig.summary:gsub('LuaLaTeX', 'Lua\\TeX')
  token.set_macro('pkgdescription', new_summary)
  token.set_macro('pkgversion', uploadconfig.version)
  token.set_macro('pkgname', uploadconfig.pkg)
  token.set_macro('pkgauthor', uploadconfig.author)
  token.set_macro('pkgemail', uploadconfig.email)
}

\title{The \textsf{\pkgname} package\medbreak{\Large\pkgversion}}
\author{\pkgauthor\footnote{E-mail: \href{mailto:vdao.texdev@gmail.com}{\ttfamily\pkgemail}}}
\date{\today}

\begin{document}
  \DocInput{fits-astro.dtx}
\end{document}
%</driver>
% \fi
% 
% \begin{documentation}
% 
% \maketitle
% 
% The \tsobj[pkg]{\pkgname} package provides a simple interface to read Flexible Image Transport System (FITS) files and access their header to retrieve information. It relies on a custom Lua script and therefore requires the Lua\hologo{LaTeXTeX} engine. Unlike more complex libraries, such as \texttt{astropy.io.fits} in Python, this package is designed solely for directly recovering key-value pairs from headers. It is \emph{not} intended for modifying the header or fixing standardisation issues, let alone for reading the data part.
% 
% \section{Loading the package}
% 
% The package may be loaded in either of the following ways:
% \begin{itemize}
%   \item \tsobj[code]{\usepackage{fits-astro}} if compiling with Lua\LaTeX.
%   \item \tsobj[code]{\input{fits-astro.tex}} if compiling with Lua\TeX.
% \end{itemize}
% 
% \section{Package macros}
% 
% \begin{codedescribe}[macro]{\loadheader}
% \begin{codesyntax}
%   \tsobj[code]{\loadheader}\tsargs[marg]{FITS identification}\tsargs[marg]{FITS file path}
% \end{codesyntax}
%   This macro reads the \texttt{FITS} file and allocates the header information to the specified FITS identification, which can either be a string or a control sequence (similarly to how \tsobj[macro]{\newread} works for file streams). In the latter case, \tsobj[macro]{\csstring} is used to convert the control sequence to a string for internal storage. Therefore, if a control sequence is specified, it does not actually contain the header in any way. As a result, the two inputs are interchangeable. The macro furthermore uses the \texttt{Kpathsea} library, so the file shouldn't necessarily be in the current directory. 
% \end{codedescribe}
% 
% \begin{codestore}[loadfits]
%   \loadheader\flameHalpha{Halpha_raw.fits} % A frame of the Flame Nebula with H-alpha filter
%   \loadheader{flameHalpha}{Halpha_raw.fits} % Alternative way to load the file
% \end{codestore}
% 
% \tscode*[fits]{loadfits}
% \loadheader\flameHalpha{Halpha_raw.fits}
% 
% \begin{codedescribe}[macro]{\showheader}
% \begin{codesyntax}
%   \tsobj[code]{\showheader}\tsargs[marg]{FITS identification}
% \end{codesyntax}
%   This macro displays the header information for the specified FITS identification in the log file.
% \end{codedescribe}
% 
% \begin{codestore}[showfits]
%   \showheader\flameHalpha
%   \showheader{flameHalpha} % Would produce the same result
% \end{codestore}
% 
% \tscode*[fits]{showfits}
% 
% \begin{codedescribe}[macro]{\headerkey}
% \begin{codesyntax}
%   \tsobj[code]{\headerkey}\tsargs[marg]{FITS identification}\tsargs[marg]{key}
% \end{codesyntax}
%   This macro retrieves the value associated with the specified key from the header of the given FITS identification. 
% \end{codedescribe}
% 
% \begin{codestore}[keyfits]
%   \headerkey\flameHalpha{OBJECT}\par
%   \headerkey\flameHalpha{FILTER}
% \end{codestore}
% 
% \tsdemo[fits]{keyfits}
% 
% \begin{codedescribe}[macro]{\setheaderkey, \gsetheaderkey}
% \begin{codesyntax}
%   \tsobj[code]{\setheaderkey}\tsargs[marg]{FITS identification}\tsargs[marg]{key}\tsargs[marg]{control sequence}
% \end{codesyntax}
%   This macro retrieves the value associated with the specified key from the header of the given FITS identification and assigns it to the specified control sequence. The assignment is either local to the current group or global.
% \end{codedescribe}
% 
% \begin{codestore}[setfitskey]
%   \tt
%   \begingroup
%   \setheaderkey\flameHalpha{EXPTIME}\exptime
%   \gsetheaderkey\flameHalpha{OBSERVER}\observer
%   \meaning\exptime\par
%   \endgroup
%   \meaning\exptime\par
%   \meaning\observer
% \end{codestore}
% 
% \tsdemo[fits]{setfitskey}
% 
% \begin{changelog}[sectioncmd=\section*]
% \shortversion{v=1.0.0, date=06-04-2026, changes=Initial version.}
% \end{changelog}
% 
% \end{documentation}
% 
% \newpage
% 
% \section{Implementation}
% 
% \subsection{\TeX\ package}
% 
% \begin{implementation}
% 
%    \begin{macrocode}
%<*package-tex>
\ifdefined\loadheader\expandafter\endinput\fi

\def\FITSerr#1{\errmessage{Package fits Error: #1}}
\def\FITSwar#1{\message{Package fits Warning: #1}}

\unless\ifdefined\directlua
  \FITSerr{LuaTeX required}
\fi

\directlua{dofile('fits-astro.lua')}

\def\loadheader#1#2{
  \directlua{
    local key = '\csstring#1'
    if FITS_data[key] then
      tex.print('\luaescapestring{\FITSerr{FITS file '#2' already loaded}}')
    else
      FITS_data[key] = load_fits('#2')
    end
  }
}

\def\showheader#1{
  \directlua{
    show_fits(FITS_data['\csstring#1'])
  }
}

\def\headerkey#1#2{
  \directlua{
    if FITS_data['\csstring#1']['#2'] then
      get_header_key(FITS_data['\csstring#1'], '#2')
    else
      tex.print('\luaescapestring{\FITSwar{Header key '#2' not found}}')
    end
  }
}

\def\setheaderkey#1#2#3{
  \directlua{
    if FITS_data['\csstring#1']['#2'] then
      token.set_macro('\csstring#3', FITS_data['\csstring#1']['#2'])
    end
  }
}

\def\gsetheaderkey#1#2#3{
  \directlua{
    if FITS_data['\csstring#1']['#2'] then
      token.set_macro('\csstring#3', FITS_data['\csstring#1']['#2'], 'global')
    end
  }
}
%</package-tex>
%    \end{macrocode}
% \subsection{\LaTeX\ package}
% \makeatletter
% \c@CodelineNo=\z@
% \makeatother
%    \begin{macrocode}
%<*package-latex>
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{fits-astro}[2026-04-06 v1.0.0 Retrieving FITS file header information in LuaTeX]

\input{fits-astro.tex}
%</package-latex>
%    \end{macrocode}
% \end{implementation}
