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
|
/*
* reflections.c
*
* Utilities for handling reflections
*
* (c) 2006-2010 Thomas White <taw@physics.org>
*
* Part of CrystFEL - crystallography with a FEL
*
*/
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#include <complex.h>
#include <string.h>
#include "utils.h"
#include "cell.h"
#include "reflections.h"
void write_reflections(const char *filename, unsigned int *counts,
double *ref, double *phases, int zone_axis,
UnitCell *cell, unsigned int min_counts)
{
FILE *fh;
signed int h, k, l;
if ( filename == NULL ) {
fh = stdout;
} else {
fh = fopen(filename, "w");
}
if ( fh == NULL ) {
ERROR("Couldn't open output file '%s'.\n", filename);
return;
}
/* Write spacings and angle if zone axis pattern */
if ( zone_axis ) {
double a, b, c, alpha, beta, gamma;
cell_get_parameters(cell, &a, &b, &c, &alpha, &beta, &gamma);
fprintf(fh, "a %5.3f nm\n",
(0.5/resolution(cell, 1, 0, 0))*1e9);
fprintf(fh, "b %5.3f nm\n",
(0.5/resolution(cell, 0, 1, 0))*1e9);
fprintf(fh, "angle %5.3f deg\n", rad2deg(alpha));
fprintf(fh, "scale 10\n");
} else {
fprintf(fh, " h k l I phase sigma(I) "
" 1/d(nm^-1) counts\n");
}
for ( h=-INDMAX; h<INDMAX; h++ ) {
for ( k=-INDMAX; k<INDMAX; k++ ) {
for ( l=-INDMAX; l<INDMAX; l++ ) {
int N;
double intensity, s;
char ph[32];
if ( counts ) {
N = lookup_count(counts, h, k, l);
if ( N < min_counts ) continue;
} else {
N = 1;
}
if ( zone_axis && (l != 0) ) continue;
intensity = lookup_phase(ref, h, k, l) / N;
if ( phases != NULL ) {
double p;
p = lookup_intensity(phases, h, k, l);
snprintf(ph, 31, "%8.6f", p);
} else {
strncpy(ph, " -", 31);
}
if ( cell != NULL ) {
s = 2.0*resolution(cell, h, k, l);
} else {
s = 0.0;
}
/* h, k, l, I, sigma(I), s */
fprintf(fh, "%3i %3i %3i %10.2f %s %10.2f %10.2f %7i\n",
h, k, l, intensity, ph, 0.0, s/1.0e9, N);
}
}
}
fclose(fh);
}
double *read_reflections(const char *filename, unsigned int *counts,
double *phases)
{
double *ref;
FILE *fh;
char *rval;
fh = fopen(filename, "r");
if ( fh == NULL ) {
ERROR("Failed to open input file '%s'\n", filename);
return NULL;
}
ref = new_list_intensity();
do {
char line[1024];
signed int h, k, l;
float intensity, ph, res, sigma;
char phs[1024];
int r;
int cts;
rval = fgets(line, 1023, fh);
r = sscanf(line, "%i %i %i %f %s %f %f %i",
&h, &k, &l, &intensity, phs, &sigma, &res, &cts);
if ( r >= 8 ) {
/* Woohoo */
} else if ( r >= 7 ) {
/* No "counts", that's fine.. */
cts = 1;
} else if ( r >= 6 ) {
/* No resolution. Didn't want it anyway. */
res = 0.0;
} else if ( r >= 5 ) {
/* No sigma. It's OK today, but one
* day I'll get you... */
sigma = 0.0;
} else if ( r >= 4 ) {
/* No phase. Better not need it.. */
if ( phases != NULL ) {
ERROR("Need phases and none were specified!\n");
abort();
}
} else {
/* You lose. */
continue;
}
set_intensity(ref, h, k, l, intensity);
if ( phases != NULL ) {
ph = atof(phs);
set_phase(phases, h, k, l, ph);
}
if ( counts != NULL ) {
set_count(counts, h, k, l, cts);
/* In this case, the intensity must be multiplied up
* because other parts of the program will try to
* divide it down. */
set_intensity(ref, h, k, l, intensity*(double)cts);
}
} while ( rval != NULL );
fclose(fh);
return ref;
}
double *ideal_intensities(double complex *sfac)
{
double *ref;
signed int h, k, l;
ref = new_list_intensity();
/* Generate ideal reflections from complex structure factors */
for ( h=-INDMAX; h<=INDMAX; h++ ) {
for ( k=-INDMAX; k<=INDMAX; k++ ) {
for ( l=-INDMAX; l<=INDMAX; l++ ) {
double complex F = lookup_sfac(sfac, h, k, l);
double intensity = pow(cabs(F), 2.0);
set_intensity(ref, h, k, l, intensity);
}
}
}
return ref;
}
void divide_down(double *intensities, unsigned int *counts)
{
int i;
for ( i=0; i<IDIM*IDIM*IDIM; i++ ) {
if ( counts[i] > 0 ) {
intensities[i] /= (double)counts[i];
counts[i] = 1;
}
}
}
|