!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
! S.Guilloteau
!   Support for VIEW NOISE
!   As SHOW NOISE, but with a Single panel
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
if (mosaic) then
  say "W-NOISE,  Noise is NOT uniform in Mosaic case..."
endif
if .not.(exist(noise)) then
  define real noise /global
endif
!
define integer local_first local_last nplanes
if (first.eq.0) then
  let local_first 1
else
  let local_first first
endif
if (last.eq.0) then
  let local_last max(1,'ActiveVariable'%dim[3])
else
  let local_last last
endif
let nplanes local_last-local_first+1
!
! Compute 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] * real /global
let histo% 'ActiveVariable'%
define integer kplane
!
if ('ActiveVariable'%ndim.eq.2) then
  if pro%narg.ne.0
    let rms[1] 'pro%arg[1]'
  endif
  !
  if (rms[1].le.0) then
    statistic 'ActiveVariable' /noise rms
!    compute rms[1] rms 'ActiveVariable' /blanking  'ActiveVariable'%blank[1] 'ActiveVariable'%blank[2]
!    exa rms[1]
!    exa 'ActiveVariable'%noise
!    exa 'ActiveVariable'%rms
  endif
  let hist_min -2.5*rms[1]
  let hist_max +2.5*rms[1]
  compute histogram histogram 'ActiveVariable' hist_min hist_max -
  /blanking 'ActiveVariable'%blank[1] 'ActiveVariable'%blank[2]
  let histo[,1:2,1] histogram
	!
else if ('ActiveVariable'%ndim.eq.3) then
  if pro%narg.ne.0
    let rms 'pro%arg[1]'
  endif
  for iplane 1 to nplanes
    let kplane iplane+local_first-1
    if (rms[iplane].le.0) then
      statistic 'ActiveVariable' kplane /noise rms[iplane]
!      compute    rms[iplane] rms 'ActiveVariable'[kplane] /blanking  'ActiveVariable'%blank[1] 'ActiveVariable'%blank[2]
!      exa rms[iplane]
!      exa 'ActiveVariable'%noise
!      exa 'ActiveVariable'%rms
    endif
    let hist_min -2.5*rms[iplane]
    let hist_max +2.5*rms[iplane]
    compute histogram histogram 'ActiveVariable'[kplane] hist_min hist_max -
      /blanking 'ActiveVariable'%blank[1] 'ActiveVariable'%blank[2]
    let histo[,1:2,iplane] histogram
  next iplane
else
  message e noise "data number of dimensions must be 2 or 3, not "''ActiveVariable'%ndim'
  return base
endif
!
! Fit gaussians.
!
define function my_gaussian(x,a,b,c) a*exp(-0.5*((x-b)|c)^2)
if exist(noises)  delete /var noises
define real noises[nplanes] /global
!
define real xx[nbin] yy[nbin] xxm yym
for iplane 1 to nplanes
  let xx histo[,2,iplane]
  let yy histo[,1,iplane]
  compute xxm min xx
  let xxm -xxm/3
  compute yym max yy
  mfit yy=MY_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
    xxm = abs(mfit%par[3])
    yym = mfit%par[1]
    mfit yy=MY_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
dele /var xx yy xxm yym
!
delete /function MY_GAUSSIAN
define char*10 cmark
!
! Get median
define real max_noise min_noise
compute noise median noises
compute max_noise max noises
compute min_noise min noises
!
! Plot histograms and Gaussian fits.
if pro%narg.ne.0 then
  @ window_tools.greg
  gtvl\destroy all
  let aspect_ratio yes
  let aspect 1
  let fixed_trc yes
  @ plot_position
  !
  if (exist(noise_single)) then
    @ find_nx_ny 1
    @ window_xy 1 1 1 1
    @ xy_limits histo[,2,] histo[,1,]
    if pro%narg.ge.3
      limits &2 &3 = =
    endif
    for iplane 1 to nplanes
      greg1\histogram histo[,2,iplane] histo[,1,iplane]
      greg1\pencil 1
      greg1\connect histo[,2,iplane] histo[,3,iplane]
      greg1\pencil 0
    next
    greg1\box
    let cmark min_noise /format 1PG10.2
    greg1\draw text 1 -0.5 "Min "'cmark' 3 0 /char 7
    let cmark noise /format 1PG10.2
    greg1\draw text 0 -0.5 "Median "'cmark' 2 0 /char 8
    let cmark max_noise /format 1PG10.2
    greg1\draw text -1 -0.5 "Max "'cmark' 1 0 /char 9
  else
    @ find_nx_ny 'nplanes'
    greg1\set expand min(6.0/(nx_box+ny_box),1.)
    @ xy_limits histo[,2,] histo[,1,]
    if pro%narg.ge.3
      limits &2 &3 = =
    endif
    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
      let cmark noises[iplane] /format 1PG10.2
      greg1\draw text 1 -0.5 'cmark' 3 0 /char 7
      greg1\box n n
    next
    @ window_xy 'nx_box' 'ny_box' 1 1
    greg1\box
    greg1\set expand 1.
  endif
  @ header_position
  @ p_header 'ActiveVariable'
endif
!
say
say 'ActiveVariable'" Noise levels: Min " min_noise ", Median " noise ",  Max " max_noise  /format A 1PG10.2 A 1PG10.2 A 1PG10.2
say
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
