OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
lspline.c
Go to the documentation of this file.
1 /* -----------------------------------------------------------
2  simple linear interpolation (BAF, 6/99)
3 
4  Inputs:
5  float xin (nin ) - input independent variable
6  float yin (nin ) - input dependent variable
7  int32_t nin - number of input points
8  float xout(nout) - output independent variable
9  int32_t nout - number of output points
10 
11  Outputs:
12  float yin (nout) - output dependent variable
13 
14  Warning: xin, xout assumed to be ascending order
15 
16  ------------------------------------------------------------- */
17 
18 #include <stdint.h>
19 
20 void lspline(float xin [], float yin [], int32_t nin,
21  float xout[], float yout[], int32_t nout) {
22  float a, b, x;
23  int32_t i, j;
24 
25  /* Can't interpolate if only 1 input point */
26 
27  if (nin < 2) {
28  for (i = 0; i < nout; i++)
29  yout[i] = yin[0];
30  return;
31  }
32 
33  j = 0;
34 
35  for (i = 0; i < nout; i++) {
36 
37  while (xin[j] < xout[i]) {
38  j = j + 1;
39  if (j >= nin) {
40  j = nin - 1;
41  break;
42  }
43  }
44 
45  if (xout[i] == xin[j]) {
46  yout[i] = yin[j];
47  } else {
48  if (j < 1) j = 1;
49  a = (yin[j] - yin[j - 1]) / (xin[j] - xin[j - 1]);
50  b = yin[j - 1];
51  x = xout[i] - xin[j - 1];
52  yout[i] = a * x + b;
53  }
54  }
55 
56  return;
57 }
58 
59 float linterp(float xin [], float yin [], int32_t nin, float xout) {
60  float yout;
61  lspline(xin, yin, nin, &xout, &yout, 1);
62  return (yout);
63 }
64 
65 #ifdef LSPINE_TEST
66 
67 int main(int argc, char *argv[]) {
68  int i;
69  int32_t order = 2;
70  int32_t n = 5;
71 
72  float x1[5] = {1, 2, 3, 4, 5};
73  float y1[5] = {2.5, 4.5, 6.5, 8.5, 10.5};
74  float x2[9] = {-0.5, 0.5, 1.5, 1.9, 2.1, 3.5, 4.5, 5.0, 6.5};
75  float y2[9];
76 
77  float ylgint_(float x1[], float y1[], int32_t *n, float *x2, int32_t * order);
78 
79  lspline(x1, y1, 5, x2, y2, 8);
80 
81  for (i = 0; i < 7; i++)
82  printf("%d %f %f %f %f\n", i, x2[i], y2[i], linterp(x1, y1, n, x2[i]), ylgint_(x1, y1, &n, &x2[i], &order));
83 
84  exit(0);
85 }
86 #endif
int j
Definition: decode_rs.h:73
float linterp(float xin[], float yin[], int32_t nin, float xout)
Definition: lspline.c:59
data_t b[NROOTS+1]
Definition: decode_rs.h:77
int main(int argc, char *argv[])
Definition: afrt_nc4.cpp:30
int i
Definition: decode_rs.h:71
PGE01 indicating that PGE02 PGE01 V6 for and PGE01 V2 for MOD03 were used to produce the granule By convention adopted in all MODIS Terra PGE02 code versions are The fourth digit of the PGE02 version denotes the LUT version used to produce the granule The source of the metadata environment variable ProcessingCenter was changed from a QA LUT value to the Process Configuration A sign used in error in the second order term was changed to a
Definition: HISTORY.txt:424
void lspline(float xin[], float yin[], int32_t nin, float xout[], float yout[], int32_t nout)
Definition: lspline.c:20