render_hkl.c 9.15 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/*
 * render_hkl.c
 *
 * Draw pretty renderings of reflection lists
 *
 * (c) 2006-2010 Thomas White <taw@physics.org>
 *
 * Part of CrystFEL - crystallography with a FEL
 *
 */


#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <getopt.h>
Thomas White's avatar
Thomas White committed
23
24
#include <cairo.h>
#include <cairo-pdf.h>
25
26
27

#include "utils.h"
#include "reflections.h"
28
#include "povray.h"
29
#include "symmetry.h"
30

31
32
33
34
enum {
	WGHT_I,
	WGHT_SQRTI,
	WGHT_COUNTS,
35
	WGHT_RAWCOUNTS,
36
};
37

38
39
40
41
static void show_help(const char *s)
{
	printf("Syntax: %s [options] <file.hkl>\n\n", s);
	printf(
42
"Render intensity lists in various ways.\n"
43
"\n"
44
45
46
47
48
49
50
51
52
"  -h, --help              Display this help message.\n"
"      --povray            Render a 3D animation using POV-ray.\n"
"      --zone-axis         Render a 2D zone axis pattern.\n"
"      --boost=<val>       Squash colour scale by <val>.\n"
"  -j <n>                  Run <n> instances of POV-ray in parallel.\n"
"  -p, --pdb=<file>        PDB file from which to get the unit cell.\n"
"  -y, --symmetry=<sym>    Expand reflections according to point group <sym>.\n"
"  -w  --weighting=<wght>  Colour/shade the reciprocal lattice points\n"
"                           according to:\n"
53
54
55
56
57
58
"                            I      : the intensity of the reflection.\n"
"                            sqrtI  : the square root of the intensity.\n"
"                            count  : the number of hits for the reflection.\n"
"                                     (after correcting for 'epsilon')\n"
"                            rawcts : the raw number of hits for the\n"
"                                     reflection (no 'epsilon' correction).\n"
59
);
60
61
62
}


63
static void render_za(UnitCell *cell, double *ref, unsigned int *c,
64
                      double boost, const char *sym, int wght)
