forked from gonum/hdf5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
h5d_dataset.go
191 lines (155 loc) · 5.02 KB
/
h5d_dataset.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
// Copyright ©2017 The Gonum Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package hdf5
// #include "hdf5.h"
// #include <stdlib.h>
// #include <string.h>
import "C"
import (
"fmt"
"reflect"
"unsafe"
)
type Dataset struct {
Identifier
typ *Datatype
}
func newDataset(id C.hid_t, typ *Datatype) *Dataset {
return &Dataset{Identifier: Identifier{id}, typ: typ}
}
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, dtype), nil
}
// Close releases and terminates access to a dataset.
func (s *Dataset) Close() error {
return s.closeWith(h5dclose)
}
func h5dclose(id C.hid_t) C.herr_t {
return C.H5Dclose(id)
}
// 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. The returned attribute
// must be closed by the user when it is no longer needed.
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. The returned
// attribute must be closed by the user when it is no longer needed.
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. The returned attribute must be closed
// by the user when it is no longer needed.
func (s *Dataset) OpenAttribute(name string) (*Attribute, error) {
return openAttribute(s.id, name)
}
// Datatype returns the HDF5 Datatype of the Dataset. The returned
// datatype must be closed by the user when it is no longer needed.
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
}
// hasIllegalGoPointer returns whether the Dataset is known to have
// a Go pointer to Go pointer chain. If the Dataset was created by
// a call to OpenDataset without a read operation, it will be false,
// but will not be a valid reflection of the real situation.
func (s *Dataset) hasIllegalGoPointer() bool {
return s.typ.hasIllegalGoPointer()
}