Last active
January 2, 2016 07:28
-
-
Save shelling/8269862 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <hdf5.h> | |
double ** | |
Plain_init(int x, int y) { | |
double **plain = malloc(sizeof(double*)*x); | |
double *space = malloc(sizeof(double)*x*y); | |
for (int i = 0; i < x; i++) { | |
plain[i] = space + i * y; | |
} | |
return plain; | |
} | |
herr_t | |
Plain_read(double** plain, hid_t dataset) { | |
return H5Dread( | |
dataset, H5T_NATIVE_DOUBLE, | |
H5S_ALL, H5S_ALL, H5P_DEFAULT, | |
&(plain[0][0]) | |
); | |
} | |
herr_t | |
Plain_write(double** plain, hid_t dataset) { | |
H5Dwrite( | |
dataset, H5T_NATIVE_DOUBLE, | |
H5S_ALL, H5S_ALL, H5P_DEFAULT, | |
&(plain[0][0]) | |
); | |
} | |
double *** | |
Cube_init(int x, int y, int z) { | |
double ***cube = malloc(sizeof(double**)*x); | |
double **slice = malloc(sizeof(double*)*x*y); | |
double *space = malloc(sizeof(double)*x*y*z); | |
for (int i = 0; i < x; i++) { | |
cube[i] = slice + i * y; | |
} | |
for (int i = 0; i < x; i++) { | |
for (int j = 0; j < y; j++) { | |
cube[i][j] = space + i * y * z + j * z; | |
} | |
} | |
return cube; | |
} | |
herr_t | |
Cube_read(double ***cube, hid_t dataset) { | |
return H5Dread( | |
dataset, H5T_NATIVE_DOUBLE, | |
H5S_ALL, H5S_ALL, H5P_DEFAULT, | |
&(cube[0][0][0]) | |
); | |
} | |
herr_t | |
Cube_write(double ***cube, hid_t dataset) { | |
return H5Dwrite( | |
dataset, H5T_NATIVE_DOUBLE, | |
H5S_ALL, H5S_ALL, H5P_DEFAULT, | |
&(cube[0][0][0]) | |
); | |
} | |
int main() { | |
hid_t file_id; | |
herr_t status; | |
file_id = H5Fcreate("test.hdf5", H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); | |
H5Gcreate2(file_id, "/foo", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); | |
H5Gcreate2(file_id, "/bar", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); | |
hsize_t dims[2] = { 3, 3 }; | |
hid_t dataspace_id = H5Screate_simple(2, dims, NULL); | |
hid_t dataset_id = H5Dcreate(file_id, "dataset", H5T_NATIVE_DOUBLE, dataspace_id, | |
H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); | |
double **init = Plain_init(3, 3); | |
Plain_read(init, dataset_id); | |
for (int i = 0; i < 3; i++) { | |
for (int j = 0; j < 3; j++) { | |
printf("%f\n", init[i][j]); | |
} | |
} | |
double **plain = Plain_init(3, 3); | |
for (int i = 0; i < 3; i++) { | |
for (int j = 0; j < 3; j++) { | |
plain[i][j] = (double)i + (double)j; | |
} | |
} | |
Plain_write(plain, dataset_id); | |
status = H5Dclose(dataset_id); | |
printf("close dataset %i\n", status); | |
status = H5Sclose(dataspace_id); | |
printf("close dataspace %i\n", status); | |
status = H5Fclose(file_id); | |
printf("close file %i\n", status); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment