subroutine uv_map_comm(line,comm,error)
  use clean_arrays
  use clean_default
  use gbl_message
  use gkernel_interfaces
  use imager_interfaces, except_this => uv_map_comm
  !---------------------------------------------------------------------
  ! @ private
  !*
  ! IMAGER -- Support for commands
  ! UV_MAP or UV_RESTORE /FIELDS /RANGE /TRUNCATE  /CONTINUUM /INDEX  
  !   or  
  ! UV_SELF /RANGE /RESTORE
  !!
  !---------------------------------------------------------------------
  character(len=*), intent(in) :: comm     !! Caller name
  character(len=*), intent(inout) :: line  !! Command line
  logical, intent(inout) :: error          !! Logical error flag
  !
  ! Constants
  real(kind=8), parameter :: pi=3.14159265358979323846d0
  real(kind=4), parameter :: sec_to_rad=pi/3600.0/180.0
  character(len=1), parameter :: question_mark='?'
  ! ' UV_MAP', '/TRUNCATE',  '/FIELDS', '/RANGE', '/CONTINUUM', '/INDEX'
  integer, parameter :: o_trunc=1
  integer, parameter :: o_field=2
  integer, parameter :: o_range=3
  integer, parameter :: o_cont=4
  integer, parameter :: o_index=5
  ! ' UV_SELF', '/RANGE', '/RESTORE'
  integer, parameter :: opt_rang=1  ! /RANGE Option for UV_SELF
  integer, parameter :: opt_rest=2  ! /RESTORE Option for UV_SELF
  !
  ! Local ---
  logical :: use_self, do_init
  logical :: true_mosaic   ! Distiniguish Mosaics from Single pointings
  integer :: ifi
  logical :: err, goon
  character(len=80) :: chain
  real :: offmax
  integer :: debug
  !
  ! Code ----
  call map_message(seve%t,comm,'Entering Uv_Map_Comm',seve%e)
  debug = 0
  call sic_get_inte('DEBUG_UVMAP',debug,error)
  error = .false.
  !
  ! Check command line syntax
  call map_query (line,comm,themap,goon,error) 
  if ((.not.goon).or.error) return
  !
  err = .false.
  use_self = comm.eq.'UV_SELF'      ! Self Calibration image !
  if (use_self) then
    if (huv_current%gil%column_pointer(code_uvt_if).ne.0) then
      call map_message(seve%e,comm, &
      & 'This UV table (probably a result of UV_CONTINUUM) is not suited for self-calibration')
      error = .true.
      return
    endif
  endif
  do_init = hself_self%loca%size.eq.0 .or. sic_present(opt_rang,0)
  !
  true_mosaic = themap%nfields.ne.0
  if (true_mosaic) then
    offmax = 0
    do ifi = 1,abs(themap%nfields)
      offmax = max(offmax,(themap%offxy(1,ifi)**2+themap%offxy(2,ifi)**2))
    enddo
    offmax = sqrt(offmax)/sec_to_rad
    if (offmax.lt.mosaic_tolerance) then
      if (huv_current%gil%nteles.le.1) then
        write(chain,'(A,F8.4,A,F8.4,A)') 'Treating tiny mosaic as a single field (Size ', &
          & offmax,' < Tolerance ',mosaic_tolerance,') '
        call map_message(seve%w,comm,chain)
        true_mosaic = .false.
      else
        chain = 'Mosaic with more than 1 primary beam'
        call map_message(seve%w,comm,chain)
        true_mosaic = .true.
      endif
    endif
  endif
  !
  if (true_mosaic) then
    call uv_map_mosaic(line,comm,error)
  else
    call uv_map_single(line,comm,error)
  endif
  !
  if (comm.eq.'UV_RESTORE') then
    if (true_mosaic) then
      last_shown = 'SKY'
    else
      last_shown = 'CLEAN'
    endif
  else
    last_shown = 'DIRTY'
  endif
  call check_view(1,last_shown)  ! Force VIEW re-computation for this
  !
  call map_message(seve%t,comm,'Uv_Map_Comm -- Finished',-2)
