!
if pro%narg.eq.0 then
  SAY " @ all-self *|SELECT|COMPUTE|APPLY|CHECK|SHOW"
  say " "
  say " Compute the Phase and Amplitude self-calibration on the wide-bands UV "
  say "   tables, and Apply it to all (wide or narrow band) relevant spectral line"
  say "   sub-bands which fall in the same band"
  say " "
  say " Phase Self-calibration parameters may need to be customized"
  say " "
  say " Gain tables are stored in .phase and .ampli files with the"
  say "   same name as the wide-band UV tables" 
  say " Self calibrated UV tables are stored in subdirectory SELF"
  return
endif
!
!
define logical do
!
! Sanity check : all-self can work stand-alone or be called by all-widget. 
if .not.exist(all) then
  @ all-create
endif
!
if ("&1A".eq."*A").or.("&1A".eq."FINDA").or.("&1A".eq."SELECTA") then
  let do yes
else if pro%narg.eq.0 then
  let do /prompt "Search wide band data ?"
endif
if (do) then
  @ all-find 'all%filter' 'all%minband'
  if ("&1A".ne."*A") return
  if (all%next.eq."ERROR") return
endif
!
! Sanity check: is there any wide band data ?
if .not.exist(all%wide) then
  Say "List of wideband UV tables (ALL%WIDE) is empty or undefined, use FIND button"
  return error
endif
!
let all%topdir 'sic%directory'
!
if ("&1A".eq."*A").or.("&1A".eq."CHECKA") then
  let do yes
else if pro%narg.eq.0 then
  let do /prompt "Check self-calibration feasibility ?"
endif
if do then
  let all%doself YES
  !
  for sname /in all%wide
    SAY " "
    SAY "Checking "'sname'
    SAY " "
    sic dir 'all%raw'
    @ sub-self-check 'sname'
    sic dir 'all%topdir'
    PAUSE "Finished "'sname'
  next
  if ("&1A".eq."CHECKA") return
endif
!
if ("&1A".eq."*A").or.("&1A".eq."COMPUTEA") then
  let do yes
else if pro%narg.eq.0 then
  let do /prompt "Compute self-calibration ?"
endif
if do then
  !
  for sname /in all%wide
    SAY " "
    SAY "Processing "'sname'
    SAY " "
    sic dir 'all%raw'
    @ sub-self-comp 'sname'
    sic dir 'all%topdir'
    PAUSE "Finished "'sname'
  next
  if ("&1A".eq."COMPUTEA") return
endif
!
if ("&1A".eq."*A").or.("&1A".eq."COLLECTA") then
  let do yes
else if pro%narg.eq.0 then
  let do /prompt "Apply self-calibration ?"
endif
if do then
  @ sub-self-collect 
  sic dir 'all%topdir'
  if ("&1A".eq."COLLECTA") return 
endif
!
if ("&1A".eq."*A").or.("&1A".eq."APPLYA") then
  let do yes
else if pro%narg.eq.0 then
  let do /prompt "Apply self-calibration ?"
endif
if do then
  ! The second argument of @ sub-self-apply is used to test the matching
  ! mode of narrow windows to use separate or global selfcal solutions.
  if (all%combine) then
    ! Combine mode: 
    ! Apply in DELAY mode to all Windows that fall in scope
    for i 1 to size(all%wide)
      @ sub-self-apply 'all%wide[i]' 'i'
    next
  else 
    if (size(all%wide).eq.1) then
      ! Only one Wide band window: 
      ! Apply in PHASE mode to all Windows
      ! Make sure all selected Bands are calibrated
      @ sub-self-apply 'all%wide[1]' 1  FORCE
    else
      ! Several Wide band windows: match Narrow and Wide bands
      ! Bands out of Scope will not be considered
      for sname /in all%wide
        @ sub-self-apply 'sname' 0
      next
    endif
  endif
  sic dir 'all%topdir'
  if ("&1A".eq."APPLYA") return 
endif
!
if ("&1A".eq."*A").or.("&1A".eq."SHOWA") then
  let do yes
else if pro%narg.eq.0 then
  let do /prompt "Show self-calibration ?"
endif
if do then
  define logical bars
  let bars error_bars
  !
  sic dir 'all%raw'
  let error_bars yes
  for sname /in all%wide
    @ sub-self-show 'sname'
  next
  let error_bars bars
  sic dir 'all%topdir'
endif
