NASA Logo
Ocean Color Science Software

ocssw V2022
dataintp.c
Go to the documentation of this file.
1 #include <math.h>
2 #include "anc.h"
3 
4 #define DSIGN(A, B) (B >= 0 ? fabs(A) : -fabs(A))
5 #define ERROR_RET(x, err) {err = x; if(err) return err;}
6 #define MAX_BAND 15
7 int timeint(float f1[][MAX_BAND], float f2[][MAX_BAND], double dt1, double dt2, int32_t ipt, int32_t *nband, float *mimx, float *def,
8  float *fout, float *func, int32_t ir) {
9  double t1 = fabs(dt1);
10  double t2 = fabs(dt2);
11  if (t1 == 0.0e0 || t2 == 0.0e0) t2 = 1.e0;
12  double w1 = t2 / (t1 + t2);
13  double w2 = t1 / (t1 + t2);
14  if (w1 > 1.0e0) w2 = 0.0e0;
15  if (w2 > 1.0e0) w1 = 0.0e0;
16  for (int n = 0; n < *nband; n++) {
17  int ij = n * ir;
18  for (int i = 0; i < ipt; i++) {
19  int k = ij + i;
20  int flag1 = f1[n][i] >= mimx[0] && f1[n][i] <= mimx[1];
21  int flag2 = f1[n][i] >= mimx[0] && f1[n][i] <= mimx[1];
22  int sw = flag1 * 2 + flag2;
23  switch (sw) {
24  case 0:
25  fout[k] = def[n];//
26  func[k] = 0;
27  break;
28  case 1:
29  fout[k] = f2[n][i];//
30  func[k] = 0;
31  break;
32  case 2:
33  fout[k] = f1[n][i];//
34  func[k] = 0;
35  break;
36  case 3:
37  fout[k] = w1 * f1[n][i] + w2 * f2[n][i];
38  func[k] = fabsf(f1[n][i] - f2[n][i]);
39  break;
40  default:
41  return 1;
42  }
43 
44  }
45  }
46  return 0;
47 }
48 
49 int spaceint(float *ll, float *lat, float *lon, float **f, int32_t *ipt, int32_t *nband, float *mimx, float *def,
50  float *fout,
51  int32_t *int_bad) {
52  const static int nc[4] = {1, 10, 100, 1000};
53  const static int n1[4][3] = {{1, 2, 4},
54  {10, 1, 3},
55  {100, 2, 4},
56  {1000, 1, 3}};
57  const static int n2[6][5] = {{11, 1, 2, 4, 3},
58  {101, 1, 3, 4, 2},
59  {1001, 1, 4, 2, 3},
60  {110, 2, 3, 1, 4},
61  {1010, 2, 4, 3, 1},
62  {1100,
63  3, 4, 2, 1}};
64  const static int n3[4][2] = {{111, 4},
65  {1110, 1},
66  {1101, 2},
67  {1011, 3}};
68  float llft[2], urht[2];
69  const int npt = *ipt;
70  const static double error_tolerance = 1e-9;
71  llft[1 - 1] = lat[1 - 1];
72  llft[2 - 1] = lon[1 - 1];
73  urht[1 - 1] = lat[3 - 1];
74  urht[2 - 1] = lon[3 - 1];
75  // C: Rectangular bi-linear interpolation.
76  float xp = ll[1] - llft[1];
77  if (fabsf(xp) > 180.0f) { xp = DSIGN(360.0f - fabsf(xp), xp); }
78  float yp = ll[0] - llft[0];
79  float dx = urht[1] - llft[1];
80  if (fabsf(dx) > 180.f) dx = DSIGN(360.0f - fabsf(dx), dx);
81  float dy = urht[0] - llft[0];
82  float dd = dx * dy;
83  for (int n = 0; n < *nband; n++) {
84  int ncc = 0;
85  int nng = 0;
86  *int_bad = 0;
87  float g[npt];
88  for (int i = 0; i < npt; i++) {
89  g[i] = f[n][i];
90  if (g[i] < mimx[0] || g[i] > mimx[1]) {
91  ncc += nc[i];
92  nng++;
93  }
94  }
95  switch (nng) {
96  case 0:
97  break;
98  case 1:
99  for (int i = 0; i < npt; i++) {
100  if (ncc == n1[i][0]) {
101  g[i] = (g[n1[i][1]] + g[n1[i][2]]) / 2.f;
102  }
103  }
104  break;
105  case 2:
106  for (int i = 0; i < 6; i++) { // ! WDR replace IPT with 6 to correct
107  if (ncc == n2[i][0]) {
108  g[n2[i][1]] = g[n2[i][3]];
109  g[n2[i][2]] = g[n2[i][4]];
110  }
111  }
112  break;
113  case 3:
114  for (int i = 0; i < npt; i++) {
115  if (ncc == n3[i][0]) {
116  fout[n] = g[n3[i][1]];
117  continue;
118  }
119  }
120  break;
121  case 4:
122  fout[n] = def[n];
123  *int_bad = 1;
124  continue;
125  default:
126  return 1;
127  }
128  float a[4];
129  a[0] = g[0];
130  if (fabsf(dx) < error_tolerance)
131  a[1] = 0.0f;
132  else
133  a[1] = (g[3] - a[0]) / dx;
134  if (fabsf(dy) < error_tolerance)
135  a[2] = 0.0f;
136  else
137  a[2] = (g[1] - a[0]) / dy;
138  if (fabsf(dd) < error_tolerance)
139  a[3] = 0.0f;
140  else
141  a[3] = (a[0] - g[1] + g[2] - g[3]) / dd;
142  fout[n] = a[0] + a[1] * xp + yp * a[2] + yp * xp * a[3];
143  }
144  return 0;
145 }
146 
147 
148 int dataintp(float in_latlon[2], float *lat, float *lon,
149  float *data_list1, double *dt1, float *data_list2, double *dt2,
150  int32_t *ipt, int32_t *nband, float rng[2], float *def,
151  int32_t *intporder, float *dummy, float *dataout, float *unc,
152  int32_t *int_bad, int32_t *row, int32_t *col) {
153  float mimx[2];
154  float fout1[1][MAX_BAND];
155  float fout2[1][MAX_BAND];
156  const int ntot = *nband * *row;
157  int int_bad1, int_bad2;
158  if (rng[1] > rng[0]) {
159  mimx[1] = rng[1];
160  mimx[0] = rng[0];
161  } else {
162  mimx[1] = rng[0];
163  mimx[0] = rng[1];
164  }
165  *int_bad = 0;
166  for (int i = 0; i < ntot; i++) {
167  unc[i] = 0.0f;
168  }
169  int ret_ = 0;
170  switch (*intporder) {
171  case 112: ERROR_RET(
172  spaceint(in_latlon, lat, lon, &data_list1, ipt, nband, mimx, def, (float *) fout1, &int_bad1), ret_);
173  ERROR_RET(spaceint(in_latlon, lat, lon, &data_list2, ipt, nband, mimx, def, (float *) fout2, &int_bad2),
174  ret_);
175  ERROR_RET(
176  timeint( fout1, fout2, *dt1, *dt2, 1, nband, mimx, def, dataout, unc, *row),
177  ret_);
178  *int_bad = int_bad1 || int_bad2;
179  break;
180  case 110: ERROR_RET(spaceint(in_latlon, lat, lon, &data_list1, ipt, nband, mimx, def, dataout, int_bad),
181  ret_);
182  break;
183  case 101: ERROR_RET(
184  timeint( fout1, fout2, *dt1, *dt2, *ipt, nband, mimx, def, dataout, unc, *row),
185  ret_);
186  break;
187  case 121: ERROR_RET(
188  timeint(fout1, fout2, *dt1, *dt2, *ipt, nband, mimx, def, dummy, unc, *ipt),
189  ret_);
190  ERROR_RET(spaceint(in_latlon, lat, lon, &dummy, ipt, nband, mimx, def, dataout, int_bad),
191  ret_);
192  default:
193  return 1;
194  }
195 }
float f1(float x)
#define MAX_BAND
Definition: dataintp.c:6
float f2(float y)
double precision function f(R1)
Definition: tmd.lp.f:1454
subroutine func(x, conec, n, bconecno, bn, units, u, inno, i, outno, o, Input, Targ, p, sqerr)
Definition: ffnet.f:287
int timeint(float f1[][MAX_BAND], float f2[][MAX_BAND], double dt1, double dt2, int32_t ipt, int32_t *nband, float *mimx, float *def, float *fout, float *func, int32_t ir)
Definition: dataintp.c:7
int dataintp(float in_latlon[2], float *lat, float *lon, float *data_list1, double *dt1, float *data_list2, double *dt2, int32_t *ipt, int32_t *nband, float rng[2], float *def, int32_t *intporder, float *dummy, float *dataout, float *unc, int32_t *int_bad, int32_t *row, int32_t *col)
Definition: dataintp.c:148
#define ERROR_RET(x, err)
Definition: dataintp.c:5
#define DSIGN(A, B)
Definition: dataintp.c:4
int32_t nband
#define fabs(a)
Definition: misc.h:93
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
int spaceint(float *ll, float *lat, float *lon, float **f, int32_t *ipt, int32_t *nband, float *mimx, float *def, float *fout, int32_t *int_bad)
Definition: dataintp.c:49
int k
Definition: decode_rs.h:73