begin procedure tracking-usage
  message i plot-tracking "Usage:"
  say "@ plot-tracking FitsName [/THRESHOLD Value]"
  say ""
  say "Plot the azimuth and elevation tracking errors from antenna fast"
  say "tables. Compute the fraction of traces above threshold (in arcsec,"
  say "default 1)."
  return base
end procedure tracking-usage

begin procedure tracking-variables
  !---------------------------------------------------------------------
  ! Define and initialize the global variables
  !---------------------------------------------------------------------
  !
  if (.not.exist(tracking)) then
    define structure tracking /global
    !
    define structure tracking%box /global
    define real      tracking%box%xmin tracking%box%xmax /global
    define double    tracking%box%xmin_mjd tracking%box%xmax_mjd /global
    define real      tracking%box%xmin_sec tracking%box%xmax_sec /global
    define real      tracking%box%ymin tracking%box%ymax tracking%box%yrange /global
    define real      tracking%box%az_ymin tracking%box%az_ymax /global
    define real      tracking%box%el_ymin tracking%box%el_ymax /global
    define real      tracking%box%err_ymin tracking%box%err_ymax /global
    !
    define char*512  tracking%file /global
    define logical   tracking%doerr /global
    define double    tracking%freq tracking%threshold /global
    define integer   tracking%nbad tracking%ntrace /global
    define double    tracking%mjd_obs tracking%mjd_beg tracking%mjd_end /global
    define real      tracking%ytext tracking%yrms tracking%yscan /global
    define real      tracking%xsubs tracking%ysubs tracking%justif /global
  endif
  let tracking%doerr yes
  let tracking%nbad 0
  let tracking%ntrace 0
  if exist(tracking%err)  del /var tracking%az tracking%el tracking%err
end procedure tracking-variables

begin procedure tracking-scan-values
  !---------------------------------------------------------------------
  ! Get the scan start and length
  !---------------------------------------------------------------------
  !
  read 'tracking%file' /file /subscan 1
  variable
  let tracking%mjd_obs imbf%prim%mjd_obs
  let tracking%mjd_beg subs%backdata%head%mjd_beg
  !
  read 'tracking%file' /file /subscan imbf%prim%n_obs
  variable
  let tracking%mjd_end subs%backdata%head%mjd_end
  !
  ! Frequency
  compute tracking%freq max imbf%front%table%restfreq
  !
  ! Threshold
  if tracking%threshold.le.0 then
    if tracking%freq.lt.120 then
      let tracking%threshold 3  ! 3 arcsec @ 3 mm
    else if tracking%freq.lt.190 then
      let tracking%threshold 2  ! 2 arcsec @ 2 mm
    else
      let tracking%threshold 1  ! 1 arcsec @ 1 mm
    endif
  endif
  !
end procedure tracking-scan-values

begin procedure tracking-plot-areas
  g\destroy all
  g\set char 0.5
  !
  let tracking%box%xmin 2.0  ! [cm]
  let tracking%box%xmax 24.5 ! [cm]
  let tracking%box%ymin 2.5  ! [cm]
  let tracking%box%ymax 19   ! [cm]
  let tracking%box%yrange tracking%box%ymax-tracking%box%ymin
  !
  let tracking%box%xmin_mjd tracking%mjd_beg
  let tracking%box%xmax_mjd tracking%mjd_end
  let tracking%box%xmin_sec (tracking%mjd_beg-tracking%mjd_obs)*86400
  let tracking%box%xmax_sec (tracking%mjd_end-tracking%mjd_obs)*86400
  !
  if tracking%doerr then
    let tracking%box%err_ymin tracking%box%ymin
    let tracking%box%err_ymax tracking%box%err_ymin+tracking%box%yrange/3
    let tracking%box%el_ymin  tracking%box%err_ymax
    let tracking%box%el_ymax  tracking%box%el_ymin+tracking%box%yrange/3
    let tracking%box%az_ymin  tracking%box%el_ymax
    let tracking%box%az_ymax  tracking%box%ymax
  else
    let tracking%box%el_ymin tracking%box%ymin
    let tracking%box%el_ymax tracking%box%el_ymin+tracking%box%yrange/2
    let tracking%box%az_ymin tracking%box%el_ymax
    let tracking%box%az_ymax tracking%box%ymax
  endif
  !
  @ tracking-draw-box-main
  @ tracking-draw-box-err
  @ tracking-draw-box-az
  @ tracking-draw-box-el
  @ tracking-draw-header
end procedure tracking-plot-areas

