-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathbag_simplelayer.cpp
More file actions
285 lines (227 loc) · 8.75 KB
/
bag_simplelayer.cpp
File metadata and controls
285 lines (227 loc) · 8.75 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#include "bag_attributeinfo.h"
#include "bag_private.h"
#include "bag_simplelayer.h"
#include "bag_simplelayerdescriptor.h"
#include <algorithm>
#include <array>
#include <H5Cpp.h>
namespace BAG {
//! Constructor.
/*!
\param dataset
The BAG Dataset this layer belongs to.
\param descriptor
The descriptor of this layer.
\param pH5dataSet
The HDF5 DataSet that stores this layer.
*/
SimpleLayer::SimpleLayer(
Dataset& dataset,
SimpleLayerDescriptor& descriptor,
std::unique_ptr<::H5::DataSet, DeleteH5dataSet> pH5dataSet)
: Layer(dataset, descriptor)
, m_pH5dataSet(std::move(pH5dataSet))
{
}
//! Create a new simple layer.
/*!
\param dataset
The BAG Dataset this layer belongs to.
\param type
The type of layer.
\param chunkSize
The chunk size the HDF5 DataSet will use.
\param compressionLevel
The compression level the HDF5 DataSet will use.
\return
The new simple layer.
*/
std::shared_ptr<SimpleLayer> SimpleLayer::create(
Dataset& dataset,
LayerType type,
uint32_t rows, uint32_t cols,
uint64_t chunkSize,
int compressionLevel)
{
auto descriptor = SimpleLayerDescriptor::create(dataset, type, rows, cols, chunkSize, compressionLevel);
auto h5dataSet = SimpleLayer::createH5dataSet(dataset, *descriptor);
return std::make_shared<SimpleLayer>(dataset, *descriptor, std::move(h5dataSet));
}
//! Open an existing simple layer.
/*!
\param dataset
The BAG Dataset this layer belongs to.
\param descriptor
The descriptor of this layer.
\return
The specified simple layer.
*/
std::shared_ptr<SimpleLayer> SimpleLayer::open(
Dataset& dataset,
SimpleLayerDescriptor& descriptor)
{
const auto& h5file = dataset.getH5file();
auto h5dataSet = std::unique_ptr<::H5::DataSet, DeleteH5dataSet>(
new ::H5::DataSet{h5file.openDataSet(descriptor.getInternalPath())},
DeleteH5dataSet{});
// Configure the layer dimensions in the descriptor (we implicitally expect the layer
// to be two-dimensional)
hsize_t dims[2];
h5dataSet->getSpace().getSimpleExtentDims(dims);
descriptor.setDims(dims[0], dims[1]);
// Read the min/max attribute values.
const auto possibleMinMax = dataset.getMinMax(descriptor.getLayerType());
if (std::get<0>(possibleMinMax))
descriptor.setMinMax(std::get<1>(possibleMinMax),
std::get<2>(possibleMinMax));
return std::make_shared<SimpleLayer>(dataset, descriptor,std::move(h5dataSet));
}
//! Create the HDF5 DataSet.
/*!
\param dataset
The BAG Dataset this layer belongs to.
\param descriptor
The descriptor of this layer.
\return
The new HDF5 DataSet.
*/
std::unique_ptr<::H5::DataSet, DeleteH5dataSet>
SimpleLayer::createH5dataSet(
const Dataset& dataset,
const SimpleLayerDescriptor& descriptor)
{
uint32_t dim0 = 0, dim1 = 0;
std::tie(dim0, dim1) = descriptor.getDims();
const std::array<hsize_t, kRank> fileDims{dim0, dim1};
::H5::DataSpace h5dataSpace{kRank, fileDims.data(), fileDims.data()};
::H5::FloatType h5dataType;
h5dataType.copy(::H5::PredType::NATIVE_FLOAT);
h5dataType.setOrder(H5T_ORDER_LE);
// Create the creation property list.
const ::H5::DSetCreatPropList h5createPropList{};
h5createPropList.setFillTime(H5D_FILL_TIME_ALLOC);
constexpr float kFillValue = BAG_NULL_ELEVATION;
h5createPropList.setFillValue(h5dataType, &kFillValue);
// Use chunk size and compression level from the descriptor.
const auto compressionLevel = descriptor.getCompressionLevel();
const auto chunkSize = descriptor.getChunkSize();
if (chunkSize > 0)
{
const std::array<hsize_t, kRank> chunkDims{chunkSize, chunkSize};
h5createPropList.setChunk(kRank, chunkDims.data());
if (compressionLevel > 0 && compressionLevel <= kMaxCompressionLevel)
h5createPropList.setDeflate(compressionLevel);
}
else if (compressionLevel > 0)
throw CompressionNeedsChunkingSet{};
// Create the DataSet using the above.
const auto& h5file = dataset.getH5file();
auto pH5dataSet = std::unique_ptr<::H5::DataSet, DeleteH5dataSet>(
new ::H5::DataSet{h5file.createDataSet(descriptor.getInternalPath(),
h5dataType, h5dataSpace, h5createPropList)},
DeleteH5dataSet{});
// Create any attributes.
const auto attInfo = getAttributeInfo(descriptor.getLayerType());
const ::H5::DataSpace minElevDataSpace{};
const auto minElevAtt = pH5dataSet->createAttribute(attInfo.minName,
attInfo.h5type, minElevDataSpace);
const ::H5::DataSpace maxElevDataSpace{};
const auto maxElevAtt = pH5dataSet->createAttribute(attInfo.maxName,
attInfo.h5type, maxElevDataSpace);
// Set initial min/max values.
constexpr float minElev = std::numeric_limits<float>::max();
minElevAtt.write(attInfo.h5type, &minElev);
constexpr float maxElev = std::numeric_limits<float>::lowest();
maxElevAtt.write(attInfo.h5type, &maxElev);
return pH5dataSet;
}
//! \copydoc Layer::read
UInt8Array SimpleLayer::readProxy(
uint32_t rowStart,
uint32_t columnStart,
uint32_t rowEnd,
uint32_t columnEnd) const
{
// Query the file for the specified rows and columns.
const auto h5fileDataSpace = m_pH5dataSet->getSpace();
const auto rows = (rowEnd - rowStart) + 1;
const auto columns = (columnEnd - columnStart) + 1;
const std::array<hsize_t, kRank> count{rows, columns};
const std::array<hsize_t, kRank> offset{rowStart, columnStart};
h5fileDataSpace.selectHyperslab(H5S_SELECT_SET, count.data(), offset.data());
// Initialize the output buffer.
const auto bufferSize = this->getDescriptor()->getReadBufferSize(rows,
columns);
UInt8Array buffer{bufferSize};
// Prepare the memory space.
const ::H5::DataSpace h5memSpace{kRank, count.data(), count.data()};
m_pH5dataSet->read(buffer.data(), H5Dget_type(m_pH5dataSet->getId()),
h5memSpace, h5fileDataSpace);
return buffer;
}
//! \copydoc Layer::writeAttributes
void SimpleLayer::writeAttributesProxy() const
{
auto pDescriptor = this->getDescriptor();
const auto attInfo = getAttributeInfo(pDescriptor->getLayerType());
// Write any attributes, from the layer descriptor.
// min value
const auto minMax = pDescriptor->getMinMax();
const auto minAtt = m_pH5dataSet->openAttribute(attInfo.minName);
minAtt.write(attInfo.h5type, &std::get<0>(minMax));
// max value
const auto maxAtt = m_pH5dataSet->openAttribute(attInfo.maxName);
maxAtt.write(attInfo.h5type, &std::get<1>(minMax));
}
//! \copydoc Layer::write
void SimpleLayer::writeProxy(
uint32_t rowStart,
uint32_t columnStart,
uint32_t rowEnd,
uint32_t columnEnd,
const uint8_t* buffer)
{
auto h5fileDataSpace = m_pH5dataSet->getSpace();
// Make sure the area being written to does not exceed the file dimensions.
std::array<hsize_t, kRank> fileDims{};
h5fileDataSpace.getSimpleExtentDims(fileDims.data());
if ((rowEnd >= fileDims[0]) || (columnEnd >= fileDims[1]))
throw InvalidWriteSize{};
const auto rows = (rowEnd - rowStart) + 1;
const auto columns = (columnEnd - columnStart) + 1;
const std::array<hsize_t, kRank> count{rows, columns};
const std::array<hsize_t, kRank> offset{rowStart, columnStart};
h5fileDataSpace.selectHyperslab(H5S_SELECT_SET, count.data(), offset.data());
// Prepare the memory space.
const ::H5::DataSpace h5memDataSpace{kRank, count.data(), count.data()};
m_pH5dataSet->write(buffer, H5Dget_type(m_pH5dataSet->getId()),
h5memDataSpace, h5fileDataSpace);
// Update min/max attributes
auto pDescriptor = this->getDescriptor();
const auto attInfo = getAttributeInfo(pDescriptor->getLayerType());
float min = 0.f, max = 0.f;
if (attInfo.h5type == ::H5::PredType::NATIVE_FLOAT)
{
const auto* floatBuffer = reinterpret_cast<const float*>(buffer);
const auto begin = floatBuffer;
const auto end = floatBuffer + rows * columns;
const auto mm = std::minmax_element(begin, end);
min = *std::get<0>(mm);
max = *std::get<1>(mm);
}
else if (attInfo.h5type == ::H5::PredType::NATIVE_UINT32)
{
const auto* uint32Buffer = reinterpret_cast<const uint32_t*>(buffer);
const auto begin = uint32Buffer;
const auto end = uint32Buffer + rows * columns;
const auto mm = std::minmax_element(begin, end);
min = static_cast<float>(*std::get<0>(mm));
max = static_cast<float>(*std::get<1>(mm));
}
else
throw UnsupportedAttributeType{};
const float currentMin = std::get<0>(pDescriptor->getMinMax());
const float currentMax = std::get<1>(pDescriptor->getMinMax());
pDescriptor->setMinMax(std::min(currentMin, min), std::max(currentMax, max));
}
} //namespace BAG