
      subroutine setloops
c*********************************************************************
c
c  purpose:  to determine the loop control variables.
c            ( determine loops over the transmitter and receiver)
c
c*********************************************************************
c
      include 'mmprms2.inc'
      include 'mmloops.inc'
c
      real pi, dtr, bi2

      pi = 2. * acos(0.)
      dtr = pi/180.
c
c------------- initialize all possible controls to zero --------------
c--------- transmitter loop controls
      azts = 0.
      azte = 0.
      aztdt = 0.
      nstt = 0
c--------- receiver loop controls
      azrs = 0.
      azre = 0.
      azrdt = 0.
      nstr = 0
c
c---------------------------------------------------------------------
c.. convert all starting, ending & delta loop control variables to radians
c.. leave bistatic angle (biang) in degrees to output to file
c---------------------------------------------------------------------
c
c
      nth = 0           ! # of rcs values
c---------------------------------------------------------------------
c
      if (imode .eq. 0) then                    ! monostatic
c-- transmitter
        azts = angst * dtr              ! (radians)
        azte = angend * dtr             ! (radians)
        aztdt = angdt * dtr             ! (radians)
        nstt = int((azte-azts)/aztdt + 1.5)
c-- receiver
        nstr = 1
c
      elseif (imode .eq. 1) then             ! fixed bistatic angle
c-- transmitter
         bi2 = (biang/2.)              ! (degs)
         azts = (angst - bi2) * dtr    ! (radians)
         azte = (angend - bi2) * dtr   ! (radians)
         aztdt = angdt * dtr           ! (radians)
         bi2 = bi2 * dtr               ! (radians) 
         nstt = int((azte-azts)/aztdt + 1.5)
c-- receiver
         nstr = 1
c
      elseif (imode .eq. 2) then            ! fixed transmitter
c-- transmitter
          azts = azim * dtr           ! (radians)
          nstt = 1
c-- receiver                               ! add azim to angst and
          azrs = (azim + angst) * dtr      !azre because want bistatic
          azre = (azim + angend) * dtr     !angle to start from x-mitter
          azrdt = angdt * dtr         ! (radians)
          nstr = int((azre-azrs)/azrdt + 1.5)
c
      else                                 ! fixed equiv. line of sight
c-- transmitter
           biang = angst                             ! (degs)
           azts = (azim - angst/2.) * dtr                   ! (radians)
           azte = (azim - angend/2.) * dtr                  ! (radians)
           aztdt = sign(1.,(azte-azts)) * (angdt/2.) * dtr  ! (radians)
           nstt = int((azte-azts)/aztdt + 1.5)
c-- receiver
           nstr = 1
      endif
c
c-- determine the number of rcs values
      if (imode.eq.2) then
        nth = nstr                         ! fixed transmitter
      else
        nth = nstt
      endif
c
      return
      end


      subroutine tcomp(p,r,t)
c*********************************************************************
c
c  purpose : to compute the coordinate transform matrix due to pitch 
c            and roll.
c
c*********************************************************************
c
        real p, r
	real t(3,3)
c 1st index is column   , 2nd is row
c
	t(1,1) =   cos(r)
	t(2,1) =   sin(r) * sin(p)
	t(3,1) = - cos(p) * sin(r)
	t(1,2) =   0.
	t(2,2) =   cos(p)
	t(3,2) =   sin(p)
	t(1,3) =   sin(r)
	t(2,3) = - cos(r) * sin(p)
	t(3,3) =   cos(p) * cos(r)
c
	return
	end
c	
c====================================================================
	subroutine cvmult3x3(b,a)
c********************************************************************
c
c   purpose: to multiply a 3 by 3 real matrix and a 3 by 1 complex
c            matrix.
c
c********************************************************************
c
	real b(3,3)
	complex a(3),a1,a2,a3
c
c solves for [a] = [b] * [a]  where a is a complex vector
c
	a1 = b(1,1)*a(1) + b(2,1)*a(2) + b(3,1)*a(3)
	a2 = b(1,2)*a(1) + b(2,2)*a(2) + b(3,2)*a(3)
	a3 = b(1,3)*a(1) + b(2,3)*a(2) + b(3,3)*a(3)
c
	a(1) = a1
	a(2) = a2
	a(3) = a3	
c
	return
	end
c
c
	subroutine parmconv(itxrx,imode,az,el,npol,tpr,
     &		theta,phi,vphi,vtheta)