begin procedure tracking-draw-header-one
  g\draw text user_xmin tracking%ytext 'pro%arg[1]' 3 /user
!   let tracking%ytext tracking%ytext-0.4
  g\draw text user_xmax tracking%ytext 'pro%arg[2]' 1 /user
  let tracking%ytext tracking%ytext-0.6
end procedure tracking-draw-header-one

begin procedure tracking-draw-header
  !---------------------------------------------------------------------
  ! Draw the "header" box
  !---------------------------------------------------------------------
  g\create directory <greg<head
  g\change directory <greg<head
  g\set box_loc 25 29.5 3 19
  g\limits 0 1 0 15
  ! g\box
  !
  define char*12 string
  let tracking%ytext user_ymax
  !
  let string tracking%freq /format f5.1
  @ tracking-draw-header-one "Freq.:" 'string'" GHz"
  !
  let string subs%antslow%table%celevatio[1]*180/pi /format f4.1
  @ tracking-draw-header-one "Elevation:" 'string'"^"
  !
  let string imbf%scan%head%windvel /format f4.1
  @ tracking-draw-header-one "Wind:" 'string'" m/s"
  !
  let string imbf%scan%head%windvelm /format f4.1
  @ tracking-draw-header-one "Max wind:" 'string'" m/s"
  !
  @ tracking-draw-header-one " " " "  ! Line jump
  !
  let tracking%yrms tracking%ytext
  @ tracking-draw-header-one "Az. RMS:" " "
  @ tracking-draw-header-one "El. RMS:" " "
  @ tracking-draw-header-one "Mean error:" " "
  !
  let string tracking%threshold /format f5.1
  @ tracking-draw-header-one "Threshold:" 'string'"`"
  !
  let tracking%yscan tracking%ytext
  @ tracking-draw-header-one "Scan errors:" " "
  !
  let tracking%ysubs tracking%ytext-0.3
  @ tracking-draw-header-one "Subcan errors:" " "
  let tracking%ytext tracking%ysubs
  let tracking%justif 6
  let tracking%xsubs user_xmin
end procedure tracking-draw-header

begin procedure tracking-draw-box-main
  !---------------------------------------------------------------------
  ! Draw the "main" tracking box
  !---------------------------------------------------------------------
  g\create directory <greg<main
  g\change directory <greg<main
  !
  g\set box_loc tracking%box%xmin tracking%box%xmax tracking%box%ymin tracking%box%ymax
  !
  ! Lower scale in MJD
  g\limits tracking%box%xmin_mjd tracking%box%xmax_mjd 0 1 /margin 5 0
  g\axis xl /tick in /label p
  g\draw text 0 -0.8 "Time [MJD]" /box 2
  !
  ! Upper scale in seconds
  g\limits tracking%box%xmin_sec tracking%box%xmax_sec 0 1 /margin 5 0
  g\axis xu /tick in /label p
  g\draw text 0 0.7 "Time [sec]" /box 8
  !
  ! Title (cosmetics: strip off the directory name)
  define char*128 basename ext
  sic\sic parse 'tracking%file' basename ext
  g\draw text 0 1.4 'basename'"."'ext' /box 8
end procedure tracking-draw-box-main

begin procedure tracking-draw-box-az
  !---------------------------------------------------------------------
  ! Draw the azimuth tracking box
  !---------------------------------------------------------------------
  g\create directory <greg<az
  g\change directory <greg<az
  g\set box_loc tracking%box%xmin tracking%box%xmax tracking%box%az_ymin tracking%box%az_ymax
  g\limits tracking%box%xmin_mjd tracking%box%xmax_mjd -
           -4*tracking%threshold 4*tracking%threshold -
           /margin 5 0  ! Y scale in arcsec
  ! g\box
  g\axis xl /tick n
  g\axis yl
  g\axis yr
  g\draw text -1.5 0 "tracking_az [`]" 5 90 /box 4
  @ tracking-draw-threshold
  g\limits = = user_ymin*sec user_ymax*sec  ! Y scale in radians
end procedure tracking-draw-box-az

begin procedure tracking-draw-box-el
  !---------------------------------------------------------------------
  ! Draw the elevation tracking box
  !---------------------------------------------------------------------
  g\create directory <greg<el
  g\change directory <greg<el
  g\set box_loc tracking%box%xmin tracking%box%xmax tracking%box%el_ymin tracking%box%el_ymax
  g\limits tracking%box%xmin_mjd tracking%box%xmax_mjd -
           -4*tracking%threshold 4*tracking%threshold -
           /margin 5 0  ! X scale in sec, Y scale in arcsec
  ! g\box
  g\axis xl /tick n
  g\axis xu /tick n
  g\axis yl
  g\axis yr
  g\draw text -1.5 0 "tracking_el [`]" 5 90 /box 4
  @ tracking-draw-threshold
  g\limits = = user_ymin*sec user_ymax*sec  ! Y scale in radians
