get_hkl.c 3.93 KB
Newer Older
1
2
3
4
5
/*
 * get_hkl.c
 *
 * Small program to write out a list of h,k,l,I values given a structure
 *
Thomas White's avatar
Thomas White committed
6
 * (c) 2006-2010 Thomas White <taw@physics.org>
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
 *
 * 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 "sfac.h"
#include "reflections.h"


static void show_help(const char *s)
{
	printf("Syntax: %s [options]\n\n", s);
	printf(
"Write idealised intensity lists.\n"
"\n"
Thomas White's avatar
Thomas White committed
35
36
"  -h, --help                 Display this help message.\n"
"\n"
Thomas White's avatar
Thomas White committed
37
38
"  -t, --template=<filename>  Only include reflections mentioned in file.\n"
"      --poisson              Simulate Poisson samples.\n"
Thomas White's avatar
Thomas White committed
39
"      --twin                 Generate twinned data.\n"
Thomas White's avatar
Thomas White committed
40
"  -o  --output=<filename>    Output filename (default: stdout).\n");
Thomas White's avatar
Thomas White committed
41
42
43
}


44
static int template_reflections(const char *filename, unsigned int *counts)
Thomas White's avatar
Thomas White committed
45
46
47
48
49
50
{
	char *rval;
	FILE *fh;

	fh = fopen(filename, "r");
	if ( fh == NULL ) {
51
		return 1;
Thomas White's avatar
Thomas White committed
52
53
54
55
56
57
58
59
60
61
62
63
64
	}

	do {

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

		rval = fgets(line, 1023, fh);

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

65
		set_count(counts, h, k, l, 1);
Thomas White's avatar
Thomas White committed
66
67
68
69
70

	} while ( rval != NULL );

	fclose(fh);

71
	return 0;
Thomas White's avatar
Thomas White committed
72
73
74
75
76
77
78
79
80
81
82
83
84
}


/* Apply Poisson noise to all reflections */
static void noisify_reflections(double *ref)
{
	signed int h, k, l;

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

		double val;
Thomas White's avatar
Thomas White committed
85
		int c;
Thomas White's avatar
Thomas White committed
86
87

		val = lookup_intensity(ref, h, k, l);
Thomas White's avatar
Thomas White committed
88
89
		c = poisson_noise(val);
		set_intensity(ref, h, k, l, c);
Thomas White's avatar
Thomas White committed
90
91
92

	}
	}
Thomas White's avatar
Thomas White committed
93
	progress_bar(h+INDMAX, 2*INDMAX, "Simulating noise");
Thomas White's avatar
Thomas White committed
94
	}
95
96
97
98
99
100
}


int main(int argc, char *argv[])
{
	int c;
Thomas White's avatar
Thomas White committed
101
	double *ideal_ref;
102
	struct molecule *mol;
Thomas White's avatar
Thomas White committed
103
104
	char *template = NULL;
	int config_noisify = 0;
Thomas White's avatar
Thomas White committed
105
	int config_twin = 0;
106
	int config_za = 0;
Thomas White's avatar
Thomas White committed
107
	char *output = NULL;
Thomas White's avatar
Thomas White committed
108
109
	unsigned int *counts;
	signed int h, k, l;
110
111
112
113

	/* Long options */
	const struct option longopts[] = {
		{"help",               0, NULL,               'h'},
Thomas White's avatar
Thomas White committed
114
115
		{"template",           1, NULL,               't'},
		{"poisson",            0, &config_noisify,     1},
Thomas White's avatar
Thomas White committed
116
		{"output",             1, NULL,               'o'},
Thomas White's avatar
Thomas White committed
117
		{"twin",               0, &config_twin,        1},
118
		{"zone-axis",          0, &config_za,          1},
119
120
121
122
		{0, 0, NULL, 0}
	};

	/* Short options */
Thomas White's avatar
Thomas White committed
123
	while ((c = getopt_long(argc, argv, "ht:o:", longopts, NULL)) != -1) {
124
125
126
127
128
129
130

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

Thomas White's avatar
Thomas White committed
131
132
133
134
135
		case 't' : {
			template = strdup(optarg);
			break;
		}

Thomas White's avatar
Thomas White committed
136
137
138
139
140
		case 'o' : {
			output = strdup(optarg);
			break;
		}

141
142
143
144
145
146
147
148
149
150
151
152
		case 0 : {
			break;
		}

		default : {
			return 1;
		}
		}

	}

	mol = load_molecule();
153
	get_reflections_cached(mol, eV_to_J(1.8e3));
Thomas White's avatar
Thomas White committed
154
155
	ideal_ref = ideal_intensities(mol->reflections);
	counts = new_list_count();
Thomas White's avatar
Thomas White committed
156

Thomas White's avatar
Thomas White committed
157
	if ( template != NULL ) {
Thomas White's avatar
Thomas White committed
158

159
160
		if ( template_reflections(template, counts) != 0 ) {
			ERROR("Failed to template reflections.\n");
Thomas White's avatar
Thomas White committed
161
162
			return 1;
		}
Thomas White's avatar
Thomas White committed
163
164
165
166
167
168
169
170
171
172

	} else {

		for ( h=-INDMAX; h<=INDMAX; h++ ) {
		for ( k=-INDMAX; k<=INDMAX; k++ ) {
		for ( l=-INDMAX; l<=INDMAX; l++ ) {
			set_count(counts, h, k, l, 1);
		}
		}
		}
Thomas White's avatar
Thomas White committed
173

Thomas White's avatar
Thomas White committed
174
175
	}

176
	if ( config_noisify ) noisify_reflections(ideal_ref);
Thomas White's avatar
Thomas White committed
177

Thomas White's avatar
Thomas White committed
178
179
180
181
182
183
	if ( config_twin ) {

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

184
185
186
187
188
189
190
			double a, b, c, d;
			double t;

			if ( abs(h+k) > INDMAX ) {
				set_intensity(ideal_ref, h, k, l, 0.0);
				continue;
			}
Thomas White's avatar
Thomas White committed
191

192
193
			a = lookup_intensity(ideal_ref, h, k, l);
			b = lookup_intensity(ideal_ref, k, h, -l);
194
195
196
197
			c = lookup_intensity(ideal_ref, -(h+k), k, -l);
			d = lookup_intensity(ideal_ref, -(h+k), h, l);

			t = (a+b+c+d)/4.0;
Thomas White's avatar
Thomas White committed
198

199
200
201
202
			set_intensity(ideal_ref, h, k, l, t);
			set_intensity(ideal_ref, k, h, -l, t);
			set_intensity(ideal_ref, -(h+k), h, l, t);
			set_intensity(ideal_ref, -(h+k), k, -l, t);
Thomas White's avatar
Thomas White committed
203
204
205
206
207
208
209

		}
		}
		}

	}

210
	write_reflections(output, counts, ideal_ref, config_za, mol->cell);
211
212
213

	return 0;
}