indexamajig.c 5.35 KB
Newer Older
Thomas White's avatar
Thomas White committed
1
/*
2
 * indexamajig.c
Thomas White's avatar
Thomas White committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 *
 * Find hits, index patterns, output hkl+intensity etc.
 *
 * (c) 2006-2009 Thomas White <taw@physics.org>
 *
 * 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 "hdf5-file.h"
#include "dirax.h"
Thomas White's avatar
Thomas White committed
27
28
#include "intensities.h"
#include "ewald.h"
Thomas White's avatar
Thomas White committed
29
30
31
32
33
34
35
36
37
38
39
40


static void show_help(const char *s)
{
	printf("Syntax: %s [options]\n\n", s);
	printf(
"Process and index FEL diffraction images.\n"
"\n"
"  -h, --help              Display this help message.\n"
"\n"
"  -i, --input=<filename>  Specify file containing list of images to process.\n"
"                           '-' means stdin, which is the default.\n"
41
42
"      --no-index          Do everything else (including fine peak search),\n"
"                           but don't invoke the indexing program.\n"
Thomas White's avatar
Thomas White committed
43
44
45
46
"\n");
}


Thomas White's avatar
Thomas White committed
47
48
49
50
51
52
53
54
55
struct peak {
	int x;
	int y;
	int i;
	int invalid;
};


static int image_fom(struct image *image)
Thomas White's avatar
Thomas White committed
56
57
{
	int x, y;
Thomas White's avatar
Thomas White committed
58
59
	int integr, n;
	float fintegr, mean, sd, th;
Thomas White's avatar
Thomas White committed
60
61
	struct peak peaks[1024];
	int i, n_peaks, n_nearby, n_valid;
Thomas White's avatar
Thomas White committed
62

Thomas White's avatar
Thomas White committed
63
64
65
66
	/* Measure mean */
	integr = 0;
	n = 0;
	for ( x=0; x<1024; x++ ) {
67
	for ( y=600; y<1024; y++ ) {
Thomas White's avatar
Thomas White committed
68
69

		int val;
Thomas White's avatar
Thomas White committed
70
		if ( (x>400) && (x<600) ) continue;
Thomas White's avatar
Thomas White committed
71
		val = image->data[x+image->height*y];
Thomas White's avatar
Thomas White committed
72
73
74
75
76
77
78
79
80
		if ( val < 0 ) continue;
		integr += val;
		n++;

	}
	}
	mean = (float)integr / n;  /* As integer to keep maths fast */

	/* Standard deviation */
Thomas White's avatar
Thomas White committed
81
	fintegr = 0;
Thomas White's avatar
Thomas White committed
82
83
	for ( x=0; x<1024; x++ ) {
	for ( y=600; y<1024; y++ ) {
Thomas White's avatar
Thomas White committed
84

Thomas White's avatar
Thomas White committed
85
86
87
88
89
90
91
92
93
		float val;

		if ( (x>400) && (x<600) ) continue;
		val = (float)image->data[x+image->height*y];
		if ( val < 0 ) continue;

		val -= mean;
		val = powf(val, 2.0);
		fintegr += val;
Thomas White's avatar
Thomas White committed
94
95
96

	}
	}
Thomas White's avatar
Thomas White committed
97
98
99
100
101
102
	sd = sqrtf(fintegr / n);

	/* Threshold */
	th = mean + 5*sd;
	STATUS("mean=%f ,sd=%f, th=%f\n", mean, sd, th);

Thomas White's avatar
Thomas White committed
103
104
	/* Find pixels above threshold */
	n_peaks = 0;
Thomas White's avatar
Thomas White committed
105
	for ( x=0; x<1024; x++ ) {
106
107
108
109
	for ( y=600; y<1024; y++ ) {

		int val;

Thomas White's avatar
Thomas White committed
110
111
112
		/* Chop out streaky region */
		if ( (x>400) && (x<600) ) continue;

113
114
		val = image->data[x+image->height*y];

Thomas White's avatar
Thomas White committed
115
116
117
118
119
120
121
		if ( val > th ) {
			peaks[n_peaks].x = x;
			peaks[n_peaks].y = y;
			peaks[n_peaks].i = val;
			peaks[n_peaks].invalid = 0;
			n_peaks++;
		}
122
123
124
125

	}
	}

