MISR Toolkit  1.5.1
MtkFileLGID.c
Go to the documentation of this file.
1 /*===========================================================================
2 = =
3 = MtkFileLGID =
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 "MisrError.h"
19 #include <hdf.h>
20 
38  const char *filename,
39  char **lgid )
40 {
41  MTKt_status status; /* Return status */
42 
43  status = MtkFileLGIDNC(filename, lgid); // try netCDF
44  if (status != MTK_NETCDF_OPEN_FAILED) return status;
45 
46  return MtkFileLGIDHDF(filename, lgid); // try HDF
47 }
48 
50  const char *filename,
51  char **lgid )
52 {
53  MTKt_status status;
54  MTKt_status status_code;
55  int ncid = 0;
56 
57  if (filename == NULL) MTK_ERR_CODE_JUMP(MTK_NULLPTR);
58 
59  /* Open file */
60  {
61  int nc_status = nc_open(filename, NC_NOWRITE, &ncid);
63  }
64 
65  /* Get Local Granual ID */
66  status = MtkFileLGIDNcid(ncid,lgid);
67  MTK_ERR_COND_JUMP(status);
68 
69  /* Close file */
70  {
71  int nc_status = nc_close(ncid);
73  }
74  ncid = 0;
75 
76  return MTK_SUCCESS;
77 
78  ERROR_HANDLE:
79  if (ncid != 0) nc_close(ncid);
80  return status_code;
81 }
82 
84  const char *filename,
85  char **lgid )
86 {
87  MTKt_status status;
88  MTKt_status status_code; /* Return status of this function */
89  intn hdf_status;
90  int32 sds_id = FAIL;
91 
92  if (filename == NULL)
94 
95  /* Open HDF File */
96  hdf_status = sds_id = SDstart(filename, DFACC_READ);
97  if (hdf_status == FAIL)
99 
100  /* Get Local Granual ID */
101  status = MtkFileLGIDFid(sds_id,lgid);
102  if (status != MTK_SUCCESS)
103  MTK_ERR_CODE_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  hdf_status = SDend(sds_id);
116  }
117  return status_code;
118 }
119 
120 /* ----------------------------------------------- */
121 /* MtkFileLGIDFid */
122 /* ----------------------------------------------- */
123 
130  int32 sds_id,
131  char **lgid )
132 {
133  MTKt_status status_code; /* Return status of this function */
134  intn hdf_status;
135  int32 attr_index;
136  char attr_name[80];
137  int32 data_type;
138  int32 count;
139  char *attr_buf = NULL;
140  char *fn_start;
141  char *fn_end;
142 
143  if (lgid == NULL)
145 
146  /* Find attribute index */
147  hdf_status = attr_index = SDfindattr(sds_id, "coremetadata");
148  if (hdf_status == FAIL)
150 
151  /* Get attribute information */
152  hdf_status = SDattrinfo(sds_id, attr_index, attr_name, &data_type, &count);
153  if (hdf_status == FAIL)
155 
156  /* Allocate Memory */
157  attr_buf = (char*)malloc((count + 1) * sizeof(char));
158  if (attr_buf == NULL)
160 
161  /* Read attribute */
162  hdf_status = SDreadattr(sds_id, attr_index, (VOIDP)attr_buf);
163  if (hdf_status == FAIL)
165 
166  attr_buf[count] = '\0';
167 
168  fn_start = strstr(attr_buf, "MISR_AM1_");
169  if (fn_start == NULL) {
170  fn_start = strstr(attr_buf, "MISR_HR_");
171  if (fn_start == NULL)
173  }
174  fn_end = strstr(attr_buf, ".hdf");
175  if (fn_end == NULL)
177 
178  fn_end += 4; /* Move to end of file name */
179 
180  *lgid = (char*)malloc((fn_end - fn_start + 1) * sizeof(char));
181  strncpy(*lgid,fn_start,fn_end - fn_start);
182  (*lgid)[fn_end - fn_start] = '\0';
183 
184  free(attr_buf);
185 
186  return MTK_SUCCESS;
187 
188 ERROR_HANDLE:
189  if (attr_buf != NULL)
190  free(attr_buf);
191 
192  return status_code;
193 }
194 
196  int ncid,
197  char **lgid )
198 {
199  MTKt_status status_code; /* Return status of this function */
200  char *lgid_tmp = NULL;
201 
202  if (lgid == NULL)
204 
205  size_t len;
206  {
207  int nc_status = nc_inq_attlen(ncid, NC_GLOBAL, "Local_granule_id", &len);
209  }
210 
211  lgid_tmp = calloc(len+1, sizeof(char)); /* Add 1 for null terminator */
212 
213  {
214  int nc_status = nc_get_att(ncid, NC_GLOBAL, "Local_granule_id", lgid_tmp);
216  }
217 
218  *lgid = lgid_tmp;
219 
220  return MTK_SUCCESS;
221 
222 ERROR_HANDLE:
223  if (lgid_tmp != NULL)
224  free(lgid_tmp);
225 
226  return status_code;
227 }
MTKt_status MtkFileLGID(const char *filename, char **lgid)
Determine local granual ID of MISR product file.
Definition: MtkFileLGID.c:37
HDFFCLIBAPI intf intf intf * count
HDFFCLIBAPI intf * len
#define DFACC_READ
Definition: hdf.h:44
char * filename
Definition: cdjpeg.h:133
MTKt_status MtkFileLGIDHDF(const char *filename, char **lgid)
Definition: MtkFileLGID.c:83
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
EXTERNL int nc_inq_attlen(int ncid, int varid, const char *name, size_t *lenp)
HDFLIBAPI int32 SDstart(const char *name, int32 accs)
#define NC_NOERR
Definition: netcdf.h:313
HDFLIBAPI intn SDreadattr(int32 id, int32 idx, void *buf)
MTKt_status MtkFileLGIDNcid(int ncid, char **lgid)
Definition: MtkFileLGID.c:195
HDFLIBAPI intn SDend(int32 fid)
MTKt_status MtkFileLGIDFid(int32 sds_id, char **lgid)
Version of MtkFileLGID that takes an HDF SDS ID rather than a filename.
Definition: MtkFileLGID.c:129
#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 MtkFileLGIDNC(const char *filename, char **lgid)
Definition: MtkFileLGID.c:49
#define MTK_ERR_COND_JUMP(code)
Definition: MisrError.h:188
MTKt_status
Definition: MisrError.h:11
EXTERNL int nc_close(int 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