!
! @ all-widget
!
!   Launch the widget interface for the all-* scripts
! that drive the multi spectral window imaging.
!
defin log already_done
if (.not.exist(all)) then
  @ all-create
else
  let already_done yes
endif
@ d_sed
!
begin procedure add_argument
  let  &1 '&1'" "'&2'
end procedure add_argument
!
begin procedure make_let_command
  define integer nloop
  let nloop size(&1)
  !
  define chara*256 long_string /global
  !
  let long_string "let &1" 
  if (nloop.eq.0) then
    @ add_argument long_string '&1'
  else
    for i 1 to nloop
      @ add_argument long_string '&1[i]'
    next
  endif
  symbol &2  'long_string'
  delete /variab long_string 
end procedure
!
@ make_let_command all%phase_niter pha_niter
@ make_let_command all%phase_times pha_times
@ make_let_command all%ampli_niter amp_niter
@ make_let_command all%ampli_times amp_times
!! @ make_let_command self_minflux let_minflux

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
gui\panel "Imaging PIPELINE control panel" gag_scratch:xima_all.hlp 
gui\button "@ all-layout" "ORGANIZE   |"
gui\button "@ all-list"   "FIND       |"
gui\button "@ all-self *"         "SELF-CALIBRATION   |"
gui\button "@ all-process TIME"   "TIME Averaging   |"
gui\button "@ all-process IMAGE"  "IMAGING          |"
gui\button "@ all-process TABLES" "TABLES Creation  |" 
!
gui\button "@ all-command *" "Whole PIPELINE" 
!
let all%telescope 'all%telescope'   /choice /prompt "Telescope"
if .not.exist(linedb%dbin) then
  let all%catalog " (none defined) " 
else
  let all%catalog 'linedb%dbin'
endif
let all%mode 'all%mode'  /Prompt "Pipeline Mode" /Choice CONTINUUM ALL SPLIT SURVEY
let all%catalog 'all%catalog' /choice /Prompt "Spectral Line Catalog"
if (exist(redshift)) then
  let redshift 'redshift' /Prompt "Source Redshift"
endif
say " "
!
let all%drop 'all%drop[1]' 'all%drop[2]' /prompt "Edge channels dropped"
let all%filter 'all%filter' /prompt "File filter (NO extension !..)"
let all%range 'all%range[1]' 'all%range[2]' 'all%range[3]' /prompt "Velocity Range & Resolution (km/s)"
let all%minfres  'all%minfres'  /prompt "Maximum Frequency Resolution (MHz)"
!
say " "
say " "
gui\button "@ all-pipeline *"  PIPELINE "Execute all steps"  gag_scratch:xima_pipe.hlp "Pipeline"
say " "
say " "
!
gui\button "PIPELINE ORGANIZE" ORGANIZE "Create and/or move .UVT files to proper place" gag_scratch:xima_layout.hlp "Organization"
  let all%uvfits 'all%uvfits' /prompt "Initial UVFITS files sub-directory"
  let all%raw 'all%raw' /prompt "Raw UV Tables sub-directory"
  let all%self 'all%self' /prompt "Self calibrated or Time compressed UV Tables sub-directory"
  let all%maps 'all%maps' /prompt "Images sub-directory" 
  let all%tables 'all%tables' /prompt "Final UV Tables sub-directory"

gui\button "PIPELINE FIND" FIND "Find and List UV tables" gag_scratch:xima_list.hlp "File List"
  let all%filter 'all%filter' /prompt "File filter (NO extension !..)"

gui\button "PIPELINE SELECT" SELECT "Select UV tables to process" gag_scratch:xima_find.hlp "File Selection"
  let all%filter 'all%filter' /prompt "File filter (NO extension !..)"
  let all%minband  'all%minband'  /prompt "Minimum bandwith for self calibration (MHz)"
! 
gui\button "PIPELINE CHECK" CHECK "Check Self-Cal feasibility" gag_scratch:xima_self.hlp "Check Self Calibration"
gui\button "PIPELINE COMPUTE" COMPUTE "Self-Calibration Solution" gag_scratch:xima_self.hlp "Compute Self Calibration"
  let all%prefix_self 'all%prefix_self'" " /prompt "Prefix for self-calibrated files"
  pha_niter /prompt "PHASE: Number of clean components for each loop" /resize
  pha_times /prompt "PHASE: Integration time for each loop" /resize
  amp_niter /prompt "AMPLI: Number of clean components for each loop" /resize
  amp_times /prompt "AMPLI: Integration time for each loop" /resize
