!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
! J.Pety 2018-2023
! This script works with the oct18 gildas version
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
begin procedure deconv-compute-diff
  define image clean 'name'".lmv-clean" read
  define image res   'name'".lmv-res"   read
  define image diff  'name'".lmv-diff"  real /like clean
  let diff% clean%
  let diff clean-res
  delete /var clean res diff
end procedure deconv-compute-diff
!
begin procedure deconv-noise
  let type lmv-clean
  @ p_noise.greg
end procedure deconv-noise
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
begin procedure deconv-wedge
  greg1\set expand 0.8
  greg2\wedge &1
  greg1\set expand 1.0
end procedure deconv-wedge
!
begin procedure deconv-plot
  @ deconv-noise
  @ deconv-compute-diff
  !
  @ window_tools.greg
  gtvl\clear
  let aspect_ratio yes
  let position 0.1 1.208 0.125 0.925
  let aspect 1.0
  let inter 0.05 0.05
  !
  let type lmv
  @ p_load.greg dimage
  greg2\rgdata dimage /variable
  if (size[1].eq.0) then
     greg1\limits /rgdata
  else if (size[1].gt.0) then
     greg1\limits size[1]/2 -size[1]/2 -size[1]/2 size[1]/2 seconds
  endif
  !
  @ window_xy 3 2 1
  let type beam
  @ p_load.greg dbeam
  @ p_scale.greg dbeam
  greg2\rgdata dbeam /variable
  greg2\plot /scaling linear true_scale[1] true_scale[2]
  greg1\set expand 0.8
  greg1\box n o /unit sec
  greg1\label "\gdDec" /y 2.5
  greg1\set expand 1.0
  @ deconv-wedge top
  greg1\draw text 0 3 "Dirty Beam" 5 0 /char 8
  !
  @ window_xy 3 2 2
  let type lmv
  @ p_load.greg dimage
  @ p_scale.greg dimage
  greg2\rgdata dimage /variable
  greg2\plot /scaling linear true_scale[1] true_scale[2]
  greg1\box n n /unit sec
  @ deconv-wedge top
  greg1\draw text 0 3 "Dirty Image (Jy/Beam)" 5 0 /char 8
  !
  @ window_xy 3 2 4
  let type lmv-res
  @ p_load.greg resi
  @ p_scale.greg resi
  greg2\rgdata resi /variable
  greg2\plot /scaling linear true_scale[1] true_scale[2]
  greg1\set expand 0.8
  greg1\box n o /unit sec
  greg1\label "\gdDec" /y 2.5
  greg1\set expand 1.0
  greg1\set axis expo z
  @ deconv-wedge bottom
  greg1\set axis noexpo z
  greg1\draw text 0 -3 "Residuals (Jy/Beam)" 5 0 /char 2
  !
  if (pro%narg.eq.2) then
     let scale &1 &2
  else
     let scale 0 0
  endif
  @ window_xy 3 2 5
  let type lmv-diff
  @ p_load.greg diff
  @ p_scale.greg diff
  greg2\rgdata diff /variable
  greg2\plot /scaling linear true_scale[1] true_scale[2]
  greg1\box n n /unit sec
  @ deconv-wedge bottom
  greg1\draw text 0 -3 "Clean components (Jy/Beam)" 5 0 /char 2
  !
  @ window_xy 3 2 6
  let type lmv-clean
  @ p_load.greg iclean
  @ p_scale.greg iclean
  greg2\rgdata iclean /variable
  greg2\plot /scaling linear true_scale[1] true_scale[2]
  @ p_beam.greg iclean
  greg1\box n n /unit sec
  @ deconv-wedge bottom
  define double max
  sic\compute max max iclean /blanking iclean%blank[1] iclean%blank[2]
  greg1\draw text 0 -3 "Clean Image (Jy/Beam)" 5 0 /char 2
  greg1\set expand 0.9
  define real y0
  let y0 -2.0
  greg1\draw text 0.5 y0-1.0 "\\iBeam:\\i " 3 0 /char 9
  greg1\draw text 0.5 y0-3.5 "\\iPeak:\\i " 3 0 /char 9
  greg1\draw text 0.5 y0-6.0 "\\iNoise:\\i " 3 0 /char 9
  greg1\draw text 0.5 y0-8.5 "\\iPeak SNR:\\i " 3 0 /char 9
  greg1\pencil 1
  greg1\draw text 0.5 y0-2.0 'nint(100*iclean%major/sec)/100'"`x"'nint(100*iclean%minor/sec)/100'"`@"'nint(iclean%pa/sec/3600)'"^" 3 0 /char 9
  greg1\draw text 0.5 y0-4.5 'nint(1000*max)/1000'" Jy/Beam" 3 0 /char 9
  greg1\draw text 0.5 y0-7.0 'nint(1e5*noise)/1e5'" Jy/Beam" 3 0 /char 9
  greg1\draw text 0.5 y0-9.5 'nint(max/noise)' 3 0 /char 9
  greg1\pencil 0
  greg1\set expand 1.0
  let scale 0
  !
  @ window_xy 3 2 3
  define image cct 'name'".cct" read
  if exist(comp) then
     delete /var comp cflux
  endif
  if exist(cflux) then
     delete /var cflux
  endif
  define integer ncomp nplanes
  let ncomp cct%dim[3]
  let nplanes cct%dim[2]
  define real comp['ncomp'] cflux['ncomp','cct%dim[2]'] /global
  define integer k
  let comp[i] i
  for j 1 to cct%dim[2]
     let cflux[1,j] cct[3,j,1]
     for i 2 to ncomp
       let k i-1
        let cflux[i,j] cflux[k,j]+cct[3,j,i]
     next
  next
  @ xy_limits comp cflux
  ! Plot
  if (nplanes.eq.1) then
     greg1\connect comp cflux
  else
     for i 1 to nplanes
        greg1\connect comp cflux[i]
        greg1\box n n
     next
  endif
  greg1\box n n
  ! Make axis and labels
  greg1\set expand 0.8
  greg1\axis yr /label o
  greg1\axis xu /label p
  greg1\set expand 1.0
  greg1\draw text 0 3 "Clean component number" 5 0 /char 8
  greg1\draw text 3 0 "Cumulated flux (Jy)" 5 90 /char 6
  !
  let aspect_ratio no
  let inter 0
  @ window_xy 1 1 1
  greg1\draw text -3.0 0 'name' 5 90 /char 4
  ! Clean up
  delete /var cct
