!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
! Compute mosaic noise and SNR maps from signal and weight files.
! J.Pety 2004-2015
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
if file("'name'.'type'") then
   define image signal 'name'"."'type' read
else
   message e snr "Inexistent file "'name'"."'type'
   return base
endif
if file("'name'.lobe") then
   vector\transpose 'name'".lobe" 'name'".ebol" 231
   define image lobe 'name'".ebol" read
else
   message e snr "Inexistent file "'name'".lobe"
   return base
endif
!
define double rms
if (pro%narg.eq.1) then
   let rms &1
else
   if (signal%rms.eq.0.0) then
      message e snr "signal%rms is 0.0, please give your rms with:"
      message e snr "MOSAIC> GO SNR <RMS>"
      return base
   else
      let rms signal%rms
   endif
endif
!
define image noise 'name'"."'type'"-no" real /like lobe[1]
define image snr   'name'"."'type'"-sn" real /like signal
!
let noise% lobe%
let noise%blank signal%blank
let noise%sigma signal%sigma
let noise%rms rms
let noise 0
for ilobe 1 to lobe%dim[3]
   let noise noise+lobe[ilobe]^2
next ilobe
define real max
compute max max noise
let noise rms/sqrt(noise/max)
let noise signal%blank[1] /where noise.gt.10*rms
!
let snr% signal%
let snr snr%blank[1]
if (snr%ndim.eq.2) then
   let snr abs(signal/noise) /where abs(signal-signal%blank[1]).gt.signal%blank[2]
else
   for iplane 1 to snr%dim[3]
      let snr[iplane] abs(signal[iplane]/noise) /where abs(signal[iplane]-signal%blank[1]).gt.signal%blank[2]
   next iplane
endif
delete /var signal noise snr
!
vector\header 'name'"."'type'"-no" /extrema
vector\header 'name'"."'type'"-sn" /extrema
!
! Set type to cleaned image
let type 'type'-sn
message i clean "Set type variable to "'type'
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
