aboutsummaryrefslogtreecommitdiff
path: root/libcrystfel/src/crystal.c
blob: 71d707d6b9806797a07e57d42c8c7e827b3ce177 (plain)
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
/*
 * crystal.c
 *
 * A class representing a single crystal
 *
 * Copyright © 2013-2021 Deutsches Elektronen-Synchrotron DESY,
 *                       a research centre of the Helmholtz Association.
 *
 * Authors:
 *   2013-2020 Thomas White <taw@physics.org>
 *   2016      Valerio Mariani
 *
 * 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/>.
 *
 */

#include <libcrystfel-config.h>

#include "crystal.h"
#include "utils.h"
#include "reflist-utils.h"


/**
 * \file crystal.h
 */


struct _crystal
{
	/* The image containing the crystal */
	struct image            *image;

	/* Information about the crystal */
	UnitCell                *cell;
	double                  m;     /* Mosaicity in radians */
	double                  osf;
	double                  Bfac;
	double                  profile_radius;
	int                     pr_dud;
	double                  resolution_limit;

	/* Integrated (or about-to-be-integrated) reflections */
	RefList                 *reflections;
	long long int           n_saturated;  /* Number of overloads */
	long long int           n_implausible;  /* Number of implausibly
	                                         * negative reflectionss */

	/* User flag, e.g. for "this is a bad crystal". */
	int                     user_flag;

	/* Text notes, which go in the stream */
	char                    *notes;

	/* Detector shift in metres */
	double			det_shift_x;
	double			det_shift_y;
};


/************************** Constructor / Destructor **************************/


/**
 * Create a new \ref Crystal.
 *
 * \returns The new unit cell, or NULL on failure.
 *
 */
Crystal *crystal_new()
{
	Crystal *cryst;

	cryst = malloc(sizeof(Crystal));
	if ( cryst == NULL ) return NULL;

	cryst->cell = NULL;
	cryst->reflections = NULL;
	cryst->resolution_limit = INFINITY;
	cryst->n_saturated = 0;
	cryst->n_implausible = 0;
	cryst->notes = NULL;
	cryst->user_flag = 0;
	cryst->det_shift_x = 0;
	cryst->det_shift_y = 0;

	return cryst;
}


/**
 * \param cryst: A \ref Crystal to copy.
 *
 * Creates a new \ref Crystal which is a copy of \p cryst.  The copy is a "shallow
 * copy", which means that copies are NOT made of the data structures which
 * \p cryst contains references to, for example its \ref RefList.
 *
 * \returns A (shallow) copy of \p cryst, or NULL on failure.
 *
 */
Crystal *crystal_copy(const Crystal *cryst)
{
	Crystal *c;

	c = crystal_new();
	if ( c == NULL ) return NULL;

	memcpy(c, cryst, sizeof(Crystal));
	if ( c->notes != NULL ) c->notes = strdup(c->notes);

	return c;
}


/**
 * \param cryst: A \ref Crystal to copy.
 *
 * Creates a new \ref Crystal which is a copy of \p cryst.  The copy is a "deep
 * copy", which means that copies ARE made of the data structures which
 * \p cryst contains references to, for example its \ref RefList.
 *
 * \returns A (deep) copy of \p cryst, or NULL on failure.
 *
 */
Crystal *crystal_copy_deep(const Crystal *cryst)
{
	Crystal *c;

	c = crystal_new();
	if ( c == NULL ) return NULL;

	memcpy(c, cryst, sizeof(Crystal));
	if ( c->notes != NULL ) c->notes = strdup(c->notes);

	if ( cryst->cell != NULL ) {
		UnitCell *cell;
		cell = cell_new_from_cell(cryst->cell);
		if ( cell == NULL ) return NULL;
		c->cell = cell;
	}

	if ( cryst->reflections != NULL ) {
		RefList *refls;
		refls = copy_reflist(cryst->reflections);
		if ( refls == NULL ) return NULL;
		c->reflections = refls;
	}

	return c;
}


/**
 * \param cryst: A \ref Crystal to free.
 *
 * Frees a \ref Crystal, and all internal resources concerning that crystal.
 *
 */
