コード例 #1
0
ファイル: attribute.hpp プロジェクト: jb--/h5xx
inline typename boost::enable_if<boost::is_same<T, std::string>, T>::type
read_attribute(H5::H5Object const& object, std::string const& name)
{
    H5::Attribute attr;
    try {
        H5XX_NO_AUTO_PRINT(H5::AttributeIException);
        attr = object.openAttribute(name);
    }
    catch (H5::AttributeIException const&) {
        throw;
    }
    if (!has_scalar_space(attr)) {
        throw H5::AttributeIException("H5::attribute::as", "incompatible dataspace");
    }
    H5::DataType tid = attr.getDataType();
    std::string value;
    if (!tid.isVariableStr()) {
        // read fixed-size string, allocate space in advance and let the HDF5
        // library take care about NULLTERM and NULLPAD strings
        value.resize(tid.getSize(), std::string::value_type());
        attr.read(tid, &*value.begin());
    }
    else {
        // read variable-length string, memory will be allocated by HDF5 C
        // library and must be freed by us
        char *c_str;
        if (H5Aread(attr.getId(), tid.getId(), &c_str) < 0) {
            throw H5::AttributeIException("Attribute::read", "H5Aread failed");
        }
        value = c_str;  // copy '\0'-terminated string
        free(c_str);
    }
    return value;
}
コード例 #2
0
ファイル: loadV3dFFMpeg.cpp プロジェクト: Vaa3D/v3d_external
bool loadStackHDF5( const char* fileName, Image4DSimple& img )
{
#ifdef USE_HDF5
    H5::Exception::dontPrint();
    H5::H5File file( fileName, H5F_ACC_RDONLY );

    for ( size_t i = 0; i < file.getObjCount(); i++ )
    {
        H5std_string name = file.getObjnameByIdx( i );
        if ( name == "Channels" )
        {
            H5::Group channels = file.openGroup( name );

            // Grab the attributes
            H5::Attribute attr = channels.openAttribute( "width" );
            H5::DataType type = attr.getDataType();
            long width, height;
            attr.read( type, &width );
            attr.close();

            attr = channels.openAttribute( "height" );
            attr.read( type, &height );
            attr.close();

            int num_channels = 0;
            // Count the number of channels
            for ( size_t obj = 0; obj < channels.getNumObjs(); obj++ )
                if ( channels.getObjTypeByIdx( obj ) == H5G_DATASET )
                    num_channels++;

            int channel_idx = 0;
            for ( size_t obj = 0; obj < channels.getNumObjs(); obj++ )
            {
                if ( channels.getObjTypeByIdx( obj ) == H5G_DATASET )
                {
                    H5std_string ds_name = channels.getObjnameByIdx( obj );
                    H5::DataSet data = channels.openDataSet( ds_name );
                    uint8_t* buffer = new uint8_t[ data.getStorageSize() ];
                    data.read( buffer, data.getDataType() );
                    QByteArray qbarray( ( const char* )buffer, data.getStorageSize() );
                    data.close();

                    if ( !loadIndexedStackFFMpeg( &qbarray, img, channel_idx++, num_channels,
                                                  width, height ) )
                    {
                        v3d_msg( "Error happened in HDF file reading. Stop. \n", false );
                        return false;
                    }

                    delete [] buffer;
                }
            }
        }
    }

#endif

    return true;
}
コード例 #3
0
ファイル: DataSetIO.cpp プロジェクト: arushanova/oxsx
OXSXDataSet
DataSetIO::LoadDataSet(const std::string& filename_){
    // Get Data Set
    H5::H5File  file(filename_, H5F_ACC_RDONLY);
    H5::DataSet dataSet = file.openDataSet("observations");
 
    // read meta information
    unsigned nObs = 0;
    H5::Attribute nameAtt  = dataSet.openAttribute("observed_quantities");
    H5::Attribute countAtt  = dataSet.openAttribute("n_observables");
    H5std_string strreadbuf("");
    nameAtt.read(nameAtt.getDataType(), strreadbuf);
    countAtt.read(countAtt.getDataType(), &nObs);

    // Read data out as 1D array
    hsize_t nData = 0;
    dataSet.getSpace().getSimpleExtentDims(&nData, NULL);
    size_t nEntries = nData/nObs;

    std::vector<double> flatData(nData, 0);
    dataSet.read(&flatData.at(0), H5::PredType::NATIVE_DOUBLE);

    assert(nData%nObs == 0); // logic error in writing file (this class!) if assert fails.

    // Assemble into an OXSX data set
    OXSXDataSet oxsxDataSet;

    // Set the variable names
    oxsxDataSet.SetObservableNames(UnpackString(strreadbuf, fDelimiter));

    // then the data
    std::vector<double> oneEventObs(nObs, 0);
    for(size_t i = 0; i < nEntries; i++){
        for(size_t j = 0; j < nObs; j++)
            oneEventObs[j] = flatData.at(i * nObs + j);
        
        oxsxDataSet.AddEntry(EventData(oneEventObs));
    }
      
    return oxsxDataSet;
}
コード例 #4
0
ファイル: ossimH5Util.cpp プロジェクト: bradh/ossim-plugins
bool ossim_hdf5::getGroupAttributeValue( H5::H5File* file,
                                         const std::string& group,
                                         const std::string& key,
                                         std::string& value )
{
   static const char MODULE[] = "ossim_hdf5::getGroupAttributeValue";
   
   bool result = false;
   
   if (  file )
   {
      try // HDF5 library throws exceptions so wrap with try{}catch...
      {
         // Open the root group:
         H5::Group* h5Group = new H5::Group( file->openGroup( group ) );
         
         // Lookw for key:
         H5::Attribute attr      = h5Group->openAttribute( key );
         std::string   name      = attr.getName();
         H5::DataType  type      = attr.getDataType();
         H5T_class_t   typeClass = attr.getTypeClass();
         
         if ( ( name == key ) && ( typeClass == H5T_STRING ) )
         {
            attr.read( type, value );
            result = true;
         }

         // Cleanup:
         attr.close();
         h5Group->close();
         delete h5Group;
         h5Group = 0;
      }
      catch( const H5::Exception& e )
      {
         if (traceDebug())
         {
            ossimNotify(ossimNotifyLevel_WARN)
               << MODULE << " WARNING: Caught exception!\n"
               << e.getDetailMsg() << std::endl;
         }
      }
      catch( ... )
      {
         ossimNotify(ossimNotifyLevel_WARN)
            << MODULE << " WARNING: Caught unknown exception!" << std::endl;
      }      
   }

   return result;
   
} // End: ossim_hdf5::getGroupAttributeValue
コード例 #5
0
ファイル: ossimH5Util.cpp プロジェクト: bradh/ossim-plugins
bool ossim_hdf5::getDatasetAttributeValue( H5::H5File* file,
                                           const std::string& objectName,
                                           const std::string& key,
                                           std::string& value )
{
   static const char MODULE[] = "ossim_hdf5::getDatasetAttributeValue";

   bool result = false;
   
   if (  file )
   {
      try // HDF5 library throws exceptions so wrap with try{}catch...
      {
         // Open the dataset:
         H5::DataSet dataset = file->openDataSet( objectName );
         
         // Lookw for key:
         H5::Attribute attr = dataset.openAttribute( key );

         std::string  name = attr.getName();
         H5::DataType type = attr.getDataType();
         H5T_class_t  typeClass = attr.getTypeClass();
         
         if ( ( name == key ) && ( typeClass == H5T_STRING ) )
         {
            attr.read( type, value );
            result = true;
         }

         // Cleanup:
         attr.close();
         dataset.close();
      }
      catch( const H5::Exception& e )
      {
         ossimNotify(ossimNotifyLevel_WARN)
            << MODULE << " WARNING: Caught exception!\n"
            << e.getDetailMsg() << std::endl;
      }
      catch( ... )
      {
         ossimNotify(ossimNotifyLevel_WARN)
            << MODULE << " WARNING: Caught unknown exception!" << std::endl;
      }      
   }

   return result;
   
} // End: ossim_hdf5::getDatasetAttributeValue
コード例 #6
0
ファイル: attribute.hpp プロジェクト: jb--/h5xx
inline typename boost::enable_if<boost::mpl::and_<
    is_vector<T>
  , boost::is_same<typename T::value_type, std::string>
