aboutsummaryrefslogtreecommitdiff
path: root/src/hdf5-file.c
diff options
context:
space:
mode:
authorThomas White <taw@bitwiz.org.uk>2009-10-14 18:30:49 +0200
committerThomas White <taw@bitwiz.org.uk>2009-10-14 18:30:49 +0200
commit69b33e88a4ebecc5bc6e2dae8b35643f26cdb22d (patch)
tree4ca274d0990c827486dc2ebfc99ab5723dd382ef /src/hdf5-file.c
parent4e1e2ef4472e28a146e6c83d053f1fc4d2419f88 (diff)
Write an HDF5 file
Diffstat (limited to 'src/hdf5-file.c')
-rw-r--r--src/hdf5-file.c76
1 files changed, 76 insertions, 0 deletions
diff --git a/src/hdf5-file.c b/src/hdf5-file.c
new file mode 100644
index 00000000..2992357d
--- /dev/null
+++ b/src/hdf5-file.c
@@ -0,0 +1,76 @@
+/*
+ * hdf5.c
+ *
+ * Read/write HDF5 data files
+ *
+ * (c) 2006-2009 Thomas White <thomas.white@desy.de>
+ *
+ * template_index - Indexing diffraction patterns by template matching
+ *
+ */
+
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <hdf5.h>
+
+
+int hdf5_write(const char *filename, const uint16_t *data,
+ int width, int height)
+{
+ hid_t fh, gh, sh, dh; /* File, group, dataspace and data handles */
+ herr_t r;
+ hsize_t size[2];
+ hsize_t max_size[2];
+
+ fh = H5Fcreate(filename, H5F_ACC_EXCL, H5P_DEFAULT, H5P_DEFAULT);
+ if ( fh < 0 ) {
+ fprintf(stderr, "Couldn't create file: %s\n", filename);
+ return 1;
+ }
+
+ gh = H5Gcreate(fh, "data", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
+ if ( gh < 0 ) {
+ fprintf(stderr, "Couldn't create group\n");
+ H5Fclose(fh);
+ return 1;
+ }
+
+ size[0] = width;
+ size[1] = height;
+ max_size[0] = width;
+ max_size[1] = height;
+ sh = H5Screate_simple(2, size, max_size);
+
+ dh = H5Dcreate(gh, "data", H5T_NATIVE_UINT16, sh,
+ H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
+ if ( dh < 0 ) {
+ fprintf(stderr, "Couldn't create dataset\n");
+ H5Fclose(fh);
+ return 1;
+ }
+
+ /* Muppet check */
+ H5Sget_simple_extent_dims(sh, size, max_size);
+ printf("Data dimensions %i %i (max %i %i)\n",
+ (int)size[1], (int)size[0],
+ (int)max_size[1], (int)max_size[0]);
+
+ r = H5Dwrite(dh, H5T_NATIVE_UINT16, H5S_ALL,
+ H5S_ALL, H5P_DEFAULT, data);
+ if ( r < 0 ) {
+ fprintf(stderr, "Couldn't write data\n");
+ H5Dclose(dh);
+ H5Fclose(fh);
+ return 1;
+ }
+
+ H5Fclose(fh);
+
+ return 0;
+}