SAY " "
SAY "Demonstration of IMAGER: Advanced Spectral Line case"
SAY " "
@ gag_demo:demo-setup
say " "
SAY "-----------------------------------------------------------"
SAY " The first step: Reading UV data"
SAY " "
define logical down
@ tutorial-pause
if (exist(my_clean)) dele /var my_clean
if (exist(my_cont)) dele /var my_cont
!
if (file("gag_demo:demo-spectro-imager.uvt")) then
  DO "read uv gag_demo:demo-spectro-imager.uvt"  "Read the UV data"
else if (file("$HOME/demo-spectro-imager.uvt")) then
  DO "read uv $HOME/demo-spectro-imager.uvt"  "Read the UV data"
else
  say 'ansi%bold''ansi%red'"File ""demo-spectro-imager.uvt"" Does not exists"'ansi%none'
  LET DOWN /PROMPT 'ansi%bold''ansi%blue'"Download a local copy ?"'ansi%none'
  if (down) then
    SAY "Downloading, please wait..."
    system "curl -o $HOME/demo-spectro-imager.uvt https://mycore.core-cloud.net/index.php/s/ATycmmcZgxmvAwe/download"
    DO "read uv $HOME/demo-spectro-imager.uvt"  "Read the UV data"
  else
    return error
  endif
endif

DO "uv_preview"  "Preview the UV data to locate the spectral line(s)"
!
SAY "-----------------------------------------------------------"
SAY " Imaging without Continuum separation "
SAY " "
DO "uv_stat weight"       "Search for optimal Robust factor"
DO "let map_robust 1.0"   "This one is a good compromise"
DO "let map_size 512"     "Limit the image size"
DO "let map_cell 0.04"    "  and use a reasonable pixel for this size"
DO uv_map "Compute Dirty image and Beam"
DO "let method CLARK"     "Use Barry Clark method"
DO "clean"                "Deconvolve (parallel mode)"
DO "view cct"             "View the Clean components"
DO "view clean /nopause"  "(note noise aliasing at edges)"
DO "uv_restore"           "Restore with Clean component subtraction in UV plane"
DO "view clean /nopause"  "See lower noise level at edges"
DO "let view%range 0 15"  "Select (manually) a velocity range for next VIEW commands"
DO "view clean /nopause"  "Dual vision of the spectra, with specified velocity range"
!
! without cont
SAY "-----------------------------------------------------------"
SAY " Imaging with Continuum separation "
SAY " 1)  Imaging continuum"
DO "uv_filter"            "flag the spectral line regions"
DO "uv_map /cont"         "Image the continuum only signal"
DO "clean"                "Deconvolve"
DO "uv_restore"           "Better restoration"
SAY 'ansi%red'"Caution: will now be using the interactive VIEW. Press H if lost..."'ansi%none'
DO "view clean /nopause"  "Note that VIEW falls back to SHOW"
SAY "-----------------------------------------------------------"
SAY " Image is automatically saved under CONTINUUM variable"
SAY "-----------------------------------------------------------"
SAY " Imaging with Continuum separation "
SAY " 2)  Imaging without continuum"
DO "uv_baseline"          "Remove the continuum defined outside the spectral region"
DO "uv_map"               "Image the line-only data"
DO "clean"                "Deconvolve"
DO "stat clean"
DO "mask threshold 10 sigma"    "Initialize a 3-D mask"
DO "view mask /nopause"   "View the Mask"
DO "mask use"             "Use the Mask"
DO "clean"
DO "uv_restore"           "Restore"
SAY 'ansi%red'"Caution: will now be using the interactive VIEW. Press H if lost..."'ansi%none'
DO "view clean"           "Note the lack of signal apart from the line"
SAY
SAY "-----------------------------------------------------------"
SAY " Adding back the Continuum "
SAY " 3)  Imaging with better continuum"
DO "define image my_clean * real /like clean /global"   "CLEAN is ReadOnly, create a copy"
DO "map_combine my_clean ADD clean continuum"  "Add the continuum and clean images into it" 
DO "view my_clean"   "View this datacube. See how the continuum behaves"
!
if (down) then
  LET DOWN /PROMPT 'ansi%bold''ansi%blue'"Keep file $HOME/demo-spectro-imager.uvt ?"'ansi%none'
  if (down) return
  sic delete $HOME/demo-spectro-imager.uvt
endif
