/**---------------------------------------------------------------------------
 ** 
 ** nav_.h -- Main module for reading NMEA navigation data files. 
 ** 
 ** Author          : Pierre Jaccard
 ** Created On      : 1999/07/30 16:03:12
 ** Last Modified By: Pierre Jaccard
 ** Last Modified On: 1999/09/04 17:00:33
 ** Update Count    : 16
 ** Directory       : /home/pego/pcd1/codas3c/gfi/src/apps/nav/
 ** Version         : 0.0
 ** Status          : Unknown
 ** ---------------------------------------------------------------------- ** 
 ** DESCRIPTION: 
 ** 
 **    Must be included in the main file.
 ** 
 ** ---------------------------------------------------------------------- ** 
 ** REVISIONS: 
 ** ---------------------------------------------------------------------- ** 
 ** CHANGES: 
 ** 
 ** 1999/09/03 Pierre Jaccard
 **            Adding support for RMC messages.
 ** 1999/09/04 Pierre Jaccard
 **            Adding support for GLL messages.
 **------------------------------------------------------------------------**/

#ifndef nav__already_included
#define nav__already_included

#include "geninc.h"
#include "ioserv.h"
#include "dbsource.h"
#include "use_db.h"
#include "vstat.h"
#include "gfimisc.h"
#include "gfimath.h"
#include "gfinav.h"

/* ##################################################################
	 -Control File Variables:
	 ################################################################## */

/* ------------------------------------------------------------------
	 Below is a list of definitions corresponding to the bit numbers in the
	 different options bits. They can be used in the code in order to make it
	 more transparent.
	 ------------------------------------------------------------------ */

#define SKIP_NEXT_LINES           -1000  

#define TIME_BETWEEN_ENSEMBLE_CODE    0 
#define TIME_BETWEEN_NAV_PING_CODE    1
#define TIME_INCREMENT_CODE           2
#define NAV_TRAVELLED_DISTANCE_CODE   3
#define NAV_SPEED_FROM_POS_CODE       4
#define NAV_COURSE_FROM_POS_CODE      5
#define NAV_HEADING_CODE              6
#define NAV_ROLL_CODE                 7
#define NAV_PITCH_CODE                8
#define SATELLITE_NUMBER_CODE         9
#define SATELLITE_HDOP_CODE          10
#define SATELLITE_HEIGHT_CODE        11
#define NAV_SHIP_COURSE_CODE         12
#define NAV_SHIP_SPEED_CODE          13

#define GPS_CODE                     24
#define DGPS_CODE                    25

/* ------------------------------------------------------------------
	 The same, but long bits
	 ------------------------------------------------------------------ */

#define TIME_BETWEEN_ENSEMBLE_BIT  ((ULONG) BIT0)
#define TIME_BETWEEN_NAV_PING_BIT  ((ULONG) BIT1)
#define TIME_INCREMENT_BIT         ((ULONG) BIT2)
#define NAV_TRAVELLED_DISTANCE_BIT ((ULONG) BIT3)
#define NAV_SPEED_FROM_POS_BIT     ((ULONG) BIT4)
#define NAV_COURSE_FROM_POS_BIT    ((ULONG) BIT5)
#define NAV_HEADING_BIT            ((ULONG) BIT6)
#define NAV_ROLL_BIT               ((ULONG) BIT7)
#define NAV_PITCH_BIT              ((ULONG) BIT8)
#define SATELLITE_NUMBER_BIT       ((ULONG) BIT9)
#define SATELLITE_HDOP_BIT         ((ULONG) BIT10)
#define SATELLITE_HEIGHT_BIT       ((ULONG) BIT11)
#define NAV_SHIP_COURSE_BIT        ((ULONG) BIT12)
#define NAV_SHIP_SPEED_BIT         ((ULONG) BIT13)
#define ALL_DATA_STAT_BIT          ((ULONG) \
																		BIT0  | BIT1  | BIT2  | BIT3  | \
																		BIT4  | BIT5  | BIT6  | BIT7  | \
																		BIT8  | BIT9  | BIT10 | BIT11 | \
																		BIT12 | BIT13)
																		
#define GPS_BIT                    ((ULONG) BIT24)
#define DGPS_BIT                   ((ULONG) BIT25)
#define ALL_FLAGS_BIT              ((ULONG) BIT24 | BIT25)

