!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
begin procedure plot-pointing-set-minmax
  define double min max xmin xmax ymin ymax
  let xmin 1e10
  let ymin 1e10
  let xmax -1e10
  let ymax -1e10
  for ient 1 to found
     get next
     compute min min rx
     let xmin min(min,xmin)
     compute min min ry /blank r%head%dri%cbad
     if min.ne.r%head%dri%cbad  ! cbad was returned if all are bad
       let ymin min(min,ymin)
     endif
     !
     compute max max rx
     let xmax max(max,xmax)
     compute max max ry /blank r%head%dri%cbad
     if max.ne.r%head%dri%cbad  ! cbad was returned if all are bad
       let ymax max(max,ymax)
     endif
  next ! ient
  if (xmin.ne.xmax) then   
     set mode x xmin/sec xmax/sec
  else
     set mode x xmin/sec-1 xmin/sec+1
  endif
  if (ymin.ne.ymax) then
     set mode y ymin ymax
  else
     set mode y ymin-1 ymin+1
  endif
end procedure plot-pointing-set-minmax
!
begin procedure plot-pointing-one-drift
  define character date*32 scan*8 tele*32 label*2   
  define integer ix iy ny idrift
  let ix &1
  let iy &2
  let ny &3
  let date "&4"
  let scan "&5"
  let tele "&6"
  let idrift &7
  let label "&8"
  !
  @ window_xy 2 3 'ix' 'iy+(3-ny)'
  if (ix.eq.1) g\label 'tele[4:8]' /y 5.0 !***JP: /y not taken into account
  if ((ix.eq.1).and.(iy.eq.1)) then
     box
  else if (ix.eq.1) then
     box n o
  else if (iy.eq.1) then
     box p n
  else
     box n n
  endif
  if (iy.eq.ny) then
     pen 1
     g\draw text 0 0 'label' 9 0 /char 7
     pen 0
  endif
  if (iy.eq.1) g\label "Offset [`]" /x
  !
  find /obs 'date' 'date' /scan 'scan' /tele 'tele' /subscan 'idrift'
  if (found.gt.0) then
     get first
     pen 0
     spectrum
     visu /pen 1
  endif
end procedure plot-pointing-one-drift
!
begin procedure plot-pointing-two-drifts
  define character date*32 scan*8 tele*32 label*2   
  define integer ix iy ny iforward ibackward
  let ix &1
  let iy &2
  let ny &3
  let date "&4"
  let scan "&5"
  let tele "&6"
  let iforward &7
  let ibackward &8
  let label "&9"
  !
  @ window_xy 2 3 'ix' 'iy+(3-ny)'
  if (ix.eq.1) g\label 'tele[4:8]' /y 5.0 !***JP: /y not taken into account
  if ((ix.eq.1).and.(iy.eq.1)) then
     box
  else if (ix.eq.1) then
     box n o
  else if (iy.eq.1) then
     box p n
  else
     box n n
  endif
  if (iy.eq.ny) then
     pen 1
     g\draw text 0 0 "+"'label' 9 0 /char 7 !***JP: Sign needs to be checked
     pen 4
     g\draw text 0 0 "-"'label' 7 0 /char 9 !***JP: Sign needs to be checked
     pen 0
  endif
  if (iy.eq.1) g\label "Offset [`]" /x
  !
  find /obs 'date' 'date' /scan 'scan' /tele 'tele' /subscan 'iforward'
  if (found.gt.0) then
     get first
     pen 'iforward'
     spectrum
     visu /pen 'iforward'
  endif
  !
  find /obs 'date' 'date' /scan 'scan' /tele 'tele' /subscan 'ibackward'
  if (found.gt.0) then
     get first
     pen 'ibackward'
     spectrum
     visu /pen 'ibackward'
     pen 0
  endif
end procedure plot-pointing-two-drifts
!
begin procedure plot-pointing-results
  define character date*32 scan*8
  let date "&1"
  let scan "&2"
  !
  find /obs 'date' 'date' /scan 'scan'
  if (found.le.0) then
     message w plot-pointing-results-2 "No data found => Do nothing"
     return
  endif
  !
  @ window_tools.greg
  let position 0.214 1.322 0.1 0.8
  let inter 0.05 0.10
  let aspect_ratio no
  !
  define character tele*12 mydate*32 myname*32
  !
  list /toc frontend /var fetoc
  for ife 1 to fetoc%nsetup
     clear
     let tele 'fetoc%setup[ife,1]'
     find /obs 'date' 'date' /scan 'scan' /tele  'tele[1:6]'"*"
     @ plot-pointing-set-minmax
     list /toc fesb /var betoc
     if (betoc%nsetup.eq.0) break
     if (betoc%nsetup.gt.3) then
        message w plot-pointing-results-2 'betoc%nsetup'" backends => Restricting to 3"
        let betoc%nsetup 3
     endif
     for ibe 1 to betoc%nsetup
        let tele 'betoc%setup[ibe,1]'
        let tele 'tele[1:9]'"*"
        find /obs 'date' 'date' /scan 'scan' /tele 'tele'
        list /toc direction /var dirtoc
        if (dirtoc%nsetup.eq.2) then
           @ plot-pointing-one-drift 1 'ibe' 'betoc%nsetup' 'date' 'scan' 'tele' 1 "Az" ! Azimuth
           @ plot-pointing-one-drift 2 'ibe' 'betoc%nsetup' 'date' 'scan' 'tele' 3 "El" ! Elevation
        else if (dirtoc%nsetup.eq.4) then
           @ plot-pointing-two-drifts 1 'ibe' 'betoc%nsetup' 'date' 'scan' 'tele' 1 4 "Az" ! Azimuth
           @ plot-pointing-two-drifts 2 'ibe' 'betoc%nsetup' 'date' 'scan' 'tele' 2 3 "El" ! Elevation
        else
           message e plot-pointing-results-2 "Two or Four directions required. Got "'dirtoc%nsetup'
           break
        endif
     next ! ibe
     !
     ! let tele 'fetoc%setup[ife,1]'
     ! find /obs 'date' 'date' /scan 'scan' /tele  'tele[1:6]'"*"
     ! average /nocheck
     if (found.gt.0) then
        set format long
        set format position on
        set format quality on
        set format spectral off
        set format calibration off
        set format atmosphere off
        set format continuum off
        set format origin off
        title
     endif
     !
     ! Note: you can control the output directory by setting pipe%plot%pdir
     ! to e.g. "ha" before calling the procedure.
     ! Hardcopy: no backend since they are mixed; use frontend name.
     @ pipeline-plot-hardcopy 'date' 'scan' "" "pointing-"'tele[4:5]'
  next ife
end procedure plot-pointing-results
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
set var cont write
method basecont
!
if (pro%narg.ne.2) then
   message e plot-pointing-results-2 "Usage: @ plot-pointing-results-2 date scan"
   return base
endif
!
pen 1 /col 1
pen 2 /col 1
pen 3 /col 3
pen 4 /col 3
pen 0
!
@ plot-pointing-results &1 &2
!
pen /default
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
