process_hkl.c 7.25 KB
Newer Older
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
/*
 * process_hkl.c
 *
 * Assemble and process FEL Bragg intensities
 *
 * (c) 2006-2009 Thomas White <thomas.white@desy.de>
 *
 * Part of CrystFEL - crystallography with a FEL
 *
 */


#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <getopt.h>

#include "utils.h"
#include "statistics.h"
#include "sfac.h"


Thomas White's avatar
Thomas White committed
29
30
31
32
/* Number of divisions for R vs |q| graphs */
#define RVQDV (20)


33
34
35
static void show_help(const char *s)
{
	printf("Syntax: %s [options]\n\n", s);
Thomas White's avatar
Thomas White committed
36
37
38
39
40
41
42
43
44
	printf(
"Assemble and process FEL Bragg intensities.\n"
"\n"
"  -h, --help              Display this help message.\n"
"  -i, --input=<filename>  Specify input filename (\"-\" for stdin).\n"
"\n"
"      --max-only          Take the integrated intensity to be equal to the\n"
"                           maximum intensity measured for that reflection.\n"
"                           The default is to use the mean value from all\n"
Thomas White's avatar
Thomas White committed
45
"                           measurements.\n"
Thomas White's avatar
Thomas White committed
46
47
"  -e, --output-every=<n>  Analyse figures of merit after every n patterns.\n"
"  -r, --rvsq              Output lists of R vs |q| (\"Luzzatti plots\") when\n"
Thomas White's avatar
Thomas White committed
48
49
50
"                           analysing figures of merit.\n"
"      --stop-after=<n>    Stop after processing n patterns (zero means)\n"
"                           never stop).\n");
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
}


static void write_RvsQ(const char *name, double *ref, double *trueref,
                       unsigned int *counts, double scale, UnitCell *cell)
{
	FILE *fh;
	double smax, sbracket;
	signed int h, k, l;

	fh = fopen(name, "w");

	smax = 0.0;
	for ( h=-INDMAX; h<INDMAX; h++ ) {
	for ( k=-INDMAX; k<INDMAX; k++ ) {
	for ( l=-INDMAX; l<INDMAX; l++ ) {
		double s = resolution(cell, h, k, l);
		if ( (lookup_count(counts, h, k, l) > 0) && (s > smax) ) {
			smax = s;
		}
	}
	}
	}

Thomas White's avatar
Thomas White committed
75
	for ( sbracket=0.0; sbracket<smax; sbracket+=smax/RVQDV ) {
76
77
78

		double top = 0.0;
		double bot = 0.0;
Thomas White's avatar
Thomas White committed
79
80
81
82
		int nhits = 0;
		int nrefl = 0;
		double R;
		double hits_per_refl;
83
84
85
86
87
88
89

		for ( h=-INDMAX; h<INDMAX; h++ ) {
		for ( k=-INDMAX; k<INDMAX; k++ ) {
		for ( l=-INDMAX; l<INDMAX; l++ ) {

			double s;
			int c;
Thomas White's avatar
Thomas White committed
90
91
92

			if ( (h==0) && (k==0) && (l==0) ) continue;

93
94
			c = lookup_count(counts, h, k, l);
			s = resolution(cell, h, k, l);
Thomas White's avatar
Thomas White committed
95
			if ((s>=sbracket) && (s<sbracket+smax/RVQDV) && (c>0)) {
96
97
98
99
100
101
102

				double obs, calc, obsi;

				obs = lookup_intensity(ref, h, k, l);
				calc = lookup_intensity(trueref, h, k, l);

				obsi = obs / (double)c;
Thomas White's avatar
Thomas White committed
103
104
105
106
107
				top += pow(obsi - scale*calc, 2.0);
				bot += pow(obsi, 2.0);
				nhits += c;
				nrefl++;

108
109
110
111
112
113
			}

		}
		}
		}

Thomas White's avatar
Thomas White committed
114
115
116
117
118
		R = sqrt(top/bot);
		hits_per_refl = nrefl ? (double)nhits/nrefl : 0;

		fprintf(fh, "%8.5f %8.5f %5.2f\n", sbracket+smax/(2.0*RVQDV),
		                                R, hits_per_refl);
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
167
168
169
170

	}
	fclose(fh);
}


