!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
begin procedure monitor-tracking-defaults
  @ window_tools.greg
  let aspect_ratio no
  let inter 0.3 0.12
  !
  if .not.exist(tracking) then
     define structure tracking /global
     define integer tracking%nsub tracking%nd /global
  endif
  !
  index update &1
  mfind /back &2
  mlist /column observed ut scan source obstype switchmode backend calib
end procedure monitor-tracking-defaults
!
begin procedure monitor-tracking-title
  define integer icurr
  let icurr mdx%curr
  if (icurr.eq.0) then
     message e monitor-tracking "No scan in memory"
     return base
  endif
  define integer iobstype iswitch
  let iobstype 1+mdx%obstype[icurr]
  let iswitch  1+mdx%switchmode[icurr]
  define character title*256 date*11
  compute date gag_date mdx%dobs[icurr]
  define double ut
  let ut nint(100*mdx%ut[icurr]*12/pi)/100
  let title 'mdx%projid[icurr]'" #"'mdx%scan[icurr]'" "'mdx%source[icurr]'
  let title 'title'"   "'mrt%obstype[iobstype]'" "'mrt%switchmode[iswitch]'
  let title 'title'"   "'date'" @ "'ut'"hr"
  @ window_xy 1 1 1
  g\draw text 0 1 'title' 5 /char 8
end procedure monitor-tracking-title
!
begin procedure monitor-tracking-plot-box
  g\box
  g\pen 0 /dash 3
  g\rule
  g\pen 0 /dash 1
end procedure monitor-tracking-plot-box
!
begin procedure monitor-tracking-plot-point
  define integer ifirst ilast
  for isub 1 to tracking%nsub
     g\pen 2+mod(isub,2)
     let ifirst tracking%first[isub]
     let ilast  tracking%last[isub]
     g\point &1[ifirst:ilast] &2[ifirst:ilast]
  next isub
  g\pen 0
end procedure monitor-tracking-plot-point
!
begin procedure monitor-tracking-plot-coord
  g\limits /var &1 &2
  @ monitor-tracking-plot-box
  @ monitor-tracking-plot-point &1 &2
  @ monitor-tracking-beg-end
end procedure monitor-tracking-plot-coord
!
begin procedure monitor-tracking-plot-diff
  define double threshold
  let threshold 1
  g\limits * * -10 +10 /var &1 &2
  @ monitor-tracking-plot-box
  @ monitor-tracking-plot-point &1 &2
  g\pen 1
  g\draw relo user_xmin threshold /user
  g\draw line user_xmax threshold /user
  g\draw relo user_xmin -threshold /user
  g\draw line user_xmax -threshold /user
  g\pen 0
  @ monitor-tracking-beg-end
end procedure monitor-tracking-plot-diff
!
begin procedure monitor-tracking-beg-end
  for isub 1 to tracking%nsub
     g\pen 2+mod(isub,2) ! /dash 8
     g\draw relo tracking%mjd_beg[isub] user_ymin /user
     g\draw line tracking%mjd_beg[isub] user_ymax /user
     g\draw relo tracking%mjd_end[isub] user_ymin /user
     g\draw line tracking%mjd_end[isub] user_ymax /user
  next isub
  g\pen 0
end procedure monitor-tracking-beg-end
!
begin procedure monitor-tracking-plot
  clear
  @ window_xy 2 2 1
  @ monitor-tracking-plot-coord tracking%mjd tracking%az
  g\label "Azimuth [deg]" /y 6
  @ window_xy 2 2 2
  @ monitor-tracking-plot-coord tracking%mjd tracking%el
  g\label "Elevation [deg]" /y 5
  @ window_xy 2 2 3
  @ monitor-tracking-plot-diff tracking%mjd tracking%daz
  g\label "daz [`]" /y 6
  g\label "Time [min]" /x
  @ window_xy 2 2 4
  @ monitor-tracking-plot-diff tracking%mjd tracking%del
  g\label "del [`]" /y 5
  g\label "Time [min]" /x
  @ monitor-tracking-title
