on error return
!
clear 
g\set tick 0.1
g\set plot landscape
!
define integer ibe ifb ipix i1 i2 ii jpix j1 j2
define integer nbasebands nchan inchan nphases npix nrecords nsubscan nsub
define real bxmax bxmin bymax bymin uymax ymax ymin
define double aux chi chi0 cpa elevation spa
define double max mean coldCounts hotCounts calCounts[3] 
define double pAmb rHum tAmb tHot tCold tRec tSys tCal tauZ pwv
define logical doHera badColdFlag badSkyFlag saturationFlag xpolMode
define character rxFlag*1 xpolString*3
!
if .not.exist(switchMode) then
  define character switchMode*20 /global
endif
!
let ibe = &1
let jpix = &2
!
if scan%header%nfebe.gt.2 then
   let xpolString 'febe3%header%febe'
endif
!
if xpolString.eq."E0R".or.xpolString.eq."E1R".or.xpolString.eq."E2R".or.xpolString.eq."E3R" then
   let xpolMode .true.
else
   let xpolMode .false.
endif
!
let rxFlag 'febe1%header%febe'
!
if ibe.gt.scan%header%nfebe then
   ifb = ibe
   ibe = ibe-scan%header%nfebe
else
   ifb = ibe
endif
!
if jpix.ne.0 then
   npix = 1
else
   npix = febe'ibe'%header%febefeed
endif
!
dy = (mira_ymax-mira_ymin)/sqrt(npix)
dx = dy
!
let doHera = scan%data%fe[ibe].eq.50.or.scan%data%fe[ibe].eq.60
!
if doHera.and.npix.gt.1 then
   g\set expand 0.6
else
   g\set expand 1.0
endif
nbasebands = febe'ibe'%header%febeband
define real channels_per_band[nbasebands]
!
define real raux /like data'ibe'%data%integnum
raux = data'ibe'%data%integnum
!
compute aux max raux 
nrecords = int(aux)
nsubscan = data'ibe'%data%subscan[nrecords]
!
for i 1 to nbasebands 
    channels_per_band[i] = array'ibe'%header'i'%usedchan
next
!
@ computeOffsets 'ibe' 0 'doHera'
!
compute aux max channels_per_band
nchan = int(aux)
!
if scan%data%be[ibe].lt.10.or.scan%data%be[ibe].gt.19 then
   define integer subscanEnd[nsubscan] subscanStart[nsubscan]
   nsub = max(3,nsubscan)
   define real rx[nchan,nbasebands] ry[npix,nchan,nbasebands,nsub]
else
   define real rx[nrecords] ry[npix,nrecords]
endif
define real w[nchan] caux[nchan]
!
nphases = febe'ibe'%header%nphases
!
!
if scan%data%be[ibe].lt.10.or.scan%data%be[ibe].gt.19 then
!
   for i 1 to nbasebands 
       inchan = int(channels_per_band[i])
       j1 = 1+array'ibe'%header'i'%dropchan
       j2 = j1+array'ibe'%header'i'%usedchan-1
       define integer indx[nchan]
       let indx[j] = j1+j-1
       rx[i] = (indx-array'ibe'%header'i'%crpx4f_2)*array'ibe'%header'i'%cd4f_21+array'ibe'%header'i'%crvl4f_2
       del /var indx
       if npix.eq.1 then
          ry[1,1:inchan,i,1] = gains'ifb'%psky[i,jpix,j1:j2]
          ry[1,1:inchan,i,2] = gains'ifb'%phot[i,jpix,j1:j2]
          ry[1,1:inchan,i,3] = gains'ifb'%pcold[i,jpix,j1:j2]
          if mod(scan%data%fe[ibe],10).ge.5.and.mod(scan%data%fe[ibe],10).le.8.and.rxFlag.ne."E" then
             ry[1,1:inchan,i,4] = gains'ifb'%pgrid[i,jpix,j1:j2]
          else if rxFlag.eq."E".and.nsub.gt.3 then
             ry[1,1:inchan,i,4] = gains'ifb'%pgrid[i,jpix,j1:j2]
          endif
       else
          ry[1:npix,1:inchan,i,1] = gains'ifb'%psky[i,1:npix,j1:j2]
          ry[1:npix,1:inchan,i,2] = gains'ifb'%phot[i,1:npix,j1:j2]
          ry[1:npix,1:inchan,i,3] = gains'ifb'%pcold[i,1:npix,j1:j2]
          if mod(scan%data%fe[ibe],10).ge.5.and.mod(scan%data%fe[ibe],10).le.8.and.rxFlag.ne."E" then
             ry[1:npix,1:inchan,i,4] = gains'ifb'%pgrid[i,1:npix,j1:j2]
          endif
       endif
    next
    rx = rx*1.e-9
