/* **********************************************************************
 * *                                                                    *
 * *   TDWR_LLWAS integrator and runway direction header file           *
 * *                                                                    *
 * **********************************************************************
 * File: utils/llwas/llwas.h
 *
 *                COPYRIGHT (c) 1999, 2000, 2002, 2004 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.
 */

#ifdef LLWAS_ALLOCATE
#define EXTERN
#else
#define EXTERN extern
#endif

#define MAX_TIMEOUT (60*60)  //Do not allow timeouts longer than 1 hour.

/* This structure is stored in shared memory between the
 * runways program and the tdwr_llwas_int program.
 */

struct one_runway_state
{

  /* Control input to IRIS for runway direction */
  UINT1 iCommand;        // Choices: PRIMARY, SECONDARY, (AUTO), INACTIVE 
  /* Direction deduced from Auto source */
  /* UINT1 iAutoDirection; */ // Choices: PRIMARY, SECONDARY, DEAD, ERROR
  /* Direction in effect */
  UINT1 iDirection;      // Choices: PRIMARY, SECONDARY, INACTIVE
};

struct runway_state
{
  struct one_runway_state Runway[MAX_RUNWAYS];
};

/* This structure comes from a configuration file, and does
 * not change while we are running.
 */
struct one_runway_setup
{
  char  sPrimaryDirection[8];
  char  sSecondaryDirection[8];
  char  sThirdDirection[8];
  UINT4 iDefaultCommand;    /* Command to start with at powerup */
};

/* Configuration information about the integrator */
struct one_channel_config
{
  UINT4 iPort;
  char  sHost[16];  /* Host name for outputs */
  char  sDirectory[FILENAME_SIZE];
  UINT4 iTimeout;   /* Age in ms before input data is considered stale */
  /* For output it is the retransmit time even if there is no change */
};

struct runway_coverage
{
  UINT1 iMin;       /* Minimum runway coverage (1-7) */
  UINT1 iMax;       /* Maximum runway coverage (1-7) */
};

/* Holds all the IPF values which are different for each runway */
struct runway_ipf
{
  FLT4 fMb_factor;          /* Minimum loss that will be passed */
  FLT4 fGf_factor;          /* Minimum gain that will be passed */
  FLT4 fThresh[6];
  struct runway_coverage Coverage[2];  /* Coverage for both directions */
};
/* Holds all the IPF values */
struct integration_parameter_file
{
  FLT4 fmin_mba;         /* Seam between MBA and WSA, positive */
  FLT4 fmax_wsa;         /* Value weak MBAs are reduced to if not confirmed, must be neg */
  FLT4 fLossIncrement;
  FLT4 fLossGainBuffer;
  struct runway_ipf Tdwr[MAX_RUNWAYS];
  struct runway_ipf Llwas[MAX_RUNWAYS];
};

/* Holds all the information to define the projection for a typical
 * IRIS GEOM_EARTH, GEOM_CAPPI, or GEOM_PPI product.  There is some redundant
 * information.
 */
struct projection_summary
{
  BIN4  iRadarLat;                  /* Latitude of the radar */
  BIN4  iRadarLon;                  /* Longitude */
  FLT8  fRMax;                      /* Range from center to right side of screen */
  BIN4  iCenterLat;                 /* Latitude of center of product */
  BIN4  iCenterLon;                 /* Longitude */
  UINT1 iType;                      /* Type of projection */
  BIN4  iReferenceLat;              /* Latitude of reference point */
  BIN4  iReferenceLon;              /* Longitude */
  SINT4 iXSize, iYSize;             /* Sizes in pixels */
  SINT4 iXScale, iYScale;           /* Scales in cm/pixel */
  SINT4 iXRadPos, iYRadPos;         /* Radar positions in 1/1000 of pixels */
};

#define MAX_OPERATORS 8
struct runway_setup
{
  SINT4 iRunwayCount;    /* Count is between 0 and MAX_RUNWAYS */
  struct one_runway_setup Runway[MAX_RUNWAYS];
  char  sOperators[MAX_OPERATORS][16];  /* Null terminated, unused slots are all zero */
  UINT1 lLogToFile;      /* Enable logging to terminal or file or both */
  UINT1 lLogToTerm;
  UINT1 lFilterRunways;  /* Filter out unused runways */
  UINT1 lUseIrisSite;    /* Use sitename from IRIS inputs, if available */
  char  sSiteName[20];
  UINT1 lUseIrisLatLon;  /* Use lat/lon and range from IRIS inputs, if available */
  struct projection_summary Projection;

