if (.not.exist(imager)) then
  !
  ! IMAGER is the ACTIVE structure. It inherits the MAPPER values 
  define structure imager /global
  ! Velocity range
  define real imager%range[2] /global
  ! Source velocity
  define character imager%velocity*12 /global
  ! Rest frequency
  define character imager%frequency*24 /global
  ! Velocity resolution
  define character imager%resolution*12  /global
  ! Line name
  define character imager%line*12 /global
  ! Integration time
  define real imager%time /global
  !
  ! Subtract Continuum
  define logical imager%nocont /global
  ! Clone the structure
  ! MAPPER is the User Specified Structure
  define structure mapper /like imager /global
  let mapper%nocont yes
  !
endif
!
if pro%narg.ne.0 then
  let name &1
endif
if name.ne." " then
  if exist(mapper%head) dele /var mapper%head
  defin head mapper%head 'name'.uvt write /global
  if mapper%frequency.ne." " then
    modify mapper%head /frequency 'mapper%head%line' 'mapper%frequency'
  endif
  let mapper%head /status read
endif
!
gui\panel "GILDAS Imager" gag_scratch:gildas-imager.hlp 
  ! Top buttons
  gui\button "@ imager_setup"      "Read   " 
  gui\button "@ imager_save"       "UPDATE" 
  gui\button "@ imager_preview DO" "Preview"   
  gui\button "@ imager_clean DO"   "Image  " 
  gui\button "view clean"          "View CLEAN" 
  gui\button "show continuum"      "Show CONTINUUM"
  gui\button "@ imager_write"      "WRITE  "
  ! Main panel
  let name 'name' /Prompt "Generic name"
  if exist(mapper%head) then  
    define double v1 v2 
    define character ch1*18 ch2*18
    let mapper%velocity 'mapper%velocity' /Prompt -
      "Source velocity (km/s) ["'mapper%head%veloff'"] " 
    let mapper%resolution 'mapper%resolution' /Prompt - 
      "Desired velocity resolution (km/s) ["'0.01*nint(100*mapper%head%velres)'"] "
    let ch1 mapper%head%restfre /format F18.6
    let mapper%frequency 'mapper%frequency' /Prompt "Rest frequency (MHz) ["'ch1'"] "
    let mapper%line 'mapper%line' /Prompt "Line name ["'mapper%head%line'"] "  
    v1 = (0.5d0-mapper%head%convert[1,1])*mapper%head%velres+mapper%head%veloff
    v2 = (mapper%head%nchan+0.5d0-mapper%head%convert[1,1])*mapper%head%velres+mapper%head%veloff
    let ch1 min(v1,v2) /format F8.1 
    let ch2 max(v1,V2) /format F8.1 
    let mapper%range 'mapper%range[1]' 'mapper%range[2]' /Prompt -
       "Velocity Range (km/s) ["'ch1'","'ch2'" ] "
    let mapper%time 'mapper%time'  /Prompt -
       "UV data integration time (s) ( <"'uv_time'" ) "    
  else
    let mapper%velocity 'mapper%velocity' /Prompt "Source velocity (km/s)"
    let mapper%resolution 'mapper%resolution' /Prompt "Desired velocity resolution (km/s)"
    let mapper%frequency 'mapper%frequency' /Prompt "Rest frequency (MHz)"
    let mapper%line 'mapper%line' /Prompt "Line name" 
    let mapper%range 'mapper%range[1]' 'mapper%range[2]' /Prompt "Velocity Range (km/s)"
    let mapper%time 'mapper%time'  /Prompt -
       "UV data integration time (s)"
  endif
  let mapper%nocont 'mapper%nocont' /Prompt "Separate Continuum"
  let imager%time 0 ! Reset smoothing time ?
gui\go "@ imager_run"
!
if (exist(mapper_init)) return
define logical mapper_init /global
!
! First initialization
sic precis auto          ! Automatic precision for formatting
!
let map_power 2          ! Uses smallest possible map size
let mapping_optimize 1   ! Optimize reading 
let fres 0               ! The default of 2.5 % is stupid
let mapper%time -1       ! Automatic Nyquist time sampling
!
begin procedure imager_save
  @ imager_update
  sic output imager_parameters.last
  say "let name "'name'  "/Prompt ""Generic name"""
  say " "
  say "SAY "" --- Recovering parameters for """ 'name' """ ---"""
  say " "
  say "let mapper%velocity   " 'imager%velocity'   "/Prompt ""Source velocity (km/s)"""
  say "let mapper%resolution " 'imager%resolution' "/Prompt ""Desired velocity resolution (km/s)"""
  say "let mapper%frequency  " 'imager%frequency'  "/Prompt ""Rest frequency (MHz)"""
  say "let mapper%line       " 'imager%line'       "/Prompt ""Line name""" 
  say "let mapper%range      " 'imager%range[1]' 'imager%range[2]' "/Prompt ""Velocity Range (km/s)"""
  say "let mapper%nocont     " 'mapper%nocont'     "/Prompt ""Subtract continuum"""
  sic output
  !
  SAY "----------------------------------------------------------------"
  SAY "Imaging "'name'" around Line "'imager%line'" at "'imager%frequency' -
      " at spectral resolution ["'imager%resolution'"]"
  if any(imager%range.ne.0) then
    SAY " with spectral coverage ["'imager%range[1]'","'imager%range[2]'"] km/s"
  endif
end procedure imager_save

