!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
! VIEWER specific definitions
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
if exist(viewer_defined) RETURN
!
SAY "I-VIEWER,  Executing ""@ define-viewer.ima"""
let type " "
!
! 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"
!
! Generic definitions
!
! 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
define command VIEwER  "@ welcome.ima" gag_pro:viewer.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
!
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
!
@ 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 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"
!
! 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 viewer_defined /global
let viewer_defined yes
!
! 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 get_flux
  say "Define polygon"
  greg2\polygon
  !
  define real theflux
  greg2\mean
  let theflux poly%sum|pi|&1%major|&1%minor*4*log(2)
  if (theflux.lt.0.03) then
    if (theflux.gt.0.001) then
      say "Total Flux "'0.1*nint(theflux*10000.)'" mJy"
    else
      say "Total Flux "'nint(theflux*1E6)'" microJy"
    endif
  else if (theflux.lt.1.0) then
    say "Total Flux "'nint(theflux*1000.)'" mJy"
  else
    say "Total Flux "'0.01*nint(theflux*100.)'" Jy"
  endif
end procedure
!
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
!