!
else
!
   rx = data'ibe'%data%mjd-data'ibe'%data%mjd[1]
   rx = rx*24*3600
   for i 1 to npix
       for j 1 to nrecords
           if npix.eq.1 then
              compute mean mean array'ibe'%data1%data[jpix,1,j,1:nphases]
           else
              compute mean mean array'ibe'%data1%data[i,1,j,1:nphases]
           endif
           ry[i,j] = mean
       next
   next
!
endif
!
ipix = 0 
!
if nsubscan.eq.2 then
   @p_plot_trx 'ibe' 1
   clear 
endif
@ plotHeader "CAL" 'ibe'
!
if scan%data%be[ibe].lt.10.or.scan%data%be[ibe].gt.19 then
!
!!   nchan = int(channels_per_band[1])
   compute ymax max ry[1,1:nchan,1,1]
   compute ymin min ry[1,1:nchan,1,1]
   if ymax.ne.ymin then
      limits /var rx[1] ry[1,1:nchan,1,1] /blanking blankingRaw 0
   else
      limits * * -1 1 /var rx[1] ry[1,1:nchan,1,1] /blanking blankingRaw 0
   endif
   for i 1 to nbasebands
       inchan = int(channels_per_band[i])
       for j 1 to npix           
           compute ymax max ry[j,1:inchan,i,1]
           compute ymin min ry[j,1:inchan,i,1]
	   if ymax.ne.ymin then
             limits < > < > /var rx[i] ry[j,1:inchan,i,1] /blanking blankingRaw 0
           endif
           compute ymax max ry[j,1:inchan,i,2]
           compute ymin min ry[j,1:inchan,i,2]
	   if ymax.ne.ymin then
             limits < > < > /var rx[i] ry[j,1:inchan,i,2] /blanking blankingRaw 0
           endif
           if nsubscan.gt.2 then
              compute ymax max ry[j,1:inchan,i,3]
              compute ymin min ry[j,1:inchan,i,3]
	      if ymax.ne.ymin then
                limits < > < > /var rx[i] ry[j,1:inchan,i,3] /blanking blankingRaw 0
              endif
	   endif
           if mod(scan%data%fe[ibe],10).ge.5.and.mod(scan%data%fe[ibe],10).le.8.and.rxFlag.ne."E" then
              limits < > < > /var rx[i] ry[j,1:inchan,i,4] /blanking blankingRaw 0.
           else if rxFlag.eq."E".and.int(mod(scan%data%fe[ibe],1000)/100).ge.3 then
              limits < > < > /var rx[i] ry[j,1:inchan,i,4] /blanking blankingRaw 0.
           endif
       next
   next
!
else
!
   for j 1 to npix           
       if j.eq.1 then
          limits /var rx ry[j,1:nrecords] /blanking blankingRaw 0.
       else
          limits < > < > /var rx ry[j,1:nrecords] /blanking blankingRaw 0.
       endif
   next
!
endif
!
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 blankingRaw 0.
else
   uymax = user_ymax+0.18*(user_ymax-user_ymin)
   limits = = = uymax /blanking blankingRaw 0.
endif
let blc[1:2] 0. 0.
let trc[1:2] 0. 0.
!
if doHera.and.npix.gt.1 then
   g\set expand 0.6
else
   g\set expand 1.0 !0.8
endif
!
for i 1 to sqrt(npix)
    bxmin = 1.2*mira_ymin+(i-1)*dx
    bxmax = bxmin+dx
    for j 1 to sqrt(npix) 
        ipix = ipix+1
        if npix.gt.1 then
           jpix = ipix
        endif
        bymin = mira_ymin+(j-1)*dy
	bymax = bymin+dy
        g\set box bxmin bxmax bymin bymax
