-
Notifications
You must be signed in to change notification settings - Fork 46
/
reflection_table_msgpack_adapter.h
844 lines (740 loc) · 26.4 KB
/
reflection_table_msgpack_adapter.h
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
/*
* reflection_table_msgpack_adapter.h
*
* Copyright (C) 2013 Diamond Light Source
*
* Author: James Parkhurst
*
* This code is distributed under the BSD license, a copy of which is
* included in the root directory of this package.
*/
#ifndef DIALS_ARRAY_FAMILY_REFLECTION_TABLE_MSGPACK_ADAPTER_H
#define DIALS_ARRAY_FAMILY_REFLECTION_TABLE_MSGPACK_ADAPTER_H
#include <scitbx/array_family/shared.h>
#include <dials/array_family/reflection_table.h>
#include <msgpack.hpp>
namespace msgpack {
MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS) {
namespace adaptor {
/**
* Helper struct to give a column type a name
*/
template <typename T>
struct column_type {};
/**
* Helper struct for bool column type
*/
template <>
struct column_type<bool> {
static std::string name() {
return "bool";
}
};
/**
* Helper struct for int column type
*/
template <>
struct column_type<int> {
static std::string name() {
return "int";
}
};
/**
* Helper struct for std::size_t column type
*/
template <>
struct column_type<std::size_t> {
static std::string name() {
return "std::size_t";
}
};
/**
* Helper struct for double column type
*/
template <>
struct column_type<double> {
static std::string name() {
return "double";
}
};
/**
* Helper struct for std::string column type
*/
template <>
struct column_type<std::string> {
static std::string name() {
return "std::string";
}
};
/**
* Helper struct for vec2<double> column type
*/
template <>
struct column_type<scitbx::vec2<double> > {
static std::string name() {
return "vec2<double>";
}
};
/**
* Helper struct for vec3<double> column type
*/
template <>
struct column_type<scitbx::vec3<double> > {
static std::string name() {
return "vec3<double>";
}
};
/**
* Helper struct for mat3<double> column type
*/
template <>
struct column_type<scitbx::mat3<double> > {
static std::string name() {
return "mat3<double>";
}
};
/**
* Helper struct for int6 column type
*/
template <>
struct column_type<scitbx::af::tiny<int, 6> > {
static std::string name() {
return "int6";
}
};
/**
* Helper struct for miller index column type
*/
template <>
struct column_type<cctbx::miller::index<> > {
static std::string name() {
return "cctbx::miller::index<>";
}
};
/**
* Helper struct for Shoebox column type
*/
template <>
struct column_type<dials::af::Shoebox<> > {
static std::string name() {
return "Shoebox<>";
}
};
/**
* A helper class to return size of an element
*/
template <typename T>
struct element_size_helper {
static std::size_t size() {
return sizeof(T);
}
};
/**
* A helper class to give size of a tiny type
*/
template <typename T, std::size_t N>
struct element_size_helper<scitbx::af::tiny_plain<T, N> > {
static std::size_t size() {
return N * element_size_helper<T>::size();
}
};
/**
* Pack a const ref into a msgpack raw binary structure. Serializing the whole
* array as a binary blob is much faster than serializing as an array. It also takes
* less space in the case of elements which are fixed sized arrays (such as
* vec2/vec3/mat3 etc).
*/
template <typename T>
struct pack<scitbx::af::const_ref<T> > {
template <typename Stream>
msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o,
const scitbx::af::const_ref<T>& v) const {
std::size_t num_elements = v.size();
std::size_t element_size = element_size_helper<T>::size();
std::size_t binary_size = num_elements * element_size;
o.pack_bin(binary_size);
o.pack_bin_body(reinterpret_cast<const char*>(&v[0]), binary_size);
return o;
}
};
/**
* Pack a shared<Shoebox<>> into a msgpack array.
*
* Shoebox arrays are treated differently because they are themselves
* structs with multiple items.
*/
template <typename T>
struct pack<scitbx::af::const_ref<dials::af::Shoebox<T> > > {
template <typename Stream>
msgpack::packer<Stream>& operator()(
msgpack::packer<Stream>& o,
const scitbx::af::const_ref<dials::af::Shoebox<T> >& v) const {
typedef typename scitbx::af::const_ref<dials::af::Shoebox<T> >::const_iterator
iterator;
std::stringstream buffer;
for (iterator it = v.begin(); it != v.end(); ++it) {
// Write the panel
write(buffer, (uint32_t)it->panel);
// Check the bounding box makes sense
DIALS_ASSERT(it->bbox[1] >= it->bbox[0]);
DIALS_ASSERT(it->bbox[3] >= it->bbox[2]);
DIALS_ASSERT(it->bbox[5] >= it->bbox[4]);
// Write the bounding box
write(buffer, (int32_t)it->bbox[0]);
write(buffer, (int32_t)it->bbox[1]);
write(buffer, (int32_t)it->bbox[2]);
write(buffer, (int32_t)it->bbox[3]);
write(buffer, (int32_t)it->bbox[4]);
write(buffer, (int32_t)it->bbox[5]);
// Serialise data
if (it->data.size() > 0) {
DIALS_ASSERT(it->is_consistent());
// Write 1 to indicate data is present
write(buffer, (uint8_t)1);
// Write data array
buffer.write((const char*)&it->data[0],
it->data.size() * element_size_helper<T>::size());
// Write mask array
buffer.write((const char*)&it->mask[0],
it->mask.size() * element_size_helper<int>::size());
// Write background array
buffer.write((const char*)&it->background[0],
it->background.size() * element_size_helper<T>::size());
} else {
// Write zero to indicate data is not present
write(buffer, (uint8_t)0);
}
}
// Serialise the string to msgpack binary
std::string buffer_string = buffer.str();
o.pack_bin(buffer_string.size());
o.pack_bin_body(buffer_string.c_str(), buffer_string.size());
return o;
}
template <typename Stream, typename ValueType>
void write(Stream& buffer, const ValueType& x) const {
buffer.write((const char*)&x, sizeof(ValueType));
}
};
/**
* Pack a shared into a msgpack raw binary structure
*/
template <typename T>
struct pack<scitbx::af::shared<T> > {
template <typename Stream>
msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o,
const scitbx::af::shared<T>& v) const {
o.pack_array(2);
o.pack(v.size());
o.pack(v.const_ref());
return o;
}
};
/**
* Pack the c_grid accessor into a message pack array
*/
template <std::size_t N>
struct pack<scitbx::af::c_grid<N> > {
template <typename Stream>
msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o,
const scitbx::af::c_grid<N>& v) const {
o.pack_array(N);
for (std::size_t i = 0; i < N; ++i) {
o.pack(v[i]);
}
return o;
}
};
/**
* Pack a versa into a msgpack array and preserve accessor size
*/
template <typename T, typename Accessor>
struct pack<scitbx::af::versa<T, Accessor> > {
template <typename Stream>
msgpack::packer<Stream>& operator()(
msgpack::packer<Stream>& o,
const scitbx::af::versa<T, Accessor>& v) const {
o.pack_array(2);
o.pack(v.accessor());
o.pack(scitbx::af::const_ref<T>(&v[0], v.size()));
return o;
}
};
/**
* Pack an af::tiny into a msgpack array
*/
template <typename T, std::size_t N>
struct pack<scitbx::af::tiny<T, N> > {
template <typename Stream>
msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o,
const scitbx::af::tiny<T, N>& v) const {
o.pack_array(N);
for (std::size_t i = 0; i < N; ++i) {
o.pack(v[i]);
}
return o;
}
};
/**
* A visitor to help with packing a column variant type.
* Pack the column into an array like: [ name, data ]
*/
template <typename Stream>
struct packer_visitor : boost::static_visitor<void> {
packer<Stream>& o_;
packer_visitor(packer<Stream>& o) : o_(o) {}
template <typename T>
void operator()(T const& value) const {
o_.pack_array(2);
o_.pack(column_type<typename T::value_type>::name());
o_.pack(value);
}
};
/**
* Pack the reflection table into an array with a map like:
* [
* "dials::af::reflection_table",
* VERSION,
* {
* "nrows" : N_ROWS,
* "data" : DATA
* }
* ]
*
* The first entry identifies the data as a reflection table.
* The second entry gives the version number in case this changes
* The third entry gives the expected number of rows in the table
* The fourth entry is a map with key value pairs corresponding to the names
* and arrays of the column data.
*
*/
template <>
struct pack<dials::af::reflection_table> {
template <typename Stream>
msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o,
const dials::af::reflection_table& v) const {
typedef dials::af::reflection_table::const_iterator iterator;
std::string filetype = "dials::af::reflection_table";
std::size_t version = 1;
// Pack the:
// filetype
// version
// contents map
o.pack_array(3);
o.pack(filetype);
o.pack(version);
o.pack_map(3);
// Pack the experiment identifiers
o.pack("identifiers");
o.pack_map(v.experiment_identifiers()->size());
for (dials::af::reflection_table::experiment_map_type::const_iterator it =
v.experiment_identifiers()->begin();
it != v.experiment_identifiers()->end();
++it) {
o.pack(it->first);
o.pack(it->second);
}
// Pack the number of rows
o.pack("nrows");
o.pack(v.nrows());
// Pack the data
o.pack("data");
o.pack_map(v.ncols());
for (iterator it = v.begin(); it != v.end(); ++it) {
o.pack(it->first);
boost::apply_visitor(packer_visitor<Stream>(o), it->second);
}
return o;
}
};
/**
* Convert a msgpack array into a fixed size scitbx::af::ref
*/
template <typename T>
struct convert<scitbx::af::ref<T> > {
msgpack::object const& operator()(msgpack::object const& o,
scitbx::af::ref<T>& v) const {
// Ensure the type is an array
if (o.type != msgpack::type::BIN) {
throw DIALS_ERROR("scitbx::af::ref: msgpack type is not BIN");
}
// Compute the element and binary sizes
std::size_t element_size = element_size_helper<T>::size();
std::size_t binary_size = o.via.bin.size;
std::size_t num_elements = binary_size / element_size;
// Check the sizes are consistent
if (num_elements * element_size != binary_size) {
throw DIALS_ERROR(
"scitbx::af::ref: msgpack bin data does not have correct size");
}
// Ensure it is of the correct size
if (num_elements != v.size()) {
throw DIALS_ERROR(
"scitbx::af::ref: msgpack bin data does not have correct size");
}
// Copy the binary data
const T* first = reinterpret_cast<const T*>(o.via.bin.ptr);
const T* last = first + num_elements;
std::copy(first, last, v.begin());
return o;
}
};
/**
* Convert a msgpack array into a variable sized scitbx::af::shared<Shoebox<>>>
*
* Shoebox arrays are treated differently because they are themselves
* structs with multiple items.
*/
template <typename T>
struct convert<scitbx::af::ref<dials::af::Shoebox<T> > > {
msgpack::object const& operator()(
msgpack::object const& o,
scitbx::af::ref<dials::af::Shoebox<T> >& v) const {
typedef typename scitbx::af::ref<dials::af::Shoebox<T> >::iterator iterator;
// Ensure the type is an array
if (o.type != msgpack::type::BIN) {
throw DIALS_ERROR("scitbx::af::ref<Shoebox>: msgpack type is not BIN");
}
// Get the data and size
std::size_t binary_size = o.via.bin.size;
const char* binary_data = reinterpret_cast<const char*>(o.via.bin.ptr);
std::stringstream buffer(std::string(binary_data, binary_size));
// Stream into shoeboxes
for (iterator it = v.begin(); it != v.end(); ++it) {
// Read the panel
it->panel = read<uint32_t>(buffer);
// Read the bounding box
it->bbox[0] = read<uint32_t>(buffer);
it->bbox[1] = read<uint32_t>(buffer);
it->bbox[2] = read<uint32_t>(buffer);
it->bbox[3] = read<uint32_t>(buffer);
it->bbox[4] = read<uint32_t>(buffer);
it->bbox[5] = read<uint32_t>(buffer);
// Check that the bounding box makes sense
DIALS_ASSERT(it->bbox[1] >= it->bbox[0]);
DIALS_ASSERT(it->bbox[3] >= it->bbox[2]);
DIALS_ASSERT(it->bbox[5] >= it->bbox[4]);
// If the data present
bool read_data = read<uint8_t>(buffer);
if (read_data) {
// Create the accessor
scitbx::af::c_grid<3> accessor(it->bbox[5] - it->bbox[4],
it->bbox[3] - it->bbox[2],
it->bbox[1] - it->bbox[0]);
// Allocate the array
it->data = scitbx::af::versa<T, scitbx::af::c_grid<3> >(accessor);
// Copy to the buffer
buffer.read((char*)&it->data[0],
it->data.size() * element_size_helper<T>::size());
// Allocate the array
it->mask = scitbx::af::versa<int, scitbx::af::c_grid<3> >(accessor);
// Copy to the buffer
buffer.read((char*)&it->mask[0],
it->mask.size() * element_size_helper<int>::size());
// Allocate the array
it->background = scitbx::af::versa<T, scitbx::af::c_grid<3> >(accessor);
// Copy to the buffer
buffer.read((char*)&it->background[0],
it->background.size() * element_size_helper<T>::size());
// Check to ensure consistency
DIALS_ASSERT(it->is_consistent());
}
}
return o;
}
template <typename ValueType, typename Stream>
ValueType read(Stream& buffer) const {
ValueType x;
buffer.read((char*)&x, sizeof(ValueType));
return x;
}
};
/**
* Convert a msgpack array into a variable size scitbx::af::shared
*/
template <typename T>
struct convert<scitbx::af::shared<T> > {
msgpack::object const& operator()(msgpack::object const& o,
scitbx::af::shared<T>& v) const {
// Ensure type is an array
if (o.type != msgpack::type::ARRAY) {
throw DIALS_ERROR("scitbx::af::shared: msgpack type is not an array");
}
// Ensure that we have an accessor and data element
if (o.via.array.size != 2) {
throw DIALS_ERROR(
"scitbx::af::shared: msgpack array does not have correct dimensions");
}
// Read the accessor element
std::size_t size = 0;
o.via.array.ptr[0].convert(size);
// Resize the versa
v = scitbx::af::shared<T>(size);
// Read the data
scitbx::af::ref<T> data_ref = v.ref();
o.via.array.ptr[1].convert(data_ref);
return o;
}
};
/**
* Convert a msgpack array into a scitbx::af::versa with accessor
*/
template <typename T, typename Accessor>
struct convert<scitbx::af::versa<T, Accessor> > {
msgpack::object const& operator()(msgpack::object const& o,
scitbx::af::versa<T, Accessor>& v) const {
// Ensure type is an array
if (o.type != msgpack::type::ARRAY) {
throw DIALS_ERROR("scitbx::af::versa: msgpack type is not an array");
}
// Ensure that we have an accessor and data element
if (o.via.array.size != 2) {
throw DIALS_ERROR(
"scitbx::af::versa: msgpack array does not have correct dimensions");
}
// Read the accessor element
Accessor grid;
o.via.array.ptr[0].convert(grid);
// Resize the versa
v = scitbx::af::versa<T, Accessor>(grid);
// Read the data
scitbx::af::ref<T> data_ref(&v[0], v.size());
o.via.array.ptr[1].convert(data_ref);
return o;
}
};
/**
* Convert a msgpack array to a c_grid<N>
*/
template <std::size_t N>
struct convert<scitbx::af::c_grid<N> > {
msgpack::object const& operator()(msgpack::object const& o,
scitbx::af::c_grid<N>& v) const {
// Ensure type is an array
if (o.type != msgpack::type::ARRAY) {
throw DIALS_ERROR("scitbx::af::c_grid: msgpack type is not an array");
}
// Ensure that we have an accessor and data element
if (o.via.array.size != N) {
throw DIALS_ERROR(
"scitbx::af::c_grid: msgpack array does not have correct dimensions");
}
// Convert the elements
for (std::size_t i = 0; i < N; ++i) {
o.via.array.ptr[i].convert(v[i]);
}
return o;
}
};
/**
* Convert a msgpack array to an tiny<T,N>
*/
template <typename T, std::size_t N>
struct convert<scitbx::af::tiny<T, N> > {
msgpack::object const& operator()(msgpack::object const& o,
scitbx::af::tiny<T, N>& v) const {
// Ensure type is an array
if (o.type != msgpack::type::ARRAY) {
throw DIALS_ERROR("scitbx::af::tiny: msgpack type is not an array");
}
// Ensure that we have an accessor and data element
if (o.via.array.size != N) {
throw DIALS_ERROR(
"scitbx::af::tiny: msgpack array does not have correct dimensions");
}
// Convert the elements
for (std::size_t i = 0; i < N; ++i) {
o.via.array.ptr[i].convert(v[i]);
}
return o;
}
};
/**
* Convert a msgpack array into a column. The array will have a structure like
* [ type, data ]
*/
template <>
struct convert<dials::af::reflection_table::mapped_type> {
msgpack::object const& operator()(
msgpack::object const& o,
dials::af::reflection_table::mapped_type& v) const {
// Check the type is an array
if (o.type != msgpack::type::ARRAY) {
throw DIALS_ERROR(
"dials::af::reflection_table::mapped_type: msgpack type is not an array");
}
// Check there are 2 elements
if (o.via.array.size != 2) {
throw DIALS_ERROR(
"dials::af::reflection_table::mapped_type: msgpack array does not have "
"correct dimensions");
}
// Read the type name from the first element
std::string name;
o.via.array.ptr[0].convert(name);
// Read an af::shared<T> from the second element
if (name == "bool") {
v = extract<bool>(o.via.array.ptr[1]);
} else if (name == "int") {
v = extract<int>(o.via.array.ptr[1]);
} else if (name == "std::size_t") {
v = extract<std::size_t>(o.via.array.ptr[1]);
} else if (name == "double") {
v = extract<double>(o.via.array.ptr[1]);
} else if (name == "std::string") {
v = extract<std::string>(o.via.array.ptr[1]);
} else if (name == "vec2<double>") {
v = extract<scitbx::vec2<double> >(o.via.array.ptr[1]);
} else if (name == "vec3<double>") {
v = extract<scitbx::vec3<double> >(o.via.array.ptr[1]);
} else if (name == "mat3<double>") {
v = extract<scitbx::mat3<double> >(o.via.array.ptr[1]);
} else if (name == "int6") {
v = extract<scitbx::af::int6>(o.via.array.ptr[1]);
} else if (name == "cctbx::miller::index<>") {
v = extract<cctbx::miller::index<> >(o.via.array.ptr[1]);
} else if (name == "Shoebox<>") {
v = extract<dials::af::Shoebox<> >(o.via.array.ptr[1]);
} else {
throw DIALS_ERROR(
"dials::af::reflection_table::mapped_type: unexpected column type");
}
return o;
}
template <typename T>
scitbx::af::shared<T> extract(msgpack::object const& o) const {
scitbx::af::shared<T> data;
o.convert(data);
return data;
}
};
/**
* Convert a msgpack structure into a reflection table. The msgpack structure
* will be like:
* [
* "dials::af::reflection_table",
* VERSION,
* {
* "nrows" : N_ROWS,
* "data" : DATA
* }
* ]
*
* The first entry identifies the data as a reflection table.
* The second entry gives the version number in case this changes
* The third entry is a dictionary containing data and metadata
*/
template <>
struct convert<dials::af::reflection_table> {
msgpack::object const& operator()(msgpack::object const& o,
dials::af::reflection_table& v) const {
typedef dials::af::reflection_table::key_type key_type;
typedef dials::af::reflection_table::mapped_type mapped_type;
// Check the type is an array
if (o.type != msgpack::type::ARRAY) {
throw DIALS_ERROR(
"dials::af::reflection_table: msgpack type is not an array");
}
// Check there are 4 elements
if (o.via.array.size != 3) {
throw DIALS_ERROR(
"dials::af::reflection_table: msgpack array does not have correct "
"dimensions");
}
// Check the file type
std::string filetype;
o.via.array.ptr[0].convert(filetype);
if (filetype != "dials::af::reflection_table") {
throw DIALS_ERROR(
"dials::af::reflection_table: expected dials::af::reflection_table, got "
"something else");
}
// Check the version
std::size_t version;
o.via.array.ptr[1].convert(version);
if (version != 1) {
throw DIALS_ERROR(
"dials::af::reflection_table: expected version 1, got something else");
}
// Get the header object
msgpack::object* header_object = &o.via.array.ptr[2];
// Check the type is an array
if (header_object->type != msgpack::type::MAP) {
throw DIALS_ERROR(
"dials::af::reflection_table: header msgpack type is not an map");
}
// Set the the column map object to NULL
msgpack::object* map_object = NULL;
msgpack::object* identifier_object = NULL;
// Required colulmns
bool found_nrows = false;
// Loop through the meta data
msgpack::object_kv* first = header_object->via.map.ptr;
msgpack::object_kv* last = first + header_object->via.map.size;
for (msgpack::object_kv* it = first; it != last; ++it) {
// Get the item name
std::string name;
it->key.convert(name);
if (name == "nrows") {
// Resize the expected number of rows
std::size_t nrows;
it->val.convert(nrows);
v.resize(nrows);
// nrows is required
found_nrows = true;
} else if (name == "identifiers") {
// Get the identifier ptr
identifier_object = &it->val;
} else if (name == "data") {
// Get the data ptr
map_object = &it->val;
} else {
throw DIALS_ERROR(
"dials::af::reflection_table: unknown key in reflection file");
}
}
// Check the identifiers
if (identifier_object != NULL) {
if (identifier_object->type != msgpack::type::MAP) {
throw DIALS_ERROR("dials::af::reflection_table: identifier data not found");
}
// Read the identifiers from the map
if (identifier_object->via.map.size != 0) {
msgpack::object_kv* first = identifier_object->via.map.ptr;
msgpack::object_kv* last = first + identifier_object->via.map.size;
for (msgpack::object_kv* it = first; it != last; ++it) {
dials::af::reflection_table::experiment_map_type::key_type key = -1;
dials::af::reflection_table::experiment_map_type::mapped_type value;
it->key.convert(key);
it->val.convert(value);
(*v.experiment_identifiers())[key] = value;
}
}
}
// Check the number of rows has been found
if (!found_nrows) {
throw DIALS_ERROR("dials::af::reflection_table: number of rows not found");
}
// Check the table data
if (map_object == NULL || map_object->type != msgpack::type::MAP) {
throw DIALS_ERROR("dials::af::reflection_table: table data not found");
}
// Read the columns from the map
if (map_object->via.map.size != 0) {
msgpack::object_kv* first = map_object->via.map.ptr;
msgpack::object_kv* last = first + map_object->via.map.size;
for (msgpack::object_kv* it = first; it != last; ++it) {
key_type key;
mapped_type value;
it->key.convert(key);
it->val.convert(value);
v[key] = value;
}
}
return o;
}
};
} // namespace adaptor
} // MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS)
} // namespace msgpack
#endif