!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
begin procedure l-spectrum-init
  if (.not.exist(spectrum)) then
     define structure spectrum /global
     define character spectrum%cubeid*256 /global
     define integer spectrum%nx spectrum%ny /global
     define real spectrum%noise spectrum%cutoff /global
     define real spectrum%expand /global
     define function asinh(x,a) a*log10(((x/a)+sqrt((x/a)^2+1)))
  endif
  let spectrum%nx 1
  let spectrum%ny 1
  let spectrum%expand 1.0
  let spectrum%noise 0.1
  let spectrum%cutoff 4*spectrum%noise
  ! The next ones are correct defaults when used standalone
  gtvl\clear
  set\axset /ignoredegenera off ! *** JP Should only ignore non LMC axes
end procedure l-spectrum-init
!
begin procedure l-spectrum-plot-layout
  go\topology 'spectrum%cubeid'
  let spectrum%nx ceiling(max(topology%x%n,1.0))
  let spectrum%ny ceiling(max(topology%y%n,1.0))
  set\panel /default /position plot /inter 0 0 /aspect * /fixed trc /nxy spectrum%nx spectrum%ny
  let spectrum%expand min(9.0/(spectrum%nx+spectrum%ny),1.) ! Define character size
end procedure l-spectrum-plot-layout
!
begin procedure l-spectrum-prepare-marking
  if exist(spectrum%xmark) delete /var spectrum%xmark
  define character*12 spectrum%xmark /global
  if exist(spectrum%ymark) delete /var spectrum%ymark
  define character*12 spectrum%ymark /global
end procedure l-spectrum-prepare-marking
!
begin procedure l-spectrum-plot-box
  set\panel &1 &2
  greg1\set expand 'spectrum%expand'
  if ((&1.eq.1).and.(&2.eq.1)) then
     greg1\box
  else if (&1.eq.1) then
     greg1\box n o
  else if (&2.eq.1) then
     greg1\box p n
  else
     greg1\box n n
  endif
end procedure l-spectrum-plot-box
!
begin procedure l-spectrum-mark-box
  greg1\set expand '2*spectrum%expand'
  if ((&1.eq.1).and.(&2.eq.1)) then
     let spectrum%xmark '0.1*nint(10*&3)'"'"
     let spectrum%ymark '0.1*nint(10*&4)'"'"
     greg1\draw text -1.5 -1.5 "\gdx = " 4 /char 2
     greg1\draw text 0 -1.5 'spectrum%xmark' 5 /char 2
     greg1\draw text -1.5 0 "\gdy = "'spectrum%ymark' 4 0 /char 4
  else if (&1.eq.1) then
     let spectrum%ymark '0.1*nint(10*&4)'"'"
     greg1\set expand '2*spectrum%expand'
     greg1\draw text -1.5 0 'spectrum%ymark' 4 0 /char 4
  else if (&2.eq.1) then
     let spectrum%xmark '0.1*nint(10*&3)'"'"
     greg1\set expand '2*spectrum%expand'
     greg1\draw text 0 -1.5 'spectrum%xmark' 5 /char 2
  else
     ! Does nothing
  endif
end procedure l-spectrum-mark-box
!
begin procedure l-spectrum-load-pixel
  ! *** JP: This one should be done directly inside the LOAD command!
!  say "(ix,iy) = (&1,&2)"
!!$  define integer ix iy
!!$  let ix &1
!!$  let iy &2
  if exist(x) delete /var x
  define double x /like &3%coord /global
  let x &3%coord
  on error "return"
  go\load 'spectrum%cubeid' /pixel &1 &2 rel /into y global yes
!  load 'spectrum%cubeid' /entry 'ix+(iy-1)*n%l' spectrum /into y global yes
!  load 'spectrum%cubeid' /pixel ix iy abs pix /into brightness
  on error "pause"
!  let y y/spectrum%cutoff
!  let y spectrum%cutoff*log10((y+sqrt(y^2+1)))
end procedure l-spectrum-load-pixel
!
begin procedure l-spectrum-plot-pixel
  @ l-spectrum-load-pixel &1 &2 velo
  greg1\histogram /base 0 /fill 5
  greg1\histogram
end procedure l-spectrum-plot-pixel
!
begin procedure l-spectrum-plot-level
  greg1\pencil 1 /dash 2
  greg1\draw relo user_xmin +spectrum%noise /user
  greg1\draw line user_xmax +spectrum%noise /user
  greg1\draw relo user_xmin -spectrum%noise /user
  greg1\draw line user_xmax -spectrum%noise /user
  greg1\pencil 0
end procedure l-spectrum-plot-level
!
begin procedure l-spectrum-plot-all
  let spectrum%cubeid 'cubego%cubes%id[1]'
  go\load 'spectrum%cubeid' /extrema /into ext
  go\load 'spectrum%cubeid' /shape /into n global yes
  go\load 'spectrum%cubeid' /axis velo abs /into velo global yes
  !
  @ l-spectrum-plot-layout
  @ l-spectrum-prepare-marking
  !
  greg1\limits * * 'ext%min%val' 'ext%max%val' /var velo%coord velo%coord /margin 0 5
!  limits -5 18 asinh(-0.2,spectrum%cutoff) asinh(40,spectrum%cutoff) /margin 0 5
  !
  define double xcurr ycurr
  for ipx 1 to spectrum%nx
     let xcurr topology%x%first+(ipx-1)*topology%x%step
     for ipy 1 to spectrum%ny
        let ycurr topology%y%first+(ipy-1)*topology%y%step
        @ l-spectrum-plot-box 'ipx' 'ipy'
        @ l-spectrum-mark-box 'ipx' 'ipy' 'xcurr' 'ycurr'
        @ l-spectrum-plot-pixel 'xcurr' 'ycurr'
!        @ l-spectrum-plot-level
     next ipy
  next ipx
end procedure l-spectrum-plot-all
!
begin procedure l-spectrum-over
  let spectrum%cubeid 'cubego%cubes%id[1]'
  define double xcurr ycurr
  for ipx 1 to spectrum%nx
     let xcurr topology%x%first+(ipx-1)*topology%x%step
     for ipy 1 to spectrum%ny
        let ycurr topology%y%first+(ipy-1)*topology%y%step
        set\panel 'ipx' 'ipy'
        @ l-spectrum-plot-pixel 'xcurr' 'ycurr'
     next ipy
  next ipx
end procedure l-spectrum-over
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
