MISR Toolkit  1.5.1
MtkGridAttrGet.c
Go to the documentation of this file.
1 /*===========================================================================
2 = =
3 = MtkGridAttrGet =
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 <mfhdf.h>
21 #include <HdfEosDef.h>
22 
40  const char *filename,
41  const char *gridname,
42  const char *attrname,
43  MTKt_DataBuffer *attrbuf )
44 {
45  MTKt_status status; /* Return status */
46 
47  status = MtkGridAttrGetNC(filename, gridname, attrname, attrbuf); // try netCDF
48  if (status != MTK_NETCDF_OPEN_FAILED) return status;
49 
50  return MtkGridAttrGetHDF(filename, gridname, attrname, attrbuf); // try HDF
51 }
52 
54  const char *filename,
55  const char *gridname,
56  const char *attrname,
57  MTKt_DataBuffer *attrbuf )
58 {
59  MTKt_status status_code; /* Return code 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 = MtkGridAttrGetNcid(ncid, gridname, attrname, attrbuf);
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 
90  const char *filename,
91  const char *gridname,
92  const char *attrname,
93  MTKt_DataBuffer *attrbuf )
94 {
95  MTKt_status status_code; /* Return code 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) MTK_ERR_CODE_JUMP(MTK_NULLPTR);
101 
102  fid = GDopen((char*)filename, DFACC_READ);
104 
105  /* Read grid attribute */
106  status = MtkGridAttrGetFid(fid, gridname, attrname, attrbuf);
107  MTK_ERR_COND_JUMP(status);
108 
109  hdfstatus = GDclose(fid);
111 
112  return MTK_SUCCESS;
113 ERROR_HANDLE:
114  if (fid != FAIL) GDclose(fid);
115  return status_code;
116 }
117 
124  int32 fid,
125  const char *gridname,
126  const char *attrname,
127  MTKt_DataBuffer *attrbuf )
128 {
129  MTKt_status status_code; /* Return code of this function */
130  MTKt_status status; /* Return status */
131  intn hdfstatus; /* HDF-EOS return status */
132  int32 gid = FAIL; /* HDF-EOS Grid id */
133  int32 hdf_datatype; /* HDF-EOS data type */
134  int32 hdf_datasize; /* HDF-EOS attribute datasize in bytes */
136  /* Temp attribute buffer */
137  MTKt_DataType datatype; /* Mtk data type */
138 
139  if (gridname == NULL ||
140  attrname == NULL || attrbuf == NULL)
142 
143  gid = GDattach(fid, (char*)gridname);
145 
146  hdfstatus = GDattrinfo(gid, (char*)attrname, &hdf_datatype, &hdf_datasize);
148 
149  status = MtkHdfToMtkDataTypeConvert(hdf_datatype, &datatype);
150  if (status != MTK_SUCCESS) MTK_ERR_CODE_JUMP(status);
151 
152  status = MtkDataBufferAllocate(1, 1, datatype, &attrbuf_tmp);
153  if (status != MTK_SUCCESS) MTK_ERR_CODE_JUMP(status);
154 
155  hdfstatus = GDreadattr(gid, (char*)attrname, attrbuf_tmp.dataptr);
157 
158  hdfstatus = GDdetach(gid);
160 
161  *attrbuf = attrbuf_tmp;
162 
163  return MTK_SUCCESS;
164 ERROR_HANDLE:
165  if (gid != FAIL) GDdetach(gid);
166  MtkDataBufferFree(&attrbuf_tmp);
167  return status_code;
168 }
169 
171  int ncid,
172  const char *gridname,
173  const char *attrname,
174  MTKt_DataBuffer *attrbuf )
175 {
176  MTKt_status status_code; /* Return code of this function */
177  MTKt_status status; /* Return status */
179  /* Temp attribute buffer */
180  MTKt_DataType datatype; /* Mtk data type */
181 
182  if (gridname == NULL ||
183  attrname == NULL || attrbuf == NULL)
185 
186  int group_id;
187  {
188  int nc_status = nc_inq_grp_ncid(ncid, gridname, &group_id);
190  }
191 
192  nc_type nc_datatype;
193  {
194  int nc_status = nc_inq_atttype(group_id, NC_GLOBAL, attrname, &nc_datatype);
196  }
197 
198  status = MtkNcToMtkDataTypeConvert(nc_datatype, &datatype);
199  if (status != MTK_SUCCESS) MTK_ERR_CODE_JUMP(status);
200 
201  status = MtkDataBufferAllocate(1, 1, datatype, &attrbuf_tmp);
202  if (status != MTK_SUCCESS) MTK_ERR_CODE_JUMP(status);
203 
204  {
205  int nc_status = nc_get_att(group_id, NC_GLOBAL, attrname, attrbuf_tmp.dataptr);
207  }
208 
209  *attrbuf = attrbuf_tmp;
210 
211  return MTK_SUCCESS;
212 ERROR_HANDLE:
213  MtkDataBufferFree(&attrbuf_tmp);
214  return status_code;
215 }
MTKt_status MtkGridAttrGetNC(const char *filename, const char *gridname, const char *attrname, MTKt_DataBuffer *attrbuf)
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
int32 GDattach(int32, char *)
#define DFACC_READ
Definition: hdf.h:44
char * filename
Definition: cdjpeg.h:133
intn GDreadattr(int32, char *, VOIDP)
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
#define NC_GLOBAL
Definition: netcdf.h:214
intn GDdetach(int32)
intn GDattrinfo(int32, char *, int32 *, int32 *)
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
#define MTKT_DATABUFFER_INIT
Definition: MisrUtil.h:109
void * dataptr
Definition: MisrUtil.h:106
MTKt_status MtkDataBufferFree(MTKt_DataBuffer *databuf)
Free data buffer.
#define NC_NOWRITE
Definition: netcdf.h:201
MTKt_status MtkGridAttrGet(const char *filename, const char *gridname, const char *attrname, MTKt_DataBuffer *attrbuf)
Get a grid attribute.
MTKt_status MtkGridAttrGetHDF(const char *filename, const char *gridname, const char *attrname, MTKt_DataBuffer *attrbuf)
MTKt_status MtkGridAttrGetFid(int32 fid, const char *gridname, const char *attrname, MTKt_DataBuffer *attrbuf)
Version of MtkFileGridAttrGet that takes an HDF-EOS file ID rather than a filename.
MTKt_status MtkGridAttrGetNcid(int ncid, const char *gridname, const char *attrname, MTKt_DataBuffer *attrbuf)
#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_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