/* **********************************************************************
 * *                                                                    *
 * *  NORDRAD API structure and constant definitions                    *
 * *                                                                    *
 * **********************************************************************
 * File: include/nrd_api.h
 *
 *                        COPYRIGHT (c) 2000  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.
 */
#ifndef SIGMET_NRD_API_H
#define SIGMET_NRD_API_H 1

#ifdef __cplusplus
extern "C" {
#endif

#define Tag_Terminate        (0)
#define Tag_Name             (1)
#define Tag_Area             (2)
#define Tag_AreaProperties   (3)
#define Tag_Time             (4)
#define Tag_Quantity         (5)
#define Tag_IntStore         (6)
#define Tag_Rrtype           (7)
#define Tag_Height           (8)
#define Tag_Pixel            (9)
#define Tag_Levels           (10)
#define Tag_AreaWall         (11)
#define Tag_Line             (12)
#define Tag_Bitmap           (13)
#define Tag_SubAreas         (14)
#define Tag_Resolution       (15)
#define Tag_Z_To_R_Constants (16)
#define Tag_Clutter_Treatment (17)
#define Tag_Occultation_Correction (18)
#define Tag_Period           (19)

struct NRD_Pixel_t
{
  SINT2 RowCnt;
  SINT2 ColCnt;
  SINT4 StorePixels;
  SINT1 StoreBits;
  SINT1 StoreMax;
  UINT1 Compressed;
  SINT4 FileOffset;
};

typedef FLT4 NRD_Lat_t;
typedef FLT4 NRD_Long_t;

struct NRD_LatLong_t
{
  NRD_Lat_t  Lat;
  NRD_Long_t Long;
};

struct NRD_Projection_t
{
  UINT1             ProjectionType;
#define NRD_PolarStereographic (0)
#define NRD_Gnomonic           (1)
  NRD_Lat_t  Lat;
  NRD_Long_t Long;
};

struct NRD_Area_t
{
  struct NRD_Projection_t Proj;
  struct NRD_LatLong_t    SouthWest;
  struct NRD_LatLong_t    NorthEast;
};

struct NRD_IntStore_t
{
  FLT4  Ord;
  FLT4  Slope;
  SINT4 Offset;
};

struct NRD_BitMap_t
{
  SINT4 FileOffset;
  SINT4 StorePixels;
  UINT1 Compressed;
};

struct NRD_Period_t
{
  SINT4 Period;    /* Period in minutes */
  SINT4 LostTime;  /* Time lost during the above period due because of broken radar */
};

#define Z_TO_R_CONSTANTS_SIZE (9)
/* Note that the structure is not used because of UNIX packing */
struct NRD_Z_To_R_Constants_t
{
  FLT4 Factor;
  FLT4 Exponent;
  UINT1 Type;
};
/* Values used in Type above */
#define NRD_ZH_To_R_Conversion          (0)
#define NRD_ZH_ZDR_To_NO_DO_To_R        (1)
#define NRD_ZF1_ZF2_To_Attenuation_To_R (2)
#define NRD_Reserved_1                  (3)
#define NRD_Reserved_2                  (4)
#define NRD_Reserved_3                  (5)
#define NRD_Other                       (6)
#define NRD_Missing_Value               (7)

/* AreaDef.MaxAreaName_c */
#define NRD_MaxAreaName (10)

struct NRD_Areas_t
{
  SINT4 Count;
  char  Areas[16][8];
};

/* -- FilName.MaxFilNameLength */
#define NRD_MaxFilNameLength (130)


/* -- NodeDef.MaxNodeNameSize */
#define NRD_MAX_NODENAME_SIZE (8)
#define NRD_MAX_HOSTNAME_SIZE (80)

/* -- ReceiverIdDef.MaxReceiverIdSize */
#define NRD_MaxReceiverIdSize (12)

/* Radar type  (enumeration type) */
#define NRD_Rr_Normal  (1)
#define NRD_Rr_Doppler (2)
#define NRD_Rr_Dual    (3)
#define NRD_Rr_X_Band  (4)
#define NRD_Rr_S_Band  (5)
#define NRD_Rr_X1_Band (6)
#define NRD_Rr_X2_Band (7)

/* Radar modes (enumeration type Prod.RrMode_t) */
#define NRD_RrMode_Doppler  (1)
#define NRD_RrMode_NonDop   (2)

/* QuantityUnit (enumeration type) */
#define NRD_q_None     (0)
#define NRD_q_Refl     (1)
#define NRD_q_Prec     (2)
#define NRD_q_Velocity (3)
#define NRD_q_Turb     (4)
#define NRD_q_Height   (5)
#define NRD_q_Log_Surface_Rain_Intensity (6)
#define NRD_q_Log_Surface_Rain_Total (7)


/* PhysicalUnit (enumeration type) */
#define NRD_p_None (0)
#define NRD_p_km   (1)
#define NRD_p_m_s  (2)
#define NRD_p_Dbz  (3)
#define NRD_p_mm   (4)
#define NRD_p_mm_h (5)


/* Product names   (enumeration type Prod.ProdName_t) */

#define NRD_PRD_None      (0)
#define NRD_PRD_Foreign   (1)
#define NRD_PRD_GeoMap    (2)
#define NRD_PRD_RrStatus  (3)

#define NRD_PRD_RrPPI_Z   (4)
#define NRD_PRD_RrPPI_W	  (5)
#define NRD_PRD_RrPPI_T	  (6)
#define NRD_PRD_RrCAPPI_Z (7)
#define NRD_PRD_RrCAPPI_W (8)
#define NRD_PRD_RrCAPPI_T (9)
#define NRD_PRD_RrPsC_Z  (10)
#define NRD_PRD_RrPsC_W	 (11)
#define NRD_PRD_RrPsC_T	 (12)
#define NRD_PRD_RrETM_Z	 (13)
#define NRD_PRD_RrVMI_Z	 (14)
#define NRD_PRD_RrVMI_W	 (15)
#define NRD_PRD_RrVMI_T	 (16)
#define NRD_PRD_RrHMI_Z	 (17)
#define NRD_PRD_RrHMI_W	 (18)
#define NRD_PRD_RrHMI_T	 (19)
#define NRD_PRD_RrHMAX_Z (20)
#define NRD_PRD_RrHMAX_W (21)
#define NRD_PRD_RrHMAX_T (22)
#define NRD_PRD_RrVSI_Z  (23)
#define NRD_PRD_RrVSI_W	 (24)
#define NRD_PRD_RrVSI_T	 (25)
#define NRD_PRD_RrSRT_Z	 (26)
#define NRD_PRD_RrSRI_Z	 (27)

/* Pack Algoritms  (enumeration type Nrd_Api.PackAlg_t) */
#define NRD_PackAlg_RLE (0)
#define NRD_PackAlg_MaxValue (0)

/* -- OperCal.LongDur_t */
struct NRD_LongDur_t
{
  SINT4	days;
  SINT4	secs;
  /* SECS is Ada predefined type Duration which is a fixed point number with
   * four decimals encoded in a 32-bit binary value.
   * 
   * The integer part starts at bit 14;
   *   1 second (1.0000) is hex 4000, dec 16384
   *  10 seconds (10.0000) is hex 28000
   * 100 seconds (100.0000) is hex 190000
   *	
   * only range 0.0000 .. +86399.9999 is used here
   */
};

struct NRD_ProdVSI_t
{
  UINT1 bVsiWall;
  char cStartLat[15];
  char cStartLong[15];
  char cEndLat[15];
  char cEndLong[15];
  char cVsiName[12];
};

union NRD_Normal_PSI_u
{
  /* -- PROD=RrCAPPI_Z..RrCAPPI_T */
  SINT4 height;   /* -- Range NRD_HEIGHT_MINVALUE/MAXVALUE */

  /* -- PROD=RrHMI_Z..RrHMI_T */
  UINT1 hmi_wall; /* -- one of NRD_AREAWALL_* */

  /* -- PROD=RrVSI_Z..RrVSI_T */
  struct NRD_ProdVSI_t VSI;

  /* -- PROD=RrSRT_Z */
  struct NRD_LongDur_t period;
};

struct NRD_Normal_PSI
{
  UINT1  rrmode;  /* -- one of NRD_RrMode_t* */
  union NRD_Normal_PSI_u u;
};

/* Defined in Prod */
#define NRD_MaxCommentSize (36)

union NRD_PSI
{

  /* -- PROD=Foreign */
  char comment[NRD_MaxCommentSize];

  /* -- PROD=GeoMap */
  UINT1	maptype;     /*	-- one of NRD_MAPTYPE_* */
  /*  BYTE  mapgraphic; * -- one of NRD_MAPGRAPHIC_* */

  /* -- PROD=RrStatus */
  char cStatusNode[NRD_MAX_NODENAME_SIZE];

  /* -- PROD=RrPPI_Z..RrSRT_Z */
  struct NRD_Normal_PSI normal;
};

/* -- Prod.ProdDesc_t */
struct NRD_ProdDesc_t
{
  /* -- one of NRD_PRD_* */
  UINT1 prod;
  char  cTimeStr[24];    /* Time in YYYY-MM-DD HH:MM:SS.00 */
  char  cArea[NRD_MaxAreaName];
  SINT4 resolution;

  union NRD_PSI psi;
};


/* -- AviDef.Avi_t */
struct NRD_Avi_t
{
  char cNodeName[NRD_MAX_NODENAME_SIZE];
  struct NRD_ProdDesc_t  pd;
};

/* An array of these structures are used to hold information for converting 
 * NORDRAD areas to IRIS sites.
 */
struct nor_ntoi_map
{
  SINT4 iwavelength;      /*Wavelength in 1/100 of cm */
  char  sitename[20];     /*IRIS site name, null terminated*/
  char  sareaname[12];    /*NORDRAD Area name, null terminated*/
  char  sLocalTZName[TZNAME_SIZE];  /*TZ name of local time */
  SINT2 iLocalWest;       /*TZ offset to local time in minutes */
  char  ipad_end[2];
};

/* An array of these structures are used to hold information for converting 
 * IRIS site to NORDRAD areas.
 */
struct nor_iton_map
{
  char sitename[20];  /*IRIS site name, null terminated*/
  char sareaname[12]; /*NORDRAD Area name, null terminated*/
  UINT4 iPType;       /*If nonzero, then PType controls. */
  char  sPName[16];   /*NULL terminated */
  SINT4 iwidth;       /*Width of area in cm */
  SINT4 iheight;      /*Height of area in cm */
  SINT4 iresolution;  /*Range resolution in cm */
};

/* Information used to convert data types to NORDRAD scale */
#define NRD_MaxAccessCtrlSize 46
struct nor_convert_header
{
  FLT4  frain_acc_min;        /* Log of minimum rainfall accumulation in mm */
  FLT4  frain_acc_max;        /* Log of maximum rainfall accumulation in mm */
  FLT4  fDefaultCappiHeight;  /* Height to assume for incoming CAPPIs with no attribute */
  SINT4 iApiDevice[2];       /* Device numbers for two output channels */
  char  sApiAccessCtrl[2][NRD_MaxAccessCtrlSize];
  char  sApiHostName[2][NRD_MAX_HOSTNAME_SIZE];
  char  sApiReceiverId[2][NRD_MaxReceiverIdSize];

  SINT4 iNtoICount;    /* Number of elements in NtoI map */
  SINT4 iItoNCount;    /* Numbef of elements in ItoN map */
  struct nor_ntoi_map *pNtoI;
  struct nor_iton_map *pItoN;
};

/*
 *============================================================================
 *	Function prototypes for NORDRAD supplied functions
 *============================================================================
 */

void CancelAvi
  (SINT4 isocket
   );

void GetProduct
  (char  *access,
   char  *node,
   struct NRD_Avi_t        *advice,
   char  *file,
   SINT4 iwaittime,
   SINT4 *istatus
   );

void ReceiveAvi
  (SINT4 isocket,
   struct NRD_Avi_t *advice,
   SINT4 iwaittime,
   SINT4 *istatus
   );

void StoreProduct  /* Unix style */
  (char  *access,
   char  *node,
   struct NRD_ProdDesc_t   *desc,
   char  *file,
   SINT4 iwaittime,
   SINT4 *istatus
   );

void SubscribeAvi  /* Unix style */
  (char *access,
   char *node,
   char *recid,
   SINT4 iwaittime,
   SINT4 *ichannel,
   SINT4 *istatus
   );

void Pack
  (char  *source,
   char  *dest,
   UINT1 ipackalg,
   SINT4 *istatus
   );

void UnPack
  (char  *source,
   char  *dest,
   UINT1 ipackalg,
   SINT4 *istatus
   );

#ifdef __cplusplus
}
#endif
#endif /* #ifndef SIGMET_NRD_API_H */
