MISR Toolkit  1.5.1
MtkFileCoreMetaDataGet.c
Go to the documentation of this file.
1 /*===========================================================================
2 = =
3 = MtkFileCoreMetaDataGet =
4 = =
5 =============================================================================
6 
7  Jet Propulsion Laboratory
8  MISR
9  MISR Toolkit
10 
11  Copyright 2006, California Institute of Technology.
12  ALL RIGHTS RESERVED.
13  U.S. Government Sponsorship acknowledged.
14 
15 ============================================================================*/
16 
17 #include "MisrFileQuery.h"
18 #include "odldef.h"
19 #include "odlinter.h"
20 #include <errno.h>
21 #include <stdlib.h>
22 
41  const char *filename,
42  const char *param,
43  MtkCoreMetaData *metadata )
44 {
45  MTKt_status status; /* Return status */
46 
47  status = MtkFileCoreMetaDataGetNC(filename, param, metadata); // try netCDF
48  if (status != MTK_NETCDF_OPEN_FAILED) return status;
49 
50  return MtkFileCoreMetaDataGetHDF(filename, param, metadata); // try HDF
51 }
52 
54  const char *filename,
55  const char *param,
56  MtkCoreMetaData *metadata )
57 {
58  MTKt_status status_code; /* Return status of this function */
59  MTKt_status status; /* Return status */
60 
61  if (filename == NULL) return MTK_NULLPTR;
62 
63  /* Open file */
64  int ncid = 0;
65  {
66  int nc_status = nc_open(filename, NC_NOWRITE, &ncid);
68  }
69 
70  /* Read grid attribute */
71  status = MtkFileCoreMetaDataGetNcid(ncid, param, metadata);
72  MTK_ERR_COND_JUMP(status);
73 
74  /* Close file */
75  {
76  int nc_status = nc_close(ncid);
78  }
79  ncid = 0;
80 
81  return MTK_SUCCESS;
82 
83  ERROR_HANDLE:
84  if (ncid != 0) nc_close(ncid);
85  return status_code;
86 }
87 
89  const char *filename,
90  const char *param,
91  MtkCoreMetaData *metadata )
92 {
93  MTKt_status status_code; /* Return status of this function */
94  MTKt_status status;
95  int32 hdf_status; /* HDF-EOS return status */
96  int32 sd_id = FAIL; /* HDF SD file identifier. */
97 
98  if (filename == NULL)
100 
101  /* Open HDF File */
102  hdf_status = sd_id = SDstart(filename, DFACC_READ);
103  if (hdf_status == FAIL)
105 
106  /* Read coremetadata. */
107  status = MtkFileCoreMetaDataGetFid(sd_id, param, metadata);
108  MTK_ERR_COND_JUMP(status);
109 
110  /* Close HDF File */
111  hdf_status = SDend(sd_id);
112  if (hdf_status == FAIL)
114  sd_id = FAIL;
115 
116  return MTK_SUCCESS;
117 
118 ERROR_HANDLE:
119  if (sd_id != FAIL)
120  SDend(sd_id);
121 
122  return status_code;
123 }
124 
131  int32 sd_id,
132  const char *param,
133  MtkCoreMetaData *metadata )
134 {
135  MTKt_status status_code; /* Return status of this function */
136  MTKt_status status;
137  char *coremeta = NULL;
138  FILE *temp = NULL;
139 #ifdef _WIN32
140  char *temp_file_name;
141 #endif
143 
144  AGGREGATE aggNode = NULL;
145  GROUP grpNode = NULL;
146  OBJECT objNode = NULL;
147  PARAMETER parmNode = NULL;
148  VALUE valueNode = NULL;
149  char **strPtr1 = NULL;
150  int *intPtr = NULL;
151  double *dblPtr = NULL;
152  int odlRetVal = 0; /* ODl function return value */
153 
154  if (param == NULL || metadata == NULL)
156 
157  /* clear the errno for the ODL routines */
158  if (errno == ERANGE)
159  {
160  errno = 0;
161  }
162 
163  /* attributes are contained in a separate file */
164  aggNode = NewAggregate(aggNode, KA_GROUP, "locAggr", "");
165  if (aggNode == NULL)
166  {
167  printf("Unable to create odl aggregate locAggr\n");
169  }
170 
171  /* Read core metadata from HDF file */
172  status = MtkFileCoreMetaDataRawFid(sd_id, &coremeta);
173  MTK_ERR_COND_JUMP(status);
174 
175  /* ODL parser requires input from a FILE */
176  #ifdef _WIN32
177  /* On Win32 tmpfile() creates a file in the root directory, */
178  /* the user may not have write access.*/
179  temp_file_name = _tempnam(NULL, NULL);
180  temp = fopen(temp_file_name, "w+");
181  #else
182  temp = tmpfile();
183  #endif
184 
185  if (temp == NULL)
187 
188  fprintf(temp,"%s",coremeta);
189  rewind(temp);
190 
191  free(coremeta);
192  coremeta = NULL;
193 
194  odlRetVal = ReadLabel(temp, aggNode);
195  if (odlRetVal != 1)
196  {
197  printf("Unable to convert to an odl structure\n");
199  }
200 
201  fclose(temp);
202  temp = NULL;
203 
204  /* extract the values and dump in the buffer provided */
205  grpNode = FindGroup(aggNode, "INVENTORYMETADATA");
206  if (grpNode == NULL)
207  {
209  }
210 
211  objNode = FindObject(grpNode, (char *)param, NULL);
212  if (objNode == NULL)
213  {
215  }
216 
217  parmNode = FindParameter(objNode, "VALUE");
218  if (parmNode == NULL)
219  {
221  }
222 
223  valueNode = FirstValue(parmNode);
224 
225  /* copy the value string(s) into user provided string */
226  if (valueNode->item.type == TV_STRING || valueNode->item.type == TV_SYMBOL)
227  {
228  md.data.s = (char**)calloc(valueNode->parameter->value_count,sizeof(char*));
229  md.num_values = valueNode->parameter->value_count;
230  if (md.data.s == NULL)
232 
233  md.dataptr = md.data.s;
234  md.datatype = MTKMETA_CHAR;
235  strPtr1 = md.data.s;
236  while (valueNode != NULL)
237  {
238  *strPtr1 = (char*)malloc((strlen(valueNode->item.value.string) + 1) * sizeof(char));
239  if (*strPtr1 == NULL)
241 
242  strcpy(*strPtr1, valueNode->item.value.string);
243  strPtr1++;
244  valueNode = NextValue(valueNode);
245  }
246  }
247  else if (valueNode->item.type == TV_INTEGER)
248  {
249  md.data.i = (int*)malloc(valueNode->parameter->value_count * sizeof(int));
250  md.num_values = valueNode->parameter->value_count;
251  if (md.data.i == NULL)
253 
254  md.dataptr = md.data.i;
255  md.datatype = MTKMETA_INT;
256  intPtr = md.data.i;
257  while (valueNode != NULL)
258  {
259  *intPtr = (int)valueNode->item.value.integer.number;
260  intPtr++;
261  valueNode = NextValue(valueNode);
262  }
263  }
264  else
265  {
266  md.data.d = (double*)malloc(valueNode->parameter->value_count * sizeof(double));
267  md.num_values = valueNode->parameter->value_count;
268  if (md.data.d == NULL)
270 
271  md.dataptr = md.data.d;
272  md.datatype = MTKMETA_DOUBLE;
273  dblPtr = md.data.d;
274  while (valueNode != NULL)
275  {
276  *dblPtr = (double)valueNode->item.value.real.number;
277  dblPtr++;
278  valueNode = NextValue(valueNode);
279  }
280  }
281 
282  /* Destroy tree */
283  RemoveAggregate(aggNode);
284 
285  /* Copy data to output */
286  metadata->data.s = md.data.s;
287  metadata->num_values = md.num_values;
288  metadata->datatype = md.datatype;
289  metadata->dataptr = md.dataptr;
290 
291  return MTK_SUCCESS;
292 
293 ERROR_HANDLE:
294  RemoveAggregate(aggNode);
295 
296  if (temp != NULL)
297  fclose(temp);
298 
299  if (coremeta != NULL)
300  free(coremeta);
301 
302  MtkCoreMetaDataFree(&md);
303 
304  return status_code;
305 }
306 
308  int ncid,
309  const char *param,
310  MtkCoreMetaData *metadata )
311 {
312  MTKt_status status_code; /* Return status of this function */
313  MTKt_status status;
314  char *coremeta = NULL;
315  FILE *temp = NULL;
316 #ifdef _WIN32
317  char *temp_file_name;
318 #endif
320 
321  AGGREGATE aggNode = NULL;
322  GROUP grpNode = NULL;
323  OBJECT objNode = NULL;
324  PARAMETER parmNode = NULL;
325  VALUE valueNode = NULL;
326  char **strPtr1 = NULL;
327  int *intPtr = NULL;
328  double *dblPtr = NULL;
329  int odlRetVal = 0; /* ODl function return value */
330 
331  if (param == NULL || metadata == NULL)
333 
334  /* clear the errno for the ODL routines */
335  if (errno == ERANGE)
336  {
337  errno = 0;
338  }
339 
340  /* attributes are contained in a separate file */
341  aggNode = NewAggregate(aggNode, KA_GROUP, "locAggr", "");
342  if (aggNode == NULL)
343  {
344  printf("Unable to create odl aggregate locAggr\n");
346  }
347 
348  /* Read core metadata from HDF file */
349  status = MtkFileCoreMetaDataRawNcid(ncid, &coremeta);
350  MTK_ERR_COND_JUMP(status);
351 
352  /* ODL parser requires input from a FILE */
353  #ifdef _WIN32
354  /* On Win32 tmpfile() creates a file in the root directory, */
355  /* the user may not have write access.*/
356  temp_file_name = _tempnam(NULL, NULL);
357  temp = fopen(temp_file_name, "w+");
358  #else
359  temp = tmpfile();
360  #endif
361 
362  if (temp == NULL)
364 
365  fprintf(temp,"%s",coremeta);
366  rewind(temp);
367 
368  free(coremeta);
369  coremeta = NULL;
370 
371  odlRetVal = ReadLabel(temp, aggNode);
372  if (odlRetVal != 1)
373  {
374  printf("Unable to convert to an odl structure\n");
376  }
377 
378  fclose(temp);
379  temp = NULL;
380 
381  /* extract the values and dump in the buffer provided */
382  grpNode = FindGroup(aggNode, "INVENTORYMETADATA");
383  if (grpNode == NULL)
384  {
386  }
387 
388  objNode = FindObject(grpNode, (char *)param, NULL);
389  if (objNode == NULL)
390  {
392  }
393 
394  parmNode = FindParameter(objNode, "VALUE");
395  if (parmNode == NULL)
396  {
398  }
399 
400  valueNode = FirstValue(parmNode);
401 
402  /* copy the value string(s) into user provided string */
403  if (valueNode->item.type == TV_STRING || valueNode->item.type == TV_SYMBOL)
404  {
405  md.data.s = (char**)calloc(valueNode->parameter->value_count,sizeof(char*));
406  md.num_values = valueNode->parameter->value_count;
407  if (md.data.s == NULL)
409 
410  md.dataptr = md.data.s;
411  md.datatype = MTKMETA_CHAR;
412  strPtr1 = md.data.s;
413  while (valueNode != NULL)
414  {
415  *strPtr1 = (char*)malloc((strlen(valueNode->item.value.string) + 1) * sizeof(char));
416  if (*strPtr1 == NULL)
418 
419  strcpy(*strPtr1, valueNode->item.value.string);
420  strPtr1++;
421  valueNode = NextValue(valueNode);
422  }
423  }
424  else if (valueNode->item.type == TV_INTEGER)
425  {
426  md.data.i = (int*)malloc(valueNode->parameter->value_count * sizeof(int));
427  md.num_values = valueNode->parameter->value_count;
428  if (md.data.i == NULL)
430 
431  md.dataptr = md.data.i;
432  md.datatype = MTKMETA_INT;
433  intPtr = md.data.i;
434  while (valueNode != NULL)
435  {
436  *intPtr = (int)valueNode->item.value.integer.number;
437  intPtr++;
438  valueNode = NextValue(valueNode);
439  }
440  }
441  else
442  {
443  md.data.d = (double*)malloc(valueNode->parameter->value_count * sizeof(double));
444  md.num_values = valueNode->parameter->value_count;
445  if (md.data.d == NULL)
447 
448  md.dataptr = md.data.d;
449  md.datatype = MTKMETA_DOUBLE;
450  dblPtr = md.data.d;
451  while (valueNode != NULL)
452  {
453  *dblPtr = (double)valueNode->item.value.real.number;
454  dblPtr++;
455  valueNode = NextValue(valueNode);
456  }
457  }
458 
459  /* Destroy tree */
460  RemoveAggregate(aggNode);
461 
462  /* Copy data to output */
463  metadata->data.s = md.data.s;
464  metadata->num_values = md.num_values;
465  metadata->datatype = md.datatype;
466  metadata->dataptr = md.dataptr;
467 
468  return MTK_SUCCESS;
469 
470 ERROR_HANDLE:
471  RemoveAggregate(aggNode);
472 
473  if (temp != NULL)
474  fclose(temp);
475 
476  if (coremeta != NULL)
477  free(coremeta);
478 
479  MtkCoreMetaDataFree(&md);
480 
481  return status_code;
482 }
#define DFACC_READ
Definition: hdf.h:44
char * filename
Definition: cdjpeg.h:133
union MtkCoreMetaData::@0 data
MTKt_status MtkFileCoreMetaDataGetHDF(const char *filename, const char *param, MtkCoreMetaData *metadata)
AGGREGATE RemoveAggregate(AGGREGATE base_node)
Definition: a_nodesa.c:208
MTKt_status MtkCoreMetaDataFree(MtkCoreMetaData *metadata)
Free core metadata.
struct ODLInteger integer
Definition: odldef.h:219
long value_count
Definition: odldef.h:142
#define MTK_ERR_CODE_JUMP(code)
Definition: MisrError.h:175
GROUP FindGroup(AGGREGATE base_node, char *name)
Definition: ag_nodesag.c:161
OBJECT FindObject(AGGREGATE base_node, char *name, char *thisClass)
Definition: ao_nodesao.c:177
MTKt_status MtkFileCoreMetaDataGetFid(int32 sd_id, const char *param, MtkCoreMetaData *metadata)
Version of MtkFileCoreMetaDataGet that takes an HDF SD identifier rather than a filename.
double number
Definition: odldef.h:184
HDFLIBAPI int32 SDstart(const char *name, int32 accs)
#define NC_NOERR
Definition: netcdf.h:313
Core Metadata.
Definition: MisrFileQuery.h:57
MTKt_status MtkFileCoreMetaDataGet(const char *filename, const char *param, MtkCoreMetaData *metadata)
Get core metadata parameter.
#define MTK_CORE_METADATA_INIT
Definition: MisrFileQuery.h:72
enum MtkCoreMetaData::@1 datatype
struct ODLReal real
Definition: odldef.h:220
struct Value_Data item
Definition: odldef.h:229
MTKt_status MtkFileCoreMetaDataGetNcid(int ncid, const char *param, MtkCoreMetaData *metadata)
HDFLIBAPI intn SDend(int32 fid)
int ReadLabel()
char * string
Definition: odldef.h:222
#define NC_NOWRITE
Definition: netcdf.h:201
MTKt_status MtkFileCoreMetaDataRawFid(int32 sds_id, char **coremeta)
Version of MtkFileCoreMetaDataRaw that takes an HDF SD file identifier rather than a filename...
VALUE_TYPE type
Definition: odldef.h:213
PARAMETER FindParameter()
struct Parameter_Node * parameter
Definition: odldef.h:230
MTKt_status MtkFileCoreMetaDataRawNcid(int ncid, char **coremeta)
VALUE FirstValue()
#define MTK_ERR_COND_JUMP(code)
Definition: MisrError.h:188
MTKt_status MtkFileCoreMetaDataGetNC(const char *filename, const char *param, MtkCoreMetaData *metadata)
MTKt_status
Definition: MisrError.h:11
VALUE NextValue()
EXTERNL int nc_close(int ncid)
AGGREGATE NewAggregate(AGGREGATE base_node, AGGREGATE_KIND kind, char *name, char *classType)
Definition: a_nodesa.c:75
union Value_Data::@2 value
EXTERNL int nc_open(const char *path, int mode, int *ncidp)
#define FAIL
Definition: hdf.h:94
long number
Definition: odldef.h:191

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