forked from gonum/hdf5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
h5g_group.go
171 lines (143 loc) · 5.78 KB
/
h5g_group.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
// 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 "hdf5_hl.h"
// #include <stdlib.h>
// #include <string.h>
import "C"
import (
"fmt"
"unsafe"
)
// CommonFG is for methods common to both File and Group
type CommonFG struct {
Identifier
}
// Group is an HDF5 container object. It can contain any Location.
type Group struct {
CommonFG
}
// CreateGroup creates and returns a new empty group and links it to a location
// in the file. The returned group must be closed by the user when it is no
// longer needed.
func (g *CommonFG) CreateGroup(name string) (*Group, error) {
c_name := C.CString(name)
defer C.free(unsafe.Pointer(c_name))
link_flags := C.hid_t(C.H5P_DEFAULT)
grp_c_flags := C.hid_t(C.H5P_DEFAULT)
hid := C.H5Gcreate2(g.id, c_name, link_flags, grp_c_flags, P_DEFAULT.id)
if err := checkID(hid); err != nil {
return nil, err
}
group := &Group{CommonFG{Identifier{hid}}}
return group, nil
}
// CreateDataset creates a new Dataset. The returned dataset must be
// closed by the user when it is no longer needed.
func (g *CommonFG) CreateDataset(name string, dtype *Datatype, dspace *Dataspace) (*Dataset, error) {
return createDataset(g.id, name, dtype, dspace, P_DEFAULT)
}
// CreateDatasetWith creates a new Dataset with a user-defined PropList.
// The returned dataset must be closed by the user when it is no longer needed.
func (g *CommonFG) CreateDatasetWith(name string, dtype *Datatype, dspace *Dataspace, dcpl *PropList) (*Dataset, error) {
return createDataset(g.id, name, dtype, dspace, dcpl)
}
// CreateAttribute creates a new attribute at this location. The returned
// attribute must be closed by the user when it is no longer needed.
func (g *Group) CreateAttribute(name string, dtype *Datatype, dspace *Dataspace) (*Attribute, error) {
return createAttribute(g.id, name, dtype, dspace, P_DEFAULT)
}
// CreateAttributeWith creates a new attribute at this location with a user-defined
// PropList. The returned dataset must be closed by the user when it is no longer needed.
func (g *Group) CreateAttributeWith(name string, dtype *Datatype, dspace *Dataspace, acpl *PropList) (*Attribute, error) {
return createAttribute(g.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 (g *Group) OpenAttribute(name string) (*Attribute, error) {
return openAttribute(g.id, name)
}
// Close closes the Group.
func (g *Group) Close() error {
return g.closeWith(h5gclose)
}
func h5gclose(id C.hid_t) C.herr_t {
return C.H5Gclose(id)
}
// OpenGroup opens and returns an existing child group from this Group.
// The returned group must be closed by the user when it is no longer needed.
func (g *CommonFG) OpenGroup(name string) (*Group, error) {
c_name := C.CString(name)
defer C.free(unsafe.Pointer(c_name))
hid := C.H5Gopen2(g.id, c_name, P_DEFAULT.id)
if err := checkID(hid); err != nil {
return nil, err
}
group := &Group{CommonFG{Identifier{hid}}}
return group, nil
}
// OpenDataset opens and returns a named Dataset. The returned
// dataset must be closed by the user when it is no longer needed.
func (g *CommonFG) OpenDataset(name string) (*Dataset, error) {
c_name := C.CString(name)
defer C.free(unsafe.Pointer(c_name))
hid := C.H5Dopen2(g.id, c_name, P_DEFAULT.id)
if err := checkID(hid); err != nil {
return nil, err
}
return newDataset(hid, nil), nil
}
// OpenDatasetWith opens and returns a named Dataset with a user-defined PropList.
// The returned dataset must be closed by the user when it is no longer needed.
func (g *CommonFG) OpenDatasetWith(name string, dapl *PropList) (*Dataset, error) {
c_name := C.CString(name)
defer C.free(unsafe.Pointer(c_name))
hid := C.H5Dopen2(g.id, c_name, dapl.id)
if err := checkID(hid); err != nil {
return nil, err
}
return newDataset(hid, nil), nil
}
// NumObjects returns the number of objects in the Group.
func (g *CommonFG) NumObjects() (uint, error) {
var info C.H5G_info_t
err := h5err(C.H5Gget_info(g.id, &info))
return uint(info.nlinks), err
}
// ObjectNameByIndex returns the name of the object at idx.
func (g *CommonFG) ObjectNameByIndex(idx uint) (string, error) {
cidx := C.hsize_t(idx)
size := C.H5Lget_name_by_idx(g.id, cdot, C.H5_INDEX_NAME, C.H5_ITER_INC, cidx, nil, 0, C.H5P_DEFAULT)
if size < 0 {
return "", fmt.Errorf("could not get name")
}
name := make([]C.char, size+1)
size = C.H5Lget_name_by_idx(g.id, cdot, C.H5_INDEX_NAME, C.H5_ITER_INC, cidx, &name[0], C.size_t(size)+1, C.H5P_DEFAULT)
if size < 0 {
return "", fmt.Errorf("could not get name")
}
return C.GoString(&name[0]), nil
}
// CreateTable creates a packet table to store fixed-length packets.
// The returned table must be closed by the user when it is no longer needed.
func (g *Group) CreateTable(name string, dtype *Datatype, chunkSize, compression int) (*Table, error) {
return createTable(g.id, name, dtype, chunkSize, compression)
}
// CreateTableFrom creates a packet table to store fixed-length packets.
// The returned table must be closed by the user when it is no longer needed.
func (g *Group) CreateTableFrom(name string, dtype interface{}, chunkSize, compression int) (*Table, error) {
return createTableFrom(g.id, name, dtype, chunkSize, compression)
}
// OpenTable opens an existing packet table. The returned table must be
// closed by the user when it is no longer needed.
func (g *Group) OpenTable(name string) (*Table, error) {
return openTable(g.id, name)
}
// LinkExists returns whether a link with the specified name exists in the group.
func (g *CommonFG) LinkExists(name string) bool {
c_name := C.CString(name)
defer C.free(unsafe.Pointer(c_name))
return C.H5Lexists(g.id, c_name, 0) > 0
}