forked from sbinet/go-hdf5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
h5d.go
183 lines (150 loc) · 4.33 KB
/
h5d.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
package hdf5
// #include "hdf5.h"
// #include <stdlib.h>
// #include <string.h>
import "C"
import (
"fmt"
"reflect"
"runtime"
"unsafe"
)
type Dataset struct {
Location
}
func newDataset(id C.hid_t) *Dataset {
d := &Dataset{Location{Identifier{id}}}
runtime.SetFinalizer(d, (*Dataset).finalizer)
return d
}
func createDataset(id C.hid_t, name string, dtype *Datatype, dspace *Dataspace, dcpl *PropList) (*Dataset, error) {
dtype, err := dtype.Copy() // For safety
if err != nil {
return nil, err
}
c_name := C.CString(name)
defer C.free(unsafe.Pointer(c_name))
hid := C.H5Dcreate2(id, c_name, dtype.id, dspace.id, P_DEFAULT.id, dcpl.id, P_DEFAULT.id)
if err := checkID(hid); err != nil {
return nil, err
}
return newDataset(hid), nil
}
func (s *Dataset) finalizer() {
if err := s.Close(); err != nil {
panic(fmt.Errorf("error closing dset: %s", err))
}
}
// Close releases and terminates access to a dataset.
func (s *Dataset) Close() error {
if s.id == 0 {
return nil
}
err := h5err(C.H5Dclose(s.id))
s.id = 0
return err
}
// Space returns an identifier for a copy of the dataspace for a dataset.
func (s *Dataset) Space() *Dataspace {
hid := C.H5Dget_space(s.id)
if int(hid) > 0 {
return newDataspace(hid)
}
return nil
}
// ReadSubset reads a subset of raw data from a dataset into a buffer.
func (s *Dataset) ReadSubset(data interface{}, memspace, filespace *Dataspace) error {
dtype, err := s.Datatype()
defer dtype.Close()
if err != nil {
return err
}
var addr unsafe.Pointer
v := reflect.Indirect(reflect.ValueOf(data))
switch v.Kind() {
case reflect.Array:
addr = unsafe.Pointer(v.UnsafeAddr())
case reflect.Slice:
slice := (*reflect.SliceHeader)(unsafe.Pointer(v.UnsafeAddr()))
addr = unsafe.Pointer(slice.Data)
case reflect.String:
str := (*reflect.StringHeader)(unsafe.Pointer(v.UnsafeAddr()))
addr = unsafe.Pointer(str.Data)
case reflect.Ptr:
addr = unsafe.Pointer(v.Pointer())
default:
addr = unsafe.Pointer(v.UnsafeAddr())
}
var filespace_id, memspace_id C.hid_t = 0, 0
if memspace != nil {
memspace_id = memspace.id
}
if filespace != nil {
filespace_id = filespace.id
}
rc := C.H5Dread(s.id, dtype.id, memspace_id, filespace_id, 0, addr)
err = h5err(rc)
return err
}
// Read reads raw data from a dataset into a buffer.
func (s *Dataset) Read(data interface{}) error {
return s.ReadSubset(data, nil, nil)
}
// WriteSubset writes a subset of raw data from a buffer to a dataset.
func (s *Dataset) WriteSubset(data interface{}, memspace, filespace *Dataspace) error {
dtype, err := s.Datatype()
defer dtype.Close()
if err != nil {
return err
}
addr := unsafe.Pointer(nil)
v := reflect.Indirect(reflect.ValueOf(data))
switch v.Kind() {
case reflect.Array:
addr = unsafe.Pointer(v.UnsafeAddr())
case reflect.Slice:
slice := (*reflect.SliceHeader)(unsafe.Pointer(v.UnsafeAddr()))
addr = unsafe.Pointer(slice.Data)
case reflect.String:
str := (*reflect.StringHeader)(unsafe.Pointer(v.UnsafeAddr()))
addr = unsafe.Pointer(str.Data)
case reflect.Ptr:
addr = unsafe.Pointer(v.Pointer())
default:
addr = unsafe.Pointer(v.UnsafeAddr())
}
var filespace_id, memspace_id C.hid_t = 0, 0
if memspace != nil {
memspace_id = memspace.id
}
if filespace != nil {
filespace_id = filespace.id
}
rc := C.H5Dwrite(s.id, dtype.id, memspace_id, filespace_id, 0, addr)
err = h5err(rc)
return err
}
// Write writes raw data from a buffer to a dataset.
func (s *Dataset) Write(data interface{}) error {
return s.WriteSubset(data, nil, nil)
}
// Creates a new attribute at this location.
func (s *Dataset) CreateAttribute(name string, dtype *Datatype, dspace *Dataspace) (*Attribute, error) {
return createAttribute(s.id, name, dtype, dspace, P_DEFAULT)
}
// Creates a new attribute at this location.
func (s *Dataset) CreateAttributeWith(name string, dtype *Datatype, dspace *Dataspace, acpl *PropList) (*Attribute, error) {
return createAttribute(s.id, name, dtype, dspace, acpl)
}
// Opens an existing attribute.
func (s *Dataset) OpenAttribute(name string) (*Attribute, error) {
return openAttribute(s.id, name)
}
// Datatype returns the HDF5 Datatype of the Dataset
func (s *Dataset) Datatype() (*Datatype, error) {
dtype_id := C.H5Dget_type(s.id)
if dtype_id < 0 {
return nil, fmt.Errorf("couldn't open Datatype from Dataset %q", s.Name())
}
return NewDatatype(dtype_id), nil
}