Thomas White's avatar
Thomas White committed
126
127
	do {

Thomas White's avatar
Thomas White committed
128
		int max, max_i = -1;
Thomas White's avatar
Thomas White committed
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
171
172
173
174
175
176
177
178
179
180
181
182
		int adjacent;

		n_nearby = 0;

		/* Find brightest peak */
		max = 0;
		for ( i=0; i<n_peaks; i++ ) {
			if ( peaks[i].i > max ) {
				max = peaks[i].i;
				max_i = i;
			}
		}

		/* Must be at least one adjacent bright pixel */
		adjacent = 0;
		for ( i=0; i<n_peaks; i++ ) {

			int td;

			td = abs(peaks[i].x - peaks[max_i].x) +
			     abs(peaks[i].y - peaks[max_i].y);
			if ( td == 1 ) {
				adjacent++;
				break;  /* One is enough */
			}
		}
		if ( adjacent < 1 ) {
			peaks[i].invalid = 1;
			continue;
		}

		/* Remove nearby (non-invalidated) peaks from list */
		n_nearby = 0;
		for ( i=0; i<n_peaks; i++ ) {

			int dx, dy, ds;

			if ( peaks[i].invalid ) continue;

			dx = abs(peaks[i].x - peaks[max_i].x);
			dy = abs(peaks[i].y - peaks[max_i].y);
			ds = dx*dx + dy*dy;
			if ( ds < 36 ) {
				n_nearby++;
				peaks[i].invalid = 1;
			}

		}

	} while ( n_nearby );

	n_valid = 0;
	for ( i=0; i<n_peaks; i++ ) {
		if ( peaks[i].invalid ) continue;
Thomas White's avatar
Thomas White committed
183
		//printf("%i %i\n", peaks[i].x, peaks[i].y);
Thomas White's avatar
Thomas White committed
184
185
186
187
		n_valid++;
	}

	return n_valid;
Thomas White's avatar
Thomas White committed
188
189
190
191
192
193
194
195
196
197
198
}


int main(int argc, char *argv[])
{
	int c;
	char *filename = NULL;
	FILE *fh;
	char *rval;
	int n_images;
	int n_hits;
199
	int config_noindex = 0;
Thomas White's avatar
Thomas White committed
200
	int config_dumpfound = 0;
Thomas White's avatar
Thomas White committed
201
202
203
204
205

	/* Long options */
	const struct option longopts[] = {
		{"help",               0, NULL,               'h'},
		{"input",              1, NULL,               'i'},
206
		{"no-index",           0, &config_noindex,     1},
Thomas White's avatar
Thomas White committed
207
		{"dump-found-peaks",   0, &config_dumpfound,   1},
Thomas White's avatar
Thomas White committed
208
209
210
211
		{0, 0, NULL, 0}
	};

	/* Short options */
212
	while ((c = getopt_long(argc, argv, "hi:w", longopts, NULL)) != -1) {
Thomas White's avatar
Thomas White committed
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256

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

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

		case 0 : {
			break;
		}

		default : {
			return 1;
		}
		}

	}

	if ( filename == NULL ) {
		filename = strdup("-");
	}
	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_images = 0;
	n_hits = 0;
	do {

		char line[1024];
		struct hdfile *hdfile;
		struct image image;
Thomas White's avatar
Thomas White committed
257
		int fom;
Thomas White's avatar
Thomas White committed
258
259

		rval = fgets(line, 1023, fh);
Thomas White's avatar
Thomas White committed
260
		if ( rval == NULL ) continue;
Thomas White's avatar
Thomas White committed
261
262
		chomp(line);

Thomas White's avatar
Thomas White committed
263
		image.features = NULL;
Thomas White's avatar
Thomas White committed
264
		image.molecule = NULL;
Thomas White's avatar
Thomas White committed
265

Thomas White's avatar
Thomas White committed
266
267
268
269
270
271
272
273
274
275
276
		STATUS("Processing '%s'\n", line);

		hdfile = hdfile_open(line);
		if ( hdfile == NULL ) {
			ERROR("Couldn't open file '%s'\n", filename);
		} else if ( hdfile_set_first_image(hdfile, "/") ) {
			ERROR("Couldn't select path\n");
		}

		hdf5_read(hdfile, &image);

Thomas White's avatar
Thomas White committed
277
278
		fom = image_fom(&image);
		if ( fom > 0 ) {
Thomas White's avatar
Thomas White committed
279

Thomas White's avatar
Thomas White committed
280
281
282
283
284
285
286
287
288
289
290
291
			/* Calculate orientation matrix (by magic) */
			index_pattern(&image, config_noindex, config_dumpfound);

			/* View head-on (unit cell is tilted) */
			image.orientation.x = 0.0;
			image.orientation.y = 0.0;
			image.orientation.z = 0.0;
			image.orientation.w = 1.0;
			get_ewald(&image);

			/* Read h,k,l,I */
			output_intensities(&image);
Thomas White's avatar
Thomas White committed
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307

			n_hits++;

		}

		n_images++;

	} while ( rval != NULL );

	fclose(fh);

	STATUS("There were %i images.\n", n_images);
	STATUS("%i hits were found.\n", n_hits);

	return 0;
}