aboutsummaryrefslogtreecommitdiff
path: root/libcrystfel/src/crystfel-mille.c
blob: 8c9029fc8b51518635f838ac6b039560ff027de9 (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
/*
 * crystfel-mille.c
 *
 * Interface to Millepede geometry refinement
 *
 * Copyright © 2023 Deutsches Elektronen-Synchrotron DESY,
 *                  a research centre of the Helmholtz Association.
 *
 * Authors:
 *   2023 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/>.
 *
 */

#include <libcrystfel-config.h>

#include <stdlib.h>
#include <assert.h>

#include "image.h"
#include "geometry.h"
#include "cell-utils.h"
#include "predict-refine.h"
#include "profile.h"

#include <mille_c_wrap.h>


static const enum gparam rv[] =
{
	GPARAM_ASX,
	GPARAM_ASY,
	GPARAM_ASZ,
	GPARAM_BSX,
	GPARAM_BSY,
	GPARAM_BSZ,
	GPARAM_CSX,
	GPARAM_CSY,
	GPARAM_CSZ,
};


int mille_label(int group_serial, enum gparam param)
{
	switch ( param ) {
		case GPARAM_DET_TX : return group_serial+1;  /* x-shift */
		case GPARAM_DET_TY : return group_serial+2;  /* y-shift */
		case GPARAM_DET_TZ : return group_serial+3;  /* z-shift */
		case GPARAM_DET_RX : return group_serial+4;  /* Rotation around x */
		case GPARAM_DET_RY : return group_serial+5;  /* Rotation around y */
		case GPARAM_DET_RZ : return group_serial+6;  /* Rotation around z */
		default : abort();
	}
}


void write_mille(Mille *mille, int n, UnitCell *cell,
                 struct reflpeak *rps, struct image *image,
                 gsl_matrix **Minvs)
{
	int i;

	for ( i=0; i<n; i++ ) {

		float local_gradients_fs[9];
		float local_gradients_ss[9];
		float global_gradients_fs[64];
		float global_gradients_ss[64];
		int labels[64];
		int j;
		const struct detgeom_panel_group *group;

		/* Local gradients */
		for ( j=0; j<9; j++ ) {
			fs_ss_gradient(rv[j], rps[i].refl, cell,
			               &image->detgeom->panels[rps[i].peak->pn],
			               Minvs[rps[i].peak->pn], 0, 0, 0,
			               &local_gradients_fs[j],
			               &local_gradients_ss[j]);
		}

		/* Global gradients (for each group, at least level of hierarchy) */
		j = 0;
		group = image->detgeom->panels[rps[i].peak->pn].group;
		while ( group != NULL ) {

			double cx, cy, cz;

			detgeom_group_center(group, &cx, &cy, &cz);

			fs_ss_gradient(GPARAM_DET_TX, rps[i].refl, cell,
			               &image->detgeom->panels[rps[i].peak->pn],
			               Minvs[rps[i].peak->pn], 0, 0, 0,
			               &global_gradients_fs[j],
			               &global_gradients_ss[j]);
			labels[j] = mille_label(group->serial, GPARAM_DET_TX);
			j++;

			fs_ss_gradient(GPARAM_DET_TY, rps[i].refl, cell,
			               &image->detgeom->panels[rps[i].peak->pn],
			               Minvs[rps[i].peak->pn], 0, 0, 0,
			               &global_gradients_fs[j],
			               &global_gradients_ss[j]);
			labels[j] = mille_label(group->serial, GPARAM_DET_TY);
			j++;

			fs_ss_gradient(GPARAM_DET_TZ, rps[i].refl, cell,
			               &image->detgeom->panels[rps[i].peak->pn],
			               Minvs[rps[i].peak->pn], 0, 0, 0,
			               &global_gradients_fs[j],
			               &global_gradients_ss[j]);
			labels[j] = mille_label(group->serial, GPARAM_DET_TZ);
			j++;

			fs_ss_gradient(GPARAM_DET_RX, rps[i].refl, cell,
			               &image->detgeom->panels[rps[i].peak->pn],
			               Minvs[rps[i].peak->pn], cx, cy, cz,
			               &global_gradients_fs[j],
			               &global_gradients_ss[j]);
			labels[j] = mille_label(group->serial, GPARAM_DET_RX);
			j++;

			fs_ss_gradient(GPARAM_DET_RY, rps[i].refl, cell,
			               &image->detgeom->panels[rps[i].peak->pn],
			               Minvs[rps[i].peak->pn], cx, cy, cz,
			               &global_gradients_fs[j],
			               &global_gradients_ss[j]);
			labels[j] = mille_label(group->serial, GPARAM_DET_RY);
			j++;

			fs_ss_gradient(GPARAM_DET_RZ, rps[i].refl, cell,
			               &image->detgeom->panels[rps[i].peak->pn],
			               Minvs[rps[i].peak->pn], cx, cy, cz,
			               &global_gradients_fs[j],
			               &global_gradients_ss[j]);
			labels[j] = mille_label(group->serial, GPARAM_DET_RZ);
			j++;

			group = group->parent;
		}

		/* Add fs measurement */
		mille_add_measurement(mille,
		                      9, local_gradients_fs,
		                      j, global_gradients_fs, labels,
		                      fs_dev(&rps[i], image->detgeom),
		                      0.65*image->detgeom->panels[rps[i].peak->pn].pixel_pitch);

		/* Add ss measurement */
		mille_add_measurement(mille,
		                      9, local_gradients_ss,
		                      j, global_gradients_ss, labels,
		                      ss_dev(&rps[i], image->detgeom),
		                      0.65*image->detgeom->panels[rps[i].peak->pn].pixel_pitch);
	}
}


Mille *crystfel_mille_new(const char *outFileName,
                          int asBinary,
                          int writeZero)
{
	return mille_new(outFileName, asBinary, writeZero);
}


void crystfel_mille_free(Mille *m)
{
	mille_free(m);
}


void crystfel_mille_delete_last_record(Mille *m)
{
	mille_delete_last_record(m);
}


void crystfel_mille_write_record(Mille *m)
{
	mille_write_record(m);
}