-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread.go
165 lines (145 loc) · 3.26 KB
/
read.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
// legacy dump data format.
package dump
import (
"fmt"
"github.com/mumax/3/data"
"github.com/mumax/3/util"
"hash"
"hash/crc64"
"io"
"math"
"os"
"unsafe"
)
func Read(in io.Reader) (*data.Slice, data.Meta, error) {
r := newReader(in)
return r.readSlice()
}
func ReadFile(fname string) (*data.Slice, data.Meta, error) {
f, err := os.Open(fname)
if err != nil {
return nil, data.Meta{}, err
}
defer f.Close()
return Read(f)
}
func MustReadFile(fname string) (*data.Slice, data.Meta) {
s, t, err := ReadFile(fname)
util.FatalErr(err)
return s, t
}
// Reads successive data frames in dump format.
type reader struct {
in io.Reader
crc hash.Hash64
err error
}
func newReader(in io.Reader) *reader {
r := new(reader)
r.in = in
r.crc = crc64.New(table)
return r
}
func (r *reader) readSlice() (s *data.Slice, info data.Meta, err error) {
r.err = nil // clear previous error, if any
magic := r.readString()
if r.err != nil {
return nil, data.Meta{}, r.err
}
if magic != MAGIC {
r.err = fmt.Errorf("dump: bad magic number:%v", magic)
return nil, data.Meta{}, r.err
}
nComp := r.readInt()
size := [3]int{}
size[2] = r.readInt() // backwards compatible coordinates!
size[1] = r.readInt()
size[0] = r.readInt()
cell := [3]float64{}
cell[2] = r.readFloat64()
cell[1] = r.readFloat64()
cell[0] = r.readFloat64()
info.CellSize = cell
info.MeshUnit = r.readString()
info.Time = r.readFloat64()
_ = r.readString() // time unit
s = data.NewSlice(nComp, size)
info.Name = r.readString()
info.Unit = r.readString()
precission := r.readUint64()
util.AssertMsg(precission == 4, "only single precission supported")
if r.err != nil {
return
}
host := s.Tensors()
ncomp := s.NComp()
for c := 0; c < ncomp; c++ {
for iz := 0; iz < size[2]; iz++ {
for iy := 0; iy < size[1]; iy++ {
for ix := 0; ix < size[0]; ix++ {
host[c][iz][iy][ix] = r.readFloat32()
}
}
}
}
// Check CRC
var mycrc uint64 // checksum by this reader
if r.crc != nil {
mycrc = r.crc.Sum64()
}
storedcrc := r.readUint64() // checksum from data stream. 0 means not set
if r.err != nil {
return nil, data.Meta{}, r.err
}
if r.crc != nil {
r.crc.Reset() // reset for next frame
}
if r.crc != nil && storedcrc != 0 && mycrc != storedcrc {
r.err = fmt.Errorf("dump CRC error: expected %16x, got %16x", storedcrc, mycrc)
return nil, data.Meta{}, r.err
}
return s, info, nil
}
func (r *reader) readInt() int {
x := r.readUint64()
if uint64(int(x)) != x {
r.err = fmt.Errorf("value overflows int: %v", x)
}
return int(x)
}
// read until the buffer is full
func (r *reader) read(buf []byte) {
_, err := io.ReadFull(r.in, buf[:])
if err != nil {
r.err = err
}
if r.crc != nil {
r.crc.Write(buf)
}
}
// read a maximum 8-byte string
func (r *reader) readString() string {
var buf [8]byte
r.read(buf[:])
// trim trailing NULs.
i := 0
for i = 0; i < len(buf); i++ {
if buf[i] == 0 {
break
}
}
return string(buf[:i])
}
func (r *reader) readFloat64() float64 {
return math.Float64frombits(r.readUint64())
}
func (r *reader) readUint64() uint64 {
var buf [8]byte
r.read(buf[:])
return *((*uint64)(unsafe.Pointer(&buf[0])))
}
func (r *reader) readFloat32() float32 {
var buf [4]byte
r.read(buf[:])
return *((*float32)(unsafe.Pointer(&buf[0])))
}