#define ALL_NAV_MESSAGES_BIT       ((ULONG) \
																		BIT0  | BIT1  | BIT2  | BIT3  | \
																		BIT4  | BIT5)

#define ASCII_LOG_BIT              ((ULONG) BIT0)

/* ------------------------------------------------------------------
	 --Parameters:

	 In addition to the file names, the following parameters are required:

	    +first_day_str : not all navigation lines do contain full information
			                 about time. Therefore, it is necessary to know the date
											 measurments began.
	 ------------------------------------------------------------------ */

static char first_day_str[15];
static int year_base;
static YMDHMS_TIME_TYPE first_day;
static FILE_NAME_TYPE file_list;
static FILE_NAME_TYPE out_file;

static PARAMETER_LIST_ENTRY_TYPE scan_params[]=
{
	{" first_day:", "%s", first_day_str, TYPE_STRING},
	{" file_list:", "%79s", file_list, TYPE_STRING},
	{" out_file:", "%79s", out_file, TYPE_STRING},
	{NULL, NULL, NULL, 0}
};

/* ------------------------------------------------------------------
	 --Options:

	 DATA STATISTICS:

	 These options are stored as bits in [[data_stat_bits]] from BIT 0 to BIT
	 23. The meaning of each bit is the following:

	    BIT  0 : time_between_ensemble
			BIT  1 : time_between_nav_ping
			BIT  2 : time_increment
			BIT  3 : nav_travelled_distance
			BIT  4 : nav_speed_from_pos
			BIT  5 : nav_course_from_pos
			BIT  6 : nav_heading
			BIT  7 : nav_roll
			BIT  8 : nav_pitch
			BIT  9 : satellite_number
			BIT 10 : satellite_hdop
			BIT 11 : satellite_height
			BIT 12 : nav_ship_course
			BIT 13 : nav_ship_speed

			BIT 23 : all of the above

	 DATA FLAGS:

	    These are also stored in [[data_stat_bits]], from BIT24. The meaning of
			each bit is the following:

			BIT 24 : gps
			BIT 25 : dgps
			
			BIT 31 : all flags

	 NAVIGATION MESSAGES:

	    By sellecting specific navigation messages from the navigation files,
			scanning of these will filter out all other messages. The values of
			these bits correspond to the navigation line codes defined in
			[[misc_nav.h]] (see this file for the most updated information):

			BIT  0 : TRANSECT
			BIT  1 : ZDA
			BIT  2 : GGA
			BIT  3 : HDT
			BIT  4 : PRDID
			BIT  5 : VTG
			BIT  6 : RMC
			BIT  7 : GLL
			
			BIT 31 : all of the above

	 SCAN_BITS:

	    These bits are used to control the behavior of scanning. Possible bits
	    are:

			   BIT0 : also output ascii log data in log file.

	 ------------------------------------------------------------------ */

static int skip_next_lines=10;
static FILE_NAME_TYPE nav_file;
static ULONG data_stat_bits=0;
static ULONG nav_lines_bits=0;
static ULONG scan_bits=0;

