!
! @ all-process [Filter]
!
! Process all UV tables selected with the optional Filter 
! 3 possible actions
!
! 1) TIME
!      Time compress
! 2) TABLES
!     Separate the Line from the Continuum emission, with one
!    separate UV table for each possible spectral line in the
!    window band, and save separate UV tables under TABLES/
!    sub-directory
! 3) IMAGE
!     Separate the Line from the Continuum emission and image
!    these, again with one image for each possible spectral
!    line, saving the CLEAN and CCT tables under MAPS/
!    sub-directory
!
! The naming convention is 'name'-C.uvt for continuum, 
! 'name'-D for detected lines, 'name'-U for undetected lines
! "name" derives from the most likely line identification in
! the corresponding window, with a sequence number for species
! with more than 1 line in the band (including hyperfine components
! for example), or is the mean frequency for continuum data.
!
! Sanity check: all-process can work stand-alone, or called by all-widget
if .not.exist(all) then
  @ all-create
endif
!
if (all%mode.ne."CONTINUUM") then
  if (all%mode.ne."SURVEY") then
    if (exist(LINEDB%DBIN)) then
      if (all(all%range.eq.0)) then
        let all%range /Prompt "Enter velocity range for lines"
      endif
      if (all(all%range.eq.0)) then
        MESSAGE E DO-ALL "Velocity range is Null"
        return error
      endif
    endif
  endif
  !
  if (all%minfres.le.0) then
    let all%minfres /Prompt "Enter maximum frequency resolution (MHz)" 
  endif
  if (all%minfres.le.0) then
    MESSAGE E DO-ALL "Maximum frequency resolution is Null"
    return error
  endif
endif
!
define char dadir*32 prefix*8
let all%topdir 'sic%directory'"/"
define logical fallback
let fallback YES
if file(all%self) then
  let dadir 'all%self'
  sic find *.uvt 'dadir'
  if (dir%nfile.ne.0) then
    message I DO-ALL "Using SELF calibrated data files"
    let fallback NO
  endif
endif
if fallback then
  let dadir 'all%raw' 
  if .not.file(dadir) then
    message E DO-ALL "No RAW data files, use ORGANIZE first..."
    return error
  endif
  message W DO-ALL "Using RAW data files"
endif
sic dir 'dadir'
!
define logical do doall
let doall "&1A".eq."*A"
!
! Use 'all%filter' unless an argument is given
if ("&2none".ne."none") then
  sic find &2*.uvt 
else
  sic find 'all%filter'.uvt
  if (dir%nfile.eq.0) sic find 'all%prefix_self''all%filter'.uvt
endif
!
if (dir%nfile.eq.0) then
  if (doall.and.dadir.eq.all%self) then
    let dadir 'all%raw'
    sic dir ..
    sic dir 'all%raw'
    if ("&2none".ne."none") then
      sic find &2*.uvt 
    else
      sic find 'all%filter'.uvt
      if (dir%nfile.eq.0) sic find 'all%prefix_self''all%filter'.uvt
    endif
  endif
  if (dir%nfile.eq.0) then
    MESSAGE E DO-ALL "No such UV tables &2*.uvt in "'dadir' 
    sic dir 'all%topdir'
    return error
  endif
endif
!
if doall.or.("&1A".eq."TIMEA") then
  let do yes
else if ("&1A".eq."A") then
  let do /prompt "Do time compress ?"
endif
sic dir 'all%topdir'
if do then
  if dadir.ne.all%self then
    sic mkdir 'all%self'  ! Create the Sub-Directory
  endif
  for i 1 to dir%nfile
    @ do-time 'dir%file[I]' 'dadir' 'all%self'
  next
  sic dir 'all%topdir'
  do = doall
else  
  if (dadir.ne.all%self) then
    MESSAGE E DO-ALL "No directory "'all%self'
    MESSAGE I DO-ALL "Use the SELF or TIME button before" 
    return error
  endif  
endif
!
if dadir.ne.all%self then
  sic dir 'all%self'
  sic find &2*.uvt 
  if (dir%nfile.eq.0) then
    MESSAGE E DO-ALL "No such UV tables &2*.uvt in "'all%self'
    MESSAGE I DO-ALL "Use the SELF or TIME button before" 
    sic dir 'all%topdir'
    return error
  endif
endif
!
if doall.or.("&1A".eq."IMAGEA") then
  let do yes
else if ("&1A".eq."A") then
  let do /prompt "Do imaging  ?"
endif
sic dir 'all%topdir'
if do then
  sic mkdir 'all%maps'
  !
  sic cpu 
  let runtime%start SIC%CPU%CUMUL%ELAPSED
  for i 1 to dir%nfile
    sic cpu 
    say "!---------------------------------------"
    say "!"
    say "! --- Imaging "'dir%file[i]'"   ---"
    say "!"
    @ do-image 'all%self''dir%file[I]' 'all%maps' "[ "'i'"/"'dir%nfile'" ]"
    sic cpu 
    let runtime%left (dir%nfile-i)*(SIC%CPU%CUMUL%ELAPSED-runtime%start)/i
    !
    @ say-hms runtime%left runtime%end 
    say "!"
    say "!---------------------------------------"
  next
  do = doall
  sic dir 'all%topdir'
  let runtime%left SIC%CPU%CUMUL%ELAPSED-runtime%start
  @ say-hms runtime%left runtime%end 
  say "!---------------------------------------"
  say "Total Imaging Elapsed Time "'runtime%end'
  say "!---------------------------------------"
endif
!
if doall.or.("&1A".eq."TABLESA") then
  let do yes
else if ("&1A".eq."A") then
  let do /prompt "Do line table extraction ?"
endif
sic dir 'all%topdir'
if do then
  sic mkdir 'all%tables'
  !
  for i 1 to dir%nfile
    @ do-extract 'dadir''dir%file[I]' 'all%tables'
  next
  do = doall
endif
!
sic dir 'all%topdir'
!
!
if doall.or.("&1A".eq."TABLESA") then
  let do yes
else if ("&1A".eq."A") then
  let do /prompt "Do line table extraction ?"
endif
sic dir 'all%topdir'
if do then
  sic mkdir 'all%tables'
  !
  for i 1 to dir%nfile
    @ do-extract 'dadir''dir%file[I]' 'all%tables'
  next
  do = doall
endif
!
sic dir 'all%topdir'
!
! Try SKY
let do NO
!
if doall.or.("&1A".eq."SKYA") then
  let do yes
else if ("&1A".eq."A") then
  let do /prompt "Do Primary Beam correction ?"
endif
sic dir 'all%topdir'
if do then
  !
  sic find "*.lmv-clean" 'all%maps'
  if dir%nfile.gt.0 then
    for i 1 to dir%nfile
      @ do-sky  'dir%file[I]' 'all%maps'
    next
  else
    message W PIPELINE "No Clean images"
  endif
  do = doall
endif
!
sic dir 'all%topdir'
return
