aboutsummaryrefslogtreecommitdiff
path: root/src/stream.c
diff options
context:
space:
mode:
authorThomas White <taw@physics.org>2010-09-24 18:25:49 +0200
committerThomas White <taw@physics.org>2012-02-22 15:26:59 +0100
commit8bb2c7e3f5f4cfe9793256c009f62b0289607332 (patch)
treec00a292d572d268eb6a108105edb7154ab97deb4 /src/stream.c
parent7b3e720829c62b63259b65ede4a4006e4417b32e (diff)
Move stream handling functions to a separate module
Diffstat (limited to 'src/stream.c')
-rw-r--r--src/stream.c111
1 files changed, 111 insertions, 0 deletions
diff --git a/src/stream.c b/src/stream.c
new file mode 100644
index 00000000..75a80c72
--- /dev/null
+++ b/src/stream.c
@@ -0,0 +1,111 @@
+/*
+ * stream.c
+ *
+ * Indexed stream tools
+ *
+ * (c) 2006-2010 Thomas White <taw@physics.org>
+ *
+ * Part of CrystFEL - crystallography with a FEL
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "cell.h"
+#include "utils.h"
+
+
+int count_patterns(FILE *fh)
+{
+ char *rval;
+
+ int n_total_patterns = 0;
+ do {
+ char line[1024];
+
+ rval = fgets(line, 1023, fh);
+ if ( rval == NULL ) continue;
+ if ( (strncmp(line, "Reflections from indexing", 25) == 0)
+ || (strncmp(line, "New pattern", 11) == 0) ) {
+ n_total_patterns++;
+ }
+ } while ( rval != NULL );
+
+ return n_total_patterns;
+}
+
+
+static UnitCell *read_orientation_matrix(FILE *fh)
+{
+ float u, v, w;
+ struct rvec as, bs, cs;
+ UnitCell *cell;
+ char line[1024];
+
+ if ( fgets(line, 1023, fh) == NULL ) return NULL;
+ if ( sscanf(line, "astar = %f %f %f", &u, &v, &w) != 3 ) {
+ ERROR("Couldn't read a-star\n");
+ return NULL;
+ }
+ as.u = u*1e9; as.v = v*1e9; as.w = w*1e9;
+ if ( fgets(line, 1023, fh) == NULL ) return NULL;
+ if ( sscanf(line, "bstar = %f %f %f", &u, &v, &w) != 3 ) {
+ ERROR("Couldn't read b-star\n");
+ return NULL;
+ }
+ bs.u = u*1e9; bs.v = v*1e9; bs.w = w*1e9;
+ if ( fgets(line, 1023, fh) == NULL ) return NULL;
+ if ( sscanf(line, "cstar = %f %f %f", &u, &v, &w) != 3 ) {
+ ERROR("Couldn't read c-star\n");
+ return NULL;
+ }
+ cs.u = u*1e9; cs.v = v*1e9; cs.w = w*1e9;
+ cell = cell_new_from_axes(as, bs, cs);
+
+ return cell;
+}
+
+
+int find_chunk(FILE *fh, UnitCell **cell, char **filename)
+{
+ char line[1024];
+ char *rval = NULL;
+
+ do {
+
+ rval = fgets(line, 1023, fh);
+ if ( rval == NULL ) continue;
+
+ chomp(line);
+
+ if ( strncmp(line, "Reflections from indexing", 25) != 0 ) {
+ continue;
+ }
+
+ *filename = strdup(line+29);
+
+ /* Skip two lines (while checking for errors) */
+ rval = fgets(line, 1023, fh);
+ if ( rval == NULL ) continue;
+ rval = fgets(line, 1023, fh);
+ if ( rval == NULL ) continue;
+
+ *cell = read_orientation_matrix(fh);
+ if ( *cell == NULL ) {
+ STATUS("Got filename but no cell for %s\n", *filename);
+ continue;
+ }
+
+ return 0;
+
+ } while ( rval != NULL );
+
+ return 1;
+}