OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
var_decode.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <limits.h>
5 #include <math.h>
6 #include "fmt_check.h"
7 
8 int var_decode(char *str, int32 hdf_type, void *array, int32 index, int32 adj_flg)
9 /*******************************************************************
10 
11  var_decode
12 
13  purpose: decode a value in a string to the proper hdf type specified
14 
15  Returns type: int - 0 if all went well, -1 if unable to read from the
16  string, -2 if value read does not fit in storage type, -3 if
17  hdf_type is not a valid one (or one handled currently)
18 
19  Parameters: (in calling order)
20  Type Name I/O Description
21  ---- ---- --- -----------
22  char * str I variable in the string to decode
23  int32 hdf_type I type of parameter to put data
24  into (DFNT_...)
25  void * array I/O array to place decoded value
26  int32 index I position in array to place value
27  (not used for char output)
28  int32 adj_flg I limit adjust flag. If 0, do not adjust
29  the variable decoded. If 1, consider
30  index = 0 to mean low
31  limit and index = 1 to mean high limit
32  and adjust limits for rounding error
33 
34  Note on adj_flg. var_decode is used to get the actual values of attributes
35  that should come in. It is also used to get the valid range for sds elements.
36  In this case, it was found that the sds values could exceed the limits by small
37  amounts. The adjustment will be to expand the range limits by the uncertainty
38  in the float values of the limits. In other words
39 
40  low limit = (input low limit) - (input low limit) * 10 ** ( - # digits accuracy )
41  hi limit = (input hi limit) + (input hi limit) * 10 ** ( - # digits accuracy )
42 
43  This is only done for float types
44 
45  Modification history:
46  Programmer Date Description of change
47  ---------- ---- ---------------------
48  W. Robinson 1-Oct-1996 Original development
49  W. Robinson 31-Oct-1996 upgrade to handle float64 data type
50  W. Robinson 18-Feb-1997 modify range limits in float32 and 64
51  to include excess due to rounding error
52 
53  *******************************************************************/ {
54  int32 i32val, *ptri32;
55  float32 f32val, *ptrf32;
56  float64 f64val, *ptrf64;
57  int8 i8val, *ptri8;
58  uint8 ui8val, *ptrui8;
59  int16 i16val, *ptri16;
60  char *ptrc;
61 
62  /*
63  * be able to point to the array in all these ways
64  */
65  ptri32 = (int32 *) array;
66  ptrf32 = (float32 *) array;
67  ptrf64 = (float64 *) array;
68  ptri8 = (int8 *) array;
69  ptrc = (char *) array;
70  ptrui8 = (uint8 *) array;
71  ptri16 = (int16 *) array;
72 
73  /*
74  * do 32 bit integer
75  */
76  if (hdf_type == DFNT_INT32) {
77  if (sscanf(str, "%d", &i32val) == EOF) {
78  return -1;
79  }
80  *(ptri32 + index) = i32val;
81  }
82  /*
83  * do 32 bit float
84  */
85  else if (hdf_type == DFNT_FLOAT32) {
86  if (sscanf(str, "%f", &f32val) == EOF) {
87  return -1;
88  }
89  /*
90  * for low limit, expand it lower by the value * 10 ** -( # bits of significance),
91  * for high limit, expand it higher in same way
92  */
93  if (adj_flg != 0)
94  f32val = (index == 0) ?
95  f32val - fabs(f32val) * pow(10., -(FLT_DIG)) :
96  f32val + fabs(f32val) * pow(10., -(FLT_DIG));
97  *(ptrf32 + index) = f32val;
98  }
99  /*
100  * do 64 bit float
101  */
102  else if (hdf_type == DFNT_FLOAT64) {
103  if (sscanf(str, "%lf", &f64val) == EOF) {
104  return -1;
105  }
106  /*
107  * for low limit, expand it lower by the value * 10 ** -( # bits of significance),
108  * for high limit, expand it higher in same way
109  */
110  if (adj_flg != 0)
111  f64val = (index == 0) ?
112  f64val - fabs(f64val) * pow(10., -(DBL_DIG)) :
113  f64val + fabs(f64val) * pow(10., -(DBL_DIG));
114  *(ptrf64 + index) = f64val;
115  } /*
116  * do 8 bit integer
117  */
118  else if (hdf_type == DFNT_INT8) {
119  if (sscanf(str, "%d", &i32val) == EOF) {
120  return -1;
121  }
122  if (i32val > 127 || i32val < -128) {
123  return -2;
124  }
125  i8val = i32val;
126  *(ptri8 + index) = i8val;
127  }
128  /*
129  * do character
130  */
131  else if (hdf_type == DFNT_CHAR) {
132  strcpy(ptrc, str);
133  }
134  /*
135  * do 8 bit unsigned integer
136  */
137  else if (hdf_type == DFNT_UINT8) {
138  if (sscanf(str, "%d", &i32val) == EOF) {
139  return -1;
140  }
141  if (i32val > 255 || i32val < 0) {
142  return -2;
143  }
144  ui8val = i32val;
145  *(ptrui8 + index) = ui8val;
146  }
147  /*
148  * do 16 bit integer
149  */
150  else if (hdf_type == DFNT_INT16) {
151  if (sscanf(str, "%hd", &i16val) == EOF) {
152  return -1;
153  }
154  *(ptri16 + index) = i16val;
155  }
156  /*
157  * for an unplanned hdf_type
158  */
159  else {
160  return -3;
161  }
162 
163  return 0;
164 }
an array had not been initialized Several spelling and grammar corrections were which is read from the appropriate MCF the above metadata values were hard coded A problem calculating the average background DN for SWIR bands when the moon is in the space view port was corrected The new algorithm used to calculate the average background DN for all reflective bands when the moon is in the space view port is now the same as the algorithm employed by the thermal bands For non SWIR changes in the averages are typically less than Also for non SWIR the black body DNs remain a backup in case the SV DNs are not available For SWIR the changes in computed averages were larger because the old which used the black body suffered from contamination by the micron leak As a consequence of the if SV DNs are not available for the SWIR the EV pixels will not be the granule time is used to identify the appropriate tables within the set given for one LUT the first two or last two tables respectively will be used for the interpolation If there is only one LUT in the set of it will be treated as a constant LUT The manner in which Earth View data is checked for saturation was changed Previously the raw Earth View DNs and Space View DNs were checked against the lookup table values contained in the table dn_sat The change made is to check the raw Earth and Space View DNs to be sure they are less than the maximum saturation value and to check the Space View subtracted Earth View dns against a set of values contained in the new lookup table dn_sat_ev The metadata configuration and ASSOCIATEDINSTRUMENTSHORTNAME from the MOD02HKM product The same metatdata with extensions and were removed from the MOD021KM and MOD02OBC products ASSOCIATEDSENSORSHORTNAME was set to MODIS in all products These changes are reflected in new File Specification which users may consult for exact the pow functions were eliminated in Emissive_Cal and Emissive bands replaced by more efficient code Other calculations throughout the code were also made more efficient Aside from a few round off there was no difference to the product The CPU time decreased by about for a day case and for a night case A minor bug in calculating the uncertainty index for emissive bands was corrected The frame index(0-based) was previously being used the frame number(1-based) should have been used. There were only a few minor changes to the uncertainty index(maximum of 1 digit). 3. Some inefficient arrays(Sigma_RVS_norm_sq) were eliminated and some code lines in Preprocess_L1A_Data were moved into Process_OBCEng_Emiss. There were no changes to the product. Required RAM was reduced by 20 MB. Now
integer, parameter int16
Definition: cubeio.f90:3
#define DBL_DIG
Definition: fmt_check.h:230
HDF4 data type of the output SDS Default is DFNT_FLOAT32 Common types used DFNT_INT32
int var_decode(char *str, int32 hdf_type, void *array, int32 index, int32 adj_flg)
Definition: var_decode.c:8
#define FLT_DIG
Definition: fmt_check.h:234
HDF4 data type of the output SDS Default is DFNT_FLOAT32 Common types used DFNT_INT16
const char * str
Definition: l1c_msi.cpp:35
#define fabs(a)
Definition: misc.h:93
HDF4 data type of the output SDS Default is DFNT_FLOAT32 Common types used DFNT_FLOAT32
How many dimensions is the output array Default is Not sure if anything above will work correctly strcpy(l2prod->title, "no title yet")