!
on error return
!
g\set marker 4 3 .07
!
define integer ndim[5]
define real nFast nObs dxu pol[4,2] traceLimits
define double speedMax
define character date*19
!
let traceLimits &1
!
@ identFeBe 1
!
g\set tick 0.1
g\set plot landscape
!
define real bxmin bxmax bymin bymax
let bxmin mira_xmin-3.0
let bxmax mira_xmax
let bymin mira_ymax-2.5
let bymax mira_ymax-0.5
!
g\set box bxmin bxmax bymin bymax
!
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
!
compute ndim dim mon1%data%subscanFast
let nFast ndim[1]
define double speed['nFast-1']
define double dAz dEl speedTime timeStep /like speed
let nobs mon1%data%subscanFast[nFast]
!
draw relocate 0.5 -1 /box 7
!
label "Procedure: " /center 6
label 'scan%header%scantype' /center 6
label "  Switchmode: " /center 6
label 'febe1%header%swtchmod' /center 6
label "  Frontend: "'feId' /center 6
label "  Backend: "'beId' /center 6
!
label "  "'nObs'" subscans" /center 6
!
g\set expand .8
!
let dx (mira_xmax-mira_xmin+3.0)/2
let dy (mira_ymax-2.5-mira_ymin)/3
!
define double ftraceTime trackingAz trackingEl /like antenna%encoder_az_el[3]
define double straceTime dLambdaSlow dBetaSlow /like antenna%slowtrace[3]
define double backendTime dLambda dBeta /like data1%data%midtime
define double subscanEnd[nObs] subscanStart[nObs]
!
let dLambdaSlow antenna%slowtrace[1]*3600
let dBetaSlow   antenna%slowtrace[2]*3600
let ftraceTime antenna%encoder_az_el[3]-antenna%encoder_az_el[1,3]
let ftraceTime (ftraceTime+mod(antenna%encoder_az_el[1,3],1))*24
let straceTime antenna%slowtrace[3]-antenna%slowtrace[1,3]
let straceTime (straceTime+mod(antenna%slowtrace[1,3],1))*24
!
let backendTime data1%data%midtime-data1%data%midtime[1]
let backendTime (backendTime+mod(data1%data%midtime[1],1))*24
!
let trackingAz antenna%tracking_az_el[1]*3600
let trackingEl antenna%tracking_az_el[2]*3600

let dLambda data1%data%longoff*3600
let dBeta data1%data%latoff*3600

let speedTime mon1%data%encoder_az_el[2:nFast,3]-mon1%data%encoder_az_el[1,3]
let speedTime (speedTime+mod(mon1%data%encoder_az_el[1,3],1))*24
let dAz mon1%data%encoder_az_el[2:nfast,1]-mon1%data%encoder_az_el[1:'nFast-1',1]
let dEl mon1%data%encoder_az_el[2:nfast,2]-mon1%data%encoder_az_el[1:'nFast-1',2]
let timeStep 24.*(mon1%data%encoder_az_el[2:nfast,3]-mon1%data%encoder_az_el[1:'nFast-1',3])

let speed sqrt(dAz^2+dEl^2)/timeStep /where timeStep.ne.0
compute speedMax max speed
let speed blankingRaw /where timeStep.eq.0

for l 1 to nObs
    let subscanStart[l] raw'l'%antenna1%subscanStart-raw1%antenna1%subscanStart
    let subscanEnd[l] raw'l'%antenna1%subscanEnd-raw1%antenna1%subscanStart
next
let subscanStart (subscanStart+mod(raw1%antenna1%subscanStart,1))*24
let subscanEnd   (subscanEnd+mod(raw1%antenna1%subscanStart,1))*24

