aboutsummaryrefslogtreecommitdiff
path: root/src/gui_backend_local.c
diff options
context:
space:
mode:
authorThomas White <taw@physics.org>2020-03-17 12:12:01 +0100
committerThomas White <taw@physics.org>2020-07-29 18:42:57 +0200
commit3972e0d898a73374838d9ea5b69570757f4b161c (patch)
tree3c59bf4ce9cdf1740f284a926e4d3c94e968b834 /src/gui_backend_local.c
parent14dd1029b41e5b50428160a56d025b0e56141f94 (diff)
Actually run indexamajig
Diffstat (limited to 'src/gui_backend_local.c')
-rw-r--r--src/gui_backend_local.c141
1 files changed, 141 insertions, 0 deletions
diff --git a/src/gui_backend_local.c b/src/gui_backend_local.c
index b3ba8657..c811a655 100644
--- a/src/gui_backend_local.c
+++ b/src/gui_backend_local.c
@@ -26,13 +26,154 @@
*
*/
+#include <pty.h>
+#include <glib.h>
#include "crystfel_gui.h"
+
+static gboolean index_readable(GIOChannel *source, GIOCondition cond,
+ void *vp)
+{
+ GIOStatus r;
+ GError *err = NULL;
+ struct crystfelproject *proj = vp;
+ gchar *line;
+
+ r = g_io_channel_read_line(source, &line, NULL, NULL, &err);
+ if ( r == G_IO_STATUS_EOF ) {
+ STATUS("End of output.\n");
+ return FALSE;
+ }
+ if ( r != G_IO_STATUS_NORMAL ) {
+ STATUS("Read error?\n");
+ return FALSE;
+ }
+ STATUS("Got line '%s'\n", line);
+
+ g_free(line);
+
+ return TRUE;
+}
+
+
+static int write_file_list(struct crystfelproject *proj)
+{
+ FILE *fh;
+ int i;
+
+ fh = fopen("files.lst", "w");
+ if ( fh == NULL ) return 1;
+
+ for ( i=0; i<proj->n_frames; i++ ) {
+ fprintf(fh, "%s", proj->filenames[i]);
+ if ( proj->events[i] != NULL ) {
+ fprintf(fh, " %s\n", proj->events[i]);
+ } else {
+ fprintf(fh, "\n");
+ }
+ }
+
+ fclose(fh);
+
+ return 0;
+}
+
+
+static void add_arg(char **args, int pos, const char *label,
+ float val)
+{
+ char *str;
+
+ str = malloc(64);
+ if ( str == NULL ) return;
+
+ snprintf(str, 63, "--%s=%f", label, val);
+ args[pos] = str;
+}
+
+
static int run_unitcell(struct crystfelproject *proj,
const char *algo)
{
+ pid_t pid;
+ int pty;
+ GIOChannel *ioch;
+ char *args[64];
+ char index_str[64];
+ char peaks_str[64];
+ int n_args;
+
STATUS("run unit cell with '%s'!\n", algo);
+
+ if ( write_file_list(proj) ) {
+ STATUS("Failed to write list\n");
+ return 1;
+ }
+
+ strcpy(index_str, "--indexing=");
+ strncat(index_str, algo, 50);
+
+ strcpy(peaks_str, "--peaks=");
+ strncat(peaks_str,
+ str_peaksearch(proj->peak_search_params.method), 50);
+
+ args[0] = "indexamajig";
+ args[1] = "-i";
+ args[2] = "files.lst";
+ args[3] = "-g";
+ args[4] = proj->geom_filename;
+ args[5] = "-o";
+ args[6] = "test.stream";
+ args[7] = index_str;
+ args[8] = "--no-check-cell";
+ args[9] = "-j";
+ args[10] = "1";
+ args[11] = "--integration=none";
+ args[12] = peaks_str;
+ n_args = 13;
+
+ if ( proj->peak_search_params.method == PEAK_ZAEF ) {
+ add_arg(args, n_args++, "threshold",
+ proj->peak_search_params.threshold);
+ add_arg(args, n_args++, "min-squared-gradient",
+ proj->peak_search_params.min_sq_gradient);
+ add_arg(args, n_args++, "min-snr",
+ proj->peak_search_params.min_snr);
+ }
+
+ args[n_args] = NULL;
+
+ int i;
+ for ( i=0; i<n_args; i++ ) {
+ STATUS("%s ", args[i]);
+ }
+ STATUS("\n");
+
+ pid = forkpty(&pty, NULL, NULL, NULL);
+ if ( pid == -1 ) return 1;
+
+ if ( pid == 0 ) {
+
+ /* Child process */
+ struct termios t;
+
+ /* Turn echo off */
+ tcgetattr(STDIN_FILENO, &t);
+ t.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL);
+ tcsetattr(STDIN_FILENO, TCSANOW, &t);
+
+ execvp("indexamajig", args);
+ _exit(1);
+
+ }
+
+ ioch = g_io_channel_unix_new(pty);
+ g_io_add_watch(ioch, G_IO_IN | G_IO_ERR | G_IO_HUP,
+ index_readable, proj);
+
+ /* FIXME: waitpid() on SIGCHLD */
+
return 0;
}