!
clear 
g\set tick 0.1
g\set expand 0.8
!
define integer ibe npix isign
define real bxmax bxmin bymax bymin dxSave dySave uymax sum y0
define double chi0 long[4] lat[4] pos restFreq tmpTcal
define character phaseId*4 tmpDate*10 posString*10 tmpDewar*5 viewMode*10
define logical doHera
!
let viewMode &1
let ibe &2
!
let dxSave dx
let dySave dy
!
npix = febe'ibe'%header%febefeed
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 viewMode.eq."PHASE" then
   let phaseId &3 
else
   phaseId = "ON"
endif
@ identFeBe 'ibe'
!
g\set expand 0.9
bxmin = mira_ymax+0.03*dx
bxmax = mira_xmax
bymin = mira_ymin
bymax = mira_ymax
g\set box bxmin bxmax bymin bymax
!
y0 = 0.4
box n n none 
y0 = y0-0.8
draw relocate 0 y0   /box 7
label " Source:" /center 6
draw relocate 0 y0   /box 8
label 'scan%header%object' /center 6
y0 = y0-0.8
draw relocate 0 y0   /box 7
posString = \ga('nint(10.*scan%header%equinox)/10')
pos = scan%header%longobj/15.
long[1] = int(pos)
long[2] = int((pos-long[1])*60)
long[3] = int((pos-long[1])*3600-long[2]*60)
long[4] = nint(((pos-long[1])*3600-long[2]*60-long[3])*10)
label " "'posString'" :" /center 6
draw relocate 0 y0   /box 8
label 'long[1]'"\uh "'long[2]'"\um "'long[3]'"\us."'long[4]' /center 6
y0 = y0-0.8
draw relocate 0 y0   /box 7
posString = \gd('nint(10.*scan%header%equinox)/10')
isign = sign(1,scan%header%latobj)
pos = abs(scan%header%latobj)
!
lat[1] = int(pos)
lat[2] = int((pos-lat[1])*60)
lat[3] = int((pos-lat[1])*3600-lat[2]*60)
lat[4] = nint(((pos-lat[1])*3600-lat[2]*60-lat[3])*100)
label " "'posString'" : " /cent 6
draw relocate 0 y0   /box 8
if isign.eq.1 then
   label 'lat[1]'"\r^ "'lat[2]'"\r' "'lat[3]'"\r`."'lat[4]' /cent 6
else
   label "-"'lat[1]'"\r^ "'lat[2]'"\r' "'lat[3]'"\r`."'lat[4]' /cent 6
endif
!
!
y0 = y0-0.8
draw relocate 0 y0   /box 7
label " Date:" /center 6
draw relocate 0 y0   /box 8
tmpDate = 'scan%header%date_obs'
label 'tmpDate' /center 6
y0 = y0-0.8
draw relocate 0 y0   /box 7
label " Scan :" /center 6
draw relocate 0 y0   /box 8
label 'scan%header%scannum' /center 6
y0 = y0-0.8
draw relocate 0 y0   /box 7
label " Telescope:" /center 6
draw relocate 0 y0   /box 8
label 'prim%header%telescop' /center 6
y0 = y0-0.8
draw relocate 0 y0   /box 7
label " Frontend:" /center 6 
draw relocate 0 y0   /box 8
label 'feId' /center 6
y0 = y0-0.8
draw relocate 0 y0   /box 7
label " Backend:" /center 6
draw relocate 0 y0   /box 8
label 'beId' /center 6 
y0 = y0-0.8
draw relocate 0 y0   /box 7
label " Line:" /center 6
draw relocate 0 y0   /box 8
label 'specLin' /center 6
y0 = y0-0.8
draw relocate 0 y0   /box 7
label " Frequency:" /center 6
draw relocate 0 y0   /box 8
restFreq = nint(array'ibe'%header1%restfreq*1.d-6)/1000.
label 'restFreq'" GHz" /center 6
y0 = y0-0.8
draw relocate 0 y0   /box 7
label " Procedure:" /center 6
draw relocate 0 y0   /box 8
label 'scan%header%scantype' /center 6
y0 = y0-0.8
draw relocate 0 y0   /box 7
label " Switch mode:" /center 6
draw relocate 0 y0   /box 8
label 'switchMode' /center 6
!
if viewMode.eq."PHASE" then
   y0 = y0-0.8
   draw relocate 0 y0   /box 7
   label " Phase:" /center 6
   draw relocate 0 y0   /box 8
   label 'phaseId' /center 6
