/*

    FUNCTION:  fix_to_velocity

    CALLED BY:  TIMSLIP, add_ref_piece()

    UPDATES:

    Fri  09-01-1989  from REFABSBT.C
                     Added correction for dateline crossing.

*/

#include <stdio.h>
#include "use_db.h"  
#include "pos_to_m.h" /* dlon_to_meters(), dlat_to_meters() */
#include "cal.h"     /* FIX_TYPE, VELOCITY_TYPE, SECONDS_PER_DAY */

#if PROTOTYPE_ALLOWED
int fix_to_velocity(FIX_TYPE *fix0, FIX_TYPE *fix1, VELOCITY_TYPE *ship)
#else
int fix_to_velocity(fix0, fix1, ship)
FIX_TYPE *fix0, *fix1;
VELOCITY_TYPE *ship;
#endif
{
   double dt, avg_lat, del_x;

   dt = (fix1->t - fix0->t) * SECONDS_PER_DAY;
   if (dt != 0)
   {
      avg_lat = 0.5 * (fix1->y + fix0->y);
      del_x = fix1->x - fix0->x;
      /* Adjust interval if the dateline was crossed. */
      if (del_x > 180.0)         del_x -= 360.0;
      else if (del_x < -180.0)   del_x += 360.0;

      ship->u = dlon_to_meters(del_x, avg_lat) / dt;
      ship->v = dlat_to_meters(fix1->y - fix0->y, avg_lat) / dt;
      return 0;
   }
   else
      return 1;   /* error */
}

