6 #include <boost/filesystem.hpp> 18 namespace ScopedHandles {
21 if (h < 0)
return true;
26 static inline void Close(hid_t h) {
27 herr_t err = H5Fclose(h);
29 .add(
"Reason",
"Cannot close an open HDF5 file.");
33 static inline void Close(hid_t h) {
34 herr_t err = H5Dclose(h);
36 .add(
"Reason",
"Cannot close an HDF5 dataset.");
40 static inline void Close(hid_t h) {
41 herr_t err = H5Sclose(h);
43 .add(
"Reason",
"Cannot close an HDF5 dataspace.");
47 static inline void Close(hid_t h) {
48 herr_t err = H5Tclose(h);
50 .add(
"Reason",
"Cannot close an HDF5 datatype.");
56 template <
typename HandleType,
class Inval
idValueClass,
class CloseMethod>
60 bool valid()
const {
return !InvalidValueClass::isInvalid(h); }
62 if (valid()) CloseMethod::Close(h);
76 template <
class DataType>
80 .add(
"Reason",
"Unsupported type during conversion.");
83 return H5T_NATIVE_INT32;
86 return H5T_NATIVE_FLOAT;
90 void readDataset(hid_t file_id,
const char* dataset_name, std::vector<T> &outdata)
98 int rank = H5Sget_simple_extent_ndims(hSpace.
h);
100 hssize_t numElems = H5Sget_simple_extent_npoints(hSpace.
h);
104 MatchType_t funcDataType = MatchType<T>();
112 outdata.resize(numElems);
121 (
void*)outdata.data())
127 .
add(
"Reason",
"Cannot open an HDF5 dataspace.")
128 .
add(
"Dataset", dataset_name);
142 boost::filesystem::path p(filename);
143 auto pfile = p.filename();
144 #if BOOST_VERSION < 104600 145 string id = pfile.c_str();
147 string id = pfile.string().c_str();
152 .add(
"Reason",
"Cannot open a netCDF/HDF5 file.")
153 .add(
"Filename", filename);
159 auto verifyDatasetExists = [](hid_t file_id,
const char* dataset_name) ->
bool {
160 if ((H5Lexists(file_id, dataset_name, H5P_DEFAULT) <= 0))
return false;
162 if ((H5Oget_info_by_name(file_id, dataset_name, &objinfo, H5P_DEFAULT) < 0))
return false;
163 if (objinfo.type != H5O_TYPE_DATASET)
return false;
167 if (!verifyDatasetExists(hFile.
h,
"/particle_index")
168 || !verifyDatasetExists(hFile.
h,
"/sphere_index")
169 || !verifyDatasetExists(hFile.
h,
"/r")
170 || !verifyDatasetExists(hFile.
h,
"/x")
171 || !verifyDatasetExists(hFile.
h,
"/y")
172 || !verifyDatasetExists(hFile.
h,
"/z"))
174 .add(
"Reason",
"This file does not have the proper structure for a Penn State geometry file.")
175 .add(
"Filename", filename);
179 vector<float> xs, ys, zs, rs;
180 vector<int32_t> sphere_indices;
183 readDataset<int32_t>(hFile.
h,
"/sphere_index", sphere_indices);
184 readDataset<float>(hFile.
h,
"/r", rs);
190 const size_t numPoints = rs.size();
206 for (
size_t i = 0; i < numPoints; ++i) {
std::string particle_constituent_single_name
MatchType_t MatchType< float >()
uint64_t number_of_particle_scattering_elements
ScopedHandle< hid_t, InvalidHDF5Handle, CloseHDF5Datatype > H5T_handle
void readDataset(hid_t file_id, const char *dataset_name, std::vector< T > &outdata)
uint8_t particle_scattering_element_coordinates_are_integral
ScopedHandle(HandleType newh)
MatchType_t MatchType< int32_t >()
static void Close(hid_t h)
FloatData_t particle_scattering_element_radius
static void Close(hid_t h)
static void Close(hid_t h)
xError & add(const std::string &key, const T value)
float particle_scattering_element_spacing
ScopedHandle< hid_t, InvalidHDF5Handle, CloseHDF5Dataset > H5D_handle
ScopedHandle< hid_t, InvalidHDF5Handle, CloseHDF5File > H5F_handle
FloatData_t particle_scattering_element_coordinates
ShapeDataBasic readPSUfile(const std::string &filename)
Reads a Penn State-style geometry file.
ShapeCommonOptionalData optional
static void Close(hid_t h)
ScopedHandle< hid_t, InvalidHDF5Handle, CloseHDF5Dataspace > H5DS_handle
ShapeRequiredData required
uint8_t number_of_particle_constituents
static bool isInvalid(hid_t h)