Commit 69b33e88 authored by Thomas White's avatar Thomas White
Browse files

Write an HDF5 file

parent 4e1e2ef4
......@@ -18,4 +18,6 @@ AC_FUNC_MALLOC
AC_TYPE_SIGNAL
AC_CHECK_FUNCS([strdup])
LIBS="$LIBS -lhdf5"
AC_OUTPUT(Makefile src/Makefile)
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
/*
* 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;
}
/*
* hdf5.h
*
* 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
#ifndef HDF5_H
#define HDF5_H
#include <stdint.h>
extern int hdf5_write(const char *filename, const uint16_t *data,
int width, int height);
#endif /* HDF5_H */
......@@ -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];
}
......@@ -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 */
......@@ -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; i<nrefl; i++ ) {
struct imagefeature *f;
int x, y;
f = image_get_feature(image.rflist, i);
x = f->x;
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;
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment