/*

   FUNCTION:  is_jump()

   Here we detect speed jumps and place them just past the
   middle of the array of ref velocities.  n_refs should be
   odd, for example 13, in which case indices 0-6 should be
   before the jump and 7-12 would be after the jump.  The
   reason for having the larger number of ensembles before
   the jump is that the start time of the first ensemble is
   taken as the recorded time of the previous ensemble.  Hence
   we need to include one ensemble before the first one for
   which we need the velocity.

   CALLED BY:  TIMSLIP, ARRDEP

*/
#include <math.h>      /* sqrt() */
#ifndef cal_included
#include "cal.h"       /* sqr(), JUMP_UP, JUMP_DOWN */
#endif

#if PROTOTYPE_ALLOWED
int is_jump(VELOCITY_TYPE refs[], int n_refs, double up_thresh, double down_thresh)
#else
int is_jump(refs, n_refs, up_thresh, down_thresh)
VELOCITY_TYPE refs[];
int n_refs;
double up_thresh, down_thresh;
#endif
{
   static int up, down;
   double speed0, speed1;   /* first, last speeds in ref array */

   speed0 = sqrt(sqr(refs[0].u) + sqr(refs[0].v));
   speed1 = sqrt(sqr(refs[n_refs-1].u) + sqr(refs[n_refs-1].v));

   if ( (speed1 - speed0) > up_thresh )
      up++;
   else
      up = 0;

   if ( (speed0 - speed1) > down_thresh )
      down++;
   else
      down = 0;

   if (up == n_refs/2)        return(JUMP_UP);     /* speed increasing */
   else if (down == n_refs/2) return(JUMP_DOWN);    /* speed decreasing */
   else                       return(0);     /* no centered speed jump */
}                       /* is_jump() */

