MISR Toolkit  1.5.1
MtkFileGridFieldCheck.c
Go to the documentation of this file.
1 /*===========================================================================
2 = =
3 = MtkFileGridFieldCheck =
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 <stdlib.h>
21 #include <mfhdf.h>
22 #include <HdfEosDef.h>
23 #include <stdio.h>
24 
43  const char *filename,
44  const char *gridname,
45  const char *fieldname )
46 {
47  MTKt_status status; /* Return status */
48 
49  status = MtkFileGridFieldCheckNC(filename, gridname, fieldname); // try netCDF
50  if (status != MTK_NETCDF_OPEN_FAILED) return status;
51 
52  return MtkFileGridFieldCheckHDF(filename, gridname, fieldname); // try HDF
53 }
54 
56  const char *filename,
57  const char *gridname,
58  const char *fieldname )
59 {
60  MTKt_status status;
61  MTKt_status status_code;
62  int ncid = 0;
63 
64  if (filename == NULL) MTK_ERR_CODE_JUMP(MTK_NULLPTR);
65 
66  /* Open file */
67  {
68  int nc_status = nc_open(filename, NC_NOWRITE, &ncid);
70  }
71 
72  /* Check if file/grid/field/dimension are valid */
73  status = MtkFileGridFieldCheckNcid(ncid, gridname, fieldname);
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  const char *fieldname )
94 {
95  MTKt_status status_code; /* Return status of this function */
96  MTKt_status status; /* Return status */
97  intn hdfstatus; /* HDF-EOS return status */
98  int32 Fid = FAIL; /* HDF-EOS File ID */
99 
100  if (filename == NULL)
102 
103  /* Open HDF file for reading */
104  hdfstatus = Fid = GDopen((char*)filename,DFACC_READ);
105  if (hdfstatus == FAIL)
107 
108  /* Check if file/grid/field/dimension are valid */
109  status = MtkFileGridFieldCheckFid(Fid, gridname, fieldname);
110  MTK_ERR_COND_JUMP(status);
111 
112  /* Close 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) GDclose(Fid);
122 
123  return status_code;
124 }
125 
132  int32 Fid,
133  const char *gridname,
134  const char *fieldname )
135 {
136  MTKt_status status_code; /* Return status of this function */
137  MTKt_status status; /* Return status */
138  int nfields; /* Number fields */
139  char **fieldlist = NULL; /* Field list */
140  int i; /* Field index */
141  char *basefield = NULL; /* Base fieldname */
142  int nextradims; /* Number of extra dimensions */
143  int *extradims = NULL; /* Extra dimension list */
144  char **dimlist = NULL; /* Dimension name list */
145  int *dimsize = NULL; /* Dimension size list */
146  int dimcnt; /* Number of dimensions */
147 
148  if (gridname == NULL || fieldname == NULL)
150 
151  /* Parse fieldname into basefield number of dimensions and size of each dimension */
152  status = MtkParseFieldname(fieldname, &basefield, &nextradims, &extradims);
154 
155  /* Check if gridname and fieldname are valid (includes derived fields) */
156  status = MtkFileGridToFieldListFid(Fid, gridname, &nfields, &fieldlist);
157 
158  switch (status) {
161  break;
162 
165  break;
166 
167  case MTK_SUCCESS:
168  for (i = 0; i < nfields; i++) {
169  if (strncmp(basefield, fieldlist[i], strlen(fieldlist[i])) == 0)
170  break;
171  }
172  if (i >= nfields) {
174  }
175  break;
176 
177  default:
178  MTK_ERR_CODE_JUMP(status);
179  }
180 
181  /* Check if number of dimensions and bounds of each dimension are valid */
182  status = MtkFileGridFieldToDimListFid(Fid, gridname, fieldname,
183  &dimcnt, &dimlist, &dimsize);
184  MTK_ERR_COND_JUMP(status);
185 
186  if (nextradims < dimcnt) {
187 
189 
190  } else if (nextradims > dimcnt) {
191 
193 
194  }
195  for (i = 0; i < nextradims; i++) {
196  if ( extradims[i] < 0 || extradims[i] >= dimsize[i] ) {
198  }
199  }
200 
201  free(basefield);
202  free(extradims);
203  free(dimsize);
204  MtkStringListFree(nfields, &fieldlist);
205  MtkStringListFree(dimcnt, &dimlist);
206  return MTK_SUCCESS;
207 
208 ERROR_HANDLE:
209  if (fieldlist != NULL)MtkStringListFree(nfields, &fieldlist);
210  if (basefield != NULL) free(basefield);
211  if (extradims != NULL) free(extradims);
212  if (dimlist != NULL) MtkStringListFree(dimcnt, &dimlist);
213  if (dimsize != NULL) free(dimsize);
214  return status_code;
215 }
216 
218  int ncid,
219  const char *gridname,
220  const char *fieldname )
221 {
222  MTKt_status status_code; /* Return status of this function */
223  MTKt_status status; /* Return status */
224  int nfields; /* Number fields */
225  char **fieldlist = NULL; /* Field list */
226  int i; /* Field index */
227  char *basefield = NULL; /* Base fieldname */
228  int nextradims; /* Number of extra dimensions */
229  int *extradims = NULL; /* Extra dimension list */
230  char **dimlist = NULL; /* Dimension name list */
231  int *dimsize = NULL; /* Dimension size list */
232  int dimcnt; /* Number of dimensions */
233 
234  if (gridname == NULL || fieldname == NULL)
236 
237  /* Parse fieldname into basefield number of dimensions and size of each dimension */
238  status = MtkParseFieldname(fieldname, &basefield, &nextradims, &extradims);
240 
241  /* Check if gridname and fieldname are valid (includes derived fields) */
242  status = MtkFileGridToFieldListNcid(ncid, gridname, &nfields, &fieldlist);
244 
245  for (i = 0; i < nfields; i++) {
246  if (strncmp(basefield, fieldlist[i], strlen(fieldlist[i])) == 0)
247  break;
248  }
249  if (i >= nfields) {
251  }
252 
253  /* Check if number of dimensions and bounds of each dimension are valid */
254  status = MtkFileGridFieldToDimListNcid(ncid, gridname, fieldname,
255  &dimcnt, &dimlist, &dimsize);
256  MTK_ERR_COND_JUMP(status);
257 
258  if (nextradims < dimcnt) {
259 
261 
262  } else if (nextradims > dimcnt) {
263 
265 
266  }
267  for (i = 0; i < nextradims; i++) {
268  if ( extradims[i] < 0 || extradims[i] >= dimsize[i] ) {
270  }
271  }
272 
273  free(basefield);
274  free(extradims);
275  free(dimsize);
276  MtkStringListFree(nfields, &fieldlist);
277  MtkStringListFree(dimcnt, &dimlist);
278  return MTK_SUCCESS;
279 
280 ERROR_HANDLE:
281  if (fieldlist != NULL)MtkStringListFree(nfields, &fieldlist);
282  if (basefield != NULL) free(basefield);
283  if (extradims != NULL) free(extradims);
284  if (dimlist != NULL) MtkStringListFree(dimcnt, &dimlist);
285  if (dimsize != NULL) free(dimsize);
286  return status_code;
287 }
MTKt_status MtkStringListFree(int strcnt, char **strlist[])
Free string list.
MTKt_status MtkFileGridFieldCheckNC(const char *filename, const char *gridname, const char *fieldname)
MTKt_status MtkFileGridFieldCheckFid(int32 Fid, const char *gridname, const char *fieldname)
Version of MtkFileGridFieldCheck that takes an HDF-EOS file identifier rather than a filename...
MTKt_status MtkFileGridFieldCheckHDF(const char *filename, const char *gridname, const char *fieldname)
#define DFACC_READ
Definition: hdf.h:44
char * filename
Definition: cdjpeg.h:133
intn GDclose(int32)
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.
MTKt_status MtkParseFieldname(const char *fieldname, char **basefieldname, int *ndim, int **dimlist)
Parses extra dimensions from fieldnames.
HDFFCLIBAPI intf * dimsize
#define MTK_ERR_CODE_JUMP(code)
Definition: MisrError.h:175
MTKt_status MtkFileGridFieldCheckNcid(int ncid, const char *gridname, const char *fieldname)
#define NC_NOERR
Definition: netcdf.h:313
MTKt_status MtkFileGridFieldCheck(const char *filename, const char *gridname, const char *fieldname)
Check if file/grid/field/dimension are valid.
MTKt_status MtkFileGridToFieldListNcid(int ncid, const char *gridname, int *nfields, char **fieldlist[])
#define NC_NOWRITE
Definition: netcdf.h:201
MTKt_status MtkFileGridToFieldListFid(int32 Fid, const char *gridname, int *nfields, char **fieldlist[])
Version of MtkFileGridToFieldList that takes an HDF-EOS file identifier rather than a filename...
#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)
EXTERNL int nc_open(const char *path, int mode, int *ncidp)
MTKt_status MtkFileGridFieldToDimListNcid(int ncid, const char *gridname, const char *fieldname, int *dimcnt, char **dimlist[], int **dimsize)
#define FAIL
Definition: hdf.h:94

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