void addrow( H5::DataSet& ds, const std::vector<double>& rowtowrite )
{
  //Get the space (since it may have grown in length since last time of course )
  H5::DataSpace origspace = ds.getSpace();

  //get the rank, even though I know it is 2
  int rank = origspace.getSimpleExtentNdims();

  //Get the actual dimensions of the ranks.
  hsize_t dims[rank];
  int ndims = origspace.getSimpleExtentDims( dims, NULL);

  //Want to ADD a row, so need to offset at row = nrows, and col = 0;
  hsize_t offset[rank] = { dims[0], 0 }; 
  hsize_t dims_toadd[rank] = { 1, rowtowrite.size() }; //will write 1 row, ncols columns.

  //Compute "new" size (extended by 1 row).
  hsize_t size[rank] = { dims[0]+dims_toadd[0], rowtowrite.size() };

  //Do the extension.
  ds.extend( size );

  //Get the new (extended) space, and select the hyperslab to write the row to.
  origspace = ds.getSpace();
  origspace.selectHyperslab( H5S_SELECT_SET, dims_toadd, offset );

  //Make the "memory" data space?
  H5::DataSpace toaddspace(rank, dims_toadd);

  ds.write(  rowtowrite.data(), H5::PredType::NATIVE_DOUBLE, toaddspace, origspace );

  //Can close toaddspace/origspace with no effect.
  //Can also close/open data set at the beginning of each time with no effect.
}
Exemple #2
0
void ossim_hdf5::getExtents( const H5::DataSet* dataset,
                             std::vector<ossim_uint32>& extents )
{
   extents.clear();
   
   if ( dataset )
   {
      // Get dataspace of the dataset.
      H5::DataSpace dataspace = dataset->getSpace();
      
      // Number of dimensions:
      int ndims = dataspace.getSimpleExtentNdims();
      if ( ndims )
      {
         //hsize_t dims_out[ndims];
         std::vector<hsize_t> dims_out(ndims);
         dataspace.getSimpleExtentDims( &dims_out.front(), 0 );
         for ( ossim_int32 i = 0; i < ndims; ++i )
         {
            extents.push_back(static_cast<ossim_uint32>(dims_out[i]));
         }
      }

      dataspace.close();
   }
}
Exemple #3
0
arma::Mat<uint16_t> readLUT(const std::string& path)
{
    H5::H5File file (path.c_str(), H5F_ACC_RDONLY);
    H5::DataSet ds = file.openDataSet("LUT");

    H5::DataSpace filespace = ds.getSpace();
    int ndims = filespace.getSimpleExtentNdims();
    assert(ndims == 2);
    hsize_t dims[2] = {1, 1};
    filespace.getSimpleExtentDims(dims);

    H5::DataSpace memspace (ndims, dims);

    arma::Mat<uint16_t> res (dims[0], dims[1]);

    ds.read(res.memptr(), H5::PredType::NATIVE_UINT16, memspace, filespace);

    filespace.close();
    memspace.close();
    ds.close();
    file.close();

    // NOTE: Armadillo stores data in column-major order, while HDF5 uses
    // row-major ordering. Above, we read the data directly from HDF5 into
    // the arma matrix, so it was implicitly transposed. The next function
    // fixes this problem.
    arma::inplace_trans(res);
    return res;
}
UInt GetDatasetNDim(H5::CommonFG &parentGroup, std::string datasetName) {
    HDFData tmpDataset;
    tmpDataset.InitializeDataset(parentGroup, datasetName);
    H5::DataSpace dataspace = tmpDataset.dataset.getSpace();
    UInt nDims = dataspace.getSimpleExtentNdims();
    dataspace.close();
    tmpDataset.dataset.close();
    return nDims;
}
Exemple #5
0
int H5Signal::signal_count(void)
/*----------------------------*/
{
  H5::DataSpace uspace = dataset.openAttribute("uri").getSpace() ;
  int udims = uspace.getSimpleExtentNdims() ;
  if (udims == 0) return 1 ;  // SCALAR
  else {
    if (udims != 1) throw H5Exception("Dataset's 'uri' attribute has wrong shape: " + uri) ;
    return uspace.getSimpleExtentNpoints() ;
    }
  }
	/**
	 * @brief Returns a pointer to a std::vector<float> containing the values of the selected variable
	 *
	 * This allocates a new std::vector<float> pointer.  Make sure you
	 * delete the contents when you done using it, or you will have a memory leak.
	 *
	 * @param variable
	 * @return std::vector<float> containing the values of the selected variable.
	 */
	std::vector<float>* HDF5FileReader::getVariable(const std::string& variable)
	{
		std::vector<float>* variableData = new std::vector<float>();

		if (this->doesVariableExist(variable))
		{
			//std::cout << "reading " << variable << std::endl;
			//get variable number
//			long variableNum = this->getVariableID(variable);

			//std::cout << "variableNum for " << variable << ": " << variableNum << std::endl;
			//get dim sizes

			H5::Group group = this->current_file->openGroup("Variables");
			//cout << "variable: " << variable << ": " << counts[0] << endl;
			H5::DataSet * dataset = new H5::DataSet(group.openDataSet(variable));
			H5::DataSpace dataspace = dataset->getSpace();
			int rank = dataspace.getSimpleExtentNdims(); //should be 1
			hsize_t count[1];
			hsize_t offset[1] = {0};
//			int ndims = dataspace.getSimpleExtentDims(count, NULL);

			//std::cout << "count[0]: " << count[0] << std::endl;
			float * buffer = new float[count[0]];



			dataspace.selectHyperslab(H5S_SELECT_SET, count, offset);

			H5::DataSpace memspace( rank, count);
			memspace.selectHyperslab(H5S_SELECT_SET, count, offset);

			dataset->read(buffer, H5::PredType::NATIVE_FLOAT, memspace, dataspace);
			//std::cout << "after read" << std::endl;

			//add data to vector type, and delete original array
			variableData->reserve(count[0]);
			for (int i = 0; i < count[0]; i++)
			{
				variableData->push_back(buffer[i]);
			}
			//std::cout << "after adding to variableData vector" << std::endl;

			delete[] buffer;
			delete dataset;
			//std::cout << "finished reading " << variable << std::endl;
			//std::cout << "size of variable: " << variableData.size() << std::endl;
			//std::cout << "dimSizes[0]: " << dimSizes[0] << std::endl;

		}

		return variableData;
	}
Exemple #7
0
   const std::vector<hsize_t> TableDims(){

      if(flags_ != hdf5::READ) 
         throw std::runtime_error("TableDims() is only valid in READ mode");

      H5::DataSet dSet = file_->openDataSet("T00000000");
      H5::DataSpace dSpace = dSet.getSpace();

      std::vector<hsize_t> dims(dSpace.getSimpleExtentNdims());
      dSpace.getSimpleExtentDims(&dims[0]);

      return dims;
   }
