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

   FUNCTIONS:

                      read_simple_fix
                      read_HIG_fix

      A simple fix consists of a line with time in decimal
      days, followed by longitude and latitude in decimal
      degrees.  Anything else on the line will be ignored.

      An HIG fix is in the format used at HIG for raw and
      processed fixes.  This format is almost the same for
      the various types of files for time and position.  The
      rest of the line depends on the type of file and is
      ignored here.

      Any line starting with '%' is ignored.

   PARAMETERS:

      fix = pointer to FIX_TYPE
      (fp_fix = pointer to fix file)

   RETURNS:  0 if valid fix line found;
             1 otherwise (end of file or error).

   CALLED BY:  REFABS, TIMSLIP, add_ref_piece()

   UPDATES:

   Mon  02-27-1989  from REFABSBT.C
                    Modified the reading of HIG fix file format
                    to expect the date and time in fixed locations,
                    to avoid problems with spaces in the time string.

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

#include "common.h"   /* FILE, atoi(), ... */
#include "jdtime.h"   /* JD_TIME_TYPE */
#include "cal.h"      /* FIX_TYPE */
#include "use_adcp.h"  /* sscan_pos() */


/* -- read_fix.c -- */
#if PROTOTYPE_ALLOWED
int (*read_fix_ptr)(FILE *fp_fix, FIX_TYPE *fix);
static int read_simple_fix(FILE *fp_fix, FIX_TYPE *fix);
static int read_HIG_fix(FILE *fp_fix, FIX_TYPE *fix);
#else
int (*read_fix_ptr)();
static int read_simple_fix();
static int read_HIG_fix();
#endif
   /*---------------------------------------------------------
      Order of elements must be coordinated between the following
      two arrays, since the second names the index into the first.
   */
   int (*read_fix_ptr_array[])()=
   {
      read_simple_fix,
      read_HIG_fix
   };
   NAME_LIST_ENTRY_TYPE fix_file_types[]=
   {
      {"simple",        0},
      {"HIG",           1},
      {NULL,            0}
   };

#if PROTOTYPE_ALLOWED
static int read_simple_fix(FILE *fp_fix, FIX_TYPE *fix)
#else
static int read_simple_fix(fp_fix, fix)
FILE *fp_fix;
FIX_TYPE *fix;
#endif
{
   char line[100];
   int nscan, nline;

   do
   {
      if ( (nline = getline_nc(fp_fix, line, 100)) == EOF ) return(1);
   } while (nline <= 10);

   nscan = sscanf(line, "%lf %lf %lf", &(fix->t), &(fix->x), &(fix->y));
   return( (nscan != 3) ?  1 : 0);
}

#if PROTOTYPE_ALLOWED
static int read_HIG_fix(FILE *fp_fix, FIX_TYPE *fix)
#else
static int read_HIG_fix(fp_fix, fix)
FILE *fp_fix;
FIX_TYPE *fix;
#endif
{
   char line[100], piece[4];
   JD_TIME_TYPE  j;
   int nline;
   extern int year_base;

   do
   {
      if ( (nline = getline_nc(fp_fix, line, 100)) == EOF ) return(1);
#ifdef REFABS /* accept only lines containing an asterisk */
   } while (nline <= 30 || strchr(line, '*') == NULL);
#else
#ifdef REFABSBT
   } while (nline <= 30 || strchr(line, '*') == NULL);
#else
   } while (nline <= 30);   /* used to be 10 (2/19/91) */
#endif
#endif

   /*
      In Turbo C 2.0, sscanf is not correctly handling format strings with no
      white space; it is adding whitespace, so that the proper
      character count is not being maintained.

   if (sscanf(line,"%2hd%4hd%3hd%2hd%2hd", &(j.year), &(j.julian_day),
                        &(j.hour), &(j.minute), &(j.second)) != 5)
   */
   piece[2] = NULL;
   j.year = atoi(strncpy(piece, line, 2));
   piece[3] = NULL;
   j.julian_day = atoi(strncpy(piece, line+3, 3));
   piece[2] = NULL;
   j.hour = atoi(strncpy(piece, line+7, 2));
   j.minute = atoi(strncpy(piece, line+9, 2));
   j.second = atoi(strncpy(piece, line+11, 2));

   fix->t = jd_to_ddtime(&j, year_base);
   return( sscan_pos(line, &(fix->x), &(fix->y)) );
}

