@ gag_demo:demo-setup
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
begin data gag_scratch:flux-ref-0.dat
! Method    Support ellipse      Flux  Tolerance
! (Name) (Xc Yc Major Minor PA)  [Jy]     [%]
  HOGBOM  0  0   5.0   5.5   0   0.562     2
  CLARK   0  0   5.0   5.5   0   0.553     2
  SDI     0  0   5.0   5.5   0   0.522     2
  MULTI   0  0   5.0   5.5   0   0.556     2
  MRC     0  0   5.0   5.5   0   0.563     2
  MX      0  0   5.0   5.5   0   0.566     2
  UV_RESTORE 0 0 5.0   5.5   0   0.567     2
end data gag_scratch:flux-ref-0.dat
begin data gag_scratch:flux-ref-1.dat
! Method    Support ellipse      Flux  Tolerance
! (Name) (Xc Yc Major Minor PA)  [Jy]     [%]
  HOGBOM  0  0   5.0   5.5   0   0.530     5
  CLARK   0  0   5.0   5.5   0   0.530     5
  SDI     0  0   5.0   5.5   0   0.490     5
  MULTI   0  0   5.0   5.5   0   0.549     5
  MRC     0  0   5.0   5.5   0   0.570     5
  MX      0  0   5.0   5.5   0   0.530     5
  UV_RESTORE 0 0 5.0   5.5   0   0.530     5
end data gag_scratch:flux-ref-1.dat
!
begin procedure check-flux
  !---------------------------------------------------------------------
  ! Check if the flux of the current RG buffer matches the expected flux
  ! as found in the flux-reference table. Arguments:
  !   &1: method name (uppercase)
  !---------------------------------------------------------------------
  !
  if (pro%narg.ne.1) then
    message e check-flux "Missing method name"
    return error
  endif
  define char*12 action
  let action &1 /upper
  !
  ! Load the flux reference table
  define integer nmeth
  compute nmeth lines gag_scratch:flux-ref-0.dat /blank
  define char*10 mname[nmeth]
  define real xc yc majo mino pa refflux tol /like mname
  if map_robust.eq.0 then
    accept mname xc yc majo mino pa refflux tol /col gag_scratch:flux-ref-0.dat
  else
    accept mname xc yc majo mino pa refflux tol /col gag_scratch:flux-ref-1.dat
  endif
  !
  ! Find the corresponding line in the reference table
  define integer imeth
  let imeth 0
  for iline 1 to nmeth
    if mname[iline].eq.action then
      let imeth iline
      break
    endif
  next iline
  if imeth.eq.0 then
    message e check-flux "Method "'action'" not found in reference table"
    return error
  endif
  !
  ! Measure the flux in the reference ellipse support
  @ p_poly YES "ellipse" 'xc[imeth]' 'xc[imeth]' 'majo[imeth]' 'mino[imeth]' 'pa[imeth]'
  define real my_flux
  support poly /var
  flux support
  let my_flux flux%values[1]
  !
  ! Check and feedback
  if abs(my_flux-refflux[imeth]).gt.refflux[imeth]*tol[imeth]/100 then
    say 'ansi%red'
    message e check-flux "Unexpected flux for method "'mname[imeth]'":" -
     'my_flux' "Jy instead of" 'refflux[imeth]' "+-" 'tol[imeth]' "%"'ansi%none' -
     /format A F0.3 A F0.3 A F0.1 A
    return base
  else
    say 'ansi%green'
    message i check-flux "Measured flux (" 'my_flux' -
     "Jy) matches the expected flux for "'mname[imeth]'" within" -
     'tol[imeth]' "% tolerance"'ansi%none' /format A F0.3 A F0.1 A
  endif
  !
end procedure check-flux
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
say "Testing UV_MAP"
read uv gag_demo:demo-single-imager
if (pro%narg.eq.1) let map_robust &1
uv_map
let do_contour yes
show dirty 
!
! Imager does not need any specific initialization
!let niter 500
!let fres 0
!let ares 2e-3
let scale -0.015 0.060
let clean_ncycle 100
clean ?
!
define character*12 ACTIONS[6]
let actions HOGBOM CLARK SDI MULTI MRC MX
!
for i 1 to 6
  let method 'actions[i]' /upper
  SAY "Click in Graphic Window to test "'method'
  greg1\draw relo
  destroy all
  support gag_demo:demo-single.pol
  if method.eq."MX" then
    let clean_niter 500
    MX    /flux 0 1
    let clean_niter 0
  else if method.eq."SDI" then
    let clean_ares 2e-3
    SDI /PLOT 
    let clean_ares 0
  else 
    clean /flux 0 1 /plot 
  endif
  show clean 
  support gag_demo:demo-single.pol
  say " "
  stat clean
  say " " 
  @ check-flux 'method'
  say " "
next
!
SAY "Click in Graphic Window to test UV_RESTORE"
greg1\draw relo
destroy all
DO "uv_map" "UV_MAP with last imaging parameters"
DO "let method HOGBOM" "Select HOGBOM method" toto
DO "clean" "HOGBOM deconvolution" toto
DO "uv_restore" "Restore by subtraction in visibilities" toto
DO "show clean " "Display CLEAN image" toto
say " "
stat clean
@ check-flux UV_RESTORE
say " "
