-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHDFEOS2ArraySwathGeoDimMapExtraField.cc
More file actions
376 lines (324 loc) · 12.3 KB
/
HDFEOS2ArraySwathGeoDimMapExtraField.cc
File metadata and controls
376 lines (324 loc) · 12.3 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
/////////////////////////////////////////////////////////////////////////////
// Retrieves the latitude and longitude of the HDF-EOS2 Swath having dimension maps
// Authors: MuQun Yang <myang6@hdfgroup.org>
// Copyright (c) 2010-2012 The HDF Group
/////////////////////////////////////////////////////////////////////////////
// SOME MODIS products provide the latitude and longitude files for
// swaths using dimension map. The files are still HDF-EOS2 files.
// The file name is determined at the hdfdesc.cc.
// Since the latitude and longitude fields are stored as
// the real data fields in an HDF-EOS2 file,
// The read function is essentially the same as retrieving the data value of a
// general HDF-EOS2 field.
#ifdef USE_HDFEOS2_LIB
#include "config.h"
#include "HDFEOS2ArraySwathGeoDimMapExtraField.h"
#include <iostream>
#include <sstream>
#include <cassert>
#include <libdap/debug.h>
#include "HDFEOS2.h"
#include "HDFCFUtil.h"
#include <libdap/InternalErr.h>
#include "BESDebug.h"
using namespace std;
using namespace libdap;
#define SIGNED_BYTE_TO_INT32 1
bool
HDFEOS2ArraySwathGeoDimMapExtraField::read ()
{
BESDEBUG("h4","Coming to HDFEOS2ArraySwathGeoDimMapExtraField read "<<endl);
if(length() == 0)
return true;
// Declare 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]);
// Just declare offset,count and step in the int32 type.
vector<int32>offset32;
offset32.resize(rank);
vector<int32>count32;
count32.resize(rank);
vector<int32>step32;
step32.resize(rank);
// Just obtain the offset,count and step in the datatype of int32.
for (int i = 0; i < rank; i++) {
offset32[i] = (int32) offset[i];
count32[i] = (int32) count[i];
step32[i] = (int32) step[i];
}
int32 (*openfunc) (char *, intn);
intn (*closefunc) (int32);
int32 (*attachfunc) (int32, char *);
intn (*detachfunc) (int32);
intn (*fieldinfofunc) (int32, char *, int32 *, int32 *, int32 *, char *);
intn (*readfieldfunc) (int32, char *, int32 *, int32 *, int32 *, void *);
int32 (*inqfunc) (char *, char *, int32 *);
// Define function pointers to handle the swath
openfunc = SWopen;
closefunc = SWclose;
attachfunc = SWattach;
detachfunc = SWdetach;
fieldinfofunc = SWfieldinfo;
readfieldfunc = SWreadfield;
inqfunc = SWinqswath;
// We may eventually combine the following code with other code, so
// we don't add many comments from here to the end of the file.
// The jira ticket about combining code is HFRHANDLER-166.
int32 fileid = -1;
int32 swathid = -1;
fileid = openfunc (const_cast < char *>(filename.c_str ()), DFACC_READ);
if (fileid < 0) {
ostringstream eherr;
eherr << "File " << filename.c_str () << " cannot be open.";
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
// Check if this file only contains one swath
int numswath = 0;
int32 swathnamesize = 0;
numswath = inqfunc (const_cast < char *>(filename.c_str ()), NULL,
&swathnamesize);
if (numswath == -1) {
closefunc (fileid);
ostringstream eherr;
eherr << "File " << filename.c_str () << " cannot obtain the swath list.";
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
if (numswath != 1) {
closefunc (fileid);
ostringstream eherr;
eherr << " Currently we only support reading geo-location fields from one swath."
<< " This file has more than one swath. ";
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
char *swathname = new char[swathnamesize + 1];
numswath = inqfunc (const_cast < char *>(filename.c_str ()), swathname,
&swathnamesize);
if (numswath == -1) {
delete[]swathname;
closefunc (fileid);
ostringstream eherr;
eherr << "File " << filename.c_str () << " cannot obtain the swath list.";
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
swathid = attachfunc (fileid, swathname);
if (swathid < 0) {
closefunc (fileid);
ostringstream eherr;
eherr << "Grid/Swath " << swathname << " cannot be attached.";
delete[]swathname;
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
delete[]swathname;
int32 tmp_rank = 0;
int32 tmp_dims[rank];
char tmp_dimlist[1024];
int32 type = 0;
intn r = -1;
r = fieldinfofunc (swathid, const_cast < char *>(fieldname.c_str ()),
&tmp_rank, tmp_dims, &type, tmp_dimlist);
if (r != 0) {
detachfunc (swathid);
closefunc (fileid);
ostringstream eherr;
eherr << "Field " << fieldname.c_str () << " information cannot be obtained.";
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
switch (type) {
case DFNT_INT8:
{
vector<int8>val;
val.resize(nelms);
r = readfieldfunc (swathid, const_cast < char *>(fieldname.c_str ()),
&offset32[0], &step32[0], &count32[0], &val[0]);
if (r != 0) {
detachfunc (swathid);
closefunc (fileid);
ostringstream eherr;
eherr << "field " << fieldname.c_str () << "cannot be read.";
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
#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<uint>val;
val.resize(nelms);
r = readfieldfunc (swathid, const_cast < char *>(fieldname.c_str ()),
&offset32[0], &step32[0], &count32[0], &val[0]);
if (r != 0) {
detachfunc (swathid);
closefunc (fileid);
ostringstream eherr;
eherr << "field " << fieldname.c_str () << "cannot be read.";
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
set_value ((dods_byte *) &val[0], nelms);
}
break;
case DFNT_INT16:
{
vector<int16>val;
val.resize(nelms);
r = readfieldfunc (swathid, const_cast < char *>(fieldname.c_str ()),
&offset32[0], &step32[0], &count32[0], &val[0]);
if (r != 0) {
detachfunc (swathid);
closefunc (fileid);
ostringstream eherr;
eherr << "field " << fieldname.c_str () << "cannot be read.";
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
set_value ((dods_int16 *) &val[0], nelms);
}
break;
case DFNT_UINT16:
{
vector<uint16>val;
val.resize(nelms);
r = readfieldfunc (swathid, const_cast < char *>(fieldname.c_str ()),
&offset32[0], &step32[0], &count32[0], &val[0]);
if (r != 0) {
detachfunc (swathid);
closefunc (fileid);
ostringstream eherr;
eherr << "field " << fieldname.c_str () << "cannot be read.";
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
set_value ((dods_uint16 *) &val[0], nelms);
}
break;
case DFNT_INT32:
{
vector<int32>val;
val.resize(nelms);
r = readfieldfunc (swathid, const_cast < char *>(fieldname.c_str ()),
&offset32[0], &step32[0], &count32[0], &val[0]);
if (r != 0) {
detachfunc (swathid);
closefunc (fileid);
ostringstream eherr;
eherr << "field " << fieldname.c_str () << "cannot be read.";
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
set_value ((dods_int32 *) &val[0], nelms);
}
break;
case DFNT_UINT32:
{
vector<uint32>val;
val.resize(nelms);
r = readfieldfunc (swathid, const_cast < char *>(fieldname.c_str ()),
&offset32[0], &step32[0], &count32[0], &val[0]);
if (r != 0) {
detachfunc (swathid);
closefunc (fileid);
ostringstream eherr;
eherr << "field " << fieldname.c_str () << "cannot be read.";
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
set_value ((dods_uint32 *) &val[0], nelms);
}
break;
case DFNT_FLOAT32:
{
vector<float32>val;
val.resize(nelms);
r = readfieldfunc (swathid, const_cast < char *>(fieldname.c_str ()),
&offset32[0], &step32[0], &count32[0], &val[0]);
if (r != 0) {
detachfunc (swathid);
closefunc (fileid);
ostringstream eherr;
eherr << "field " << fieldname.c_str () << "cannot be read.";
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
set_value ((dods_float32 *) &val[0], nelms);
}
break;
case DFNT_FLOAT64:
{
vector<float64>val;
val.resize(nelms);
r = readfieldfunc (swathid, const_cast < char *>(fieldname.c_str ()),
&offset32[0], &step32[0], &count32[0], &val[0]);
if (r != 0) {
detachfunc (swathid);
closefunc (fileid);
ostringstream eherr;
eherr << "field " << fieldname.c_str () << "cannot be read.";
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
set_value ((dods_float64 *) &val[0], nelms);
}
break;
default:
{
detachfunc(swathid);
closefunc(fileid);
throw InternalErr (__FILE__, __LINE__, "unsupported data type.");
}
}
r = detachfunc (swathid);
if (r != 0) {
closefunc (fileid);
throw InternalErr (__FILE__, __LINE__, "The swath cannot be detached.");
}
r = closefunc (fileid);
if (r != 0) {
ostringstream eherr;
eherr << "Grid/Swath " << filename.c_str () << " cannot be closed.";
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
return false;
}
// Standard way of DAP handlers to pass the coordinates of the subsetted region to the handlers
// Return the number of elements to read.
int
HDFEOS2ArraySwathGeoDimMapExtraField::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;
}
#endif