if ("&1?".eq."??") then
  say " "
  say "@ all-tables Source Filter Type [Prefix]"
  say " "
  say "Create all (or WIDE or LINE) UV tables from a set of NOEMA .hpb"
  say "files, depending on Type.  IPB files are assumed to be in IPB_DATA:"
  say "    previously defined by the user."
  say " "
  say "RF calibration mode must be preset by variable RF_MODE"
  say "Calibration mode must be preset by variable CAL_MODE"
  say " "
  say "Default values are"
  say "    Source      current source selected by SET SOURCE"
  say "    Filter      *  (take all .hpb files)" 
  say "    Type        *  (WIDE + LINE); can be WIDE or LINE also"
  say "    Prefix      (no prefix)"
  say " "
  say "@ all-tables *"
  say "    will generate all tables for all sources, each prefixed by the"
  say "    source name"
  say "@ all-tables"
  say "    will generate all tables for the current source"
  return
endif
if .not.exist(rf_mode) then
  define char*8 rf_mode /global
  say " "
  say "RF calibration mode is defined by variable RF_MODE"
  say " "
  say "Allowed values are AUTO, SPECTRUM, FREQUENCY and CHANNEL"
  say "In AUTO, use SPECTRUM mode if possible, fall back to FREQUENCY if not"
  say "Using default value AUTO"
  let rf_mode AUTO
endif
if .not.exist(cal_mode) then
  define char*8 cal_mode /global
  say "Amplitude and Phase calibration mode are defined by variable CAL_MODE"
  say "Allowed values are ANTENNA, BASE and USER"
  let cal_mode
endif
if (cal_mode.eq."ANTENNA") then
  set phas ant
  set ampl ant
else if (cal_mode.eq."BASE") then
  set phas base
  set ampl base
else if (cal_mode.eq."USER") then
  SAY "Using user-specified calibration mode"
  show phase
  show amp
else
  SAY "Invalid CAL_MODE "'cal_mode'
  return error
endif
!
if (rf_mode.ne."AUTO") then
  set rf 'rf_mode'
  show rf
else
  say "RF passband mode is AUTO"
  set rf SPECTRUM	! In case, by default
endif
!
set leve 6 4
set rf on file
set amp relative jansky
set pha relative atm
set aver scan
set proc corr
!
define char*8 ttype
define char*8 Type
define char*256 Filter
!
let Filter *
let Type *
!
if (pro%narg.ne.0) then
  symbol source_list "&1"
  set source 'source_list'
  if (pro%narg.gt.1) let Filter &2
  if (pro%narg.gt.2) let Type &3
endif
!
sic find 'Filter'.hpb
if (dir%nfile.eq.0) then
  message I TABLE "Use ""@ all-tables ?"" to get help"
  message E TABLE "No such .hpb files "'Filter'".hpb"
  return error
endif
file in 'dir%file[1]'
!
find /proc corr
if (found.eq.0) then
  message I TABLE  "Use ""@ all-tables ?"" to get help"
  message E TABLE  "Please verify SET SOURCE and/or SET TYPE"
  return error
endif
!
get first
if .not.exist(spw) then
  var spw on
endif
!
if (spw%bw[1].gt.3000) then
  let ttype "noema"
else if (spw%bw[spw%nspw].eq.2000) then
  let ttype "widex"
else
  let ttype "narrow"
endif
!
!
if ("&1none".eq."none").or.("&1none".eq."*none") then
  !
  define integer m_calib n_calib i_calib
  define logical lfound
  let n_calib 0
  let m_calib 0
  let i_calib 0
  !
  for fich /in dir%file
    !
    file in 'fich'  
    find 
    list /source      
    if (n_calib.ne.0) then
      define char*12 o_calib[n_calib]
      for k 1 to n_calib
        let o_calib[k] 'c_calib[k]'
      next
      delete /var c_calib
    endif
    let m_calib n_calib+n_source
    define character*12 c_calib[m_calib]
    if (n_calib.ne.0) then
      let i_calib n_calib
      for k 1 to n_calib
        let c_calib[k] 'o_calib[k]'
      next
      for i 1 to n_source
        let lfound no
        for j 1 to n_calib
          if c_calib[j].eq.c_source[i] then
            let lfound yes
            break
          endif
        next
        if (.not.lfound) then
          i_calib = i_calib+1
          let c_calib[i_calib] 'c_source[i]'
        endif
      next
      let n_calib i_calib
      delet /var o_calib
    else
      let n_calib n_source
      for k 1 to n_calib
        let c_calib[k] 'c_source[k]'
      next
    endif
  next
  !
  ! Now we have it...
  if (n_calib.gt.1).or.("&1none".eq."*none") then    
    for k 1 to n_calib
      say " "
      say " --------------------------------------------------------"
      say " Treating Source "'c_calib[k]'
      say " "
      @ table-'ttype' 'Filter' 'Type' 'c_calib[k]' &4'c_calib[k]'"-"
    next
  else
    say " "
    say " --------------------------------------------------------"
    say " Treating Source "'c_calib[1]'
    say " "
    @ table-'ttype' 'Filter' 'Type' 'c_calib[1]' &4 
  endif
else
  define character*12 c_calib[1] 
  let c_calib 'source_list' /resize
  define integer n_calib
  let n_calib size(c_calib)
  if (n_calib.gt.1) then
    for i 1 to n_calib
      say " "
      say " --------------------------------------------------------"
      say " Treating Source "'c_calib[k]'
      say " "
      @ table-'ttype' 'Filter' 'Type' 'c_calib[k]' &4'c_calib[k]'"-"
    next
  else
    say " "
    say " --------------------------------------------------------"
    say " Treating Source "'c_calib[1]'
    say " "
    @ table-'ttype' 'Filter' 'Type' 'c_calib[1]' &4 
  endif
endif


  
