MISR Toolkit  1.5.1
MtkFileToGridList.c
Go to the documentation of this file.
1 /*===========================================================================
2 = =
3 = MtkFileToGridList =
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 #include <stdio.h>
24 
42  const char *filename,
43  int *ngrids,
44  char **gridlist[] )
45 {
46  MTKt_status status; /* Return status */
47 
48  status = MtkFileToGridListNC(filename, ngrids, gridlist); // try netCDF
49  if (status != MTK_NETCDF_OPEN_FAILED) return status;
50 
51  return MtkFileToGridListHDF(filename, ngrids, gridlist); // try HDF
52 }
53 
55  const char *filename,
56  int *ngrids,
57  char **gridlist[] )
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 = MtkFileToGridListNcid(ncid, ngrids, gridlist);
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  int *ngrids,
92  char **gridlist[] )
93 {
94  MTKt_status status_code; /* Return status of this function. */
95  MTKt_status status; /* Return status */
96  intn hdfstatus; /* HDF return status */
97  int32 fid = FAIL; /* HDF-EOS file identifier */
98 
99  /* Check Arguments */
100  if (filename == NULL)
102 
103  /* Open file. */
104  fid = GDopen((char *)filename, DFACC_READ);
106 
107  /* Read the list of grids. */
108  status = MtkFileToGridListFid(fid, ngrids, gridlist);
109  MTK_ERR_COND_JUMP(status);
110 
111  /* Close file. */
112  hdfstatus = GDclose(fid);
114  fid = FAIL;
115 
116  return MTK_SUCCESS;
117 
118 ERROR_HANDLE:
119  if (fid != FAIL) GDclose(fid);
120  return status_code;
121 }
122 
129  int32 fid,
130  int *ngrids,
131  char **gridlist[] )
132 {
133  MTKt_status status_code; /* Return status of this function. */
134  intn status; /* HDF return status */
135  int32 strbufsize; /* String length of grid list */
136  int32 num_grids = 0; /* Number of grids */
137  char *list = NULL; /* List of grids */
138  char *temp = NULL;
139  int i;
140 
141  /* Check Arguments */
142  if (gridlist == NULL)
144 
145  *gridlist = NULL; /* Set output to NULL to prevent freeing unallocated
146  memory in case of error. */
147 
148  if (ngrids == NULL)
150 
151  /* Get the size of the buffer */
152  status = GDinqgridfid(fid, NULL, &strbufsize);
153  if (status == FAIL)
155 
156  /* GDinqgrid does not account for null termination, */
157  /* so we add 1 to strbufsize */
158  list = (char*)malloc((strbufsize + 1) * sizeof(char));
159  if (list == NULL)
161 
162  /* Get list of grids */
163  status = num_grids = GDinqgridfid(fid, list, &strbufsize);
164  if (status == FAIL)
166 
167  *ngrids = num_grids;
168  *gridlist = (char**)calloc(num_grids, sizeof(char*));
169  if (*gridlist == NULL)
171 
172  temp = strtok(list,",");
173  i = 0;
174  while (temp != NULL)
175  {
176  (*gridlist)[i] = (char*)malloc((strlen(temp) + 1) * sizeof(char));
177  if ((*gridlist)[i] == NULL)
179  strcpy((*gridlist)[i],temp);
180  temp = strtok(NULL,",");
181  ++i;
182  }
183 
184  free(list);
185 
186  return MTK_SUCCESS;
187 
188 ERROR_HANDLE:
189  if (gridlist != NULL)
190  MtkStringListFree(num_grids, gridlist);
191 
192  if (ngrids != NULL)
193  *ngrids = -1;
194 
195  free(list);
196 
197  return status_code;
198 }
199 
201  int ncid,
202  int *ngrids,
203  char **gridlist[] )
204 {
205  MTKt_status status_code; /* Return status of this function. */
206  int32 num_grids = 0; /* Number of grids */
207  int *group_ids = NULL;
208 
209  /* Check Arguments */
210  if (gridlist == NULL)
212 
213  *gridlist = NULL; /* Set output to NULL to prevent freeing unallocated
214  memory in case of error. */
215 
216  if (ngrids == NULL)
218 
219  {
220  int num_groups;
221  int nc_status = nc_inq_grps(ncid, &num_groups, NULL);
223 
224  group_ids = (int *)calloc(num_groups, sizeof(int));
225  nc_status = nc_inq_grps(ncid, NULL, group_ids);
227 
228  *gridlist = (char**)calloc(num_groups, sizeof(char*));
229  if (*gridlist == NULL)
231 
232  for (int i = 0 ; i < num_groups ; i++) {
233  int group_id = group_ids[i];
234  char temp[MAX_NC_NAME+1];
235 
236  int nc_status = nc_inq_grpname(group_id, temp);
238 
239  nc_status = nc_inq_att(group_id, NC_GLOBAL, "resolution_in_meters", NULL, NULL); // must contain recongized grid attibute
240  if (nc_status != NC_NOERR) continue;
241 
242  (*gridlist)[num_grids] = (char*)malloc((strlen(temp) + 1) * sizeof(char));
243  if ((*gridlist)[num_grids] == NULL)
245  strcpy((*gridlist)[num_grids],temp);
246  num_grids++;
247  }
248 
249  *ngrids = num_grids;
250  free(group_ids);
251  group_ids = NULL;
252  }
253 
254  return MTK_SUCCESS;
255 
256 ERROR_HANDLE:
257  if (gridlist != NULL)
258  MtkStringListFree(num_grids, gridlist);
259 
260  if (ngrids != NULL)
261  *ngrids = -1;
262 
263  if (group_ids != NULL) {
264  free(group_ids);
265  }
266 
267  return status_code;
268 }
269 
278 int32
279 GDinqgridfid(int32 fid, char *objectlist, int32 * strbufsize)
280 {
281  int32 vgRef; /* Vgroup reference number */
282  int32 vGrpID; /* Vgroup ID */
283  int32 nobj = 0; /* Number of HDFEOS objects in file */
284  int32 slen; /* String length */
285 
286  char name[FIELDNAMELENMAX]; /* Object name */
287  char class[FIELDNAMELENMAX]; /* Object class */
288  int32 HDFfid; /* HDF file identifier */
289  int32 sid; /* HDF SD identifier (not used) */
290  intn hdf_status_code;
291 
292 
293  /* Get HDF file identifier. */
294  hdf_status_code = EHidinfo(fid, &HDFfid, &sid);
295  if (hdf_status_code == FAIL)
296  return FAIL;
297 
298  /* Start Vgroup Interface */
299  /* ---------------------- */
300  Vstart(HDFfid);
301 
302 
303  /* If string buffer size is requested then zero out counter */
304  /* -------------------------------------------------------- */
305  if (strbufsize != NULL)
306  {
307  *strbufsize = 0;
308  }
309  /* Search for objects from begining of HDF file */
310  /* -------------------------------------------- */
311  vgRef = -1;
312 
313  /* Loop through all objects */
314  /* ------------------------ */
315  while (1)
316  {
317  /* Get Vgroup reference number */
318  /* --------------------------- */
319  vgRef = Vgetid(HDFfid, vgRef);
320 
321  /* If no more then exist search loop */
322  /* --------------------------------- */
323  if (vgRef == -1)
324  {
325  break;
326  }
327  /* Get Vgroup ID, name, and class */
328  /* ------------------------------ */
329  vGrpID = Vattach(HDFfid, vgRef, "r");
330  Vgetname(vGrpID, name);
331  Vgetclass(vGrpID, class);
332 
333 
334  /* If object of desired type (GRID) ... */
335  /* -------------------------------------------------- */
336  if (strcmp(class, "GRID") == 0)
337  {
338 
339  /* Increment counter */
340  /* ----------------- */
341  nobj++;
342 
343 
344  /* If object list requested add name to list */
345  /* ----------------------------------------- */
346  if (objectlist != NULL)
347  {
348  if (nobj == 1)
349  {
350  strcpy(objectlist, name);
351  } else
352  {
353  strcat(objectlist, ",");
354  strcat(objectlist, name);
355  }
356  }
357  /* Compute string length of object entry */
358  /* ------------------------------------- */
359  slen = (nobj == 1) ? strlen(name) : strlen(name) + 1;
360 
361 
362  /* If string buffer size is requested then increment buffer size */
363  /* ------------------------------------------------------------- */
364  if (strbufsize != NULL)
365  {
366  *strbufsize += slen;
367  }
368  }
369  /* Detach Vgroup */
370  /* ------------- */
371  Vdetach(vGrpID);
372  }
373 
374  /* "Close" Vgroup interface and HDFEOS file */
375  /* ---------------------------------------- */
376  Vend(HDFfid);
377 
378  return (nobj);
379 }
380 
MTKt_status MtkStringListFree(int strcnt, char **strlist[])
Free string list.
MTKt_status MtkFileToGridList(const char *filename, int *ngrids, char **gridlist[])
Read list of grids from a file.
#define DFACC_READ
Definition: hdf.h:44
char * filename
Definition: cdjpeg.h:133
intn GDclose(int32)
#define MTK_ERR_CODE_JUMP(code)
Definition: MisrError.h:175
HDFLIBAPI int32 Vgetid(HFILEID f, int32 vgid)
#define FIELDNAMELENMAX
Definition: hlimits.h:67
#define NC_GLOBAL
Definition: netcdf.h:214
#define MAX_NC_NAME
Definition: netcdf.h:231
MTKt_status MtkFileToGridListHDF(const char *filename, int *ngrids, char **gridlist[])
HDFLIBAPI int32 Vdetach(int32 vkey)
#define NC_NOERR
Definition: netcdf.h:313
EXTERNL int nc_inq_att(int ncid, int varid, const char *name, nc_type *xtypep, size_t *lenp)
HDFLIBAPI int32 Vgetclass(int32 vkey, char *vgclass)
HDFLIBAPI int32 Vattach(HFILEID f, int32 vgid, const char *accesstype)
HDFFCLIBAPI _fcd name
#define Vstart(f)
Definition: hdf.h:162
MTKt_status MtkFileToGridListFid(int32 fid, int *ngrids, char **gridlist[])
Version of MtkFileToGridList that takes an HDF-EOS file identifier rather than a filename.
EXTERNL int nc_inq_grps(int ncid, int *numgrps, int *ncids)
EXTERNL int nc_inq_grpname(int ncid, char *name)
#define NC_NOWRITE
Definition: netcdf.h:201
intn EHidinfo(int32, int32 *, int32 *)
HDFLIBAPI int32 Vgetname(int32 vkey, char *vgname)
MTKt_status MtkFileToGridListNC(const char *filename, int *ngrids, char **gridlist[])
MTKt_status MtkFileToGridListNcid(int ncid, int *ngrids, char **gridlist[])
#define MTK_ERR_COND_JUMP(code)
Definition: MisrError.h:188
MTKt_status
Definition: MisrError.h:11
int32 GDopen(char *, intn)
EXTERNL int nc_close(int ncid)
#define Vend(f)
Definition: hdf.h:163
EXTERNL int nc_open(const char *path, int mode, int *ncidp)
int32 GDinqgridfid(int32 fid, char *objectlist, int32 *strbufsize)
Replacement for HDF-EOS GDinqgrid that takes an HDF-EOS file id 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