MISR Toolkit  1.5.1
MtkFileBlockMetaFieldRead.c
Go to the documentation of this file.
1 /*===========================================================================
2 = =
3 = MtkFileBlockMetaFieldRead =
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 "MisrUtil.h"
19 #include "MisrError.h"
20 #include <hdf.h>
21 
41  const char *filename,
42  const char *blockmetaname,
43  const char *fieldname,
44  MTKt_DataBuffer *blockmetabuf )
45 {
46  MTKt_status status_code; /* Return status of this function */
47  MTKt_status status; /* Return status */
48  intn hdfstatus;
49  int32 file_id = FAIL;
50 
51  if (filename == NULL)
53 
54  /* Open HDF File */
55  file_id = HDFopen(filename, DFACC_READ, 0);
56  if (file_id == FAIL)
58 
59  /* Read block metadata field. */
60  status = MtkFileBlockMetaFieldReadFid(file_id, blockmetaname, fieldname,
61  blockmetabuf);
62  MTK_ERR_COND_JUMP(status);
63 
64  /* Close HDF file */
65  hdfstatus = HDFclose(file_id);
66  if (hdfstatus == FAIL)
68  file_id = FAIL;
69 
70  return MTK_SUCCESS;
71 
72 ERROR_HANDLE:
73  if (file_id != FAIL)
74  HDFclose(file_id);
75 
76  return status_code;
77 }
78 
85  int32 file_id,
86  const char *blockmetaname,
87  const char *fieldname,
88  MTKt_DataBuffer *blockmetabuf )
89 {
90  MTKt_status status_code; /* Return status of this function */
91  MTKt_status status; /* Return status */
92  intn hdfstatus;
93  int32 hdf_datatype;
94  int32 vdata_ref = FAIL;
95  int32 vdata_id = FAIL;
96  int32 count;
97  int32 field_index;
98  int32 n_records;
99  MTKt_DataBuffer blockmetabuf_tmp = MTKT_DATABUFFER_INIT;
100  /* Temp attribute buffer */
101  MTKt_DataType datatype; /* Mtk data type */
102  int vstart_active = 0;
103 
104  if (blockmetaname == NULL || fieldname == NULL ||
105  blockmetabuf == NULL)
107 
108  /* Initialize the vdata interface */
109  hdfstatus = Vstart(file_id);
110  if (hdfstatus == FAIL)
112  vstart_active = 1;
113 
114  /* Find reference number of block metadata structure */
115  vdata_ref = VSfind(file_id, blockmetaname);
116  if (vdata_ref == 0) /* Failure */
118 
119  vdata_id = VSattach(file_id, vdata_ref, "r");
120  if (vdata_id == FAIL)
122 
123  /* Find field index */
124  hdfstatus = VSfindex(vdata_id, fieldname, &field_index);
125  if (hdfstatus == FAIL)
127 
128  /* Determine data type of field */
129  hdf_datatype = VFfieldtype(vdata_id, field_index);
130  if (hdf_datatype == FAIL)
132 
133  /* Convert from HDF data type to MTK data type */
134  status = MtkHdfToMtkDataTypeConvert(hdf_datatype, &datatype);
135  MTK_ERR_COND_JUMP(status);
136 
137  /* Number of records in a field */
138  n_records = VSelts(vdata_id);
139  if (n_records == FAIL)
141 
142  /* Allocate Memory */
143  if (datatype == MTKe_char8) /* Data is a string */
144  {
145  count = VSsizeof(vdata_id, (char*)fieldname);
146  if (count == FAIL)
148 
149  status = MtkDataBufferAllocate(n_records, count, datatype, &blockmetabuf_tmp);
150  MTK_ERR_COND_JUMP(status);
151  }
152  else
153  {
154  count = VFfieldorder(vdata_id, field_index);
155  if (count == FAIL)
157 
158  status = MtkDataBufferAllocate(n_records, count, datatype, &blockmetabuf_tmp);
159  MTK_ERR_COND_JUMP(status);
160  }
161 
162  /* Select field to read */
163  hdfstatus = VSsetfields(vdata_id, fieldname);
164  if (hdfstatus == FAIL)
166 
167  /* Read field, since only one field is being read there is no need to unpack */
168  hdfstatus = VSread(vdata_id, blockmetabuf_tmp.dataptr, n_records, FULL_INTERLACE);
169  if (hdfstatus == FAIL)
171 
172  hdfstatus = VSdetach(vdata_id);
173  if (hdfstatus == FAIL)
175 
176  /* End access to the vdata interface */
177  hdfstatus = Vend(file_id);
178  if (hdfstatus == FAIL)
180  vstart_active = 0;
181 
182  *blockmetabuf = blockmetabuf_tmp;
183 
184  return MTK_SUCCESS;
185 
186 ERROR_HANDLE:
187  if (vdata_id != FAIL)
188  VSdetach(vdata_id);
189 
190  if (vstart_active)
191  Vend(file_id);
192 
193  MtkDataBufferFree(&blockmetabuf_tmp);
194 
195  return status_code;
196 }
HDFFCLIBAPI intf intf intf * count
HDFLIBAPI int32 VFfieldorder(int32 vkey, int32 idx)
HDFLIBAPI int32 VSread(int32 vkey, uint8 buf[], int32 nelt, int32 interlace)
HDFLIBAPI int32 VSsizeof(int32 vkey, char *fields)
HDFLIBAPI int32 VFfieldtype(int32 vkey, int32 idx)
MTKt_status MtkDataBufferAllocate(int nline, int nsample, MTKt_DataType datatype, MTKt_DataBuffer *databuf)
Allocate Data Buffer.
HDFFCLIBAPI _fcd _fcd intf intf * datatype
#define FULL_INTERLACE
Definition: hdf.h:130
#define DFACC_READ
Definition: hdf.h:44
char * filename
Definition: cdjpeg.h:133
MTKt_DataType
Definition: MisrUtil.h:36
HDFLIBAPI int32 VSdetach(int32 vkey)
#define MTK_ERR_CODE_JUMP(code)
Definition: MisrError.h:175
2-dimensional Data Buffer
Definition: MisrUtil.h:98
MTKt_status MtkHdfToMtkDataTypeConvert(int32 hdf_datatype, MTKt_DataType *datatype)
Convert HDF data type to MISR Toolkit data type.
MTKt_status MtkFileBlockMetaFieldRead(const char *filename, const char *blockmetaname, const char *fieldname, MTKt_DataBuffer *blockmetabuf)
Read a block metadata field.
#define MTKT_DATABUFFER_INIT
Definition: MisrUtil.h:109
void * dataptr
Definition: MisrUtil.h:106
MTKt_status MtkDataBufferFree(MTKt_DataBuffer *databuf)
Free data buffer.
#define HDFopen(f, a, d)
Definition: hdf.h:160
HDFLIBAPI int32 VSfind(HFILEID f, const char *vsname)
#define Vstart(f)
Definition: hdf.h:162
HDFLIBAPI int32 VSattach(HFILEID f, int32 vsref, const char *accesstype)
#define MTK_ERR_COND_JUMP(code)
Definition: MisrError.h:188
MTKt_status
Definition: MisrError.h:11
HDFLIBAPI intn VSfindex(int32 vsid, const char *fieldname, int32 *fldindex)
HDFLIBAPI int32 VSelts(int32 vkey)
#define Vend(f)
Definition: hdf.h:163
MTKt_status MtkFileBlockMetaFieldReadFid(int32 file_id, const char *blockmetaname, const char *fieldname, MTKt_DataBuffer *blockmetabuf)
Version of MtkFileBlockMetaFileRead that takes an HDF file identifier rather than a filename...
#define FAIL
Definition: hdf.h:94
#define HDFclose(f)
Definition: hdf.h:161
HDFLIBAPI intn VSsetfields(int32 vkey, const char *fields)

MISR Toolkit - Copyright © 2005 - 2020 Jet Propulsion Laboratory
Generated on Fri Jun 19 2020 22:49:51