!
! Initialize
define logical DO DOALL
let doall no
define character*12 ARG KEY
!
if (.not.exist(all%done)) then
  @ all-setup
  let doall NO
  let all%last "?"
endif
!
! @ all-pipeline
!   Perform automatic imaging in IMAGER using an ensemble of UV Tables
!   and a spectral line catalog
!
! Control Parameters defined in all-setup.ima, or recovered from
! a context previously saved using "@ all-save" by executing
! "@ all-memory"
!
!
let arg "&1" /upper
let key "&2" /upper
!
if (key.ne." ").and.(key.ne."?") then
  let all%mode 'key'
else
  let all%mode 'all%mode' /upper
endif
define character r*5 n*5 b*5
r = 'ansi%red'
n = 'ansi%none'
b = 'ansi%blue'
!
if .not.exist(all%next) define character*8 all%next /GLOBAL ! Next operation
!
if (key.eq."?") then
  !
  if (arg.eq."ORGANIZE") then 
    say "  -- Overall tree layout --"
    say 'b'"ALL%UVFITS "'n'" = "'r''all%uvfits''n'
    say 'b'"ALL%RAW    "'n'" = "'r''all%raw''n'
    say 'b'"ALL%SELF   "'n'" = "'r''all%self''n'
    say 'b'"ALL%MAPS   "'n'" = "'r''all%maps''n'
    say 'b'"ALL%TABLES "'n'" = "'r''all%tables''n'
  else if (arg.eq."FIND") then
    say "  -- File name selection filter --"
    say 'b'"ALL%FILTER "'n'" = "'r''all%filter''n'
  else if (arg.eq."SELECT") then
    say " -- Wide band selection limit --"
    say 'b'"ALL%FILTER "'n'" = "'r''all%filter''n'
    say 'b'"ALL%MINBAND"'n'" = "'r''all%minband''n' "! Minimum Bandwidth for SelfCal (MHz)"
  else if (arg.eq."COMPUTE") then
    say " -- Self-Calibration criteria -- "
    say 'b'"ALL%PREFIX_SELF "'n'" = """'r''all%prefix_self''n'""" ! Self-calibrated filename prefix"
    say 'b'"ALL%PHASE_NITER "'n'" = "'r' all%phase_niter 'n' /format A (*(I0,1X)) A
    say 'b'"ALL%PHASE_TIMES "'n'" = "'r' all%phase_times 'n' /format A (*(I0,1X)) A
    say 'b'"ALL%AMPLI_NITER "'n'" = "'r' all%ampli_niter 'n' /format A (*(I0,1X)) A
    say 'b'"ALL%AMPLI_TIMES "'n'" = "'r' all%ampli_times 'n' /format A (*(I0,1X)) A
  else if (arg.eq."COLLECT") then
    say " -- Self-Calibration selection -- "
    say 'b'"ALL%COLLECT     "'n'" = "'r''all%collect''n'  " ! Flagging threshold"
    say 'b'"ALL%COMBINE     "'n'" = "'r''all%combine''n'  " ! Combine for a DELAY solution ?"
  else if (arg.eq."TIME") then
    say " -- Time compression criterium --"
    say 'b'"ALL%ITIME       "'n'" = "'r''all%itime''n'
  else if (arg.eq."IMAGE") then
    say " -- Imaging Parameters --" 
    say 'b'"ALL%RANGE         "'n'" = "'r' all%range 'n'   " ! Velocity range & resolution" /format A (*(F10.3,1X)) A A
    say 'b'"ALL%MINFRES       "'n'" = "'r''all%minfres''n' 
    say 'b'"ALL%RESTORE       "'n'" = "'r''all%restore''n' " ! Use UV_RESTORE"
    say 'b'"MAP_SIZE          "'n'" = "'r''map_size[1]'" " 'map_size[2]''n' "! Number of pixels"
    say 'b'"MAP_FIELD         "'n'" = "'r''map_field[1]'" " 'map_field[2]''n' "! Field of view (in arcsec)"
    say 'b'"MAP_CELL          "'n'" = "'r''map_cell[1]'" " 'map_cell[2]''n' "! Pixel size (in arcsec)"
    say 'b'"MAP_ROBUST        "'n'" = "'r''map_robust''n' "! Robust factor"
  else if (arg.eq."TABLES") then
    say " -- UV Tables extraction --"
    say 'b'"ALL%RESAMPLE      "'n'" = "'r''all%resample''n' " ! Use spectral resampling if needed"
    say 'b'"ALL%RANGE         "'n'" = "'r' all%range 'n'    " ! Velocity range & resolution" /format A (*(F10.3,1X)) A A
    say 'b'"ALL%MINFRES       "'n'" = "'r''all%minfres''n'  " ! Maximum channel width for spectral line data (MHz)"  
  endif 
  return
endif
!
if (arg.eq." ") then
  let arg 'all%last'" " /upper
else
  let doall arg.eq."*"
  if arg.eq."NEXT" then
    let arg 'all%next'" " /upper
  else if arg.eq."LAST" then
    let arg 'all%last'" " /upper  
  endif
endif
if (arg.eq."?") then
  @ all-display
  return
endif
!
! Now the job
SIC CPU
SYMBOL SIC\PAUSE CONTINUE  ! Make sure the pipeline does not wait
say 'b'"Pipeline Mode ALL%MODE "'n'" = "'r''all%mode''n'
!
if (doall) then
  @ all-layout  ORGANIZE    ! Organize the data
  @ all-list    FIND        ! Find and List UV tables
  @ all-self    SELECT      ! Select UV tables to process
  if all%next.eq."ERROR" return error
  @ all-self    CHECK       ! Verify if Self-Calibration possible
  @ all-self    COMPUTE     ! Solve for Self-Calibration
  @ all-self    COLLECT     ! Merge Self-calibration solutions - Only needed if S/N is limited
  @ all-self    APPLY       ! Apply Self-calibration solution
  let error_bars yes
  @ all-self    SHOW        ! Show Self-Calibration
  @ all-process * ! TIME + TABLES + IMAGE
  @ all-save                ! Save context for re-use
  let all%next " "
  let all%last "?"
  delete /symb SIC\PAUSE
  SIC CPU VERBOSE
  return
endif
!
if (arg.eq."ORGANIZE") then
  @ all-layout  ORGANIZE    ! Organize the data
  let all%next "FIND"
else if (arg.eq."FIND")    then
  @ all-list    FIND        ! Find and List UV tables
  let all%next "SELECT"
else if (arg.eq."SELECT")  then 
  @ all-self    SELECT      ! Select UV tables to process
  if all%next.eq."ERROR" return error
  let all%next "CHECK"
else if (arg.eq."CHECK") then
  @ all-self    CHECK
  if all%next.eq."ERROR" return error
  let all%next "CHECK"  
else if (arg.eq."COMPUTE").or.(arg.eq."SELF") then
  @ all-self    COMPUTE     ! Solve for Self-Calibration
  let all%next "COLLECT"
else if (arg.eq."COLLECT")  then
  @ all-self    COLLECT     ! Merge Self-calibration solutions - Only needed if S/N is limited
  let all%next "APPLY"
else if (arg.eq."APPLY")    then
  @ all-self    APPLY       ! Apply Self-calibration solution
  let all%next "TIME"
else if (arg.eq."TIME")     then
  @ all-process TIME        ! Time compression 
  let all%next "IMAGE"
else if (arg.eq."TABLES")   then
  @ all-process TABLES      ! Extract UV tables around each spectral line
  let all%next "INDEX"
else if (arg.eq."IMAGE")    then
  @ all-process IMAGE       ! Automatic imaging
  @ all-save
  let all%next "TABLES"
else if (arg.eq."SHOW")     then  
  let self_hard yes
  @ all-self SHOW
else if (arg.eq."SAVE")     then 
  @ all-save                ! Save context for re-use
  let all%next " "
  delet /symb sic\pause
  return
!else if (arg.eq."INDEX") then
!  if .not.exist(TABLES) @ all-process TABLES
!  @ p_spindex
else if (arg.eq."SKY")      then
  @ all-process SKY
!  delete /symbol SIC\PAUSE
!  return
!  PAUSE "Finished all-process"
else if (arg.ne." ")        then
  define character*16 pipestr[16]
  let pipestr "?" "ORGANIZE" "FIND" "SELECT" "CHECK" "COMPUTE" "COLLECT" "APPLY" "TIME" "TABLES" "IMAGE" "SHOW" "SAVE" "LAST" "NEXT" "SKY"
  message E PIPELINE "Unknown keyword "'arg'", Choices (in sequential order) are:"
  say pipestr[2:11] /form A16
  message E PIPELINE "Other Choices:"
  say pipestr[12:16] /form A16
endif
let all%last 'arg' /upper
!
delete /symbol SIC\PAUSE
if (all%last.eq." ") return
!
SIC CPU VERBOSE