!
        draw relocate 0.2 -0.5 /box 7 
        if doHera then
           label 'jpix' /center 6
           draw relocate 0.5 -0.5 /box 7 
        endif
        if exist(offset%off) then
	   if (offset%off%x[jpix].ge.0) then
	      label "Sky offset: ( " /center 6
	   else
	      label "Sky offset: (" /center 6
	   endif
	   label 'offset%off%x[jpix]' /center 6
	   if (offset%off%y[jpix].ge.0) then
	      label "\r`, " /center 6
	   else
	      label "\r`," /center 6
	   endif
	   label 'offset%off%y[jpix]' /center 6
	   label "\r`)" /center 6
	endif
        if doHera then
           draw relocate 0.5 -1.0 /box 7 
           if (offset%pix%x[jpix].ge.0) then
              label "Pixel offset: ( " /center 6
           else 
              label "Pixel offset: (" /center 6
           endif
           label 'offset%pix%x[jpix]' /center 6
           if (offset%pix%y[jpix].ge.0) then
              label "\r`, " /center 6
           else 
              label "\r`," /center 6
           endif
           label 'offset%pix%y[jpix]' /center 6
           label "\r`)" /center 6
        endif
	elevation = nint(10.*mon'ibe'%data%encoder_az_el[1,2])/10.
	draw relocate 0.2 -1.5 /box 7
	label "Elevation = "'elevation'"\uo" /center 6
        tHot  = nint(100*mon'ibe'%data%thotcold[1,1])/100
	tCold = nint(100*mon'ibe'%data%thotcold[1,2])/100
	rHum = nint(10*mon'ibe'%data%tamb_p_humid[1,3])/10
	tAmb = nint(10*(mon'ibe'%data%tamb_p_humid[1,1]+273.15))/10
	pAmb = nint(10*mon'ibe'%data%tamb_p_humid[1,2])/10
	compute tRec mean gains'ifb'%trx[,jpix,]
	tRec = nint(100*tRec)/100.
	if nsubscan.ge.3 then
	   compute tSys mean gains'ifb'%tsys[,jpix,]
	   compute tCal mean gains'ifb'%tcal[,jpix,]
	   compute tauZ mean gains'ifb'%tauzen[,jpix,]
	   compute pwv  mean gains'ifb'%h2omm[,jpix,]
	   tSys = nint(100*tSys)/100.
	   tCal = nint(100*tCal)/100.
	   tauZ = nint(100*tauZ)/100.
	   pwv  = nint(10*pwv)/10.
        endif

	if .not.doHera.or.npix.eq.1 then
	   draw relocate -11 -.5 /box 9
	   label "T\dH\do\dt" /center 6
	   draw relocate -10 -.5 /box 9
	   label " = "'tHot'" K" /center 6
	   draw relocate -5.5 -.5 /box 9
	   label "T\dC\do\dl\dd"
	   draw relocate -4.5 -.5 /box 9
	   label " = "'tCold'" K" /center 6
	   draw relocate -11 -1 /box 9
	   label "T\dr\de\dc"
	   draw relocate -10 -1 /box 9
	   label " = "'tRec'" K" /center 6
	   if nsubscan.ge.3 then
	      draw relocate -5.5 -1 /box 9
	      label "T\ds\dy\ds"
	      draw relocate -4.5 -1 /box 9
	      label " = "'tSys'" K" /center 6
	      draw relocate -11 -1.5 /box 9
	      label "T\dc\da\dl"
	      draw relocate -10 -1.5 /box 9
	      label " = "'tCal'" K" /center 6
	      draw relocate -5.5 -1.5 /box 9
	      label "\gt\dZ"
	      draw relocate -4.5 -1.5 /box 9
	      label " = "'tauZ'" Np" /center 6
	      draw relocate -11 -2 /box 9
	      label "pwv"
	      draw relocate -10 -2 /box 9
	      label " = "'pwv'" mm" /center 6
	   endif
	   draw relocate -5.5 -2 /box 9
	   label "R.H."
	   draw relocate -4.5 -2 /box 9
	   label " = "'rHum'" %" /center 6
	   draw relocate -11 -2.5 /box 9
	   label "T\da\dm\db"
	   draw relocate -10 -2.5 /box 9
	   label " = "'tAmb'" K" /center 6
	   draw relocate -5.5 -2.5 /box 9
	   label "P\da\dm\db"
	   draw relocate -4.5 -2.5 /box 9
	   label " = "'pAmb'" hPa" /center 6
	endif

        if i.eq.1.and.j.eq.1 then
           box p o in 
           if scan%data%be[ibe].lt.10.or.scan%data%be[ibe].gt.19 then
              label "Frequency [GHz]" /x
           else
              label "elapsed time [s]" /x
           endif
           g\set orient 90
           draw relocate -1.8 0 /box 4
           label "Counts" /center 5 
           g\set orient 0
        else
           box n n
        endif 
	let badSkyFlag .false.
	let badColdFlag .false.
        if scan%data%be[ibe].lt.10.or.scan%data%be[ibe].gt.19 then
           @ plotBasebands 'ibe' F
           for l 1 to nbasebands
               inchan = int(channels_per_band[l])
               pen /col 0  
               histogram rx[l] ry[ipix,1:inchan,l,1] /blanking blankingRaw 0.
               pen /col 1  
               histogram rx[l] ry[ipix,1:inchan,l,2] /blanking blankingRaw 0.
               pen /col 2  
               histogram rx[l] ry[ipix,1:inchan,l,3] /blanking blankingRaw 0.
               if nsubscan.eq.4 then
                  pen /col 3
                  histogram rx[l] ry[ipix,1:inchan,l,4] /blanking blankingRaw 0.
               endif
