/**---------------------------------------------------------------------------
 ** 
 ** io_misc.c -- Plugin to io.c 
 ** 
 ** Author          : Pierre Jaccard
 ** Created On      : 1999/07/15 14:30:42
 ** Last Modified By: Pierre Jaccard
 ** Last Modified On: 1999/09/22 15:01:41
 ** Update Count    : 11
 ** Directory       : /pcdata1/jaccard/codas3c/gfi/src/libs/nav/
 ** Version         : 0.0
 ** Status          : Unknown
 ** ---------------------------------------------------------------------- ** 
 ** DESCRIPTION: 
 ** 
 **    These utilities are included in the main io navigation utilities. This
 **    file contains miscellaneous functions.
 ** 
 ** ---------------------------------------------------------------------- ** 
 ** REVISIONS: 
 ** ---------------------------------------------------------------------- ** 
 ** CHANGES: 
 **
 ** 1999/09/03 Pierre Jaccard
 **            Addding support for RMC message
 ** 1999/09/04 Pierre Jaccard
 **            Addding support for GLL message
 **------------------------------------------------------------------------**/

#ifndef gfilib_nav_io_misc_already_included
#define gfilib_nav_io_misc_already_included

/* ------------------------------------------------------------------      
	 Inquire if current input line is a defined navigation line. Checking is
	 done by testing the presence of the NMEA string at the beginning of the
	 line. Therefore, no checks are carried out to test the line's integrity.

	 RETURNS: pointer to beginning of first navigation field, just after the NMEA
	         string, or NULL if line is not expected to be of the expected
           code. An expeption is done if the line contains TRANSECT
           information: the returned value is the pointer to the line string
           itself. 
   ------------------------------------------------------------------ */
#if PROTOTYPE_ALLOWED
char *is_nav_line(/* ASCII nav string */
                  char *line, 
                  /* The NMEA message to check */
                  int code)
#else
     char *is_nav_line(line, code)
     char *line;
     int code;
#endif
{

	char *ptr, str[15];

	/* Check the line according to code's value */
	switch(code){
	case TRANSECT_LINE_CODE:
		strcpy(str, "ENSEMBLE");
		ptr = strstr(line, str);
		return( (ptr) ? line : (char *) NULL);
		break;
	case GGA_LINE_CODE:
		strcpy(str, "GGA,");
		ptr = strstr(line, str);
		break;
	case RMC_LINE_CODE:
		strcpy(str, "RMC,");
		ptr = strstr(line, str);
		break;
	case GLL_LINE_CODE:
		strcpy(str, "GLL,");
		ptr = strstr(line, str);
		break;
	case VTG_LINE_CODE:
		strcpy(str, "VTG,");
		ptr = strstr(line, str);
		break;
	case ZDA_LINE_CODE:
		strcpy(str, "ZDA,");
		ptr = strstr(line, str);
		break;
	case HDT_LINE_CODE:
		strcpy(str, "HDT,");
		ptr = strstr(line, str);
		break;
	case PRDID_LINE_CODE:
		strcpy(str, "PRDID,");
		ptr = strstr(line, str);
		break;
	default:
		ptr = (char *) NULL;
		break;
	}

	/* Default return value: pointer to the first NMEA field after the
		 NMEA string */
	return( (ptr) ? ptr+strlen(str) : (char *) NULL);
		
}

/* ------------------------------------------------------------------
	 Since navigation data can be contained in different NMEA messges, it is
	 best to centralize it in the [[NAV_TYPE]] structure before further
	 processing is carried out. The value of [[flag]] must correspond to the
	 current navigation line code. If [[inc]] is positive, then the time is
	 incremented from the value of [[last]], unless the current line contains
	 time information. The value of [[inc]] is also saved in the navigation
	 structure. However, if no valid time is defined in the structure, and the
	 current line does not contain time information, the navigation time is set
	 to [[last]].

   RETURNS: 0
	 ------------------------------------------------------------------ */
#if PROTOTYPE_ALLOWED
int fill_navigation(/* Structure combining all navigation information */
                    NAV_TYPE *nav, 
                    /* Structure containing information from the different
                       NMEA messages */
                    NAV_LINES_TYPE *lines, 
                    /* NMEA type to consider */
                    int flag,
										/* Last time */
                    double last, 
                    /* Time increment in decimal days, or 0 if time should not 
                       be incremented */
                    double inc)
