!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
! @ c-old-area
!
! Compute integrated emission between true%first[1] and true%last[1] plane
! taking blanking into account
!
! This assumes that the velocity axis is the axis #3
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
define character action*5
let action "&1"
!
! Pre-processing
if (action.ne."inter") then
   go\oldload 'name' /syntax hgdf /in aaa1
   define structure area
   define image area%map 'name'"-area."'type' real /like aaa1[1]
   define image area%ncha "*" integer /like aaa1[1]
   define image area%mask "*" integer /like aaa1[1]
   if (do%nranges.lt.0) then
      message e area "Negative number of ranges: "'do%nranges'
   else if (do%nranges.eq.0) then
      let true%vranges%n 1
      let true%vranges%v[1] range
   else
      let true%vranges%n do%nranges
      let true%vranges%v do%ranges
   endif
else
   if exist(area) then
      delete /var area
   endif
   define structure area /global
   define image area%map  "*" real    /like aaa1[1] /global
   define image area%ncha "*" integer /like aaa1[1] /global
   define image area%mask "*" integer /like aaa1[1] /global
   let true%vranges%n 1
endif
!
let area%map 0
let area%ncha 0
let area%mask 0
for irange 1 to true%vranges%n
   if (action.ne."inter") then
      let range true%vranges%v[irange]
      @ l-old-vrange.cube aaa1 range true%vrange
      @ l-old-layout.cube aaa1 "1d" true%vrange%p[1] true%vrange%p[2] 1
   else
      @ l-old-vrange.cube aaa1 do%win true%win
      @ l-old-layout.cube aaa1 "1d" true%win%p[1] true%win%p[2] 1
   endif
   for iplane true%first[1] to true%last[1] by true%step[1]
!!$      let area%map area%map+aaa1[iplane] /where abs(aaa1[iplane]-aaa1%blank[1]).gt.aaa1%blank[2]
!!$      let area%ncha area%ncha+1 /where abs(aaa1[iplane]-aaa1%blank[1]).gt.aaa1%blank[2]
!!$      let area%mask area%mask+1 /where abs(aaa1[iplane]-aaa1%blank[1]).le.aaa1%blank[2]
      let area%map area%map+aaa1[iplane] /where aaa1[iplane].eq.aaa1[iplane]
      let area%ncha area%ncha+1 /where aaa1[iplane].eq.aaa1[iplane]
      let area%mask area%mask+1 /where aaa1[iplane].ne.aaa1[iplane]
   next iplane
next irange
let area%map sqrt(-1) /where area%mask.gt.0
!
! *** JP ugly patch
if .not.exist(true%nchan) define double true%nchan /global
! *** JP ugly patch
! VVV option /blank should stay as this is to avoid true%nchan being
! contaminated by pixels without signal
sic\compute true%nchan median area%ncha /blanking 0 0
let area%map% aaa1%
let area%map%f_axis 0
let area%map%convert[3] 0.0
let area%map%unit3 ""
if (aaa1%unit.eq."---") then
   let area%map area%map/area%ncha
   let area%map%unit 'aaa1%unit'
else
   let area%map area%map*abs(aaa1%convert[3,3])
   let area%map%unit 'aaa1%unit'".km/s"
   let area%map%velres 'aaa1%velres*true%nchan'
   let area%map%freqres 'aaa1%freqres*true%nchan'
   let area%map%noise 'aaa1%noise*abs(aaa1%convert[3,3])*sqrt(true%nchan)'
   let area%map%rms 'aaa1%rms*abs(aaa1%convert[3,3])*sqrt(true%nchan)'
endif
!
! Post-processing
if (action.ne."inter") then
   delete /var area
   let name 'name'"-area"
   say "Name set to "'name'
   vector\header 'name'"."'type' /extrema
else
   let area%mask% area%map%
endif
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
