convview.f90 Source File


Source Code

! kippy - Kippenhahn diagram viewer
! Copyright (C) 2026 the kippy authors (see AUTHORS)
!
! This file is part of kippy.
!
! kippy is free software: you can redistribute it and/or modify
! it under the terms of the GNU Lesser General Public License as
! published by the Free Software Foundation, either version 3 of the
! License, or (at your option) any later version.
!
! kippy is distributed in the hope that it will be useful, but WITHOUT
! ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
! FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
! License for more details.
!
! You should have received a copy of the GNU Lesser General Public
! License along with kippy. If not, see <https://www.gnu.org/licenses/>.

! convview.f90 -- interactive Kippenhahn viewer
program convview
   !! Interactive Kippenhahn viewer.
   !!
   !! Usage: `convview [path]`, default `convdata.cnv` (docs/usage.md). Reads commands from stdin,
   !! re-renders after each (docs/commands.md): `/xw` window when `$DISPLAY` is set, else PNG
   !! snapshots.

   use kipp
   implicit none (type, external)

   character(len=512) :: line
   character(len=64)  :: cmd, a2, a3
   character(len=256) :: a1   ! carries save paths, giza's filename cap
   character(len=256) :: fname
   character(len=8)   :: envbuf
   integer :: ios, envlen, envstat
   real(8) :: v1, v2
   logical :: scripted, done, ok

   ! KIPP_SCRIPT enables the hidden `key` command so a script (e.g. a
   ! fifo) can drive cursor-mode actions through the REPL, and skips the
   ! cursor-mode auto-enter so the REPL keeps reading while the /xw
   ! window stays live.  injection guard: off unless explicitly set
   call get_environment_variable('KIPP_SCRIPT', envbuf, envlen, envstat)
   scripted = (envstat == 0 .and. envlen > 0)

   if (command_argument_count() >= 1) then
      call get_command_argument(1, fname)
   else
      fname = 'convdata.cnv'
   end if

   print '(a)', '[convview] loading '//trim(fname)//' ...'
   call kipp_load(trim(fname))
   print '(a,i0,a)', '[convview] ', nmodels, ' models loaded'
   call kipp_choose_device()
   call kipp_render()
   if (.not. scripted) call kipp_interact()   ! cursor mode first when a window is up
   call help()

   do
      write (*, '(a)', advance='no') 'kipp> '
      if (.not. scripted) then
         select case (kipp_wait_input())
         case (1)                              ! clicked the plot window
            print '(a)', 'cursor'
            call kipp_interact()
            cycle
         case (-1)                             ! closed the plot window
            print '(a)', 'quit'
            exit
         end select
      end if
      read (*, '(a)', iostat=ios) line
      if (ios /= 0) exit                       ! EOF / Ctrl-D
      call tokens(line, cmd, a1, a2, a3)
      if (len_trim(cmd) == 0) cycle

      select case (trim(cmd))
      case ('quit', 'q', 'exit')
         exit
      case ('help', 'h', '?')
         call help()
      case ('redraw', 'r')
         call kipp_render()
      case ('cursor', 'i', 'interact')
         call kipp_interact()
      case ('reset')
         st%xauto = .true.; st%yauto = .true.
         call kipp_autoscale(); call kipp_render()
      case ('xlim')
         if (rd2(a1, a2, v1, v2)) then
            st%xmin = v1; st%xmax = v2; st%xauto = .false.; call kipp_render()
         end if
      case ('ylim')
         if (rd2(a1, a2, v1, v2)) then
            st%ymin = v1; st%ymax = v2; st%yauto = .false.; call kipp_render()
         end if
      case ('xscale')
         st%xlog = (trim(a1) == 'log')
         call kipp_autoscale(); call kipp_render()
      case ('yscale')
         st%ylog = (trim(a1) == 'log')
         call kipp_autoscale(); call kipp_render()
      case ('xaxis')
         if (trim(a1) == 'time' .or. trim(a1) == 'model') then
            st%xaxis = a1; st%xauto = .true.
            call kipp_rebuild(); call kipp_autoscale(); call kipp_render()
         else
            print *, 'xaxis: time|model'
         end if
      case ('yaxis')
         if (trim(a1) == 'mass' .or. trim(a1) == 'radius') then
            st%yaxis = a1; st%yauto = .true.
            call kipp_rebuild(); call kipp_autoscale(); call kipp_render()
         else
            print *, 'yaxis: mass|radius'
         end if
      case ('units')
         call kipp_set_unit(trim(a1), ok)
         if (ok) then
            call kipp_render()
         else
            print *, 'units: auto|solar|cgs|s|min|hr|d|yr|kyr|Myr|Gyr|g|kg|Msun|cm|m|km|Rsun'
         end if
      case ('offset')
         st%xoffset = (trim(a1) == 'on')
         call kipp_render()
      case ('color', 'colour')
         if (len_trim(a1) == 0) then
            call kipp_list_fields()
         else
            call kipp_set_cfield(trim(a1), ok)
            if (ok) call kipp_render()
         end if
      case ('cmap', 'colormap', 'colourmap')
         if (len_trim(a1) == 0) then
            call kipp_list_cmaps()
         else
            call kipp_set_cmap(trim(a1), ok)
            if (ok) then
               call kipp_render()
            else
               print *, 'unknown colormap ', trim(a1), '   (type cmap for the list)'
            end if
         end if
      case ('models')
         st%showmodels = (trim(a1) == 'on')
         call kipp_render()
      case ('grid')
         st%showgrid = (trim(a1) == 'on')
         call kipp_render()
      case ('fields', 'columns')
         call kipp_list_fields()
      case ('save')
         if (len_trim(a1) > 0) then
            call kipp_save(trim(a1))
         else
            print *, 'save <file.png|file.pdf>'
         end if
      case ('key')
         ! scripted cursor-mode action; hidden from help, accepted only
         ! when KIPP_SCRIPT is set (position defaults to the view centre)
         if (.not. scripted) then
            print *, 'unknown command: ', trim(cmd), '   (try help)'
         else if (len_trim(a1) == 0) then
            print *, 'key <char> [x y]'
         else
            if (len_trim(a2) > 0 .or. len_trim(a3) > 0) then
               if (.not. rd2(a2, a3, v1, v2)) cycle
            else
               call kipp_window_center(v1, v2)
            end if
            call kipp_action(a1(1:1), v1, v2, done)
         end if
      case default
         print *, 'unknown command: ', trim(cmd), '   (try help)'
      end select
   end do

   call kipp_close()
   print '(a)', '[convview] bye'

