Canoe
Comprehensive Atmosphere N' Ocean Engine
interpnf.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <stdlib.h>
3 
4 #include "interpolation.h"
5 
13 void interpnf(double *val, double const *coor, double const *data,
14  double const *axis, size_t const *len, int ndim) {
15  int i1, i2;
16  i1 = locate(axis, *coor, *len);
17 
18  // if the interpolation value is out of bound
19  // use the closest value
20  if (i1 == -1) {
21  i1 = 0;
22  i2 = 0;
23  } else if (i1 == *len - 1) {
24  i1 = *len - 1;
25  i2 = *len - 1;
26  } else
27  i2 = i1 + 1;
28 
29  double x1 = axis[i1];
30  double x2 = axis[i2];
31  double v1, v2;
32 
33  if (ndim == 1) {
34  v1 = data[i1];
35  v2 = data[i2];
36  } else {
37  int s = 1;
38  for (int j = 1; j < ndim; ++j) s *= len[j];
39  interpnf(&v1, coor + 1, data + i1 * s, axis + *len, len + 1, ndim - 1);
40  interpnf(&v2, coor + 1, data + i2 * s, axis + *len, len + 1, ndim - 1);
41  }
42 
43  if (x2 != x1)
44  *val = ((*coor - x1) * v2 + (x2 - *coor) * v1) / (x2 - x1);
45  else
46  *val = (v1 + v2) / 2.;
47 }
void interpnf(double *val, double const *coor, double const *data, double const *axis, size_t const *len, int ndim)
Definition: interpnf.c:13
int locate(double const *xx, double x, int n)
Definition: locate.c:7