forked from sbinet/go-hdf5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
h5f.go
147 lines (124 loc) · 4.01 KB
/
h5f.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
package hdf5
// #include "hdf5.h"
// #include "hdf5_hl.h"
// #include <stdlib.h>
// #include <string.h>
import "C"
import (
"fmt"
"runtime"
"unsafe"
)
// File constants
const (
F_ACC_RDONLY int = 0x0000 // absence of rdwr => rd-only
F_ACC_RDWR int = 0x0001 // open for read and write
F_ACC_TRUNC int = 0x0002 // Truncate file, if it already exists, erasing all data previously stored in the file.
F_ACC_EXCL int = 0x0004 // Fail if file already exists.
F_ACC_DEBUG int = 0x0008 // print debug info
F_ACC_CREAT int = 0x0010 // create non-existing files
F_ACC_DEFAULT int = 0xffff // value passed to set_elink_acc_flags to cause flags to be taken from the parent file
)
// The difference between a single file and a set of mounted files.
type Scope C.H5F_scope_t
const (
F_SCOPE_LOCAL Scope = 0 // specified file handle only.
F_SCOPE_GLOBAL Scope = 1 // entire virtual file.
)
// a HDF5 file
type File struct {
CommonFG
}
func (f *File) finalizer() {
if err := f.Close(); err != nil {
panic(fmt.Errorf("error closing file: %s", err))
}
}
func newFile(id C.hid_t) *File {
f := &File{CommonFG{Location{Identifier{id}}}}
runtime.SetFinalizer(f, (*File).finalizer)
return f
}
// Creates an HDF5 file.
func CreateFile(name string, flags int) (*File, error) {
c_name := C.CString(name)
defer C.free(unsafe.Pointer(c_name))
// FIXME: file props
hid := C.H5Fcreate(c_name, C.uint(flags), P_DEFAULT.id, P_DEFAULT.id)
if err := checkID(hid); err != nil {
return nil, fmt.Errorf("error creating hdf5 file: %s", err)
}
return newFile(hid), nil
}
// Opens an existing HDF5 file.
func OpenFile(name string, flags int) (*File, error) {
c_name := C.CString(name)
defer C.free(unsafe.Pointer(c_name))
// FIXME: file props
hid := C.H5Fopen(c_name, C.uint(flags), P_DEFAULT.id)
if err := checkID(hid); err != nil {
return nil, fmt.Errorf("error opening hdf5 file: %s", err)
}
return newFile(hid), nil
}
// Returns a new identifier for a previously-opened HDF5 file.
func (f *File) ReOpen() (*File, error) {
hid := C.H5Freopen(f.id)
if err := checkID(hid); err != nil {
return nil, fmt.Errorf("error reopening hdf5 file: %s", err)
}
return newFile(hid), nil
}
// IsHDF5 Determines whether a file is in the HDF5 format.
func IsHDF5(name string) bool {
c_name := C.CString(name)
defer C.free(unsafe.Pointer(c_name))
return C.H5Fis_hdf5(c_name) > 0
}
// Terminates access to an HDF5 file.
func (f *File) Close() error {
if f.id == 0 {
return nil
}
err := h5err(C.H5Fclose(f.id))
f.id = 0
return err
}
// Flushes all buffers associated with a file to disk.
// herr_t H5Fflush(hid_t object_id, H5F_scope_t scope )
func (f *File) Flush(scope Scope) error {
return h5err(C.H5Fflush(f.id, C.H5F_scope_t(scope)))
}
// FIXME
// Retrieves name of file to which object belongs.
// ssize_t H5Fget_name(hid_t obj_id, char *name, size_t size )
func (f *File) FileName() string {
sz := int(C.H5Fget_name(f.id, nil, 0)) + 1
if sz < 0 {
return ""
}
buf := string(make([]byte, sz))
c_buf := C.CString(buf)
defer C.free(unsafe.Pointer(c_buf))
sz = int(C.H5Fget_name(f.id, c_buf, C.size_t(sz)))
if sz < 0 {
return ""
}
return C.GoString(c_buf)
}
var cdot = C.CString(".")
// Creates a packet table to store fixed-length packets.
// hid_t H5PTcreate_fl( hid_t loc_id, const char * dset_name, hid_t dtype_id, hsize_t chunk_size, int compression )
func (f *File) CreateTable(name string, dtype *Datatype, chunkSize, compression int) (*Table, error) {
return createTable(f.id, name, dtype, chunkSize, compression)
}
// Creates a packet table to store fixed-length packets.
// hid_t H5PTcreate_fl( hid_t loc_id, const char * dset_name, hid_t dtype_id, hsize_t chunk_size, int compression )
func (f *File) CreateTableFrom(name string, dtype interface{}, chunkSize, compression int) (*Table, error) {
return createTableFrom(f.id, name, dtype, chunkSize, compression)
}
// Opens an existing packet table.
// hid_t H5PTopen( hid_t loc_id, const char *dset_name )
func (f *File) OpenTable(name string) (*Table, error) {
return openTable(f.id, name)
}