OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
scale_sds.c
Go to the documentation of this file.
1 #include <math.h>
2 #include <stddef.h>
3 #include <stdio.h>
4 #include <dfutils.h>
5 #include <genutils.h>
6 #include <productInfo.h>
7 
8 #include <hdf.h>
9 #include <mfhdf.h>
10 
16 int16_t getDataTypeInt(productInfo_t *p_info) {
17  int16_t nt;
18 
19  if (strcmp(p_info->dataType, "byte") == 0)
20  nt = DFNT_INT8;
21  else if (strcmp(p_info->dataType, "ubyte") == 0)
22  nt = DFNT_UINT8;
23  else if (strcmp(p_info->dataType, "short") == 0)
24  nt = DFNT_INT16;
25  else if (strcmp(p_info->dataType, "ushort") == 0)
26  nt = DFNT_UINT16;
27  else if (strcmp(p_info->dataType, "int") == 0)
28  nt = DFNT_INT32;
29  else if (strcmp(p_info->dataType, "uint") == 0)
30  nt = DFNT_UINT32;
31  else if (strcmp(p_info->dataType, "float") == 0)
32  nt = DFNT_FLOAT32;
33  else if (strcmp(p_info->dataType, "double") == 0)
34  nt = DFNT_FLOAT64;
35  else {
36  printf("-E- %s %d: datatype %s is not valid\n", __FILE__, __LINE__, p_info->dataType);
37  exit(1);
38  }
39 
40  return nt;
41 }
42 /* -------------------------------------------------------- */
43 
44 /* -------------------------------------------------------- */
45 int16_t *float2int16(float32 fbuf[], int32_t spix, int32_t npix, int incr,
46  float slope, float offset) {
47  static int32_t npix_alloc = 0;
48  static int16 *ibuf = NULL;
49  float32 fval;
50  int32_t i;
51 
52  double maxval = slope * 32767 + offset;
53  double minval = slope * (-32766) + offset;
54 
55  /*
56  * -32766 is used for minval to allow -32767 to remain the sentinal bad value
57  * Yes, this does leave one digit hanging out down low, but meh....
58  */
59 
60  if (npix > npix_alloc) {
61  npix_alloc = npix;
62  if (ibuf)
63  free(ibuf);
64  if ((ibuf = calloc(npix, sizeof (int16))) == NULL) {
65  fprintf(stderr,
66  "-E- %s line %d: Unable to allocate buffer space.\n",
67  __FILE__, __LINE__);
68  exit(1);
69  }
70  }
71 
72  for (i = 0; i < npix; i++) {
73  fval = fbuf[spix + i * incr];
74  if (fval == BAD_FLT)
75  ibuf[i] = BAD_INT;
76  else if (fval >= maxval)
77  ibuf[i] = 32767;
78  else if (fval <= minval)
79  ibuf[i] = -32766;
80  else
81  ibuf[i] = round((fval - offset) / slope);
82  }
83 
84  return (ibuf);
85 }
86 
87 /* -------------------------------------------------------- */
88 
89 /* -------------------------------------------------------- */
90 uint16_t *float2uint16(float32 fbuf[], int32_t spix, int32_t npix, int incr,
91  float slope, float offset) {
92  static int32_t npix_alloc = 0;
93  static uint16 *ibuf = NULL;
94  float32 fval;
95  int32_t i;
96 
97  double maxval = slope * 65534 + offset;
98  double minval = offset;
99 
100  if (npix > npix_alloc) {
101  npix_alloc = npix;
102  if (ibuf)
103  free(ibuf);
104  if ((ibuf = calloc(npix, sizeof (uint16))) == NULL) {
105  fprintf(stderr,
106  "-E- %s line %d: Unable to allocate buffer space.\n",
107  __FILE__, __LINE__);
108  exit(1);
109  }
110  }
111 
112  for (i = 0; i < npix; i++) {
113  fval = fbuf[spix + i * incr];
114  if (fval == BAD_FLT)
115  ibuf[i] = BAD_UINT;
116  else if (fval >= maxval)
117  ibuf[i] = 65534;
118  else if (fval <= minval)
119  ibuf[i] = 0;
120  else
121  ibuf[i] = round((fval - offset) / slope);
122  }
123 
124  return (ibuf);
125 }
126 /* -------------------------------------------------------- */
127 
128 /* -------------------------------------------------------- */
129 uint8_t *float2uint8(float32 fbuf[], int32_t spix, int32_t npix, int incr,
130  float slope, float offset) {
131  static int32_t npix_alloc = 0;
132  static uint8_t *ibuf = NULL;
133  float32 fval;
134  int32_t i;
135 
136  double maxval = slope * 254 + offset;
137  double minval = slope * 0 + offset;
138 
139  if (npix > npix_alloc) {
140  npix_alloc = npix;
141  if (ibuf)
142  free(ibuf);
143  if ((ibuf = calloc(npix, sizeof (int8))) == NULL) {
144  fprintf(stderr,
145  "-E- %s line %d: Unable to allocate buffer space.\n",
146  __FILE__, __LINE__);
147  exit(1);
148  }
149  }
150 
151  for (i = 0; i < npix; i++) {
152  fval = fbuf[spix + i * incr];
153  if (fval == BAD_FLT)
154  ibuf[i] = BAD_UBYTE;
155  if (fval >= maxval)
156  ibuf[i] = 254;
157  else if (fval <= minval)
158  ibuf[i] = 0;
159  else
160  ibuf[i] = round((fval - offset) / slope);
161  }
162 
163  return (ibuf);
164 }
165 
166 /* -------------------------------------------------------- */
167 
168 /* -------------------------------------------------------- */
169 int8_t *float2int8(float32 fbuf[], int32_t spix, int32_t npix, int incr,
170  float slope, float offset) {
171  static int32_t npix_alloc = 0;
172  static int8_t *ibuf = NULL;
173  float32 fval;
174  int32_t i;
175 
176  double maxval = slope * 127 + offset;
177  double minval = slope * -127 + offset;
178 
179  if (npix > npix_alloc) {
180  npix_alloc = npix;
181  if (ibuf)
182  free(ibuf);
183  if ((ibuf = calloc(npix, sizeof (int8))) == NULL) {
184  fprintf(stderr,
185  "-E- %s line %d: Unable to allocate buffer space.\n",
186  __FILE__, __LINE__);
187  exit(1);
188  }
189  }
190 
191  for (i = 0; i < npix; i++) {
192  fval = fbuf[spix + i * incr];
193  if (fval == BAD_FLT)
194  ibuf[i] = BAD_BYTE;
195  if (fval >= maxval)
196  ibuf[i] = 127;
197  else if (fval <= minval)
198  ibuf[i] = -127;
199  else
200  ibuf[i] = round((fval - offset) / slope);
201  }
202 
203  return (ibuf);
204 }
205 
206 
207 /* -------------------------------------------------------- */
208 
209 /* -------------------------------------------------------- */
210 void *scale_sds(float *data, productInfo_t *p, int npix) {
211  void *pbuf;
212 
213  switch (getDataTypeInt(p)) {
214  case DFNT_INT16:
215  pbuf = (void *) float2int16(data, 0, npix, 1, p->scaleFactor, p->addOffset);
216  break;
217  case DFNT_UINT16:
218  pbuf = (void *) float2uint16(data, 0, npix, 1, p->scaleFactor, p->addOffset);
219  break;
220  case DFNT_INT8:
221  pbuf = (void *) float2int8(data, 0, npix, 1, p->scaleFactor, p->addOffset);
222  break;
223  case DFNT_UINT8:
224  pbuf = (void *) float2uint8(data, 0, npix, 1, p->scaleFactor, p->addOffset);
225  break;
226  case DFNT_FLOAT32:
227  fprintf(stderr,
228  "-W- %s Line %d: Stubbornly refusing to scale floating point data: \n\t%s\n", __FILE__, __LINE__, p->productName);
229  pbuf = data;
230  break;
231  default:
232  fprintf(stderr,
233  "-W- %s Line %d: Unknown data type %s.\n", __FILE__, __LINE__, p->dataType);
234  pbuf = data;
235  break;
236  }
237 
238  return (pbuf);
239 }
240 
241 
242 /* -------------------------------------------------------- */
243 
244 /* -------------------------------------------------------- */
245 float *unscale_sds(void *data, productInfo_t *p, int32_t spix, int32_t npix, int incr) {
246  static int32_t npix_alloc = 0;
247  static float32 *fbuf = NULL;
248 
249  float32 fval, *fptr;
250  int16 ival, *iptr;
251  uint8_t bval, *bptr;
252  int32_t i;
253 
254  if (npix > npix_alloc) {
255  npix_alloc = npix;
256  if (fbuf)
257  free(fbuf);
258  if ((fbuf = calloc(npix, sizeof (float32))) == NULL) {
259  fprintf(stderr,
260  "-E- %s line %d: Unable to allocate buffer space.\n",
261  __FILE__, __LINE__);
262  exit(1);
263  }
264  }
265 
266  switch (getDataTypeInt(p)) {
267  case DFNT_INT8:
268  case DFNT_UINT8:
269  bptr = (uint8_t *) data;
270  for (i = 0; i < npix && i < npix; i++) {
271  bval = bptr[spix + i * incr];
272  fbuf[i] = bval * p->scaleFactor + p->addOffset;
273  }
274  break;
275  case DFNT_INT16:
276  case DFNT_UINT16:
277  iptr = (int16 *) data;
278  for (i = 0; i < npix && i < npix; i++) {
279  ival = iptr[spix + i * incr];
280  fbuf[i] = ival * p->scaleFactor + p->addOffset;
281  }
282  break;
283  case DFNT_FLOAT32:
284  fptr = (float32 *) data;
285  for (i = 0; i < npix && i < npix; i++) {
286  fval = fptr[spix + i * incr];
287  fbuf[i] = fval * p->scaleFactor + p->addOffset;
288  }
289  break;
290  default:
291  fprintf(stderr, "-W- %s Line %d: Unknown data type %s product %d.\n",
292  __FILE__, __LINE__, p->dataType, p->cat_ix);
293  break;
294  }
295 
296  return (fbuf);
297 }
298 
299 
integer, parameter int16
Definition: cubeio.f90:3
uint16_t * float2uint16(float32 fbuf[], int32_t spix, int32_t npix, int incr, float slope, float offset)
Definition: scale_sds.c:90
#define BAD_BYTE
Definition: genutils.h:25
#define NULL
Definition: decode_rs.h:63
#define BAD_UBYTE
Definition: genutils.h:26
HDF4 data type of the output SDS Default is DFNT_FLOAT32 Common types used DFNT_INT32
void * scale_sds(float *data, productInfo_t *p, int npix)
Definition: scale_sds.c:210
float32 slope[]
Definition: l2lists.h:30
int16_t * float2int16(float32 fbuf[], int32_t spix, int32_t npix, int incr, float slope, float offset)
Definition: scale_sds.c:45
int8_t * float2int8(float32 fbuf[], int32_t spix, int32_t npix, int incr, float slope, float offset)
Definition: scale_sds.c:169
int16_t getDataTypeInt(productInfo_t *p_info)
Definition: scale_sds.c:16
HDF4 data type of the output SDS Default is DFNT_FLOAT32 Common types used DFNT_INT16
no change in intended resolving MODur00064 Corrected handling of bad ephemeris attitude data
Definition: HISTORY.txt:356
uint8_t * float2uint8(float32 fbuf[], int32_t spix, int32_t npix, int incr, float slope, float offset)
Definition: scale_sds.c:129
#define BAD_FLT
Definition: jplaeriallib.h:19
int32 spix
Definition: l1_czcs_hdf.c:21
This should be set to the NetCDF standard name if exists for this product Create a function that computes your product edit get_myprod c add prototype to l12_proto h add get_myprod c to add_executable for l2gen and l3gen in CMakeLists txt Add an entry to the output routine to call your function edit prodgen c edit function prodgen() case CAT_myprod pbuf
#define BAD_INT
Definition: genutils.h:23
float * unscale_sds(void *data, productInfo_t *p, int32_t spix, int32_t npix, int incr)
Definition: scale_sds.c:245
l2prod offset
HDF4 data type of the output SDS Default is DFNT_FLOAT32 Common types used DFNT_FLOAT32
#define BAD_UINT
Definition: genutils.h:24
int i
Definition: decode_rs.h:71
int npix
Definition: get_cmp.c:27
float p[MODELMAX]
Definition: atrem_corl1.h:131