!
on error return
!
clear 
g\set tick 0.1
g\set plot landscape
!
define integer ibe ichan idump iphase ipix i1 i2 jphase jpix j0 j1 j2 
define integer nbasebands nchan nphases npix nrecords
define real bxmax bxmin bymax bymin dx dy uymax y0
define double aux bval chi chi0 cpa spa sum weight
define logical doHera onOffMode
!
if .not.exist(phaseId) then
   define character phaseId*4 /global
endif
!
let ibe = &1
let phaseId = &2
let idump = &3
let jpix = &4
!
let jphase 0
if phaseId.eq."1" then
   let jphase 1
else if phaseId.eq."2" then
   let jphase 2
else if phaseId.eq."3" then
   let jphase 3
else if phaseId.eq."4" then
   let jphase 4
endif
if jphase.gt.febe'ibe'%header%nphases then
   say " "
   say "E-PLOT: phase index to plot exceeds number of phases."
   say " "
   return
endif
!
!if reduce'ibe'%calibration_done[4].eq.0 then
!   say "E-PLOT: please concatenate basebands of the same backend part"
!   say "        (cal /concatenate)."
!   return
!endif
!
if jpix.ne.0 then
   npix = 1
else
   npix = febe'ibe'%header%febefeed
endif
!
!
define real raux /like data'ibe'%data%integnum
raux = data'ibe'%data%integnum
!
compute aux max raux 
nrecords = int(aux)
nphases = febe'ibe'%header%nphases
!
doHera = scan%data%fe[ibe].eq.50.or.scan%data%fe[ibe].eq.60
onOffMode = -
scan%header%scantype.eq."onOff".or.scan%header%scantype.eq."onoff".or.scan%header%scantype.eq."ONOFF"
!
!
if reduce'ibe'%calibration_done[1].ne.1-
.and.reduce'ibe'%calibration_done[3].ne.1 then
  bval = blankingRaw
else
  bval = blankingRed
endif
!
define double tmp[nrecords,nphases] w[nrecords,nphases] ww[nrecords,nphases]
!
iphase = 0
sum = 0.0
if idump.ne.0 then
   if jphase.eq.0 then
      for i 1 to nphases
          if array'ibe'%data1%iswitch[idump,i].eq.phaseId then
             iphase = i
             break
          endif
      next
   else
      iphase = jphase
   endif
else
   if jphase.eq.0 then
      for i 1 to nrecords
          for j 1 to nphases
              if array'ibe'%data1%iswitch[i,j].eq.phaseId then
                 iphase = j
                 w[i,j] = 1.
              else
                 w[i,j] = 0.
              endif
          next
      next
   else
      for i 1 to nrecords
          w[i,1:nphases] = 0
	  w[i,jphase] = 1
	  iphase = jphase
      next
   endif
   compute weight sum w
endif
!
if iphase.eq.0.and.weight.eq.0 then
   say "E-PLOT: no such phase in data."
   return
endif
!
dy = (mira_ymax-mira_ymin)/sqrt(npix)
dx = dy
!
@ plotHeader "PHASE" 'ibe' 'phaseId'
@ computeOffsets 'ibe' 'idump' 'doHera'
!
if doHera.and.npix.gt.1 then
   g\set expand .6
else
   g\set expand .8
endif
!
nbasebands = febe'ibe'%header%febeband
!
define real channels_per_band[nbasebands]
!
for i 1 to nbasebands 
    channels_per_band[i] = array'ibe'%header'i'%usedchan
next
!
compute aux max channels_per_band
nchan = int(aux)
!
if exist(rx) then
   delete /var rx ry
endif
if scan%data%be[ibe].lt.10.or.scan%data%be[ibe].gt.19 then
   define double rx[nchan,nbasebands]      /global
   define double ry[npix,nchan,nbasebands] /global
else
   define double rx[nrecords] ry[npix,nrecords]
endif
!
if scan%data%be[ibe].lt.10.or.scan%data%be[ibe].gt.19 then
!
   i2 = 0
   for i 1 to nbasebands 
       nchan = int(channels_per_band[i])
       j0 = 0  
       j1 = 1+array'ibe'%header'i'%dropchan
       j2 = j1+array'ibe'%header'i'%usedchan-1
       define integer indx[nchan]
       let indx[j] j+j1-1
       let rx[i] = (indx-array'ibe'%header'i'%crpx4f_2)*array'ibe'%header'i'%cd4f_21+array'ibe'%header'i'%crvl4f_2
       del /var indx
       for j j1 to j2 
           j0 = j0+1
           rx[j0,i] = (j-array'ibe'%header'i'%crpx4f_2)*array'ibe'%header'i'%cd4f_21+array'ibe'%header'i'%crvl4f_2
           for k 1 to npix
	      if idump.ne.0 then
	        if npix.eq.1 then
                   ry[1,j0,i] = array'ibe'%data'i'%data[jpix,j,idump,iphase]
		else 
                   ry[k,j0,i] = array'ibe'%data'i'%data[k,j,idump,iphase]
		endif
	      else
                let tmp = 0.
		if npix.eq.1 then
                   let tmp = array'ibe'%data'i'%data[jpix,j,1:nrecords,1:nphases] -
     /where w.eq.1.and.array'ibe'%data'i'%data[jpix,j,1:nrecords,1:nphases].ne.bval
		else
                   let tmp = array'ibe'%data'i'%data[k,j,1:nrecords,1:nphases] -
     /where w.eq.1.and.array'ibe'%data'i'%data[k,j,1:nrecords,1:nphases].ne.bval
		endif
                compute sum sum tmp
                let ww = w
		if npix.eq.1 then
                   let ww = 0. -
                /where array'ibe'%data'i'%data[jpix,j,1:nrecords,1:nphases].eq.bval
		else
                   let ww = 0. -
                /where array'ibe'%data'i'%data[k,j,1:nrecords,1:nphases].eq.bval
		endif
                compute weight sum ww
                sum = sum/weight
                ry[k,j0,i] = sum
	      endif
           next
       next
   next
   rx = rx*1.e-9