  struct one_channel_config IrisIn;
  struct one_channel_config IrisOut;
  struct one_channel_config LlwasIn;
  struct integration_parameter_file IPF;
};

/* State information about the integrator */
struct one_input_state
{
  UINT1 lWorking;       /* Channel is fully working */
  int   iSocket;        /* Socket number */
};

struct one_output_state
{
  UINT1 lWorking;       /* Channel is fully working */
  UINT1 lInProgress;    /* connect call in progress, needs to be completed later */
  int   iSocket;        /* Socket number */
  UINT4 iLastSendTime;  /* Time of last transmission */
};

/* ======================================================
 * Structures below here only used in the integrator.
 */
struct alert_list
{
  char  sCorridor[16];    /* Name of corridor */
  FLT8  fValue;           /* Alert value in m/s, 0 means no alert */
  UINT1 iArenaIndex;      /* Number 1-7 to indicate arena order, 0 if not runway */
  UINT1 iRunwayIndex;     /* Index of which runway this corridor is on, 0 if N/A */
  UINT1 lOtherCoverage;   /* Other system covers this alert location */
  UINT4 iSetupMask;       /* Which areas were checked for this alert */
  char  sArena[8];        /* Name of arena of hit */
  char  sThresholdDirection[3];
  char  sThresholdSpeed[2];
};

#ifdef SIGMET_HEADERS_H
struct input_header
{
  UINT1 lValid;                     /* Product data is valid and not timed out */
  UINT1 lNew;                       /* A new product has arrived */
  UINT4 iTime;                      /* Arrival time of last product */
  char  sIngSiteName[20];           /* Ingest Sitename, null terminated */
  char  sHardwareName[20];          /* Ingest Hardware name, null terminated */
  char  sProdSiteName[20];          /* Product gen Site name, null terminated */
  struct projection_summary Projection;  /* Projection information */
  UINT4 iCorridorCount;             /* Number of corridors available in list */
  struct warning_results *pWarnings;/* Pointer to malloced results array */
  struct sline_results *pSlines;    /* Pointer to malloced results array */
  struct tdwr_psi_struct TdwrPsi;   /* CF and LLWAS status */
};
#endif  /* #ifdef SIGMET_HEADERS_H */

#ifdef SIGMET_SETUP_H
struct input_info
{
  struct input_header Header;
  struct alert_list Alerts[IMAX_PROT_REGIONS];
};
#endif  /* #ifdef SIGMET_SETUP_H */

struct alert_list *FindCorridorInInfo  /* File: Process.C */
( const char *sCorridor_a,
  struct input_info *pList_a );

const struct alert_list *FindCorridorInInfo  /* File: Process.C */
( const char *sCorridor_a,
  const struct input_info *pList_a );

/* Function to initialize the runway state when shared memory is
 * first created. */
void InitializeRunwayState    /* File: LoadSetup.C */
( volatile struct runway_state *pState,
  const struct runway_setup *pRunwaySetup );

/* Function to load the runway setup from a configuration file */
void LoadRunwaySetup          /* File: LoadSetup.C */
( struct runway_setup *pRunwaySetup );
  

/* Print to log file and/or screen */
void Lprintf(const char *log_str);  /* File: tdwr_llwas_int.C */

struct input_info *pGetLlwasInput(void); /* File: Output.C */

struct input_info *pGetSlineInput(void); /* File: Output.C */

struct input_info *pGetWarnInput(void); /* File: Output.C */

/* Process inputs from IRIS */
void ProcessIrisInputs        /* File: Process.C */
( struct one_input_state *pState_a,
  const struct arena_list *pArenaList_a );

/* Process inputs from LLWAS */
void ProcessLlwasInputs
( struct one_input_state *pState_a,
  const struct arena_list *pArenaList_a );

/* Send a TDWR product to IRIS if needed */
void SendTdwrIfNeeded         /* File: Process.C */
( struct one_output_state *pState_a,
  const struct iris_setup *pISetup_a,
  const struct arena_list *pArenaList_a );

/* Updates the runway directions.  This only needs to be called whenever
 * the direction command is changed, or the LLWAS input is changed.
 */
void UpdateDirections         /* File: LoadSetup.C */
( volatile struct runway_state *pState_a );

/* Shared storage between files */
EXTERN volatile struct runway_state *pRunwayState_c;
EXTERN struct runway_setup RSetup_c;


#undef EXTERN