contains

   subroutine tokens(s, t0, t1, t2, t3)
      !! Split line into up to four whitespace-separated tokens.
      !!
      !! Missing tokens blank, tokens past fourth dropped.
      ! Arguments
      character(len=*), intent(in)  :: s
         !! Input line.
      character(len=*), intent(out) :: t0
         !! First token (command).
      character(len=*), intent(out) :: t1
         !! Second token.
      character(len=*), intent(out) :: t2
         !! Third token.
      character(len=*), intent(out) :: t3
         !! Fourth token.
      character(len=len(s)) :: buf
      t0 = ''; t1 = ''; t2 = ''; t3 = ''
      buf = adjustl(s)
      call pop(buf, t0)
      call pop(buf, t1)
      call pop(buf, t2)
      call pop(buf, t3)
   end subroutine tokens

   subroutine pop(buf, tok)
      !! Remove first whitespace-separated token from `buf`.
      ! Arguments
      character(len=*), intent(inout) :: buf
         !! Text left to split.
      character(len=*), intent(out)   :: tok
         !! Removed token, blank when `buf` is empty.
      integer :: p
      buf = adjustl(buf)
      p = index(buf, ' ')
      if (p <= 1) then
         tok = trim(buf); buf = ''
      else
         tok = buf(1:p - 1); buf = buf(p:)
      end if
   end subroutine pop

   logical function rd2(a, b, x, y)
      !! Read one number from each of two tokens.
      !!
      !! False, w/ `need two numbers` printed, if either fails to parse.
      ! Arguments
      character(len=*), intent(in) :: a
         !! First token.
      character(len=*), intent(in) :: b
         !! Second token.
      real(8), intent(out) :: x
         !! Number read from `a`.
      real(8), intent(out) :: y
         !! Number read from `b`.
      integer :: e1, e2
      rd2 = .false.
      read (a, *, iostat=e1) x
      read (b, *, iostat=e2) y
      if (e1 /= 0 .or. e2 /= 0) then
         print *, 'need two numbers'
         return
      end if
      rd2 = .true.
   end function rd2

   subroutine help()
      !! Print the command summary.
      print '(a)', 'commands:'
      print '(a)', '  xlim <min> <max>      ylim <min> <max>'
      print '(a)', '  xscale lin|log        yscale lin|log'
      print '(a)', '  xaxis time|model      yaxis mass|radius'
      print '(a)', '  units <unit>          axis unit: auto | s min hr d yr kyr Myr Gyr | g kg Msun | cm m km Rsun | solar | cgs'
      print '(a)', '  offset on|off         matplotlib-style time offset (+3.81233 Gyr), on by default'
      print '(a)', '  color <name|number>   colour field, -number for the other lin/log scale (color alone lists them)'
      print '(a)', '  cmap <name|number>    colour-field colormap, _r reverses (cmap alone lists them)'
      print '(a)', '  models on|off         grid on|off   (cell and time boundaries)'
      print '(a)', '  cursor (i)            interactive zoom/pan (or click the plot window)'
      print '(a)', '  reset   redraw   save <file.png|file.pdf>   help   quit'
   end subroutine help

end program convview