!
else
!
   rx = (data'ibe'%data%mjd-int(data'ibe'%data%mjd[1]))*24.
      if iphase.eq.1.and.jphase.eq.0 then 
         if npix.eq.1 then
            ry = array'ibe'%data1%data[jpix,1,1:nrecords,iphase]
	    if nphases.gt.1 then
	       ry = ry-array'ibe'%data1%data[jpix,1,1:nrecords,'iphase+1']
	    endif
	 else
            ry = array'ibe'%data1%data[1:npix,1,1:nrecords,iphase]
	    if nphases.gt.1 then
	       ry = ry-array'ibe'%data1%data[1:npix,1,1:nrecords,'iphase+1']
	    endif
	 endif
      else if jphase.eq.0 then
         if npix.eq.1 then
            ry = array'ibe'%data1%data[jpix,1,1:nrecords,iphase]
	    if nphases.gt.1 then
	       ry = ry-array'ibe'%data1%data[jpix,1,1:nrecords,'iphase-1']
            endif
	 else 
            ry = array'ibe'%data1%data[1:npix,1,1:nrecords,iphase]
	    if nphases.gt.1 then
	       ry = ry-array'ibe'%data1%data[1:npix,1,1:nrecords,'iphase-1']
            endif
	 endif
      else if jphase.ne.0 then
            ry = array'ibe'%data1%data[1:npix,1,1:nrecords,jphase]
      endif

   !else
   !   if npix.eq.1 then
   !      ry = array'ibe'%data1%data[jpix,1,1:nrecords,iphase]
   !   else
   !      ry = array'ibe'%data1%data[1:npix,1,1:nrecords,iphase]
   !   endif
   !endif

!
endif
!
ipix = 0
!
!
if scan%data%be[ibe].lt.10.or.scan%data%be[ibe].gt.19 then
!
   nchan = int(channels_per_band[1])
   for i 1 to nbasebands
       nchan = int(channels_per_band[i])
       for j 1 to npix
           if i.eq.1.and.j.eq.1 then
              limits /var rx[i] ry[j,1:nchan,i] /blanking bval 0.
           else
              limits < > < > /var rx[i] ry[j,1:nchan,i] /blanking bval 0.
           endif
       next
   next
!
else
!
   for j 1 to npix
       if j.eq.1 then
          limits /var rx ry[j,1:nrecords] /blanking bval 0.
       else
          limits < > < > /var rx ry[j,1:nrecords] /blanking bval 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 bval 0.
else
   uymax = user_ymax+0.15*(user_ymax-user_ymin)
   limits = = = uymax /blanking bval 0.
endif
let blc[1:2] 0. 0.
let trc[1:2] 0. 0.
!
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%on) then
           if (offset%on%x[jpix].ge.0) then
              label "Offset  ( " /center 6
           else 
              label "Offset  (" /center 6
           endif
           label 'offset%on%x[jpix]' /center 6
           if (offset%on%y[jpix].ge.0) then
              label "\r`, " /center 6
           else 
              label "\r`," /center 6
           endif
           label 'offset%on%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
!	   
        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 "Sky Frequency [GHz]" /x
           else
              label "UTC [h]" /x
           endif
           g\set orient 90
           draw relocate -1.8 0 /box 4
	   if reduce'ibe'%calibration_done[2].eq.0 then
              label "backend counts" /center 5 
          else if febe'ibe'%data%beameff[1,1].ne.febe'ibe'%data%etafss[1,1] then
	      label "T\dm\db [K]" /center 5
	   else
	      label "T\dA\u* [K]" /center 5
	   endif
           g\set orient 0
        else
           box n n
        endif 
!
        if scan%data%be[ibe].lt.10.or.scan%data%be[ibe].gt.19 then
!
           @ plotBasebands 'ibe' F
           for l 1 to nbasebands
               nchan = int(channels_per_band[l])
               histogram rx[l] ry[ipix,1:nchan,l] /blanking bval 0.
           next
