44 #include "config_hdf.h"
61 #include <InternalErr.h>
71 static void LoadField(int32 vid,
int index, int32 begin, int32 end,
73 static bool IsInternalVdata(int32 fid, int32 ref);
80 void hdfistream_vdata::_init(
void) {
81 _vdata_id = _index = _attr_index = _nattrs = 0;
88 void hdfistream_vdata::_get_fileinfo(
void) {
91 while ((ref = VSgetid(_file_id, ref)) != -1) {
92 if (!IsInternalVdata(_file_id, ref))
93 _vdata_refs.push_back(ref);
98 void hdfistream_vdata::_seek_next(
void) {
101 _seek(_vdata_refs[_index]);
105 void hdfistream_vdata::_seek(
const char *name) {
106 int32 ref = VSfind(_file_id, name);
115 void hdfistream_vdata::_seek(int32 ref) {
118 vector<int32>::iterator r = find(_vdata_refs.begin(), _vdata_refs.end(),
120 if (r == _vdata_refs.end())
122 _index = r - _vdata_refs.begin();
123 if ((_vdata_id = VSattach(_file_id, ref,
"r")) < 0) {
128 _nattrs = VSfnattrs(_vdata_id, _HDF_VDATA);
136 hdfistream_vdata::hdfistream_vdata(
const string filename) :
139 if (_filename.length() != 0)
140 open(_filename.c_str());
144 void hdfistream_vdata::open(
const string & filename) {
145 open(filename.c_str());
149 void hdfistream_vdata::open(
const char *filename) {
152 if ((_file_id = Hopen(filename, DFACC_RDONLY, 0)) < 0)
154 if (Vstart(_file_id) < 0)
157 BESDEBUG(
"h4",
"vdata file opened: id=" << _file_id << endl);
159 _filename = filename;
165 void hdfistream_vdata::close(
void) {
167 "vdata file closed: id=" << _file_id <<
", this: " <<
this << endl);
172 int status = Vend(_file_id);
174 "vdata Vend status: " << status <<
", this: " <<
this << endl);
176 status = Hclose(_file_id);
178 "vdata HClose status: " << status <<
", this: " <<
this << endl);
180 _vdata_id = _file_id = _index = _attr_index = _nattrs = 0;
186 void hdfistream_vdata::seek(
int index) {
187 if (index < 0 || index >= (
int) _vdata_refs.size())
189 _seek(_vdata_refs[index]);
194 void hdfistream_vdata::seek_ref(
int ref) {
199 void hdfistream_vdata::seek(
const string & name) {
203 void hdfistream_vdata::seek(
const char *name) {
208 bool hdfistream_vdata::setrecs(int32 begin, int32 end) {
209 if (_vdata_id != 0) {
211 VSQueryinterlace(_vdata_id, &il);
212 if (il != FULL_INTERLACE)
216 VSQuerycount(_vdata_id, &cnt);
217 if (begin < 0 || end >= cnt)
231 bool hdfistream_vdata::eo_attr(
void)
const {
232 if (_filename.length() == 0)
237 return (_attr_index >= _nattrs);
266 if (_filename.length() == 0)
271 char name[hdfclass::MAXSTR];
272 int32 number_type, count, size;
273 if (VSattrinfo(_vdata_id, _HDF_VDATA, _attr_index, name, &number_type,
279 data =
new char[count * DFKNTsize(number_type)];
284 if (VSgetattr(_vdata_id, _HDF_VDATA, _attr_index, data) < 0) {
290 ha.values =
hdf_genvec(number_type, data, count);
310 hv.vclass = hv.name = string();
318 hv.ref = _vdata_refs[_index];
322 char name[hdfclass::MAXSTR];
323 char vclass[hdfclass::MAXSTR];
325 if (VSinquire(_vdata_id, &nrecs, (int32 *) 0, (
char *) 0, (int32 *) 0, name)
328 hv.name = string(name);
329 if (VSgetclass(_vdata_id, vclass) < 0)
331 hv.vclass = string(vclass);
334 int nfields = VFnfields(_vdata_id);
339 hv.fields = vector<hdf_field> ();
340 for (
int i = 0; i < nfields; ++i) {
343 LoadField(_vdata_id, i, 0, 0, hv.fields[i]);
345 LoadField(_vdata_id, i, _recs.begin, _recs.end, hv.fields[i]);
347 LoadField(_vdata_id, i, 0, nrecs - 1, hv.fields[i]);
357 bool hdfistream_vdata::isInternalVdata(
int ref)
const {
358 set<string, less<string> > reserved_names;
359 reserved_names.insert(
"RIATTR0.0N");
361 set<string, less<string> > reserved_classes;
362 reserved_classes.insert(
"Attr0.0");
363 reserved_classes.insert(
"RIATTR0.0C");
364 reserved_classes.insert(
"DimVal0.0");
365 reserved_classes.insert(
"DimVal0.1");
366 reserved_classes.insert(
"_HDF_CHK_TBL_0");
370 if ((vid = VSattach(_file_id, ref,
"r")) < 0) {
373 char name[hdfclass::MAXSTR];
374 char vclass[hdfclass::MAXSTR];
375 if (VSgetname(vid, name) < 0) {
379 if (reserved_names.find(
string(name)) != reserved_names.end()) {
384 if (VSgetclass(vid, vclass) < 0) {
391 if (reserved_classes.find(
string(vclass)) != reserved_classes.end())
398 static void LoadField(int32 vid,
int index, int32 begin, int32 end,
400 DBG(cerr <<
"LoadField - vid: " << vid << endl);
403 if (VSseek(vid, begin) < 0)
405 int32 nrecs = end - begin + 1;
408 DBG(cerr <<
"vid: " << vid <<
", index: " << index << endl);
409 char *fieldname = VFfieldname(vid, index);
412 f.name = string(fieldname);
415 int32 fieldorder = VFfieldorder(vid, index);
420 int32 fieldsize = VFfieldisize(vid, index);
425 int32 fieldtype = VFfieldtype(vid, index);
435 data.resize(fieldsize * nrecs);
436 DBG(cerr <<
"LoadField: vid=" << vid <<
", fieldname=" << fieldname << endl);
445 if (VSsetfields(vid, fieldname) < 0) {
450 if (VSread(vid, (uchar8 *)&data[0], nrecs, FULL_INTERLACE) < 0) {
451 throw InternalErr(__FILE__, __LINE__,
"VSread error with the field: " + f.name +
" (" + long_to_string(vid) +
").");
454 if ((VSsetfields(vid, fieldname) < 0) || (VSread(vid, (uchar8 *) data,
455 nrecs, FULL_INTERLACE) < 0)) {
461 int stride = fieldorder;
462 for (
int i = 0; i < fieldorder; ++i) {
466 gv =
hdf_genvec(fieldtype, &data[0], i, (nrecs * fieldorder) - 1, stride);
467 f.vals.push_back(gv);
477 bool hdf_field::_ok(
void)
const {
480 if (vals.size() == 0)
485 if (vals.size() > 1) {
486 int32 nt = vals[0].number_type();
489 for (
int i = 1; i < (int) vals.size(); ++i)
490 if (vals[i].number_type() != nt || vals[i].number_type() == 0)
497 bool hdf_vdata::_ok(
void)
const {
500 if (fields.size() == 0)
504 for (
int i = 0; i < (int) fields.size(); ++i)
511 bool IsInternalVdata(int32 fid, int32 ref) {
512 set<string, less<string> > reserved_names;
513 reserved_names.insert(
"RIATTR0.0N");
515 set<string, less<string> > reserved_classes;
516 reserved_classes.insert(
"Attr0.0");
517 reserved_classes.insert(
"RIATTR0.0C");
518 reserved_classes.insert(
"DimVal0.0");
519 reserved_classes.insert(
"DimVal0.1");
520 reserved_classes.insert(
"_HDF_CHK_TBL_0");
524 if ((vid = VSattach(fid, ref,
"r")) < 0) {
527 char name[hdfclass::MAXSTR];
528 char vclass[hdfclass::MAXSTR];
529 if (VSgetname(vid, name) < 0) {
533 if (reserved_names.find(
string(name)) != reserved_names.end()) {
538 if (VSgetclass(vid, vclass) < 0) {
545 if (reserved_classes.find(
string(vclass)) != reserved_classes.end())