SAY " "
SAY "Demonstration of IMAGER: Selfcal Demonstration on a Spectral Line case"
SAY " "
!
@ gag_demo:demo-setup
define logical do_simple
!
SAY "-----------------------------------------------------------"
SAY " The first step: Reading UV data"
!
if (file("gag_demo:demo-selfcal-cont.uvt")) then
  DO "read uv gag_demo:demo-selfcal-cont.uvt" "Read the UV data"
  do_simple = no
else if (file("gag_demo:demo-selfcal-small.uvt")) then
  do_simple = yes
  DO "read uv gag_demo:demo-selfcal-small.uvt" "Read the UV data"
  DO "specify telescope ALMA" "Add the Telescope information that is missing"
  DO "uv_map"  "Image it"
  DO "clean"   "Clean it"
  DO "let spacing 6E-4" "Using an appropriate level spacing"
  DO "show clean" "See Cleaned image"
  DO "statistic"  "Compute noise level"
  DO "mask threshold 6 sigma SMOOTH 3 0.1" "Define a mask by thresholding" 
  DO "show mask" "See it"
  DO "mask use" "Restrict the clean region to this mask"
else if (file("$HOME/demo-selfcal-cont.uvt")) then
  DO "read uv $HOME/demo-selfcal-cont.uvt" "Read the UV data"
  do_simple = no
else if (file("$HOME/demo-selfcal-small.uvt")) then
  do_simple = yes
  DO "read uv $HOME/demo-selfcal-small.uvt" "Read the UV data"
  DO "specify telescope ALMA" "Add the Telescope information that is missing"
  DO "uv_map"  "Image it"
  DO "clean"   "Clean it"
  DO "let spacing 6E-4" "Using an appropriate level spacing"
  DO "show clean" "See Cleaned image"
  DO "statistic"  "Compute noise level"
  DO "mask threshold 6 sigma SMOOTH 3 0.1" "Define a mask by thresholding" 
  DO "show mask" "See it"
  DO "mask use" "Restrict the clean region to this mask"
else
  SAY "No suitable demonstration data file found"
  SAY "  no gag_demo:demo-selfcal-cont.uvt"
  SAY "  no gag_demo:demo-selfcal-small.uvt"
  LET do_simple /PROMPT 'ansi%bold''ansi%blue'"Download a local copy to your $HOME ?"'ansi%none'
  if (do_simple) then
    SAY "Downloading, please wait..."
    system "curl -o $HOME/demo-selfcal-cont.uvt https://mycore.core-cloud.net/index.php/s/uGiEiz8FhP1RCFk/download"
    DO "read uv $HOME/demo-selfcal-cont.uvt"  "Read the UV data"
    do_simple = no
  else
    return error
  endif
end if
!
if .not.do_simple then
  DO uv_preview "This is a spectrally resolved continuum data"
  DO "specify telescope ALMA" "Add the Telescope information that is missing"
  DO "uv_map"  "Image the few (4 actually) channels"
  DO "clean"   "Clean them"
  DO "show clean" "All channels are similar"
  !
  DO "let spacing 3E-4" "Using an appropriate level spacing"
endif
!
DO "selfcal ?"  "Current SELFCAL parameters"
DO "let self_times 60 30 18" "Modify the integration times"
SAY "You will have to type C (for CONTINUE) at each PAUSE"
DO "selfcal phase" "Self Calibrated the PHASE"
DO "selfcal apply" "Apply the PHASE self-calibration"
if do_simple then
  DO "uv_map" "Image again"
else
  DO "uv_map /cont" "Image in Continuum mode"
endif
DO "clean" "Clean"
DO "show clean" "See the high aliasing at map edges ?"
DO "uv_restore" "Remove it by subtraction in the UV plane of Clean Components"
DO "show clean" "Much nicer display now"
DO "statistic clean" "See small source at Dec 17:31:30 ?"
DO "mask threshold 6 sigma SMOOTH 3 .1" "Let us include it in Mask"
DO "show mask" "Should be in, now..."
SAY " "
SAY "Now we do the AMPLItude Self Calibration"
DO "let self_times 60" "Longer timescale better for Amplitude"
DO "selfcal ampli" "Do the job..."
DO "show selfcal"  "See the corrections"
DO "selfcal apply" "Apply the corrections"
if do_simple then
  DO "uv_map" "Image again"
else
  DO "uv_map /cont" "Image in Continuum mode"
endif
do "clean"        "Deconvolve"
do "uv_restore"   "Remove aliasing"
DO "show clean"   
!
! We are done.
! Better results can be obtained through
!   - Higher CLEAN_GAIN
!   - Support definition
