-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHDFCFStrField.cc
More file actions
352 lines (287 loc) · 11.1 KB
/
HDFCFStrField.cc
File metadata and controls
352 lines (287 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/////////////////////////////////////////////////////////////////////////////
// This file is part of the hdf4 data handler for the OPeNDAP data server.
// It retrieves the HDF4 DFNT_CHAR >1D array and then send to DAP as a DAP string array for the CF option.
// Authors: MuQun Yang <myang6@hdfgroup.org>
// Copyright (c) 2010-2012 The HDF Group
/////////////////////////////////////////////////////////////////////////////
#include "config.h"
#include "config_hdf.h"
#include <iostream>
#include <sstream>
#include <cassert>
#include <libdap/debug.h>
#include <libdap/InternalErr.h>
#include <BESDebug.h>
#include <BESLog.h>
#include "HDFCFUtil.h"
#include "HDFCFStrField.h"
#include "HDF4RequestHandler.h"
using namespace std;
using namespace libdap;
bool
HDFCFStrField::read ()
{
BESDEBUG("h4","Coming to HDFCFStrField read "<<endl);
if(length() == 0)
return true;
#if 0
string check_pass_fileid_key_str="H4.EnablePassFileID";
bool check_pass_fileid_key = false;
check_pass_fileid_key = HDFCFUtil::check_beskeys(check_pass_fileid_key_str);
#endif
bool check_pass_fileid_key = HDF4RequestHandler::get_pass_fileid();
// Note that one dimensional character array is one string,
// so the rank for character arrays should be rank from string+1
// offset32,step32 and count32 will be new subsetting parameters for
// character arrays.
vector<int32>offset32;
offset32.resize(rank+1);
vector<int32>count32;
count32.resize(rank+1);
vector<int32>step32;
step32.resize(rank+1);
int nelms = 1;
if (rank != 0) {
// Declare offset, count and step,
vector<int>offset;
offset.resize(rank);
vector<int>count;
count.resize(rank);
vector<int>step;
step.resize(rank);
// Declare offset, count and step,
// Note that one dimensional character array is one string,
// so the rank for character arrays should be rank from string+1
// Obtain offset,step and count from the client expression constraint
nelms = format_constraint (&offset[0], &step[0], &count[0]);
// Assign the offset32,count32 and step32 up to the dimension rank-1.
// Will assign the dimension rank later.
for (int i = 0; i < rank; i++) {
offset32[i] = (int32) offset[i];
count32[i] = (int32) count[i];
step32[i] = (int32) step[i];
}
}
// Initialize the temp. returned value.
int32 r = 0;
// First SDS
if(false == is_vdata) {
int32 sdid = -1;
if(false == check_pass_fileid_key) {
sdid = SDstart (const_cast < char *>(filename.c_str ()), DFACC_READ);
if (sdid < 0) {
ostringstream eherr;
eherr << "File " << filename.c_str () << " cannot be open.";
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
}
else
sdid = h4fd;
int32 sdsid = 0;
int32 sdsindex = SDreftoindex (sdid, fieldref);
if (sdsindex == -1) {
HDFCFUtil::close_fileid(sdid,-1,-1,-1,check_pass_fileid_key);
ostringstream eherr;
eherr << "SDS index " << sdsindex << " is not right.";
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
// Obtain this SDS ID.
sdsid = SDselect (sdid, sdsindex);
if (sdsid < 0) {
HDFCFUtil::close_fileid(sdid,-1,-1,-1,check_pass_fileid_key);
ostringstream eherr;
eherr << "SDselect failed.";
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
int32 dim_sizes[H4_MAX_VAR_DIMS];
int32 sds_rank, data_type, n_attrs;
char name[H4_MAX_NC_NAME];
r = SDgetinfo (sdsid, name, &sds_rank, dim_sizes,
&data_type, &n_attrs);
if(r == FAIL) {
SDendaccess(sdsid);
HDFCFUtil::close_fileid(sdid,-1,-1,-1,check_pass_fileid_key);
ostringstream eherr;
eherr << "SDgetinfo failed.";
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
if(sds_rank != (rank+1)) {
SDendaccess(sdsid);
HDFCFUtil::close_fileid(sdid,-1,-1,-1,check_pass_fileid_key);
ostringstream eherr;
eherr << "The rank of string doesn't match with the rank of character array";
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
offset32[rank] = 0;
count32[rank] = dim_sizes[rank];
step32[rank] = 1;
int32 last_dim_size = dim_sizes[rank];
vector<char>val;
val.resize(nelms*count32[rank]);
r = SDreaddata (sdsid, &offset32[0], &step32[0], &count32[0], &val[0]);
if (r != 0) {
SDendaccess (sdsid);
HDFCFUtil::close_fileid(sdid,-1,-1,-1,check_pass_fileid_key);
ostringstream eherr;
eherr << "SDreaddata failed.";
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
vector<string>final_val;
final_val.resize(nelms);
vector<char> temp_buf;
temp_buf.resize(last_dim_size+1);
// Since the number of the dimension for a string is reduced by 1,
// the value of each string is the subset of the whole last dimension
// of the original array.
for (int i = 0; i<nelms;i++) {
strncpy(&temp_buf[0],&val[0]+last_dim_size*i,last_dim_size);
temp_buf[last_dim_size]='\0';
final_val[i] = &temp_buf[0];
}
set_value(&final_val[0],nelms);
SDendaccess(sdsid);
HDFCFUtil::close_fileid(sdid,-1,-1,-1,check_pass_fileid_key);
}
else {
int32 file_id = -1;
if(true == check_pass_fileid_key)
file_id = h4fd;
else {
// Open the file
file_id = Hopen (filename.c_str (), DFACC_READ, 0);
if (file_id < 0) {
ostringstream eherr;
eherr << "File " << filename.c_str () << " cannot be open.";
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
}
// Start the Vdata interface
if (Vstart (file_id) < 0) {
HDFCFUtil::close_fileid(-1,file_id,-1,-1,check_pass_fileid_key);
ostringstream eherr;
eherr << "This file cannot be open.";
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
// Attach the vdata
int32 vdata_id = VSattach (file_id, fieldref, "r");
if (vdata_id == -1) {
Vend (file_id);
HDFCFUtil::close_fileid(-1,file_id,-1,-1,check_pass_fileid_key);
ostringstream eherr;
eherr << "Vdata cannot be attached.";
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
// Seek the position of the starting point
if (VSseek (vdata_id, (int32) offset32[0]) == -1) {
VSdetach (vdata_id);
Vend (file_id);
HDFCFUtil::close_fileid(-1,file_id,-1,-1,check_pass_fileid_key);
ostringstream eherr;
eherr << "VSseek failed at " << offset32[0];
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
// Prepare the vdata field
if (VSsetfields (vdata_id, fieldname.c_str ()) == -1) {
VSdetach (vdata_id);
Vend (file_id);
HDFCFUtil::close_fileid(-1,file_id,-1,-1,check_pass_fileid_key);
ostringstream eherr;
eherr << "VSsetfields failed with the name " << fieldname;
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
int32 vdfelms = fieldorder * count32[0] * step32[0];
vector<char> val;
val.resize(vdfelms);
// Read the data
r = VSread (vdata_id, (uint8 *) &val[0], 1+(count32[0] -1)* step32[0],
FULL_INTERLACE);
if (r == -1) {
VSdetach (vdata_id);
Vend (file_id);
HDFCFUtil::close_fileid(-1,file_id,-1,-1,check_pass_fileid_key);
ostringstream eherr;
eherr << "VSread failed.";
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
vector<string>final_val;
final_val.resize(nelms);
vector<char> temp_buf;
temp_buf.resize(fieldorder+1);
for (int i = 0; i<nelms;i++) {
strncpy(&temp_buf[0],&val[0]+fieldorder*i*step32[0],fieldorder);
temp_buf[fieldorder]='\0';
final_val[i] = &temp_buf[0];
}
set_value(&final_val[0],nelms);
VSdetach(vdata_id);
Vend(file_id);
HDFCFUtil::close_fileid(-1,file_id,-1,-1,check_pass_fileid_key);
}
return false;
}
int
HDFCFStrField::format_constraint (int *offset, int *step, int *count)
{
long nels = 1;
int id = 0;
Dim_iter p = dim_begin ();
while (p != dim_end ()) {
int start = dimension_start (p, true);
int stride = dimension_stride (p, true);
int stop = dimension_stop (p, true);
// Check for illegal constraint
if (start > stop) {
ostringstream oss;
oss << "Array/Grid hyperslab start point "<< start <<
" is greater than stop point " << stop <<".";
throw Error(malformed_expr, oss.str());
}
offset[id] = start;
step[id] = stride;
count[id] = ((stop - start) / stride) + 1; // count of elements
nels *= count[id]; // total number of values for variable
BESDEBUG ("h4",
"=format_constraint():"
<< "id=" << id << " offset=" << offset[id]
<< " step=" << step[id]
<< " count=" << count[id]
<< endl);
id++;
p++;
}// while
return nels;
}
#if 0
while (p != dim_end ()) {
int start = dimension_start (p, true);
int stride = dimension_stride (p, true);
int stop = dimension_stop (p, true);
// Check for illegical constraint
if (stride < 0 || start < 0 || stop < 0 || start > stop) {
ostringstream oss;
oss << "Array/Grid hyperslab indices are bad: [" << start <<
":" << stride << ":" << stop << "]";
throw Error (malformed_expr, oss.str ());
}
// Check for an empty constraint and use the whole dimension if so.
if (start == 0 && stop == 0 && stride == 0) {
start = dimension_start (p, false);
stride = dimension_stride (p, false);
stop = dimension_stop (p, false);
}
offset[id] = start;
step[id] = stride;
count[id] = ((stop - start) / stride) + 1;// count of elements
nels *= count[id];// total number of values for variable
BESDEBUG ("h4",
"=format_constraint():"
<< "id=" << id << " offset=" << offset[id]
<< " step=" << step[id]
<< " count=" << count[id]
<< endl);
id++;
p++;
}
return nels;
}
#endif