indexamajig.c 4.69 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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
 *
 * 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"


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"
"\n");
}


Thomas White's avatar
Thomas White committed
43
44
45
46
47
48
49
50
51
struct peak {
	int x;
	int y;
	int i;
	int invalid;
};


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

Thomas White's avatar
Thomas White committed
59
60
61
62
	/* Measure mean */
	integr = 0;
	n = 0;
	for ( x=0; x<1024; x++ ) {
63
	for ( y=600; y<1024; y++ ) {
Thomas White's avatar
Thomas White committed
64
65

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

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

	/* Standard deviation */
	integr = 0;
	for ( x=0; x<1024; x++ ) {
	for ( y=600; y<1024; y++ ) {
Thomas White's avatar
Thomas White committed
80

Thomas White's avatar
Thomas White committed
81
82
83
84
85
86
87
88
89
		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
90
91
92

	}
	}
Thomas White's avatar
Thomas White committed
93
94
95
96
97
98
	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
99
100
	/* Find pixels above threshold */
	n_peaks = 0;
Thomas White's avatar
Thomas White committed
101
	for ( x=0; x<1024; x++ ) {
102
103
104
105
	for ( y=600; y<1024; y++ ) {

		int val;

Thomas White's avatar
Thomas White committed
106
107
108
		/* Chop out streaky region */
		if ( (x>400) && (x<600) ) continue;

109
110
		val = image->data[x+image->height*y];

Thomas White's avatar
Thomas White committed
111
112
113
114
115
116
117
		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++;
		}
118
119
120
121

	}
	}

Thomas White's avatar
Thomas White committed
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
171
172
173
174
175
176
177
178
	do {

		int max, max_i;
		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
179
		//printf("%i %i\n", peaks[i].x, peaks[i].y);
Thomas White's avatar
Thomas White committed
180
181
182
183
		n_valid++;
	}

	return n_valid;
Thomas White's avatar
Thomas White committed
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
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
}


int main(int argc, char *argv[])
{
	int c;
	char *filename = NULL;
	FILE *fh;
	char *rval;
	int n_images;
	int n_hits;

	/* Long options */
	const struct option longopts[] = {
		{"help",               0, NULL,               'h'},
		{"input",              1, NULL,               'i'},
		{0, 0, NULL, 0}
	};

	/* Short options */
	while ((c = getopt_long(argc, argv, "hi:", longopts, NULL)) != -1) {

		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
249
		int fom;
Thomas White's avatar
Thomas White committed
250
251

		rval = fgets(line, 1023, fh);
Thomas White's avatar
Thomas White committed
252
		if ( rval == NULL ) continue;
Thomas White's avatar
Thomas White committed
253
254
255
256
257
258
259
260
261
262
263
264
265
		chomp(line);

		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
266
267
268
		fom = image_fom(&image);
		printf("%6i %i\n", n_images, fom);
		if ( fom > 0 ) {
Thomas White's avatar
Thomas White committed
269

Thomas White's avatar
Thomas White committed
270
			printf("%s\n", line);
Thomas White's avatar
Thomas White committed
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288

			index_pattern(&image);

			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;
}