MISR Toolkit  1.5.1
MtkFileBlockMetaFieldList.c
Go to the documentation of this file.
1 /*===========================================================================
2 = =
3 = MtkFileBlockMetaFieldList =
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 "MisrUtil.h"
19 #include "MisrError.h"
20 #include <hdf.h>
21 #include <string.h>
22 
40  const char *filename,
41  const char *blockmetaname,
42  int *nfields,
43  char **fieldlist[] )
44 {
45  MTKt_status status_code; /* Return status of this function. */
46  MTKt_status status; /* Return status */
47  intn hdfstatus; /* HDF-EOS return status */
48  int32 file_id = FAIL;
49 
50  /* Check Arguments */
51  if (filename == NULL)
53 
54  /* Open HDF File */
55  file_id = HDFopen(filename, DFACC_READ, 0);
56  if (file_id == FAIL)
58 
59  /* Read list of fields in a block metadata structure */
60  status = MtkFileBlockMetaFieldListFid(file_id, blockmetaname,
61  nfields, fieldlist);
62  MTK_ERR_COND_JUMP(status);
63 
64  /* Close HDF file */
65  hdfstatus = HDFclose(file_id);
66  if (hdfstatus == FAIL)
68  file_id = FAIL;
69 
70  return MTK_SUCCESS;
71 
72 ERROR_HANDLE:
73  if (file_id != FAIL)
74  HDFclose(file_id);
75 
76  return status_code;
77 }
78 
85  int32 file_id,
86  const char *blockmetaname,
87  int *nfields,
88  char **fieldlist[] )
89 {
90  MTKt_status status_code; /* Return status of this function. */
91  intn hdfstatus; /* HDF-EOS return status */
92  int32 vdata_ref = FAIL;
93  int32 vdata_id = FAIL;
94  int32 num_fields = 0; /* Number of fields */
95  char *list = NULL; /* List of fields */
96  int i;
97  char *temp = NULL;
98  int vstart_active = 0;
99 
100  /* Check Arguments */
101  if (blockmetaname == NULL ||
102  nfields == NULL || fieldlist == NULL)
104 
105  *fieldlist = NULL;
106 
107  /* Initialize the vdata interface */
108  hdfstatus = Vstart(file_id);
109  if (hdfstatus == FAIL)
111  vstart_active = 1;
112 
113  /* Find reference number of block metadata structure */
114  vdata_ref = VSfind(file_id, blockmetaname);
115  if (vdata_ref == 0) /* Failure */
117 
118  vdata_id = VSattach(file_id, vdata_ref, "r");
119  if (vdata_id == FAIL)
121 
122  /* Determine number of fields in block metadata structure */
123  num_fields = VFnfields(vdata_id);
124  if (num_fields == FAIL)
126 
127  list = (char*)malloc((VSNAMELENMAX + 1) * num_fields * sizeof(char));
128  if (list == NULL)
130 
131  /* Get list of fields */
132  hdfstatus = VSgetfields(vdata_id, list);
133  if (hdfstatus == FAIL)
135 
136  *nfields = num_fields;
137  *fieldlist = (char**)calloc(num_fields,sizeof(char*));
138  if (*fieldlist == NULL)
140 
141  temp = strtok(list,",");
142  i = 0;
143  while (temp != NULL)
144  {
145  (*fieldlist)[i] = (char*)malloc((strlen(temp) + 1) * sizeof(char));
146  if ((*fieldlist)[i] == NULL)
148  strcpy((*fieldlist)[i],temp);
149  temp = strtok(NULL,",");
150  ++i;
151  }
152 
153  free(list);
154  list = NULL;
155  temp = NULL;
156 
157  hdfstatus = VSdetach(vdata_id);
158  if (hdfstatus == FAIL)
160 
161  /* End access to the vdata interface */
162  hdfstatus = Vend(file_id);
163  if (hdfstatus == FAIL)
165  vstart_active = 0;
166 
167  return MTK_SUCCESS;
168 
169 ERROR_HANDLE:
170  if (vdata_id != FAIL)
171  VSdetach(vdata_id);
172 
173  if (vstart_active)
174  Vend(file_id);
175 
176  free(list);
177 
178  if (fieldlist != NULL && *fieldlist != NULL)
179  MtkStringListFree(num_fields,&*fieldlist);
180 
181  return status_code;
182 }
MTKt_status MtkStringListFree(int strcnt, char **strlist[])
Free string list.
#define VSNAMELENMAX
Definition: hlimits.h:69
#define DFACC_READ
Definition: hdf.h:44
char * filename
Definition: cdjpeg.h:133
HDFLIBAPI int32 VSdetach(int32 vkey)
#define MTK_ERR_CODE_JUMP(code)
Definition: MisrError.h:175
MTKt_status MtkFileBlockMetaFieldList(const char *filename, const char *blockmetaname, int *nfields, char **fieldlist[])
Read list of fields in a block metadata structure.
#define HDFopen(f, a, d)
Definition: hdf.h:160
HDFLIBAPI int32 VSfind(HFILEID f, const char *vsname)
#define Vstart(f)
Definition: hdf.h:162
HDFLIBAPI int32 VSgetfields(int32 vkey, char *fields)
HDFLIBAPI int32 VSattach(HFILEID f, int32 vsref, const char *accesstype)
#define MTK_ERR_COND_JUMP(code)
Definition: MisrError.h:188
MTKt_status
Definition: MisrError.h:11
MTKt_status MtkFileBlockMetaFieldListFid(int32 file_id, const char *blockmetaname, int *nfields, char **fieldlist[])
Version of MtkFileBlockMetaFieldList that takes an HDF file identifier rather than a filename...
HDFLIBAPI int32 VFnfields(int32 vkey)
#define Vend(f)
Definition: hdf.h:163
#define FAIL
Definition: hdf.h:94
#define HDFclose(f)
Definition: hdf.h:161

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