MISR Toolkit  1.5.1
MtkFieldAttrGet.c
Go to the documentation of this file.
1 /*===========================================================================
2 = =
3 = MtkFieldAttrGet =
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 "MisrUtil.h"
19 #include "MisrError.h"
20 #include <mfhdf.h>
21 #include <HdfEosDef.h>
22 
39  const char *filename,
40  const char *fieldname,
41  const char *attrname,
42  MTKt_DataBuffer *attrbuf )
43 {
44  MTKt_status status; /* Return status */
45 
46  status = MtkFieldAttrGetNC(filename, fieldname, attrname, attrbuf); // try netCDF
47  if (status != MTK_NETCDF_OPEN_FAILED) return status;
48 
49  return MtkFieldAttrGetHDF(filename, fieldname, attrname, attrbuf); // try HDF
50 }
51 
53  const char *filename,
54  const char *fieldname,
55  const char *attrname,
56  MTKt_DataBuffer *attrbuf )
57 {
58  MTKt_status status_code; /* Return status of this function */
59  MTKt_status status; /* Return status */
60 
61  if (filename == NULL) return MTK_NULLPTR;
62 
63  /* Open file */
64  int ncid = 0;
65  {
66  int nc_status = nc_open(filename, NC_NOWRITE, &ncid);
68  }
69 
70  /* Read grid attribute */
71  status = MtkFieldAttrGetNcid(ncid, fieldname, attrname, attrbuf);
72  MTK_ERR_COND_JUMP(status);
73 
74  /* Close file */
75  {
76  int nc_status = nc_close(ncid);
78  }
79  ncid = 0;
80 
81  return MTK_SUCCESS;
82 
83  ERROR_HANDLE:
84  if (ncid != 0) nc_close(ncid);
85  return status_code;
86 }
87 
89  const char *filename,
90  const char *fieldname,
91  const char *attrname,
92  MTKt_DataBuffer *attrbuf )
93 {
94  MTKt_status status_code; /* Return status of this function */
95  MTKt_status status; /* Return status */
96  intn hdfstatus; /* HDF-EOS return status */
97  int32 fid = FAIL; /* HDF-EOS File id */
98 
99  if (filename == NULL) MTK_ERR_CODE_JUMP(MTK_NULLPTR);
100 
101  fid = GDopen((char*)filename, DFACC_READ);
103 
104  /* Read grid attribute */
105  status = MtkFieldAttrGetFid(fid, fieldname, attrname, attrbuf);
106  MTK_ERR_COND_JUMP(status);
107 
108  hdfstatus = GDclose(fid);
110 
111  return MTK_SUCCESS;
112 ERROR_HANDLE:
113  if (fid != FAIL) GDclose(fid);
114  return status_code;
115 }
116 
117 
124  int32 fid,
125  const char *fieldname,
126  const char *attrname,
127  MTKt_DataBuffer *attrbuf )
128 {
129  MTKt_status status_code; /* Return status of this function */
130  MTKt_status status; /* Return status */
131  intn hdf_status;
132  int32 attr_index;
133  int32 hdf_datatype;
134  int32 count;
135  MTKt_DataType datatype; /* Mtk data type */
136  MTKt_DataBuffer attrbuf_tmp = MTKT_DATABUFFER_INIT; /* Temp attribute buffer */
137  char attr_name_tmp[MAXSTR];
138  int32 HDFfid = FAIL; /* HDF File ID */
139  int32 sdInterfaceID = FAIL; /* SD Interface ID (file level) */
140  int32 sd_id = FAIL; /* SDS ID (field level) */
141  int32 sds_index = 0; /* SDS index/offset for field */
142 
143  if (fieldname == NULL || attrname == NULL || attrbuf == NULL)
145 
146  /* Transform HDF-EOS fid to plain HDF fid and SDS fid for file */
147  EHidinfo(fid, &HDFfid, &sdInterfaceID);
148 
149  sds_index = SDnametoindex(sdInterfaceID, fieldname);
150  sd_id = SDselect(sdInterfaceID,sds_index);
151 
152  /* Find attribute index */
153  hdf_status = attr_index = SDfindattr(sd_id, attrname);
154  if (hdf_status == FAIL)
156 
157  /* Get attribute information */
158  hdf_status = SDattrinfo(sd_id, attr_index, attr_name_tmp, &hdf_datatype, &count);
159  if (hdf_status == FAIL)
161 
162  /* Convert to Mtk Data Type */
163  status = MtkHdfToMtkDataTypeConvert(hdf_datatype, &datatype);
164  if (status != MTK_SUCCESS)
165  MTK_ERR_CODE_JUMP(status);
166 
167  /* Allocate Memory */
168  status = MtkDataBufferAllocate(1, count, datatype, &attrbuf_tmp);
169  if (status != MTK_SUCCESS)
170  MTK_ERR_CODE_JUMP(status);
171 
172  /* Read attribute */
173  hdf_status = SDreadattr(sd_id, attr_index, attrbuf_tmp.dataptr);
174  if (hdf_status == FAIL)
176 
177  *attrbuf = attrbuf_tmp;
178 
179  return MTK_SUCCESS;
180 
181 ERROR_HANDLE:
182 
183  MtkDataBufferFree(&attrbuf_tmp);
184 
185  return status_code;
186 }
187 
189  int ncid,
190  const char *fieldname,
191  const char *attrname,
192  MTKt_DataBuffer *attrbuf )
193 {
194  MTKt_status status_code; /* Return status of this function */
195  MTKt_status status; /* Return status */
196  MTKt_DataType datatype; /* Mtk data type */
197  MTKt_DataBuffer attrbuf_tmp = MTKT_DATABUFFER_INIT; /* Temp attribute buffer */
198  int *gids = NULL;
199 
200  if (fieldname == NULL || attrname == NULL || attrbuf == NULL)
202 
203 
204  /* Iterate over groups and fields until fieldname matches. Only the first match will be tried. */
205  MTKt_ncvarid var;
206  int found = 0;
207  {
208  int number_group;
209  int nc_status = nc_inq_grps(ncid, &number_group, NULL);
211 
212  gids = (int *)calloc(number_group, sizeof(int));
213  if (gids == NULL) MTK_ERR_CODE_JUMP(MTK_CALLOC_FAILED);
214 
215  nc_status = nc_inq_grps(ncid, &number_group, gids);
217 
218  for (int i = 0 ; i < number_group ; i++) {
219  int gid = gids[i];
220 
221  status = MtkNCVarId(gid, fieldname, &var);
222  if (status == MTK_SUCCESS) {
223  found = 1;
224  break;
225  }
226  }
227  free(gids);
228  gids = NULL;
229  }
230 
231  if (found == 0) {
233  }
234 
235  nc_type nc_datatype;
236  {
237  int nc_status = nc_inq_atttype(var.gid, var.varid, attrname, &nc_datatype);
239  }
240 
241  status = MtkNcToMtkDataTypeConvert(nc_datatype, &datatype);
242  if (status != MTK_SUCCESS) MTK_ERR_CODE_JUMP(status);
243 
244  status = MtkDataBufferAllocate(1, 1, datatype, &attrbuf_tmp);
245  if (status != MTK_SUCCESS) MTK_ERR_CODE_JUMP(status);
246 
247  {
248  int nc_status = nc_get_att(var.gid, var.varid, attrname, attrbuf_tmp.dataptr);
250  }
251 
252  *attrbuf = attrbuf_tmp;
253 
254  return MTK_SUCCESS;
255 
256 ERROR_HANDLE:
257  if (gids != NULL) {
258  free(gids);
259  }
260  MtkDataBufferFree(&attrbuf_tmp);
261 
262  return status_code;
263 }
HDFLIBAPI int32 SDnametoindex(int32 fid, const char *name)
HDFFCLIBAPI intf intf intf * count
MTKt_status MtkFieldAttrGet(const char *filename, const char *fieldname, const char *attrname, MTKt_DataBuffer *attrbuf)
Get a file attribute.
MTKt_status MtkNcToMtkDataTypeConvert(nc_type nc_datatype, MTKt_DataType *datatype)
nc_type
Definition: netcdf.h:253
EXTERNL int nc_inq_atttype(int ncid, int varid, const char *name, nc_type *xtypep)
MTKt_status MtkDataBufferAllocate(int nline, int nsample, MTKt_DataType datatype, MTKt_DataBuffer *databuf)
Allocate Data Buffer.
HDFFCLIBAPI _fcd _fcd intf intf * datatype
#define DFACC_READ
Definition: hdf.h:44
char * filename
Definition: cdjpeg.h:133
intn GDclose(int32)
MTKt_DataType
Definition: MisrUtil.h:36
EXTERNL int nc_get_att(int ncid, int varid, const char *name, void *ip)
#define MTK_ERR_CODE_JUMP(code)
Definition: MisrError.h:175
2-dimensional Data Buffer
Definition: MisrUtil.h:98
HDFFCLIBAPI intf intf _fcd attrname
MTKt_status MtkHdfToMtkDataTypeConvert(int32 hdf_datatype, MTKt_DataType *datatype)
Convert HDF data type to MISR Toolkit data type.
#define NC_NOERR
Definition: netcdf.h:313
MTKt_status MtkFieldAttrGetNcid(int ncid, const char *fieldname, const char *attrname, MTKt_DataBuffer *attrbuf)
MTKt_status MtkFieldAttrGetFid(int32 fid, const char *fieldname, const char *attrname, MTKt_DataBuffer *attrbuf)
Version of MtkFieldAttrGet that takes an HDF SD file identifier rather than a filename.
HDFLIBAPI intn SDreadattr(int32 id, int32 idx, void *buf)
#define MTKT_DATABUFFER_INIT
Definition: MisrUtil.h:109
void * dataptr
Definition: MisrUtil.h:106
MTKt_status MtkDataBufferFree(MTKt_DataBuffer *databuf)
Free data buffer.
MTKt_status MtkFieldAttrGetNC(const char *filename, const char *fieldname, const char *attrname, MTKt_DataBuffer *attrbuf)
EXTERNL int nc_inq_grps(int ncid, int *numgrps, int *ncids)
HDFLIBAPI int32 SDselect(int32 fid, int32 idx)
#define NC_NOWRITE
Definition: netcdf.h:201
HDFLIBAPI int32 SDfindattr(int32 id, const char *attrname)
intn EHidinfo(int32, int32 *, int32 *)
HDFLIBAPI intn SDattrinfo(int32 id, int32 idx, char *name, int32 *nt, int32 *count)
#define MAXSTR
Definition: MisrUtil.h:28
#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)
MTKt_status MtkNCVarId(int Ncid, const char *Name, MTKt_ncvarid *Var)
Definition: MtkNCVarId.c:21
MTKt_status MtkFieldAttrGetHDF(const char *filename, const char *fieldname, const char *attrname, MTKt_DataBuffer *attrbuf)
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