!
clear 
g\set tick 0.1
g\set plot landscape
!
define integer ibe ichan idump ipix i1 jpix
define integer icmin icmax
define integer nbasebands nchan npix npoints nrecords
define real bxmax bxmin bymax bymin dx dy uymax sum y0
define real uxmax uxmax2 uxmin uxmin2
define double aux chi chi0 cpa spa
define logical doHera doMambo doArray
!
let ibe   = &1
let idump = &2
let jpix  = &3
!
if jpix.ne.0 then
   npix = 1
else
   npix = febe'ibe'%header%febefeed
endif
!
let doHera  = scan%data%fe[ibe].eq.50.or.scan%data%fe[ibe].eq.60
let doMambo = scan%data%fe[ibe].eq.70
let doArray = doHera.or.doMambo
!
if doMambo then
   dy = (mira_ymax-mira_ymin)/12
   dx = dy*12/10
else
   dy = (mira_ymax-mira_ymin)/sqrt(npix)
   dx = dy
endif
!
@ plotHeader "SIGNAL" 'ibe'
!
if doArray.and.npix.gt.1 then
   g\set expand 0.6
else 
   g\set expand 0.8
endif
!
nbasebands = febe'ibe'%header%febeband
!
define real raux /like data'ibe'%data%integnum
raux = data'ibe'%data%integnum
!
compute aux max raux 
nrecords = int(aux)
!
@ computeOffsets 'ibe' 'idump' 'doArray'
!
nchan = data'ibe'%header%channels
!
if (scan%data%be[ibe].ge.10.and.scan%data%be[ibe].lt.20).or.scan%data%be[ibe].ge.90 then
   define real rx1[nrecords] ry[npix,nrecords]
   rx1[1:nrecords] = (data'ibe'%data%mjd[1:nrecords]-int(data'ibe'%data%mjd[1]))*24.
   if npix.eq.1 then
      ry[1,1:nrecords] = data'ibe'%data%otfdata[jpix,1,1:nrecords]
   else
      ry[1:npix,1:nrecords] = data'ibe'%data%otfdata[1:npix,1,1:nrecords]
   endif
   npoints = nrecords
else
   define real rx1[nchan] rx2[nchan] ry[npix,nchan]
   define real i0
   define double fstep f0 vstep v0 
!
   vstep = data'ibe'%header%cd4r_21
   v0 = data'ibe'%header%crvl4r_2
!
   fstep = data'ibe'%header%cd4f_21*1.e-9
   f0 = data'ibe'%header%crvl4f_2*1.e-9
!
   i0 = data'ibe'%header%crpx4r_2
   let rx1[i] (i-i0)*fstep+f0
   let rx2[i] (i-i0)*vstep+v0
!
   if npix.eq.1 then
      if scan%header%scantype.eq."onTheFlyMap" then 
         ry[1,1:nchan] = data'ibe'%data%otfdata[jpix,1:nchan,idump]
      else
         ry[1,1:nchan] = data'ibe'%data%rdata[jpix,1:nchan]
      endif
   else
      if scan%header%scantype.eq."onTheFlyMap" then 
         ry[1:npix,1:nchan] = data'ibe'%data%otfdata[1:npix,1:nchan,idump]
      else
         ry[1:npix,1:nchan] = data'ibe'%data%rdata[1:npix,1:nchan]
      endif
   endif
   npoints = nchan
!
endif
!
ipix = 0
!
!if reduce'ibe'%calibration_done[3].eq.0.and.reduce'ibe'%calibration_done[4] then
!   say "E-PLOT: Only for data with off subtracted and spectral basebands "
!   say "        concatenated. Please execute:"
!   say "        MIRA\CAL [/GAINS [/TCAL]] /OFF /CONCATENATE"
!   return
!endif
!
for i 1 to npix
    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 .not.doMambo
