-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHDFEOS2ArraySwathGeoField.cc
More file actions
370 lines (319 loc) · 12.9 KB
/
HDFEOS2ArraySwathGeoField.cc
File metadata and controls
370 lines (319 loc) · 12.9 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
/////////////////////////////////////////////////////////////////////////////
// Retrieves the latitude and longitude of the HDF-EOS2 Swath without using dimension maps
//
// Authors: MuQun Yang <myang6@hdfgroup.org>
// Copyright (c) 2010-2012 The HDF Group
/////////////////////////////////////////////////////////////////////////////
// For the swath without using dimension maps, for most cases, the retrieving of latitude and
// longitude is the same as retrieving other fields. Some MODIS latitude and longitude need
// to be arranged specially.
#ifdef USE_HDFEOS2_LIB
#include "config.h"
#include "HDFEOS2ArraySwathGeoField.h"
#include <iostream>
#include <sstream>
#include <cassert>
#include <libdap/debug.h>
#include <libdap/InternalErr.h>
#include "BESDebug.h"
#include "HDF4RequestHandler.h"
using namespace std;
using namespace libdap;
#define SIGNED_BYTE_TO_INT32 1
bool
HDFEOS2ArraySwathGeoField::read ()
{
BESDEBUG("h4","Coming to HDFEOS2ArraySwathGeoField 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();
// 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);
int32 (*attachfunc) (int32, char *);
intn (*detachfunc) (int32);
intn (*fieldinfofunc) (int32, char *, int32 *, int32 *, int32 *, char *);
intn (*readfieldfunc) (int32, char *, int32 *, int32 *, int32 *, void *);
// Define function pointers to handle the swath
string datasetname;
openfunc = SWopen;
attachfunc = SWattach;
detachfunc = SWdetach;
fieldinfofunc = SWfieldinfo;
readfieldfunc = SWreadfield;
datasetname = swathname;
// 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 sfid = -1;
int32 swathid = -1;
if(false == check_pass_fileid_key) {
sfid = openfunc (const_cast < char *>(filename.c_str ()), DFACC_READ);
if (sfid < 0) {
ostringstream eherr;
eherr << "File " << filename.c_str () << " cannot be open.";
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
}
else
sfid = swathfd;
swathid = attachfunc (sfid, const_cast < char *>(datasetname.c_str ()));
if (swathid < 0) {
HDFCFUtil::close_fileid(-1,-1,-1,sfid,check_pass_fileid_key);
ostringstream eherr;
eherr << "Swath " << datasetname.c_str () << " cannot be attached.";
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
int32 tmp_rank = -1;
vector<int32>tmp_dims;
tmp_dims.resize(rank);
char tmp_dimlist[1024];
int32 type =-1;
intn r = -1;
r = fieldinfofunc (swathid, const_cast < char *>(fieldname.c_str ()),
&tmp_rank, &tmp_dims[0], &type, tmp_dimlist);
if (r != 0) {
detachfunc (swathid);
HDFCFUtil::close_fileid(-1,-1,-1,sfid,check_pass_fileid_key);
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);
HDFCFUtil::close_fileid(-1,-1,-1,sfid,check_pass_fileid_key);
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<uint8>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);
HDFCFUtil::close_fileid(-1,-1,-1,sfid,check_pass_fileid_key);
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);
HDFCFUtil::close_fileid(-1,-1,-1,sfid,check_pass_fileid_key);
ostringstream eherr;
eherr << "field " << fieldname.c_str () << "cannot be read.";
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
// We found a special MODIS file that used int16 to store lat/lon. The scale-factor is 0.01.
// We cannot retrieve attributes from EOS. To make this work, we have to manually check the values of lat/lon;
// If lat/lon value is in thousands, we will hard code by applying the scale factor to calculate the latitude and longitude.
// We will see and may find a good solution in the future. KY 2010-7-12
bool needadjust = false;
if ((val[0] < -1000) || (val[0] > 1000))
needadjust = true;
if (!needadjust)
if ((val[nelms / 2] < -1000)
|| (val[nelms / 2] > 1000))
needadjust = true;
if (!needadjust)
if ((val[nelms - 1] < -1000)
|| (val[nelms - 1] > 1000))
needadjust = true;
if (true == needadjust) {
vector<float32>newval;
newval.resize(nelms);
float scale_factor = 0.01;
for (int i = 0; i < nelms; i++)
newval[i] = scale_factor * (float32) val[i];
set_value ((dods_float32 *) &newval[0], nelms);
}
else {
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);
HDFCFUtil::close_fileid(-1,-1,-1,sfid,check_pass_fileid_key);
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);
HDFCFUtil::close_fileid(-1,-1,-1,sfid,check_pass_fileid_key);
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);
HDFCFUtil::close_fileid(-1,-1,-1,sfid,check_pass_fileid_key);
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);
HDFCFUtil::close_fileid(-1,-1,-1,sfid,check_pass_fileid_key);
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);
HDFCFUtil::close_fileid(-1,-1,-1,sfid,check_pass_fileid_key);
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);
HDFCFUtil::close_fileid(-1,-1,-1,sfid,check_pass_fileid_key);
throw InternalErr (__FILE__, __LINE__, "unsupported data type.");
}
r = detachfunc (swathid);
if (r != 0) {
HDFCFUtil::close_fileid(-1,-1,-1,sfid,check_pass_fileid_key);
ostringstream eherr;
eherr << "Swath " << datasetname.c_str () << " cannot be detached.";
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
HDFCFUtil::close_fileid(-1,-1,-1,sfid,check_pass_fileid_key);
#if 0
r = closefunc (sfid);
if (r != 0) {
ostringstream eherr;
eherr << "Swath " << filename.c_str () << " cannot be closed.";
throw InternalErr (__FILE__, __LINE__, eherr.str ());
}
#endif
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
HDFEOS2ArraySwathGeoField::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;
}
#endif