end procedure tracking-draw-box-el

begin procedure tracking-draw-box-err
  !---------------------------------------------------------------------
  ! Draw the error tracking box
  !---------------------------------------------------------------------
  !
  if .not.tracking%doerr  return
  !
  g\create directory <greg<error
  g\change directory <greg<error
  g\set box_loc tracking%box%xmin tracking%box%xmax tracking%box%err_ymin tracking%box%err_ymax
  g\limits tracking%box%xmin_mjd tracking%box%xmax_mjd -
           0 3*tracking%threshold -
           /margin 5 0  ! X scale in MJD, Y scale in arcsec
  ! g\box
  g\axis xu /tick n
  g\axis yl
  g\axis yr
  g\draw text -1.5 0 "tracking error [`]" 5 90 /box 4
  @ tracking-draw-threshold
end procedure tracking-draw-box-err

begin procedure tracking-draw-threshold
  !---------------------------------------------------------------------
  ! Draw the threshold horizontal lines
  !---------------------------------------------------------------------
  pen /col silver
  if -tracking%threshold.gt.user_ymin then
    g\draw relo user_xmin -tracking%threshold /user
    g\draw line user_xmax -tracking%threshold /user
  endif
  g\draw relo user_xmin +tracking%threshold /user
  g\draw line user_xmax +tracking%threshold /user
  pen /col black
end procedure tracking-draw-threshold

begin procedure tracking-draw-on-track
  !---------------------------------------------------------------------
  ! Draw the on-track range for the current subscan
  !---------------------------------------------------------------------
  define double xtext ytext
  ! let xtext (subs%backdata%head%mjd_beg+subs%backdata%head%mjd_end)/2
  let xtext subs%backdata%head%mjd_beg
  let ytext user_ymin
  !
  g\set char 1/sqrt(imbf%prim%n_obsp)
  !
  g\pen /col web_green
  g\draw relo subs%backdata%head%mjd_beg user_ymin /user
  g\draw line subs%backdata%head%mjd_beg user_ymax /user
  g\draw relo subs%backdata%head%mjd_end user_ymin /user
  g\draw line subs%backdata%head%mjd_end user_ymax /user
  g\draw text xtext ytext "#"'subs%isub'" "'subs%antslow%head%substype' 3 90 /user
  g\pen /col black
  !
  g\set char 0.5
end procedure tracking-draw-on-track

begin procedure tracking-subscan-error
  !---------------------------------------------------------------------
  ! Compute the fraction of tracking errors for the current subscan
  !---------------------------------------------------------------------
  define integer first last ntrace
  !
  ! Find first on-track fast trace
  for ifast 1 to size(subs%antfast%table%mjd)
    if subs%antfast%table%mjd[ifast].ge.subs%antfast%head%mjd_beg then
      let first ifast
      break
    endif
  next ifast
  !
  ! Find last on-track fast trace
  for ifast size(subs%antfast%table%mjd) to 1 by -1
    if subs%antfast%table%mjd[ifast].le.subs%antfast%head%mjd_end then
      let last ifast
      break
    endif
  next ifast
  !
  let ntrace last-first+1
  define real tracking_az[ntrace] tracking_el[ntrace] error[ntrace]
  let tracking_az subs%antfast%table%tracking_az[first:last]/sec  ! [arcsec]
  let tracking_el subs%antfast%table%tracking_el[first:last]/sec  ! [arcsec]
  let error       sqrt(tracking_az**2+tracking_el**2)             ! [arcsec]
  if tracking%doerr then
    g\change directory <greg<error
    if mod(subs%isub,2).eq.0  pen /col blue
    g\connect subs%antfast%table%mjd[first:last] error
    g\pen /col black
  endif
  !
  define integer flag[ntrace]
  let flag 1 /where error.gt.tracking%threshold
  define real nbad
  compute nbad sum flag
  !
  define char*12 frac
  let frac nbad/ntrace*100 /format F4.1
  message i tracking "Subscan #"'subs%isub'": "'frac'"% on-track fast traces above "'tracking%threshold'" arcsec"
  !
  g\change directory <greg<head
  g\set char 0.35
  let tracking%ytext tracking%ytext-0.4
  if tracking%ytext.lt.user_ymin then
    let tracking%ytext tracking%ysubs-0.4
    let tracking%xsubs tracking%xsubs+(user_xmax-user_xmin)/2
    let tracking%justif tracking%justif-1
  endif
  g\draw text tracking%xsubs tracking%ytext 'subs%isub'":"'frac'"%" tracking%justif /user
  g\set char 0.5
  !
  ! Whole scan statistics:
  if exist(tracking%err) then
    ! Enlarge arrays with new values
    define integer nold nnew
    let nold tracking%ntrace
    let nnew nold+ntrace
    !
    define real tmpaz[nold] tmpel[nold] tmperr[nold]
    let tmpaz  tracking%az
    let tmpel  tracking%el
    let tmperr tracking%err
    del /var tracking%az tracking%el tracking%err
    define double tracking%az[nnew] tracking%el[nnew] tracking%err[nnew] /global
    ! Copy back previous values:
    let first 1
    let last  nold
    let tracking%az[first:last]  tmpaz
    let tracking%el[first:last]  tmpel
    let tracking%err[first:last] tmperr
    del /var tmpaz tmpel tmperr
    ! Append new values
    let first nold+1
    let last  nnew
    let tracking%az[first:last]  tracking_az
    let tracking%el[first:last]  tracking_el
    let tracking%err[first:last] error
  else
    define double tracking%az[ntrace] tracking%el[ntrace] tracking%err[ntrace] /global
    let tracking%az  tracking_az
    let tracking%el  tracking_el
    let tracking%err error
  endif
  let tracking%ntrace tracking%ntrace+ntrace
  let tracking%nbad tracking%nbad+nbad
