MISR Toolkit  1.5.1
MtkFileBlockMetaList.c
Go to the documentation of this file.
1 /*===========================================================================
2 = =
3 = MtkFileBlockMetaList =
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 #include <stdlib.h>
23 
24 
41  const char *filename,
42  int *nblockmeta,
43  char ***blockmetalist )
44 {
45  MTKt_status status_code; /* Return status of this function */
46  MTKt_status status; /* Return status */
47  intn hdf_status;
48  int32 file_id = FAIL;
49 
50  if (filename == NULL)
52 
53  /* Open HDF File */
54  file_id = HDFopen(filename, DFACC_READ, 0);
55  if (file_id == FAIL)
57 
58  /* Read list of block metadata structures. */
59  status = MtkFileBlockMetaListFid(file_id, nblockmeta, blockmetalist);
60  MTK_ERR_COND_JUMP(status);
61 
62  /* Close HDF file */
63  hdf_status = HDFclose(file_id);
64  if (hdf_status == FAIL)
66  file_id = FAIL;
67 
68  return MTK_SUCCESS;
69 
70 ERROR_HANDLE:
71  if (file_id != FAIL)
72  HDFclose(file_id);
73 
74  return status_code;
75 }
76 
83  int32 file_id,
84  int *nblockmeta,
85  char ***blockmetalist )
86 {
87  MTKt_status status_code; /* Return status of this function */
88  /*MTKt_status status;*/ /* Return status */
89  intn hdf_status;
90  int32 vdata_ref = FAIL;
91  int32 vdata_id = FAIL;
92  char vdata_name[VSNAMELENMAX + 1];
93  char **temp_list = NULL;
94  int temp_count = 0;
95  int temp_list_max = 5;
96  int i;
97  int vstart_active = 0;
98 
99  if (nblockmeta == NULL || blockmetalist == NULL)
101 
102  /* Initialize the vdata interface */
103  hdf_status = Vstart(file_id);
104  if (hdf_status == FAIL)
106  vstart_active = 1;
107 
108  temp_list = (char**)calloc(temp_list_max, sizeof(char*));
109  if (temp_list == NULL)
111 
112  /* Search through file for vdatas */
113  vdata_ref = VSgetid(file_id, vdata_ref);
114  while (vdata_ref != FAIL)
115  {
116  vdata_id = VSattach(file_id, vdata_ref, "r");
117  if (vdata_id == FAIL)
119 
120  hdf_status = VSgetname(vdata_id, vdata_name);
121  if (hdf_status == FAIL)
123 
124  if (strstr(vdata_name,"Metadata") != NULL && strstr(vdata_name,"Block") != NULL)
125  {
126  if (temp_list_max == temp_count)
127  {
128  temp_list_max += 5;
129  temp_list = (char**)realloc(temp_list,temp_list_max);
130  if (temp_list == NULL)
132  }
133 
134  temp_list[temp_count] = (char*)malloc((strlen(vdata_name) + 1) *
135  sizeof(char));
136  if (temp_list[temp_count] == NULL)
138 
139  strcpy(temp_list[temp_count],vdata_name);
140  ++temp_count;
141  }
142 
143  hdf_status = VSdetach(vdata_id);
144  if (hdf_status == FAIL)
146  vdata_id = FAIL;
147 
148  vdata_ref = VSgetid(file_id, vdata_ref);
149  }
150 
151  /* End access to the vdata interface */
152  hdf_status = Vend(file_id);
153  if (hdf_status == FAIL)
155  vstart_active = 0;
156 
157  /* Copy list to output arguments */
158  *nblockmeta = temp_count;
159 
160  *blockmetalist = (char**)malloc(temp_count * sizeof(char*));
161  if (*blockmetalist == NULL)
163 
164  for (i = 0; i < temp_count; ++i)
165  (*blockmetalist)[i] = temp_list[i];
166 
167  free(temp_list);
168 
169  return MTK_SUCCESS;
170 
171 ERROR_HANDLE:
172  if (vdata_id != FAIL)
173  VSdetach(vdata_id);
174 
175  if (vstart_active)
176  Vend(file_id);
177 
178  MtkStringListFree(temp_count, &temp_list);
179 
180  return status_code;
181 }
MTKt_status MtkStringListFree(int strcnt, char **strlist[])
Free string list.
#define VSNAMELENMAX
Definition: hlimits.h:69
MTKt_status MtkFileBlockMetaList(const char *filename, int *nblockmeta, char ***blockmetalist)
List block metadata structures.
#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
#define HDFopen(f, a, d)
Definition: hdf.h:160
HDFLIBAPI int32 VSgetid(HFILEID f, int32 vsref)
HDFLIBAPI int32 VSgetname(int32 vkey, char *vsname)
#define Vstart(f)
Definition: hdf.h:162
MTKt_status MtkFileBlockMetaListFid(int32 file_id, int *nblockmeta, char ***blockmetalist)
Version of MtkFileBlockMetaList that takes an HDF file identifier rather than a filename.
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
#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