!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
! Plot cumulative flux as a function of the number of components.
! J. Pety
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
@ window_tools.greg
!
gtvl\clear directory
!
if exist(comp) then
  delete /var comp
endif
if exist(cflux) then
  delete /var cflux
endif
define integer ncomp nplanes
define integer ucomp jfirst jlast
let ucomp &2+0
!
if (&1%unit2.eq."COMPONENT") then
  let ncomp &1%dim[2]
  let nplanes &1%dim[3]
  SAY 'Nplanes'" planes with "'ncomp'" components"
  define real comp[ncomp] cflux[ncomp,nplanes] /global
  define real cvalue[ncomp,nplanes]
  define integer k
  !
  for iplane 1 to nplanes
    let cvalue[iplane] &1[3,,iplane]
    sic\compute cflux[iplane] integral cvalue[iplane]
  next
else
  let ncomp &1%dim[3]
  let nplanes &1%dim[2]
  define real comp[ncomp] cflux[ncomp,nplanes] /global
  define real cvalue[ncomp,nplanes]
  define integer k
  !
  for iplane 1 to nplanes
    let cvalue[iplane] &1[3,iplane,]
    sic\compute cflux[iplane] integral cvalue[iplane]
  next
endif
!
if ucomp.eq.0 then
  define real lcomp[ncomp]
  for iplane 1 to nplanes
    let lcomp[icomp] icomp
    let lcomp 0 /where cvalue[iplane].eq.0
    let comp max(comp,lcomp)
  next
else
  let comp ucomp
  let comp[1] 1
endif
!
! Compute the limit
!
@ xy_limits comp cflux
!
! Define plotting window
!
if first.le.0 then
  let jfirst 1
else
  let jfirst min(first,nplanes)
endif
!
if last.le.0 then
  let jlast max(1,nplanes+last)
else
  let jlast min(last,nplanes)
endif
!
@ plot_position
let aspect_ratio yes
let aspect 1
@ find_nx_ny 'jlast-jfirst+1'
if (expand.eq.0) then
  greg1\set expand min(6.0/(nx_box+ny_box),1.) ! Automatic definition character size
else
  greg1\set expand 'expand' ! User defined character size
endif
!
! Plot
!
define real mcomp
for iplane jfirst to jlast
  let comp[icomp] icomp
  let comp 0 /where cvalue[iplane].eq.0
  sic\compute mcomp max comp
  if (ucomp.ne.0) then
    let mcomp min(ucomp,mcomp)
  endif
  @ window_xy 'nx_box' 'ny_box' 'iplane-jfirst+1'
  if (mcomp.gt.0) then
    greg1\connect comp[1:mcomp] cflux[1:mcomp,iplane]
  endif
  greg1\box n n
next
!
! Make labels: locally or not according to number of boxes
!
if (nx_box.le.3.and.ny_box.le.3) then
  @ window_xy 'nx_box' 'ny_box' 1 1
  greg1\box
  greg1\set expand 1.0
  @ window_xy 1 1 1 1
  greg1\label "Clean component number" /x
  greg1\label "Cumulated flux (Jy)"    /y
endif
!
! Make header
!
@ header_position
@ p_header.greg &1
!
! Plot the box
if (nx_box.le.3.and.ny_box.le.3) then
  continue
else
  greg1\set expand 0.6
  define real dx dy x2 y2
  x2 = box_xmax
  y2 = box_ymax
  if (page_x.gt.page_y) then
    let dx min((page_x-x2),0.15*page_y)
    let dy dx
    greg1\set box (page_x+x2)|2.-2*dx|5. (page_x+x2)|2.+3*dx|5. page_y*0.5-dy page_y*0.5
  else
    let dx min((0.15*page_x),(page_y-y2))
    let dy dx
    greg1\set box page_x*0.95-dx page_x*0.95 page_y*0.95-dy page_y*0.95
  endif
  greg1\box
  greg1\label "Clean component number" /x
  greg1\draw text -3 0 "Cumulated flux (Jy)" 5 90 /char 4
endif
!
greg1\set expand 1.0
!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
