!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
if .not.exist(window_init) then
   @ window_tools.greg
endif
!
var scan r
!
define character date*16
!
!!clear plot from gildas apr11
!!clear alpha
clear
@ window_init
!
var array r
var data r
var febe r
var mon r
var array r
var reduce
!
define integer ifb isign isub i1 i2 l ndir
define double beamSep d_az d_el fwhm restfreq tmpRounded
define real aux /like data1%data%subscan
define real iStart
!
let ifb  &1
let isub &2
!
@ identFeBe 'ifb'
!
let inter 0.25
let aspect_ratio no
!
g\set plot landscape
!
g\set expand 0.8
!
draw relocate 0.5 1 /box 7
label "Source: " /center 6
label 'scan%header%object' /center 6
label "      Scan: " /center  6
label 'scan%header%scannum' /center  6
label "      Telescope: " /center 6
label 'scan%header%telescop' /center 6
label "      Date: " /center 6
let date 'scan%header%date_obs'
label 'date' /center 6
!
draw relocate 0.5 0.3 /box 7
!
label "Frontend: "'feId' /center 6
label "      Backend: "'beId' /center 6
!
let restfreq array'ifb'%header1%restfreq
if array'ifb'%header1%restfreq.gt.1.e9 then
   let restfreq restfreq*1.e-9
endif
label "    Rest Frequency = " /center 6
label 'nint(restfreq*1000.)/1000' /center 6
label " GHz" /center 6
!
label "   Azimuth = " /center 6
let tmpRounded nint(10.*mon'ifb'%data%encoder_az_el[1,1])/10.
label 'tmpRounded' /center 6
label "\uo   Elevation = " /center 6
let tmpRounded nint(10.*mon'ifb'%data%encoder_az_el[1,2])/10.
label 'tmpRounded' /center 6
!
g\set expand 1.
!
let i1 isub
let i2 min(scan%header%nobs,'i1+3')
for idir i1 to i2 
  if idir.gt.4 then
     let l mod(idir,4)
     if l.eq.0 then
        let l 4
     endif
  else
     let l idir
  endif
  @ window_xy 2 2 'l'
  limits /var pcross%sub'idir'%dat%x pcross%sub'idir'%dat%y /blanking blankingRed 0.
  pen 0
  histogram pcross%sub'idir'%dat%x pcross%sub'idir'%dat%y /blanking blankingRed 0.
  pen 1
  connect pcross%sub'idir'%sol%x pcross%sub'idir'%sol%y
!  if (pcross%sub'idir'%fun%flag.eq.0) then
!     limits /var pcross%sub'idir'%dat%x pcross%sub'idir'%dat%y /blanking blankingRed 0.
!     pen 0
!     histogram pcross%sub'idir'%dat%x pcross%sub'idir'%dat%y /blanking blankingRed 0.
!     pen 1
!     connect pcross%sub'idir'%sol%x pcross%sub'idir'%sol%y
!  else if (pcross%sub'idir'%fun%flag.eq.-1000) then
  if (pcross%sub'idir'%fun%flag.eq.-1000) then
     pen 1
     draw text 0 0 "Waiting for data" 5 0 /char 5
  else if (pcross%sub'idir'%fun%flag.ne.0) then
     pen 1
     draw text 0 0 "Could not fit data." 5 0 /char 5     
  endif
  pen 0
  box
  if (pcross%sub'idir'%dir.eq."+LAM".or.pcross%sub'idir'%dir.eq."-LAM") then
     if pcross%sub'idir'%dir.eq."+LAM" then
        draw text -1.5 -.5 "+AZM" /box 9   
     else if pcross%sub'idir'%dir.eq."-LAM" then
        draw text -1.5 -.5 "-AZM" /box 9   
     endif
     draw text 0.5 -.5 "\gDAZ = " 6 0 /box 7 
     if (pcross%sub'idir'%fun%flag.ne.-1000) then
        let d_az nint(100*pcross%sub'idir'%fun%par2%value)/100
        label 'd_az' /center 6
        label "\r`" /center 6
     endif
     label "\gD Az [arc sec]" /x
     draw text 0.5 -1.5 "\gQ = " 6 0 /box 7 
     if (pcross%sub'idir'%fun%flag.ne.-1000) then
        let fwhm nint(100*pcross%sub'idir'%fun%par3%value)/100
        label 'fwhm' /center 6
        label "\r`" /center 6
     endif
     label "\gD Az [arc sec]" /x
!
     if febe'ifb'%header%swtchmod.eq."wobblerSwitching" then
        let aux 1e10
        let aux data'ifb'%data%integnum /where data'ifb'%data%subscan.eq.isub 
	compute iStart min aux
        if array'ifb'%data1%iswitch[iStart,1].eq."ON" then
           let isign 1
	else if array'ifb'%data1%iswitch[iStart,1].eq."OFF" then
           let isign -1
	endif
        if pcross%sub'idir'%dir.eq."+LAM" then
           let beamSep scan%header%wobthrow*3600-isign*scan%header%scanxvel*scan%header%wobcycle/2.
        else
           let beamSep scan%header%wobthrow*3600+isign*scan%header%scanxvel*scan%header%wobcycle/2.
	endif
	let beamSep nint(100*beamSep)/100.
	draw text 0.5 2.0 "beam separation:" 6 0 /box 1
	draw relocate 0.5 1.5 /box 1
	label 'beamSep'" (commanded)" /center 6
	let beamSep nint(100*pcross%sub'idir'%fun%par5%value)/100.
	draw relocate 0.5 1.0 /box 1
	label 'beamSep'" (fitted)" /center 6
     endif
!        
  else if (pcross%sub'idir'%dir.eq."+BET".or.pcross%sub'idir'%dir.eq."-BET") then
     if pcross%sub'idir'%dir.eq."+BET" then
        draw text -1.5 -.5 "+ELV" /box 9   
     else if pcross%sub'idir'%dir.eq."-BET" then
        draw text -1.5 -.5 "-ELV" /box 9   
     endif
     draw text 0.5 -.5 "\gDEL = " 6 0 /box 7 
     if (pcross%sub'idir'%fun%flag.ne.-1000) then
        let d_el nint(100*pcross%sub'idir'%fun%par2%value)/100
        label 'd_el' /center 6
        label "\r`" /center 6
     endif
     draw text 0.5 -1.5 "\gQ = " 6 0 /box 7 
     if (pcross%sub'idir'%fun%flag.ne.-1000) then
        let fwhm nint(100*pcross%sub'idir'%fun%par3%value)/100
        label 'fwhm' /center 6
        label "\r`" /center 6
     endif
     label "\gD El [arc sec]" /x
  else
     say "W-POINT, Unknown scanning direction "'pcross%sub'idir'%dir'
  endif
  if idir-isub+1.eq.1.or.idir-isub+1.eq.3 then
     if reduce'ifb'%calibration_done[2].eq.0 then
        draw text -4.5 0 "backend counts" 5 90 /char 4
     else if febe'ifb'%data%beameff[1,1].eq.febe'ifb'%data%etafss[1,1] then
        draw text -4.5 0 "T\dA\u* [K]" 5 90 /char 4
     else
        draw text -4.5 0 "T\dm\db [K]" 5 90 /char 4
     endif
  endif
!
next
!
return
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
