aboutsummaryrefslogtreecommitdiff
path: root/src/dirax.c
blob: 8606ff44a7c6413f9445d184d13b1c3ba72190cb (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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
/*
 * dirax.c
 *
 * Invoke the DirAx auto-indexing program
 *
 * (c) 2006-2009 Thomas White <taw@physics.org>
 *
 * Part of CrystFEL - crystallography with a FEL
 *
 */


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


#include <glib.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <pty.h>
#include <unistd.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <assert.h>
#include <sys/ioctl.h>
#include <termio.h>
#include <sgtty.h>


#include "image.h"
#include "dirax.h"
#include "utils.h"
#include "sfac.h"


typedef enum {
	DIRAX_INPUT_NONE,
	DIRAX_INPUT_LINE,
	DIRAX_INPUT_PROMPT
} DirAxInputType;


static void dirax_parseline(const char *line, struct image *image)
{
	int i, rf;
	char *copy;

	copy = strdup(line);
	for ( i=0; i<strlen(copy); i++ ) {
		if ( copy[i] == '\r' ) copy[i]='r';
		if ( copy[i] == '\n' ) copy[i]='\0';
	}
	STATUS("DirAx: %s\n", copy);
	free(copy);

	if ( strstr(line, "reflections from file") ) {
		ERROR("DirAx can't understand this data.");
		return;
	}

	/* Is this the first line of a unit cell specification? */
	rf = 0; i = 0;
	while ( (i<strlen(line)) && ((line[i] == 'R')
				|| (line[i] == 'D') || (line[i] == ' ')) ) {
		if ( line[i] == 'R' ) rf = 1;
		if ( (line[i] == 'D') && rf ) {
			image->dirax_read_cell = 1;
			if ( image->molecule == NULL ) {
				image->molecule = malloc(sizeof(struct molecule));
			} else if ( image->molecule->cell ) {
				free(image->molecule->cell);
				free(image->molecule);
			}
			image->molecule->cell = cell_new();
			return;
		}
		i++;
	}

	/* Parse unit cell vectors as appropriate */
	if ( image->dirax_read_cell == 1 ) {
		/* First row of unit cell values */
		float x1, x2, x3, d;
		sscanf(line, "%f %f %f %f %f %f", &d, &d, &d, &x1, &x2, &x3);
		cell_set_cartesian_x(image->molecule->cell,
		                     x1*1e-10, x2*1e-10, x3*1e-10);
		image->dirax_read_cell++;
		return;
	} else if ( image->dirax_read_cell == 2 ) {
		/* First row of unit cell values */
		float y1, y2, y3, d;
		sscanf(line, "%f %f %f %f %f %f", &d, &d, &d, &y1, &y2, &y3);
		cell_set_cartesian_y(image->molecule->cell,
		                     y1*1e-10, y2*1e-10, y3*1e-10);
		image->dirax_read_cell++;
		return;
	} else if ( image->dirax_read_cell == 3 ) {
		/* First row of unit cell values */
		float z1, z2, z3, d;
		sscanf(line, "%f %f %f %f %f %f", &d, &d, &d, &z1, &z2, &z3);
		cell_set_cartesian_z(image->molecule->cell,
		                     z1*1e-10, z2*1e-10, z3*1e-10);
		STATUS("Read a direct space unit cell from DirAx\n");
		/* FIXME: Do something */
		image->dirax_read_cell = 0;
		return;
	}

	image->dirax_read_cell = 0;
}


static void dirax_sendline(const char *line, struct image *image)
{
	char *copy;
	int i;

	write(image->dirax_pty, line, strlen(line));

	copy = strdup(line);
	for ( i=0; i<strlen(copy); i++ ) {
		if ( copy[i] == '\r' ) copy[i]='\0';
		if ( copy[i] == '\n' ) copy[i]='\0';
	}
	STATUS("To DirAx: '%s'\n", copy);
	free(copy);
}


static void dirax_send_next(struct image *image)
{
	switch ( image->dirax_step ) {

		case 1 : {
			dirax_sendline("\\echo off\n", image);
			image->dirax_step++;
			break;
		}

		case 2 : {
			dirax_sendline("read xfel.drx\n", image);
			image->dirax_step++;
			break;
		}

		case 3 : {
			dirax_sendline("dmax 10\n", image);
			image->dirax_step++;
			break;
		}

		case 4 : {
			dirax_sendline("indexfit 2\n", image);
			image->dirax_step++;
			break;
		}

		case 5 : {
			dirax_sendline("levelfit 200\n", image);
			image->dirax_step++;
			break;
		}

		case 6 : {
			dirax_sendline("go\n", image);
			image->dirax_step++;
			break;
		}

		case 7 : {
			dirax_sendline("cell\n", image);
			image->dirax_step++;
			break;
		}

		default: {
			image->dirax_step = 0;
			STATUS("DirAx is idle\n");
			g_main_loop_quit(image->dirax_ml);
		}

	}
}


static gboolean dirax_readable(GIOChannel *dirax, GIOCondition condition,
                               struct image *image)
{
	int rval;

	rval = read(image->dirax_pty, image->dirax_rbuffer+image->dirax_rbufpos,
			image->dirax_rbuflen-image->dirax_rbufpos);

	if ( (rval == -1) || (rval == 0) ) {

		ERROR("Lost connection to DirAx\n");
		waitpid(image->dirax_pid, NULL, 0);
		g_io_channel_shutdown(image->dirax, FALSE, NULL);
		image->dirax = NULL;
		return FALSE;

	} else {

		int no_string = 0;

		image->dirax_rbufpos += rval;
		assert(image->dirax_rbufpos <= image->dirax_rbuflen);

		while ( (!no_string) && (image->dirax_rbufpos > 0) ) {

			int i;
			int block_ready = 0;
			DirAxInputType type = DIRAX_INPUT_NONE;

			/* See if there's a full line in the buffer yet */
			for ( i=0; i<image->dirax_rbufpos-1; i++ ) {
				/* Means the last value looked at is rbufpos-2 */

				/* Is there a prompt in the buffer? */
				if ( i+7 <= image->dirax_rbufpos ) {
					if ( (strncmp(image->dirax_rbuffer+i,
							"Dirax> ", 7) == 0)
					  || (strncmp(image->dirax_rbuffer+i,
					  		"PROMPT:", 7) == 0) ) {
						block_ready = 1;
						type = DIRAX_INPUT_PROMPT;
						break;
					}
				}

				if ( (image->dirax_rbuffer[i] == '\r')
				  && (image->dirax_rbuffer[i+1] == '\n') ) {
					block_ready = 1;
					type = DIRAX_INPUT_LINE;
					break;
				}

			}

			if ( block_ready ) {

				unsigned int new_rbuflen;
				unsigned int endbit_length;

				switch ( type ) {

					case DIRAX_INPUT_LINE : {

						char *block_buffer = NULL;

						block_buffer = malloc(i+1);
						memcpy(block_buffer,
							image->dirax_rbuffer, i);
						block_buffer[i] = '\0';

						if ( block_buffer[0] == '\r' ) {
							memmove(block_buffer,
							  block_buffer+1, i);
						}

						dirax_parseline(block_buffer,
									image);
						free(block_buffer);
						endbit_length = i+2;

						break;

					}

					case DIRAX_INPUT_PROMPT : {

						dirax_send_next(image);
						endbit_length = i+7;
						break;

					}

					default : {
						ERROR(
			" Unrecognised input mode (this never happens!)\n");
						abort();
					}

				}

				/* Now the block's been parsed, it should be
				 * forgotten about */
				memmove(image->dirax_rbuffer,
					image->dirax_rbuffer + endbit_length,
					image->dirax_rbuflen - endbit_length);

				/* Subtract the number of bytes removed */
				image->dirax_rbufpos = image->dirax_rbufpos
								- endbit_length;
				new_rbuflen = image->dirax_rbuflen
								- endbit_length;
				if ( new_rbuflen == 0 ) {
					new_rbuflen = 256;
				}
				image->dirax_rbuffer = realloc(image->dirax_rbuffer,
								new_rbuflen);
				image->dirax_rbuflen = new_rbuflen;

			} else {

				if ( image->dirax_rbufpos==image->dirax_rbuflen ) {

					/* More buffer space is needed */
					image->dirax_rbuffer = realloc(
						image->dirax_rbuffer,
						image->dirax_rbuflen + 256);
					image->dirax_rbuflen = image->dirax_rbuflen
									+ 256;
					/* The new space gets used at the next
					 * read, shortly... */

				}
				no_string = 1;

			}

		}

	}

	return TRUE;
}


static int map_position(struct image *image, double x, double y,
                        double *rx, double *ry, double *rz)
{
	/* "Input" space */
	double d;

	/* Angular description of reflection */
	double theta, psi, k;

	x -= image->x_centre;
	y -= image->y_centre;
	k = 1.0 / image->lambda;

	/* FIXME: Don't process lower CCD for now */
	if ( y < 0 ) return 0;

	if ( image->fmode == FORMULATION_CLEN ) {

		/* Convert pixels to metres */
		x /= image->resolution;
		y /= image->resolution;	/* Convert pixels to metres */
		d = sqrt((x*x) + (y*y));
		theta = atan2(d, image->camera_len);

	} else if (image->fmode == FORMULATION_PIXELSIZE ) {

		/* Convert pixels to metres^-1 */
		x *= image->pixel_size;
		y *= image->pixel_size;	/* Convert pixels to metres^-1 */
		d = sqrt((x*x) + (y*y));
		theta = atan2(d, k);

	} else {
		ERROR("Unrecognised formulation mode in mapping_scale.\n");
		return -1;
	}

	psi = atan2(y, x);

	*rx = k*sin(theta)*cos(psi);
	*ry = k*sin(theta)*sin(psi);
	*rz = k - k*cos(theta);

	return 0;
}


#define PEAK_WINDOW_SIZE (10)

static void search_peaks(struct image *image, int dump_peaks)
{
	FILE *fh;
	int x, y, width, height;
	int16_t *data;

	fh = fopen("xfel.drx", "w");
	if ( !fh ) {
		ERROR("Couldn't open temporary file xfel.drx\n");
		return;
	}
	fprintf(fh, "%f\n", 0.5);  /* Lie about the wavelength.  */

	data = image->data;
	width = image->width;
	height = image->height;

	if ( image->features != NULL ) {
		image_feature_list_free(image->features);
	}
	image->features = image_feature_list_new();

	for ( x=1; x<image->width-1; x++ ) {
	for ( y=1; y<image->height-1; y++ ) {

		double dx1, dx2, dy1, dy2;
		double dxs, dys;
		double grad;
		int mask_x, mask_y;
		int sx, sy;
		double max;
		unsigned int did_something = 1;

		/* Overall threshold */
		if ( data[x+width*y] < 800 ) continue;

		/* Ignore streak */
		if ( abs(x-image->x_centre) < 15 ) continue;

		/* Get gradients */
		dx1 = data[x+width*y] - data[(x+1)+width*y];
		dx2 = data[(x-1)+width*y] - data[x+width*y];
		dy1 = data[x+width*y] - data[(x+1)+width*(y+1)];
		dy2 = data[x+width*(y-1)] - data[x+width*y];

		/* Average gradient measurements from both sides */
		dxs = ((dx1*dx1) + (dx2*dx2)) / 2;
		dys = ((dy1*dy1) + (dy2*dy2)) / 2;

		/* Calculate overall gradient */
		grad = dxs + dys;

		if ( grad < 2000000 ) continue;

		mask_x = x;
		mask_y = y;

		while ( (did_something)
		     && (distance(mask_x, mask_y, x, y)<50) ) {

			max = data[mask_x+width*mask_y];
			did_something = 0;

			for ( sy=biggest(mask_y-PEAK_WINDOW_SIZE/2, 0);
			      sy<smallest(mask_y+PEAK_WINDOW_SIZE/2, height);
			      sy++ ) {
			for ( sx=biggest(mask_x-PEAK_WINDOW_SIZE/2, 0);
			      sx<smallest(mask_x+PEAK_WINDOW_SIZE/2, width);
			      sx++ ) {

				if ( data[sx+width*sy] > max ) {
					max = data[sx+width*sy];
					mask_x = sx;
					mask_y = sy;
					did_something = 1;
				}

			}
			}

		}

		if ( !did_something ) {

			double d;
			int idx;

			assert(mask_x<image->width);
			assert(mask_y<image->height);
			assert(mask_x>=0);
			assert(mask_y>=0);

			/* Too far from foot point? */
			if ( distance(mask_x, mask_y, x, y) > 50.0 )  continue;

			/* Check for a feature at exactly the
			 * same coordinates */
			image_feature_closest(image->features, mask_x, mask_y,
			                      &d, &idx);

			if ( d > 1.0 ) {

				double rx = 0.0;
				double ry = 0.0;
				double rz = 0.0;

				/* Map and record reflection */
				if ( dump_peaks ) {
					printf("%i %i\n", x, y);
				}

				image_add_feature(image->features,
				                  mask_x, mask_y, image, 1.0);
				map_position(image, x, y, &rx, &ry, &rz);
				fprintf(fh, "%10f %10f %10f %8f\n",
				        rx/1e10, ry/1e10, rz/1e10, 1.0);
			}

		}


	}
	}

	fclose(fh);
}


void index_pattern(struct image *image, int no_index, int dump_peaks)
{
	unsigned int opts;
	int saved_stderr;

	/* Do peak search and splurge out 'xfel.drx' */
	search_peaks(image, dump_peaks);

	if ( no_index ) return;

	saved_stderr = dup(STDERR_FILENO);
	image->dirax_pid = forkpty(&image->dirax_pty, NULL, NULL, NULL);
	if ( image->dirax_pid == -1 ) {
		ERROR("Failed to fork for DirAx\n");
		return;
	}
	if ( image->dirax_pid == 0 ) {

		/* Child process: invoke DirAx */
		struct termios t;

		/* Turn echo off */
		tcgetattr(STDIN_FILENO, &t);
		t.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL);
		tcsetattr(STDIN_FILENO, TCSANOW, &t);

		/* Reconnect stderr */
		dup2(saved_stderr, STDERR_FILENO);

		execlp("dirax", "", (char *)NULL);
		ERROR("Failed to invoke DirAx.\n");
		_exit(0);

	}

	image->dirax_rbuffer = malloc(256);
	image->dirax_rbuflen = 256;
	image->dirax_rbufpos = 0;

	/* Set non-blocking */
	opts = fcntl(image->dirax_pty, F_GETFL);
	fcntl(image->dirax_pty, F_SETFL, opts | O_NONBLOCK);

	image->dirax_step = 1;	/* This starts the "initialisation" procedure */
	image->dirax_read_cell = 0;

	image->dirax = g_io_channel_unix_new(image->dirax_pty);
	g_io_add_watch(image->dirax, G_IO_IN | G_IO_HUP,
	               (GIOFunc)dirax_readable, image);

	image->dirax_ml = g_main_loop_new(NULL, FALSE);
	g_main_loop_run(image->dirax_ml);

	return;
}