Пример #1
0
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
std::string hdfutil::ReadString (const H5::CommonFG& group, const std::string & dsname) {
    const H5::DataSet & dataset = group.openDataSet(dsname);
	try {
        H5::DataType type = dataset.getDataType();
        std::string retval;
        retval.resize(type.getSize());
		dataset.read(&retval[0], dataset.getStrType());
        return retval;
	} catch (H5::Exception e) {
        std::string error = "Unable to ReadString.";// + dsname;
        throw std::runtime_error(error.c_str());
	}
}
Пример #3
0
DataSet DataSet::create(const H5::CommonFG &parent,
                        const std::string &name,
                        const H5::DataType &fileType,
                        const NDSize &size,
                        const NDSize &maxsize,
                        const NDSize &chunks,
                        bool max_size_unlimited,
                        bool guess_chunks)
{
    H5::DataSpace space;

    if (size) {
        if (maxsize) {
            space = DataSpace::create(size, maxsize);
        } else {
            space = DataSpace::create(size, max_size_unlimited);
        }
    }

    H5::DSetCreatPropList plcreate = H5::DSetCreatPropList::DEFAULT;

    if (chunks) {
        int rank = static_cast<int>(chunks.size());
        plcreate.setChunk(rank, chunks.data());
    } else if (guess_chunks) {
        NDSize guessedChunks = DataSet::guessChunking(size, fileType.getSize());
        plcreate.setChunk(static_cast<int>(guessedChunks.size()), guessedChunks.data());
    }

    H5::DataSet dset = parent.createDataSet(name, fileType, space);
    return DataSet(dset);
}
Пример #4
0
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;
}
Пример #5
0
	/**
	 * @param variable
	 * @param vattribute
	 * @return
	 */
	Attribute HDF5FileReader::getVariableAttribute(const std::string& variable, const std::string& vattribute)
	{


		//first, check the vAttributes map
//		std::cout<<"Checking variable attributes map\n";
		boost::unordered_map<std::string, boost::unordered_map< std::string, Attribute> >::iterator iter =
				vAttributes.find(variable);
		if (iter != vAttributes.end())
		{
			boost::unordered_map< std::string, Attribute>::iterator iter2 = vAttributes[variable].find(vattribute);
			if (iter2 != vAttributes[variable].end())
			{
				return (*iter2).second;
			}
		}
	//	std::cout<<"Attribute not loaded, opening Variables group\n";

		H5::Group group = this->current_file->openGroup("Variables");
	//	std::cout<<"Group opened. Creating memory for H5::DataSet\n";
		H5::DataSet * dataset = new H5::DataSet(group.openDataSet(variable));
	//	std::cout<<"creating h5attribute for variable\n";
		H5::Attribute h5attribute = dataset->openAttribute(vattribute); //changed from group.openAttribute(vattribute);
	//	std::cout<<"attribute opened, obtaining data type\n";
		H5::DataType dataType = h5attribute.getDataType();
		Attribute attribute;
	//	std::cerr<<"Retrieving Variable attribute info:"<<std::endl;
		if (dataType.getClass() == H5T_STRING)
		{
//			std::cout<<"String type variable attribute\n";
			std::string attributeValue = "NULL";
			h5attribute.read(dataType, &attributeValue);

			std::string attributeName = "";
			attributeName = h5attribute.getName();


			attribute.setAttributeName(attributeName);
			//std::cout << "attributeBuffer: " << attributeBuffer << endl;
			attribute.setAttributeValue(attributeValue);
			//return attribute;
		} else if (dataType.getClass() == H5T_INTEGER) //shouldn't this be H5T_INT or something?
		{
//			std::cout<<"Int type variable attribute\n";
			//int attributeValue = 0.f;
			int attributeBuffer;// = new int[1];

			h5attribute.read(dataType, &attributeBuffer);
			std::string attributeName = "";
			attributeName = h5attribute.getName();
			attribute.setAttributeName(attributeName);
			attribute.setAttributeValue(attributeBuffer);
			//return attribute;
		} else if (dataType.getClass() == H5T_FLOAT)//CDF_FLOAT
		{
//			std::cout<<"Float type variable attribute\n";
			//int attributeValue = 0.f;
			float attributeValue;// = new int[1];

			h5attribute.read(dataType, &attributeValue);
			std::string attributeName = "";
			attributeName = h5attribute.getName();
			attribute.setAttributeName(attributeName);
			attribute.setAttributeValue(attributeValue);
			//return attribute;
		}

		//cout << "added: " << i << " name: " << attribute.getAttributeName() << endl;
		//std::cout << "Attribute: " << attribute.toString() << std::endl;


		(vAttributes[variable])[vattribute] = attribute;
		return attribute;

	}
