h5cpp  0.6.0
A modern C++ wrapper for the HDF5 C library
dataset.hpp
Go to the documentation of this file.
1 //
2 // (c) Copyright 2017 DESY,ESS
3 //
4 // This file is part of h5cpp.
5 //
6 // This library is free software; you can redistribute it and/or modify it
7 // under the terms of the GNU Lesser General Public License as published
8 // by the Free Software Foundation; either version 2.1 of the License, or
9 // (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful, but
12 // WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY
13 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 // License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public License
17 // along with this library; if not, write to the
18 // Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor
19 // Boston, MA 02110-1301 USA
20 // ===========================================================================
21 //
22 // Authors:
23 // Eugen Wintersberger <eugen.wintersberger@desy.de>
24 // Jan Kotanski <jan.kotanski@desy.de>
25 //
26 // Created on: Sep 7, 2017
27 //
28 #pragma once
29 
30 #include <h5cpp/node/node.hpp>
34 #include <h5cpp/dataspace/pool.hpp>
36 #include <h5cpp/core/types.hpp>
39 #include <h5cpp/core/windows.hpp>
45 #include <h5cpp/error/error.hpp>
46 #include <vector>
47 #include <h5cpp/core/utilities.hpp>
48 
49 namespace hdf5 {
50 namespace node {
51 
52 class Selection;
53 
54 #ifdef __clang__
55 #pragma clang diagnostic push
56 #pragma clang diagnostic ignored "-Wpadded"
57 #pragma clang diagnostic ignored "-Wweak-vtables"
58 #endif
59 class DLL_EXPORT Dataset : public Node
60 {
61  public:
70  Dataset() = default;
71 
81  Dataset(const Node &node);
82 
95  Dataset(const Group &base,const Path &path,
96  const datatype::Datatype &type,
97  const dataspace::Dataspace &space = dataspace::Scalar(),
101 
111 
122 
133 
144 
159  #ifdef __clang__
160  #pragma clang diagnostic push
161  #pragma clang diagnostic ignored "-Wdocumentation-deprecated-sync"
162  #endif
163  void extent(const Dimensions &dims) const;
164  #ifdef __clang__
165  #pragma clang diagnostic pop
166  #endif
167 
168 
169 
170  void extent(size_t dim,ssize_t delta_elements) const;
171 
172 
186  void resize(const Dimensions &dims) const;
187 
188 
189 
190 #if (defined(_DOXYGEN_) || H5_VERSION_GE(1,10,0))
197  void refresh() const;
198 #endif
199 
200 
218  template<typename T>
219  void write(const T &data,const datatype::Datatype &mem_type,
220  const dataspace::Dataspace &mem_space,
221  const dataspace::Dataspace &file_space,
222  const property::DatasetTransferList &dtpl =
224 
231  template<typename T>
232  void write(const T &data,const datatype::Datatype &mem_type,
233  const dataspace::Dataspace &mem_space,
234  const property::DatasetTransferList &dtpl =
236 
254  template<typename T>
255  void read(T &data,const datatype::Datatype &mem_type,
256  const dataspace::Dataspace &mem_space,
257  const dataspace::Dataspace &file_space,
258  const property::DatasetTransferList &dtpl =
260 
261  template<typename T>
262  void read(T &data,
263  const datatype::Datatype &mem_type,
264  const dataspace::Dataspace &mem_space,
265  const property::DatasetTransferList &dtpl =
267 
268  template<typename T>
269  void read_reshape(T &data,
270  const datatype::Datatype &mem_type,
271  const dataspace::Dataspace &mem_space,
272  const property::DatasetTransferList &dtpl =
274 
275 
286  template<typename T>
287  void write(const T &data,const property::DatasetTransferList &dtpl =
289  template<typename T>
290  void write(const T &data,const property::DatasetTransferList &dtpl =
292 
306  template<typename T>
307  void write_reshape(const T &data,
308  const datatype::Datatype &mem_type,
309  const dataspace::Dataspace &mem_space,
310  const property::DatasetTransferList &dtpl =
312 
322  void write(const char *data,const property::DatasetTransferList &dtpl =
324  void write(const char *data,const property::DatasetTransferList &dtpl =
326 
339  template<typename T>
340  void write_chunk(const T &data,
341  std::vector<hsize_t> offset,
342  std::uint32_t filter_mask = 0,
343  const property::DatasetTransferList &dtpl =
345 
346  template<typename T>
347  void write_chunk(const T &data,
348  std::vector<hsize_t> offset,
349  std::uint32_t filter_mask = 0,
350  const property::DatasetTransferList &dtpl =
352 
367  template<typename T>
368  void write_chunk(const T &data,
369  const datatype::Datatype &mem_type,
370  const dataspace::Dataspace &mem_space,
371  std::vector<hsize_t> & offset,
372  std::uint32_t filter_mask = 0,
373  const property::DatasetTransferList &dtpl =
375 
376 #if (defined(_DOXYGEN_) || H5_VERSION_GE(1,10,2))
377 
390  template<typename T>
391  std::uint32_t read_chunk(T &data,
392  std::vector<hsize_t> offset,
393  const property::DatasetTransferList &dtpl =
395 
396 
410  template<typename T>
411  std::uint32_t read_chunk(T &data,
412  const datatype::Datatype &mem_type,
413  std::vector<hsize_t> & offset,
414  const property::DatasetTransferList &dtpl =
416 
417 
427  hsize_t chunk_storage_size(std::vector<hsize_t> offset) const;
428 
429 #endif
430 
441  template<typename T>
442  void read(T &data,const property::DatasetTransferList &dtpl =
444  template<typename T>
445  void read(T &data,const property::DatasetTransferList &dtpl =
447 
459  template<typename T>
460  void write(const T &data,const dataspace::Selection &selection,
461  const property::DatasetTransferList &dtpl =
463  template<typename T>
464  void write(const T &data,const dataspace::Selection &selection,
465  const property::DatasetTransferList &dtpl =
467 
482  template<typename T>
483  void write_reshape(const T &data,
484  const datatype::Datatype &mem_type,
485  const dataspace::Dataspace &mem_space,
486  const dataspace::Selection &selection,
487  const property::DatasetTransferList &dtpl =
489 
502  template<typename T>
503  void read(T &data,const dataspace::Selection &selection,
504  const property::DatasetTransferList &dtpl =
506  template<typename T>
507  void read(T &data,const dataspace::Selection &selection,
508  const property::DatasetTransferList &dtpl =
510 
527  template<typename T>
528  void read_reshape(T &data,
529  const datatype::Datatype &mem_type,
530  const dataspace::Dataspace &mem_space,
531  const dataspace::Selection &selection,
532  const property::DatasetTransferList &dtpl =
534 
545  template<typename T>
546  void read(T &data,
547  const datatype::Datatype &memory_type,
548  const dataspace::Dataspace &memory_space,
549  const dataspace::Selection &file_selection,
551 
562 
563  private:
564  datatype::Datatype file_type_;
565  datatype::Class file_type_class;
566  dataspace::DataspacePool space_pool;
573  static Node create_dataset(const Group &base,
574  const Path &path,
575  const datatype::Datatype &type,
576  const dataspace::Dataspace &space,
577  const property::LinkCreationList &lcpl,
578  const property::DatasetCreationList &dcpl,
579  const property::DatasetAccessList &dapl);
580 
581  //
582  // writing template methods for various data configurations
583  //
584  template<typename T>
585  void write_variable_length_data(const T &data,
586  const datatype::Datatype &mem_type,
587  const dataspace::Dataspace &mem_space,
588  const datatype::Datatype &,
589  const dataspace::Dataspace &file_space,
590  const property::DatasetTransferList &dtpl) const
591  {
592  VarLengthDataBuffer buffer;
594  if(H5Dwrite(static_cast<hid_t>(*this),
595  static_cast<hid_t>(mem_type),
596  static_cast<hid_t>(mem_space),
597  static_cast<hid_t>(file_space),
598  static_cast<hid_t>(dtpl),
599  buffer.data())<0)
600  {
601  std::stringstream ss;
602  ss<<"Failure to write variable length data to dataset ["<<link().path()<<"]!";
604  }
605  }
606 
607  template<typename T>
608  void write_contiguous_data(const T &data,
609  const datatype::Datatype &mem_type,
610  const dataspace::Dataspace &mem_space,
611  const datatype::Datatype &,
612  const dataspace::Dataspace &file_space,
613  const property::DatasetTransferList &dtpl) const
614  {
615  if(H5Dwrite(static_cast<hid_t>(*this),
616  static_cast<hid_t>(mem_type),
617  static_cast<hid_t>(mem_space),
618  static_cast<hid_t>(file_space),
619  static_cast<hid_t>(dtpl),
620  dataspace::cptr(data))<0)
621  {
622  std::stringstream ss;
623  ss<<"Failure to write contiguous data to dataset ["<<link().path()<<"]!";
625  }
626  }
627 
628  template<typename T>
629  void write_variable_length_string_data(const T &data,
630  const datatype::Datatype &mem_type,
631  const dataspace::Dataspace &mem_space,
632  const datatype::Datatype &,
633  const dataspace::Dataspace &file_space,
634  const property::DatasetTransferList &dtpl) const
635  {
636  using Trait = VarLengthStringTrait<T>;
637  auto buffer = Trait::to_buffer(data);
638 
639  if(H5Dwrite(static_cast<hid_t>(*this),
640  static_cast<hid_t>(mem_type),
641  static_cast<hid_t>(mem_space),
642  static_cast<hid_t>(file_space),
643  static_cast<hid_t>(dtpl),
644  reinterpret_cast<void*>(buffer.data()))<0)
645  {
646  std::stringstream ss;
647  ss<<"Failure to write variable length string data to dataset ["<<link().path()<<"]!";
649  }
650  }
651 
652  template<typename T>
653  void write_fixed_length_string_data(const T &data,
654  const datatype::Datatype &mem_type,
655  const dataspace::Dataspace &mem_space,
656  const datatype::Datatype &,
657  const dataspace::Dataspace &file_space,
658  const property::DatasetTransferList &dtpl) const
659  {
660  using Trait = FixedLengthStringTrait<T>;
661  auto buffer = Trait::to_buffer(data,mem_type,mem_space);
662 
663  if(H5Dwrite(static_cast<hid_t>(*this),
664  static_cast<hid_t>(mem_type),
665  static_cast<hid_t>(mem_space),
666  static_cast<hid_t>(file_space),
667  static_cast<hid_t>(dtpl),
668  reinterpret_cast<void*>(buffer.data()))<0)
669  {
670  std::stringstream ss;
671  ss<<"Failure to write fixed length string data to dataset ["<<link().path()<<"]!";
673  }
674 
675 
676  }
677 
678  //
679  // reading template methods for various data configurations
680  //
681  template<typename T>
682  void read_variable_length_data(T &data,
683  const datatype::Datatype &mem_type,
684  const dataspace::Dataspace &mem_space,
685  const datatype::Datatype &file_type,
686  const dataspace::Dataspace &file_space,
687  const property::DatasetTransferList &dtpl) const
688  {
689  VarLengthDataBuffer buffer;
690  if(file_space.selection.type() != dataspace::SelectionType::All)
691  {
692  buffer.resize(file_space.selection.size());
693  }
694  else
695  {
696  buffer.resize(signed2unsigned<size_t>(file_space.size()));
697  }
698 
699  if(H5Dread(static_cast<hid_t>(*this),
700  static_cast<hid_t>(mem_type),
701  static_cast<hid_t>(mem_space),
702  static_cast<hid_t>(file_space),
703  static_cast<hid_t>(dtpl),
704  buffer.data())<0)
705  {
706  std::stringstream ss;
707  ss<<"Failure to read variable length data from dataset ["<<link().path()<<"]!";
709  }
710 
712 
713  if(H5Dvlen_reclaim(static_cast<hid_t>(file_type),
714  static_cast<hid_t>(file_space),
715  static_cast<hid_t>(dtpl),
716  buffer.data())<0)
717  {
718  error::Singleton::instance().throw_with_stack("Error reclaiming variable length memory!");
719  }
720  }
721 
722  template<typename T>
723  void read_contiguous_data(T &data,
724  const datatype::Datatype &mem_type,
725  const dataspace::Dataspace &mem_space,
726  const datatype::Datatype &,
727  const dataspace::Dataspace &file_space,
728  const property::DatasetTransferList &dtpl) const
729  {
730  if(H5Dread(static_cast<hid_t>(*this),
731  static_cast<hid_t>(mem_type),
732  static_cast<hid_t>(mem_space),
733  static_cast<hid_t>(file_space),
734  static_cast<hid_t>(dtpl),
735  dataspace::ptr(data))<0)
736  {
737  std::stringstream ss;
738  ss<<"Failure to read contiguous data from dataset ["<<link().path()<<"]!";
740  }
741  }
742 
743  template<typename T>
744  void read_variable_length_string_data(T &data,
745  const datatype::Datatype &mem_type,
746  const dataspace::Dataspace &mem_space,
747  const datatype::Datatype &,
748  const dataspace::Dataspace &file_space,
749  const property::DatasetTransferList &dtpl) const
750  {
751  using Trait = VarLengthStringTrait<T>;
752  using size_type = typename std::vector<T>::size_type;
753 
754  typename Trait::BufferType buffer(static_cast<size_type>(mem_space.size()));
755 
756 
757  if(H5Dread(static_cast<hid_t>(*this),
758  static_cast<hid_t>(mem_type),
759  static_cast<hid_t>(mem_space),
760  static_cast<hid_t>(file_space),
761  static_cast<hid_t>(dtpl),
762  buffer.data())<0)
763  {
764  std::stringstream ss;
765  ss<<"Failure to read variable length string data from dataset ["
766  <<link().path()<<"]!";
768  }
769 
770  Trait::from_buffer(buffer,data);
771 
772  if(buffer.size() > 0)
773  {
774  if(H5Dvlen_reclaim(static_cast<hid_t>(mem_type),
775  static_cast<hid_t>(mem_space),
776  static_cast<hid_t>(dtpl),
777  buffer.data())<0)
778  {
779  std::stringstream ss;
780  ss<<"Error reclaiming memory from variable length string data in "
781  <<"dataset ["<<link().path()<<"]!";
783  }
784  }
785  }
786 
787  template<typename T>
788  void read_fixed_length_string_data(T &data,
789  const datatype::Datatype &mem_type,
790  const dataspace::Dataspace &mem_space,
791  const datatype::Datatype &,
792  const dataspace::Dataspace &file_space,
793  const property::DatasetTransferList &dtpl) const
794  {
795  using Trait = FixedLengthStringTrait<T>;
796 
797  auto buffer = Trait::BufferType::create(mem_type,mem_space);
798 
799  if(file_space.size() > 0)
800  {
801  if(H5Dread(static_cast<hid_t>(*this),
802  static_cast<hid_t>(mem_type),
803  static_cast<hid_t>(mem_space),
804  static_cast<hid_t>(file_space),
805  static_cast<hid_t>(dtpl),
806  buffer.data())<0)
807  {
808  std::stringstream ss;
809  ss<<"Failure to read fixed length string data to dataset ["<<link().path()<<"]!";
811  }
812 
813  //get data out of the buffer
814  data = Trait::from_buffer(buffer,mem_type,mem_space);
815  }
816  }
817 };
818 #ifdef __clang__
819 #pragma clang diagnostic pop
820 #endif
821 
822 template<typename T>
823 void Dataset::write(const T &data,const datatype::Datatype &mem_type,
824  const dataspace::Dataspace &mem_space,
825  const dataspace::Dataspace &file_space,
826  const property::DatasetTransferList &dtpl) const
827 {
828  if(file_type_class == datatype::Class::VarLength)
829  {
830  write_variable_length_data(data,mem_type,mem_space,file_type_,file_space,dtpl);
831  }
832  else if(file_type_class == datatype::Class::String)
833  {
834  datatype::String string_type(file_type_);
835  if(string_type.is_variable_length())
836  {
837  write_variable_length_string_data(data,mem_type,mem_space,file_type_,file_space,dtpl);
838  }
839  else
840  {
841  write_fixed_length_string_data(data,mem_type,mem_space,file_type_,file_space,dtpl);
842  }
843  }
844  else
845  {
846  write_contiguous_data(data,mem_type,mem_space,file_type_,file_space,dtpl);
847  }
848 
849 }
850 
851 template<typename T>
852 void Dataset::write(const T &data,
853  const datatype::Datatype &mem_type,
854  const dataspace::Dataspace &mem_space,
855  const property::DatasetTransferList &dtpl) const
856 {
857  auto file_space = dataspace();
858  file_space.selection.all();
859 
860  write(data,mem_type,mem_space,file_space,dtpl);
861 
862 }
863 
864 template<typename T>
865 void Dataset::write_chunk(const T &data,
866  std::vector<hsize_t> offset,
867  std::uint32_t filter_mask,
868  const property::DatasetTransferList &dtpl)
869 {
870  hdf5::datatype::DatatypeHolder mem_type_holder;
871  hdf5::dataspace::DataspaceHolder mem_space_holder(space_pool);
872  write_chunk(data, mem_type_holder.get(data), mem_space_holder.get(data), offset, filter_mask, dtpl);
873 }
874 
875 template<typename T>
876 void Dataset::write_chunk(const T &data,
877  std::vector<hsize_t> offset,
878  std::uint32_t filter_mask,
879  const property::DatasetTransferList &dtpl) const
880 {
881  hdf5::datatype::DatatypeHolder mem_type_holder;
882  hdf5::dataspace::DataspaceHolder mem_space_holder;
883  write_chunk(data, mem_type_holder.get(data), mem_space_holder.get(data), offset, filter_mask, dtpl);
884 }
885 
886 template<typename T>
887 void Dataset::write_chunk(const T &data,
888  const datatype::Datatype &mem_type,
889  const dataspace::Dataspace &mem_space,
890  std::vector<hsize_t> & offset,
891  std::uint32_t filter_mask,
892  const property::DatasetTransferList &dtpl) const
893 {
894  size_t databytesize = signed2unsigned<hsize_t>(mem_space.size()) * mem_type.size();
895 
896  if(mem_type.get_class() == datatype::Class::Integer)
897  {
898 #if H5_VERSION_GE(1,10,3)
899  if(H5Dwrite_chunk(static_cast<hid_t>(*this),
900  static_cast<hid_t>(dtpl),
901  filter_mask,
902  offset.data(),
903  databytesize,
904  dataspace::cptr(data))<0)
905  {
906  std::stringstream ss;
907  ss<<"Failure to write chunk data to dataset ["<<link().path()<<"]!";
909  }
910 #else
911  if(H5DOwrite_chunk(static_cast<hid_t>(*this),
912  static_cast<hid_t>(dtpl),
913  filter_mask,
914  offset.data(),
915  databytesize,
916  dataspace::cptr(data))<0)
917  {
918  std::stringstream ss;
919  ss<<"Failure to write chunk data to dataset ["<<link().path()<<"]!";
921  }
922 #endif
923  }
924  else
925  {
926  std::stringstream ss;
927  ss<<"Failure to write non-integer chunk data to dataset ["<<link().path()<<"]!";
929  }
930 }
931 
932 #if H5_VERSION_GE(1,10,2)
933 
934 template<typename T>
935 std::uint32_t Dataset::read_chunk(T &data,
936  std::vector<hsize_t> offset,
937  const property::DatasetTransferList &dtpl) const
938 {
939  hdf5::datatype::DatatypeHolder mem_type_holder;
940  return read_chunk(data, mem_type_holder.get(data), offset, dtpl);
941 }
942 
943 template<typename T>
944 std::uint32_t Dataset::read_chunk(T &data,
945  const datatype::Datatype &mem_type,
946  std::vector<hsize_t> & offset,
947  const property::DatasetTransferList &dtpl) const
948 {
949  std::uint32_t filter_mask;
950  if(mem_type.get_class() == datatype::Class::Integer)
951  {
952 #if H5_VERSION_GE(1,10,3)
953  if(H5Dread_chunk(static_cast<hid_t>(*this),
954  static_cast<hid_t>(dtpl),
955  offset.data(),
956  &filter_mask,
957  dataspace::ptr(data))<0)
958  {
959  std::stringstream ss;
960  ss<<"Failure to read chunk data from dataset ["<<link().path()<<"]!";
962  }
963 #else
964  if(H5DOread_chunk(static_cast<hid_t>(*this),
965  static_cast<hid_t>(dtpl),
966  offset.data(),
967  &filter_mask,
968  dataspace::ptr(data))<0)
969  {
970  std::stringstream ss;
971  ss<<"Failure to read chunk data from dataset ["<<link().path()<<"]!";
973  }
974 #endif
975  }
976  else
977  {
978  std::stringstream ss;
979  ss<<"Failure to read non-integer chunk data from dataset ["<<link().path()<<"]!";
981  }
982  return filter_mask;
983 }
984 
985 #endif
986 
987 template<typename T>
988 void Dataset::write(const T &data,const property::DatasetTransferList &dtpl)
989 {
990  hdf5::datatype::DatatypeHolder mem_type_holder;
991  hdf5::dataspace::DataspaceHolder mem_space_holder(space_pool);
992  write_reshape(data, mem_type_holder.get(data), mem_space_holder.get(data), dtpl);
993 }
994 
995 template<typename T>
996 void Dataset::write(const T &data,const property::DatasetTransferList &dtpl) const
997 {
998  hdf5::datatype::DatatypeHolder mem_type_holder;
999  hdf5::dataspace::DataspaceHolder mem_space_holder;
1000  write_reshape(data, mem_type_holder.get(data), mem_space_holder.get(data), dtpl);
1001 }
1002 
1003 template<typename T>
1004 void Dataset::write_reshape(const T &data,
1005  const datatype::Datatype &mem_type,
1006  const dataspace::Dataspace &mem_space,
1007  const property::DatasetTransferList &dtpl) const
1008 {
1009  auto file_space = dataspace();
1010  file_space.selection.all();
1011 
1012  if (file_space.size() == mem_space.size() &&
1013  mem_space.type() == dataspace::Type::Simple &&
1014  file_space.type() == dataspace::Type::Simple){
1015  const dataspace::Simple & mem_simple_space = dataspace::Simple(mem_space);
1016  const dataspace::Simple & file_simple_space = dataspace::Simple(file_space);
1017  if(file_simple_space.rank() > 1 && mem_simple_space.rank() == 1){
1018  return write(data,mem_type,file_space,file_space,dtpl);
1019  }
1020  }
1021  write(data,mem_type,mem_space,file_space,dtpl);
1022 }
1023 
1024 
1025 template<typename T>
1026 void Dataset::read(T &data,const datatype::Datatype &mem_type,
1027  const dataspace::Dataspace &mem_space,
1028  const dataspace::Dataspace &file_space,
1029  const property::DatasetTransferList &dtpl) const
1030 {
1031  if(file_type_class == datatype::Class::VarLength)
1032  {
1033  read_variable_length_data(data,mem_type,mem_space,file_type_,file_space,dtpl);
1034  }
1035  else if(file_type_class == datatype::Class::String)
1036  {
1037  datatype::String string_type(file_type_);
1038  if(string_type.is_variable_length())
1039  {
1040  read_variable_length_string_data(data,mem_type,mem_space,file_type_,file_space,dtpl);
1041  }
1042  else
1043  {
1044  read_fixed_length_string_data(data,mem_type,mem_space,file_type_,file_space,dtpl);
1045  }
1046  }
1047  else
1048  {
1049  read_contiguous_data(data,mem_type,mem_space,file_type_,file_space,dtpl);
1050  }
1051 
1052 }
1053 
1054 template<typename T>
1055 void Dataset::read(T &data,const dataspace::Selection &selection,
1056  const property::DatasetTransferList &dtpl)
1057 {
1058  hdf5::datatype::DatatypeHolder mem_type_holder;
1059  hdf5::dataspace::DataspaceHolder mem_space_holder(space_pool);
1060  read_reshape(data, mem_type_holder.get(data), mem_space_holder.get(data), selection, dtpl);
1061 }
1062 
1063 template<typename T>
1064 void Dataset::read(T &data,const dataspace::Selection &selection,
1065  const property::DatasetTransferList &dtpl) const
1066 {
1067  hdf5::datatype::DatatypeHolder mem_type_holder;
1068  hdf5::dataspace::DataspaceHolder mem_space_holder;
1069  read_reshape(data, mem_type_holder.get(data), mem_space_holder.get(data), selection, dtpl);
1070 }
1071 
1072 template<typename T>
1074  const datatype::Datatype &mem_type,
1075  const dataspace::Dataspace &mem_space,
1076  const dataspace::Selection &selection,
1077  const property::DatasetTransferList &dtpl) const
1078 {
1079  dataspace::Dataspace file_space = dataspace();
1080  file_space.selection(dataspace::SelectionOperation::Set,selection);
1081  if (selection.type() != dataspace::SelectionType::Hyperslab)
1082  return read(data,mem_type,mem_space,file_space,dtpl);
1083  try{
1084  const dataspace::Hyperslab & hyper = dynamic_cast<const dataspace::Hyperslab &>(selection);
1085  if(hyper.rank() > 1) {
1086  dataspace::Simple selected_space(hyper.dimensions());
1087  if (selected_space.size() == mem_space.size()) {
1088  // reads to the reshaped memory data buffer
1089  return read(data,mem_type,selected_space,file_space,dtpl);
1090  }
1091  }
1092  }
1093  catch(const std::bad_cast&) { }
1094  read(data,mem_type,mem_space,file_space,dtpl);
1095 }
1096 
1097 template<typename T>
1098 void Dataset::read(T &data,
1099  const datatype::Datatype &memory_type,
1100  const dataspace::Dataspace &memory_space,
1101  const dataspace::Selection &file_selection,
1102  const property::DatasetTransferList &dtpl) const
1103 {
1104  dataspace::Dataspace file_space = dataspace();
1105  file_space.selection(dataspace::SelectionOperation::Set,file_selection);
1106  read(data,memory_type,memory_space,file_space,dtpl);
1107 }
1108 
1109 template<typename T>
1110 void Dataset::read(T &data,
1111  const datatype::Datatype &memory_type,
1112  const dataspace::Dataspace &memory_space,
1113  const property::DatasetTransferList &dtpl) const
1114 {
1115  dataspace::Dataspace file_space = dataspace();
1116  file_space.selection.all();
1117  read(data,memory_type,memory_space,file_space,dtpl);
1118 }
1119 
1120 template<typename T>
1121 void Dataset::write(const T &data,const dataspace::Selection &selection,
1122  const property::DatasetTransferList &dtpl)
1123 {
1124  hdf5::datatype::DatatypeHolder mem_type_holder;
1125  hdf5::dataspace::DataspaceHolder mem_space_holder(space_pool);
1126  write_reshape(data, mem_type_holder.get(data), mem_space_holder.get(data), selection, dtpl);
1127 }
1128 
1129 template<typename T>
1130 void Dataset::write(const T &data,const dataspace::Selection &selection,
1131  const property::DatasetTransferList &dtpl) const
1132 {
1133  hdf5::datatype::DatatypeHolder mem_type_holder;
1134  hdf5::dataspace::DataspaceHolder mem_space_holder;
1135  write_reshape(data, mem_type_holder.get(data), mem_space_holder.get(data), selection, dtpl);
1136 }
1137 
1138 template<typename T>
1139 void Dataset::write_reshape(const T &data,
1140  const datatype::Datatype &mem_type,
1141  const dataspace::Dataspace &mem_space,
1142  const dataspace::Selection &selection,
1143  const property::DatasetTransferList &dtpl) const
1144 {
1145  dataspace::Dataspace file_space = dataspace();
1146  file_space.selection(dataspace::SelectionOperation::Set,selection);
1147  if (mem_space.type() != dataspace::Type::Simple
1148  || selection.type() != dataspace::SelectionType::Hyperslab)
1149  return write(data,mem_type,mem_space,file_space,dtpl);
1150  try{
1151  const dataspace::Hyperslab & hyper = dynamic_cast<const dataspace::Hyperslab &>(selection);
1152  if(hyper.rank() > 1) {
1153  const dataspace::Simple & mem_simple_space = dataspace::Simple(mem_space);
1154  dataspace::Simple selected_space(hyper.dimensions());
1155  if(selected_space.rank() > 1 &&
1156  mem_simple_space.rank() == 1 &&
1157  selected_space.size() == mem_space.size()) {
1158  // writes from the reshaped memory data buffer
1159  return write(data,mem_type,selected_space,file_space,dtpl);
1160  }
1161  }
1162  }
1163  catch(const std::bad_cast&) { }
1164  write(data,mem_type,mem_space,file_space,dtpl);
1165 }
1166 
1167 
1168 template<typename T>
1170 {
1171  hdf5::dataspace::DataspaceHolder mem_space_holder(space_pool);
1172  if(file_type_class == datatype::Class::String){
1173  // in hdf5 1.12.1 UFT8 data cannot be read to an ASCII buffer
1174  read_reshape(data, file_type_, mem_space_holder.get(data), dtpl);
1175  }
1176  else {
1177  hdf5::datatype::DatatypeHolder mem_type_holder;
1178  read_reshape(data, mem_type_holder.get(data), mem_space_holder.get(data), dtpl);
1179  }
1180 }
1181 
1182 template<typename T>
1183 void Dataset::read(T &data,const property::DatasetTransferList &dtpl) const
1184 {
1185  hdf5::datatype::DatatypeHolder mem_type_holder;
1186  hdf5::dataspace::DataspaceHolder mem_space_holder;
1187  read_reshape(data, mem_type_holder.get(data), mem_space_holder.get(data), dtpl);
1188 }
1189 
1190 
1191 template<typename T>
1193  const datatype::Datatype &mem_type,
1194  const dataspace::Dataspace &mem_space,
1195  const property::DatasetTransferList &dtpl) const
1196 {
1197  auto file_space = dataspace();
1198  file_space.selection.all();
1199 
1200  if (file_space.size() == mem_space.size()){
1201  read(data,mem_type,file_space,file_space,dtpl);
1202  }
1203  else{
1204  read(data,mem_type,mem_space,file_space,dtpl);
1205  }
1206 
1207 }
1208 
1209 
1222 DLL_EXPORT void resize_by(const Dataset &dataset,size_t dimension_index,ssize_t delta);
1223 
1224 
1225 
1226 } // namespace node
1227 } // namespace hdf5
path to a node object
Definition: path.hpp:54
data space object holder
Definition: type_trait.hpp:124
const Dataspace & get(const T &v)
factory holder method for getting reference of data spaces
Definition: type_trait.hpp:158
data space object pool
Definition: pool.hpp:42
dataspace base class
Definition: dataspace.hpp:41
SelectionManager selection
access to selection manager
Definition: dataspace.hpp:144
Type type() const
get the type of the dataspace
virtual hssize_t size() const
number of elements in the dataspace
hyperslab selection class
Definition: hyperslab.hpp:52
size_t rank() const noexcept
get rank
virtual Dimensions dimensions() const override
get current dimensions
scalar dataspace
Definition: scalar.hpp:44
void all() const
select everything
selection base class
Definition: selection.hpp:42
virtual SelectionType type() const =0
get the selection type
simple multidimensional dataspace
Definition: simple.hpp:43
size_t rank() const
get number of dimensions
data type object holder
Definition: factory.hpp:54
const Datatype & get(const T &v=T{})
factory holder method for getting reference of data types
Definition: factory.hpp:74
base class for all data types
Definition: datatype.hpp:42
Class get_class() const
returns the datatypes class
virtual size_t size() const
get size of type in bytes
string datatype
Definition: string.hpp:40
bool is_variable_length() const
return true if type is a variable length string
static Singleton & instance()
reference to singleton
Definition: error.hpp:59
void throw_with_stack(const std::string &message)
throws an exception, potentially nested with error stack
utility container for external filters
Definition: external_filter.hpp:123
Definition: dataset.hpp:60
hsize_t chunk_storage_size(std::vector< hsize_t > offset) const
read dataset chunk
void write(const T &data, const datatype::Datatype &mem_type, const dataspace::Dataspace &mem_space, const dataspace::Dataspace &file_space, const property::DatasetTransferList &dtpl=property::DatasetTransferList::get()) const
write data to the dataset
Definition: dataset.hpp:823
void read_reshape(T &data, const datatype::Datatype &mem_type, const dataspace::Dataspace &mem_space, const property::DatasetTransferList &dtpl=property::DatasetTransferList::get()) const
Definition: dataset.hpp:1192
void write(const char *data, const property::DatasetTransferList &dtpl=property::DatasetTransferList::get()) const
dataspace::Dataspace dataspace() const
get dataspace of dataset
void extent(const Dimensions &dims) const
set extent of the dataset
void refresh() const
refresh the dataset (since hdf5 1.10.0)
property::DatasetAccessList access_list() const
get the dataset access property list for the instance
void write_chunk(const T &data, std::vector< hsize_t > offset, std::uint32_t filter_mask=0, const property::DatasetTransferList &dtpl=property::DatasetTransferList::get())
write dataset chunk
Definition: dataset.hpp:865
Dataset()=default
default constructor
void resize(const Dimensions &dims) const
resize the dataset
std::uint32_t read_chunk(T &data, const datatype::Datatype &mem_type, std::vector< hsize_t > &offset, const property::DatasetTransferList &dtpl=property::DatasetTransferList::get()) const
read dataset chunk
datatype::Datatype datatype() const
get datatype of dataset
Dataset(const Node &node)
construct
void extent(size_t dim, ssize_t delta_elements) const
void write(const char *data, const property::DatasetTransferList &dtpl=property::DatasetTransferList::get())
write entire dataset
Dataset(const Group &base, const Path &path, const datatype::Datatype &type, const dataspace::Dataspace &space=dataspace::Scalar(), const property::LinkCreationList &lcpl=property::LinkCreationList(), const property::DatasetCreationList &dcpl=property::DatasetCreationList(), const property::DatasetAccessList &dapl=property::DatasetAccessList())
constructor
property::DatasetCreationList creation_list() const
get the dataset creation property list used for creation
std::uint32_t read_chunk(T &data, std::vector< hsize_t > offset, const property::DatasetTransferList &dtpl=property::DatasetTransferList::get()) const
read dataset chunk (since hdf5 1.10.2)
void read(T &data, const datatype::Datatype &mem_type, const dataspace::Dataspace &mem_space, const dataspace::Dataspace &file_space, const property::DatasetTransferList &dtpl=property::DatasetTransferList::get()) const
read data from the dataset
Definition: dataset.hpp:1026
filter::ExternalFilters filters() const
get the dataset external filters for the instance
void write_reshape(const T &data, const datatype::Datatype &mem_type, const dataspace::Dataspace &mem_space, const property::DatasetTransferList &dtpl=property::DatasetTransferList::get()) const
write entire dataset
Definition: dataset.hpp:1004
Definition: group.hpp:51
Definition: node.hpp:40
const Link & link() const
return link to object
dataset access property list
Definition: dataset_access.hpp:76
dataset creation property list
Definition: dataset_creation.hpp:111
class for a dataset transfer property list
Definition: dataset_transfer.hpp:57
static const DatasetTransferList & get()
reference to const static DatasetTransferList object
Definition: dataset_transfer.hpp:88
Definition: link_creation.hpp:35
const void * cptr(const T &value)
Definition: type_trait.hpp:116
void * ptr(T &value)
Definition: type_trait.hpp:111
TypeTrait< T >::DataspaceType create(const T &value)
factory function for dataspaces
Definition: type_trait.hpp:89
Class
Definition: types.hpp:53
@ String
indicates a string type
@ VarLength
indicates a variable length type
@ Integer
indicates an integer type
File from_buffer(T &data, ImageFlags flags=ImageFlags::ReadOnly)
load an image file from a buffer
Definition: functions.hpp:126
void resize_by(const Dataset &dataset, size_t dimension_index, ssize_t delta)
resize a dataset by a particular offset
void link(const Node &target, const Group &link_base, const Path &link_path, const property::LinkCreationList &lcpl=property::LinkCreationList(), const property::LinkAccessList &lapl=property::LinkAccessList())
Create a soft or external link.
top-level namespace of the entire library
Definition: attribute.hpp:45
std::vector< hsize_t > Dimensions
Definition: types.hpp:32
std::vector< hvl_t > VarLengthDataBuffer
Definition: types.hpp:34
static void to_buffer(const T &, VarLengthDataBuffer &)
Definition: types.hpp:45
static void from_buffer(const VarLengthDataBuffer &, T &)
Definition: types.hpp:48
#define DLL_EXPORT
Definition: windows.hpp:29