h5_read (h5::group_or_file f, std::string const & name, S & A) { if (!f.exists(name)) TRIQS_RUNTIME_ERROR << "no such dataset : "<<name <<" in file "; try { DataSet ds = f->openDataSet( name.c_str() ); DataSpace dataspace = ds.getSpace(); int rank = dataspace.getSimpleExtentNdims(); if (rank != 0) TRIQS_RUNTIME_ERROR << "triqs::array::h5::read. Rank mismatch : expecting a scalar (rank =0)" <<" while the array stored in the hdf5 file has rank = "<<rank; ds.read( (void *)(&A), data_type_mem_scalar(A), DataSpace() , DataSpace() ); } TRIQS_ARRAYS_H5_CATCH_EXCEPTION; }
/** * \brief Read a string from an hdf5 file * \param f The h5 file or group of type H5::H5File or H5::Group * \param name The name of the hdf5 array in the file/group where the stack will be stored * \param value The string to fill * \exception The HDF5 exceptions will be caught and rethrown as TRIQS_RUNTIME_ERROR (with a full stackstrace, cf triqs doc). */ inline void h5_read (h5::group_or_file f, std::string const & name, std::string & value) { if (!f.exists(name)) TRIQS_RUNTIME_ERROR << "no such dataset : "<<name <<" in file "; try { DataSet ds = f->openDataSet( name.c_str() ); DataSpace dataspace = ds.getSpace(); int rank = dataspace.getSimpleExtentNdims(); if (rank != 0) TRIQS_RUNTIME_ERROR << "Reading a string and got rank !=0"; size_t size = ds.getStorageSize(); StrType strdatatype(PredType::C_S1, size); std::vector<char> buf(size+1, 0x00); ds.read( (void *)(&buf[0]), strdatatype, DataSpace(), DataSpace() ); value = ""; value.append( &(buf.front()) ); } TRIQS_ARRAYS_H5_CATCH_EXCEPTION; }
inline DataSpace DataSpace::From(const boost::multi_array<Value, Dims> & container){ std::vector<size_t> dims(Dims); for(std::size_t i = 0; i < Dims; ++i){ dims[i] = container.shape()[i]; } return DataSpace(dims); }
/** * \brief Write a string into an hdf5 file * \param f The h5 file or group of type H5::H5File or H5::Group * \param name The name of the hdf5 array in the file/group where the stack will be stored * \param value The string * \exception The HDF5 exceptions will be caught and rethrown as TRIQS_RUNTIME_ERROR (with a full stackstrace, cf triqs doc). */ inline void h5_write (h5::group_or_file f, std::string const & name, std::string const & value) { try { DataSet ds; StrType strdatatype(PredType::C_S1, value.size()); ds = f->createDataSet( name.c_str(), strdatatype, DataSpace() ); ds.write((void*)(value.c_str()), strdatatype ); } TRIQS_ARRAYS_H5_CATCH_EXCEPTION; }
inline void write_attribute ( DataSet & dataset, std::string name, std::string value ) { DataSpace attr_dataspace = DataSpace(H5S_SCALAR); // Create new string datatype for attribute StrType strdatatype(PredType::C_S1, value.size()); // Set up write buffer for attribute const H5std_string strwritebuf (value); // Create attribute and write to it Attribute myatt_in = dataset.createAttribute(name, strdatatype, attr_dataspace); myatt_in.write(strdatatype, strwritebuf); }
inline DataSpace DataSpace::From(const std::vector<Value> & container){ return DataSpace(details::get_dim_vector<Value>(container)); }
DataSpace DataSet::getSpace () const { return DataSpace (Exception::check ("H5Dget_space", H5Dget_space (handle ()))); }
int WriteHDF5DoubleAttribute(string filename , string datasetname, string attr_name, int attr_dims, double* attr_data) { hsize_t dims[1] = { attr_dims }; // Try block to detect exceptions raised by any of the calls inside it try { // Turn off the auto-printing when failure occurs so that we can // handle the errors appropriately Exception::dontPrint(); // Open an existing file and dataset. H5File file(filename, H5F_ACC_RDWR ); DataSet dataset = file.openDataSet(datasetname); // Create the data space for the attribute. DataSpace attr_dataspace = DataSpace (1, dims ); // Create a dataset attribute. Attribute attribute = dataset.createAttribute(attr_name, PredType::NATIVE_DOUBLE, attr_dataspace, PropList::DEFAULT); // Write the attribute data. attribute.write( PredType::NATIVE_DOUBLE, attr_data); } // end of try block // catch failure caused by the H5File operations catch( DataSpaceIException error ) { error.printError(); return -1; } // catch failure caused by the H5File operations catch( AttributeIException error ) { error.printError(); return -1; } // catch failure caused by the H5File operations catch( FileIException error ) { error.printError(); return -1; } // catch failure caused by the DataSet operations catch( DataSetIException error ) { error.printError(); return -1; } return 0; // successfully terminated }