/* **********************************************************************
 * *                                                                    *
 * *    Structures used in TDWR data files                              *
 * *                                                                    *
 * **********************************************************************
 * File: utils/tdwr/tdwr_interface.h
 *
 *                        COPYRIGHT (c) 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.
 */

struct tdwr_hdr
{
  unsigned short msg_id;  /* 0x2B00: Normal dwell; 0x2B01: Low PRF dwell */
  unsigned short msg_len;
};

struct buf_hdr
{
  unsigned short volume_scan_count;
  unsigned char iFlags2;
#define HDR_FLG2_EOV (0x80)
#define HDR_FLG2_SOV (0x40)
  /*
  unsigned char eov:1;
  unsigned char sov:1;
  unsigned char pad1:6;
  */
  unsigned char scan_strategy;      /* 1:Monitor mode, 2:Hazardous mode */
  unsigned short peak_power;
  unsigned short iFlags6;
#define HDR_FLG6_DUMMY_RECORD (0x0004)
  /*
  unsigned short pad2:13;
  unsigned short dummy_record:1;
  unsigned short start_live_data:1;
  unsigned short start_playback_data:1;
  */
  unsigned long iFlags8;
#define HDR_MSK8_SCAN_NUMBER (0xff000000)
#define HDR_SHFT8_SCAN_NUMBER (24)
#define HDR_FLG8_EOE         (0x00800000)
#define HDR_FLG8_SOE         (0x00400000)
  /*
  unsigned int scan_number:8;
  unsigned int eoe:1;
  unsigned int soe:1;
  unsigned int crmn:3;
  unsigned int pad3:5;
  unsigned int obscuration_flag:1;
  unsigned int spike_removal:1;
  unsigned int dealiasing_scan:2;
  unsigned int sector_scan:1;
  unsigned int microburst_aloft_scan:1;
  unsigned int precip_scan:2;
  unsigned int wind_shift_scan:1;
  unsigned int low_elevation_scan:1;
  unsigned int microburst_scan:1;
  unsigned int gust_front_scan:1;
  unsigned int low_prf_scan:1;
  */
  float current_elevation; /* Floating degrees */
  float angular_scan_rate;
  unsigned short pri;
  unsigned short iFlags22;
#define HDR_MSK22_DWELL_ID (0xc000)
#define HDR_SHFT22_DWELL_ID (14)
  
  /*
  unsigned short dwell_id:2;
  unsigned short solar_indicator:1;
  unsigned short pad4:1;
  unsigned short pulses_per_dwell:12;
  */
  unsigned short final_range_sample;
  unsigned short range_samples_per_dwell;
  float current_azimuth;  /* Floating degrees, but 2 degrees ahead */
  float total_noise_power;
  long timestamp;
  unsigned short base_data_type;
  unsigned short iFlag42;
  /*
  unsigned short pad6:11;
  unsigned short wind_field_init:1;
  unsigned short incomplete_vol_scan:1;
  unsigned short incomplete_ele_scan:1;
  unsigned short vol_scan_restart:1;
  unsigned short ele_scan_restart:1;
  */
  unsigned short integer_azimuth;   /* Azimuth index number */
  unsigned short load_short_final_sample;
};

struct normal_sample
{
  unsigned char Z;
  unsigned char SNR;
  unsigned short V;
  unsigned char W;
  unsigned char iFlags5;
  /* Original non portable code
  unsigned char pad1:2;
  unsigned char caf:3;
  unsigned char point_target_flag:1;
  unsigned char velocity_valid_flag:1;
  unsigned char conditioned_valid_flag:1;
  */
#define SAMPLE_MSK_CAF               (0x38)
#define SAMPLE_FLG_POINT_TARGET      (0x04)
#define SAMPLE_FLG_VELOCITY_VALID    (0x02)
#define SAMPLE_FLG_CONDITIONED_VALID (0x01)
  unsigned short VC;
};

struct lprf_sample
{
  unsigned char iFlags;
#define SAMPLE_FLG_VALID             (0x40)
  /* Original non portable code */
  /*
  unsigned char pad1:1;
  unsigned char valid_flag:1;
  unsigned char pad2:3;
  unsigned char point_target_flag:1;
  unsigned char pad3:2;
  */
};

struct tdwr_buf_norm
{
  struct tdwr_hdr tdwr_hdr;
  struct buf_hdr  hdr;
  struct normal_sample normal_sample[600];
  unsigned int pad7[323];
};

struct tdwr_buf_lprf
{
  struct tdwr_hdr tdwr_hdr;
  struct buf_hdr  hdr;
  unsigned char Z[1984];
  unsigned char SNR[1984];
  struct lprf_sample lprf_sample[1984];
  unsigned int pad7[35];
};

/* This union is what is sent out for a ray from a TDWR */
union tdwr_buf
{
  struct tdwr_hdr tdwr_hdr;
  struct tdwr_buf_norm tdwr_buf_norm;
  struct tdwr_buf_lprf tdwr_buf_lprf;
};

struct dwell_table
{
  unsigned char node[300];
};

struct mail_ptr
{
  long mtype;
  int  index;
};

void global_memory_attach(void);

#ifdef TDWR_ALLOCATE
#define EXTERN
#else
#define EXTERN extern
#endif

EXTERN int    logging;
EXTERN FILE   *log_file;
EXTERN struct dwell_table *dwell_table;
EXTERN union  tdwr_buf *dwell_p;

#undef EXTERN

void Lprintf(const char *log_str);

#ifdef SIGMET_SIGTYPES_H
MESSAGE SignalHandler
(MESSAGE iStatus_a,
 UINT4 iCount_a,
 const UINT4 iSigArgs_a[] );
#endif  /* #ifdef SIGMET_SIGTYPES_H */

#ifdef SIGMET_PRODUCT_H
/* In file raw_c.c */
MESSAGE iraw_prod
(const struct product_configuration *pcf_a, /* Product configuration input */
 const char ssum_a[20],                     /* Ingest summary file to use */
 struct product_hdr *phdr_a,                /* Header of the product file produced */
 char bfile_a[],                           /* Name of product file produced */
 int  close_flag);                          /* Force close even if sweep is not last */
#endif  /* #ifdef SIGMET_PRODUCT_H */


