!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! A copy of GO NOISE that computes the noise and the maximum signal per
! channel.
!
! Calling sequence:
! MAPPING> let name coucou
! MAPPING> @ snr-compute
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
begin procedure snr-compute
  let type lmv-clean
  if (mosaic) then
     say "W-NOISE,  Noise is NOT uniform in Mosaic case..."
  endif
  !
  parse /display 0 0
  define logical dodisplay
  let dodisplay pro%parse%display%narg.eq.0
  !
  @ p_load.greg aaa1
  !
  define integer myfirst mylast nplanes
  if (first.eq.0) then
     let myfirst 1
  else
     let myfirst first
  endif
  if (last.eq.0) then
     let mylast max(1,aaa1%dim[3])
  else
     let mylast last
  endif
  let nplanes mylast-myfirst+1
  !
  if exist(noises) delete /var noises
  define real noises[nplanes] /global
  if exist(maxs) delete /var maxs
  define real maxs[nplanes] /global
  if exist(snrs) delete /var snrs
  define real snrs[nplanes] /global
  !
  ! Compute maxs and histograms
  define integer nbin
  let nbin 50
  define real rms[nplanes] hist_min hist_max
  if exist(histo) then
     delete /var histo
  endif
  define real histogram[nbin,2]
  define image histo[nbin,3,nplanes] 'name'".noi" real /global
  let histo% aaa1%
  define integer kplane
  if (aaa1%ndim.eq.2) then
     sic\compute maxs[1] max aaa1 /blanking  aaa1%blank[1] aaa1%blank[2]
     sic\compute rms[1] rms aaa1 /blanking  aaa1%blank[1] aaa1%blank[2]
     let hist_min -2.5*rms[1]
     let hist_max +2.5*rms[1]
     sic\compute histogram histogram aaa1 hist_min hist_max /blanking aaa1%blank[1] aaa1%blank[2]
     let histo[,1:2,1] histogram
  else if (aaa1%ndim.eq.3) then
     for iplane 1 to nplanes
        let kplane iplane+myfirst-1
        sic\compute maxs[iplane] max aaa1[kplane] /blanking  aaa1%blank[1] aaa1%blank[2]
        sic\compute rms[iplane] rms aaa1[kplane] /blanking  aaa1%blank[1] aaa1%blank[2]
        let hist_min -2.5*rms[iplane]
        let hist_max +2.5*rms[iplane]
        sic\compute histogram histogram aaa1[kplane] hist_min hist_max -
          /blanking aaa1%blank[1] aaa1%blank[2]
        let histo[,1:2,iplane] histogram
     next iplane
  else
     message e noise "data number of dimensions must be 2 or 3, not "'aaa1%ndim'
     return base
  endif
  !
  ! Fit gaussians
  define function gaussian(x,a,b,c) a*exp(-0.5*((x-b)|c)^2)
  define real xx[nbin] yy[nbin] xxm yym
  for iplane 1 to nplanes
     let xx histo[,2,iplane]
     let yy histo[,1,iplane]
     sic\compute xxm min xx
     let xxm -xxm/3
     sic\compute yym max yy
     mfit yy=GAUSSIAN(xx,&a,&b,&c) /start yym 0 xxm /meth slatec /quiet -
       /step yym/20 xxm/10 xxm/10
     if (mfit%par[3].lt.0) then
        let xxm abs(mfit%par[3])
        let yym mfit%par[1]
        mfit yy=GAUSSIAN(xx,&a,&b,&c) /start yym 0 xxm /meth slatec /quiet -
                                      /step yym/20 xxm/10 xxm/10
     endif
     let histo[,3,iplane] mfit%fit
     let noises[iplane] mfit%par[3]
  next iplane
  delete /var xx yy xxm yym
  delete /function GAUSSIAN
  !
  ! Plot histograms and Gaussian fits
  if (dodisplay) then
     @ window_tools.greg
     gtvl\destroy all
     gtvl\clear
     let aspect_ratio yes
     let aspect 1
     let fixed_trc yes
     @ plot_position
     @ find_nx_ny 'nplanes'
     @ xy_limits histo[,2,] histo[,1,]
     for iplane 1 to nplanes
        @ window_xy 'nx_box' 'ny_box' 'iplane'
        greg1\histogram histo[,2,iplane] histo[,1,iplane]
        greg1\pencil 1
        greg1\connect histo[,2,iplane] histo[,3,iplane]
        greg1\pencil 0
        greg1\box n n
     next
     @ window_xy 'nx_box' 'ny_box' 1 1
     greg1\box
     @ header_position
     @ p_header.greg aaa1
  endif
  !
  ! Write output files
  sic output 'name'".dat"
     for iplane 1 to nplanes
        say iplane maxs[iplane] noises[iplane] /format i10 e14.4 e14.4
     next iplane
  sic output
end procedure snr-compute
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! A set of procedures the analyzes the previously computed maximum
! brightnesses and noises.
!
! Calling sequence:
! MAPPING> let name coucou
! MAPPING> @ snr-plot
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
begin procedure snr-init
  if .not.(exist(stat&1)) then
     define structure stat&1 /global
     define integer stat&1%nc /global
     define structure stat&1%noi stat&1%max stat&1%snr /global
     define double stat&1%noi%min stat&1%noi%max stat&1%noi%med stat&1%noi%mad /global
     define double stat&1%max%min stat&1%max%max stat&1%max%med stat&1%max%mad /global
     define double stat&1%snr%min stat&1%snr%max stat&1%snr%med stat&1%snr%mad /global
  endif
