MISR Toolkit  1.5.1
MtkFillValueGet.c
Go to the documentation of this file.
1 /*===========================================================================
2 = =
3 = MtkFillValueGet =
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 
41  const char *filename,
42  const char *gridname,
43  const char *fieldname,
44  MTKt_DataBuffer *fillbuf )
45 {
46  MTKt_status status; /* Return status */
47 
48  status = MtkFillValueGetNC(filename, gridname, fieldname, fillbuf); // try netCDF
49  if (status != MTK_NETCDF_OPEN_FAILED) return status;
50 
51  return MtkFillValueGetHDF(filename, gridname, fieldname, fillbuf); // try HDF
52 }
53 
55  const char *filename,
56  const char *gridname,
57  const char *fieldname,
58  MTKt_DataBuffer *fillbuf )
59 {
60  MTKt_status status_code; /* Return code of this function */
61  MTKt_status status; /* Return status */
62 
63  if (filename == NULL) return MTK_NULLPTR;
64 
65  /* Open file */
66  int ncid = 0;
67  {
68  int nc_status = nc_open(filename, NC_NOWRITE, &ncid);
70  }
71 
72  /* Get fill value */
73  status = MtkFillValueGetNcid(ncid, gridname, fieldname, fillbuf);
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  MTKt_DataBuffer *fillbuf )
95 {
96  MTKt_status status_code; /* Return code of this function */
97  MTKt_status status; /* Return status */
98  intn hdfstatus; /* HDF-EOS return status */
99  int32 fid = FAIL; /* HDF-EOS File id */
100 
101  if (filename == NULL) MTK_ERR_CODE_JUMP(MTK_NULLPTR);
102 
103  fid = GDopen((char*)filename, DFACC_READ);
105 
106  /* Get fill value */
107  status = MtkFillValueGetFid(fid, gridname, fieldname, fillbuf);
108  MTK_ERR_COND_JUMP(status);
109 
110  hdfstatus = GDclose(fid);
112  fid = FAIL;
113 
114  return MTK_SUCCESS;
115 
116  ERROR_HANDLE:
117  if (fid != FAIL) GDclose(fid);
118  return status_code;
119 }
120 
127  int32 fid,
128  const char *gridname,
129  const char *fieldname,
130  MTKt_DataBuffer *fillbuf )
131 {
132  MTKt_status status_code; /* Return code of this function */
133  MTKt_status status; /* Return status */
134  intn hdfstatus; /* HDF-EOS return status */
135  int32 gid = FAIL; /* HDF-EOS Grid id */
136  int32 hdf_datatype; /* HDF-EOS data type */
137  int32 rank; /* Not used */
138  int32 dims[10]; /* Not used */
139  char dimlist[80]; /* Not used */
141  /* Temp fill buffer */
142  MTKt_DataType datatype; /* Mtk data type */
143  char *basefield = NULL; /* Base fieldname */
144  int nextradims; /* Number of extra dimensions */
145  int *extradims = NULL; /* Extra dimension list */
146 
147  if (gridname == NULL ||
148  fieldname == NULL || fillbuf == NULL)
150 
151  gid = GDattach(fid, (char*)gridname);
153 
154  status = MtkParseFieldname(fieldname, &basefield, &nextradims, &extradims);
155  MTK_ERR_COND_JUMP(status);
156 
157  hdfstatus = GDfieldinfo(gid, basefield, &rank, dims, &hdf_datatype, dimlist);
159 
160  status = MtkHdfToMtkDataTypeConvert(hdf_datatype, &datatype);
161  if (status != MTK_SUCCESS) MTK_ERR_CODE_JUMP(status);
162 
163  status = MtkDataBufferAllocate(1, 1, datatype, &fillbuf_tmp);
164  if (status != MTK_SUCCESS) MTK_ERR_CODE_JUMP(status);
165 
166  hdfstatus = GDgetfillvalue(gid, basefield, fillbuf_tmp.dataptr);
167  if (hdfstatus == FAIL) {
168  if (datatype == MTKe_uint8) {
169  fillbuf_tmp.data.u8[0][0] = 253;
170  } else {
172  }
173  }
174 
175  hdfstatus = GDdetach(gid);
177 
178  *fillbuf = fillbuf_tmp;
179 
180  free(basefield);
181  free(extradims);
182  return MTK_SUCCESS;
183  ERROR_HANDLE:
184  if (gid != FAIL) GDdetach(gid);
185  if (basefield != NULL) free(basefield);
186  if (extradims != NULL) free(extradims);
187  MtkDataBufferFree(&fillbuf_tmp);
188  return status_code;
189 }
190 
192  int ncid,
193  const char *gridname,
194  const char *fieldname,
195  MTKt_DataBuffer *fillbuf )
196 {
197  MTKt_status status_code; /* Return code of this function */
198  MTKt_status status; /* Return status */
200  /* Temp fill buffer */
201  MTKt_DataType datatype; /* Mtk data type */
202  char *basefield = NULL; /* Base fieldname */
203  int nextradims; /* Number of extra dimensions */
204  int *extradims = NULL; /* Extra dimension list */
205 
206  if (gridname == NULL ||
207  fieldname == NULL || fillbuf == NULL)
209 
210  status = MtkParseFieldname(fieldname, &basefield, &nextradims, &extradims);
211  MTK_ERR_COND_JUMP(status);
212 
213  int group_id;
214  {
215  int nc_status = nc_inq_grp_ncid(ncid, gridname, &group_id);
217  }
218 
219  MTKt_ncvarid var;
220  status = MtkNCVarId(group_id, basefield, &var);
221  MTK_ERR_COND_JUMP(status);
222 
223  nc_type nc_datatype;
224  {
225  int nc_status = nc_inq_vartype(var.gid, var.varid, &nc_datatype);
227  }
228 
229  status = MtkNcToMtkDataTypeConvert(nc_datatype, &datatype);
230  if (status != MTK_SUCCESS) MTK_ERR_CODE_JUMP(status);
231 
232  status = MtkDataBufferAllocate(1, 1, datatype, &fillbuf_tmp);
233  if (status != MTK_SUCCESS) MTK_ERR_CODE_JUMP(status);
234 
235  {
236  int nc_status = nc_get_att(var.gid, var.varid, "_FillValue", fillbuf_tmp.dataptr);
238  }
239 
240  *fillbuf = fillbuf_tmp;
241 
242  free(basefield);
243  free(extradims);
244  return MTK_SUCCESS;
245  ERROR_HANDLE:
246  if (basefield != NULL) free(basefield);
247  if (extradims != NULL) free(extradims);
248  MtkDataBufferFree(&fillbuf_tmp);
249  return status_code;
250 }
MTKt_status MtkNcToMtkDataTypeConvert(nc_type nc_datatype, MTKt_DataType *datatype)
MTKt_status MtkFillValueGetFid(int32 fid, const char *gridname, const char *fieldname, MTKt_DataBuffer *fillbuf)
Version of MtkFillValueGet that takes an HDF-EOS file ID rather than a filename.
nc_type
Definition: netcdf.h:253
MTKt_status MtkDataBufferAllocate(int nline, int nsample, MTKt_DataType datatype, MTKt_DataBuffer *databuf)
Allocate Data Buffer.
HDFFCLIBAPI _fcd _fcd intf intf * datatype
MTKt_status MtkFillValueGet(const char *filename, const char *gridname, const char *fieldname, MTKt_DataBuffer *fillbuf)
Get fill value.
int32 GDattach(int32, char *)
MTKt_status MtkFillValueGetNcid(int ncid, const char *gridname, const char *fieldname, MTKt_DataBuffer *fillbuf)
#define DFACC_READ
Definition: hdf.h:44
char * filename
Definition: cdjpeg.h:133
MTKt_DataBufferType data
Definition: MisrUtil.h:104
intn GDclose(int32)
MTKt_DataType
Definition: MisrUtil.h:36
intn GDgetfillvalue(int32, char *, VOIDP)
EXTERNL int nc_get_att(int ncid, int varid, const char *name, void *ip)
HDFFCLIBAPI void intf dims[]
MTKt_status MtkParseFieldname(const char *fieldname, char **basefieldname, int *ndim, int **dimlist)
Parses extra dimensions from fieldnames.
#define MTK_ERR_CODE_JUMP(code)
Definition: MisrError.h:175
intn GDdetach(int32)
2-dimensional Data Buffer
Definition: MisrUtil.h:98
MTKt_status MtkFillValueGetHDF(const char *filename, const char *gridname, const char *fieldname, MTKt_DataBuffer *fillbuf)
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.
EXTERNL int nc_inq_vartype(int ncid, int varid, nc_type *xtypep)
MTKt_uint8 ** u8
Definition: MisrUtil.h:86
intn GDfieldinfo(int32, char *, int32 *, int32 [], int32 *, char *)
MTKt_status MtkFillValueGetNC(const char *filename, const char *gridname, const char *fieldname, MTKt_DataBuffer *fillbuf)
#define NC_NOWRITE
Definition: netcdf.h:201
#define MTK_ERR_COND_JUMP(code)
Definition: MisrError.h:188
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_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