>, T>::type
read_attribute(H5::H5Object const& object, std::string const& name)
{
    H5::Attribute attr;
    try {
        H5XX_NO_AUTO_PRINT(H5::AttributeIException);
        attr = object.openAttribute(name);
    }
    catch (H5::AttributeIException const&) {
        throw;
    }

    H5::DataSpace ds(attr.getSpace());
    if (!ds.isSimple()) {
        throw H5::AttributeIException("H5::attribute::as", "incompatible dataspace");
    }
    size_t size = ds.getSimpleExtentNpoints();

    H5::DataType tid = attr.getDataType();
    if (tid.isVariableStr()) {
        throw error("reading non-scalar attribute of variable-length strings not supported");
    }
    size_t str_len = tid.getSize();

    // read to contiguous buffer and copy to std::vector
    std::vector<char> buffer(str_len * size);
    attr.read(tid, &*buffer.begin());

    T value;
    value.reserve(size);
    char const* s = buffer.data();
    for (size_t i = 0; i < size; ++i, s += str_len) {
        size_t len = strnlen(s, str_len);     // strings of str_len size are not '\0'-terminated
        value.push_back(std::string(s, len)); // copy len bytes from buffer
    }

    return value;
}
コード例 #7
0
H5RandomReader::H5RandomReader(const std::string fileName, const std::string groupPath) throw (InvalidFileException) {

    try {
        file.openFile(fileName, H5F_ACC_RDONLY);}
    catch ( H5::FileIException ) {
        throw InvalidFileException("Cannot acces file");}
    try {
        group = file.openGroup(groupPath);}
    catch ( H5::GroupIException ) {
        file.close();
        throw InvalidFileException("Cannot access group");}
    /*
     * extract timeline. This is also necessary to get the nbSteps.
     */
    try {
        timeline = group.openDataSet("timeline");
    	nSteps = timeline.getSpace().getSimpleExtentNpoints();}
    catch ( H5::DataSetIException error ) {
        //error.printError();
        group.close();
        file.close();
        throw InvalidFileException("Cannot access timeline dataset");}
    if (logging::info)
        std::cerr << "Opened group \"" <<  fileName << groupPath << "\" which has " << nSteps << " steps.\n";
    /*
     * extract objects names in the xpGroup
     */

    std::vector<std::string>  names;
    H5Literate(group.getId(), H5_INDEX_NAME, H5_ITER_INC, NULL, iterInGroup, &names);
    /*
     * extract data from object in xpGroup
     * these data can be of 3 types: matrix, translate or wrench
     * each data are saved in related map
     */
    for (unsigned int i=0; i<names.size(); i++){ //TODO: skip timeline
        H5::DataSet dSet = group.openDataSet(names[i]);
        if (H5Aexists(dSet.getId(), "ArborisViewerType")) {
            H5::Attribute att = dSet.openAttribute("ArborisViewerType");
            std::string type;
            att.read(att.getDataType(), type);
            if (type == "matrix"){
                H5::DataSpace dSpace = dSet.getSpace();
                bool dimension_ok = false;
                if (dSpace.getSimpleExtentNdims()==3) {
                    hsize_t dims[3];
                    dSpace.getSimpleExtentDims (dims);
                    if (dims[0] == nSteps && dims[1] == 4 && dims[2] == 4)
                        dimension_ok = true;}
                if (dimension_ok)
                    matrices[names[i]] = dSet;
                else {
                    if (logging::warning)
                        std::cerr << "Skipping dataset \"" << names[i] << "\" which has wrong dimensions. I was expecting (" << nSteps << ",4,4).\n";
                    dSet.close();}}
            else if (type == "translate"){
                H5::DataSpace dSpace = dSet.getSpace();
                bool dimension_ok = false;
                if (dSpace.getSimpleExtentNdims()==2) {
                    hsize_t dims[2];
                    dSpace.getSimpleExtentDims (dims);
                    if (dims[0] == nSteps && dims[1] == 3)
                        dimension_ok = true;}
                if (dimension_ok)
                    translates[names[i]] = dSet;
                else {
                    if (logging::warning)
                        std::cerr << "Skipping dataset \"" << names[i] << "\" which has wrong dimensions. I was expecting (" << nSteps << ",3).\n";
                    dSet.close();}}
            else if (type == "wrench") {
                H5::DataSpace dSpace = dSet.getSpace();
                bool dimension_ok = false;
                if (dSpace.getSimpleExtentNdims()==2) {
                    hsize_t dims[2];
                    dSpace.getSimpleExtentDims (dims);
                    if (dims[0] == nSteps && dims[1] == 6)
                        dimension_ok = true;}
                if (dimension_ok)
                    wrenches[names[i]] = dSet;
                else {
                    if (logging::warning)
                        std::cerr << "Skipping dataset \"" << names[i] << "\" which as wrong dimensions. I was expecting (" << nSteps << ",6).\n";
                    dSet.close();}}
            else {
                if (logging::warning)
                    std::cerr << "Skipping dataset \"" << names[i] << "\" whose ArborisViewerType attribute as unknown value \"" << type << "\".\n";
                dSet.close();}
            att.close();
        }
        else {
            if (logging::info)
                std::cerr << "Skipping dataset \"" << names[i] << "\" which has no ArborisViewerType attribute.\n";
            dSet.close();
        }
    }
};
コード例 #8
0
//--------------------------------------------------------------------------------------------------
/// 
//--------------------------------------------------------------------------------------------------
TEST(DISABLED_HDFTests, BasicFileRead)
{
    std::string file_path = "D:/ResInsight/SourSim/PKMUNK_NOV_TEST_SS.sourpre.00001";

    try
    {
        H5::Exception::dontPrint();                                // Turn off auto-printing of failures to handle the errors appropriately

        H5::H5File file(file_path.c_str(), H5F_ACC_RDONLY);

        {
            H5::Group        timestep = file.openGroup("Timestep_00001");
            H5::Attribute    attr     = timestep.openAttribute("timestep");

            double timestep_value = 0.0;

            H5::DataType type = attr.getDataType();
            attr.read(type, &timestep_value);

            //std::cout << "Timestep value " << timestep_value << std::endl;
            EXPECT_NEAR(timestep_value, 1.0, 1e-1);
        }


        {
            // Group size is not an attribute!

            H5::Group GridFunctions = file.openGroup("Timestep_00001/GridParts/GridPart_00000/GridFunctions");

            hsize_t   group_size = GridFunctions.getNumObjs();

            //std::cout << "GridFunctions group_size " << group_size << std::endl;
            EXPECT_EQ(size_t(20), group_size);

/*            for (hsize_t i = 0; i < group_size; i++)
            {
                // H5std_string node_name = GridFunctions.getObjnameByIdx(i);     // crashes on VS2017 due to lib/heap/runtime differences to HDF5 VS2015 lib

                std::string node_name;
                node_name.resize(1024);

                ssize_t slen = GridFunctions.getObjnameByIdx(i, &node_name[0], 1023);

                node_name.resize(slen + 1);

                std::cout << "GridFunctions sub-node name " << node_name << std::endl;
            }
*/

            std::string first_subnode(1024, '\0');

            ssize_t slen = GridFunctions.getObjnameByIdx(0, &first_subnode[0], 1023);
            first_subnode.resize(slen + 1);

            EXPECT_TRUE(first_subnode.compare(0, slen, "GridFunction_00002") == 0);
        }


        {
            H5::Group        GridFunction_00002 = file.openGroup("Timestep_00001/GridParts/GridPart_00000/GridFunctions/GridFunction_00002");
            H5::Attribute    attr = GridFunction_00002.openAttribute("limits_max");

            double limits_max = 0.0;
        
            H5::DataType type = attr.getDataType();
            attr.read(type, &limits_max);

//            std::cout << "limits_max " << limits_max << std::endl;
            EXPECT_NEAR(limits_max, 0.3970204292629652, 1e-10);
        }


        {
            H5::Group        GridFunction_00002 = file.openGroup("Timestep_00001/GridParts/GridPart_00000/GridFunctions/GridFunction_00002");
            H5::DataSet        dataset = H5::DataSet(GridFunction_00002.openDataSet("values"));

            hsize_t dims[2];
            H5::DataSpace    dataspace = dataset.getSpace();
            dataspace.getSimpleExtentDims(dims, nullptr);

            std::vector<double> values;
            values.resize(dims[0]);
            dataset.read(values.data(), H5::PredType::NATIVE_DOUBLE);

/*            for (hsize_t i = 0; i < dims[0]; i++)
            {
                std::cout << "value " << i << " " << values[i] << std::endl;

            }
*/
            EXPECT_NEAR(values[0], 0.32356910366452146, 1e-10);
            EXPECT_NEAR(values[dims[0] - 1], 0.12200070891582514, 1e-10);
        }



    }  // end of try block
    
       
    catch (H5::FileIException error)        // catch failure caused by the H5File operations
    {
        std::cout << error.getCDetailMsg();
    }
    
    catch (H5::DataSetIException error)        // catch failure caused by the DataSet operations
    {
        std::cout << error.getCDetailMsg();
    }
    
    catch (H5::DataSpaceIException error)    // catch failure caused by the DataSpace operations
    {
        std::cout << error.getCDetailMsg(); 
    }
    
    catch (H5::DataTypeIException error)        // catch failure caused by the DataSpace operations
    {
        std::cout << error.getCDetailMsg();
    }

}
コード例 #9
0
ファイル: ossimH5Util.cpp プロジェクト: bradh/ossim-plugins
void ossim_hdf5::printAttribute( const H5::Attribute& attr,
                                 const std::string& prefix,
                                 std::ostream& out )
{
   std::string  name      = attr.getName();
   H5::DataType type      = attr.getDataType();
   H5T_class_t  typeClass = attr.getTypeClass();
   size_t       size      = type.getSize();
   
   std::string  value; // Initialized below.

   if ( ( typeClass == H5T_INTEGER ) || ( typeClass == H5T_FLOAT ) )
   {
      H5::IntType intType = attr.getIntType();

      ossimScalarType scalar = ossim_hdf5::getScalarType( intType.getId() );
      
      ossimByteOrder order = ossim_hdf5::getByteOrder( &attr );
      ossimEndian* endian = 0;
      if ( ( size > 1 ) && ( order != ossim::byteOrder() ) )
      {
         endian = new ossimEndian(); // If set used as flag to byte swap.
      }
      
      if ( typeClass == H5T_INTEGER )
      {
         switch ( scalar )
         {
            case OSSIM_UINT8:
            {
               if ( size == 1 )
               {
                  ossim_uint8 i;
                  attr.read( type, (void*)&i );
                  value = ossimString::toString( ossim_int32(i) ).string();
               }
               break;
            }
            case OSSIM_SINT8:
            {
               if ( size == 1 )
               {
                  ossim_sint8 i;
                  attr.read( type, (void*)&i );
                  value = ossimString::toString( ossim_int32(i) ).string();
               }
               break;
            }
            case OSSIM_UINT16:            
            {
               if ( size == 2 )
               {
                  ossim_uint16 i;
                  attr.read( type, (void*)&i );
                  if ( endian )
                  {
                     endian->swap( i );
                  }  
                  value = ossimString::toString( i ).string();
               }
               break;
            }
            case OSSIM_SINT16:
            {
               if ( size == 2 )
               {
                  ossim_sint16 i;
                  attr.read( type, (void*)&i );
                  if ( endian )
                  {
                     endian->swap( i );
                  }
                  value = ossimString::toString( i ).string();
               }
               break;
            }
            case OSSIM_UINT32:        
            {
               if ( size == 4 )
               {
                  ossim_uint32 i;
                  attr.read( type, (void*)&i );
                  if ( endian )
                  {
                     endian->swap( i );
                  }  
                  value = ossimString::toString( i ).string();
               }
               break;
            }
            case OSSIM_SINT32:
            {
               if ( size == 4 )
               {
                  ossim_sint32 i;
                  attr.read( type, (void*)&i );
                  if ( endian )
                  {
                     endian->swap( i );
                  }
                  value = ossimString::toString( i ).string();
               }
               break;
            }
            case OSSIM_UINT64:        
            {
               if ( size == 8 )
               {
                  ossim_uint64 i;
                  attr.read( type, (void*)&i );
                  if ( endian )
                  {
                     endian->swap( i );
                  }  
                  value = ossimString::toString( i ).string();
               }
               break;
            }
            case OSSIM_SINT64:
            {
               if ( size == 8 )
               {
                  ossim_sint32 i;
                  attr.read( type, (void*)&i );
                  if ( endian )
                  {
                     endian->swap( i );
                  }
                  value = ossimString::toString( i ).string();
               }
               break;
            }
            default:
               break;
         }
      }
      else if ( typeClass == H5T_FLOAT )
      {
         if ( scalar == OSSIM_FLOAT32 )
         {
            if ( size == 4 )
            {
               ossim_float32 f;
               attr.read( type, (void*)&f );
               if ( endian )
               {
                  endian->swap( f );
               }
               value = ossimString::toString( f ).string();
            }
         }
         if ( scalar == OSSIM_FLOAT64 )
         {
            if ( size == 8 )
            {
               ossim_float64 f;
               attr.read( type, (void*)&f );
               if ( endian )
               {
                  endian->swap( f );
               }
               value = ossimString::toString( f ).string();
            }
         }
      }

      if ( endian )
      {
         delete endian;
         endian = 0;
      }
   }
   else if ( typeClass == H5T_STRING )
   {
      attr.read( type, value );
   }
   else
   {
      ossimNotify(ossimNotifyLevel_DEBUG)
         << "ossimH5Util::printAttribute WARN: Unhandled type class: " << typeClass
         << std::endl;
   }

   out << prefix << "." << name << ": " << value << std::endl;
   
} // End: ossim_hdf5::printAttribute