/* ***********************************
 * *                                 *
 * *  NEXRAD format definition file  *
 * *                                 *
 * ***********************************
 * File: include/nexrad.h
 *
 *                COPYRIGHT (c) 2001, 2002, 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.
 */

/* This is the format used by NEXRAD.  It is sometimes called WSR88D level 2
 * format.  Also called archive 2 format.
 */

#define NR_NO_DATA   (0x00)
#define NR_OVERLAID  (0x01)
#define NR_MAX_PACKET_LENGTH (2432)
#define NR_MAX_RECORD_LENGTH (NR_MAX_PACKET_LENGTH*13)

/* Tape filename,        page 3-4 of 2620010  */
/* Volume Header Record, page 7-3 of 2620010B */
struct nr_volume_header
{
  char sFileName[9];  
  /* Filename options:
   * "ARCHIVE2."  --  1998 build 10.0
   * "AR2V0001."  --  2005 build  7.0 Adds Radar ID
   */
  char sFileExt[3];   /* Increments starting with "000", reset each time date changes */
  /* File creation date, in GMT */
  UINT4 iFileDate;    /* Number of days since 1/1/70, origin 1 */
  UINT4 iFileTime;    /* Number of milliseconds since midnight, origin 0 */
  char  sRadarId[4];
};

/* Message Header, page 7-6 of 2620010B */
struct nr_message_header
{
  UINT2    CTM[6];    /* Channel Terminal Manager (CTM) info not used */
  
  /* halfword 7 : message header information */
  UINT2    iMsgSize;    /* # halfwords from here to end of record? */
  UINT1    iRdaChannel; /* Channel number*/
  UINT1    iMsgType;    /* Digital Radar Data.  This message may contain
			 * a combination of either reflectivity,
			 * aliased velocity, or spectrum width.
			 */
#define NR_MSG_TYPE_DIGITAL_RADAR      (1)
#define NR_MSG_TYPE_RDA_STATUS         (2)
#define NR_MSG_TYPE_PERFORMANCE        (3)
#define NR_MSG_TYPE_CONSOLE_MSG_TO_RPG (4)
#define NR_MSG_TYPE_MAINTENANCE_LOG    (5)
#define NR_MSG_TYPE_RDA_CONTROL        (6)
#define NR_MSG_TYPE_VOLUME_COVERAGE    (7)
#define NR_MSG_TYPE_CLUTTER_SENSOR     (8)
#define NR_MSG_TYPE_REQUEST_DATA       (9)
#define NR_MSG_TYPE_CONSOLE_MSG_TO_RDA (10)
#define NR_MSG_TYPE_LOOPBACK_TO_RPG    (11)
#define NR_MSG_TYPE_LOOPBACK_TO_RDA    (12)
#define NR_MSG_TYPE_CLUTTER_BYPASS     (13)
#define NR_MSG_TYPE_EDITED_CLUTTER     (14)
#define NR_MSG_TYPE_NOTCHWIDTH_MAP     (15)
#define NR_MSG_TYPE_RDA_ADAPTATION     (18)

  UINT2    iIDSequence;    /* I.d. Seq = 0 to 7FFF, then roll over to 0 */
  /* This time is the same as ray time on an IRIS system. */
  UINT2    iMsgDate;       /* modified Julian date from 1/1/70 */
  UINT4    iMsgTime;       /* packet generation time in ms past midnite */
  UINT2    iSegmentCount;  /* Origin 1, always set to 1 */
  UINT2    iSegmentNumber; /* Origin 1, always set to 1 */
};

/* Digital Radar Data, page 4-5 of 2620002A */
struct nr_radar_data_header
{
  /* halfword 15 : data header information */
  UINT4    iDataTime;      /* collection time for this ray in ms, origin 0 */
  UINT2    iDataDate;      /* modified Julian date for this ray, origin 1 */
  UINT2    iUnambRange;    /* unambiguous range, in 1/10 of km, ignore possible range unfolding */
  BIN2     iAzimuth;       /* azimuth angle, binary angle */
  UINT2    iRayNumber;     /* ray no. within elevation scan, origin 1 */
  UINT2    iRayStatus;     /* ray status flag */
#define NR_RAY_STATUS_START_SWEEP  (0)
#define NR_RAY_STATUS_MIDDLE       (1)
#define NR_RAY_STATUS_END_SWEEP    (2)
#define NR_RAY_STATUS_START_VOLUME (3)
#define NR_RAY_STATUS_END_VOLUME   (4)
  BIN2     iElevation;     /* elevation angle, binary angle */
  UINT2    iSweepNumber;   /* elevation no. within volume scan, origin 1 */
  
  /* halfword 24 : gate/bin information*/
  SINT2    iBinStartRef;   /* range to first gate of refl data, meters */
  SINT2    iBinStartDop;   /* range to first gate of doppler data, meters */
  UINT2    iBinStepRef;    /* refl data gate size, meters */
  UINT2    iBinStepDop;    /* doppler data gate size, meters */
  UINT2    iBinNumberRef;  /* no. of reflectivity gates */
  UINT2    iBinNumberDop;  /* no. of doppler gates */
  
  /* halfword 30 */
  UINT2    iSectorNumber;  /* sector no. within cut, purpose unknown, set to 1 */
  FLT4     fSystemCal;     /* gain calibration constant, set to 0 */
  
  /* halfword 33 : data parameters */
  UINT2    iRefPointer;    /* reflectivity data ptr */ 
  UINT2    iVelPointer;    /* velocity data ptr */
  UINT2    iWidPointer;    /* spectrum width ptr */
  UINT2    iVelocityRes;   /* Doppler velocity resolution (2=0.5m/s, 4=1.0m/s)*/
  UINT2    iVolumePattern; /* volume coverage pattern */
  UINT2    unused1[4];
  