Пример #6
0
	/**
	 * @param attribute
	 * @return
	 */
	Attribute HDF5FileReader::getGlobalAttribute(const std::string& attribute)
	{
		//first, check if the attribute has already been requested. If so, return stored value
		boost::unordered_map<std::string, Attribute>::iterator iter = gAttributes.find(attribute);
		if (iter != gAttributes.end())
			return (*iter).second;

//		std::cout << "after search in getGlobalAttribute(const std::string& attribute)" << std::endl;
//		std::cout << "attribute: " << attribute << std::endl;
		H5::Group group = this->current_file->openGroup("/");
		H5::Attribute h5attribute = group.openAttribute(attribute);
		long attrNum = h5attribute.getId();
		H5::DataType dataType = h5attribute.getDataType();
//		std::cout << "attrNum after attribute: " << attrNum << std::endl;
		Attribute current_attribute;
		if (attrNum < 0)
		{
			std::cout << "attrNum: " << attrNum << " returned for " << attribute << std::endl;
		}
		else
		{
//			std::cout << "attribute: " << attribute << " attribute number: " << attrNum << std::endl;
//			std::cout << "attempting to get attribute without using attribute number\n";
			if (dataType.getClass() == H5T_STRING)
			{
				std::string attributeValue = "NULL";
				h5attribute.read(dataType, attributeValue);

				std::string attributeName = "";
				attributeName = h5attribute.getName();


				current_attribute.setAttributeName(attributeName);
				//std::cout << "name: '" << attributeName << "' string attributeBuffer: '" << attributeValue << "'"<< std::endl;
				current_attribute.setAttributeValue(attributeValue);
				gAttributeByID[(int)attrNum] = current_attribute;
				gAttributes[current_attribute.getAttributeName()] = current_attribute;
				//return attribute;
			} else if (dataType.getClass() == H5T_INTEGER)
			{
				//int attributeValue = 0.f;
				int attributeBuffer;// = new int[1];

				h5attribute.read(dataType, &attributeBuffer);
				std::string attributeName = "";
				attributeName = h5attribute.getName();
				current_attribute.setAttributeName(attributeName);
				//std::cout << "int attributeBuffer: '" << attributeBuffer << "'"<< std::endl;
				current_attribute.setAttributeValue(attributeBuffer);
				gAttributeByID[(int)attrNum] = current_attribute;
				gAttributes[current_attribute.getAttributeName()] = current_attribute;
				//return attribute;
			} else if (dataType.getClass() == H5T_FLOAT)//CDF_FLOAT
			{
				//int attributeValue = 0.f;
				float attributeValue;// = new int[1];

				h5attribute.read(dataType, &attributeValue);
				std::string attributeName = "";
				attributeName = h5attribute.getName();
				current_attribute.setAttributeName(attributeName);
				//std::cout << "float attributeBuffer: '" << attributeValue << "'"<< std::endl;

				current_attribute.setAttributeValue(attributeValue);
				gAttributeByID[(int)attrNum] = current_attribute;
				gAttributes[current_attribute.getAttributeName()] = current_attribute;
				//return attribute;
			}

		}//gAttributes[attribute] = current_attribute;
		return current_attribute;
//		return Attribute();
	}
Пример #7
0
	/**
	 * @param i The attribute number
	 * @return
	 */
	Attribute HDF5FileReader::getGlobalAttribute(long i)
	{

		std::cerr << "entered " << BOOST_CURRENT_FUNCTION << " i = " << (int)i << std::endl;
		H5::Group group = this->current_file->openGroup("/");
		std::cout<< "group assigned\n";
		H5::Attribute h5attribute = group.openAttribute((unsigned int)i);
		std::cout<< "attribute opened\n";
		H5::DataType dataType = h5attribute.getDataType();
		std::cout<< "dataType retrieved\n";
		Attribute attribute;
		std::cout << "checking dataType"<<std::endl;
		if (dataType.getClass() == H5T_STRING)
		{
			std::string attributeValue = "NULL";
			h5attribute.read(dataType, attributeValue);

			std::string attributeName = "";
			attributeName = h5attribute.getName();


			attribute.setAttributeName(attributeName);
			//std::cout << "name: '" << attributeName << "' string attributeBuffer: '" << attributeValue << "'"<< std::endl;
			attribute.setAttributeValue(attributeValue);
			gAttributeByID[i] = attribute;
			gAttributes[attribute.getAttributeName()] = attribute;
			//return attribute;
		} else if (dataType.getClass() == H5T_INTEGER)
		{
			//int attributeValue = 0.f;
			int attributeBuffer;// = new int[1];

			h5attribute.read(dataType, &attributeBuffer);
			std::string attributeName = "";
			attributeName = h5attribute.getName();
			attribute.setAttributeName(attributeName);
			//std::cout << "int attributeBuffer: '" << attributeBuffer << "'"<< std::endl;
			attribute.setAttributeValue(attributeBuffer);
			gAttributeByID[i] = attribute;
			gAttributes[attribute.getAttributeName()] = attribute;
			//return attribute;
		} else if (dataType.getClass() == H5T_FLOAT)//CDF_FLOAT
		{
			//int attributeValue = 0.f;
			float attributeValue;// = new int[1];

			h5attribute.read(dataType, &attributeValue);
			std::string attributeName = "";
			attributeName = h5attribute.getName();
			attribute.setAttributeName(attributeName);
			//std::cout << "float attributeBuffer: '" << attributeValue << "'"<< std::endl;

			attribute.setAttributeValue(attributeValue);
			gAttributeByID[i] = attribute;
			gAttributes[attribute.getAttributeName()] = attribute;
			//return attribute;
		}

		//std::cout << "added: " << i << " name: '" << attribute.getAttributeName() << "'" << endl;
		//std::cout << "Attribute: " << attribute.toString() << std::endl;
		return attribute;

	}
