-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathNeXusFile.cpp
More file actions
1960 lines (1765 loc) · 62.6 KB
/
NeXusFile.cpp
File metadata and controls
1960 lines (1765 loc) · 62.6 KB
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
#include <cstring>
// REMOVE
#include <iostream>
#include <sstream>
#include <typeinfo>
#include "napiconfig.h"
#include "NeXusFile.hpp"
#include "NeXusException.hpp"
using namespace NeXus;
using std::map;
using std::pair;
using std::string;
using std::stringstream;
using std::vector;
/**
* \file NeXusFile.cpp
* The implementation of the NeXus C++ API
*/
static const string NULL_STR = "NULL";
namespace { // anonymous namespace to keep it in the file
template <typename NumT>
static string toString(const vector<NumT>& data) {
stringstream result;
result << "[";
size_t size = data.size();
for (size_t i = 0; i < size; i++) {
result << data[i];
if (i+1 < size) {
result << ",";
}
}
result << "]";
return result.str();
}
static vector<int64_t> toInt64(const vector<int> & small_v) {
// copy the dims over to call the int64_t version
return vector<int64_t>(small_v.begin(),small_v.end());
}
} // end of anonymous namespace
namespace NeXus {
// catch for undefined types
template <typename NumT>
NXnumtype getType(NumT number) {
stringstream msg;
msg << "NeXus::getType() does not know type of " << typeid(number).name();
throw Exception(msg.str());
}
template<>
NXDLL_EXPORT NXnumtype getType(char number) {
(void)number; // Avoid compiler warning
return CHAR;
}
// template specialisations for types we know
template<>
NXDLL_EXPORT NXnumtype getType(float number) {
(void)number; // Avoid compiler warning
return FLOAT32;
}
template<>
NXDLL_EXPORT NXnumtype getType(double number) {
(void)number; // Avoid compiler warning
return FLOAT64;
}
template<>
NXDLL_EXPORT NXnumtype getType(int8_t number) {
(void)number; // Avoid compiler warning
return INT8;
}
template<>
NXDLL_EXPORT NXnumtype getType(uint8_t number) {
(void)number; // Avoid compiler warning
return UINT8;
}
template<>
NXDLL_EXPORT NXnumtype getType(int16_t number) {
(void)number; // Avoid compiler warning
return INT16;
}
template<>
NXDLL_EXPORT NXnumtype getType(uint16_t number) {
(void)number; // Avoid compiler warning
return UINT16;
}
template<>
NXDLL_EXPORT NXnumtype getType(int32_t number) {
(void)number; // Avoid compiler warning
return INT32;
}
template<>
NXDLL_EXPORT NXnumtype getType(uint32_t number) {
(void)number; // Avoid compiler warning
return UINT32;
}
template<>
NXDLL_EXPORT NXnumtype getType(int64_t number) {
(void)number; // Avoid compiler warning
return INT64;
}
template<>
NXDLL_EXPORT NXnumtype getType(uint64_t number) {
(void)number; // Avoid compiler warning
return UINT64;
}
}
// check type sizes - uses a trick that you cannot allocate an
// array of negative length
#ifdef _MSC_VER
#define ARRAY_OFFSET 1 /* cannot dimension an array with zero elements */
#else
#define ARRAY_OFFSET 0 /* can dimension an array with zero elements */
#endif /* _MSC_VER */
/*
static int check_float_too_big[4 - sizeof(float) + ARRAY_OFFSET]; // error if float > 4 bytes
static int check_float_too_small[sizeof(float) - 4 + ARRAY_OFFSET]; // error if float < 4 bytes
static int check_double_too_big[8 - sizeof(double) + ARRAY_OFFSET]; // error if double > 8 bytes
static int check_double_too_small[sizeof(double) - 8 + ARRAY_OFFSET]; // error if double < 8 bytes
static int check_char_too_big[1 - sizeof(char) + ARRAY_OFFSET]; // error if char > 1 byte
*/
namespace {
static void inner_malloc(void* & data, const std::vector<int64_t>& dims, NXnumtype type) {
int rank = dims.size();
int64_t c_dims[NX_MAXRANK];
for (int i = 0; i < rank; i++) {
c_dims[i] = dims[i];
}
NXstatus status = NXmalloc64(&data, rank, c_dims, type);
if (status != NX_OK) {
throw Exception("NXmalloc failed", status);
}
}
static void inner_free(void* & data) {
NXstatus status = NXfree(&data);
if (status != NX_OK) {
throw Exception("NXfree failed", status);
}
}
} // end of anonymous namespace
namespace NeXus {
File::File(NXhandle handle, bool close_handle) : m_file_id(handle), m_close_handle(close_handle) {
}
File::File(const string& filename, const NXaccess access) : m_close_handle (true) {
this->initOpenFile(filename, access);
}
File::File(const char *filename, const NXaccess access) : m_close_handle (true) {
this->initOpenFile(string(filename), access);
}
void File::initOpenFile(const string& filename, const NXaccess access) {
if (filename.empty()) {
throw Exception("Filename specified is empty constructor");
}
NXstatus status = NXopen(filename.c_str(), access, &(this->m_file_id));
if (status != NX_OK) {
stringstream msg;
msg << "NXopen(" << filename << ", " << access << ") failed";
throw Exception(msg.str(), status);
}
}
File::~File() {
if (m_close_handle && m_file_id != NULL) {
NXstatus status = NXclose(&(this->m_file_id));
this->m_file_id = NULL;
if (status != NX_OK) {
stringstream msg;
msg << "NXclose failed with status: " << status << "\n";
NXReportError(const_cast<char*>(msg.str().c_str()));
}
}
}
void File::close() {
if (this->m_file_id != NULL) {
NXstatus status = NXclose(&(this->m_file_id));
this->m_file_id = NULL;
if (status != NX_OK) {
throw Exception("NXclose failed", status);
}
}
}
void File::flush() {
NXstatus status = NXflush(&(this->m_file_id));
if (status != NX_OK) {
throw Exception("NXflush failed", status);
}
}
void File::makeGroup(const string& name, const string& class_name, bool open_group) {
if (name.empty()) {
throw Exception("Supplied empty name to makeGroup");
}
if (class_name.empty()) {
throw Exception("Supplied empty class name to makeGroup");
}
NXstatus status = NXmakegroup(this->m_file_id, name.c_str(),
class_name.c_str());
if (status != NX_OK) {
stringstream msg;
msg << "NXmakegroup(" << name << ", " << class_name << ") failed";
throw Exception(msg.str(), status);
}
if (open_group) {
this->openGroup(name, class_name);
}
}
void File::openGroup(const string& name, const string& class_name) {
if (name.empty()) {
throw Exception("Supplied empty name to openGroup");
}
if (class_name.empty()) {
throw Exception("Supplied empty class name to openGroup");
}
NXstatus status = NXopengroup(this->m_file_id, name.c_str(),
class_name.c_str());
if (status != NX_OK) {
stringstream msg;
msg << "NXopengroup(" << name << ", " << class_name << ") failed";
throw Exception(msg.str(), status);
}
}
void File::openPath(const string& path) {
if (path.empty()) {
throw Exception("Supplied empty path to openPath");
}
NXstatus status = NXopenpath(this->m_file_id, path.c_str());
if (status != NX_OK) {
stringstream msg;
msg << "NXopenpath(" << path << ") failed";
throw Exception(msg.str(), status);
}
}
void File::openGroupPath(const string& path) {
if (path.empty()) {
throw Exception("Supplied empty path to openGroupPath");
}
NXstatus status = NXopengrouppath(this->m_file_id, path.c_str());
if (status != NX_OK) {
stringstream msg;
msg << "NXopengrouppath(" << path << ") failed";
throw Exception(msg.str(), status);
}
}
std::string File::getPath(){
char cPath[2048];
memset(cPath,0,sizeof(cPath));
NXstatus status = NXgetpath(this->m_file_id,cPath, sizeof(cPath)-1);
if (status != NX_OK) {
stringstream msg;
msg << "NXgetpath() failed";
throw Exception(msg.str(), status);
}
return std::string(cPath);
}
void File::closeGroup() {
NXstatus status = NXclosegroup(this->m_file_id);
if (status != NX_OK) {
throw Exception("NXclosegroup failed", status);
}
}
void File::makeData(const string& name, NXnumtype type,
const vector<int>& dims, bool open_data) {
this->makeData(name, type, toInt64(dims), open_data);
}
void File::makeData(const string& name, NXnumtype type,
const vector<int64_t>& dims, bool open_data) {
// error check the parameters
if (name.empty()) {
throw Exception("Supplied empty label to makeData");
}
if (dims.empty()) {
throw Exception("Supplied empty dimensions to makeData");
}
// do the work
NXstatus status = NXmakedata64(this->m_file_id, name.c_str(), (int)type,
dims.size(), const_cast<int64_t*>(&(dims[0])));
// report errors
if (status != NX_OK) {
stringstream msg;
msg << "NXmakedata(" << name << ", " << type << ", " << dims.size()
<< ", " << toString(dims) << ") failed";
throw Exception(msg.str(), status);
}
if (open_data) {
this->openData(name);
}
}
template <typename NumT>
void File::makeData(const string & name, const NXnumtype type,
const NumT length, bool open_data) {
vector<int64_t> dims;
dims.push_back(static_cast<int64_t>(length));
this->makeData(name, type, dims, open_data);
}
template <typename NumT>
void File::writeData(const string& name, const NumT& value) {
std::vector<NumT> v(1, value);
this->writeData(name, v);
}
void File::writeData(const string& name, const char* value) {
this->writeData(name, std::string(value));
}
void File::writeData(const string& name, const string& value)
{
string my_value(value);
// Allow empty strings by defaulting to a space
if (my_value.empty())
my_value = " ";
vector<int> dims;
dims.push_back(static_cast<int>(my_value.size()));
this->makeData(name, CHAR, dims, true);
this->putData(&(my_value[0]));
this->closeData();
}
template <typename NumT>
void File::writeData(const string& name, const vector<NumT>& value) {
vector<int64_t> dims(1, value.size());
this->writeData(name, value, dims);
}
template <typename NumT>
void File::writeData(const string& name, const vector<NumT>& value,
const vector<int>& dims) {
this->makeData(name, getType<NumT>(), dims, true);
this->putData(value);
this->closeData();
}
template <typename NumT>
void File::writeData(const string& name, const vector<NumT>& value,
const vector<int64_t>& dims) {
this->makeData(name, getType<NumT>(), dims, true);
this->putData(value);
this->closeData();
}
template <typename NumT>
void File::writeExtendibleData(const string& name, vector<NumT>& value)
{
// Use a default chunk size of 4096 bytes. TODO: Is this optimal?
writeExtendibleData(name, value, 4096);
}
template <typename NumT>
void File::writeExtendibleData(const string& name, vector<NumT>& value, const int64_t chunk)
{
vector<int64_t> dims(1, NX_UNLIMITED);
vector<int64_t> chunk_dims(1, chunk);
// Use chunking without using compression
this->makeCompData(name, getType<NumT>(), dims, NONE, chunk_dims, true );
this->putSlab(value, int64_t(0), int64_t(value.size()));
this->closeData();
}
template <typename NumT>
void File::writeExtendibleData(const string& name, vector<NumT>& value,
vector<int64_t>& dims, std::vector<int64_t> & chunk)
{
// Create the data with unlimited 0th dimensions
std::vector<int64_t> unlim_dims(dims);
unlim_dims[0] = NX_UNLIMITED;
// Use chunking without using compression
this->makeCompData(name, getType<NumT>(), unlim_dims, NONE, chunk, true );
// And put that slab of that of that given size in there
std::vector<int64_t> start( dims.size(), 0 );
this->putSlab(value, start, dims);
this->closeData();
}
template <typename NumT>
void File::writeUpdatedData(const std::string& name, std::vector<NumT>& value)
{
this->openData(name);
this->putSlab(value, int64_t(0), int64_t(value.size()));
this->closeData();
}
template <typename NumT>
void File::writeUpdatedData(const std::string& name, std::vector<NumT>& value,
std::vector<int64_t>& dims)
{
this->openData(name);
std::vector<int64_t> start( dims.size(), 0 );
this->putSlab(value, start, dims);
this->closeData();
}
void File::makeCompData(const string& name, const NXnumtype type,
const vector<int>& dims, const NXcompression comp,
const vector<int>& bufsize, bool open_data) {
this->makeCompData(name, type, toInt64(dims), comp, toInt64(bufsize), open_data);
}
void File::makeCompData(const string& name, const NXnumtype type,
const vector<int64_t>& dims, const NXcompression comp,
const vector<int64_t>& bufsize, bool open_data) {
// error check the parameters
if (name.empty()) {
throw Exception("Supplied empty name to makeCompData");
}
if (dims.empty()) {
throw Exception("Supplied empty dimensions to makeCompData");
}
if (bufsize.empty()) {
throw Exception("Supplied empty bufsize to makeCompData");
}
if (dims.size() != bufsize.size()) {
stringstream msg;
msg << "Supplied dims rank=" << dims.size()
<< " must match supplied bufsize rank=" << bufsize.size()
<< "in makeCompData";
throw Exception(msg.str());
}
// do the work
int i_type = static_cast<int>(type);
int i_comp = static_cast<int>(comp);
NXstatus status = NXcompmakedata64(this->m_file_id, name.c_str(), i_type,
dims.size(),
const_cast<int64_t *>(&(dims[0])), i_comp,
const_cast<int64_t *>(&(bufsize[0])));
// report errors
if (status != NX_OK) {
stringstream msg;
msg << "NXcompmakedata64(" << name << ", " << type << ", " << dims.size()
<< ", " << toString(dims) << ", " << comp << ", " << toString(bufsize)
<< ") failed";
throw Exception(msg.str(), status);
}
if (open_data) {
this->openData(name);
}
}
template <typename NumT>
void File::writeCompData(const string & name, const vector<NumT> & value,
const vector<int> & dims, const NXcompression comp,
const vector<int> & bufsize) {
this->writeCompData(name, value, toInt64(dims), comp, toInt64(bufsize));
}
template <typename NumT>
void File::writeCompData(const string & name, const vector<NumT> & value,
const vector<int64_t> & dims, const NXcompression comp,
const vector<int64_t> & bufsize) {
this->makeCompData(name, getType<NumT>(), dims, comp, bufsize, true);
this->putData(value);
this->closeData();
}
void File::compress(NXcompression comp) {
stringstream msg;
msg << "compress(" << comp << ") is deprecated - use makeCompData()";
throw Exception(msg.str());
}
void File::openData(const string & name) {
if (name.empty()) {
throw Exception("Supplied empty name to openData");
}
NXstatus status = NXopendata(this->m_file_id, name.c_str());
if (status != NX_OK) {
throw Exception("NXopendata(" + name + ") failed", status);
}
}
void File::closeData() {
NXstatus status = NXclosedata(this->m_file_id);
if (status != NX_OK) {
throw Exception("NXclosedata() failed", status);
}
}
void File::putData(const void* data) {
if (data == NULL) {
throw Exception("Data specified as null in putData");
}
NXstatus status = NXputdata(this->m_file_id, const_cast<void *>(data));
if (status != NX_OK) {
throw Exception("NXputdata(void *) failed", status);
}
}
template <typename NumT>
void File::putData(const vector<NumT> & data) {
if (data.empty()) {
throw Exception("Supplied empty data to putData");
}
this->putData(&(data[0]));
}
void File::putAttr(const AttrInfo& info, const void* data) {
if (info.name == NULL_STR) {
throw Exception("Supplied bad attribute name \"" + NULL_STR + "\"");
}
if (info.name.empty()) {
throw Exception("Supplied empty name to putAttr");
}
NXstatus status = NXputattr(this->m_file_id, info.name.c_str(),
const_cast<void *>(data), info.length,
(int)(info.type));
if (status != NX_OK) {
stringstream msg;
msg << "NXputattr(" << info.name << ", data, " << info.length << ", "
<< info.type << ") failed";
throw Exception(msg.str(), status);
}
}
void File::putAttr(const std::string& name, const std::vector<std::string>& array) {
if (name == NULL_STR) {
throw Exception("Supplied bad attribute name \"" + NULL_STR + "\"");
}
if (name.empty()) {
throw Exception("Supplied empty name to putAttr");
}
int maxLength = 0;
for ( std::vector<std::string>::const_iterator it = array.begin();
it != array.end(); ++it ) {
if ( maxLength < it->size() ) {
maxLength = it->size();
}
}
// fill data
std::string data(maxLength*array.size(), '\0');
std::size_t pos = 0;
for ( std::vector<std::string>::const_iterator it = array.begin();
it != array.end(); ++it, pos += maxLength ) {
if ( !(it->empty()) ) {
data.replace(pos, it->size(), *it);
}
}
// set rank and dim
const int rank = 2;
int dim[rank] = {static_cast<int>(array.size()), maxLength};
// write data
NXstatus status = NXputattra(this->m_file_id, name.c_str(),
data.c_str(), rank, dim, CHAR);
if (status != NX_OK) {
stringstream msg;
msg << "NXputattra(" << name << ", data, " << rank
<< ", [" << dim[0] << ", " << dim[1] << "], CHAR) failed";
throw Exception(msg.str(), status);
}
}
template <typename NumT>
void File::putAttr(const std::string& name, const std::vector<NumT>& array) {
if (name == NULL_STR) {
throw Exception("Supplied bad attribute name \"" + NULL_STR + "\"");
}
if (name.empty()) {
throw Exception("Supplied empty name to putAttr");
}
// set rank and dim
const int rank = 1;
int dim[rank] = {static_cast<int>(array.size())};
// write data
NXnumtype type = getType<NumT>();
NXstatus status = NXputattra(this->m_file_id, name.c_str(),
&(array[0]), rank, dim, type);
if (status != NX_OK) {
stringstream msg;
msg << "NXputattra(" << name << ", data, " << rank
<< ", [" << dim[0] << "], " << type << ") failed";
throw Exception(msg.str(), status);
}
}
template <typename NumT>
void File::putAttr(const std::string& name, const NumT value) {
AttrInfo info;
info.name = name;
info.length = 1;
info.type = getType<NumT>();
this->putAttr(info, &value);
}
void File::putAttr(const char* name, const char* value) {
if (name == NULL) {
throw Exception("Specified name as null to putAttr");
}
if (value == NULL) {
throw Exception("Specified value as null to putAttr");
}
string s_name(name);
string s_value(value);
this->putAttr(s_name, s_value);
}
void File::putAttr(const std::string& name, const std::string value) {
string my_value(value);
if (my_value.empty())
my_value = " "; // Make a default "space" to avoid errors.
AttrInfo info;
info.name = name;
info.length = static_cast<int>(my_value.size());
info.type = CHAR;
this->putAttr(info, &(my_value[0]));
}
void File::putSlab(const void* data, const vector<int>& start, const vector<int>& size) {
vector<int64_t> start_big = toInt64(start);
vector<int64_t> size_big = toInt64(size);
this->putSlab(data, start_big, size_big);
}
void File::putSlab(const void* data, const vector<int64_t>& start, const vector<int64_t>& size) {
if (data == NULL) {
throw Exception("Data specified as null in putSlab");
}
if (start.empty()) {
throw Exception("Supplied empty start to putSlab");
}
if (size.empty()) {
throw Exception("Supplied empty size to putSlab");
}
if (start.size() != size.size()) {
stringstream msg;
msg << "Supplied start rank=" << start.size()
<< " must match supplied size rank=" << size.size()
<< "in putSlab";
throw Exception(msg.str());
}
NXstatus status = NXputslab64(this->m_file_id, data, &(start[0]), &(size[0]));
if (status != NX_OK) {
stringstream msg;
msg << "NXputslab64(data, " << toString(start) << ", " << toString(size)
<< ") failed";
throw Exception(msg.str(), status);
}
}
template <typename NumT>
void File::putSlab(const vector<NumT>& data, const vector<int>& start,
const vector<int>& size) {
vector<int64_t> start_big = toInt64(start);
vector<int64_t> size_big = toInt64(size);
this->putSlab(data, start_big, size_big);
}
template <typename NumT>
void File::putSlab(const vector<NumT>& data, const vector<int64_t>& start,
const vector<int64_t>& size) {
if (data.empty()) {
throw Exception("Supplied empty data to putSlab");
}
this->putSlab(&(data[0]), start, size);
}
template <typename NumT>
void File::putSlab(const vector<NumT>& data, int start, int size) {
this->putSlab(data, static_cast<int64_t>(start), static_cast<int64_t>(size));
}
template <typename NumT>
void File::putSlab(const vector<NumT>& data, int64_t start, int64_t size) {
vector<int64_t> start_v(1, start);
vector<int64_t> size_v(1, size);
this->putSlab(data, start_v, size_v);
}
NXlink File::getDataID() {
NXlink link;
NXstatus status = NXgetdataID(this->m_file_id, &link);
if (status != NX_OK) {
throw Exception("NXgetdataID failed", status);
}
return link;
}
bool File::isDataSetOpen()
{
NXlink id;
if(NXgetdataID(this->m_file_id,&id) == NX_ERROR)
{
return false;
}
else
{
return true;
}
}
/*----------------------------------------------------------------------*/
void File::makeLink(NXlink& link) {
NXstatus status = NXmakelink(this->m_file_id, &link);
if (status != NX_OK) {
throw Exception("NXmakelink failed", status);
}
}
void File::makeNamedLink(const string& name, NXlink& link) {
if (name.empty()) {
throw Exception("Supplied empty name to makeNamedLink");
}
NXstatus status = NXmakenamedlink(this->m_file_id, name.c_str(), &link);
if (status != NX_OK) {
throw Exception("NXmakenamedlink(" + name + ", link)", status);
}
}
void File::openSourceGroup() {
NXstatus status = NXopensourcegroup(this->m_file_id);
if (status != NX_OK) {
throw Exception("NXopensourcegroup failed");
}
}
void File::getData(void* data) {
if (data == NULL) {
throw Exception("Supplied null pointer to getData");
}
NXstatus status = NXgetdata(this->m_file_id, data);
if (status != NX_OK) {
throw Exception("NXgetdata failed", status);
}
}
template <typename NumT>
std::vector<NumT> * File::getData() {
Info info = this->getInfo();
if (info.type != getType<NumT>()) {
throw Exception("NXgetdata failed - invalid vector type");
}
// determine the number of elements
int64_t length=1;
for (vector<int64_t>::const_iterator it = info.dims.begin();
it != info.dims.end(); it++) {
length *= *it;
}
// allocate memory to put the data into
void * temp;
inner_malloc(temp, info.dims, info.type);
// fetch the data
this->getData(temp);
// put it in the vector
vector<NumT> * result = new vector<NumT>(static_cast<NumT *>(temp),
static_cast<NumT *>(temp)
+ static_cast<size_t>(length));
inner_free(temp);
return result;
}
template <typename NumT>
void File::getData(vector<NumT>& data) {
Info info = this->getInfo();
if (info.type != getType<NumT>())
{
throw Exception("NXgetdata failed - invalid vector type");
}
// determine the number of elements
int64_t length=1;
for (vector<int64_t>::const_iterator it = info.dims.begin();
it != info.dims.end(); it++) {
length *= *it;
}
// allocate memory to put the data into
// need to use resize() rather than reserve() so vector length gets set
data.resize(length);
// fetch the data
this->getData(&(data[0]));
}
void File::getDataCoerce(vector<int> &data)
{
Info info = this->getInfo();
if (info.type == INT8)
{
vector<int8_t> result;
this->getData(result);
data.assign(result.begin(), result.end());
}
else if (info.type == UINT8)
{
vector<uint8_t> result;
this->getData(result);
data.assign(result.begin(), result.end());
}
else if (info.type == INT16)
{
vector<int16_t> result;
this->getData(result);
data.assign(result.begin(), result.end());
}
else if (info.type == UINT16)
{
vector<uint16_t> result;
this->getData(result);
data.assign(result.begin(), result.end());
}
else if (info.type == INT32)
{
vector<int32_t> result;
this->getData(result);
data.assign(result.begin(), result.end());
}
else if (info.type == UINT32)
{
vector<uint32_t> result;
this->getData(result);
data.assign(result.begin(), result.end());
}
else
{
throw Exception("NexusFile::getDataCoerce(): Could not coerce to int.");
}
}
void File::getDataCoerce(vector<double> &data)
{
Info info = this->getInfo();
if (info.type == INT8)
{
vector<int8_t> result;
this->getData(result);
data.assign(result.begin(), result.end());
}
else if (info.type == UINT8)
{
vector<uint8_t> result;
this->getData(result);
data.assign(result.begin(), result.end());
}
else if (info.type == INT16)
{
vector<int16_t> result;
this->getData(result);
data.assign(result.begin(), result.end());
}
else if (info.type == UINT16)
{
vector<uint16_t> result;
this->getData(result);
data.assign(result.begin(), result.end());
}
else if (info.type == INT32)
{
vector<int32_t> result;
this->getData(result);
data.assign(result.begin(), result.end());
}
else if (info.type == UINT32)
{
vector<uint32_t> result;
this->getData(result);
data.assign(result.begin(), result.end());
}
else if (info.type == FLOAT32)
{
vector<float> result;
this->getData(result);
data.assign(result.begin(), result.end());
}
else if (info.type == FLOAT64)
{
this->getData(data);
}
else
{
throw Exception("NexusFile::getDataCoerce(): Could not coerce to double.");
}
}
template <typename NumT>
void File::readData(const std::string & dataName, std::vector<NumT>& data)
{
this->openData(dataName);
this->getData(data);
this->closeData();
}
template <typename NumT>
void File::readData(const std::string & dataName, NumT & data)
{
std::vector<NumT> dataVector;
this->openData(dataName);
this->getData(dataVector);
if (dataVector.size() > 0)
data = dataVector[0];
this->closeData();
}
void File::readData(const std::string & dataName, std::string& data)
{
this->openData(dataName);
data = this->getStrData();
this->closeData();
}
bool File::isDataInt()
{
Info info = this->getInfo();
switch(info.type)
{
case INT8:
case UINT8:
case INT16:
case UINT16:
case INT32:
case UINT32:
return true;
default:
return false;
}
}
string File::getStrData() {
string res;
Info info = this->getInfo();
if (info.type != NX_CHAR) {
stringstream msg;
msg << "Cannot use getStrData() on non-character data. Found type="
<< info.type;
throw Exception(msg.str());
}
if (info.dims.size() != 1) {
stringstream msg;
msg << "getStrData() only understand rank=1 data. Found rank="
<< info.dims.size();
throw Exception(msg.str());
}
char* value = new char[info.dims[0]+1]; // probably do not need +1, but being safe
try{
this->getData(value);
}