MISR Toolkit  1.5.1
MtkFileGridFieldToDimList.c
Go to the documentation of this file.
1 /*===========================================================================
2 = =
3 = MtkFileGridFieldToDimList =
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 <stdlib.h>
24 
43  const char *filename,
44  const char *gridname,
45  const char *fieldname,
46  int *dimcnt,
47  char **dimlist[],
48  int **dimsize )
49 {
50  MTKt_status status; /* Return status */
51 
52  status = MtkFileGridFieldToDimListNC(filename, gridname, fieldname, dimcnt, dimlist, dimsize); // try netCDF
53  if (status != MTK_NETCDF_OPEN_FAILED) return status;
54 
55  return MtkFileGridFieldToDimListHDF(filename, gridname, fieldname, dimcnt, dimlist, dimsize); // try HDF
56 }
57 
59  const char *filename,
60  const char *gridname,
61  const char *fieldname,
62  int *dimcnt,
63  char **dimlist[],
64  int **dimsize )
65 {
66  MTKt_status status;
67  MTKt_status status_code;
68  int ncid = 0;
69 
70  if (filename == NULL) MTK_ERR_CODE_JUMP(MTK_NULLPTR);
71 
72  /* Open file */
73  {
74  int nc_status = nc_open(filename, NC_NOWRITE, &ncid);
76  }
77 
78  /* Read dimension list. */
79  status = MtkFileGridFieldToDimListNcid(ncid, gridname, fieldname, dimcnt, dimlist, dimsize);
80  MTK_ERR_COND_JUMP(status);
81 
82  /* Close file */
83  {
84  int nc_status = nc_close(ncid);
86  }
87  ncid = 0;
88 
89  return MTK_SUCCESS;
90 
91  ERROR_HANDLE:
92  if (ncid != 0) nc_close(ncid);
93  return status_code;
94 }
95 
97  const char *filename,
98  const char *gridname,
99  const char *fieldname,
100  int *dimcnt,
101  char **dimlist[],
102  int **dimsize )
103 {
104  MTKt_status status; /* Return status. */
105  MTKt_status status_code; /* Return status of this function. */
106  intn hdfstatus; /* HDF-EOS return status */
107  int32 Fid = FAIL; /* HDF-EOS File ID */
108 
109  if (filename == NULL)
111 
112  /* Open HDF file for reading */
113  hdfstatus = Fid = GDopen((char*)filename,DFACC_READ);
114  if (hdfstatus == FAIL)
116 
117  /* Read dimension list. */
118  status = MtkFileGridFieldToDimListFid(Fid, gridname, fieldname,
119  dimcnt, dimlist, dimsize);
120  MTK_ERR_COND_JUMP(status);
121 
122  /* Close HDF file. */
123  GDclose(Fid);
124 
125  return MTK_SUCCESS;
126 
127 ERROR_HANDLE:
128  GDclose(Fid);
129 
130  return status_code;
131 }
132 
133 
140  int32 Fid,
141  const char *gridname,
142  const char *fieldname,
143  int *dimcnt,
144  char **dimlist[],
145  int **dimsize )
146 {
147  MTKt_status status; /* Return status. */
148  MTKt_status status_code; /* Return status of this function. */
149  intn hdfstatus; /* HDF-EOS return status */
150  int32 Gid = FAIL; /* HDF-EOS Grid ID */
151  int32 rank = 0;
152  int32 dims[10]; /* Dimension sizes */
153  int32 numbertype;
154  char *list = NULL; /* List of dimensions */
155  int i;
156  char *temp = NULL;
157  char *basefield = NULL; /* Base fieldname */
158  int nextradims; /* Number of extra dimensions */
159  int *extradims = NULL; /* Extra dimension list */
160 
161  /* Check Arguments */
162  if (dimlist == NULL)
164 
165  if (dimsize == NULL)
167 
168  *dimlist = NULL; /* Set output to NULL to prevent freeing unallocated
169  memory in case of error. */
170  *dimsize = NULL;
171 
172  if (gridname == NULL || fieldname == NULL ||
173  dimcnt == NULL)
175 
176  /* Attach to grid */
177  hdfstatus = Gid = GDattach(Fid,(char*)gridname);
178  if (hdfstatus == FAIL)
180 
181  list = (char*)malloc(5000 * sizeof(char));
182  if (list == NULL)
184 
185  status = MtkParseFieldname(fieldname, &basefield, &nextradims, &extradims);
186  MTK_ERR_COND_JUMP(status);
187 
188  /* Get field info */
189  hdfstatus = GDfieldinfo(Gid, basefield, &rank, dims, &numbertype, list);
190  if (hdfstatus == FAIL)
192 
193  *dimlist = (char**)calloc(rank,sizeof(char*));
194  if (*dimlist == NULL)
196  *dimsize = (int*)calloc(rank,sizeof(int));
197  if (*dimsize == NULL)
199 
200  temp = strtok(list,",");
201  i = 0;
202  while (temp != NULL)
203  {
204  if ((strncmp(temp, "SOMBlockDim", sizeof(*temp))) != 0 &&
205  (strncmp(temp, "XDim", sizeof(*temp))) != 0 &&
206  (strncmp(temp, "YDim", sizeof(*temp))) != 0) {
207  (*dimlist)[i] = (char*)malloc((strlen(temp) + 1) * sizeof(char));
208  if ((*dimlist)[i] == NULL)
210  strcpy((*dimlist)[i],temp);
211  ++i;
212  }
213  temp = strtok(NULL,",");
214  }
215 
216  /* Dimension count with out implied dimensions */
217  *dimcnt = i;
218 
219  if (*dimcnt > 0) {
220 
221  /* Get dimension size info */
222  for (i = 0; i < *dimcnt; ++i) {
223  (*dimsize)[i] = GDdiminfo(Gid, (*dimlist)[i]);
224  }
225 
226  /* Resize lists to account for removing dimensions */
227  *dimlist = (char **)realloc((void *)*dimlist, *dimcnt * sizeof(char*));
228  if (*dimlist == NULL)
230  *dimsize = (int *)realloc((void *)*dimsize, *dimcnt * sizeof(int));
231  if (*dimsize == NULL)
233  } else {
234  MtkStringListFree(*dimcnt, dimlist);
235  free(*dimsize);
236  *dimsize = NULL;
237  }
238 
239  free(basefield);
240  free(extradims);
241  free(list);
242  GDdetach(Gid);
243 
244  return MTK_SUCCESS;
245 
246 ERROR_HANDLE:
247  if (dimlist != NULL && *dimlist != NULL)
248  MtkStringListFree(*dimcnt, dimlist);
249 
250  if (dimsize != NULL)
251  {
252  free(*dimsize);
253  *dimsize = NULL;
254  }
255 
256  if (dimcnt != NULL)
257  *dimcnt = -1;
258 
259  if (basefield != NULL) free(basefield);
260  if (extradims != NULL) free(extradims);
261 
262  free(list);
263  GDdetach(Gid);
264 
265  return status_code;
266 }
267 
269  int ncid,
270  const char *gridname,
271  const char *fieldname,
272  int *dimcnt,
273  char **dimlist[],
274  int **dimsize )
275 {
276  MTKt_status status; /* Return status. */
277  MTKt_status status_code; /* Return status of this function. */
278  char *basefield = NULL; /* Base fieldname */
279  int nextradims; /* Number of extra dimensions */
280  int *extradims = NULL; /* Extra dimension list */
281 
282  int *dimids = NULL;
283 
284  /* Check Arguments */
285  if (dimlist == NULL)
287 
288  if (dimsize == NULL)
290 
291  *dimlist = NULL; /* Set output to NULL to prevent freeing unallocated
292  memory in case of error. */
293  *dimsize = NULL;
294 
295  if (gridname == NULL || fieldname == NULL ||
296  dimcnt == NULL)
298 
299  status = MtkParseFieldname(fieldname, &basefield, &nextradims, &extradims);
300  MTK_ERR_COND_JUMP(status);
301 
302  int group_id;
303  {
304  int nc_status = nc_inq_grp_ncid(ncid, gridname, &group_id);
306  }
307 
308  MTKt_ncvarid var;
309  status = MtkNCVarId(group_id, basefield, &var);
310  MTK_ERR_COND_JUMP(status);
311 
312  int ndims;
313  {
314  int nc_status = nc_inq_varndims(var.gid, var.varid, &ndims);
316  }
317 
318  dimids = calloc(ndims, sizeof(int));
319  {
320  int nc_status = nc_inq_var(var.gid, var.varid, NULL, NULL, NULL, dimids, NULL);
322  }
323 
324  *dimlist = calloc(ndims, sizeof(char *));
325  *dimsize = calloc(ndims, sizeof(int));
326  *dimcnt = 0;
327 
328  for (size_t i = 0 ; i < ndims ; i++) {
329  char name[NC_MAX_NAME+1]; // add 1 for string terminator
330  int this_dimid = dimids[i];
331  size_t size;
332  int nc_status = nc_inq_dim(group_id, this_dimid, name, &size);
334 
335  if (0 == strcmp(name, "X_Dim")) continue; // skip X_Dim
336  if (0 == strcmp(name, "Y_Dim")) continue; // skip Y_Dim
337 
338  char *this_name = calloc(sizeof(name), sizeof(char));
339  strcpy(this_name, name);
340  (*dimlist)[(*dimcnt)] = this_name;
341  (*dimsize)[(*dimcnt)] = size;
342  (*dimcnt)++;
343  }
344 
345  if (*dimcnt == 0) {
346  MtkStringListFree(*dimcnt, dimlist);
347  free(*dimsize);
348  *dimsize = NULL;
349  }
350 
351  free(dimids);
352  free(basefield);
353  free(extradims);
354 
355  return MTK_SUCCESS;
356 
357 ERROR_HANDLE:
358  if (dimids != NULL) free(dimids);
359  if (dimlist != NULL && *dimlist != NULL)
360  MtkStringListFree(ndims, dimlist);
361 
362  if (dimsize != NULL)
363  {
364  free(*dimsize);
365  *dimsize = NULL;
366  }
367 
368  if (dimcnt != NULL)
369  *dimcnt = -1;
370 
371  if (basefield != NULL) free(basefield);
372  if (extradims != NULL) free(extradims);
373 
374  return status_code;
375 }
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
intn GDclose(int32)
MTKt_status MtkFileGridFieldToDimListHDF(const char *filename, const char *gridname, const char *fieldname, int *dimcnt, char **dimlist[], int **dimsize)
MTKt_status MtkFileGridFieldToDimListNcid(int ncid, const char *gridname, const char *fieldname, int *dimcnt, char **dimlist[], int **dimsize)
HDFFCLIBAPI void intf dims[]
MTKt_status MtkParseFieldname(const char *fieldname, char **basefieldname, int *ndim, int **dimlist)
Parses extra dimensions from fieldnames.
EXTERNL int nc_inq_var(int ncid, int varid, char *name, nc_type *xtypep, int *ndimsp, int *dimidsp, int *nattsp)
HDFFCLIBAPI intf * dimsize
#define MTK_ERR_CODE_JUMP(code)
Definition: MisrError.h:175
intn GDdetach(int32)
#define NC_NOERR
Definition: netcdf.h:313
MTKt_status MtkFileGridFieldToDimListNC(const char *filename, const char *gridname, const char *fieldname, int *dimcnt, char **dimlist[], int **dimsize)
HDFFCLIBAPI _fcd name
intn GDfieldinfo(int32, char *, int32 *, int32 [], int32 *, char *)
#define NC_MAX_NAME
Definition: netcdf.h:275
MTKt_status MtkFileGridFieldToDimList(const char *filename, const char *gridname, const char *fieldname, int *dimcnt, char **dimlist[], int **dimsize)
Read dimension list of a particular field.
HDFFCLIBAPI intf * size
#define NC_NOWRITE
Definition: netcdf.h:201
MTKt_status MtkFileGridFieldToDimListFid(int32 Fid, const char *gridname, const char *fieldname, int *dimcnt, char **dimlist[], int **dimsize)
Version of MtkFileGridFieldToDimList that takes an HDF-EOS file ID rather than a filename.
#define MTK_ERR_COND_JUMP(code)
Definition: MisrError.h:188
int32 GDdiminfo(int32, char *)
EXTERNL int nc_inq_varndims(int ncid, int varid, int *ndimsp)
MTKt_status
Definition: MisrError.h:11
HDFFCLIBAPI intf * rank
int32 GDopen(char *, intn)
EXTERNL int nc_close(int ncid)
MTKt_status MtkNCVarId(int Ncid, const char *Name, MTKt_ncvarid *Var)
Definition: MtkNCVarId.c:21
EXTERNL int nc_inq_dim(int ncid, int dimid, char *name, size_t *lenp)
EXTERNL int nc_inq_grp_ncid(int ncid, const char *grp_name, int *grp_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