MISR Toolkit  1.5.1
MtkFileAttrList.c
Go to the documentation of this file.
1 /*===========================================================================
2 = =
3 = MtkFileAttrList =
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 "MisrError.h"
19 #include <mfhdf.h>
20 #include <stdlib.h>
21 #include <string.h>
22 
39  const char *filename,
40  int *num_attrs,
41  char **attrlist[] )
42 {
43  MTKt_status status; /* Return status */
44 
45  status = MtkFileAttrListNC(filename, num_attrs, attrlist); // try netCDF
46  if (status != MTK_NETCDF_OPEN_FAILED) return status;
47 
48  return MtkFileAttrListHDF(filename, num_attrs, attrlist); // try HDF
49 }
50 
52  const char *filename,
53  int *num_attrs,
54  char **attrlist[] )
55 {
56  MTKt_status status_code; /* Return status of this function */
57  MTKt_status status; /* Return status */
58 
59  if (filename == NULL) return MTK_NULLPTR;
60 
61  /* Open file */
62  int ncid = 0;
63  {
64  int nc_status = nc_open(filename, NC_NOWRITE, &ncid);
66  }
67 
68  /* Read grid attribute */
69  status = MtkFileAttrListNcid(ncid, num_attrs, attrlist);
70  MTK_ERR_COND_JUMP(status);
71 
72  /* Close file */
73  {
74  int nc_status = nc_close(ncid);
76  }
77  ncid = 0;
78 
79  return MTK_SUCCESS;
80 
81  ERROR_HANDLE:
82  if (ncid != 0) nc_close(ncid);
83  return status_code;
84 }
85 
87  const char *filename,
88  int *num_attrs,
89  char **attrlist[] )
90 {
91  MTKt_status status_code; /* Return status of this function */
92  MTKt_status status; /* Return status */
93  intn hdf_status;
94  int32 sd_id = FAIL;
95 
96  if (filename == NULL)
98 
99  /* Open HDF File */
100  hdf_status = sd_id = SDstart(filename, DFACC_READ);
101  if (hdf_status == FAIL)
103 
104  /* Read attrribute list. */
105  status = MtkFileAttrListFid(sd_id, num_attrs, attrlist);
106  MTK_ERR_COND_JUMP(status);
107 
108  /* Close HDF File */
109  hdf_status = SDend(sd_id);
110  if (hdf_status == FAIL)
112  sd_id = FAIL;
113 
114  return MTK_SUCCESS;
115 
116 ERROR_HANDLE:
117  if (sd_id != FAIL)
118  SDend(sd_id);
119  return status_code;
120 }
121 
128  int32 sd_id,
129  int *num_attrs,
130  char **attrlist[] )
131 {
132  MTKt_status status_code; /* Return status of this function */
133  intn hdf_status;
134  int32 num_datasets;
135  int32 num_global_attrs = 0;
136  char attr_name[H4_MAX_NC_NAME];
137  int32 attr_index = 0;
138  int32 hdf_datatype;
139  int32 count;
140  char **attrlist_tmp = NULL;
141 
142  if (num_attrs == NULL || attrlist == NULL)
144 
145  /* Get number of gloabl attributes */
146  hdf_status = SDfileinfo(sd_id, &num_datasets, &num_global_attrs);
147  if (hdf_status == FAIL)
149 
150  /* Temp attribute list */
151  attrlist_tmp = (char**)calloc(num_global_attrs,sizeof(char**));
152  if (attrlist_tmp == NULL)
154 
155  for (attr_index = 0; attr_index < num_global_attrs; ++attr_index)
156  {
157  /* Get attribute information */
158  hdf_status = SDattrinfo(sd_id, attr_index, attr_name, &hdf_datatype, &count);
159  if (hdf_status == FAIL)
161 
162  attrlist_tmp[attr_index] = (char*)malloc((strlen(attr_name) + 1) * sizeof(char));
163  if (attrlist_tmp[attr_index] == NULL)
165 
166  strcpy(attrlist_tmp[attr_index],attr_name);
167  }
168 
169  *attrlist = attrlist_tmp;
170  *num_attrs = num_global_attrs;
171 
172  return MTK_SUCCESS;
173 
174 ERROR_HANDLE:
175 
176  MtkStringListFree(num_global_attrs, &attrlist_tmp);
177 
178  return status_code;
179 }
180 
182  int ncid,
183  int *num_attrs,
184  char **attrlist[] )
185 {
186  MTKt_status status_code; /* Return status of this function */
187  int32 num_global_attrs = 0;
188  int32 attr_index = 0;
189  char **attrlist_tmp = NULL;
190 
191  if (num_attrs == NULL || attrlist == NULL)
193 
194  {
195  int nc_status = nc_inq_varnatts(ncid, NC_GLOBAL, &num_global_attrs);
197  }
198 
199  /* Temp attribute list */
200  attrlist_tmp = (char**)calloc(num_global_attrs,sizeof(char**));
201  if (attrlist_tmp == NULL)
203 
204  for (attr_index = 0; attr_index < num_global_attrs; ++attr_index)
205  {
206  /* Get attribute information */
207  char attr_name[NC_MAX_NAME+1];
208  int nc_status = nc_inq_attname(ncid, NC_GLOBAL, attr_index, attr_name);
210 
211  attrlist_tmp[attr_index] = (char*)malloc((strlen(attr_name) + 1) * sizeof(char));
212  if (attrlist_tmp[attr_index] == NULL)
214 
215  strcpy(attrlist_tmp[attr_index],attr_name);
216  }
217 
218  *attrlist = attrlist_tmp;
219  *num_attrs = num_global_attrs;
220 
221  return MTK_SUCCESS;
222 
223 ERROR_HANDLE:
224 
225  MtkStringListFree(num_global_attrs, &attrlist_tmp);
226 
227  return status_code;
228 }
HDFFCLIBAPI intf intf intf * count
MTKt_status MtkStringListFree(int strcnt, char **strlist[])
Free string list.
MTKt_status MtkFileAttrListNcid(int ncid, int *num_attrs, char **attrlist[])
MTKt_status MtkFileAttrListFid(int32 sd_id, int *num_attrs, char **attrlist[])
Version of MtkFileAttrList that takes an HDF SD file identifier rather than a filename.
#define DFACC_READ
Definition: hdf.h:44
char * filename
Definition: cdjpeg.h:133
MTKt_status MtkFileAttrList(const char *filename, int *num_attrs, char **attrlist[])
Get a list of file attributes.
#define MTK_ERR_CODE_JUMP(code)
Definition: MisrError.h:175
#define NC_GLOBAL
Definition: netcdf.h:214
EXTERNL int nc_inq_varnatts(int ncid, int varid, int *nattsp)
HDFLIBAPI int32 SDstart(const char *name, int32 accs)
#define NC_NOERR
Definition: netcdf.h:313
#define NC_MAX_NAME
Definition: netcdf.h:275
HDFLIBAPI intn SDend(int32 fid)
#define NC_NOWRITE
Definition: netcdf.h:201
HDFLIBAPI intn SDattrinfo(int32 id, int32 idx, char *name, int32 *nt, int32 *count)
EXTERNL int nc_inq_attname(int ncid, int varid, int attnum, char *name)
HDFLIBAPI intn SDfileinfo(int32 fid, int32 *datasets, int32 *attrs)
#define MTK_ERR_COND_JUMP(code)
Definition: MisrError.h:188
#define H4_MAX_NC_NAME
Definition: hlimits.h:189
MTKt_status
Definition: MisrError.h:11
MTKt_status MtkFileAttrListHDF(const char *filename, int *num_attrs, char **attrlist[])
EXTERNL int nc_close(int ncid)
MTKt_status MtkFileAttrListNC(const char *filename, int *num_attrs, char **attrlist[])
EXTERNL int nc_open(const char *path, int mode, int *ncidp)
#define FAIL
Definition: hdf.h:94

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