MISR Toolkit  1.5.1
MtkFileCoreMetaDataRaw.c
Go to the documentation of this file.
1 /*===========================================================================
2 = =
3 = MtkFileCoreMetaDataRaw =
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 "MisrUtil.h"
20 #include <hdf.h>
21 
39  const char *filename,
40  char **coremeta )
41 {
42  MTKt_status status; /* Return status */
43 
44  status = MtkFileCoreMetaDataRawNC(filename, coremeta); // try netCDF
45  if (status != MTK_NETCDF_OPEN_FAILED) return status;
46 
47  return MtkFileCoreMetaDataRawHDF(filename, coremeta); // try HDF
48 }
49 
51  const char *filename,
52  char **coremeta )
53 {
54  MTKt_status status_code; /* Return status of this function */
55  MTKt_status status; /* Return status */
56 
57  if (filename == NULL) return MTK_NULLPTR;
58 
59  /* Open file */
60  int ncid = 0;
61  {
62  int nc_status = nc_open(filename, NC_NOWRITE, &ncid);
64  }
65 
66  /* Read grid attribute */
67  status = MtkFileCoreMetaDataRawNcid(ncid, coremeta);
68  MTK_ERR_COND_JUMP(status);
69 
70  /* Close file */
71  {
72  int nc_status = nc_close(ncid);
74  }
75  ncid = 0;
76 
77  return MTK_SUCCESS;
78 
79  ERROR_HANDLE:
80  if (ncid != 0) nc_close(ncid);
81  return status_code;
82 }
83 
85  const char *filename,
86  char **coremeta )
87 {
88  MTKt_status status_code; /* Return status of this function */
89  MTKt_status status; /* Return status */
90  intn hdf_status;
91  int32 sds_id = FAIL;
92 
93  if (filename == NULL)
95 
96  /* Open HDF File */
97  hdf_status = sds_id = SDstart(filename, DFACC_READ);
98  if (hdf_status == FAIL)
100 
101  /* Read coremetadata. */
102  status = MtkFileCoreMetaDataRawFid(sds_id, coremeta);
103  MTK_ERR_COND_JUMP(status);
104 
105  /* Close HDF File */
106  hdf_status = SDend(sds_id);
107  if (hdf_status == FAIL)
109  sds_id = FAIL;
110 
111  return MTK_SUCCESS;
112 
113 ERROR_HANDLE:
114  if (sds_id != FAIL)
115  SDend(sds_id);
116 
117  return status_code;
118 }
119 
126  int32 sds_id,
127  char **coremeta )
128 {
129  MTKt_status status_code; /* Return status of this function */
130  intn hdf_status;
131  int32 attr_index;
132  char attr_name[80];
133  int32 data_type;
134  int32 count;
135  char *attr_buf = NULL;
136 
137  if (coremeta == NULL)
139 
140  /* Find attribute index */
141  hdf_status = attr_index = SDfindattr(sds_id, "coremetadata");
142  if (hdf_status == FAIL)
144 
145  /* Get attribute information */
146  hdf_status = SDattrinfo(sds_id, attr_index, attr_name, &data_type, &count);
147  if (hdf_status == FAIL)
149 
150  /* Allocate Memory */
151  attr_buf = (char*)malloc((count + 1) * sizeof(char));
152  if (attr_buf == NULL)
154 
155  /* Read attribute */
156  hdf_status = SDreadattr(sds_id, attr_index, (VOIDP)attr_buf);
157  if (hdf_status == FAIL)
159 
160  attr_buf[count] = '\0';
161 
162  *coremeta = attr_buf;
163 
164  return MTK_SUCCESS;
165 
166 ERROR_HANDLE:
167  if (attr_buf != NULL)
168  free(attr_buf);
169 
170  return status_code;
171 }
172 
174  int ncid,
175  char **coremeta )
176 {
177  MTKt_status status_code; /* Return status of this function */
178  char *attr_buf = NULL;
179 
180  if (coremeta == NULL) {
182  }
183 
184  const char *group_name = "HDFEOS INFORMATION";
185  const char *var_name = "coremetadata";
186 
187  {
188  int group_id;
189  int nc_status = nc_inq_ncid(ncid, group_name, &group_id);
191 
192  MTKt_ncvarid var;
193  int status = MtkNCVarId(group_id, var_name, &var);
194  MTK_ERR_COND_JUMP(status);
195 
196  int ndims;
197  nc_status = nc_inq_varndims(var.gid, var.varid, &ndims);
199 
200  if (ndims != 1) {
202  }
203 
204  int dimid;
205  nc_status = nc_inq_vardimid(var.gid, var.varid, &dimid);
207 
208  size_t len;
209  nc_status = nc_inq_dimlen(group_id, dimid, &len);
211 
212  /* Allocate Memory */
213  attr_buf = (char*)calloc((len + 1), sizeof(char));
214  if (attr_buf == NULL)
216 
217  nc_status = nc_get_var_text(var.gid, var.varid, attr_buf);
219  }
220 
221  *coremeta = attr_buf;
222 
223  return MTK_SUCCESS;
224 
225 ERROR_HANDLE:
226  if (attr_buf != NULL)
227  free(attr_buf);
228 
229  return status_code;
230 }
HDFFCLIBAPI intf intf intf * count
HDFFCLIBAPI intf * len
EXTERNL int nc_inq_vardimid(int ncid, int varid, int *dimidsp)
#define DFACC_READ
Definition: hdf.h:44
char * filename
Definition: cdjpeg.h:133
#define MTK_ERR_CODE_JUMP(code)
Definition: MisrError.h:175
HDFLIBAPI int32 SDstart(const char *name, int32 accs)
#define NC_NOERR
Definition: netcdf.h:313
HDFLIBAPI intn SDreadattr(int32 id, int32 idx, void *buf)
EXTERNL int nc_inq_ncid(int ncid, const char *name, int *grp_ncid)
EXTERNL int nc_get_var_text(int ncid, int varid, char *ip)
MTKt_status MtkFileCoreMetaDataRawNC(const char *filename, char **coremeta)
EXTERNL int nc_inq_dimlen(int ncid, int dimid, size_t *lenp)
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 MtkFileCoreMetaDataRawFid(int32 sds_id, char **coremeta)
Version of MtkFileCoreMetaDataRaw that takes an HDF SD file identifier rather than a filename...
MTKt_status MtkFileCoreMetaDataRawHDF(const char *filename, char **coremeta)
#define MTK_ERR_COND_JUMP(code)
Definition: MisrError.h:188
MTKt_status MtkFileCoreMetaDataRawNcid(int ncid, char **coremeta)
EXTERNL int nc_inq_varndims(int ncid, int varid, int *ndimsp)
MTKt_status
Definition: MisrError.h:11
EXTERNL int nc_close(int ncid)
MTKt_status MtkNCVarId(int Ncid, const char *Name, MTKt_ncvarid *Var)
Definition: MtkNCVarId.c:21
MTKt_status MtkFileCoreMetaDataRaw(const char *filename, char **coremeta)
Read core metadata from a MISR product file into a buffer.
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