Working on HDF5 reader

This commit is contained in:
Yohai Meiron 2020-03-26 19:56:12 -04:00
parent f065566c6b
commit 0b9fa6e46d

91
io.cpp
View file

@ -1,5 +1,7 @@
#include "hdf5.h" #include "hdf5.h"
#include <string> #include <string>
#include <vector>
#include <algorithm>
struct double3 {double x,y,z;}; struct double3 {double x,y,z;};
void h5_write(const std::string file_name, const int step_num, const double t, const int N, const double *m, const double3 *x, const double3 *v, const double *pot, const double3 *acc, const double3 *jrk, const int write_mode=0, const bool use_double_precision=true) void h5_write(const std::string file_name, const int step_num, const double t, const int N, const double *m, const double3 *x, const double3 *v, const double *pot, const double3 *acc, const double3 *jrk, const int write_mode=0, const bool use_double_precision=true)
@ -9,9 +11,8 @@ void h5_write(const std::string file_name, const int step_num, const double t, c
bool write_acc = (write_mode>>1)%2; bool write_acc = (write_mode>>1)%2;
bool write_jrk = (write_mode>>2)%2; bool write_jrk = (write_mode>>2)%2;
hid_t file_id, group_id, attribute_id, dataset_id, dataspace_id; /* identifiers */ hid_t file_id, group_id, attribute_id, dataset_id, dataspace_id;
hsize_t dims[2] = {(hsize_t)N, 3}; hsize_t dims[2] = {(hsize_t)N, 3};
herr_t status;
hid_t h5_float_type; hid_t h5_float_type;
if (use_double_precision) h5_float_type = H5T_IEEE_F64LE; if (use_double_precision) h5_float_type = H5T_IEEE_F64LE;
@ -23,53 +24,99 @@ void h5_write(const std::string file_name, const int step_num, const double t, c
dataspace_id = H5Screate(H5S_SCALAR); dataspace_id = H5Screate(H5S_SCALAR);
attribute_id = H5Acreate2 (group_id, "Time", H5T_IEEE_F64LE, dataspace_id, H5P_DEFAULT, H5P_DEFAULT); attribute_id = H5Acreate2 (group_id, "Time", H5T_IEEE_F64LE, dataspace_id, H5P_DEFAULT, H5P_DEFAULT);
H5Awrite(attribute_id, H5T_NATIVE_DOUBLE, &t); H5Awrite(attribute_id, H5T_NATIVE_DOUBLE, &t);
H5Sclose(dataspace_id);
dataspace_id = H5Screate_simple(1, dims, NULL); dataspace_id = H5Screate_simple(1, dims, NULL);
sprintf(dataset_path, "%s/MASS", group_name); sprintf(dataset_path, "%s/MASS", group_name);
dataset_id = H5Dcreate2(file_id, dataset_path, h5_float_type, dataspace_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); dataset_id = H5Dcreate2(file_id, dataset_path, h5_float_type, dataspace_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
H5Dwrite(dataset_id, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, m); H5Dwrite(dataset_id, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, m);
H5Dclose(dataset_id);
H5Sclose(dataspace_id);
dataspace_id = H5Screate_simple(2, dims, NULL); dataspace_id = H5Screate_simple(2, dims, NULL);
sprintf(dataset_path, "%s/X", group_name); sprintf(dataset_path, "%s/X", group_name);
dataset_id = H5Dcreate2(file_id, dataset_path, h5_float_type, dataspace_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); dataset_id = H5Dcreate2(file_id, dataset_path, h5_float_type, dataspace_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
H5Dwrite(dataset_id, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, x); H5Dwrite(dataset_id, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, x);
H5Dclose(dataset_id);
H5Sclose(dataspace_id);
dataspace_id = H5Screate_simple(2, dims, NULL); dataspace_id = H5Screate_simple(2, dims, NULL);
sprintf(dataset_path, "%s/V", group_name); sprintf(dataset_path, "%s/V", group_name);
dataset_id = H5Dcreate2(file_id, dataset_path, h5_float_type, dataspace_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); dataset_id = H5Dcreate2(file_id, dataset_path, h5_float_type, dataspace_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
H5Dwrite(dataset_id, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, v); H5Dwrite(dataset_id, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, v);
H5Dclose(dataset_id);
H5Sclose(dataspace_id);
if (write_pot) { if (write_pot) {
dataspace_id = H5Screate_simple(1, dims, NULL); dataspace_id = H5Screate_simple(1, dims, NULL);
sprintf(dataset_path, "%s/POT", group_name); sprintf(dataset_path, "%s/POT", group_name);
dataset_id = H5Dcreate2(file_id, dataset_path, h5_float_type, dataspace_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); dataset_id = H5Dcreate2(file_id, dataset_path, h5_float_type, dataspace_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
H5Dwrite(dataset_id, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, pot); H5Dwrite(dataset_id, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, pot);
H5Dclose(dataset_id);
H5Sclose(dataspace_id);
} }
if (write_acc) { if (write_acc) {
dataspace_id = H5Screate_simple(2, dims, NULL); dataspace_id = H5Screate_simple(2, dims, NULL);
sprintf(dataset_path, "%s/ACC", group_name); sprintf(dataset_path, "%s/ACC", group_name);
dataset_id = H5Dcreate2(file_id, dataset_path, h5_float_type, dataspace_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); dataset_id = H5Dcreate2(file_id, dataset_path, h5_float_type, dataspace_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
H5Dwrite(dataset_id, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, acc); H5Dwrite(dataset_id, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, acc);
H5Dclose(dataset_id);
H5Sclose(dataspace_id);
} }
if (write_jrk) { if (write_jrk) {
dataspace_id = H5Screate_simple(2, dims, NULL); dataspace_id = H5Screate_simple(2, dims, NULL);
sprintf(dataset_path, "%s/JRK", group_name); sprintf(dataset_path, "%s/JRK", group_name);
dataset_id = H5Dcreate2(file_id, dataset_path, h5_float_type, dataspace_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); dataset_id = H5Dcreate2(file_id, dataset_path, h5_float_type, dataspace_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
H5Dwrite(dataset_id, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, jrk); H5Dwrite(dataset_id, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, jrk);
}
H5Dclose(dataset_id); H5Dclose(dataset_id);
H5Gclose(group_id);
H5Sclose(dataspace_id); H5Sclose(dataspace_id);
}
H5Gclose(group_id);
group_id = H5Gcreate2(file_id, "/Step#22", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
H5Gclose(group_id);
H5Fclose(file_id); H5Fclose(file_id);
} }
herr_t file_info(hid_t loc_id, const char *name, void *opdata)
{
H5G_stat_t statbuf;
/*
* Get type of the object and display its name and type.
* The name of the object is passed to this function by
* the Library. Some magic :-)
*/
H5Gget_objinfo(loc_id, name, 0, &statbuf);
switch (statbuf.type) {
case H5G_GROUP:
printf(" Object with name %s is a group \n", name);
break;
case H5G_DATASET:
printf(" Object with name %s is a dataset \n", name);
break;
case H5G_TYPE:
printf(" Object with name %s is a named datatype \n", name);
break;
default:
printf(" Unable to identify an object ");
}
return 0;
}
int main() int main()
{ {
double3 aaa; // double3 aaa;
double pot; // double pot;
int N = 25; int N = 25;
double3 x[N]; double3 x[N];
for (int i=0; i<N; i++) { for (int i=0; i<N; i++) {
@ -78,4 +125,30 @@ int main()
x[i].z=i+0.03; x[i].z=i+0.03;
} }
h5_write("new_file.h5", 34, 987.654, N, (double*)x, x, x, (double*)x, x, x, 7); h5_write("new_file.h5", 34, 987.654, N, (double*)x, x, x, (double*)x, x, x, 7);
auto file_id = H5Fopen("new_file.h5", H5F_ACC_RDONLY, H5P_DEFAULT);
auto group_id = H5Gopen2(file_id, "/", H5P_DEFAULT);
H5G_info_t object_info;
H5Gget_info(group_id, &object_info);
std::vector<int> step_num;
for (int i=0; i<object_info.nlinks; i++) {
char name_cstr[256];
H5Gget_objname_by_idx(group_id, i, name_cstr, 256);
std::string name(name_cstr);
if (name.substr(0, 5) == "Step#") step_num.push_back(std::stoi(name.substr(5, std::string::npos)));
}
int step_num_max = *max_element(step_num.begin(), step_num.end());
char dataset_path[256];
sprintf(dataset_path, "/Step#%d/X", step_num_max);
double dset_data[1024*1024];
auto dataset_id = H5Dopen2(file_id, dataset_path, H5P_DEFAULT);
H5Dread(dataset_id, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, dset_data);
// printf("aaaa %d\n", step_num_max);
} }