let k 0 /new integer
for i 1 to 3
    let bymax mira_ymax-2.5-(i-1)*dy
    let bymin bymax-dy
    for j 1 to 2
        let bxmin mira_xmin+(j-1)*dx+0.2*dx-3.0
	let bxmax bxmin+0.8*dx
	g\set box bxmin bxmax bymin bymax

        let k k+1
	if k.eq.2.or.k.eq.4 then
	   if traceLimits.eq.0 then
              limits /var ftraceTime trackingAz 
              limits = = < > /var ftraceTime trackingEl 
	   else
              limits * * -traceLimits traceLimits /var ftraceTime trackingAz
	   endif
        endif
	if k.eq.1 then 
           limits /var ftraceTime antenna%encoder_az_el[1] /blanking 0. 0.
           limits < > < > /var backendTime data1%data%azimuth /blanking 0. 0.
	   let pol[1,2] user_ymin
	   let pol[2,2] user_ymin
	   let pol[3,2] user_ymax
	   let pol[4,2] user_ymax
	   for l 1 to nObs
	       let pol[1,1] subscanStart[l]
	       let pol[2,1] subscanEnd[l]
	       let pol[3,1] subscanEnd[l]
	       let pol[4,1] subscanStart[l]
	       poly pol /variable /fill 9
           next
	   box n o
	   points ftraceTime antenna%encoder_az_el[1]
	   pen /col 3
           g\set marker 4 3 .1
	   points backendTime data1%data%azimuth
           g\set marker 4 3 .07
	   pen /def
	   g\set orient 90
	   g\draw relocate -3 0 /box 4
	   label "fast trace azimuth [deg]" /center 5
	   g\draw relocate -2.5 0 /box 4
	   pen /col 3
	   label "as interpolated" /center 5
	   pen /def
	   g\set orient 0
        else if k.eq.3 then
           limits /var ftraceTime antenna%encoder_az_el[2] /blanking 0. 0.
           limits < > < > /var backendTime data1%data%elevatio /blanking 0. 0.
	   let pol[1,2] user_ymin
	   let pol[2,2] user_ymin
	   let pol[3,2] user_ymax
	   let pol[4,2] user_ymax
	   for l 1 to nObs
	       let pol[1,1] subscanStart[l]
	       let pol[2,1] subscanEnd[l]
	       let pol[3,1] subscanEnd[l]
	       let pol[4,1] subscanStart[l]
	       poly pol /variable /fill 9
           next
	   box n o
	   points ftraceTime antenna%encoder_az_el[2]
	   pen /col 3
           g\set marker 4 3 .1
	   points backendTime data1%data%elevatio
           g\set marker 4 3 .07
	   pen /def
	   g\set orient 90
	   g\draw relocate -3.0 0 /box 4
	   label "elevation [deg]" /center 5
	   g\draw relocate -2.5 0 /box 4
	   pen /col 3
	   label "as interpolated" /center 5
	   pen /def
	   g\set orient 0
        else if k.eq.2 then
	   let pol[1,2] user_ymin
	   let pol[2,2] user_ymin
	   let pol[3,2] user_ymax
	   let pol[4,2] user_ymax
	   for l 1 to nObs
	       let pol[1,1] subscanStart[l]
	       let pol[2,1] subscanEnd[l]
	       let pol[3,1] subscanEnd[l]
	       let pol[4,1] subscanStart[l]
	       poly pol /variable /fill 9
           next
	   box n o
	   points ftraceTime trackingAz 
	   g\draw relocate -2.0 0 /box 4
	   g\set orient 90
	   label "Azimuth Tracking [\r`]" /center 5
	   g\set orient 0
        else if k.eq.4 then
	   let pol[1,2] user_ymin
	   let pol[2,2] user_ymin
	   let pol[3,2] user_ymax
	   let pol[4,2] user_ymax
	   for l 1 to nObs
	       let pol[1,1] subscanStart[l]
	       let pol[2,1] subscanEnd[l]
	       let pol[3,1] subscanEnd[l]
	       let pol[4,1] subscanStart[l]
	       poly pol /variable /fill 9
           next
	   box n o
	   points ftraceTime trackingEl
	   g\draw relocate -2.0 0 /box 4
	   g\set orient 90
	   label "Elevation Tracking [\r`]" /center 5
	   g\set orient 0
        else if k.eq.5 then
	   g\draw relocate 0 -1 /box 2
	   label "UTC [h]" /center 5
           g\draw relocate -2.0 0 /box 4
	   g\set orient 90
	   label "|Speed| [\r`/sec]" /center 5
	   g\set orient 0
	   limits /var ftraceTime antenna%encoder_az_el[2]
           limits = = < > /var speedTime speed /blanking blankingRaw 0.
	   let pol[1,2] user_ymin
	   let pol[2,2] user_ymin
	   let pol[3,2] user_ymax
	   let pol[4,2] user_ymax
	   for l 1 to nObs
	       let pol[1,1] subscanStart[l]
	       let pol[2,1] subscanEnd[l]
	       let pol[3,1] subscanEnd[l]
	       let pol[4,1] subscanStart[l]
	       poly pol /variable /fill 9
           next
	   box p o
	   points speedTime speed /blanking blankingRaw 0
	   let dxu (user_xmax-user_xmin)*0.25
	   if abs(speedMax).ge.1800 then
	      pen /col 6
	      g\draw relocate user_xmin 1800 /user
	      g\draw line 'user_xmin+dxu' 1800 /user
	      g\draw relocate 'user_xmin+dxu' 1800 /user
	      label " max. telescope speed" /center 6
	      g\draw relocate 'user_xmax-dxu' 1800 /user
	      g\draw line 'user_xmax' 1800 /user
	      pen /def
	   endif
        else if k.eq.6 then
           limits = = * * /var stracetime  dLambdaSlow
           limits = = < > /var stracetime  dBetaSlow
	   limits = = < > /var backendTime dLambda
	   limits = = < > /var backendTime dBeta
	   let pol[1,2] user_ymin
	   let pol[2,2] user_ymin
	   let pol[3,2] user_ymax
	   let pol[4,2] user_ymax
	   for l 1 to nObs
	       let pol[1,1] subscanStart[l]
	       let pol[2,1] subscanEnd[l]
	       let pol[3,1] subscanEnd[l]
	       let pol[4,1] subscanStart[l]
	       poly pol /variable /fill 9
           next
	   box p o
	   g\draw relocate 0 -1 /box 2
	   label "UTC [h]" /center 5
           g\draw relocate -2.0 0 /box 4
	   g\set orient 90
	   label "\gD\gl, " /center 5
	   pen /col 1
	   label "\gD\gb" /center 6
	   pen /def
	   label " [\r`]" /center 6
	   g\set orient 0
	   pen /col 11
           g\points straceTime dLambdaSlow
           g\points straceTime dBetaSlow
           pen /def
	   g\points backendTime dLambda
	   pen /col 1
	   g\points backendTime dBeta
           pen /def
        endif

    next
next
!
g\set expand 1.0
!
