void BlockReader::getBlock(int level, int blkno, uchar* block) { int lod2 = qPow(2, level); int bb = m_blockSize/lod2; int dno = blkno/(m_wblocks*m_hblocks); int wno = (blkno - dno*m_wblocks*m_hblocks)/m_hblocks; int hno = blkno - dno*m_wblocks*m_hblocks - wno*m_hblocks; if (! m_hdf5file) { QString filename; filename = m_baseFilename + ".h5"; m_hdf5file = new H5File(filename.toAscii().data(), H5F_ACC_RDONLY); for(int il=0; il<m_minLevel; il++) { QString dataname = QString("lod-%1").arg(il); m_hdf5dataset[il] = m_hdf5file->openDataSet(dataname.toAscii().data()); } m_dataType.copy(m_hdf5dataset[0]); } DataSpace dataspace = m_hdf5dataset[level].getSpace(); hsize_t offset[3], count[3]; offset[0] = dno*bb; offset[1] = wno*bb; offset[2] = hno*bb; count[0] = count[1] = count[2] = bb; count[0] = qMin(m_blockSize, m_depth -dno*m_blockSize-1)/lod2; count[1] = qMin(m_blockSize, m_width -wno*m_blockSize-1)/lod2; count[2] = qMin(m_blockSize, m_height-hno*m_blockSize-1)/lod2; dataspace.selectHyperslab( H5S_SELECT_SET, count, offset ); hsize_t mdim[3]; mdim[0] = bb; mdim[1] = bb; mdim[2] = bb; hsize_t memoffset[3]; memoffset[0] = 0; memoffset[1] = 0; memoffset[2] = 0; DataSpace memspace( 3, mdim ); memspace.selectHyperslab(H5S_SELECT_SET, count, memoffset); m_hdf5dataset[level].read( block, m_dataType, memspace, dataspace ); }
/** * @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; }
int write_data(GroupAccess *group, const Field_Data *field, double *buf, std::string dataset_str) { // ======================================== // Initialize all variables // ======================================== double *_buf; hsize_t count[3]; hsize_t offset[3]; std::string temp_str; herr_t status; // ======================================== // Create dataset // ======================================== // The total array of particles is (n_pts*num_processes, 6) in size // Remember that each slave has n_pts of different particles! int rank = 3; int x_len = field->x_pts; int y_len = field->y_pts; int z_len = field->z_pts; /* std::cout << "x_len: " << x_len << std::endl; */ /* std::cout << "y_len: " << y_len << std::endl; */ /* std::cout << "z_len: " << z_len << std::endl; */ count[0] = x_len; count[1] = y_len; count[2] = z_len; DatasetAccess dataset(group->group_id, dataset_str, rank, count); // Updates dataspace_id DataspaceCreate memspace(rank, count); // ======================================== // Write dataset rows // ======================================== _buf = new double[x_len*y_len*z_len]; for (int i=0; i < x_len; i++) { for (int j=0; j < y_len; j++) { for (int k=0; k < z_len; k++) { _buf[k + z_len*(i + j*x_len)] = buf[field->_index(i, j, k)]; } } } status = H5Dwrite(dataset.dataset_id, H5T_NATIVE_DOUBLE, memspace.dataspace_id, dataset.dataspace_id, H5P_DEFAULT, _buf); if (status > 0) std::cout << "Something bad" << std::endl; delete [] _buf; return 0; }
/** * @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; }
/** * 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; }
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; }