-
Notifications
You must be signed in to change notification settings - Fork 16
/
main.go
116 lines (99 loc) · 2.05 KB
/
main.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
package main
import (
"fmt"
"github.com/sbinet/go-hdf5"
)
const (
fname string = "SDScompound.h5"
dsname string = "ArrayOfStructures"
mbr1 string = "A_name"
mbr2 string = "B_name"
mbr3 string = "C_name"
length uint = 10
rank int = 1
)
type s1Type struct {
a int
b float32
c float64
d [3]int
e string
}
type s2Type struct {
c float64
a int
}
func main() {
// initialize data
// s1 := make([]s1_t, LENGTH)
// for i:=0; i<LENGTH; i++ {
// s1[i] = s1_t{a:i, b:float32(i*i), c:1./(float64(i)+1)}
// }
// fmt.Printf(":: data: %v\n", s1)
s1 := [length]s1Type{}
for i := 0; i < int(length); i++ {
s1[i] = s1Type{
a: i,
b: float32(i * i),
c: 1. / (float64(i) + 1),
d: [...]int{i, i * 2, i * 3},
e: fmt.Sprintf("--%d--", i),
}
//s1[i].d = []float64{float64(i), float64(2*i), 3.*i}}
}
fmt.Printf(":: data: %v\n", s1)
// create data space
dims := []uint{length}
space, err := hdf5.CreateSimpleDataspace(dims, nil)
if err != nil {
panic(err)
}
// create the file
f, err := hdf5.CreateFile(fname, hdf5.F_ACC_TRUNC)
if err != nil {
panic(err)
}
defer f.Close()
fmt.Printf(":: file [%s] created (id=%d)\n", fname, f.Id())
// create the memory data type
dtype, err := hdf5.NewDatatypeFromValue(s1[0])
if err != nil {
panic("could not create a dtype")
}
// create the dataset
dset, err := f.CreateDataset(dsname, dtype, space)
if err != nil {
panic(err)
}
fmt.Printf(":: dset (id=%d)\n", dset.Id())
// write data to the dataset
fmt.Printf(":: dset.Write...\n")
err = dset.Write(&s1)
if err != nil {
panic(err)
}
fmt.Printf(":: dset.Write... [ok]\n")
// release resources
dset.Close()
f.Close()
// open the file and the dataset
f, err = hdf5.OpenFile(fname, hdf5.F_ACC_RDONLY)
if err != nil {
panic(err)
}
dset, err = f.OpenDataset(dsname)
if err != nil {
panic(err)
}
// read it back into a new slice
s2 := make([]s1Type, length)
err = dset.Read(&s2)
if err != nil {
panic(err)
}
// display the fields
fmt.Printf(":: data: %v\n", s2)
// release resources
dset.Close()
f.Close()
}