44 #include "config_hdf.h"
67 inline int min(
int t1,
int t2)
69 return (t1 < t2 ? t1 : t2);
73 const string hdfistream_sds::long_name =
"long_name";
74 const string hdfistream_sds::units =
"units";
75 const string hdfistream_sds::format =
"format";
82 void hdfistream_sds::_init(
void)
84 _sds_id = _attr_index = _dim_index = _nsds = _rank = _nattrs =
87 _meta = _slab.set =
false;
93 void hdfistream_sds::_get_fileinfo(
void)
95 if (SDfileinfo(_file_id, &_nsds, &_nfattrs) < 0)
101 void hdfistream_sds::_get_sdsinfo(
void)
103 char junk0[hdfclass::MAXSTR];
104 int32 junk1[hdfclass::MAXDIMS];
108 if (SDgetinfo(_sds_id, junk0, &_rank, junk1, &junk2, &_nattrs) < 0)
111 if (_rank > hdfclass::MAXDIMS)
117 void hdfistream_sds::_close_sds(
void)
120 (void) SDendaccess(_sds_id);
121 _sds_id = _attr_index = _dim_index = _rank = _nattrs = 0;
128 void hdfistream_sds::_seek_next_arr(
void)
131 BESDEBUG(
"h4",
"hdfistream_sds::_seek_next_arr called with an open sds: "
133 SDendaccess(_sds_id);
137 for (_index++, _dim_index = _attr_index = 0; _index < _nsds; ++_index) {
139 BESDEBUG(
"h4",
"hdfistream_sds::_seek_next_arr inside for-loop with an open sds: "
142 if ((_sds_id = SDselect(_file_id, _index)) < 0)
144 if (!SDiscoordvar(_sds_id))
146 SDendaccess(_sds_id);
152 void hdfistream_sds::_seek_arr(
int arr_index)
155 for (_rewind(); _index < _nsds && arr_count <= arr_index;
156 _seek_next_arr(), arr_count++);
160 void hdfistream_sds::_seek_arr(
const string & name)
163 BESDEBUG(
"h4",
"hdfistream_sds::_seek_arr called with an open sds: "
169 const char *nm = name.c_str();
170 if ((index = SDnametoindex(_file_id, (
char *) nm)) < 0)
172 if ((_sds_id = SDselect(_file_id, index)) < 0)
174 bool iscoord = SDiscoordvar(_sds_id);
176 SDendaccess(_sds_id);
185 void hdfistream_sds::_seek_arr_ref(
int ref)
188 BESDEBUG(
"h4",
"hdfistream_sds::_seek_arr_ref called with an open sds: "
194 if ((index = SDreftoindex(_file_id, ref)) < 0)
196 if ((_sds_id = SDselect(_file_id, index)) < 0)
198 bool iscoord = SDiscoordvar(_sds_id);
200 SDendaccess(_sds_id);
214 hdfistream_sds::hdfistream_sds(
const string filename):
218 if (_filename.length() != 0)
219 open(_filename.c_str());
224 bool hdfistream_sds::eos(
void)
const
226 if (_filename.length() == 0)
234 return (_index >= _nsds);
239 bool hdfistream_sds::bos(
void)
const
241 if (_filename.length() == 0)
253 bool hdfistream_sds::eo_attr(
void)
const
255 if (_filename.length() == 0)
261 return (_attr_index >= _nfattrs);
263 return (_attr_index >= _nattrs);
269 bool hdfistream_sds::eo_dim(
void)
const
271 if (_filename.length() == 0)
279 return (_dim_index >= _rank);
284 void hdfistream_sds::open(
const char *filename)
288 BESDEBUG(
"h4",
"sds opening file " << filename << endl);
291 if ((_file_id = SDstart((
char *) filename, DFACC_READ)) < 0)
296 BESDEBUG(
"h4",
"sds file opened: id=" << _file_id << endl);
298 _filename = filename;
305 void hdfistream_sds::close(
void)
307 BESDEBUG(
"h4",
"sds file closed: id=" << _file_id <<
", this: " <<
this<< endl);
311 (void) SDend(_file_id);
312 _file_id = _nsds = _nfattrs = 0;
317 void hdfistream_sds::seek(
int index)
319 if (_filename.length() == 0)
323 if (!eos() && !bos())
328 void hdfistream_sds::seek(
const char *name)
330 if (_filename.length() == 0)
333 _seek_arr(
string(name));
334 if (!eos() && !bos())
339 void hdfistream_sds::rewind(
void)
341 if (_filename.length() == 0)
348 void hdfistream_sds::seek_next(
void)
350 if (_filename.length() == 0)
358 void hdfistream_sds::seek_ref(
int ref)
360 if (_filename.length() == 0)
364 if (!eos() && !bos())
369 void hdfistream_sds::setslab(vector < int >start, vector < int >edge,
370 vector < int >stride,
bool reduce_rank)
373 if (start.size() != edge.size() || edge.size() != stride.size()
374 || start.size() == 0)
378 for (i = 0; i < (int) start.size() && i < hdfclass::MAXDIMS; ++i) {
385 _slab.start[i] = start[i];
386 _slab.edge[i] = edge[i];
387 _slab.stride[i] = stride[i];
390 _slab.reduce_rank = reduce_rank;
400 hs.dims = vector < hdf_dim > ();
401 hs.attrs = vector < hdf_attr > ();
405 if (_filename.length() == 0)
413 char name[hdfclass::MAXSTR];
415 int32 dim_sizes[hdfclass::MAXDIMS];
418 if (SDgetinfo(_sds_id, name, &rank, dim_sizes, &number_type, &nattrs) <
423 hs.ref = SDidtoref(_sds_id);
431 hs.data.import(number_type);
434 for (
int i = 0; i < rank; ++i)
435 nelts *= _slab.edge[i];
438 int datasize = nelts * DFKNTsize(number_type);
439 data =
new char[datasize];
442 BESDEBUG(
"h4",
"SDreaddata() on line 387. _sds_id: " << _sds_id
444 if (SDreaddata(_sds_id, _slab.start, _slab.stride, _slab.edge,
452 int32 zero[hdfclass::MAXDIMS];
453 for (
int i = 0; i < rank && i < hdfclass::MAXDIMS; ++i) {
455 nelts *= dim_sizes[i];
459 int datasize = nelts * DFKNTsize(number_type);
460 data =
new char[datasize];
465 if (SDreaddata(_sds_id, zero, 0, dim_sizes, data) < 0) {
471 hs.data.import(number_type, data, nelts);
481 class ce_name_match:
public std::unary_function < array_ce, bool > {
484 ce_name_match(
const string & n):name(n) {
485 }
bool operator() (
const array_ce & a_ce) {
486 return name == a_ce.name;
495 hd.name = hd.label = hd.unit = hd.format = string();
498 hd.attrs = vector < hdf_attr > ();
500 if (_filename.length() == 0)
515 while (_slab.set && _slab.reduce_rank && !eo_dim() &&
516 _slab.edge[_dim_index] == 1)
525 if ((dim_id = SDgetdimid(_sds_id, _dim_index)) < 0)
530 char name[hdfclass::MAXSTR];
531 int32 count, number_type, nattrs;
532 if (SDdiminfo(dim_id, name, &count, &number_type, &nattrs) < 0)
548 if (is_map_ce_set()) {
552 cerr <<
"dim name: " << name << endl;
553 cerr <<
"slab set: " << _slab.set << endl;
554 cerr <<
"dim index: " << _dim_index << endl;
555 cerr <<
"slab start: " << _slab.start[_dim_index] << endl;
556 cerr <<
"slab edge: " << _slab.edge[_dim_index] << endl;
559 vector < array_ce > ce = get_map_ce();
560 vector < array_ce >::iterator ce_iter =
561 find_if(ce.begin(), ce.end(), ce_name_match(
string(name)));
563 cerr <<
"ce name: " << ce_iter->name << endl;
564 cerr <<
"ce set: " << (ce_iter->start != 0 || ce_iter->edge != 0
565 || ce_iter->stride != 0) << endl;
566 cerr <<
"ce start: " << ce_iter->start << endl;
567 cerr <<
"ce edge: " << ce_iter->edge << endl << endl;
571 if(ce_iter!=ce.end()) {
572 _slab.set = ce_iter->start != 0 || ce_iter->edge != 0
573 || ce_iter->stride != 0;
574 _slab.reduce_rank =
false;
575 _slab.start[_dim_index] = ce_iter->start;
576 _slab.edge[_dim_index] = ce_iter->edge;
577 _slab.stride[_dim_index] = ce_iter->stride;
584 char label[hdfclass::MAXSTR];
585 char unit[hdfclass::MAXSTR];
586 char cformat[hdfclass::MAXSTR];
587 if (SDgetdimstrs(dim_id, label, unit, cformat, hdfclass::MAXSTR) ==
598 char junk[hdfclass::MAXSTR];
599 int32 junk2, junk3, junk4;
600 int32 dim_sizes[hdfclass::MAXDIMS];
601 if (SDgetinfo(_sds_id, junk, &junk2, dim_sizes, &junk3, &junk4)
604 count = dim_sizes[0];
611 if (number_type != 0) {
617 if (number_type != DFNT_CHAR) {
620 char *data =
new char[count * DFKNTsize(number_type)];
626 if (SDgetdimscale(dim_id, data) < 0) {
632 void *datastart = (
char *) data +
633 _slab.start[_dim_index] * DFKNTsize(number_type);
634 hd.scale =
hdf_genvec(number_type, datastart, 0,
635 _slab.edge[_dim_index] *
636 _slab.stride[_dim_index] - 1,
637 _slab.stride[_dim_index]);
639 hd.scale =
hdf_genvec(number_type, data, count);
647 hd.count = _slab.edge[_dim_index];
670 if (_filename.length() == 0)
686 char name[hdfclass::MAXSTR];
687 int32 number_type, count;
688 if (SDattrinfo(
id, _attr_index, name, &number_type, &count) < 0)
693 data =
new char[count * DFKNTsize(number_type)];
698 if (SDreadattr(
id, _attr_index, data) < 0) {
707 if (number_type == DFNT_CHAR)
708 count = (int32) min((
int) count, (int) strlen((
char *) data));
713 ha.values =
hdf_genvec(number_type, data, count);
731 hdfistream_sds & hdfistream_sds::operator>>(vector < hdf_sds > &hsv)
742 hdfistream_sds & hdfistream_sds::operator>>(vector < hdf_attr > &hav)
753 hdfistream_sds & hdfistream_sds::operator>>(vector < hdf_dim > &hdv)
756 for (
hdf_dim dim; !eo_dim();) {
765 bool hdf_sds::has_scale(
void)
const
768 if (!_ok(&has_scale)) {
782 bool hdf_sds::_ok(
bool * has_scale)
const
790 for (
int i = 0; i < (int) dims.size(); ++i)
791 if (dims[i].scale.size() != 0) {
794 if (dims[i].scale.size() != dims[i].count)