c*********************************************************************
c
c   purpose:  converts the location of the transmitter or receiver to
c             conventional spherical coordinated (theta,phi) and the
c             polarization (h,v,r,l) to the components of the 
c             incident h-field (htheta,hphi).
c
c*********************************************************************
        integer itxrx, npol
        real az, el, theta, phi
	complex vtheta,vphi,v(3),vr
	real k(3),tpr(3,3),ktemp(3)
        integer imode
c---------------------------------------------------------------------
c
c  itxrx  =  1  for  tx (v=h-field and az=azt)
c            2  for  rx (v=e-field and az=azr)
c  imode = 0 for monostatic, else bistatic
c  npol  =  1     v pol
c      	    2     h pol
c 	    3     r pol
c	    4     l pol
c
c set up k-vector (rlos) for either inc. and scat. field in the radar
c coordinate system w/ cartesian coord.
c
c---------------------------------------------------------------------
c
	if (itxrx . eq. 1) then		! tx
	   if( imode .eq. 0 ) then	! tx & monostatic
 	     k(1) = - cos(el) * sin(az)
	     k(2) = - cos(el) * cos(az)
	     k(3) = - sin(el)
	   else				! tx & bistatic
	     k(1) = - sin(az)
	     k(2) = - cos(az)
	     k(3) = 0.
	   endif
	else				!rx
	   if(imode .eq. 0) then	! rx & monostatic
 	     k(1) = cos(el) * sin(az)
	     k(2) = cos(el) * cos(az)
	     k(3) = sin(el)
	   else				! rx & bistatic
	     k(1) = sin(az)
	     k(2) = cos(az)
	     k(3) = 0.
	   endif
	endif
c
c set up  rec e(or inc h)-field vector in sph coord.
	vtheta = (0.,0.)
	vphi = (0.,0.)
c
  	if (itxrx .eq. 1) then				! tx
	  if( npol .eq. 1 ) vphi = (1.,0.)
	  if( npol .eq. 2 ) vtheta = (1.,0.)
	  if( npol .eq. 3 ) then
	    vtheta = sqrt(1./2.) * cmplx(0.,1.)
	    vphi = sqrt(1./2.) * cmplx(1.,0.)
	  endif
	  if( npol .eq. 4 ) then
	    vtheta = sqrt(1./2.) * cmplx(0.,-1.)
	    vphi = sqrt(1./2.) * cmplx(+1.,0.)
	  endif
	else						! rx
	  if( npol .eq. 1 ) vtheta = (1.,0.)
	  if( npol .eq. 2 ) vphi = (1.,0.)
	  if( npol .eq. 3 ) then
	    vtheta = sqrt(1./2.) * cmplx(1.,0.)
	    vphi = sqrt(1./2.) * cmplx(0.,1.)
	  endif
	  if( npol .eq. 4 ) then
	    vtheta = sqrt(1./2.) * cmplx(1.,0.)
	    vphi = sqrt(1./2.) * cmplx(0.,-1.)
	  endif
	endif
c
c convert to rectangular coord.
	if ( itxrx .eq. 1) then
	  v(1) = vtheta * sin(el) * sin(az) + vphi * cos(az)
	  v(2) = vtheta * sin(el) * cos(az) - vphi * sin(az)
	  v(3) = - vtheta * cos(el)
	else
	  v(1) = vtheta * sin(el) * sin(az) - vphi * cos(az)
	  v(2) = vtheta * sin(el) * cos(az) + vphi * sin(az)
	  v(3) = - vtheta * cos(el)
	endif
c
c transform k and h/e vectors to the target coordinate system 
c (ie include pitch and roll) by multiplying by the 3x3 
c transformation matrix.
c
	call matvec(tpr,k,ktemp)
        call vecscal(1.,ktemp,k)  !put result of tpr*k  back into k
	call cvmult3x3(tpr,v)
c
c back to spherical coord...
	theta = atan2( sqrt( k(1)**2 + k(2)**2 ) , k(3) )
	phi = atan2 ( k(2) , k(1) )
c
	vtheta = v(1) * cos(theta) * cos(phi)  +  
     &		 v(2) * cos(theta) * sin(phi)  -  v(3) * sin(theta)
	vphi = - v(1) * sin(phi)  +  v(2) * cos(phi)
c  
	return
	end

