aboutsummaryrefslogtreecommitdiff
path: root/src/stream.c
blob: b56c20ce2d1559e6861675c03e6df52962e4113c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
 * 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;
}


static UnitCell *read_orientation_matrix_rick(FILE *fh)
{
	float a, b, c;
	struct rvec as, bs, cs;
	UnitCell *cell;
	char line[1024];

	if ( fgets(line, 1023, fh) == NULL ) return NULL;
	if ( sscanf(line, "%f %f %f", &a, &b, &c) != 3 ) {
		ERROR("Couldn't read a-star\n");
		return NULL;
	}
	as.u = a*1e10;  bs.u = b*1e10;  cs.u = c*1e10;
	if ( fgets(line, 1023, fh) == NULL ) return NULL;
	if ( sscanf(line, "%f %f %f", &a, &b, &c) != 3 ) {
		ERROR("Couldn't read b-star\n");
		return NULL;
	}
	as.v = a*1e10;  bs.v = b*1e10;  cs.v = c*1e10;
	if ( fgets(line, 1023, fh) == NULL ) return NULL;
	if ( sscanf(line, "%f %f %f", &a, &b, &c) != 3 ) {
		ERROR("Couldn't read c-star\n");
		return NULL;
	}
	as.w = -a*1e10;  bs.w = -b*1e10;  cs.w = -c*1e10;
	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 {

		int i;

		rval = fgets(line, 1023, fh);
		if ( rval == NULL ) continue;

		chomp(line);

		/* Look for the first line of a chunk */
		if ( (strncmp(line, "Reflections from indexing", 25) != 0)
		  && (strncmp(line, "## h5FilePath:", 14) != 0 ) ) {
			continue;
		}

		/* Read in "Tom Mode"? */
		if ( strncmp(line, "Reflections from indexing", 25) == 0 ) {

			*filename = strdup(line+29);
			/* Skip two lines */
			for ( i=0; i<2; i++ ) {
				rval = fgets(line, 1023, fh);
				if ( rval == NULL ) continue;
			}
			*cell = read_orientation_matrix(fh);

		}

		/* Read in "Rick Mode"? */
		if ( strncmp(line, "## h5FilePath:", 14) == 0 ) {

			/* Filename is on next line */
			rval = fgets(line, 1023, fh);
			if ( rval == NULL ) continue;
			chomp(line);
			*filename = strdup(line);
			/* Look for the start of the orientation matrix */
			do {
				rval = fgets(line, 1023, fh);
				if ( rval == NULL ) continue;
			} while ( strncmp(line, "## A:", 5) != 0 );
			*cell = read_orientation_matrix_rick(fh);
		}

		if ( *cell == NULL ) {
			STATUS("Got filename but no cell for %s\n", *filename);
			continue;
		}

		return 0;

	} while ( rval != NULL );

	return 1;
}