!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
begin procedure get-pointing-results
  if exist(&1) delete /var &1
  define structure &1 /global
  define integer &1%n /global
  let &1%n found
  if (found.gt.0) then
     define double &1%freq[&1%n] /global
     define real &1%areares[&1%n] &1%areaerr[&1%n] /global
     define real &1%posires[&1%n] &1%posierr[&1%n] /global
     define real &1%widtres[&1%n] &1%widterr[&1%n] /global
     !
     for ient 1 to found
        get next
        let &1%freq[ient] r%head%dri%freq
        let &1%areares[ient] r%head%poi%result[3]/sec
        let &1%posires[ient] r%head%poi%result[4]/sec
        let &1%widtres[ient] r%head%poi%result[5]/sec
        let &1%areaerr[ient] r%head%poi%error[3]/sec
        let &1%posierr[ient] r%head%poi%error[4]/sec
        let &1%widterr[ient] r%head%poi%error[5]/sec
     next ! ient
  endif
end procedure get-pointing-results
!
begin procedure plot-pointing-results
  define character date*32 scan*8
  define integer iscan
  let date "&1"
  let scan "&2"
  !
  find /obs 'date' 'date' /scan 'scan'
  if (found.le.0) then
     message w plot-pointing-results-1 "No data found => Do nothing"
     return
  endif
  !
  clear
  !
  define character tele*12
  define real maz paz
  define real mel pel
  let maz 180
  let paz   0
  let mel -90
  let pel +90
  !
  define real az daz saz
  define real el del sel
  define real size azhpbw elhpbw
  !
  let size 2 ! [---] Size ratio
  limits +size -size -size +size
  g\set box 2 14 3 15
  g\box
  g\label "\gdAz/HPBW" /x
  g\label "\gdEl/HPBW" /y 3.5
  pen 0 /dash 3
  g\draw relo +1 -1 /user
  g\draw line +1 +1 /user
  g\draw line -1 +1 /user
  g\draw line -1 -1 /user
  g\draw line +1 -1 /user
  !
  g\draw relo user_xmin 0 /user
  g\draw line user_xmax 0 /user /clip
  g\draw relo 0 user_ymin /user
  g\draw line 0 user_ymax /user /clip
  pen 0 /dash 1
  !
  list /toc frontend /var fetoc
  if (fetoc%nsetup.eq.0) then
     message w plot-pointing-results-1 "No frontend found for "'date'" "'scan'" => Do nothing"
     return
  endif
  !
  for ife 1 to fetoc%nsetup
     let tele 'fetoc%setup[ife,1]'
     find /obs 'date' 'date' /scan 'scan' /tele 'tele[1:6]'"*"
     list /toc fesb /var betoc
     for ibe 1 to betoc%nsetup
        pen 'ibe+(ife-1)*3'
        g\set box 2 14 3 15
        let tele 'betoc%setup[ibe,1]'
        let tele 'tele[1:9]'"*"
        find /obs 'date' 'date' /scan 'scan' /tele 'tele'
        list /toc drift /var drifttoc
        if (drifttoc%nsetup.ne.2) then
           message e plot-pointing-results-1 "Two drifts required. Got "'drifttoc%nsetup'
           break
        endif
        !
        find        /obs 'date' 'date' /scan 'scan' /tele 'tele' /subscan 1
        find append /obs 'date' 'date' /scan 'scan' /tele 'tele' /subscan 4
        @ get-pointing-results azpoint
        if (azpoint%n.gt.0) let azhpbw 2460e3/azpoint%freq[1]
        !
        find        /obs 'date' 'date' /scan 'scan' /tele 'tele' /subscan 2
        find append /obs 'date' 'date' /scan 'scan' /tele 'tele' /subscan 3
        @ get-pointing-results elpoint
        if (elpoint%n.gt.0) let elhpbw 2460e3/elpoint%freq[1]
        !
        if (azpoint%n.ne.elpoint%n) then
           message e plot-pointing-results-1 "Different number of subscans in Az and El" ! It can just be that the pointing has been interrupted
           break
        endif
        !
        for isub 1 to azpoint%n
           let az azpoint%posires[isub]/azhpbw
           let el elpoint%posires[isub]/elhpbw
           let daz azpoint%posierr[isub]/azhpbw
           let del elpoint%posierr[isub]/elhpbw
           let saz azpoint%widtres[isub]/azhpbw
           let sel elpoint%widtres[isub]/elhpbw
           g\draw relo az-daz el /user
           g\draw line az+daz el /user /clip
           g\draw relo az el-del /user
           g\draw line az el+del /user /clip
           g\ellipse saz sel /user az el
        next ! ipoint
        !   
        g\set box 14 17 3 15
        g\draw text '-6+4*ife' 'ibe-0.5' 'tele[4:8]' 8 0 /char 8
        pen 0   
     next ! ibe
  next ! ife
  !
  let size 12 ! [arcsec]
  limits +size -size -size +size
  g\set box 17 29 3 15
  g\box
  g\label "\gdAz [`]" /x
  g\label "\gdEl [`]" /y 3.5
  pen 0 /dash 3
  g\draw relo user_xmin 0 /user
  g\draw line user_xmax 0 /user /clip
  g\draw relo 0 user_ymin /user
  g\draw line 0 user_ymax /user /clip
  pen 0 /dash 1
  !
  for ife 1 to fetoc%nsetup
     let tele 'fetoc%setup[ife,1]'
     find /obs 'date' 'date' /scan 'scan' /tele 'tele[1:6]'"*"
     list /toc fesb /var betoc
     for ibe 1 to betoc%nsetup
        pen 'ibe+(ife-1)*3'
        let tele 'betoc%setup[ibe,1]'
        let tele 'tele[1:9]'"*"
        find /obs 'date' 'date' /scan 'scan' /tele 'tele'
        list /toc drift /var drifttoc
        if (drifttoc%nsetup.ne.2) then
           message e plot-pointing-results-1 "Two directions required. Got "'drifttoc%nsetup'
           break
        endif
        !
        find        /obs 'date' 'date' /scan 'scan' /tele 'tele' /subscan 1
        find append /obs 'date' 'date' /scan 'scan' /tele 'tele' /subscan 4
        @ get-pointing-results azpoint
        if (azpoint%n.gt.0) let azhpbw 2460e3/azpoint%freq[1]
        !
        find        /obs 'date' 'date' /scan 'scan' /tele 'tele' /subscan 2
        find append /obs 'date' 'date' /scan 'scan' /tele 'tele' /subscan 3
        @ get-pointing-results elpoint
        if (elpoint%n.gt.0) let elhpbw 2460e3/elpoint%freq[1]
        !
        if (azpoint%n.ne.elpoint%n) then
           message e plot-pointing-results-1 "Different number of subscans in Az and El" ! It can just be that the pointing has been interrupted
           break
        endif
        !
        ellipse azhpbw elhpbw /user 0 0        
        for isub 1 to azpoint%n
           let az azpoint%posires[isub]
           let el elpoint%posires[isub]
           let daz azpoint%posierr[isub]
           let del elpoint%posierr[isub]
           g\draw relo az-daz el /user
           g\draw line az+daz el /user /clip
           g\draw relo az el-del /user
           g\draw line az el+del /user /clip
        next ! ipoint
        pen 0
     next ! ibe
  next ! ife
  !
!  find /obs 'fetoc%setup[1,1]' /scan 'fetoc%setup[1,2]'
!  average /nocheck
  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
  !
  ! Note: you can control the output directory by setting pipe%plot%pdir
  ! to e.g. "ha" before calling the procedure.
  ! Hardcopy: no backend nor frontend in hardcopy name since they are all mixed
  @ pipeline-plot-hardcopy 'date' 'scan' "" "pointing-all"
  !
!!$  sic output pointings.tex append
!!$     say "\fullheight{"'myname'"}{"'myname'"}"
!!$  sic output
end procedure plot-pointing-results
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
if (pro%narg.ne.2) then
   message e "Usage: @ plot-pointing-results-1 date scan"
   return base
endif
!
pen 1 /col red
pen 2 /col brown
pen 3 /col light_coral
pen 4 /col medium_sea_green
pen 5 /col sea_green
pen 6 /col spring_green
pen 0
!
@ plot-pointing-results &1 &2
!
pen /default
! 
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
