
begin procedure plot-azel-define
  if exist(azel)  delete /var azel
  define structure     azel             /global
  define character*256 azel%file        /global
  define integer       azel%nsub        /global
  define integer       azel%first       /global
  define integer       azel%last        /global
  define logical       azel%doderiv     /global
  define logical       azel%docommanded /global
  !
  define structure     azel%slow             /global
 !define integer       azel%slow%ndump[nsub] /global
  define integer       azel%slow%ndumps      /global
  !
  define structure     azel%fast             /global
 !define integer       azel%fast%ndump[nsub] /global
  define integer       azel%fast%ndumps      /global
end procedure plot-azel-define

begin procedure plot-azel-ndumps
  !
  mset input bandwidth 0.0  ! Play fast
  read 'azel%file' /file /sub 1
  variable
  !
  if azel%first.eq.0  let azel%first 1
  if azel%last.eq.0   let azel%last  IMBF%PRIM%N_OBSP
  let azel%nsub azel%last-azel%first+1  ! Number of subscans read
  !
  define integer jsub
  define char*8  azel%color[azel%nsub]  /global
  define integer azel%slow%ndump[azel%nsub]  /global
  define integer azel%fast%ndump[azel%nsub]  /global
  for isub azel%first to azel%last
    read 'azel%file' /file /sub isub
    variable subs
    let jsub isub-azel%first+1
    let azel%slow%ndump[jsub] subs%antslow%head%desc%naxis2
    let azel%fast%ndump[jsub] size(SUBS%ANTFAST%TABLE%AZIMUTH)
    if mod(isub,2).eq.0 then
      let azel%color[jsub] "blue"
    else
      let azel%color[jsub] "web_green"
    endif
  next isub
  define double tmp
  compute tmp sum azel%slow%ndump
  let azel%slow%ndumps nint(tmp)
  compute tmp sum azel%fast%ndump
  let azel%fast%ndumps nint(tmp)
  ! exa azel%fast%ndumps
end procedure plot-azel-ndumps

begin procedure plot-azel-collect-data
  !
  define double  azel%mjdbeg[azel%nsub]   /global
  define double  azel%mjdend[azel%nsub]   /global
  define char*16 azel%substype[azel%nsub] /global
  !
  define double  azel%slow%mjd[azel%slow%ndumps] /global
  define double  azel%slow%az[azel%slow%ndumps]  /global
  define double  azel%slow%el[azel%slow%ndumps]  /global
  !
  define double  azel%fast%mjd[azel%fast%ndumps]  /global
  define double  azel%fast%time[azel%fast%ndumps] /global
  define double  azel%fast%az[azel%fast%ndumps]   /global
  define double  azel%fast%el[azel%fast%ndumps]   /global
  define double  azel%fast%daz[azel%fast%ndumps]  /global
  define double  azel%fast%del[azel%fast%ndumps]  /global
  !
  define integer jsub
  define integer sfirst slast
  let sfirst 1
  define integer ffirst flast
  let ffirst 1
  for isub azel%first to azel%last
    read 'azel%file' /file /sub isub
    variable
    let jsub isub-azel%first+1
    let azel%mjdbeg[jsub] subs%antfast%head%mjd_beg
    let azel%mjdend[jsub] subs%antfast%head%mjd_end
    let azel%substype[jsub] 'subs%antslow%head%substype'
    !
    let slast sfirst+subs%antslow%head%desc%naxis2-1
    let azel%slow%mjd[sfirst:slast] subs%antslow%table%mjd
    let azel%slow%az[sfirst:slast] subs%antslow%table%cazimuth
    let azel%slow%el[sfirst:slast] subs%antslow%table%celevatio
    let sfirst slast+1
    !
    let flast ffirst+size(subs%antfast%table%azimuth)-1
    let azel%fast%mjd[ffirst:flast] subs%antfast%table%mjd
    let azel%fast%az[ffirst:flast] subs%antfast%table%azimuth
    let azel%fast%el[ffirst:flast] subs%antfast%table%elevation
    let ffirst flast+1
  next isub

  if azel%doderiv then
    let azel%fast%daz 0
    let azel%fast%del 0
    define integer blast bblast
    let flast azel%fast%ndumps
    let blast azel%fast%ndumps-1
    let bblast azel%fast%ndumps-2
    let azel%fast%daz[2:blast] azel%fast%az[3:flast]-azel%fast%az[1:bblast]
    let azel%fast%del[2:blast] azel%fast%el[3:flast]-azel%fast%el[1:bblast]
  endif

  ! Convert to friendly units
  let azel%slow%az azel%slow%az*180/pi
  let azel%slow%el azel%slow%el*180/pi
  let azel%fast%time (azel%fast%mjd-imbf%prim%mjd_obs)*24*60  ! Convert MJD to minutes relative to start of scan
  let azel%fast%az azel%fast%az*180/pi
  let azel%fast%el azel%fast%el*180/pi
  let azel%fast%daz azel%fast%daz/sec
  let azel%fast%del azel%fast%del/sec
end procedure plot-azel-collect-data

