MISR Toolkit  1.5.1
MtkFieldAttrList.c
Go to the documentation of this file.
1 /*===========================================================================
2 = =
3 = MtkFieldAttrList =
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 *fieldname,
42  int *num_attrs,
43  char **attrlist[] )
44 {
45  MTKt_status status; /* Return status */
46 
47  status = MtkFieldAttrListNC(filename, fieldname, num_attrs, attrlist); // try netCDF
48  if (status != MTK_NETCDF_OPEN_FAILED) return status;
49 
50  return MtkFieldAttrListHDF(filename, fieldname, num_attrs, attrlist); // try HDF
51 }
52 
54  const char *filename,
55  const char *fieldname,
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 = MtkFieldAttrListNcid(ncid, fieldname, 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 
89 
91  const char *filename,
92  const char *fieldname,
93  int *num_attrs,
94  char **attrlist[] )
95 {
96  MTKt_status status_code; /* Return status of this function */
97  MTKt_status status; /* Return status */
98  int32 hdfstatus; /* HDF-EOS return status */
99  int32 fid = FAIL; /* HDF-EOS File id */
100 
101  if (filename == NULL)
103 
104  /* Open HDF File */
105  fid = GDopen((char*)filename, DFACC_READ);
106  if (fid == FAIL)
108 
109  /* Get list of field attributes. */
110  status = MtkFieldAttrListFid(fid, fieldname, num_attrs, attrlist);
111  MTK_ERR_COND_JUMP(status);
112 
113  /* Close HDF 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)
123  GDclose(fid);
124 
125  return status_code;
126 }
127 
134  int32 fid,
135  const char *fieldname,
136  int *num_attrs,
137  char **attrlist[] )
138 {
139  MTKt_status status_code; /* Return status of this function */
140  int32 n_attrs = 0; /* Number of attributes */
141  int32 count; /* Number of values in attribute */
142  int32 HDFfid = FAIL; /* HDF File ID */
143  int32 sdInterfaceID = FAIL; /* SD Interface ID (file level) */
144  int32 sd_id = FAIL; /* SDS ID (field level) */
145  int32 sds_index = 0; /* SDS index/offset for field */
146  int32 rank; /* Number of dimensions for field */
147  int32 data_type; /* Field data type */
148  char name[H4_MAX_NC_NAME]; /* SDS field name */
149  intn hdf_status; /* HDF return status */
150  char attr_name[H4_MAX_NC_NAME]; /* Name of attribute */
151  int32 attr_index = 0; /* Index of attribute */
152  int32 hdf_datatype; /* Attribute data type */
153  char **attrlist_tmp = NULL;
154  int32 dim_sizes[H4_MAX_VAR_DIMS];
155 
156  if (fieldname == NULL ||
157  num_attrs == NULL || attrlist == NULL)
159 
160  /* Transform HDF-EOS fid to plain HDF fid and SDS fid for file */
161  EHidinfo(fid, &HDFfid, &sdInterfaceID);
162 
163  sds_index = SDnametoindex(sdInterfaceID, fieldname);
164  sd_id = SDselect(sdInterfaceID,sds_index);
165  SDgetinfo(sd_id, name, &rank, dim_sizes, &data_type, &n_attrs);
166 
167  attrlist_tmp = (char**)calloc(n_attrs,sizeof(char**));
168  if (attrlist_tmp == NULL)
170 
171  for (attr_index = 0; attr_index < n_attrs; ++attr_index)
172  {
173  /* Get attribute information */
174  hdf_status = SDattrinfo(sd_id, attr_index, attr_name, &hdf_datatype, &count);
175  if (hdf_status == FAIL)
177 
178  attrlist_tmp[attr_index] = (char*)malloc((strlen(attr_name) + 1) * sizeof(char));
179  if (attrlist_tmp[attr_index] == NULL)
181 
182  strcpy(attrlist_tmp[attr_index],attr_name);
183  }
184  *attrlist = attrlist_tmp;
185  *num_attrs = n_attrs;
186  return MTK_SUCCESS;
187 
188 
189 ERROR_HANDLE:
190  if (attrlist_tmp != NULL)
191  MtkStringListFree(n_attrs, &attrlist_tmp);
192 
193  if (num_attrs != NULL)
194  *num_attrs = -1;
195 
196  return status_code;
197 }
198 
200  int ncid,
201  const char *fieldname,
202  int *num_attrs,
203  char **attrlist[] )
204 {
205  MTKt_status status_code; /* Return status of this function */
206  char **attrlist_tmp = NULL;
207  int *gids = NULL;
208 
209  if (fieldname == NULL ||
210  num_attrs == NULL || attrlist == NULL)
212 
213  /* Iterate over groups and fields until fieldname matches. Only the first match will be tried. */
214  MTKt_ncvarid var;
215  int found = 0;
216  {
217  int number_group;
218  int nc_status = nc_inq_grps(ncid, &number_group, NULL);
220 
221  gids = (int *)calloc(number_group, sizeof(int));
222  if (gids == NULL) MTK_ERR_CODE_JUMP(MTK_CALLOC_FAILED);
223 
224  nc_status = nc_inq_grps(ncid, &number_group, gids);
226 
227  for (int i = 0 ; i < number_group ; i++) {
228  int gid = gids[i];
229 
230  int status = MtkNCVarId(gid, fieldname, &var);
231  if (status == MTK_SUCCESS) {
232  found = 1;
233  break;
234  }
235  }
236  free(gids);
237  gids = NULL;
238  }
239 
240  if (found == 0) {
242  }
243 
244  int n_attrs;
245  {
246  int nc_status = nc_inq_varnatts(var.gid, var.varid, &n_attrs);
248  }
249 
250  attrlist_tmp = (char**)calloc(n_attrs,sizeof(char**));
251  if (attrlist_tmp == NULL)
253 
254  for (int attr_index = 0; attr_index < n_attrs; ++attr_index)
255  {
256  /* Get attribute information */
257  char attr_name[NC_MAX_NAME+1];
258  int nc_status = nc_inq_attname(var.gid, var.varid, attr_index, attr_name);
260 
261  attrlist_tmp[attr_index] = (char*)malloc((strlen(attr_name) + 1) * sizeof(char));
262  if (attrlist_tmp[attr_index] == NULL)
264 
265  strcpy(attrlist_tmp[attr_index],attr_name);
266  }
267  *attrlist = attrlist_tmp;
268  *num_attrs = n_attrs;
269  return MTK_SUCCESS;
270 
271 
272 ERROR_HANDLE:
273  if (gids != NULL) {
274  free(gids);
275  }
276  if (attrlist_tmp != NULL)
277  MtkStringListFree(n_attrs, &attrlist_tmp);
278 
279  if (num_attrs != NULL)
280  *num_attrs = -1;
281 
282  return status_code;
283 }
HDFLIBAPI int32 SDnametoindex(int32 fid, const char *name)
HDFFCLIBAPI intf intf intf * count
MTKt_status MtkStringListFree(int strcnt, char **strlist[])
Free string list.
#define H4_MAX_VAR_DIMS
Definition: hlimits.h:193
#define DFACC_READ
Definition: hdf.h:44
char * filename
Definition: cdjpeg.h:133
intn GDclose(int32)
MTKt_status MtkFieldAttrListHDF(const char *filename, const char *fieldname, int *num_attrs, char **attrlist[])
#define MTK_ERR_CODE_JUMP(code)
Definition: MisrError.h:175
EXTERNL int nc_inq_varnatts(int ncid, int varid, int *nattsp)
#define NC_NOERR
Definition: netcdf.h:313
MTKt_status MtkFieldAttrListNC(const char *filename, const char *fieldname, int *num_attrs, char **attrlist[])
MTKt_status MtkFieldAttrList(const char *filename, const char *fieldname, int *num_attrs, char **attrlist[])
Get a list of field attributes.
HDFFCLIBAPI _fcd name
HDFLIBAPI intn SDgetinfo(int32 sdsid, char *name, int32 *rank, int32 *dimsizes, int32 *nt, int32 *nattr)
#define NC_MAX_NAME
Definition: netcdf.h:275
EXTERNL int nc_inq_grps(int ncid, int *numgrps, int *ncids)
MTKt_status MtkFieldAttrListFid(int32 fid, const char *fieldname, int *num_attrs, char **attrlist[])
Version of MtkFieldAttrList that takes an HDF-EOS file identifier rather than a filename.
HDFLIBAPI int32 SDselect(int32 fid, int32 idx)
#define NC_NOWRITE
Definition: netcdf.h:201
intn EHidinfo(int32, int32 *, int32 *)
HDFLIBAPI intn SDattrinfo(int32 id, int32 idx, char *name, int32 *nt, int32 *count)
EXTERNL int nc_inq_attname(int ncid, int varid, int attnum, char *name)
MTKt_status MtkFieldAttrListNcid(int ncid, const char *fieldname, int *num_attrs, char **attrlist[])
HDFFCLIBAPI intf intf * n_attrs
#define MTK_ERR_COND_JUMP(code)
Definition: MisrError.h:188
#define H4_MAX_NC_NAME
Definition: hlimits.h:189
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_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