end procedure monitor-tracking-plot
!
begin procedure monitor-tracking-read
  read &1
  variable
  if (imbf%prim%n_obs.le.0) then
     message e monitor-tracking "No subscan"
     return
  endif
  let tracking%nsub imbf%prim%n_obs
  if exist(tracking%first) then
     delete /var tracking%first tracking%last tracking%mjd_beg tracking%mjd_end
  endif
  define integer tracking%first[tracking%nsub] tracking%last[tracking%nsub] /global
  define double  tracking%mjd_beg[tracking%nsub] tracking%mjd_end[tracking%nsub] /global
  !
  define integer dims[8]
  let tracking%nd 0
  for isub 1 to tracking%nsub
    say "Reading subscan #"'isub'"/"'tracking%nsub'
    read /subscan 'isub'
    variable
    compute dims dimof subs%antfast%table%mjd
    let tracking%first[isub] tracking%nd+1
    let tracking%last[isub] tracking%nd+dims[1]
    let tracking%nd tracking%nd+dims[1]
    let tracking%mjd_beg[isub] subs%antfast%head%mjd_beg
    let tracking%mjd_end[isub] subs%antfast%head%mjd_end
  next isub
  !
  define double deg_per_rad sec_per_rad min_per_day
  let deg_per_rad 180/pi
  let sec_per_rad 3600*deg_per_rad
  let min_per_day 24*60
  !
  if exist(tracking%mjd) then
     delete /var tracking%mjd tracking%az tracking%el tracking%daz tracking%del
  endif
  define double tracking%mjd[tracking%nd] /global
  define double tracking%az[tracking%nd] tracking%el[tracking%nd] /global
  define double tracking%daz[tracking%nd] tracking%del[tracking%nd] /global
  define integer ifirst ilast
  for isub 1 to tracking%nsub
     say "Reading subscan #"'isub'"/"'imbf%prim%n_obs'
     read /subscan 'isub'
     variable
     let ifirst tracking%first[isub]
     let ilast  tracking%last[isub]
     let tracking%mjd[ifirst:ilast] subs%antfast%table%mjd
     let tracking%az[ifirst:ilast]  subs%antfast%table%azimuth*deg_per_rad
     let tracking%el[ifirst:ilast]  subs%antfast%table%elevation*deg_per_rad
     let tracking%daz[ifirst:ilast] subs%antfast%table%tracking_az*sec_per_rad
     let tracking%del[ifirst:ilast] subs%antfast%table%tracking_el*sec_per_rad
  next isub
  define double mjd_ref
  let mjd_ref tracking%mjd[1]
  let tracking%mjd     (tracking%mjd-mjd_ref)*min_per_day
  let tracking%mjd_beg (tracking%mjd_beg-mjd_ref)*min_per_day
  let tracking%mjd_end (tracking%mjd_end-mjd_ref)*min_per_day
end procedure monitor-tracking-read
!
begin procedure monitor-tracking-usage
  say "To be written"
end procedure monitor-tracking-usage
!
begin procedure monitor-tracking-loop
  @ monitor-tracking-read first
  @ monitor-tracking-usage
  for /while .true.
    @ monitor-tracking-plot
    ! Next position
    g\draw relo
    if ((cursor_code.eq."E").or.(cursor_code.eq."*")) then
       break
    else if (cursor_code.eq."H") then
       @ monitor-tracking-usage
    else if (cursor_code.eq."N") then
       @ monitor-tracking-read next
    else if (cursor_code.eq."P") then
       @ monitor-tracking-read prev
    else if (cursor_code.eq."F") then
       @ monitor-tracking-read first
    else if (cursor_code.eq."L") then
       @ monitor-tracking-read last
    else
       message w browse "Unknown key: "'cursor_code'
    endif
  next
end procedure monitor-tracking-loop
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
@ monitor-tracking-defaults 13019 nbc
@ monitor-tracking-loop
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
