MISR Toolkit  1.5.1
MtkFileToBlockRange.c
Go to the documentation of this file.
1 /*===========================================================================
2 = =
3 = MtkFileToBlockRange =
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 #include <mfhdf.h>
21 
36  const char *filename,
37  int *start_block,
38  int *end_block )
39 {
40  MTKt_status status; /* Return status */
41 
42  status = MtkFileToBlockRangeNC(filename, start_block, end_block); // try netCDF
43  if (status != MTK_NETCDF_OPEN_FAILED) return status;
44 
45  return MtkFileToBlockRangeHDF(filename, start_block, end_block); // try HDF
46 }
47 
49  const char *filename,
50  int *start_block,
51  int *end_block )
52 {
53  MTKt_status status_code; /* Return status of this function */
54  MTKt_status status; /* Return status */
55 
56  if (filename == NULL) return MTK_NULLPTR;
57 
58  /* Open file */
59  int ncid = 0;
60  {
61  int nc_status = nc_open(filename, NC_NOWRITE, &ncid);
63  }
64 
65  /* Read grid attribute */
66  status = MtkFileToBlockRangeNcid(ncid, start_block, end_block);
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  int *start_block,
86  int *end_block )
87 {
88  MTKt_status status_code; /* Return status of this function */
89  MTKt_status status; /* Return status */
90  int32 sid=FAIL; /* HDF SD file identifier */
91  intn hdfstatus; /* HDF return status */
92 
93  if (filename == NULL)
95 
96  /* Open file. */
97  sid = SDstart(filename, DFACC_READ);
98  if (sid == FAIL)
100 
101  /* Read block range. */
102  status = MtkFileToBlockRangeFid(sid, start_block, end_block);
103  MTK_ERR_COND_JUMP(status);
104 
105  /* Close file. */
106  hdfstatus = SDend(sid);
107  if (hdfstatus == FAIL)
109  sid = FAIL;
110 
111  return MTK_SUCCESS;
112 
113 ERROR_HANDLE:
114  if (sid != FAIL) SDend(sid);
115  return status_code;
116 }
117 
124  int32 sid,
125  int *start_block,
126  int *end_block )
127 {
128  MTKt_status status_code; /* Return status of this function */
129  intn status; /* HDF return status */
130  int32 attr_index; /* HDF attribute index */
131  int32 start_block_tmp; /* Temp start block */
132  int32 end_block_tmp; /* Temp end block */
133 
134  if (start_block == NULL || end_block == NULL)
136 
137  attr_index = SDfindattr(sid, "Start_block");
138  if (attr_index == FAIL)
140 
141  status = SDreadattr(sid, attr_index, &start_block_tmp);
142  if (status == FAIL)
144 
145  attr_index = SDfindattr(sid, "End block");
146  if (attr_index == FAIL)
148 
149  status = SDreadattr(sid, attr_index, &end_block_tmp);
150  if (status == FAIL)
152 
153  *start_block = start_block_tmp;
154  *end_block = end_block_tmp;
155 
156  return MTK_SUCCESS;
157 
158 ERROR_HANDLE:
159  return status_code;
160 }
161 
163  int ncid,
164  int *start_block,
165  int *end_block )
166 {
167  MTKt_status status_code; /* Return status of this function */
168  int start_block_tmp; /* Temp start block */
169  int end_block_tmp; /* Temp end block */
170 
171  if (start_block == NULL || end_block == NULL)
173 
174  {
175  int nc_status = nc_get_att_int(ncid, NC_GLOBAL, "Start_block", &start_block_tmp);
177  }
178  {
179  int nc_status = nc_get_att_int(ncid, NC_GLOBAL, "End_block", &end_block_tmp);
181  }
182 
183  *start_block = start_block_tmp;
184  *end_block = end_block_tmp;
185 
186  return MTK_SUCCESS;
187 
188 ERROR_HANDLE:
189  return status_code;
190 }
#define DFACC_READ
Definition: hdf.h:44
char * filename
Definition: cdjpeg.h:133
MTKt_status MtkFileToBlockRangeHDF(const char *filename, int *start_block, int *end_block)
EXTERNL int nc_get_att_int(int ncid, int varid, const char *name, int *ip)
#define MTK_ERR_CODE_JUMP(code)
Definition: MisrError.h:175
#define NC_GLOBAL
Definition: netcdf.h:214
MTKt_status MtkFileToBlockRangeFid(int32 sid, int *start_block, int *end_block)
Version of MtkFileToBlockRange that takes an HDF SD file identifier rather than a filename...
HDFLIBAPI int32 SDstart(const char *name, int32 accs)
#define NC_NOERR
Definition: netcdf.h:313
HDFLIBAPI intn SDreadattr(int32 id, int32 idx, void *buf)
HDFLIBAPI intn SDend(int32 fid)
#define NC_NOWRITE
Definition: netcdf.h:201
HDFLIBAPI int32 SDfindattr(int32 id, const char *attrname)
MTKt_status MtkFileToBlockRangeNC(const char *filename, int *start_block, int *end_block)
#define MTK_ERR_COND_JUMP(code)
Definition: MisrError.h:188
MTKt_status
Definition: MisrError.h:11
EXTERNL int nc_close(int ncid)
MTKt_status MtkFileToBlockRangeNcid(int ncid, int *start_block, int *end_block)
EXTERNL int nc_open(const char *path, int mode, int *ncidp)
MTKt_status MtkFileToBlockRange(const char *filename, int *start_block, int *end_block)
Read start and end block numbers from file.
#define FAIL
Definition: hdf.h:94

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