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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
|
/*
* reflist-utils.c
*
* Utilities to complement the core reflist.c
*
* Copyright © 2012 Thomas White <taw@physics.org>
*
* This file is part of CrystFEL.
*
* CrystFEL is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* CrystFEL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with CrystFEL. If not, see <http://www.gnu.org/licenses/>.
*
*/
#define _ISOC99_SOURCE
#define _GNU_SOURCE
#include <math.h>
#include <stdio.h>
#include <assert.h>
#include "reflist.h"
#include "cell.h"
#include "utils.h"
#include "reflist-utils.h"
#include "symmetry.h"
/**
* SECTION:reflist-utils
* @short_description: Reflection list utilities
* @title: RefList utilities
* @section_id:
* @see_also:
* @include: "reflist-utils.h"
* @Image:
*
* There are some utility functions associated with the core %RefList.
**/
int check_list_symmetry(RefList *list, const SymOpList *sym)
{
Reflection *refl;
RefListIterator *iter;
SymOpMask *mask;
mask = new_symopmask(sym);
if ( mask == NULL ) {
ERROR("Couldn't create mask for list symmetry check.\n");
return 1;
}
for ( refl = first_refl(list, &iter);
refl != NULL;
refl = next_refl(refl, iter) ) {
int j;
int found = 0;
signed int h, k, l;
int n;
get_indices(refl, &h, &k, &l);
special_position(sym, mask, h, k, l);
n = num_equivs(sym, mask);
for ( j=0; j<n; j++ ) {
signed int he, ke, le;
Reflection *f;
get_equiv(sym, mask, j, h, k, l, &he, &ke, &le);
f = find_refl(list, he, ke, le);
if ( f != NULL ) found++;
}
assert(found != 0); /* That'd just be silly */
if ( found > 1 ) {
STATUS("Found %i %i %i: %i times:\n", h, k, l, found);
for ( j=0; j<n; j++ ) {
signed int he, ke, le;
Reflection *f;
get_equiv(sym, mask, j, h, k, l, &he, &ke, &le);
f = find_refl(list, he, ke, le);
if ( f != NULL ) {
STATUS("%3i %3i %3i\n", he, ke, le);
}
}
free_symopmask(mask);
return 1; /* Symmetry is wrong! */
}
}
free_symopmask(mask);
return 0;
}
int find_equiv_in_list(RefList *list, signed int h, signed int k,
signed int l, const SymOpList *sym, signed int *hu,
signed int *ku, signed int *lu)
{
int i;
int found = 0;
for ( i=0; i<num_equivs(sym, NULL); i++ ) {
signed int he, ke, le;
Reflection *f;
get_equiv(sym, NULL, i, h, k, l, &he, &ke, &le);
f = find_refl(list, he, ke, le);
/* There must only be one equivalent. If there are more, it
* indicates that the user lied about the input symmetry.
* This situation should have been checked for earlier by
* calling check_symmetry() with 'items' and 'mero'. */
if ( (f != NULL) && !found ) {
*hu = he; *ku = ke; *lu = le;
return 1;
}
}
return 0;
}
/**
* write_reflections_to_file:
* @fh: File handle to write to
* @list: The reflection list to write
*
* This function writes the contents of @list to @fh,
*
* Reflections which have a redundancy of zero will not be written.
*
* The resulting list can be read back with read_reflections_from_file().
**/
void write_reflections_to_file(FILE *fh, RefList *list)
{
Reflection *refl;
RefListIterator *iter;
fprintf(fh, " h k l I phase sigma(I) "
" counts fs/px ss/px\n");
for ( refl = first_refl(list, &iter);
refl != NULL;
refl = next_refl(refl, iter) )
{
signed int h, k, l;
double intensity, esd_i, s, ph;
int red;
double fs, ss;
char res[16];
char phs[16];
int have_phase;
get_indices(refl, &h, &k, &l);
get_detector_pos(refl, &fs, &ss);
intensity = get_intensity(refl);
esd_i = get_esd_intensity(refl);
red = get_redundancy(refl);
ph = get_phase(refl, &have_phase);
/* Reflections with redundancy = 0 are not written */
if ( red == 0 ) continue;
if ( have_phase ) {
snprintf(phs, 16, "%8.2f", rad2deg(ph));
} else {
strncpy(phs, " -", 15);
}
fprintf(fh,
"%3i %3i %3i %10.2f %s %10.2f %7i %6.1f %6.1f\n",
h, k, l, intensity, phs, esd_i, red, fs, ss);
}
}
/**
* write_reflist:
* @filename: Filename
* @list: The reflection list to write
*
* This function writes the contents of @list to @file,
*
* Reflections which have a redundancy of zero will not be written.
*
* The resulting list can be read back with read_reflections_from_file() or
* read_reflections().
*
* This is a convenience function which simply opens @filename and then calls
* write_reflections_to_file.
*
* Returns: zero on success, non-zero on failure.
**/
int write_reflist(const char *filename, RefList *list)
{
FILE *fh;
if ( filename == NULL ) {
fh = stdout;
} else {
fh = fopen(filename, "w");
}
if ( fh == NULL ) {
ERROR("Couldn't open output file '%s'.\n", filename);
return 1;
}
write_reflections_to_file(fh, list);
fprintf(fh, REFLECTION_END_MARKER"\n");
fclose(fh);
return 0;
}
RefList *read_reflections_from_file(FILE *fh)
{
char *rval = NULL;
int first = 1;
RefList *out;
out = reflist_new();
do {
char line[1024];
signed int h, k, l;
float intensity, sigma, fs, ss;
char phs[1024];
char ress[1024];
int cts;
int r;
Reflection *refl;
rval = fgets(line, 1023, fh);
if ( rval == NULL ) continue;
chomp(line);
if ( strcmp(line, REFLECTION_END_MARKER) == 0 ) return out;
r = sscanf(line, "%i %i %i %f %s %f %i %f %f",
&h, &k, &l, &intensity, phs, &sigma, &cts, &fs, &ss);
if ( (r != 9) && (!first) ) {
reflist_free(out);
return NULL;
}
first = 0;
if ( r == 9 ) {
double ph;
char *v;
refl = add_refl(out, h, k, l);
set_intensity(refl, intensity);
set_detector_pos(refl, 0.0, fs, ss);
set_esd_intensity(refl, sigma);
set_redundancy(refl, cts);
ph = strtod(phs, &v);
if ( v != NULL ) set_phase(refl, deg2rad(ph));
/* The 1/d value is actually ignored. */
}
} while ( rval != NULL );
/* Got read error of some kind before finding PEAK_LIST_END_MARKER */
return NULL;
}
RefList *read_reflections(const char *filename)
{
FILE *fh;
RefList *out;
if ( filename == NULL ) {
fh = stdout;
} else {
fh = fopen(filename, "r");
}
if ( fh == NULL ) {
ERROR("Couldn't open input file '%s'.\n", filename);
return NULL;
}
out = read_reflections_from_file(fh);
fclose(fh);
return out;
}
/**
* asymmetric_indices:
* @in: A %RefList
* @sym: A %SymOpList
*
* This function creates a newly allocated copy of @in, but indexed using the
* asymmetric indices according to @sym instead of the original indices. The
* original indices are stored and can be retrieved using
* get_symmetric_indices() if required.
*
* Returns: the new %RefList, or NULL on failure.
**/
RefList *asymmetric_indices(RefList *in, const SymOpList *sym)
{
Reflection *refl;
RefListIterator *iter;
RefList *new;
new = reflist_new();
if ( new == NULL ) return NULL;
for ( refl = first_refl(in, &iter);
refl != NULL;
refl = next_refl(refl, iter) ) {
signed int h, k, l;
signed int ha, ka, la;
Reflection *cr;
get_indices(refl, &h, &k, &l);
get_asymm(sym, h, k, l, &ha, &ka, &la);
cr = add_refl(new, ha, ka, la);
assert(cr != NULL);
copy_data(cr, refl);
set_symmetric_indices(cr, h, k, l);
}
return new;
}
/**
* resolution_limits:
* @list: A %RefList
* @cell: A %UnitCell
* @rmin: Place to store the minimum 1/d value
* @rmax: Place to store the maximum 1/d value
*
* This function calculates the minimum and maximum values of 1/d, where
* 2dsin(theta) = wavelength. The answers are in m^-1.
**/
void resolution_limits(RefList *list, UnitCell *cell,
double *rmin, double *rmax)
{
Reflection *refl;
RefListIterator *iter;
*rmin = INFINITY;
*rmax = 0.0;
for ( refl = first_refl(list, &iter);
refl != NULL;
refl = next_refl(refl, iter) )
{
double r;
signed int h, k, l;
get_indices(refl, &h, &k, &l);
r = 2.0 * resolution(cell, h, k, l);
if ( r > *rmax ) *rmax = r;
if ( r < *rmin ) *rmin = r;
}
}
/**
* max_intensity:
* @list: A %RefList
*
* Returns: The maximum intensity in @list.
**/
double max_intensity(RefList *list)
{
Reflection *refl;
RefListIterator *iter;
double max;
max = -INFINITY;
for ( refl = first_refl(list, &iter);
refl != NULL;
refl = next_refl(refl, iter) )
{
double val = get_intensity(refl);
if ( val > max ) max = val;
}
return max;
}
/**
* res_cutoff:
* @list: A %RefList
*
* Applies a resolution cutoff to @list, returning the new version and freeing
* the old version.
*
* Returns: A new %RefList with resolution cutoff applied
**/
RefList *res_cutoff(RefList *list, UnitCell *cell, double min, double max)
{
Reflection *refl;
RefListIterator *iter;
RefList *new;
new = reflist_new();
for ( refl = first_refl(list, &iter);
refl != NULL;
refl = next_refl(refl, iter) )
{
double one_over_d;
signed int h, k, l;
Reflection *n;
get_indices(refl, &h, &k, &l);
one_over_d = 2.0 * resolution(cell, h, k, l);
if ( one_over_d < min ) continue;
if ( one_over_d > max ) continue;
n = add_refl(new, h, k, l);
copy_data(n, refl);
}
reflist_free(list);
return new;
}
/**
* copy_reflist:
* @list: A %RefList
*
* Returns: A copy of %RefList.
**/
RefList *copy_reflist(RefList *list)
{
Reflection *refl;
RefListIterator *iter;
RefList *new;
new = reflist_new();
for ( refl = first_refl(list, &iter);
refl != NULL;
refl = next_refl(refl, iter) )
{
signed int h, k, l;
Reflection *n;
get_indices(refl, &h, &k, &l);
n = add_refl(new, h, k, l);
copy_data(n, refl);
}
return new;
}
|