128struct Exporter :
public HDF5Base
130 Exporter(std::string
const& name, std::string
const& out_format, [[maybe_unsed]]
int prec,
131 int smart_size_limit = 4)
133 , m_smart_size_limit(smart_size_limit)
134 , m_write_xml_hdf(
false)
136 if (out_format.compare(
"ascii") == 0) {
139 fout.open(xfile_name.c_str());
141 fout <<
"<?xml version=\"1.0\" encoding=\"iso-8859-1\" ?>" << std::endl;
143 if (out_format.compare(
"hdf5") == 0) {
146 if (m_write_xml_hdf) {
147 fout.open(xfile_name.c_str());
149 fout <<
"<?xml version=\"1.0\" encoding=\"iso-8859-1\" ?>" << std::endl;
152 hfile = H5Fcreate(hfile_name.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
154 throw FatalErrorException(A_FUNCINFO,
155 "hdf5 format requested while there is no support for hdf5 in Alien");
158 if (out_format.compare(
"smart") == 0) {
161 fout.open(xfile_name.c_str());
163 fout <<
"<?xml version=\"1.0\" encoding=\"iso-8859-1\" ?>" << std::endl;
165 hfile = H5Fcreate(hfile_name.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
167 throw FatalErrorException(A_FUNCINFO,
168 "smart format requested while there is no support for hdf5 in Alien");
174 if (type == ASCII || m_write_xml_hdf || type == SMART) {
177 if (type == HDF5 || type == SMART) {
185 FileNode createFileNode(
const std::string& name,
int level = 0)
189 node.path_name =
"/" + name;
191 if (type == ASCII || m_write_xml_hdf || type == SMART) {
192 fout <<
"<" << name <<
">" << std::endl;
194 if (type == HDF5 || type == SMART) {
197 H5Gcreate2(hfile, node.name.c_str(), H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
207 node.path_name = parent.path_name +
"/" + name;
208 node.level = parent.level + 1;
209 if (type == ASCII || m_write_xml_hdf || type == SMART) {
211 fout <<
"<" << name <<
">" << std::endl;
213 if (type == HDF5 || type == SMART) {
215 node.h_id = H5Gcreate2(
216 parent.h_id, node.name.c_str(), H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
223 FileNode const& parent, std::string name, std::string group_name)
227 node.path_name = parent.path_name +
"/" + name;
228 node.level = parent.level + 1;
229 if (type == ASCII || m_write_xml_hdf || type == SMART) {
230 for (
int i = 0; i < parent.level; ++i)
232 fout <<
"<" << name <<
" group-name=\"" << group_name <<
"\">" << std::endl;
234 if (type == HDF5 || type == SMART) {
236 node.h_id = H5Gcreate2(
237 parent.h_id, node.name.c_str(), H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
243 FileNode createFileNode(
FileNode const& parent, std::string name, std::string att_name,
244 std::string att_kind,
bool is_group =
false)
249 node.path_name = parent.path_name +
"/" + name +
"_" + att_kind +
"_" + att_name;
251 node.path_name = parent.path_name +
"/" + att_name;
253 node.level = parent.level + 1;
254 if (type == ASCII || m_write_xml_hdf || type == SMART) {
256 fout <<
"<" << name <<
" name=\"" << att_name <<
"\" kind=\"" << att_kind <<
"\">"
259 if (type == HDF5 || type == SMART) {
261 node.h_id = H5Gcreate2(
262 parent.h_id, att_name.c_str(), H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
268 void closeFileNode(
FileNode const& group)
270 if (type == ASCII || m_write_xml_hdf || type == SMART) {
271 _tab(group.level - 1);
272 fout <<
"</" << group.name <<
">" << std::endl;
274 if (type == HDF5 || type == SMART) {
276 H5Gclose(group.h_id);
281 template <
typename ValueT>
282 void write(
FileNode const& parent_node,
const std::string& node_name,
const ValueT& val)
286 _tab(parent_node.level);
287 fout <<
"<" << node_name <<
" format=\"xml\" type=\"" << m_types.type(ValueT())
288 <<
"\">" << std::endl;
289 fout << val << std::endl;
290 _tab(parent_node.level);
291 fout <<
"</" << node_name <<
">" << std::endl;
294 if (m_write_xml_hdf) {
295 _tab(parent_node.level);
296 fout <<
"<" << node_name <<
" format=\"hdf\" type=\"" << m_types.type(ValueT())
297 <<
"\">" << std::endl;
298 _tab(parent_node.level);
299 fout << hfile_name <<
":" << parent_node.path_name <<
"/" << node_name
301 _tab(parent_node.level);
302 fout <<
"</" << node_name <<
">" << std::endl;
306 hid_t dataspace_id = H5Screate_simple(1, &dim, NULL);
308 H5Dcreate2(parent_node.h_id, node_name.c_str(), m_types.nativeType(ValueT()),
309 dataspace_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
310 herr_t status = H5Dwrite(
311 dataset_id, m_types.nativeType(ValueT()), H5S_ALL, H5S_ALL, H5P_DEFAULT, &val);
313 std::cerr <<
"Error while writing HDF5 data set" << std::endl;
314 H5Dclose(dataset_id);
315 H5Sclose(dataspace_id);
319 _tab(parent_node.level);
320 fout <<
"<" << node_name <<
" format=\"xml\" type=\"" << m_types.type(ValueT())
321 <<
"\">" << std::endl;
322 fout << val << std::endl;
323 _tab(parent_node.level);
324 fout <<
"</" << node_name <<
">" << std::endl;
327 hid_t dataspace_id = H5Screate_simple(1, &dim, NULL);
329 H5Dcreate2(parent_node.h_id, node_name.c_str(), m_types.nativeType(ValueT()),
330 dataspace_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
331 herr_t status = H5Dwrite(
332 dataset_id, m_types.nativeType(ValueT()), H5S_ALL, H5S_ALL, H5P_DEFAULT, &val);
334 std::cerr <<
"Error while writing HDF5 data set" << std::endl;
335 H5Dclose(dataset_id);
336 H5Sclose(dataspace_id);
340 std::cerr <<
"Unknown output format " << std::endl;
345 template <
typename ValueT>
346 void write(
FileNode const& parent_node,
const std::string& node_name,
347 std::vector<ValueT>& buffer,
int nb_elems_per_line = 1)
351 _tab(parent_node.level);
352 fout <<
"<" << node_name <<
" format=\"xml\" type=\"" << m_types.type(ValueT())
353 <<
"\">" << std::endl;
356 for (std::size_t n = 0; n < buffer.size() / nb_elems_per_line; ++n) {
357 for (
int k = 0; k < nb_elems_per_line; ++k)
358 fout << buffer[icount + k] <<
" ";
359 icount += nb_elems_per_line;
362 _tab(parent_node.level);
363 fout <<
"</" << node_name <<
">" << std::endl;
366 if (m_write_xml_hdf) {
367 _tab(parent_node.level);
368 fout <<
"<" << node_name <<
" format=\"hdf\" type=\"" << m_types.type(ValueT())
369 <<
"\">" << std::endl;
370 _tab(parent_node.level);
371 fout << hfile_name <<
":" << parent_node.path_name <<
"/" << node_name
373 _tab(parent_node.level);
374 fout <<
"</" << node_name <<
">" << std::endl;
377 hsize_t dim = buffer.size();
378 hid_t dataspace_id = H5Screate_simple(1, &dim, NULL);
380 H5Dcreate2(parent_node.h_id, node_name.c_str(), m_types.nativeType(ValueT()),
381 dataspace_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
382 herr_t status = H5Dwrite(dataset_id, m_types.nativeType(ValueT()), H5S_ALL, H5S_ALL,
383 H5P_DEFAULT, buffer.data());
385 std::cerr <<
"Error while writing HDF5 data set" << std::endl;
386 H5Dclose(dataset_id);
387 H5Sclose(dataspace_id);
391 if ((
int)buffer.size() < m_smart_size_limit) {
392 _tab(parent_node.level);
393 fout <<
"<" << node_name <<
" format=\"xml\" type=\"" << m_types.type(ValueT())
394 <<
"\">" << std::endl;
397 for (std::size_t n = 0; n < buffer.size() / nb_elems_per_line; ++n) {
398 for (
int k = 0; k < nb_elems_per_line; ++k)
399 fout << buffer[icount + k] <<
" ";
400 icount += nb_elems_per_line;
403 _tab(parent_node.level);
404 fout <<
"</" << node_name <<
">" << std::endl;
407 _tab(parent_node.level);
408 fout <<
"<" << node_name <<
" format=\"hdf\" type=\"" << m_types.type(ValueT())
409 <<
"\">" << std::endl;
410 _tab(parent_node.level + 1);
411 fout << hfile_name <<
":" << parent_node.path_name <<
"/" << node_name
413 _tab(parent_node.level);
414 fout <<
"</" << node_name <<
">" << std::endl;
417 hsize_t dim = buffer.size();
418 hid_t dataspace_id = H5Screate_simple(1, &dim, NULL);
420 H5Dcreate2(parent_node.h_id, node_name.c_str(), m_types.nativeType(ValueT()),
421 dataspace_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
422 herr_t status = H5Dwrite(dataset_id, m_types.nativeType(ValueT()), H5S_ALL, H5S_ALL,
423 H5P_DEFAULT, buffer.data());
425 std::cerr <<
"Error while writing HDF5 data set" << std::endl;
426 H5Dclose(dataset_id);
427 H5Sclose(dataspace_id);
431 std::cerr <<
"Unknown output format " << std::endl;
437 void write(
FileNode const& parent_node,
const std::string& node_name,
438 const std::string& buffer)
442 _tab(parent_node.level);
443 fout <<
"<" << node_name <<
" format=\"xml\" type=\"" << m_types.type(
char())
444 <<
"\">" << std::endl;
445 fout << buffer << std::endl;
446 _tab(parent_node.level);
447 fout <<
"</" << node_name <<
">" << std::endl;
450 if (m_write_xml_hdf) {
451 _tab(parent_node.level);
452 fout <<
"<" << node_name <<
" format=\"hdf\" type=\"" << m_types.type(
char())
453 <<
"\">" << std::endl;
454 _tab(parent_node.level);
455 fout << hfile_name <<
":" << parent_node.path_name << std::endl;
456 _tab(parent_node.level);
457 fout <<
"</" << node_name <<
">" << std::endl;
460 hsize_t dim = buffer.size();
461 hid_t dataspace_id = H5Screate_simple(1, &dim, NULL);
463 H5Dcreate2(parent_node.h_id, node_name.c_str(), m_types.nativeType(
char()),
464 dataspace_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
465 herr_t status = H5Dwrite(dataset_id, m_types.nativeType(
char()), H5S_ALL, H5S_ALL,
466 H5P_DEFAULT, buffer.c_str());
468 std::cerr <<
"Error while writing HDF5 data set" << std::endl;
469 H5Dclose(dataset_id);
470 H5Sclose(dataspace_id);
474 _tab(parent_node.level);
475 fout <<
"<" << node_name <<
" format=\"xml\" type=\"" << m_types.type(
char())
476 <<
"\">" << std::endl;
477 fout << buffer << std::endl;
478 _tab(parent_node.level);
479 fout <<
"</" << node_name <<
">" << std::endl;
481 hsize_t dim = buffer.size();
482 hid_t dataspace_id = H5Screate_simple(1, &dim, NULL);
484 H5Dcreate2(parent_node.h_id, node_name.c_str(), m_types.nativeType(
char()),
485 dataspace_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
486 herr_t status = H5Dwrite(dataset_id, m_types.nativeType(
char()), H5S_ALL, H5S_ALL,
487 H5P_DEFAULT, buffer.data());
489 std::cerr <<
"Error while writing HDF5 data set" << std::endl;
490 H5Dclose(dataset_id);
491 H5Sclose(dataspace_id);
495 std::cerr <<
"Unknown output format " << std::endl;
501 int m_smart_size_limit;
502 bool m_write_xml_hdf;
504 void _tab(
const int level)
506 for (
int i = 0; i < level; ++i)
511struct Importer :
public HDF5Base
514 Importer(std::string
const& name, std::string
const& in_format,
int prec)
517 if (in_format.compare(
"ascii") == 0) {
520#ifdef ALIEN_USE_LIBXML2
521 doc = xmlParseFile(xfile_name.c_str());
523 std::cerr <<
"Error while parsing XML file : " << xfile_name << std::endl;
526 throw FatalErrorException(
527 A_FUNCINFO,
"xml format requested while there is no support for xml in Alien");
530 else if (in_format.compare(
"hdf5") == 0) {
534 hfile = H5Fopen(hfile_name.c_str(), H5F_ACC_RDWR, H5P_DEFAULT);
536 throw FatalErrorException(A_FUNCINFO,
537 "hdf5 format requested while there is no support for hdf5 in Alien");
541 throw FatalErrorException(
542 A_FUNCINFO,
"format requested not supported for read operations");
550#ifdef ALIEN_USE_LIBXML2
565 FileNode openFileNode(
const std::string& node_name)
571#ifdef ALIEN_USE_LIBXML2
572 node.x_id = xmlDocGetRootElement(doc);
573 if (xmlStrcmp(node.x_id->name, (
const xmlChar*)node_name.c_str())) {
574 throw FatalErrorException(A_FUNCINFO,
"node not found");
580 node.h_id = H5Gopen2(hfile, node.name.c_str(), H5P_DEFAULT);
584 throw FatalErrorException(
585 A_FUNCINFO,
"format requested not supported for read operations");
595 node.path_name = parent.path_name +
"/" + name;
596 node.level = parent.level + 1;
599#ifdef ALIEN_USE_LIBXML2
600 xmlNodePtr cur = parent.x_id->xmlChildrenNode;
601 while (cur != NULL) {
602 if ((!xmlStrcmp(cur->name, (
const xmlChar*)name.c_str()))) {
614 node.h_id = H5Gopen2(parent.h_id, node.name.c_str(), H5P_DEFAULT);
619 throw FatalErrorException(
620 A_FUNCINFO,
"format requested not supported for read operations");
626 void closeFileNode(
FileNode const& group)
634 H5Gclose(group.h_id);
638 throw FatalErrorException(
639 A_FUNCINFO,
"format requested not supported for read operations");
644 template <
typename ValueT>
645 void read(
FileNode const& parent_node,
const std::string& node_name,
646 std::vector<ValueT>& buffer)
650#ifdef ALIEN_USE_LIBXML2
653 xmlNodePtr cur = parent_node.x_id->xmlChildrenNode;
654 while (cur !=
nullptr) {
655 if ((!xmlStrcmp(cur->name, (
const xmlChar*)node_name.c_str()))) {
663 xmlChar* contenu = xmlNodeGetContent(node.x_id);
665 std::stringstream flux;
666 flux << (
const xmlChar*)contenu;
667 for (std::size_t i = 0; i < buffer.size(); ++i)
675 hid_t dataset_id = H5Dopen2(parent_node.h_id, node_name.c_str(), H5P_DEFAULT);
676 hid_t dataspace_id = H5Dget_space(dataset_id);
678 int ndim = H5Sget_simple_extent_ndims(dataspace_id);
681 throw FatalErrorException(A_FUNCINFO,
"Error while reading HDF5 data set");
683 hsize_t size, maxsize;
684 herr_t status = H5Sget_simple_extent_dims(dataspace_id, &size, &maxsize);
686 throw FatalErrorException(A_FUNCINFO,
"Error while reading HDF5 data set");
688 if (size != buffer.size()) {
690 throw FatalErrorException(A_FUNCINFO,
"Error while reading HDF5 data set");
693 status = H5Dread(dataset_id, m_types.nativeType(ValueT()), H5S_ALL, H5S_ALL,
694 H5P_DEFAULT, buffer.data());
696 throw FatalErrorException(A_FUNCINFO,
"Error while reading HDF5 node");
698 H5Sclose(dataspace_id);
699 H5Dclose(dataset_id);
703 throw FatalErrorException(A_FUNCINFO,
"Unknown input format ");
708 template <
typename ValueT>
709 void read(
FileNode const& parent_node,
const std::string& node_name, ValueT& val)
713#ifdef ALIEN_USE_LIBXML2
716 xmlNodePtr cur = parent_node.x_id->xmlChildrenNode;
717 while (cur !=
nullptr) {
718 if ((!xmlStrcmp(cur->name, (
const xmlChar*)node_name.c_str()))) {
726 xmlChar* contenu = xmlNodeGetContent(node.x_id);
728 std::stringstream flux;
729 flux << (
const xmlChar*)contenu;
737 hid_t dataset_id = H5Dopen2(parent_node.h_id, node_name.c_str(), H5P_DEFAULT);
738 hid_t dataspace_id = H5Dget_space(dataset_id);
740 int ndim = H5Sget_simple_extent_ndims(dataspace_id);
743 throw FatalErrorException(A_FUNCINFO,
"Error while reading HDF5 data set");
745 hsize_t size, maxsize;
746 herr_t status = H5Sget_simple_extent_dims(dataspace_id, &size, &maxsize);
748 throw FatalErrorException(A_FUNCINFO,
"Error while reading HDF5 data set");
752 throw FatalErrorException(A_FUNCINFO,
"Error while reading HDF5 data set");
756 dataset_id, m_types.nativeType(ValueT()), H5S_ALL, H5S_ALL, H5P_DEFAULT, &val);
758 throw FatalErrorException(A_FUNCINFO,
"Error while reading HDF5 node");
760 H5Sclose(dataspace_id);
761 H5Dclose(dataset_id);
765 throw FatalErrorException(A_FUNCINFO,
"Unknown input format ");
770 void read(
FileNode const& node, std::string& buffer)
774#ifdef ALIEN_USE_LIBXML2
775 xmlChar* contenu = xmlNodeGetContent(node.x_id);
776 buffer = std::string((
char*)contenu);
783 hid_t dataset_id = H5Dopen2(node.h_id,
"data", H5P_DEFAULT);
784 hid_t dataspace_id = H5Dget_space(dataset_id);
785 int ndim = H5Sget_simple_extent_ndims(dataspace_id);
788 throw FatalErrorException(A_FUNCINFO,
"Error while reading HDF5 data set");
791 hsize_t size, maxsize;
792 herr_t status = H5Sget_simple_extent_dims(dataspace_id, &size, &maxsize);
794 throw FatalErrorException(A_FUNCINFO,
"Error while reading HDF5 data set");
797 buffer.reserve(size + 1);
798 buffer.resize(size + 1);
800 status = H5Dread(dataset_id, m_types.nativeType(
char()), H5S_ALL, H5S_ALL,
801 H5P_DEFAULT, &buffer[0]);
803 throw FatalErrorException(A_FUNCINFO,
"Error while reading HDF5 data set");
806 status = H5Sclose(dataspace_id);
807 status = H5Dclose(dataset_id);
811 std::cerr <<
"Unknown output format " << std::endl;