-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHDFDataReader.cpp
More file actions
executable file
·147 lines (122 loc) · 4.82 KB
/
HDFDataReader.cpp
File metadata and controls
executable file
·147 lines (122 loc) · 4.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
//
// Created by Arkadiy Simonov on 02.10.2024.
//
#include "HDFDataReader.h"
#include "H5Cpp.h"
using namespace H5;
#include <stdexcept>
#include <iostream>
#include <filesystem>
#include <cassert>
//TODO: delete this once everything works
//std::pair<std::string, std::string> parseHDF5FilenameAndDataset(const std::string& filename_and_dataset) {
// size_t colonPos = filename_and_dataset.find(':');
// if (colonPos == std::string::npos) {
// throw std::invalid_argument("Invalid filename and dataset format. Expected 'filename.h5:/dataset'");
// }
//
// std::string filename = filename_and_dataset.substr(0, colonPos);
// std::string dataset_name = filename_and_dataset.substr(colonPos + 1);
//
// return std::make_pair(filename, dataset_name);
//}
#include "H5PLextern.h"
#include "bitshuffle/bitshuffle.h"
#include "bitshuffle/bshuf_h5filter.h"
#include <cassert>
HDFDataReader::HDFDataReader(string filename, string dataset_name) :
m_filename(filename), m_dataset_name(dataset_name) {
// Check if file exists
if (!std::filesystem::exists(filename)) {
std::cerr << "Error: File does not exist: " << filename << std::endl;
throw std::runtime_error("File not found");
}
// Read in the files
try {
/*
* Similarly, check for availability of the shuffle filter.
*/
bshuf_register_h5filter();
auto avail = H5Zfilter_avail(H5Z_FILTER_SHUFFLE);
if (!avail) {
std::cout << "Shuffle filter not available.\n";
}
in = H5File(filename, H5F_ACC_RDONLY);
dataset = in.openDataSet(dataset_name);
// If we reach here, file and dataset were opened successfully
std::cout << "Successfully opened file: " << filename << " and dataset: " << dataset_name << std::endl;
// Perform your operations with the file and dataset here
/*
* Get dataspace of the dataset.
*/
DataSpace dataspace = dataset.getSpace();
/*
* Get the number of dimensions in the dataspace.
*/
int rank = dataspace.getSimpleExtentNdims();
assert(rank==3);
/*
* Get the dimension size of each dimension in the dataspace and
* display them.
*/
hsize_t dims_in[3];
int ndims = dataspace.getSimpleExtentDims( dims_in, NULL);
assert(ndims == 3);
// dims_in layout is [frames, slow, fast].
// Convention in this codebase (matching CBF): m_dim1 = fast (width) → ny(), m_dim2 = slow (height) → nx()
m_no_frames = dims_in[0]; m_dim1 = dims_in[2]; m_dim2 = dims_in[1];
} catch (H5::FileIException& file_error) {
std::cerr << "Error opening file: " << filename << std::endl;
std::cerr << "Error details: " << file_error.getDetailMsg() << std::endl;
throw std::runtime_error("HDF5 file error");
} catch (H5::DataSetIException& dataset_error) {
std::cerr << "Error opening dataset: " << dataset_name << " in file: " << filename << std::endl;
std::cerr << "Error details: " << dataset_error.getDetailMsg() << std::endl;
throw std::runtime_error("HDF5 dataset error");
} catch (H5::Exception& hdf5_error) {
std::cerr << "An HDF5 error occurred while processing file: " << filename << std::endl;
std::cerr << "Error details: " << hdf5_error.getDetailMsg() << std::endl;
throw std::runtime_error("HDF5 error");
} catch (std::exception& e) {
std::cerr << "An unexpected error occurred: " << e.what() << std::endl;
throw;
}
}
void HDFDataReader::read_frame(int frame_no, int* out) {
DataSpace dataspace = dataset.getSpace();
/*
* Define hyperslab in the dataset; implicitly giving stride and
* block NULL.
*/
hsize_t offset[3]; // hyperslab offset in the file
hsize_t count[3]; // size of the hyperslab in the file
offset[0] = frame_no;
offset[1] = 0;
offset[2] = 0;
count[0] = 1;
count[1] = dim2(); // slow (height) — HDF5 dimension index 1
count[2] = dim1(); // fast (width) — HDF5 dimension index 2
dataspace.selectHyperslab( H5S_SELECT_SET, count, offset );
/*
* Define the memory dataspace.
*/
hsize_t dimsm[2]; /* memory space dimensions */
dimsm[0] = dim2(); // slow (height)
dimsm[1] = dim1(); // fast (width)
DataSpace memspace( 2, dimsm );
/*
* Define memory hyperslab.
*/
hsize_t offset_out[2]; // hyperslab offset in memory
hsize_t count_out[2]; // size of the hyperslab in memory
offset_out[0] = 0;
offset_out[1] = 0;
count_out[0] = dim2(); // slow (height)
count_out[1] = dim1(); // fast (width)
memspace.selectHyperslab( H5S_SELECT_SET, count_out, offset_out );
/*
* Read data from hyperslab in the file into the hyperslab in
* memory and display the data.
*/
dataset.read( out, PredType::NATIVE_INT32, memspace, dataspace );
}