!
! @ uv_preview
!   Display line emission regions found by command
!     UV_PREVIEW
!
if .not.exist(spectrum) return  ! In case nothing done..
if (exist(p_browse)) then
  p_browse%last = -1
endif
!
gtvl\clear
greg2\projection /type none           ! Disable any projection
greg1\set system UNKNOWN            ! And no system too...
!
greg1\set box 4 28 5.5 17.5           ! Change the default box
define real margin
let margin 0.35                 ! Space for line identifications
!
greg1\pencil 0
defin integer nt nc
let nc size(spectrum,1)
let nt size(spectrum,2)-1
!
greg1\set blank spectrum%blank[1] spectrum%blank[2]
!
define double vup vlow cmargin
let cmargin max(1,min(40,0.025*nc))
vlow = (1.0-cmargin-spectrum%convert[1,1])*spectrum%velres+spectrum%veloff
vup  = (nc+cmargin-spectrum%convert[1,1])*spectrum%velres+spectrum%veloff
greg1\limits vlow vup 0 1
if exist(clipped) then
  if .not.all(spectrum.eq.clipped) then
    ! Now plot the Masked windows
    greg1\limits vlow vup 0 1+margin /var spectrum[1] clipped[1]
    greg1\histogram spectrum[1] clipped[1] /base 0 /fill 15
  endif
endif
!
if (nc.eq.1) then
  defi real a b
  let a max(spectrum[2],spectrum[nt])
  let b min(spectrum[2],spectrum[nt])
  if (a.gt.0.and.b.ge.0) then
    greg1\limi = = 0 '(1+margin)*a'
  else if (a.le.0.and.b.lt.0) then
    greg1\limi = = '(1+margin)*b' 0
  else
    greg1\limi = = '(1+margin)*b' '(1+margin)*a'
  endif
else
  greg1\limi = =  0 * /var spectrum[1] spectrum[2]
  greg1\limi = = < > /var spectrum[1] spectrum[2]
  greg1\limi = = < > /var spectrum[1] spectrum[nt]
  if (user_ymin.eq.user_ymax) then
    if (user_ymax.gt.0) then
      greg1\limi = = 0 '(1+margin)*user_ymax'
    else
      greg1\limi =  '(1+margin)*user_ymin' 0
    endif
  else
    greg1\limi = = = user_ymax+margin*(user_ymax-user_ymin)
  endif
endif
greg1\axis xu /label p
greg1\draw TEXT 0 2 "Velocity km/s" 5 0 /CHAR 8
greg1\axis yl
greg1\axis yr
!
if (nc.eq.1) then
  ! There is no Histogram for 1 channel, so trace a pseudo one
  ! with the channel width...
  define real xx[2] yy[2]
  let xx[i] (i-1.5)*spectrum%freqres+spectrum[1]
endif
! Loop on tapers
for i 2 to nt
  greg1\pencil /col i-1
  if nc.eq.1 then
    let yy spectrum[i]
    greg1\histogram xx yy
  else
    greg1\histogram spectrum[1] spectrum[i]
  endif
next
greg1\pencil /col 0
if exist(clipped) then
  for i 2 to nt
    greg1\histogram spectrum[1] clipped[i]
  next
endif
!
! Plot the Frequency axis
vlow = (-cmargin-spectrum%convert[1,1])*spectrum%freqres+spectrum%restfre
vup  = (nc+cmargin-spectrum%convert[1,1])*spectrum%freqres+spectrum%restfre
limit vlow vup = =
greg1\axis xl
greg1\label "Frequency (MHz)" /X
if (spectrum%unit.eq."Jy") then
  greg1\label "Flux density (Jy)" /Y
else
  greg1\label "Signal to Noise" /Y
endif
!
! Remember the frequency boundaries
define double preview%fmin preview%fmax /global
define integer preview%nfound /global
preview%fmin = min(vlow,vup)
preview%fmax = max(vlow,vup)
!
greg1\set blank 0 -1
!
