on error return
!
g\set expand 1.0
!
if .not.exist(window_init) then
   @ window_tools.greg
endif
!
var scan r
var febe r
var data r
var array r
var reduce r
var mon r
var raw r
!
define character date*10
define integer   nsubscan
define real      max mean
define double    currentFocus focusOffset newFocus sum tmpRounded
!
clear 
!!clear alpha
!
define integer idir ifb ipix
let ifb = &1
let ipix = &2
!
if raw1%subRef1%focusTransl.eq."X" then
   idir = 1
else if raw1%subRef1%focusTransl.eq."Y" then
   idir = 2
else if raw1%subRef1%focusTransl.eq."Z" then
   idir = 3
endif
!
define real aux w /like data'ifb'%data%dfocus_x_y_z[idir]
!
aux = data'ifb'%data%subscan
compute max max aux 
nsubscan = int(max)
!
@ window_init
@ identFeBe 'ifb'
!
let inter 0.25
let aspect_ratio no 
g\set plot landscape
!
@window_xy 1 2 1 
!
draw relocate 0.5 0 /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
date = 'scan%header%date_obs'
label 'date' /center 6
!
draw relocate 0.5 -1 /box 7 
!
label "Frontend: "'feId' /center 6
label "         Backend: "'beId' /center 6
!
draw relocate 0.5 -2 /box 7 
label "Azimuth = " /center 6
tmpRounded = nint(10.*mon'ifb'%data%encoder_az_el[1,1])/10.
label 'tmpRounded' /center 6
label "\uo      Elevation = " /center 6
tmpRounded = nint(10.*mon'ifb'%data%encoder_az_el[1,2])/10.
label 'tmpRounded' /center 6
label "\uo" /center 6
!
currentFocus = mon'ifb'%header%focobs_x_y_z[idir]
focusOffset  = -zfocus%fun1%par2%value/(2.*zfocus%fun1%par3%value)-currentFocus
newFocus     = currentFocus+focusOffset
!
currentFocus = nint(100.*currentFocus)/100.
focusOffset  = nint(100.*focusOffset)/100.
newFocus     = nint(100.*newFocus)/100.
!
draw relocate 0.5 -4 /box 7
if idir.eq.1 then
   label "Current focus: sfcx" /center 6
else if idir.eq.2 then
   label "Current focus: sfcy" /center 6
else if idir.eq.3 then
   label "Current focus: sfcz" /center 6
endif
draw relocate 6 -4 /box 7
if currentFocus.ge.0 then
   label " = +"'currentFocus'" mm" /center 6
else
   label " = "'currentFocus'" mm" /center 6
endif
draw relocate 0.5 -5 /box 7
label "Focus offset:  \gD" /center 6
draw relocate 6 -5 /box 7
if focusOffset.ge.0 then
   label " = +"'focusOffset'" mm" /center 6
else
   label " = "'focusOffset'" mm" /center 6
endif
draw relocate 0.5 -6 /box 7
if idir.eq.1 then
   label "New focus:    sfcx" /center 6
else if idir.eq.2 then
   label "New focus:    sfcy" /center 6
else if idir.eq.3 then
   label "New focus:    sfcz" /center 6
endif
draw relocate 6 -6 /box 7
if newFocus.ge.0 then
   label " = +"'newFocus'" mm" /center 6
else
   label " = "'newFocus'" mm" /center 6
endif
!
@window_xy 1 2 2
!
greg\set marker 4 3 .3
!
define real data[nsubscan] offset[nsubscan]
!
for i 1 to nsubscan
    let aux = blankingRed
    let aux = data'ifb'%data%dfocus_x_y_z[idir] /where data'ifb'%data%subscan.eq.i
    compute max max aux 
    offset[i] = max
    if max.eq.blankingRed then
       data[i] = blankingRed
    else
       let w = 1
       let w = 0 /where data'ifb'%data%subscan.ne.i
       let aux = data'ifb'%data%otfdata[ipix,1,]*w
       compute sum sum aux
       data[i] = sum 
       compute sum sum w
       data[i] = data[i]/sum
    endif
next
!
!
limits /var zfocus%sol1%x zfocus%sol1%y         /blanking blankingRed 0.
limits < > < > /var zfocus%dat1%x zfocus%dat1%y /blanking blankingRed 0.
limits < > < > /var offset data                 /blanking blankingRed 0.
box 
!
points offset data
!
label "Displacement [mm]" /x
!
if reduce'ifb'%calibration_done[2].eq.0 then
   draw text -4 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 0 "T\dA\u* [K]" 5 90 /char 4
else
   draw text -4 0 "T\dm\db [K]" 5 90 /char 4
endif
!
pen /col 1
conn zfocus%sol1%x zfocus%sol1%y
pen /def
!
