/* **********************************
 * *                                *  
 * *           DSP Driver           *
 * *                                *  
 * **********************************  
 * File: libs/dsp/fradar_constant.c
 *
 *                  COPYRIGHT (c) 1988, 1989, 1999  BY 
 *          SIGMET INCORPORATED, WESTFORD MASSACHUSETTS, U.S.A.  
 * 
 * THIS SOFTWARE IS FURNISHED UNDER A LICENSE AND MAY BE USED AND  COPIED
 * ONLY  IN  ACCORDANCE WITH  THE  TERMS  OF  SUCH  LICENSE  AND WITH THE
 * INCLUSION OF THE ABOVE COPYRIGHT NOTICE.  THIS SOFTWARE  OR  ANY OTHER
 * COPIES  THEREOF MAY NOT BE PROVIDED OR OTHERWISE MADE AVAILABLE TO ANY
 * OTHER PERSON.  NO TITLE TO AND OWNERSHIP OF  THE  SOFTWARE  IS  HEREBY
 * TRANSFERED. 
 */
#include <math.h>

#include "sigtypes.h"
#include "setup.h"
#include "dsp_lib.h"
#include "dsp_def.h"

/* =================================================================== 
 * This subroutine returns the radar constant calculated from the
 * setup information
 * Part of info library, but not callable from a process which did not
 * do the DspOpen().
 */
FLT4 fDspRadarConstant
(Dsp *pDsp_a,
 const SINT4 iwidth_a,            /* Pulse width index, origin 0 */
 UINT1 lHorizontalPolarization_a  /* Which constant to return */
 )    
{
  FLT4 rwlength, rvertbeam, rhorzbeam, rpower, rpwidth;
  FLT4 rantgain, ff, rloss, fresult;

  /* --------------------------------------------------------------------- 
   * Pick out numbers from setup structure
   */
  rwlength = ((FLT4)dspdrv_setup_c.iwavelength)/100.;
      
  rpwidth = ((FLT4)dspdrv_setup_c.ipwidths[iwidth_a])/100.;
      
  rantgain = ((FLT4)dspdrv_setup_c.iantgain)/10.;
  rantgain = pow( 10.0, rantgain/10. );
      
  rhorzbeam = ((FLT4)dspdrv_setup_c.ihorizwidth)/100.;
      
  rvertbeam = ((FLT4)dspdrv_setup_c.ivertwidth)/100.;
      
  rpower = ((FLT4)dspdrv_setup_c.itpower[iwidth_a])/1000.;

  if (dspdrv_setup_c.lPolarAvailable)
    {
      if (lHorizontalPolarization_a)
	{
	  if (dspdrv_setup_c.lXmtHorizontalOnly)
	    rloss = ((FLT4)dspdrv_setup_c.Horiz.iXmtLossSingle)/10.;
	  else
	    rloss = ((FLT4)dspdrv_setup_c.Horiz.iXmtLossHAndV)/10.;
	}
      else
	{
	  if (dspdrv_setup_c.lXmtVerticalOnly)
	    rloss = ((FLT4)dspdrv_setup_c.Vert.iXmtLossSingle)/10.;
	  else
	    rloss = ((FLT4)dspdrv_setup_c.Vert.iXmtLossHAndV)/10.;
	}
    }
  else
    rloss = ((FLT4)dspdrv_setup_c.Horiz.iXmtLossSingle)/10.;

  rloss = pow( 10.0, rloss/10. );
      
  /* Calculate it
   */
  ff = 2.69e16 * SQUARE(rwlength) * rloss;
  ff = ff/(rpower * rpwidth * rhorzbeam * rvertbeam);
  fresult = ff/SQUARE(rantgain);
      
  return fresult;
}