!
! sanity checks
!
               for m 1 to 3
                   let w 1.
                   let w 0. /where ry[ipix,1:nchan,l,m].eq.blankingRaw
	           compute aux sum w
	           let caux ry[ipix,1:nchan,l,m]*w
	           compute calCounts[m] sum caux
	           let calCounts[m] calCounts[m]/aux
	       next
	       let w 0.
	       let w 1. /where -
	            ry[ipix,1:nchan,l,2]/ry[ipix,1:nchan,l,1].lt.1.05-
.and.ry[ipix,1:nchan,l,1].ne.blankingRaw-
.and.ry[ipix,1:nchan,l,2].ne.blankingRaw
               compute aux sum w
	       if aux/inchan.gt.0.1 then
	          saturationFlag = .true.
               endif
!
               if calCounts[1].ge.calCounts[2].and.scan%header%scantype.ne."calibGrid" then
	          let badSkyFlag .true.
	       endif
	       if calCounts[3].ge.calCounts[2].and.scan%header%scantype.ne."calibGrid" then
	          let badColdFlag .true.
               endif
!
            next
	    if .not.xpolMode then
	       if badSkyFlag.or.badColdFlag.or.saturationFlag then
	          pen /col 6
	          g\draw relocate .0 1.6 /box 4
	          g\label " BAD CALIBRATION !" /center 6
	          pen /def 
	       endif
	       if badSkyFlag then
	          pen /col 6
	          g\draw relocate .0 1.1 /box 4
	          g\label " (sky counts higher than hot counts)" /center 6
	          pen /def 
               endif
	       if badColdFlag then
	          pen /col 6
	          g\draw relocate .0 0.6 /box 4
	          g\label " (cold counts higher than hot counts)" /center 6
	          pen /def 
               endif
	       if saturationFlag then
	          pen /col 6
	          g\draw relocate .0 0.6 /box 4
	          g\label " (saturation problem, check with VIEW /SATURATION)" /center 6
	          pen /def 
	       endif
	    endif
            pen /def
        else
           i2 = 0
           for k 1 to nsubscan
               i1 = i2+1
               for l i1+1 to nrecords
                 if data'ibe'%data%subscan[l].ne.data'ibe'%data%subscan[i1] then
                    i2 = l-1
                    break
                 endif
               next 
               if k.eq.nsubscan then
                  i2 = nrecords
               endif
	       if (array'ibe'%data1%iswitch[i1,1].eq."SKY") then
	          ii = 0
               else if (array'ibe'%data1%iswitch[i1,1].eq."HOT") then
	          ii = 1
               else if (array'ibe'%data1%iswitch[i1,1].eq."COLD") then
	          ii = 2
               endif
               pen /col 'ii'
               histogram rx[i1:i2] ry[ipix,i1:i2] /blanking blankingRaw 0.
            next
            pen /def
        endif
    next
next
!
