forked from sbinet/go-hdf5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
h5s.go
173 lines (146 loc) · 4.27 KB
/
h5s.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
package hdf5
// #include "hdf5.h"
// #include <stdlib.h>
// #include <string.h>
import "C"
import (
"errors"
"fmt"
"runtime"
"unsafe"
)
type Dataspace struct {
Identifier
}
type SpaceClass C.H5S_class_t
const (
S_NO_CLASS SpaceClass = -1 // error
S_SCALAR SpaceClass = 0 // scalar variable
S_SIMPLE SpaceClass = 1 // simple data space
S_NULL SpaceClass = 2 // null data space
)
func newDataspace(id C.hid_t) *Dataspace {
ds := &Dataspace{Identifier{id}}
runtime.SetFinalizer(ds, (*Dataspace).finalizer)
return ds
}
// CreateDataspace creates a new dataspace of a specified type.
func CreateDataspace(class SpaceClass) (*Dataspace, error) {
hid := C.H5Screate(C.H5S_class_t(class))
if err := checkID(hid); err != nil {
return nil, err
}
ds := newDataspace(hid)
return ds, nil
}
func (s *Dataspace) finalizer() {
if err := s.Close(); err != nil {
panic(fmt.Errorf("error closing dspace: %s", err))
}
}
// Copy creates an exact copy of a dataspace.
func (s *Dataspace) Copy() (*Dataspace, error) {
hid := C.H5Scopy(s.id)
if err := checkID(hid); err != nil {
return nil, err
}
return newDataspace(hid), nil
}
// Close releases and terminates access to a dataspace.
func (s *Dataspace) Close() error {
if s.id == 0 {
return nil
}
err := h5err(C.H5Sclose(s.id))
s.id = 0
return err
}
func (s *Dataspace) Id() int {
return int(s.id)
}
// CreateSimpleDataspace creates a new simple dataspace and opens it for access.
func CreateSimpleDataspace(dims, maxDims []uint) (*Dataspace, error) {
var c_dims, c_maxdims *C.hsize_t
rank := C.int(0)
if dims != nil {
rank = C.int(len(dims))
c_dims = (*C.hsize_t)(unsafe.Pointer(&dims[0]))
}
if maxDims != nil {
rank = C.int(len(maxDims))
c_maxdims = (*C.hsize_t)(unsafe.Pointer(&maxDims[0]))
}
if len(dims) != len(maxDims) && (dims != nil && maxDims != nil) {
return nil, errors.New("lengths of dims and maxDims do not match")
}
hid := C.H5Screate_simple(rank, c_dims, c_maxdims)
if hid < 0 {
return nil, fmt.Errorf("failed to create dataspace")
}
return newDataspace(hid), nil
}
// IsSimple returns whether a dataspace is a simple dataspace.
func (s *Dataspace) IsSimple() bool {
return int(C.H5Sis_simple(s.id)) > 0
}
// SetOffset sets the offset of a simple dataspace.
func (s *Dataspace) SetOffset(offset []uint) error {
rank := len(offset)
if rank == 0 {
err := C.H5Soffset_simple(s.id, nil)
return h5err(err)
}
if rank != s.SimpleExtentNDims() {
err := errors.New("size of offset does not match extent")
return err
}
c_offset := (*C.hssize_t)(unsafe.Pointer(&offset[0]))
err := C.H5Soffset_simple(s.id, c_offset)
return h5err(err)
}
// SelectHyperslab creates a subset of the data space.
func (s *Dataspace) SelectHyperslab(offset, stride, count, block []uint) error {
rank := len(offset)
if rank == 0 {
err := C.H5Soffset_simple(s.id, nil)
return h5err(err)
}
if rank != s.SimpleExtentNDims() {
err := errors.New("size of offset does not match extent")
return err
}
c_offset := (*C.hsize_t)(unsafe.Pointer(&offset[0]))
c_count := (*C.hsize_t)(unsafe.Pointer(&count[0]))
var c_stride, c_block *C.hsize_t
if stride != nil {
c_stride = (*C.hsize_t)(unsafe.Pointer(&stride[0]))
}
if block != nil {
c_block = (*C.hsize_t)(unsafe.Pointer(&block[0]))
}
err := C.H5Sselect_hyperslab(s.id, C.H5S_SELECT_SET, c_offset, c_stride, c_count, c_block)
return h5err(err)
}
// SimpleExtentDims returns dataspace dimension size and maximum size.
func (s *Dataspace) SimpleExtentDims() (dims, maxdims []uint, err error) {
rank := s.SimpleExtentNDims()
dims = make([]uint, rank)
maxdims = make([]uint, rank)
c_dims := (*C.hsize_t)(unsafe.Pointer(&dims[0]))
c_maxdims := (*C.hsize_t)(unsafe.Pointer(&maxdims[0]))
rc := C.H5Sget_simple_extent_dims(s.id, c_dims, c_maxdims)
err = h5err(C.herr_t(rc))
return
}
// SimpleExtentNDims returns the dimensionality of a dataspace.
func (s *Dataspace) SimpleExtentNDims() int {
return int(C.H5Sget_simple_extent_ndims(s.id))
}
// SimpleExtentNPoints returns the number of elements in a dataspace.
func (s *Dataspace) SimpleExtentNPoints() int {
return int(C.H5Sget_simple_extent_npoints(s.id))
}
// SimpleExtentType returns the current class of a dataspace.
func (s *Dataspace) SimpleExtentType() SpaceClass {
return SpaceClass(C.H5Sget_simple_extent_type(s.id))
}