44#include "config_hdf.h"
67inline int min(
int t1,
int t2)
69 return (t1 < t2 ? t1 : t2);
73const string hdfistream_sds::long_name =
"long_name";
74const string hdfistream_sds::units =
"units";
75const string hdfistream_sds::format =
"format";
82void hdfistream_sds::_init(
void)
84 _sds_id = _attr_index = _dim_index = _nsds = _rank = _nattrs =
87 _meta = _slab.set =
false;
93void hdfistream_sds::_get_fileinfo(
void)
95 if (SDfileinfo(_file_id, &_nsds, &_nfattrs) < 0)
101void 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)
109 THROW(hcerr_sdsinfo);
111 if (_rank > hdfclass::MAXDIMS)
117void hdfistream_sds::_close_sds(
void)
120 (void) SDendaccess(_sds_id);
121 _sds_id = _attr_index = _dim_index = _rank = _nattrs = 0;
128void 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)
143 THROW(hcerr_sdsopen);
144 if (!SDiscoordvar(_sds_id))
146 SDendaccess(_sds_id);
152void hdfistream_sds::_seek_arr(
int arr_index)
155 for (_rewind(); _index < _nsds && arr_count <= arr_index;
156 _seek_next_arr(), arr_count++);
160void 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)
171 THROW(hcerr_sdsfind);
172 if ((_sds_id = SDselect(_file_id, index)) < 0)
173 THROW(hcerr_sdsopen);
174 bool iscoord = SDiscoordvar(_sds_id);
176 SDendaccess(_sds_id);
178 THROW(hcerr_sdsfind);
185void 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)
195 THROW(hcerr_sdsfind);
196 if ((_sds_id = SDselect(_file_id, index)) < 0)
197 THROW(hcerr_sdsopen);
198 bool iscoord = SDiscoordvar(_sds_id);
200 SDendaccess(_sds_id);
202 THROW(hcerr_sdsfind);
214hdfistream_sds::hdfistream_sds(
const string& filename):
218 if (_filename.size() != 0)
219 open(_filename.c_str());
224bool hdfistream_sds::eos(
void)
const
226 if (_filename.size() == 0)
227 THROW(hcerr_invstream);
234 return (_index >= _nsds);
239bool hdfistream_sds::bos(
void)
const
241 if (_filename.size() == 0)
242 THROW(hcerr_invstream);
253bool hdfistream_sds::eo_attr(
void)
const
255 if (_filename.size() == 0)
256 THROW(hcerr_invstream);
261 return (_attr_index >= _nfattrs);
263 return (_attr_index >= _nattrs);
269bool hdfistream_sds::eo_dim(
void)
const
271 if (_filename.size() == 0)
272 THROW(hcerr_invstream);
279 return (_dim_index >= _rank);
284void hdfistream_sds::open(
const char *filename)
286 if (filename ==
nullptr)
287 THROW(hcerr_openfile);
288 BESDEBUG(
"h4",
"sds opening file " << filename << endl);
291 if ((_file_id = SDstart((
char *) filename, DFACC_READ)) < 0)
293 THROW(hcerr_openfile);
296 BESDEBUG(
"h4",
"sds file opened: id=" << _file_id << endl);
298 _filename = filename;
305void 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;
317void hdfistream_sds::seek(
int index)
319 if (_filename.size() == 0)
320 THROW(hcerr_invstream);
323 if (!eos() && !bos())
328void hdfistream_sds::seek(
const char *name)
330 if (_filename.size() == 0)
331 THROW(hcerr_invstream);
333 _seek_arr(
string(name));
334 if (!eos() && !bos())
339void hdfistream_sds::rewind(
void)
341 if (_filename.size() == 0)
342 THROW(hcerr_invstream);
348void hdfistream_sds::seek_next(
void)
350 if (_filename.size() == 0)
351 THROW(hcerr_invstream);
358void hdfistream_sds::seek_ref(
int ref)
360 if (_filename.size() == 0)
361 THROW(hcerr_invstream);
364 if (!eos() && !bos())
369void 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)
375 THROW(hcerr_invslab);
377 for (
int i = 0; i < (int) start.size() && i < hdfclass::MAXDIMS; ++i) {
379 THROW(hcerr_invslab);
381 THROW(hcerr_invslab);
383 THROW(hcerr_invslab);
384 _slab.start[i] = start[i];
385 _slab.edge[i] = edge[i];
386 _slab.stride[i] = stride[i];
389 _slab.reduce_rank = reduce_rank;
399 hs.dims = vector < hdf_dim > ();
400 hs.attrs = vector < hdf_attr > ();
401 hs.data = hdf_genvec();
404 if (_filename.size() == 0)
405 THROW(hcerr_invstream);
412 char name[hdfclass::MAXSTR];
414 int32 dim_sizes[hdfclass::MAXDIMS];
417 if (SDgetinfo(_sds_id, name, &rank, dim_sizes, &number_type, &nattrs) <
419 THROW(hcerr_sdsinfo);
422 hs.ref = SDidtoref(_sds_id);
427 char *data =
nullptr;
430 hs.data.import_vec(number_type);
433 for (
int i = 0; i < rank; ++i)
434 nelts *= _slab.edge[i];
437 int datasize = nelts * DFKNTsize(number_type);
438 data =
new char[datasize];
440 THROW(hcerr_nomemory);
441 BESDEBUG(
"h4",
"SDreaddata() on line 387. _sds_id: " << _sds_id
443 if (SDreaddata(_sds_id, _slab.start, _slab.stride, _slab.edge,
446 THROW(hcerr_sdsread);
451 int32 zero[hdfclass::MAXDIMS];
452 for (
int i = 0; i < rank && i < hdfclass::MAXDIMS; ++i) {
454 nelts *= dim_sizes[i];
458 int datasize = nelts * DFKNTsize(number_type);
459 data =
new char[datasize];
461 THROW(hcerr_nomemory);
464 if (SDreaddata(_sds_id, zero,
nullptr, dim_sizes, data) < 0) {
466 THROW(hcerr_sdsread);
470 hs.data.import_vec(number_type, data, nelts);
480class ce_name_match:
public std::unary_function < array_ce, bool > {
483 explicit ce_name_match(
const string & n):name(n) {
485 bool operator() (
const array_ce & a_ce)
const {
486 return name == a_ce.name;
495 hd.name = hd.label = hd.unit = hd.format = string();
497 hd.scale = hdf_genvec();
498 hd.attrs = vector < hdf_attr > ();
500 if (_filename.size() == 0)
501 THROW(hcerr_invstream);
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)
526 THROW(hcerr_sdsinfo);
530 char name[hdfclass::MAXSTR];
534 if (SDdiminfo(dim_id, name, &count, &number_type, &nattrs) < 0)
535 THROW(hcerr_sdsinfo);
550 if (is_map_ce_set()) {
554 cerr <<
"dim name: " << name << endl;
555 cerr <<
"slab set: " << _slab.set << endl;
556 cerr <<
"dim index: " << _dim_index << endl;
557 cerr <<
"slab start: " << _slab.start[_dim_index] << endl;
558 cerr <<
"slab edge: " << _slab.edge[_dim_index] << endl;
561 vector < array_ce > ce = get_map_ce();
562 auto ce_iter = find_if(ce.begin(), ce.end(), ce_name_match(
string(name)));
564 cerr <<
"ce name: " << ce_iter->name << endl;
565 cerr <<
"ce set: " << (ce_iter->start != 0 || ce_iter->edge != 0
566 || ce_iter->stride != 0) << endl;
567 cerr <<
"ce start: " << ce_iter->start << endl;
568 cerr <<
"ce edge: " << ce_iter->edge << endl << endl;
572 if(ce_iter!=ce.end()) {
573 _slab.set = ce_iter->start != 0 || ce_iter->edge != 0
574 || ce_iter->stride != 0;
575 _slab.reduce_rank =
false;
576 _slab.start[_dim_index] = ce_iter->start;
577 _slab.edge[_dim_index] = ce_iter->edge;
578 _slab.stride[_dim_index] = ce_iter->stride;
581 THROW(hcerr_sdsinfo);
585 char label[hdfclass::MAXSTR];
586 char unit[hdfclass::MAXSTR];
587 char cformat[hdfclass::MAXSTR];
588 if (SDgetdimstrs(dim_id, label, unit, cformat, hdfclass::MAXSTR) ==
598 THROW(hcerr_sdsinfo);
599 char junk[hdfclass::MAXSTR];
600 int32 junk2, junk3, junk4;
601 int32 dim_sizes[hdfclass::MAXDIMS];
602 if (SDgetinfo(_sds_id, junk, &junk2, dim_sizes, &junk3, &junk4)
604 THROW(hcerr_sdsinfo);
605 count = dim_sizes[0];
612 if (number_type != 0) {
618 if (number_type != DFNT_CHAR) {
621 auto data =
new char[count * DFKNTsize(number_type)];
624 THROW(hcerr_nomemory);
627 if (SDgetdimscale(dim_id, data) < 0) {
629 THROW(hcerr_sdsinfo);
633 void *datastart = data +
634 _slab.start[_dim_index] * DFKNTsize(number_type);
635 hd.scale = hdf_genvec(number_type, datastart, 0,
636 _slab.edge[_dim_index] *
637 _slab.stride[_dim_index] - 1,
638 _slab.stride[_dim_index]);
640 hd.scale = hdf_genvec(number_type, data, count);
648 hd.count = _slab.edge[_dim_index];
669 ha.values = hdf_genvec();
671 if (_filename.size() == 0)
672 THROW(hcerr_invstream);
684 char name[hdfclass::MAXSTR];
688 if (SDattrinfo(
id, _attr_index, name, &number_type, &count) < 0)
689 THROW(hcerr_sdsinfo);
692 auto data =
new char[count * DFKNTsize(number_type)];
694 THROW(hcerr_nomemory);
697 if (SDreadattr(
id, _attr_index, data) < 0) {
699 THROW(hcerr_sdsinfo);
706 if (number_type == DFNT_CHAR)
707 count = (int32) min((
int) count, (
int) strlen((
char *) data));
711 ha.values = hdf_genvec(number_type, data, count);
724hdfistream_sds & hdfistream_sds::operator>>(vector < hdf_sds > &hsv)
727 for (hdf_sds sds; !eos();) {
735hdfistream_sds & hdfistream_sds::operator>>(vector < hdf_attr > &hav)
738 for (hdf_attr att; !eo_attr();) {
746hdfistream_sds & hdfistream_sds::operator>>(vector < hdf_dim > &hdv)
749 for (hdf_dim dim; !eo_dim();) {
758bool hdf_sds::has_scale(
void)
const
761 if (!_ok(&has_scale)) {
762 THROW(hcerr_sdsscale);
775bool hdf_sds::_ok(
bool * has_scale)
const
783 for (
const auto & dim:dims)
784 if (dim.scale.size() != 0) {
787 if (dim.scale.size() != dim.count)