on error return
!
greg1\set expand 1.
!
define integer ifb
define double azNew azOld elNew elOld restfreq tmpRounded
!
var scan r
var febe r
var mon  r
var array r
var reduce
!
let ifb &1
@ identFeBe 'ifb'
!
if .not.exist(window_init) then
   @ window_tools.greg
endif
!
define character date*16 sideband*3
!
clear 
!! clear alpha
@ window_init
!
let inter 0.25
!
let aspect_ratio yes
!
g\set plot landscape
draw relocate 0.5 0.5 /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.5 /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
!
let azOld mon'ifb'%header%iaobs_caobs_ieobs[2]*3.6d+3
let elOld mon'ifb'%header%iaobs_caobs_ieobs[3]*3.6d+3
let azNew mon'ifb'%header%iaobs_caobs_ieobs[2]*3.6d+3+pnt%azm%fit%position
let elNew mon'ifb'%header%iaobs_caobs_ieobs[3]*3.6d+3+pnt%elv%fit%position
!
draw relocate 0.5 -1.5 /box 7 
label "Azimuth = " /center 6
let tmpRounded nint(10.*mon'ifb'%data%encoder_az_el[1,1])/10.
label 'tmpRounded' /center 6
label "\uo    old Az corr = " /center 6
label 'nint(azOld*10.)/10.'
label "\r`" /center 6
pen /col 1
label "    new Az corr = " /center 6
label 'nint(azNew*10.)/10.'
label "\r`" /center 6
pen /def
!
draw relocate 0.5 -2.5 /box 7
label "Elevation = " /center 6
let tmpRounded nint(10.*mon'ifb'%data%encoder_az_el[1,2])/10.
label 'tmpRounded' /center 6
label "\uo    old El  corr = " /center 6
label 'nint(elOld*10.)/10.'
label "\r`" /center 6
pen /col 1
label "    new El  corr = " /center 6
label 'nint(elNew*10.)/10.'
label "\r`" /center 6
pen /def
!
!
if trc[2]-blc[2].ne.0.and.trc[1]-blc[1].ne.0 then
   limits blc[1] trc[1] blc[2] trc[2] /blanking blankingRed 0.
else
   if pnt%azm%data%n.gt.1.and.pnt%elv%data%n.gt.1.and.febe'ifb'%header%swtchmod.ne."wobblerSwitching" then
      limits         /var pnt%azm%data%x pnt%azm%data%y /blanking blankingRed 0.
      limits < > < > /var pnt%elv%data%x pnt%elv%data%y /blanking blankingRed 0. 
   else if pnt%azm%data%n.gt.1 then
      limits         /var pnt%azm%data%x pnt%azm%data%y /blanking blankingRed 0.
   else if pnt%elv%data%n.gt.1 then
      limits         /var pnt%elv%data%x pnt%elv%data%y /blanking blankingRed 0.
   endif
endif
!
let blc[1:2] 0. 0.
let trc[1:2] 0. 0. 
!
if pnt%azm%data%n.gt.1 then
!
   @window_xy 2 1 1
!
   if febe'ifb'%header%swtchmod.eq."wobblerSwitching".and.pnt%elv%data%n.gt.1 then
      limits /var pnt%elv%data%x pnt%elv%data%y /blanking blankingRed 0.
      let xmin2 user_xmin /new real
      let xmax2 user_xmax /new real
      limits * * < > /var pnt%azm%data%x pnt%azm%data%y /blanking blankingRed 0.
      let xmin1 user_xmin /new real
      let xmax1 user_xmax /new real
      let xScale2 2./(1+(xmax1-xmin1)/(xmax2-xmin2)) /new real
      let xScale1 xScale2*(xmax1-xmin1)/(xmax2-xmin2) /new real
      let boxXmax box_xmin+xScale1*(box_xmax-box_xmin) /new real
      g\set box box_xmin boxXmax box_ymin box_ymax 
   endif
!
   box 
!
   draw relocate user_xmin+0.05*(user_xmax-user_xmin) user_ymax-0.05*(user_ymax-user_ymin) /user 
   label "\gDAZ = " /center 6
   label 'nint(pnt%azm%fit%position*10.)/10.'
   label "\r`" /center 6
   draw relocate user_xmin+0.05*(user_xmax-user_xmin) user_ymax-0.15*(user_ymax-user_ymin) /user 
   label "\gQ = " /center 6
   label 'nint(pnt%azm%fit%width*10.)/10.'
   label "\r`" /center 6
