forked from biochem-fan/eiger2cbf
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathxsplambda2cbf.c
705 lines (639 loc) · 26.6 KB
/
xsplambda2cbf.c
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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
/*
X-Spectrum Lambda HDF5 to CBF converter
Derived by Herbert J. Bernstein from
eiger2cbf
Written by Takanori Nakane
To build:
Linux
gcc -std=c99 -o xsplambda2cbf -g \
-I$HOME/prog/dials/modules/cbflib/include \
-L$HOME/prog/dials/build/lib -Ilz4 \
xsplambda2cbf.c \
lz4/lz4.c lz4/h5zlz4.c \
bitshuffle/bshuf_h5filter.c \
bitshuffle/bshuf_h5plugin.c \
bitshuffle/bitshuffle.c \
/usr/lib/x86_64-linux-gnu/libhdf5_hl.a \
/usr/lib/x86_64-linux-gnu/libhdf5.a \
-lcbf -lm -lpthread -lz -ldl
Mac OS X
gcc -std=c99 -o xsplambda2cbf -g \
-ICBFlib-0.9.5.2/include -Ilz4 \
xsplambda2cbf.c \
lz4/lz4.c lz4/h5zlz4.c \
bitshuffle/bshuf_h5filter.c \
bitshuffle/bshuf_h5plugin.c \
bitshuffle/bitshuffle.c \
$HOME/local/lib/libhdf5_hl.a \
$HOME/local/lib/libhdf5.a \
CBFlib-0.9.5.2/lib/libcbf.a \
-lm -lpthread -lz -ldl
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cbf.h"
#include "cbf_simple.h"
#include "cbf_string.h"
#include "hdf5.h"
#include "hdf5_hl.h"
extern const H5Z_class2_t H5Z_LZ4;
extern const H5Z_class2_t bshuf_H5Filter;
void register_filters() {
H5Zregister(&H5Z_LZ4);
H5Zregister(&bshuf_H5Filter);
}
void usage( int argc, char **argv ) {
printf("Usage:\n");
printf(" %s [options] filename.h5 -- get number of frames\n", argv[0]);
printf(" %s [options] filename.h5 N out.cbf -- write N-th frame to out.cbf\n", argv[0]);
printf(" %s [options] filename.h5 N -- write N-th frame to STDOUT\n", argv[0]);
printf(" %s [options] filename.h5 N:M out -- write N to M-th frames to outNNNNNN.cbf\n", argv[0]);
printf(" N starts from 1. The file should be \"master\" h5.\n");
printf(" options:\n");
printf(" -h or --help -- print this message\n");
printf(" -v or --verbose -- provide more detail in output\n");
printf(" --beam-center beamx,beamy -- new beam center in pixels\n");
printf(" --wavelength wavelen -- new wavelength in Angstroms\n");
printf(" --distance dist -- new distance in mm\n");
printf(" --osc-start ang -- new start angle for frame 1 in degrees\n");
printf(" --osc-width wid -- new oscillation angle for each frame in degrees\n");
printf(" --nimages images -- override the number of images\n");
return;
}
int main(int argc, char **argv) {
cbf_handle cbf;
char header[4096] = {};
int xpixels = -1, ypixels = -1;
double beamx = -1, beamy = -1;
long ibeamx, ibeamy;
int nimages = -1, depth = -1, countrate_cutoff = -1;
int ntrigger = -1;
double nbeamx = -1, nbeamy = -1;
long nnimages = -1L;
int from = -1, to = -1;
int ret;
int retfromto;
int usage_printed = 0;
int optcount = 0; /* count of command line options */
int verbose = 0; /* verbose mode */
int new_beam_cent = 0; /* new beam center provided */
int new_nimages = 0; /* new number of images provided */
int ii;
char* endptr;
char* fndptr;
double pixelsize = -1., wavelength = -1., distance = -1., count_time = -1.,
frame_time = -1., osc_width = -1., osc_start = -9999., thickness = -1.;
double new_wavelength = -1., new_distance = -1., new_osc_width = .1, new_osc_start = -9999. ;
char detector_sn[4096] = {}, version[4096] = {};
char* description;
description="LAMBDA";
hid_t hdf;
fprintf(stderr, "X-Spectrum Lambda HDF5 to CBF converter (version 180818)\n");
fprintf(stderr, " derived by Herbert J. Berstein\n");
fprintf(stderr, " from eiger2cbf written by Takanori Nakane\n");
fprintf(stderr, " see https://github.com/nsls-ii-mx/eiger2cbf for details.\n");
fprintf(stderr, " forked from https://github.com/biochem-fan/eiger2cbf for details.\n\n");
for (ii=1; ii < argc; ii++) {
if (!strcmp(argv[ii],"-h") || !strcmp(argv[ii],"--help")) {
usage (argc, argv);
optcount ++;
usage_printed ++;
} else if (!cbf_cistrcmp(argv[ii],"-v") || !cbf_cistrcmp(argv[ii],"--verbose")) {
verbose = 1;
optcount ++;
} else if (!cbf_cistrcmp(argv[ii],"--beam-center") || !cbf_cistrcmp(argv[ii],"--beam_center") ) {
new_beam_cent = 1;
optcount ++;
if (ii < argc-1) {
ii++;
optcount ++;
nbeamx=strtod(argv[ii],&endptr);
if (!endptr || endptr==argv[ii] || *endptr!=',') {
new_beam_cent = 0;
fprintf(stderr, "xsplambda2cbf error: --beam-center provided without two comma-separated values; ignored\n");
usage(argc, argv);
usage_printed ++;
} else {
endptr++;
nbeamy=strtod(endptr,&fndptr);
if (!fndptr || fndptr==endptr|| *fndptr!='\0') {
new_beam_cent = 0;
fprintf(stderr, "xsplambda2cbf error: --beam-center provided without two comma-separated values; ignored\n");
usage(argc, argv);
usage_printed ++; }
}
} else {
fprintf(stderr, "xsplambda2cbf error: --beam-center provided without a value; ignored\n");
usage(argc, argv);
usage_printed ++;
new_beam_cent = 0;
}
} else if (!cbf_cistrcmp(argv[ii],"--nimages")) {
new_nimages = 1;
optcount ++;
if (ii < argc-1) {
ii++; optcount++;
nnimages=strtol(argv[ii],&endptr,10);
if (!endptr || endptr==argv[ii]) {
new_nimages = 0;
fprintf(stderr, "xsplambda2cbf error: --nimages invalid value; ignored\n");
usage(argc,argv);
usage_printed++;
}
} else {
fprintf(stderr, "xsplambda2cbf error: --nimages no value; ignored\n");
usage(argc,argv);
usage_printed++;
}
} else if (!cbf_cistrcmp(argv[ii],"--wavelength")) {
new_wavelength = -1.;
optcount ++;
if (ii < argc-1) {
ii++; optcount++;
new_wavelength=strtod(argv[ii],&endptr);
if (!endptr || endptr==argv[ii]) {
new_wavelength = -1.;
fprintf(stderr, "xsplambda2cbf error: --wavelength invalid value; ignored\n");
usage(argc,argv);
usage_printed++;
}
} else {
fprintf(stderr, "xsplambda2cbf error: --wavelength no value; ignored\n");
usage(argc,argv);
usage_printed++;
}
} else if (!cbf_cistrcmp(argv[ii],"--distance")) {
new_distance = -1.;
optcount ++;
if (ii < argc-1) {
ii++; optcount++;
new_distance=strtod(argv[ii],&endptr);
if (!endptr || endptr==argv[ii]) {
new_distance = -1.;
fprintf(stderr, "xsplambda2cbf error: --distance invalid value; ignored\n");
usage(argc,argv);
usage_printed++;
}
} else {
fprintf(stderr, "xsplambda2cbf error: --distance no value; ignored\n");
usage(argc,argv);
usage_printed++;
}
} else if (!cbf_cistrcmp(argv[ii],"--osc_width") || !cbf_cistrcmp(argv[ii],"--osc-width")) {
new_osc_width = -1.;
optcount ++;
if (ii < argc-1) {
ii++; optcount++;
new_osc_width=strtod(argv[ii],&endptr);
if (!endptr || endptr==argv[ii]) {
new_osc_width = -1.;
fprintf(stderr, "xsplambda2cbf error: --osc_width invalid value; ignored\n");
usage(argc,argv);
usage_printed++;
}
} else {
fprintf(stderr, "xsplambda2cbf error: --osc_width no value; ignored\n");
usage(argc,argv);
usage_printed++;
}
} else if (!cbf_cistrcmp(argv[ii],"--osc_start") || !cbf_cistrcmp(argv[ii],"--osc-start") ) {
new_osc_start = -9999.;
optcount ++;
if (ii < argc-1) {
ii++; optcount++;
new_osc_start=strtod(argv[ii],&endptr);
if (!endptr || endptr==argv[ii]) {
new_osc_start = -9999.;
fprintf(stderr, "xsplambda2cbf error: --osc_start invalid value; ignored\n");
usage(argc,argv);
usage_printed++;
}
} else {
fprintf(stderr, "xsplambda2cbf error: --osc_start no value; ignored\n");
usage(argc,argv);
usage_printed++;
}
} else break;
}
if (argc-optcount <= 1 || argc-optcount >= 5) {
if (usage_printed == 0) usage (argc, argv);
return -1;
}
register_filters();
hdf = H5Fopen(argv[1+optcount], H5F_ACC_RDONLY, H5P_DEFAULT);
if (hdf < 0) {
fprintf(stderr, "xsplambda2cbf error: failed to open file %s\n", argv[1+optcount]);
return -1;
}
/* try for nimages from X-spectrum nxs favored location */
nimages = 0;
H5LTread_dataset_int(hdf, "/entry/instrument/detector/collection/number_of_frames", &nimages);
if (nimages < 1) {/* If that did not work, try eiger locations */
H5LTread_dataset_int(hdf, "/entry/instrument/detector/detectorSpecific/nimages", &nimages);
H5LTread_dataset_int(hdf, "/entry/instrument/detector/detectorSpecific/ntrigger", &ntrigger);
if (nimages == 1 && ntrigger > 1) {
fprintf(stderr, "xsplambda2cbf warning: nimages == 1 and ntrigger == %d \n", ntrigger);
fprintf(stderr, "xsplambda2cbf warning: setting nimages to ntrigger \n");
nimages = ntrigger;
}
}
if (new_nimages && nnimages > 0) {
nimages = nnimages;
fprintf(stderr, "xsplambda2cbf warning: setting nimages to %d \n",nimages);
}
if (argc-optcount == 2) {
if (!verbose) {
printf("%d\n", nimages);
} else {
printf("No. images: %d\n", nimages);
}
H5Fclose(hdf);
return 0;
}
retfromto = sscanf(argv[2+optcount], "%d:%d", &from, &to);
if (retfromto == 0) {
fprintf(stderr, "Failed to parse output frame number(s).");
return -1;\
} else if (retfromto == 1) {
to = from;
}
if ((to != from || retfromto != -1) && argc-optcount < 4) {
fprintf(stderr, "You cannot output multiple images into STDOUT.");
return -1;
}
fprintf(stderr, "Going to convert frame %d to %d.\n", from, to);
H5Eset_auto(0, NULL, NULL); // Comment out this line for debugging.
fprintf(stderr, "Metadata in HDF5:\n");
if ( H5LTread_dataset_string(hdf, "/entry/instrument/detector/detector_number", detector_sn) >= 0 ) {
fprintf(stderr, " /entry/instrument/detector/detector_number = %s\n", detector_sn);
} else {
detector_sn[0]='0';
detector_sn[1]='\0';
}
if ( H5LTread_dataset_string(hdf, "/entry/instrument/detector/detectorSpecific/software_version", version) >= 0 ) {
fprintf(stderr, " /entry/instrument/detector/detectorSpecific/software_version = %s\n", version);
}
if ( H5LTread_dataset_int(hdf, "/entry/instrument/detector/collection/frame_depth", &depth) >= 0
|| H5LTread_dataset_int(hdf, "/entry/instrument/detector/bit_depth_readout", &depth) >= 0
|| H5LTread_dataset_int(hdf, "/entry/instrument/detector/bit_depth_image", &depth) >= 0 ) {
fprintf(stderr, " /entry/instrument/detector/bit_depth_image = %d\n", depth);
} else {
fprintf(stderr, " WARNING: /entry/instrument/detector/bit_depth_image is not avaialble. We assume 16 bit.\n");
depth = 16;
}
unsigned int error_val = (unsigned int)(((unsigned long long)1 << depth) - 1);
// Saturation value
H5LTread_dataset_int(hdf, "/entry/instrument/detector/detectorSpecific/saturation_value", &countrate_cutoff);
if (countrate_cutoff > 0) {
fprintf(stderr, " /entry/instrument/detector/detectorSpecific/saturation_value = %d\n", countrate_cutoff);
} else {
fprintf(stderr, " /entry/instrument/detector/detectorSpecific/saturation_value not present. Trying another place.\n");
H5LTread_dataset_int(hdf, "/entry/instrument/detector/detectorSpecific/countrate_correction_count_cutoff", &countrate_cutoff);
if (countrate_cutoff > 0) {
fprintf(stderr, " /entry/instrument/detector/detectorSpecific/countrate_correction_count_cutoff = %d\n", countrate_cutoff);
countrate_cutoff++;
} else {
fprintf(stderr, " /entry/instrument/detector/detectorSpecific/countrate_correction_count_cutoff not present. Trying another place.\n");
// < 1.4
H5LTread_dataset_int(hdf, "/entry/instrument/detector/detectorSpecific/detectorModule_000/countrate_correction_count_cutoff", &countrate_cutoff);
if (countrate_cutoff > 0) {
fprintf(stderr, " /entry/instrument/detector/detectorSpecific/detectorModule_000/countrate_correction_count_cutoff = %d\n", countrate_cutoff);
fprintf(stderr, " WARNING: The use of this field is not recommended now.\n");
fprintf(stderr, " You might want to change the OVERLOAD setting in your subsequent processing.\n");
countrate_cutoff++;
} else {
fprintf(stderr, " /entry/instrument/detector/detectorSpecific/detectorModule_000/countrate_correction_count_cutoff not present.\n");
countrate_cutoff = error_val - 1;
fprintf(stderr, " As a last resort, we will put an arbitrary large number (%d) in the header.\n", countrate_cutoff);
fprintf(stderr, " You might want to change the OVERLOAD setting in your subsequent processing.\n");
}
}
}
H5LTread_dataset_double(hdf, "/entry/instrument/detector/sensor_thickness", &thickness); // in um
if (thickness > 0) {
if (thickness < .001) {
fprintf(stderr, " /entry/instrument/detector/sensor_thickness = %f (mm)\n", thickness * 1E3);
} else if (thickness < 1.) {
fprintf(stderr, " /entry/instrument/detector/sensor_thickness = %f (mm)\n", thickness);
thickness *= 1.e-3;
} else {
fprintf(stderr, " /entry/instrument/detector/sensor_thickness = %f (mm)\n", thickness *1.E-3);
}
} else {
thickness = 450E-6;
fprintf(stderr, " /entry/instrument/detector/sensor_thickness is not avaialble. We assume it is %f mm\n", thickness * 1E3);
}
xpixels = -1;
ypixels = -1;
H5LTread_dataset_int(hdf, "/entry/instrument/detector/collection/frame_width", &xpixels);
H5LTread_dataset_int(hdf, "/entry/instrument/detector/collection/frame_height", &ypixels);
if ( xpixels < 1 || ypixels < 1 ) {
H5LTread_dataset_int(hdf, "/entry/instrument/detector/detectorSpecific/x_pixels_in_detector", &xpixels);
H5LTread_dataset_int(hdf, "/entry/instrument/detector/detectorSpecific/y_pixels_in_detector", &ypixels);
fprintf(stderr, " /entry/instrument/detector/detectorSpecific/{x,y}_pixels_in_detector = (%d, %d) (px)\n",
xpixels, ypixels);
} else {
fprintf(stderr, " /entry/instrument/detector/collection/frame{width,height} = (%d, %d) (px)\n",
xpixels, ypixels);
}
if ( xpixels >= 248 && xpixels <= 264
&& ypixels >= 248 && ypixels <= 264) description = "LAMBDA-60K";
if ( xpixels >= 504 && xpixels <= 520
&& ypixels <= 504 && ypixels <= 520 ) description = "LAMBDA-250K";
if ( xpixels >= 504 && xpixels <= 520
&& ypixels >= 756 && ypixels <= 772 ) description = "LAMBDA-350KP";
if ( ypixels >= 504 && ypixels <= 520
&& xpixels >= 756 && xpixels <= 772 ) description = "LAMBDA-350KL";
if ( xpixels == 512
&& ( ypixels == 1536 || ypixels == 1528 ) ) description = "LAMBDA-750K";
if ( xpixels == 1536
&& ( ypixels == 1536 || ypixels == 1528 ) ) description = "LAMBDA-2M";
H5LTread_dataset_double(hdf, "/entry/instrument/detector/beam_center_x", &beamx);
H5LTread_dataset_double(hdf, "/entry/instrument/detector/beam_center_y", &beamy);
fprintf(stderr, " /entry/instrument/detector/beam_center_{x,y} = (%.2f, %.2f) (px)\n", new_beam_cent?nbeamx:beamx, new_beam_cent?nbeamy:beamy);
H5LTread_dataset_double(hdf, "/entry/instrument/detector/count_time", &count_time); // in m
fprintf(stderr, " /entry/instrument/detector/count_time = %f (sec)\n", count_time);
H5LTread_dataset_double(hdf, "/entry/instrument/detector/frame_time", &frame_time); // in
fprintf(stderr, " /entry/instrument/detector/frame_time = %f (sec)\n", frame_time);
if (H5LTread_dataset_double(hdf, "/entry/instrument/detector/x_pixel_size", &pixelsize)> 0)
fprintf(stderr, " /entry/instrument/detector/x_pixel_size = %f (um)\n", pixelsize);
// Detector distance
H5LTread_dataset_double(hdf, "/entry/instrument/detector/distance", &distance);
if (new_distance > 0.) distance = new_distance;
if (distance > 0.) {
fprintf(stderr, " /entry/instrument/detector/distance = %f (mm)\n", distance);
} else {
fprintf(stderr, " /entry/instrument/detector/distance not present. Trying another place.\n");
H5LTread_dataset_double(hdf, "/entry/instrument/detector/detector_distance", &distance); // Firmware< 1.7
if (distance > 0) {
fprintf(stderr, " /entry/instrument/detector/detector_distance = %f (m)\n", distance);
} else {
fprintf(stderr, " /entry/instrument/detector/detector_distance not present.\n");
fprintf(stderr, " WARNING: detector distance was not defined! \"Detector distance\" field in the output is set to -1.\n");
}
}
// Wavelength
H5LTread_dataset_double(hdf, "/entry/sample/beam/incident_wavelength", &wavelength); // Firmware >= 1.7
if (new_wavelength > 0.) {
wavelength = new_wavelength;
}
if (wavelength > 0.) {
fprintf(stderr, " /entry/sample/beam/incident_wavelength = %f (A)\n", wavelength);
} else {
fprintf(stderr, " /entry/sample/beam/incident_wavelength not present. Trying another place.\n");
H5LTread_dataset_double(hdf, "/entry/instrument/beam/wavelength", &wavelength);
if (wavelength > 0.) {
fprintf(stderr, " /entry/instrument/beam/wavelength = %f (A)\n", wavelength);
} else {
fprintf(stderr, " /entry/instrument/beam/wavelength not present. Trying another place.\n");
H5LTread_dataset_double(hdf, "/entry/instrument/monochromator/wavelength", &wavelength);
if (wavelength > 0.) {
fprintf(stderr, " /entry/instrument/monochromator/wavelength = %f (A)\n", wavelength);
} else {
fprintf(stderr, " /entry/instrument/monochromator/wavelength not present. Trying another place.\n");
H5LTread_dataset_double(hdf, "/entry/instrument/beam/incident_wavelength", &wavelength); // Firmware 1.6
if (wavelength > 0.) {
fprintf(stderr, " /entry/instrument/beam/incident_wavelength = %f (A)\n", wavelength);
} else {
fprintf(stderr, " /entry/instrument/beam/incident_wavelength not present.\n");
}
}
}
}
if (wavelength < 0.) {
fprintf(stderr, " WARNING: wavelength was not defined! \"Wavelength\" field in the output is set to -1.\n");
}
if (H5LTread_dataset_double(hdf, "/entry/sample/goniometer/omega_range_average", &osc_width)< 0) osc_width = -1.;
if (new_osc_width > 0.) osc_width=new_osc_width;
if (osc_width > 0.) {
fprintf(stderr, " /entry/sample/goniometer/omega_range_average = %f (deg)\n", osc_width);
} else {
fprintf(stderr, " WARNING: oscillation width was not defined. \"Start_angle\" field in the output is set to 0!\n");
osc_width = 0.;
}
unsigned int *buf = (unsigned int*)malloc(sizeof(unsigned int) * xpixels * ypixels);
signed int *buf_signed = (signed int*)malloc(sizeof(signed int) * xpixels * ypixels);
signed int *pixel_mask = (signed int*)malloc(sizeof(signed int) * xpixels * ypixels);
if (buf == NULL || buf_signed == NULL || pixel_mask == NULL) {
fprintf(stderr, "Failed to allocate image buffer.\n");
return -1;
}
// TODO: Is it always in omega?
int bufsize = (nimages < 100000) ? 100000 : nimages;
double *angles = (double*)malloc(bufsize * sizeof(double));
// I don't know why but nimages can be too small ...
if (angles == NULL) {
fprintf(stderr, "failed to allocate buffer for omega.\n");
return -1;
}
for (ii=0; ii < bufsize; ii++) angles[ii]=-9999.;
H5LTread_dataset_double(hdf, "/entry/sample/goniometer/omega", angles);
if (new_osc_start > -9999.) osc_start = angles[0] = new_osc_start;
fprintf(stderr, "\n");
hid_t entry, group;
entry = H5Gopen2(hdf, "/entry", H5P_DEFAULT);
if (entry < 0) {
fprintf(stderr, "/entry does not exist!\n");
return -1;
}
pixel_mask[0] = -9999;
H5LTread_dataset_int(hdf, "/entry/instrument/detector/detectorSpecific/pixel_mask", pixel_mask);
if (pixel_mask[0] == -9999) {
fprintf(stderr, "WARNING: failed to read the pixel mask from /entry/instrument/detector/detectorSpecific/pixel_mask.\n");
fprintf(stderr, " Thus, we mask pixels whose intensity is %u (= (2 ^ bit_depth_image) - 1) by converting them to -1. \n", error_val);
fprintf(stderr, " However, this might mask overloaded (saturated) pixels as well.\n");
}
// Check if /entry/data present
group = H5Gopen2(entry, "data", H5P_DEFAULT);
if (group < 0) {
group = entry; // leak!
}
int block_start = 1;
if (H5LTfind_dataset(group, "data")) {
fprintf(stderr, "This dataset starts from data.\n");
block_start = -1;
} else if (H5LTfind_dataset(group, "data_000000")) {
fprintf(stderr, "This dataset starts from data_000000.\n");
block_start = 0;
} else {
fprintf(stderr, "This dataset starts from data_000001.\n");
block_start = 1;
}
char data_name[20] = {};
hid_t data, dataspace;
int number_per_block = 0;
// Open the first data block to get the number of frames in a block
if (block_start > -1 ) {
snprintf(data_name, 20, "data_%06d", block_start);
} else {
snprintf(data_name, 20, "data");
}
data = H5Dopen2(group, data_name, H5P_DEFAULT);
dataspace = H5Dget_space(data);
if (data < 0) {
fprintf(stderr, "failed to open /entry/%s\n", data_name);
return -1;
}
if (H5Sget_simple_extent_ndims(dataspace) != 3) {
fprintf(stderr, "Dimension of /entry/%s is not 3!\n", data_name);
return -1;
}
hsize_t dims[3];
H5Sget_simple_extent_dims(dataspace, dims, NULL);
number_per_block = dims[0];
fprintf(stderr, "The number of images per data block is %d.\n", number_per_block);
H5Sclose(dataspace);
H5Dclose(data);
fprintf(stderr, "\nFile analysis completed.\n\n");
int frame;
for (frame = from; frame <= to; frame++) {
fprintf(stderr, "Converting frame %d (%d / %d)\n", frame, frame - from + 1, to - from + 1);
if (angles[frame-1] != -9999.) {
osc_start = angles[frame - 1];
fprintf(stderr, " /entry/sample/goniometer/omega[%d] = %.3f (1-indexed)\n", frame, osc_start);
} else {
if (new_osc_start != -9999.) {
osc_start = new_osc_start+osc_width * frame;
} else {
fprintf(stderr, " oscillation start not defined. \"Start_angle\" field in the output is set to 0!\n");
osc_start = osc_width * frame;
}
}
if (frame > nimages) {
fprintf(stderr, "WARNING: invalid frame number specified. %d is bigger than nimages (%d)\n", frame, nimages);
// Due to a firmware bug, nimages can be smaller than the actual value.
// So we don't exit here
}
char header_format[] =
"\n"
"# Detector: %s, S/N %s\n"
"# Pixel_size %.6f m x %.6f m\n"
"# Silicon sensor, thickness %.6f m\n"
"# Exposure_time %f ms\n"
"# Exposure_period %f ms\n"
"# Count_cutoff %d counts\n"
"# Wavelength %f A\n"
"# Detector_distance %f m\n"
"# Beam_xy (%.2f, %.2f) pixels\n"
"# Start_angle %f deg.\n"
"# Angle_increment %f deg.\n";
char header_content[4096] = {};
if (frame_time < count_time) frame_time = count_time;
snprintf(header_content, 4096, header_format,
description, detector_sn,
pixelsize*1.e-6, pixelsize*1.e-6,
thickness*1.e-6,
count_time, frame_time, countrate_cutoff, wavelength, distance,
new_beam_cent?nbeamx:beamx, new_beam_cent?nbeamy:beamy, osc_start, osc_width);
// Now open the required data
int block_number;
int frame_in_block;
if (block_start > -1 ) {
block_number = block_start + (frame - 1) / number_per_block;
frame_in_block = (frame - 1) % number_per_block;
snprintf(data_name, 20, "data_%06d", block_number);
} else {
block_number = 0;
frame_in_block = frame -1;
snprintf(data_name, 20, "data");
}
data = H5Dopen2(group, data_name, H5P_DEFAULT);
dataspace = H5Dget_space(data);
if (data < 0) {
fprintf(stderr, "failed to open /entry/%s\n", data_name);
return -1;
}
if (H5Sget_simple_extent_ndims(dataspace) != 3) {
fprintf(stderr, "Dimension of /entry/%s is not 3!\n", data_name);
return -1;
}
// Get the frame
H5Sget_simple_extent_dims(dataspace, dims, NULL);
hsize_t offset_in[3] = {frame_in_block, 0, 0};
hsize_t offset_out[3] = {0, 0, 0};
hsize_t count[3] = {1, ypixels, xpixels};
hid_t memspace = H5Screate_simple(3, dims, NULL);
if (memspace < 0) {
fprintf(stderr, "failed to create memspace\n");
return -1;
}
ret = H5Sselect_hyperslab(dataspace, H5S_SELECT_SET, offset_in, NULL,
count, NULL);
if (ret < 0) {
fprintf(stderr, "select_hyperslab for file failed\n");
return -1;
}
ret = H5Sselect_hyperslab(memspace, H5S_SELECT_SET, offset_out, NULL,
count, NULL);
if (ret < 0) {
fprintf(stderr, "select_hyperslab for memory failed\n");
return -1;
}
ret = H5Dread(data, H5T_NATIVE_UINT, memspace, dataspace, H5P_DEFAULT, buf);
if (ret < 0) {
fprintf(stderr, "H5Dread for image failed. Wrong frame number?\n");
return -1;
}
H5Sclose(dataspace);
H5Sclose(memspace);
H5Dclose(data);
/////////////////////////////////////////////////////////////////
// Reading done. Here output starts...
FILE *fh = stdout;
if (argc-optcount > 3) {
if (from == to && retfromto !=2 ) {
fh = fopen(argv[3+optcount], "wb");
} else {
char filename[4096];
snprintf(filename, 4096, "%s%06d.cbf", argv[3+optcount], frame);
fh = fopen(filename, "wb");
}
}
// create a CBF
cbf_make_handle(&cbf);
cbf_new_datablock(cbf, "image_1");
// put a miniCBF header
cbf_new_category(cbf, "array_data");
cbf_new_column(cbf, "header_convention");
cbf_set_value(cbf, "SLS_1.0");
cbf_new_column(cbf, "header_contents");
cbf_set_value(cbf, header_content);
// put the image
cbf_new_category(cbf, "array_data");
cbf_new_column(cbf, "data");
int i;
for (i = 0; i < xpixels * ypixels; i++) {
if ((pixel_mask[0] != -9999 && pixel_mask[i] == 1) || // the pixel mask is available
(pixel_mask[0] == -9999 && buf[i] == error_val)) { // not available
buf_signed[i] = -1;
} else if (pixel_mask[0] != -9999 && pixel_mask[i] > 1) { // the pixel mask is 2, 4, 8, 16
buf_signed[i] = -2;
} else {
buf_signed[i] = buf[i];
}
}
cbf_set_integerarray_wdims_fs(cbf,
CBF_BYTE_OFFSET,
1, // binary id
buf_signed,
sizeof(int),
1, // signed?
xpixels * ypixels,
"little_endian",
xpixels,
ypixels,
0,
0); //padding
cbf_write_file(cbf, fh, 1, CBF, MSG_DIGEST | MIME_HEADERS | PAD_4K, 0);
// no need to fclose() here as the 3rd argument "readable" is 1
cbf_free_handle(cbf);
}
H5Gclose(group);
H5Fclose(hdf);
free(buf);
free(buf_signed);
free(angles);
fprintf(stderr, "\nAll done!\n");
return 0;
}