begin procedure plot-lonlat-usage
  !---------------------------------------------------------------------
  ! Usage displayed if bad calling sequence
  !---------------------------------------------------------------------
  message i plot-lonlat "Usage:"
  message r plot-lonlat " @ plot-lonlat FitsName"
  message r plot-lonlat ""
end procedure plot-lonlat-usage

begin procedure plot-lonlat-structure
  if .not.exist(lonlat) then
    define structure     lonlat          /global
    define character*256 lonlat%fitsfile /global
    define real          lonlat%lmin     /global
    define real          lonlat%lmax     /global
    define real          lonlat%bmin     /global
    define real          lonlat%bmax     /global
  endif
end procedure plot-lonlat-structure

begin procedure plot-lonlat-minmax
  read 'lonlat%fitsfile' /file
  variable imbf
  let lonlat%lmin +1000
  let lonlat%lmax -1000
  let lonlat%bmin +1000
  let lonlat%bmax -1000
  define real lmin lmax bmin bmax
  for isub 1 2 imbf%prim%n_obsp-1 imbf%prim%n_obsp  ! 2 first and 2 last should be enough
    read 'lonlat%fitsfile' /file /subscan isub
    variable subs
    if subs%antslow%head%substype.eq."onTheFly" then
      compute lmin min subs%antslow%table%longoff
      compute lmax max subs%antslow%table%longoff
      compute bmin min subs%antslow%table%latoff
      compute bmax max subs%antslow%table%latoff
      let lonlat%lmin min(lonlat%lmin,lmin)
      let lonlat%lmax max(lonlat%lmax,lmax)
      let lonlat%bmin min(lonlat%bmin,bmin)
      let lonlat%bmax max(lonlat%bmax,bmax)
    endif
  next isub
end procedure plot-lonlat-minmax

! Command line parsing
if pro%narg.le.0 then
  @ plot-lonlat-usage
  return
endif

@ plot-lonlat-structure
let lonlat%fitsfile 'pro%arg[1]'

g\set plot landscape
g\clear

! Compute plot min-max
@ plot-lonlat-minmax

! Plot box
g\limits lonlat%lmin/sec lonlat%lmax/sec lonlat%bmin/sec lonlat%bmax/sec /margin 5
g\set box match
g\box
g\limits lonlat%lmin     lonlat%lmax     lonlat%bmin     lonlat%bmax     /margin 5
g\draw text 0 0.5 'lonlat%fitsfile' /box 8
g\label "LongOFF [sec]" /x
g\label "LatOFF [sec]" /y

g\set mark 4 1 0.1 45
define integer icol bounds[2] lbound ubound
for isub 1 to imbf%prim%n_obsp
  read 'lonlat%fitsfile' /file /subscan isub
  variable subs
  if subs%antslow%head%substype.eq."onTheFly" then
    let icol icol+1
    if icol.gt.3 let icol 0
    ! Antslow
    compute bounds location subs%antslow%table%mjd subs%antslow%head%mjd_beg
    let lbound bounds[2]  ! Should be interpolation between both
    compute bounds location subs%antslow%table%mjd subs%antslow%head%mjd_end
    let ubound bounds[1]  ! Should be interpolation between both
    say "Subscan #"'isub'" antslow has "'ubound-lbound+1'" on-track dumps"
    g\pen /col icol
    g\points subs%antslow%table%longoff[lbound:ubound] subs%antslow%table%latoff[lbound:ubound]
    g\pen /col foreground
    ! BackendHOLO start
    compute bounds location subs%backdata%table%mjd subs%backdata%head%mjd_beg
    let lbound bounds[2]
    compute bounds location subs%antslow%table%mjd subs%backdata%table%mjd[lbound]
    let lbound bounds[2]
    g\draw text subs%antslow%table%longoff[lbound] subs%antslow%table%latoff[lbound] "o" /user
    ! backendholo stop
    compute bounds location subs%backdata%table%mjd subs%backdata%head%mjd_end
    let ubound bounds[2]
    if subs%backdata%table%mjd[subs%backdata%head%desc%naxis2].lt.subs%backdata%head%mjd_end then
      say "Subscan #"'isub'" backendHOLO data does not cover on-track range until the end"
    endif
    compute bounds location subs%antslow%table%mjd subs%backdata%table%mjd[ubound]
    let ubound bounds[2]
    g\draw text subs%antslow%table%longoff[ubound] subs%antslow%table%latoff[ubound] "o" /user
  endif
next isub
