-
Notifications
You must be signed in to change notification settings - Fork 1
/
with_napi.cpp
104 lines (85 loc) · 2.37 KB
/
with_napi.cpp
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
#include <nexus/napi.h>
#include <string>
#include <iostream>
#include <vector>
#include "process_args.h"
#include "timer.h"
using common::Timer;
std::string read_string_data(NXhandle &fileId, const std::string &path) {
int rank;
int dims[4];
int type;
NXopendata(fileId, path.c_str());
NXgetinfo(fileId, &rank, dims, &type);
int n = dims[0];
char *buffer = new char[n];
NXstatus stat = NXgetdata(fileId, buffer);
NXclosedata(fileId);
return std::string(buffer);
}
std::vector<double> read_double_data(NXhandle &fileId,
const std::string &path) {
NXopendata(fileId, path.c_str());
int rank;
int dims[4];
int type;
NXgetinfo(fileId, &rank, dims, &type);
int n = dims[0] * dims[1];
std::vector<double> vec(n);
NXstatus stat = NXgetdata(fileId, &vec[0]);
NXclosedata(fileId);
return vec;
}
void do_test_workspace2d(const std::string &filename) {
Timer timer;
timer.reset();
NXhandle fileId;
NXstatus stat = NXopen(filename.c_str(), NXACC_READ, &fileId);
// Open the workspace group
std::string path = "mantid_workspace_1";
NXopengroup(fileId, path.c_str(), "NXentry");
path = "instrument/instrument_xml/data";
read_string_data(fileId, path);
// Open the workspace entry
path = "workspace";
NXopengroup(fileId, path.c_str(), "NXdata");
// Open values
path = "values";
read_double_data(fileId, path);
// Open errors
path = "errors";
read_double_data(fileId, path);
// Close the groups
NXclosegroup(fileId);
NXclosegroup(fileId);
// Close the file
NXclose(&fileId);
timer.print_elapsed_ms();
};
void do_test_event_workspace(const std::string &filename) {
Timer timer;
// TODO. The HDF5/Nexus file structure is different from the workspace2D so
// new implementation needed of above.
timer.print_elapsed_ms();
}
int main(int argc, const char *argv[]) {
// get the filename from the command line
if (argc == 1) {
common::print_usage(argv[0]);
return 1;
}
// process every file
int result = 0;
for (int i = 1; i < argc; ++i) {
const std::string filename = common::to_filename(argv[i]);
if (filename.empty()) {
std::cerr << "Failed to find readable file \"" << argv[i] << "\""
<< std::endl;
result = -1;
} else {
do_test_workspace2d(filename);
// do_test_event_workspace(filename);
}
}
return result;
}