!
       if doHera then
          if i.eq.1 then
             limits /var rx2 ry[i,1:npoints] /blanking blankingRed 0.
          else
             limits < > < > /var rx2 ry[i,1:npoints] /blanking blankingRed 0. 
          endif
       else
          if i.eq.1 then
             limits /var rx1 ry[i,1:npoints] /blanking blankingRed 0.
          else
             limits < > < > /var rx1 ry[i,1:npoints] /blanking blankingRed 0. 
          endif
       endif
!
       uymax = user_ymax+0.15*(user_ymax-user_ymin)
       limits = = = uymax /blanking blankingRed 0.
!
    else
!
       if i.eq.1 then
          limits /var rx1 ry[i,1:npoints] /blanking blankingRed 0.
       else
          limits < > < > /var rx1 ry[i,1:npoints] /blanking blankingRed 0. 
       endif
!
    endif
next
!
!
let blc[1:2] 0. 0.
let trc[1:2] 0. 0.
!
if doMambo then
   let npix1 10 /new integer
   let npix2 12 /new integer
else
   let npix1 sqrt(npix) /new integer
   let npix2 sqrt(npix) /new integer
endif
for i 1 to npix1
    bxmin = 1.2*mira_ymin+(i-1)*dx
    bxmax = bxmin+dx 
    for j 1 to npix2 
        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 doArray then
           label 'jpix' /center 6
           draw relocate 0.5 -0.5 /box 7
        endif
	if exist(offset%on.and..not.doMambo) then
           if (offset%on%x[jpix].ge.0) then
              label "ON ( " /center 6
           else 
              label "ON (" /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 exist(offset%off.and..not.doMambo) then
           if doArray then
              draw relocate 0.5 -1.0 /box 7
              if (offset%off%x[jpix].ge.0) then
                 label "OFF ( " /center 6
              else 
                 label "OFF (" /center 6
              endif 
           else
              if (offset%off%x[jpix].ge.0) then
                 label ", OFF ( " /center 6
              else 
                 label ", OFF (" /center 6
              endif 
           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
	   if exist(offset%off) then
              draw relocate 0.5 -1.5 /box 7
	   else
              draw relocate 0.5 -1.0 /box 7
	   endif
           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 npix1.eq.1.and.npix2.eq.1 then
           if (scan%data%be[ibe].ge.10.and.scan%data%be[ibe].lt.20).or.scan%data%be[ibe].ge.90 then
	      box p o in 
              label "UTC [hours]" /x
           else
              axis yl /label o     
	      axis yr /label n
	      axis xl /label p
	      let uxmin user_xmin
	      let uxmax user_xmax
	      let icmin (uxmin-f0)/fstep+i0
	      let icmax (uxmax-f0)/fstep+i0
	      let uxmin2 (icmin-i0)*vstep+v0
	      let uxmax2 (icmax-i0)*vstep+v0
	      limits uxmin2 uxmax2 user_ymin user_ymax
	      axis xu /label p
	      limits uxmin uxmax user_ymin user_ymax
              draw relocate 0 -1 /box 2
              label "sky frequency [GHz]" /center 5
	      dra relocate 0 1.2 /box 8
	      label "V\dl\ds\dr [km s\u-\u1]" /center 5
           endif
           g\set orient 90
           g\draw relocate -1.5 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
           if i.eq.1.and.j.eq.1 then
              box p o in 
              if (scan%data%be[ibe].ge.10.and.scan%data%be[ibe].lt.20).or.scan%data%be[ibe].ge.90 then
                 label "UTC [hours]" /x
              else
                 draw relocate 0 -1 /box 2
		 if doHera then
                    label "V\dl\ds\dr [km/s]" /center 5
		 else
                    label "sky frequency [GHz]" /center 5
                 endif
              endif
              g\set orient 90
              g\draw relocate -1.5 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 
	endif
	if doHera then
           @ plotBasebands 'ibe' V
           histogram rx2 ry[ipix,1:npoints] /blanking blankingRed 0.
        else if doMambo then
           histogram rx ry[ipix,1:npoints] /blanking blankingRed 0.
	else
           @ plotBasebands 'ibe' F
           histogram rx1 ry[ipix,1:npoints] /blanking blankingRed 0.
	endif
    next
next
!