begin procedure imager_setup
  if exist(mapper%head) dele /var mapper%head
  defin head mapper%head 'name'.uvt read /global
  let imager% mapper%
  if mapper%line.eq." " then
    let imager%line 'mapper%head%line' /Prompt "Enter line name (no spaces)"
  else
    let imager%line 'mapper%line'
  endif
  !
  ! Read all the UV data
  read uv 'name' 
  !
  @ imager_update
  !
  SAY "----------------------------------------------------------------"
  SAY "Imaging "'name'" around Line "'imager%line'" at "'imager%frequency' -
      " at spectral resolution ["'imager%resolution'"]"
  if any(imager%range.ne.0) then
    SAY " with spectral coverage ["'imager%range[1]'","'imager%range[2]'"] km/s"
  endif
end procedure imager_setup
!
begin procedure imager_update
  ! Update ranges and Velocity Frame
  let imager% mapper%
  if (imager%frequency.eq." ") then
    specify frequency 'mapper%head%restfre'
  else
    specify frequency 'imager%frequency'
  endif
  if (imager%velocity.eq." ")  then
    specify velocity 'mapper%head%veloff'
  else
    specify velocity 'imager%velocity'
  endif
  ! Change the line name
  if (imager%line.ne." ") then
    specify linename 'imager%line'
  endif
  !
  if (mapper%time.ne.0).and.(mapper%time.ne.imager%time) then
    let imager%time 'mapper%time'
    if imager%time.lt.0 then
      uv_time
    else
      uv_time 'imager%time'
    endif
    do_weig = YES
  endif
  !
  ! UV_RESAMPLE is able to work with only VELOCITY and VRES as argument
  if (imager%resolution.ne." ") then
    let imager%resolution 'mapper%resolution'
    if (imager%velocity.eq." ") 
      UV_RESAMPLE * * 'mapper%head%veloff' 'imager%resolution' 
    else
      UV_RESAMPLE * * 'imager%velocity' 'imager%resolution' 
    endif
    do_weig = yes
  else
    let imager%resolution 'mapper%head%velres'
  endif
  if any(imager%range.ne.0) let do_weig = YES
  !
  @ imager_init
end procedure imager_update
!
begin procedure imager_cont
  if ("&1NONE".ne."NONE") @ imager_update 
  ! 1) Continuum
  uv_filter
  uv_map /cont
  clean
  uv_restore
  ! 2)  Imaging without continuum"
  uv_baseline
  uv_map
  clean
  uv_restore
  ! 3)  Imaging with better continuum"
  define image spectral * real /like clean /global
  map_combine spectral ADD clean continuum
  stat spectral
end procedure
!
begin procedure imager_clean
  if ("&1NONE".ne."NONE") @ imager_update 
  !
  if mapper%nocont then
    @ imager_cont 
  else
    if (exist(continuum)) discard continuum
    if (exist(spectral)) delet /var spectral
    if do_weig then
      if any(imager%range.ne.0) then
        uv_map /range 'imager%range[1]' 'imager%range[2]' VELOCITY
      else
        uv_map
      endif
    endif
    clean 
    view cct
  endif
end procedure imager_clean
!
begin procedure imager_write
  if exist(continuum) then
    write continuum 'name'-'imager%line'"+C.lmv-clean"
    write spectral 'name'-'imager%line'"+A.lmv-clean"
  endif
  write * 'name'-'imager%line'
end procedure imager_write
!
begin procedure imager_run
  @ imager_setup
  @ imager_update
  @ imager_preview
  @ imager_clean
  view clean
  @ imager_save
  @ imager_write
end procedure imager_run
!
begin procedure imager_preview
  if ("&1NONE".ne."NONE") @ imager_update 
  !
  uv_preview
  if exist(preview%line) then
    if (preview%nfound.gt.0) then
      let mapper%frequency preview%freq /format F14.6
      let mapper%line  preview%line /format A
  !!    if ("&1NONE".ne."NONE") @ imager_update 
      @ imager_update
    endif
  endif
end procedure imager_preview
!
begin data gag_scratch:gildas-imager.hlp
1 Simple Imager
  The Simple Imager is a widget to drive the imaging commands 
  one image out of a UV data set that may contain many channels and
  many different spectral lines.
  
  The controls allow to specify in a simple way which spectral 
  line is to be imaged, and other what velocity range. 
  
  Standard UV_MAP and CLEAN parameters are used for the Imaging.
  
2 NAME

  Generic name of the data set.  IMAGER will read (and write) files
  with this name, and a data type dependent file extension (e.g. .uvt
  for UV tables,  .lmv-clean for Cleaned images, .msk for masks)
  
2 MAPPER%VELOCITY

  Source velocity to be used to define the Rest Frequency frame.
  
2 MAPPER%RESOLUTION

  Desired spectral resolution. The data will be resampled to this
  resolution
  
2 MAPPER%FREQUENCY

  Rest frequency to which the velocity frame is referred to.
  
2 MAPPER%LINE

  Spectral line name (only for bookkeeping purpose)

2 MAPPER%RANGE

  Velocity range used to generate the images.
  
2 MAPPER%TIME

  Integration time used to smooth the data before imaging. This speeds
  up the imaging (UV_MAP) and restoration process (UV_RESTORE). If
  set to -1, the data will be smoothed to the Nyquist sampling time.
 
2 MAPPER%NOCONT

  Specify whether Continuum must treated or not in the imaging process.
  Default is YES. It will create files ending with +C.lmv-clean for the
  Continuum (CONTINUUM buffer), and +A.lmv-clean for the line data 
  (SPECTRAL buffer) that also includes the continuum, while the line-only 
  data (CLEAN buffer) will be in the .lmv-clean usual file.
  
end data gag_scratch:gildas-imager.hlp
return

 
