-
Notifications
You must be signed in to change notification settings - Fork 16
/
h5pt.go
219 lines (187 loc) · 5.58 KB
/
h5pt.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
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
package hdf5
// #include "hdf5.h"
// #include "hdf5_hl.h"
// #include <stdlib.h>
// #include <string.h>
import "C"
import (
"fmt"
"reflect"
"runtime"
"unsafe"
)
// Table is an hdf5 packet-table.
type Table struct {
Location
}
func newPacketTable(id C.hid_t) *Table {
t := &Table{Location{Identifier{id}}}
runtime.SetFinalizer(t, (*Table).finalizer)
return t
}
func (t *Table) finalizer() {
if err := t.Close(); err != nil {
panic(fmt.Errorf("error closing packet table: %s", err))
}
}
// Close closes an open packet table.
func (t *Table) Close() error {
if t.id == 0 {
return nil
}
err := h5err(C.H5PTclose(t.id))
t.id = 0
return err
}
// IsValid returns whether or not an indentifier points to a packet table.
func (t *Table) IsValid() bool {
return C.H5PTis_valid(t.id) >= 0
}
func (t *Table) Id() int {
return int(t.id)
}
// ReadPackets reads a number of packets from a packet table.
func (t *Table) ReadPackets(start, nrecords int, data interface{}) error {
c_start := C.hsize_t(start)
c_nrecords := C.size_t(nrecords)
rv := reflect.Indirect(reflect.ValueOf(data))
rt := rv.Type()
c_data := unsafe.Pointer(nil)
switch rt.Kind() {
case reflect.Array:
if rv.Len() < nrecords {
panic(fmt.Errorf("not enough capacity in array (cap=%d)", rv.Len()))
}
c_data = unsafe.Pointer(rv.Index(0).UnsafeAddr())
case reflect.Slice:
if rv.Len() < nrecords {
panic(fmt.Errorf("not enough capacity in slice (cap=%d)", rv.Len()))
}
slice := (*reflect.SliceHeader)(unsafe.Pointer(rv.UnsafeAddr()))
c_data = unsafe.Pointer(slice.Data)
default:
panic(fmt.Errorf("unhandled kind (%s), need slice or array", rt.Kind()))
}
err := C.H5PTread_packets(t.id, c_start, c_nrecords, c_data)
return h5err(err)
}
// Append appends packets to the end of a packet table.
func (t *Table) Append(data interface{}) error {
rv := reflect.Indirect(reflect.ValueOf(data))
rt := rv.Type()
c_nrecords := C.size_t(0)
c_data := unsafe.Pointer(nil)
switch rt.Kind() {
case reflect.Array:
c_nrecords = C.size_t(rv.Len())
c_data = unsafe.Pointer(rv.UnsafeAddr())
case reflect.Slice:
c_nrecords = C.size_t(rv.Len())
slice := (*reflect.SliceHeader)(unsafe.Pointer(rv.UnsafeAddr()))
c_data = unsafe.Pointer(slice.Data)
case reflect.String:
c_nrecords = C.size_t(rv.Len())
str := (*reflect.StringHeader)(unsafe.Pointer(rv.UnsafeAddr()))
c_data = unsafe.Pointer(str.Data)
case reflect.Ptr:
c_nrecords = C.size_t(1)
c_data = unsafe.Pointer(rv.Elem().UnsafeAddr())
default:
c_nrecords = C.size_t(1)
c_data = unsafe.Pointer(rv.UnsafeAddr())
}
err := C.H5PTappend(t.id, c_nrecords, c_data)
return h5err(err)
}
// Next reads packets from a packet table starting at the current index into the value pointed at by data.
// i.e. data is a pointer to an array or a slice.
func (t *Table) Next(data interface{}) error {
rt := reflect.TypeOf(data)
if rt.Kind() != reflect.Ptr {
return fmt.Errorf("hdf5: invalid value type. got=%v, want pointer", rt.Kind())
}
rt = rt.Elem()
rv := reflect.Indirect(reflect.ValueOf(data))
n := C.size_t(0)
cdata := unsafe.Pointer(nil)
switch rt.Kind() {
case reflect.Array:
if rv.Cap() <= 0 {
panic(fmt.Errorf("not enough capacity in array (cap=%d)", rv.Cap()))
}
cdata = unsafe.Pointer(rv.UnsafeAddr())
n = C.size_t(rv.Cap())
case reflect.Slice:
if rv.Cap() <= 0 {
panic(fmt.Errorf("not enough capacity in slice (cap=%d)", rv.Cap()))
}
slice := (*reflect.SliceHeader)(unsafe.Pointer(rv.UnsafeAddr()))
cdata = unsafe.Pointer(slice.Data)
n = C.size_t(rv.Cap())
default:
panic(fmt.Errorf("unsupported kind (%s), need slice or array", rt.Kind()))
}
err := C.H5PTget_next(t.id, n, cdata)
return h5err(err)
}
// NumPackets returns the number of packets in a packet table.
func (t *Table) NumPackets() (int, error) {
c_nrecords := C.hsize_t(0)
err := C.H5PTget_num_packets(t.id, &c_nrecords)
return int(c_nrecords), h5err(err)
}
// CreateIndex resets a packet table's index to the first packet.
func (t *Table) CreateIndex() error {
err := C.H5PTcreate_index(t.id)
return h5err(err)
}
// SetIndex sets a packet table's index.
func (t *Table) SetIndex(index int) error {
c_idx := C.hsize_t(index)
err := C.H5PTset_index(t.id, c_idx)
return h5err(err)
}
// Type returns an identifier for a copy of the datatype for a dataset.
func (t *Table) Type() (*Datatype, error) {
hid := C.H5Dget_type(t.id)
if err := checkID(hid); err != nil {
return nil, err
}
return NewDatatype(hid), nil
}
func createTable(id C.hid_t, name string, dtype *Datatype, chunkSize, compression int) (*Table, error) {
c_name := C.CString(name)
defer C.free(unsafe.Pointer(c_name))
chunk := C.hsize_t(chunkSize)
compr := C.int(compression)
hid := C.H5PTcreate_fl(id, c_name, dtype.id, chunk, compr)
if err := checkID(hid); err != nil {
return nil, err
}
return newPacketTable(hid), nil
}
func createTableFrom(id C.hid_t, name string, dtype interface{}, chunkSize, compression int) (*Table, error) {
var err error
switch dt := dtype.(type) {
case reflect.Type:
if hdfDtype, err := NewDataTypeFromType(dt); err == nil {
return createTable(id, name, hdfDtype, chunkSize, compression)
}
case *Datatype:
return createTable(id, name, dt, chunkSize, compression)
default:
if hdfDtype, err := NewDataTypeFromType(reflect.TypeOf(dtype)); err == nil {
return createTable(id, name, hdfDtype, chunkSize, compression)
}
}
return nil, err
}
func openTable(id C.hid_t, name string) (*Table, error) {
c_name := C.CString(name)
defer C.free(unsafe.Pointer(c_name))
hid := C.H5PTopen(id, c_name)
if err := checkID(hid); err != nil {
return nil, err
}
return newPacketTable(hid), nil
}