Пример #8
0
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
Пример #9
0
void ossimH5ImageDataset::getTileBuf(void* buffer, const ossimIrect& rect, ossim_uint32 band)
{
   static const char MODULE[] = "ossimH5ImageDataset::getTileBuf";

   if ( m_dataset )
   {
      try
      {
         // Shift rectangle by the sub image offse (if any) from the m_validRect.
         ossimIrect irect = rect + m_validRect.ul();
         
         //--
         // Turn off the auto-printing when failure occurs so that we can
         // handle the errors appropriately
         //---
         // H5::Exception::dontPrint();

         // NOTE: rank == array dimensions in hdf5 documentation lingo.

         // Get dataspace of the dataset.
         H5::DataSpace imageDataSpace = m_dataset->getSpace();
      
         // Number of dimensions of the input dataspace.:
         const ossim_int32 IN_DIM_COUNT = imageDataSpace.getSimpleExtentNdims();

         // Native type:
         H5::DataType dataType = m_dataset->getDataType();
      
         std::vector<hsize_t> inputCount(IN_DIM_COUNT);
         std::vector<hsize_t> inputOffset(IN_DIM_COUNT);

         if ( IN_DIM_COUNT == 2 )
         {
            inputOffset[0] = irect.ul().y;
            inputOffset[1] = irect.ul().x;
         
            inputCount[0] = irect.height();
            inputCount[1] = irect.width();
         }
         else
         {
            inputOffset[0] = band;
            inputOffset[1] = irect.ul().y;
            inputOffset[2] = irect.ul().x;
         
            inputCount[0] = 1;
            inputCount[1] = irect.height();
            inputCount[2] = irect.width();
         }
      
         // Define hyperslab in the dataset; implicitly giving strike strike and block NULL.
         imageDataSpace.selectHyperslab( H5S_SELECT_SET,
                                         &inputCount.front(),
                                         &inputOffset.front() );
      
         // Output dataspace dimensions.
         const ossim_int32 OUT_DIM_COUNT = 3;
         std::vector<hsize_t> outputCount(OUT_DIM_COUNT);
         outputCount[0] = 1;             // single band
         outputCount[1] = irect.height(); // lines
         outputCount[2] = irect.width();  // samples

         // Output dataspace offset.
         std::vector<hsize_t> outputOffset(OUT_DIM_COUNT);
         outputOffset[0] = 0;
         outputOffset[1] = 0;
         outputOffset[2] = 0;
      
         // Output dataspace.
         H5::DataSpace bufferDataSpace( OUT_DIM_COUNT, &outputCount.front());
         bufferDataSpace.selectHyperslab( H5S_SELECT_SET,
                                          &outputCount.front(),
                                          &outputOffset.front() );
      
         // Read data from file into the buffer.
         m_dataset->read( buffer, dataType, bufferDataSpace, imageDataSpace );

         if ( m_endian )
         {
            // If the m_endian pointer is initialized(not zero) swap the bytes.
            m_endian->swap( m_scalar, buffer, irect.area() );
         }
      
         // Cleanup:
         bufferDataSpace.close();
         dataType.close();
         imageDataSpace.close();
         
         // memSpace.close();
         // dataType.close();
         // dataSpace.close();
      }
      catch( const H5::FileIException& error )
      {
         ossimNotify(ossimNotifyLevel_WARN)
            << MODULE << " caught H5::FileIException!" << std::endl;
         error.printError();
      }
   
      // catch failure caused by the DataSet operations
      catch( const H5::DataSetIException& error )
      {
         ossimNotify(ossimNotifyLevel_WARN)
            << MODULE << " caught H5::DataSetIException!" << std::endl;
         error.printError();
      }
   
      // catch failure caused by the DataSpace operations
      catch( const H5::DataSpaceIException& error )
      {
         ossimNotify(ossimNotifyLevel_WARN)
            << MODULE << " caught H5::DataSpaceIException!" << std::endl;
         error.printError();
      }
   
      // catch failure caused by the DataSpace operations
      catch( const H5::DataTypeIException& error )
      {
         ossimNotify(ossimNotifyLevel_WARN)
            << MODULE << " caught H5::DataTypeIException!" << std::endl;
         error.printError();
      }
      catch( ... )
      {
         ossimNotify(ossimNotifyLevel_WARN)
            << MODULE << " caught unknown exception !" << std::endl;
      }
      
   } // Matches: if ( m_dataset )
   
} // End: ossimH5ImageDataset::getTileBuf