!
! @ image-mosaic	UV_Table  [Memory]
!	Image (but do not deconvolve) a Mosaic within a given Memory restriction
!
if (pro%narg.lt.1).or.("&1none".eq."?none") then
  SAY "Usage  "
  SAY "  @ image-mosaic UV_Table [Memory]"
  SAY " "
  SAY "Automatic imaging of a Mosaic UV Table within a given Memory limit"
  SAY "Imaging parameters guessed or taken from user"
  SAY "  "
  SAY "No Deconvolution step so far"
  return
endif
!
define character MyMosaic*64
sic parse &1 MyMosaic
!
clean\read uv 'MyMosaic'
!
! Check number of different weights / beams
clean\uv_check beams
define integer fchan lchan nranges
if exist(beam_ranges) then
  nranges = size(beam_ranges,2)
  define integer ranges[nranges] mrange krange
  ranges = beam_ranges[2,]-beam_ranges[1,]
  !
  mrange = 0
  for i 1 to nranges
    if (ranges[i].gt.mrange) then
      mrange = ranges[i]
      krange = i
    endif
  next
  !
  ranges[krange] = 0
  if any(ranges.gt.mrange/2) then
    say "More than 3 different synthesized beams - Cannot handle"
    return error
  else
    let fchan beam_ranges[1,krange]
    let lchan beam_ranges[2,krange]
  endif
  !
  say "Found more than one beam for all channels"
  say "   Dropping edges, keeping channel range ["'fchan'","'lchan'"]"
else
  nranges = 1
  fchan = 1
  lchan = uv%chan
endif
let nchan lchan-fchan+1 /new integer
!
clean\uv_stat advise
!
define long s_map s_uv s_left s_ram
let s_ram 0.8*sic%ramsize
if (pro%narg.gt.1) let s_ram &2*1024
exa s_ram
let s_map defmap%size[1]*defmap%size[2]/512/512*nchan  ! Size of MAP in GBytes
say "Map memory use " 's_map/1024' " Gbyte" /format A F9.2 A
let s_uv  uv%dim[1]*uv%dim[2]/512/512                     ! Size of UV in Gbytes
!
define integer nfields nb nc nt mc
nt = sic%openmp%nthreads ! Number of Threads
if exist(fields) then
  nfields = abs(fields%n)
  SAY "Data is a Mosaic of "'nfields'" fields"
  nt = min(nt,nfields)
  let s_map 2*s_map*nt
  say "UV_MAP memory use " 's_map/1024' " Gbyte" /format A F9.2 A
else
  SAY "Data is a Single field"
endif
!
if (s_map+s_uv).gt.0.9*s_ram  then
  s_left = s_ram-s_uv
  !
  ! How many channels can fit in s_left ?
  s_map = 2*defmap%size[1]*defmap%size[2]/512/512*nt
  nc = s_left/s_map
  if (nc.lt.nchan) then
    SAY "Data is HUGE, slicing into blocks of "'nc'" channels"
    let nb int((nchan+nc)/nc)
    let nc int(nchan/nb)+1
    SAY "Need "'nb'" blocks of "'nc'" channels"
  else
    SAY "Data is LARGE, 1 block of "'uv%nchan'" channels"
    nb = 1
    nc = nchan
  endif
else
  SAY "Problem fits into memory"
  nb = 1
  nc = nchan
endif
let mc lchan
!
define integer ifi ila
let ifi fchan
let ila min(ifi-1+nc,mc)
!
sic cpu
let elapsed sic%cpu%raw%elapsed /new double
for ib 1 to nb
  clean\uv_map /range ifi ila channel
  if (ib.eq.1) then
    clean\write * 'MyMosaic'
  else
    clean\write dirty 'MyMosaic' /append
  endif
  let ifi ifi+nc
  let ila min(ila+nc,mc)
next
sic cpu
let elapsed sic%cpu%raw%elapsed-elapsed
!
define charac string*12
let string elapsed/3600d0 /sexa H H
SAY 'ansi%green''ansi%bold'"I-MOSAIC,  UV_MAP completed, elapsed time "'string''ansi%none'
