forked from sbinet/go-hdf5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
h5d_test.go
193 lines (166 loc) · 4.1 KB
/
h5d_test.go
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
package hdf5
import (
"os"
"reflect"
"testing"
)
func createDataset1(t *testing.T) error {
// create a file with a single 5x20 dataset
f, err := CreateFile(fname, F_ACC_TRUNC)
if err != nil {
t.Fatalf("CreateFile failed: %s", err)
return err
}
defer f.Close()
var data [100]uint16
for i := range data {
data[i] = uint16(i)
}
dims := []uint{20, 5}
dspace, err := CreateSimpleDataspace(dims, dims)
if err != nil {
t.Fatal(err)
return err
}
dset, err := f.CreateDataset("dset", T_NATIVE_USHORT, dspace)
if err != nil {
t.Fatal(err)
return err
}
err = dset.Write(&data[0])
if err != nil {
t.Fatal(err)
return err
}
return err
}
/**
* TestReadSubset based on the h5_subset.c sample with the HDF5 C library.
* Original copyright notice:
*
* HDF5 (Hierarchical Data Format 5) Software Library and Utilities
* Copyright 2006-2013 by The HDF Group.
*
* NCSA HDF5 (Hierarchical Data Format 5) Software Library and Utilities
* Copyright 1998-2006 by the Board of Trustees of the University of Illinois.
****
* Write some test data then read back a subset.
*/
func TestReadSubset(t *testing.T) {
DisplayErrors(true)
defer DisplayErrors(false)
defer os.Remove(fname)
err := createDataset1(t)
if err != nil {
return
}
// load a subset of the data
f, err := OpenFile(fname, F_ACC_RDONLY)
if err != nil {
t.Fatal(err)
}
defer f.Close()
dset, err := f.OpenDataset("dset")
if err != nil {
t.Fatal(err)
}
// get the filespace and select the subset
filespace := dset.Space()
offset, stride, count, block := [2]uint{5, 1}, [2]uint{1, 1}, [2]uint{5, 2}, [2]uint{1, 1}
err = filespace.SelectHyperslab(offset[:], stride[:], count[:], block[:])
if err != nil {
t.Fatal(err)
}
// create the memory space for the subset
dims, maxdims := [2]uint{2, 5}, [2]uint{2, 5}
if err != nil {
t.Fatal(err)
}
memspace, err := CreateSimpleDataspace(dims[:], maxdims[:])
if err != nil {
t.Fatal(err)
}
expected := [10]uint16{26, 27, 31, 32, 36, 37, 41, 42, 46, 47}
// test array
{
// create a buffer for the data
data := [10]uint16{}
// read the subset
err = dset.ReadSubset(&data, memspace, filespace)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(data, expected) {
t.Fatalf("ReadSubset-array error\ngot= %#v\nwant=%#v\n", data, expected)
}
}
// test slice
{
// create a buffer for the data
data := make([]uint16, 10)
// read the subset
err = dset.ReadSubset(&data, memspace, filespace)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(data, expected[:]) {
t.Fatalf("ReadSubset-slice error\ngot= %#v\nwant=%#v\n", data, expected[:])
}
}
}
func TestWriteSubset(t *testing.T) {
DisplayErrors(true)
defer DisplayErrors(false)
defer os.Remove(fname)
fdims := []uint{12, 4, 6}
fspace, err := CreateSimpleDataspace(fdims, nil)
if err != nil {
t.Fatal(err)
}
mdims := []uint{2, 6}
mspace, err := CreateSimpleDataspace(mdims, nil)
if err != nil {
t.Fatal(err)
}
f, err := CreateFile(fname, F_ACC_TRUNC)
if err != nil {
t.Fatalf("CreateFile failed: %s\n", err)
}
defer f.Close()
dset, err := f.CreateDataset("dset", T_NATIVE_USHORT, fspace)
if err != nil {
t.Fatal(err)
}
offset := []uint{6, 0, 0}
stride := []uint{3, 1, 1}
count := []uint{mdims[0], 1, mdims[1]}
if err = fspace.SelectHyperslab(offset, stride, count, nil); err != nil {
t.Fatal(err)
}
data := make([]uint16, mdims[0]*mdims[1])
if err = dset.WriteSubset(&data, mspace, fspace); err != nil {
t.Fatal(err)
}
}
func TestSelectHyperslab(t *testing.T) {
DisplayErrors(true)
defer DisplayErrors(false)
dims := []uint{12, 4}
dspace, err := CreateSimpleDataspace(dims, nil)
if err != nil {
t.Fatal(err)
}
offset, stride, count, block := []uint{1, 2}, []uint{2, 1}, []uint{4, 2}, []uint{1, 1}
if err = dspace.SelectHyperslab(offset, stride, count, block); err != nil {
t.Fatal(err)
}
if err = dspace.SelectHyperslab(offset, nil, count, block); err != nil {
t.Fatal(err)
}
if err = dspace.SelectHyperslab(offset, stride, count, nil); err != nil {
t.Fatal(err)
}
if err = dspace.SelectHyperslab(offset, nil, count, nil); err != nil {
t.Fatal(err)
}
}