!
        else 
!
           histogram rx ry[ipix,1:nrecords] /blanking bval 0.
!
           if reduce'ibe'%calibration_done[3].eq.1.and.onOffMode then
              if .not.exist(nsubscan) then
                 define integer ip nsubscan nrecus
                 define double flux sigmaFlux fluxav sumSqu fluxi
                 define character fluxString*6 sigmaString*6
              endif 
              let nsubscan data'ibe'%data%subscan[nrecords]
	      fluxav = 0.0
              for k 1 to nsubscan 
                  for l 1 to nrecords
                      i1 = l 
                      if data'ibe'%data%subscan[l].eq.k then
                         break
                      endif
                  next
                  for l nrecords to 1 by -1
                      i2 = l
                      if data'ibe'%data%subscan[l].eq.k then
                         break
                      endif
                  next
		  compute flux mean ry[ipix,i1:i2]
! Normal non-symmetric
!		  if mod(k,2).eq.0 then
!		     fluxav = fluxav+flux
!		  else
!		     fluxav = fluxav-flux	 
!              	  endif  			 
! Symmetyric case first subscan +
		  if (mod(k,4).eq.1) then
		     fluxav = fluxav+flux
		  endif
		  if (mod(k,4).eq.2) then
	             fluxav = fluxav-flux
		  endif	 
		  if (mod(k,4).eq.3) then 	        
		     fluxav = fluxav-flux
		  endif
		  if (mod(k,4).eq.0) then	 
		     fluxav = fluxav+flux
                  endif	 
		  say 'mod(k,4)' of 'k' sum 'fluxav' 			 
                  if array'ibe'%data1%iswitch[i1,iphase].eq."ON" then
                     pen /col 2
                  else if array'ibe'%data1%iswitch[i1,iphase].eq."SKY" then
                     pen /col 2
                  else
                     pen /col 1
                  endif
                  draw relocate rx[i1] flux /user
                  draw line rx[i2] flux /user
                  pen /def
              next
	      let flux = fluxav/nsubscan
              sumSqu = 0.0
	      fluxi = 0.0
	      nrecus = 0
	      for k 1 to nsubscan 
              	  for l 1 to nrecords
                      i1 = l 
                      if data'ibe'%data%subscan[l].eq.k then
                         break
                      endif
                  next
                  for l nrecords to 1 by -1
                      i2 = l
                      if data'ibe'%data%subscan[l].eq.k then
                         break
                      endif
                  next
		  compute fluxi mean ry[ipix,i1:i2]

		  if mod(k,2).eq.0 then
		     sumSqu = sumSqu+(fluxi-flux)^2 
		  else
		     sumSqu = sumSqu+(fluxi+flux)^2  
              	  endif  			 

!!		  let sumSqu  sumSqu+(fluxi-flux)^2
	          let nrecus  nrecus+(i2-i1+1) 
!		  examine fluxi sumSqu nrecus
!		  say 'i2-i1+1' 'nrecords'
	      next
! 
              !compute flux mean data'ibe'%data%otfdata[jpix,1,]
              !raux = (data'ibe'%data%otfdata[jpix,1,]-flux)^2
	      !examine raux data'ibe'%data%otfdata[jpix,1,]
              !compute sum sum raux
              let sigmaFlux sqrt(sumSqu/(nrecus-1))/sqrt(nrecus)
	      examine flux sigmaFlux
              let ip 1-nint(log10(sigmaFlux))
!	      examine ip
              !let ip -1+nint(log10(sigmaFlux))
              let sigmaFlux nint(sigmaFlux*10^ip)/10^ip
              let flux nint(flux*10^ip)/10^ip
              draw relocate 0.2 -1.5 /box 7 
              if doHera then
                g\set expand .8
              else
                g\set expand 1.0
              endif 
              pen /col 3
              if ip.eq.0 then
                 let fluxString = 'flux' /format f6.0
                 let sigmaString = 'sigmaFlux' /format f6.0
              else if ip.eq.1 then
                 let fluxString = 'flux' /format f6.1
                 let sigmaString = 'sigmaFlux' /format f6.1
              else if ip.eq.2 then
                 let fluxString = 'flux' /format f6.2
                 let sigmaString = 'sigmaFlux' /format f6.2
              else if ip.eq.3 then
                 let fluxString = 'flux' /format f6.3
                 let sigmaString = 'sigmaFlux' /format f6.3
              else if ip.ge.4 then
                 let fluxString = 'flux' /format f6.3
                 let sigmaString = 'sigmaFlux' /format f6.4
              endif
	      say 'fluxString' 'sigmaString'
              label "Flux = "'fluxString' /center 6 
              label "\s+"'sigmaString' /center 6 
	      if reduce'ibe'%calibration_done[2].eq.0 then
                 label " cnts" /center 6 
              else
                 label " K" /center 6
              endif
              pen /def
              if doHera then
                 g\set expand .6
              else
                 g\set expand .8
              endif
           endif
!
        endif 
!
    next
next
!