static void write_reflections(const char *filename, unsigned int *counts,
                              double *ref)
{
	FILE *fh;
	signed int h, k, l;

	fh = fopen(filename, "w");
	for ( h=-INDMAX; h<INDMAX; h++ ) {
	for ( k=-INDMAX; k<INDMAX; k++ ) {
	for ( l=-INDMAX; l<INDMAX; l++ ) {
		int N;
		N = lookup_count(counts, h, k, l);
		if ( N == 0 ) continue;
		double F = lookup_intensity(ref, h, k, l) / N;
		fprintf(fh, "%3i %3i %3i %f\n", h, k, l, F);
	}
	}
	}
	fclose(fh);
}


static double *ideal_intensities(double complex *sfac)
{
	double *ref;
	signed int h, k, l;

	ref = new_list_intensity();

	/* Generate ideal reflections from complex structure factors */
	for ( h=-INDMAX; h<=INDMAX; h++ ) {
	for ( k=-INDMAX; k<=INDMAX; k++ ) {
	for ( l=-INDMAX; l<=INDMAX; l++ ) {
		double complex F = lookup_sfac(sfac, h, k, l);
		double intensity = pow(cabs(F), 2.0);
		set_intensity(ref, h, k, l, intensity);
	}
	}
	}

	return ref;
}


static void process_reflections(double *ref, double *trueref,
                                unsigned int *counts, unsigned int n_patterns,
Thomas White's avatar
Thomas White committed
171
                                UnitCell *cell, int do_rvsq)
172
173
174
175
176
177
178
{
	int j;
	double mean_counts;
	int ctot = 0;
	int nmeas = 0;
	char name[64];
	double R, scale;
Thomas White's avatar
Thomas White committed
179
	double calc_222, obs_222;
180
181
182
183
184
185
186

	for ( j=0; j<LIST_SIZE; j++ ) {
		ctot += counts[j];
		if ( counts[j] > 0 ) nmeas++;
	}
	mean_counts = (double)ctot/nmeas;

Thomas White's avatar
Thomas White committed
187
188
	calc_222 = lookup_intensity(ref, 2, 2, 2) / lookup_count(counts, 2, 2, 2);
	obs_222 = lookup_intensity(trueref, 2, 2, 2);
189
190

	R = stat_r2(ref, trueref, counts, LIST_SIZE, &scale);
Thomas White's avatar
Thomas White committed
191
192
193
	STATUS("%8u: R=%5.2f%% (sf=%7.4e)  mean meas/refl=%5.2f,"
	       " %i reflections measured, %f\n",
	       n_patterns, R*100.0, scale, mean_counts, nmeas, calc_222/obs_222);
194

Thomas White's avatar
Thomas White committed
195
196
197
198
199
	if ( do_rvsq ) {
		/* Record graph of R against q for this N */
		snprintf(name, 63, "results/R_vs_q-%u.dat", n_patterns);
		write_RvsQ(name, ref, trueref, counts, scale, cell);
	}
200
201
202
203
204
205
206
207
208
209
210
211
212
}


