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
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
|
/*
* image-hdf5.c
*
* Image loading, HDF5 parts
*
* Copyright © 2012-2020 Deutsches Elektronen-Synchrotron DESY,
* a research centre of the Helmholtz Association.
*
* Authors:
* 2020 Thomas White <taw@physics.org>
* 2014-2018 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 <config.h>
#include <stdlib.h>
#include <assert.h>
#include <math.h>
#include <stdio.h>
#include <hdf5.h>
#include <unistd.h>
#include "image.h"
#include "utils.h"
#include "detgeom.h"
#include "datatemplate.h"
#include "datatemplate_priv.h"
/* Get the path parts of the event ID
* e.g. ev_orig = abc/def/ghi//5/2/7
* -> [abc, def, ghi], with *pn_plvals=3 */
static char **read_path_parts(const char *ev_orig, int *pn_plvals)
{
char **plvals;
char *ev;
int n_plvals = 0;
char *start;
plvals = malloc(MAX_PATH_PARTS*sizeof(char *));
if ( plvals == NULL ) return NULL;
if ( ev_orig == NULL ) {
/* No ev -> no path parts */
*pn_plvals = 0;
return plvals;
}
ev = strdup(ev_orig);
if ( ev == NULL ) return NULL;
start = ev;
do {
char *sep;
sep = strchr(start, '/');
if ( sep == NULL ) {
/* This would be very strange, because it
* must at least have // */
ERROR("Couldn't read path parts ('%s')\n",
start);
free(ev);
free(plvals);
return NULL;
}
/* Remaining string starts with '/' is end condition */
if ( sep == start ) break;
if ( n_plvals == MAX_PATH_PARTS ) {
ERROR("Too many path parts: %s\n", ev_orig);
free(ev);
free(plvals);
return NULL;
}
sep[0] = '\0';
plvals[n_plvals++] = strdup(start);
start = sep+1;
} while ( 1 );
free(ev);
*pn_plvals = n_plvals;
return plvals;
}
/* Get the dimension parts of the event ID
* e.g. ev_orig = abc/def/ghi//5/2/7
* -> [5, 2, 7], with *pn_dvals=3 */
static int *read_dim_parts(const char *ev_orig, int *pn_dvals)
{
char *ev;
int n_dvals = 0;
int *dvals;
char *start;
int done;
/* Valid event ID? (Just the part after //, please) */
ev = strstr(ev_orig, "//");
if ( ev == NULL ) return NULL;
dvals = malloc(MAX_DIMS*sizeof(int));
if ( dvals == NULL ) return NULL;
if ( ev[2] == '\0' ) {
/* No dimension parts - early bailout */
*pn_dvals = 0;
return dvals; /* NB Not NULL */
}
ev = strdup(ev+2);
if ( ev == NULL ) return NULL;
start = ev;
done = 0;
do {
char *sep = strchr(start, '/');
if ( sep != NULL ) {
sep[0] = '\0';
} else {
done = 1;
}
if ( n_dvals == MAX_PATH_PARTS ) {
ERROR("Too many path parts: %s\n", ev_orig);
free(ev);
free(dvals);
return NULL;
}
if ( start[0] == '\0' ) {
ERROR("Missing dimension: %s\n", ev_orig);
free(ev);
free(dvals);
return NULL;
}
dvals[n_dvals++] = atoi(start);
start = sep+1;
} while ( !done );
free(ev);
*pn_dvals = n_dvals;
return dvals;
}
/* ev = abc/def/ghi//5/2/7
* pattern = /data/%/somewhere/%/%/data
* output = /data/abc/somewhere/def/ghi/data
*/
static char *substitute_path(const char *ev, const char *pattern)
{
char **plvals;
int n_plvals;
int n_pl_exp;
size_t l, total_len;
int i;
char *subs;
const char *start;
const char *pl_pos;
if ( pattern == NULL ) {
ERROR("Pattern cannot be NULL\n");
return NULL;
}
plvals = read_path_parts(ev, &n_plvals);
if ( plvals == NULL ) return NULL;
l = strlen(pattern);
n_pl_exp = 0;
for ( i=0; i<l; i++ ) {
if ( pattern[i] == '%' ) n_pl_exp++;
}
if ( n_plvals != n_pl_exp ) {
ERROR("Wrong number of path placeholders: "
"'%s' into '%s'\n", ev, pattern);
return NULL;
}
if ( n_pl_exp == 0 ) {
/* No placeholders in path */
for ( i=0; i<n_plvals; i++ ) {
free(plvals[i]);
}
free(plvals);
return strdup(pattern);
}
total_len = strlen(pattern) - n_pl_exp;
for ( i=0; i<n_plvals; i++ ) {
total_len += strlen(plvals[i]);
}
subs = malloc(total_len+1);
if ( subs == NULL ) {
free(plvals);
return NULL;
}
subs[0] = '\0';
pl_pos = strchr(pattern, '%');
if ( pl_pos == NULL ) {
ERROR("Expected a placeholder char (%): '%s'\n",
pattern);
return NULL;
}
strncpy(subs, pattern, pl_pos-pattern);
start = pl_pos+1;
for ( i=0; i<n_plvals; i++ ) {
/* Add the placeholder's value */
strcat(subs, plvals[i]);
free(plvals[i]);
/* Add the chars up to the next placeholder... */
pl_pos = strchr(start, '%');
if ( pl_pos == NULL ) {
/* ... or the end */
pl_pos = start+strlen(start);
}
strncat(subs, start, pl_pos-start);
start = pl_pos+1;
}
free(plvals);
return subs;
}
static void make_placeholder_skip(signed int *dt_dims,
signed int *panel_dims)
{
int i;
int n_dt = 0;
for ( i=0; i<MAX_DIMS; i++ ) {
if ( panel_dims[i] != DIM_PLACEHOLDER ) {
dt_dims[n_dt++] = panel_dims[i];
}
}
}
static int num_placeholders(const struct panel_template *p)
{
int i;
int n_pl = 0;
for ( i=0; i<MAX_DIMS; i++ ) {
if ( p->dims[i] == DIM_PLACEHOLDER ) n_pl++;
}
return n_pl;
}
static int total_dimensions(const struct panel_template *p)
{
int i;
int n_dim = 0;
for ( i=0; i<MAX_DIMS; i++ ) {
if ( p->dims[i] != DIM_UNDEFINED ) n_dim++;
}
return n_dim;
}
static int load_hdf5_hyperslab(struct panel_template *p,
const char *filename,
const char *event,
void **pdata,
hid_t el_type, size_t el_size,
int skip_placeholders_ok,
const char *path_spec)
{
hid_t fh;
int total_dt_dims;
int plh_dt_dims;
int dt_dims[MAX_DIMS];
int n_dt_dims;
herr_t r;
hsize_t *f_offset, *f_count;
hid_t dh;
herr_t check;
hid_t dataspace, memspace;
hsize_t dims[2];
char *panel_full_path;
void *data;
int ndims;
int dim;
int *dim_vals;
int n_dim_vals;
int pl_pos;
if ( access(filename, R_OK) == -1 ) {
ERROR("File does not exist or cannot be read: %s\n",
filename);
return 1;
}
fh = H5Fopen(filename, H5F_ACC_RDONLY, H5P_DEFAULT);
if ( fh < 0 ) {
ERROR("Couldn't open file: %s\n", filename);
return 1;
}
panel_full_path = substitute_path(event, path_spec);
if ( panel_full_path == NULL ) {
ERROR("Invalid path substitution: '%s' '%s'\n",
event, path_spec);
return 1;
}
dh = H5Dopen2(fh, panel_full_path, H5P_DEFAULT);
if ( dh < 0 ) {
ERROR("Cannot open data for panel %s (%s)\n",
p->name, panel_full_path);
free(panel_full_path);
H5Fclose(fh);
return 1;
}
free(panel_full_path);
/* Set up dataspace for file
* (determine where to read the data from) */
dataspace = H5Dget_space(dh);
ndims = H5Sget_simple_extent_ndims(dataspace);
if ( ndims < 0 ) {
ERROR("Failed to get number of dimensions for panel %s\n",
p->name);
H5Fclose(fh);
return 1;
}
/* Does the array have the expected number of dimensions? */
total_dt_dims = total_dimensions(p);
plh_dt_dims = num_placeholders(p);
if ( ndims != total_dt_dims ) {
/* If the dimensions match after excluding
* placeholders, it's OK - probably a static mask
* in a multi-event file. */
if ( skip_placeholders_ok
&& (ndims == total_dt_dims - plh_dt_dims) )
{
make_placeholder_skip(dt_dims, p->dims);
n_dt_dims = total_dt_dims - plh_dt_dims;
} else {
ERROR("Unexpected number of dimensions for "
"panel %s (%i, but expected %i or %i)\n",
p->name, ndims, total_dt_dims,
total_dt_dims - plh_dt_dims);
H5Fclose(fh);
return 1;
}
} else {
int i;
for ( i=0; i<MAX_DIMS; i++ ) {
dt_dims[i] = p->dims[i];
}
n_dt_dims = total_dt_dims;
}
f_offset = malloc(ndims*sizeof(hsize_t));
f_count = malloc(ndims*sizeof(hsize_t));
if ( (f_offset == NULL) || (f_count == NULL ) ) {
ERROR("Failed to allocate offset or count.\n");
H5Fclose(fh);
return 1;
}
/* Get those placeholder values from the event ID */
dim_vals = read_dim_parts(event, &n_dim_vals);
pl_pos = 0;
for ( dim=0; dim<n_dt_dims; dim++ ) {
switch ( dt_dims[dim] ) {
case DIM_FS:
f_offset[dim] = p->orig_min_fs;
f_count[dim] = p->orig_max_fs - p->orig_min_fs+1;
break;
case DIM_SS:
f_offset[dim] = p->orig_min_ss;
f_count[dim] = p->orig_max_ss - p->orig_min_ss+1;
break;
case DIM_PLACEHOLDER:
f_offset[dim] = dim_vals[pl_pos++];
f_count[dim] = 1;
break;
case DIM_UNDEFINED:
ERROR("Undefined dimension found!\n");
break;
default:
/* Fixed value */
f_offset[dim] = dt_dims[dim];
f_count[dim] = 1;
break;
}
}
free(dim_vals);
check = H5Sselect_hyperslab(dataspace, H5S_SELECT_SET,
f_offset, NULL, f_count, NULL);
if ( check < 0 ) {
ERROR("Error selecting file dataspace for panel %s\n",
p->name);
free(f_offset);
free(f_count);
H5Fclose(fh);
return 1;
}
dims[0] = p->orig_max_ss - p->orig_min_ss + 1;
dims[1] = p->orig_max_fs - p->orig_min_fs + 1;
memspace = H5Screate_simple(2, dims, NULL);
data = malloc(dims[0]*dims[1]*el_size);
if ( data == NULL ) {
ERROR("Failed to allocate panel %s\n", p->name);
free(f_offset);
free(f_count);
H5Fclose(fh);
return 1;
}
r = H5Dread(dh, el_type, memspace, dataspace,
H5P_DEFAULT, data);
if ( r < 0 ) {
ERROR("Couldn't read data for panel %s\n",
p->name);
free(f_offset);
free(f_count);
free(data);
H5Fclose(fh);
return 1;
}
H5Dclose(dh);
H5Sclose(dataspace);
free(f_offset);
free(f_count);
H5Fclose(fh);
*pdata = data;
return 0;
}
struct image *image_hdf5_read(DataTemplate *dtempl,
const char *filename, const char *event)
{
struct image *image;
int i;
image = image_new();
if ( image == NULL ) {
ERROR("Couldn't allocate image structure.\n");
return NULL;
}
image->dp = malloc(dtempl->n_panels*sizeof(float *));
if ( image->dp == NULL ) {
ERROR("Failed to allocate data array.\n");
image_free(image);
return NULL;
}
/* Set all pointers to NULL for easier clean-up */
for ( i=0; i<dtempl->n_panels; i++ ) image->dp[i] = NULL;
for ( i=0; i<dtempl->n_panels; i++ ) {
if ( load_hdf5_hyperslab(&dtempl->panels[i], filename,
event, (void *)&image->dp[i],
H5T_NATIVE_FLOAT,
sizeof(float), 0,
dtempl->panels[i].data) )
{
ERROR("Failed to load panel data\n");
image_free(image);
return NULL;
}
}
image->filename = strdup(filename);
image->ev = safe_strdup(event);
return image;
}
int image_hdf5_read_mask(struct panel_template *p,
const char *filename, const char *event,
int *bad, int mask_good, int mask_bad)
{
int p_w, p_h;
int *mask = NULL;
long unsigned int j;
p_w = p->orig_max_fs - p->orig_min_fs + 1;
p_h = p->orig_max_ss - p->orig_min_ss + 1;
if ( load_hdf5_hyperslab(p, filename, event,
(void *)&mask, H5T_NATIVE_INT,
sizeof(int), 1, p->mask) )
{
ERROR("Failed to load mask data\n");
free(mask);
return 1;
}
for ( j=0; j<p_w*p_h; j++ ) {
/* Bad if it's missing any of the "good" bits */
if ( (mask[j] & mask_good) != mask_good ) bad[j] = 1;
/* Bad if it has any of the "bad" bits. */
if ( mask[j] & mask_bad ) bad[j] = 1;
}
free(mask);
return 0;
}
double image_hdf5_get_value(const char *name, const char *filename,
const char *event)
{
hid_t dh;
hid_t type;
hid_t class;
hid_t sh;
hid_t ms;
hsize_t *f_offset = NULL;
hsize_t *f_count = NULL;
hsize_t m_offset[1];
hsize_t m_count[1];
hsize_t msdims[1];
hsize_t size[64];
herr_t r;
herr_t check;
int ndims;
int i;
char *subst_name = NULL;
hid_t fh;
double val;
int *dim_vals;
int n_dim_vals;
int dim_val_pos;
if ( access(filename, R_OK) == -1 ) {
ERROR("File does not exist or cannot be read: %s\n", filename);
return NAN;
}
fh = H5Fopen(filename, H5F_ACC_RDONLY, H5P_DEFAULT);
if ( fh < 0 ) {
ERROR("Couldn't open file: %s\n", filename);
return NAN;
}
subst_name = substitute_path(event, name);
if ( subst_name == NULL ) {
ERROR("Invalid event ID '%s'\n", event);
H5Fclose(fh);
return NAN;
}
dh = H5Dopen2(fh, subst_name, H5P_DEFAULT);
if ( dh < 0 ) {
ERROR("No such numeric field '%s'\n", subst_name);
H5Fclose(fh);
return NAN;
}
type = H5Dget_type(dh);
class = H5Tget_class(type);
if ( (class != H5T_FLOAT) && (class != H5T_INTEGER) ) {
ERROR("Not a floating point or integer value.\n");
H5Tclose(type);
H5Dclose(dh);
return NAN;
}
/* Get the dimensionality. We have to cope with scalars expressed as
* arrays with all dimensions 1, as well as zero-d arrays. */
sh = H5Dget_space(dh);
ndims = H5Sget_simple_extent_ndims(sh);
if ( ndims > 64 ) {
ERROR("Too many dimensions for numeric value\n");
H5Tclose(type);
H5Dclose(dh);
return NAN;
}
H5Sget_simple_extent_dims(sh, size, NULL);
/* We expect a scalar value */
m_offset[0] = 0;
m_count[0] = 1;
msdims[0] = 1;
ms = H5Screate_simple(1, msdims, NULL);
dim_vals = read_dim_parts(event, &n_dim_vals);
if ( dim_vals == NULL ) {
ERROR("Couldn't parse event '%s'\n");
H5Tclose(type);
H5Dclose(dh);
return NAN;
}
f_offset = malloc(ndims*sizeof(hsize_t));
f_count = malloc(ndims*sizeof(hsize_t));
if ( (f_offset == NULL) || (f_count == NULL) ) {
ERROR("Couldn't allocate dimension arrays\n");
H5Tclose(type);
H5Dclose(dh);
return NAN;
}
/* Every dimension of the dataset must either be size 1 or
* large enough to contain the next value from the event ID */
dim_val_pos = 0;
for ( i=0; i<ndims; i++ ) {
if ( size[i] != 1 ) {
if ( size[i] <= dim_vals[dim_val_pos] ) {
ERROR("Array of scalar values is too "
"small (%s, dim %i, ev value %i,"
" size %i)\n",
subst_name, i,
dim_vals[dim_val_pos], size[i]);
return NAN;
}
f_offset[i] = dim_vals[dim_val_pos];
f_count[i] = 1;
dim_val_pos++;
} else {
f_offset[i] = 0;
f_count[i] = 1;
}
}
check = H5Sselect_hyperslab(sh, H5S_SELECT_SET,
f_offset, NULL, f_count, NULL);
if ( check <0 ) {
ERROR("Error selecting dataspace for float value");
free(f_offset);
free(f_count);
return NAN;
}
ms = H5Screate_simple(1,msdims,NULL);
check = H5Sselect_hyperslab(ms, H5S_SELECT_SET,
m_offset, NULL, m_count, NULL);
if ( check < 0 ) {
ERROR("Error selecting memory dataspace for float value");
free(f_offset);
free(f_count);
return NAN;
}
r = H5Dread(dh, H5T_NATIVE_DOUBLE, ms, sh, H5P_DEFAULT, &val);
if ( r < 0 ) {
ERROR("Couldn't read value.\n");
H5Tclose(type);
H5Dclose(dh);
return NAN;
}
free(f_offset);
free(f_count);
H5Tclose(type);
free(subst_name);
H5Fclose(fh);
return val;
}
static int read_peak_count(hid_t fh, char *path, int line,
int *num_peaks)
{
hid_t dh, sh, mh;
hsize_t size[1];
hsize_t max_size[1];
hsize_t offset[1], count[1];
hsize_t m_offset[1], m_count[1], dimmh[1];
int tw, r;
dh = H5Dopen2(fh, path, H5P_DEFAULT);
if ( dh < 0 ) {
ERROR("Data block %s not found.\n", path);
return 1;
}
sh = H5Dget_space(dh);
if ( sh < 0 ) {
H5Dclose(dh);
ERROR("Couldn't get dataspace for data.\n");
return 1;
}
if ( H5Sget_simple_extent_ndims(sh) != 1 ) {
ERROR("Data block %s has the wrong dimensionality (%i).\n",
path, H5Sget_simple_extent_ndims(sh));
H5Sclose(sh);
H5Dclose(dh);
return 1;
}
H5Sget_simple_extent_dims(sh, size, max_size);
tw = size[0];
if ( line > tw-1 ) {
H5Sclose(sh);
H5Dclose(dh);
ERROR("Data block %s does not contain data for required event.\n",
path);
return 1;
}
offset[0] = line;
count[0] = 1;
r = H5Sselect_hyperslab(sh, H5S_SELECT_SET,
offset, NULL, count, NULL);
if ( r < 0 ) {
ERROR("Error selecting file dataspace "
"for data block %s\n", path);
H5Dclose(dh);
H5Sclose(sh);
return 1;
}
m_offset[0] = 0;
m_count[0] = 1;
dimmh[0] = 1;
mh = H5Screate_simple(1, dimmh, NULL);
r = H5Sselect_hyperslab(mh, H5S_SELECT_SET,
m_offset, NULL, m_count, NULL);
if ( r < 0 ) {
ERROR("Error selecting memory dataspace "
"for data block %s\n", path);
H5Dclose(dh);
H5Sclose(sh);
H5Sclose(mh);
return 1;
}
r = H5Dread(dh, H5T_NATIVE_INT, mh,
sh, H5P_DEFAULT, num_peaks);
if ( r < 0 ) {
ERROR("Couldn't read data for block %s, line %i\n", path, line);
H5Dclose(dh);
H5Sclose(sh);
H5Sclose(mh);
return 1;
}
H5Dclose(dh);
H5Sclose(sh);
H5Sclose(mh);
return 0;
}
static float *read_peak_line(hid_t fh, char *path, int line)
{
hid_t dh, sh, mh;
hsize_t size[2];
hsize_t max_size[2];
hsize_t offset[2], count[2];
hsize_t m_offset[2], m_count[2], dimmh[2];
float *buf;
int tw, r;
dh = H5Dopen2(fh, path, H5P_DEFAULT);
if ( dh < 0 ) {
ERROR("Data block (%s) not found.\n", path);
return NULL;
}
sh = H5Dget_space(dh);
if ( sh < 0 ) {
H5Dclose(dh);
ERROR("Couldn't get dataspace for data.\n");
return NULL;
}
if ( H5Sget_simple_extent_ndims(sh) != 2 ) {
ERROR("Data block %s has the wrong dimensionality (%i).\n",
path, H5Sget_simple_extent_ndims(sh));
H5Sclose(sh);
H5Dclose(dh);
return NULL;
}
H5Sget_simple_extent_dims(sh, size, max_size);
tw = size[0];
if ( line> tw-1 ) {
H5Sclose(sh);
H5Dclose(dh);
ERROR("Data block %s does not contain data for required event.\n",
path);
return NULL;
}
offset[0] = line;
offset[1] = 0;
count[0] = 1;
count[1] = size[1];
r = H5Sselect_hyperslab(sh, H5S_SELECT_SET, offset, NULL, count, NULL);
if ( r < 0 ) {
ERROR("Error selecting file dataspace "
"for data block %s\n", path);
H5Dclose(dh);
H5Sclose(sh);
return NULL;
}
m_offset[0] = 0;
m_offset[1] = 0;
m_count[0] = 1;
m_count[1] = size[1];
dimmh[0] = 1;
dimmh[1] = size[1];
mh = H5Screate_simple(2, dimmh, NULL);
r = H5Sselect_hyperslab(mh, H5S_SELECT_SET,
m_offset, NULL, m_count, NULL);
if ( r < 0 ) {
ERROR("Error selecting memory dataspace "
"for data block %s\n", path);
H5Dclose(dh);
H5Sclose(sh);
H5Sclose(mh);
return NULL;
}
buf = malloc(size[1]*sizeof(float));
if ( buf == NULL ) return NULL;
r = H5Dread(dh, H5T_NATIVE_FLOAT, mh, sh, H5P_DEFAULT, buf);
if ( r < 0 ) {
ERROR("Couldn't read data for block %s, line %i\n", path, line);
H5Dclose(dh);
H5Sclose(sh);
H5Sclose(mh);
return NULL;
}
H5Dclose(dh);
H5Sclose(sh);
H5Sclose(mh);
return buf;
}
ImageFeatureList *image_hdf5_read_peaks_cxi(const DataTemplate *dtempl,
const char *filename,
const char *event,
int half_pixel_shift)
{
ImageFeatureList *features;
hid_t fh;
char path_n[1024];
char path_x[1024];
char path_y[1024];
char path_i[1024];
int r;
int pk;
char *subst_name;
int line;
int num_peaks;
float *buf_x;
float *buf_y;
float *buf_i;
int *dim_vals;
int n_dim_vals;
double peak_offset = half_pixel_shift ? 0.5 : 0.0;
if ( access(filename, R_OK) == -1 ) {
ERROR("File does not exist or cannot be read: %s\n",
filename);
return NULL;
}
subst_name = substitute_path(event, dtempl->peak_list);
if ( subst_name == NULL ) {
ERROR("Invalid peak path %s\n", subst_name);
return NULL;
}
dim_vals = read_dim_parts(event, &n_dim_vals);
if ( dim_vals == NULL ) {
ERROR("Couldn't parse event '%s'\n");
return NULL;
}
if ( n_dim_vals < 1 ) {
ERROR("Not enough dimensions in event ID to use CXI "
"peak lists (%i)\n", n_dim_vals);
return NULL;
}
line = dim_vals[0];
free(dim_vals);
snprintf(path_n, 1024, "%s/nPeaks", subst_name);
snprintf(path_x, 1024, "%s/peakXPosRaw", subst_name);
snprintf(path_y, 1024, "%s/peakYPosRaw", subst_name);
snprintf(path_i, 1024, "%s/peakTotalIntensity", subst_name);
fh = H5Fopen(filename, H5F_ACC_RDONLY, H5P_DEFAULT);
if ( fh < 0 ) {
ERROR("Couldn't open file: %s\n", filename);
return NULL;
}
r = read_peak_count(fh, path_n, line, &num_peaks);
if ( r != 0 ) return NULL;
buf_x = read_peak_line(fh, path_x, line);
if ( r != 0 ) return NULL;
buf_y = read_peak_line(fh, path_y, line);
if ( r != 0 ) return NULL;
buf_i = read_peak_line(fh, path_i, line);
if ( r != 0 ) return NULL;
features = image_feature_list_new();
for ( pk=0; pk<num_peaks; pk++ ) {
float fs, ss, val;
int pn;
fs = buf_x[pk] + peak_offset;
ss = buf_y[pk] + peak_offset;
val = buf_i[pk];
if ( data_template_file_to_panel_coords(dtempl,
&fs, &ss,
&pn) )
{
ERROR("Failed to convert %i,%i to "
"panel-relative coordinates\n", fs, ss);
} else {
image_add_feature(features, fs, ss, pn,
NULL, val, NULL);
}
}
return features;
}
ImageFeatureList *image_hdf5_read_peaks_hdf5(const DataTemplate *dtempl,
const char *filename,
const char *event,
int half_pixel_shift)
{
hid_t fh, dh, sh;
hsize_t size[2];
hsize_t max_size[2];
int i;
float *buf;
herr_t r;
int tw;
char *subst_name;
ImageFeatureList *features;
double peak_offset = half_pixel_shift ? 0.5 : 0.0;
if ( access(filename, R_OK) == -1 ) {
ERROR("File does not exist or cannot be read: %s\n",
filename);
return NULL;
}
fh = H5Fopen(filename, H5F_ACC_RDONLY, H5P_DEFAULT);
if ( fh < 0 ) {
ERROR("Couldn't open file: %s\n", filename);
return NULL;
}
subst_name = substitute_path(event, dtempl->peak_list);
if ( subst_name == NULL ) {
ERROR("Invalid peak path: '%s' '%s'\n",
event, dtempl->peak_list);
return NULL;
}
dh = H5Dopen2(fh, subst_name, H5P_DEFAULT);
if ( dh < 0 ) {
ERROR("Peak list (%s) not found.\n", subst_name);
free(subst_name);
H5Fclose(fh);
return NULL;
}
free(subst_name);
sh = H5Dget_space(dh);
if ( sh < 0 ) {
ERROR("Couldn't get dataspace for peak list.\n");
H5Dclose(dh);
H5Fclose(fh);
return NULL;
}
if ( H5Sget_simple_extent_ndims(sh) != 2 ) {
ERROR("Peak list has the wrong dimensionality (%i).\n",
H5Sget_simple_extent_ndims(sh));
H5Sclose(sh);
H5Dclose(dh);
H5Fclose(fh);
return NULL;
}
H5Sget_simple_extent_dims(sh, size, max_size);
H5Sclose(sh);
tw = size[1];
if ( (tw != 3) && (tw != 4) ) {
ERROR("Peak list has the wrong dimensions.\n");
H5Dclose(dh);
H5Fclose(fh);
return NULL;
}
buf = malloc(sizeof(float)*size[0]*size[1]);
if ( buf == NULL ) {
ERROR("Couldn't reserve memory for the peak list.\n");
H5Dclose(dh);
H5Fclose(fh);
return NULL;
}
r = H5Dread(dh, H5T_NATIVE_FLOAT, H5S_ALL, H5S_ALL,
H5P_DEFAULT, buf);
if ( r < 0 ) {
ERROR("Couldn't read peak list.\n");
H5Dclose(dh);
H5Fclose(fh);
return NULL;
}
features = image_feature_list_new();
if ( features == NULL ) {
ERROR("Failed to allocate peak list\n");
H5Dclose(dh);
H5Fclose(fh);
return NULL;
}
for ( i=0; i<size[0]; i++ ) {
float fs, ss, val;
int pn;
fs = buf[tw*i+0] + peak_offset;
ss = buf[tw*i+1] + peak_offset;
val = buf[tw*i+2];
if ( data_template_file_to_panel_coords(dtempl,
&fs, &ss,
&pn) )
{
ERROR("Failed to convert %i,%i to "
"panel-relative coordinates\n", fs, ss);
} else {
image_add_feature(features, fs, ss, pn,
NULL, val, NULL);
}
}
free(buf);
H5Dclose(dh);
H5Fclose(fh);
return features;
}
struct event_list *image_hdf5_expand_frames(const DataTemplate *dtempl,
const char *filename)
{
struct event_list *master_el;
hid_t fh;
fh = H5Fopen(filename, H5F_ACC_RDONLY, H5P_DEFAULT);
if ( fh < 0 ) {
ERROR("Couldn't open file '%s'\n", filename);
return NULL;
}
master_el = initialize_event_list();
if ( master_el == NULL ) {
ERROR("Failed to allocate event list.\n");
H5Fclose(fh);
return NULL;
}
/* First expand any placeholders in the HDF5 paths */
if ( dtempl->path_dim != 0 ) {
int pi;
for ( pi=0; pi<dtempl->n_panels; pi++ ) {
if ( fill_paths(fh, dtempl, pi, master_el) ) {
ERROR("Failed to enumerate paths.\n");
H5Fclose(fh);
return NULL;
}
}
}
/* Now enumerate the placeholder dimensions */
if ( dtempl->dim_dim > 0 ) {
struct event_list *master_el_with_dims;
int evi;
/* If there were no HDF5 path placeholders, add a dummy event */
if ( master_el->num_events == 0 ) {
struct event *empty_ev;
empty_ev = initialize_event();
append_event_to_event_list(master_el, empty_ev);
free(empty_ev);
}
master_el_with_dims = initialize_event_list();
/* For each event so far, expand the dimensions */
for ( evi=0; evi<master_el->num_events; evi++ ) {
int pi;
int global_path_dim = -1;
int mlwd;
/* Check the dimensionality of each panel */
for ( pi=0; pi<dtempl->n_panels; pi++ ) {
if ( check_dims(fh, &dtempl->panels[pi],
master_el->events[evi],
master_el_with_dims,
&global_path_dim) )
{
ERROR("Failed to enumerate dims.\n");
H5Fclose(fh);
return NULL;
}
}
/* Add this dimensionality to all events */
for ( mlwd=0; mlwd<global_path_dim; mlwd++ ) {
struct event *mlwd_ev;
mlwd_ev = copy_event(master_el->events[evi]);
push_dim_entry_to_event(mlwd_ev, mlwd);
append_event_to_event_list(master_el_with_dims,
mlwd_ev);
free_event(mlwd_ev);
}
}
free_event_list(master_el);
H5Fclose(fh);
return master_el_with_dims;
} else {
H5Fclose(fh);
return master_el;
}
}
int is_hdf5_file(const char *filename)
{
return (H5Fis_hdf5(filename) > 0);
}
|