MISR Toolkit  1.5.1
MtkSmoothData.c
Go to the documentation of this file.
1 /*===========================================================================
2 = =
3 = MtkSmoothData =
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 *Data,
38  const MTKt_DataBuffer *Valid_mask,
39  int Width_line,
40  int Width_sample,
41  MTKt_DataBuffer *Data_smoothed
42 )
43 {
44  MTKt_status status_code; /* Return status of this function */
45  MTKt_status status; /* Return status of called routines. */
46  MTKt_DataBuffer data_smoothed_tmp = MTKT_DATABUFFER_INIT;
47  int iline;
48  int isample;
49 
50  /* -------------------------------------------------------------- */
51  /* Argument check: Data == NULL */
52  /* Data->nline < 1 */
53  /* Data->nsample < 1 */
54  /* Data->datatype != MTKe_float */
55  /* -------------------------------------------------------------- */
56 
57  if (Data == NULL) {
59  }
60  if (Data->nline < 1) {
62  }
63  if (Data->nsample < 1) {
65  }
66  if (Data->datatype != MTKe_float) {
68  }
69 
70  /* -------------------------------------------------------------- */
71  /* Argument check: Valid_mask == NULL */
72  /* Valid_mask->nline != Data->nline */
73  /* Valid_mask->nsample != Data->nsample */
74  /* Valid_mask->datatype != MTKe_uint8 */
75  /* -------------------------------------------------------------- */
76 
77  if (Valid_mask == NULL) {
79  }
80  if (Valid_mask->nline != Data->nline) {
82  }
83  if (Valid_mask->nsample != Data->nsample) {
85  }
86  if (Valid_mask->datatype != MTKe_uint8) {
88  }
89 
90  /* -------------------------------------------------------------- */
91  /* Argument check: Width_line < 1 */
92  /* Width_line > Data->nline */
93  /* Width_line is not odd */
94  /* -------------------------------------------------------------- */
95 
96  if (Width_line < 1) {
98  }
99  if (Width_line > Data->nline) {
101  }
102  if ((Width_line & 1) == 0) {
104  }
105 
106  /* -------------------------------------------------------------- */
107  /* Argument check: Width_sample < 1 */
108  /* Width_sample > Data->nsample */
109  /* Width_sample is not odd */
110  /* -------------------------------------------------------------- */
111 
112  if (Width_sample < 1) {
114  }
115  if (Width_sample > Data->nsample) {
117  }
118  if ((Width_sample & 1) == 0) {
120  }
121 
122  /* -------------------------------------------------------------- */
123  /* Argument check: Data_smoothed = NULL */
124  /* -------------------------------------------------------------- */
125 
126  if (Data_smoothed == NULL) {
128  }
129 
130  /* -------------------------------------------------------------- */
131  /* Allocate memory for smoothed data */
132  /* -------------------------------------------------------------- */
133 
134  status = MtkDataBufferAllocate(Data->nline,
135  Data->nsample,
136  MTKe_float,
137  &data_smoothed_tmp);
138  MTK_ERR_COND_JUMP(status);
139 
140  /* -------------------------------------------------------------- */
141  /* For each location in smoothed data....(begin loop) */
142  /* -------------------------------------------------------------- */
143 
144  for (iline = 0 ; iline < Data->nline ; iline++) {
145  for (isample = 0 ; isample < Data->nsample ; isample++) {
146  double window_sum = 0.0; /* Sum of valid elements in the window */
147  int count = 0; /* Count of valid elements in the window */
148  int wline;
149  int wsample;
150 
151  /* -------------------------------------------------------------- */
152  /* If this location is not valid in the input data, then skip to */
153  /* the next location. Don't attempt to fill in gaps. */
154  /* -------------------------------------------------------------- */
155 
156  if (!Valid_mask->data.u8[iline][isample]) {
157  continue;
158  }
159 
160  /* -------------------------------------------------------------- */
161  /* Sum all valid data points in the smoothing window centered at */
162  /* this location. Where data is not available, duplicate the */
163  /* data value at the center of the window. This has the effect */
164  /* of giving higher weight to the center of the window where */
165  /* information is not available across the entire window. */
166  /* -------------------------------------------------------------- */
167 
168  for (wline = iline - ((Width_line - 1) / 2) ;
169  wline <= iline + ((Width_line - 1) / 2) ; wline++) {
170  for (wsample = isample - ((Width_sample - 1) / 2) ;
171  wsample <= isample + ((Width_sample - 1) / 2) ; wsample++) {
172 
173  if ((wline >= 0 && wline < Data->nline) &&
174  (wsample >= 0 && wsample < Data->nsample) &&
175  Valid_mask->data.u8[wline][wsample]) {
176  window_sum += Data->data.f[wline][wsample];
177  } else {
178  window_sum += Data->data.f[iline][isample];
179  }
180  count++;
181  }
182  }
183 
184  /* -------------------------------------------------------------- */
185  /* Set smoothed value at this location. */
186  /* -------------------------------------------------------------- */
187 
188  data_smoothed_tmp.data.f[iline][isample] =
189  window_sum / (double)count;
190 
191  /* -------------------------------------------------------------- */
192  /* End loop for each location in smoothed data. */
193  /* -------------------------------------------------------------- */
194 
195  }
196  }
197 
198  *Data_smoothed = data_smoothed_tmp;
199  return MTK_SUCCESS;
200 
201 ERROR_HANDLE:
202  MtkDataBufferFree(&data_smoothed_tmp);
203  return status_code;
204 }
HDFFCLIBAPI intf intf intf * count
MTKt_status MtkDataBufferAllocate(int nline, int nsample, MTKt_DataType datatype, MTKt_DataBuffer *databuf)
Allocate Data Buffer.
MTKt_DataBufferType data
Definition: MisrUtil.h:104
#define MTK_ERR_CODE_JUMP(code)
Definition: MisrError.h:175
2-dimensional Data Buffer
Definition: MisrUtil.h:98
MTKt_float ** f
Definition: MisrUtil.h:93
#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 MtkSmoothData(const MTKt_DataBuffer *Data, const MTKt_DataBuffer *Valid_mask, int Width_line, int Width_sample, MTKt_DataBuffer *Data_smoothed)
Smooth the given array with a boxcar average of the specified width. The algorithm is similar to the ...
Definition: MtkSmoothData.c:36
#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