MISR Toolkit  1.5.1
MtkFileAttrGet.c
Go to the documentation of this file.
1 /*===========================================================================
2 = =
3 = MtkFileAttrGet =
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 *attrname,
41  MTKt_DataBuffer *attrbuf )
42 {
43  MTKt_status status; /* Return status */
44 
45  status = MtkFileAttrGetNC(filename, attrname, attrbuf); // try netCDF
46  if (status != MTK_NETCDF_OPEN_FAILED) return status;
47 
48  return MtkFileAttrGetHDF(filename, attrname, attrbuf); // try HDF
49 }
50 
52  const char *filename,
53  const char *attrname,
54  MTKt_DataBuffer *attrbuf )
55 {
56  MTKt_status status_code; /* Return status of this function */
57  MTKt_status status; /* Return status */
58 
59  if (filename == NULL) return MTK_NULLPTR;
60 
61  /* Open file */
62  int ncid = 0;
63  {
64  int nc_status = nc_open(filename, NC_NOWRITE, &ncid);
66  }
67 
68  /* Read grid attribute */
69  status = MtkFileAttrGetNcid(ncid, attrname, attrbuf);
70  MTK_ERR_COND_JUMP(status);
71 
72  /* Close file */
73  {
74  int nc_status = nc_close(ncid);
76  }
77  ncid = 0;
78 
79  return MTK_SUCCESS;
80 
81  ERROR_HANDLE:
82  if (ncid != 0) nc_close(ncid);
83  return status_code;
84 }
85 
87  const char *filename,
88  const char *attrname,
89  MTKt_DataBuffer *attrbuf )
90 {
91  MTKt_status status_code; /* Return status of this function */
92  MTKt_status status; /* Return status */
93  intn hdf_status;
94  int32 sds_id = FAIL;
95 
96  if (filename == NULL)
98 
99  /* Open HDF File */
100  hdf_status = sds_id = SDstart(filename, DFACC_READ);
101  if (hdf_status == FAIL)
103 
104  /* Read attribute. */
105  status = MtkFileAttrGetFid(sds_id, attrname, attrbuf);
106  MTK_ERR_COND_JUMP(status);
107 
108  /* Close HDF File */
109  hdf_status = SDend(sds_id);
110  if (hdf_status == FAIL)
112  sds_id = FAIL;
113 
114  return MTK_SUCCESS;
115 
116 ERROR_HANDLE:
117  if (sds_id != FAIL)
118  SDend(sds_id);
119  return status_code;
120 }
121 
128  int32 sds_id,
129  const char *attrname,
130  MTKt_DataBuffer *attrbuf )
131 {
132  MTKt_status status_code; /* Return status of this function */
133  MTKt_status status; /* Return status */
134  intn hdf_status;
135  int32 attr_index;
136  int32 hdf_datatype;
137  int32 count;
138  MTKt_DataType datatype; /* Mtk data type */
139  MTKt_DataBuffer attrbuf_tmp = MTKT_DATABUFFER_INIT; /* Temp attribute buffer */
140  char attr_name_tmp[MAXSTR];
141 
142  if (attrname == NULL || attrbuf == NULL)
144 
145  /* Find attribute index */
146  hdf_status = attr_index = SDfindattr(sds_id, attrname);
147  if (hdf_status == FAIL)
149 
150  /* Get attribute information */
151  hdf_status = SDattrinfo(sds_id, attr_index, attr_name_tmp, &hdf_datatype, &count);
152  if (hdf_status == FAIL)
154 
155  /* Convert to Mtk Data Type */
156  status = MtkHdfToMtkDataTypeConvert(hdf_datatype, &datatype);
157  if (status != MTK_SUCCESS)
158  MTK_ERR_CODE_JUMP(status);
159 
160  /* Allocate Memory */
161  status = MtkDataBufferAllocate(1, count, datatype, &attrbuf_tmp);
162  if (status != MTK_SUCCESS)
163  MTK_ERR_CODE_JUMP(status);
164 
165  /* Read attribute */
166  hdf_status = SDreadattr(sds_id, attr_index, attrbuf_tmp.dataptr);
167  if (hdf_status == FAIL)
169 
170  *attrbuf = attrbuf_tmp;
171 
172  return MTK_SUCCESS;
173 
174 ERROR_HANDLE:
175 
176  MtkDataBufferFree(&attrbuf_tmp);
177 
178  return status_code;
179 }
180 
182  int ncid,
183  const char *attrname,
184  MTKt_DataBuffer *attrbuf )
185 {
186  MTKt_status status_code; /* Return status of this function */
187  MTKt_status status; /* Return status */
188  MTKt_DataType datatype; /* Mtk data type */
189  MTKt_DataBuffer attrbuf_tmp = MTKT_DATABUFFER_INIT; /* Temp attribute buffer */
190 
191  if (attrname == NULL || attrbuf == NULL)
193 
194  nc_type nc_datatype;
195  {
196  int nc_status = nc_inq_atttype(ncid, NC_GLOBAL, attrname, &nc_datatype);
198  }
199 
200  status = MtkNcToMtkDataTypeConvert(nc_datatype, &datatype);
201  if (status != MTK_SUCCESS) MTK_ERR_CODE_JUMP(status);
202 
203  status = MtkDataBufferAllocate(1, 1, datatype, &attrbuf_tmp);
204  if (status != MTK_SUCCESS) MTK_ERR_CODE_JUMP(status);
205 
206  {
207  int nc_status = nc_get_att(ncid, NC_GLOBAL, attrname, attrbuf_tmp.dataptr);
209  }
210  *attrbuf = attrbuf_tmp;
211 
212  return MTK_SUCCESS;
213 
214 ERROR_HANDLE:
215 
216  MtkDataBufferFree(&attrbuf_tmp);
217 
218  return status_code;
219 }
HDFFCLIBAPI intf intf intf * count
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
MTKt_DataType
Definition: MisrUtil.h:36
EXTERNL int nc_get_att(int ncid, int varid, const char *name, void *ip)
MTKt_status MtkFileAttrGetHDF(const char *filename, const char *attrname, MTKt_DataBuffer *attrbuf)
#define MTK_ERR_CODE_JUMP(code)
Definition: MisrError.h:175
#define NC_GLOBAL
Definition: netcdf.h:214
2-dimensional Data Buffer
Definition: MisrUtil.h:98
HDFFCLIBAPI intf intf _fcd attrname
HDFLIBAPI int32 SDstart(const char *name, int32 accs)
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
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.
HDFLIBAPI intn SDend(int32 fid)
#define NC_NOWRITE
Definition: netcdf.h:201
HDFLIBAPI int32 SDfindattr(int32 id, const char *attrname)
HDFLIBAPI intn SDattrinfo(int32 id, int32 idx, char *name, int32 *nt, int32 *count)
MTKt_status MtkFileAttrGetFid(int32 sds_id, const char *attrname, MTKt_DataBuffer *attrbuf)
Version of MtkFileAttrGet that takes an HDF SD file identifier rather than a filename.
#define MAXSTR
Definition: MisrUtil.h:28
#define MTK_ERR_COND_JUMP(code)
Definition: MisrError.h:188
MTKt_status
Definition: MisrError.h:11
MTKt_status MtkFileAttrGet(const char *filename, const char *attrname, MTKt_DataBuffer *attrbuf)
Get a file attribute.
EXTERNL int nc_close(int ncid)
EXTERNL int nc_open(const char *path, int mode, int *ncidp)
MTKt_status MtkFileAttrGetNcid(int ncid, const char *attrname, MTKt_DataBuffer *attrbuf)
#define FAIL
Definition: hdf.h:94
MTKt_status MtkFileAttrGetNC(const char *filename, const char *attrname, MTKt_DataBuffer *attrbuf)

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