void WriteStringsT( hid_t iParent, const std::string &iAttrName, size_t iNumStrings, const StringT *iStrings ) { // Validate ABCA_ASSERT( iNumStrings > 0, "Degenerate num strings in WriteStringsT" ); ABCA_ASSERT( iStrings, "Degenerate strings buffer in WriteStringsT" ); // Compact the strings. std::vector<CharT> charBuffer; CompactStrings( iStrings, iNumStrings, charBuffer ); // Create the dataspace. size_t len = charBuffer.size(); assert( len >= iNumStrings ); Dimensions dims( len ); HDimensions hdims( dims ); hid_t dspaceId = H5Screate_simple( hdims.rank(), hdims.rootPtr(), NULL ); DspaceCloser dspaceCloser( dspaceId ); ABCA_ASSERT( dspaceId >= 0, "WriteStringsT() Failed in dataspace constructor" ); // Create the attribute. WriteDataToAttr( iParent, dspaceId, iAttrName, GetFileDtype<CharT>(), GetNativeDtype<CharT>(), ( const void * )&charBuffer.front() ); // That's it! }
//-***************************************************************************** static void getDatatypeArrayDims( hid_t iDtype, Dimensions &dims ) { ABCA_ASSERT( iDtype >= 0, "Invalid datatype in getDatatypeArrayDims" ); int ndims = H5Tget_array_ndims( iDtype ); ABCA_ASSERT( ndims > 0, "getDatatypeArrayDims() H5Tget_array_ndims failed" ); HDimensions hdims( ( size_t )ndims ); ndims = H5Tget_array_dims2( iDtype, hdims.rootPtr() ); ABCA_ASSERT( ndims > 0, "getDatatypeArrayDims() H5Tget_array_dims failed" ); ABCA_ASSERT( ndims == hdims.rank(), "getDatatypeArrayDims() inconsistent ranks" ); dims = hdims; }
//-***************************************************************************** void WriteSmallArray( hid_t iParent, const std::string &iAttrName, hid_t iFileType, hid_t iNativeType, size_t iNumVals, const void *iData ) { Dimensions dims( iNumVals ); HDimensions hdims( dims ); size_t npoints = hdims.numPoints(); ABCA_ASSERT( npoints > 0, "Cannot create degenerate dataspace" ); hid_t dspaceId = H5Screate_simple( hdims.rank(), hdims.rootPtr(), NULL ); DspaceCloser dspaceCloser( dspaceId ); WriteDataToAttr( iParent, dspaceId, iAttrName, iFileType, iNativeType, iData ); }
//-***************************************************************************** //-***************************************************************************** // GZIP COMPRESSION FOR DATASETS //-***************************************************************************** //-***************************************************************************** hid_t DsetGzipCreatePlist( const Dimensions &dims, int level ) { herr_t status; hid_t ID = H5Pcreate( H5P_DATASET_CREATE ); ABCA_ASSERT( ID >= 0, "DsetGzipCreatePlist: H5Pcreate failed" ); // Chunking. HDimensions hdims( dims ); status = H5Pset_chunk( ID, hdims.rank(), hdims.rootPtr() ); ABCA_ASSERT( status >= 0, "DsetGzipCreatePlist: " "H5Pset_chunk() failed" ); level = level < 0 ? 0 : level > 9 ? 9 : level; status = H5Pset_deflate( ID, ( unsigned int )level ); ABCA_ASSERT( status >= 0, "DsetGzipCreatePlist: " "H5Pset_link_creation_order() failed" ); return ID; }
void WriteStringT( hid_t iParent, const std::string &iAttrName, const StringT &iString ) { // Verify that no NULL characters have been hidden in the string. CharT NullChar = ( CharT )0; ABCA_ASSERT( iString.find( NullChar ) == StringT::npos, "Illegal NULL character found in string in WriteStringT" ); // Create the dataspace. size_t len = iString.length(); Dimensions dims( len + 1 ); HDimensions hdims( dims ); size_t npoints = hdims.numPoints(); ABCA_ASSERT( npoints > 0, "Cannot create degenerate dataspace" ); hid_t dspaceId = H5Screate_simple( hdims.rank(), hdims.rootPtr(), NULL ); DspaceCloser dspaceCloser( dspaceId ); // Get the data. const CharT *data; if ( len == 0 ) { data = &NullChar; } else { data = iString.c_str(); } // Write into it. WriteDataToAttr( iParent, dspaceId, iAttrName, GetFileDtype<CharT>(), GetNativeDtype<CharT>(), ( const void * )data ); }
WrittenArraySampleIDPtr WriteStringArrayT( WrittenArraySampleMap &iMap, hid_t iGroup, const std::string &iName, const AbcA::ArraySample &iSamp, const AbcA::ArraySample::Key &iKey, int iCompressionLevel ) { // because strings are packed together, always write out the dimensions Dimensions dims = iSamp.getDimensions(); ABCA_ASSERT( dims.rank() > 0, "String type can not have a rank-0 array sample" ); std::string dimsName = iName + ".dims"; WriteDimensions( iGroup, dimsName, dims ); // See whether or not we've already stored this. WrittenArraySampleIDPtr writeID = iMap.find( iKey ); if ( writeID ) { CopyWrittenArray( iGroup, iName, writeID ); return writeID; } // Okay, need to actually store it. // It will be a dataset with an internal attribute for storing // the hash id. bool hasData = dims.numPoints() > 0; hid_t dspaceId = -1; Dimensions wdims; // Used to store the "fake" dimensions. std::vector<CharT> charBuffer; // Get the dimensions, validate sample size. if ( hasData ) { size_t extent = iSamp.getDataType().getExtent(); size_t numStrings = dims.numPoints() * extent; ABCA_ASSERT( dims.rank() > 0 && numStrings > 0, "Degenerate array sample in WriteStringArrayT" ); // Get the data out of the array sample. const StringT *strings = reinterpret_cast<const StringT *>( iSamp.getData() ); ABCA_ASSERT( strings, "Degenerate strings in WriteStringArrayT" ); // Compact the strings in the string array. CompactStrings( strings, numStrings, charBuffer ); // Create the dataspace. size_t len = charBuffer.size(); assert( len >= numStrings ); wdims = Dimensions( len ); HDimensions hdims( wdims ); dspaceId = H5Screate_simple( hdims.rank(), hdims.rootPtr(), NULL ); } else { dspaceId = H5Screate( H5S_NULL ); } ABCA_ASSERT( dspaceId >= 0, "WriteStringsT() Failed in dataspace constructor" ); DspaceCloser dspaceCloser( dspaceId ); hid_t dsetId = -1; if ( iCompressionLevel >= 0 && hasData ) { // Make a compression plist hid_t zipPlist = DsetGzipCreatePlist( wdims, iCompressionLevel > 9 ? 9 : iCompressionLevel ); PlistCloser plistCloser( zipPlist ); //std::cout << "Creating compressed data set named: " // << iName << " in group named: " << iGroup.name() // << std::endl; // Make the dataset. dsetId = H5Dcreate2( iGroup, iName.c_str(), GetFileDtype<CharT>(), dspaceId, H5P_DEFAULT, zipPlist, H5P_DEFAULT ); } else { //std::cout << "Creating uncompressed data set named: " // << iName << " in group named: " << iGroup.name() // << std::endl; dsetId = H5Dcreate2( iGroup, iName.c_str(), GetFileDtype<CharT>(), dspaceId, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT ); } DsetCloser dsetCloser(dsetId); ABCA_ASSERT( dsetId >= 0, "WriteArray() Failed in dataset constructor" ); // Write the data. if ( hasData ) { H5Dwrite( dsetId, GetNativeDtype<CharT>(), H5S_ALL, H5S_ALL, H5P_DEFAULT, &charBuffer.front() ); } // Write the key WriteKey( dsetId, "key", iKey ); writeID.reset( new WrittenArraySampleID( iKey, dsetId ) ); iMap.store( writeID ); // Return the reference. return writeID; }