static OPTION_TYPE data_stat_options[]=
{
	{"end", 0, NULL, (char *) NULL},
	{"time_between_ensemble",  0, set_long_bit, (char *) &data_stat_bits},
	{"time_between_nav_ping",  1, set_long_bit, (char *) &data_stat_bits},
	{"time_increment",         2, set_long_bit, (char *) &data_stat_bits},
	{"nav_travelled_distance", 3, set_long_bit, (char *) &data_stat_bits},
	{"nav_speed_from_pos",     4, set_long_bit, (char *) &data_stat_bits},
	{"nav_course_from_pos",		 5, set_long_bit, (char *) &data_stat_bits},
	{"nav_heading",					   6, set_long_bit, (char *) &data_stat_bits},
	{"nav_roll",					     7, set_long_bit, (char *) &data_stat_bits},
	{"nav_pitch",					     8, set_long_bit, (char *) &data_stat_bits},
	{"satellite_number",			 9, set_long_bit, (char *) &data_stat_bits},
	{"satellite_hdop",        10, set_long_bit, (char *) &data_stat_bits},
	{"satellite_height",      11, set_long_bit, (char *) &data_stat_bits},
	{"nav_ship_course",       12, set_long_bit, (char *) &data_stat_bits},
	{"nav_ship_speed",	      13, set_long_bit, (char *) &data_stat_bits},
	/* ---------------------------------
		 Annulating options
		 --------------------------------- */
	{"!time_between_ensemble",  100, set_long_bit, (char *) &data_stat_bits},
	{"!time_between_nav_ping",  101, set_long_bit, (char *) &data_stat_bits},
	{"!time_increment",         102, set_long_bit, (char *) &data_stat_bits},
	{"!nav_travelled_distance", 103, set_long_bit, (char *) &data_stat_bits},
	{"!nav_speed_from_pos",     104, set_long_bit, (char *) &data_stat_bits},
	{"!nav_course_from_pos",		105, set_long_bit, (char *) &data_stat_bits},
	{"!nav_heading",					  106, set_long_bit, (char *) &data_stat_bits},
	{"!nav_roll",					      107, set_long_bit, (char *) &data_stat_bits},
	{"!nav_pitch",					    108, set_long_bit, (char *) &data_stat_bits},
	{"!satellite_number",			  109, set_long_bit, (char *) &data_stat_bits},
	{"!satellite_hdop",         110, set_long_bit, (char *) &data_stat_bits},
	{"!satellite_height",       111, set_long_bit, (char *) &data_stat_bits},
	{"!nav_ship_course",        112, set_long_bit, (char *) &data_stat_bits},
	{"!nav_ship_speed",	        113, set_long_bit, (char *) &data_stat_bits},
	/* ---------------------------------
		 To select all
		 --------------------------------- */
	{"ALL", (int) ALL_DATA_STAT_BIT, logical_or, (char *) &data_stat_bits},
	{NULL, 0, NULL, (char *) NULL}
};

static OPTION_TYPE nav_messages_options[]=
{
	{"end",      0, NULL, (char *) NULL},
	{"TRANSECT", 0, set_long_bit, (char *) &nav_lines_bits},
	{"ZDA",      1, set_long_bit, (char *) &nav_lines_bits},
	{"GGA",      2, set_long_bit, (char *) &nav_lines_bits},
	{"HDT",      3, set_long_bit, (char *) &nav_lines_bits},
	{"PRDID",    4, set_long_bit, (char *) &nav_lines_bits},
	{"VTG",      5, set_long_bit, (char *) &nav_lines_bits},
	{"RMC",      6, set_long_bit, (char *) &nav_lines_bits},
	{"GLL",      7, set_long_bit, (char *) &nav_lines_bits},
	/* ---------------------------------
		 Annulating options:
		 --------------------------------- */
	{"!TRANSECT", 100, set_long_bit, (char *) &nav_lines_bits},
	{"!ZDA",      101, set_long_bit, (char *) &nav_lines_bits},
	{"!GGA",      102, set_long_bit, (char *) &nav_lines_bits},
	{"!HDT",      103, set_long_bit, (char *) &nav_lines_bits},
	{"!PRDID",    104, set_long_bit, (char *) &nav_lines_bits},
	{"!VTG",      105, set_long_bit, (char *) &nav_lines_bits},
	{"!RMC",      106, set_long_bit, (char *) &nav_lines_bits},
	{"!GLL",      107, set_long_bit, (char *) &nav_lines_bits},
	/* ---------------------------------
		 To select all
		 --------------------------------- */
	{"ALL", (int) ALL_NAV_MESSAGES_BIT, logical_or, (char *) &nav_lines_bits},
	{NULL, 0, NULL, (char *) NULL}
};

static OPTION_TYPE data_flag_options[]=
{
	{"end", 0, NULL, (char *) NULL},
	{"gps",	   24, set_long_bit, (char *) &data_stat_bits},
	{"dgps",   25, set_long_bit, (char *) &data_stat_bits},
	/* ---------------------------------
		 Annulating options:
		 --------------------------------- */
	{"!gps",	   124, set_long_bit, (char *) &data_stat_bits},
	{"!dgps",    125, set_long_bit, (char *) &data_stat_bits},
	/* ---------------------------------
		 To select all
		 --------------------------------- */
	{"ALL", (int) ALL_FLAGS_BIT, logical_or, (char *) &data_stat_bits},
	{NULL, 0, NULL, (char *) NULL}
};

