aboutsummaryrefslogtreecommitdiff
path: root/src/detector.c
blob: 6ca5e8e731d1620706a64bf1396c15211d116e45 (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
/*
 * detector.c
 *
 * Detector properties
 *
 * (c) 2007-2009 Thomas White <thomas.white@desy.de>
 *
 * pattern_sim - Simulate diffraction patterns from small crystals
 *
 */


#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#include <string.h>

#include "image.h"
#include "utils.h"


/* Pulse energy density in J/m^2 */
#define PULSE_ENERGY_DENSITY (30.0e7)


/* Detector's quantum efficiency */
#define DQE (0.9)


/* Detector's saturation value */
#define SATURATION (60000)


/* Bleed excess intensity into neighbouring pixels */
static void bloom_values(double *tmp, int x, int y,
                          int width, int height, double val)
{
	double overflow;

	overflow = val - SATURATION;

	/* Intensity which bleeds off the edge of the detector is lost */
	if ( x > 0 ) {
		tmp[x-1 + width*y] += overflow / 6.0;
		if ( y > 0 ) {
			tmp[x-1 + width*(y-1)] += overflow / 12.0;
		}
		if ( y < height-1 ) {
			tmp[x-1 + width*(y+1)] += overflow / 12.0;
		}
	}

	if ( x < width-1 ) {
		tmp[x+1 + width*y] += overflow / 6.0;
		if ( y > 0 ) {
			tmp[x+1 + width*(y-1)] += overflow / 12.0;
		}
		if ( y < height-1 ) {
			tmp[x+1 + width*(y+1)] += overflow / 12.0;
		}
	}

	if ( y > 0 ) {
		tmp[x + width*(y-1)] += overflow / 6.0;
	}
	if ( y < height-1 ) {
		tmp[x + width*(y+1)] += overflow / 6.0;
	}
}


static uint16_t *bloom(double *hdr_in, int width, int height)
{
	int x, y;
	uint16_t *data;
	double *tmp;
	double *hdr;
	int did_something;

	data = malloc(width * height * sizeof(uint16_t));
	tmp = malloc(width * height * sizeof(double));
	hdr = malloc(width * height * sizeof(double));

	memcpy(hdr, hdr_in, width*height*sizeof(double));

	/* Apply DQE (once only) */
	for ( x=0; x<width; x++ ) {
	for ( y=0; y<height; y++ ) {
		 hdr[x + width*y] *= DQE;
	}
	}

	do {

		memset(tmp, 0, width*height*sizeof(double));
		did_something = 0;

		for ( x=0; x<width; x++ ) {
		for ( y=0; y<height; y++ ) {

			double hdval;

			hdval = hdr[x + width*y];

			/* Pixel not saturated? */
			if ( hdval <= SATURATION ) {
				tmp[x + width*y] += hdval;
				continue;
			}

			bloom_values(tmp, x, y, width, height, hdval);
			tmp[x + width*y] += SATURATION;
			did_something = 1;

		}
		}

		/* Prepare new input if we're going round for another pass */
		if ( did_something ) {
			memcpy(hdr, tmp, width*height*sizeof(double));
		}

	} while ( did_something );

	/* Turn into integer array of counts */
	for ( x=0; x<width; x++ ) {
	for ( y=0; y<height; y++ ) {
		data[x + width*y] = tmp[x + width*y];
	}
	}

	free(tmp);
	free(hdr);

	return data;
}


void record_image(struct image *image)
{
	int x, y;
	double ph_per_e;
	double twotheta_max, np, sa_per_pixel;

	/* How many photons are scattered per electron? */
	ph_per_e = PULSE_ENERGY_DENSITY * pow(THOMSON_LENGTH, 2.0)
	          / image->xray_energy;
	printf("%e photons are scattered per electron\n", ph_per_e);

	twotheta_max = image->twotheta[0];
	np = sqrt(pow(image->x_centre, 2.0) + pow(image->y_centre, 2.0));
	sa_per_pixel = pow(2.0 * twotheta_max / np, 2.0);
	printf("sa per pixel=%e\n", sa_per_pixel);

	image->hdr = malloc(image->width * image->height * sizeof(double));

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

		double counts;
		double intensity;
		double sa;
		double complex val;

		val = image->sfacs[x + image->width*y];
		intensity = val * conj(val);

		/* What solid angle is subtended by this pixel? */
		sa = sa_per_pixel * cos(image->twotheta[x + image->width*y]);

		counts = intensity * ph_per_e * sa;

		image->hdr[x + image->width*y] = counts;

	}
	}

	image->data = bloom(image->hdr, image->width, image->height);
}