From 69b33e88a4ebecc5bc6e2dae8b35643f26cdb22d Mon Sep 17 00:00:00 2001 From: Thomas White Date: Wed, 14 Oct 2009 18:30:49 +0200 Subject: Write an HDF5 file --- configure.ac | 2 ++ src/Makefile.am | 5 ++-- src/hdf5-file.c | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/hdf5-file.h | 24 ++++++++++++++++++ src/image.c | 17 +++++++++++++ src/image.h | 2 ++ src/sim-main.c | 31 +++++++++++++++++------ 7 files changed, 147 insertions(+), 10 deletions(-) create mode 100644 src/hdf5-file.c create mode 100644 src/hdf5-file.h diff --git a/configure.ac b/configure.ac index 9a495108..7ed085ff 100644 --- a/configure.ac +++ b/configure.ac @@ -18,4 +18,6 @@ AC_FUNC_MALLOC AC_TYPE_SIGNAL AC_CHECK_FUNCS([strdup]) +LIBS="$LIBS -lhdf5" + AC_OUTPUT(Makefile src/Makefile) diff --git a/src/Makefile.am b/src/Makefile.am index 46a5e9d9..ccb4527d 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,8 +1,9 @@ bin_PROGRAMS = template_index simulate_patterns -template_index_SOURCES = main.c relrod.c utils.c image.c cell.c +template_index_SOURCES = main.c relrod.c utils.c image.c cell.c hdf5-file.c template_index_LDADD = @LIBS@ -lm -lgsl -lgslcblas AM_CFLAGS = -Wall -g -simulate_patterns_SOURCES = sim-main.c relrod.c utils.c image.c cell.c +simulate_patterns_SOURCES = sim-main.c relrod.c utils.c image.c cell.c \ + hdf5-file.c simulate_patterns_LDADD = @LIBS@ -lm -lgsl -lgslcblas 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 + * + * template_index - Indexing diffraction patterns by template matching + * + */ + + +#ifdef HAVE_CONFIG_H +#include +#endif + +#include +#include +#include +#include + + +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; +} diff --git a/src/hdf5-file.h b/src/hdf5-file.h new file mode 100644 index 00000000..fa0de15f --- /dev/null +++ b/src/hdf5-file.h @@ -0,0 +1,24 @@ +/* + * hdf5.h + * + * Read/write HDF5 data files + * + * (c) 2006-2009 Thomas White + * + * template_index - Indexing diffraction patterns by template matching + * + */ + +#ifdef HAVE_CONFIG_H +#include +#endif + +#ifndef HDF5_H +#define HDF5_H + +#include + +extern int hdf5_write(const char *filename, const uint16_t *data, + int width, int height); + +#endif /* HDF5_H */ diff --git a/src/image.c b/src/image.c index d3e65685..b7cf4c31 100644 --- a/src/image.c +++ b/src/image.c @@ -145,3 +145,20 @@ struct imagefeature *image_feature_closest(ImageFeatureList *flist, *d = +INFINITY; return NULL; } + + +int image_feature_count(ImageFeatureList *flist) +{ + if ( flist == NULL ) return 0; + return flist->n_features; +} + + +struct imagefeature *image_get_feature(ImageFeatureList *flist, int idx) +{ + /* Sanity check */ + if ( flist == NULL ) return NULL; + if ( idx > flist->n_features ) return NULL; + + return &flist->features[idx]; +} diff --git a/src/image.h b/src/image.h index fc8a1f2f..41e6e2b6 100644 --- a/src/image.h +++ b/src/image.h @@ -100,5 +100,7 @@ extern struct imagefeature *image_feature_closest(ImageFeatureList *flist, double x, double y, double *d, int *idx); +extern int image_feature_count(ImageFeatureList *flist); +extern struct imagefeature *image_get_feature(ImageFeatureList *flist, int idx); #endif /* IMAGE_H */ diff --git a/src/sim-main.c b/src/sim-main.c index 23ba3e99..af7f57bd 100644 --- a/src/sim-main.c +++ b/src/sim-main.c @@ -24,6 +24,7 @@ #include "relrod.h" #include "cell.h" #include "utils.h" +#include "hdf5-file.h" static void main_show_help(const char *s) @@ -36,10 +37,11 @@ static void main_show_help(const char *s) int main(int argc, char *argv[]) { - int c; + int c, i; ImageList *list; UnitCell *cell; struct image image; + int nrefl; while ((c = getopt(argc, argv, "h")) != -1) { @@ -69,14 +71,27 @@ int main(int argc, char *argv[]) image.data = malloc(512*512*2); image_add(list, &image); - cell = cell_new_from_parameters(28.10e-9, - 28.10e-9, - 16.52e-9, - deg2rad(90.0), - deg2rad(90.0), - deg2rad(120.0)); - + /* Calculate reflections */ get_reflections(&image, cell); + /* Construct the image */ + nrefl = image_feature_count(image.rflist); + for ( i=0; ix; + y = f->y; /* Discards digits after the decimal point */ + + image.data[y*image.width+x] = 1; + + } + + /* Write the output file */ + hdf5_write("simulated.h5", image.data, image.width, image.height); + return 0; } -- cgit v1.2.3