void crystal_free(Crystal *cryst)
{
	if ( cryst == NULL ) return;
	free(cryst->notes);
	free(cryst);
}


/********************************** Getters ***********************************/


UnitCell *crystal_get_cell(Crystal *cryst)
{
	return cryst->cell;
}


const UnitCell *crystal_get_cell_const(const Crystal *cryst)
{
	return cryst->cell;
}


double crystal_get_profile_radius(const Crystal *cryst)
{
	return cryst->profile_radius;
}


RefList *crystal_get_reflections(Crystal *cryst)
{
	return cryst->reflections;
}


double crystal_get_resolution_limit(Crystal *cryst)
{
	return cryst->resolution_limit;
}


long long int crystal_get_num_saturated_reflections(Crystal *cryst)
{
	return cryst->n_saturated;
}


long long int crystal_get_num_implausible_reflections(Crystal *cryst)
{
	return cryst->n_implausible;
}


struct image *crystal_get_image(Crystal *cryst)
{
	return cryst->image;
}


const struct image *crystal_get_image_const(const Crystal *cryst)
{
	return cryst->image;
}


double crystal_get_osf(Crystal *cryst)
{
	return cryst->osf;
}


double crystal_get_Bfac(Crystal *cryst)
{
	return cryst->Bfac;
}


int crystal_get_user_flag(Crystal *cryst)
{
	return cryst->user_flag;
}


double crystal_get_mosaicity(Crystal *cryst)
{
	return cryst->m;
}


const char *crystal_get_notes(Crystal *cryst)
{
	return cryst->notes;
}


void crystal_get_det_shift(Crystal *cryst, double* shift_x, double *shift_y)
{
	*shift_x = cryst->det_shift_x;
	*shift_y = cryst->det_shift_y;
}



/********************************** Setters ***********************************/


void crystal_set_cell(Crystal *cryst, UnitCell *cell)
{
	cryst->cell = cell;
}


void crystal_set_profile_radius(Crystal *cryst, double r)
{
	cryst->profile_radius = r;
}


void crystal_set_reflections(Crystal *cryst, RefList *reflist)
{
	cryst->reflections = reflist;
}


void crystal_set_resolution_limit(Crystal *cryst, double res)
{
	cryst->resolution_limit = res;
}


void crystal_set_num_saturated_reflections(Crystal *cryst, long long int n)
{
	cryst->n_saturated = n;
}


void crystal_set_num_implausible_reflections(Crystal *cryst, long long int n)
{
	cryst->n_implausible = n;
}


void crystal_set_image(Crystal *cryst, struct image *image)
{
	cryst->image = image;
}


void crystal_set_osf(Crystal *cryst, double osf)
{
	cryst->osf = osf;
}


void crystal_set_Bfac(Crystal *cryst, double Bfac)
{
	cryst->Bfac = Bfac;
}


void crystal_set_user_flag(Crystal *cryst, int user_flag)
{
	cryst->user_flag = user_flag;
}


void crystal_set_mosaicity(Crystal *cryst, double m)
{
	cryst->m = m;
}


void crystal_set_notes(Crystal *cryst, const char *notes)
{
	free(cryst->notes);  /* free(NULL) is OK */
	cryst->notes = strdup(notes);
}


void crystal_add_notes(Crystal *cryst, const char *notes_add)
{
	size_t len;
	char *nnotes;

	if ( cryst->notes == NULL ) {
		crystal_set_notes(cryst, notes_add);
		return;
	}

	len = strlen(notes_add) + strlen(cryst->notes) + 2;
	nnotes = malloc(len);
	if ( nnotes == NULL ) {
		ERROR("Failed to add notes to crystal.\n");
		return;
	}

	strcpy(nnotes, cryst->notes);
	strcat(nnotes, "\n");
	strcat(nnotes, notes_add);
	free(cryst->notes);
	cryst->notes = nnotes;
}


void crystal_set_det_shift(Crystal *cryst, double shift_x, double shift_y)
{
	cryst->det_shift_x = shift_x;
	cryst->det_shift_y = shift_y;
}