get_hkl.c 5.05 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"
40
"  -o, --output=<filename>    Output filename (default: stdout).\n"
Thomas White's avatar
Thomas White committed
41
42
"      --zone-axis            Generate hk0 intensities only (and add\n"
"                              Synth2D-style header.\n"
43
"  -i, --intensities=<file>   Read intensities from file instead of\n"
44
45
46
"                              calculating them from scratch.  You might use\n"
"                              this if you need to apply noise or twinning.\n"
"  -p, --pdb=<file>           PDB file from which to get the structure.\n"
Thomas White's avatar
Thomas White committed
47
);
Thomas White's avatar
Thomas White committed
48
49
50
}


51
static int template_reflections(const char *filename, unsigned int *counts)
Thomas White's avatar
Thomas White committed
52
53
54
55
56
57
{
	char *rval;
	FILE *fh;

	fh = fopen(filename, "r");
	if ( fh == NULL ) {
58
		return 1;
Thomas White's avatar
Thomas White committed
59
60
61
62
63
64
65
66
67
68
69
70
71
	}

	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;

72
		set_count(counts, h, k, l, 1);
Thomas White's avatar
Thomas White committed
73
74
75
76
77

	} while ( rval != NULL );

	fclose(fh);

78
	return 0;
Thomas White's avatar
Thomas White committed
79
80
81
82
83
84
85
86
87
88
89
90
91
}


/* 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
92
		int c;
Thomas White's avatar
Thomas White committed
93
94

		val = lookup_intensity(ref, h, k, l);
Thomas White's avatar
Thomas White committed
95
96
		c = poisson_noise(val);
		set_intensity(ref, h, k, l, c);
Thomas White's avatar
Thomas White committed
97
98
99

	}
	}
Thomas White's avatar
Thomas White committed
100
	progress_bar(h+INDMAX, 2*INDMAX, "Simulating noise");
Thomas White's avatar
Thomas White committed
101
	}
102
103
104
105
106
107
}


int main(int argc, char *argv[])
{
	int c;
Thomas White's avatar
Thomas White committed
108
	double *ideal_ref;
109
	double *phases;
110
	struct molecule *mol;
Thomas White's avatar
Thomas White committed
111
112
	char *template = NULL;
	int config_noisify = 0;
Thomas White's avatar
Thomas White committed
113
	int config_twin = 0;
114
	int config_za = 0;
Thomas White's avatar
Thomas White committed
115
	char *output = NULL;
Thomas White's avatar
Thomas White committed
116
	unsigned int *counts;
117
	unsigned int *cts;
118
	char *input = NULL;
Thomas White's avatar
Thomas White committed
119
	signed int h, k, l;
120
	char *filename = NULL;
121
122
123
124

	/* Long options */
	const struct option longopts[] = {
		{"help",               0, NULL,               'h'},
Thomas White's avatar
Thomas White committed
125
126
		{"template",           1, NULL,               't'},
		{"poisson",            0, &config_noisify,     1},
Thomas White's avatar
Thomas White committed
127
		{"output",             1, NULL,               'o'},
Thomas White's avatar
Thomas White committed
128
		{"twin",               0, &config_twin,        1},
129
		{"zone-axis",          0, &config_za,          1},
130
		{"intensities",        1, NULL,               'i'},
131
		{"pdb",                1, NULL,               'p'},
132
133
134
135
		{0, 0, NULL, 0}
	};

	/* Short options */
136
	while ((c = getopt_long(argc, argv, "ht:o:i:p:", longopts, NULL)) != -1) {
137
138

		switch (c) {
Thomas White's avatar
Thomas White committed
139
		case 'h' :
140
141
142
			show_help(argv[0]);
			return 0;

Thomas White's avatar
Thomas White committed
143
		case 't' :
Thomas White's avatar
Thomas White committed
144
145
146
			template = strdup(optarg);
			break;

Thomas White's avatar
Thomas White committed
147
		case 'o' :
Thomas White's avatar
Thomas White committed
148
149
150
			output = strdup(optarg);
			break;

Thomas White's avatar
Thomas White committed
151
		case 'i' :
152
153
154
			input = strdup(optarg);
			break;

Thomas White's avatar
Thomas White committed
155
		case 'p' :
156
157
158
			filename = strdup(optarg);
			break;

Thomas White's avatar
Thomas White committed
159
		case 0 :
160
161
			break;

Thomas White's avatar
Thomas White committed
162
		default :
163
164
165
166
167
			return 1;
		}

	}

168
169
170
171
172
	if ( filename == NULL ) {
		filename = strdup("molecule.pdb");
	}

	mol = load_molecule(filename);
173
	cts = new_list_count();
174
	phases = new_list_intensity(); /* "intensity" type used for phases */
175
	if ( input == NULL ) {
176
177
		ideal_ref = get_reflections(mol, eV_to_J(1790.0), 1/(0.05e-9),
		                            cts, phases);
178
	} else {
179
		ideal_ref = read_reflections(input, cts, phases);
180
181
		free(input);
	}
182

Thomas White's avatar
Thomas White committed
183
	counts = new_list_count();
Thomas White's avatar
Thomas White committed
184

Thomas White's avatar
Thomas White committed
185
	if ( template != NULL ) {
Thomas White's avatar
Thomas White committed
186

187
188
		if ( template_reflections(template, counts) != 0 ) {
			ERROR("Failed to template reflections.\n");
Thomas White's avatar
Thomas White committed
189
190
			return 1;
		}
Thomas White's avatar
Thomas White committed
191
192
193

	} else {

194
195
		/* No template? Then only mark reflections which were
		 * calculated. */
Thomas White's avatar
Thomas White committed
196
197
198
		for ( h=-INDMAX; h<=INDMAX; h++ ) {
		for ( k=-INDMAX; k<=INDMAX; k++ ) {
		for ( l=-INDMAX; l<=INDMAX; l++ ) {
199
200
201
			unsigned int c;
			c = lookup_count(cts, h, k, l);
			set_count(counts, h, k, l, c);
Thomas White's avatar
Thomas White committed
202
203
204
		}
		}
		}
Thomas White's avatar
Thomas White committed
205

Thomas White's avatar
Thomas White committed
206
207
	}

208
	if ( config_noisify ) noisify_reflections(ideal_ref);
Thomas White's avatar
Thomas White committed
209

Thomas White's avatar
Thomas White committed
210
211
212
213
214
215
	if ( config_twin ) {

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

216
217
218
219
220
221
222
			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
223

224
225
			a = lookup_intensity(ideal_ref, h, k, l);
			b = lookup_intensity(ideal_ref, k, h, -l);
226
227
228
229
			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
230

231
232
233
234
			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
235
236
237
238
239
240
241

		}
		}
		}

	}

242
243
	write_reflections(output, counts, ideal_ref, phases,
	                  config_za, mol->cell, 1);
244
245
246

	return 0;
}