-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHDFSPArray_VDField.cc
More file actions
444 lines (364 loc) · 14.2 KB
/
HDFSPArray_VDField.cc
File metadata and controls
444 lines (364 loc) · 14.2 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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/////////////////////////////////////////////////////////////////////////////
// This file is part of the hdf4 data handler for the OPeNDAP data server.
// It retrieves the Vdata fields from NASA HDF4 data products.
// Each Vdata will be decomposed into individual Vdata fields.
// Each field will be mapped to A DAP variable.
// Authors: MuQun Yang <myang6@hdfgroup.org>
// Copyright (c) 2010-2012 The HDF Group
/////////////////////////////////////////////////////////////////////////////
#include "HDFSPArray_VDField.h"
#include <iostream>
#include <sstream>
#include <cassert>
#include <libdap/debug.h>
#include "hdf.h"
#include "mfhdf.h"
#include <libdap/InternalErr.h>
#include <BESDebug.h>
#include "HDFCFUtil.h"
#include "HDF4RequestHandler.h"
using namespace std;
using namespace libdap;
#define SIGNED_BYTE_TO_INT32 1
bool
HDFSPArray_VDField::read ()
{
BESDEBUG("h4","Coming to HDFSPArray_VDField 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();
// Declaration of offset,count and step
vector<int>offset;
offset.resize(rank);
vector<int>count;
count.resize(rank);
vector<int>step;
step.resize(rank);
// Obtain offset,step and count from the client expression constraint
int nelms = format_constraint(&offset[0],&step[0],&count[0]);
int32 file_id = -1;
if(true == check_pass_fileid_key)
file_id = fileid;
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
int32 vdata_id = 0;
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
vdata_id = VSattach (file_id, vdref, "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 ());
}
try {
int32 r = -1;
// Seek the position of the starting point
if (VSseek (vdata_id, (int32) offset[0]) == -1) {
ostringstream eherr;
eherr << "VSseek failed at " << offset[0];
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
// Prepare the vdata field
if (VSsetfields (vdata_id, fdname.c_str ()) == -1) {
ostringstream eherr;
eherr << "VSsetfields failed with the name " << fdname;
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
int32 vdfelms = fdorder * count[0] * step[0];
// Loop through each data type
switch (dtype) {
case DFNT_INT8:
{
vector<int8> val;
val.resize(nelms);
vector<int8>orival;
orival.resize(vdfelms);
// Read the data
r = VSread (vdata_id, (uint8 *) &orival[0], 1+(count[0] -1)* step[0],
FULL_INTERLACE);
if (r == -1) {
ostringstream eherr;
eherr << "VSread failed.";
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
// Obtain the subset portion of the data
if (fdorder > 1) {
for (int i = 0; i < count[0]; i++)
for (int j = 0; j < count[1]; j++)
val[i * count[1] + j] =
orival[i * fdorder * step[0] + offset[1] + j * step[1]];
}
else {
for (int i = 0; i < count[0]; i++)
val[i] = orival[i * step[0]];
}
#ifndef SIGNED_BYTE_TO_INT32
set_value ((dods_byte *) &val[0], nelms);
#else
vector<int32>newval;
newval.resize(nelms);
for (int counter = 0; counter < nelms; counter++)
newval[counter] = (int32) (val[counter]);
set_value ((dods_int32 *) &newval[0], nelms);
#endif
}
break;
case DFNT_UINT8:
case DFNT_UCHAR8:
{
vector<uint8>val;
val.resize(nelms);
vector<uint8>orival;
orival.resize(vdfelms);
r = VSread (vdata_id, &orival[0], 1+(count[0] -1)* step[0], FULL_INTERLACE);
if (r == -1) {
ostringstream eherr;
eherr << "VSread failed.";
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
if (fdorder > 1) {
for (int i = 0; i < count[0]; i++)
for (int j = 0; j < count[1]; j++)
val[i * count[1] + j] = orival[i * fdorder * step[0] + offset[1] + j * step[1]];
}
else {
for (int i = 0; i < count[0]; i++)
val[i] = orival[i * step[0]];
}
set_value ((dods_byte *) &val[0], nelms);
}
break;
case DFNT_INT16:
{
vector<int16>val;
val.resize(nelms);
vector<int16>orival;
orival.resize(vdfelms);
r = VSread (vdata_id, (uint8 *) &orival[0], 1+(count[0] -1)* step[0],
FULL_INTERLACE);
if (r == -1) {
ostringstream eherr;
eherr << "VSread failed.";
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
if (fdorder > 1) {
for (int i = 0; i < count[0]; i++)
for (int j = 0; j < count[1]; j++)
val[i * count[1] + j] = orival[i * fdorder * step[0] + offset[1] + j * step[1]];
}
else {
for (int i = 0; i < count[0]; i++)
val[i] = orival[i * step[0]];
}
set_value ((dods_int16 *) &val[0], nelms);
}
break;
case DFNT_UINT16:
{
vector<uint16>val;
val.resize(nelms);
vector<uint16>orival;
orival.resize(vdfelms);
r = VSread (vdata_id, (uint8 *) &orival[0], 1+(count[0] -1)* step[0],
FULL_INTERLACE);
if (r == -1) {
ostringstream eherr;
eherr << "VSread failed.";
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
if (fdorder > 1) {
for (int i = 0; i < count[0]; i++)
for (int j = 0; j < count[1]; j++)
val[i * count[1] + j] = orival[i * fdorder * step[0] + offset[1] + j * step[1]];
}
else {
for (int i = 0; i < count[0]; i++)
val[i] = orival[i * step[0]];
}
set_value ((dods_uint16 *) &val[0], nelms);
}
break;
case DFNT_INT32:
{
vector<int32>val;
val.resize(nelms);
vector<int32>orival;
orival.resize(vdfelms);
r = VSread (vdata_id, (uint8 *) &orival[0], 1+(count[0] -1)* step[0],
FULL_INTERLACE);
if (r == -1) {
ostringstream eherr;
eherr << "VSread failed.";
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
if (fdorder > 1) {
for (int i = 0; i < count[0]; i++)
for (int j = 0; j < count[1]; j++)
val[i * count[1] + j] = orival[i * fdorder * step[0] + offset[1] + j * step[1]];
}
else {
for (int i = 0; i < count[0]; i++)
val[i] = orival[i * step[0]];
}
set_value ((dods_int32 *) &val[0], nelms);
}
break;
case DFNT_UINT32:
{
vector<uint32>val;
val.resize(nelms);
vector<uint32>orival;
orival.resize(vdfelms);
r = VSread (vdata_id, (uint8 *) &orival[0], 1+(count[0] -1)* step[0],
FULL_INTERLACE);
if (r == -1) {
ostringstream eherr;
eherr << "VSread failed.";
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
if (fdorder > 1) {
for (int i = 0; i < count[0]; i++)
for (int j = 0; j < count[1]; j++)
val[i * count[1] + j] = orival[i * fdorder * step[0] + offset[1] + j * step[1]];
}
else {
for (int i = 0; i < count[0]; i++)
val[i] = orival[i * step[0]];
}
set_value ((dods_uint32 *) &val[0], nelms);
}
break;
case DFNT_FLOAT32:
{
vector<float32>val;
val.resize(nelms);
vector<float32>orival;
orival.resize(vdfelms);
r = VSread (vdata_id, (uint8 *) &orival[0], 1+(count[0] -1)* step[0],
FULL_INTERLACE);
if (r == -1) {
ostringstream eherr;
eherr << "VSread failed.";
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
if (fdorder > 1) {
for (int i = 0; i < count[0]; i++)
for (int j = 0; j < count[1]; j++)
val[i * count[1] + j] = orival[i * fdorder * step[0] + offset[1] + j * step[1]];
}
else {
for (int i = 0; i < count[0]; i++)
val[i] = orival[i * step[0]];
}
set_value ((dods_float32 *) &val[0], nelms);
}
break;
case DFNT_FLOAT64:
{
vector<float64>val;
val.resize(nelms);
vector<float64>orival;
orival.resize(vdfelms);
r = VSread (vdata_id, (uint8 *) &orival[0], 1+(count[0] -1)* step[0],
FULL_INTERLACE);
if (r == -1) {
ostringstream eherr;
eherr << "VSread failed.";
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
if (fdorder > 1) {
for (int i = 0; i < count[0]; i++)
for (int j = 0; j < count[1]; j++)
val[i * count[1] + j] = orival[i * fdorder * step[0] + offset[1] + j * step[1]];
}
else {
for (int i = 0; i < count[0]; i++)
val[i] = orival[i * step[0]];
}
set_value ((dods_float64 *) &val[0], nelms);
}
break;
default:
throw InternalErr (__FILE__, __LINE__, "unsupported data type.");
}
if (VSdetach (vdata_id) == -1) {
ostringstream eherr;
eherr << "VSdetach failed.";
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
if (Vend (file_id) == -1) {
ostringstream eherr;
eherr << "VSdetach failed.";
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
HDFCFUtil::close_fileid(-1,file_id,-1,-1,check_pass_fileid_key);
}
catch(...) {
VSdetach(vdata_id);
Vend(file_id);
HDFCFUtil::close_fileid(-1,fileid,-1,-1,check_pass_fileid_key);
throw;
}
#if 0
if (Hclose (file_id) == -1) {
ostringstream eherr;
eherr << "VSdetach failed.";
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
#endif
return true;
}
// Standard way of DAP handlers to pass the coordinates of the subsetted region to the handlers
// Return the number of elements to read.
int
HDFSPArray_VDField::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 (p != dim_end ())
return nels;
}