NDArray<T, Nd> NDArray<T,Nd>::ReadFromH5(const H5::DataSet& h5Dset) {
  H5::DataSpace dspace = h5Dset.getSpace(); 
  int ndim = dspace.getSimpleExtentNdims(); 
  if (ndim>Nd) 
      throw std::range_error("Too many dimensions in H5 dataset for NDArray");
  hsize_t dimSize[ndim];
  dspace.getSimpleExtentDims(dimSize); 
  std::array<std::size_t, Nd> dimSizeArr;
  for (int i=0; i<Nd; ++i) dimSizeArr[i] = dimSize[i];
  NDArray<T, Nd> arr(dimSizeArr);
  // Read in data here
  H5::DataType h5DType = GetH5DataType<T>();
  h5Dset.read(arr.mData, h5DType);
  return arr;
}
void HDF5IO::loadStdVector(const std::string& GroupName, const std::string& Name,
    std::vector<RealType>& V)
{
  try{
    H5::Group FG = getGroup( GroupName );
    H5::DataSet DataSet = FG.openDataSet(Name.c_str());
    H5::DataSpace DataSpace = DataSet.getSpace();
    if(DataSpace.getSimpleExtentNdims() != 1)
      throw(H5::DataSpaceIException("HDF5IO::loadRealVector()","Unexpected multidimentional dataspace."));
    V.resize(DataSpace.getSimpleExtentNpoints());
    DataSet.read(V.data(),H5::PredType::NATIVE_DOUBLE);
    FG.close();
  } catch( const H5::Exception err ){
    RUNTIME_ERROR("HDF5IO::loadRealStdVector");
  }
}
void HDF5IO::loadVector(const std::string& GroupName, const std::string& Name,
  RealVectorType& V)
{
  H5::Group FG = getGroup( GroupName );
  H5::DataSet DataSet = FG.openDataSet(Name.c_str());
  H5::DataSpace DataSpace = DataSet.getSpace();
  if(DataSpace.getSimpleExtentNdims() != 1)
	throw(H5::DataSpaceIException("HDF5IO::loadRealVector()",
    "Unexpected multidimentional dataspace."));
  V.resize(DataSpace.getSimpleExtentNpoints());
  try{
    DataSet.read(V.data(),H5::PredType::NATIVE_DOUBLE);
  }catch( H5::GroupIException not_found_error ){
    RUNTIME_ERROR("No dataset found in loadRealVector. ");
  }
  FG.close();
}
void HDF5IO::loadStdVector(const std::string& GroupName, const std::string& Name,
    std::vector<ComplexType>& V)
{
  try{
    H5::CompType ComplexDataType = this->openCompType("complex");
    H5::Group FG = getGroup( GroupName );
    H5::DataSet DataSet = FG.openDataSet(Name.c_str());
    H5::DataSpace DataSpace = DataSet.getSpace();
    if(DataSpace.getSimpleExtentNdims() != 1)
      throw(H5::DataSpaceIException("HDF5IO::loadComplexVector()","Unexpected multidimentional dataspace."));
    V.resize(DataSpace.getSimpleExtentNpoints());
    DataSet.read(V.data(),ComplexDataType);
    FG.close();
  } catch( const H5::Exception err ){
    RUNTIME_ERROR("HDF5IO::loadComplexStdVector");
  }
}
	/**
	 * @brief Returns a value in the flat array of the variable and index requested.
	 *
	 * Use this method on variables that have a type of int
	 *
	 * @param variable The variable in the file
	 * @param index The index in the variable's array in the file
	 *
	 * @return int of the value in the array.
	 */
	int HDF5FileReader::getVariableIntAtIndex(const std::string& variable, long index)
	{
//		long counts[1];
		int value = std::numeric_limits<int>::min();
		if (this->doesVariableExist(variable))
		{
			//std::cout << "reading " << variable << std::endl;
			//get variable number
//			long variableNum = this->getVariableID(variable);

			//get dim sizes

			H5::Group group = this->current_file->openGroup("Variables");
			//cout << "variable: " << variable << ": " << counts[0] << endl;
			H5::DataSet * dataset = new H5::DataSet(group.openDataSet(variable));
			H5::DataSpace dataspace = dataset->getSpace();
			int rank = dataspace.getSimpleExtentNdims(); //should be 1
			hsize_t count[1] = {1};
			hsize_t offset[1] = {0};
			//int ndims = dataspace.getSimpleExtentDims(count, NULL);
			float * buffer = new float[count[0]];



			dataspace.selectHyperslab(H5S_SELECT_SET, count, offset);

			hsize_t dim[] = {count[0]};
			H5::DataSpace memspace( rank, dim);
			memspace.selectHyperslab(H5S_SELECT_SET, dim, offset);

			dataset->read(buffer, H5::PredType::NATIVE_INT, memspace, dataspace);


			//add data to vector type, and delete original array
			value = buffer[0];
			delete[] buffer;
			delete dataset;
		}
		return value;
			//std::cout << "finished reading " << variable << std::endl;
			//std::cout << "size of variable: " << variableData.size() << std::endl;
			//std::cout << "dimSizes[0]: " << dimSizes[0] << std::endl;


	}
Exemple #13
0
size_t H5Signal::clock_size(void)
/*-----------------------------*/
{
  try {
    H5::Attribute attr = dataset.openAttribute("clock") ;
    hobj_ref_t ref ;
    attr.read(H5::PredType::STD_REF_OBJ, &ref) ;
    attr.close() ;
    H5::DataSet clk(H5Rdereference(H5Iget_file_id(dataset.getId()), H5R_OBJECT, &ref)) ;
    H5::DataSpace cspace = clk.getSpace() ;
    int cdims = cspace.getSimpleExtentNdims() ;
    hsize_t cshape[cdims] ;
    cspace.getSimpleExtentDims(cshape) ;
    return cshape[0] ;
    }
  catch (H5::AttributeIException e) { }
  return -1 ;
  }
Exemple #14
0
/** Retrieves the dimensions of the dataset */
int hdfutil::GetDsDims (const H5::DataSet & dataset, int dims[3]) {
	H5::DataSpace dataspace = dataset.getSpace();
    bool simple = dataspace.isSimple();
    if (!simple)
        throw std::runtime_error("complex HDF5 dataspace");
	int rank = (int)dataspace.getSimpleExtentNdims();
	if (rank > 3)
        throw std::runtime_error("unsupported dimensionality");

    hsize_t h5_dims[3];
	dataspace.getSimpleExtentDims(h5_dims, NULL);
    for (int i = 0; i < rank; i++)
        dims[i] = (int)h5_dims[i];
    for (int i = rank; i < 3; i++)
        dims[i] =      -1;

    return rank;
}
	/**
	 * Returns a pointer to a std::vector<float> containing the values of the selected variable
	 * in the range specified by the startIndex and count (the number of records to read) stored
	 * in the selected file.  This allocates a new std::vector<float> pointer.  Make sure you
	 * delete the contents when you done using it, or you will have a memory leak.
	 * @param variableID
	 * @param startIndex
	 * @param count
	 * @return std::vector<float> containing the values of the selected variable.
	 */
	std::vector<float>* HDF5FileReader::getVariable(long variable, long indexOffset, long count)
	{

		std::vector<float>* variableData = new std::vector<float>();



		//get dim sizes
		H5::Group group = this->current_file->openGroup("Variables");
		//cout << "variable: " << variable << ": " << counts[0] << endl;
		H5::DataSet * dataset = new H5::DataSet(group.openDataSet(group.getObjnameByIdx(variable)));
		H5::DataSpace dataspace = dataset->getSpace();
		int rank = dataspace.getSimpleExtentNdims(); //should be 1
		hsize_t length[1] = {count};
		hsize_t offset[1] = {indexOffset};
		//int ndims = dataspace.getSimpleExtentDims(count, NULL);
		float * buffer = new float[length[0]];



		dataspace.selectHyperslab(H5S_SELECT_SET, length, offset);

		hsize_t dim[] = {length[0]};
		H5::DataSpace memspace( rank, dim);
		memspace.selectHyperslab(H5S_SELECT_SET, dim, offset);

		dataset->read(buffer, H5::PredType::NATIVE_FLOAT, memspace, dataspace);


		//add data to vector type, and delete original array
		variableData->reserve(length[0]);
		for (int i = 0; i < length[0]; i++)
		{
			variableData->push_back(buffer[i]);
		}

		delete[] buffer;
		delete dataset;
		//std::cout << "finished reading " << variable << std::endl;
		//std::cout << "size of variable: " << variableData.size() << std::endl;
		//std::cout << "dimSizes[0]: " << dimSizes[0] << std::endl;
		return variableData;
	}
