IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Mar 9, 2026, 11:26:15 AM (4 months ago)
Author:
eugene
Message:

add spline options to extrapolate or saturate at end points; add vweave function to interpolate a vector with varying bin sizes

File:
1 copied

Legend:

Unmodified
Added
Removed
  • trunk/Ohana/src/opihi/cmd.data/vweave.c

    r42946 r42967  
    66 
    77  int i, j, N;
    8   Vector *xin, *yin, *xout, *yout;
     8  Vector *xin, *yin, *yout;
     9  Vector *xminV = NULL, *xmaxV = NULL, *xout = NULL;
    910  opihi_flt xmin, xmax, Xmin, Xmax;
    1011
     
    2223  }
    2324
    24   if (argc != 5) {
     25  if ((argc != 5) && (argc != 6)) {
    2526    gprint (GP_ERR, "USAGE: vweave <xin> <yin> <xout> <yout>\n");
     27    gprint (GP_ERR, "   OR: vweave <xin> <yin> <xmin> <xmax> <yout>\n");
    2628    gprint (GP_ERR, " fully interpolate input bin values into output bins, using fractional input bin contributions\n");
    2729    gprint (GP_ERR, " by default, yout has the sum of the associated input values\n");
    2830    gprint (GP_ERR, " use -count to find the fractional input bin contributions\n");
    29     gprint (GP_ERR, " output bin boundaries defined as midpoint of xout values\n");
     31    gprint (GP_ERR, " with a single <xout> vector, output bin boundaries defined as midpoint of xout values\n");
     32    gprint (GP_ERR, " with <xmin> and <xmax>, the output bin boundaries are explicitly provided\n");
    3033    gprint (GP_ERR, " use -binsize to specify a fixed bin width (<xout> will define the bin center)\n");
    3134    gprint (GP_ERR, " input values are treated as uniformly spread between midpoints of xin values\n");
     
    3841    return FALSE;
    3942  }
    40   if ((xout = SelectVector (argv[3], OLDVECTOR, TRUE)) == NULL) return (FALSE);
    41   if (xout->Nelements < 2) {
    42     gprint (GP_ERR, "output vector of length < 2 is invalid\n");
    43     return FALSE;
     43  if ((yin  = SelectVector (argv[2], OLDVECTOR, TRUE)) == NULL) return (FALSE);
     44
     45  int Nout = -1;
     46  if (argc == 5) {
     47    if ((xout = SelectVector (argv[3], OLDVECTOR, TRUE)) == NULL) return (FALSE);
     48    if (xout->Nelements < 2) {
     49      gprint (GP_ERR, "output vector of length < 2 is invalid\n");
     50      return FALSE;
     51    }
     52    Nout = xout->Nelements;
     53    if ((yout = SelectVector (argv[4], ANYVECTOR, TRUE)) == NULL) return (FALSE);
     54  } else {
     55    if (!isnan(binsize)) {
     56      gprint (GP_ERR, "-binsize is incompatible with <xmin>, <xmax>\n");
     57      return FALSE;
     58    }
     59    if ((xminV = SelectVector (argv[3], OLDVECTOR, TRUE)) == NULL) return (FALSE);
     60    if (xminV->Nelements < 2) {
     61      gprint (GP_ERR, "output vector of length < 2 is invalid\n");
     62      return FALSE;
     63    }
     64    Nout = xminV->Nelements;
     65    if ((xmaxV = SelectVector (argv[4], OLDVECTOR, TRUE)) == NULL) return (FALSE);
     66    if (xmaxV->Nelements != Nout) {
     67      gprint (GP_ERR, "<xmin> and <xmax> lengths must match\n");
     68      return FALSE;
     69    }
     70    if ((yout = SelectVector (argv[5], ANYVECTOR, TRUE)) == NULL) return (FALSE);
    4471  }
    4572
    46   if ((yin  = SelectVector (argv[2], OLDVECTOR, TRUE)) == NULL) return (FALSE);
    47   if ((yout = SelectVector (argv[4], ANYVECTOR, TRUE)) == NULL) return (FALSE);
    48 
    4973  // re-binning creates a float vector
    50   ResetVector (yout, OPIHI_FLT, xout[0].Nelements);
     74  ResetVector (yout, OPIHI_FLT, Nout);
    5175
    5276  // XXX this algorithm is not optimized: full scale of input vector for each output bin
    5377  // could speed up with sorting and pre-defined boundaries
    54   for (i = 0; i < xout[0].Nelements; i++) {
     78  for (i = 0; i < Nout; i++) {
    5579    if (!isnan(binsize)) {
    5680      xmin = xout[0].elements.Flt[i] - 0.5*binsize;
    5781      xmax = xout[0].elements.Flt[i] + 0.5*binsize;
    5882    } else {
    59       if (i > 0) {
    60         xmin = 0.5*(xout[0].elements.Flt[i] + xout[0].elements.Flt[i-1]);
     83      if (xout) {
     84        if (i > 0) {
     85          xmin = 0.5*(xout[0].elements.Flt[i] + xout[0].elements.Flt[i-1]);
     86        } else {
     87          xmin = 1.5*xout[0].elements.Flt[i] - 0.5*xout[0].elements.Flt[i+1];
     88        }
     89        if (i < xout[0].Nelements - 1) {
     90          xmax = 0.5*(xout[0].elements.Flt[i] + xout[0].elements.Flt[i+1]);
     91        } else {
     92          xmax = 1.5*xout[0].elements.Flt[i] - 0.5*xout[0].elements.Flt[i-1];
     93        }
    6194      } else {
    62         xmin = 1.5*xout[0].elements.Flt[i] + 0.5*xout[0].elements.Flt[i+1];
    63       }
    64       if (i < xout[0].Nelements - 1) {
    65         xmax = 0.5*(xout[0].elements.Flt[i] + xout[0].elements.Flt[i+1]);
    66       } else {
    67         xmax = 1.5*xout[0].elements.Flt[i] - 0.5*xout[0].elements.Flt[i-1];
     95        xmin = xminV[0].elements.Flt[i];
     96        xmax = xmaxV[0].elements.Flt[i];
    6897      }
    6998    }
Note: See TracChangeset for help on using the changeset viewer.