end procedure snr-init
!
begin procedure snr-stat
  sic\compute stat&1%&2%min min stat&1%&2%val /blank 0 0
  sic\compute stat&1%&2%max max stat&1%&2%val /blank 0 0
  sic\compute stat&1%&2%med med stat&1%&2%val /blank 0 0
  sic\compute stat&1%&2%mad rms stat&1%&2%val /blank 0 0
  if exist(stat&1%&2%cdf) delete /var stat&1%&2%cdf
  define double stat&1%&2%cdf[stat&1%nc] /global
  let stat&1%&2%cdf stat&1%&2%val
  sort stat&1%&2%cdf stat&1%&2%cdf
!  let stat&1%&2%cdf stat&1%&2%cdf[stat&1%nc]-stat&1%&2%cdf
end procedure snr-stat
!
begin procedure snr-get-data
  @ snr-init &1
  sic\compute stat&1%nc line "&2.dat"
  if exist(stat&1%ichan) delete /var stat&1%ichan
  define integer stat&1%ichan[stat&1%nc] /global
  if exist(stat&1%noi%val) delete /var stat&1%noi%val
  define double stat&1%noi%val[stat&1%nc] /global
  if exist(stat&1%max%val) delete /var stat&1%max%val
  define double stat&1%max%val[stat&1%nc] /global
  if exist(stat&1%snr%val) delete /var stat&1%snr%val
  define double stat&1%snr%val[stat&1%nc] /global
  accept stat&1%ichan stat&1%max%val stat&1%noi%val /column "&2.dat"
  !
  let stat&1%snr%val stat&1%max%val/stat&1%noi%val
  !
  @ snr-stat &1 noi
  @ snr-stat &1 max
  @ snr-stat &1 snr
  exa stat&1%
end procedure snr-get-data
!
begin procedure snr-plot-data
  greg1\set blank 0 0
  @ window_xy 3 2 &1
  greg1\limits * * * * /var stat1%ichan stat1%&2%val /ylog
  greg1\limits < > < > /var stat2%ichan stat2%&2%val /ylog
  greg1\box
  greg1\label "Channel" /x
  greg1\draw text 0 0.5 "&3" 5 0 /char 8
  parse /reverse 0 0
  if (pro%parse%reverse%narg.eq.0) then
     greg1\pencil 1
     greg1\connect stat2%ichan stat2%&2%val
     greg1\pencil 0
     greg1\connect stat1%ichan stat1%&2%val
  else
     greg1\pencil 0
     greg1\connect stat1%ichan stat1%&2%val
     greg1\pencil 1
     greg1\connect stat2%ichan stat2%&2%val
     greg1\pencil 0
  endif
end procedure snr-plot-data
!
begin procedure snr-plot-cdf
  @ window_xy 3 2 &1
  greg1\limits * * * * /var stat1%&2%cdf stat1%ichan /xlog
  greg1\limits < > < > /var stat2%&2%cdf stat2%ichan /xlog
  greg1\box
  greg1\label "&3" /x
  parse /reverse 0 0
  if (pro%parse%reverse%narg.eq.0) then
     greg1\pencil 1
     greg1\connect stat2%&2%cdf stat2%ichan
     greg1\pencil 0
     greg1\connect stat1%&2%cdf stat1%ichan
  else
     greg1\pencil 0
     greg1\connect stat1%&2%cdf stat1%ichan
     greg1\pencil 1
     greg1\connect stat2%&2%cdf stat2%ichan
     greg1\pencil 0
  endif
end procedure snr-plot-cdf
!
begin procedure snr-plot
  @ snr-get-data 1 'name'
  @ snr-get-data 2 'name'"-selfcal"
  !
  @ window_tools.greg
  let aspect_ratio no
  let inter 0.25 0.3
  !
  gtvl\clear
  !
  @ window_xy 1 1 1
  greg1\set expand 1.2
  greg1\draw text 0 1 'name'"."'type' 9 0 /char 7
  greg1\pencil /w 3
  greg1\draw text -7 1 "Before selfcal" 7 0 /char 9
  greg1\pencil 1 /w 3
  greg1\draw text  0 1 "After selfcal" 7 0 /char 9
  greg1\pencil 1 /w 1
  greg1\pencil 0 /w 1
  greg1\set expand 1.0
  !
  @ snr-plot-data 1 noi "Noise"
  greg1\label "Measurement [unit]" /y 4.0
  @ snr-plot-data 2 max "Maximum" /reverse
  @ snr-plot-data 3 snr "SNR"     /reverse
  !
  let stat1%ichan 100*stat1%ichan/stat1%nc
  let stat2%ichan 100*stat2%ichan/stat2%nc
  @ snr-plot-cdf 4 noi "Noise" /reverse
  greg1\label "Occurence [%]" /y 4.0
  @ snr-plot-cdf 5 max "Maximum"
  @ snr-plot-cdf 6 snr "SNR"
end procedure snr-plot
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
