!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
if (mosaic) then
   say "W-NOISE,  Noise is NOT uniform in Mosaic case..." 
endif
!
if .not.(exist(noise)) then
   define real noise /global
endif
!
@ p_load.greg aaa
!
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,aaa%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] 'sic%directory'"/"'name'".noi" real /global
let histo% aaa%
define integer kplane 
!
if (aaa%ndim.eq.2) then
   if pro%narg.eq.3
     let rms[1] 'pro%arg[3]'/2.5 
   else
     sic\compute    rms[1] rms aaa /blanking  aaa%blank[1] aaa%blank[2]
   endif
   let hist_min -2.5*rms[1]
   let hist_max +2.5*rms[1]
   sic\compute histogram histogram aaa hist_min hist_max -
     /blanking aaa%blank[1] aaa%blank[2]
   let histo[,1:2,1] histogram
	
else if (aaa%ndim.eq.3) then
   for iplane 1 to nplanes
      let kplane iplane+local_first-1
      if pro%narg.eq.3
        let rms[iplane] 'pro%arg[3]'/2.5
      else
        sic\compute    rms[iplane] rms aaa[kplane] /blanking  aaa%blank[1] aaa%blank[2]
      endif
      let hist_min -2.5*rms[iplane]
      let hist_max +2.5*rms[iplane]
      sic\compute histogram histogram aaa[kplane] hist_min hist_max -
        /blanking aaa%blank[1] aaa%blank[2]
      let histo[,1:2,iplane] histogram
   next iplane
else
   message e noise "data number of dimensions must be 2 or 3, not "'aaa%ndim'
   return base
endif
!
! Fit gaussians.
!
define function gaussian(x,a,b,c) a*exp(-0.5*((x-b)|c)^2)
define real noises[nplanes]
!
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
    xxm = abs(mfit%par[3]) 
    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
dele /var xx yy xxm yym
!
delete /function GAUSSIAN
!
! Plot histograms and Gaussian fits.
!
if pro%narg.ne.1 then
  @ window_tools.greg
  gtvl\destroy all
  gtvl\clear alpha
  let aspect_ratio yes
  let aspect 1
  let fixed_trc yes
  @ plot_position
  @ find_nx_ny 'nplanes'
  @ xy_limits histo[,2,] histo[,1,]
  if pro%narg.ge.2 
     limits &1 &2 = =  
  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
    greg1\box n n
  next
  @ window_xy 'nx_box' 'ny_box' 1 1
  greg1\box
  @ header_position
  @ p_header.greg aaa
endif
!
define real max_noise
sic\compute noise median noises
sic\compute max_noise max noises
exa max_noise
exa noise
delete /var histo
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
