MISR Toolkit  1.5.1
MtkRegressionCoeffCalc.c
Go to the documentation of this file.
1 /*===========================================================================
2 = =
3 = MtkRegressionCoeffCalc =
4 = =
5 =============================================================================
6 
7  Jet Propulsion Laboratory
8  MISR
9  MISR Toolkit
10 
11  Copyright 2008, California Institute of Technology.
12  ALL RIGHTS RESERVED.
13  U.S. Government Sponsorship acknowledged.
14 
15 ============================================================================*/
16 
17 #include "MisrRegression.h"
18 #include "MisrUtil.h"
19 #include <stdlib.h>
20 #include <math.h>
21 
37  const MTKt_DataBuffer *Data1,
38  const MTKt_DataBuffer *Valid_mask1,
39  const MTKt_DataBuffer *Data2,
40  const MTKt_DataBuffer *Data2_sigma,
41  const MTKt_DataBuffer *Valid_mask2,
42  const MTKt_MapInfo *Map_info,
43  int Regression_size_factor,
44  MTKt_RegressionCoeff *Regression_coeff,
45  MTKt_MapInfo *Regression_coeff_map_info
46 )
47 {
48  MTKt_status status_code; /* Return status of this function */
49  MTKt_status status; /* Return status of called routines. */
50  MTKt_RegressionCoeff regression_coeff_tmp = MTKT_REGRESSION_COEFF_INIT;
51  MTKt_MapInfo regression_coeff_map_info_tmp = MTKT_MAPINFO_INIT;
52  int iline;
53  int isample;
54  int number_line_out; /* Number of lines in output grid.*/
55  int number_sample_out; /* Number of samples in output grid.*/
56  int number_line_in; /* Number of lines in input grid. */
57  int number_sample_in; /* Number of samples in input grid. */
58  double *data1 = NULL; /* Temporary buffer to contain data values. */
59  double *data2 = NULL; /* Temporary buffer to contain data values. */
60  double *data2_sigma = NULL; /* Temporary buffer to contain uncertainty. */
61 
62  /* ------------------------------------------------------------------ */
63  /* Argument check: Data1 == NULL */
64  /* Data1->nline < 1 */
65  /* Data1->nsample < 1 */
66  /* Data1->datatype = MTKe_float */
67  /* ------------------------------------------------------------------ */
68 
69  if (Data1 == NULL) {
70  MTK_ERR_CODE_MSG_JUMP(MTK_NULLPTR,"Data1 == NULL");
71  }
72  if (Data1->nline < 1) {
73  MTK_ERR_CODE_MSG_JUMP(MTK_OUTBOUNDS,"Data1->nline < 1");
74  }
75  if (Data1->nsample < 1) {
76  MTK_ERR_CODE_MSG_JUMP(MTK_OUTBOUNDS,"Data1->nsample < 1");
77  }
78  if (Data1->datatype != MTKe_float) {
79  MTK_ERR_CODE_MSG_JUMP(MTK_OUTBOUNDS,"Data1->datatype != MTKe_float");
80  }
81 
82  /* ------------------------------------------------------------------ */
83  /* Get size of input data. */
84  /* ------------------------------------------------------------------ */
85 
86  number_line_in = Data1->nline;
87  number_sample_in = Data1->nsample;
88 
89  /* ------------------------------------------------------------------ */
90  /* Argument check: Valid_mask1 == NULL */
91  /* Valid_mask1->nline != Data1->nline */
92  /* Valid_mask1->nsample != Data1->nsample */
93  /* Valid_mask1->datatype = MTKe_uint8 */
94  /* ------------------------------------------------------------------ */
95 
96  if (Valid_mask1 == NULL) {
97  MTK_ERR_CODE_MSG_JUMP(MTK_NULLPTR,"Valid_mask1 == NULL");
98  }
99  if (Valid_mask1->nline != number_line_in) {
100  MTK_ERR_CODE_MSG_JUMP(MTK_OUTBOUNDS,"Valid_mask1->nline != Data1->nline");
101  }
102  if (Valid_mask1->nsample != number_sample_in) {
103  MTK_ERR_CODE_MSG_JUMP(MTK_OUTBOUNDS,"Valid_mask1->nsample != Data1->nsample");
104  }
105  if (Valid_mask1->datatype != MTKe_uint8) {
106  MTK_ERR_CODE_MSG_JUMP(MTK_OUTBOUNDS,"Valid_mask1->datatype != MTKe_uint8");
107  }
108 
109  /* ------------------------------------------------------------------ */
110  /* Argument check: Data2 == NULL */
111  /* Data2->nline != Data1->nline */
112  /* Data2->nsample != Data1->nsample */
113  /* Data2->datatype = MTKe_float */
114  /* ------------------------------------------------------------------ */
115 
116  if (Data2 == NULL) {
117  MTK_ERR_CODE_MSG_JUMP(MTK_NULLPTR,"Data2 == NULL");
118  }
119  if (Data2->nline != number_line_in) {
120  MTK_ERR_CODE_MSG_JUMP(MTK_OUTBOUNDS,"Data2->nline != Data1->nline");
121  }
122  if (Data2->nsample != number_sample_in) {
123  MTK_ERR_CODE_MSG_JUMP(MTK_OUTBOUNDS,"Data2->nsample != Data1->nsample");
124  }
125  if (Data2->datatype != MTKe_float) {
126  MTK_ERR_CODE_MSG_JUMP(MTK_OUTBOUNDS,"Data2->datatype != MTKe_float");
127  }
128 
129  /* ------------------------------------------------------------------ */
130  /* Argument check: Data2_sigma == NULL */
131  /* Data2_sigma->nline != Data1->nline */
132  /* Data2_sigma->nsample != Data1->nsample */
133  /* Data2_sigma->datatype = MTKe_float */
134  /* ------------------------------------------------------------------ */
135 
136  if (Data2_sigma == NULL) {
137  MTK_ERR_CODE_MSG_JUMP(MTK_NULLPTR,"Data2_sigma == NULL");
138  }
139  if (Data2_sigma->nline != number_line_in) {
140  MTK_ERR_CODE_MSG_JUMP(MTK_OUTBOUNDS,"Data2_sigma->nline != Data1->nline");
141  }
142  if (Data2_sigma->nsample != number_sample_in) {
143  MTK_ERR_CODE_MSG_JUMP(MTK_OUTBOUNDS,"Data2_sigma->nsample != Data1->nsample");
144  }
145  if (Data2_sigma->datatype != MTKe_float) {
146  MTK_ERR_CODE_MSG_JUMP(MTK_OUTBOUNDS,"Data2_sigma->datatype != MTKe_float");
147  }
148 
149  /* ------------------------------------------------------------------ */
150  /* Argument check: Valid_mask2 == NULL */
151  /* Valid_mask2->nline != Data1->nline */
152  /* Valid_mask2->nsample != Data1->nsample */
153  /* Valid_mask2->datatype = MTKe_uint8 */
154  /* ------------------------------------------------------------------ */
155 
156  if (Valid_mask2 == NULL) {
157  MTK_ERR_CODE_MSG_JUMP(MTK_NULLPTR,"Valid_mask2 == NULL");
158  }
159  if (Valid_mask2->nline != number_line_in) {
160  MTK_ERR_CODE_MSG_JUMP(MTK_OUTBOUNDS,"Valid_mask2->nline != Data1->nline");
161  }
162  if (Valid_mask2->nsample != number_sample_in) {
163  MTK_ERR_CODE_MSG_JUMP(MTK_OUTBOUNDS,"Valid_mask2->nsample != Data1->nsample");
164  }
165  if (Valid_mask2->datatype != MTKe_uint8) {
166  MTK_ERR_CODE_MSG_JUMP(MTK_OUTBOUNDS,"Valid_mask2->datatype != MTKe_uint8");
167  }
168 
169  /* ------------------------------------------------------------------ */
170  /* Argument check: Map_info == NULL */
171  /* Map_info->nline != Data1->nline */
172  /* Map_info->nsample != Data1->nsample */
173  /* ------------------------------------------------------------------ */
174 
175  if (Map_info == NULL) {
176  MTK_ERR_CODE_MSG_JUMP(MTK_NULLPTR,"Map_info == NULL");
177  }
178  if (Map_info->nline != number_line_in) {
179  MTK_ERR_CODE_MSG_JUMP(MTK_OUTBOUNDS,"Map_info->nline != Data1->nline");
180  }
181  if (Map_info->nsample != number_sample_in) {
182  MTK_ERR_CODE_MSG_JUMP(MTK_OUTBOUNDS,"Map_info->nsample != Data1->nsample");
183  }
184 
185  /* ------------------------------------------------------------------ */
186  /* Argument check: */
187  /* Data1->nline % Regression_size_factor != 0 */
188  /* Data1->nsample in % Regression_size_factor != 0 */
189  /* ------------------------------------------------------------------ */
190 
191  if (number_line_in % Regression_size_factor != 0) {
192  MTK_ERR_CODE_MSG_JUMP(MTK_OUTBOUNDS,"Data1->nline % Regression_size_factor != 0");
193  }
194  if (number_sample_in % Regression_size_factor != 0) {
195  MTK_ERR_CODE_MSG_JUMP(MTK_OUTBOUNDS,"Data1->nsample % Regression_size_factor != 0");
196  }
197 
198  /* ------------------------------------------------------------------ */
199  /* Argument check: Regression_coeff == NULL */
200  /* ------------------------------------------------------------------ */
201 
202  if (Regression_coeff == NULL) {
203  MTK_ERR_CODE_MSG_JUMP(MTK_NULLPTR,"Regression_coeff == NULL");
204  }
205 
206  /* ------------------------------------------------------------------ */
207  /* Argument check: Regression_coeff_map_info == NULL */
208  /* ------------------------------------------------------------------ */
209 
210  if (Regression_coeff_map_info == NULL) {
211  MTK_ERR_CODE_MSG_JUMP(MTK_NULLPTR,"Regression_coeff_map_info == NULL");
212  }
213 
214  /* ------------------------------------------------------------------ */
215  /* Determine size of output array. */
216  /* ------------------------------------------------------------------ */
217 
218  number_line_out = number_line_in / Regression_size_factor;
219  number_sample_out = number_sample_in / Regression_size_factor;
220 
221  /* ------------------------------------------------------------------ */
222  /* Allocate memory for regression coefficients */
223  /* ------------------------------------------------------------------ */
224 
225  status = MtkRegressionCoeffAllocate(number_line_out, number_sample_out,
226  &regression_coeff_tmp);
227  MTK_ERR_COND_JUMP(status);
228 
229  /* ------------------------------------------------------------------ */
230  /* Allocate memory. */
231  /* ------------------------------------------------------------------ */
232 
233  data1 = (double *)calloc(Regression_size_factor*Regression_size_factor,
234  sizeof(double));
235  if (data1 == NULL) {
237  }
238  data2 = (double *)calloc(Regression_size_factor*Regression_size_factor,
239  sizeof(double));
240  if (data2 == NULL) {
242  }
243  data2_sigma = (double *)calloc(Regression_size_factor*Regression_size_factor,
244  sizeof(double));
245  if (data2_sigma == NULL) {
247  }
248 
249  /* ------------------------------------------------------------------ */
250  /* For each location in regression coefficient grid... */
251  /* ------------------------------------------------------------------ */
252 
253  for (iline = 0; iline < number_line_out ; iline++) {
254  for (isample = 0; isample < number_sample_out; isample++) {
255  int rline_start, rsample_start;
256  int rline_end, rsample_end;
257  int rline, rsample;
258  int count;
259  double intercept, slope, correlation;
260 
261  rline_start = iline * Regression_size_factor;
262  rsample_start = isample * Regression_size_factor;
263  rline_end = rline_start + Regression_size_factor;
264  rsample_end = rsample_start + Regression_size_factor;
265  count = 0;
266 
267  /* ------------------------------------------------------------------ */
268  /* Calculate linear regression at this location. */
269  /* If the linear regression cannot be calculated due to a */
270  /* divide-by-zero condition, then skip this location. */
271  /* ------------------------------------------------------------------ */
272 
273  for (rline = rline_start ; rline < rline_end ; rline++) {
274  for (rsample = rsample_start ; rsample < rsample_end ; rsample++) {
275  if (Valid_mask1->data.u8[rline][rsample] &&
276  Valid_mask2->data.u8[rline][rsample]) {
277  data1[count] = Data1->data.f[rline][rsample];
278  data2[count] = Data2->data.f[rline][rsample];
279  data2_sigma[count] = Data2_sigma->data.f[rline][rsample];
280  count++;
281  }
282  }
283  }
284 
285  regression_coeff_tmp.valid_mask.data.u8[iline][isample] = 0;
286 
287  if (count > 0) {
288  status = MtkLinearRegressionCalc(count, data1, data2, data2_sigma,
289  &intercept, &slope, &correlation);
290  if (status == MTK_DIV_BY_ZERO) {
291  continue;
292  } else {
293  MTK_ERR_COND_JUMP(status);
294  }
295 
296  regression_coeff_tmp.intercept.data.f[iline][isample] = intercept;
297  regression_coeff_tmp.slope.data.f[iline][isample] = slope;
298  regression_coeff_tmp.correlation.data.f[iline][isample] = correlation;
299 
300  regression_coeff_tmp.valid_mask.data.u8[iline][isample] = 1;
301  }
302 
303  /* ------------------------------------------------------------------ */
304  /* End loop for each location in regression coefficient grid. */
305  /* ------------------------------------------------------------------ */
306 
307  }
308  }
309 
310  /* ------------------------------------------------------------------ */
311  /* Set map info for regression coefficients. */
312  /* ------------------------------------------------------------------ */
313 
314  status = MtkChangeMapResolution(Map_info,
315  Map_info->resolution * Regression_size_factor,
316  &regression_coeff_map_info_tmp);
317  MTK_ERR_COND_JUMP(status);
318 
319  /* ------------------------------------------------------------------ */
320  /* Return. */
321  /* ------------------------------------------------------------------ */
322 
323  *Regression_coeff = regression_coeff_tmp;
324  *Regression_coeff_map_info = regression_coeff_map_info_tmp;
325  return MTK_SUCCESS;
326 
327 ERROR_HANDLE:
328  if (data1 != NULL) {
329  free(data1);
330  }
331  if (data2 != NULL) {
332  free(data2);
333  }
334  if (data2_sigma != NULL) {
335  free(data2_sigma);
336  }
337 
338  MtkRegressionCoeffFree(&regression_coeff_tmp);
339  return status_code;
340 }
HDFFCLIBAPI intf intf intf * count
MTKt_DataBufferType data
Definition: MisrUtil.h:104
#define MTKT_MAPINFO_INIT
Definition: MisrMapQuery.h:79
MTKt_status MtkRegressionCoeffCalc(const MTKt_DataBuffer *Data1, const MTKt_DataBuffer *Valid_mask1, const MTKt_DataBuffer *Data2, const MTKt_DataBuffer *Data2_sigma, const MTKt_DataBuffer *Valid_mask2, const MTKt_MapInfo *Map_info, int Regression_size_factor, MTKt_RegressionCoeff *Regression_coeff, MTKt_MapInfo *Regression_coeff_map_info)
Calculate linear regression coefficients for translating values in data buffer 1 to corresponding val...
MTKt_DataBuffer valid_mask
MTKt_status MtkChangeMapResolution(const MTKt_MapInfo *Map_info_in, int Resolution, MTKt_MapInfo *Map_info_out)
Change resolution of an MTKt_MapInfo structure.
#define MTK_ERR_CODE_JUMP(code)
Definition: MisrError.h:175
MTKt_status MtkLinearRegressionCalc(int Size, const double *X, const double *Y, const double *YSigma, double *A, double *B, double *Correlation)
Use linear regression to fit a set of observations (x,y) to the model: y(x) = a + b * x...
Map Information.
Definition: MisrMapQuery.h:65
2-dimensional Data Buffer
Definition: MisrUtil.h:98
MTKt_float ** f
Definition: MisrUtil.h:93
MTKt_DataBuffer slope
MTKt_DataType datatype
Definition: MisrUtil.h:102
MTKt_uint8 ** u8
Definition: MisrUtil.h:86
MTKt_DataBuffer intercept
MTKt_status MtkRegressionCoeffFree(MTKt_RegressionCoeff *regressbuf)
Free memory for regression coefficients.
#define MTK_ERR_CODE_MSG_JUMP(code, msg)
Definition: MisrError.h:181
#define MTKT_REGRESSION_COEFF_INIT
MTKt_status MtkRegressionCoeffAllocate(int nline, int nsample, MTKt_RegressionCoeff *regressbuf)
Allocate buffer to contain regression coefficients.
#define MTK_ERR_COND_JUMP(code)
Definition: MisrError.h:188
MTKt_status
Definition: MisrError.h:11
MTKt_DataBuffer correlation

MISR Toolkit - Copyright © 2005 - 2020 Jet Propulsion Laboratory
Generated on Fri Jun 19 2020 22:49:51