/**************************************************************

   S_REGRLI.C

   This function regrids an array with or without gaps,
   leaving BADFLOAT in the gaps. There is still no checking
   for monotonicity of either grid, or for the same sense of
   change in both.

   The function returns the number of good points in the new array.

   The function of changing decreasing into increasing
   grids could be done in a modified version of regrid()
   itself.

*/
#include "common.h"
#include "vector.h"

/* Note: in the variables, a second character of 'o' means
   "old", and 'n' means "new", that is, the regridded version.
*/

#if PROTOTYPE_ALLOWED
int s_regridli(float *zo, float *fo, float *zn, float *fn, int no, int nn)
#else
int s_regridli(zo, fo, zn, fn, no, nn)
float *zo, *fo, *zn, *fn;
int no, nn;
#endif
{
   int i;
   int in0 = 0, inm;
   int io0, io1;
   int n0, nout;
   int ngood = 0;

/* Find the valid range of zn.  It is the first range with no gaps. */

   while ((in0 < nn) && !good_float(zn[in0]))   in0++;
   inm = in0;
   while ((inm < nn) && good_float(zn[inm]))   inm++;


/* Initialize fn as bad everywhere. */

   for (i=0; i<nn; i++)  fn[i] = BADFLOAT;

/* Find each good range in fo, and regrid it.*/

   io0 = 0;
   while (io0 < no)
   {
      while ((io0 < no) && (!good_float(fo[io0]))) io0++;
      /* Now io0 is no or the beginning of a good section. */

      io1 = io0;
      while ((io1 < no) && (good_float(fo[io1]))) io1++;
      /* Now io1 is no or the beginning of a bad section. */
      /* If either is n, both should be n */

      if (io1-io0 > 1)
      {
         regridli(zo+io0, fo+io0, zn+in0, fn+in0, io1-io0, inm-in0, &n0, &nout);
         ngood += nout;
      }

      io0 = io1;
   }

   return (ngood);
}

