MISR Toolkit  1.5.1
MtkReadBlock.c
Go to the documentation of this file.
1 /*===========================================================================
2 = =
3 = MtkReadBlock =
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 "MisrReadData.h"
18 #include "MisrUtil.h"
19 #include "MisrError.h"
20 #include <stdlib.h>
21 #include <mfhdf.h>
22 #include <HdfEosDef.h>
23 
52  const char *filename,
53  const char *gridname,
54  const char *fieldname,
55  int block,
56  MTKt_DataBuffer *databuf )
57 {
58  MTKt_status status; /* Return status */
59 
60  status = MtkReadBlockNC(filename, gridname, fieldname, block, databuf); // try netCDF
61  if (status != MTK_NETCDF_OPEN_FAILED) return status;
62 
63  return MtkReadBlockHDF(filename, gridname, fieldname, block, databuf); // try HDF
64 }
65 
67  const char *filename,
68  const char *gridname,
69  const char *fieldname,
70  int block,
71  MTKt_DataBuffer *databuf )
72 {
73  MTKt_status status;
74  MTKt_status status_code;
75  int ncid = 0;
76 
77  if (filename == NULL) MTK_ERR_CODE_JUMP(MTK_NULLPTR);
78 
79  /* Open file */
80  {
81  int nc_status = nc_open(filename, NC_NOWRITE, &ncid);
83  }
84 
85  /* Read block. */
86  status = MtkReadBlockNcid(ncid, gridname, fieldname, block, databuf);
87  MTK_ERR_COND_JUMP(status);
88 
89  /* Close file */
90  {
91  int nc_status = nc_close(ncid);
93  }
94  ncid = 0;
95 
96  return MTK_SUCCESS;
97 
98  ERROR_HANDLE:
99  if (ncid != 0) nc_close(ncid);
100  return status_code;
101 }
102 
104  const char *filename,
105  const char *gridname,
106  const char *fieldname,
107  int block,
108  MTKt_DataBuffer *databuf )
109 {
110  MTKt_status status; /* Return status */
111  MTKt_status status_code; /* Return code of this function */
112  intn hdfstatus; /* HDF-EOS return status */
113  int32 fid = FAIL; /* HDF-EOS File id */
114 
115  if (filename == NULL) MTK_ERR_CODE_JUMP(MTK_NULLPTR);
116 
117  /* Open file. */
118  fid = GDopen((char*)filename, DFACC_READ);
120 
121  /* Read block. */
122  status = MtkReadBlockFid(fid, gridname, fieldname, block, databuf);
123  MTK_ERR_COND_JUMP(status);
124 
125  /* Close file. */
126  hdfstatus = GDclose(fid);
128  fid = FAIL;
129 
130  return MTK_SUCCESS;
131  ERROR_HANDLE:
132  if (fid != FAIL) GDclose(fid);
133  return status_code;
134 }
135 
142  int32 fid,
143  const char *gridname,
144  const char *fieldname,
145  int block,
146  MTKt_DataBuffer *databuf )
147 {
148  MTKt_status status; /* Return status */
149  MTKt_status status_code; /* Return code of this function */
150  intn hdfstatus; /* HDF-EOS return status */
151  int32 gid = FAIL; /* HDF-EOS Grid id */
152  int32 start[10]; /* HDF-EOS start dimension */
153  int32 edge[10]; /* HDF-EOS edge dimension */
154  int32 hdf_datatype; /* HDF-EOS data type */
155  int32 rank; /* HDF-EOS rank */
156  int32 dims[10]; /* HDF-EOS dimensions */
157  char dimlist[80]; /* HDF-EOS dimension name list */
158  char *basefield = NULL; /* Base fieldname */
159  int nextradims; /* Number of extra dimensions */
160  int *extradims = NULL; /* Extra dimension list */
162  /* Temp data buffer */
163  MTKt_DataType datatype; /* Mtk data type */
164  int i; /* Loop index */
165 
166  if (gridname == NULL) MTK_ERR_CODE_JUMP(MTK_NULLPTR);
167  if (fieldname == NULL) MTK_ERR_CODE_JUMP(MTK_NULLPTR);
168  if (block < 1 || block > 180) MTK_ERR_CODE_JUMP(MTK_OUTBOUNDS);
169 
170  gid = GDattach(fid, (char*)gridname);
172 
173  status = MtkParseFieldname(fieldname, &basefield, &nextradims, &extradims);
174  if (status != MTK_SUCCESS) MTK_ERR_CODE_JUMP(status);
175 
176  hdfstatus = GDfieldinfo(gid, basefield, &rank, dims, &hdf_datatype, dimlist);
178 
179  if (rank != nextradims + 3) MTK_ERR_CODE_JUMP(MTK_BAD_ARGUMENT);
180 
181  status = MtkHdfToMtkDataTypeConvert(hdf_datatype, &datatype);
182  if (status != MTK_SUCCESS) MTK_ERR_CODE_JUMP(status);
183 
184  status = MtkDataBufferAllocate(dims[1], dims[2], datatype, &databuf_tmp);
185  if (status != MTK_SUCCESS) MTK_ERR_CODE_JUMP(status);
186 
187  start[0] = block - 1;
188  start[1] = 0;
189  start[2] = 0;
190 
191  edge[0] = 1;
192  edge[1] = dims[1];
193  edge[2] = dims[2];
194 
195  for (i = 3; i < rank; i++) {
196  start[i] = extradims[i-3];
197  edge[i] = 1;
198  }
199 
200  hdfstatus = GDreadfield(gid, basefield, start, NULL, edge,
201  databuf_tmp.dataptr);
203 
204  hdfstatus = GDdetach(gid);
206 
207  *databuf = databuf_tmp;
208 
209  free(basefield);
210  free(extradims);
211  return MTK_SUCCESS;
212  ERROR_HANDLE:
213  if (gid != FAIL) GDdetach(gid);
214  if (basefield != NULL) free(basefield);
215  if (extradims != NULL) free(extradims);
216  MtkDataBufferFree(&databuf_tmp);
217  return status_code;
218 }
219 
221  int ncid,
222  const char *gridname,
223  const char *fieldname,
224  int block,
225  MTKt_DataBuffer *databuf )
226 {
227  MTKt_status status; /* Return status */
228  MTKt_status status_code; /* Return code of this function */
229  char *basefield = NULL; /* Base fieldname */
230  int nextradims; /* Number of extra dimensions */
231  int *extradims = NULL; /* Extra dimension list */
233  /* Temp data buffer */
234  MTKt_DataType datatype; /* Mtk data type */
235 
236  if (gridname == NULL) MTK_ERR_CODE_JUMP(MTK_NULLPTR);
237  if (fieldname == NULL) MTK_ERR_CODE_JUMP(MTK_NULLPTR);
238  if (block < 1 || block > 180) MTK_ERR_CODE_JUMP(MTK_OUTBOUNDS);
239 
240  int group_id;
241  {
242  int nc_status = nc_inq_grp_ncid(ncid, gridname, &group_id);
244  }
245 
246  int size_x;
247  int size_y;
248  int start_x;
249  int start_y;
250  int found = 0;
251 
252  {
253  int nc_status;
254  nc_status = nc_get_att(group_id, NC_GLOBAL, "block_size_in_lines", &size_x);
256  nc_status = nc_get_att(group_id, NC_GLOBAL, "block_size_in_samples", &size_y);
258 
259  int bvarid;
260  nc_status = nc_inq_varid(group_id, "Block_Number", &bvarid);
262 
263  int dimid[NC_MAX_DIMS];
264  nc_status = nc_inq_var(group_id, bvarid, NULL, NULL, NULL, dimid, NULL);
266 
267  size_t number_block;
268  nc_status = nc_inq_dimlen(group_id, dimid[0], &number_block);
270 
271  int block_numbers[180];
272  nc_status = nc_get_var_int(group_id, bvarid, block_numbers);
274 
275  size_t block_index;
276  for (size_t i = 0 ; i < number_block ; i++) {
277  if (block_numbers[i] == block) {
278  found = 1;
279  block_index = i;
280  break;
281  }
282  }
283 
284  if (found == 1) {
285 
286  int xvarid;
287  nc_status = nc_inq_varid(group_id, "Block_Start_X_Index", &xvarid);
289 
290  int yvarid;
291  nc_status = nc_inq_varid(group_id, "Block_Start_Y_Index", &yvarid);
293 
294  nc_status = nc_get_var1_int(group_id, xvarid, &block_index, &start_x);
296 
297  nc_status = nc_get_var1_int(group_id, yvarid, &block_index, &start_y);
299  }
300  }
301 
302  if (found == 0) MTK_ERR_CODE_JUMP(MTK_OUTBOUNDS); // Block is outside file extent
303 
304  status = MtkParseFieldname(fieldname, &basefield, &nextradims, &extradims);
305  if (status != MTK_SUCCESS) MTK_ERR_CODE_JUMP(status);
306 
307  int rank;
308  nc_type nc_datatype;
309  MTKt_ncvarid var;
310  {
311  int nc_status;
312 
313  status = MtkNCVarId(group_id, basefield, &var);
314  MTK_ERR_COND_JUMP(status);
315 
316  nc_status = nc_inq_varndims(var.gid, var.varid, &rank);
318  nc_status = nc_inq_vartype(var.gid, var.varid, &nc_datatype);
320  }
321 
322  if (rank != nextradims + 2) MTK_ERR_CODE_JUMP(MTK_BAD_ARGUMENT);
323 
324  status = MtkNcToMtkDataTypeConvert(nc_datatype, &datatype);
325  if (status != MTK_SUCCESS) MTK_ERR_CODE_JUMP(status);
326 
327  status = MtkDataBufferAllocate(size_x, size_y, datatype, &databuf_tmp);
328  if (status != MTK_SUCCESS) MTK_ERR_CODE_JUMP(status);
329 
330  size_t start[10];
331  size_t count[10];
332 
333  start[0] = start_x;
334  start[1] = start_y;
335 
336  count[0] = size_x;
337  count[1] = size_y;
338 
339  for (int i = 2; i < rank; i++) {
340  start[i] = extradims[i-2];
341  count[i] = 1;
342  }
343 
344  {
345  int nc_status = nc_get_vara(var.gid, var.varid, start, count, databuf_tmp.dataptr);
347  }
348 
349  *databuf = databuf_tmp;
350 
351  free(basefield);
352  free(extradims);
353  return MTK_SUCCESS;
354  ERROR_HANDLE:
355  if (basefield != NULL) free(basefield);
356  if (extradims != NULL) free(extradims);
357  MtkDataBufferFree(&databuf_tmp);
358  return status_code;
359 }
HDFFCLIBAPI intf intf intf * count
MTKt_status MtkNcToMtkDataTypeConvert(nc_type nc_datatype, MTKt_DataType *datatype)
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
int32 GDattach(int32, char *)
#define DFACC_READ
Definition: hdf.h:44
char * filename
Definition: cdjpeg.h:133
MTKt_status MtkReadBlockNcid(int ncid, const char *gridname, const char *fieldname, int block, MTKt_DataBuffer *databuf)
Definition: MtkReadBlock.c:220
intn GDclose(int32)
MTKt_DataType
Definition: MisrUtil.h:36
EXTERNL int nc_get_att(int ncid, int varid, const char *name, void *ip)
HDFFCLIBAPI void intf dims[]
MTKt_status MtkReadBlockHDF(const char *filename, const char *gridname, const char *fieldname, int block, MTKt_DataBuffer *databuf)
Definition: MtkReadBlock.c:103
MTKt_status MtkParseFieldname(const char *fieldname, char **basefieldname, int *ndim, int **dimlist)
Parses extra dimensions from fieldnames.
EXTERNL int nc_inq_var(int ncid, int varid, char *name, nc_type *xtypep, int *ndimsp, int *dimidsp, int *nattsp)
#define MTK_ERR_CODE_JUMP(code)
Definition: MisrError.h:175
#define NC_GLOBAL
Definition: netcdf.h:214
intn GDdetach(int32)
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 MtkReadBlockNC(const char *filename, const char *gridname, const char *fieldname, int block, MTKt_DataBuffer *databuf)
Definition: MtkReadBlock.c:66
#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_status MtkReadBlock(const char *filename, const char *gridname, const char *fieldname, int block, MTKt_DataBuffer *databuf)
Read block of data.
Definition: MtkReadBlock.c:51
EXTERNL int nc_get_var_int(int ncid, int varid, int *ip)
EXTERNL int nc_get_var1_int(int ncid, int varid, const size_t *indexp, int *ip)
intn GDfieldinfo(int32, char *, int32 *, int32 [], int32 *, char *)
EXTERNL int nc_inq_dimlen(int ncid, int dimid, size_t *lenp)
MTKt_status MtkReadBlockFid(int32 fid, const char *gridname, const char *fieldname, int block, MTKt_DataBuffer *databuf)
Version of MtkReadBlock that takes an HDF-EOS file identifier rather than a filename.
Definition: MtkReadBlock.c:141
#define NC_NOWRITE
Definition: netcdf.h:201
HDFFCLIBAPI intf intf start[]
#define NC_MAX_DIMS
Definition: netcdf.h:272
EXTERNL int nc_get_vara(int ncid, int varid, const size_t *startp, const size_t *countp, void *ip)
EXTERNL int nc_inq_varid(int ncid, const char *name, int *varidp)
intn GDreadfield(int32, char *, int32 [], int32 [], int32 [], VOIDP)
#define MTK_ERR_COND_JUMP(code)
Definition: MisrError.h:188
EXTERNL int nc_inq_varndims(int ncid, int varid, int *ndimsp)
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