aboutsummaryrefslogtreecommitdiff
path: root/libcrystfel/src/indexers/fromfile.c
blob: 040b35e57dbf67c082f9d637b2c2c0b0472969de (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
/*
 * fromfile.c
 *
 * Perform indexing from solution file
 *
 * Authors:
 *   2020 Pascal Hogan-Lamarre <pascal.hogan.lamarre@mail.utoronto.ca>
 *
 * 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 <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <assert.h>
#include <fenv.h>
#include <unistd.h>

#include "image.h"
#include "uthash.h"

/** \file fromfile.h */

/* There are 9 vector components,
 * 2 detector shifts, 1 profile radius,
 *  1 resolution limit */
#define NPARAMS_PER_LINE 11
/* The keys read from file
 * are the filename, event */
#define NKEYS_PER_LINE 2


struct fromfile_keys
{
	char filename[100];
	char event[100];
	int crystal_number;
};


struct fromfile_entries
{
	struct fromfile_keys key;
	float solution[NPARAMS_PER_LINE];
	UT_hash_handle hh;
};


struct fromfile_private
{
	UnitCell *cellTemplate;
	struct fromfile_entries *sol_hash;
};


void print_struct(struct fromfile_entries *sol_hash)
{
	struct fromfile_entries *s;

	s = calloc(1, sizeof(struct fromfile_entries));
	if ( s == NULL ) return;

	for( s=sol_hash; s != NULL; s=s->hh.next ) {
		printf("File %s, event %s, and crystal_number %d \n",
		       s->key.filename, s->key.event, s->key.crystal_number);
	}
}


void full_print_struct(struct fromfile_entries *sol_hash)
{
	struct fromfile_entries *s;
	s = calloc(1, sizeof(struct fromfile_entries));
	if ( s == NULL ) return;

	for( s=sol_hash; s != NULL; s=s->hh.next ) {
		printf("File %s, event %s, and crystal_number %d \n",
		       s->key.filename, s->key.event,
		       s->key.crystal_number);

		printf("Solution parameters:\n");
		for( int i = 0; i < NPARAMS_PER_LINE; i++ ){
			printf("%e", s->solution[i]);
		}
		printf("\n");
	}
}


int ncrystals_in_sol(char *path)
{
	FILE *fh;
	int count = 0;  /* Line counter (result) */
	char c;         /* To store a character read from file */

	fh = fopen(path, "r");

	if ( fh == NULL ) {
		ERROR("%s not found by ncrystals_in_sol\n", path);
		return 0;
	}

	for ( c = getc(fh); c != EOF; c = getc(fh) ) {
		if ( c == '\n' ) {
			count = count + 1;
		}
	}

	/* For the last line, which has no \n at the end*/
	count = count + 1;

	fclose(fh);

	return count;
}


char *read_unknown_string(FILE *fh)
{
	/* Source: "https://stackoverflow.com/questions/16870485/
	 * how-can-i-read-an-input-string-of-unknown-length" */

	char *str = NULL;
	int ch;
	size_t len = 0;
	size_t size = 1;

	str = realloc(NULL, sizeof(char)*size); //size is start size
	if ( !str ) {
		ERROR("Can't reallocate string size");
	}

	while( ( ch = fgetc(fh) ) != ' ' && ch != EOF ){
		if (ch != '\n'){
			str[len++]=ch;
		}
		if(len==size){
			size+=64;
			str = realloc(str, sizeof(char)*(size));
			if ( !str ) {
				ERROR("Can't reallocate string size");
			}
		}
	}

	return realloc(str, sizeof(char)*len);
}


void *fromfile_prepare(char *solution_filename, UnitCell *cell)
{
	FILE *fh;
	int nlines;
	int nparams_in_solution;
	int nentries;
	char *filename;
	char *event;
	int crystal_number;
	int current_line;
	int position_in_current_line;
	struct fromfile_entries *sol_hash = NULL;
	struct fromfile_entries *item = NULL;
	float params[NPARAMS_PER_LINE];

	/* Assembling solution file name from input file name*/
	char *path_to_sol;
	size_t path_len;
	char *core_name = strtok(solution_filename, ".");
	char *extension = ".sol";
	path_len = sizeof(core_name) + sizeof(extension) + sizeof("../");
	path_to_sol = realloc(NULL, sizeof(char)*path_len);
	strcpy(path_to_sol, "../");
	strcat(path_to_sol, core_name);
	strcat(path_to_sol, extension);

	fh = fopen(path_to_sol, "r");

	if ( fh == NULL ) {
		ERROR("%s not found by fromfile_prepare\n", path_to_sol);
		return 0;
	} else {
		STATUS("Found solution file %s\n", path_to_sol);
	}

	nlines = ncrystals_in_sol(path_to_sol);
	/* Total crystal parameters in solution file */
	nparams_in_solution = nlines*NPARAMS_PER_LINE;
	/* Total entries in solution file */
	nentries = nlines*(NPARAMS_PER_LINE+NKEYS_PER_LINE);

	STATUS("Parsing solution file containing %d lines...\n", nlines);

	/* Reads indexing solutions */
	int j = 0; /* follows the solution parameter [0,NPARAMS_PER_LINE] */
	for(int i = 0; i < nentries; i++) {

		crystal_number = 0;

		current_line = i/(NPARAMS_PER_LINE+NKEYS_PER_LINE);

		position_in_current_line = (i)%(NPARAMS_PER_LINE+NKEYS_PER_LINE);

		if ( position_in_current_line == 0 ) {

			filename = read_unknown_string(fh);

			if ( !filename ){
				if ( current_line == nlines-1 ) break;
				printf("Failed to read a filename\n");
				return 0;
			}

		}

		if ( position_in_current_line == 1 ) {
			event = read_unknown_string(fh);
			if ( !event ){
				printf("Failed to read a event\n");
				return 0;
			}

		}

		if ( position_in_current_line > 1 ) {
			if ( fscanf(fh, "%e", &params[j]) != 1 ) {
				printf("Failed to read a parameter\n");
				return 0;
			}
			j+=1;
		}

		if ( j == (NPARAMS_PER_LINE) ) {

			/* Prepare to add to the hash table */
			item = (struct fromfile_entries *)malloc(sizeof *item);
			memset(item, 0, sizeof *item);
			strcpy(item->key.filename, filename);
			strcpy(item->key.event, event);
			item->key.crystal_number = crystal_number;
			for ( int k = 0; k < NPARAMS_PER_LINE; k++){
				item->solution[k] = params[k];
			}

			/* Verify the uniqueness of the key */
			struct fromfile_entries *uniqueness_test;
			HASH_FIND(hh, sol_hash, &item->key,
			          sizeof(struct fromfile_keys), uniqueness_test);

			if ( uniqueness_test == NULL ) {
				HASH_ADD(hh, sol_hash, key,
				         sizeof(struct fromfile_keys), item);
			} else {
				/* Look for the next available set of keys */
				do {
					uniqueness_test = NULL;
					crystal_number += 1;
					item->key.crystal_number = crystal_number;
					HASH_FIND(hh, sol_hash, &item->key,
					          sizeof(struct fromfile_keys),
					          uniqueness_test);
				} while ( uniqueness_test != NULL );

				HASH_ADD(hh, sol_hash, key,
				         sizeof(struct fromfile_keys), item);
			}

			j=0;

		}
	}

	fclose(fh);

	STATUS("Solution parsing done. Have %d parameters and %d total entries.\n",
	       nparams_in_solution, nentries);

	struct fromfile_private *dp;
	dp = (struct fromfile_private *) malloc( sizeof(struct fromfile_private));

	if ( dp == NULL ){
		return NULL;
	}

	dp->cellTemplate = cell;
	dp->sol_hash = sol_hash;

	STATUS("Solution lookup table initialized!\n");

	return (void *)dp;
}


int fromfile_index(struct image *image, void *mpriv, int crystal_number)
{
	Crystal *cr;
	UnitCell *cell;
	float asx, asy, asz, bsx, bsy, bsz, csx, csy, csz;
	float xshift, yshift;
	struct fromfile_entries *item, *p, *pprime;
	int ncryst = 0;
	float *sol;
	struct fromfile_private *dp = mpriv;

	/* Look up the hash table */
	item = calloc(1, sizeof(struct fromfile_entries));
	strcpy(item->key.filename, image->filename);
	strcpy(item->key.event, get_event_string(image->event));
	item->key.crystal_number = crystal_number;

	/* key already in the hash? */
	HASH_FIND(hh, dp->sol_hash, &item->key, sizeof(struct fromfile_keys), p);
	if ( p == NULL ) return 0;

	sol = &(p->solution)[0];

	asx = sol[0] * 1e9;
	asy = sol[1] * 1e9;
	asz = sol[2] * 1e9;
	bsx = sol[3] * 1e9;
	bsy = sol[4] * 1e9;
	bsz = sol[5] * 1e9;
	csx = sol[6] * 1e9;
	csy = sol[7] * 1e9;
	csz = sol[8] * 1e9;
	xshift = sol[9] * 1e-3;
	yshift = sol[10] * 1e-3;

	cell = cell_new();
	cell_set_reciprocal(cell, asx, asy, asz, bsx, bsy, bsz, csx, csy, csz);
	cell_set_lattice_type(cell, cell_get_lattice_type(dp->cellTemplate));
	cell_set_centering(cell, cell_get_centering(dp->cellTemplate));
	cell_set_unique_axis(cell, cell_get_unique_axis(dp->cellTemplate));

	cr = crystal_new();
	ncryst += 1;
	crystal_set_cell(cr, cell);
	crystal_set_det_shift(cr, xshift , yshift);
	image_add_crystal(image, cr);

	/* Look for additional crystals */
	item->key.crystal_number = crystal_number+1;
	HASH_FIND(hh,  dp->sol_hash, &item->key,
	          sizeof(struct fromfile_keys), pprime);

	/* If a similar tag exist,
	 * recursive call increasing the crystal_number by 1 */
	if ( pprime != NULL ) {
		ncryst += fromfile_index(image, mpriv, crystal_number+1);
	}

	return ncryst;
}