MISR Toolkit  1.5.1
MtkGridAttrList.c
Go to the documentation of this file.
1 /*===========================================================================
2 = =
3 = MtkGridAttrList =
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 <HdfEosDef.h>
21 #include <stdlib.h>
22 #include <string.h>
23 
40  const char *filename,
41  const char *gridname,
42  int *num_attrs,
43  char **attrlist[] )
44 {
45  MTKt_status status; /* Return status */
46 
47  status = MtkGridAttrListNC(filename, gridname, num_attrs, attrlist); // try netCDF
48  if (status != MTK_NETCDF_OPEN_FAILED) return status;
49 
50  return MtkGridAttrListHDF(filename, gridname, num_attrs, attrlist); // try HDF
51 }
52 
54  const char *filename,
55  const char *gridname,
56  int *num_attrs,
57  char **attrlist[] )
58 {
59  MTKt_status status_code; /* Return status of this function */
60  MTKt_status status; /* Return status */
61 
62  if (filename == NULL) return MTK_NULLPTR;
63 
64  /* Open file */
65  int ncid = 0;
66  {
67  int nc_status = nc_open(filename, NC_NOWRITE, &ncid);
69  }
70 
71  /* Read grid attribute */
72  status = MtkGridAttrListNcid(ncid, gridname, num_attrs, attrlist);
73  MTK_ERR_COND_JUMP(status);
74 
75  /* Close file */
76  {
77  int nc_status = nc_close(ncid);
79  }
80  ncid = 0;
81 
82  return MTK_SUCCESS;
83 
84  ERROR_HANDLE:
85  if (ncid != 0) nc_close(ncid);
86  return status_code;
87 }
88 
90  const char *filename,
91  const char *gridname,
92  int *num_attrs,
93  char **attrlist[] )
94 {
95  MTKt_status status_code; /* Return status of this function */
96  MTKt_status status; /* Return status */
97  int32 hdfstatus; /* HDF-EOS return status */
98  int32 fid = FAIL; /* HDF-EOS File id */
99 
100  if (filename == NULL)
102 
103  /* Open HDF File */
104  fid = GDopen((char*)filename, DFACC_READ);
105  if (fid == FAIL)
107 
108  /* Get list of grid attributes. */
109  status = MtkGridAttrListFid(fid, gridname, num_attrs, attrlist);
110  MTK_ERR_COND_JUMP(status);
111 
112  /* Close HDF file */
113  hdfstatus = GDclose(fid);
114  if (hdfstatus == FAIL)
116  fid = FAIL;
117 
118  return MTK_SUCCESS;
119 
120 ERROR_HANDLE:
121  if (fid != FAIL)
122  GDclose(fid);
123 
124  return status_code;
125 }
126 
133  int32 fid,
134  const char *gridname,
135  int *num_attrs,
136  char **attrlist[] )
137 {
138  MTKt_status status_code; /* Return status of this function */
139  int32 hdfstatus; /* HDF-EOS return status */
140  int32 gid = FAIL; /* HDF-EOS Grid id */
141  int32 nattrs = 0; /* Number of attributes */
142  int32 count;
143  char **attrlist_tmp = NULL;
144  char *list = NULL;
145  char *temp = NULL;
146  int i;
147 
148  if (gridname == NULL ||
149  num_attrs == NULL || attrlist == NULL)
151 
152  /* Attach to Grid */
153  gid = GDattach(fid, (char*)gridname);
154  if (gid == FAIL)
156 
157  /* Get buffer size */
158  hdfstatus = GDinqattrs(gid,NULL,&count);
159  if (hdfstatus == FAIL)
161 
162  list = (char*)malloc((count + 1) * sizeof(char));
163  if (list == NULL)
165 
166  /* Get grid attributes */
167  hdfstatus = nattrs = GDinqattrs(gid,list,&count);
168  if (hdfstatus == FAIL)
170 
171  /* Detach from grid */
172  hdfstatus = GDdetach(gid);
173  if (hdfstatus == FAIL)
175 
176  attrlist_tmp = (char**)calloc(nattrs,sizeof(char*));
177  if (attrlist_tmp == NULL)
179 
180  temp = strtok(list,",");
181  i = 0;
182  while (temp != NULL)
183  {
184  attrlist_tmp[i] = (char*)malloc((strlen(temp) + 1) * sizeof(char));
185  if (attrlist_tmp[i] == NULL)
187  strcpy(attrlist_tmp[i],temp);
188  temp = strtok(NULL,",");
189  ++i;
190  }
191 
192  free(list);
193 
194  *attrlist = attrlist_tmp;
195  *num_attrs = nattrs;
196 
197  return MTK_SUCCESS;
198 
199 ERROR_HANDLE:
200  if (attrlist_tmp != NULL)
201  MtkStringListFree(nattrs, &attrlist_tmp);
202 
203  if (num_attrs != NULL)
204  *num_attrs = -1;
205 
206  free(list);
207  GDdetach(gid);
208 
209  return status_code;
210 }
211 
213  int ncid,
214  const char *gridname,
215  int *num_attrs,
216  char **attrlist[] )
217 {
218  MTKt_status status_code; /* Return status of this function */
219  int32 nattrs = 0; /* Number of attributes */
220  char **attrlist_tmp = NULL;
221 
222  if (gridname == NULL ||
223  num_attrs == NULL || attrlist == NULL)
225 
226  int group_id;
227  {
228  int nc_status = nc_inq_ncid(ncid, gridname, &group_id);
230 
231  nc_status = nc_inq_varnatts(group_id, NC_GLOBAL, &nattrs);
233  }
234 
235  attrlist_tmp = (char**)calloc(nattrs,sizeof(char*));
236  if (attrlist_tmp == NULL)
238 
239  for (int i = 0; i < nattrs; ++i) {
240  char attr_name[NC_MAX_NAME+1];
241  int nc_status = nc_inq_attname(group_id, NC_GLOBAL, i, attr_name);
243 
244  attrlist_tmp[i] = (char*)malloc((strlen(attr_name) + 1) * sizeof(char));
245  if (attrlist_tmp[i] == NULL)
247  strcpy(attrlist_tmp[i],attr_name);
248  }
249 
250  *attrlist = attrlist_tmp;
251  *num_attrs = nattrs;
252 
253  return MTK_SUCCESS;
254 
255 ERROR_HANDLE:
256  if (attrlist_tmp != NULL)
257  MtkStringListFree(nattrs, &attrlist_tmp);
258 
259  if (num_attrs != NULL)
260  *num_attrs = -1;
261 
262  return status_code;
263 }
HDFFCLIBAPI intf intf intf * count
MTKt_status MtkStringListFree(int strcnt, char **strlist[])
Free string list.
int32 GDattach(int32, char *)
#define DFACC_READ
Definition: hdf.h:44
char * filename
Definition: cdjpeg.h:133
MTKt_status MtkGridAttrList(const char *filename, const char *gridname, int *num_attrs, char **attrlist[])
Get a list of grid attributes.
intn GDclose(int32)
#define MTK_ERR_CODE_JUMP(code)
Definition: MisrError.h:175
#define NC_GLOBAL
Definition: netcdf.h:214
intn GDdetach(int32)
EXTERNL int nc_inq_varnatts(int ncid, int varid, int *nattsp)
#define NC_NOERR
Definition: netcdf.h:313
EXTERNL int nc_inq_ncid(int ncid, const char *name, int *grp_ncid)
#define NC_MAX_NAME
Definition: netcdf.h:275
#define NC_NOWRITE
Definition: netcdf.h:201
MTKt_status MtkGridAttrListNcid(int ncid, const char *gridname, int *num_attrs, char **attrlist[])
EXTERNL int nc_inq_attname(int ncid, int varid, int attnum, char *name)
MTKt_status MtkGridAttrListNC(const char *filename, const char *gridname, int *num_attrs, char **attrlist[])
MTKt_status MtkGridAttrListHDF(const char *filename, const char *gridname, int *num_attrs, char **attrlist[])
#define MTK_ERR_COND_JUMP(code)
Definition: MisrError.h:188
MTKt_status
Definition: MisrError.h:11
MTKt_status MtkGridAttrListFid(int32 fid, const char *gridname, int *num_attrs, char **attrlist[])
Version of MtkGridAttrList that takes an HDF-EOS file identifier rather than a filename.
int32 GDinqattrs(int32, char *, int32 *)
int32 GDopen(char *, intn)
EXTERNL int nc_close(int ncid)
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