get_hkl.c 2.67 KB
Newer Older
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
/*
 * get_hkl.c
 *
 * Small program to write out a list of h,k,l,I values given a structure
 *
 * (c) 2006-2009 Thomas White <thomas.white@desy.de>
 *
 * 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
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
"  -h, --help                 Display this help message.\n"
"\n"
"  -t, --template=<filename>  Only include reflections mentioned in file.\n");
}


static double *template_reflections(double *ref, const char *filename)
{
	char *rval;
	double *out;
	FILE *fh;

	fh = fopen(filename, "r");
	if ( fh == NULL ) {
		return NULL;
	}

	out = new_list_intensity();

	do {

		char line[1024];
		double val;
		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;

		val = lookup_intensity(ref, h, k, l);
		set_intensity(out, h, k, l, val);

	} while ( rval != NULL );

	fclose(fh);

	return out;
}


/* 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;

		val = lookup_intensity(ref, h, k, l);
		val = poisson_noise(val);
		set_intensity(ref, h, k, l, val);

	}
	}
	}
95
96
97
98
99
100
}


int main(int argc, char *argv[])
{
	int c;
Thomas White's avatar
Thomas White committed
101
	double *ref, *tref;
102
	struct molecule *mol;
Thomas White's avatar
Thomas White committed
103
104
	char *template = NULL;
	int config_noisify = 0;
105
106
107
108

	/* Long options */
	const struct option longopts[] = {
		{"help",               0, NULL,               'h'},
Thomas White's avatar
Thomas White committed
109
110
		{"template",           1, NULL,               't'},
		{"poisson",            0, &config_noisify,     1},
111
112
113
114
		{0, 0, NULL, 0}
	};

	/* Short options */
Thomas White's avatar
Thomas White committed
115
	while ((c = getopt_long(argc, argv, "ht:", longopts, NULL)) != -1) {
116
117
118
119
120
121
122

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

Thomas White's avatar
Thomas White committed
123
124
125
126
127
		case 't' : {
			template = strdup(optarg);
			break;
		}

128
129
130
131
132
133
134
135
136
137
138
139
140
141
		case 0 : {
			break;
		}

		default : {
			return 1;
		}
		}

	}

	mol = load_molecule();
	get_reflections_cached(mol, eV_to_J(2.0e3));
	ref = ideal_intensities(mol->reflections);
Thomas White's avatar
Thomas White committed
142
143
144
145
146
147
148
149
150
151
152
153
154
155

	if ( template != NULL ) {
		tref = template_reflections(ref, template);
		if ( tref == NULL ) {
			ERROR("Couldn't read template file!\n");
			return 1;
		}
		free(ref);
		ref = tref;
	}

	if ( config_noisify ) noisify_reflections(ref);

	write_reflections("results/ideal-reflections.hkl", NULL, tref, 1,
156
157
158
159
	                  mol->cell);

	return 0;
}