!
define integer ifb irec isub i1 i2 i3 npix nrecords

define double  aux median sum
define double  chi elevation parAngle

define logical doArray doTransOn doTransOff

var raw

let iTrans = 0 /new integer

let ifb = &1
let irec = &2
let doArray = &3

if raw1%antenna'ifb'%systemOff.eq."horizontalTrue" then
   iTrans = 1
else if raw1%antenna'ifb'%systemOff.eq."Nasmyth" then
   iTrans = 2
endif

doTransOn = .false.

!
! doTransOn = .true. -> remains to be done
!

npix = febe'ifb'%header%febefeed
if exist(offset) then
   del /var offset
endif

define structure offset /global
define structure offset%on /global
define double offset%on%x[npix]  /global 
define double offset%on%y[npix]  /global 
define structure offset%pix /global
define double offset%pix%x[npix]  /global 
define double offset%pix%y[npix]  /global 

define double tmpX[npix] tmpY[npix]

if doArray then

   offset%pix%x = febe'ifb'%data%feedoffx*3.6d+3
   offset%pix%y = febe'ifb'%data%feedoffy*3.6d+3
   if febe'ifb'%header%dewrtmod.ne."EQUA" then
      if febe'ifb'%header%dewrtmod.eq."CABIN" then
         compute aux median data'ifb'%data%parangle
	 chi = aux
         compute aux median data'ifb'%data%elevatio
	 chi = chi-aux
      else if febe'ifb'%header%dewrtmod.eq."HORIZ" then
         compute aux median data'ifb'%data%parangle
	 chi = aux
      else
         chi = 0.
         say " "
         say "W-PLOT, dewar rotation mode unknown."
         say "W-PLOT, dewar rotation angle set to zero."
         say " "
      endif
      chi = chi*pi/180.
      tmpX = offset%pix%x
      tmpY = offset%pix%y
      offset%pix%x = -tmpX*cos(chi)+tmpY*sin(chi)
      offset%pix%y =  tmpX*sin(chi)+tmpY*cos(chi)
   endif

else

   offset%pix%x = 0.d0
   offset%pix%y = 0.d0

endif

if scan%header%scantype.eq."calibrate" then

   define structure offset%off /global
   define double offset%off%x[npix]  /global 
   define double offset%off%y[npix]  /global 

   offset%off%x = nint(gains'ifb'%lcalof*3.6d+4+10.*offset%pix%x)/10.
   offset%off%y = nint(gains'ifb'%bcalof*3.6d+4+10.*offset%pix%y)/10.

   if doArray then
      offset%pix%x = nint(10.*offset%pix%x)/10.
      offset%pix%y = nint(10.*offset%pix%y)/10.
   endif

   return

endif

!
! get ON position
!
if irec.eq.0 then
   offset%on%x = nint(scan%header%longoff*3.6d+4)/10.
   offset%on%y = nint(scan%header%latoff*3.6d+4)/10.
else
   offset%on%x = nint(data'ifb'%data%longoff[irec]*3.6d+4)/10.
   offset%on%y = nint(data'ifb'%data%latoff[irec]*3.6d+4)/10.
endif
!
! compute median OFF position (only useful for onOffs)
!

if switchMode.eq."totalPower" then

   define real max
   define real raux w /like array'ifb'%data1%iswitch[,1]

   raux = data'ifb'%data%integnum
   compute max max raux
   nrecords = int(max) 

   let w 0
   for i 1 to nrecords
       if array'ifb'%data1%iswitch[i,1].eq."OFF" then
          let w[i] = 1
       endif
   next
   compute sum sum w

   if sum.ne.0 then
   
      define structure offset%off /global
      define double offset%off%x[npix]  /global 
      define double offset%off%y[npix]  /global 
      define double aux1['int(sum)'] aux2['int(sum)']

      i1 = 0

      if irec.eq.0 then
         i2 = 1
	 i3 = nrecords
      else
         i2 = irec
	 i3 = irec
      endif

      for i i2 to i3
          if array'ifb'%data1%iswitch[i,1].eq."OFF" then
             i1 = i1+1
             aux1[i1] = data'ifb'%data%longoff[i]*3600.
             aux2[i1] = data'ifb'%data%latoff[i]*3600.
             isub = data'ifb'%data%subscan[i]
             if raw'isub'%antenna'ifb'%systemOff.ne."projection" then
                doTransOff = .true.
             endif
          endif
      next
      compute median median aux1
      offset%off%x[1:npix] = median
      compute median median aux2
      offset%off%y[1:npix] = median
      del /var aux1 aux2

   endif

endif

if doTransOn.or.doTransOff then

   if iTrans.eq.1 then          ! horizontal to equatorial system
      compute aux median data'ifb'%data%parangle
      chi = aux*pi/180.
      if doTransOn then
         tmpX = offset%on%x
	 tmpY = offset%on%y
	 offset%on%x = -tmpX*cos(chi)+tmpY*sin(chi)
	 offset%on%y =  tmpX*sin(chi)+tmpY*cos(chi)
      endif
      if doTransOff then
         tmpX = offset%off%x
	 tmpY = offset%off%y
	 offset%off%x = offset%on%x-tmpX*cos(chi)+tmpY*sin(chi)
	 offset%off%y = offset%on%y+tmpX*sin(chi)+tmpY*cos(chi)
      endif

   else if iTrans.eq.2 then     ! Nasmyth to equatorial system

      compute aux median data'ifb'%data%parangle
      chi = aux
      compute aux median data'ifb'%data%elevatio
      chi = chi-aux
      chi = chi*pi/180.

      if doTransOn then
         tmpX = offset%on%x
	 tmpY = offset%on%y
	 offset%on%x = -tmpX*cos(chi)+tmpY*sin(chi)
	 offset%on%y =  tmpX*sin(chi)+tmpY*cos(chi)
      endif
      if doTransOff then
         tmpX = offset%off%x
	 tmpY = offset%off%y
	 offset%off%x = -tmpX*cos(chi)+tmpY*sin(chi)
	 offset%off%y =  tmpX*sin(chi)+tmpY*cos(chi)
      endif

   endif

endif
!
offset%on%x = nint(10.*(offset%on%x+offset%pix%x))/10.
offset%on%y = nint(10.*(offset%on%y+offset%pix%y))/10.

if exist(offset%off) then
   offset%off%x = nint(10.*(offset%off%x+offset%pix%x))/10.
   offset%off%y = nint(10.*(offset%off%y+offset%pix%y))/10.
endif
!
if doArray then
   offset%pix%x = nint(10.*offset%pix%x)/10.
   offset%pix%y = nint(10.*offset%pix%y)/10.
endif