#else
int fill_navigation(nav, lines, flag, last, inc)
#endif
{

	nav->inc = inc;

	switch(flag){
  case GLL_LINE_CODE:
		nav->pos.lat = lines->gll.pos.lat;
		nav->pos.lon = lines->gll.pos.lon;
    break;
  case RMC_LINE_CODE:
    nav->yd = lines->rmc.yd;
		nav->pos.lat = lines->rmc.pos.lat;
		nav->pos.lon = lines->rmc.pos.lon;
		nav->course = lines->rmc.course;
		nav->speed = lines->rmc.speed;
    break;
	case GGA_LINE_CODE:
		nav->yd = lines->gga.yd;
		nav->pos.lat = lines->gga.pos.lat;
		nav->pos.lon = lines->gga.pos.lon;
		nav->qual = lines->gga.qual;
		nav->nsat = lines->gga.nsat;
		nav->hdop = lines->gga.hdop;
		nav->height = lines->gga.height;
		break;
	case ZDA_LINE_CODE:
		nav->yd = lines->zda.yd;
		break;
	case HDT_LINE_CODE:
		nav->heading = lines->hdt.heading;
		if(inc > 0.0)
			nav->yd = last + nav->inc;
		break;
	case VTG_LINE_CODE:
		nav->course = lines->vtg.course;
		nav->speed = lines->vtg.speed;
		if(inc > 0.0)
			nav->yd = last + nav->inc;
		break;
	case PRDID_LINE_CODE:
		nav->heading = lines->prdid.heading;
		nav->pitch = lines->prdid.pitch;
		nav->roll = lines->prdid.roll;
		if(inc > 0.0)
			nav->yd = last + nav->inc;
		break;
	case TRANSECT_LINE_CODE:
		nav->number = lines->transect.ensemble_number;
		if(inc > 0.0)
			nav->yd = last + nav->inc;
		break;
	default:
		/* Nothing to do */
		break;
	}

	/* Set the time */
	if(!(nav->yd < ADJ_BADFLOAT))
		nav->yd = last;

	return(0);

}

/* ------------------------------------------------------------------
	 If [[flag]] is negative, put bad values in all variables. Else, [[flag]]
	 must correspond to a navigation line code, and only the specific fields to
	 this code are initialized. If [[flag]] is [[MAXINT]], then set the time
	 increment to bad.

   RETURNS: 0
	 ------------------------------------------------------------------ */
#if PROTOTYPE_ALLOWED
int init_navigation(/* Pointer to navigation structure */
                    NAV_TYPE *nav, 
                    /* Navigation code, or negative to initialize all
                       fields. If MAXINT, set time increment to bad */
                    int flag)
#else
int init_navigation(nav)
#endif
{

	if((flag == TRANSECT_LINE_CODE) || (flag < 0)){
		nav->number = BADUSHORT;
	}
	if((flag == GGA_LINE_CODE) || (flag < 0)){
		nav->yd = BADDOUBLE;
		nav->pos.lat = BADDOUBLE;
		nav->pos.lon = BADDOUBLE;
		nav->qual = BADUSHORT;
		nav->nsat = BADUSHORT;
		nav->hdop = BADDOUBLE;
		nav->height = BADDOUBLE;
	}
	if((flag == ZDA_LINE_CODE) || (flag < 0)){
		nav->yd = BADDOUBLE;
	}
	if((flag == VTG_LINE_CODE) || (flag < 0)){
		nav->course = BADDOUBLE;
		nav->speed = BADDOUBLE;
	}
	if((flag == PRDID_LINE_CODE) || (flag < 0)){
		nav->heading = BADDOUBLE;
		nav->pitch = BADDOUBLE;
		nav->roll = BADDOUBLE;
	}
	if((flag == HDT_LINE_CODE) || (flag < 0)){
		nav->heading = BADDOUBLE;
	}

	/* Special: time increment */
	if((flag == MAXINT) || (flag < 0)){
		nav->inc = BADDOUBLE;
	}

	return(0);

}

#endif /* gfilib_nav_io_misc_already_included */