begin procedure plot-subscans-beg-end
  g\set char 2.0/azel%nsub
  define real yrange y1 y2
  define integer jsub
  let yrange user_ymax-user_ymin
  let y1 user_ymax-1*yrange/4/azel%nsub
  let y2 user_ymax-2*yrange/4/azel%nsub
  for isub 1 to azel%nsub
    let jsub isub+azel%first-1
    g\pen /col 'azel%color[isub]'
    g\draw text (azel%mjdbeg[isub]+azel%mjdend[isub])/2 y1 "Subs. #"'jsub' 2 /user
    g\draw text (azel%mjdbeg[isub]+azel%mjdend[isub])/2 y2 'azel%substype[isub]' 2 /user
    g\draw relo azel%mjdbeg[isub] user_ymin /user
    g\draw line azel%mjdbeg[isub] user_ymax /user
    g\draw relo azel%mjdend[isub] user_ymin /user
    g\draw line azel%mjdend[isub] user_ymax /user
  next isub
  g\pen /col foreground
  g\set char 0.48
end procedure plot-subscans-beg-end

begin procedure plot-azel-plot-box
  !
  @ plot-subscans-beg-end
  g\axis YL
  g\axis YR
  !
  if pro%arg[1].eq."bottom" then
    g\axis XL
    g\label "MJD [day]" /x
  else
    g\axis XL /label N
  endif
  !
  g\limits * * = = /var azel%fast%time azel%fast%az /margin 5 10
  if pro%arg[1].eq."top" then
    g\axis XU /label p
    g\draw text 0 0.8 "Time [min]" /box 8
  else
    g\axis XU /label N
  endif
end procedure plot-azel-plot-box

begin procedure plot-azel-plot-main
  clear
  g\set /def
  g\set mark 4 1 0.001 45.0  ! Cross
  ! g\set mark 1 1 0 0  ! Dot

  define char*16 azlabel ellabel
  define double mjdmin mjdmax azmin azmax elmin elmax
  compute mjdmin min azel%fast%mjd
  compute mjdmax max azel%fast%mjd
  if azel%doderiv then
    let azlabel "DAZIMUTH [sec]"
    let ellabel "DELEVATION [sec]"
    define alias myaz azel%fast%daz
    define alias myel azel%fast%del
    compute azmax rms azel%fast%daz
    compute elmax rms azel%fast%del
    let azmax (azmax+elmax)/2  ! Sort of average RMS
    let azmax 1.5*azmax
    let azmin -azmax
    let elmin azmin
    let elmax azmax
  else
    let azlabel "AZIMUTH [deg]"
    let ellabel "ELEVATION [deg]"
    define alias myaz azel%fast%az
    define alias myel azel%fast%el
    compute azmin min azel%fast%az
    compute azmax max azel%fast%az
    compute elmin min azel%fast%el
    compute elmax max azel%fast%el
  endif

  if azel%docommanded then
    define double val
    compute val min azel%slow%az
    let azmin min(val,azmin)
    compute val max azel%slow%az
    let azmax max(val,azmax)
    compute val min azel%slow%el
    let elmin min(val,elmin)
    compute val max azel%slow%el
    let elmax max(val,elmax)
  endif

  clear
  g\set char 0.48

  ! AZIMUTH
  g\set box_loc 3.0 28.0 10.5 18.5
  g\limits mjdmin mjdmax azmin azmax /margin 5 8
  g\point azel%fast%mjd myaz
  if azel%docommanded then
    g\pen /col red
    g\point azel%slow%mjd azel%slow%az
    g\pen /col foreground
  endif
  @ plot-azel-plot-box top
  g\label 'azlabel' /y 4.5
  if azel%docommanded then
    g\pen /col red
    g\draw text 0.8 0 "CAZIMUTH [deg]" 5 90 /box 6
    g\pen /col foreground
  endif

  ! ELEVATION
  g\set box_loc 3.0 28.0 1.5 10.5
  g\limits mjdmin mjdmax elmin elmax /margin 5 8
  g\point azel%fast%mjd myel
  if azel%docommanded then
    g\pen /col red
    g\point azel%slow%mjd azel%slow%el
    g\pen /col foreground
  endif
  @ plot-azel-plot-box bottom
  g\label 'ellabel' /y 4.5
  if azel%docommanded then
    g\pen /col red
    g\draw text 0.8 0 "CELEVATIO [deg]" 5 90 /box 6
    g\pen /col foreground
  endif

  ! Title
  g\set box_loc 4.0 28.0 1.5 18.5
  define char*64 shortname
  sic parse 'azel%file' shortname
  g\draw text 0 1.4 'shortname'".fits ("'imbf%prim%obstype'" "'imbf%scan%head%swtchmod'")" /box 8
end procedure plot-azel-plot-main

! Initialization
@ plot-azel-define

! Command line parsing
parse 0 1 /subscan 2 /derivative 0 /commanded 0
if pro%parse%command%narg.le.0 then
  message e plot-azel "Usage: @ plot-azel FileName [/subscan First Last] [/derivative] [/commanded]"
  return base
endif
if pro%parse%subscan%narg.eq.2 then
  let azel%first 'pro%parse%subscan%arg[1]'
  let azel%last  'pro%parse%subscan%arg[2]'
else
  let azel%first 0  ! Automatic
  let azel%last  0  ! Automatic
endif
let azel%file 'pro%arg[1]'
let azel%doderiv     pro%parse%derivative%narg.ge.0
let azel%docommanded pro%parse%commanded%narg.ge.0

! Main work
@ plot-azel-ndumps
@ plot-azel-collect-data
@ plot-azel-plot-main
! hard 'shortname'".ps" /dev ps grey /over
g\set char 0.6
