!
if .not.exist(all) then
  @ all-create
endif
!
! Move the .UVT files to the 'all%raw' directory if not already
! done, or create them from the .uvfits files, which are to be
! moved in the 'all%uvfits' directory if not already done too...
!
define logical no_uvt
if .not.file(all%raw) then
  ! 'all%raw' does not exists.
  ! Check if any .UVT file
  sic find *.uvt
  if dir%nfile.eq.0 then
    ! There are no .uvt files at top level, may be some .uvfits files
    if .not.file(all%uvfits) then
      sic find *.uvfits
    else
      sic find 'all%uvfits'/*.uvfits 
    endif
    if (dir%nfile.eq.0) then
      message E ORGANIZE "No data found (no .uvfits, no .uvt)"
      return error
    endif
    if .not.file(all%uvfits) then
      sic mkdir 'all%uvfits'
      ! Move the .uvfits files to the 'all%uvfits'
      for i 1 to dir%nfile
        sic rename 'dir%file[I]' 'all%uvfits'
      next
    endif
    ! Make the conversion from UVFITS to UVT
    sic mkdir 'all%raw'
    @ all-uvfits
    !
  else
    !
    ! The .uvt files are here. Move them
    sic mkdir 'all%raw'
    for i 1 to dir%nfile
      sic rename 'dir%file[I]' 'all%raw'
    next
    MESSAGE I ALL 'dir%nfile'" .uvt files moved to "'all%raw'
  endif
  sic find 'all%raw'/*.uvt 
  !
else
  sic find 'all%raw'/*.uvt 
  if (dir%nfile.eq.0) then
    message E ORGANIZE 'all%raw'" has no .uvt data files"
    return error
  endif
  MESSAGE I ALL 'dir%nfile'" .uvt files found in "'all%raw'
endif
!
! Get the first file header
define header a 'dir%file[1]' read 
!
! Set Self-Calibration defaults
if (exist(a%tel1)) then
  let all%telescope 'a%tel1%name' /upper
  sic find *.uvt 'all%self'
  if (dir%nfile.eq.0) then
    if (all%telescope.eq."ALMA") then
      let all%phase_times 6 
      let all%ampli_times 60
      message I ALL "Using ALMA default Self-Calibration parameters"
    else if (all%telescope.eq."NOEMA") then
      let all%phase_times 45
      let all%ampli_times 120
      message I ALL "Using NOEMA default Self-Calibration parameters"
    endif
  endif
else
  let all%telescope "Unknown"
endif
!
! Remember conditions
@ all-save
!