  /* halfword 42 : data pointers for Archive II playback */
  UINT2    ref_ptrp;       /* These 3 words are unused, and set to zero */
  UINT2    vel_ptrp;
  UINT2    spc_ptrp;
  
  /* halfword 45 */
  UINT2    iNyquistVel;    /* Nyquist velocity in 1/100 of a m/s */
  SINT2    iAttenuation;   /* Negative of atmospheric attenuation in 1/1000 of a dB/km */
  UINT2    min_dif;        /* Unused, and set to zero */
  
  /* halfwords 48 to 64 */
  UINT2    unused2[17];    /* Set to zero */
  
};
/* RDA Adaptation, table XV of 2620002D */
struct nr_rda_adaptation
{
  char     ipad0x1092[644];
  FLT4     fpath_losses[71];  /* (644)  Path loss array, unused */
  char     ipad928x164[164];
  UINT4    itfreq_mhz;        /* (1092) Transmitter frequency in MHz */
  char     ipad1096x36[36];
  FLT4     fbeam_width;       /* (1132) Antenna beamwidth in degrees */
  char     ipad1136x152[152];
  FLT4     fLatSec;           /* (1288) Latitude seconds */
  FLT4     fLonSec;           /* (1292) Longitude seconds */
  char     ipad1296x4[4];
  UINT4    iLatDeg;           /* (1300) Latitude degrees */
  UINT4    iLatMin;           /* (1304) Latitude minutes */
  UINT4    iLonDeg;           /* (1308) Longitude degrees */
  UINT4    iLonMin;           /* (1312) Longitude minutes */
  char     sLatDir[4];        /* (1316) N or S */
  char     sLonDir[4];        /* (1320) E or W */
  char     ipad1324x7074[7064];
  int      ignd_hgt;          /* (8388) Ground height in meters */
  int      irad_hgt;          /* (8392) Radar height above ground in meters */
  char     ipad_8396x1072[1072];  /* overall size=9468 bytes */
};

/* Overall structure for a Nexrad packet.  For radar data these packets 
 * are always a fixed 2432 bytes long.
 */
struct nr_radar_packet
{
  struct nr_message_header    MessageHeader;
  struct nr_radar_data_header DataHeader;
  UINT1  iData[460+920+920];
  UINT2    CTM_end[2];    /* Channel Terminal Manager (CTM) info not used */
};

struct nr_generic_packet
{
  struct nr_message_header    MessageHeader;
  UINT1  iData[2400];
  UINT2    CTM_end[2];    /* Channel Terminal Manager (CTM) info not used */
};

union nr_packet
{
  struct nr_message_header    MessageHeader;
  struct nr_radar_packet      RadarData;
  struct nr_generic_packet    Generic;
};

/* Structures below here used for nexrad level 3 data */

/* Message Header, page 3-37 of 2620001A */
struct l3_message_header2
{
  UINT2 iMessageCode;
  UINT2 iMsgDate;
  UINT4 iMsgTime;
  UINT4 iMsgLength;
  UINT2 iSourceID;
  UINT2 iDestID;
  UINT2 iNumberBlocks;
};

/* Product descriptor block, page 3-54 of 26200001A */
struct l3_product_description
{
  SINT2 iBlockDivider;
  SINT4 iLatitude;
  SINT4 iLongitude;
  SINT2 iHeight;
#define L3_REFLECTIVITY_1KM_8   (16)
#define L3_REFLECTIVITY_2KM_8   (17)
#define L3_REFLECTIVITY_4KM_8   (18)
#define L3_REFLECTIVITY_1KM_16  (19)
#define L3_REFLECTIVITY_2KM_16  (20)
#define L3_REFLECTIVITY_4KM_16  (21)
#define L3_VELOCITY_250M_8      (22)
#define L3_VELOCITY_500M_8      (23)
#define L3_VELOCITY_1KM_8       (24)
#define L3_VELOCITY_250M_16     (25)
#define L3_VELOCITY_500M_16     (26)
#define L3_VELOCITY_1KM_16      (27)
#define L3_WIDTH_250M_8         (28)
#define L3_WIDTH_500M_8         (29)
#define L3_WIDTH_1KM_8          (30)
  SINT2 iProductCode;      /* HW 16 */
  UINT2 iOperationalMode;
  UINT2 iVolumeCoverage;
  SINT2 iSequenceNumber;
  UINT2 iVolumeNumber;     /* HW 20 Volume scan number, range 1 - 80 cyclic */
  UINT2 iVolumeDate;
  UINT4 iVolumeTime;
  UINT2 iProductData;
  UINT4 iProductTime;      /* HW 25&26 */
  SINT2 iHW27;
  SINT2 iHW28;
  UINT2 iElevationNumber;  /* HW 29 Origin zero */
  SINT2 iElevationAngle;   /* HW 30 Angle in 1/10 of degrees */
  UINT2 iHW31;
  UINT2 iHW32;
  UINT2 iHW33;
  UINT2 iHW34;
  UINT2 iHW35;
  UINT2 iHW36;
  UINT2 iHW37;
  UINT2 iHW38;
  UINT2 iHW39;
  UINT2 iHW40;
  UINT2 iHW41;
  UINT2 iHW42;
  UINT2 iHW43;
  UINT2 iHW44;
  UINT2 iHW45;
  UINT2 iHW46;
  UINT2 iMaxDataValue;     /* HW 47 */
  UINT2 iMaxPosVel;        /* HW 48 */
  UINT2 iHW49;
  UINT2 iHW50;
  FLT4  fCalConstant;      /* HW 51&52 */
  UINT2 iHW53;
};
