begin data gag_scratch:add.map
  symb &1 "'&1' &2"
end data gag_scratch:add.map
!
if (.not.exist(myflux)) then
  if .not.exist(UV) then
    message E SCALE_FLUX "No UV data loaded, please read one"
    return error
  endif
  !!if .not.exist(MODEL) then
  !!  message E SCALE_FLUX "No MODEL loaded, please read or compute one"
  !!  return error
  !! endif
  !! read model &2
  defin integer nvisi nvals
  let nvisi uv%nvisi
  let nvals 2*uv%nchan
  define structure myflux% /global
  define double myflux%visi[nvisi,nvals]  /Global
  define real myflux%weight /like myflux%visi /Global
  define real myflux%work myflux%uv2 /like myflux%visi /Global
  define real myflux%uvmin myflux%uvmax /Global
  !
  define integer re im we k
  k = 0
  for i 1 to uv%nchan
    k = k+1
    re = 5+3*i
    im = re+1
    we = im+1
    myflux%visi[k] = uv[re,]
    myflux%weight[k] = uv[we,]
    let myflux%uv2[k] uv[1,]^2+uv[2,]^2
    k = k+1
    myflux%visi[k] = uv[im,]
    myflux%weight[k] = uv[we,]
    let myflux%uv2[k] uv[1,]^2+uv[2,]^2
  next
  !
  calibrate\scale_flux FIND &1 &2 &3
endif
myflux%uvmax = (&3+0)^2
myflux%uvmin = (&2+0)^2
if (pro%narg.ne.0) then
  calibrate\scale_flux FIND &1 &2 &3
endif
if myflux%uvmax.eq.0 let myflux%uvmax = 1e38
!
symb var s_flux[1]
let d_flux 0.1*s_flux /where d_flux.EQ.0
symb step d_flux[1]
for i 2 to size(s_flux)
  @ gag_scratch:add.map var s_flux['i']
  @ gag_scratch:add.map step d_flux['i']
next
calibrate\scale_flux list
!
begin procedure my_proc
  calibrate\scale_flux APPLY MyVar
  define integer re im we k
  k = 0
  for i 1 to uv%nchan
    k = k+1
    re = 5+3*i
    im = re+1
    we = im+1
    myflux%work[k] = MyVar[re,]
    k = k+1
    myflux%work[k] = MyVar[im,]
  next
  let adj%res Myflux%visi-Myflux%work /where myflux%uv2.gt.myflux%uvmin.and.myflux%uv2.le.myflux%uvmax
end procedure my_proc
!
adjust\adjust myflux%visi "@ my_proc" /start /par 'var' /root adj /step 'step' /meth powell /weight myflux%weight
!
let s_flux adj%par
let d_flux adj%errors
calibrate\scale_flux list
!
