/*
 * *************************************************
 * *                                               *
 * *               USER LIBRARY                    *
 * *   FNYQUIST - COMPUTE THE NYQUIST VELOCITY   *
 * *                                               *
 * *************************************************
 * File: libs/user/fnyquist.c
 *
 *                        COPYRIGHT (c) 1988, 1993, 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 "sigtypes.h"
#include "dsp.h"
#include "user_lib.h"
#include "dsp_lib.h"

/* ===========================================================
 * This real function computes the Nyquist velocity for the DSP data.  This
 * velocity is a simple function of the PRF, the type of multiple PRF used,
 * the wavelength, and the polarization.  It is the fold value for the
 * velocities.
 *
 */

FLT4 fNyquistVelocity
(SINT4 iprf_a,           /* PRF used (higher one in dual mode) */
 UINT2 iprfcase_a,       /* The multiple prf mode. See dsp_lib.h */
 SINT4 iwavelength_a,    /* Wavelength in 1/100 of cm */
 UINT2 iPolarization_a ) /* Polarization mode, see dsp_lib.h */
{
  FLT4 fresult;
  /* ------------------------------------------------------------------- */

  /* Convert the ambiguous velocity in meters per second */
  fresult = ( ((FLT4)iwavelength_a) * ((FLT4)iprf_a) )/40000.0;

  /* Correct velocity for multiple PRF unfolding: */
  switch(iprfcase_a)
    {
    case PRF_4_5:
      fresult = 4. * fresult;
      break;

    case PRF_3_4:
      fresult = 3. * fresult;
      break;

    case PRF_2_3:
      fresult = 2. * fresult;
      break;

    default:
      break;
    }

  if (iPolarization_a == POL_ALTERNATING) fresult = fresult/2.0;

  return fresult;
}

/* ===========================================================
 * This function computes the maximum value for the 1-byte spectrum
 * width data. 
 */
FLT4 fNyquistWidth
(SINT4 iprf_a,           /* PRF used (higher one in dual mode) */
 SINT4 iwavelength_a,    /* Wavelength in 1/100 of cm */
 UINT2 iPolarization_a ) /* Polarization mode, see dsp_lib.h */
{
  FLT4 fresult;
  /* ------------------------------------------------------------------- */

  /* Convert the ambiguous velocity in meters per second */
  fresult = ( ((FLT4)iwavelength_a) * ((FLT4)iprf_a) )/40000.0;

  if (iPolarization_a == POL_ALTERNATING) fresult = fresult/2.0;

  return fresult;
}

/* =============================================================
 * This function computes the low PRF given the high PRF and the 
 * dual PRF flag stored in IRIS products.
 */
FLT4 fPrfLowFromHighCase
( SINT4 iHighPrf_a,
  UINT2 iPrfCase_a )       /* The multiple prf mode. See dsp_lib.h */
{
  FLT4 fLowPrf;

  switch( iPrfCase_a )
    {
    default:
      fLowPrf = (FLT4)iHighPrf_a;
      break;
    case PRF_2_3:
      fLowPrf = 2.0*((FLT4)iHighPrf_a)/3.0;
      break;
    case PRF_3_4:
      fLowPrf = 3.0*((FLT4)iHighPrf_a)/4.0;
      break;
    case PRF_4_5:
      fLowPrf = 4.0*((FLT4)iHighPrf_a)/5.0;
      break;
    }

  return fLowPrf;
}

/* =============================================================
 * This function computes the IRIS dual PRF flag given the high PRF and
 * low PRFs being used.
 */
UINT2 iPrfCaseFromHighLow
( FLT4 fHighPrf_a,
  FLT4 fLowPrf_a ) 
{
  FLT4 fRatio;

  /* Give up if either number zero or negative */
  if ((fLowPrf_a<=0.0)||(fHighPrf_a<=0.0)) return PRF_FIXED;

  fRatio = fLowPrf_a/fHighPrf_a;

  if ( fRatio > 0.900 )
    return PRF_FIXED;
  else if ( fRatio > 0.775 )
    return PRF_4_5;
  else if ( fRatio > 0.708 )
    return PRF_3_4;
  else
    return PRF_2_3;

}
