From efb829adf0c494a3ed632cef472ac1e8e5267765 Mon Sep 17 00:00:00 2001 From: Thomas White Date: Tue, 8 Jan 2013 17:22:02 +0100 Subject: Skeleton indexing method for GrainSpotter --- libcrystfel/src/grainspotter.c | 214 +++++++++++++++++++++++++++++++++++++++++ libcrystfel/src/grainspotter.h | 42 ++++++++ libcrystfel/src/index.c | 15 +++ libcrystfel/src/index.h | 1 + 4 files changed, 272 insertions(+) create mode 100644 libcrystfel/src/grainspotter.c create mode 100644 libcrystfel/src/grainspotter.h (limited to 'libcrystfel/src') 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 + * + * 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 . + * + */ + +#ifdef HAVE_CONFIG_H +#include +#endif + + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#if HAVE_FORKPTY_LINUX +#include +#elif HAVE_FORKPTY_BSD +#include +#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; ifeatures); 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 + * + * 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 . + * + */ + +#ifndef GRAINSPOTTER_H +#define GRAINSPOTTER_H + +#ifdef HAVE_CONFIG_H +#include +#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; -- cgit v1.2.3