!
gui\button "PIPELINE COLLECT" COLLECT "Merge Self-Calibration" gag_scratch:xima_collect.hlp "Merge Self-calibration"
  let all%collect 'all%collect'  /prompt "Phase Stability Rejection Threshold" 
!
gui\button "PIPELINE APPLY" APPLY "Apply Self-Calibration" gag_scratch:xima_apply.hlp "Apply Self-calibration"
!
gui\button "PIPELINE SHOW" SHOW "Show Self-calibration" gag_scratch:xima_apply.hlp "Show Self-calibration"
  let self_hard 'self_hard' /Prompt "Make Hardcopy of plots"
!
gui\button "PIPELINE TIME" TIME  "Time compress UV tables"  gag_scratch:xima_time.hlp "Time compression" 
  let all%itime 'all%itime'  /prompt "Integration time (s) -- <0 means guess" 
!
gui\button "PIPELINE IMAGE" IMAGING  "Imaging" gag_scratch:xima_image.hlp "Image everything"
  let all%range 'all%range[1]' 'all%range[2]' 'all%range[3]' /prompt "Velocity Range & Resolution (km/s)"
  let all%minfres  'all%minfres'  /prompt "Maximum Frequency Resolution (MHz)"
  let all%restore 'all%restore' /prompt "Use UV_RESTORE after Clean"
  !
  let map_size 'map_size[1]' 'map_size[2]' /prompt "Image size"
  let map_field 'map_field[1]' 'map_field[2]' /prompt "Field  size"
  let map_size 'map_cell[1]' 'map_cell[2]' /prompt "Pixel size"
  let map_robust 'map_robust'  /prompt "Robust factor"
!
gui\button "PIPELINE TABLES" TABLES  "Spectral Table extraction" gag_scratch:xima_tables.hlp "Spectral Table extraction"
  let all%resample 'all%resample' /prompt "Resample UV Tables"
  let all%range 'all%range[1]' 'all%range[2]' 'all%range[3]' /prompt "Velocity Range & Resolution (km/s)"
  let all%minfres  'all%minfres'  /prompt "Maximum Frequency Resolution (MHz)"
!  
gui\button "PIPELINE SKY" SKY "Apply Primary Beam correction"  gag_scratch:xima_self.hlp "Primary Beam correction"
gui\button "@ p_sed"  Spectral_Index "SED and Spectral Index display" gag_pro:p_sed.hlp "SED and Spectral Index display"
  let sed%filter 'all%filter' /prompt "File filter"
  let sed%type   'sed%type'   /prompt "File extension"
  let sed%freq   'sed%freq'   /prompt "Reference frequency"
  let sed%name   'sed%name'   /prompt "Intermediate result file name"
  let sed%model  'sed%model'  /prompt "Source model type"
!
on error return
gui\go "@ all-pipeline *"
!
if (already_done) then
  return
endif
!
begin data gag_scratch:xima_all.hlp
1 Automatic Imager

  Pipeline to image an ensemble of UV tables, of extension .uvt
  It provides an automatic identification of spectral lines for each 
  UV table, with line removal to derive the corresponding continuum data.
  
  This automatic imager offers a number of steps:
  - ORGANIZE      Move the .UVT files to the appropriate place, creating them 
                from the .UVFITS files if needed
  - LIST          Get a list of UV tables properties
  - FIND          Build the list of Wide Band UV tables
  - SELF          Compute Self calibration from Wide Band UV tables.
  - COLLECT       Derive pathlength corrections from Self calibration solutions.
  - APPLY         Apply Self calibration to all (narrow and wide) UV tables. 
  - TIME          Time compression of the self calibrated UV tables
  - IMAGING       Imaging a velocity range around each spectral line, with 
                continuum suppression by baseline fitting, and imaging separately 
                the line-free continuum data.
  - TABLES 
                  Creation of the corresponding continuum-free spectral line 
                window and line-free continuum UV data. 

  The automatic imager can be used as a non-interactive pipeline using the GO 
  or PIPELINE buttons,  or manually through command PIPELINE
  
  Normal user-specified imaging and cleaning parameters (including support
  and or mask) apply to the process. 
  
  For self calibration, the PHASE part can be controlled by specific 
  variable ALL%PHASE_TIMES and ALL%PHASE_NITER (which are used to
  set SELF_TIMES and SELF_NITER in the Self calibration process), while 
  the AMPLI part uses ALL%AMPLI_TIMES and ALL%AMPLI_NITER.
  
