subroutine check_area(method,head,quiet)
  use clean_def
  use image_def
  use gbl_message
  use mapping_interfaces, only : map_message, check_box
  !-----------------------------------------------
  ! @ private
  !
  ! MAPPING
  !   Check search area for Cleaning
  !-----------------------------------------------
  type (gildas), intent(in) :: head
  type (clean_par), intent(inout) :: method
  logical, intent(in) :: quiet
  !
  real ares,hmax
  integer max_iter0, nx, ny
  character(len=message_length) :: chain
  character(len=5) :: rname = 'CLEAN'
  !
  ! BLC & TRC
  nx = head%gil%dim(1)
  ny = head%gil%dim(2)
  call check_box(nx,ny,method%blc,method%trc)
  !
  ! Support for Clean
  method%box = (/method%blc(1),method%blc(2),   &
       &    method%trc(1),method%trc(2)/)
  !
  hmax = max(abs(head%gil%rmax),abs(head%gil%rmin))
  ares = method%ares
  if (ares.eq.0) then
    ares = method%fres*hmax
  endif
  if (ares.eq.0) then
    ares = head%gil%noise
  endif
  !
  if (.not.quiet) then
    write(chain,'(A,1pg11.3)') 'Cleaning down to ',ares
    call map_message(seve%i,rname,chain)
  endif
  !
  if (hmax.lt.ares) then
    max_iter0 = 0
    if (.not.quiet) then
      write(chain,'(A,1pg11.3,A)') 'Peak flux ',hmax,' is smaller, no Cleaning needed'
      call map_message(seve%w,rname,chain)
    endif
  else
    max_iter0 = log(ares/hmax)/log(1.0-method%gain)*  &
         &(method%trc(1)-method%blc(1)+1)*(method%trc(2)-method%blc(2)+1)/50.0
    if (max_iter0.lt.0) then
      Print *,'Unable to guess Iteration number - set to 100 '
      Print *,'Ares ',ares,'  Hmax',hmax,'  Gain ',method%gain
      max_iter0 = 100
    endif
    !
    if (.not.quiet) then
      if (ares.lt.head%gil%noise) then
        write(chain,'(A,1pg11.3)') 'Expected noise is larger... ',head%gil%noise
        call map_message(seve%w,rname,chain)
      endif
      write(chain,'(A,i12,a)') 'May need ',max_iter0,' iterations'
      call map_message(seve%i,rname,chain)
    endif
  endif
  !
  if (method%m_iter.eq.0) then
     method%m_iter = max_iter0
     method%ares = ares
  endif
end subroutine check_area
!
subroutine beam_plane(method,hbeam,hdirty)
  use clean_def
  use image_def
  use gbl_message
  use mapping_interfaces, only : map_message, beam_for_channel
  !----------------------------------------------------------------
  ! @ private
  !
  ! MAPPING
  !   Define beam plane and check if fit is required
  !----------------------------------------------------------------
  type (clean_par), intent(inout) :: method
  type (gildas), intent(in) :: hbeam
  type (gildas), intent(in) :: hdirty ! Used to figure out which plane
  !
  integer :: nstep, ibeam
  real(8) :: i_freq
  !
  ! Check if beam parameters must be recomputed
  if (hbeam%gil%dim(4).le.1) then
    method%ibeam = 1
  else
    ! Cannot set method%ibeam  directly ?
    ibeam = beam_for_channel(method%iplane,hdirty,hbeam)
    !!call map_message(seve%w,'CLEAN','Recomputing the beam parameters')
    method%ibeam = ibeam
    !! Print *,'IBEAM ',method%ibeam,'... '
  endif