static OPTION_TYPE scan_options[]=
{
	{"end",                                    0, NULL, (char *) NULL},
	{"OPTIONS:",                               0, NULL, (char *) NULL},
	{"data_stat_options:",                     1, execute_sub_options, 
	 (char *) data_stat_options},
	{"data_flag_options:",                     1, execute_sub_options,
	 (char *) data_flag_options},
	{"nav_messages:",                          1, execute_sub_options,
	 (char *) nav_messages_options},
	{"scan_nav_file:",               TYPE_STRING, op_get_param, 
	 (char *) &nav_file},
	{"ascii_log",                              0, set_long_bit, 
	 (char *) &scan_bits },
	{"skip_nav_lines:",                 TYPE_INT, op_get_param, 
	 (char *) &skip_next_lines},
	{NULL, 0, NULL, (char *) NULL}
};

/* ##################################################################
	 -Data Statistics:

	 Next initialization is an unistat list for the data to calculate the
	 statistics from. The [[code]] value corresponds to the bit number in the
	 option list. Another unistat list is defined for statistics that has to be
	 applied for each ensemble during scanning.
	 ################################################################## */

static UNISTAT_LIST_TYPE data_stat_list[]=
{
	{"time_between_ensemble (s)",   "%8.2lf", 1, U_ALL,  0, NULL, NULL},
	{"time_between_nav_ping (s)",   "%8.2lf", 1, U_ALL,  1, NULL, NULL},
	{"time_increment (s)",          "%8.5lf", 1, U_ALL,  2, NULL, NULL},
	{"nav_travelled_distance (m)",  "%8.2lf", 1, U_ALL,  3, NULL, NULL},
	{"nav_speed_from_pos (m/s)",    "%8.2lf", 1, U_ALL,  4, NULL, NULL},
	{"nav_course_from_pos [HDG]",   "%8.2lf", 1, U_ALL,  5, NULL, NULL},
	{"nav_heading [HDG]",					  "%8.2lf", 1, U_ALL,  6, NULL, NULL},
	{"nav_roll",					          "%8.2lf", 1, U_ALL,  7, NULL, NULL},
	{"nav_pitch",					          "%8.2lf", 1, U_ALL,  8, NULL, NULL},
	{"satellite_number",				    "%8.2lf", 1, U_ALL,  9, NULL, NULL},
	{"satellite_hdop",              "%8.2lf", 1, U_ALL, 10, NULL, NULL},
	{"satellite_height",            "%8.2lf", 1, U_ALL, 11, NULL, NULL},
	{"nav_ship_course [HDG]",				"%8.2lf", 1, U_ALL, 12, NULL, NULL},
	{"nav_ship_speed (m/s)",				"%8.2lf", 1, U_ALL, 13, NULL, NULL},
	{(char *) NULL, (char *) NULL, 0, 0, 0, NULL, NULL}
};

static UNISTAT_LIST_TYPE scan_stat_list[]=
{
	{"heading",					     "%8.2lf", 1, U_ALL,  6, NULL, NULL},
	{"roll",					       "%8.2lf", 1, U_ALL,  7, NULL, NULL},
	{"pitch",					       "%8.2lf", 1, U_ALL,  8, NULL, NULL},
	{"course",					     "%8.2lf", 1, U_ALL, 12, NULL, NULL},
	{"speed (m/s)",				   "%8.2lf", 1, U_ALL, 13, NULL, NULL},
	{(char *) NULL, (char *) NULL, 0, 0, 0, NULL, NULL}
};
	
