MISR Toolkit  1.5.1
MtkFileGridToFieldList.c
Go to the documentation of this file.
1 /*===========================================================================
2 = =
3 = MtkFileGridToFieldList =
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 <hdf.h>
21 #include <HdfEosDef.h>
22 #include <string.h>
23 
41  const char *filename,
42  const char *gridname,
43  int *nfields,
44  char **fieldlist[] )
45 {
46  MTKt_status status; /* Return status */
47 
48  status = MtkFileGridToFieldListNC(filename, gridname, nfields, fieldlist); // try netCDF
49  if (status != MTK_NETCDF_OPEN_FAILED) return status;
50 
51  return MtkFileGridToFieldListHDF(filename, gridname, nfields, fieldlist); // try HDF
52 }
53 
55  const char *filename,
56  const char *gridname,
57  int *nfields,
58  char **fieldlist[] )
59 {
60  MTKt_status status;
61  MTKt_status status_code;
62  int ncid = 0;
63 
64  if (filename == NULL) MTK_ERR_CODE_JUMP(MTK_NULLPTR);
65 
66  /* Open file */
67  {
68  int nc_status = nc_open(filename, NC_NOWRITE, &ncid);
70  }
71 
72  /* Read list of fields. */
73  status = MtkFileGridToFieldListNcid(ncid, gridname, nfields, fieldlist);
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  int *nfields,
94  char **fieldlist[] )
95 {
96  MTKt_status status; /* Return status of called functions */
97  MTKt_status status_code; /* Return status of this function. */
98  intn hdfstatus; /* HDF-EOS return status */
99  int32 Fid = FAIL; /* HDF-EOS File ID */
100 
101  if (filename == NULL)
103 
104  /* Open HDF file for reading */
105  hdfstatus = Fid = GDopen((char*)filename,DFACC_READ);
106  if (hdfstatus == FAIL)
108 
109  /* Read list of fields. */
110  status = MtkFileGridToFieldListFid(Fid, gridname, nfields, fieldlist);
111  MTK_ERR_COND_JUMP(status);
112 
113  /* Close file. */
114  hdfstatus = GDclose(Fid);
115  if (hdfstatus == FAIL)
117  Fid = FAIL;
118 
119  return MTK_SUCCESS;
120 
121  ERROR_HANDLE:
122  if (Fid != FAIL) GDclose(Fid);
123 
124  return status_code;
125 }
126 
133  int32 Fid,
134  const char *gridname,
135  int *nfields,
136  char **fieldlist[] )
137 {
138  MTKt_status status; /* Return status of called functions */
139  MTKt_status status_code; /* Return status of this function. */
140  intn hdfstatus; /* HDF-EOS return status */
141  int32 Gid = FAIL; /* HDF-EOS Grid ID */
142  int32 num_fields = 0; /* Number of fields */
143  char *list = NULL; /* List of fields */
144  int i;
145  char *temp = NULL;
146  MTKt_FileType filetype;
147  int32 str_buffer_size = 0;
148 
149  /* Check Arguments */
150  if (fieldlist == NULL)
152 
153  *fieldlist = NULL; /* Set output to NULL to prevent freeing unallocated
154  memory in case of error. */
155 
156  if (gridname == NULL)
158 
159  if (nfields == NULL)
161 
162  /* Attach to grid */
163  hdfstatus = Gid = GDattach(Fid,(char*)gridname);
164  if (hdfstatus == FAIL)
166 
167  /* Query length of fields string */
168  hdfstatus = GDnentries(Gid, HDFE_NENTDFLD, &str_buffer_size);
169  if(hdfstatus == FAIL)
171 
172  list = (char*)malloc((str_buffer_size + 1) * sizeof(char));
173  if (list == NULL)
175 
176  /* Get list of fields */
177  hdfstatus = num_fields = GDinqfields(Gid,list,NULL,NULL);
178  if (hdfstatus == FAIL)
180 
181  *nfields = num_fields;
182  *fieldlist = (char**)calloc(num_fields,sizeof(char*));
183  if (*fieldlist == NULL)
185 
186  temp = strtok(list,",");
187  i = 0;
188  while (temp != NULL)
189  {
190  (*fieldlist)[i] = (char*)malloc((strlen(temp) + 1) * sizeof(char));
191  if ((*fieldlist)[i] == NULL)
193  strcpy((*fieldlist)[i],temp);
194  temp = strtok(NULL,",");
195  ++i;
196  }
197 
198  free(list);
199 
200  hdfstatus = GDdetach(Gid);
201  if (hdfstatus == FAIL)
203 
204  /* Add all possible unpacked and unscaled fields to raw field list */
205  status = MtkFileTypeFid(Fid, &filetype);
206  MTK_ERR_COND_JUMP(status);
207 
208  switch (filetype) {
209 
211  case MTK_GRP_TERRAIN_GM:
213  case MTK_GRP_TERRAIN_LM:
214  if (strstr(gridname, "Band") != NULL)
215  {
216  char **fieldlist_tmp;
217  char band[15];
218  char *nf[] = { "Radiance", "RDQI", "DN", "Equivalent Reflectance", "Brf" };
219  int nf_size = sizeof(nf)/sizeof(*nf);
220  char *sp;
221  int k, len;
222 
223  *nfields += nf_size;
224  fieldlist_tmp = (char**)calloc(*nfields,sizeof(char*));
225  if (fieldlist_tmp == NULL)
227 
228  /* Copy the native fields */
229  for (i = 0; i < num_fields; ++i) {
230  fieldlist_tmp[i] = (*fieldlist)[i];
231  }
232 
233  /* Isolate the Band */
234  strncpy(band, gridname, sizeof(band));
235  if ((sp = strstr(band, "Band")) == NULL)
237  *sp = '\0';
238 
239  /* Fill in unpacked/unscaled fields */
240  for(i = 0; i < nf_size; i++) {
241  k = i + num_fields;
242  len = (int)(strlen(band) + strlen(nf[i]) + 2);
243  fieldlist_tmp[k] = (char *)malloc(len *sizeof(char));
244  if(fieldlist_tmp[k] == NULL)
246  strcpy(fieldlist_tmp[k], band);
247  strcat(fieldlist_tmp[k], " ");
248  strcat(fieldlist_tmp[k], nf[i]);
249  }
250 
251  free(*fieldlist);
252  *fieldlist = fieldlist_tmp;
253  }
254  break;
255  case MTK_AS_LAND:
256  {
257  char **fieldlist_tmp;
258  int j = 0; /* field count */
260  char attrname[MAXSTR];
261  int len;
262 
263  fieldlist_tmp = (char**)calloc(num_fields * 2,sizeof(char*));
264  if (fieldlist_tmp == NULL)
266 
267  for (i = 0; i < num_fields; ++i) {
268  fieldlist_tmp[j++] = (*fieldlist)[i];
269  strncpy(attrname, "Scale ", MAXSTR);
270  strncat(attrname, (*fieldlist)[i], MAXSTR);
271  status = MtkGridAttrGetFid(Fid, gridname, attrname, &sf);
272  if (status == MTK_SUCCESS) {
273  len = (int)(strlen("Raw ") + strlen((*fieldlist)[i]) + 1);
274  fieldlist_tmp[j] = (char *)malloc(len * sizeof(char));
275  if (fieldlist_tmp[j] == NULL)
277  strncpy(fieldlist_tmp[j], "Raw ", len);
278  strncat(fieldlist_tmp[j++], (*fieldlist)[i], len);
279  MtkDataBufferFree(&sf);
280  }
281  if (((strncmp((*fieldlist)[i], "LandHDRF", MAXSTR) == 0) &&
282  (strncmp((*fieldlist)[i], "LandHDRFUnc", MAXSTR) != 0)) ||
283  (strncmp((*fieldlist)[i], "LandBRF", MAXSTR) == 0) ||
284  (strncmp((*fieldlist)[i], "LAIDelta1", MAXSTR) ==0) ||
285  (strncmp((*fieldlist)[i], "LAIDelta2", MAXSTR) ==0)) {
286  len = (int)(strlen("Flag ") + strlen((*fieldlist)[i]) + 1);
287  fieldlist_tmp[j] = (char *)malloc(len * sizeof(char));
288  if (fieldlist_tmp[j] == NULL)
290  strncpy(fieldlist_tmp[j], "Flag ", len);
291  strncat(fieldlist_tmp[j++], (*fieldlist)[i], len);
292  }
293  }
294  *nfields = j;
295 
296  free(*fieldlist);
297  *fieldlist = (char **)realloc((void *)fieldlist_tmp,
298  *nfields * sizeof(char *));
299  if (*fieldlist == NULL)
301  }
302  break;
303  case MTK_TC_CLOUD:
304  {
305  char **fieldlist_tmp;
306  int j = 0; /* field count */
307  int len;
308 
309 
310  fieldlist_tmp = (char**)calloc(num_fields * 2,sizeof(char*));
311  if (fieldlist_tmp == NULL)
313 
314  for (i = 0; i < num_fields; ++i) {
316  fieldlist_tmp[j++] = (*fieldlist)[i];
317  MtkFieldAttrGetFid(Fid, (*fieldlist)[i], "scale_factor", &scale);
318  if (scale.nline) {
319  len = (int)(strlen("Raw ") + strlen((*fieldlist)[i]) + 1);
320  fieldlist_tmp[j] = (char *)malloc(len * sizeof(char));
321  if (fieldlist_tmp[j] == NULL)
323  strncpy(fieldlist_tmp[j], "Raw ", len);
324  strncat(fieldlist_tmp[j++], (*fieldlist)[i], len);
325  }
326  MtkDataBufferFree(&scale);
327  }
328  *nfields = j;
329  free(*fieldlist);
330  *fieldlist = (char **)realloc((void *)fieldlist_tmp,
331  *nfields * sizeof(char *));
332  if (*fieldlist == NULL)
334  break;
335  }
336  default:
337  break;
338  }
339 
340  return MTK_SUCCESS;
341 
342  ERROR_HANDLE:
343  if (fieldlist != NULL)
344  MtkStringListFree(num_fields, fieldlist);
345 
346  if (nfields != NULL)
347  *nfields = -1;
348 
349  free(list);
350  GDdetach(Gid);
351 
352  return status_code;
353 }
354 
356  int ncid,
357  const char *gridname,
358  int *nfields,
359  char **fieldlist[] )
360 {
361  return MtkFileGridToNativeFieldListNcid(ncid, gridname, nfields, fieldlist); // No derived fields
362 }
MTKt_status MtkStringListFree(int strcnt, char **strlist[])
Free string list.
HDFFCLIBAPI intf * len
#define HDFE_NENTDFLD
Definition: HdfEosDef.h:32
int32 GDattach(int32, char *)
#define DFACC_READ
Definition: hdf.h:44
char * filename
Definition: cdjpeg.h:133
intn GDclose(int32)
MTKt_status MtkFileGridToFieldListHDF(const char *filename, const char *gridname, int *nfields, char **fieldlist[])
int32 GDinqfields(int32, char *, int32 [], int32 [])
MTKt_status MtkFieldAttrGetFid(int32 sds_id, const char *fieldname, const char *attrname, MTKt_DataBuffer *attrbuf)
Version of MtkFieldAttrGet that takes an HDF SD file identifier rather than a filename.
#define MTK_ERR_CODE_JUMP(code)
Definition: MisrError.h:175
MTKt_FileType
Definition: MisrFileQuery.h:24
intn GDdetach(int32)
2-dimensional Data Buffer
Definition: MisrUtil.h:98
MTKt_status MtkFileGridToFieldListFid(int32 Fid, const char *gridname, int *nfields, char **fieldlist[])
Version of MtkFileGridToFieldList that takes an HDF-EOS file identifier rather than a filename...
HDFFCLIBAPI intf intf _fcd attrname
#define NC_NOERR
Definition: netcdf.h:313
#define MTKT_DATABUFFER_INIT
Definition: MisrUtil.h:109
MTKt_status MtkDataBufferFree(MTKt_DataBuffer *databuf)
Free data buffer.
int32 GDnentries(int32, int32, int32 *)
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 MtkFileGridToFieldListNcid(int ncid, const char *gridname, int *nfields, char **fieldlist[])
#define NC_NOWRITE
Definition: netcdf.h:201
MTKt_status MtkFileGridToFieldList(const char *filename, const char *gridname, int *nfields, char **fieldlist[])
Read list of fields from file.
HDFFCLIBAPI intf void * scale
MTKt_status MtkFileGridToNativeFieldListNcid(int ncid, const char *gridname, int *nfields, char **fieldlist[])
MTKt_status MtkFileGridToFieldListNC(const char *filename, const char *gridname, int *nfields, char **fieldlist[])
#define MAXSTR
Definition: MisrUtil.h:28
#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)
MTKt_status MtkFileTypeFid(int32 Fid, MTKt_FileType *filetype)
Version of MtkFileType that takes an HDF-EOS file identifier rather than a filename.
Definition: MtkFileType.c:119
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