!
if .not.exist(box) @ d_box
!
if sic%program.eq."viewer" then
  @ define-viewer
  return
endif
sic message /color W=BLUE
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
! IMAGER specific definitions
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
SAY "I-IMAGER,  Executing ""@ define.ima"""
!
! Patch for a Bug on Mac-OS/X where Python blocks if activated first
!! from a script launched by a widget.
! The patch no longer works. It prevents IMAGER to be imported in Python
! This is a Mac-OS/X problem
!! python print("Starting Python")  ! Use Python 3 syntax
!! say "Python succesfully activated"
!
! Override INPUT, GO (no longer defined by the embedded GreG)
define command GO "@ p_go.ima" gag_pro:go_imager.hlp
define command INPUT "@ p_input.ima" gag_pro:go_imager.hlp
!
! And a few badly defined variables
if exist(box_limits) delete /var box_limits
define char*64 box_limits /global
if exist(command) delete /var command
define char*32 command /global
@ h_imager
!
if file("init.ima") then
  SAY 'ansi%bold'"I-IMAGER,  Executing ""@ init.ima""  (local initialisation file)"'ansi%none'
  @ init.ima
endif
!
! Generic definitions
if exist(imager_defined) RETURN
!
if (exist(color)) then
  dele /var color
  define real color[3] /global
  let color 20 360 0   ! A good default value
endif
define real show_color /global
let show_color 0       ! If non-zero, call COLOR -show_color command
!
if .not.exist(do_fields) then
  define logical do_fields /global
endif
!
! This is required for UV_MAP to work before VIEW
define structure view /global
define logical view%first /global
! Will be re-defined, but must be present
define character VIEW%CONT*8 /global
define structure view%do /global
define logical view%do%window view%do%extrema view%do%flux view%do%line /global
define real view%jyperk /global
!
if (.not.exist(map_field)) then
  ! Generic for old Mapping, but not used at all in IMAGER
  say "@ define.ima is only valid in IMAGER"
  return error
  !! @ old_mapping_define
else
  define real UV_TIME /global     ! Time averaging value
  ! This is for IMAGER,  Pre-set the "old" names
  let taper_expo 2
  let map_uvcell 0                ! Derive it from Beam size ...
  let map_robust 0                ! To be consistent with "Natural Weighting" definition by default
  let uv_cell 7.5 1         ! must match code definition for backward compatibility
  let weight_mode "NATURAL"  !    "        "
endif
!
! Compatibility patch
define alias MAP_BEAM_STEP beam_step /global
let map_power 2  ! Allow smaller images
!
define integer wcol mcol[2] /global
let mcol 0
let wcol 0                      ! Weight channel: Use "automatic" determination
!
! Cleaning
!
define character method*12 /global
define alias clean_method method /global
!   define integer niter beam_patch[2] blc[2] trc[2] positive /global
!   define double gain fres ares major minor angle /global
!
let method "hogbom" ! Cleaning method
let gain 0.2        ! Clean gain
let niter   0       ! Maximum number of clean components
let fres 0.0        ! Deconvolution will stop when | residuals | <= fres * max(dirty image)
let ares 0.0        ! Deconvolution will stop when | residuals | <= ares
let blc 0           ! Bottom, left corner: Default 0 0 for inner quarter
let trc 0           ! Top, right corner:   Default 0 0 for inner quarter
!let major 0         ! Clean Beam major axis (arc sec):      Default 0 for automatic
!let minor 0         ! Clean Beam minor axis (arc sec):      determination by Gaussian
!let angle 0         ! Clean Beam position angle (degrees):  fit of dirty beam
let beam_size 0     ! 0 means automatic fit of Beam size
let beam_patch 0    ! Default 0 0 ==> Inner Quarter
!
let space_type NATIVE
!
! Backward compatibility
if (exist(keep)) let keep yes        ! Keep cleaning after convergence
if (exist(clean_nkeep)) let clean_nkeep 70
if (exist(clean_smooth)) define alias smooth clean_smooth
if (exist(clean_ncycle)) define alias nmajor clean_ncycle
!
@ d_plotfit                         ! UV fit plots
  !
  ! UV display
  !
  define character xtype*16 ytype*16 /global
  define logical error_bars /global
  !
  define structure uvshow /global
  define character uvshow%marker*12 /global
  define integer uvshow%step /global
  define integer uvshow%antenna /global
  define character*8 uvshow%fit /global
  define logical uvshow%track /global
  define logical uvshow%zero  /global
  define logical uvshow%flag  /global
  !
  let xtype "radius"  ! time, number, u, v, radius, angle
  let ytype "amp"     ! amp, pha, real, imag, weight, scan
  let error_bars no   ! Do not plot error bars
  !
  let uvshow%marker "4 1 .1" ! Point markers
  let uvshow%zero  yes       ! Display zero level
  let uvshow%track yes       ! Give one color per track
  let uvshow%step 12         ! 12 hours to separate 2 tracks
  let uvshow%fit  NO         ! Do not plot model from uv_fit
  let uvshow%flag no         ! But default, "go uvshow" is not
                            ! called by command UV_FLAG
  let uvshow%antenna 0       ! By default, no antenna highlighted
  !
