MISR Toolkit  1.5.1
MtkFileGridToNativeFieldList.c
Go to the documentation of this file.
1 /*===========================================================================
2 = =
3 = MtkFileGridToNativeFieldList =
4 = =
5 =============================================================================
6 
7  Jet Propulsion Laboratory
8  MISR
9  MISR Toolkit
10 
11  Copyright 2005, 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 <HdfEosDef.h>
22 #include <string.h>
23 
41  const char *filename,
42  const char *gridname,
43  int *nfields,
44  char **fieldlist[] )
45 {
46  MTKt_status status; /* Return status */
47 
48  status = MtkFileGridToNativeFieldListNC(filename, gridname, nfields, fieldlist); // try netCDF
49  if (status != MTK_NETCDF_OPEN_FAILED) return status;
50 
51  return MtkFileGridToNativeFieldListHDF(filename, gridname, nfields, fieldlist); // try HDF
52 }
53 
55  const char *filename,
56  const char *gridname,
57  int *nfields,
58  char **fieldlist[] )
59 {
60  MTKt_status status_code; /* Return status of this function */
61  MTKt_status status; /* Return status */
62 
63  if (filename == NULL) return MTK_NULLPTR;
64 
65  /* Open file */
66  int ncid = 0;
67  {
68  int nc_status = nc_open(filename, NC_NOWRITE, &ncid);
70  }
71 
72  /* Read grid attribute */
73  status = MtkFileGridToNativeFieldListNcid(ncid, gridname, nfields, fieldlist);
74  MTK_ERR_COND_JUMP(status);
75 
76  /* Close file */
77  {
78  int nc_status = nc_close(ncid);
80  }
81  ncid = 0;
82 
83  return MTK_SUCCESS;
84 
85  ERROR_HANDLE:
86  if (ncid != 0) nc_close(ncid);
87  return status_code;
88 }
89 
91  const char *filename,
92  const char *gridname,
93  int *nfields,
94  char **fieldlist[] )
95 {
96  MTKt_status status; /* Return status of called functions */
97  MTKt_status status_code; /* Return status of this function. */
98  intn hdfstatus; /* HDF-EOS return status */
99  int32 Fid = FAIL; /* HDF-EOS File ID */
100 
101  if (filename == NULL)
103 
104  /* Open HDF file for reading */
105  hdfstatus = Fid = GDopen((char*)filename,DFACC_READ);
106  if (hdfstatus == FAIL)
108 
109  /* Read list of fields. */
110  status = MtkFileGridToNativeFieldListFid(Fid, gridname, nfields, fieldlist);
111  MTK_ERR_COND_JUMP(status);
112 
113  /* Close file. */
114  hdfstatus = GDclose(Fid);
115  if (hdfstatus == FAIL)
117  Fid = FAIL;
118 
119  return MTK_SUCCESS;
120 
121  ERROR_HANDLE:
122  if (Fid != FAIL) GDclose(Fid);
123 
124  return status_code;
125 }
126 
133  int32 Fid,
134  const char *gridname,
135  int *nfields,
136  char **fieldlist[] )
137 {
138  MTKt_status status_code; /* Return status of this function. */
139  intn hdfstatus; /* HDF-EOS return status */
140  int32 Gid = FAIL; /* HDF-EOS Grid ID */
141  int32 num_fields = 0; /* Number of fields */
142  char *list = NULL; /* List of fields */
143  int i;
144  char *temp = NULL;
145  int32 str_buffer_size = 0;
146 
147  /* Check Arguments */
148  if (fieldlist == NULL)
150 
151  *fieldlist = NULL; /* Set output to NULL to prevent freeing unallocated
152  memory in case of error. */
153 
154  if (gridname == NULL)
156 
157  if (nfields == NULL)
159 
160  /* Attach to grid */
161  hdfstatus = Gid = GDattach(Fid,(char*)gridname);
162  if (hdfstatus == FAIL)
164 
165  /* Query length of fields string */
166  hdfstatus = GDnentries(Gid, HDFE_NENTDFLD, &str_buffer_size);
167  if(hdfstatus == FAIL)
169 
170  list = (char*)malloc((str_buffer_size + 1) * sizeof(char));
171  if (list == NULL)
173 
174  /* Get list of fields */
175  hdfstatus = num_fields = GDinqfields(Gid,list,NULL,NULL);
176  if (hdfstatus == FAIL)
178 
179  *nfields = num_fields;
180  *fieldlist = (char**)calloc(num_fields,sizeof(char*));
181  if (*fieldlist == NULL)
183 
184  temp = strtok(list,",");
185  i = 0;
186  while (temp != NULL)
187  {
188  (*fieldlist)[i] = (char*)malloc((strlen(temp) + 1) * sizeof(char));
189  if ((*fieldlist)[i] == NULL)
191  strcpy((*fieldlist)[i],temp);
192  temp = strtok(NULL,",");
193  ++i;
194  }
195 
196  free(list);
197 
198  hdfstatus = GDdetach(Gid);
199  if (hdfstatus == FAIL)
201 
202  return MTK_SUCCESS;
203 
204  ERROR_HANDLE:
205  if (fieldlist != NULL)
206  MtkStringListFree(num_fields, fieldlist);
207 
208  if (nfields != NULL)
209  *nfields = -1;
210 
211  free(list);
212  GDdetach(Gid);
213 
214  return status_code;
215 }
216 
218  int ncid,
219  const char *gridname,
220  int *nfields,
221  char **fieldlist[] )
222 {
223  MTKt_status status_code; /* Return status of this function. */
224  int32 num_fields = 0; /* Number of fields */
225  int *varids = NULL;
226  int *group_ids = NULL;
227 
228  /* Check Arguments */
229  if (fieldlist == NULL)
231 
232  *fieldlist = NULL; /* Set output to NULL to prevent freeing unallocated
233  memory in case of error. */
234 
235  if (gridname == NULL)
237 
238  if (nfields == NULL)
240 
241  int group_id;
242  int nc_status = nc_inq_ncid(ncid, gridname, &group_id);
244 
245  int ngroups;
246  nc_status = nc_inq_grps(group_id, &ngroups, NULL);
248 
249  ngroups++; // Add 1 for base group
250  group_ids = (int *)calloc(ngroups, sizeof(int));
251  group_ids[0] = group_id; // include base group for the grid
252 
253  if (ngroups > 1) {
254  nc_status = nc_inq_grps(group_id, NULL, group_ids+1);
256  }
257 
258  for (int i = 0 ; i < ngroups ; i++) {
259  int group_id = group_ids[i];
260 
261  char group_name[MAX_NC_NAME] = {0}; // empty string for base group
262  if (i > 0) { // if this is not the base group
263  nc_status = nc_inq_grpname(group_id, group_name);
264  if (nc_status != NC_NOERR) {
266  }
267  }
268 
269  int nvars;
270  nc_status = nc_inq_varids(group_id, &nvars, NULL);
271  if (nc_status != NC_NOERR) {
273  }
274 
275  varids = (int *)calloc(nvars, sizeof(int));
276  nc_status = nc_inq_varids(group_id, NULL, varids);
277  if (nc_status != NC_NOERR) {
279  }
280 
281  int start = num_fields;
282  num_fields += nvars;
283  *nfields = num_fields;
284  *fieldlist = (char**)realloc(*fieldlist, num_fields * sizeof(char*));
285  if (*fieldlist == NULL) {
287  }
288 
289  for (int i = 0 ; i < nvars ; i++) {
290  int ifield = i + start;
291  int varid = varids[i];
292  char temp[MAX_NC_NAME];
293 
294  int nc_status = nc_inq_varname(group_id, varid, temp);
295  if (nc_status != NC_NOERR) {
297  }
298 
299  (*fieldlist)[ifield] = (char*)calloc(strlen(group_name) + strlen(temp) + 2, sizeof(char));
300  if ((*fieldlist)[ifield] == NULL) {
302  }
303  strcpy((*fieldlist)[ifield],group_name);
304  if (strlen(group_name) > 0) {
305  strcat((*fieldlist)[ifield], "/");
306  }
307  strcat((*fieldlist)[ifield],temp);
308  }
309 
310  free(varids);
311  varids = NULL;
312  }
313 
314  free(group_ids);
315  group_ids = NULL;
316  return MTK_SUCCESS;
317 
318  ERROR_HANDLE:
319  if (fieldlist != NULL)
320  MtkStringListFree(num_fields, fieldlist);
321 
322  if (nfields != NULL)
323  *nfields = -1;
324 
325  if (varids != NULL) {
326  free(varids);
327  }
328 
329  if (group_ids != NULL) {
330  free(group_ids);
331  }
332 
333  return status_code;
334 }
MTKt_status MtkStringListFree(int strcnt, char **strlist[])
Free string list.
#define HDFE_NENTDFLD
Definition: HdfEosDef.h:32
int32 GDattach(int32, char *)
#define DFACC_READ
Definition: hdf.h:44
EXTERNL int nc_inq_varids(int ncid, int *nvars, int *varids)
char * filename
Definition: cdjpeg.h:133
intn GDclose(int32)
int32 GDinqfields(int32, char *, int32 [], int32 [])
#define MTK_ERR_CODE_JUMP(code)
Definition: MisrError.h:175
intn GDdetach(int32)
#define MAX_NC_NAME
Definition: netcdf.h:231
MTKt_status MtkFileGridToNativeFieldListHDF(const char *filename, const char *gridname, int *nfields, char **fieldlist[])
#define NC_NOERR
Definition: netcdf.h:313
EXTERNL int nc_inq_ncid(int ncid, const char *name, int *grp_ncid)
EXTERNL int nc_inq_grps(int ncid, int *numgrps, int *ncids)
int32 GDnentries(int32, int32, int32 *)
EXTERNL int nc_inq_grpname(int ncid, char *name)
#define NC_NOWRITE
Definition: netcdf.h:201
HDFFCLIBAPI intf intf start[]
MTKt_status MtkFileGridToNativeFieldListNC(const char *filename, const char *gridname, int *nfields, char **fieldlist[])
MTKt_status MtkFileGridToNativeFieldList(const char *filename, const char *gridname, int *nfields, char **fieldlist[])
Read list of native fields from file (excludes derived fields)
#define MTK_ERR_COND_JUMP(code)
Definition: MisrError.h:188
MTKt_status
Definition: MisrError.h:11
MTKt_status MtkFileGridToNativeFieldListNcid(int ncid, const char *gridname, int *nfields, char **fieldlist[])
int32 GDopen(char *, intn)
EXTERNL int nc_close(int ncid)
EXTERNL int nc_inq_varname(int ncid, int varid, char *name)
EXTERNL int nc_open(const char *path, int mode, int *ncidp)
MTKt_status MtkFileGridToNativeFieldListFid(int32 Fid, const char *gridname, int *nfields, char **fieldlist[])
Version of MtkFileGridToNativeFieldList that takes an HDF-EOS file identifier rather than a filename...
#define FAIL
Definition: hdf.h:94

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