MISR Toolkit  1.5.1
MtkApplyRegression.c
Go to the documentation of this file.
1 /*===========================================================================
2 = =
3 = MtkApplyRegression =
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 *Source,
38  const MTKt_DataBuffer *Source_mask,
39  const MTKt_MapInfo *Source_map_info,
40  const MTKt_RegressionCoeff *Regression_coeff,
41  const MTKt_MapInfo *Regression_coeff_map_info,
42  MTKt_DataBuffer *Regressed,
43  MTKt_DataBuffer *Regressed_mask
44 )
45 {
46  MTKt_status status; /* Return status */
47  MTKt_status status_code; /* Return status of this function */
48  MTKt_DataBuffer regressed_tmp = MTKT_DATABUFFER_INIT;
49  /* Output data. */
50  MTKt_DataBuffer regressed_mask_tmp = MTKT_DATABUFFER_INIT;
51  /* Output data mask */
52  MTKt_RegressionCoeff regression_coeff_resampled = MTKT_REGRESSION_COEFF_INIT;
53  /* Regression coefficients resampled to
54  resolution of source data. */
55  int iline; /* Loop iterator. */
56  int isample; /* Loop iterator. */
57 
58  /* ------------------------------------------------------------------ */
59  /* Argument check: Source == NULL */
60  /* Source->nline < 1 */
61  /* Source->nsample < 1 */
62  /* Source->datatype = MTKe_float */
63  /* ------------------------------------------------------------------ */
64 
65  if (Source == NULL) {
66  MTK_ERR_CODE_MSG_JUMP(MTK_NULLPTR,"Source == NULL");
67  }
68  if (Source->nline < 1) {
69  MTK_ERR_CODE_MSG_JUMP(MTK_OUTBOUNDS,"Source->nline < 1");
70  }
71  if (Source->nsample < 1) {
72  MTK_ERR_CODE_MSG_JUMP(MTK_OUTBOUNDS,"Source->nsample < 1");
73  }
74  if (Source->datatype != MTKe_float) {
75  MTK_ERR_CODE_MSG_JUMP(MTK_OUTBOUNDS,"Source->datatype != MTKe_float");
76  }
77 
78  /* ------------------------------------------------------------------ */
79  /* Argument check: Source_mask == NULL */
80  /* Source_mask->nline != Source->nline */
81  /* Source_mask->nsample != Source->nsample */
82  /* Source_mask->datatype = MTKe_uint8 */
83  /* ------------------------------------------------------------------ */
84 
85  if (Source_mask == NULL) {
86  MTK_ERR_CODE_MSG_JUMP(MTK_NULLPTR,"Source_mask == NULL");
87  }
88  if (Source_mask->nline != Source->nline) {
89  MTK_ERR_CODE_MSG_JUMP(MTK_OUTBOUNDS,"Source_mask->nline != Source->nline");
90  }
91  if (Source_mask->nsample != Source->nsample) {
92  MTK_ERR_CODE_MSG_JUMP(MTK_OUTBOUNDS,"Source_mask->nsample != Source->nsample");
93  }
94  if (Source_mask->datatype != MTKe_uint8) {
95  MTK_ERR_CODE_MSG_JUMP(MTK_OUTBOUNDS,"Source_mask->datatype != MTKe_uint8");
96  }
97 
98  /* ------------------------------------------------------------------ */
99  /* Argument check: Regressed == NULL */
100  /* ------------------------------------------------------------------ */
101 
102  if (Regressed == NULL) {
103  MTK_ERR_CODE_MSG_JUMP(MTK_NULLPTR,"Regressed == NULL");
104  }
105 
106  /* ------------------------------------------------------------------ */
107  /* Argument check: Regressed_mask == NULL */
108  /* ------------------------------------------------------------------ */
109 
110  if (Regressed_mask == NULL) {
111  MTK_ERR_CODE_MSG_JUMP(MTK_NULLPTR,"Regressed_mask == NULL");
112  }
113 
114  /* ------------------------------------------------------------------ */
115  /* Allocate memory for output data. */
116  /* ------------------------------------------------------------------ */
117 
118  status = MtkDataBufferAllocate(Source->nline, Source->nsample,
119  MTKe_float, &regressed_tmp);
120  MTK_ERR_COND_JUMP(status);
121 
122  status = MtkDataBufferAllocate(Source->nline, Source->nsample,
123  MTKe_uint8, &regressed_mask_tmp);
124  MTK_ERR_COND_JUMP(status);
125 
126  /* ------------------------------------------------------------------ */
127  /* Resample regression coefficients to resolution of source data. */
128  /* ------------------------------------------------------------------ */
129 
130  status = MtkResampleRegressionCoeff(Regression_coeff,
131  Regression_coeff_map_info,
132  Source_map_info,
133  &regression_coeff_resampled);
134  MTK_ERR_COND_JUMP(status);
135 
136  /* ------------------------------------------------------------------ */
137  /* For each valid pixel in source map... */
138  /* ------------------------------------------------------------------ */
139 
140  for (iline = 0 ; iline < Source->nline ; iline++) {
141  for (isample = 0 ; isample < Source->nsample ; isample++) {
142  if (Source_mask->data.u8[iline][isample]) {
143 
144  /* ------------------------------------------------------------------ */
145  /* Appy regression at this pixel. */
146  /* ------------------------------------------------------------------ */
147 
148  if (regression_coeff_resampled.valid_mask.data.u8[iline][isample]) {
149  regressed_tmp.data.f[iline][isample] =
150  Source->data.f[iline][isample] *
151  regression_coeff_resampled.slope.data.f[iline][isample] +
152  regression_coeff_resampled.intercept.data.f[iline][isample];
153  regressed_mask_tmp.data.u8[iline][isample] = 1;
154  }
155 
156  /* ------------------------------------------------------------------ */
157  /* End loop for each valid pixel in source map. */
158  /* ------------------------------------------------------------------ */
159 
160  }
161  }
162  }
163 
164  /* ------------------------------------------------------------------ */
165  /* Free memory. */
166  /* ------------------------------------------------------------------ */
167 
168  status = MtkRegressionCoeffFree(&regression_coeff_resampled);
169  MTK_ERR_COND_JUMP(status);
170 
171  /* ------------------------------------------------------------------ */
172  /* Return. */
173  /* ------------------------------------------------------------------ */
174 
175  *Regressed = regressed_tmp;
176  *Regressed_mask = regressed_mask_tmp;
177  return MTK_SUCCESS;
178 
179 ERROR_HANDLE:
180  MtkDataBufferFree(&regressed_tmp);
181  MtkDataBufferFree(&regressed_mask_tmp);
182  return status_code;
183 }
184 
185 
MTKt_status MtkDataBufferAllocate(int nline, int nsample, MTKt_DataType datatype, MTKt_DataBuffer *databuf)
Allocate Data Buffer.
MTKt_DataBufferType data
Definition: MisrUtil.h:104
MTKt_DataBuffer valid_mask
Map Information.
Definition: MisrMapQuery.h:65
MTKt_status MtkApplyRegression(const MTKt_DataBuffer *Source, const MTKt_DataBuffer *Source_mask, const MTKt_MapInfo *Source_map_info, const MTKt_RegressionCoeff *Regression_coeff, const MTKt_MapInfo *Regression_coeff_map_info, MTKt_DataBuffer *Regressed, MTKt_DataBuffer *Regressed_mask)
Apply regression to given data. Uses MtkResampleCubicConvolution to resample regression coefficients ...
2-dimensional Data Buffer
Definition: MisrUtil.h:98
MTKt_float ** f
Definition: MisrUtil.h:93
MTKt_DataBuffer slope
#define MTKT_DATABUFFER_INIT
Definition: MisrUtil.h:109
MTKt_status MtkDataBufferFree(MTKt_DataBuffer *databuf)
Free data buffer.
MTKt_DataType datatype
Definition: MisrUtil.h:102
MTKt_uint8 ** u8
Definition: MisrUtil.h:86
MTKt_status MtkResampleRegressionCoeff(const MTKt_RegressionCoeff *Regression_coeff, const MTKt_MapInfo *Regression_coeff_map_info, const MTKt_MapInfo *Target_map_info, MTKt_RegressionCoeff *Regression_coeff_out)
Resample regression coefficients at each pixel in the target map. Resampling is by cubic convolution...
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
#define MTK_ERR_COND_JUMP(code)
Definition: MisrError.h:188
MTKt_status
Definition: MisrError.h:11

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