2 TELESCOPE
    Name of the telescope used for the observations. This name is used
  to setup some default parameters, such as integration times for self-calibration.
  It is usually derived from the observed data set, but can be specified
  if needed.
  
2 ALL%CATALOG
    Name of spectral line catalog. This is defined using the CATALOG
  command. See HELP CATALOG for details.
  
  If a line catalog is selected, the all-in-one scripts will produce one
  image for each spectral line falling into the observed "high resolution"
  spectral windows, over the specified velocity range ALL%RANGE.
  
  If no line catalog is selected, all channels of all "high resolution"
  spectral windows will be imaged.  No velocity range is used in this case.
  
  "Low resolution" spectral windows will be used to produce continuum
  only images. The distinction between "Low" and "High" resolution spectral
  windows is made from variable ALL%MINFRES
  
2 ALL%DROP

     Number of channels dropped at each edge of a spectral band.
  0 0 is acceptable for Single fields, 5 5 is recommended for Mosaics.
  
2 ALL%FILTER

    File filter to be used to select the relevant UV tables.

  This filter MUST NOT contain the .uvt extension. The default filter
  is *, but a specific file name can be given to process only one
  UV table instead of all of them.

2 ALL%RANGE

  3-element array containing the Velocity range selected around each 
  spectral line, followed by the Velocity resolution (all in km/s).
  
2 ALL%MINFRES

  Maximum frequency resolution (in MHz) under which a UV Table is considered
  as a spectral line data. UV Tables with coarser spectral resolution
  are treated as continuum only data.
  
end data gag_scratch:xima_all.hlp

begin data gag_scratch:xima_pipe.hlp
1 Pipeline

  Pipeline imager for an ensemble of UV tables. It executes all setps
  of the automatic imager in sequence.
  - ORGANIZE    Put the .uvt table at appropriate place
  - LIST        Get a list of UV tables properties
  - FIND        Build the list of Wide Band UV tables
  - SELF        Compute Self calibration from Wide Band UV tables.
  - COLLECT     Derive pathlength corrections from Self calibration solutions.
  - APPLY       Apply Self calibration to all (narrow and wide) UV tables. 
  - TIME        Time compression of the self calibrated UV tables
  - IMAGING     Imaging from self-calibrated, time compressed UV tables
  
  The Pipeline can also be activated from the GO button, or through the
  PIPELINE command; See HELP PIPELINE for more details.

end data gag_scratch:xima_pipe.hlp
!
!
begin data gag_scratch:xima_layout.hlp
1 ORGANIZE

  Prepare the proper directory layout for automatic processing.
  
  This step moves the original UV tables (*.uvt files) to the 'all%raw'
  directory, creating them from the UVFITS files (*.uvfits files) if
  needed. UVFITS files are moved to the 'all%uvfits' directory too.
  
  Nothing happens if the layout is adequate.
end data gag_scratch:xima_layout.hlp 
!
begin data gag_scratch:xima_collect.hlp
1 COLLECT

  Interpolates the self-calibration solution as a function of
  frequency from solutions derived from several wide bands, assuming the
  corrections are due to pathlength (delay) differences for phases,
  or are constant as a function of frequency for amplitudes.

2 ALL%COLLECT

  Stability Threshold. For Self-calibration solutions with more than
  one iteration, the solution is flagged if the difference between the
  last two iterations exceeds Threshold times the expected phase error
  due to noise.

  The default is 0, a convention indicating to ignore this flagging step.

  When the noise level is appropriately estimated, a Threshold of 3
  is a good value to suppress unstable solutions that reflect some
  issues in the self-calibration model.
  