end subroutine uv_map_comm
!
subroutine uv_map_single(line,comm,error)
  use clean_arrays
  use clean_default
  use gbl_message
  use gkernel_interfaces
  use imager_interfaces, except_this => uv_map_single
  !---------------------------------------------------------------------
  ! @ private
  !*
  ! IMAGER -- Support for commands
  ! UV_MAP or UV_RESTORE /FIELDS /RANGE /TRUNCATE  /CONTINUUM /INDEX  
  !   or  
  ! UV_SELF /RANGE /RESTORE
  !!
  !---------------------------------------------------------------------
  character(len=*), intent(in) :: comm     !! Caller name
  character(len=*), intent(inout) :: line  !! Command line
  logical, intent(inout) :: error          !! Logical error flag
  !
  ! Constants
  real(kind=8), parameter :: pi=3.14159265358979323846d0
  real(kind=4), parameter :: sec_to_rad=pi/3600.0/180.0
  character(len=1), parameter :: question_mark='?'
  ! ' UV_MAP', '/TRUNCATE',  '/FIELDS', '/RANGE', '/CONTINUUM', '/INDEX'
  integer, parameter :: o_trunc=1
  integer, parameter :: o_field=2
  integer, parameter :: o_range=3
  integer, parameter :: o_cont=4
  integer, parameter :: o_index=5
  ! ' UV_SELF', '/RANGE', '/RESTORE'
  integer, parameter :: opt_rang=1  ! /RANGE Option for UV_SELF
  integer, parameter :: opt_rest=2  ! /RESTORE Option for UV_SELF
  ! Memories ...
  logical, save :: use_cont=.false.
  logical, save :: do_self=.false.
  !
  ! Local ---
  type(gildas) :: huv_local
  !
  type(imaging_mode) :: tmp_mode
  logical :: use_self, do_cct, do_init
  integer :: nline, ier
  character(len=16) :: acomm
  logical :: err, goon
  character(len=80) :: chain, string
  integer :: debug
  !
  ! Code ----
  call map_message(seve%t,comm,'Entering Uv_Map_Single',seve%e)
  debug = 0
  call sic_get_inte('DEBUG_UVMAP',debug,error)
  error = .false.
  !
  ! Check command line syntax
  call map_query (line,comm,themap,goon,error) 
  if ((.not.goon).or.error) return
  !
  err = .false.
  use_self = comm.eq.'UV_SELF'      ! Self Calibration image !
  if (use_self) then
    if (huv_current%gil%column_pointer(code_uvt_if).ne.0) then
      call map_message(seve%e,comm, &
      & 'This UV table (probably a result of UV_CONTINUUM) is not suited for self-calibration')
      error = .true.
      return
    endif
  endif
  do_init = hself_self%loca%size.eq.0 .or. sic_present(opt_rang,0)
  !
  !
  call sub_mosaic('OFF',error)
  call gildas_null(huv_local,type='UVT')
  !
  ! Single-Field, New version
  if (use_self) then
    !
    string = defmap_center
    if (.not.do_self) do_weig = .true.
    !
    ! Self-calibrated data 
    do_cct = sic_present(opt_rest,0) ! /RESTORE option
    if (.not.do_cct) then
      if (hself_self%loca%size.eq.0 .or. sic_present(opt_rang,0)) then
        call init_selfcal('UV_SELF',line,error)
        if (error) return
        ! ! call uv_dump_buffers('UV_SELF *')
        line = 'UV_SELF'
        nline = len_trim(line)
        call sic_analyse(acomm,line,nline,error)
      endif
    else 
      if (hself_self%loca%size.eq.0) then
        call map_message(seve%e,comm,'/RESTORE  Self calibrated UV data not defined')
        error = .true.
      else if (sic_narg(opt_rang).ge.0) then
        call map_message(seve%e,comm,'/RESTORE  conflicts with /RANGE')
        error = .true.
      endif
    endif
    if (error) return  
    !
    ! Here, sub_map_single works because the UV table is already sorted...
    call sub_map_single(comm,line,the_method,hself_self,do_cct,error)
    defmap_center = string          ! Restore MAP_CENTER
    hself_self%img%mode = code_img_cont
    if (error) return
    do_self = .true.
  else
    ! Normal UV_MAP or UV_RESTORE
    string = defmap_center
    do_self = .false.
    !
    ! Select current data set or possibly its Continuum sibbling 
    do_cct = comm.eq.'UV_RESTORE'
    ! In a UV_RESTORE command, USE_CONT is remembered from last UV_MAP command
    ! In a UV_MAP command, USE_CONT is defined by the /CONT option
    if (.not.do_cct) use_cont = sic_present(o_cont,0)
    !
    if (use_cont) then
      !
      ! Select Continuum data set (UV_MAP /CONT or UV_RESTORE after a UV_MAP /CONT)
      call switch_uv_cont(line,comm,do_cct,error)
      if (error) return
      !
      ! Perform the imaging
      if (do_cct) then
        call print_imaging(comm,'DO_CCT ',huv_cont%img) 
        defmap_center = ' '   ! The Continuum UV data has already been shifted
      endif
      tmp_mode = huv_cont%img ! Save the initial imaging mode
      call sub_map_single(comm,line,the_method,huv_cont,do_cct,error) 
      defmap_center = string 
      if (error) return
      !
      if (do_cct) then
        ! There can be a size mis-match here ...
        if (any(hcont%gil%dim(1:2).ne.hclean%gil%dim(1:2))) then
          write(chain,'(A,I0,A,I0,A)') 'Re-sizing continuum image to [', &
            &   hclean%gil%dim(1),',',hclean%gil%dim(2),']'
          call map_message(seve%w,comm,chain)
          deallocate(dcont,stat=ier)
          allocate(dcont(hclean%gil%dim(1),hclean%gil%dim(2)),stat=ier)
          call gdf_copy_header(hclean,hcont,error)
          hcont%gil%ndim = 2
          hcont%loca%size = hcont%gil%dim(1)*hcont%gil%dim(2)
          call sic_delvariable('CONTINUUM',.false.,error)
          call sic_mapgildas ('CONTINUUM',hcont,error,dcont)
        endif          
        !
        dcont(:,:) = dclean(:,:,1) ! Update CONTINUUM image too
        hcont%gil%minloc = hclean%gil%minloc
        hcont%gil%maxloc = hclean%gil%maxloc
        hcont%gil%rmin = hclean%gil%rmin
        hcont%gil%rmax = hclean%gil%rmax
      else
        do_continuum = .true.  ! Next CLEAN, there will be a CONTINUUM image
      endif
      saved_chan = tmp_mode  ! Restore the proper imaging mode
      huv_current%img = saved_chan
      huv_current%img%mode = code_img_cont
    else
      ! Cube Mode ---
      ! Select the data set. Can be UV_DATA, UV_MODEL, or UV_RESIDUAL
      if (current_uvdata.eq.'DATA_UV') then
        if (do_cct) defmap_center = ' '
        call sub_map_single(comm,line,the_method,huv_current,do_cct,error) 
        defmap_center = string
        huv_current%img%bounds = saved_chan%bounds
        huv_current%img%mode = code_img_line 
      else if (.true.) then
        call gildas_null (huv_local,type='UVT')
        call uv_select_data(comm,huv_local,error)
        if (do_cct) defmap_center = ' '
        call sub_map_single(comm,line,the_method,huv_local,do_cct,error) 
        defmap_center = string
        huv_local%img%bounds = saved_chan%bounds
        huv_local%img%mode = code_img_line !! This may not work...
      endif
      do_continuum = .false.  ! Next CLEAN will be no CONTINUUM
    endif
    if (error) return
  endif
  !
  if (.not.do_cct) call no_mosaic(comm)
  !
  call map_message(seve%t,comm,'Uv_Map_Single  -- Finished',-2)
end subroutine uv_map_single
