/* **********************************************************************
 * *                                                                    *
 * *                 Routines to Help With Data Types                   *
 * *                                                                    *
 * **********************************************************************
 * File: libs/user/data_types.c
 *
 *       COPYRIGHT (c) 1995, 1997, 1998, 1999, 2001, 2004, 2006 BY
 *              VAISALA INC., 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 <stdio.h>

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

/* ======================================================================
 * Routine to convert a data type into the index in the scales setup
 * structures.  Invalid types all return zero.
 */
#define DBZ_INDEX      0
#define RAINRATE_INDEX 1
#define VELOCITY_INDEX 2
#define WIDTH_INDEX    3
#define VIL_INDEX      4
#define HEIGHT_INDEX   5
#define FLIQUID_INDEX  6
#define SHEAR_INDEX    7
#define ZDR_INDEX      8
#define KDP_INDEX      9
#define USER_INDEX    10
#define PHIDP_INDEX   11
#define RHOHV_INDEX   12
#define TIME_INDEX    13
#define LDR_INDEX     14
#define SQI_INDEX     15
#define HCLASS_INDEX  16
#define DIVERGE_INDEX (SETUP_DTYPE_COUNT + 0)
#define VVEL_INDEX    (SETUP_DTYPE_COUNT + 1)
#define DEFORM_INDEX  (SETUP_DTYPE_COUNT + 2)
UINT4 index_from_datatype
 (UINT4 data_type)
{
  switch(data_type)
    {
    case DB_DBT :
    case DB_DBZ :
    case DB_DBT2 :
    case DB_DBZ2 :
    case DB_DBZC :
    case DB_DBZC2:
      return DBZ_INDEX;
      break;
    case DB_DEFORM2:
      return DEFORM_INDEX;
      break;
    case DB_DIVERGE2:
      return DIVERGE_INDEX;
      break;
    case DB_FLIQUID2 :
      return FLIQUID_INDEX;
      break;
    case DB_HCLASS :
    case DB_HCLASS2:
      return HCLASS_INDEX;
      break;
    case DB_HEIGHT :
      return HEIGHT_INDEX;
      break;
    case DB_KDP :
    case DB_KDP2 :
      return KDP_INDEX;
      break;
    case DB_LDRH:
    case DB_LDRV:
    case DB_LDRH2:
    case DB_LDRV2:
      return LDR_INDEX;
      break;
    case DB_PHIH :
    case DB_PHIH2 :
    case DB_PHIV :
    case DB_PHIV2 :
    case DB_PHIDP :
    case DB_PHIDP2 :
      return PHIDP_INDEX;
      break;
    case DB_RAINRATE2 :
      return RAINRATE_INDEX;
      break;
    case DB_RHOH :
    case DB_RHOH2 :
    case DB_RHOV :
    case DB_RHOV2 :
    case DB_RHOHV :
    case DB_RHOHV2 :
      return RHOHV_INDEX;
      break;
    case DB_SHEAR :
      return SHEAR_INDEX;
      break;
    case DB_SQI :
    case DB_SQI2 :
      return SQI_INDEX;
      break;
    case DB_TIME2 :
      return TIME_INDEX;
      break;
    case DB_USER :
    case DB_USER2 :
      return USER_INDEX;
      break;
    case DB_VEL:
    case DB_VELC:
    case DB_VEL2:
    case DB_VELC2:
      return VELOCITY_INDEX;
      break;
    case DB_VIL2:
      return VIL_INDEX;
      break;
    case DB_VVEL2:
      return VVEL_INDEX;
      break;
    case DB_WIDTH :
    case DB_WIDTH2 :
      return WIDTH_INDEX;
      break;
    case DB_ZDR :
    case DB_ZDR2 :
    case DB_ZDRC :
    case DB_ZDRC2 :
      return ZDR_INDEX;
      break;
    }
  
  return 0;
}
/* ======================================================================
 * Routine to convert an index in the scales setup structures to the
 * nominal datatype for that slot.  Invalid types all return zero.
 */
UINT4 idatatype_from_index
 (UINT4 index_a)
 {
  switch(index_a)
    {
    case DBZ_INDEX:
      return DB_DBZ2;
      break;
    case DEFORM_INDEX:
      return DB_DEFORM2;
      break;
    case DIVERGE_INDEX:
      return DB_DIVERGE2;
      break;
    case FLIQUID_INDEX:
      return DB_FLIQUID2;
      break;
    case HCLASS_INDEX:
      return DB_HCLASS2;
      break;
    case HEIGHT_INDEX:
      return DB_HEIGHT;
      break;
    case KDP_INDEX:
      return DB_KDP2;
      break;
    case LDR_INDEX:
      return DB_LDRH2;
      break;
    case PHIDP_INDEX:
      return DB_PHIDP2;
      break;
    case RAINRATE_INDEX:
      return DB_RAINRATE2;
      break;
    case RHOHV_INDEX:
      return DB_RHOHV2;
      break;
    case SHEAR_INDEX:
      return DB_SHEAR;
      break;
    case SQI_INDEX:
      return DB_SQI;
      break;
    case TIME_INDEX:
      return DB_TIME2;
      break;
    case USER_INDEX:
      return DB_USER2;
      break;
    case VELOCITY_INDEX:
      return DB_VEL2;
      break;
    case VIL_INDEX:
      return DB_VIL2;
      break;
    case VVEL_INDEX:
      return DB_VVEL2;
      break;
    case WIDTH_INDEX:
      return DB_WIDTH2;
      break;
    case ZDR_INDEX:
      return DB_ZDR2;
      break;
    }
  
  return 0;
}
/* ======================================================================
 * Routine to convert a data type into a flag indicating which type of
 * data it is.  Invalid types all return zero.
 */
