11 #ifdef USE_HDFEOS2_LIB
13 #include "HDFEOS2ArraySwathGeoField.h"
18 #include "InternalErr.h"
20 #include "HDF4RequestHandler.h"
24 #define SIGNED_BYTE_TO_INT32 1
27 HDFEOS2ArraySwathGeoField::read ()
30 BESDEBUG(
"h4",
"Coming to HDFEOS2ArraySwathGeoField read "<<endl);
35 string check_pass_fileid_key_str=
"H4.EnablePassFileID";
36 bool check_pass_fileid_key =
false;
37 check_pass_fileid_key = HDFCFUtil::check_beskeys(check_pass_fileid_key_str);
40 bool check_pass_fileid_key = HDF4RequestHandler::get_pass_fileid();
51 int nelms = format_constraint (&offset[0], &step[0], &count[0]);
54 vector<int32>offset32;
55 offset32.resize(rank);
62 for (
int i = 0; i < rank; i++) {
63 offset32[i] = (int32) offset[i];
64 count32[i] = (int32) count[i];
65 step32[i] = (int32) step[i];
68 int32 (*openfunc) (
char *, intn);
69 int32 (*attachfunc) (int32,
char *);
70 intn (*detachfunc) (int32);
71 intn (*fieldinfofunc) (int32,
char *, int32 *, int32 *, int32 *,
char *);
72 intn (*readfieldfunc) (int32,
char *, int32 *, int32 *, int32 *,
void *);
77 attachfunc = SWattach;
78 detachfunc = SWdetach;
79 fieldinfofunc = SWfieldinfo;
80 readfieldfunc = SWreadfield;
81 datasetname = swathname;
89 if(
false == check_pass_fileid_key) {
90 sfid = openfunc (
const_cast < char *
>(filename.c_str ()), DFACC_READ);
93 eherr <<
"File " << filename.c_str () <<
" cannot be open.";
94 throw InternalErr (__FILE__, __LINE__, eherr.str ());
100 swathid = attachfunc (sfid,
const_cast < char *
>(datasetname.c_str ()));
105 eherr <<
"Swath " << datasetname.c_str () <<
" cannot be attached.";
106 throw InternalErr (__FILE__, __LINE__, eherr.str ());
111 vector<int32>tmp_dims;
112 tmp_dims.resize(rank);
113 char tmp_dimlist[1024];
116 r = fieldinfofunc (swathid,
const_cast < char *
>(fieldname.c_str ()),
117 &tmp_rank, &tmp_dims[0], &type, tmp_dimlist);
119 detachfunc (swathid);
122 eherr <<
"Field " << fieldname.c_str () <<
" information cannot be obtained.";
123 throw InternalErr (__FILE__, __LINE__, eherr.str ());
132 r = readfieldfunc (swathid,
const_cast < char *
>(fieldname.c_str ()), &offset32[0], &step32[0], &count32[0], &val[0]);
134 detachfunc (swathid);
137 eherr <<
"field " << fieldname.c_str () <<
"cannot be read.";
138 throw InternalErr (__FILE__, __LINE__, eherr.str ());
143 #ifndef SIGNED_BYTE_TO_INT32
144 set_value ((dods_byte *) &val[0], nelms);
147 newval.resize(nelms);
149 for (
int counter = 0; counter < nelms; counter++)
150 newval[counter] = (int32) (val[counter]);
151 set_value ((dods_int32 *) &newval[0], nelms);
160 r = readfieldfunc (swathid,
const_cast < char *
>(fieldname.c_str ()), &offset32[0], &step32[0], &count32[0], &val[0]);
162 detachfunc (swathid);
165 eherr <<
"field " << fieldname.c_str () <<
"cannot be read.";
166 throw InternalErr (__FILE__, __LINE__, eherr.str ());
169 set_value ((dods_byte *) &val[0], nelms);
177 r = readfieldfunc (swathid,
const_cast < char *
>(fieldname.c_str ()), &offset32[0], &step32[0], &count32[0], &val[0]);
179 detachfunc (swathid);
182 eherr <<
"field " << fieldname.c_str () <<
"cannot be read.";
183 throw InternalErr (__FILE__, __LINE__, eherr.str ());
189 bool needadjust =
false;
191 if ((val[0] < -1000) || (val[0] > 1000))
194 if ((val[nelms / 2] < -1000)
195 || (val[nelms / 2] > 1000))
198 if ((val[nelms - 1] < -1000)
199 || (val[nelms - 1] > 1000))
201 if (
true == needadjust) {
202 vector<float32>newval;
203 newval.resize(nelms);
204 float scale_factor = 0.01;
206 for (
int i = 0; i < nelms; i++)
207 newval[i] = scale_factor * (float32) val[i];
208 set_value ((dods_float32 *) &newval[0], nelms);
212 set_value ((dods_int16 *) &val[0], nelms);
221 r = readfieldfunc (swathid,
const_cast < char *
>(fieldname.c_str ()), &offset32[0], &step32[0], &count32[0], &val[0]);
223 detachfunc (swathid);
226 eherr <<
"field " << fieldname.c_str () <<
"cannot be read.";
227 throw InternalErr (__FILE__, __LINE__, eherr.str ());
230 set_value ((dods_uint16 *) &val[0], nelms);
238 r = readfieldfunc (swathid,
const_cast < char *
>(fieldname.c_str ()), &offset32[0], &step32[0], &count32[0], &val[0]);
240 detachfunc (swathid);
243 eherr <<
"field " << fieldname.c_str () <<
"cannot be read.";
244 throw InternalErr (__FILE__, __LINE__, eherr.str ());
247 set_value ((dods_int32 *) &val[0], nelms);
255 r = readfieldfunc (swathid,
const_cast < char *
>(fieldname.c_str ()), &offset32[0], &step32[0], &count32[0], &val[0]);
257 detachfunc (swathid);
260 eherr <<
"field " << fieldname.c_str () <<
"cannot be read.";
261 throw InternalErr (__FILE__, __LINE__, eherr.str ());
264 set_value ((dods_uint32 *) &val[0], nelms);
272 r = readfieldfunc (swathid,
const_cast < char *
>(fieldname.c_str ()), &offset32[0], &step32[0], &count32[0], &val[0]);
274 detachfunc (swathid);
277 eherr <<
"field " << fieldname.c_str () <<
"cannot be read.";
278 throw InternalErr (__FILE__, __LINE__, eherr.str ());
280 set_value ((dods_float32 *) &val[0], nelms);
287 r = readfieldfunc (swathid,
const_cast < char *
>(fieldname.c_str ()), &offset32[0], &step32[0], &count32[0], &val[0]);
289 detachfunc (swathid);
292 eherr <<
"field " << fieldname.c_str () <<
"cannot be read.";
293 throw InternalErr (__FILE__, __LINE__, eherr.str ());
296 set_value ((dods_float64 *) &val[0], nelms);
300 detachfunc (swathid);
302 throw InternalErr (__FILE__, __LINE__,
"unsupported data type.");
305 r = detachfunc (swathid);
309 eherr <<
"Swath " << datasetname.c_str () <<
" cannot be detached.";
310 throw InternalErr (__FILE__, __LINE__, eherr.str ());
316 r = closefunc (sfid);
319 eherr <<
"Swath " << filename.c_str () <<
" cannot be closed.";
320 throw InternalErr (__FILE__, __LINE__, eherr.str ());
330 HDFEOS2ArraySwathGeoField::format_constraint (
int *offset,
int *step,
int *count)
335 Dim_iter p = dim_begin ();
336 while (p != dim_end ()) {
338 int start = dimension_start (p,
true);
339 int stride = dimension_stride (p,
true);
340 int stop = dimension_stop (p,
true);
345 oss <<
"Array/Grid hyperslab start point "<< start <<
346 " is greater than stop point " << stop <<
".";
347 throw Error(malformed_expr, oss.str());
352 count[id] = ((stop - start) / stride) + 1;
356 "=format_constraint():"
357 <<
"id=" <<
id <<
" offset=" << offset[
id]
358 <<
" step=" << step[
id]
359 <<
" count=" << count[
id]