--
-- This is file `fits-astro.lua',
-- generated with the docstrip utility.
--
-- The original source files were:
--
-- fits-astro-en.dtx  (with options: `package-lua')
-- 
-- ┌─────────────────────────────────────────────────────────────┐
-- │                                                             │
-- │                  The “fits-astro” package                   │
-- │       Retrieving FITS file header information in LuaTeX     │
-- │                                                             │
-- │  Copyright (C) 2026 Valentin Dao                            │
-- │                                                             │
-- │  This file may be distributed and/or modified under the     │
-- │  conditions of the LaTeX Project Public License, either     │
-- │  version 1.3c 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                  │
-- │                                                             │
-- │  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 listed in the              │
-- │  'MANIFEST.md' file.                                        │
-- │                                                             │
-- │  The repository of this work can be found here:             │
-- │                                                             │
-- │    > https://github.com/TeXackers/fits-astro                │
-- │                                                             │
-- └─────────────────────────────────────────────────────────────┘
-- 
tex = tex or {}
texio = texio or {}
FITS_data = {}

function load_FITS_header(file)
  local file_path = kpse.find_file(file)
  if not file_path then
    tex.error("FITS file not found: " .. file)
  else
    local FITS_file = io.open(file_path, "rb")
    local header = {}
    repeat
      local header_line = FITS_file:read(80)
      if not header_line or #header_line < 80 then break end

      -- string
      local key, value = header_line:match("([%u%-%d_]+)%s*=%s*'([^']*)'")
      if not key then
        -- number or boolean
        key, value = header_line:match("([%u%-%d_]+)%s*=%s*([^/]+)")
      end

      if key and value then
        value = sanitize_FITS_key(value)
        if value:match("^%s*$") then
          header[key] = nil
        else
          header[key] = parse_FITS_key(key, value)
        end
      end
  until header_line:sub(1, 3) == "END"
  FITS_file:close()
  return header
  end
end

function parse_FITS_key(key, value)
  -- converts FITS standard status to a boolean
  if key == "SIMPLE" then
    if value == "T" then
      value = true
    else
      value = false
    end
    return value
  end
  -- converts numbers into proper integers
  local num = tonumber(value)
  if num then value = num end
  return value
end

function sanitize_FITS_key(value)
  value = value:match("^%s*(.-)%s*$") or value
  return value
end

function get_header_key(header, key)
  local value = header[key]
  if value then
    tex.print(value)
  end
end

function show_fits(header)
  texio.write_nl("FITS header information:")
  for key, value in pairs(header) do
    texio.write_nl(string.format("%s = %s", key, tostring(value)))
  end
end
-- 
--
-- End of file `fits-astro.lua'.
