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

    FUNCTION: sscan_pos

              Scans for latitude and longitude from file buffer and
              converts these to double degrees.

    RETURNS:  1 if scan error occurs
              0 otherwise

    UPDATES:

    Fri  09-01-1989  from REFABSBT.C:
                     Fixed bug in reading HIG fix format, in
                     which west longitudes were not converted to negative
                     numbers.  Added testing for both lon and lat.decimal
                     point when reading fixes.

    Mon  02-27-1989  from REFABSBT.C
                     Modified the position reading algorithm
                     to expect the elements in fixed locations,
                     to avoid problems with spaces in the string.

*/
#include "common.h"
#include "use_adcp.h"  /* POSITION_TYPE */

#if PROTOTYPE_ALLOWED
int sscan_pos(char *buff, double *rlon, double *rlat)
#else
int sscan_pos(buff, rlon, rlat)
char *buff;
double *rlat, *rlon;
#endif
{
   POSITION_TYPE slat, slon;
   char piece[4];

/*  Problems with Turbo C 2.0 sscanf. (for reading mw8601.fix combined
    fix file).

   if (sscanf(buff+13, "%2d %2d %4lf %c %3d %2d %4lf %c ", 
       &slat.degree, &slat.minute, &slat.decimal, &slat.nsew, 
       &slon.degree, &slon.minute, &slon.decimal, &slon.nsew) != 8) return(1);
*/
   piece[2] = NULL;
   slat.degree = atoi(strncpy(piece, buff+14, 2));
   slat.minute = atoi(strncpy(piece, buff+17, 2));

   if (sscanf(buff+19, "%4lf%c", 
       &slat.decimal, &slat.nsew) != 2) return(1);

   piece[3] = NULL;
   slon.degree = atoi(strncpy(piece, buff+25, 3));
   piece[2] = NULL;
   slon.minute = atoi(strncpy(piece, buff+29, 2));

   if (sscanf(buff+31, "%4lf%c", 
       &slon.decimal, &slon.nsew) != 2) return(1);

   if (slat.decimal >= 1 || slon.decimal >= 1)   /* 89-02-27 fixed: changed > to >= */
   {
      slat.decimal /= 10000.0;
      slon.decimal /= 10000.0;
   }
   *rlat = slat.degree + (slat.minute + slat.decimal) / 60.0;
   *rlon = slon.degree + (slon.minute + slon.decimal) / 60.0;
   if (slat.nsew == 'S') *rlat = -*rlat;
   if (slon.nsew == 'W') *rlon = - *rlon;
   return(0);
}