/* ------------------------------------------------------------------
	 Similarly, follows a [[FLAG_LIST_TYPE]] for the different flaggs.
	 [[BADBYTE]] flag values mean [[undefined_flag]], zero means [[bad_value]],
	 and flags are set with a value of one. 

	 The only exception is for the [[NAV_LINE_FLAG]] flag that contains
	 information about the navigation line currently read from the file. Its
	 values are the same as the navigation line codes defined in [[misc_nav.h]]
	 (see this file for the most updated information):
	 
	    0  : TRANSECT
			1  : ZDA
			2  : GGA
			3  : HDT
			4  : PRDID
			5  : VTG
			6  : RMC
			7  : GLL

	 In addition, this flag can also have the following values:

	  127  : bad navigation line
		126  : empty navigation line
		125  : no navigation data for transect corrections

	 The [[TRANSECT_FLAG]] has several dimensions, each of these having the
	 following meaning:

	  0 : apply transect corrections:
		       
		       -2 : transect could not be corrected
           -1 : Prepare for transect corrections
					  0 : unused
						1 : calculate transect corrections
						2 : transect corrected

		1 : available time information for transect

           -3 : no info before and after
           -2 : no info after
		       -1 : no info before
            0 : unused
		        1 : info before transect available
						2 : info after transect available
            3 : info before and after transect available
						
		3 : available position information for transect

           -3 : no info before and after
           -2 : no info after
		       -1 : no info before
            0 : unused
		        1 : info before transect available
						2 : info after transect available
            3 : info before and after transect available

	 Other flags are set by [[update_flags]] and give information about
	 the data contained in the last navigation line.

	 The positive flags are those used for log information.
	 ------------------------------------------------------------------ */

#define NAV_LINE_FLAG                    -1
#define ENSEMBLE_DONE_FLAG               -2 
#define TIME_BEFORE_TRANSECT_FLAG        -3
#define TIME_AFTER_TRANSECT_FLAG         -4
#define POS_BEFORE_TRANSECT_FLAG         -5
#define POS_AFTER_TRANSECT_FLAG          -6
#define TRANSECT_CORRECTED_FLAG          -7
#define CORRECT_TRANSECT_FLAG            -8
#define APPLY_TRANSECT_CORRECTIONS_FLAG  -9
#define NAV_TIME_FLAG                  -100
#define NAV_POSITION_FLAG              -101
#define NAV_HEADING_FLAG               -102
#define NAV_ROLL_FLAG                  -103
#define NAV_PITCH_FLAG                 -104
#define NAV_COURSE_FLAG                -105
#define NAV_SPEED_FLAG                 -106
#define NAV_SATELLITE_FLAG             -107
#define NAV_PRECISION_FLAG             -108

static FLAG_LIST_TYPE flag_list[]=
{
	{"nav_line",                  1, NULL,    -1},
	{"ensemble_done",             1, NULL,    -2},
	{"time_before_transect",      1, NULL,    -3},
	{"time_after_transect",       1, NULL,    -4},
	{"pos_before_transect",       1, NULL,    -5},
	{"pos_after_transect",        1, NULL,    -6},
	{"transect_corrected",        1, NULL,    -7},
	{"correct_transect",          1, NULL,    -8},
	{"apply_transect_corrections",1, NULL,    -9},
	{"nav_time",                  1, NULL,  -100},
	{"nav_position",              1, NULL,  -101},
	{"nav_heading",               1, NULL,  -102},
	{"nav_roll",                  1, NULL,  -103},
	{"nav_pitch",                 1, NULL,  -104},
	{"nav_course",                1, NULL,  -105},
	{"nav_speed",                 1, NULL,  -106},
	{"nav_satellite",             1, NULL,  -107},
	{"nav_precision",             1, NULL,  -108},
	{"gps",                       1, NULL,    24},
	{"dgps",                      1, NULL,    25},
	{NULL, 0, 0, 0}
};

/* ##################################################################
	 -SCAN Data Type:

	 To facilitate argument passing between functions, the following structures
	 are defined:
	 ################################################################## */

typedef struct
{
	DATA_FILE_INFO_TYPE  fi;
	NAV_TYPE             nav;
	NAV_TYPE             nav0;
	SCAN_NAV_LINE_TYPE   scan;
	UNISTAT_LIST_TYPE    *data_stat_list;
	UNISTAT_LIST_TYPE    *data_stat_list0;
	UNISTAT_LIST_TYPE    *scan_stat_list;
	UNISTAT_LIST_TYPE    *scan_stat_list0;
	FLAG_LIST_TYPE       *flag_list;
} SCAN_NAV_TYPE;

/* ------------------------------------------------------------------
	 The next structure groups all kind of files related to navigation files
	 ------------------------------------------------------------------ */

typedef struct
{
	FILE *lst;
	FILE *log;
	FILE *bin;
	FILE *sta;
	FILE *out;
	FILE *nav;
} NAV_FILES_TYPE;


#endif /* nav__already_included */