UINT4 iflag_from_datatype
 (UINT4 data_type)
{
  switch(data_type)
    {
    case DB_DBT :
    case DB_DBZ :
    case DB_DBZC :
    case DB_DBT2 :
    case DB_DBZ2 :
    case DB_DBZC2 :
    case DB_FLIQUID2 :
    case DB_HCLASS :
    case DB_HCLASS2 :
    case DB_HEIGHT :
    case DB_KDP :
    case DB_KDP2 :
    case DB_LDRH :
    case DB_LDRV :
    case DB_LDRH2 :
    case DB_LDRV2 :
    case DB_PHIH :
    case DB_PHIH2 :
    case DB_PHIV :
    case DB_PHIV2 :
    case DB_PHIDP :
    case DB_PHIDP2 :
    case DB_RAINRATE2 :
    case DB_RHOH :
    case DB_RHOH2 :
    case DB_RHOV :
    case DB_RHOV2 :
    case DB_RHOHV :
    case DB_RHOHV2 :
    case DB_SHEAR :
    case DB_SQI :
    case DB_SQI2 :
    case DB_TIME2 :
    case DB_USER :
    case DB_USER2 :
    case DB_VEL :
    case DB_VELC :
    case DB_VEL2 :
    case DB_VELC2:
    case DB_VIL2 :
    case DB_WIDTH :
    case DB_WIDTH2 :
    case DB_ZDR :
    case DB_ZDR2 :
    case DB_ZDRC :
    case DB_ZDRC2 :
      return DATATYPE_IN_SETUP | DATATYPE_IN_OPTIONS ;
      break;

    case DB_DEFORM2:
    case DB_DIVERGE2:
    case DB_VVEL2:
      return DATATYPE_IN_OPTIONS;
      break;
    }
  
  return 0;
}

/* =================================================================
 * Given an index, return 1-byte data type from the list of data types
 * possible in a task.  Order does not need to match the DSP output order.
 */
SINT2 iDataFromTaskIndex
( UINT1 iIndex_a )
{
  SINT2 iDataList[ TASK_NUM_DEFINED_DATA ] = 
  { DB_DBZ,   DB_DBZC,  DB_DBT,   DB_VEL,  DB_VELC,  DB_WIDTH, DB_ZDR,   DB_ZDRC,
    DB_KDP,   DB_PHIDP, DB_RHOHV, DB_SQI,  DB_LDRH,  DB_RHOH,  DB_PHIH,  DB_LDRV, 
    DB_RHOV,  DB_PHIV,  DB_XHDR,  DB_USER, DB_HCLASS } ;
  if( iIndex_a >= TASK_NUM_DEFINED_DATA ) return -1;
 
  return iDataList[iIndex_a];
}

/* =================================================================
 * Given an index, return 1-byte data type from the list of dsp output order.
 * Return -1 if invalid.
 */
SINT2 iDataFromDspIndex
( UINT1 iIndex_a )
{
  SINT2 iDataList[ DSP_NUM_DEFINED_DATA ] = 
  { DB_DBZ, DB_DBT,  DB_VEL,  DB_WIDTH, DB_ZDR,  DB_KDP,  DB_PHIDP, DB_RHOHV,
    DB_SQI, DB_LDRH, DB_RHOH, DB_PHIH,  DB_LDRV, DB_RHOV, DB_PHIV , DB_FLAGS,
    DB_HCLASS } ;
  if( iIndex_a >= DSP_NUM_DEFINED_DATA ) return -1;
 
  return iDataList[iIndex_a];
}


/* =================================================================
 * Given an index, return 2-byte data type from the list of dsp output order.
 * Return -1 if invalid.
 */
SINT2 iData2FromDspIndex
( UINT1 iIndex_a )
{
  SINT2 iDataList[ DSP_NUM_DEFINED_DATA ] = 
    { DB_DBZ2, DB_DBT2,  DB_VEL2,  DB_WIDTH2, DB_ZDR2,  DB_KDP2,  DB_PHIDP2, DB_RHOHV2,
      DB_SQI2, DB_LDRH2, DB_RHOH2, DB_PHIH2,  DB_LDRV2, DB_RHOV2, DB_PHIV2 , DB_FLAGS2,
      DB_HCLASS2 } ;
  if( iIndex_a >= DSP_NUM_DEFINED_DATA ) return -1;
 
  return iDataList[iIndex_a];
}