65
{
Thomas White's avatar
Thomas White committed
66
67
	cairo_surface_t *surface;
	cairo_t *dctx;
68
	double max_u, max_v, max_res, max_val;
69
	double scale_u, scale_v, scale;
Thomas White's avatar
Thomas White committed
70
	double sep_u, sep_v, max_r;
71
72
	double u, v;
	signed int max_h, max_k;
Thomas White's avatar
Thomas White committed
73
	double as, bs, theta;
74
75
76
	double asx, asy, asz;
	double bsx, bsy, bsz;
	double csx, csy, csz;
Thomas White's avatar
Thomas White committed
77
78
	signed int h, k;
	float wh, ht;
79

Thomas White's avatar
Thomas White committed
80
81
	wh = 1024;
	ht = 1024;
82

Thomas White's avatar
Thomas White committed
83
	surface = cairo_pdf_surface_create("za.pdf", wh, ht);
84

Thomas White's avatar
Thomas White committed
85
86
87
88
89
	if ( cairo_surface_status(surface) != CAIRO_STATUS_SUCCESS ) {
		fprintf(stderr, "Couldn't create Cairo surface\n");
		cairo_surface_destroy(surface);
		return;
	}
90

Thomas White's avatar
Thomas White committed
91
	dctx = cairo_create(surface);
92

Thomas White's avatar
Thomas White committed
93
94
95
96
	/* Black background */
	cairo_rectangle(dctx, 0.0, 0.0, wh, ht);
	cairo_set_source_rgb(dctx, 0.0, 0.0, 0.0);
	cairo_fill(dctx);
97

98
	max_u = 0.0;  max_v = 0.0;  max_val = 0.0;
99
	max_res = 0.0;  max_h = 0;  max_k = 0;
Thomas White's avatar
Thomas White committed
100
101

	/* Work out reciprocal lattice spacings and angles for this cut */
102
	if ( cell_get_reciprocal(cell, &asx, &asy, &asz,
Thomas White's avatar
Thomas White committed
103
	                          &bsx, &bsy, &bsz,
104
105
106
107
	                          &csx, &csy, &csz) ) {
		ERROR("Couldn't get reciprocal parameters\n");
		return;
	}
Thomas White's avatar
Thomas White committed
108
109
110
111
112
113
114
115
	theta = angle_between(asx, asy, asz, bsx, bsy, bsz);
	as = modulus(asx, asy, asz) / 1e9;
	bs = modulus(bsx, bsy, bsz) / 1e9;
	STATUS("theta=%f\n", rad2deg(theta));

	for ( h=-INDMAX; h<INDMAX; h++ ) {
	for ( k=-INDMAX; k<INDMAX; k++ ) {

116
		double u, v, val, res;
Thomas White's avatar
Thomas White committed
117
		int ct;
118
		int nequiv, p;
Thomas White's avatar
Thomas White committed
119
120
121
122

		ct = lookup_count(c, h, k, 0);
		if ( ct < 1 ) continue;

123
124
125
126
127
128
129
130
131
		switch ( wght ) {
		case WGHT_I :
			val = lookup_intensity(ref, h, k, 0) / (float)ct;
			break;
		case WGHT_SQRTI :
			val = lookup_intensity(ref, h, k, 0) / (float)ct;
			val = (val>0.0) ? sqrt(val) : 0.0;
			break;
		case WGHT_COUNTS :
132
133
134
135
			val = (float)ct;
			val /= (float)num_equivs(h, k, 0, sym);
			break;
		case WGHT_RAWCOUNTS :
136
137
			val = (float)ct;
			break;
Thomas White's avatar
Thomas White committed
138
		}
139

140
141
142
143
144
145
146
147
148
149
150
151
		nequiv = num_equivs(h, k, 0, sym);
		for ( p=0; p<nequiv; p++ ) {

			signed int he, ke, le;
			get_equiv(h, k, 0, &he, &ke, &le, sym, p);

			u =  (double)he*as*sin(theta);
			v =  (double)he*as*cos(theta) + ke*bs;
			if ( fabs(u) > fabs(max_u) ) max_u = fabs(u);
			if ( fabs(v) > fabs(max_v) ) max_v = fabs(v);
			if ( fabs(val) > fabs(max_val) ) {
				max_val = fabs(val);
152
			}
153
154
155
156
157
			if ( fabs(he) > max_h ) max_h = fabs(he);
			if ( fabs(ke) > max_k ) max_k = fabs(ke);
			res = resolution(cell, he, ke, 0);
			if ( res > max_res ) max_res = res;

158
159
160
		}

	}
Thomas White's avatar
Thomas White committed
161
	}
162

Thomas White's avatar
Thomas White committed
163
	max_res /= 1e9;
164
165
	printf("Maximum resolution is 1/d = %5.3f nm^-1, d = %5.3f nm\n",
	       max_res*2.0, 1.0/(max_res*2.0));
Thomas White's avatar
Thomas White committed
166

167
	if ( max_val <= 0.0 ) {
Thomas White's avatar
Thomas White committed
168
169
		max_r = 4.0;
		goto out;
170
171
	}

Thomas White's avatar
Thomas White committed
172
	/* Choose whichever scaling factor gives the smallest value */
173
174
175
	scale_u = ((double)(wh/2.0)-50.0) / max_u;
	scale_v = ((double)(ht/2.0)-50.0) / max_v;
	scale = (scale_u < scale_v) ? scale_u : scale_v;
176

Thomas White's avatar
Thomas White committed
177
178
	sep_u = as * scale * cos(theta);
	sep_v = bs * scale;
179
180
	max_r = (sep_u < sep_v) ? sep_u : sep_v;
	max_r -= 1.0;  /* Add a tiny separation between circles */
Thomas White's avatar
Thomas White committed
181
182
183
184

	for ( h=-INDMAX; h<INDMAX; h++ ) {
	for ( k=-INDMAX; k<INDMAX; k++ ) {

185
		double u, v, val;
Thomas White's avatar
Thomas White committed
186
		int ct;
187
		int nequiv, p;
Thomas White's avatar
Thomas White committed
188
189

		ct = lookup_count(c, h, k, 0);
190
		if ( ct < 1 ) continue;  /* Must have at least one count */
Thomas White's avatar
Thomas White committed
191

192
193
194
195
196
197
198
199
200
		switch ( wght ) {
		case WGHT_I :
			val = lookup_intensity(ref, h, k, 0) / (float)ct;
			break;
		case WGHT_SQRTI :
			val = lookup_intensity(ref, h, k, 0) / (float)ct;
			val = (val>0.0) ? sqrt(val) : 0.0;
			break;
		case WGHT_COUNTS :
201
202
203
204
			val = (float)ct;
			val /= (float)num_equivs(h, k, 0, sym);
			break;
		case WGHT_RAWCOUNTS :
205
206
			val = (float)ct;
			break;
Thomas White's avatar
Thomas White committed
207
		}
208
209

		val = boost*val/max_val;
Thomas White's avatar
Thomas White committed
210

211
212
213
214
215
216
217
218
		nequiv = num_equivs(h, k, 0, sym);
		for ( p=0; p<nequiv; p++ ) {

			signed int he, ke, le;
			get_equiv(h, k, 0, &he, &ke, &le, sym, p);

			u = (double)he*as*sin(theta);
			v = (double)he*as*cos(theta) + ke*bs;
Thomas White's avatar
Thomas White committed
219

220
221
			cairo_arc(dctx, ((double)wh/2)+u*scale,
					((double)ht/2)+v*scale, max_r,
222
					0, 2*M_PI);
Thomas White's avatar
Thomas White committed
223

224
225
226
227
			cairo_set_source_rgb(dctx, val, val, val);
			cairo_fill(dctx);

		}
Thomas White's avatar
Thomas White committed
228
229

	}
230
231
	}

Thomas White's avatar
Thomas White committed
232
233
234
235
236
237
238
out:
	/* Centre marker */
	cairo_arc(dctx, (double)wh/2,
			(double)ht/2, max_r, 0, 2*M_PI);
	cairo_set_source_rgb(dctx, 1.0, 0.0, 0.0);
	cairo_fill(dctx);

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
	/* Draw indexing lines */
	cairo_set_line_width(dctx, 4.0);
	cairo_move_to(dctx, (double)wh/2, (double)ht/2);
	u = (double)max_h*as*sin(theta);
	v = (double)max_h*as*cos(theta) + 0*bs;
	cairo_line_to(dctx, ((double)wh/2)+u*scale,
	                    ((double)ht/2)+v*scale);
	cairo_set_source_rgb(dctx, 0.0, 1.0, 0.0);
	cairo_stroke(dctx);

	cairo_move_to(dctx,((double)wh/2)+u*scale-40.0,
	                   ((double)ht/2)+v*scale+40.0);
	                   cairo_set_font_size(dctx, 40.0);
	cairo_show_text(dctx, "h");
	cairo_fill(dctx);

	cairo_move_to(dctx, (double)wh/2, (double)ht/2);
	u = 0.0;
	v = (double)max_k*bs;
	cairo_line_to(dctx, ((double)wh/2)+u*scale,
	                    ((double)ht/2)+v*scale);
	cairo_set_source_rgb(dctx, 0.0, 1.0, 0.0);
	cairo_stroke(dctx);

	cairo_move_to(dctx,((double)wh/2)+u*scale-40.0,
	                   ((double)ht/2)+v*scale-40.0);
	                   cairo_set_font_size(dctx, 40.0);
	cairo_show_text(dctx, "k");
	cairo_fill(dctx);

Thomas White's avatar
Thomas White committed
269
270
271
272
273
274
275
276
277
278
279
280
281
282
	cairo_surface_finish(surface);
	cairo_destroy(dctx);
}