end subroutine beam_plane
!
function beam_for_channel(iplane,hdirty,hbeam)
  use image_def
  !
  ! @ public
  !
  integer :: beam_for_channel !! intent(out)
  type(gildas), intent(in) :: hdirty
  type(gildas), intent(in) :: hbeam
  integer, intent(in) :: iplane
  !
  real(8) :: i_freq
  integer ibeam, nstep
  !
  if (hbeam%gil%dim(4).le.1) then
    beam_for_channel = 1
  else
    nstep = hdirty%gil%dim(3) / hbeam%gil%dim(4)
    if (hbeam%gil%dim(4)*nstep.ne.hdirty%gil%dim(3)) nstep = nstep+1
    ibeam = (iplane+nstep-1)/nstep
    !!Print *,'Approximate Beam channel ',ibeam,' for ',iplane
    ! This may be the better way of doing it:
    i_freq = (iplane-hdirty%gil%ref(3))*hdirty%gil%fres + hdirty%gil%freq
    ! i_freq = (ibeam-hbeam%gil%ref(4))*hbeam%gil%fres + hbeam%gil%freq
    ibeam = nint((i_freq-hbeam%gil%freq)/hbeam%gil%fres + hbeam%gil%ref(4))
    !!Print *,'Nearest Beam channel ',ibeam,' for ',iplane
    ibeam = min(max(1,ibeam),hbeam%gil%dim(4)) ! Just  in case
    !
    beam_for_channel = ibeam
  endif
end function beam_for_channel
!
subroutine clean_make90(method,hclean,clean,tcc)
  use clean_def
  use image_def
  use gkernel_interfaces
  use mapping_interfaces, only : mulgau
  !----------------------------------------------------------------
  ! @ private
  !
  ! MAPPING
  !   Build clean image from Component List
  !----------------------------------------------------------------
  type (clean_par), intent(inout) :: method
  type (gildas), intent(inout) :: hclean
  real, intent(inout) :: clean(hclean%gil%dim(1),hclean%gil%dim(2))
  type (cct_par), intent(in) :: tcc(method%n_iter)
  !
  real(8), parameter :: pi=3.141592653589793d0
  !
  integer nx,ny,nc,ix,iy,ic,imax,ndim,dim(2),ier
  real xinc, yinc,rmax,flux,r,fact
  real, allocatable :: wfft(:)
  complex, allocatable :: ft(:,:)
  !
  nx = hclean%gil%dim(1)
  ny = hclean%gil%dim(2)
  allocate (wfft(2*max(nx,ny)),ft(nx,ny),stat=ier)
  if (ier.ne.0) then
    Print *,'F-MAPPING,  Memory allocation error in CLEAN_MAKE90'
    STOP
  endif
  xinc = hclean%gil%convert(3,1)
  yinc = hclean%gil%convert(3,2)
  !
  if (method%method.eq.'SDI'.or.method%method.eq.'MULTI') then
    ft(:,:) = cmplx(clean,0.0)
  else
    !
    ! Check minimum flux
    ft = 0.0
    rmax = abs(tcc(1)%value)
    imax = 1
    nc = method%n_iter
    do ic=1,nc
      r = abs(tcc(ic)%value)
      if (r.lt.rmax) then
        imax = ic
        rmax = r
      endif
    enddo
    if (imax.lt.nc-10) then
    !            WRITE(CHAIN,*)
    !     $      'I-CLEAN,  Truncating CLEAN component to '
    !     $      ,IMAX,' / ',NC
    !            CALL GAGOUT(CHAIN)
    !            NC = IMAX
    endif
    !
    ! Convolve source list into residual map ---
    if (method%bshift(3).eq.0) then
      do ic=1,nc
        ix = tcc(ic)%ix
        iy = tcc(ic)%iy
        ft(ix,iy) = ft(ix,iy) + tcc(ic)%value
      enddo
    else
      do ic=1,nc
        flux = 0.5*tcc(ic)%value
        ix = tcc(ic)%ix
        iy = tcc(ic)%iy
        ft(ix,iy) = ft(ix,iy) + flux
        ix = ix+method%bshift(1)
        iy = iy+method%bshift(2)
        ft(ix,iy) = ft(ix,iy) + flux
      enddo
    endif
  endif
  !
  ndim = 2
  dim(1) = nx
  dim(2) = ny
  call fourt(ft,dim,ndim,-1,0,wfft)
  !
  ! Beam Area = PI * BMAJ * BMIN / (4 * LOG(2) ) for flux density
  ! normalisation
  fact = method%major*method%minor*pi/(4.0*log(2.0))   &
       &    /abs(xinc*yinc)/(nx*ny)
  call mulgau(ft,nx,ny,   &
       &    method%major,method%minor,method%angle,   &
       &    fact,xinc,yinc)
  call fourt(ft,dim,ndim,1,1,wfft)
  !
  ! Extract Real part
  clean = real(ft)
  deallocate (wfft,ft)
end subroutine clean_make90