end procedure tracking-subscan-error
!
begin procedure tracking-scan-error
  !---------------------------------------------------------------------
  ! Compute the fraction of tracking errors for the whole scan
  !---------------------------------------------------------------------
  g\change directory <greg<head
  define char*12 string
  !
  define real rms_az rms_el mean_err
  compute rms_az   rms  tracking%az
  compute rms_el   rms  tracking%el
  compute mean_err mean tracking%err
  let tracking%ytext tracking%yrms
  !
  let string rms_az /format f5.2
  message i tracking "Total scan: "'string'""" azimuth RMS for on-track fast traces"
  @ tracking-draw-header-one " " 'string'"`"
  !
  let string rms_el /format f5.2
  message i tracking "Total scan: "'string'""" elevation RMS for on-track fast traces"
  @ tracking-draw-header-one " " 'string'"`"
  !
  let string mean_err /format f5.2
  message i tracking "Total scan: "'string'""" mean error for on-track fast traces"
  @ tracking-draw-header-one " " 'string'"`"
  !
  let string tracking%nbad/tracking%ntrace*100 /format F4.1
  message i tracking "Total scan: "'tracking%nbad'"/"'tracking%ntrace'" ="'string'"% on-track fast antenna traces above "'tracking%threshold'" arcsec"
  let tracking%ytext tracking%yscan
  @ tracking-draw-header-one " " 'string'"%"
  !
  g\change directory <greg<main
  g\pen /col red
  g\set font duplex
  g\set char 0.55
  g\draw text 0 -1.8 -
         "For this scan "'string'"% of the on-track fast antenna traces are above "'tracking%threshold'" arcsec tracking error." -
         5 /box 2
  g\pen /col foreground
  g\set font simplex
  g\set char 0.5
end procedure tracking-scan-error

! Global variables
@ tracking-variables

! Argument parsing
if pro%narg.le.0 then
  @ tracking-usage
endif
let tracking%file 'pro%arg[1]'
sic\parse /threshold 1
if pro%parse%threshold%narg.ge.1 then
  let tracking%threshold 'pro%parse%threshold%arg[1]'
else
  let tracking%threshold 0.0  ! Default depending on frequency, need to read data
endif

! Get the scan typical values
@ tracking-scan-values

! Prepare plotting areas
@ tracking-plot-areas

! Loop over all subscans
for isub 1 to imbf%prim%n_obs
  read 'tracking%file' /file /subscan isub
  variable
  !
  g\change directory <greg<az
  if mod(isub,2).eq.0  pen /col blue
  g\connect subs%antfast%table%mjd subs%antfast%table%tracking_az
  @ tracking-draw-on-track
  !
  g\change directory <greg<el
  if mod(isub,2).eq.0  pen /col blue
  g\connect subs%antfast%table%mjd subs%antfast%table%tracking_el
  @ tracking-draw-on-track
  !
  @ tracking-subscan-error
next isub

@ tracking-scan-error

g\change directory <greg
