MISR Toolkit  1.5.1
MtkFileCoreMetaDataQuery.c
Go to the documentation of this file.
1 /*===========================================================================
2 = =
3 = MtkFileCoreMetaDataQuery =
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 
40  const char *filename,
41  int *nparam,
42  char ***paramlist )
43 {
44  MTKt_status status; /* Return status */
45 
46  status = MtkFileCoreMetaDataQueryNC(filename, nparam, paramlist); // try netCDF
47  if (status != MTK_NETCDF_OPEN_FAILED) return status;
48 
49  return MtkFileCoreMetaDataQueryHDF(filename, nparam, paramlist); // try HDF
50 }
51 
53  const char *filename,
54  int *nparam,
55  char ***paramlist )
56 {
57  MTKt_status status_code; /* Return status of this function */
58  MTKt_status status; /* Return status */
59 
60  if (filename == NULL) return MTK_NULLPTR;
61 
62  /* Open file */
63  int ncid = 0;
64  {
65  int nc_status = nc_open(filename, NC_NOWRITE, &ncid);
67  }
68 
69  /* Read grid attribute */
70  status = MtkFileCoreMetaDataQueryNcid(ncid, nparam, paramlist);
71  MTK_ERR_COND_JUMP(status);
72 
73  /* Close file */
74  {
75  int nc_status = nc_close(ncid);
77  }
78  ncid = 0;
79 
80  return MTK_SUCCESS;
81 
82  ERROR_HANDLE:
83  if (ncid != 0) nc_close(ncid);
84  return status_code;
85 }
86 
88  const char *filename,
89  int *nparam,
90  char ***paramlist )
91 {
92  MTKt_status status_code; /* Return status of this function */
93  MTKt_status status;
94  int32 hdf_status; /* HDF-EOS return status */
95  int32 sd_id = FAIL; /* HDF SD file identifier. */
96 
97  if (filename == NULL)
99 
100  /* Open HDF File */
101  hdf_status = sd_id = SDstart(filename, DFACC_READ);
102  if (hdf_status == FAIL)
104 
105  /* Read coremetadata. */
106  status = MtkFileCoreMetaDataQueryFid(sd_id, nparam, paramlist);
107  MTK_ERR_COND_JUMP(status);
108 
109  /* Close HDF File */
110  hdf_status = SDend(sd_id);
111  if (hdf_status == FAIL)
113  sd_id = FAIL;
114 
115  return MTK_SUCCESS;
116 
117 ERROR_HANDLE:
118  if (sd_id != FAIL)
119  SDend(sd_id);
120 
121  return status_code;
122 }
123 
130  int32 sd_id,
131  int *nparam,
132  char ***paramlist )
133 {
134  MTKt_status status_code; /* Return status of this function */
135  MTKt_status status;
136  char *coremeta = NULL;
137  FILE *temp = NULL;
138 #ifdef _WIN32
139  char *temp_file_name;
140 #endif
141  char **temp_list = NULL;
142  int temp_count = 0;
143  int temp_list_max = 50;
144  int i;
145 
146  AGGREGATE aggNode = NULL;
147  GROUP grpNode = NULL;
148  int odlRetVal = 0; /* ODl function return value */
149  AGGREGATE base_node;
150  AGGREGATE node; /* Pointer to current node */
151 
152  if (nparam == NULL || paramlist == NULL)
154 
155  /* clear the errno for the ODL routines */
156  if (errno == ERANGE)
157  {
158  errno = 0;
159  }
160 
161  /* attributes are contained in a separate file */
162  aggNode = NewAggregate(aggNode, KA_GROUP, "locAggr", "");
163  if (aggNode == NULL)
164  {
165  printf("Unable to create odl aggregate locAggr\n");
167  }
168 
169  /* Read core metadata from HDF file */
170  status = MtkFileCoreMetaDataRawFid(sd_id, &coremeta);
171  MTK_ERR_COND_JUMP(status);
172 
173  /* ODL parser requires input from a FILE */
174  #ifdef _WIN32
175  /* On Win32 tmpfile() creates a file in the root directory, */
176  /* the user may not have write access.*/
177  temp_file_name = _tempnam(NULL, NULL);
178  temp = fopen(temp_file_name, "w+");
179  #else
180  temp = tmpfile();
181  #endif
182 
183  if (temp == NULL)
185 
186  fprintf(temp,"%s",coremeta);
187  rewind(temp);
188 
189  free(coremeta);
190  coremeta = NULL;
191 
192  odlRetVal = ReadLabel(temp, aggNode);
193  if (odlRetVal != 1)
194  {
195  printf("Unable to convert to an odl structure\n");
197  }
198 
199  fclose(temp);
200 
201  /* extract the values and dump in the buffer provided */
202  grpNode = FindGroup(aggNode, "INVENTORYMETADATA");
203  if (grpNode == NULL)
204  {
206  }
207 
208  /* Start searching with the base node and stop searching when we
209  have visited all of the progeny of the base node */
210 
211  temp_list = (char**)calloc(temp_list_max, sizeof(char*));
212  if (temp_list == NULL)
214 
215  node = base_node = aggNode;
216 
217  while (node != NULL)
218  {
219  if (node->kind == KA_OBJECT)
220  {
221  if (temp_list_max == temp_count)
222  {
223  temp_list_max += 50;
224  temp_list = (char**)realloc(temp_list,temp_list_max);
225  if (temp_list == NULL)
227  }
228 
229  temp_list[temp_count] = (char*)malloc((strlen(node->name) + 1) *
230  sizeof(char));
231  if (temp_list[temp_count] == NULL)
233 
234  strcpy(temp_list[temp_count],node->name);
235  ++temp_count;
236  }
237 
238  node = NextSubObject (base_node, node);
239  }
240 
241  RemoveAggregate(aggNode);
242 
243  /* Copy list to output arguments */
244  *nparam = temp_count;
245 
246  *paramlist = (char**)malloc(temp_count * sizeof(char*));
247  if (*paramlist == NULL)
249 
250  for (i = 0; i < temp_count; ++i)
251  (*paramlist)[i] = temp_list[i];
252 
253  free(temp_list);
254 
255  return MTK_SUCCESS;
256 
257 ERROR_HANDLE:
258  RemoveAggregate(aggNode);
259 
260  if (temp != NULL)
261  fclose(temp);
262 
263  if (coremeta != NULL)
264  free(coremeta);
265 
266  if (temp_list != NULL)
267  {
268  for (i = 0; i < temp_count; ++i)
269  free(temp_list[i]);
270 
271  free(temp_list);
272  }
273 
274  return status_code;
275 }
276 
278  int ncid,
279  int *nparam,
280  char ***paramlist )
281 {
282  MTKt_status status_code; /* Return status of this function */
283  MTKt_status status;
284  char *coremeta = NULL;
285  FILE *temp = NULL;
286 #ifdef _WIN32
287  char *temp_file_name;
288 #endif
289  char **temp_list = NULL;
290  int temp_count = 0;
291  int temp_list_max = 50;
292  int i;
293 
294  AGGREGATE aggNode = NULL;
295  GROUP grpNode = NULL;
296  int odlRetVal = 0; /* ODl function return value */
297  AGGREGATE base_node;
298  AGGREGATE node; /* Pointer to current node */
299 
300  if (nparam == NULL || paramlist == NULL)
302 
303  /* clear the errno for the ODL routines */
304  if (errno == ERANGE)
305  {
306  errno = 0;
307  }
308 
309  /* attributes are contained in a separate file */
310  aggNode = NewAggregate(aggNode, KA_GROUP, "locAggr", "");
311  if (aggNode == NULL)
312  {
313  printf("Unable to create odl aggregate locAggr\n");
315  }
316 
317  /* Read core metadata from HDF file */
318  status = MtkFileCoreMetaDataRawNcid(ncid, &coremeta);
319  MTK_ERR_COND_JUMP(status);
320 
321  /* ODL parser requires input from a FILE */
322  #ifdef _WIN32
323  /* On Win32 tmpfile() creates a file in the root directory, */
324  /* the user may not have write access.*/
325  temp_file_name = _tempnam(NULL, NULL);
326  temp = fopen(temp_file_name, "w+");
327  #else
328  temp = tmpfile();
329  #endif
330 
331  if (temp == NULL)
333 
334  fprintf(temp,"%s",coremeta);
335  rewind(temp);
336 
337  free(coremeta);
338  coremeta = NULL;
339 
340  odlRetVal = ReadLabel(temp, aggNode);
341  if (odlRetVal != 1)
342  {
343  printf("Unable to convert to an odl structure\n");
345  }
346 
347  fclose(temp);
348 
349  /* extract the values and dump in the buffer provided */
350  grpNode = FindGroup(aggNode, "INVENTORYMETADATA");
351  if (grpNode == NULL)
352  {
354  }
355 
356  /* Start searching with the base node and stop searching when we
357  have visited all of the progeny of the base node */
358 
359  temp_list = (char**)calloc(temp_list_max, sizeof(char*));
360  if (temp_list == NULL)
362 
363  node = base_node = aggNode;
364 
365  while (node != NULL)
366  {
367  if (node->kind == KA_OBJECT)
368  {
369  if (temp_list_max == temp_count)
370  {
371  temp_list_max += 50;
372  temp_list = (char**)realloc(temp_list,temp_list_max);
373  if (temp_list == NULL)
375  }
376 
377  temp_list[temp_count] = (char*)malloc((strlen(node->name) + 1) *
378  sizeof(char));
379  if (temp_list[temp_count] == NULL)
381 
382  strcpy(temp_list[temp_count],node->name);
383  ++temp_count;
384  }
385 
386  node = NextSubObject (base_node, node);
387  }
388 
389  RemoveAggregate(aggNode);
390 
391  /* Copy list to output arguments */
392  *nparam = temp_count;
393 
394  *paramlist = (char**)malloc(temp_count * sizeof(char*));
395  if (*paramlist == NULL)
397 
398  for (i = 0; i < temp_count; ++i)
399  (*paramlist)[i] = temp_list[i];
400 
401  free(temp_list);
402 
403  return MTK_SUCCESS;
404 
405 ERROR_HANDLE:
406  RemoveAggregate(aggNode);
407 
408  if (temp != NULL)
409  fclose(temp);
410 
411  if (coremeta != NULL)
412  free(coremeta);
413 
414  if (temp_list != NULL)
415  {
416  for (i = 0; i < temp_count; ++i)
417  free(temp_list[i]);
418 
419  free(temp_list);
420  }
421 
422  return status_code;
423 }
#define DFACC_READ
Definition: hdf.h:44
char * filename
Definition: cdjpeg.h:133
MTKt_status MtkFileCoreMetaDataQuery(const char *filename, int *nparam, char ***paramlist)
Query file for core metadata.
AGGREGATE RemoveAggregate(AGGREGATE base_node)
Definition: a_nodesa.c:208
MTKt_status MtkFileCoreMetaDataQueryNC(const char *filename, int *nparam, char ***paramlist)
MTKt_status MtkFileCoreMetaDataQueryHDF(const char *filename, int *nparam, char ***paramlist)
#define MTK_ERR_CODE_JUMP(code)
Definition: MisrError.h:175
GROUP FindGroup(AGGREGATE base_node, char *name)
Definition: ag_nodesag.c:161
AGGREGATE_KIND kind
Definition: odldef.h:115
HDFLIBAPI int32 SDstart(const char *name, int32 accs)
#define NC_NOERR
Definition: netcdf.h:313
MTKt_status MtkFileCoreMetaDataQueryNcid(int ncid, int *nparam, char ***paramlist)
MTKt_status MtkFileCoreMetaDataQueryFid(int32 sd_id, int *nparam, char ***paramlist)
Version of MtkFileCoreMetaDataQuery that takes an HDF SD file identifier rather than a filename...
HDFLIBAPI intn SDend(int32 fid)
int ReadLabel()
OBJECT NextSubObject(AGGREGATE base_node, AGGREGATE start_node)
Definition: ao_nodesao.c:130
#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...
MTKt_status MtkFileCoreMetaDataRawNcid(int ncid, char **coremeta)
#define MTK_ERR_COND_JUMP(code)
Definition: MisrError.h:188
MTKt_status
Definition: MisrError.h:11
EXTERNL int nc_close(int ncid)
AGGREGATE NewAggregate(AGGREGATE base_node, AGGREGATE_KIND kind, char *name, char *classType)
Definition: a_nodesa.c:75
EXTERNL int nc_open(const char *path, int mode, int *ncidp)
char * name
Definition: odldef.h:112
#define FAIL
Definition: hdf.h:94

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