int main(int argc, char *argv[])
{
	int c;
	char *filename = NULL;
	FILE *fh;
	unsigned int n_patterns;
	double *ref, *trueref;
	unsigned int *counts;
	char *rval;
	struct molecule *mol;
Thomas White's avatar
Thomas White committed
213
	int config_maxonly = 0;
Thomas White's avatar
Thomas White committed
214
	int config_every = 1000;
Thomas White's avatar
Thomas White committed
215
	int config_rvsq = 0;
Thomas White's avatar
Thomas White committed
216
	int config_stopafter = 0;
217
218
219
220
221

	/* Long options */
	const struct option longopts[] = {
		{"help",               0, NULL,               'h'},
		{"input",              1, NULL,               'i'},
Thomas White's avatar
Thomas White committed
222
		{"max-only",           0, &config_maxonly,     1},
Thomas White's avatar
Thomas White committed
223
		{"output-every",       1, NULL,               'e'},
Thomas White's avatar
Thomas White committed
224
		{"rvsq",               0, NULL,               'r'},
Thomas White's avatar
Thomas White committed
225
		{"stop-after",         1, NULL,               's'},
226
227
228
229
		{0, 0, NULL, 0}
	};

	/* Short options */
Thomas White's avatar
Thomas White committed
230
	while ((c = getopt_long(argc, argv, "hi:e:r", longopts, NULL)) != -1) {
231
232
233
234
235
236
237
238
239
240
241
242

		switch (c) {
		case 'h' : {
			show_help(argv[0]);
			return 0;
		}

		case 'i' : {
			filename = strdup(optarg);
			break;
		}

Thomas White's avatar
Thomas White committed
243
244
245
246
247
		case 'r' : {
			config_rvsq = 1;
			break;
		}

Thomas White's avatar
Thomas White committed
248
249
250
251
252
		case 'e' : {
			config_every = atoi(optarg);
			break;
		}

Thomas White's avatar
Thomas White committed
253
254
255
256
257
		case 's' : {
			config_stopafter = atoi(optarg);
			break;
		}

258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
		case 0 : {
			break;
		}

		default : {
			return 1;
		}
		}

	}

	if ( filename == NULL ) {
		ERROR("Please specify filename using the -i option\n");
		return 1;
	}

Thomas White's avatar
Thomas White committed
274
275
276
277
278
	if ( config_every <= 0 ) {
		ERROR("Invalid value for --output-every.\n");
		return 1;
	}

279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
	mol = load_molecule();
	get_reflections_cached(mol, eV_to_J(2.0e3));

	ref = new_list_intensity();
	counts = new_list_count();
	trueref = ideal_intensities(mol->reflections);

	if ( strcmp(filename, "-") == 0 ) {
		fh = stdin;
	} else {
		fh = fopen(filename, "r");
	}
	free(filename);
	if ( fh == NULL ) {
		ERROR("Failed to open input file\n");
		return 1;
	}

	n_patterns = 0;
	do {

		char line[1024];
		int h, k, l, intensity;
		int r;

		rval = fgets(line, 1023, fh);
		if ( strncmp(line, "New pattern", 11) == 0 ) {
			n_patterns++;
Thomas White's avatar
Thomas White committed
307
			if ( n_patterns % config_every == 0 ) {
308
				process_reflections(ref, trueref, counts,
Thomas White's avatar
Thomas White committed
309
310
				                    n_patterns, mol->cell,
				                    config_rvsq);
311
			}
Thomas White's avatar
Thomas White committed
312
313
314

			if ( n_patterns == config_stopafter ) break;

315
316
317
318
319
		}

		r = sscanf(line, "%i %i %i %i", &h, &k, &l, &intensity);
		if ( r != 4 ) continue;

Thomas White's avatar
Thomas White committed
320
321
322
323
324
325
326
327
328
329
330
		//if ( (abs(h)>3) || (abs(k)>3) || (abs(l)>3) ) continue;

		if ( !config_maxonly ) {
			integrate_intensity(ref, h, k, l, intensity);
			integrate_count(counts, h, k, l, 1);
		} else {
			if ( intensity > lookup_intensity(ref, h, k, l) ) {
				set_intensity(ref, h, k, l, intensity);
			}
			set_count(counts, h, k, l, 1);
		}
331
332
333
334
335
336
337
338
339

	} while ( rval != NULL );

	fclose(fh);

	write_reflections("results/reflections.hkl", counts, ref);

	return 0;
}