if (exist(x0)) then
  dele /var x0  y0 rota incli
endif
SAY " "
SAY "Demonstration of IMAGER: UV_FIT and deprojection capabilities"
SAY " "
@ gag_demo:demo-setup
say " "
SAY "-----------------------------------------------------------"
SAY " The first step: Reading UV data"
SAY " "
DO "read uv gag_demo:demo-single-imager.uvt"  "Read the UV data"
DO "show uv ?"  "Check UV display characteristics"
DO "let xtype radius"
DO "let ytype real"  "Change the type of data to be displayed"
DO "show uv"  "Look at the UV data"
DO "uv_fit E_RING 0 0 .6 2 3 0 1.2" "Fit an elliptical ring to the visibilities"
!
DO "let uvshow%fit POINT" "Now, also show the fit results"
DO "show uv"  "Look at the UV data"
!
DO "DEFINE REAL X0 Y0 ROTA INCLI /GLOBAL " "Deprojection values"
DO "LET X0 uvf%par1%results[1]*sec" "The X offset in radian"
DO "LET Y0 uvf%par1%results[2]*sec" "The Y offset in radian"
if (uvf%par1%results[7].gt.1) then
  DO "LET ROTA uvf%par1%results[6]"  "The angle of the minor axis"
  DO "LET INCLI 180/pi*acos(1/uvf%par1%results[7])"  "The inclination"
else
  DO "LET ROTA uvf%par1%results[6]-90."  "The angle of the minor axis"
  DO "LET INCLI 180/pi*acos(uvf%par1%results[7])"  "The inclination"
endif
DO "UV_DEPROJECT x0 y0 rota incli"  "Deproject the visibilities"
!
DO "let uvshow%fit NO" 
DO "SHOW UV"
DO "uv_fit RING 0 0 .6 2 3 0 0" "This should now be a circular ring"
DO "let uvshow%fit CURVE" 
DO "SHOW UV" "See the fit now ?"
!
DO "read uv gag_demo:demo-single-imager.uvt"  "Read the UV data"
DO "UV_CIRCLE x0 y0 rota incli /SAMPLING 7.5"  "Deproject the visibilities, and circular average them"
DO "Let Error_Bars yes"
DO "SHOW UV" "See the fit now ?"
!
SAY " "
SAY "------------------------------------------------------------"
SAY " "
SAY "   Now showing UV_FIT on spectral line data"
SAY " "
let error_bars NO
let uvshow%fit NO
DO "read uv gag_demo:demo-line-imager /rang 4  9 velocity" "Read spectral line UV data"
DO "show uv" "UV data display"
DO "uv_fit E_GAUSS  0 0 1 3 2 0 0" "Fit an elliptical Gaussian to the visibilities"
DO "SHOW UV_FIT RESET"   "Reset default parameters for SHOW UV_FIT"
DO "SHOW UV_FIT ?"       "Input SHOW UV_FIT parameters" 
DO "SHOW UV_FIT"         "Show UV_FIT results as a function of Velocity"
DO "Let UVF%NY 6"        "Ask to plot all 6 parameters"
DO "SHOW UV_FIT"         "See again"
DO "LET UVF%NY 1"        "Prepare a different view..."
DO "LET UVF%XTYPE[1] """"RA * *""""" "Plot position in a RA Dec "
DO "LET UVF%YTYPE[1] """"Dec * *""""" "   and Dec frame.."
DO "SHOW UV_FIT ?"       "The Positions as functions of Velocities..."
DO "SHOW UV_FIT"         "The Positions as functions of Velocities..."