end data gag_scratch:xima_collect.hlp 
!
begin data gag_scratch:xima_list.hlp 
1 FIND

    Find and List UV tables. This tool also automatically derives
  what is the best criterium to define "wide" vs "narrow" bands
  for further use by SELECT.
  
2 ALL%FILTER

    File filter to be used to select the relevant UV tables.

  This filter MUST NOT contain the .uvt extension. The default filter
  is *, but a specific file name can be given to process only one
  UV table instead of all of them.

end data gag_scratch:xima_list.hlp
!
begin data gag_scratch:xima_find.hlp 
1 SELECT

    Identify UV tables. Find which ones are wide bands, and which
  are narrow bands. Associate them so that Self calibration (computed
  from Wide bands only) can be applied to properly to other UV tables.
  
2 ALL%FILTER

    File filter to be used to select the relevant UV tables.

  This filter MUST NOT contain the .uvt extension. The default filter
  is *, but a specific file name can be given to process only one
  UV table instead of all of them.

2 ALL%MINBAND

  Minimum bandwidth (in MHz) for a UV Table to be considered as a Wide
  Band data, and used to compute a self calibration solution. 

  UV Tables with smaller bandwidth will use the self calibration 
  solution from matching wide bands, (or an adequate interpolated delay 
  correction if the COLLECT step has been performed).

end data gag_scratch:xima_find.hlp
!
begin data gag_scratch:xima_self.hlp
1 SELF

  Self-calibrate the selected UV tables.

  The self-calibration is computed from the "Wide-band" UV tables
  (the UV tables with a bandwidth larger than ALL%MINBAND)
  and applied to all UV tables falling in the corresponding frequency
  coverage.

  The self-calibration begins by PHASE first, followed by AMPLItude
  part. The self-calibration uses the original UV table, without time
  averaging.
  
2 ALL%AMPLI_TIMES

  Variable length array specifying the integration times (in seconds)
  for every amplitude self-calibration step. Default is 120 s.
  
2 ALL%AMPLI_NITER

  Variable length array specifying the number of Clean components
  selected for every amplitude self-calibration step. 0 means all.
  
2 ALL%PHASE_TIMES

  Variable length array specifying the integration times (in seconds)
  for every phase self-calibration step. Default is 45 s, appropriate
  for NOEMA, but shorter values may be better for ALMA.
  
2 ALL%PHASE_NITER

  Variable length array specifying the number of Clean components
  selected for every phase self-calibration step. 0 means all.
end data gag_scratch:xima_self.hlp
!
begin data gag_scratch:xima_apply.hlp
1 APPLY

  Apply self-calibration (computed by SELF widget) to all UV tables.
  Proper matching between the narrow and wide bands is ensured.

end data gag_scratch:xima_apply.hlp
!
begin data gag_scratch:xima_time.hlp
1 TIME

  Time compress the UV tables of the SELF directory.

2 ALL%ITIME

  Integration time for time compression of the UV Tables.
  0 means the Nyquist sampling time, which depends on maximum
  baseline length and field of view.
end data gag_scratch:xima_time.hlp
!
begin data gag_scratch:xima_tables.hlp
1 TABLES

  Extract the UV tables corresponding to the specified velocity
  range for each spectral line identified in the data set.

2 ALL%RANGE

  3-element array containing the Velocity range selected around each 
  spectral line, followed by the Velocity resolution (all in km/s).
  
  Velocity resolution is currently ignored for Table creation.
  
2 ALL%MINFRES

  Maximum frequency resolution under which a UV Table is considered
  as a spectral line data. UV Tables with coarser spectral resolution
  are treated as continuum only data.
end data gag_scratch:xima_tables.hlp
!
begin data gag_scratch:xima_image.hlp
1 IMAGING
  Image the specified velocity range around each spectral line 
  identified in the data set. Normal imaging parameters
  (BEAM_*, MAP_* and CLEAN_* variables) apply.

2 ALL%RANGE

  3-element array containing the Velocity range selected around each 
  spectral line, followed by the Velocity resolution (all in km/s).
  
2 ALL%MINFRES

  Maximum frequency resolution under which a UV Table is considered
  as a spectral line data. UV Tables with coarser spectral resolution
  are treated as continuum only data.
end data gag_scratch:xima_image.hlp