end procedure deconv-plot
!
begin procedure deconv-single-plot
  define character*10 position  thiswedge
  define real thisnoise ytext
  define integer pos textpos
  let position &1
  if (position.eq."Top") then
     let pos 3
     let textpos 2
     let thisnoise mynoise%top
     let thiswedge "bottom"
     let ytext  -3
  else if (position.eq."Bottom") then
     let pos 0
     let textpos 8
     let thisnoise mynoise%bottom
     let thiswedge "top"
     let ytext  3
  else
     message e decov-single-plot "Unrecognized position "'position'
     return base
  endif
  !
  let type lmv
  @ p_load.greg dimage
  greg2\rgdata dimage /variable
  if (size[1].eq.0) then
     greg1\limits /rgdata
  else if (size[1].gt.0) then
     greg1\limits size[1]/2 -size[1]/2 -size[1]/2 size[1]/2 seconds
  endif
  !
  @ window_xy 3 2 'pos+1'
  let type lmv-res
  @ p_load.greg resi
  @ p_scale.greg resi
  greg2\rgdata resi /variable
  greg2\plot /scaling linear true_scale[1] true_scale[2]
  greg1\set expand 0.8
  greg1\box n o /unit sec
  greg1\label "\gdDec" /y 2.5
  greg1\set expand 1.0
  greg1\set axis expo z
  @ deconv-wedge 'thiswedge'
  greg1\set axis noexpo z
  greg1\draw text 0 'ytext' "Residuals (Jy/Beam)" 5 0 /char 'textpos'
  greg1\draw text -3 0 'name' 5 90 /char 4
  !
  @ window_xy 3 2 'pos+2'
  let type lmv-diff
  @ p_load.greg diff
  @ p_scale.greg diff
  greg2\rgdata diff /variable
  greg2\plot /scaling linear true_scale[1] true_scale[2]
  greg1\box n n /unit sec
  @ deconv-wedge 'thiswedge'
  greg1\draw text 0 'ytext' "Clean components (Jy/Beam)" 5 0 /char 'textpos'
  !
  @ window_xy 3 2 'pos+3'
  let type lmv-clean
  @ p_load.greg iclean
  @ p_scale.greg iclean
  greg2\rgdata iclean /variable
  greg2\plot /scaling linear true_scale[1] true_scale[2]
  @ p_beam.greg iclean
  greg1\box n n /unit sec
  @ deconv-wedge 'thiswedge'
  define double max
  sic\compute max max iclean
  greg1\draw text 0 'ytext' "Clean Image (Jy/Beam)" 5 0 /char 'textpos'
  greg1\set expand 0.9
  define real y0
  let y0 -2.0
  greg1\draw text 0.5 y0-1.0 "\\iBeam:\\i " 3 0 /char 9
  greg1\draw text 0.5 y0-3.5 "\\iPeak:\\i " 3 0 /char 9
  greg1\draw text 0.5 y0-6.0 "\\iNoise:\\i " 3 0 /char 9
  greg1\draw text 0.5 y0-8.5 "\\iPeak SNR:\\i " 3 0 /char 9
  greg1\pencil 1
  greg1\draw text 0.5 y0-2.0 'nint(100*iclean%major/sec)/100'"`x"'nint(100*iclean%minor/sec)/100'"`@"'nint(iclean%pa/sec/3600)'"^" 3 0 /char 9
  greg1\draw text 0.5 y0-4.5 'nint(1000*max)/1000'" Jy/Beam" 3 0 /char 9
  greg1\draw text 0.5 y0-7.0 'nint(1e5*thisnoise)/1e5'" Jy/Beam" 3 0 /char 9
  greg1\draw text 0.5 y0-9.5 'nint(max/thisnoise)' 3 0 /char 9
  greg1\pencil 0
  greg1\set expand 1.0
  let scale 0
  !
end procedure deconv-single-plot
!
begin procedure deconv-compare-plot
  define character*256 top bottom
  if (exist(mynoise)) delete /var mynoise
  define structure mynoise /global
  define real mynoise%top mynoise%bottom /global
  let top &1
  let bottom &2
  !
  let name 'top'
  @ deconv-noise
  let mynoise%top noise
  @ deconv-compute-diff
  !
  let name 'bottom'
  @ deconv-noise
  let mynoise%bottom noise
  @ deconv-compute-diff
  !
  @ window_tools.greg
  gtvl\clear
  let aspect_ratio yes
  let position 0.1 1.208 0.125 0.925
  let aspect 1.0
  let inter 0.05 0.05
  !
  let name 'top'
  @ deconv-single-plot Top
  let name 'bottom'
  @ deconv-single-plot Bottom
  !
  delete /var mynoise
end procedure deconv-compare-plot
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