!
! Basic symbols to provide customary abbreviations
!
symbol write "CLEAN\WRITE"
symbol cle*ar "GTVL\CLEAR"
!
! UV_SHORT
define real SHORT_SD_FACTOR SHORT_SD_WEIGHT SHORT_UV_MAX SHORT_UV_MIN /global
let short_sd_weight 1.0
!
! SHOW with permutted axes
define double span[2] /global
!
! SHOW MOMENTS
define double peak_spacing /global
define double area_scale[2] peak_scale[2] velo_scale[2] width_scale[2] /global
!
let command "input"
!
! Define USER commands before any other one (bug on the Help)
define command SOLVE_FLUX "@ solve_flux"        gag_pro:solve_flux.hlp
!
! Re-define SPACING
if .not.exist(spacing_rule) then
  define structure spacing_rule /global
  define double spacing_rule%r spacing_rule%s spacing_rule%n /global
  define char spacing_rule%c*8 spacing_rule%l*4 /global
endif
if exist(spacing) then
  if typeof(spacing).lt.0 then
    ! Old mode compatibility
    define character cspacing*16
    let cspacing spacing /format 1PG14.6 ! Avoid bit extension issues
    delete /var spacing
    define char*16 spacing[2] /global
    let spacing 'cspacing'
    delete /var cspacing
  else
    delete /var spacing
    define char*16 spacing[2] /global  
  endif
else
  define char*16 spacing[2] /global  
endif
let spacing[2] Units
let spacing_rule%c "Units"
!
! Success
define logical imager_defined /global
let imager_defined yes
!
if exist(omp_map%uv_map_lock) let omp_map%uv_map_lock NO
!
! Global READ optimization
let mapping_optimize 1
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
! Self Calibration (variables and scripts)
@ p_selfcal
@ h_selfcal
!
! Popup command mode
define chara PopupName*16 /global
define real  PopupExpand  /global
!
! Currently, this is initialized in the code because of a bug in the
! LINEDB\USE IN command which cannot (yet) be used in procedures.
if file("~/imager.linedb") then
  catalog ~/imager.linedb
else if file("gag_data:imager.linedb") then
  catalog gag_data:imager.linedb
else if file("gag_data:imager-linedb.sic") then
  SAY 'ansi%bold'"No Imager Line Data base in $HOME"'ansi%none'
  SAY 'ansi%bold'"Use ""@ gag_data:imager-linedb.sic"" to create one"'ansi%none'
  SAY 'ansi%bold'"or specify your own catalog with the CATALOG command"'ansi%none'
endif
catalog ?
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
begin procedure side
  !
  ! Create or Open the "Side" graphic window
  gtvl\gtv search <vside
  if (.not.gtv%exist) then
    gtvl\create direct <vside /plot_page 10 10 /geometry 256 256 /pos 100% 100%
    gtvl\change directory <vside
    greg1\set box 0 10 0 10
  else
    gtvl\change directory <vside
    gtvl\clear directory
  endif
end procedure
!
begin procedure get_flux
  if .not.exist(poly%nxy) then
    say "Define polygon"
    greg2\polygon
  endif
  if (&1%major.ne.0) then
    support poly /var 
    flux support
  endif
end procedure
!
! S.Guilloteau - Automatic install of casa2gildas.py tools if possible
!
if .not.file("$HOME/.casa") return      ! This is not a CASA user
!
! This is a CASA user
begin procedure casa-setup
  if file("$HOME/.casa/&1.py") then
    ! He already has an &1.py
    if .not.sic%systemerror sic system error
    if sic%systemerror then
      define logical failed
      on error continue     ! To avoid pausing here
      system "grep GILDAS ~/.casa/&1.py > /dev/null "
      let failed sic%error  ! Using SIC%ERROR directly is not safe
      ! If found, this is OK - If not, append the GILDAS initialisation
      if failed then
        SAY "W-DEFINE,  Appending gag_data:casa.&1.py.src to ~/.casa/&1.py"
        SIC APPEND gag_data:casa.&1.py.src ~/.casa/&1.py
      else
        SAY "I-DEFINE,  casagildas() tool already available in your .casa/&1.py"
      endif
      on error
    else
      ! No way to check if this &1.py contains the GILDAS stuff
    endif
  else
    !   he has no &1.py
    if file("gag_data:casa.&1.py.src") then
      !   install the Gildas one.
      SAY "I-DEFINE,  Creating ~/.casa/&1.py from gag_data:casa.&1.py.src"
      SIC COPY gag_data:casa.&1.py.src ~/.casa/&1.py
    endif
  endif
  !
  sic find $PYTHONPATH/casa2gildas.py
  if dir%nfile.eq.0 return
  sic copy 'dir%file' $HOME/.casa/casa2gildas.py
end procedure
!
@ casa-setup init     ! Python Version 2, CASA Version < 6
@ casa-setup startup  ! Python Version 3, CASA Version > 5