int main(int argc, char *argv[])
{
	int c;
	UnitCell *cell;
	char *infile;
	double *ref;
	unsigned int *cts;
	int config_povray = 0;
	int config_zoneaxis = 0;
Thomas White's avatar
Thomas White committed
283
	int config_sqrt = 0;
Thomas White's avatar
Thomas White committed
284
	unsigned int nproc = 1;
285
	char *pdb = NULL;
286
	int r = 0;
287
	double boost = 1.0;
288
	char *sym = NULL;
289
290
	char *weighting = NULL;
	int wght;
Thomas White's avatar
Thomas White committed
291
292
293
294
295
296

	/* Long options */
	const struct option longopts[] = {
		{"help",               0, NULL,               'h'},
		{"povray",             0, &config_povray,      1},
		{"zone-axis",          0, &config_zoneaxis,    1},
297
		{"pdb",                1, NULL,               'p'},
298
		{"boost",              1, NULL,               'b'},
299
		{"symmetry",           1, NULL,               'y'},
300
301
		{"weighting",          1, NULL,               'w'},
		{"counts",             0, &config_sqrt,        1},
Thomas White's avatar
Thomas White committed
302
303
304
305
		{0, 0, NULL, 0}
	};

	/* Short options */
306
	while ((c = getopt_long(argc, argv, "hj:p:w:", longopts, NULL)) != -1) {
Thomas White's avatar
Thomas White committed
307
308

		switch (c) {
Thomas White's avatar
Thomas White committed
309
		case 'h' :
Thomas White's avatar
Thomas White committed
310
311
312
			show_help(argv[0]);
			return 0;

Thomas White's avatar
Thomas White committed
313
		case 'j' :
Thomas White's avatar
Thomas White committed
314
315
316
			nproc = atoi(optarg);
			break;

317
318
319
320
		case 'p' :
			pdb = strdup(optarg);
			break;

321
322
323
324
		case 'b' :
			boost = atof(optarg);
			break;

325
326
327
328
		case 'y' :
			sym = strdup(optarg);
			break;

329
330
331
332
		case 'w' :
			weighting = strdup(optarg);
			break;

Thomas White's avatar
Thomas White committed
333
		case 0 :
Thomas White's avatar
Thomas White committed
334
335
			break;

Thomas White's avatar
Thomas White committed
336
		default :
Thomas White's avatar
Thomas White committed
337
338
339
340
341
			return 1;
		}

	}

342
343
344
345
	if ( pdb == NULL ) {
		pdb = strdup("molecule.pdb");
	}

346
347
348
349
	if ( sym == NULL ) {
		sym = strdup("1");
	}

350
351
352
353
354
355
356
357
358
359
	if ( weighting == NULL ) {
		weighting = strdup("I");
	}

	if ( strcmp(weighting, "I") == 0 ) {
		wght = WGHT_I;
	} else if ( strcmp(weighting, "sqrtI") == 0 ) {
		wght = WGHT_SQRTI;
	} else if ( strcmp(weighting, "count") == 0 ) {
		wght = WGHT_COUNTS;
360
361
	} else if ( strcmp(weighting, "rawcts") == 0 ) {
		wght = WGHT_RAWCOUNTS;
362
363
364
365
366
	} else {
		ERROR("Unrecognised weighting '%s'\n", weighting);
		return 1;
	}

Thomas White's avatar
Thomas White committed
367
368
	infile = argv[optind];

369
	cell = load_cell_from_pdb(pdb);
370
371
372
373
	if ( cell == NULL ) {
		ERROR("Couldn't load unit cell from %s\n", pdb);
		return 1;
	}
Thomas White's avatar
Thomas White committed
374
375
376
377
378
379
380
381
	cts = new_list_count();
	ref = read_reflections(infile, cts, NULL);
	if ( ref == NULL ) {
		ERROR("Couldn't open file '%s'\n", infile);
		return 1;
	}

	if ( config_povray ) {
382
		r = povray_render_animation(cell, ref, cts, nproc);
Thomas White's avatar
Thomas White committed
383
	} else if ( config_zoneaxis ) {
384
		render_za(cell, ref, cts, boost, sym, wght);
Thomas White's avatar
Thomas White committed
385
386
387
388
	} else {
		ERROR("Try again with either --povray or --zone-axis.\n");
	}

389
	free(pdb);
390
	free(sym);
391

392
	return r;
393
}