-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhdf5io.c
200 lines (156 loc) · 6.47 KB
/
hdf5io.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
#include <stdlib.h>
#include <hdf5.h>
#include "waveform.h"
#include "hdf5io.h"
struct hdf5io_waveform_file *hdf5io_open_file(const char *fname)
{
struct hdf5io_waveform_file *wavFile;
wavFile = (struct hdf5io_waveform_file *)malloc(sizeof(struct hdf5io_waveform_file));
wavFile->waveFid = H5Fcreate(fname, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
return wavFile;
}
struct hdf5io_waveform_file *hdf5io_open_file_for_read(const char *fname)
{
struct hdf5io_waveform_file *wavFile;
wavFile = (struct hdf5io_waveform_file *)malloc(sizeof(struct hdf5io_waveform_file));
wavFile->waveFid = H5Fopen(fname, H5F_ACC_RDONLY, H5P_DEFAULT);
return wavFile;
}
int hdf5io_close_file(struct hdf5io_waveform_file *wavFile)
{
herr_t ret;
ret = H5Fclose(wavFile->waveFid);
free(wavFile);
return (int)ret;
}
int hdf5io_flush_file(struct hdf5io_waveform_file *wavFile)
{
herr_t ret;
ret = H5Fflush(wavFile->waveFid, H5F_SCOPE_GLOBAL);
return (int)ret;
}
int hdf5io_write_waveform_attribute_in_file_header(struct hdf5io_waveform_file *wavFile,
struct waveform_attribute *wavAttr)
{
herr_t ret;
hid_t wavAttrTid, wavAttrSid, wavAttrAid, doubleArrayTid, rootGid;
const hsize_t doubleArrayDims[1]={SCOPE_NCH};
const unsigned doubleArrayRank = 1;
doubleArrayTid = H5Tarray_create(H5T_NATIVE_DOUBLE, doubleArrayRank, doubleArrayDims);
wavAttrTid = H5Tcreate(H5T_COMPOUND, sizeof(struct waveform_attribute));
H5Tinsert(wavAttrTid, "wavAttr.dt", HOFFSET(struct waveform_attribute, dt), H5T_NATIVE_DOUBLE);
H5Tinsert(wavAttrTid, "wavAttr.t0", HOFFSET(struct waveform_attribute, t0), H5T_NATIVE_DOUBLE);
H5Tinsert(wavAttrTid, "wavAttr.ymult",
HOFFSET(struct waveform_attribute, ymult), doubleArrayTid);
H5Tinsert(wavAttrTid, "wavAttr.yoff",
HOFFSET(struct waveform_attribute, yoff), doubleArrayTid);
H5Tinsert(wavAttrTid, "wavAttr.yzero",
HOFFSET(struct waveform_attribute, yzero), doubleArrayTid);
wavAttrSid = H5Screate(H5S_SCALAR);
rootGid = H5Gopen(wavFile->waveFid, "/", H5P_DEFAULT);
wavAttrAid = H5Acreate(rootGid, "Waveform Attributes", wavAttrTid, wavAttrSid,
H5P_DEFAULT, H5P_DEFAULT);
ret = H5Awrite(wavAttrAid, wavAttrTid, wavAttr);
H5Aclose(wavAttrAid);
H5Sclose(wavAttrSid);
H5Tclose(wavAttrTid);
H5Tclose(doubleArrayTid);
H5Gclose(rootGid);
return (int)ret;
}
int hdf5io_read_waveform_attribute_in_file_header(struct hdf5io_waveform_file *wavFile,
struct waveform_attribute *wavAttr)
{
herr_t ret;
hid_t wavAttrTid, wavAttrAid, doubleArrayTid;
const hsize_t doubleArrayDims[1]={SCOPE_NCH};
const unsigned doubleArrayRank = 1;
doubleArrayTid = H5Tarray_create(H5T_NATIVE_DOUBLE, doubleArrayRank, doubleArrayDims);
wavAttrTid = H5Tcreate(H5T_COMPOUND, sizeof(struct waveform_attribute));
H5Tinsert(wavAttrTid, "wavAttr.dt", HOFFSET(struct waveform_attribute, dt), H5T_NATIVE_DOUBLE);
H5Tinsert(wavAttrTid, "wavAttr.t0", HOFFSET(struct waveform_attribute, t0), H5T_NATIVE_DOUBLE);
H5Tinsert(wavAttrTid, "wavAttr.ymult",
HOFFSET(struct waveform_attribute, ymult), doubleArrayTid);
H5Tinsert(wavAttrTid, "wavAttr.yoff",
HOFFSET(struct waveform_attribute, yoff), doubleArrayTid);
H5Tinsert(wavAttrTid, "wavAttr.yzero",
HOFFSET(struct waveform_attribute, yzero), doubleArrayTid);
wavAttrAid = H5Aopen_by_name(wavFile->waveFid, "/", "Waveform Attributes",
H5P_DEFAULT, H5P_DEFAULT);
ret = H5Aread(wavAttrAid, wavAttrTid, wavAttr);
H5Aclose(wavAttrAid);
H5Tclose(wavAttrTid);
H5Tclose(doubleArrayTid);
return (int)ret;
}
int hdf5io_write_event(struct hdf5io_waveform_file *wavFile,
struct hdf5io_waveform_event *wavEvent)
{
char buf[HDF5IO_NAME_BUF_SIZE];
herr_t ret;
hid_t eventGid, chSid, chTid, chDid, chPid;
hsize_t chDims[1], chChunkDims[1];
int ich;
snprintf(buf, HDF5IO_NAME_BUF_SIZE, "/Event%d", wavEvent->eventId);
eventGid = H5Gcreate(wavFile->waveFid, buf, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
for(ich=0; ich<wavEvent->nch; ich++) {
if((wavEvent->chMask >> ich) & 0x01) {
chDims[0] = wavEvent->waveSize;
chSid = H5Screate_simple(1, chDims, NULL);
chPid = H5Pcreate(H5P_DATASET_CREATE);
chChunkDims[0] = chDims[0];
H5Pset_chunk(chPid, 1, chChunkDims);
H5Pset_deflate(chPid, 6);
chTid = H5Tcopy(H5T_NATIVE_CHAR);
snprintf(buf, HDF5IO_NAME_BUF_SIZE, "Ch%d", ich);
chDid = H5Dcreate(eventGid, buf, chTid, chSid,
H5P_DEFAULT, chPid, H5P_DEFAULT);
ret = H5Dwrite(chDid, H5T_NATIVE_CHAR, H5S_ALL, H5S_ALL, H5P_DEFAULT,
wavEvent->wavBuf[ich]);
H5Dclose(chDid);
H5Tclose(chTid);
H5Pclose(chPid);
H5Sclose(chSid);
}
}
H5Gclose(eventGid);
return (int)ret;
}
int hdf5io_read_event(struct hdf5io_waveform_file *wavFile,
struct hdf5io_waveform_event *wavEvent)
{
char buf[HDF5IO_NAME_BUF_SIZE];
herr_t ret;
hid_t eventGid, chDid, chDspaceId;
hsize_t chDims[1];
int ich;
snprintf(buf, HDF5IO_NAME_BUF_SIZE, "/Event%d", wavEvent->eventId);
eventGid = H5Gopen(wavFile->waveFid, buf, H5P_DEFAULT);
for(ich=0; ich<wavEvent->nch; ich++) {
if((wavEvent->chMask >> ich) & 0x01) {
snprintf(buf, HDF5IO_NAME_BUF_SIZE, "Ch%d", ich);
chDid = H5Dopen(eventGid, buf, H5P_DEFAULT);
chDspaceId = H5Dget_space(chDid);
H5Sget_simple_extent_dims(chDspaceId, chDims, NULL);
wavEvent->waveSize = chDims[0];
H5Sclose(chDspaceId);
ret = H5Dread(chDid, H5T_NATIVE_CHAR, H5S_ALL, H5S_ALL, H5P_DEFAULT,
wavEvent->wavBuf[ich]);
H5Dclose(chDid);
}
}
H5Gclose(eventGid);
return (int)ret;
}
int hdf5io_get_number_of_event(struct hdf5io_waveform_file *wavFile)
{
herr_t ret;
hid_t rootGid;
H5G_info_t rootGinfo;
int nEvents;
rootGid = H5Gopen(wavFile->waveFid, "/", H5P_DEFAULT);
ret = H5Gget_info(rootGid, &rootGinfo);
nEvents = rootGinfo.nlinks;
H5Gclose(rootGid);
return nEvents;
}