void HDF5IO::loadMatrix(const std::string& GroupName, const std::string& Name,
    ComplexMatrixType& M)
{
  try{
    H5::CompType ComplexDataType = this->openCompType("complex");
    H5::Group FG = getGroup( GroupName );
    H5::DataSet DataSet = FG.openDataSet(Name.c_str());
    H5::DataSpace DataSpace = DataSet.getSpace();
    if(DataSpace.getSimpleExtentNdims() != 2)
	throw(H5::DataSpaceIException("HDF5IO::loadMatrix()","A dataspace must be precisely two-dimensional."));
    hsize_t Dims[2];
    DataSpace.getSimpleExtentDims(Dims);
    M.resize(Dims[0],Dims[1]);
    DataSet.read(M.data(), ComplexDataType);
    FG.close();
  } catch( const H5::Exception err ){
    RUNTIME_ERROR("HDF5IO::loadComplexMatrix at ");
  }
}
Exemple #17
0
 /// Return the attribute name of obj, and "" if the attribute does not exist.
 inline std::string read_string_attribute (H5::H5Object const * obj, std::string name ) { 
  std::string value ="";
  H5::Attribute attr;
  if (H5LTfind_attribute(obj -> getId(), name.c_str() )==0)  return value;// not present
  // can not find how to get the size with hl. Using full interface 
  //herr_t err2 =  H5LTget_attribute_string(gr.getId(), x.c_str(), name.c_str() , &(buf.front())  ) ;
  //value.append( &(buf.front()) );
  try { attr= obj->openAttribute(name.c_str());}
  catch (H5::AttributeIException) { return value;}
  try { 
   H5::DataSpace dataspace = attr.getSpace();
   int rank = dataspace.getSimpleExtentNdims();
   if (rank != 0) TRIQS_RUNTIME_ERROR << "Reading a string attribute and got rank !=0";
   size_t size = attr.getStorageSize();
   H5::StrType strdatatype(H5::PredType::C_S1, size+1);
   std::vector<char> buf(size+1, 0x00);
   attr.read(strdatatype, (void *)(&buf[0])); 
   value.append( &(buf.front()) );
  }
  TRIQS_ARRAYS_H5_CATCH_EXCEPTION;
  return value;
 } 
