OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
hdfio.c
Go to the documentation of this file.
1 /*******************************************************************
2 
3  hdfio.c - general routines for hdf file input, output
4 
5  Contents:
6  hdfio_open - open the file
7  hdfio_rd_gattr - read a global attribute
8  hdfio_rd_sd - read a science dataset
9  hdfio_close - close a hdf file
10 
11  *******************************************************************/
12 
13 #include <string.h>
14 #include "hdfio.h"
15 
16 int hdfio_open(char *fname, hdfio_struc *hdfinfo)
17 /*******************************************************************
18 
19  hdfio_open
20 
21  purpose: open a general hdf file
22 
23  Returns type: int - return status: 0 is good
24 
25  Parameters: (in calling order)
26  Type Name I/O Description
27  ---- ---- --- -----------
28  char * fname I name of file to open
29  hdfio_struc * hdfinfo O information struct
30  about the opened file
31 
32  Modification history:
33  Programmer Date Description of change
34  ---------- ---- ---------------------
35  W. Robinson, SAIC 5 Aug 2004 Original development
36 
37  *******************************************************************/
38  {
39 
40  if ((hdfinfo->fid = Hopen(fname, DFACC_RDONLY, 0)) < 0) {
41  printf("hdfio_open: Failed on the Hopen of \n%s\n", fname);
42  return -1;
43  }
44 
45  /*
46  * SDstart opens the hdf interface and inits the SD interface, wierd.
47  * apparently, if both SD and HDF are used, both these need to
48  * be called.
49  */
50  if ((hdfinfo->sdfid = SDstart(fname, DFACC_RDONLY)) < 0) {
51  printf("hdfio_open: Failure at SDstart of \n%s\n", fname);
52  hdfio_close(*hdfinfo);
53  return -1;
54  }
55 
56  /*
57  * ok, it's open.
58  */
59  return 0;
60 }
61 
62 int hdfio_rd_gattr(hdfio_struc hdfinfo, char *name, int32 n_type,
63  int32 count, void *data)
64 /*******************************************************************
65 
66  hdfio_rd_gattr
67 
68  purpose: read a global attribute to a waiting array (if size correct
69  and type is correct)
70 
71  Returns type: int - return status: 0 is good
72 
73  Parameters: (in calling order)
74  Type Name I/O Description
75  ---- ---- --- -----------
76  hdfio_struc hdfinfo I information struct
77  about the opened file
78  char * name I name of glabal attr to read
79  int32 n_type I number type expected using
80  HDF nomenclature
81  int32 count I count of values
82  void* data O array of data from the
83  global attribute
84 
85  Modification history:
86  Programmer Date Description of change
87  ---------- ---- ---------------------
88  W. Robinson, SAIC 5 Aug 2004 Original development
89 
90  *******************************************************************/
91  {
92  int32 attr_index;
93  char a_name[200];
94  int32 a_n_type, a_count;
95 
96  /*
97  * get the index for that attribute name
98  */
99  if ((attr_index = SDfindattr(hdfinfo.sdfid, name))
100  == -1) {
101  printf("hdfio_rd_gattr: Error in SDfindattr for attribute: '%s'\n",
102  name);
103  return -1;
104  }
105  /*
106  * find out about the attribute and be sure it is the type and size expected
107  */
108  if (SDattrinfo(hdfinfo.sdfid, attr_index, a_name, &a_n_type, &a_count)
109  == -1) {
110  printf("hdfio_rd_gattr: Error in SDattrinfo for attribute: '%s'\n",
111  name);
112  return -1;
113  }
114 
115  if (a_n_type != n_type) {
116  printf(
117  "hdfio_rd_gattr: attribute: '%s', difference in number type found\n",
118  name);
119  printf(" expected: %d, read: %d\n", (int) n_type, (int) a_n_type);
120  return -1;
121  }
122 
123  if (((n_type != DFNT_CHAR) && (a_count != count)) ||
124  ((n_type == DFNT_CHAR) && (count < a_count))) {
125  printf("hdfio_rd_gattr: attribute: '%s', difference in count found\n",
126  name);
127  printf(" expected: %d, read: %d\n", (int) count, (int) a_count);
128  return -1;
129  }
130 
131  /*
132  * read in the stuff
133  */
134  if (SDreadattr(hdfinfo.sdfid, attr_index, data) == -1) {
135  printf("hdfio_rd_gattr: attribute: '%s', Error reading data\n",
136  name);
137  return -1;
138  }
139  return 0;
140 }
141 
142 int hdfio_rd_sd(hdfio_struc hdfinfo, char *arr_name, void *array)
143 /*******************************************************************
144 
145  hdfio_rd_sd
146 
147  purpose: read a full science dataset to a float array
148 
149  Returns type: int - return status: 0 is good
150 
151  Parameters: (in calling order)
152  Type Name I/O Description
153  ---- ---- --- -----------
154  hdfio_struc hdfinfo I hdf information struct
155  about the opened file
156  char * arr_name I name of science dataset
157  to read
158  void * array O array of data from the sds
159  already allocated outside
160 
161  Modification history:
162  Programmer Date Description of change
163  ---------- ---- ---------------------
164  W. Robinson 9-Feb-1995 Original development
165  W. Robinson, SAIC 5 Aug 2004 update this version to do more general
166  void array type
167 
168  *******************************************************************/
169  {
170  int j;
171  int32 index, rank, sdid, numbertype, nattrs, data_dims[5];
172  int32 start[5], edge[5];
173  char name[H4_MAX_NC_NAME]; /* MAX_NC_NAME is max # chars for this */
174 
175  /*
176  * SDnametoindex is used to zero in on the data we want,
177  * ie. get the index of the data
178  */
179  if ((index = SDnametoindex(hdfinfo.sdfid, arr_name)) < 0) {
180  printf("hdfio_rd_sd: couldn't find %s in the dataset\n",
181  arr_name);
182  return -1;
183  }
184 
185  /*
186  * next, get the SD ID for the data
187  */
188  if ((sdid = SDselect(hdfinfo.sdfid, index)) < 0) {
189  printf("hdfio_rd_sd: Failed in SDselect for item %s\n",
190  arr_name);
191  return -1;
192  }
193 
194  /*
195  * now, get the size information of the data item
196  */
197  if (SDgetinfo(sdid, name, &rank, data_dims, &numbertype,
198  &nattrs) < 0) {
199  printf("hdfio_rd_sd: Failed in SDgetinfo for item %s\n",
200  arr_name);
201  return -1;
202  }
203 
204  /*
205  * compute the read controls for whole array
206  */
207 
208  for (j = 0; j < rank; j++) {
209  start[j] = 0;
210  edge[j] = data_dims[j];
211  }
212 
213  /*
214  * read the data
215  */
216  if (SDreaddata(sdid, start, NULL, edge, array) < 0) {
217  printf("hdfio_rd_sd: failure to read data for item %s\n", arr_name);
218  return -1;
219  }
220  return 0;
221 }
222 
223 void hdfio_close(hdfio_struc hdfinfo)
224 /*******************************************************************
225 
226  hdfio_close
227 
228  purpose: close the hdf dataset
229 
230  Returns type: void - nothing
231 
232  Parameters: (in calling order)
233  Type Name I/O Description
234  ---- ---- --- -----------
235  hdfio_struc hdfinfo O information struct
236  about the opened file
237 
238  Modification history:
239  Programmer Date Description of change
240  ---------- ---- ---------------------
241  W. Robinson, SAIC, 5 Aug 2004 Original development
242 
243  *******************************************************************/
244  {
245 
246  /*
247  * just close the file up
248  */
249  Hclose(hdfinfo.fid);
250  SDend(hdfinfo.sdfid);
251 }
an array had not been initialized Several spelling and grammar corrections were which is read from the appropriate MCF the above metadata values were hard coded A problem calculating the average background DN for SWIR bands when the moon is in the space view port was corrected The new algorithm used to calculate the average background DN for all reflective bands when the moon is in the space view port is now the same as the algorithm employed by the thermal bands For non SWIR changes in the averages are typically less than Also for non SWIR the black body DNs remain a backup in case the SV DNs are not available For SWIR the changes in computed averages were larger because the old which used the black body suffered from contamination by the micron leak As a consequence of the if SV DNs are not available for the SWIR the EV pixels will not be the granule time is used to identify the appropriate tables within the set given for one LUT the first two or last two tables respectively will be used for the interpolation If there is only one LUT in the set of it will be treated as a constant LUT The manner in which Earth View data is checked for saturation was changed Previously the raw Earth View DNs and Space View DNs were checked against the lookup table values contained in the table dn_sat The change made is to check the raw Earth and Space View DNs to be sure they are less than the maximum saturation value and to check the Space View subtracted Earth View dns against a set of values contained in the new lookup table dn_sat_ev The metadata configuration and ASSOCIATEDINSTRUMENTSHORTNAME from the MOD02HKM product The same metatdata with extensions and were removed from the MOD021KM and MOD02OBC products ASSOCIATEDSENSORSHORTNAME was set to MODIS in all products These changes are reflected in new File Specification which users may consult for exact the pow functions were eliminated in Emissive_Cal and Emissive bands replaced by more efficient code Other calculations throughout the code were also made more efficient Aside from a few round off there was no difference to the product The CPU time decreased by about for a day case and for a night case A minor bug in calculating the uncertainty index for emissive bands was corrected The frame index(0-based) was previously being used the frame number(1-based) should have been used. There were only a few minor changes to the uncertainty index(maximum of 1 digit). 3. Some inefficient arrays(Sigma_RVS_norm_sq) were eliminated and some code lines in Preprocess_L1A_Data were moved into Process_OBCEng_Emiss. There were no changes to the product. Required RAM was reduced by 20 MB. Now
int j
Definition: decode_rs.h:73
#define NULL
Definition: decode_rs.h:63
void hdfio_close(hdfio_struc hdfinfo)
Definition: hdfio.c:223
int hdfio_rd_gattr(hdfio_struc hdfinfo, char *name, int32 n_type, int32 count, void *data)
Definition: hdfio.c:62
int hdfio_open(char *fname, hdfio_struc *hdfinfo)
Definition: hdfio.c:16
no change in intended resolving MODur00064 Corrected handling of bad ephemeris attitude data
Definition: HISTORY.txt:356
Extra metadata that will be written to the HDF4 file l2prod rank
int hdfio_rd_sd(hdfio_struc hdfinfo, char *arr_name, void *array)
Definition: hdfio.c:142
int count
Definition: decode_rs.h:79