!
   if (pnt%azm%fit%flag.ne.0) then
      pen /col 1
      draw text 0 0 "No fit (useless data)." 5 0 /char 5
      pen /def
   endif
!
!
   draw relocate user_xmax-0.15*(user_xmax-user_xmin) user_ymax-0.15*(user_ymax-user_ymin) /user 
   if reduce'ifb'%calibration_done[2].eq.0 then
      let tmpRounded pnt%azm%fit%area/pnt%azm%fit%width/sqrt(pi/(4.*log(2.d0)))
      let tmpRounded nint(tmpRounded)
      label 'tmpRounded' /center 4
      label "cnts" /center 6
   else if febe'ifb'%data%beameff[1,1].eq.febe'ifb'%data%etafss[1,1] then
      let tmpRounded pnt%azm%fit%area/pnt%azm%fit%width/sqrt(pi/(4.*log(2.d0)))
      let tmpRounded nint(100.*tmpRounded)/100.
      label 'tmpRounded' /center 4
      label " K" /center 6
   else
      let tmpRounded pnt%azm%fit%area/pnt%azm%fit%width/sqrt(pi/(4.*log(2.d0)))
      let tmpRounded nint(100.*tmpRounded)/100.
      label 'tmpRounded' /center 4
      label " K" /center 6
   endif
!
   histogram pnt%azm%data%x pnt%azm%data%y /blanking blankingRed 0.
   label "\gD Az [\r`]" /x
!
   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
!
   pen /col 1
   conn pnt%azm%profile%x pnt%azm%profile%y
   pen /def
!
endif
!
if pnt%elv%data%n.gt.1 then
!
   @window_xy 2 1 2
!
   if febe'ifb'%header%swtchmod.eq."wobblerSwitching".and.pnt%azm%data%n.gt.1 then
      limits * * = = /var pnt%elv%data%x pnt%elv%data%y /blanking blankingRed 0.
      let boxXmin box_xmax-xScale2*(box_xmax-box_xmin) /new real
      g\set box boxXmin box_xmax box_ymin box_ymax 
   endif
   box 
!
   draw relocate user_xmin+0.05*(user_xmax-user_xmin) user_ymax-0.05*(user_ymax-user_ymin) /user 
   label "\gDEL = " /center 6
   label 'nint(pnt%elv%fit%position*10.)/10.'
   label "\r`" /center 6
   draw relocate user_xmin+0.05*(user_xmax-user_xmin) user_ymax-0.15*(user_ymax-user_ymin) /user 
   label "\gQ = " /center 6
   label 'nint(pnt%elv%fit%width*10.)/10.'
   label "\r`" /center 6
!
   if (pnt%elv%fit%flag.ne.0) then
      pen /col 1
      draw text 0 0 "No fit (useless data)." 5 0 /char 5
      pen /def
   endif
!
   draw relocate user_xmax-0.15*(user_xmax-user_xmin) user_ymax-0.15*(user_ymax-user_ymin) /user 
   if reduce'ifb'%calibration_done[2].eq.0 then
      let tmpRounded pnt%elv%fit%area/pnt%elv%fit%width/sqrt(pi/(4.*log(2.d0)))
      let tmpRounded nint(tmpRounded)
      label 'tmpRounded' /center 4
      label "cnts" /center 6
   else if febe'ifb'%data%beameff[1,1].eq.febe'ifb'%data%etafss[1,1] then
      let tmpRounded pnt%elv%fit%area/pnt%elv%fit%width/sqrt(pi/(4.*log(2.d0)))
      let tmpRounded nint(100.*tmpRounded)/100.
      label 'tmpRounded' /center 4
      label " K" /center 6
   else
      let tmpRounded pnt%elv%fit%area/pnt%elv%fit%width/sqrt(pi/(4.*log(2.d0)))
      let tmpRounded nint(100.*tmpRounded)/100.
      label 'tmpRounded' /center 4
      label " K" /center 6
   endif
!
   histogram pnt%elv%data%x pnt%elv%data%y /blanking blankingRed 0.
   label "\gD El [\r`]" /x
!
!
   pen /col 1
   conn pnt%elv%profile%x pnt%elv%profile%y
   pen /def
!
endif
