/*
   FILE:  tran_nbp.h

	  Narrowband processed (NBP) transect file structures 

*/
#include <dbext.h>     /* UBYTE, etc. */
#include "transect.h"  /* TRANSECT_NAV_TYPE */

#define	CONFIGURATION_ID	0x000A
#define	LEADER_ID		0x000B
#define	VELOCITY_ID		0x0001
#define	AMPLITUDE_ID		0x0003
#define	PGOOD_ID		0x0104
#define	DISCHARGE_ID		0x000C
#define	NAVIGATION_ID		0x000E

typedef struct
{
   USHORT 	n_bytes_in_data_segment;
   UBYTE 	n_depth_cells;
   UBYTE 	n_data_types;
   USHORT 	*offset;
} NBP_HEADER_TYPE;

#define NBP_HEADER_FIXED_SIZE 4  /* up to n_data_types, without padding */

typedef struct
{
   UBYTE 	adcp_type;		/* 0 = Narrowband adcp, 1 = Broadband adcp */
   UBYTE 	transect_version;
   UBYTE 	transect_revision;
   USHORT 	ADCP_serial_number;
   USHORT 	ADCP_firmware_number;
   UBYTE 	n_beams;	        /* # Beams for calculating velocity data */
   UBYTE 	beam_angle;
   USHORT 	frequency;
   UBYTE 	range_switch;
   UBYTE 	coord_system;
   UBYTE 	orientation;		/* 0 = Up, 1 = Down */
   UBYTE 	beam_pattern;		/* 0 = Convex, 1 = Concave */
   UBYTE 	n_bins;		        /* Range = 1 - 128 depth cells */
   ULONG 	ping_interval;          /* 0.01 s */
   USHORT 	pings_per_ens;	        /* Range = 0 to 16,384 pings */
   UBYTE 	bin_length;		/* Range = 1 to 32 m */
   USHORT 	pulse_length;		/* Range = 1 to 99 m */
   USHORT 	blank;	                /* Range = 0 to 9999 cm */
   ULONG 	adcp_depth;		/* Range = 0 to 200,000 cm */
   UBYTE 	avg_method;		/* 0 = Temporal, 1 = Spatial */
   ULONG 	avg_interval;           /* s or cm */
   LONG 	magnetic_variation;	/* 0.001 deg; Range = -360 to 360 deg */
   LONG 	compass_offset;		/* 0.001 deg; Range = -360 to 360 deg */
   LONG 	transducer_misalignment;/* 0.001 deg CW from keel to beam 3; Range = -360 to 360 deg */
   USHORT 	intensity_scale;	/* 0.001 dB/count; Range = 0 to 1 dB/count */
   USHORT 	absorption_coef;        /* 0.001 dB/m; Range = 0 to 2 dB/m */
   USHORT 	salinity;		/* 0.001 ppt; Range = 0 to 42 ppt */
   LONG 	sound_speed;		/* cm/s; Range = 0 to 160,000 cm/s */
   UBYTE 	use_default_sound_speed;/* 1 = used 1536; 0 = used value other than 1536 */
   UBYTE 	no_PR_compensation;     /* 1 = no compensation; 0 = with compensation */
					/* begin NA: */
   LONG 	start_discharge;	/* Range = -214,748,364.8 to 214,748,364.7 */
   LONG 	end_discharge;		/* Range = -214,748,364.8 to 214,748,364.7 */
   ULONG 	start_distance;		/* Range = 0 to 300,000 cm */
   ULONG 	end_distance;		/* Range = 0 to 300,000 cm */
   UBYTE 	top_q_method;
   UBYTE 	btm_q_method;
   USHORT 	power_curve_exp;	/* Range = 0 to 0.5 */
   LONG 	spare1;
   LONG 	spare2;
					/* end NA */
   UBYTE 	dply_name_size;
   char 	*name;                  /* goes into block comments */
   UBYTE 	primary_drive;          /* NA */
   UBYTE 	secondary_drive;        /* NA */
   UBYTE 	direct_cmd_size;
   char 	*direct_cmd;            /* goes into block comments */
} NBP_CONFIGURATION_TYPE;

/* The elements marked NA are of no possible interest to us.
   Some of the other elements are also most likely not useful,
   but perhaps it is just as well to read and store them anyway--
   it is not much harder than skipping over them.
*/

#define NBP_CONFIGURATION_FIXED_SIZE 89  /* up to dply_name_size, without padding */

typedef struct
{
   LONG 	ens_start_seconds; /* seconds in year, but no leap years!; Range = 0 to 2,147,483,647 sec */
   USHORT 	ens_number;	   /* processed, not raw; Range =  1 to 65,535 ensembles */
   SHORT 	avg_pitch;	   /* 0.01 deg; Range = -20.00 to +20.00 deg */
   SHORT 	avg_roll;          /* 0.01 deg; Range = -20.00 to +20.00 deg */
   USHORT 	avg_heading;	   /* 0.01 deg; Range = 000.00 to 359.99 deg */
   SHORT 	avg_temperature;   /* 0.01 deg C; Range = -5.00 to +40.00 deg */
   SHORT 	bt_u;
   SHORT 	bt_v;
   SHORT 	bt_w;
   SHORT 	bt_e;
   USHORT 	bt_range1;         /* cm */
   USHORT 	bt_range2;
   USHORT 	bt_range3;
   USHORT 	bt_range4;
   LONG 	x_displacement;    /* Range = -2,147,483,648 to 2,147,483,647 cm */
   LONG 	y_displacement;	   /* Range = -2,147,483,648 to 2,147,483,647 cm */
   ULONG 	elapsed_time;      /* sec; Range = 0 to 42,949,672.95 sec */
   ULONG 	path_length;	   /* cm; Range = 0 to 4,294,967,295 cm */
   LONG 	top_discharge;     /* NA; Range = -214,748,364.8 to 214,748,364.7 */
   LONG 	bottom_discharge;  /* Range = -214,748,364.8 to 214,748,364.7 */
   LONG 	total_discharge;   /* Range = -214,748,364.8 to 214,748,364.7 */
   UBYTE 	bit_result;	   /* Range = 0 to 65535 */
   USHORT 	n_raw_ens;         /* Range = 1 to 65535 ensembles */
}  NBP_LEADER_TYPE;

/* #define NBP_LEADER_SIZE  61     /* without padding */
#define NBP_LEADER_SIZE  62     /* ?? seems to have an extra byte!! */

typedef struct
{
   NBP_HEADER_TYPE 		header;
   NBP_CONFIGURATION_TYPE 	conf;
   NBP_LEADER_TYPE 		leader;
   SHORT 			u[MAX_BINS];
   SHORT 			v[MAX_BINS];
   SHORT 			w[MAX_BINS];
   SHORT 			e[MAX_BINS];
   UBYTE 			raw_amp[4*MAX_BINS];
   UBYTE 			mean_amp[MAX_BINS];
   BYTE 			pg[MAX_BINS];
   LONG 			discharge[MAX_BINS];
   TRANSECT_NAV_TYPE 		nav;
   UBYTE 			leader_present;
   UBYTE 			vel_present;
   UBYTE 			amp_present;
   UBYTE 			pg_present;
   UBYTE 			discharge_present;
   UBYTE 			nav_present;
}  NBP_ENSEMBLE_TYPE;