Exemple #18
0
static void read_attr(Attribute *attr, H5::Group g, cpath basename, cpath group_path, cpath name, BaseType &type)
{
  int total = 1;
  H5::Attribute h5attr = g.openAttribute(name.generic_string().c_str());
    
  type =  toBaseType(H5Aget_type(h5attr.getId()));
  
  H5::DataSpace space = h5attr.getSpace();
  int dimcount = space.getSimpleExtentNdims();

  hsize_t *dims = new hsize_t[dimcount];
  hsize_t *maxdims = new hsize_t[dimcount];
  
  space.getSimpleExtentDims(dims, maxdims);
  for(int i=0;i<dimcount;i++)
    total *= dims[i];
  
  group_path = remove_prefix(group_path, basename);
  name = group_path / name;
  
  //legacy attribute reading
  if (dimcount == 1) {
    void *buf = malloc(baseType_size(type)*total);
    
    h5attr.read(toH5NativeDataType(type), buf);
    
    attr->set<hsize_t>(name, dimcount, dims, type, buf);
  }
  else {
    Mat m;
    Mat_H5AttrRead(m, h5attr);
    attr->setName(name);
    attr->set(m);
  }

  delete[] dims;
  delete[] maxdims;
}
Exemple #19
0
std::vector<double> readEloss(const std::string& path)
{
    H5::H5File file (path.c_str(), H5F_ACC_RDONLY);
    H5::DataSet ds = file.openDataSet("eloss");

    H5::DataSpace filespace = ds.getSpace();
    int ndims = filespace.getSimpleExtentNdims();
    assert(ndims == 1);
    hsize_t dim;
    filespace.getSimpleExtentDims(&dim);

    H5::DataSpace memspace (ndims, &dim);

    std::vector<double> res (dim);

    ds.read(res.data(), H5::PredType::NATIVE_DOUBLE, memspace, filespace);

    filespace.close();
    memspace.close();
    ds.close();
    file.close();
    return res;
}
Exemple #20
0
std::vector<double> readlastrow( H5::DataSet& ds )
{
  H5::DataSpace origspace = ds.getSpace();
  int rank = origspace.getSimpleExtentNdims();
  hsize_t dims[rank];
  int ndims = origspace.getSimpleExtentDims( dims, NULL);
  hsize_t nrows=dims[0];
  hsize_t ncols=dims[1];
  std::vector<double> returnvect( ncols );

  
  
  hsize_t targrowoffset = nrows-1;
  hsize_t targcoloffset = 0;
  hsize_t dimsmem[rank] = {1,  ncols};
  H5::DataSpace memspace(rank, dimsmem);

  hsize_t offset[rank] = { targrowoffset, targcoloffset };
  origspace.selectHyperslab( H5S_SELECT_SET, dimsmem, offset );
  ds.read( returnvect.data(), H5::PredType::NATIVE_DOUBLE, memspace, origspace );

  return returnvect;
}
Exemple #21
0
bool ossimHdfGridModel::setGridNodes( H5::DataSet* latDataSet,
                                      H5::DataSet* lonDataSet,
                                      ossim_uint32 imageRows,
                                      ossim_uint32 imageCols )
{
    bool status = false;

    if ( latDataSet && lonDataSet )
    {
        const ossim_uint32 GRID_SIZE = 32; // Only grab every 32nd value.

        // Get dataspace of the dataset.
        H5::DataSpace latDataSpace = latDataSet->getSpace();
        H5::DataSpace lonDataSpace = lonDataSet->getSpace();
        const ossim_int32 LAT_DIM_COUNT = latDataSpace.getSimpleExtentNdims();
        const ossim_int32 LON_DIM_COUNT = latDataSpace.getSimpleExtentNdims();

        // Number of dimensions of the input dataspace:
        if ( ( LAT_DIM_COUNT == 2 ) && ( LON_DIM_COUNT == 2 ) )
        {
            //---
            // Get the extents:
            // dimsOut[0] is height, dimsOut[1] is width:         //---
            std::vector<hsize_t> latDimsOut(LAT_DIM_COUNT);
            latDataSpace.getSimpleExtentDims( &latDimsOut.front(), 0 );
            std::vector<hsize_t> lonDimsOut(LON_DIM_COUNT);
            lonDataSpace.getSimpleExtentDims( &lonDimsOut.front(), 0 );

            // Verify same as image:
            if ( (latDimsOut[0] == imageRows) &&
                    (lonDimsOut[0] == imageRows) &&
                    (latDimsOut[1] == imageCols) &&
                    (lonDimsOut[1] == imageCols) )
            {
                //---
                // Capture the rectangle:
                // dimsOut[0] is height, dimsOut[1] is width:
                //---
                ossimIrect rect = ossimIrect( 0, 0,
                                              static_cast<ossim_int32>( latDimsOut[1]-1 ),
                                              static_cast<ossim_int32>( latDimsOut[0]-1 ) );

                //----
                // Initialize the ossimDblGrids:
                //---
                ossimDpt dspacing (GRID_SIZE, GRID_SIZE);

                ossim_uint32 gridRows = imageRows / GRID_SIZE + 1;
                ossim_uint32 gridCols = imageCols / GRID_SIZE + 1;

                // Round up if size doesn't fall on end pixel.
                if ( imageRows % GRID_SIZE) ++gridRows;
                if ( imageCols % GRID_SIZE) ++gridCols;

                ossimIpt gridSize (gridCols, gridRows);

                // The grid as used in base class, has UV-space always at 0,0 origin
                ossimDpt gridOrigin(0.0,0.0);

                const ossim_float64 NULL_VALUE = -999.0;
                theLatGrid.setNullValue(ossim::nan());
                theLonGrid.setNullValue(ossim::nan());
                theLatGrid.initialize(gridSize, gridOrigin, dspacing);
                theLonGrid.initialize(gridSize, gridOrigin, dspacing);

                std::vector<hsize_t> inputCount(LAT_DIM_COUNT);
                std::vector<hsize_t> inputOffset(LAT_DIM_COUNT);

                inputOffset[0] = 0; // row
                inputOffset[1] = 0; // col

                inputCount[0] = 1; // row
                inputCount[1] = (hsize_t)imageCols; // col

                // Output dataspace dimensions. Reading a line at a time.
                const ossim_int32 OUT_DIM_COUNT = 3;
                std::vector<hsize_t> outputCount(OUT_DIM_COUNT);
                outputCount[0] = 1; // band
                outputCount[1] = 1; // row
                outputCount[2] = imageCols; // col

                // Output dataspace offset.
                std::vector<hsize_t> outputOffset(OUT_DIM_COUNT);
                outputOffset[0] = 0;
                outputOffset[1] = 0;
                outputOffset[2] = 0;

                ossimScalarType scalar = ossim_hdf5::getScalarType( latDataSet );
                if ( scalar == OSSIM_FLOAT32 )
                {
                    // Set the return status to true if we get here...
                    status = true;

                    // See if we need to swap bytes:
                    ossimEndian* endian = 0;
                    if ( ( ossim::byteOrder() != ossim_hdf5::getByteOrder( latDataSet ) ) )
                    {
                        endian = new ossimEndian();
                    }

                    // Native type:
                    H5::DataType latDataType = latDataSet->getDataType();
                    H5::DataType lonDataType = lonDataSet->getDataType();

                    // Output dataspace always the same, width of one line.
                    H5::DataSpace bufferDataSpace( OUT_DIM_COUNT, &outputCount.front());
                    bufferDataSpace.selectHyperslab( H5S_SELECT_SET,
                                                     &outputCount.front(),
                                                     &outputOffset.front() );

                    //  Arrays to hold a single line of latitude longitude values.
                    vector<ossim_float32> latValue(imageCols);
                    vector<ossim_float32> lonValue(imageCols);
                    hsize_t row = 0;

                    // Line loop:
                    for ( ossim_uint32 y = 0; y < gridRows; ++y )
                    {
                        // row = line in image space
                        row = y*GRID_SIZE;

                        bool hitNans = false;

                        if ( row < imageRows )
                        {
                            inputOffset[0] = row;

                            latDataSpace.selectHyperslab( H5S_SELECT_SET,
                                                          &inputCount.front(),
                                                          &inputOffset.front() );
                            lonDataSpace.selectHyperslab( H5S_SELECT_SET,
                                                          &inputCount.front(),
                                                          &inputOffset.front() );

                            // Read data from file into the buffer.
                            latDataSet->read( &(latValue.front()), latDataType,
                                              bufferDataSpace, latDataSpace );
                            lonDataSet->read( &(lonValue.front()), lonDataType,
                                              bufferDataSpace, lonDataSpace );

                            if ( endian )
                            {
                                // If the endian pointer is initialized(not zero) swap the bytes.
                                endian->swap( &(latValue.front()), imageCols );
                                endian->swap( &(lonValue.front()), imageCols );
                            }

                            // Sample loop:
                            hsize_t col = 0;

                            for ( ossim_uint32 x = 0; x < gridCols; ++x )
                            {
                                ossim_float32 lat = ossim::nan();
                                ossim_float32 lon = ossim::nan();

                                // col = sample in image space
                                col = x*GRID_SIZE;

                                if ( col < imageCols )
                                {
                                    if ( (latValue[col] > NULL_VALUE)&&(lonValue[col] > NULL_VALUE) )
                                    {
                                        lat = latValue[col];
                                        lon = lonValue[col];
                                    }
                                    else
                                    {
                                        hitNans = true;
                                    }
                                }
                                else // Last column is outside of image bounds.
                                {
                                    ossim_float32 latSpacing = ossim::nan();
                                    ossim_float32 lonSpacing = ossim::nan();

                                    // Get the last two latitude values:
                                    ossim_float32 lat1 = latValue[imageCols-2];
                                    ossim_float32 lat2 = latValue[imageCols-1];

                                    // Get the last two longitude values
                                    ossim_float32 lon1 = lonValue[imageCols-2];
                                    ossim_float32 lon2 = lonValue[imageCols-1];

                                    if ( ( lat1 > NULL_VALUE ) && ( lat2 > NULL_VALUE ) )
                                    {
                                        // Delta between last two latitude values.
                                        latSpacing = lat2 - lat1;

                                        // Compute:
                                        lat = lat2 + ( col - (imageCols-1) ) * latSpacing;
                                    }
                                    else
                                    {
                                        hitNans = true;
                                    }

                                    if ( ( lon1 > NULL_VALUE ) && ( lon2 > NULL_VALUE ) )
                                    {
                                        // Consider dateline crossing.
                                        if ( (lon1 > 0.0) && ( lon2 < 0.0 ) )
                                        {
                                            lon2 += 360.0;
                                        }

                                        // Delta between last two longitude values.
                                        lonSpacing = lon2 - lon1;

                                        // Compute:
                                        lon = lon2 + ( col - (imageCols-1) ) * lonSpacing;

                                        // Check for wrap:
                                        if ( lon > 180 )
                                        {
                                            lon -= 360.0;
                                        }
                                    }
                                    else
                                    {
                                        hitNans = true;
                                    }

#if 0 /* Please leave for debug. (drb) */
                                    cout << "lat1: " << lat1 << " lat2 " << lat2
                                         << " lon1 " << lon1  << " lon2 " << lon2
                                         << "\n";
#endif
                                }

                                if ( hitNans )
                                {
                                    std::string errMsg =
                                        "ossimHdfGridModel::setGridNodes encountered nans!";
                                    throw ossimException(errMsg);
                                }

                                // Assign the latitude and longitude.
                                theLatGrid.setNode( x, y, lat );
                                theLonGrid.setNode( x, y, lon );

#if 0 /* Please leave for debug. (drb) */
                                cout << "x,y,col,row,lat,lon:" << x << "," << y << ","
                                     << col << "," << row << ","
                                     << theLatGrid.getNode(x, y)
                                     << "," << theLonGrid.getNode( x, y) << "\n";
#endif

                            } // End sample loop.

                        }
                        else // Row is outside of image bounds:
                        {
                            // Read the last two rows in.
                            vector<ossim_float32> latValue2(imageCols);
                            vector<ossim_float32> lonValue2(imageCols);

                            inputOffset[0] = imageRows-2; // 2nd to last line

                            latDataSpace.selectHyperslab( H5S_SELECT_SET,
                                                          &inputCount.front(),
                                                          &inputOffset.front() );
                            lonDataSpace.selectHyperslab( H5S_SELECT_SET,
                                                          &inputCount.front(),
                                                          &inputOffset.front() );

                            // Read data from file into the buffer.
                            latDataSet->read( &(latValue.front()), latDataType,
                                              bufferDataSpace, latDataSpace );
                            lonDataSet->read( &(lonValue.front()), lonDataType,
                                              bufferDataSpace, lonDataSpace );

                            inputOffset[0] = imageRows-1; // last line

                            latDataSpace.selectHyperslab( H5S_SELECT_SET,
                                                          &inputCount.front(),
                                                          &inputOffset.front() );
                            lonDataSpace.selectHyperslab( H5S_SELECT_SET,
                                                          &inputCount.front(),
                                                          &inputOffset.front() );

                            // Read data from file into the buffer.
                            latDataSet->read( &(latValue2.front()), latDataType,
                                              bufferDataSpace, latDataSpace );
                            lonDataSet->read( &(lonValue2.front()), lonDataType,
                                              bufferDataSpace, lonDataSpace );

                            if ( endian )
                            {
                                // If the endian pointer is initialized(not zero) swap the bytes.
                                endian->swap( &(latValue.front()), imageCols );
                                endian->swap( &(lonValue.front()), imageCols );
                                endian->swap( &(latValue2.front()), imageCols );
                                endian->swap( &(lonValue2.front()), imageCols );
                            }

                            // Sample loop:
                            hsize_t col = 0;
                            for ( ossim_uint32 x = 0; x < gridCols; ++x )
                            {
                                col = x*GRID_SIZE; // Sample in image space.

                                ossim_float32 lat        = ossim::nan();
                                ossim_float32 lat1       = ossim::nan();
                                ossim_float32 lat2       = ossim::nan();
                                ossim_float32 latSpacing = ossim::nan();
                                ossim_float32 lon        = ossim::nan();
                                ossim_float32 lon1       = ossim::nan();
                                ossim_float32 lon2       = ossim::nan();
                                ossim_float32 lonSpacing = ossim::nan();

                                if ( col < imageCols )
                                {
                                    lat1 = latValue[col];
                                    lat2 = latValue2[col];
                                    lon1 = lonValue[col];
                                    lon2 = lonValue2[col];
                                }
                                else // Very last grid point.
                                {
                                    // Compute the missing column for the last two image lines:
                                    lat1 = latValue[imageCols-1] + ( col - (imageCols-1)) *
                                           ( latValue[imageCols-1] - latValue[imageCols-2] );

                                    lat2 = latValue2[imageCols-1] + ( col - (imageCols-1)) *
                                           ( latValue2[imageCols-1] - latValue2[imageCols-2] );

                                    lon1 = lonValue[imageCols-1] + ( col - (imageCols-1)) *
                                           ( lonValue[imageCols-1] - lonValue[imageCols-2] );

                                    lon2 = lonValue2[imageCols-1] + ( col - (imageCols-1)) *
                                           ( lonValue2[imageCols-1] - lonValue2[imageCols-2] );
                                }

#if 0 /* Please leave for debug. (drb) */
                                cout << "lat1: " << lat1 << " lat2 " << lat2
                                     << " lon1 " << lon1  << " lon2 " << lon2
                                     << "\n";
#endif

                                if ( ( lon1 > NULL_VALUE ) && ( lon2 > NULL_VALUE ) )
                                {
                                    // Consider dateline crossing.
                                    if ( (lon1 > 0.0) && ( lon2 < 0.0 ) )
                                    {
                                        lon2 += 360.0;
                                    }

                                    // Delta between last two longitude values.
                                    lonSpacing = lon2 - lon1;

                                    // Compute:
                                    lon = lon2 + ( row - (imageRows-1) ) * lonSpacing;

                                    // Check for wrap:
                                    if ( lon > 180 )
                                    {
                                        lon -= 360.0;
                                    }
                                }
                                else
                                {
                                    hitNans = true;
                                }

                                if ( ( lat1 > NULL_VALUE ) && ( lat2 > NULL_VALUE ) )
                                {
                                    // Delta between last two latitude values.
                                    latSpacing = lat2 - lat1;

                                    // Compute:
                                    lat = lat2 + ( row - (imageRows-1) ) * latSpacing;
                                }
                                else
                                {
                                    hitNans = true;
                                }

                                if ( hitNans )
                                {
                                    std::string errMsg =
                                        "ossimHdfGridModel::setGridNodes encountered nans!";
                                    throw ossimException(errMsg);
                                }

                                // Assign the latitude::
                                theLatGrid.setNode( x, y, lat );

                                // Assign the longitude.
                                theLonGrid.setNode( x, y, lon );

#if 0 /* Please leave for debug. (drb) */
                                cout << "x,y,col,row,lat,lon:" << x << "," << y << ","
                                     << col << "," << row << "," << lat << "," << lon << "\n";
#endif

                            } // End sample loop.

                        } // Matches if ( row < imageRows ){...}else{

                    } // End line loop.

                    latDataSpace.close();
                    lonDataSpace.close();

                    if ( status )
                    {
                        theLatGrid.enableExtrapolation();
                        theLonGrid.enableExtrapolation();
                        theHeightEnabledFlag = false;
                        ossimDrect imageRect(rect);
                        initializeModelParams(imageRect);
                        // debugDump();
                    }

                } // Matches: if ( scalar == OSSIM_FLOAT32 )

            } // Matches: if ( (latDimsOut[0] == imageRows) ...

        } // Matches: if ( ( LAT_DIM_COUNT == 2 ) ...

    } // Matches: if ( latDataSet && lonDataSet

    return status;

} // End: bool ossimHdfGridModel::setGridNodes( H5::DataSet* latDataSet, ... )
bool ossimH5GridModel::setGridNodes( H5::DataSet* latDataSet,
                                     H5::DataSet* lonDataSet,
                                     const ossimIrect& validRect )
{
   bool status = false;

   if ( latDataSet && lonDataSet )
   {
      m_crossesDateline = ossim_hdf5::crossesDateline( *lonDataSet, validRect );
      
      if ( m_crossesDateline )
      {
         theLonGrid.setDomainType(ossimDblGrid::WRAP_360);
      }
      else
      {
         theLonGrid.setDomainType(ossimDblGrid::WRAP_180);
      }

      // Get dataspace of the dataset.
      H5::DataSpace latDataSpace = latDataSet->getSpace();
      H5::DataSpace lonDataSpace = lonDataSet->getSpace();
      const ossim_int32 LAT_DIM_COUNT = latDataSpace.getSimpleExtentNdims();
      const ossim_int32 LON_DIM_COUNT = lonDataSpace.getSimpleExtentNdims();
      
      // Number of dimensions of the input dataspace:
      if ( ( LAT_DIM_COUNT == 2 ) && ( LON_DIM_COUNT == 2 ) )
      {
         const ossim_uint32 ROWS = validRect.height();
         const ossim_uint32 COLS = validRect.width();
         const ossim_uint32 GRID_SIZE = 4; // Only grab every 4th value.

         //---
         // Get the extents:
         // dimsOut[0] is height, dimsOut[1] is width:
         //---
         std::vector<hsize_t> latDimsOut(LAT_DIM_COUNT);
         latDataSpace.getSimpleExtentDims( &latDimsOut.front(), 0 );
         std::vector<hsize_t> lonDimsOut(LON_DIM_COUNT);
         lonDataSpace.getSimpleExtentDims( &lonDimsOut.front(), 0 );
         
         // Verify valid rect within our bounds:
         if ( (ROWS <= latDimsOut[0] ) && (ROWS <= lonDimsOut[0] ) &&
              (COLS <= latDimsOut[1] ) && (COLS <= lonDimsOut[1] ) )
         {
            //----
            // Initialize the ossimDblGrids:
            //---
            ossimDpt dspacing (GRID_SIZE, GRID_SIZE);
            
            ossim_uint32 gridRows = ROWS / GRID_SIZE + 1;
            ossim_uint32 gridCols = COLS / GRID_SIZE + 1;
            
            // Round up if size doesn't fall on end pixel.
            if ( ROWS % GRID_SIZE) ++gridRows;
            if ( COLS % GRID_SIZE) ++gridCols;

            ossimIpt gridSize (gridCols, gridRows);
            
            // The grid as used in base class, has UV-space always at 0,0 origin            
            ossimDpt gridOrigin(0.0,0.0);

            const ossim_float64 NULL_VALUE = -999.0;
            
            theLatGrid.setNullValue(ossim::nan());
            theLonGrid.setNullValue(ossim::nan());
            theLatGrid.initialize(gridSize, gridOrigin, dspacing);
            theLonGrid.initialize(gridSize, gridOrigin, dspacing);            
            
            std::vector<hsize_t> inputCount(LAT_DIM_COUNT);
            std::vector<hsize_t> inputOffset(LAT_DIM_COUNT);
            
            inputOffset[0] = 0; // row is set below.
            inputOffset[1] = validRect.ul().x; // col
            
            inputCount[0] = 1; // row
            inputCount[1] = (hsize_t)COLS; // col
            
            // Output dataspace dimensions. Reading a line at a time.
            const ossim_int32 OUT_DIM_COUNT = 3;
            std::vector<hsize_t> outputCount(OUT_DIM_COUNT);
            outputCount[0] = 1;    // band
            outputCount[1] = 1;    // row
            outputCount[2] = COLS; // col
            
            // Output dataspace offset.
            std::vector<hsize_t> outputOffset(OUT_DIM_COUNT);
            outputOffset[0] = 0;
            outputOffset[1] = 0;
            outputOffset[2] = 0;
            
            ossimScalarType scalar = ossim_hdf5::getScalarType( latDataSet );
            if ( scalar == OSSIM_FLOAT32 )
            {
               // Set the return status to true if we get here...
               status = true;
               
               // See if we need to swap bytes:
               ossimEndian* endian = 0;
               if ( ( ossim::byteOrder() != ossim_hdf5::getByteOrder( latDataSet ) ) )
               {
                  endian = new ossimEndian();
               }
               
               // Native type:
               H5::DataType latDataType = latDataSet->getDataType();
               H5::DataType lonDataType = lonDataSet->getDataType();

               // Output dataspace always the same, width of one line.
               H5::DataSpace bufferDataSpace( OUT_DIM_COUNT, &outputCount.front());
               bufferDataSpace.selectHyperslab( H5S_SELECT_SET,
                                                &outputCount.front(),
                                                &outputOffset.front() );

               //  Arrays to hold a single line of latitude longitude values.
               vector<ossim_float32> latValue(COLS);
               vector<ossim_float32> lonValue(COLS);
               hsize_t row = 0;

               // Line loop:
               for ( ossim_uint32 y = 0; y < gridRows; ++y )
               {
                  // row = line in image space
                  row = y*GRID_SIZE;

                  if ( row < ROWS )
                  {
                     inputOffset[0] = row + validRect.ul().y;

                     latDataSpace.selectHyperslab( H5S_SELECT_SET,
                                                   &inputCount.front(),
                                                   &inputOffset.front() );
                     lonDataSpace.selectHyperslab( H5S_SELECT_SET,
                                                   &inputCount.front(),
                                                   &inputOffset.front() );
                  
                     // Read data from file into the buffer.
                     latDataSet->read( &(latValue.front()), latDataType,
                                       bufferDataSpace, latDataSpace );
                     lonDataSet->read( &(lonValue.front()), lonDataType,
                                       bufferDataSpace, lonDataSpace );
                  
                     if ( endian )
                     {
                        // If the endian pointer is initialized(not zero) swap the bytes.
                        endian->swap( &(latValue.front()), COLS );
                        endian->swap( &(lonValue.front()), COLS );  
                     }
                  
                     // Sample loop:
                     hsize_t col = 0;
                     
                     for ( ossim_uint32 x = 0; x < gridCols; ++x )
                     {
                        ossim_float32 lat = ossim::nan();
                        ossim_float32 lon = ossim::nan();
                        
                        // col = sample in image space
                        col = x*GRID_SIZE;

                        if ( col < COLS )
                        {
                           if ( (latValue[col] > NULL_VALUE)&&(lonValue[col] > NULL_VALUE) )
                           {
                              lat = latValue[col];
                              lon = lonValue[col];
                              if ( m_crossesDateline )
                              {
                                 if ( lon < 0.0 ) lon += 360;
                              }
                           }
                           else // Nulls in grid!
                           {
                              std::string errMsg =
                                 "ossimH5GridModel::setGridNodes encountered nans!";
                              throw ossimException(errMsg); 
                           }
                        }
                        else // Last column is outside of image bounds.
                        {
                           // Get the last two latitude values:
                           ossim_float32 lat1 = theLatGrid.getNode( x-2, y );
                           ossim_float32 lat2 = theLatGrid.getNode( x-1, y );

                           // Get the last two longitude values
                           ossim_float32 lon1 = theLonGrid.getNode( x-2, y );
                           ossim_float32 lon2 = theLonGrid.getNode( x-1, y );
                           
                           if ( ( lat1 > NULL_VALUE ) && ( lat2 > NULL_VALUE ) )
                           {
                              // Delta between last two latitude grid values.
                              ossim_float32 latSpacing = lat2 - lat1;
                           
                              // Compute:
                              lat = lat2 + latSpacing;
                           }
                           else // Nulls in grid!
                           {
                              std::string errMsg =
                                 "ossimH5GridModel::setGridNodes encountered nans!";
                              throw ossimException(errMsg);
                           }

                           if ( ( lon1 > NULL_VALUE ) && ( lon2 > NULL_VALUE ) )
                           {
                              // Delta between last two longitude values.
                              ossim_float32 lonSpacing = lon2 - lon1;
                           
                              // Compute:
                              lon = lon2 + lonSpacing;

                              // Check for wrap:
                              if ( !m_crossesDateline && ( lon > 180 ) )
                              {
                                 lon -= 360.0;
                              }
                           }
                           else // Nulls in grid!
                           {
                              std::string errMsg =
                                 "ossimH5GridModel::setGridNodes encountered nans!";
                              throw ossimException(errMsg);
                           }

#if 0 /* Please leave for debug. (drb) */                        
                           cout << "lat1: " << lat1 << " lat2 " << lat2
                                << " lon1 " << lon1  << " lon2 " << lon2
                                << "\n";
#endif
                        }
                        
                        // Assign the latitude and longitude.
                        theLatGrid.setNode( x, y, lat );
                        theLonGrid.setNode( x, y, lon );
                        
#if 0 /* Please leave for debug. (drb) */ 
                        cout << "x,y,col,row,lat,lon:" << x << "," << y << ","
                             << col << "," << row << ","
                             << theLatGrid.getNode(x, y)
                             << "," << theLonGrid.getNode( x, y) << "\n";
#endif
                        
                     } // End sample loop.
                     
                  }
                  else // Row is outside of image bounds:
                  {
                     // Sample loop:
                     for ( ossim_uint32 x = 0; x < gridCols; ++x )
                     {
                        ossim_float32 lat        = ossim::nan();
                        ossim_float32 lon        = ossim::nan();
                        
                        ossim_float32 lat1 = theLatGrid.getNode( x, y-2 );
                        ossim_float32 lat2 = theLatGrid.getNode( x, y-1 );
                        ossim_float32 lon1 = theLonGrid.getNode( x, y-2 );
                        ossim_float32 lon2 = theLonGrid.getNode( x, y-1 );
                        
                        if ( ( lon1 > NULL_VALUE ) && ( lon2 > NULL_VALUE ) )
                        {
                           // Delta between last two longitude values.
                           ossim_float32 lonSpacing = lon2 - lon1;
                           
                           // Compute:
                           lon = lon2 + lonSpacing;
                           
                           // Check for wrap:
                           if ( !m_crossesDateline && ( lon > 180 ) )
                           {
                              lon -= 360.0;
                           }
                        }
                        else // Nulls in grid!
                        {
                           std::string errMsg =
                              "ossimH5GridModel::setGridNodes encountered nans!";
                           throw ossimException(errMsg);
                        }
                        
                        if ( ( lat1 > NULL_VALUE ) && ( lat2 > NULL_VALUE ) )
                        {
                           // Delta between last two latitude values.
                           ossim_float32 latSpacing = lat2 - lat1;

                           lat = lat2 + latSpacing;
                        }
                        else // Nulls in grid!
                        {
                           std::string errMsg =
                              "ossimH5GridModel::setGridNodes encountered nans!";
                           throw ossimException(errMsg);
                        }
                        
#if 0 /* Please leave for debug. (drb) */
                        hsize_t col = x*GRID_SIZE; // Sample in image space
                        cout << "lat1: " << lat1 << " lat2 " << lat2
                             << " lon1 " << lon1  << " lon2 " << lon2
                             << "\nx,y,col,row,lat,lon:" << x << "," << y << ","
                             << col << "," << row << "," << lat << "," << lon << "\n";
#endif
                        // Assign the latitude::
                        theLatGrid.setNode( x, y, lat );

                        // Assign the longitude.
                        theLonGrid.setNode( x, y, lon );
                     
                     } // End sample loop.
                  
                  } // Matches if ( row < imageRows ){...}else{
                  
               } // End line loop.

               latDataSpace.close();
               lonDataSpace.close();

               if ( status )
               {
                  theSeedFunction = ossim_hdf5::getBilinearProjection(
                     *latDataSet,*lonDataSet, validRect );
                  
                  // Bileaner projection to handle
                  ossimDrect imageRect(validRect);
                  initializeModelParams(imageRect);

                  // debugDump();
               }

               if ( endian )
               {
                  delete endian;
                  endian = 0;
               }
               
            } // Matches: if ( scalar == OSSIM_FLOAT32 )
            
         } // Matches: if ( (latDimsOut[0] == imageRows) ...
         
      } // Matches: if ( ( LAT_DIM_COUNT == 2 ) ...

   } // Matches: if ( latDataSet && lonDataSet

   return status;
   
} // End: bool ossimH5GridModel::setGridNodes( H5::DataSet* latDataSet, ... )
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();
        }
    }
};
Exemple #24
0
bool ossim_hdf5::getValidBoundingRect( H5::DataSet& dataset,
                                       const std::string& name,
                                       ossimIrect& rect )
{
   bool result = false;
   H5::DataSpace imageDataspace = dataset.getSpace();
   const ossim_int32 IN_DIM_COUNT = imageDataspace.getSimpleExtentNdims();
         
   if ( IN_DIM_COUNT == 2 )
   {
      // Get the extents. Assuming dimensions are same for lat lon dataset. 
      std::vector<hsize_t> dimsOut(IN_DIM_COUNT);
      imageDataspace.getSimpleExtentDims( &dimsOut.front(), 0 );

      if ( dimsOut[0] && dimsOut[1] )
      {
         
         //---
         // Capture the rectangle:
         // dimsOut[0] is height, dimsOut[1] is width:
         //---
         rect = ossimIrect( 0, 0,
                            static_cast<ossim_int32>( dimsOut[1]-1 ),
                            static_cast<ossim_int32>( dimsOut[0]-1 ) );
         
         const ossim_int32 WIDTH  = rect.width();
               
         std::vector<hsize_t> inputCount(IN_DIM_COUNT);
         std::vector<hsize_t> inputOffset(IN_DIM_COUNT);
         
         inputOffset[0] = 0;
         inputOffset[1] = 0;
         
         inputCount[0] = 1;
         inputCount[1] = WIDTH;
         
         // 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] = 1;     // single line
         outputCount[2] = WIDTH; // whole line
               
         // Output dataspace offset.
         std::vector<hsize_t> outputOffset(OUT_DIM_COUNT);
         outputOffset[0] = 0;
         outputOffset[1] = 0;
         outputOffset[2] = 0;
               
         ossimScalarType scalar = ossim_hdf5::getScalarType( &dataset );
         if ( scalar == OSSIM_FLOAT32 )
         {
            // See if we need to swap bytes:
            ossimEndian* endian = 0;
            if ( ( ossim::byteOrder() != ossim_hdf5::getByteOrder( &dataset ) ) )
            {
               endian = new ossimEndian();
            }

            // Native type:
            H5::DataType datatype = dataset.getDataType();
                  
            // Output dataspace always the same one line.
            H5::DataSpace bufferDataSpace( OUT_DIM_COUNT, &outputCount.front());
            bufferDataSpace.selectHyperslab( H5S_SELECT_SET,
                                             &outputCount.front(),
                                             &outputOffset.front() );

            //---
            // Dataset sample has NULL lines at the end so scan for valid rect.
            // Use "<= -999" for test as per NOAA as it seems the NULL value is
            // fuzzy.  e.g. -999.3.
            //---
            const ossim_float32 NULL_VALUE = -999.0;

            //---
            // VIIRS Radiance data has a -1.5e-9 in the first column.
            // Treat this as a null.
            //---
            const ossim_float32 NULL_VALUE2 = ( name == "/All_Data/VIIRS-DNB-SDR_All/Radiance" )
               ? -1.5e-9 : NULL_VALUE;
            const ossim_float32 TOLERANCE = 0.1e-9; // For ossim::almostEqual()

            // Hold one line:
            std::vector<ossim_float32> values( WIDTH );

            // Find the ul pixel:
            ossimIpt ulIpt = rect.ul();
            bool found = false;
                  
            // Line loop to find upper left pixel:
            while ( ulIpt.y <= rect.lr().y )
            {
               inputOffset[0] = static_cast<hsize_t>(ulIpt.y);
               imageDataspace.selectHyperslab( H5S_SELECT_SET,
                                               &inputCount.front(),
                                               &inputOffset.front() );
               
               // Read data from file into the buffer.
               dataset.read( (void*)&values.front(), datatype, bufferDataSpace, imageDataspace );
               
               if ( endian )
               {
                  // If the endian pointer is initialized(not zero) swap the bytes.
                  endian->swap( scalar, (void*)&values.front(), WIDTH );
               }
               
               // Sample loop:
               ulIpt.x = rect.ul().x;
               ossim_int32 index = 0;
               while ( ulIpt.x <= rect.lr().x )
               {
                  if ( !ossim::almostEqual(values[index], NULL_VALUE2, TOLERANCE) &&
                       ( values[index] > NULL_VALUE ) )
                  {
                     found = true; // Found valid pixel.
                     break;
                  }
                  ++ulIpt.x;
                  ++index;
                     
               } // End: sample loop
                     
               if ( found )
               {
                  break;
               }

               ++ulIpt.y;
                     
            } // End line loop to find ul pixel:

            // Find the lower right pixel:
            ossimIpt lrIpt = rect.lr();
            found = false;
                  
            // Line loop to find last pixel:
            while ( lrIpt.y >= rect.ul().y )
            {
               inputOffset[0] = static_cast<hsize_t>(lrIpt.y);
               imageDataspace.selectHyperslab( H5S_SELECT_SET,
                                               &inputCount.front(),
                                               &inputOffset.front() );
               
               // Read data from file into the buffer.
               dataset.read( (void*)&values.front(), datatype, bufferDataSpace, imageDataspace );

               if ( endian )
               {
                  // If the endian pointer is initialized(not zero) swap the bytes.
                  endian->swap( scalar, (void*)&values.front(), WIDTH );
               }
            
               // Sample loop:
               lrIpt.x = rect.lr().x;
               ossim_int32 index = WIDTH-1;
               
               while ( lrIpt.x >= rect.ul().x )
               {
                  if ( !ossim::almostEqual(values[index], NULL_VALUE2, TOLERANCE) &&
                       ( values[index] > NULL_VALUE ) )
                  {
                     found = true; // Found valid pixel.
                     break;
                  }
                  --lrIpt.x;
                  --index;
                     
               } // End: sample loop
                     
               if ( found )
               {
                  break;
               }

               --lrIpt.y;
                     
            } // End line loop to find lower right pixel.

            rect = ossimIrect( ulIpt, lrIpt );

            // Cleanup:
            if ( endian )
            {
               delete endian;
               endian = 0;
            }

            result = true;
            
         } 
         else // Matches: if ( scalar == OSSIM_FLOAT32 ){...}
         {
            ossimNotify(ossimNotifyLevel_WARN)
               << "ossim_hdf5::getBoundingRect WARNING!"
               << "\nUnhandled scalar type: "
               << ossimScalarTypeLut::instance()->getEntryString( scalar )
               << std::endl;
         }
               
      } // Matches: if ( dimsOut...
            
   } // Matches: if ( IN_DIM_COUNT == 2 )
         
   imageDataspace.close();

   return result;
   
} // End: ossim_hdf5::getBoundingRect(...)
Exemple #25
0
ossimRefPtr<ossimProjection> ossim_hdf5::getBilinearProjection(
   H5::DataSet& latDataSet, H5::DataSet& lonDataSet, const ossimIrect& validRect )
{
   ossimRefPtr<ossimProjection> proj = 0;

   // Get dataspace of the dataset.
   H5::DataSpace latDataSpace = latDataSet.getSpace();
   H5::DataSpace lonDataSpace = lonDataSet.getSpace();
         
   // Number of dimensions of the input dataspace:
   const ossim_int32 DIM_COUNT = latDataSpace.getSimpleExtentNdims();
         
   if ( DIM_COUNT == 2 )
   {
      // Get the extents. Assuming dimensions are same for lat lon dataset. 
      std::vector<hsize_t> dimsOut(DIM_COUNT);
      latDataSpace.getSimpleExtentDims( &dimsOut.front(), 0 );

      if ( dimsOut[0] && dimsOut[1] )
      {
         std::vector<hsize_t> inputCount(DIM_COUNT);
         std::vector<hsize_t> inputOffset(DIM_COUNT);
               
         inputOffset[0] = 0;
         inputOffset[1] = 0;
               
         inputCount[0] = 1;
         inputCount[1] = 1;
               
         // 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] = 1; // single line
         outputCount[2] = 1; // single sample
               
         // Output dataspace offset.
         std::vector<hsize_t> outputOffset(OUT_DIM_COUNT);
         outputOffset[0] = 0;
         outputOffset[1] = 0;
         outputOffset[2] = 0;
               
         ossimScalarType scalar = ossim_hdf5::getScalarType( &latDataSet );
         if ( scalar == OSSIM_FLOAT32 )
         {
            // See if we need to swap bytes:
            ossimEndian* endian = 0;
            if ( ( ossim::byteOrder() != ossim_hdf5::getByteOrder( &latDataSet ) ) )
            {
               endian = new ossimEndian();
            }

            // Native type:
            H5::DataType latDataType = latDataSet.getDataType();
            H5::DataType lonDataType = lonDataSet.getDataType();
                  
            std::vector<ossimDpt> ipts;
            std::vector<ossimGpt> gpts;
            ossimGpt gpt(0.0, 0.0, 0.0); // Assuming WGS84...
            ossim_float32 latValue = 0.0;
            ossim_float32 lonValue = 0.0;
                  
            // Only grab every 256th value.:
            const ossim_int32 GRID_SIZE = 256;
                  
            // Output dataspace always the same one pixel.
            H5::DataSpace bufferDataSpace( OUT_DIM_COUNT, &outputCount.front());
            bufferDataSpace.selectHyperslab( H5S_SELECT_SET,
                                             &outputCount.front(),
                                             &outputOffset.front() );

            //---
            // Dataset sample has NULL lines at the end so scan for valid rect.
            // Use "<= -999" for test as per NOAA as it seems the NULL value is
            // fuzzy.  e.g. -999.3.
            //---
            const ossim_float32 NULL_VALUE = -999.0;

            //---
            // Get the tie points within the valid rect:
            //---
            ossimDpt ipt = validRect.ul();
            while ( ipt.y <= validRect.lr().y )
            {
               inputOffset[0] = static_cast<hsize_t>(ipt.y);
               
               // Sample loop:
               ipt.x = validRect.ul().x;
               while ( ipt.x <= validRect.lr().x )
               {
                  inputOffset[1] = static_cast<hsize_t>(ipt.x);
                  
                  latDataSpace.selectHyperslab( H5S_SELECT_SET,
                                                &inputCount.front(),
                                                &inputOffset.front() );
                  lonDataSpace.selectHyperslab( H5S_SELECT_SET,
                                                &inputCount.front(),
                                                &inputOffset.front() );
                  
                  // Read data from file into the buffer.
                  latDataSet.read( &latValue, latDataType, bufferDataSpace, latDataSpace );
                  lonDataSet.read( &lonValue, lonDataType, bufferDataSpace, lonDataSpace );
                  
                  if ( endian )
                  {
                     // If the endian pointer is initialized(not zero) swap the bytes.
                     endian->swap( latValue );
                     endian->swap( lonValue );  
                  }
                  
                  if ( ( latValue > NULL_VALUE ) && ( lonValue > NULL_VALUE ) )
                  {
                     gpt.lat = latValue;
                     gpt.lon = lonValue;
                     gpts.push_back( gpt );
                     
                     // Add the image point subtracting the image offset.
                     ossimIpt shiftedIpt = ipt - validRect.ul();
                     ipts.push_back( shiftedIpt );
                  }

                  // Go to next point:
                  if ( ipt.x < validRect.lr().x )
                  {
                     ipt.x += GRID_SIZE;
                     if ( ipt.x > validRect.lr().x )
                     {
                        ipt.x = validRect.lr().x; // Clamp to last sample.
                     }
                  }
                  else
                  {  
                     break; // At the end:
                  }
                  
               } // End sample loop.

               if ( ipt.y < validRect.lr().y )
               {
                  ipt.y += GRID_SIZE;
                  if ( ipt.y > validRect.lr().y )
                  {
                     ipt.y = validRect.lr().y; // Clamp to last line.
                  }
               }
               else
               {  
                  break; // At the end:
               }
               
            } // End line loop.
                  
            if ( ipts.size() )
            {
               // Create the projection:
               ossimRefPtr<ossimBilinearProjection> bp = new ossimBilinearProjection();
                     
               // Add the tie points:
               bp->setTiePoints( ipts, gpts );
                     
               // Assign to output projection:
               proj = bp.get();
            }

            // Cleanup:
            if ( endian )
            {
               delete endian;
               endian = 0;
            }
         }
         else // Matches: if ( scalar == OSSIM_FLOAT32 ){...}
         {
            ossimNotify(ossimNotifyLevel_WARN)
               << "ossim_hdf5::getBilinearProjection WARNING!"
               << "\nUnhandled scalar type: "
               << ossimScalarTypeLut::instance()->getEntryString( scalar )
               << std::endl;
         }
               
      } // Matches: if ( dimsOut...
            
   } // Matches: if ( IN_DIM_COUNT == 2 )
         
   latDataSpace.close();
   lonDataSpace.close();
   
   return proj;
   
} // End: ossim_hdf5::getBilinearProjection()
Exemple #26
0
bool ossim_hdf5::crossesDateline( H5::DataSet& dataset, const ossimIrect& validRect )
{
   bool result = false;

   H5::DataSpace dataspace = dataset.getSpace();
         
   // Number of dimensions of the input dataspace:
   const ossim_int32 DIM_COUNT = dataspace.getSimpleExtentNdims();
         
   if ( DIM_COUNT == 2 )
   {
      const ossim_uint32 ROWS = validRect.height();
      const ossim_uint32 COLS = validRect.width();
      
      // Get the extents. Assuming dimensions are same for lat lon dataset. 
      std::vector<hsize_t> dimsOut(DIM_COUNT);
      dataspace.getSimpleExtentDims( &dimsOut.front(), 0 );

      if ( (ROWS <= dimsOut[0]) && (COLS <= dimsOut[1]) )
      {
         std::vector<hsize_t> inputCount(DIM_COUNT);
         std::vector<hsize_t> inputOffset(DIM_COUNT);
         
         inputCount[0] = 1;    // row
         inputCount[1] = COLS; // col
         
         // 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] = 1; // single line
         outputCount[2] = COLS; // single sample
               
         // Output dataspace offset.
         std::vector<hsize_t> outputOffset(OUT_DIM_COUNT);
         outputOffset[0] = 0;
         outputOffset[1] = 0;
         outputOffset[2] = 0;
               
         ossimScalarType scalar = ossim_hdf5::getScalarType( &dataset );
         if ( scalar == OSSIM_FLOAT32 )
         {
            // See if we need to swap bytes:
            ossimEndian* endian = 0;
            if ( ( ossim::byteOrder() != ossim_hdf5::getByteOrder( &dataset ) ) )
            {
               endian = new ossimEndian();
            }

            // Native type:
            H5::DataType datatype = dataset.getDataType();
                  
            // Output dataspace always the same one line.
            H5::DataSpace bufferDataSpace( OUT_DIM_COUNT, &outputCount.front());
            bufferDataSpace.selectHyperslab( H5S_SELECT_SET,
                                             &outputCount.front(),
                                             &outputOffset.front() );

            //---
            // Dataset sample has NULL lines at the end so scan for valid rect.
            // Use "<= -999" for test as per NOAA as it seems the NULL value is
            // fuzzy.  e.g. -999.3.
            //---

            // Buffer to hold a line:
            std::vector<ossim_float32> lineBuffer(validRect.width());

            // Read the first line:
            inputOffset[0] = static_cast<hsize_t>(validRect.ul().y);
            inputOffset[1] = static_cast<hsize_t>(validRect.ul().x);
            dataspace.selectHyperslab( H5S_SELECT_SET,
                                       &inputCount.front(),
                                       &inputOffset.front() );
            dataset.read( &(lineBuffer.front()), datatype, bufferDataSpace, dataspace );

            if ( endian )
            {
               // If the endian pointer is initialized(not zero) swap the bytes.
               endian->swap( &(lineBuffer.front()), COLS );
            }

            // Test the first line:
            result = ossim_hdf5::crossesDateline( lineBuffer );

            if ( !result )
            {
               // Test the last line:
               inputOffset[0] = static_cast<hsize_t>(validRect.ll().y);
               inputOffset[1] = static_cast<hsize_t>(validRect.ll().x);
               dataspace.selectHyperslab( H5S_SELECT_SET,
                                          &inputCount.front(),
                                          &inputOffset.front() );
               dataset.read( &(lineBuffer.front()), datatype, bufferDataSpace, dataspace );

               result = ossim_hdf5::crossesDateline( lineBuffer );
            }

            if ( endian )
            {
               delete endian;
               endian = 0;
            }
         }
         else // Matches: if ( scalar == OSSIM_FLOAT32 ){...}
         {
            ossimNotify(ossimNotifyLevel_WARN)
               << "ossim_hdf5::crossesDateline WARNING!"
               << "\nUnhandled scalar type: "
               << ossimScalarTypeLut::instance()->getEntryString( scalar )
               << std::endl;
         }
         
      } // Matches: if ( dimsOut...
      
   } // Matches: if ( IN_DIM_COUNT == 2 )
   
   dataspace.close();
   
   return result;
   
} // End: ossim_hdf5::crossesDateline(...)
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