OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
fmt_rd_sds.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "fmt_check.h"
5 extern int fmt_status; /* format check status, see fmt_check */
6 
7 int fmt_rd_sds(char *file, FILE *fid, fmt_str *fmt)
8 /*******************************************************************
9 
10  fmt_rd_sds
11 
12  purpose: read in the SDS description section of the format description for
13  a dataset
14 
15  Returns type: int - 0 if all went well,
16 
17  Parameters: (in calling order)
18  Type Name I/O Description
19  ---- ---- --- -----------
20  char * file I file name containing the format
21  table description
22  FILE * fid I format table file handle
23  fmt_str * fmt I/O format structure
24 
25  Modification history:
26  Programmer Date Description of change
27  ---------- ---- ---------------------
28  W. Robinson, SAIC 29 Mar 2005 Original development
29  (a portion of the old fmt_read)
30 
31  *******************************************************************/
32  {
33  char line[500], line_sav[500], str_typ[200], *str, *str2;
34  int i, j, k, idim, ifound, ierr;
35 
36  /*
37  * allocate space for the attribute descriptions
38  */
39  if ((fmt->sds_info = malloc(fmt->n_sds * sizeof ( sds_info_str)))
40  == NULL) {
41  printf("**************Program error\n");
42  printf("in: %s, SDS description storage allocation failed\n", __FILE__);
43  fmt_status = fmt_status | 1;
44  return -1;
45  }
46  /*
47  * read in each line of SDS description to the proper place
48  */
49  for (i = 0; i < fmt->n_sds; i++) {
50  if (get_line(line, 500, fid, '#') == NULL) {
51  printf("**************Program error\n");
52  printf("unable to read SDS description # %d of file:\n'%s'\n",
53  i, file);
54  printf("last line read:\n'%s'\n", line);
55  fmt_status = fmt_status | 1;
56  return -1;
57  }
58  strcpy(line_sav, line); /* save original line */
59 
60  /*
61  * SDS name field
62  */
63  if ((str = s_parse(line, '\"')) == NULL) {
64  printf("**************Program error\n");
65  printf("unable to read SDS ctl line %d, entry #1\n", i);
66  printf("last line read:\n'%s'\n", line_sav);
67  fmt_status = fmt_status | 1;
68  return -1;
69  }
70  strcpy(fmt->sds_info[i].name, str);
71 
72  /*
73  * The next 3 fields: data type, # attributes and rank
74  */
75  for (j = 0; j < 3; j++) {
76  if ((str = s_parse(NULL, '\"')) == NULL) {
77  printf("**************Program error\n");
78  printf(
79  "unable to read SDS ctl value %d of line # %d of file:\n'%s'\n",
80  j + 2, i, file);
81  printf("last line read:\n'%s'\n", line_sav);
82  fmt_status = fmt_status | 1;
83  return -1;
84  }
85  switch (j) {
86  case 0: /* data type */
87  strcpy(str_typ, str);
88  break;
89 
90  case 1: /* # attributes */
91  if (sscanf(str, "%d", &(fmt->sds_info[i].n_attr)) == EOF) {
92  printf("**************Program error\n");
93  printf(
94  "Cannot decode SDS n_attr ctl, line %d, file:\n'%s'\n",
95  i, file);
96  printf("last line read:\n'%s'\n", line_sav);
97  fmt_status = fmt_status | 1;
98  return -1;
99  }
100  break;
101 
102  case 2: /* rank (# dimensions) */
103  if (sscanf(str, "%d", &(fmt->sds_info[i].rank)) == EOF) {
104  printf("**************Program error\n");
105  printf("Cannot decode SDS rank ctl, line %d, file:\n'%s'\n",
106  i, file);
107  printf("last line read:\n'%s'\n", line_sav);
108  fmt_status = fmt_status | 1;
109  return -1;
110  }
111  break;
112  }
113  }
114  /*
115  * depending on the rank, there are up to 3 pairs of dimension
116  * type / size fields
117  */
118  for (idim = 0; idim < fmt->sds_info[i].rank; idim++) {
119  if ((str = s_parse(NULL, '\"')) == NULL) {
120  printf("**************Program error\n");
121  printf(
122  "unable to read SDS field %d of line # %d of file:\n'%s'\n",
123  idim * 2 + 5, i, file);
124  printf("last line read:\n'%s'\n", line_sav);
125  fmt_status = fmt_status | 1;
126  return -1;
127  }
128  /*
129  * read the next value and have it ready
130  */
131  if ((str2 = s_parse(NULL, '\"')) == NULL) {
132  printf("**************Program error\n");
133  printf(
134  "unable to read SDS field %d of line # %d of file:\n'%s'\n",
135  idim * 2 + 6, i, file);
136  printf("last line read:\n'%s'\n", line_sav);
137  fmt_status = fmt_status | 1;
138  return -1;
139  }
140  /*
141  * check the dimension type and deal with size field appropriately
142  */
143  if (strcmp(str, "DSIZE_NOCK") == 0)
144  fmt->sds_info[i].e_ranges[idim] = 0;
145 
146  else if (strcmp(str, "DSIZE_CK") == 0) {
147  if (sscanf(str2, "%d", &(fmt->sds_info[i].e_ranges[idim]))
148  == EOF) {
149  printf("**************Program error\n");
150  printf("unable to get dimension size from format table\n");
151  printf("for dimension %d, SDS field %d, of line %d of file:"
152  "\n'%s'\n", idim, idim * 2 + 6, i, file);
153  printf("last line read:\n'%s'\n", line_sav);
154  fmt_status = fmt_status | 1;
155  return -1;
156  }
157  } else if (strcmp(str, "DSIZE_DIM_NM") == 0) {
158  /*
159  * the index to the dimension structure array (+1 and negated)
160  * is placed in the e_range
161  */
162  ifound = 0;
163  for (k = 0; k < fmt->n_dim_defs; k++) {
164  if (strcmp(str2, fmt->dim_id[k].att_short) == 0) {
165  ifound = 1;
166  fmt->sds_info[i].e_ranges[idim] = -(k + 1);
167  break;
168  }
169  }
170  if (ifound == 0) {
171  printf("**************Program error\n");
172  printf(
173  "No definition of %s was found in the array definition section\n",
174  str2);
175  printf("For dimension %d, SDS field %d, of line %d of file:"
176  "\n'%s'\n", idim, idim * 2 + 6, i, file);
177  printf("last line read:\n'%s'\n", line_sav);
178  fmt_status = fmt_status | 1;
179  return -1;
180  }
181  }
182  }
183  /*
184  * get the type put in
185  */
186  if (strcmp(str_typ, "DFNT_CHAR") == 0) {
187  fmt->sds_info[i].type = DFNT_CHAR;
188  fmt->sds_info[i].byt_per_val = 1;
189  } else if (strcmp(str_typ, "DFNT_INT16") == 0) {
190  fmt->sds_info[i].type = DFNT_INT16;
191  fmt->sds_info[i].byt_per_val = 2;
192  } else if (strcmp(str_typ, "DFNT_INT32") == 0) {
193  fmt->sds_info[i].type = DFNT_INT32;
194  fmt->sds_info[i].byt_per_val = 4;
195  } else if (strcmp(str_typ, "DFNT_FLOAT32") == 0) {
196  fmt->sds_info[i].type = DFNT_FLOAT32;
197  fmt->sds_info[i].byt_per_val = 4;
198  } else if (strcmp(str_typ, "DFNT_FLOAT64") == 0) {
199  fmt->sds_info[i].type = DFNT_FLOAT64;
200  fmt->sds_info[i].byt_per_val = 4;
201  } else if (strcmp(str_typ, "DFNT_INT8") == 0) {
202  fmt->sds_info[i].type = DFNT_INT8;
203  fmt->sds_info[i].byt_per_val = 1;
204  } else if (strcmp(str_typ, "DFNT_UINT8") == 0) {
205  fmt->sds_info[i].type = DFNT_UINT8;
206  fmt->sds_info[i].byt_per_val = 1;
207  } else {
208  printf("**************Program error\n");
209  printf(
210  "Unplanned SDS format descript encountered for line %d of file:\n'%s'\n",
211  i, file);
212  printf("last line read:\n'%s'\n", line_sav);
213  fmt_status = fmt_status | 1;
214  return -1;
215  }
216  /*
217  * for optional range checking, get the range checking keyword
218  * to decide if any needs to be done
219  */
220  if ((str = s_parse(NULL, '\"')) == NULL) {
221  printf("**************Program error\n");
222  printf("range checking keyword must be provided\n");
223  printf("at field %d, line %d of file:\n'%s'\n",
224  7 + 2 * fmt->sds_info[i].rank, i, file);
225  fmt_status = fmt_status | 1;
226  return -1;
227  } else {
228  if (strcmp(str, "NO_MN_MX") == 0) {
229  fmt->sds_info[i].flg_rng = 0;
230  } else if (strcmp(str, "MN_MX_CK") == 0) {
231  fmt->sds_info[i].flg_rng = 1;
232 
233  for (j = 0; j < 2; j++) {
234  if ((str = s_parse(NULL, '\"')) == NULL) {
235  printf("**************Program error\n");
236  printf(
237  "unable to find SDS range value #%d in desc # %d of file:\n'%s'\n",
238  j, i, file);
239  printf("last line read:\n'%s'\n", line_sav);
240  fmt_status = fmt_status | 1;
241  return -1;
242  }
243  if ((ierr = var_decode(str, fmt->sds_info[i].type,
244  (void *) fmt->sds_info[i].sds_rng.i32, j, 1)) != 0) {
245  if (ierr == -1) {
246  printf("**************Program error\n");
247  printf("unable to decode SDS range value #%d"
248  " in description # %d of file: n'%s'\n", j, i, file);
249  printf("last line read:\n'%s'\n", line_sav);
250  fmt_status = fmt_status | 1;
251  return -1;
252  }
253  if (ierr == -2) {
254  printf("**************Program error\n");
255  printf("SDS range value # %d is out of range in descrip #"
256  " %d of file:\n'%s'\n", j, i, file);
257  printf("last line read:\n'%s'\n", line_sav);
258  fmt_status = fmt_status | 1;
259  return -1;
260  }
261  if (ierr == -3) {
262  printf("**************Program error\n");
263  printf("data type of: %d is not handled by fmt_read currently\n",
264  fmt->att[i].type);
265  printf("In descrip # %d of file:\n'%s'\n", i, file);
266  fmt_status = fmt_status | 1;
267  return -1;
268  }
269  }
270  }
271  } else {
272  printf("**************Program error\n");
273  printf(
274  "Range checking keyword must be either 'NO_MN_MX' or 'MN_MX_CK'\n");
275  printf("at field %d, line %d of file:\n'%s'\n",
276  7 + 2 * fmt->sds_info[i].rank, i, file);
277  fmt_status = fmt_status | 1;
278  return -1;
279  }
280  }
281  }
282  return 0;
283 }
char * get_line(char *, int, FILE *, int)
Definition: get_line.c:4
int j
Definition: decode_rs.h:73
#define NULL
Definition: decode_rs.h:63
HDF4 data type of the output SDS Default is DFNT_FLOAT32 Common types used DFNT_INT32
int var_decode(char *, int32, void *, int32, int32)
Definition: var_decode.c:8
no change in intended resolving MODur00064 Corrected handling of bad ephemeris attitude resolving resolving GSFcd00179 Corrected handling of fill values for[Sensor|Solar][Zenith|Azimuth] resolving MODxl01751 Changed to validate LUT version against a value retrieved from the resolving MODxl02056 Changed to calculate Solar Diffuser angles without adjustment for estimated post launch changes in the MODIS orientation relative to incidentally resolving defects MODxl01766 Also resolves MODxl01947 Changed to ignore fill values in SCI_ABNORM and SCI_STATE rather than treating them as resolving MODxl01780 Changed to use spacecraft ancillary data to recognise when the mirror encoder data is being set by side A or side B and to change calculations accordingly This removes the need for seperate LUTs for Side A and Side B data it makes the new LUTs incompatible with older versions of the and vice versa Also resolves MODxl01685 A more robust GRing algorithm is being which will create a non default GRing anytime there s even a single geolocated pixel in a granule Removed obsolete messages from seed file
Definition: HISTORY.txt:413
char * s_parse(char *, int)
Definition: s_parse.c:4
int fmt_rd_sds(char *file, FILE *fid, fmt_str *fmt)
Definition: fmt_rd_sds.c:7
HDF4 data type of the output SDS Default is DFNT_FLOAT32 Common types used DFNT_INT16
const char * str
Definition: l1c_msi.cpp:35
HDF4 data type of the output SDS Default is DFNT_FLOAT32 Common types used DFNT_FLOAT32
int fmt_status
Definition: fmt_check.c:7
int i
Definition: decode_rs.h:71
How many dimensions is the output array Default is Not sure if anything above will work correctly strcpy(l2prod->title, "no title yet")
int k
Definition: decode_rs.h:73