aboutsummaryrefslogtreecommitdiff
path: root/libcrystfel
diff options
context:
space:
mode:
authorThomas White <taw@physics.org>2013-01-08 17:22:02 +0100
committerThomas White <taw@physics.org>2013-01-08 17:22:02 +0100
commitefb829adf0c494a3ed632cef472ac1e8e5267765 (patch)
treeaa2bb32f5b11642667658cf926f5fbbf743b552b /libcrystfel
parent10a8969c10ac739aa2aa3de826f6bf2aa9940262 (diff)
Skeleton indexing method for GrainSpotter
Diffstat (limited to 'libcrystfel')
-rw-r--r--libcrystfel/Makefile.am5
-rw-r--r--libcrystfel/src/grainspotter.c214
-rw-r--r--libcrystfel/src/grainspotter.h42
-rw-r--r--libcrystfel/src/index.c15
-rw-r--r--libcrystfel/src/index.h1
5 files changed, 275 insertions, 2 deletions
diff --git a/libcrystfel/Makefile.am b/libcrystfel/Makefile.am
index 1c35e03f..22009223 100644
--- a/libcrystfel/Makefile.am
+++ b/libcrystfel/Makefile.am
@@ -8,7 +8,8 @@ libcrystfel_la_SOURCES = src/reflist.c src/utils.c src/cell.c src/detector.c \
src/symmetry.c src/stream.c src/peaks.c \
src/reflist-utils.c src/filters.c \
src/render.c src/index.c src/dirax.c src/mosflm.c \
- src/cell-utils.c src/integer_matrix.c
+ src/cell-utils.c src/integer_matrix.c \
+ src/grainspotter.c
if HAVE_FFTW
libcrystfel_la_SOURCES += src/reax.c
@@ -24,7 +25,7 @@ libcrystfel_la_include_HEADERS = src/beam-parameters.h src/hdf5-file.h \
src/render.h src/index.h src/image.h \
src/filters.h src/dirax.h src/mosflm.h \
src/index-priv.h src/reax.h src/cell-utils.h \
- src/integer_matrix.h
+ src/integer_matrix.h src/grainspotter.h
AM_CPPFLAGS = -DDATADIR=\""$(datadir)"\" -I$(top_builddir)/lib -Wall
AM_CPPFLAGS += -I$(top_srcdir)/lib @LIBCRYSTFEL_CFLAGS@
diff --git a/libcrystfel/src/grainspotter.c b/libcrystfel/src/grainspotter.c
new file mode 100644
index 00000000..06f65f08
--- /dev/null
+++ b/libcrystfel/src/grainspotter.c
@@ -0,0 +1,214 @@
+/*
+ * grainspotter.c
+ *
+ * Invoke GrainSpotter for multi-crystal autoindexing
+ *
+ * Copyright © 2013 Deutsches Elektronen-Synchrotron DESY,
+ * a research centre of the Helmholtz Association.
+ *
+ * Authors:
+ * 2010-2013 Thomas White <taw@physics.org>
+ *
+ * This file is part of CrystFEL.
+ *
+ * CrystFEL is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * CrystFEL is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with CrystFEL. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <math.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/wait.h>
+#include <fcntl.h>
+#include <assert.h>
+#include <sys/ioctl.h>
+#include <errno.h>
+
+#if HAVE_FORKPTY_LINUX
+#include <pty.h>
+#elif HAVE_FORKPTY_BSD
+#include <util.h>
+#endif
+
+
+#include "image.h"
+#include "grainspotter.h"
+#include "utils.h"
+#include "peaks.h"
+
+
+#define GRAINSPOTTER_VERBOSE 0
+
+
+struct grainspotter_data {
+
+ /* Low-level stuff */
+ int pty;
+ pid_t pid;
+ char *rbuffer;
+ int rbufpos;
+ int rbuflen;
+
+};
+
+
+static int grainspotter_readable(struct image *image,
+ struct grainspotter_data *grainspotter)
+{
+ int rval;
+
+ rval = read(grainspotter->pty,
+ grainspotter->rbuffer+grainspotter->rbufpos,
+ grainspotter->rbuflen-grainspotter->rbufpos);
+
+ if ( (rval == -1) || (rval == 0) ) return 1;
+
+ /* FIXME! (if needed) */
+ //grainspotter->rbufpos += rval;
+ //assert(grainspotter->rbufpos <= grainspotter->rbuflen);
+
+ return 0;
+}
+
+
+static void write_gve(struct image *image)
+{
+ FILE *fh;
+ int i;
+ char filename[1024];
+
+ snprintf(filename, 1023, "xfel-%i.gve", image->id);
+
+ fh = fopen(filename, "w");
+ if ( !fh ) {
+ ERROR("Couldn't open temporary file '%s'\n", filename);
+ return;
+ }
+ fprintf(fh, "%f\n", 0.5); /* Lie about the wavelength. */
+
+ for ( i=0; i<image_feature_count(image->features); i++ ) {
+
+ struct imagefeature *f;
+
+ f = image_get_feature(image->features, i);
+ if ( f == NULL ) continue;
+
+ fprintf(fh, "%10f %10f %10f %8f\n",
+ f->rx/1e10, f->ry/1e10, f->rz/1e10, 1.0);
+
+ }
+ fclose(fh);
+}
+
+
+void run_grainspotter(struct image *image, UnitCell *cell)
+{
+ unsigned int opts;
+ int status;
+ int rval;
+ struct grainspotter_data *grainspotter;
+
+ write_gve(image);
+
+ grainspotter = malloc(sizeof(struct grainspotter_data));
+ if ( grainspotter == NULL ) {
+ ERROR("Couldn't allocate memory for GrainSpotter data.\n");
+ return;
+ }
+
+ grainspotter->pid = forkpty(&grainspotter->pty, NULL, NULL, NULL);
+ if ( grainspotter->pid == -1 ) {
+ ERROR("Failed to fork for GrainSpotter: %s\n", strerror(errno));
+ return;
+ }
+ if ( grainspotter->pid == 0 ) {
+
+ /* Child process: invoke GrainSpotter */
+ struct termios t;
+
+ /* Turn echo off */
+ tcgetattr(STDIN_FILENO, &t);
+ t.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL);
+ tcsetattr(STDIN_FILENO, TCSANOW, &t);
+
+ execlp("GrainSpotter.0.90", "", (char *)NULL);
+ ERROR("Failed to invoke GrainSpotter.\n");
+ _exit(0);
+
+ }
+
+ grainspotter->rbuffer = malloc(256);
+ grainspotter->rbuflen = 256;
+ grainspotter->rbufpos = 0;
+
+ /* Set non-blocking */
+ opts = fcntl(grainspotter->pty, F_GETFL);
+ fcntl(grainspotter->pty, F_SETFL, opts | O_NONBLOCK);
+
+ do {
+
+ fd_set fds;
+ struct timeval tv;
+ int sval;
+
+ FD_ZERO(&fds);
+ FD_SET(grainspotter->pty, &fds);
+
+ tv.tv_sec = 30;
+ tv.tv_usec = 0;
+
+ sval = select(grainspotter->pty+1, &fds, NULL, NULL, &tv);
+
+ if ( sval == -1 ) {
+
+ const int err = errno;
+
+ switch ( err ) {
+
+ case EINTR:
+ STATUS("Restarting select()\n");
+ break;
+
+ default:
+ ERROR("select() failed: %s\n", strerror(err));
+ rval = 1;
+
+ }
+
+ } else if ( sval != 0 ) {
+ rval = grainspotter_readable(image, grainspotter);
+ } else {
+ ERROR("No response from GrainSpotter..\n");
+ rval = 1;
+ }
+
+ } while ( !rval );
+
+ close(grainspotter->pty);
+ free(grainspotter->rbuffer);
+ waitpid(grainspotter->pid, &status, 0);
+
+ if ( status != 0 ) {
+ ERROR("GrainSpotter doesn't seem to be working properly.\n");
+ }
+
+ free(grainspotter);
+}
diff --git a/libcrystfel/src/grainspotter.h b/libcrystfel/src/grainspotter.h
new file mode 100644
index 00000000..1aedf57f
--- /dev/null
+++ b/libcrystfel/src/grainspotter.h
@@ -0,0 +1,42 @@
+/*
+ * grainspotter.h
+ *
+ * Invoke GrainSpotter for multi-crystal autoindexing
+ *
+ * Copyright © 2013 Deutsches Elektronen-Synchrotron DESY,
+ * a research centre of the Helmholtz Association.
+ *
+ * Authors:
+ * 2010-2013 Thomas White <taw@physics.org>
+ *
+ * This file is part of CrystFEL.
+ *
+ * CrystFEL is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * CrystFEL is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with CrystFEL. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#ifndef GRAINSPOTTER_H
+#define GRAINSPOTTER_H
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include "cell.h"
+
+
+extern void run_grainspotter(struct image *image, UnitCell *cell);
+
+
+#endif /* GRAINSPOTTER_H */
diff --git a/libcrystfel/src/index.c b/libcrystfel/src/index.c
index 7d912902..86ab8c1a 100644
--- a/libcrystfel/src/index.c
+++ b/libcrystfel/src/index.c
@@ -48,6 +48,7 @@
#include "index.h"
#include "index-priv.h"
#include "reax.h"
+#include "grainspotter.h"
#include "geometry.h"
#include "cell-utils.h"
@@ -97,6 +98,10 @@ IndexingPrivate **prepare_indexing(IndexingMethod *indm, UnitCell *cell,
iprivs[n] = indexing_private(indm[n]);
break;
+ case INDEXING_GRAINSPOTTER :
+ iprivs[n] = indexing_private(indm[n]);
+ break;
+
case INDEXING_REAX :
iprivs[n] = reax_prepare();
break;
@@ -132,6 +137,10 @@ void cleanup_indexing(IndexingPrivate **priv)
free(priv[n]);
break;
+ case INDEXING_GRAINSPOTTER :
+ free(priv[n]);
+ break;
+
case INDEXING_REAX :
reax_cleanup(priv[n]);
break;
@@ -196,6 +205,10 @@ void index_pattern(struct image *image, UnitCell *cell, IndexingMethod *indm,
run_mosflm(image, cell);
break;
+ case INDEXING_GRAINSPOTTER :
+ run_grainspotter(image, cell);
+ break;
+
case INDEXING_REAX :
reax_index(ipriv[n], image, cell);
break;
@@ -294,6 +307,8 @@ IndexingMethod *build_indexer_list(const char *str, int *need_cell)
list[i] = INDEXING_DIRAX;
} else if ( strcmp(methods[i], "mosflm") == 0) {
list[i] = INDEXING_MOSFLM;
+ } else if ( strcmp(methods[i], "grainspotter") == 0) {
+ list[i] = INDEXING_GRAINSPOTTER;
} else if ( strcmp(methods[i], "reax") == 0) {
list[i] = INDEXING_REAX;
*need_cell = 1;
diff --git a/libcrystfel/src/index.h b/libcrystfel/src/index.h
index 4a724645..2df7a311 100644
--- a/libcrystfel/src/index.h
+++ b/libcrystfel/src/index.h
@@ -56,6 +56,7 @@ typedef enum {
INDEXING_NONE,
INDEXING_DIRAX,
INDEXING_MOSFLM,
+ INDEXING_GRAINSPOTTER,
INDEXING_REAX,
} IndexingMethod;