!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
begin procedure l-ctrlima-init
  if .not.exist(ctrlima) then
     define structure ctrlima /global
     define character ctrlima%cubeid*128 /global
     define integer ctrlima%ipanel /global
     define double ctrlima%ixbox ctrlima%iybox /global
     define double ctrlima%nxpan ctrlima%nypan /global
     define structure ctrlima%xbox ctrlima%ybox /global
     define double ctrlima%xbox%n ctrlima%xbox%val ctrlima%xbox%ref ctrlima%xbox%inc /global
     define double ctrlima%ybox%n ctrlima%ybox%val ctrlima%ybox%ref ctrlima%ybox%inc /global
     define double ctrlima%size[2] ctrlima%center[2] /global
  endif
  let ctrlima%cubeid &1
  @ l-ctrl-master-register-panel "ctrlima" 1.081 1.428 0.085 0.995 ctrlima%ipanel
  @ l-ctrlima-init-grid-and-pos 40 40
  gtvl\clear
!  g\set angle second ! *** JP this one should be set by the default UNIT.
end procedure l-ctrlima-init
!
begin procedure l-ctrlima-init-grid-and-pos
  ! First grid
  let ctrlima%nxpan &1
  let ctrlima%nypan &2
  go\load 'ctrlima%cubeid' /axis l rel rad /into l
  go\load 'ctrlima%cubeid' /axis m rel rad /into m
  set\spatial /sampling * * * ! *** JP this command at this place is a source of trouble for the user
  go\topology 'ctrlima%cubeid'
  define double xmin ymin
  let xmin (0.5-l%ref)*l%inc+l%val
  let ymin (0.5-m%ref)*m%inc+m%val
  let ctrlima%xbox%n topology%x%pix%n/ctrlima%nxpan
  let ctrlima%ybox%n topology%y%pix%n/ctrlima%nypan
  let ctrlima%xbox%val l%val
  let ctrlima%ybox%val m%val
  let ctrlima%xbox%inc topology%x%pix%step*ctrlima%nxpan*l%inc
  let ctrlima%ybox%inc topology%y%pix%step*ctrlima%nypan*m%inc
  let ctrlima%xbox%ref 0.5+(ctrlima%xbox%val-xmin)/ctrlima%xbox%inc
  let ctrlima%ybox%ref 0.5+(ctrlima%ybox%val-ymin)/ctrlima%ybox%inc
  ! Second box initial size
  let ctrlima%size 'abs(ctrlima%xbox%inc)' 'abs(ctrlima%ybox%inc)'
  ! Third box initial position
  let ctrlima%center 0 0
  let ctrlima%ixbox ctrlima%xbox%ref
  let ctrlima%iybox ctrlima%ybox%ref
end procedure l-ctrlima-init-grid-and-pos
!
begin procedure l-ctrlima-new-position-index
  let ctrlima%ixbox ctrlima%ixbox+&1
  let ctrlima%iybox ctrlima%ixbox+&2
  let ctrlima%ixbox 1+mod(ctrlima%ixbox-1,ceiling(ctrlima%xbox%n))
  let ctrlima%iybox 1+mod(ctrlima%iybox-1,ceiling(ctrlima%ybox%n))
  ! *** JP why user_xmin and user_xmax are in rad while g\set angle is second?
  !exa user_xmin user_xmax
  !pause
  ! *** JP
  let ctrlima%center[1] (ctrlima%ixbox-ctrlima%xbox%ref)*ctrlima%xbox%inc+ctrlima%xbox%val
  let ctrlima%center[2] (ctrlima%iybox-ctrlima%ybox%ref)*ctrlima%ybox%inc+ctrlima%ybox%val
end procedure l-ctrlima-new-position-index
!
begin procedure l-ctrlima-new-position-offset
  @ l-ctrl-master-get-user-coord ctrlima%center
  let ctrlima%center ctrlima%center
  let ctrlima%ixbox ctrlima%xbox%ref+(ctrlima%center[1]-ctrlima%xbox%val)/ctrlima%xbox%inc
  let ctrlima%iybox ctrlima%ybox%ref+(ctrlima%center[2]-ctrlima%ybox%val)/ctrlima%ybox%inc
end procedure l-ctrlima-new-position-offset
!
begin procedure l-ctrlima-current-position
  define double xinc yinc
  let xinc 0.5*ctrlima%size[1]
  let yinc 0.5*ctrlima%size[2]
  ! *** JP on my screen the result does not look like a square when xinc = yinc!
  greg1\pencil 7 /dash 1
  greg1\draw relo ctrlima%center[1]-xinc ctrlima%center[2]-yinc /user
  greg1\draw line ctrlima%center[1]+xinc ctrlima%center[2]-yinc /user /clip
  greg1\draw line ctrlima%center[1]+xinc ctrlima%center[2]+yinc /user /clip
  greg1\draw line ctrlima%center[1]-xinc ctrlima%center[2]+yinc /user /clip
  greg1\draw line ctrlima%center[1]-xinc ctrlima%center[2]-yinc /user /clip
  greg1\pencil 0
  set\spatial /center 'ctrlima%center[1]' 'ctrlima%center[2]' rel rad -
          /size 'ctrlima%size[1]' 'ctrlima%size[2]' rad -
          /sampling 10 10 box
!!$  define double center[2] size[2]
!!$  let center ctrlima%center
!!$  let center center/sec
!!$  let size ctrlima%size
!!$  let size size/sec
!!$  say "Center : ("'center[1]'","'center[2]'")"
!!$  say "Size   : ("'size[1]'","'size[2]'")"
end procedure l-ctrlima-current-position
!
begin procedure l-ctrlima-plot
  go\load 'ctrlima%cubeid' /shape /into n
  @ l-ctrl-master-panel-update-bound 'ctrlima%ipanel' 'n%l/n%m'
  go\load 'ctrlima%cubeid' /first image /into myarea
  greg1\limits /rg
  go\load 'ctrlima%cubeid' /extrema /into ext
  greg2\plot /scaling log 0.01 'ext%max'
  greg1\box /unit deg
  @ l-ctrlima-current-position
  gtvl\change directory <greg
end procedure l-ctrlima-plot
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
begin procedure l-ctrl-slave-init
  @ l-spectrum
  @ l-spectrum-init
  @ l-ctrlima-init 13co10-s1-fts:stack,image
end procedure l-ctrl-slave-init
!
begin procedure l-ctrl-slave-action
  gtvl\clear
  @ l-ctrlima-plot
  @ l-spectrum-plot-all 13co10-s1-fts:cube
  greg1\pencil 1 /dash 1
  @ l-spectrum-over 13co10-s1-fts-comp-all:cube
  greg1\pencil 2 /dash 1
  @ l-spectrum-over 13co10-s1-fts-residuals:cube
  greg1\pencil 0
end procedure l-ctrl-slave-action
!
begin procedure l-ctrl-slave-loop
  define double step
  let step 0.5
  ! First global behavior, then position-dependent behavior
  if (cursor_code.eq."1") then
     @  l-ctrlima-new-position-index '-step' '-step'
  else if (cursor_code.eq."2") then
     @  l-ctrlima-new-position-index 0       '-step'
  else if (cursor_code.eq."3") then
     @  l-ctrlima-new-position-index '+step' '-step'
  else if (cursor_code.eq."4") then
     @  l-ctrlima-new-position-index '-step' 0
  else if (cursor_code.eq."5") then
     ! Does nothing
  else if (cursor_code.eq."6") then
     @  l-ctrlima-new-position-index 0       '+step'
  else if (cursor_code.eq."7") then
     @  l-ctrlima-new-position-index '-step' '+step'
  else if (cursor_code.eq."8") then
     @  l-ctrlima-new-position-index 0       '+step'
  else if (cursor_code.eq."9") then
     @  l-ctrlima-new-position-index '+step' '+step'
  else
     ! Position-dependent behavior
     if (ctrlmast%pan%icurr.eq.ctrlima%ipanel) then
        @ l-ctrlima-new-position-offset 'use_curs[1]' 'use_curs[2]'
     else if (ctrlmast%pan%icurr.eq.ctrlmast%pan%iout) then
        ! Any action that depends on being outside the registered panels?
     else
        message w l-ctrl-slave-loop "Should not be here: ctrlmast%pan%icurr = "'ctrlmast%pan%icurr'
     endif
  endif
end procedure l-ctrl-slave-loop
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
adm\import red
!
@ l-ctrl-master
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