endif
!
if doHera then
!
   y0 = y0-0.8
   draw relocate 0 y0 /box 7
   label " Derotator :" /center 6 
   draw relocate 0 y0 /box 8
   chi0 = nint(febe'ibe'%header%dewang*10)/10.
   label 'chi0' /center 6
!
   if (febe'ibe'%header%dewrtmod.eq."CABIN") then
      label " CABIN" /center 6
   else if (febe'ibe'%header%dewrtmod.eq."HORIZ") then
      label " HORIZ" /center 6
   else if (febe'ibe'%header%dewrtmod.eq."EQUA") then
      label " EQUA" /center 6
   else if (febe'ibe'%header%dewrtmod.eq."UNDEF") then
      label " UNDEF" /center 6
   endif
!
endif
!
if viewMode.eq."CAL" then
   y0 = y0-1.2
   g\set expand 0.8
   draw relocate 0 y0 /box 7
   label " Calibration:" /center 6
   draw relocate -1 y0 /box 8
   draw line 1 y0 /box 8
   label " sky" /center 6
!
   y0 = y0-0.8
   draw relocate -1 y0 /box 8
   pen /col 1
   draw line 1 y0 /box 8
   pen /def
   label " hot load" /center 6
!
   y0 = y0-0.8
   draw relocate -1 y0 /box 8
   pen /col 2
   draw line 1 y0 /box 8
   pen /def
   label " cold load" /center 6
!
   if scan%header%scantype.eq."calibGrid" then
      y0 = y0-0.8
      draw relocate -1 y0 /box 8
      pen /col 3
      draw line 1 y0 /box 8
      pen /def
      label " cold + grid" /center 6
   endif
!
else if viewMode.eq."GAINS" then
!
   y0 = y0-1.2
   g\set expand 0.8
   draw relocate 0 y0 /box 7
   label " channel gains" /center 6
!
else if viewMode.eq."SATURATION" then
!
   y0 = y0-1.2
   g\set expand 0.8
   draw relocate 0 y0 /box 7
   label " saturation" /center 6
!
else if viewMode.eq."TREC" then
!
   y0 = y0-1.2
   g\set expand 0.8
   draw relocate 0 y0 /box 7
   label " receiver temperature" /center 6
!
else if viewMode.eq."XPOL" then
!
   y0 = y0-1.2
   g\set expand 0.8
   draw relocate 0 y0 /box 7
   label " xpol amp. & phase" /center 6
else
   y0 = y0-1.2
   g\set expand 0.8
   draw relocate 0 y0 /box 7
   label " Calibration:" /center 6
   compute sum sum reduce'ibe'%calibration_done 
   y0 = y0+0.5
   if sum.eq.0 then
      y0 = y0-0.5
      draw relocate -1 y0 /box 8
      label " Not applied." /center 6 
   endif
   if reduce'ibe'%calibration_done[1].eq.1 then
      y0 = y0-0.5 
      draw relocate -1 y0 /box 8
      label " Channel gains applied." /center 6
   endif
   if reduce'ibe'%calibration_done[2].eq.1 then
      y0 = y0-.5
      draw relocate -1 y0 /box 8
      if febe'ibe'%data%beameff[1,1].eq.febe'ibe'%data%etafss[1,1] then
         label " T\da\u* scale applied." /center 6
      else
         label " T\dm\db scale applied." /center 6
      endif
   endif
   if reduce'ibe'%calibration_done[5].eq.1 then
      y0 = y0-.5
      draw relocate -1 y0 /box 8
      label " Phases calibrated." /center 6
   endif
   if reduce'ibe'%calibration_done[3].eq.1 then
      y0 = y0-.5
      draw relocate -1 y0 /box 8
      label " Off subtracted." /center 6
   endif
   if reduce'ibe'%calibration_done[4].eq.1 then
      y0 = y0-.5
      draw relocate -1 y0 /box 8
      label " Basebands concatenated." /center 6
   endif
   y0 = y0-0.5
   draw relocate 0 y0 /box 7
   label " Despiking:" /center 6
   draw relocate -1 y0 /box 8
   if reduce'ibe'%despike_done then
      label " yes" /center 6
   else
      label " no" /center 6 
   endif
   y0 = y0-0.5
   draw relocate 0 y0 /box 7
   label " Baseline:" /center 6 
   draw relocate -1 y0 /box 8 
   if reduce'ibe'%base_done then
      label " yes" /center 6
   else
      label " no" /center 6
   endif
!
endif
!
if doHera.and.scan%header%scantype.ne."tip" then
   @ plotArray 'ibe'
   g\set box bxmin bxmax bymin bymax
endif
!
let dx dxSave
let dy dySave
