/*****************************************************************************
   FILE:  tran_bb.h

	  Broadband processed (BBP) transect file structures 

	Modified from NB transect file structure (tran_nbp.h) as written by UH
         - sept'95 
        Dave Senciall
	NWAFC,DFO,Gov of Canada


	Some of these structures are identical to the NB versions but I have
	given them there own names since there is no garentee that BB transect
	will not evolve away from NB formats as new versions come out. ~Davsen.

*****************************************************************************/

#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;
} BBP_HEADER_TYPE;

#define BBP_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_firmware_number;   
   UBYTE 	n_beams;	        /* # Beams for calculating velocity data */
   USHORT 	beam_angle;		/* was 1 byte in nb */
   USHORT 	frequency;		/* in Khz */
   UBYTE 	profile_mode;		/* was range switch in nb, modes are 0-3 */
   USHORT 	coord_system;		/* was 1 byte in nb 0-3 */
   USHORT 	orientation;		/* 0 = Up, 1 = Down ,1 byte in nb*/
   USHORT 	beam_pattern;		/* 0 = Convex, 1 = Concave 1 byte nb*/
   UBYTE 	n_bins;		        /* Range = 1 - 128 depth cells (WN cmd) */
   ULONG 	ping_interval;          /*  0.01 s  (TP cmd) */
   USHORT 	pings_per_ens;	        /* Range = 0 to 16,384 pings (WP cmd) */
   USHORT 	bin_length;		/* Range = 1 to 3200 cm called depth
					  cell size bb, 1 byte in nb (WS cmd) */
 /*  USHORT 	pulse_length;*/		/* NO EQUIVELENT TO PULSE LENGTH IN BB see Xmit_pluse_len below*/
   USHORT 	blank;	                /* Range = 0 to 9999 cm (WF cmd) */
   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 */
   USHORT	bin_1_dist;		/* BB only */
   USHORT	xmit_pulse_len;		/* BB only */
   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;	/* called s1 in BB in documentation */
   char 	*direct_cmd;            /* goes into block comments */
   UBYTE 	direct_cmd2_size;	/* BB  only, s2 ? only present if direct exceed  255*/
   char 	*direct_cmd2;           /* goes into block comments */
} BBP_CONFIGURATION_TYPE;

/* It is not fully clear from the docs if the direct_cmd2_size field is present
always, or if only when the direct_cmd_size is 255 , it 'appears' to always be
there for version 2.72 transect with a value of 0 for direct_cmd_size <255.
								  ~davsen
 */

/* 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 BBP_CONFIGURATION_FIXED_SIZE 90  /* up to dply_name_size, without padding */

typedef struct
{
   LONG 	ens_start_day;	   /* elapsed days from begin of century starting at 0 */
   LONG 	ens_start_seconds; /* hunds of secs in day; Range = 0 to 86399.99sec */
   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;		/* bottom refered bottom track ping velocities*/
   SHORT 	bt_v;
   SHORT 	bt_w;
   SHORT 	bt_e;
   SHORT 	wt_u;	        /* water layer refered of bottom track pulse*/
   SHORT 	wt_v;
   SHORT 	wt_w;
   SHORT 	wt_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 */
   USHORT 	bit_result;	   /* Range = 0 to 65535 ,1byte in NB*/
   USHORT 	n_raw_ens;         /* Range = 1 to 65535 ensembles */
}  BBP_LEADER_TYPE;

#define BBP_LEADER_SIZE  74     /*  */
/* note that BB does time stamp variables different from nb  ~Davsen */

typedef struct
{
   BBP_HEADER_TYPE 		header;
   BBP_CONFIGURATION_TYPE 	conf;
   BBP_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;
}  BBP_ENSEMBLE_TYPE;
