-
Notifications
You must be signed in to change notification settings - Fork 16
/
h5g.go
140 lines (117 loc) · 4.16 KB
/
h5g.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
package hdf5
// #include "hdf5.h"
// #include "hdf5_hl.h"
// #include <stdlib.h>
// #include <string.h>
import "C"
import (
"fmt"
"runtime"
"unsafe"
)
// CommonFG is for methods common to both File and Group
type CommonFG struct {
Location
}
// Group is an HDF5 container object. It can contain any Location.
type Group struct {
CommonFG
}
// CreateGroup creates a new empty group and links it to a location in the file.
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{Location{Identifier{hid}}}}
runtime.SetFinalizer(group, (*Group).finalizer)
return group, nil
}
// CreateDataset creates a new Dataset.
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.
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.
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.
func (g *Group) CreateAttributeWith(name string, dtype *Datatype, dspace *Dataspace, acpl *PropList) (*Attribute, error) {
return createAttribute(g.id, name, dtype, dspace, acpl)
}
func (g *Group) finalizer() {
if err := g.Close(); err != nil {
panic(fmt.Errorf("error closing group: %s", err))
}
}
// Close closes the Group.
func (g *Group) Close() error {
if g.id == 0 {
return nil
}
err := h5err(C.H5Gclose(g.id))
g.id = 0
return err
}
// OpenGroup opens an existing child group from this Group.
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{Location{Identifier{hid}}}}
runtime.SetFinalizer(group, (*Group).finalizer)
return group, nil
}
// OpenDataset opens a named Dataset.
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
}
// 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.
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.
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.
func (g *Group) OpenTable(name string) (*Table, error) {
return openTable(g.id, name)
}