!
!
! @ table-noema Name *|WIDE|LINE Source [Prefix]
!
! Create all (or WIDE or LINE) UV tables from a set of NOEMA .hpb
! files.  IPB files are assumed to be in IPB_DATA:  previously
! defined by the user.
!
! Name  is the generic name of the (sub-project) e.g. s18ay001
! The second argument indicates which UV tables should be created
!   * means all,  WIDE means only the 2 MHz resolution windows,
!   LINE means only the narrow band windows. No second argument
!   prompts the user for selection.
!
! Wide band UV tables will be named Wide-1 to Wide-4
! Narrow band UV tables will be named Line-1 to Line-N
!
say "@ table-noema ?    ! for help ..."
! 
if ("&1?".eq."?") then
  say " "
  say "@ table-noema Name *|WIDE|LINE Source [Prefix]"
  say " "
  say "Create all (or WIDE or LINE) UV tables from a set of NOEMA .hpb"
  say "files.  IPB files are assumed to be in IPB_DATA:  previously"
  say "defined by the user."
  say " "
  say "Arguments"
  say "   Name  is the generic name of the (sub-project) e.g. s18ay001"
  say "   The second argument indicates which UV tables should be created"
  say "       * means all,  WIDE means only the 2 MHz resolution windows,"
  say "      LINE means only the narrow band windows. No second argument"
  say "      prompts the user for selection"
  say "   The third argument is the Source name"
  say " "
  say "Wide band UV tables will be named 'Prefix'Wide-1 to 'Prefix'Wide-4"
  say "Narrow band UV tables will be named 'Prefix'Line-1 to 'Prefix'Line-N"
  say " "
  return 
else if (pro%narg.eq.0).or.(pro%narg.lt.3) then
  say "@ table-noema ?    ! for help ..."
  return
endif
!
if exist(table_prefix) delete /var table_prefix
define chara table_prefix*64 /global
define chara generic*64
let generic &1 /Prompt "Enter common .hpb sub-project name  (e.g. s18ay001)"
var line 
if exist(nline_b) dele /var nline_b
define integer nline_b /global
let table_prefix "&4"
!
set level 6 4 ! Avoid verbosity
!
! Apply calibration, but leave mode to Previous Setting
set rf on 
set phas rel 
set amp rel  
!
begin procedure table-wide
  set sel line lsb L001 and L005
  table 'table_prefix'Wide-1 &1
  set sel line lsb L002 and L006
  table 'table_prefix'Wide-2 &1
  set sel line usb L003 and L007
  table 'table_prefix'Wide-3 &1
  set sel line usb L004 and L008
  table 'table_prefix'Wide-4 &1
end procedure table-wide
!
begin procedure table-narrow
  define charac*3 ch cv
  define charac*4 strh strv
  define integer j
  for i 1 to nline_b
    let ch i+8 /format I3.3
    let cv i+8+nline_b /format I3.3
    let strh L'CH'
    let strv L'CV'
    set sel line SSB 'strh' AND 'strv' 
    table 'table_prefix'Line-'i' &1
  next
end procedure table-narrow
!
sic find 'generic'.hpb 
if (dir%nfile.le.0) then
  SAY "No file found for "'generic'.hpb
  delet /var nline_b
  return error
endif
!
! Create the Wide-* UV tables
define logical do
if ("&2A".eq."*A") then
  for i 1 to dir%nfile
	sic cpu
    file in 'dir%file[i]'
    !
    if (rf_mode.eq."AUTO") then 
      set exclude
      find /proc band /sou *
      if (found.ne.0) then
        set rf SPECTRUM
      else
        set rf FREQUENCY
      endif
    endif
    !
    find /sour &3
    if i.eq.1 then
      get f
      let nline_b (n_line_bands-8)/2
      @ table-wide new
      @ table-narrow new
    else
      @ table-wide
      @ table-narrow
    endif
    sic cpu verbose
  next
else if ("&2A".eq."WIDEA") then
  for i 1 to dir%nfile
    file in 'dir%file[i]'
    if (rf_mode.eq."AUTO") then 
      set exclude
      find /proc band /sou *
      if (found.ne.0) then
        set rf SPECTRUM
      else
        set rf FREQUENCY
      endif
    endif
    !
    find /sou &3 
    if i.eq.1 then
      @ table-wide new
    else
      @ table-wide
    endif
  next
else if ("&2A".eq."LINEA") then
  for i 1 to dir%nfile
    file in 'dir%file[i]'
    if (rf_mode.eq."AUTO") then 
      set exclude
      find /proc band /sou *
      if (found.ne.0) then
        set rf SPECTRUM
      else
        set rf FREQUENCY
      endif
    endif
    !
    find /sour &3
    if i.eq.1 then
      get f
      let nline_b (n_line_bands-8)/2
      @ table-narrow new
    else
      @ table-narrow
    endif
  next
endif
sic cpu verbose
!
delet /var nline_b


