!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
! @ c-old-mean-spectrum /mask VarName [ChanTot] /action Mode
!
! /mask VarName: 2D mask variable name with properties
!                mask[i,j]=0        no bad channel at this pixel
!              0<mask[i,j]<chantot  some good and some bad channels at this pixel
!                mask[i,j]=chantot  all channels bad at this pixel
!       ChanTot: see above definition. Default 1 if omitted.
! /action Mode: keyword "inter" or not (optional)
!
! This assumes that the velocity axis is the 3rd axis
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
! Variable definitions
define integer jplane chantot
define logical compute_flux use_mean use_mask
define character unit*12
!
! Command line parsing
sic\parse /mask 1 2 /action 1
! 1: Mask
let use_mask no
if pro%parse%mask%narg.ge.1 then
  let use_mask yes
  if exist(mymask)  sic\delete /variable mymask
  define alias mymask 'pro%parse%mask%arg[1]'
endif
if pro%parse%mask%narg.eq.2 then
  let chantot 'pro%parse%mask%arg[2]'
else
  let chantot 1
endif
! 2: Action
define character action*5
if pro%parse%action%narg.eq.1 then
  let action 'pro%parse%action%arg[1]'
else
  let action "none"
endif
!
if (action.ne."inter") then
   ! Load image
   go\oldload 'name' /syntax hgdf /in aaa1
   !
   ! Define subcube used to compute mean spectrum
   @ l-old-prange.cube aaa1 center size
   @ l-old-vrange.cube aaa1 range true%vrange
   @ l-old-corner.cube aaa1
endif
!
! Define mean spectrum structure
if exist(mean_spectrum) then
   delete /var mean_spectrum
endif
define structure mean_spectrum /global
define integer mean_spectrum%n /global
define double mean_spectrum%x['true%dim[3]'] mean_spectrum%y['true%dim[3]'] /global
define character mean_spectrum%ylegend*32 /global
!
! Spectrum number of channels
let mean_spectrum%n true%dim[3]
!
! Define velocity axis
define double velocities['aaa1%dim[3]']
let velocities[i] (i-aaa1%convert[1,3])*aaa1%convert[3,3]+aaa1%convert[2,3]
let mean_spectrum%x velocities['true%blc[3]':'true%trc[3]']
!
! Flux or Mean?
let unit aaa1%unit /lower
if ((aaa1%major.ne.0).and.(aaa1%unit.eq."Jy/beam")) then
   let compute_flux yes
else
   let compute_flux no
endif
!
! Take care of an eventual polygon
if (exist(poly%nxy)) then
   if (poly%nxy.ge.3) then
      message w mean-spectrum "Use an existing polygon"
      let use_mean yes
   else
      let use_mean no
   endif
else
   let use_mean no
endif
!
! Take care of possible mask
if use_mask then
   message w mean-spectrum "Use an existing mask"
   ! A verification of the mask dimension should probably happen here
   define real loc_mask /like mymask['true%blc[1]':'true%trc[1]','true%blc[2]':'true%trc[2]']
   let loc_mask 0
   if no then  ! Contamination enabled: one bad channel contaminates the resulting pixel
      let loc_mask 1 /where mymask['true%blc[1]':'true%trc[1]','true%blc[2]':'true%trc[2]'].gt.0
   else        ! Contamination disabled: at least 1 valid channel is enough to produce a result
      let loc_mask 1 /where mymask['true%blc[1]':'true%trc[1]','true%blc[2]':'true%trc[2]'].eq.chantot
   endif
endif
!
! Define a "small" intermediate variable
define image bbb "*" real /like aaa1['true%blc[1]':'true%trc[1]','true%blc[2]':'true%trc[2]',1]
let bbb% aaa1%
let bbb%convert[1,1] bbb%convert[1,1]-true%blc[1]+1
let bbb%convert[1,2] bbb%convert[1,2]-true%blc[2]+1
!
! Loop on velocity plane
for iplane true%blc[3] to true%trc[3]
   let jplane iplane-true%blc[3]+1
   let bbb aaa1['true%blc[1]':'true%trc[1]','true%blc[2]':'true%trc[2]',iplane]
   if (use_mask) then
      let bbb sqrt(-1) /where loc_mask.ne.0
   endif
   if (use_mean) then
      greg2\rgdata bbb /var
      greg2\mean quiet
      if (compute_flux) then
         let mean_spectrum%y[jplane] poly%sum
      else
         let mean_spectrum%y[jplane] poly%sum/poly%area
      endif
   else
      if (compute_flux) then
         sic\compute mean_spectrum%y[jplane] sum bbb
      else
         sic\compute mean_spectrum%y[jplane] mean bbb
      endif
   endif
next iplane
!
! Normalization
if (compute_flux) then
   ! Convert to flux
   if (.not.use_mean) then
      let mean_spectrum%y mean_spectrum%y*abs(aaa1%convert[3,1]*aaa1%convert[3,2])
   endif
   ! Convert to flux density
   let mean_spectrum%y mean_spectrum%y*(4*log(2)/(pi*aaa1%major*aaa1%minor))
endif
!
! Define intensity legend
if (compute_flux) then
   let mean_spectrum%ylegend "Integrated Flux (Jy)"
else
   let mean_spectrum%ylegend "Mean Intensity ("'aaa1%unit'")"
endif
!
! Plot
if (action.ne."inter") then
   if (do%fill) then
      define integer wmin wmax
      if .not.exist(mean_spectrum%wmin) then
         define integer mean_spectrum%wmin mean_spectrum%wmax /global
      endif
      @ l-old-vrange.cube aaa1 do%win true%win
      let wmin min(max(true%win%p[1],true%blc[3]),true%trc[3])-true%blc[3]+1
      let wmax min(max(true%win%p[2],true%blc[3]),true%trc[3])-true%blc[3]+1
      let mean_spectrum%wmin min(wmin,wmax)
      let mean_spectrum%wmax max(wmin,wmax)
   endif
   @ l-old-plot-spectrum.cube mean_spectrum
endif
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
