SAY " "
SAY "Demonstration of IMAGER: Primary beam correction"
SAY " "
!
@ gag_demo:demo-setup
!
SAY "-----------------------------------------------------------"
SAY " The first step: Reading UV data"
DO "read uv gag_demo:demo-primary-self" "Read the UV data"
DO "specify telescope ALMA" "Our data may not have this information yet" it
DO uv_preview "This is a spectrally resolved continuum data"
DO "uv_map /cont" "Image it in Continuum mode"
DO "clean " "Deconvolve"
DO "show clean" "See high noise due to aliasing at edges"
DO "uv_restore" "Remove aliasing"
DO "let spacing 0.3e-3" "Use some appropriate level spacing" it
DO "show clean"
!
! Now look at poorly deconvolved regions
DO "let center -1 -11" "Zoom into secondary source" it
DO "let size 2 " "Zoom into secondary source" it
DO "show clean"
!
! Try a mask to improve this
SAY "See the poor shape: source was not deconvolved, being too far off"
SAY "We use a MASK for a better deconvolution"
DO "mask threshold 20 sigma" "Automatic threshold on bright sources" 
DO "let size 0" " " it
DO "let center 0" " " it 
DO "show mask"
!
! Go to Robust Weighting
SAY "That was a Natural Weighting image, now improving resolution"
DO "fit" "Clean beam parameters in Natural (current) weighting" 
DO "Let MAP_ROBUST -1.5" "This will be a Briggs weighting, with Briggs=0.5"
DO "uv_map /cont" "Image again"
DO "fit" "Get the Clean beam parameters: much smaller now"
DO "clean" "Clean again"
DO "uv_restore" "Avoid aliasing at edges"
DO "show clean" "New clean image"
!
! Ultimately apply primary beam correction to get the proper flux.
SAY "There is a small source in the South, apply primary beam correction to derive flux"
DO "primary"    "Apply primary beam correction from known Telescope"
DO "let spacing '2*spacing_rule%r' " "Noise increase at edges..."
DO "show sky"  "After correction, it is the SKY brightness"
!
! Compare on offset source
DO "let center -1 -11" "Zoom into secondary source" it
DO "let size 2 " "Zoom into secondary source" it
DO "show sky" "Look at the peak value"
DO "show clean" "and compare without primary beam correction"

